1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * phy.h -- generic phy header file
4 *
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Author: Kishon Vijay Abraham I <kishon@ti.com>
8 */
9
10 #ifndef __DRIVERS_PHY_H
11 #define __DRIVERS_PHY_H
12
13 #include <linux/err.h>
14 #include <linux/of.h>
15 #include <linux/device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
18
19 #include <linux/phy/phy-dp.h>
20 #include <linux/phy/phy-hdmi.h>
21 #include <linux/phy/phy-lvds.h>
22 #include <linux/phy/phy-mipi-dphy.h>
23
24 struct phy;
25
26 enum phy_mode {
27 PHY_MODE_INVALID,
28 PHY_MODE_USB_HOST,
29 PHY_MODE_USB_HOST_LS,
30 PHY_MODE_USB_HOST_FS,
31 PHY_MODE_USB_HOST_HS,
32 PHY_MODE_USB_HOST_SS,
33 PHY_MODE_USB_DEVICE,
34 PHY_MODE_USB_DEVICE_LS,
35 PHY_MODE_USB_DEVICE_FS,
36 PHY_MODE_USB_DEVICE_HS,
37 PHY_MODE_USB_DEVICE_SS,
38 PHY_MODE_USB_OTG,
39 PHY_MODE_UFS_HS_A,
40 PHY_MODE_UFS_HS_B,
41 PHY_MODE_PCIE,
42 PHY_MODE_ETHERNET,
43 PHY_MODE_MIPI_DPHY,
44 PHY_MODE_SATA,
45 PHY_MODE_LVDS,
46 PHY_MODE_DP,
47 PHY_MODE_HDMI,
48 };
49
50 enum phy_media {
51 PHY_MEDIA_DEFAULT,
52 PHY_MEDIA_SR,
53 PHY_MEDIA_DAC,
54 };
55
56 /**
57 * union phy_configure_opts - Opaque generic phy configuration
58 *
59 * @mipi_dphy: Configuration set applicable for phys supporting
60 * the MIPI_DPHY phy mode.
61 * @dp: Configuration set applicable for phys supporting
62 * the DisplayPort protocol.
63 * @lvds: Configuration set applicable for phys supporting
64 * the LVDS phy mode.
65 * @hdmi: Configuration set applicable for phys supporting
66 * the HDMI phy mode.
67 */
68 union phy_configure_opts {
69 struct phy_configure_opts_mipi_dphy mipi_dphy;
70 struct phy_configure_opts_dp dp;
71 struct phy_configure_opts_lvds lvds;
72 struct phy_configure_opts_hdmi hdmi;
73 };
74
75 /**
76 * struct phy_ops - set of function pointers for performing phy operations
77 * @init: operation to be performed for initializing phy
78 * @exit: operation to be performed while exiting
79 * @power_on: powering on the phy
80 * @power_off: powering off the phy
81 * @set_mode: set the mode of the phy
82 * @set_media: set the media type of the phy (optional)
83 * @set_speed: set the speed of the phy (optional)
84 * @reset: resetting the phy
85 * @calibrate: calibrate the phy
86 * @release: ops to be performed while the consumer relinquishes the PHY
87 * @owner: the module owner containing the ops
88 */
89 struct phy_ops {
90 int (*init)(struct phy *phy);
91 int (*exit)(struct phy *phy);
92 int (*power_on)(struct phy *phy);
93 int (*power_off)(struct phy *phy);
94 int (*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
95 int (*set_media)(struct phy *phy, enum phy_media media);
96 int (*set_speed)(struct phy *phy, int speed);
97
98 /**
99 * @configure:
100 *
101 * Optional.
102 *
103 * Used to change the PHY parameters. phy_init() must have
104 * been called on the phy.
105 *
106 * Returns: 0 if successful, an negative error code otherwise
107 */
108 int (*configure)(struct phy *phy, union phy_configure_opts *opts);
109
110 /**
111 * @validate:
112 *
113 * Optional.
114 *
115 * Used to check that the current set of parameters can be
116 * handled by the phy. Implementations are free to tune the
117 * parameters passed as arguments if needed by some
118 * implementation detail or constraints. It must not change
119 * any actual configuration of the PHY, so calling it as many
120 * times as deemed fit by the consumer must have no side
121 * effect.
122 *
123 * Returns: 0 if the configuration can be applied, an negative
124 * error code otherwise
125 */
126 int (*validate)(struct phy *phy, enum phy_mode mode, int submode,
127 union phy_configure_opts *opts);
128 int (*reset)(struct phy *phy);
129 int (*calibrate)(struct phy *phy);
130
131 /* notify phy connect status change */
132 int (*connect)(struct phy *phy, int port);
133 int (*disconnect)(struct phy *phy, int port);
134
135 void (*release)(struct phy *phy);
136 struct module *owner;
137 };
138
139 /**
140 * struct phy_attrs - represents phy attributes
141 * @bus_width: Data path width implemented by PHY
142 * @max_link_rate: Maximum link rate supported by PHY (units to be decided by producer and consumer)
143 * @mode: PHY mode
144 */
145 struct phy_attrs {
146 u32 bus_width;
147 u32 max_link_rate;
148 enum phy_mode mode;
149 };
150
151 /**
152 * struct phy - represents the phy device
153 * @dev: phy device
154 * @id: id of the phy device
155 * @ops: function pointers for performing phy operations
156 * @mutex: mutex to protect phy_ops
157 * @lockdep_key: lockdep information for this mutex
158 * @init_count: used to protect when the PHY is used by multiple consumers
159 * @power_count: used to protect when the PHY is used by multiple consumers
160 * @attrs: used to specify PHY specific attributes
161 * @pwr: power regulator associated with the phy
162 * @debugfs: debugfs directory
163 */
164 struct phy {
165 struct device dev;
166 int id;
167 const struct phy_ops *ops;
168 struct mutex mutex;
169 struct lock_class_key lockdep_key;
170 int init_count;
171 int power_count;
172 struct phy_attrs attrs;
173 struct regulator *pwr;
174 struct dentry *debugfs;
175 };
176
177 /**
178 * struct phy_provider - represents the phy provider
179 * @dev: phy provider device
180 * @children: can be used to override the default (dev->of_node) child node
181 * @owner: the module owner having of_xlate
182 * @list: to maintain a linked list of PHY providers
183 * @of_xlate: function pointer to obtain phy instance from phy pointer
184 */
185 struct phy_provider {
186 struct device *dev;
187 struct device_node *children;
188 struct module *owner;
189 struct list_head list;
190 struct phy * (*of_xlate)(struct device *dev,
191 const struct of_phandle_args *args);
192 };
193
194 /**
195 * struct phy_lookup - PHY association in list of phys managed by the phy driver
196 * @node: list node
197 * @dev_id: the device of the association
198 * @con_id: connection ID string on device
199 * @phy: the phy of the association
200 */
201 struct phy_lookup {
202 struct list_head node;
203 const char *dev_id;
204 const char *con_id;
205 struct phy *phy;
206 };
207
208 #define to_phy(a) (container_of((a), struct phy, dev))
209
210 #define of_phy_provider_register(dev, xlate) \
211 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
212
213 #define devm_of_phy_provider_register(dev, xlate) \
214 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
215
216 #define of_phy_provider_register_full(dev, children, xlate) \
217 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
218
219 #define devm_of_phy_provider_register_full(dev, children, xlate) \
220 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
221
phy_set_drvdata(struct phy * phy,void * data)222 static inline void phy_set_drvdata(struct phy *phy, void *data)
223 {
224 dev_set_drvdata(&phy->dev, data);
225 }
226
phy_get_drvdata(struct phy * phy)227 static inline void *phy_get_drvdata(struct phy *phy)
228 {
229 return dev_get_drvdata(&phy->dev);
230 }
231
232 #if IS_ENABLED(CONFIG_GENERIC_PHY)
233 int phy_pm_runtime_get(struct phy *phy);
234 int phy_pm_runtime_get_sync(struct phy *phy);
235 int phy_pm_runtime_put(struct phy *phy);
236 int phy_pm_runtime_put_sync(struct phy *phy);
237 int phy_init(struct phy *phy);
238 int phy_exit(struct phy *phy);
239 int phy_power_on(struct phy *phy);
240 int phy_power_off(struct phy *phy);
241 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
242 #define phy_set_mode(phy, mode) \
243 phy_set_mode_ext(phy, mode, 0)
244 int phy_set_media(struct phy *phy, enum phy_media media);
245 int phy_set_speed(struct phy *phy, int speed);
246 int phy_configure(struct phy *phy, union phy_configure_opts *opts);
247 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
248 union phy_configure_opts *opts);
249
phy_get_mode(struct phy * phy)250 static inline enum phy_mode phy_get_mode(struct phy *phy)
251 {
252 return phy->attrs.mode;
253 }
254 int phy_reset(struct phy *phy);
255 int phy_calibrate(struct phy *phy);
256 int phy_notify_connect(struct phy *phy, int port);
257 int phy_notify_disconnect(struct phy *phy, int port);
phy_get_bus_width(struct phy * phy)258 static inline int phy_get_bus_width(struct phy *phy)
259 {
260 return phy->attrs.bus_width;
261 }
phy_set_bus_width(struct phy * phy,int bus_width)262 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
263 {
264 phy->attrs.bus_width = bus_width;
265 }
266 struct phy *phy_get(struct device *dev, const char *string);
267 struct phy *devm_phy_get(struct device *dev, const char *string);
268 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
269 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
270 const char *con_id);
271 struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np,
272 const char *con_id);
273 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
274 int index);
275 void of_phy_put(struct phy *phy);
276 void phy_put(struct device *dev, struct phy *phy);
277 void devm_phy_put(struct device *dev, struct phy *phy);
278 struct phy *of_phy_get(struct device_node *np, const char *con_id);
279 struct phy *of_phy_simple_xlate(struct device *dev,
280 const struct of_phandle_args *args);
281 struct phy *phy_create(struct device *dev, struct device_node *node,
282 const struct phy_ops *ops);
283 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
284 const struct phy_ops *ops);
285 void phy_destroy(struct phy *phy);
286 void devm_phy_destroy(struct device *dev, struct phy *phy);
287 struct phy_provider *__of_phy_provider_register(struct device *dev,
288 struct device_node *children, struct module *owner,
289 struct phy * (*of_xlate)(struct device *dev,
290 const struct of_phandle_args *args));
291 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
292 struct device_node *children, struct module *owner,
293 struct phy * (*of_xlate)(struct device *dev,
294 const struct of_phandle_args *args));
295 void of_phy_provider_unregister(struct phy_provider *phy_provider);
296 void devm_of_phy_provider_unregister(struct device *dev,
297 struct phy_provider *phy_provider);
298 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
299 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
300 #else
phy_pm_runtime_get(struct phy * phy)301 static inline int phy_pm_runtime_get(struct phy *phy)
302 {
303 if (!phy)
304 return 0;
305 return -ENOSYS;
306 }
307
phy_pm_runtime_get_sync(struct phy * phy)308 static inline int phy_pm_runtime_get_sync(struct phy *phy)
309 {
310 if (!phy)
311 return 0;
312 return -ENOSYS;
313 }
314
phy_pm_runtime_put(struct phy * phy)315 static inline int phy_pm_runtime_put(struct phy *phy)
316 {
317 if (!phy)
318 return 0;
319 return -ENOSYS;
320 }
321
phy_pm_runtime_put_sync(struct phy * phy)322 static inline int phy_pm_runtime_put_sync(struct phy *phy)
323 {
324 if (!phy)
325 return 0;
326 return -ENOSYS;
327 }
328
phy_init(struct phy * phy)329 static inline int phy_init(struct phy *phy)
330 {
331 if (!phy)
332 return 0;
333 return -ENOSYS;
334 }
335
phy_exit(struct phy * phy)336 static inline int phy_exit(struct phy *phy)
337 {
338 if (!phy)
339 return 0;
340 return -ENOSYS;
341 }
342
phy_power_on(struct phy * phy)343 static inline int phy_power_on(struct phy *phy)
344 {
345 if (!phy)
346 return 0;
347 return -ENOSYS;
348 }
349
phy_power_off(struct phy * phy)350 static inline int phy_power_off(struct phy *phy)
351 {
352 if (!phy)
353 return 0;
354 return -ENOSYS;
355 }
356
phy_set_mode_ext(struct phy * phy,enum phy_mode mode,int submode)357 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
358 int submode)
359 {
360 if (!phy)
361 return 0;
362 return -ENOSYS;
363 }
364
365 #define phy_set_mode(phy, mode) \
366 phy_set_mode_ext(phy, mode, 0)
367
phy_set_media(struct phy * phy,enum phy_media media)368 static inline int phy_set_media(struct phy *phy, enum phy_media media)
369 {
370 if (!phy)
371 return 0;
372 return -ENODEV;
373 }
374
phy_set_speed(struct phy * phy,int speed)375 static inline int phy_set_speed(struct phy *phy, int speed)
376 {
377 if (!phy)
378 return 0;
379 return -ENODEV;
380 }
381
phy_get_mode(struct phy * phy)382 static inline enum phy_mode phy_get_mode(struct phy *phy)
383 {
384 return PHY_MODE_INVALID;
385 }
386
phy_reset(struct phy * phy)387 static inline int phy_reset(struct phy *phy)
388 {
389 if (!phy)
390 return 0;
391 return -ENOSYS;
392 }
393
phy_calibrate(struct phy * phy)394 static inline int phy_calibrate(struct phy *phy)
395 {
396 if (!phy)
397 return 0;
398 return -ENOSYS;
399 }
400
phy_notify_connect(struct phy * phy,int index)401 static inline int phy_notify_connect(struct phy *phy, int index)
402 {
403 if (!phy)
404 return 0;
405 return -ENOSYS;
406 }
407
phy_notify_disconnect(struct phy * phy,int index)408 static inline int phy_notify_disconnect(struct phy *phy, int index)
409 {
410 if (!phy)
411 return 0;
412 return -ENOSYS;
413 }
414
phy_configure(struct phy * phy,union phy_configure_opts * opts)415 static inline int phy_configure(struct phy *phy,
416 union phy_configure_opts *opts)
417 {
418 if (!phy)
419 return 0;
420
421 return -ENOSYS;
422 }
423
phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)424 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
425 union phy_configure_opts *opts)
426 {
427 if (!phy)
428 return 0;
429
430 return -ENOSYS;
431 }
432
phy_get_bus_width(struct phy * phy)433 static inline int phy_get_bus_width(struct phy *phy)
434 {
435 return -ENOSYS;
436 }
437
phy_set_bus_width(struct phy * phy,int bus_width)438 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
439 {
440 return;
441 }
442
phy_get(struct device * dev,const char * string)443 static inline struct phy *phy_get(struct device *dev, const char *string)
444 {
445 return ERR_PTR(-ENOSYS);
446 }
447
devm_phy_get(struct device * dev,const char * string)448 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
449 {
450 return ERR_PTR(-ENOSYS);
451 }
452
devm_phy_optional_get(struct device * dev,const char * string)453 static inline struct phy *devm_phy_optional_get(struct device *dev,
454 const char *string)
455 {
456 return NULL;
457 }
458
devm_of_phy_get(struct device * dev,struct device_node * np,const char * con_id)459 static inline struct phy *devm_of_phy_get(struct device *dev,
460 struct device_node *np,
461 const char *con_id)
462 {
463 return ERR_PTR(-ENOSYS);
464 }
465
devm_of_phy_optional_get(struct device * dev,struct device_node * np,const char * con_id)466 static inline struct phy *devm_of_phy_optional_get(struct device *dev,
467 struct device_node *np,
468 const char *con_id)
469 {
470 return NULL;
471 }
472
devm_of_phy_get_by_index(struct device * dev,struct device_node * np,int index)473 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
474 struct device_node *np,
475 int index)
476 {
477 return ERR_PTR(-ENOSYS);
478 }
479
of_phy_put(struct phy * phy)480 static inline void of_phy_put(struct phy *phy)
481 {
482 }
483
phy_put(struct device * dev,struct phy * phy)484 static inline void phy_put(struct device *dev, struct phy *phy)
485 {
486 }
487
devm_phy_put(struct device * dev,struct phy * phy)488 static inline void devm_phy_put(struct device *dev, struct phy *phy)
489 {
490 }
491
of_phy_get(struct device_node * np,const char * con_id)492 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
493 {
494 return ERR_PTR(-ENOSYS);
495 }
496
of_phy_simple_xlate(struct device * dev,const struct of_phandle_args * args)497 static inline struct phy *of_phy_simple_xlate(struct device *dev,
498 const struct of_phandle_args *args)
499 {
500 return ERR_PTR(-ENOSYS);
501 }
502
phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)503 static inline struct phy *phy_create(struct device *dev,
504 struct device_node *node,
505 const struct phy_ops *ops)
506 {
507 return ERR_PTR(-ENOSYS);
508 }
509
devm_phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)510 static inline struct phy *devm_phy_create(struct device *dev,
511 struct device_node *node,
512 const struct phy_ops *ops)
513 {
514 return ERR_PTR(-ENOSYS);
515 }
516
phy_destroy(struct phy * phy)517 static inline void phy_destroy(struct phy *phy)
518 {
519 }
520
devm_phy_destroy(struct device * dev,struct phy * phy)521 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
522 {
523 }
524
__of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,const struct of_phandle_args * args))525 static inline struct phy_provider *__of_phy_provider_register(
526 struct device *dev, struct device_node *children, struct module *owner,
527 struct phy * (*of_xlate)(struct device *dev,
528 const struct of_phandle_args *args))
529 {
530 return ERR_PTR(-ENOSYS);
531 }
532
__devm_of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,const struct of_phandle_args * args))533 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
534 *dev, struct device_node *children, struct module *owner,
535 struct phy * (*of_xlate)(struct device *dev,
536 const struct of_phandle_args *args))
537 {
538 return ERR_PTR(-ENOSYS);
539 }
540
of_phy_provider_unregister(struct phy_provider * phy_provider)541 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
542 {
543 }
544
devm_of_phy_provider_unregister(struct device * dev,struct phy_provider * phy_provider)545 static inline void devm_of_phy_provider_unregister(struct device *dev,
546 struct phy_provider *phy_provider)
547 {
548 }
549 static inline int
phy_create_lookup(struct phy * phy,const char * con_id,const char * dev_id)550 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
551 {
552 return 0;
553 }
phy_remove_lookup(struct phy * phy,const char * con_id,const char * dev_id)554 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
555 const char *dev_id) { }
556 #endif
557
558 #endif /* __DRIVERS_PHY_H */
559