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-mipi-dphy.h>
31 PHY_MODE_USB_DEVICE_LS,
32 PHY_MODE_USB_DEVICE_FS,
33 PHY_MODE_USB_DEVICE_HS,
34 PHY_MODE_USB_DEVICE_SS,
45 * union phy_configure_opts - Opaque generic phy configuration
47 * @mipi_dphy: Configuration set applicable for phys supporting
48 * the MIPI_DPHY phy mode.
50 union phy_configure_opts {
51 struct phy_configure_opts_mipi_dphy mipi_dphy;
55 * struct phy_ops - set of function pointers for performing phy operations
56 * @init: operation to be performed for initializing phy
57 * @exit: operation to be performed while exiting
58 * @power_on: powering on the phy
59 * @power_off: powering off the phy
60 * @set_mode: set the mode of the phy
61 * @reset: resetting the phy
62 * @calibrate: calibrate the phy
63 * @release: ops to be performed while the consumer relinquishes the PHY
64 * @owner: the module owner containing the ops
67 int (*init)(struct phy *phy);
68 int (*exit)(struct phy *phy);
69 int (*power_on)(struct phy *phy);
70 int (*power_off)(struct phy *phy);
71 int (*set_mode)(struct phy *phy, enum phy_mode mode, int submode);
78 * Used to change the PHY parameters. phy_init() must have
79 * been called on the phy.
81 * Returns: 0 if successful, an negative error code otherwise
83 int (*configure)(struct phy *phy, union phy_configure_opts *opts);
90 * Used to check that the current set of parameters can be
91 * handled by the phy. Implementations are free to tune the
92 * parameters passed as arguments if needed by some
93 * implementation detail or constraints. It must not change
94 * any actual configuration of the PHY, so calling it as many
95 * times as deemed fit by the consumer must have no side
98 * Returns: 0 if the configuration can be applied, an negative
99 * error code otherwise
101 int (*validate)(struct phy *phy, enum phy_mode mode, int submode,
102 union phy_configure_opts *opts);
103 int (*reset)(struct phy *phy);
104 int (*calibrate)(struct phy *phy);
105 void (*release)(struct phy *phy);
106 struct module *owner;
110 * struct phy_attrs - represents phy attributes
111 * @bus_width: Data path width implemented by PHY
120 * struct phy - represents the phy device
122 * @id: id of the phy device
123 * @ops: function pointers for performing phy operations
124 * @mutex: mutex to protect phy_ops
125 * @init_count: used to protect when the PHY is used by multiple consumers
126 * @power_count: used to protect when the PHY is used by multiple consumers
127 * @attrs: used to specify PHY specific attributes
128 * @pwr: power regulator associated with the phy
133 const struct phy_ops *ops;
137 struct phy_attrs attrs;
138 struct regulator *pwr;
142 * struct phy_provider - represents the phy provider
143 * @dev: phy provider device
144 * @children: can be used to override the default (dev->of_node) child node
145 * @owner: the module owner having of_xlate
146 * @list: to maintain a linked list of PHY providers
147 * @of_xlate: function pointer to obtain phy instance from phy pointer
149 struct phy_provider {
151 struct device_node *children;
152 struct module *owner;
153 struct list_head list;
154 struct phy * (*of_xlate)(struct device *dev,
155 struct of_phandle_args *args);
159 * struct phy_lookup - PHY association in list of phys managed by the phy driver
161 * @dev_id: the device of the association
162 * @con_id: connection ID string on device
163 * @phy: the phy of the association
166 struct list_head node;
172 #define to_phy(a) (container_of((a), struct phy, dev))
174 #define of_phy_provider_register(dev, xlate) \
175 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
177 #define devm_of_phy_provider_register(dev, xlate) \
178 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
180 #define of_phy_provider_register_full(dev, children, xlate) \
181 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
183 #define devm_of_phy_provider_register_full(dev, children, xlate) \
184 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
186 static inline void phy_set_drvdata(struct phy *phy, void *data)
188 dev_set_drvdata(&phy->dev, data);
191 static inline void *phy_get_drvdata(struct phy *phy)
193 return dev_get_drvdata(&phy->dev);
196 #if IS_ENABLED(CONFIG_GENERIC_PHY)
197 int phy_pm_runtime_get(struct phy *phy);
198 int phy_pm_runtime_get_sync(struct phy *phy);
199 int phy_pm_runtime_put(struct phy *phy);
200 int phy_pm_runtime_put_sync(struct phy *phy);
201 void phy_pm_runtime_allow(struct phy *phy);
202 void phy_pm_runtime_forbid(struct phy *phy);
203 int phy_init(struct phy *phy);
204 int phy_exit(struct phy *phy);
205 int phy_power_on(struct phy *phy);
206 int phy_power_off(struct phy *phy);
207 int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode);
208 #define phy_set_mode(phy, mode) \
209 phy_set_mode_ext(phy, mode, 0)
210 int phy_configure(struct phy *phy, union phy_configure_opts *opts);
211 int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
212 union phy_configure_opts *opts);
214 static inline enum phy_mode phy_get_mode(struct phy *phy)
216 return phy->attrs.mode;
218 int phy_reset(struct phy *phy);
219 int phy_calibrate(struct phy *phy);
220 static inline int phy_get_bus_width(struct phy *phy)
222 return phy->attrs.bus_width;
224 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
226 phy->attrs.bus_width = bus_width;
228 struct phy *phy_get(struct device *dev, const char *string);
229 struct phy *phy_optional_get(struct device *dev, const char *string);
230 struct phy *devm_phy_get(struct device *dev, const char *string);
231 struct phy *devm_phy_optional_get(struct device *dev, const char *string);
232 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
234 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
236 void phy_put(struct phy *phy);
237 void devm_phy_put(struct device *dev, struct phy *phy);
238 struct phy *of_phy_get(struct device_node *np, const char *con_id);
239 struct phy *of_phy_simple_xlate(struct device *dev,
240 struct of_phandle_args *args);
241 struct phy *phy_create(struct device *dev, struct device_node *node,
242 const struct phy_ops *ops);
243 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
244 const struct phy_ops *ops);
245 void phy_destroy(struct phy *phy);
246 void devm_phy_destroy(struct device *dev, struct phy *phy);
247 struct phy_provider *__of_phy_provider_register(struct device *dev,
248 struct device_node *children, struct module *owner,
249 struct phy * (*of_xlate)(struct device *dev,
250 struct of_phandle_args *args));
251 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
252 struct device_node *children, struct module *owner,
253 struct phy * (*of_xlate)(struct device *dev,
254 struct of_phandle_args *args));
255 void of_phy_provider_unregister(struct phy_provider *phy_provider);
256 void devm_of_phy_provider_unregister(struct device *dev,
257 struct phy_provider *phy_provider);
258 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
259 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
261 static inline int phy_pm_runtime_get(struct phy *phy)
268 static inline int phy_pm_runtime_get_sync(struct phy *phy)
275 static inline int phy_pm_runtime_put(struct phy *phy)
282 static inline int phy_pm_runtime_put_sync(struct phy *phy)
289 static inline void phy_pm_runtime_allow(struct phy *phy)
294 static inline void phy_pm_runtime_forbid(struct phy *phy)
299 static inline int phy_init(struct phy *phy)
306 static inline int phy_exit(struct phy *phy)
313 static inline int phy_power_on(struct phy *phy)
320 static inline int phy_power_off(struct phy *phy)
327 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
335 #define phy_set_mode(phy, mode) \
336 phy_set_mode_ext(phy, mode, 0)
338 static inline enum phy_mode phy_get_mode(struct phy *phy)
340 return PHY_MODE_INVALID;
343 static inline int phy_reset(struct phy *phy)
350 static inline int phy_calibrate(struct phy *phy)
357 static inline int phy_configure(struct phy *phy,
358 union phy_configure_opts *opts)
366 static inline int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
367 union phy_configure_opts *opts)
375 static inline int phy_get_bus_width(struct phy *phy)
380 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
385 static inline struct phy *phy_get(struct device *dev, const char *string)
387 return ERR_PTR(-ENOSYS);
390 static inline struct phy *phy_optional_get(struct device *dev,
393 return ERR_PTR(-ENOSYS);
396 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
398 return ERR_PTR(-ENOSYS);
401 static inline struct phy *devm_phy_optional_get(struct device *dev,
407 static inline struct phy *devm_of_phy_get(struct device *dev,
408 struct device_node *np,
411 return ERR_PTR(-ENOSYS);
414 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
415 struct device_node *np,
418 return ERR_PTR(-ENOSYS);
421 static inline void phy_put(struct phy *phy)
425 static inline void devm_phy_put(struct device *dev, struct phy *phy)
429 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
431 return ERR_PTR(-ENOSYS);
434 static inline struct phy *of_phy_simple_xlate(struct device *dev,
435 struct of_phandle_args *args)
437 return ERR_PTR(-ENOSYS);
440 static inline struct phy *phy_create(struct device *dev,
441 struct device_node *node,
442 const struct phy_ops *ops)
444 return ERR_PTR(-ENOSYS);
447 static inline struct phy *devm_phy_create(struct device *dev,
448 struct device_node *node,
449 const struct phy_ops *ops)
451 return ERR_PTR(-ENOSYS);
454 static inline void phy_destroy(struct phy *phy)
458 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
462 static inline struct phy_provider *__of_phy_provider_register(
463 struct device *dev, struct device_node *children, struct module *owner,
464 struct phy * (*of_xlate)(struct device *dev,
465 struct of_phandle_args *args))
467 return ERR_PTR(-ENOSYS);
470 static inline struct phy_provider *__devm_of_phy_provider_register(struct device
471 *dev, struct device_node *children, struct module *owner,
472 struct phy * (*of_xlate)(struct device *dev,
473 struct of_phandle_args *args))
475 return ERR_PTR(-ENOSYS);
478 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
482 static inline void devm_of_phy_provider_unregister(struct device *dev,
483 struct phy_provider *phy_provider)
487 phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
491 static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
492 const char *dev_id) { }
495 #endif /* __DRIVERS_PHY_H */