1/*
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>
21#include <linux/regulator/consumer.h>
22
23struct phy;
24
25/**
26 * struct phy_ops - set of function pointers for performing phy operations
27 * @init: operation to be performed for initializing phy
28 * @exit: operation to be performed while exiting
29 * @power_on: powering on the phy
30 * @power_off: powering off the phy
31 * @owner: the module owner containing the ops
32 */
33struct phy_ops {
34	int	(*init)(struct phy *phy);
35	int	(*exit)(struct phy *phy);
36	int	(*power_on)(struct phy *phy);
37	int	(*power_off)(struct phy *phy);
38	struct module *owner;
39};
40
41/**
42 * struct phy_attrs - represents phy attributes
43 * @bus_width: Data path width implemented by PHY
44 */
45struct phy_attrs {
46	u32			bus_width;
47};
48
49/**
50 * struct phy - represents the phy device
51 * @dev: phy device
52 * @id: id of the phy device
53 * @ops: function pointers for performing phy operations
54 * @init_data: list of PHY consumers (non-dt only)
55 * @mutex: mutex to protect phy_ops
56 * @init_count: used to protect when the PHY is used by multiple consumers
57 * @power_count: used to protect when the PHY is used by multiple consumers
58 * @phy_attrs: used to specify PHY specific attributes
59 */
60struct phy {
61	struct device		dev;
62	int			id;
63	const struct phy_ops	*ops;
64	struct mutex		mutex;
65	int			init_count;
66	int			power_count;
67	struct phy_attrs	attrs;
68	struct regulator	*pwr;
69};
70
71/**
72 * struct phy_provider - represents the phy provider
73 * @dev: phy provider device
74 * @owner: the module owner having of_xlate
75 * @of_xlate: function pointer to obtain phy instance from phy pointer
76 * @list: to maintain a linked list of PHY providers
77 */
78struct phy_provider {
79	struct device		*dev;
80	struct module		*owner;
81	struct list_head	list;
82	struct phy * (*of_xlate)(struct device *dev,
83		struct of_phandle_args *args);
84};
85
86struct phy_lookup {
87	struct list_head node;
88	const char *dev_id;
89	const char *con_id;
90	struct phy *phy;
91};
92
93#define	to_phy(a)	(container_of((a), struct phy, dev))
94
95#define	of_phy_provider_register(dev, xlate)	\
96	__of_phy_provider_register((dev), THIS_MODULE, (xlate))
97
98#define	devm_of_phy_provider_register(dev, xlate)	\
99	__devm_of_phy_provider_register((dev), THIS_MODULE, (xlate))
100
101static inline void phy_set_drvdata(struct phy *phy, void *data)
102{
103	dev_set_drvdata(&phy->dev, data);
104}
105
106static inline void *phy_get_drvdata(struct phy *phy)
107{
108	return dev_get_drvdata(&phy->dev);
109}
110
111#if IS_ENABLED(CONFIG_GENERIC_PHY)
112int phy_pm_runtime_get(struct phy *phy);
113int phy_pm_runtime_get_sync(struct phy *phy);
114int phy_pm_runtime_put(struct phy *phy);
115int phy_pm_runtime_put_sync(struct phy *phy);
116void phy_pm_runtime_allow(struct phy *phy);
117void phy_pm_runtime_forbid(struct phy *phy);
118int phy_init(struct phy *phy);
119int phy_exit(struct phy *phy);
120int phy_power_on(struct phy *phy);
121int phy_power_off(struct phy *phy);
122static inline int phy_get_bus_width(struct phy *phy)
123{
124	return phy->attrs.bus_width;
125}
126static inline void phy_set_bus_width(struct phy *phy, int bus_width)
127{
128	phy->attrs.bus_width = bus_width;
129}
130struct phy *phy_get(struct device *dev, const char *string);
131struct phy *phy_optional_get(struct device *dev, const char *string);
132struct phy *devm_phy_get(struct device *dev, const char *string);
133struct phy *devm_phy_optional_get(struct device *dev, const char *string);
134struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
135			    const char *con_id);
136void phy_put(struct phy *phy);
137void devm_phy_put(struct device *dev, struct phy *phy);
138struct phy *of_phy_get(struct device_node *np, const char *con_id);
139struct phy *of_phy_simple_xlate(struct device *dev,
140	struct of_phandle_args *args);
141struct phy *phy_create(struct device *dev, struct device_node *node,
142		       const struct phy_ops *ops);
143struct phy *devm_phy_create(struct device *dev, struct device_node *node,
144			    const struct phy_ops *ops);
145void phy_destroy(struct phy *phy);
146void devm_phy_destroy(struct device *dev, struct phy *phy);
147struct phy_provider *__of_phy_provider_register(struct device *dev,
148	struct module *owner, struct phy * (*of_xlate)(struct device *dev,
149	struct of_phandle_args *args));
150struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
151	struct module *owner, struct phy * (*of_xlate)(struct device *dev,
152	struct of_phandle_args *args));
153void of_phy_provider_unregister(struct phy_provider *phy_provider);
154void devm_of_phy_provider_unregister(struct device *dev,
155	struct phy_provider *phy_provider);
156int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id);
157void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id);
158#else
159static inline int phy_pm_runtime_get(struct phy *phy)
160{
161	if (!phy)
162		return 0;
163	return -ENOSYS;
164}
165
166static inline int phy_pm_runtime_get_sync(struct phy *phy)
167{
168	if (!phy)
169		return 0;
170	return -ENOSYS;
171}
172
173static inline int phy_pm_runtime_put(struct phy *phy)
174{
175	if (!phy)
176		return 0;
177	return -ENOSYS;
178}
179
180static inline int phy_pm_runtime_put_sync(struct phy *phy)
181{
182	if (!phy)
183		return 0;
184	return -ENOSYS;
185}
186
187static inline void phy_pm_runtime_allow(struct phy *phy)
188{
189	return;
190}
191
192static inline void phy_pm_runtime_forbid(struct phy *phy)
193{
194	return;
195}
196
197static inline int phy_init(struct phy *phy)
198{
199	if (!phy)
200		return 0;
201	return -ENOSYS;
202}
203
204static inline int phy_exit(struct phy *phy)
205{
206	if (!phy)
207		return 0;
208	return -ENOSYS;
209}
210
211static inline int phy_power_on(struct phy *phy)
212{
213	if (!phy)
214		return 0;
215	return -ENOSYS;
216}
217
218static inline int phy_power_off(struct phy *phy)
219{
220	if (!phy)
221		return 0;
222	return -ENOSYS;
223}
224
225static inline int phy_get_bus_width(struct phy *phy)
226{
227	return -ENOSYS;
228}
229
230static inline void phy_set_bus_width(struct phy *phy, int bus_width)
231{
232	return;
233}
234
235static inline struct phy *phy_get(struct device *dev, const char *string)
236{
237	return ERR_PTR(-ENOSYS);
238}
239
240static inline struct phy *phy_optional_get(struct device *dev,
241					   const char *string)
242{
243	return ERR_PTR(-ENOSYS);
244}
245
246static inline struct phy *devm_phy_get(struct device *dev, const char *string)
247{
248	return ERR_PTR(-ENOSYS);
249}
250
251static inline struct phy *devm_phy_optional_get(struct device *dev,
252						const char *string)
253{
254	return ERR_PTR(-ENOSYS);
255}
256
257static inline struct phy *devm_of_phy_get(struct device *dev,
258					  struct device_node *np,
259					  const char *con_id)
260{
261	return ERR_PTR(-ENOSYS);
262}
263
264static inline void phy_put(struct phy *phy)
265{
266}
267
268static inline void devm_phy_put(struct device *dev, struct phy *phy)
269{
270}
271
272static inline struct phy *of_phy_get(struct device_node *np, const char *con_id)
273{
274	return ERR_PTR(-ENOSYS);
275}
276
277static inline struct phy *of_phy_simple_xlate(struct device *dev,
278	struct of_phandle_args *args)
279{
280	return ERR_PTR(-ENOSYS);
281}
282
283static inline struct phy *phy_create(struct device *dev,
284				     struct device_node *node,
285				     const struct phy_ops *ops)
286{
287	return ERR_PTR(-ENOSYS);
288}
289
290static inline struct phy *devm_phy_create(struct device *dev,
291					  struct device_node *node,
292					  const struct phy_ops *ops)
293{
294	return ERR_PTR(-ENOSYS);
295}
296
297static inline void phy_destroy(struct phy *phy)
298{
299}
300
301static inline void devm_phy_destroy(struct device *dev, struct phy *phy)
302{
303}
304
305static inline struct phy_provider *__of_phy_provider_register(
306	struct device *dev, struct module *owner, struct phy * (*of_xlate)(
307	struct device *dev, struct of_phandle_args *args))
308{
309	return ERR_PTR(-ENOSYS);
310}
311
312static inline struct phy_provider *__devm_of_phy_provider_register(struct device
313	*dev, struct module *owner, struct phy * (*of_xlate)(struct device *dev,
314	struct of_phandle_args *args))
315{
316	return ERR_PTR(-ENOSYS);
317}
318
319static inline void of_phy_provider_unregister(struct phy_provider *phy_provider)
320{
321}
322
323static inline void devm_of_phy_provider_unregister(struct device *dev,
324	struct phy_provider *phy_provider)
325{
326}
327static inline int
328phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
329{
330	return 0;
331}
332static inline void phy_remove_lookup(struct phy *phy, const char *con_id,
333				     const char *dev_id) { }
334#endif
335
336#endif /* __DRIVERS_PHY_H */
337