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