Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
22struct phy;
23
24enum 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
47enum 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 */
61union 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 */
80struct 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 */
131struct 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 */
148struct 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 */
167struct 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 */
183struct 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
204static inline void phy_set_drvdata(struct phy *phy, void *data)
205{
206 dev_set_drvdata(&phy->dev, data);
207}
208
209static 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)
215int phy_pm_runtime_get(struct phy *phy);
216int phy_pm_runtime_get_sync(struct phy *phy);
217int phy_pm_runtime_put(struct phy *phy);
218int phy_pm_runtime_put_sync(struct phy *phy);
219void phy_pm_runtime_allow(struct phy *phy);
220void phy_pm_runtime_forbid(struct phy *phy);
221int phy_init(struct phy *phy);
222int phy_exit(struct phy *phy);
223int phy_power_on(struct phy *phy);
224int phy_power_off(struct phy *phy);
225int 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)
228int phy_set_media(struct phy *phy, enum phy_media media);
229int phy_set_speed(struct phy *phy, int speed);
230int phy_configure(struct phy *phy, union phy_configure_opts *opts);
231int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
232 union phy_configure_opts *opts);
233
234static inline enum phy_mode phy_get_mode(struct phy *phy)
235{
236 return phy->attrs.mode;
237}
238int phy_reset(struct phy *phy);
239int phy_calibrate(struct phy *phy);
240static inline int phy_get_bus_width(struct phy *phy)
241{
242 return phy->attrs.bus_width;
243}
244static inline void phy_set_bus_width(struct phy *phy, int bus_width)
245{
246 phy->attrs.bus_width = bus_width;
247}
248struct phy *phy_get(struct device *dev, const char *string);
249struct phy *phy_optional_get(struct device *dev, const char *string);
250struct phy *devm_phy_get(struct device *dev, const char *string);
251struct phy *devm_phy_optional_get(struct device *dev, const char *string);
252struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
253 const char *con_id);
254struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
255 int index);
256void of_phy_put(struct phy *phy);
257void phy_put(struct device *dev, struct phy *phy);
258void devm_phy_put(struct device *dev, struct phy *phy);
259struct phy *of_phy_get(struct device_node *np, const char *con_id);
260struct phy *of_phy_simple_xlate(struct device *dev,
261 struct of_phandle_args *args);
262struct phy *phy_create(struct device *dev, struct device_node *node,
263 const struct phy_ops *ops);
264struct phy *devm_phy_create(struct device *dev, struct device_node *node,
265 const struct phy_ops *ops);
266void phy_destroy(struct phy *phy);
267void devm_phy_destroy(struct device *dev, struct phy *phy);
268struct 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));
272struct 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));
276void of_phy_provider_unregister(struct phy_provider *phy_provider);
277void devm_of_phy_provider_unregister(struct device *dev,
278 struct phy_provider *phy_provider);
279int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
280void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
281#else
282static inline int phy_pm_runtime_get(struct phy *phy)
283{
284 if (!phy)
285 return 0;
286 return -ENOSYS;
287}
288
289static inline int phy_pm_runtime_get_sync(struct phy *phy)
290{
291 if (!phy)
292 return 0;
293 return -ENOSYS;
294}
295
296static inline int phy_pm_runtime_put(struct phy *phy)
297{
298 if (!phy)
299 return 0;
300 return -ENOSYS;
301}
302
303static inline int phy_pm_runtime_put_sync(struct phy *phy)
304{
305 if (!phy)
306 return 0;
307 return -ENOSYS;
308}
309
310static inline void phy_pm_runtime_allow(struct phy *phy)
311{
312 return;
313}
314
315static inline void phy_pm_runtime_forbid(struct phy *phy)
316{
317 return;
318}
319
320static inline int phy_init(struct phy *phy)
321{
322 if (!phy)
323 return 0;
324 return -ENOSYS;
325}
326
327static inline int phy_exit(struct phy *phy)
328{
329 if (!phy)
330 return 0;
331 return -ENOSYS;
332}
333
334static inline int phy_power_on(struct phy *phy)
335{
336 if (!phy)
337 return 0;
338 return -ENOSYS;
339}
340
341static inline int phy_power_off(struct phy *phy)
342{
343 if (!phy)
344 return 0;
345 return -ENOSYS;
346}
347
348static 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
359static inline int phy_set_media(struct phy *phy, enum phy_media media)
360{
361 if (!phy)
362 return 0;
363 return -ENODEV;
364}
365
366static inline int phy_set_speed(struct phy *phy, int speed)
367{
368 if (!phy)
369 return 0;
370 return -ENODEV;
371}
372
373static inline enum phy_mode phy_get_mode(struct phy *phy)
374{
375 return PHY_MODE_INVALID;
376}
377
378static inline int phy_reset(struct phy *phy)
379{
380 if (!phy)
381 return 0;
382 return -ENOSYS;
383}
384
385static inline int phy_calibrate(struct phy *phy)
386{
387 if (!phy)
388 return 0;
389 return -ENOSYS;
390}
391
392static 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
401static 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
410static inline int phy_get_bus_width(struct phy *phy)
411{
412 return -ENOSYS;
413}
414
415static inline void phy_set_bus_width(struct phy *phy, int bus_width)
416{
417 return;
418}
419
420static inline struct phy *phy_get(struct device *dev, const char *string)
421{
422 return ERR_PTR(-ENOSYS);
423}
424
425static inline struct phy *phy_optional_get(struct device *dev,
426 const char *string)
427{
428 return ERR_PTR(-ENOSYS);
429}
430
431static inline struct phy *devm_phy_get(struct device *dev, const char *string)
432{
433 return ERR_PTR(-ENOSYS);
434}
435
436static inline struct phy *devm_phy_optional_get(struct device *dev,
437 const char *string)
438{
439 return NULL;
440}
441
442static 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
449static 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
456static inline void of_phy_put(struct phy *phy)
457{
458}
459
460static inline void phy_put(struct device *dev, struct phy *phy)
461{
462}
463
464static inline void devm_phy_put(struct device *dev, struct phy *phy)
465{
466}
467
468static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
469{
470 return ERR_PTR(-ENOSYS);
471}
472
473static inline struct phy *of_phy_simple_xlate(struct device *dev,
474 struct of_phandle_args *args)
475{
476 return ERR_PTR(-ENOSYS);
477}
478
479static 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
486static 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
493static inline void phy_destroy(struct phy *phy)
494{
495}
496
497static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
498{
499}
500
501static 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
509static 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
517static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
518{
519}
520
521static inline void devm_of_phy_provider_unregister(struct device *dev,
522 struct phy_provider *phy_provider)
523{
524}
525static inline int
526phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
527{
528 return 0;
529}
530static 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 */