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