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,
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
118 * @max_link_rate: Maximum link rate supported by PHY (in Mbps)
128 * struct phy - represents the 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
141 const struct phy_ops *ops;
145 struct phy_attrs attrs;
146 struct regulator *pwr;
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
157 struct phy_provider {
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);
167 * struct phy_lookup - PHY association in list of phys managed by the phy driver
169 * @dev_id: the device of the association
170 * @con_id: connection ID string on device
171 * @phy: the phy of the association
174 struct list_head node;
180 #define to_phy(a) (container_of((a), struct phy, dev))
182 #define of_phy_provider_register(dev, xlate) \
183 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
185 #define devm_of_phy_provider_register(dev, xlate) \
186 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
188 #define of_phy_provider_register_full(dev, children, xlate) \
189 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
191 #define devm_of_phy_provider_register_full(dev, children, xlate) \
192 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
194 static inline void phy_set_drvdata(struct phy *phy, void *data)
196 dev_set_drvdata(&phy->dev, data);
199 static inline void *phy_get_drvdata(struct phy *phy)
201 return dev_get_drvdata(&phy->dev);
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);
222 static inline enum phy_mode phy_get_mode(struct phy *phy)
224 return phy->attrs.mode;
226 int phy_reset(struct phy *phy);
227 int phy_calibrate(struct phy *phy);
228 static inline int phy_get_bus_width(struct phy *phy)
230 return phy->attrs.bus_width;
232 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
234 phy->attrs.bus_width = bus_width;
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,
242 struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
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);
270 static inline int phy_pm_runtime_get(struct phy *phy)
277 static inline int phy_pm_runtime_get_sync(struct phy *phy)
284 static inline int phy_pm_runtime_put(struct phy *phy)
291 static inline int phy_pm_runtime_put_sync(struct phy *phy)
298 static inline void phy_pm_runtime_allow(struct phy *phy)
303 static inline void phy_pm_runtime_forbid(struct phy *phy)
308 static inline int phy_init(struct phy *phy)
315 static inline int phy_exit(struct phy *phy)
322 static inline int phy_power_on(struct phy *phy)
329 static inline int phy_power_off(struct phy *phy)
336 static inline int phy_set_mode_ext(struct phy *phy, enum phy_mode mode,
344 #define phy_set_mode(phy, mode) \
345 phy_set_mode_ext(phy, mode, 0)
347 static inline enum phy_mode phy_get_mode(struct phy *phy)
349 return PHY_MODE_INVALID;
352 static inline int phy_reset(struct phy *phy)
359 static inline int phy_calibrate(struct phy *phy)
366 static inline int phy_configure(struct phy *phy,
367 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)
384 static inline int phy_get_bus_width(struct phy *phy)
389 static inline void phy_set_bus_width(struct phy *phy, int bus_width)
394 static inline struct phy *phy_get(struct device *dev, const char *string)
396 return ERR_PTR(-ENOSYS);
399 static inline struct phy *phy_optional_get(struct device *dev,
402 return ERR_PTR(-ENOSYS);
405 static inline struct phy *devm_phy_get(struct device *dev, const char *string)
407 return ERR_PTR(-ENOSYS);
410 static inline struct phy *devm_phy_optional_get(struct device *dev,
416 static inline struct phy *devm_of_phy_get(struct device *dev,
417 struct device_node *np,
420 return ERR_PTR(-ENOSYS);
423 static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
424 struct device_node *np,
427 return ERR_PTR(-ENOSYS);
430 static inline void of_phy_put(struct phy *phy)
434 static inline void phy_put(struct device *dev, struct phy *phy)
438 static inline void devm_phy_put(struct device *dev, struct phy *phy)
442 static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
444 return ERR_PTR(-ENOSYS);
447 static inline struct phy *of_phy_simple_xlate(struct device *dev,
448 struct of_phandle_args *args)
450 return ERR_PTR(-ENOSYS);
453 static inline struct phy *phy_create(struct device *dev,
454 struct device_node *node,
455 const struct phy_ops *ops)
457 return ERR_PTR(-ENOSYS);
460 static inline struct phy *devm_phy_create(struct device *dev,
461 struct device_node *node,
462 const struct phy_ops *ops)
464 return ERR_PTR(-ENOSYS);
467 static inline void phy_destroy(struct phy *phy)
471 static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
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))
480 return ERR_PTR(-ENOSYS);
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))
488 return ERR_PTR(-ENOSYS);
491 static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
495 static inline void devm_of_phy_provider_unregister(struct device *dev,
496 struct phy_provider *phy_provider)
500 phy_create_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) { }
508 #endif /* __DRIVERS_PHY_H */