1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * phy.h -- generic phy header file
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
7 * Author: Kishon Vijay Abraham I <kishon@ti.com>
10 #ifndef __DRIVERS_PHY_H
11 #define __DRIVERS_PHY_H
13 #include <linux/err.h>
15 #include <linux/device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/regulator/consumer.h>
19 #include <linux/phy/phy-dp.h>
20 #include <linux/phy/phy-mipi-dphy.h>
32 PHY_MODE_USB_DEVICE_LS,
33 PHY_MODE_USB_DEVICE_FS,
34 PHY_MODE_USB_DEVICE_HS,
35 PHY_MODE_USB_DEVICE_SS,
54 * union phy_configure_opts - Opaque generic phy configuration
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.
61 union phy_configure_opts {
62 struct phy_configure_opts_mipi_dphy mipi_dphy;
63 struct phy_configure_opts_dp dp;
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
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);
94 * Used to change the PHY parameters. phy_init() must have
95 * been called on the phy.
97 * Returns: 0 if successful, an negative error code otherwise
99 int (*configure)(struct phy *phy, union phy_configure_opts *opts);
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
114 * Returns: 0 if the configuration can be applied, an negative
115 * error code otherwise
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;
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)
138 * struct phy - represents the 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
151 const struct phy_ops *ops;
155 struct phy_attrs attrs;
156 struct regulator *pwr;
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
167 struct phy_provider {
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);
177 * struct phy_lookup - PHY association in list of phys managed by the phy driver
179 * @dev_id: the device of the association
180 * @con_id: connection ID string on device
181 * @phy: the phy of the association
184 struct list_head node;
190 #define to_phy(a) (container_of((a), struct phy, dev))
192 #define of_phy_provider_register(dev, xlate) \
193 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
195 #define devm_of_phy_provider_register(dev, xlate) \
196 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
198 #define of_phy_provider_register_full(dev, children, xlate) \
199 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
201 #define devm_of_phy_provider_register_full(dev, children, xlate) \
202 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
204 static inline void phy_set_drvdata(struct phy *phy, void *data)
206 dev_set_drvdata(&phy->dev, data);
209 static inline void *phy_get_drvdata(struct phy *phy)
211 return dev_get_drvdata(&phy->dev);
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);
234 static inline enum phy_mode phy_get_mode(struct phy *phy)
236 return phy->attrs.mode;
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)
242 return phy->attrs.bus_width;
244 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
246 phy->attrs.bus_width = bus_width;
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,
254 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
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);
282 static inline int phy_pm_runtime_get(struct phy *phy)
289 static inline int phy_pm_runtime_get_sync(struct phy *phy)
296 static inline int phy_pm_runtime_put(struct phy *phy)
303 static inline int phy_pm_runtime_put_sync(struct phy *phy)
310 static inline void phy_pm_runtime_allow(struct phy *phy)
315 static inline void phy_pm_runtime_forbid(struct phy *phy)
320 static inline int phy_init(struct phy *phy)
327 static inline int phy_exit(struct phy *phy)
334 static inline int phy_power_on(struct phy *phy)
341 static inline int phy_power_off(struct phy *phy)
348 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
356 #define phy_set_mode(phy, mode) \
357 phy_set_mode_ext(phy, mode, 0)
359 static inline int phy_set_media(struct phy *phy, enum phy_media media)
366 static inline int phy_set_speed(struct phy *phy, int speed)
373 static inline enum phy_mode phy_get_mode(struct phy *phy)
375 return PHY_MODE_INVALID;
378 static inline int phy_reset(struct phy *phy)
385 static inline int phy_calibrate(struct phy *phy)
392 static inline int phy_configure(struct phy *phy,
393 union phy_configure_opts *opts)
401 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
402 union phy_configure_opts *opts)
410 static inline int phy_get_bus_width(struct phy *phy)
415 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
420 static inline struct phy *phy_get(struct device *dev, const char *string)
422 return ERR_PTR(-ENOSYS);
425 static inline struct phy *phy_optional_get(struct device *dev,
428 return ERR_PTR(-ENOSYS);
431 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
433 return ERR_PTR(-ENOSYS);
436 static inline struct phy *devm_phy_optional_get(struct device *dev,
442 static inline struct phy *devm_of_phy_get(struct device *dev,
443 struct device_node *np,
446 return ERR_PTR(-ENOSYS);
449 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
450 struct device_node *np,
453 return ERR_PTR(-ENOSYS);
456 static inline void of_phy_put(struct phy *phy)
460 static inline void phy_put(struct device *dev, struct phy *phy)
464 static inline void devm_phy_put(struct device *dev, struct phy *phy)
468 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
470 return ERR_PTR(-ENOSYS);
473 static inline struct phy *of_phy_simple_xlate(struct device *dev,
474 struct of_phandle_args *args)
476 return ERR_PTR(-ENOSYS);
479 static inline struct phy *phy_create(struct device *dev,
480 struct device_node *node,
481 const struct phy_ops *ops)
483 return ERR_PTR(-ENOSYS);
486 static inline struct phy *devm_phy_create(struct device *dev,
487 struct device_node *node,
488 const struct phy_ops *ops)
490 return ERR_PTR(-ENOSYS);
493 static inline void phy_destroy(struct phy *phy)
497 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
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))
506 return ERR_PTR(-ENOSYS);
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))
514 return ERR_PTR(-ENOSYS);
517 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
521 static inline void devm_of_phy_provider_unregister(struct device *dev,
522 struct phy_provider *phy_provider)
526 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
530 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
531 const char *dev_id) { }
534 #endif /* __DRIVERS_PHY_H */