blob: 10888a717860cf4d8a0afd6341be25db39ea5f2c [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,
David Lechner300eb012016-05-09 18:39:59 -050032};
33
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053034/**
35 * struct phy_ops - set of function pointers for performing phy operations
36 * @init: operation to be performed for initializing phy
37 * @exit: operation to be performed while exiting
38 * @power_on: powering on the phy
39 * @power_off: powering off the phy
David Lechner300eb012016-05-09 18:39:59 -050040 * @set_mode: set the mode of the phy
Randy Licac18ec2016-09-10 02:59:37 +080041 * @reset: resetting the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053042 * @owner: the module owner containing the ops
43 */
44struct phy_ops {
45 int (*init)(struct phy *phy);
46 int (*exit)(struct phy *phy);
47 int (*power_on)(struct phy *phy);
48 int (*power_off)(struct phy *phy);
David Lechner300eb012016-05-09 18:39:59 -050049 int (*set_mode)(struct phy *phy, enum phy_mode mode);
Randy Licac18ec2016-09-10 02:59:37 +080050 int (*reset)(struct phy *phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053051 struct module *owner;
52};
53
54/**
Matt Porter8feed342013-12-19 09:23:02 -050055 * struct phy_attrs - represents phy attributes
56 * @bus_width: Data path width implemented by PHY
57 */
58struct phy_attrs {
59 u32 bus_width;
60};
61
62/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053063 * struct phy - represents the phy device
64 * @dev: phy device
65 * @id: id of the phy device
66 * @ops: function pointers for performing phy operations
67 * @init_data: list of PHY consumers (non-dt only)
68 * @mutex: mutex to protect phy_ops
69 * @init_count: used to protect when the PHY is used by multiple consumers
70 * @power_count: used to protect when the PHY is used by multiple consumers
Matt Porter8feed342013-12-19 09:23:02 -050071 * @phy_attrs: used to specify PHY specific attributes
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053072 */
73struct phy {
74 struct device dev;
75 int id;
76 const struct phy_ops *ops;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053077 struct mutex mutex;
78 int init_count;
79 int power_count;
Matt Porter8feed342013-12-19 09:23:02 -050080 struct phy_attrs attrs;
Roger Quadros3be88122014-07-04 12:55:45 +030081 struct regulator *pwr;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053082};
83
84/**
85 * struct phy_provider - represents the phy provider
86 * @dev: phy provider device
87 * @owner: the module owner having of_xlate
88 * @of_xlate: function pointer to obtain phy instance from phy pointer
89 * @list: to maintain a linked list of PHY providers
90 */
91struct phy_provider {
92 struct device *dev;
Thierry Reding1140f7c2016-04-05 17:17:34 +020093 struct device_node *children;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053094 struct module *owner;
95 struct list_head list;
96 struct phy * (*of_xlate)(struct device *dev,
97 struct of_phandle_args *args);
98};
99
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200100struct phy_lookup {
101 struct list_head node;
102 const char *dev_id;
103 const char *con_id;
104 struct phy *phy;
105};
106
Heikki Krogerusd4510572014-11-19 17:28:17 +0200107#define to_phy(a) (container_of((a), struct phy, dev))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530108
109#define of_phy_provider_register(dev, xlate) \
Thierry Reding1140f7c2016-04-05 17:17:34 +0200110 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530111
112#define devm_of_phy_provider_register(dev, xlate) \
Thierry Reding1140f7c2016-04-05 17:17:34 +0200113 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
114
115#define of_phy_provider_register_full(dev, children, xlate) \
116 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
117
118#define devm_of_phy_provider_register_full(dev, children, xlate) \
119 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530120
121static inline void phy_set_drvdata(struct phy *phy, void *data)
122{
123 dev_set_drvdata(&phy->dev, data);
124}
125
126static inline void *phy_get_drvdata(struct phy *phy)
127{
128 return dev_get_drvdata(&phy->dev);
129}
130
131#if IS_ENABLED(CONFIG_GENERIC_PHY)
132int phy_pm_runtime_get(struct phy *phy);
133int phy_pm_runtime_get_sync(struct phy *phy);
134int phy_pm_runtime_put(struct phy *phy);
135int phy_pm_runtime_put_sync(struct phy *phy);
136void phy_pm_runtime_allow(struct phy *phy);
137void phy_pm_runtime_forbid(struct phy *phy);
138int phy_init(struct phy *phy);
139int phy_exit(struct phy *phy);
140int phy_power_on(struct phy *phy);
141int phy_power_off(struct phy *phy);
David Lechner300eb012016-05-09 18:39:59 -0500142int phy_set_mode(struct phy *phy, enum phy_mode mode);
Randy Licac18ec2016-09-10 02:59:37 +0800143int phy_reset(struct phy *phy);
Matt Porter8feed342013-12-19 09:23:02 -0500144static inline int phy_get_bus_width(struct phy *phy)
145{
146 return phy->attrs.bus_width;
147}
148static inline void phy_set_bus_width(struct phy *phy, int bus_width)
149{
150 phy->attrs.bus_width = bus_width;
151}
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530152struct phy *phy_get(struct device *dev, const char *string);
Andrew Lunn788a4d52014-02-04 18:33:12 +0100153struct phy *phy_optional_get(struct device *dev, const char *string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530154struct phy *devm_phy_get(struct device *dev, const char *string);
Andrew Lunn788a4d52014-02-04 18:33:12 +0100155struct phy *devm_phy_optional_get(struct device *dev, const char *string);
Kamil Debskib5d682f2014-03-06 12:16:47 +0100156struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
157 const char *con_id);
Arun Ramamurthy6be109b2015-04-22 16:04:11 -0700158struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
159 int index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530160void phy_put(struct phy *phy);
161void devm_phy_put(struct device *dev, struct phy *phy);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100162struct phy *of_phy_get(struct device_node *np, const char *con_id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530163struct phy *of_phy_simple_xlate(struct device *dev,
164 struct of_phandle_args *args);
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530165struct phy *phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200166 const struct phy_ops *ops);
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530167struct phy *devm_phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200168 const struct phy_ops *ops);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530169void phy_destroy(struct phy *phy);
170void devm_phy_destroy(struct device *dev, struct phy *phy);
171struct phy_provider *__of_phy_provider_register(struct device *dev,
Thierry Reding1140f7c2016-04-05 17:17:34 +0200172 struct device_node *children, struct module *owner,
173 struct phy * (*of_xlate)(struct device *dev,
174 struct of_phandle_args *args));
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530175struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
Thierry Reding1140f7c2016-04-05 17:17:34 +0200176 struct device_node *children, struct module *owner,
177 struct phy * (*of_xlate)(struct device *dev,
178 struct of_phandle_args *args));
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530179void of_phy_provider_unregister(struct phy_provider *phy_provider);
180void devm_of_phy_provider_unregister(struct device *dev,
181 struct phy_provider *phy_provider);
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200182int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
183void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530184#else
185static inline int phy_pm_runtime_get(struct phy *phy)
186{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530187 if (!phy)
188 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530189 return -ENOSYS;
190}
191
192static inline int phy_pm_runtime_get_sync(struct phy *phy)
193{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530194 if (!phy)
195 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530196 return -ENOSYS;
197}
198
199static inline int phy_pm_runtime_put(struct phy *phy)
200{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530201 if (!phy)
202 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530203 return -ENOSYS;
204}
205
206static inline int phy_pm_runtime_put_sync(struct phy *phy)
207{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530208 if (!phy)
209 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530210 return -ENOSYS;
211}
212
213static inline void phy_pm_runtime_allow(struct phy *phy)
214{
215 return;
216}
217
218static inline void phy_pm_runtime_forbid(struct phy *phy)
219{
220 return;
221}
222
223static inline int phy_init(struct phy *phy)
224{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530225 if (!phy)
226 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530227 return -ENOSYS;
228}
229
230static inline int phy_exit(struct phy *phy)
231{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530232 if (!phy)
233 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530234 return -ENOSYS;
235}
236
237static inline int phy_power_on(struct phy *phy)
238{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530239 if (!phy)
240 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530241 return -ENOSYS;
242}
243
244static inline int phy_power_off(struct phy *phy)
245{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530246 if (!phy)
247 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530248 return -ENOSYS;
249}
250
David Lechner300eb012016-05-09 18:39:59 -0500251static inline int phy_set_mode(struct phy *phy, enum phy_mode mode)
252{
253 if (!phy)
254 return 0;
255 return -ENOSYS;
256}
257
Randy Li98430c72016-10-25 22:15:34 +0800258static inline int phy_reset(struct phy *phy)
259{
260 if (!phy)
261 return 0;
262 return -ENOSYS;
263}
264
Matt Porter8feed342013-12-19 09:23:02 -0500265static inline int phy_get_bus_width(struct phy *phy)
266{
267 return -ENOSYS;
268}
269
270static inline void phy_set_bus_width(struct phy *phy, int bus_width)
271{
272 return;
273}
274
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530275static inline struct phy *phy_get(struct device *dev, const char *string)
276{
277 return ERR_PTR(-ENOSYS);
278}
279
Andrew Lunn788a4d52014-02-04 18:33:12 +0100280static inline struct phy *phy_optional_get(struct device *dev,
281 const char *string)
282{
283 return ERR_PTR(-ENOSYS);
284}
285
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530286static inline struct phy *devm_phy_get(struct device *dev, const char *string)
287{
288 return ERR_PTR(-ENOSYS);
289}
290
Andrew Lunn788a4d52014-02-04 18:33:12 +0100291static inline struct phy *devm_phy_optional_get(struct device *dev,
292 const char *string)
293{
Maxime Ripard11a6e412017-09-04 14:53:13 +0200294 return NULL;
Andrew Lunn788a4d52014-02-04 18:33:12 +0100295}
296
Kamil Debskib5d682f2014-03-06 12:16:47 +0100297static inline struct phy *devm_of_phy_get(struct device *dev,
298 struct device_node *np,
299 const char *con_id)
300{
301 return ERR_PTR(-ENOSYS);
302}
303
Arun Ramamurthy6be109b2015-04-22 16:04:11 -0700304static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
305 struct device_node *np,
306 int index)
307{
308 return ERR_PTR(-ENOSYS);
309}
310
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530311static inline void phy_put(struct phy *phy)
312{
313}
314
315static inline void devm_phy_put(struct device *dev, struct phy *phy)
316{
317}
318
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100319static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
320{
321 return ERR_PTR(-ENOSYS);
322}
323
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530324static inline struct phy *of_phy_simple_xlate(struct device *dev,
325 struct of_phandle_args *args)
326{
327 return ERR_PTR(-ENOSYS);
328}
329
330static inline struct phy *phy_create(struct device *dev,
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530331 struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200332 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530333{
334 return ERR_PTR(-ENOSYS);
335}
336
337static inline struct phy *devm_phy_create(struct device *dev,
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530338 struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200339 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530340{
341 return ERR_PTR(-ENOSYS);
342}
343
344static inline void phy_destroy(struct phy *phy)
345{
346}
347
348static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
349{
350}
351
352static inline struct phy_provider *__of_phy_provider_register(
Thierry Reding1140f7c2016-04-05 17:17:34 +0200353 struct device *dev, struct device_node *children, struct module *owner,
354 struct phy * (*of_xlate)(struct device *dev,
355 struct of_phandle_args *args))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530356{
357 return ERR_PTR(-ENOSYS);
358}
359
360static inline struct phy_provider *__devm_of_phy_provider_register(struct device
Thierry Reding1140f7c2016-04-05 17:17:34 +0200361 *dev, struct device_node *children, struct module *owner,
362 struct phy * (*of_xlate)(struct device *dev,
363 struct of_phandle_args *args))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530364{
365 return ERR_PTR(-ENOSYS);
366}
367
368static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
369{
370}
371
372static inline void devm_of_phy_provider_unregister(struct device *dev,
373 struct phy_provider *phy_provider)
374{
375}
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200376static inline int
377phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
378{
379 return 0;
380}
381static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
382 const char *dev_id) { }
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530383#endif
384
385#endif /* __DRIVERS_PHY_H */