blob: 485469e6fa7f2a98406302f60de4a2c19901125e [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,
Manu Gautam3b3cd242018-01-16 16:27:09 +053028 PHY_MODE_USB_HOST_LS,
29 PHY_MODE_USB_HOST_FS,
30 PHY_MODE_USB_HOST_HS,
31 PHY_MODE_USB_HOST_SS,
David Lechner300eb012016-05-09 18:39:59 -050032 PHY_MODE_USB_DEVICE,
Manu Gautam3b3cd242018-01-16 16:27:09 +053033 PHY_MODE_USB_DEVICE_LS,
34 PHY_MODE_USB_DEVICE_FS,
35 PHY_MODE_USB_DEVICE_HS,
36 PHY_MODE_USB_DEVICE_SS,
David Lechner300eb012016-05-09 18:39:59 -050037 PHY_MODE_USB_OTG,
Antoine Tenart5c23f2d2017-08-30 10:29:12 +020038 PHY_MODE_SGMII,
39 PHY_MODE_10GKR,
Vivek Gautamfd3e4c92017-10-12 11:49:33 +053040 PHY_MODE_UFS_HS_A,
41 PHY_MODE_UFS_HS_B,
David Lechner300eb012016-05-09 18:39:59 -050042};
43
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053044/**
45 * struct phy_ops - set of function pointers for performing phy operations
46 * @init: operation to be performed for initializing phy
47 * @exit: operation to be performed while exiting
48 * @power_on: powering on the phy
49 * @power_off: powering off the phy
David Lechner300eb012016-05-09 18:39:59 -050050 * @set_mode: set the mode of the phy
Randy Licac18ec2016-09-10 02:59:37 +080051 * @reset: resetting the phy
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +020052 * @calibrate: calibrate the phy
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053053 * @owner: the module owner containing the ops
54 */
55struct phy_ops {
56 int (*init)(struct phy *phy);
57 int (*exit)(struct phy *phy);
58 int (*power_on)(struct phy *phy);
59 int (*power_off)(struct phy *phy);
David Lechner300eb012016-05-09 18:39:59 -050060 int (*set_mode)(struct phy *phy, enum phy_mode mode);
Randy Licac18ec2016-09-10 02:59:37 +080061 int (*reset)(struct phy *phy);
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +020062 int (*calibrate)(struct phy *phy);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053063 struct module *owner;
64};
65
66/**
Matt Porter8feed342013-12-19 09:23:02 -050067 * struct phy_attrs - represents phy attributes
68 * @bus_width: Data path width implemented by PHY
69 */
70struct phy_attrs {
71 u32 bus_width;
Manu Gautam3b3cd242018-01-16 16:27:09 +053072 enum phy_mode mode;
Matt Porter8feed342013-12-19 09:23:02 -050073};
74
75/**
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053076 * struct phy - represents the phy device
77 * @dev: phy device
78 * @id: id of the phy device
79 * @ops: function pointers for performing phy operations
80 * @init_data: list of PHY consumers (non-dt only)
81 * @mutex: mutex to protect phy_ops
82 * @init_count: used to protect when the PHY is used by multiple consumers
83 * @power_count: used to protect when the PHY is used by multiple consumers
Matt Porter8feed342013-12-19 09:23:02 -050084 * @phy_attrs: used to specify PHY specific attributes
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053085 */
86struct phy {
87 struct device dev;
88 int id;
89 const struct phy_ops *ops;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053090 struct mutex mutex;
91 int init_count;
92 int power_count;
Matt Porter8feed342013-12-19 09:23:02 -050093 struct phy_attrs attrs;
Roger Quadros3be88122014-07-04 12:55:45 +030094 struct regulator *pwr;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +053095};
96
97/**
98 * struct phy_provider - represents the phy provider
99 * @dev: phy provider device
100 * @owner: the module owner having of_xlate
101 * @of_xlate: function pointer to obtain phy instance from phy pointer
102 * @list: to maintain a linked list of PHY providers
103 */
104struct phy_provider {
105 struct device *dev;
Thierry Reding1140f7c2016-04-05 17:17:34 +0200106 struct device_node *children;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530107 struct module *owner;
108 struct list_head list;
109 struct phy * (*of_xlate)(struct device *dev,
110 struct of_phandle_args *args);
111};
112
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200113struct phy_lookup {
114 struct list_head node;
115 const char *dev_id;
116 const char *con_id;
117 struct phy *phy;
118};
119
Heikki Krogerusd4510572014-11-19 17:28:17 +0200120#define to_phy(a) (container_of((a), struct phy, dev))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530121
122#define of_phy_provider_register(dev, xlate) \
Thierry Reding1140f7c2016-04-05 17:17:34 +0200123 __of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530124
125#define devm_of_phy_provider_register(dev, xlate) \
Thierry Reding1140f7c2016-04-05 17:17:34 +0200126 __devm_of_phy_provider_register((dev), NULL, THIS_MODULE, (xlate))
127
128#define of_phy_provider_register_full(dev, children, xlate) \
129 __of_phy_provider_register(dev, children, THIS_MODULE, xlate)
130
131#define devm_of_phy_provider_register_full(dev, children, xlate) \
132 __devm_of_phy_provider_register(dev, children, THIS_MODULE, xlate)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530133
134static inline void phy_set_drvdata(struct phy *phy, void *data)
135{
136 dev_set_drvdata(&phy->dev, data);
137}
138
139static inline void *phy_get_drvdata(struct phy *phy)
140{
141 return dev_get_drvdata(&phy->dev);
142}
143
144#if IS_ENABLED(CONFIG_GENERIC_PHY)
145int phy_pm_runtime_get(struct phy *phy);
146int phy_pm_runtime_get_sync(struct phy *phy);
147int phy_pm_runtime_put(struct phy *phy);
148int phy_pm_runtime_put_sync(struct phy *phy);
149void phy_pm_runtime_allow(struct phy *phy);
150void phy_pm_runtime_forbid(struct phy *phy);
151int phy_init(struct phy *phy);
152int phy_exit(struct phy *phy);
153int phy_power_on(struct phy *phy);
154int phy_power_off(struct phy *phy);
David Lechner300eb012016-05-09 18:39:59 -0500155int phy_set_mode(struct phy *phy, enum phy_mode mode);
Manu Gautam3b3cd242018-01-16 16:27:09 +0530156static inline enum phy_mode phy_get_mode(struct phy *phy)
157{
158 return phy->attrs.mode;
159}
Randy Licac18ec2016-09-10 02:59:37 +0800160int phy_reset(struct phy *phy);
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +0200161int phy_calibrate(struct phy *phy);
Matt Porter8feed342013-12-19 09:23:02 -0500162static inline int phy_get_bus_width(struct phy *phy)
163{
164 return phy->attrs.bus_width;
165}
166static inline void phy_set_bus_width(struct phy *phy, int bus_width)
167{
168 phy->attrs.bus_width = bus_width;
169}
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530170struct phy *phy_get(struct device *dev, const char *string);
Andrew Lunn788a4d52014-02-04 18:33:12 +0100171struct phy *phy_optional_get(struct device *dev, const char *string);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530172struct phy *devm_phy_get(struct device *dev, const char *string);
Andrew Lunn788a4d52014-02-04 18:33:12 +0100173struct phy *devm_phy_optional_get(struct device *dev, const char *string);
Kamil Debskib5d682f2014-03-06 12:16:47 +0100174struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
175 const char *con_id);
Arun Ramamurthy6be109b2015-04-22 16:04:11 -0700176struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
177 int index);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530178void phy_put(struct phy *phy);
179void devm_phy_put(struct device *dev, struct phy *phy);
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100180struct phy *of_phy_get(struct device_node *np, const char *con_id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530181struct phy *of_phy_simple_xlate(struct device *dev,
182 struct of_phandle_args *args);
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530183struct phy *phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200184 const struct phy_ops *ops);
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530185struct phy *devm_phy_create(struct device *dev, struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200186 const struct phy_ops *ops);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530187void phy_destroy(struct phy *phy);
188void devm_phy_destroy(struct device *dev, struct phy *phy);
189struct phy_provider *__of_phy_provider_register(struct device *dev,
Thierry Reding1140f7c2016-04-05 17:17:34 +0200190 struct device_node *children, struct module *owner,
191 struct phy * (*of_xlate)(struct device *dev,
192 struct of_phandle_args *args));
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530193struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
Thierry Reding1140f7c2016-04-05 17:17:34 +0200194 struct device_node *children, struct module *owner,
195 struct phy * (*of_xlate)(struct device *dev,
196 struct of_phandle_args *args));
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530197void of_phy_provider_unregister(struct phy_provider *phy_provider);
198void devm_of_phy_provider_unregister(struct device *dev,
199 struct phy_provider *phy_provider);
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200200int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
201void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530202#else
203static inline int phy_pm_runtime_get(struct phy *phy)
204{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530205 if (!phy)
206 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530207 return -ENOSYS;
208}
209
210static inline int phy_pm_runtime_get_sync(struct phy *phy)
211{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530212 if (!phy)
213 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530214 return -ENOSYS;
215}
216
217static inline int phy_pm_runtime_put(struct phy *phy)
218{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530219 if (!phy)
220 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530221 return -ENOSYS;
222}
223
224static inline int phy_pm_runtime_put_sync(struct phy *phy)
225{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530226 if (!phy)
227 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530228 return -ENOSYS;
229}
230
231static inline void phy_pm_runtime_allow(struct phy *phy)
232{
233 return;
234}
235
236static inline void phy_pm_runtime_forbid(struct phy *phy)
237{
238 return;
239}
240
241static inline int phy_init(struct phy *phy)
242{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530243 if (!phy)
244 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530245 return -ENOSYS;
246}
247
248static inline int phy_exit(struct phy *phy)
249{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530250 if (!phy)
251 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530252 return -ENOSYS;
253}
254
255static inline int phy_power_on(struct phy *phy)
256{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530257 if (!phy)
258 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530259 return -ENOSYS;
260}
261
262static inline int phy_power_off(struct phy *phy)
263{
Grygorii Strashko2b977892014-04-19 08:51:44 +0530264 if (!phy)
265 return 0;
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530266 return -ENOSYS;
267}
268
David Lechner300eb012016-05-09 18:39:59 -0500269static inline int phy_set_mode(struct phy *phy, enum phy_mode mode)
270{
271 if (!phy)
272 return 0;
273 return -ENOSYS;
274}
275
Manu Gautam3b3cd242018-01-16 16:27:09 +0530276static inline enum phy_mode phy_get_mode(struct phy *phy)
277{
278 return PHY_MODE_INVALID;
279}
280
Randy Li98430c72016-10-25 22:15:34 +0800281static inline int phy_reset(struct phy *phy)
282{
283 if (!phy)
284 return 0;
285 return -ENOSYS;
286}
287
Andrzej Pietrasiewicz36914112017-10-09 14:00:50 +0200288static inline int phy_calibrate(struct phy *phy)
289{
290 if (!phy)
291 return 0;
292 return -ENOSYS;
293}
294
Matt Porter8feed342013-12-19 09:23:02 -0500295static inline int phy_get_bus_width(struct phy *phy)
296{
297 return -ENOSYS;
298}
299
300static inline void phy_set_bus_width(struct phy *phy, int bus_width)
301{
302 return;
303}
304
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530305static inline struct phy *phy_get(struct device *dev, const char *string)
306{
307 return ERR_PTR(-ENOSYS);
308}
309
Andrew Lunn788a4d52014-02-04 18:33:12 +0100310static inline struct phy *phy_optional_get(struct device *dev,
311 const char *string)
312{
313 return ERR_PTR(-ENOSYS);
314}
315
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530316static inline struct phy *devm_phy_get(struct device *dev, const char *string)
317{
318 return ERR_PTR(-ENOSYS);
319}
320
Andrew Lunn788a4d52014-02-04 18:33:12 +0100321static inline struct phy *devm_phy_optional_get(struct device *dev,
322 const char *string)
323{
Maxime Ripard11a6e412017-09-04 14:53:13 +0200324 return NULL;
Andrew Lunn788a4d52014-02-04 18:33:12 +0100325}
326
Kamil Debskib5d682f2014-03-06 12:16:47 +0100327static inline struct phy *devm_of_phy_get(struct device *dev,
328 struct device_node *np,
329 const char *con_id)
330{
331 return ERR_PTR(-ENOSYS);
332}
333
Arun Ramamurthy6be109b2015-04-22 16:04:11 -0700334static inline struct phy *devm_of_phy_get_by_index(struct device *dev,
335 struct device_node *np,
336 int index)
337{
338 return ERR_PTR(-ENOSYS);
339}
340
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530341static inline void phy_put(struct phy *phy)
342{
343}
344
345static inline void devm_phy_put(struct device *dev, struct phy *phy)
346{
347}
348
Kamil Debski0b3f3b22014-03-06 12:16:46 +0100349static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
350{
351 return ERR_PTR(-ENOSYS);
352}
353
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530354static inline struct phy *of_phy_simple_xlate(struct device *dev,
355 struct of_phandle_args *args)
356{
357 return ERR_PTR(-ENOSYS);
358}
359
360static inline struct phy *phy_create(struct device *dev,
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530361 struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200362 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530363{
364 return ERR_PTR(-ENOSYS);
365}
366
367static inline struct phy *devm_phy_create(struct device *dev,
Kishon Vijay Abraham If0ed8172014-07-14 15:55:02 +0530368 struct device_node *node,
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200369 const struct phy_ops *ops)
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530370{
371 return ERR_PTR(-ENOSYS);
372}
373
374static inline void phy_destroy(struct phy *phy)
375{
376}
377
378static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
379{
380}
381
382static inline struct phy_provider *__of_phy_provider_register(
Thierry Reding1140f7c2016-04-05 17:17:34 +0200383 struct device *dev, struct device_node *children, struct module *owner,
384 struct phy * (*of_xlate)(struct device *dev,
385 struct of_phandle_args *args))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530386{
387 return ERR_PTR(-ENOSYS);
388}
389
390static inline struct phy_provider *__devm_of_phy_provider_register(struct device
Thierry Reding1140f7c2016-04-05 17:17:34 +0200391 *dev, struct device_node *children, struct module *owner,
392 struct phy * (*of_xlate)(struct device *dev,
393 struct of_phandle_args *args))
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530394{
395 return ERR_PTR(-ENOSYS);
396}
397
398static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
399{
400}
401
402static inline void devm_of_phy_provider_unregister(struct device *dev,
403 struct phy_provider *phy_provider)
404{
405}
Heikki Krogerusb7bc15b2014-11-19 17:28:18 +0200406static inline int
407phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
408{
409 return 0;
410}
411static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
412 const char *dev_id) { }
Kishon Vijay Abraham Iff764962013-09-27 11:53:25 +0530413#endif
414
415#endif /* __DRIVERS_PHY_H */