xref: /openbmc/linux/drivers/phy/phy-core.c (revision 33f434d283a27116fb358ae5bc3b42967c12f85a)
1ff764963SKishon Vijay Abraham I /*
2ff764963SKishon Vijay Abraham I  * phy-core.c  --  Generic Phy framework.
3ff764963SKishon Vijay Abraham I  *
4ff764963SKishon Vijay Abraham I  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5ff764963SKishon Vijay Abraham I  *
6ff764963SKishon Vijay Abraham I  * Author: Kishon Vijay Abraham I <kishon@ti.com>
7ff764963SKishon Vijay Abraham I  *
8ff764963SKishon Vijay Abraham I  * This program is free software; you can redistribute  it and/or modify it
9ff764963SKishon Vijay Abraham I  * under  the terms of  the GNU General  Public License as published by the
10ff764963SKishon Vijay Abraham I  * Free Software Foundation;  either version 2 of the  License, or (at your
11ff764963SKishon Vijay Abraham I  * option) any later version.
12ff764963SKishon Vijay Abraham I  */
13ff764963SKishon Vijay Abraham I 
14ff764963SKishon Vijay Abraham I #include <linux/kernel.h>
15ff764963SKishon Vijay Abraham I #include <linux/export.h>
16ff764963SKishon Vijay Abraham I #include <linux/module.h>
17ff764963SKishon Vijay Abraham I #include <linux/err.h>
18ff764963SKishon Vijay Abraham I #include <linux/device.h>
19ff764963SKishon Vijay Abraham I #include <linux/slab.h>
20ff764963SKishon Vijay Abraham I #include <linux/of.h>
21ff764963SKishon Vijay Abraham I #include <linux/phy/phy.h>
22ff764963SKishon Vijay Abraham I #include <linux/idr.h>
23ff764963SKishon Vijay Abraham I #include <linux/pm_runtime.h>
243be88125SRoger Quadros #include <linux/regulator/consumer.h>
25ff764963SKishon Vijay Abraham I 
26ff764963SKishon Vijay Abraham I static struct class *phy_class;
27ff764963SKishon Vijay Abraham I static DEFINE_MUTEX(phy_provider_mutex);
28ff764963SKishon Vijay Abraham I static LIST_HEAD(phy_provider_list);
29b7bc15b9SHeikki Krogerus static LIST_HEAD(phys);
30ff764963SKishon Vijay Abraham I static DEFINE_IDA(phy_ida);
31ff764963SKishon Vijay Abraham I 
32ff764963SKishon Vijay Abraham I static void devm_phy_release(struct device *dev, void *res)
33ff764963SKishon Vijay Abraham I {
34ff764963SKishon Vijay Abraham I 	struct phy *phy = *(struct phy **)res;
35ff764963SKishon Vijay Abraham I 
36ff764963SKishon Vijay Abraham I 	phy_put(phy);
37ff764963SKishon Vijay Abraham I }
38ff764963SKishon Vijay Abraham I 
39ff764963SKishon Vijay Abraham I static void devm_phy_provider_release(struct device *dev, void *res)
40ff764963SKishon Vijay Abraham I {
41ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider = *(struct phy_provider **)res;
42ff764963SKishon Vijay Abraham I 
43ff764963SKishon Vijay Abraham I 	of_phy_provider_unregister(phy_provider);
44ff764963SKishon Vijay Abraham I }
45ff764963SKishon Vijay Abraham I 
46ff764963SKishon Vijay Abraham I static void devm_phy_consume(struct device *dev, void *res)
47ff764963SKishon Vijay Abraham I {
48ff764963SKishon Vijay Abraham I 	struct phy *phy = *(struct phy **)res;
49ff764963SKishon Vijay Abraham I 
50ff764963SKishon Vijay Abraham I 	phy_destroy(phy);
51ff764963SKishon Vijay Abraham I }
52ff764963SKishon Vijay Abraham I 
53ff764963SKishon Vijay Abraham I static int devm_phy_match(struct device *dev, void *res, void *match_data)
54ff764963SKishon Vijay Abraham I {
552f1bce48SThierry Reding 	struct phy **phy = res;
562f1bce48SThierry Reding 
572f1bce48SThierry Reding 	return *phy == match_data;
58ff764963SKishon Vijay Abraham I }
59ff764963SKishon Vijay Abraham I 
60b7bc15b9SHeikki Krogerus /**
61b7bc15b9SHeikki Krogerus  * phy_create_lookup() - allocate and register PHY/device association
62b7bc15b9SHeikki Krogerus  * @phy: the phy of the association
63b7bc15b9SHeikki Krogerus  * @con_id: connection ID string on device
64b7bc15b9SHeikki Krogerus  * @dev_id: the device of the association
65b7bc15b9SHeikki Krogerus  *
66b7bc15b9SHeikki Krogerus  * Creates and registers phy_lookup entry.
67b7bc15b9SHeikki Krogerus  */
68b7bc15b9SHeikki Krogerus int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
69b7bc15b9SHeikki Krogerus {
70b7bc15b9SHeikki Krogerus 	struct phy_lookup *pl;
71b7bc15b9SHeikki Krogerus 
72b7bc15b9SHeikki Krogerus 	if (!phy || !dev_id || !con_id)
73b7bc15b9SHeikki Krogerus 		return -EINVAL;
74b7bc15b9SHeikki Krogerus 
75b7bc15b9SHeikki Krogerus 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
76b7bc15b9SHeikki Krogerus 	if (!pl)
77b7bc15b9SHeikki Krogerus 		return -ENOMEM;
78b7bc15b9SHeikki Krogerus 
79b7bc15b9SHeikki Krogerus 	pl->dev_id = dev_id;
80b7bc15b9SHeikki Krogerus 	pl->con_id = con_id;
81b7bc15b9SHeikki Krogerus 	pl->phy = phy;
82b7bc15b9SHeikki Krogerus 
83b7bc15b9SHeikki Krogerus 	mutex_lock(&phy_provider_mutex);
84b7bc15b9SHeikki Krogerus 	list_add_tail(&pl->node, &phys);
85b7bc15b9SHeikki Krogerus 	mutex_unlock(&phy_provider_mutex);
86b7bc15b9SHeikki Krogerus 
87b7bc15b9SHeikki Krogerus 	return 0;
88b7bc15b9SHeikki Krogerus }
89b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_create_lookup);
90b7bc15b9SHeikki Krogerus 
91b7bc15b9SHeikki Krogerus /**
92b7bc15b9SHeikki Krogerus  * phy_remove_lookup() - find and remove PHY/device association
93b7bc15b9SHeikki Krogerus  * @phy: the phy of the association
94b7bc15b9SHeikki Krogerus  * @con_id: connection ID string on device
95b7bc15b9SHeikki Krogerus  * @dev_id: the device of the association
96b7bc15b9SHeikki Krogerus  *
97b7bc15b9SHeikki Krogerus  * Finds and unregisters phy_lookup entry that was created with
98b7bc15b9SHeikki Krogerus  * phy_create_lookup().
99b7bc15b9SHeikki Krogerus  */
100b7bc15b9SHeikki Krogerus void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
101b7bc15b9SHeikki Krogerus {
102b7bc15b9SHeikki Krogerus 	struct phy_lookup *pl;
103b7bc15b9SHeikki Krogerus 
104b7bc15b9SHeikki Krogerus 	if (!phy || !dev_id || !con_id)
105b7bc15b9SHeikki Krogerus 		return;
106b7bc15b9SHeikki Krogerus 
107b7bc15b9SHeikki Krogerus 	mutex_lock(&phy_provider_mutex);
108b7bc15b9SHeikki Krogerus 	list_for_each_entry(pl, &phys, node)
109b7bc15b9SHeikki Krogerus 		if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
110b7bc15b9SHeikki Krogerus 		    !strcmp(pl->con_id, con_id)) {
111b7bc15b9SHeikki Krogerus 			list_del(&pl->node);
112b7bc15b9SHeikki Krogerus 			kfree(pl);
113b7bc15b9SHeikki Krogerus 			break;
114b7bc15b9SHeikki Krogerus 		}
115b7bc15b9SHeikki Krogerus 	mutex_unlock(&phy_provider_mutex);
116b7bc15b9SHeikki Krogerus }
117b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_remove_lookup);
118b7bc15b9SHeikki Krogerus 
119b7bc15b9SHeikki Krogerus static struct phy *phy_find(struct device *dev, const char *con_id)
120b7bc15b9SHeikki Krogerus {
121b7bc15b9SHeikki Krogerus 	const char *dev_id = dev_name(dev);
122b7bc15b9SHeikki Krogerus 	struct phy_lookup *p, *pl = NULL;
123b7bc15b9SHeikki Krogerus 
124b7bc15b9SHeikki Krogerus 	mutex_lock(&phy_provider_mutex);
125b7bc15b9SHeikki Krogerus 	list_for_each_entry(p, &phys, node)
126b7bc15b9SHeikki Krogerus 		if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
127b7bc15b9SHeikki Krogerus 			pl = p;
128b7bc15b9SHeikki Krogerus 			break;
129b7bc15b9SHeikki Krogerus 		}
130b7bc15b9SHeikki Krogerus 	mutex_unlock(&phy_provider_mutex);
131b7bc15b9SHeikki Krogerus 
132dbc98635SHeikki Krogerus 	return pl ? pl->phy : ERR_PTR(-ENODEV);
133b7bc15b9SHeikki Krogerus }
134b7bc15b9SHeikki Krogerus 
135ff764963SKishon Vijay Abraham I static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
136ff764963SKishon Vijay Abraham I {
137ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
1382a4c3701SKishon Vijay Abraham I 	struct device_node *child;
139ff764963SKishon Vijay Abraham I 
140ff764963SKishon Vijay Abraham I 	list_for_each_entry(phy_provider, &phy_provider_list, list) {
141ff764963SKishon Vijay Abraham I 		if (phy_provider->dev->of_node == node)
142ff764963SKishon Vijay Abraham I 			return phy_provider;
1432a4c3701SKishon Vijay Abraham I 
1442a4c3701SKishon Vijay Abraham I 		for_each_child_of_node(phy_provider->dev->of_node, child)
1452a4c3701SKishon Vijay Abraham I 			if (child == node)
1462a4c3701SKishon Vijay Abraham I 				return phy_provider;
147ff764963SKishon Vijay Abraham I 	}
148ff764963SKishon Vijay Abraham I 
149ff764963SKishon Vijay Abraham I 	return ERR_PTR(-EPROBE_DEFER);
150ff764963SKishon Vijay Abraham I }
151ff764963SKishon Vijay Abraham I 
152ff764963SKishon Vijay Abraham I int phy_pm_runtime_get(struct phy *phy)
153ff764963SKishon Vijay Abraham I {
154cedb7f89SFelipe Balbi 	int ret;
155cedb7f89SFelipe Balbi 
156ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
157ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
158ff764963SKishon Vijay Abraham I 
159cedb7f89SFelipe Balbi 	ret = pm_runtime_get(&phy->dev);
160cedb7f89SFelipe Balbi 	if (ret < 0 && ret != -EINPROGRESS)
161cedb7f89SFelipe Balbi 		pm_runtime_put_noidle(&phy->dev);
162cedb7f89SFelipe Balbi 
163cedb7f89SFelipe Balbi 	return ret;
164ff764963SKishon Vijay Abraham I }
165ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
166ff764963SKishon Vijay Abraham I 
167ff764963SKishon Vijay Abraham I int phy_pm_runtime_get_sync(struct phy *phy)
168ff764963SKishon Vijay Abraham I {
169cedb7f89SFelipe Balbi 	int ret;
170cedb7f89SFelipe Balbi 
171ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
172ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
173ff764963SKishon Vijay Abraham I 
174cedb7f89SFelipe Balbi 	ret = pm_runtime_get_sync(&phy->dev);
175cedb7f89SFelipe Balbi 	if (ret < 0)
176cedb7f89SFelipe Balbi 		pm_runtime_put_sync(&phy->dev);
177cedb7f89SFelipe Balbi 
178cedb7f89SFelipe Balbi 	return ret;
179ff764963SKishon Vijay Abraham I }
180ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
181ff764963SKishon Vijay Abraham I 
182ff764963SKishon Vijay Abraham I int phy_pm_runtime_put(struct phy *phy)
183ff764963SKishon Vijay Abraham I {
184ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
185ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
186ff764963SKishon Vijay Abraham I 
187ff764963SKishon Vijay Abraham I 	return pm_runtime_put(&phy->dev);
188ff764963SKishon Vijay Abraham I }
189ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
190ff764963SKishon Vijay Abraham I 
191ff764963SKishon Vijay Abraham I int phy_pm_runtime_put_sync(struct phy *phy)
192ff764963SKishon Vijay Abraham I {
193ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
194ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
195ff764963SKishon Vijay Abraham I 
196ff764963SKishon Vijay Abraham I 	return pm_runtime_put_sync(&phy->dev);
197ff764963SKishon Vijay Abraham I }
198ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
199ff764963SKishon Vijay Abraham I 
200ff764963SKishon Vijay Abraham I void phy_pm_runtime_allow(struct phy *phy)
201ff764963SKishon Vijay Abraham I {
202ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
203ff764963SKishon Vijay Abraham I 		return;
204ff764963SKishon Vijay Abraham I 
205ff764963SKishon Vijay Abraham I 	pm_runtime_allow(&phy->dev);
206ff764963SKishon Vijay Abraham I }
207ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
208ff764963SKishon Vijay Abraham I 
209ff764963SKishon Vijay Abraham I void phy_pm_runtime_forbid(struct phy *phy)
210ff764963SKishon Vijay Abraham I {
211ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
212ff764963SKishon Vijay Abraham I 		return;
213ff764963SKishon Vijay Abraham I 
214ff764963SKishon Vijay Abraham I 	pm_runtime_forbid(&phy->dev);
215ff764963SKishon Vijay Abraham I }
216ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
217ff764963SKishon Vijay Abraham I 
218ff764963SKishon Vijay Abraham I int phy_init(struct phy *phy)
219ff764963SKishon Vijay Abraham I {
220ff764963SKishon Vijay Abraham I 	int ret;
221ff764963SKishon Vijay Abraham I 
22204c2facaSAndrew Lunn 	if (!phy)
22304c2facaSAndrew Lunn 		return 0;
22404c2facaSAndrew Lunn 
225ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
226ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
227ff764963SKishon Vijay Abraham I 		return ret;
228736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
229ff764963SKishon Vijay Abraham I 
230ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
231637d378cSKishon Vijay Abraham I 	if (phy->init_count == 0 && phy->ops->init) {
232ff764963SKishon Vijay Abraham I 		ret = phy->ops->init(phy);
233ff764963SKishon Vijay Abraham I 		if (ret < 0) {
234ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy init failed --> %d\n", ret);
235ff764963SKishon Vijay Abraham I 			goto out;
236ff764963SKishon Vijay Abraham I 		}
237ff764963SKishon Vijay Abraham I 	}
238637d378cSKishon Vijay Abraham I 	++phy->init_count;
239ff764963SKishon Vijay Abraham I 
240ff764963SKishon Vijay Abraham I out:
241ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
242ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
243ff764963SKishon Vijay Abraham I 	return ret;
244ff764963SKishon Vijay Abraham I }
245ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_init);
246ff764963SKishon Vijay Abraham I 
247ff764963SKishon Vijay Abraham I int phy_exit(struct phy *phy)
248ff764963SKishon Vijay Abraham I {
249ff764963SKishon Vijay Abraham I 	int ret;
250ff764963SKishon Vijay Abraham I 
25104c2facaSAndrew Lunn 	if (!phy)
25204c2facaSAndrew Lunn 		return 0;
25304c2facaSAndrew Lunn 
254ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
255ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
256ff764963SKishon Vijay Abraham I 		return ret;
257736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
258ff764963SKishon Vijay Abraham I 
259ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
260637d378cSKishon Vijay Abraham I 	if (phy->init_count == 1 && phy->ops->exit) {
261ff764963SKishon Vijay Abraham I 		ret = phy->ops->exit(phy);
262ff764963SKishon Vijay Abraham I 		if (ret < 0) {
263ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
264ff764963SKishon Vijay Abraham I 			goto out;
265ff764963SKishon Vijay Abraham I 		}
266ff764963SKishon Vijay Abraham I 	}
267637d378cSKishon Vijay Abraham I 	--phy->init_count;
268ff764963SKishon Vijay Abraham I 
269ff764963SKishon Vijay Abraham I out:
270ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
271ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
272ff764963SKishon Vijay Abraham I 	return ret;
273ff764963SKishon Vijay Abraham I }
274ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_exit);
275ff764963SKishon Vijay Abraham I 
276ff764963SKishon Vijay Abraham I int phy_power_on(struct phy *phy)
277ff764963SKishon Vijay Abraham I {
278d18c9604SKishon Vijay Abraham I 	int ret;
279ff764963SKishon Vijay Abraham I 
28004c2facaSAndrew Lunn 	if (!phy)
28104c2facaSAndrew Lunn 		return 0;
28204c2facaSAndrew Lunn 
2833be88125SRoger Quadros 	if (phy->pwr) {
2843be88125SRoger Quadros 		ret = regulator_enable(phy->pwr);
2853be88125SRoger Quadros 		if (ret)
2863be88125SRoger Quadros 			return ret;
2873be88125SRoger Quadros 	}
2883be88125SRoger Quadros 
289ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
290ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
291ff764963SKishon Vijay Abraham I 		return ret;
292736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
293ff764963SKishon Vijay Abraham I 
294ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
295637d378cSKishon Vijay Abraham I 	if (phy->power_count == 0 && phy->ops->power_on) {
296ff764963SKishon Vijay Abraham I 		ret = phy->ops->power_on(phy);
297ff764963SKishon Vijay Abraham I 		if (ret < 0) {
298ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
299ff764963SKishon Vijay Abraham I 			goto out;
300ff764963SKishon Vijay Abraham I 		}
301ff764963SKishon Vijay Abraham I 	}
302637d378cSKishon Vijay Abraham I 	++phy->power_count;
303637d378cSKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
304637d378cSKishon Vijay Abraham I 	return 0;
305ff764963SKishon Vijay Abraham I 
306ff764963SKishon Vijay Abraham I out:
307ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
308637d378cSKishon Vijay Abraham I 	phy_pm_runtime_put_sync(phy);
3093be88125SRoger Quadros 	if (phy->pwr)
3103be88125SRoger Quadros 		regulator_disable(phy->pwr);
311ff764963SKishon Vijay Abraham I 
312ff764963SKishon Vijay Abraham I 	return ret;
313ff764963SKishon Vijay Abraham I }
314ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_on);
315ff764963SKishon Vijay Abraham I 
316ff764963SKishon Vijay Abraham I int phy_power_off(struct phy *phy)
317ff764963SKishon Vijay Abraham I {
318d18c9604SKishon Vijay Abraham I 	int ret;
319ff764963SKishon Vijay Abraham I 
32004c2facaSAndrew Lunn 	if (!phy)
32104c2facaSAndrew Lunn 		return 0;
32204c2facaSAndrew Lunn 
323ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
324637d378cSKishon Vijay Abraham I 	if (phy->power_count == 1 && phy->ops->power_off) {
325ff764963SKishon Vijay Abraham I 		ret =  phy->ops->power_off(phy);
326ff764963SKishon Vijay Abraham I 		if (ret < 0) {
327ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
328637d378cSKishon Vijay Abraham I 			mutex_unlock(&phy->mutex);
329637d378cSKishon Vijay Abraham I 			return ret;
330ff764963SKishon Vijay Abraham I 		}
331ff764963SKishon Vijay Abraham I 	}
332637d378cSKishon Vijay Abraham I 	--phy->power_count;
333ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
334ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
335ff764963SKishon Vijay Abraham I 
3363be88125SRoger Quadros 	if (phy->pwr)
3373be88125SRoger Quadros 		regulator_disable(phy->pwr);
3383be88125SRoger Quadros 
339637d378cSKishon Vijay Abraham I 	return 0;
340ff764963SKishon Vijay Abraham I }
341ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_off);
342ff764963SKishon Vijay Abraham I 
343ff764963SKishon Vijay Abraham I /**
3440b3f3b2cSKamil Debski  * _of_phy_get() - lookup and obtain a reference to a phy by phandle
3450b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
346ff764963SKishon Vijay Abraham I  * @index: the index of the phy
347ff764963SKishon Vijay Abraham I  *
348ff764963SKishon Vijay Abraham I  * Returns the phy associated with the given phandle value,
349ff764963SKishon Vijay Abraham I  * after getting a refcount to it or -ENODEV if there is no such phy or
350ff764963SKishon Vijay Abraham I  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
351ff764963SKishon Vijay Abraham I  * not yet loaded. This function uses of_xlate call back function provided
352ff764963SKishon Vijay Abraham I  * while registering the phy_provider to find the phy instance.
353ff764963SKishon Vijay Abraham I  */
3540b3f3b2cSKamil Debski static struct phy *_of_phy_get(struct device_node *np, int index)
355ff764963SKishon Vijay Abraham I {
356ff764963SKishon Vijay Abraham I 	int ret;
357ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
358ff764963SKishon Vijay Abraham I 	struct phy *phy = NULL;
359ff764963SKishon Vijay Abraham I 	struct of_phandle_args args;
360ff764963SKishon Vijay Abraham I 
3610b3f3b2cSKamil Debski 	ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
362ff764963SKishon Vijay Abraham I 		index, &args);
3630b3f3b2cSKamil Debski 	if (ret)
364ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENODEV);
365ff764963SKishon Vijay Abraham I 
366ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
367ff764963SKishon Vijay Abraham I 	phy_provider = of_phy_provider_lookup(args.np);
368ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
369ff764963SKishon Vijay Abraham I 		phy = ERR_PTR(-EPROBE_DEFER);
370*33f434d2SAxel Lin 		goto out_unlock;
371*33f434d2SAxel Lin 	}
372*33f434d2SAxel Lin 
373*33f434d2SAxel Lin 	if (!of_device_is_available(args.np)) {
374*33f434d2SAxel Lin 		dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
375*33f434d2SAxel Lin 		phy = ERR_PTR(-ENODEV);
376*33f434d2SAxel Lin 		goto out_put_module;
377ff764963SKishon Vijay Abraham I 	}
378ff764963SKishon Vijay Abraham I 
379ff764963SKishon Vijay Abraham I 	phy = phy_provider->of_xlate(phy_provider->dev, &args);
380*33f434d2SAxel Lin 
381*33f434d2SAxel Lin out_put_module:
382ff764963SKishon Vijay Abraham I 	module_put(phy_provider->owner);
383ff764963SKishon Vijay Abraham I 
384*33f434d2SAxel Lin out_unlock:
385ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
386ff764963SKishon Vijay Abraham I 	of_node_put(args.np);
387ff764963SKishon Vijay Abraham I 
388ff764963SKishon Vijay Abraham I 	return phy;
389ff764963SKishon Vijay Abraham I }
390ff764963SKishon Vijay Abraham I 
391ff764963SKishon Vijay Abraham I /**
3920b3f3b2cSKamil Debski  * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
3930b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
3940b3f3b2cSKamil Debski  * @con_id: name of the phy from device's point of view
3950b3f3b2cSKamil Debski  *
3960b3f3b2cSKamil Debski  * Returns the phy driver, after getting a refcount to it; or
3970b3f3b2cSKamil Debski  * -ENODEV if there is no such phy. The caller is responsible for
3980b3f3b2cSKamil Debski  * calling phy_put() to release that count.
3990b3f3b2cSKamil Debski  */
4000b3f3b2cSKamil Debski struct phy *of_phy_get(struct device_node *np, const char *con_id)
4010b3f3b2cSKamil Debski {
4020b3f3b2cSKamil Debski 	struct phy *phy = NULL;
4030b3f3b2cSKamil Debski 	int index = 0;
4040b3f3b2cSKamil Debski 
4050b3f3b2cSKamil Debski 	if (con_id)
4060b3f3b2cSKamil Debski 		index = of_property_match_string(np, "phy-names", con_id);
4070b3f3b2cSKamil Debski 
4080b3f3b2cSKamil Debski 	phy = _of_phy_get(np, index);
4090b3f3b2cSKamil Debski 	if (IS_ERR(phy))
4100b3f3b2cSKamil Debski 		return phy;
4110b3f3b2cSKamil Debski 
4120b3f3b2cSKamil Debski 	if (!try_module_get(phy->ops->owner))
4130b3f3b2cSKamil Debski 		return ERR_PTR(-EPROBE_DEFER);
4140b3f3b2cSKamil Debski 
4150b3f3b2cSKamil Debski 	get_device(&phy->dev);
4160b3f3b2cSKamil Debski 
4170b3f3b2cSKamil Debski 	return phy;
4180b3f3b2cSKamil Debski }
4190b3f3b2cSKamil Debski EXPORT_SYMBOL_GPL(of_phy_get);
4200b3f3b2cSKamil Debski 
4210b3f3b2cSKamil Debski /**
422ff764963SKishon Vijay Abraham I  * phy_put() - release the PHY
423ff764963SKishon Vijay Abraham I  * @phy: the phy returned by phy_get()
424ff764963SKishon Vijay Abraham I  *
425ff764963SKishon Vijay Abraham I  * Releases a refcount the caller received from phy_get().
426ff764963SKishon Vijay Abraham I  */
427ff764963SKishon Vijay Abraham I void phy_put(struct phy *phy)
428ff764963SKishon Vijay Abraham I {
42904c2facaSAndrew Lunn 	if (!phy || IS_ERR(phy))
430ff764963SKishon Vijay Abraham I 		return;
431ff764963SKishon Vijay Abraham I 
432ff764963SKishon Vijay Abraham I 	module_put(phy->ops->owner);
433ff764963SKishon Vijay Abraham I 	put_device(&phy->dev);
434ff764963SKishon Vijay Abraham I }
435ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_put);
436ff764963SKishon Vijay Abraham I 
437ff764963SKishon Vijay Abraham I /**
438ff764963SKishon Vijay Abraham I  * devm_phy_put() - release the PHY
439ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
440ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
441ff764963SKishon Vijay Abraham I  *
442ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_put
443ff764963SKishon Vijay Abraham I  * to release the phy.
444ff764963SKishon Vijay Abraham I  */
445ff764963SKishon Vijay Abraham I void devm_phy_put(struct device *dev, struct phy *phy)
446ff764963SKishon Vijay Abraham I {
447ff764963SKishon Vijay Abraham I 	int r;
448ff764963SKishon Vijay Abraham I 
44904c2facaSAndrew Lunn 	if (!phy)
45004c2facaSAndrew Lunn 		return;
45104c2facaSAndrew Lunn 
452ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
453ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
454ff764963SKishon Vijay Abraham I }
455ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_put);
456ff764963SKishon Vijay Abraham I 
457ff764963SKishon Vijay Abraham I /**
458ff764963SKishon Vijay Abraham I  * of_phy_simple_xlate() - returns the phy instance from phy provider
459ff764963SKishon Vijay Abraham I  * @dev: the PHY provider device
460ff764963SKishon Vijay Abraham I  * @args: of_phandle_args (not used here)
461ff764963SKishon Vijay Abraham I  *
462ff764963SKishon Vijay Abraham I  * Intended to be used by phy provider for the common case where #phy-cells is
463ff764963SKishon Vijay Abraham I  * 0. For other cases where #phy-cells is greater than '0', the phy provider
464ff764963SKishon Vijay Abraham I  * should provide a custom of_xlate function that reads the *args* and returns
465ff764963SKishon Vijay Abraham I  * the appropriate phy.
466ff764963SKishon Vijay Abraham I  */
467ff764963SKishon Vijay Abraham I struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
468ff764963SKishon Vijay Abraham I 	*args)
469ff764963SKishon Vijay Abraham I {
470ff764963SKishon Vijay Abraham I 	struct phy *phy;
471ff764963SKishon Vijay Abraham I 	struct class_dev_iter iter;
472ff764963SKishon Vijay Abraham I 
473ff764963SKishon Vijay Abraham I 	class_dev_iter_init(&iter, phy_class, NULL, NULL);
474ff764963SKishon Vijay Abraham I 	while ((dev = class_dev_iter_next(&iter))) {
475ff764963SKishon Vijay Abraham I 		phy = to_phy(dev);
476491e0490SKishon Vijay Abraham I 		if (args->np != phy->dev.of_node)
477ff764963SKishon Vijay Abraham I 			continue;
478ff764963SKishon Vijay Abraham I 
479ff764963SKishon Vijay Abraham I 		class_dev_iter_exit(&iter);
480ff764963SKishon Vijay Abraham I 		return phy;
481ff764963SKishon Vijay Abraham I 	}
482ff764963SKishon Vijay Abraham I 
483ff764963SKishon Vijay Abraham I 	class_dev_iter_exit(&iter);
484ff764963SKishon Vijay Abraham I 	return ERR_PTR(-ENODEV);
485ff764963SKishon Vijay Abraham I }
486ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
487ff764963SKishon Vijay Abraham I 
488ff764963SKishon Vijay Abraham I /**
489ff764963SKishon Vijay Abraham I  * phy_get() - lookup and obtain a reference to a phy.
490ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
491ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or the name of the controller
492ff764963SKishon Vijay Abraham I  * port for non-dt case
493ff764963SKishon Vijay Abraham I  *
494ff764963SKishon Vijay Abraham I  * Returns the phy driver, after getting a refcount to it; or
495ff764963SKishon Vijay Abraham I  * -ENODEV if there is no such phy.  The caller is responsible for
496ff764963SKishon Vijay Abraham I  * calling phy_put() to release that count.
497ff764963SKishon Vijay Abraham I  */
498ff764963SKishon Vijay Abraham I struct phy *phy_get(struct device *dev, const char *string)
499ff764963SKishon Vijay Abraham I {
500ff764963SKishon Vijay Abraham I 	int index = 0;
501d18c9604SKishon Vijay Abraham I 	struct phy *phy;
502ff764963SKishon Vijay Abraham I 
503ff764963SKishon Vijay Abraham I 	if (string == NULL) {
504ff764963SKishon Vijay Abraham I 		dev_WARN(dev, "missing string\n");
505ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EINVAL);
506ff764963SKishon Vijay Abraham I 	}
507ff764963SKishon Vijay Abraham I 
508ff764963SKishon Vijay Abraham I 	if (dev->of_node) {
509ff764963SKishon Vijay Abraham I 		index = of_property_match_string(dev->of_node, "phy-names",
510ff764963SKishon Vijay Abraham I 			string);
5110b3f3b2cSKamil Debski 		phy = _of_phy_get(dev->of_node, index);
512ff764963SKishon Vijay Abraham I 	} else {
513b7bc15b9SHeikki Krogerus 		phy = phy_find(dev, string);
514f40037fdSHans de Goede 	}
515f40037fdSHans de Goede 	if (IS_ERR(phy))
516ff764963SKishon Vijay Abraham I 		return phy;
517ff764963SKishon Vijay Abraham I 
518ff764963SKishon Vijay Abraham I 	if (!try_module_get(phy->ops->owner))
519ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EPROBE_DEFER);
520ff764963SKishon Vijay Abraham I 
521ff764963SKishon Vijay Abraham I 	get_device(&phy->dev);
522ff764963SKishon Vijay Abraham I 
523ff764963SKishon Vijay Abraham I 	return phy;
524ff764963SKishon Vijay Abraham I }
525ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_get);
526ff764963SKishon Vijay Abraham I 
527ff764963SKishon Vijay Abraham I /**
528788a4d56SAndrew Lunn  * phy_optional_get() - lookup and obtain a reference to an optional phy.
529788a4d56SAndrew Lunn  * @dev: device that requests this phy
530788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or the name of the controller
531788a4d56SAndrew Lunn  * port for non-dt case
532788a4d56SAndrew Lunn  *
533788a4d56SAndrew Lunn  * Returns the phy driver, after getting a refcount to it; or
534788a4d56SAndrew Lunn  * NULL if there is no such phy.  The caller is responsible for
535788a4d56SAndrew Lunn  * calling phy_put() to release that count.
536788a4d56SAndrew Lunn  */
537788a4d56SAndrew Lunn struct phy *phy_optional_get(struct device *dev, const char *string)
538788a4d56SAndrew Lunn {
539788a4d56SAndrew Lunn 	struct phy *phy = phy_get(dev, string);
540788a4d56SAndrew Lunn 
541788a4d56SAndrew Lunn 	if (PTR_ERR(phy) == -ENODEV)
542788a4d56SAndrew Lunn 		phy = NULL;
543788a4d56SAndrew Lunn 
544788a4d56SAndrew Lunn 	return phy;
545788a4d56SAndrew Lunn }
546788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(phy_optional_get);
547788a4d56SAndrew Lunn 
548788a4d56SAndrew Lunn /**
549ff764963SKishon Vijay Abraham I  * devm_phy_get() - lookup and obtain a reference to a phy.
550ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
551ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or phy device name
552ff764963SKishon Vijay Abraham I  * for non-dt case
553ff764963SKishon Vijay Abraham I  *
554ff764963SKishon Vijay Abraham I  * Gets the phy using phy_get(), and associates a device with it using
555ff764963SKishon Vijay Abraham I  * devres. On driver detach, release function is invoked on the devres data,
556ff764963SKishon Vijay Abraham I  * then, devres data is freed.
557ff764963SKishon Vijay Abraham I  */
558ff764963SKishon Vijay Abraham I struct phy *devm_phy_get(struct device *dev, const char *string)
559ff764963SKishon Vijay Abraham I {
560ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
561ff764963SKishon Vijay Abraham I 
562ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
563ff764963SKishon Vijay Abraham I 	if (!ptr)
564ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
565ff764963SKishon Vijay Abraham I 
566ff764963SKishon Vijay Abraham I 	phy = phy_get(dev, string);
567ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
568ff764963SKishon Vijay Abraham I 		*ptr = phy;
569ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
570ff764963SKishon Vijay Abraham I 	} else {
571ff764963SKishon Vijay Abraham I 		devres_free(ptr);
572ff764963SKishon Vijay Abraham I 	}
573ff764963SKishon Vijay Abraham I 
574ff764963SKishon Vijay Abraham I 	return phy;
575ff764963SKishon Vijay Abraham I }
576ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_get);
577ff764963SKishon Vijay Abraham I 
578ff764963SKishon Vijay Abraham I /**
579788a4d56SAndrew Lunn  * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
580788a4d56SAndrew Lunn  * @dev: device that requests this phy
581788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or phy device name
582788a4d56SAndrew Lunn  * for non-dt case
583788a4d56SAndrew Lunn  *
584788a4d56SAndrew Lunn  * Gets the phy using phy_get(), and associates a device with it using
585788a4d56SAndrew Lunn  * devres. On driver detach, release function is invoked on the devres
586788a4d56SAndrew Lunn  * data, then, devres data is freed. This differs to devm_phy_get() in
587788a4d56SAndrew Lunn  * that if the phy does not exist, it is not considered an error and
588788a4d56SAndrew Lunn  * -ENODEV will not be returned. Instead the NULL phy is returned,
589788a4d56SAndrew Lunn  * which can be passed to all other phy consumer calls.
590788a4d56SAndrew Lunn  */
591788a4d56SAndrew Lunn struct phy *devm_phy_optional_get(struct device *dev, const char *string)
592788a4d56SAndrew Lunn {
593788a4d56SAndrew Lunn 	struct phy *phy = devm_phy_get(dev, string);
594788a4d56SAndrew Lunn 
595788a4d56SAndrew Lunn 	if (PTR_ERR(phy) == -ENODEV)
596788a4d56SAndrew Lunn 		phy = NULL;
597788a4d56SAndrew Lunn 
598788a4d56SAndrew Lunn 	return phy;
599788a4d56SAndrew Lunn }
600788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(devm_phy_optional_get);
601788a4d56SAndrew Lunn 
602788a4d56SAndrew Lunn /**
603b5d682f4SKamil Debski  * devm_of_phy_get() - lookup and obtain a reference to a phy.
604b5d682f4SKamil Debski  * @dev: device that requests this phy
605b5d682f4SKamil Debski  * @np: node containing the phy
606b5d682f4SKamil Debski  * @con_id: name of the phy from device's point of view
607b5d682f4SKamil Debski  *
608b5d682f4SKamil Debski  * Gets the phy using of_phy_get(), and associates a device with it using
609b5d682f4SKamil Debski  * devres. On driver detach, release function is invoked on the devres data,
610b5d682f4SKamil Debski  * then, devres data is freed.
611b5d682f4SKamil Debski  */
612b5d682f4SKamil Debski struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
613b5d682f4SKamil Debski 			    const char *con_id)
614b5d682f4SKamil Debski {
615b5d682f4SKamil Debski 	struct phy **ptr, *phy;
616b5d682f4SKamil Debski 
617b5d682f4SKamil Debski 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
618b5d682f4SKamil Debski 	if (!ptr)
619b5d682f4SKamil Debski 		return ERR_PTR(-ENOMEM);
620b5d682f4SKamil Debski 
621b5d682f4SKamil Debski 	phy = of_phy_get(np, con_id);
622b5d682f4SKamil Debski 	if (!IS_ERR(phy)) {
623b5d682f4SKamil Debski 		*ptr = phy;
624b5d682f4SKamil Debski 		devres_add(dev, ptr);
625b5d682f4SKamil Debski 	} else {
626b5d682f4SKamil Debski 		devres_free(ptr);
627b5d682f4SKamil Debski 	}
628b5d682f4SKamil Debski 
629b5d682f4SKamil Debski 	return phy;
630b5d682f4SKamil Debski }
631b5d682f4SKamil Debski EXPORT_SYMBOL_GPL(devm_of_phy_get);
632b5d682f4SKamil Debski 
633b5d682f4SKamil Debski /**
6346be109b3SArun Ramamurthy  * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
6356be109b3SArun Ramamurthy  * @dev: device that requests this phy
6366be109b3SArun Ramamurthy  * @np: node containing the phy
6376be109b3SArun Ramamurthy  * @index: index of the phy
6386be109b3SArun Ramamurthy  *
6396be109b3SArun Ramamurthy  * Gets the phy using _of_phy_get(), and associates a device with it using
6406be109b3SArun Ramamurthy  * devres. On driver detach, release function is invoked on the devres data,
6416be109b3SArun Ramamurthy  * then, devres data is freed.
6426be109b3SArun Ramamurthy  *
6436be109b3SArun Ramamurthy  */
6446be109b3SArun Ramamurthy struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
6456be109b3SArun Ramamurthy 				     int index)
6466be109b3SArun Ramamurthy {
6476be109b3SArun Ramamurthy 	struct phy **ptr, *phy;
6486be109b3SArun Ramamurthy 
6496be109b3SArun Ramamurthy 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
6506be109b3SArun Ramamurthy 	if (!ptr)
6516be109b3SArun Ramamurthy 		return ERR_PTR(-ENOMEM);
6526be109b3SArun Ramamurthy 
6536be109b3SArun Ramamurthy 	phy = _of_phy_get(np, index);
6546be109b3SArun Ramamurthy 	if (!IS_ERR(phy)) {
6556be109b3SArun Ramamurthy 		*ptr = phy;
6566be109b3SArun Ramamurthy 		devres_add(dev, ptr);
6576be109b3SArun Ramamurthy 	} else {
6586be109b3SArun Ramamurthy 		devres_free(ptr);
6596be109b3SArun Ramamurthy 	}
6606be109b3SArun Ramamurthy 
6616be109b3SArun Ramamurthy 	return phy;
6626be109b3SArun Ramamurthy }
6636be109b3SArun Ramamurthy EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
6646be109b3SArun Ramamurthy 
6656be109b3SArun Ramamurthy /**
666ff764963SKishon Vijay Abraham I  * phy_create() - create a new phy
667ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
668f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
669ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
670ff764963SKishon Vijay Abraham I  *
671ff764963SKishon Vijay Abraham I  * Called to create a phy using phy framework.
672ff764963SKishon Vijay Abraham I  */
673f0ed8176SKishon Vijay Abraham I struct phy *phy_create(struct device *dev, struct device_node *node,
674dbc98635SHeikki Krogerus 		       const struct phy_ops *ops)
675ff764963SKishon Vijay Abraham I {
676ff764963SKishon Vijay Abraham I 	int ret;
677ff764963SKishon Vijay Abraham I 	int id;
678ff764963SKishon Vijay Abraham I 	struct phy *phy;
679ff764963SKishon Vijay Abraham I 
68052797d29SDan Carpenter 	if (WARN_ON(!dev))
68152797d29SDan Carpenter 		return ERR_PTR(-EINVAL);
682ff764963SKishon Vijay Abraham I 
683ff764963SKishon Vijay Abraham I 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
68452797d29SDan Carpenter 	if (!phy)
68552797d29SDan Carpenter 		return ERR_PTR(-ENOMEM);
686ff764963SKishon Vijay Abraham I 
687ff764963SKishon Vijay Abraham I 	id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
688ff764963SKishon Vijay Abraham I 	if (id < 0) {
689ff764963SKishon Vijay Abraham I 		dev_err(dev, "unable to get id\n");
690ff764963SKishon Vijay Abraham I 		ret = id;
69152797d29SDan Carpenter 		goto free_phy;
692ff764963SKishon Vijay Abraham I 	}
693ff764963SKishon Vijay Abraham I 
694ff764963SKishon Vijay Abraham I 	device_initialize(&phy->dev);
695ff764963SKishon Vijay Abraham I 	mutex_init(&phy->mutex);
696ff764963SKishon Vijay Abraham I 
697ff764963SKishon Vijay Abraham I 	phy->dev.class = phy_class;
698ff764963SKishon Vijay Abraham I 	phy->dev.parent = dev;
699f0ed8176SKishon Vijay Abraham I 	phy->dev.of_node = node ?: dev->of_node;
700ff764963SKishon Vijay Abraham I 	phy->id = id;
701ff764963SKishon Vijay Abraham I 	phy->ops = ops;
702ff764963SKishon Vijay Abraham I 
703ff764963SKishon Vijay Abraham I 	ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
704ff764963SKishon Vijay Abraham I 	if (ret)
70552797d29SDan Carpenter 		goto put_dev;
706ff764963SKishon Vijay Abraham I 
70787006dd6SDmitry Torokhov 	/* phy-supply */
70887006dd6SDmitry Torokhov 	phy->pwr = regulator_get_optional(&phy->dev, "phy");
70987006dd6SDmitry Torokhov 	if (IS_ERR(phy->pwr)) {
71087006dd6SDmitry Torokhov 		ret = PTR_ERR(phy->pwr);
71187006dd6SDmitry Torokhov 		if (ret == -EPROBE_DEFER)
71287006dd6SDmitry Torokhov 			goto put_dev;
71387006dd6SDmitry Torokhov 
71487006dd6SDmitry Torokhov 		phy->pwr = NULL;
71587006dd6SDmitry Torokhov 	}
71687006dd6SDmitry Torokhov 
717ff764963SKishon Vijay Abraham I 	ret = device_add(&phy->dev);
718ff764963SKishon Vijay Abraham I 	if (ret)
71952797d29SDan Carpenter 		goto put_dev;
720ff764963SKishon Vijay Abraham I 
721ff764963SKishon Vijay Abraham I 	if (pm_runtime_enabled(dev)) {
722ff764963SKishon Vijay Abraham I 		pm_runtime_enable(&phy->dev);
723ff764963SKishon Vijay Abraham I 		pm_runtime_no_callbacks(&phy->dev);
724ff764963SKishon Vijay Abraham I 	}
725ff764963SKishon Vijay Abraham I 
726ff764963SKishon Vijay Abraham I 	return phy;
727ff764963SKishon Vijay Abraham I 
72852797d29SDan Carpenter put_dev:
729e73b49f1SRoger Quadros 	put_device(&phy->dev);  /* calls phy_release() which frees resources */
730e73b49f1SRoger Quadros 	return ERR_PTR(ret);
731e73b49f1SRoger Quadros 
73252797d29SDan Carpenter free_phy:
733ff764963SKishon Vijay Abraham I 	kfree(phy);
734ff764963SKishon Vijay Abraham I 	return ERR_PTR(ret);
735ff764963SKishon Vijay Abraham I }
736ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_create);
737ff764963SKishon Vijay Abraham I 
738ff764963SKishon Vijay Abraham I /**
739ff764963SKishon Vijay Abraham I  * devm_phy_create() - create a new phy
740ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
741f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
742ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
743ff764963SKishon Vijay Abraham I  *
744ff764963SKishon Vijay Abraham I  * Creates a new PHY device adding it to the PHY class.
745ff764963SKishon Vijay Abraham I  * While at that, it also associates the device with the phy using devres.
746ff764963SKishon Vijay Abraham I  * On driver detach, release function is invoked on the devres data,
747ff764963SKishon Vijay Abraham I  * then, devres data is freed.
748ff764963SKishon Vijay Abraham I  */
749f0ed8176SKishon Vijay Abraham I struct phy *devm_phy_create(struct device *dev, struct device_node *node,
750dbc98635SHeikki Krogerus 			    const struct phy_ops *ops)
751ff764963SKishon Vijay Abraham I {
752ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
753ff764963SKishon Vijay Abraham I 
754ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
755ff764963SKishon Vijay Abraham I 	if (!ptr)
756ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
757ff764963SKishon Vijay Abraham I 
758dbc98635SHeikki Krogerus 	phy = phy_create(dev, node, ops);
759ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
760ff764963SKishon Vijay Abraham I 		*ptr = phy;
761ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
762ff764963SKishon Vijay Abraham I 	} else {
763ff764963SKishon Vijay Abraham I 		devres_free(ptr);
764ff764963SKishon Vijay Abraham I 	}
765ff764963SKishon Vijay Abraham I 
766ff764963SKishon Vijay Abraham I 	return phy;
767ff764963SKishon Vijay Abraham I }
768ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_create);
769ff764963SKishon Vijay Abraham I 
770ff764963SKishon Vijay Abraham I /**
771ff764963SKishon Vijay Abraham I  * phy_destroy() - destroy the phy
772ff764963SKishon Vijay Abraham I  * @phy: the phy to be destroyed
773ff764963SKishon Vijay Abraham I  *
774ff764963SKishon Vijay Abraham I  * Called to destroy the phy.
775ff764963SKishon Vijay Abraham I  */
776ff764963SKishon Vijay Abraham I void phy_destroy(struct phy *phy)
777ff764963SKishon Vijay Abraham I {
778ff764963SKishon Vijay Abraham I 	pm_runtime_disable(&phy->dev);
779ff764963SKishon Vijay Abraham I 	device_unregister(&phy->dev);
780ff764963SKishon Vijay Abraham I }
781ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_destroy);
782ff764963SKishon Vijay Abraham I 
783ff764963SKishon Vijay Abraham I /**
784ff764963SKishon Vijay Abraham I  * devm_phy_destroy() - destroy the PHY
785ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
786ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
787ff764963SKishon Vijay Abraham I  *
788ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_destroy
789ff764963SKishon Vijay Abraham I  * to destroy the phy.
790ff764963SKishon Vijay Abraham I  */
791ff764963SKishon Vijay Abraham I void devm_phy_destroy(struct device *dev, struct phy *phy)
792ff764963SKishon Vijay Abraham I {
793ff764963SKishon Vijay Abraham I 	int r;
794ff764963SKishon Vijay Abraham I 
795ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
796ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
797ff764963SKishon Vijay Abraham I }
798ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_destroy);
799ff764963SKishon Vijay Abraham I 
800ff764963SKishon Vijay Abraham I /**
801ff764963SKishon Vijay Abraham I  * __of_phy_provider_register() - create/register phy provider with the framework
802ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
803ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
804ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
805ff764963SKishon Vijay Abraham I  *
806ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
807ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
808ff764963SKishon Vijay Abraham I  * phy provider.
809ff764963SKishon Vijay Abraham I  */
810ff764963SKishon Vijay Abraham I struct phy_provider *__of_phy_provider_register(struct device *dev,
811ff764963SKishon Vijay Abraham I 	struct module *owner, struct phy * (*of_xlate)(struct device *dev,
812ff764963SKishon Vijay Abraham I 	struct of_phandle_args *args))
813ff764963SKishon Vijay Abraham I {
814ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
815ff764963SKishon Vijay Abraham I 
816ff764963SKishon Vijay Abraham I 	phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
817ff764963SKishon Vijay Abraham I 	if (!phy_provider)
818ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
819ff764963SKishon Vijay Abraham I 
820ff764963SKishon Vijay Abraham I 	phy_provider->dev = dev;
821ff764963SKishon Vijay Abraham I 	phy_provider->owner = owner;
822ff764963SKishon Vijay Abraham I 	phy_provider->of_xlate = of_xlate;
823ff764963SKishon Vijay Abraham I 
824ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
825ff764963SKishon Vijay Abraham I 	list_add_tail(&phy_provider->list, &phy_provider_list);
826ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
827ff764963SKishon Vijay Abraham I 
828ff764963SKishon Vijay Abraham I 	return phy_provider;
829ff764963SKishon Vijay Abraham I }
830ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__of_phy_provider_register);
831ff764963SKishon Vijay Abraham I 
832ff764963SKishon Vijay Abraham I /**
833ff764963SKishon Vijay Abraham I  * __devm_of_phy_provider_register() - create/register phy provider with the
834ff764963SKishon Vijay Abraham I  * framework
835ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
836ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
837ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
838ff764963SKishon Vijay Abraham I  *
839ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
840ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
841ff764963SKishon Vijay Abraham I  * phy provider. While at that, it also associates the device with the
842ff764963SKishon Vijay Abraham I  * phy provider using devres. On driver detach, release function is invoked
843ff764963SKishon Vijay Abraham I  * on the devres data, then, devres data is freed.
844ff764963SKishon Vijay Abraham I  */
845ff764963SKishon Vijay Abraham I struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
846ff764963SKishon Vijay Abraham I 	struct module *owner, struct phy * (*of_xlate)(struct device *dev,
847ff764963SKishon Vijay Abraham I 	struct of_phandle_args *args))
848ff764963SKishon Vijay Abraham I {
849ff764963SKishon Vijay Abraham I 	struct phy_provider **ptr, *phy_provider;
850ff764963SKishon Vijay Abraham I 
851ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
852ff764963SKishon Vijay Abraham I 	if (!ptr)
853ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
854ff764963SKishon Vijay Abraham I 
855ff764963SKishon Vijay Abraham I 	phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
856ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy_provider)) {
857ff764963SKishon Vijay Abraham I 		*ptr = phy_provider;
858ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
859ff764963SKishon Vijay Abraham I 	} else {
860ff764963SKishon Vijay Abraham I 		devres_free(ptr);
861ff764963SKishon Vijay Abraham I 	}
862ff764963SKishon Vijay Abraham I 
863ff764963SKishon Vijay Abraham I 	return phy_provider;
864ff764963SKishon Vijay Abraham I }
865ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
866ff764963SKishon Vijay Abraham I 
867ff764963SKishon Vijay Abraham I /**
868ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister() - unregister phy provider from the framework
869ff764963SKishon Vijay Abraham I  * @phy_provider: phy provider returned by of_phy_provider_register()
870ff764963SKishon Vijay Abraham I  *
871ff764963SKishon Vijay Abraham I  * Removes the phy_provider created using of_phy_provider_register().
872ff764963SKishon Vijay Abraham I  */
873ff764963SKishon Vijay Abraham I void of_phy_provider_unregister(struct phy_provider *phy_provider)
874ff764963SKishon Vijay Abraham I {
875ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider))
876ff764963SKishon Vijay Abraham I 		return;
877ff764963SKishon Vijay Abraham I 
878ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
879ff764963SKishon Vijay Abraham I 	list_del(&phy_provider->list);
880ff764963SKishon Vijay Abraham I 	kfree(phy_provider);
881ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
882ff764963SKishon Vijay Abraham I }
883ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
884ff764963SKishon Vijay Abraham I 
885ff764963SKishon Vijay Abraham I /**
886ff764963SKishon Vijay Abraham I  * devm_of_phy_provider_unregister() - remove phy provider from the framework
887ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
888ff764963SKishon Vijay Abraham I  *
889ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy provider and invokes
890ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister to unregister the phy provider.
891ff764963SKishon Vijay Abraham I  */
892ff764963SKishon Vijay Abraham I void devm_of_phy_provider_unregister(struct device *dev,
893ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider) {
894ff764963SKishon Vijay Abraham I 	int r;
895ff764963SKishon Vijay Abraham I 
896ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
897ff764963SKishon Vijay Abraham I 		phy_provider);
898ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
899ff764963SKishon Vijay Abraham I }
900ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
901ff764963SKishon Vijay Abraham I 
902ff764963SKishon Vijay Abraham I /**
903ff764963SKishon Vijay Abraham I  * phy_release() - release the phy
904ff764963SKishon Vijay Abraham I  * @dev: the dev member within phy
905ff764963SKishon Vijay Abraham I  *
906ff764963SKishon Vijay Abraham I  * When the last reference to the device is removed, it is called
907ff764963SKishon Vijay Abraham I  * from the embedded kobject as release method.
908ff764963SKishon Vijay Abraham I  */
909ff764963SKishon Vijay Abraham I static void phy_release(struct device *dev)
910ff764963SKishon Vijay Abraham I {
911ff764963SKishon Vijay Abraham I 	struct phy *phy;
912ff764963SKishon Vijay Abraham I 
913ff764963SKishon Vijay Abraham I 	phy = to_phy(dev);
914ff764963SKishon Vijay Abraham I 	dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
9153be88125SRoger Quadros 	regulator_put(phy->pwr);
916e73b49f1SRoger Quadros 	ida_simple_remove(&phy_ida, phy->id);
917ff764963SKishon Vijay Abraham I 	kfree(phy);
918ff764963SKishon Vijay Abraham I }
919ff764963SKishon Vijay Abraham I 
920ff764963SKishon Vijay Abraham I static int __init phy_core_init(void)
921ff764963SKishon Vijay Abraham I {
922ff764963SKishon Vijay Abraham I 	phy_class = class_create(THIS_MODULE, "phy");
923ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_class)) {
924ff764963SKishon Vijay Abraham I 		pr_err("failed to create phy class --> %ld\n",
925ff764963SKishon Vijay Abraham I 			PTR_ERR(phy_class));
926ff764963SKishon Vijay Abraham I 		return PTR_ERR(phy_class);
927ff764963SKishon Vijay Abraham I 	}
928ff764963SKishon Vijay Abraham I 
929ff764963SKishon Vijay Abraham I 	phy_class->dev_release = phy_release;
930ff764963SKishon Vijay Abraham I 
931ff764963SKishon Vijay Abraham I 	return 0;
932ff764963SKishon Vijay Abraham I }
933ff764963SKishon Vijay Abraham I module_init(phy_core_init);
934ff764963SKishon Vijay Abraham I 
935ff764963SKishon Vijay Abraham I static void __exit phy_core_exit(void)
936ff764963SKishon Vijay Abraham I {
937ff764963SKishon Vijay Abraham I 	class_destroy(phy_class);
938ff764963SKishon Vijay Abraham I }
939ff764963SKishon Vijay Abraham I module_exit(phy_core_exit);
940ff764963SKishon Vijay Abraham I 
941ff764963SKishon Vijay Abraham I MODULE_DESCRIPTION("Generic PHY Framework");
942ff764963SKishon Vijay Abraham I MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
943ff764963SKishon Vijay Abraham I MODULE_LICENSE("GPL v2");
944