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