blob: 4f8423a948d5dbf1f0848f139f2d67bf2099bbfb [file] [log] [blame]
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +05301/*
2 * phy.h -- generic phy header file
3 *
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#ifndef __DRIVERS_PHY_H
15#define __DRIVERS_PHY_H
16
17#include <linux/err.h>
18#include <linux/of.h>
19#include <linux/device.h>
20#include <linux/pm_runtime.h>
Roger Quadros3be88122014-07-04 12:55:45 +030021#include <linux/regulator/consumer.h>
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053022
23struct phy;
24
David Lechner300eb012016-05-09 18:39:59 -050025enum phy_mode {
26 PHY_MODE_INVALID,
27 PHY_MODE_USB_HOST,
28 PHY_MODE_USB_DEVICE,
29 PHY_MODE_USB_OTG,
Antoine Tenart5c23f2d2017-08-30 10:29:12 +020030 PHY_MODE_SGMII,
31 PHY_MODE_10GKR,
Vivek Gautamfd3e4c92017-10-12 11:49:33 +053032 PHY_MODE_UFS_HS_A,
33 PHY_MODE_UFS_HS_B,
David Lechner300eb012016-05-09 18:39:59 -050034};
35
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053036/**
37 * struct phy_ops - set of function pointers for performing phy operations
38 * @init: operation to be performed for initializing phy
39 * @exit: operation to be performed while exiting
40 * @power_on: powering on the phy
41 * @power_off: powering off the phy
David Lechner300eb012016-05-09 18:39:59 -050042 * @set_mode: set the mode of the phy
Randy Licac18ec2016-09-10 02:59:37 +080043 * @reset: resetting the phy
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +020044 * @calibrate: calibrate the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053045 * @owner: the module owner containing the ops
46 */
47struct phy_ops {
48 int (*init)(struct phy *phy);
49 int (*exit)(struct phy *phy);
50 int (*power_on)(struct phy *phy);
51 int (*power_off)(struct phy *phy);
David Lechner300eb012016-05-09 18:39:59 -050052 int (*set_mode)(struct phy *phy, enum phy_mode mode);
Randy Licac18ec2016-09-10 02:59:37 +080053 int (*reset)(struct phy *phy);
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +020054 int (*calibrate)(struct phy *phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053055 struct module *owner;
56};
57
58/**
Matt Porter8feed342013-12-19 09:23:02 -050059 * struct phy_attrs - represents phy attributes
60 * @bus_width: Data path width implemented by PHY
61 */
62struct phy_attrs {
63 u32 bus_width;
64};
65
66/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053067 * struct phy - represents the phy device
68 * @dev: phy device
69 * @id: id of the phy device
70 * @ops: function pointers for performing phy operations
71 * @init_data: list of PHY consumers (non-dt only)
72 * @mutex: mutex to protect phy_ops
73 * @init_count: used to protect when the PHY is used by multiple consumers
74 * @power_count: used to protect when the PHY is used by multiple consumers
Matt Porter8feed342013-12-19 09:23:02 -050075 * @phy_attrs: used to specify PHY specific attributes
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053076 */
77struct phy {
78 struct device dev;
79 int id;
80 const struct phy_ops *ops;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053081 struct mutex mutex;
82 int init_count;
83 int power_count;
Matt Porter8feed342013-12-19 09:23:02 -050084 struct phy_attrs attrs;
Roger Quadros3be88122014-07-04 12:55:45 +030085 struct regulator *pwr;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053086};
87
88/**
89 * struct phy_provider - represents the phy provider
90 * @dev: phy provider device
91 * @owner: the module owner having of_xlate
92 * @of_xlate: function pointer to obtain phy instance from phy pointer
93 * @list: to maintain a linked list of PHY providers
94 */
95struct phy_provider {
96 struct device *dev;
Thierry Reding1140f7c2016-04-05 17:17:34 +020097 struct device_node *children;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053098 struct module *owner;
99 struct list_head list;
100 struct phy * (*of_xlate)(struct device *dev,
101 struct of_phandle_args *args);
102};
103
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200104struct phy_lookup {
105 struct list_head node;
106 const char *dev_id;
107 const char *con_id;
108 struct phy *phy;
109};
110
Heikki Krogerusd4510572014-11-19 17:28:17 +0200111#define to_phy(a) (container_of((a), struct phy, dev))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530112
113#define of_phy_provider_register(dev, xlate) \
Thierry Reding1140f7c2016-04-05 17:17:34 +0200114 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530115
116#define devm_of_phy_provider_register(dev, xlate) \
Thierry Reding1140f7c2016-04-05 17:17:34 +0200117 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
118
119#define of_phy_provider_register_full(dev, children, xlate) \
120 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
121
122#define devm_of_phy_provider_register_full(dev, children, xlate) \
123 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530124
125static inline void phy_set_drvdata(struct phy *phy, void *data)
126{
127 dev_set_drvdata(&phy->dev, data);
128}
129
130static inline void *phy_get_drvdata(struct phy *phy)
131{
132 return dev_get_drvdata(&phy->dev);
133}
134
135#if IS_ENABLED(CONFIG_GENERIC_PHY)
136int phy_pm_runtime_get(struct phy *phy);
137int phy_pm_runtime_get_sync(struct phy *phy);
138int phy_pm_runtime_put(struct phy *phy);
139int phy_pm_runtime_put_sync(struct phy *phy);
140void phy_pm_runtime_allow(struct phy *phy);
141void phy_pm_runtime_forbid(struct phy *phy);
142int phy_init(struct phy *phy);
143int phy_exit(struct phy *phy);
144int phy_power_on(struct phy *phy);
145int phy_power_off(struct phy *phy);
David Lechner300eb012016-05-09 18:39:59 -0500146int phy_set_mode(struct phy *phy, enum phy_mode mode);
Randy Licac18ec2016-09-10 02:59:37 +0800147int phy_reset(struct phy *phy);
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +0200148int phy_calibrate(struct phy *phy);
Matt Porter8feed342013-12-19 09:23:02 -0500149static inline int phy_get_bus_width(struct phy *phy)
150{
151 return phy->attrs.bus_width;
152}
153static inline void phy_set_bus_width(struct phy *phy, int bus_width)
154{
155 phy->attrs.bus_width = bus_width;
156}
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530157struct phy *phy_get(struct device *dev, const char *string);
Andrew Lunn788a4d52014-02-04 18:33:12 +0100158struct phy *phy_optional_get(struct device *dev, const char *string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530159struct phy *devm_phy_get(struct device *dev, const char *string);
Andrew Lunn788a4d52014-02-04 18:33:12 +0100160struct phy *devm_phy_optional_get(struct device *dev, const char *string);
Kamil Debskib5d682f2014-03-06 12:16:47 +0100161struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
162 const char *con_id);
Arun Ramamurthy6be109b2015-04-22 16:04:11 -0700163struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
164 int index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530165void phy_put(struct phy *phy);
166void devm_phy_put(struct device *dev, struct phy *phy);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100167struct phy *of_phy_get(struct device_node *np, const char *con_id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530168struct phy *of_phy_simple_xlate(struct device *dev,
169 struct of_phandle_args *args);
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530170struct phy *phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200171 const struct phy_ops *ops);
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530172struct phy *devm_phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200173 const struct phy_ops *ops);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530174void phy_destroy(struct phy *phy);
175void devm_phy_destroy(struct device *dev, struct phy *phy);
176struct phy_provider *__of_phy_provider_register(struct device *dev,
Thierry Reding1140f7c2016-04-05 17:17:34 +0200177 struct device_node *children, struct module *owner,
178 struct phy * (*of_xlate)(struct device *dev,
179 struct of_phandle_args *args));
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530180struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
Thierry Reding1140f7c2016-04-05 17:17:34 +0200181 struct device_node *children, struct module *owner,
182 struct phy * (*of_xlate)(struct device *dev,
183 struct of_phandle_args *args));
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530184void of_phy_provider_unregister(struct phy_provider *phy_provider);
185void devm_of_phy_provider_unregister(struct device *dev,
186 struct phy_provider *phy_provider);
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200187int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
188void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530189#else
190static inline int phy_pm_runtime_get(struct phy *phy)
191{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530192 if (!phy)
193 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530194 return -ENOSYS;
195}
196
197static inline int phy_pm_runtime_get_sync(struct phy *phy)
198{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530199 if (!phy)
200 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530201 return -ENOSYS;
202}
203
204static inline int phy_pm_runtime_put(struct phy *phy)
205{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530206 if (!phy)
207 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530208 return -ENOSYS;
209}
210
211static inline int phy_pm_runtime_put_sync(struct phy *phy)
212{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530213 if (!phy)
214 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530215 return -ENOSYS;
216}
217
218static inline void phy_pm_runtime_allow(struct phy *phy)
219{
220 return;
221}
222
223static inline void phy_pm_runtime_forbid(struct phy *phy)
224{
225 return;
226}
227
228static inline int phy_init(struct phy *phy)
229{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530230 if (!phy)
231 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530232 return -ENOSYS;
233}
234
235static inline int phy_exit(struct phy *phy)
236{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530237 if (!phy)
238 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530239 return -ENOSYS;
240}
241
242static inline int phy_power_on(struct phy *phy)
243{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530244 if (!phy)
245 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530246 return -ENOSYS;
247}
248
249static inline int phy_power_off(struct phy *phy)
250{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530251 if (!phy)
252 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530253 return -ENOSYS;
254}
255
David Lechner300eb012016-05-09 18:39:59 -0500256static inline int phy_set_mode(struct phy *phy, enum phy_mode mode)
257{
258 if (!phy)
259 return 0;
260 return -ENOSYS;
261}
262
Randy Li98430c72016-10-25 22:15:34 +0800263static inline int phy_reset(struct phy *phy)
264{
265 if (!phy)
266 return 0;
267 return -ENOSYS;
268}
269
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +0200270static inline int phy_calibrate(struct phy *phy)
271{
272 if (!phy)
273 return 0;
274 return -ENOSYS;
275}
276
Matt Porter8feed342013-12-19 09:23:02 -0500277static inline int phy_get_bus_width(struct phy *phy)
278{
279 return -ENOSYS;
280}
281
282static inline void phy_set_bus_width(struct phy *phy, int bus_width)
283{
284 return;
285}
286
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530287static inline struct phy *phy_get(struct device *dev, const char *string)
288{
289 return ERR_PTR(-ENOSYS);
290}
291
Andrew Lunn788a4d52014-02-04 18:33:12 +0100292static inline struct phy *phy_optional_get(struct device *dev,
293 const char *string)
294{
295 return ERR_PTR(-ENOSYS);
296}
297
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530298static inline struct phy *devm_phy_get(struct device *dev, const char *string)
299{
300 return ERR_PTR(-ENOSYS);
301}
302
Andrew Lunn788a4d52014-02-04 18:33:12 +0100303static inline struct phy *devm_phy_optional_get(struct device *dev,
304 const char *string)
305{
Maxime Ripard11a6e412017-09-04 14:53:13 +0200306 return NULL;
Andrew Lunn788a4d52014-02-04 18:33:12 +0100307}
308
Kamil Debskib5d682f2014-03-06 12:16:47 +0100309static inline struct phy *devm_of_phy_get(struct device *dev,
310 struct device_node *np,
311 const char *con_id)
312{
313 return ERR_PTR(-ENOSYS);
314}
315
Arun Ramamurthy6be109b2015-04-22 16:04:11 -0700316static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
317 struct device_node *np,
318 int index)
319{
320 return ERR_PTR(-ENOSYS);
321}
322
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530323static inline void phy_put(struct phy *phy)
324{
325}
326
327static inline void devm_phy_put(struct device *dev, struct phy *phy)
328{
329}
330
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100331static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
332{
333 return ERR_PTR(-ENOSYS);
334}
335
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530336static inline struct phy *of_phy_simple_xlate(struct device *dev,
337 struct of_phandle_args *args)
338{
339 return ERR_PTR(-ENOSYS);
340}
341
342static inline struct phy *phy_create(struct device *dev,
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530343 struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200344 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530345{
346 return ERR_PTR(-ENOSYS);
347}
348
349static inline struct phy *devm_phy_create(struct device *dev,
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530350 struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200351 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530352{
353 return ERR_PTR(-ENOSYS);
354}
355
356static inline void phy_destroy(struct phy *phy)
357{
358}
359
360static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
361{
362}
363
364static inline struct phy_provider *__of_phy_provider_register(
Thierry Reding1140f7c2016-04-05 17:17:34 +0200365 struct device *dev, struct device_node *children, struct module *owner,
366 struct phy * (*of_xlate)(struct device *dev,
367 struct of_phandle_args *args))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530368{
369 return ERR_PTR(-ENOSYS);
370}
371
372static inline struct phy_provider *__devm_of_phy_provider_register(struct device
Thierry Reding1140f7c2016-04-05 17:17:34 +0200373 *dev, struct device_node *children, struct module *owner,
374 struct phy * (*of_xlate)(struct device *dev,
375 struct of_phandle_args *args))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530376{
377 return ERR_PTR(-ENOSYS);
378}
379
380static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
381{
382}
383
384static inline void devm_of_phy_provider_unregister(struct device *dev,
385 struct phy_provider *phy_provider)
386{
387}
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200388static inline int
389phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
390{
391 return 0;
392}
393static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
394 const char *dev_id) { }
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530395#endif
396
397#endif /* __DRIVERS_PHY_H */