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
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,
48 * union phy_configure_opts - Opaque generic phy configuration
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.
55 union phy_configure_opts {
56 struct phy_configure_opts_mipi_dphy mipi_dphy;
57 struct phy_configure_opts_dp dp;
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
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);
84 * Used to change the PHY parameters. phy_init() must have
85 * been called on the phy.
87 * Returns: 0 if successful, an negative error code otherwise
89 int (*configure)(struct phy *phy, union phy_configure_opts *opts);
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
104 * Returns: 0 if the configuration can be applied, an negative
105 * error code otherwise
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;
116 * struct phy_attrs - represents phy attributes
117 * @bus_width: Data path width implemented by PHY
126 * struct phy - represents the phy device
128 * @id: id of the phy device
129 * @ops: function pointers for performing phy operations
130 * @mutex: mutex to protect phy_ops
131 * @init_count: used to protect when the PHY is used by multiple consumers
132 * @power_count: used to protect when the PHY is used by multiple consumers
133 * @attrs: used to specify PHY specific attributes
134 * @pwr: power regulator associated with the phy
139 const struct phy_ops *ops;
143 struct phy_attrs attrs;
144 struct regulator *pwr;
148 * struct phy_provider - represents the phy provider
149 * @dev: phy provider device
150 * @children: can be used to override the default (dev->of_node) child node
151 * @owner: the module owner having of_xlate
152 * @list: to maintain a linked list of PHY providers
153 * @of_xlate: function pointer to obtain phy instance from phy pointer
155 struct phy_provider {
157 struct device_node *children;
158 struct module *owner;
159 struct list_head list;
160 struct phy * (*of_xlate)(struct device *dev,
161 struct of_phandle_args *args);
165 * struct phy_lookup - PHY association in list of phys managed by the phy driver
167 * @dev_id: the device of the association
168 * @con_id: connection ID string on device
169 * @phy: the phy of the association
172 struct list_head node;
178 #define to_phy(a) (container_of((a), struct phy, dev))
180 #define of_phy_provider_register(dev, xlate) \
181 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
183 #define devm_of_phy_provider_register(dev, xlate) \
184 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
186 #define of_phy_provider_register_full(dev, children, xlate) \
187 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
189 #define devm_of_phy_provider_register_full(dev, children, xlate) \
190 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
192 static inline void phy_set_drvdata(struct phy *phy, void *data)
194 dev_set_drvdata(&phy->dev, data);
197 static inline void *phy_get_drvdata(struct phy *phy)
199 return dev_get_drvdata(&phy->dev);
202 #if IS_ENABLED(CONFIG_GENERIC_PHY)
203 int phy_pm_runtime_get(struct phy *phy);
204 int phy_pm_runtime_get_sync(struct phy *phy);
205 int phy_pm_runtime_put(struct phy *phy);
206 int phy_pm_runtime_put_sync(struct phy *phy);
207 void phy_pm_runtime_allow(struct phy *phy);
208 void phy_pm_runtime_forbid(struct phy *phy);
209 int phy_init(struct phy *phy);
210 int phy_exit(struct phy *phy);
211 int phy_power_on(struct phy *phy);
212 int phy_power_off(struct phy *phy);
213 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
214 #define phy_set_mode(phy, mode) \
215 phy_set_mode_ext(phy, mode, 0)
216 int phy_configure(struct phy *phy, union phy_configure_opts *opts);
217 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
218 union phy_configure_opts *opts);
220 static inline enum phy_mode phy_get_mode(struct phy *phy)
222 return phy->attrs.mode;
224 int phy_reset(struct phy *phy);
225 int phy_calibrate(struct phy *phy);
226 static inline int phy_get_bus_width(struct phy *phy)
228 return phy->attrs.bus_width;
230 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
232 phy->attrs.bus_width = bus_width;
234 struct phy *phy_get(struct device *dev, const char *string);
235 struct phy *phy_optional_get(struct device *dev, const char *string);
236 struct phy *devm_phy_get(struct device *dev, const char *string);
237 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
238 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
240 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
242 void of_phy_put(struct phy *phy);
243 void phy_put(struct device *dev, struct phy *phy);
244 void devm_phy_put(struct device *dev, struct phy *phy);
245 struct phy *of_phy_get(struct device_node *np, const char *con_id);
246 struct phy *of_phy_simple_xlate(struct device *dev,
247 struct of_phandle_args *args);
248 struct phy *phy_create(struct device *dev, struct device_node *node,
249 const struct phy_ops *ops);
250 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
251 const struct phy_ops *ops);
252 void phy_destroy(struct phy *phy);
253 void devm_phy_destroy(struct device *dev, struct phy *phy);
254 struct phy_provider *__of_phy_provider_register(struct device *dev,
255 struct device_node *children, struct module *owner,
256 struct phy * (*of_xlate)(struct device *dev,
257 struct of_phandle_args *args));
258 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
259 struct device_node *children, struct module *owner,
260 struct phy * (*of_xlate)(struct device *dev,
261 struct of_phandle_args *args));
262 void of_phy_provider_unregister(struct phy_provider *phy_provider);
263 void devm_of_phy_provider_unregister(struct device *dev,
264 struct phy_provider *phy_provider);
265 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
266 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
268 static inline int phy_pm_runtime_get(struct phy *phy)
275 static inline int phy_pm_runtime_get_sync(struct phy *phy)
282 static inline int phy_pm_runtime_put(struct phy *phy)
289 static inline int phy_pm_runtime_put_sync(struct phy *phy)
296 static inline void phy_pm_runtime_allow(struct phy *phy)
301 static inline void phy_pm_runtime_forbid(struct phy *phy)
306 static inline int phy_init(struct phy *phy)
313 static inline int phy_exit(struct phy *phy)
320 static inline int phy_power_on(struct phy *phy)
327 static inline int phy_power_off(struct phy *phy)
334 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
342 #define phy_set_mode(phy, mode) \
343 phy_set_mode_ext(phy, mode, 0)
345 static inline enum phy_mode phy_get_mode(struct phy *phy)
347 return PHY_MODE_INVALID;
350 static inline int phy_reset(struct phy *phy)
357 static inline int phy_calibrate(struct phy *phy)
364 static inline int phy_configure(struct phy *phy,
365 union phy_configure_opts *opts)
373 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
374 union phy_configure_opts *opts)
382 static inline int phy_get_bus_width(struct phy *phy)
387 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
392 static inline struct phy *phy_get(struct device *dev, const char *string)
394 return ERR_PTR(-ENOSYS);
397 static inline struct phy *phy_optional_get(struct device *dev,
400 return ERR_PTR(-ENOSYS);
403 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
405 return ERR_PTR(-ENOSYS);
408 static inline struct phy *devm_phy_optional_get(struct device *dev,
414 static inline struct phy *devm_of_phy_get(struct device *dev,
415 struct device_node *np,
418 return ERR_PTR(-ENOSYS);
421 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
422 struct device_node *np,
425 return ERR_PTR(-ENOSYS);
428 static inline void of_phy_put(struct phy *phy)
432 static inline void phy_put(struct device *dev, struct phy *phy)
436 static inline void devm_phy_put(struct device *dev, struct phy *phy)
440 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
442 return ERR_PTR(-ENOSYS);
445 static inline struct phy *of_phy_simple_xlate(struct device *dev,
446 struct of_phandle_args *args)
448 return ERR_PTR(-ENOSYS);
451 static inline struct phy *phy_create(struct device *dev,
452 struct device_node *node,
453 const struct phy_ops *ops)
455 return ERR_PTR(-ENOSYS);
458 static inline struct phy *devm_phy_create(struct device *dev,
459 struct device_node *node,
460 const struct phy_ops *ops)
462 return ERR_PTR(-ENOSYS);
465 static inline void phy_destroy(struct phy *phy)
469 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
473 static inline struct phy_provider *__of_phy_provider_register(
474 struct device *dev, struct device_node *children, struct module *owner,
475 struct phy * (*of_xlate)(struct device *dev,
476 struct of_phandle_args *args))
478 return ERR_PTR(-ENOSYS);
481 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
482 *dev, struct device_node *children, struct module *owner,
483 struct phy * (*of_xlate)(struct device *dev,
484 struct of_phandle_args *args))
486 return ERR_PTR(-ENOSYS);
489 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
493 static inline void devm_of_phy_provider_unregister(struct device *dev,
494 struct phy_provider *phy_provider)
498 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
502 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
503 const char *dev_id) { }
506 #endif /* __DRIVERS_PHY_H */