xref: /openbmc/linux/drivers/phy/phy-core.c (revision 987351e1ea7772cf2f0795e917fb33b2e282e1c1)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ff764963SKishon Vijay Abraham I /*
3ff764963SKishon Vijay Abraham I  * phy-core.c  --  Generic Phy framework.
4ff764963SKishon Vijay Abraham I  *
5ff764963SKishon Vijay Abraham I  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6ff764963SKishon Vijay Abraham I  *
7ff764963SKishon Vijay Abraham I  * Author: Kishon Vijay Abraham I <kishon@ti.com>
8ff764963SKishon Vijay Abraham I  */
9ff764963SKishon Vijay Abraham I 
10ff764963SKishon Vijay Abraham I #include <linux/kernel.h>
11ff764963SKishon Vijay Abraham I #include <linux/export.h>
12ff764963SKishon Vijay Abraham I #include <linux/module.h>
13ff764963SKishon Vijay Abraham I #include <linux/err.h>
14ff764963SKishon Vijay Abraham I #include <linux/device.h>
15ff764963SKishon Vijay Abraham I #include <linux/slab.h>
16ff764963SKishon Vijay Abraham I #include <linux/of.h>
17ff764963SKishon Vijay Abraham I #include <linux/phy/phy.h>
18ff764963SKishon Vijay Abraham I #include <linux/idr.h>
19ff764963SKishon Vijay Abraham I #include <linux/pm_runtime.h>
203be88125SRoger Quadros #include <linux/regulator/consumer.h>
21ff764963SKishon Vijay Abraham I 
22ff764963SKishon Vijay Abraham I static struct class *phy_class;
23ff764963SKishon Vijay Abraham I static DEFINE_MUTEX(phy_provider_mutex);
24ff764963SKishon Vijay Abraham I static LIST_HEAD(phy_provider_list);
25b7bc15b9SHeikki Krogerus static LIST_HEAD(phys);
26ff764963SKishon Vijay Abraham I static DEFINE_IDA(phy_ida);
27ff764963SKishon Vijay Abraham I 
28ff764963SKishon Vijay Abraham I static void devm_phy_release(struct device *dev, void *res)
29ff764963SKishon Vijay Abraham I {
30ff764963SKishon Vijay Abraham I 	struct phy *phy = *(struct phy **)res;
31ff764963SKishon Vijay Abraham I 
32*987351e1SAlexandre Torgue 	phy_put(dev, phy);
33ff764963SKishon Vijay Abraham I }
34ff764963SKishon Vijay Abraham I 
35ff764963SKishon Vijay Abraham I static void devm_phy_provider_release(struct device *dev, void *res)
36ff764963SKishon Vijay Abraham I {
37ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider = *(struct phy_provider **)res;
38ff764963SKishon Vijay Abraham I 
39ff764963SKishon Vijay Abraham I 	of_phy_provider_unregister(phy_provider);
40ff764963SKishon Vijay Abraham I }
41ff764963SKishon Vijay Abraham I 
42ff764963SKishon Vijay Abraham I static void devm_phy_consume(struct device *dev, void *res)
43ff764963SKishon Vijay Abraham I {
44ff764963SKishon Vijay Abraham I 	struct phy *phy = *(struct phy **)res;
45ff764963SKishon Vijay Abraham I 
46ff764963SKishon Vijay Abraham I 	phy_destroy(phy);
47ff764963SKishon Vijay Abraham I }
48ff764963SKishon Vijay Abraham I 
49ff764963SKishon Vijay Abraham I static int devm_phy_match(struct device *dev, void *res, void *match_data)
50ff764963SKishon Vijay Abraham I {
512f1bce48SThierry Reding 	struct phy **phy = res;
522f1bce48SThierry Reding 
532f1bce48SThierry Reding 	return *phy == match_data;
54ff764963SKishon Vijay Abraham I }
55ff764963SKishon Vijay Abraham I 
56b7bc15b9SHeikki Krogerus /**
57b7bc15b9SHeikki Krogerus  * phy_create_lookup() - allocate and register PHY/device association
58b7bc15b9SHeikki Krogerus  * @phy: the phy of the association
59b7bc15b9SHeikki Krogerus  * @con_id: connection ID string on device
60b7bc15b9SHeikki Krogerus  * @dev_id: the device of the association
61b7bc15b9SHeikki Krogerus  *
62b7bc15b9SHeikki Krogerus  * Creates and registers phy_lookup entry.
63b7bc15b9SHeikki Krogerus  */
64b7bc15b9SHeikki Krogerus int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
65b7bc15b9SHeikki Krogerus {
66b7bc15b9SHeikki Krogerus 	struct phy_lookup *pl;
67b7bc15b9SHeikki Krogerus 
68b7bc15b9SHeikki Krogerus 	if (!phy || !dev_id || !con_id)
69b7bc15b9SHeikki Krogerus 		return -EINVAL;
70b7bc15b9SHeikki Krogerus 
71b7bc15b9SHeikki Krogerus 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
72b7bc15b9SHeikki Krogerus 	if (!pl)
73b7bc15b9SHeikki Krogerus 		return -ENOMEM;
74b7bc15b9SHeikki Krogerus 
75b7bc15b9SHeikki Krogerus 	pl->dev_id = dev_id;
76b7bc15b9SHeikki Krogerus 	pl->con_id = con_id;
77b7bc15b9SHeikki Krogerus 	pl->phy = phy;
78b7bc15b9SHeikki Krogerus 
79b7bc15b9SHeikki Krogerus 	mutex_lock(&phy_provider_mutex);
80b7bc15b9SHeikki Krogerus 	list_add_tail(&pl->node, &phys);
81b7bc15b9SHeikki Krogerus 	mutex_unlock(&phy_provider_mutex);
82b7bc15b9SHeikki Krogerus 
83b7bc15b9SHeikki Krogerus 	return 0;
84b7bc15b9SHeikki Krogerus }
85b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_create_lookup);
86b7bc15b9SHeikki Krogerus 
87b7bc15b9SHeikki Krogerus /**
88b7bc15b9SHeikki Krogerus  * phy_remove_lookup() - find and remove PHY/device association
89b7bc15b9SHeikki Krogerus  * @phy: the phy of the association
90b7bc15b9SHeikki Krogerus  * @con_id: connection ID string on device
91b7bc15b9SHeikki Krogerus  * @dev_id: the device of the association
92b7bc15b9SHeikki Krogerus  *
93b7bc15b9SHeikki Krogerus  * Finds and unregisters phy_lookup entry that was created with
94b7bc15b9SHeikki Krogerus  * phy_create_lookup().
95b7bc15b9SHeikki Krogerus  */
96b7bc15b9SHeikki Krogerus void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
97b7bc15b9SHeikki Krogerus {
98b7bc15b9SHeikki Krogerus 	struct phy_lookup *pl;
99b7bc15b9SHeikki Krogerus 
100b7bc15b9SHeikki Krogerus 	if (!phy || !dev_id || !con_id)
101b7bc15b9SHeikki Krogerus 		return;
102b7bc15b9SHeikki Krogerus 
103b7bc15b9SHeikki Krogerus 	mutex_lock(&phy_provider_mutex);
104b7bc15b9SHeikki Krogerus 	list_for_each_entry(pl, &phys, node)
105b7bc15b9SHeikki Krogerus 		if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
106b7bc15b9SHeikki Krogerus 		    !strcmp(pl->con_id, con_id)) {
107b7bc15b9SHeikki Krogerus 			list_del(&pl->node);
108b7bc15b9SHeikki Krogerus 			kfree(pl);
109b7bc15b9SHeikki Krogerus 			break;
110b7bc15b9SHeikki Krogerus 		}
111b7bc15b9SHeikki Krogerus 	mutex_unlock(&phy_provider_mutex);
112b7bc15b9SHeikki Krogerus }
113b7bc15b9SHeikki Krogerus EXPORT_SYMBOL_GPL(phy_remove_lookup);
114b7bc15b9SHeikki Krogerus 
115b7bc15b9SHeikki Krogerus static struct phy *phy_find(struct device *dev, const char *con_id)
116b7bc15b9SHeikki Krogerus {
117b7bc15b9SHeikki Krogerus 	const char *dev_id = dev_name(dev);
118b7bc15b9SHeikki Krogerus 	struct phy_lookup *p, *pl = NULL;
119b7bc15b9SHeikki Krogerus 
120b7bc15b9SHeikki Krogerus 	mutex_lock(&phy_provider_mutex);
121b7bc15b9SHeikki Krogerus 	list_for_each_entry(p, &phys, node)
122b7bc15b9SHeikki Krogerus 		if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
123b7bc15b9SHeikki Krogerus 			pl = p;
124b7bc15b9SHeikki Krogerus 			break;
125b7bc15b9SHeikki Krogerus 		}
126b7bc15b9SHeikki Krogerus 	mutex_unlock(&phy_provider_mutex);
127b7bc15b9SHeikki Krogerus 
128dbc98635SHeikki Krogerus 	return pl ? pl->phy : ERR_PTR(-ENODEV);
129b7bc15b9SHeikki Krogerus }
130b7bc15b9SHeikki Krogerus 
131ff764963SKishon Vijay Abraham I static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
132ff764963SKishon Vijay Abraham I {
133ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
1342a4c3701SKishon Vijay Abraham I 	struct device_node *child;
135ff764963SKishon Vijay Abraham I 
136ff764963SKishon Vijay Abraham I 	list_for_each_entry(phy_provider, &phy_provider_list, list) {
137ff764963SKishon Vijay Abraham I 		if (phy_provider->dev->of_node == node)
138ff764963SKishon Vijay Abraham I 			return phy_provider;
1392a4c3701SKishon Vijay Abraham I 
1401140f7c8SThierry Reding 		for_each_child_of_node(phy_provider->children, child)
1412a4c3701SKishon Vijay Abraham I 			if (child == node)
1422a4c3701SKishon Vijay Abraham I 				return phy_provider;
143ff764963SKishon Vijay Abraham I 	}
144ff764963SKishon Vijay Abraham I 
145ff764963SKishon Vijay Abraham I 	return ERR_PTR(-EPROBE_DEFER);
146ff764963SKishon Vijay Abraham I }
147ff764963SKishon Vijay Abraham I 
148ff764963SKishon Vijay Abraham I int phy_pm_runtime_get(struct phy *phy)
149ff764963SKishon Vijay Abraham I {
150cedb7f89SFelipe Balbi 	int ret;
151cedb7f89SFelipe Balbi 
1528866df25SManu Gautam 	if (!phy)
1538866df25SManu Gautam 		return 0;
1548866df25SManu Gautam 
155ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
156ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
157ff764963SKishon Vijay Abraham I 
158cedb7f89SFelipe Balbi 	ret = pm_runtime_get(&phy->dev);
159cedb7f89SFelipe Balbi 	if (ret < 0 && ret != -EINPROGRESS)
160cedb7f89SFelipe Balbi 		pm_runtime_put_noidle(&phy->dev);
161cedb7f89SFelipe Balbi 
162cedb7f89SFelipe Balbi 	return ret;
163ff764963SKishon Vijay Abraham I }
164ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
165ff764963SKishon Vijay Abraham I 
166ff764963SKishon Vijay Abraham I int phy_pm_runtime_get_sync(struct phy *phy)
167ff764963SKishon Vijay Abraham I {
168cedb7f89SFelipe Balbi 	int ret;
169cedb7f89SFelipe Balbi 
1708866df25SManu Gautam 	if (!phy)
1718866df25SManu Gautam 		return 0;
1728866df25SManu Gautam 
173ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
174ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
175ff764963SKishon Vijay Abraham I 
176cedb7f89SFelipe Balbi 	ret = pm_runtime_get_sync(&phy->dev);
177cedb7f89SFelipe Balbi 	if (ret < 0)
178cedb7f89SFelipe Balbi 		pm_runtime_put_sync(&phy->dev);
179cedb7f89SFelipe Balbi 
180cedb7f89SFelipe Balbi 	return ret;
181ff764963SKishon Vijay Abraham I }
182ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
183ff764963SKishon Vijay Abraham I 
184ff764963SKishon Vijay Abraham I int phy_pm_runtime_put(struct phy *phy)
185ff764963SKishon Vijay Abraham I {
1868866df25SManu Gautam 	if (!phy)
1878866df25SManu Gautam 		return 0;
1888866df25SManu Gautam 
189ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
190ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
191ff764963SKishon Vijay Abraham I 
192ff764963SKishon Vijay Abraham I 	return pm_runtime_put(&phy->dev);
193ff764963SKishon Vijay Abraham I }
194ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
195ff764963SKishon Vijay Abraham I 
196ff764963SKishon Vijay Abraham I int phy_pm_runtime_put_sync(struct phy *phy)
197ff764963SKishon Vijay Abraham I {
1988866df25SManu Gautam 	if (!phy)
1998866df25SManu Gautam 		return 0;
2008866df25SManu Gautam 
201ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
202ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
203ff764963SKishon Vijay Abraham I 
204ff764963SKishon Vijay Abraham I 	return pm_runtime_put_sync(&phy->dev);
205ff764963SKishon Vijay Abraham I }
206ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
207ff764963SKishon Vijay Abraham I 
208ff764963SKishon Vijay Abraham I void phy_pm_runtime_allow(struct phy *phy)
209ff764963SKishon Vijay Abraham I {
2108866df25SManu Gautam 	if (!phy)
2118866df25SManu Gautam 		return;
2128866df25SManu Gautam 
213ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
214ff764963SKishon Vijay Abraham I 		return;
215ff764963SKishon Vijay Abraham I 
216ff764963SKishon Vijay Abraham I 	pm_runtime_allow(&phy->dev);
217ff764963SKishon Vijay Abraham I }
218ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
219ff764963SKishon Vijay Abraham I 
220ff764963SKishon Vijay Abraham I void phy_pm_runtime_forbid(struct phy *phy)
221ff764963SKishon Vijay Abraham I {
2228866df25SManu Gautam 	if (!phy)
2238866df25SManu Gautam 		return;
2248866df25SManu Gautam 
225ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
226ff764963SKishon Vijay Abraham I 		return;
227ff764963SKishon Vijay Abraham I 
228ff764963SKishon Vijay Abraham I 	pm_runtime_forbid(&phy->dev);
229ff764963SKishon Vijay Abraham I }
230ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
231ff764963SKishon Vijay Abraham I 
232ff764963SKishon Vijay Abraham I int phy_init(struct phy *phy)
233ff764963SKishon Vijay Abraham I {
234ff764963SKishon Vijay Abraham I 	int ret;
235ff764963SKishon Vijay Abraham I 
23604c2facaSAndrew Lunn 	if (!phy)
23704c2facaSAndrew Lunn 		return 0;
23804c2facaSAndrew Lunn 
239ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
240ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
241ff764963SKishon Vijay Abraham I 		return ret;
242736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
243ff764963SKishon Vijay Abraham I 
244ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
245637d378cSKishon Vijay Abraham I 	if (phy->init_count == 0 && phy->ops->init) {
246ff764963SKishon Vijay Abraham I 		ret = phy->ops->init(phy);
247ff764963SKishon Vijay Abraham I 		if (ret < 0) {
248ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy init failed --> %d\n", ret);
249ff764963SKishon Vijay Abraham I 			goto out;
250ff764963SKishon Vijay Abraham I 		}
251ff764963SKishon Vijay Abraham I 	}
252637d378cSKishon Vijay Abraham I 	++phy->init_count;
253ff764963SKishon Vijay Abraham I 
254ff764963SKishon Vijay Abraham I out:
255ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
256ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
257ff764963SKishon Vijay Abraham I 	return ret;
258ff764963SKishon Vijay Abraham I }
259ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_init);
260ff764963SKishon Vijay Abraham I 
261ff764963SKishon Vijay Abraham I int phy_exit(struct phy *phy)
262ff764963SKishon Vijay Abraham I {
263ff764963SKishon Vijay Abraham I 	int ret;
264ff764963SKishon Vijay Abraham I 
26504c2facaSAndrew Lunn 	if (!phy)
26604c2facaSAndrew Lunn 		return 0;
26704c2facaSAndrew Lunn 
268ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
269ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
270ff764963SKishon Vijay Abraham I 		return ret;
271736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
272ff764963SKishon Vijay Abraham I 
273ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
274637d378cSKishon Vijay Abraham I 	if (phy->init_count == 1 && phy->ops->exit) {
275ff764963SKishon Vijay Abraham I 		ret = phy->ops->exit(phy);
276ff764963SKishon Vijay Abraham I 		if (ret < 0) {
277ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
278ff764963SKishon Vijay Abraham I 			goto out;
279ff764963SKishon Vijay Abraham I 		}
280ff764963SKishon Vijay Abraham I 	}
281637d378cSKishon Vijay Abraham I 	--phy->init_count;
282ff764963SKishon Vijay Abraham I 
283ff764963SKishon Vijay Abraham I out:
284ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
285ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
286ff764963SKishon Vijay Abraham I 	return ret;
287ff764963SKishon Vijay Abraham I }
288ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_exit);
289ff764963SKishon Vijay Abraham I 
290ff764963SKishon Vijay Abraham I int phy_power_on(struct phy *phy)
291ff764963SKishon Vijay Abraham I {
292b82fcabeSShawn Lin 	int ret = 0;
293ff764963SKishon Vijay Abraham I 
29404c2facaSAndrew Lunn 	if (!phy)
295b82fcabeSShawn Lin 		goto out;
29604c2facaSAndrew Lunn 
2973be88125SRoger Quadros 	if (phy->pwr) {
2983be88125SRoger Quadros 		ret = regulator_enable(phy->pwr);
2993be88125SRoger Quadros 		if (ret)
300b82fcabeSShawn Lin 			goto out;
3013be88125SRoger Quadros 	}
3023be88125SRoger Quadros 
303ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
304ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
305b82fcabeSShawn Lin 		goto err_pm_sync;
306b82fcabeSShawn Lin 
307736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
308ff764963SKishon Vijay Abraham I 
309ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
310637d378cSKishon Vijay Abraham I 	if (phy->power_count == 0 && phy->ops->power_on) {
311ff764963SKishon Vijay Abraham I 		ret = phy->ops->power_on(phy);
312ff764963SKishon Vijay Abraham I 		if (ret < 0) {
313ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
314b82fcabeSShawn Lin 			goto err_pwr_on;
315ff764963SKishon Vijay Abraham I 		}
316ff764963SKishon Vijay Abraham I 	}
317637d378cSKishon Vijay Abraham I 	++phy->power_count;
318637d378cSKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
319637d378cSKishon Vijay Abraham I 	return 0;
320ff764963SKishon Vijay Abraham I 
321b82fcabeSShawn Lin err_pwr_on:
322ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
323637d378cSKishon Vijay Abraham I 	phy_pm_runtime_put_sync(phy);
324b82fcabeSShawn Lin err_pm_sync:
3253be88125SRoger Quadros 	if (phy->pwr)
3263be88125SRoger Quadros 		regulator_disable(phy->pwr);
327b82fcabeSShawn Lin out:
328ff764963SKishon Vijay Abraham I 	return ret;
329ff764963SKishon Vijay Abraham I }
330ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_on);
331ff764963SKishon Vijay Abraham I 
332ff764963SKishon Vijay Abraham I int phy_power_off(struct phy *phy)
333ff764963SKishon Vijay Abraham I {
334d18c9604SKishon Vijay Abraham I 	int ret;
335ff764963SKishon Vijay Abraham I 
33604c2facaSAndrew Lunn 	if (!phy)
33704c2facaSAndrew Lunn 		return 0;
33804c2facaSAndrew Lunn 
339ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
340637d378cSKishon Vijay Abraham I 	if (phy->power_count == 1 && phy->ops->power_off) {
341ff764963SKishon Vijay Abraham I 		ret =  phy->ops->power_off(phy);
342ff764963SKishon Vijay Abraham I 		if (ret < 0) {
343ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
344637d378cSKishon Vijay Abraham I 			mutex_unlock(&phy->mutex);
345637d378cSKishon Vijay Abraham I 			return ret;
346ff764963SKishon Vijay Abraham I 		}
347ff764963SKishon Vijay Abraham I 	}
348637d378cSKishon Vijay Abraham I 	--phy->power_count;
349ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
350ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
351ff764963SKishon Vijay Abraham I 
3523be88125SRoger Quadros 	if (phy->pwr)
3533be88125SRoger Quadros 		regulator_disable(phy->pwr);
3543be88125SRoger Quadros 
355637d378cSKishon Vijay Abraham I 	return 0;
356ff764963SKishon Vijay Abraham I }
357ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_off);
358ff764963SKishon Vijay Abraham I 
35979a5a18aSGrygorii Strashko int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
360300eb013SDavid Lechner {
361300eb013SDavid Lechner 	int ret;
362300eb013SDavid Lechner 
363300eb013SDavid Lechner 	if (!phy || !phy->ops->set_mode)
364300eb013SDavid Lechner 		return 0;
365300eb013SDavid Lechner 
366300eb013SDavid Lechner 	mutex_lock(&phy->mutex);
36779a5a18aSGrygorii Strashko 	ret = phy->ops->set_mode(phy, mode, submode);
3683b3cd24aSManu Gautam 	if (!ret)
3693b3cd24aSManu Gautam 		phy->attrs.mode = mode;
370300eb013SDavid Lechner 	mutex_unlock(&phy->mutex);
371300eb013SDavid Lechner 
372300eb013SDavid Lechner 	return ret;
373300eb013SDavid Lechner }
37479a5a18aSGrygorii Strashko EXPORT_SYMBOL_GPL(phy_set_mode_ext);
375300eb013SDavid Lechner 
376cac18ecbSRandy Li int phy_reset(struct phy *phy)
377cac18ecbSRandy Li {
378cac18ecbSRandy Li 	int ret;
379cac18ecbSRandy Li 
380cac18ecbSRandy Li 	if (!phy || !phy->ops->reset)
381cac18ecbSRandy Li 		return 0;
382cac18ecbSRandy Li 
3834df614c4SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
3844df614c4SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
3854df614c4SKishon Vijay Abraham I 		return ret;
3864df614c4SKishon Vijay Abraham I 
387cac18ecbSRandy Li 	mutex_lock(&phy->mutex);
388cac18ecbSRandy Li 	ret = phy->ops->reset(phy);
389cac18ecbSRandy Li 	mutex_unlock(&phy->mutex);
390cac18ecbSRandy Li 
3914df614c4SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
3924df614c4SKishon Vijay Abraham I 
393cac18ecbSRandy Li 	return ret;
394cac18ecbSRandy Li }
395cac18ecbSRandy Li EXPORT_SYMBOL_GPL(phy_reset);
396cac18ecbSRandy Li 
397bbae18f0SMarek Szyprowski /**
398bbae18f0SMarek Szyprowski  * phy_calibrate() - Tunes the phy hw parameters for current configuration
399bbae18f0SMarek Szyprowski  * @phy: the phy returned by phy_get()
400bbae18f0SMarek Szyprowski  *
401bbae18f0SMarek Szyprowski  * Used to calibrate phy hardware, typically by adjusting some parameters in
402bbae18f0SMarek Szyprowski  * runtime, which are otherwise lost after host controller reset and cannot
403bbae18f0SMarek Szyprowski  * be applied in phy_init() or phy_power_on().
404bbae18f0SMarek Szyprowski  *
405bbae18f0SMarek Szyprowski  * Returns: 0 if successful, an negative error code otherwise
406bbae18f0SMarek Szyprowski  */
40736914111SAndrzej Pietrasiewicz int phy_calibrate(struct phy *phy)
40836914111SAndrzej Pietrasiewicz {
40936914111SAndrzej Pietrasiewicz 	int ret;
41036914111SAndrzej Pietrasiewicz 
41136914111SAndrzej Pietrasiewicz 	if (!phy || !phy->ops->calibrate)
41236914111SAndrzej Pietrasiewicz 		return 0;
41336914111SAndrzej Pietrasiewicz 
41436914111SAndrzej Pietrasiewicz 	mutex_lock(&phy->mutex);
41536914111SAndrzej Pietrasiewicz 	ret = phy->ops->calibrate(phy);
41636914111SAndrzej Pietrasiewicz 	mutex_unlock(&phy->mutex);
41736914111SAndrzej Pietrasiewicz 
41836914111SAndrzej Pietrasiewicz 	return ret;
41936914111SAndrzej Pietrasiewicz }
42036914111SAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(phy_calibrate);
42136914111SAndrzej Pietrasiewicz 
422ff764963SKishon Vijay Abraham I /**
423aeaac93dSMaxime Ripard  * phy_configure() - Changes the phy parameters
424aeaac93dSMaxime Ripard  * @phy: the phy returned by phy_get()
425aeaac93dSMaxime Ripard  * @opts: New configuration to apply
426aeaac93dSMaxime Ripard  *
427aeaac93dSMaxime Ripard  * Used to change the PHY parameters. phy_init() must have been called
428aeaac93dSMaxime Ripard  * on the phy. The configuration will be applied on the current phy
429aeaac93dSMaxime Ripard  * mode, that can be changed using phy_set_mode().
430aeaac93dSMaxime Ripard  *
431aeaac93dSMaxime Ripard  * Returns: 0 if successful, an negative error code otherwise
432aeaac93dSMaxime Ripard  */
433aeaac93dSMaxime Ripard int phy_configure(struct phy *phy, union phy_configure_opts *opts)
434aeaac93dSMaxime Ripard {
435aeaac93dSMaxime Ripard 	int ret;
436aeaac93dSMaxime Ripard 
437aeaac93dSMaxime Ripard 	if (!phy)
438aeaac93dSMaxime Ripard 		return -EINVAL;
439aeaac93dSMaxime Ripard 
440aeaac93dSMaxime Ripard 	if (!phy->ops->configure)
441aeaac93dSMaxime Ripard 		return -EOPNOTSUPP;
442aeaac93dSMaxime Ripard 
443aeaac93dSMaxime Ripard 	mutex_lock(&phy->mutex);
444aeaac93dSMaxime Ripard 	ret = phy->ops->configure(phy, opts);
445aeaac93dSMaxime Ripard 	mutex_unlock(&phy->mutex);
446aeaac93dSMaxime Ripard 
447aeaac93dSMaxime Ripard 	return ret;
448aeaac93dSMaxime Ripard }
449aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_configure);
450aeaac93dSMaxime Ripard 
451aeaac93dSMaxime Ripard /**
452aeaac93dSMaxime Ripard  * phy_validate() - Checks the phy parameters
453aeaac93dSMaxime Ripard  * @phy: the phy returned by phy_get()
454aeaac93dSMaxime Ripard  * @mode: phy_mode the configuration is applicable to.
455aeaac93dSMaxime Ripard  * @submode: PHY submode the configuration is applicable to.
456aeaac93dSMaxime Ripard  * @opts: Configuration to check
457aeaac93dSMaxime Ripard  *
458aeaac93dSMaxime Ripard  * Used to check that the current set of parameters can be handled by
459aeaac93dSMaxime Ripard  * the phy. Implementations are free to tune the parameters passed as
460aeaac93dSMaxime Ripard  * arguments if needed by some implementation detail or
461aeaac93dSMaxime Ripard  * constraints. It will not change any actual configuration of the
462aeaac93dSMaxime Ripard  * PHY, so calling it as many times as deemed fit will have no side
463aeaac93dSMaxime Ripard  * effect.
464aeaac93dSMaxime Ripard  *
465aeaac93dSMaxime Ripard  * Returns: 0 if successful, an negative error code otherwise
466aeaac93dSMaxime Ripard  */
467aeaac93dSMaxime Ripard int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
468aeaac93dSMaxime Ripard 		 union phy_configure_opts *opts)
469aeaac93dSMaxime Ripard {
470aeaac93dSMaxime Ripard 	int ret;
471aeaac93dSMaxime Ripard 
472aeaac93dSMaxime Ripard 	if (!phy)
473aeaac93dSMaxime Ripard 		return -EINVAL;
474aeaac93dSMaxime Ripard 
475aeaac93dSMaxime Ripard 	if (!phy->ops->validate)
476aeaac93dSMaxime Ripard 		return -EOPNOTSUPP;
477aeaac93dSMaxime Ripard 
478aeaac93dSMaxime Ripard 	mutex_lock(&phy->mutex);
479aeaac93dSMaxime Ripard 	ret = phy->ops->validate(phy, mode, submode, opts);
480aeaac93dSMaxime Ripard 	mutex_unlock(&phy->mutex);
481aeaac93dSMaxime Ripard 
482aeaac93dSMaxime Ripard 	return ret;
483aeaac93dSMaxime Ripard }
484aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_validate);
485aeaac93dSMaxime Ripard 
486aeaac93dSMaxime Ripard /**
4870b3f3b2cSKamil Debski  * _of_phy_get() - lookup and obtain a reference to a phy by phandle
4880b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
489ff764963SKishon Vijay Abraham I  * @index: the index of the phy
490ff764963SKishon Vijay Abraham I  *
491ff764963SKishon Vijay Abraham I  * Returns the phy associated with the given phandle value,
492ff764963SKishon Vijay Abraham I  * after getting a refcount to it or -ENODEV if there is no such phy or
493ff764963SKishon Vijay Abraham I  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
494ff764963SKishon Vijay Abraham I  * not yet loaded. This function uses of_xlate call back function provided
495ff764963SKishon Vijay Abraham I  * while registering the phy_provider to find the phy instance.
496ff764963SKishon Vijay Abraham I  */
4970b3f3b2cSKamil Debski static struct phy *_of_phy_get(struct device_node *np, int index)
498ff764963SKishon Vijay Abraham I {
499ff764963SKishon Vijay Abraham I 	int ret;
500ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
501ff764963SKishon Vijay Abraham I 	struct phy *phy = NULL;
502ff764963SKishon Vijay Abraham I 	struct of_phandle_args args;
503ff764963SKishon Vijay Abraham I 
5040b3f3b2cSKamil Debski 	ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
505ff764963SKishon Vijay Abraham I 		index, &args);
5060b3f3b2cSKamil Debski 	if (ret)
507ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENODEV);
508ff764963SKishon Vijay Abraham I 
509b7563e27SArnd Bergmann 	/* This phy type handled by the usb-phy subsystem for now */
510b7563e27SArnd Bergmann 	if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
511b7563e27SArnd Bergmann 		return ERR_PTR(-ENODEV);
512b7563e27SArnd Bergmann 
513ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
514ff764963SKishon Vijay Abraham I 	phy_provider = of_phy_provider_lookup(args.np);
515ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
516ff764963SKishon Vijay Abraham I 		phy = ERR_PTR(-EPROBE_DEFER);
51733f434d2SAxel Lin 		goto out_unlock;
51833f434d2SAxel Lin 	}
51933f434d2SAxel Lin 
52033f434d2SAxel Lin 	if (!of_device_is_available(args.np)) {
52133f434d2SAxel Lin 		dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
52233f434d2SAxel Lin 		phy = ERR_PTR(-ENODEV);
52333f434d2SAxel Lin 		goto out_put_module;
524ff764963SKishon Vijay Abraham I 	}
525ff764963SKishon Vijay Abraham I 
526ff764963SKishon Vijay Abraham I 	phy = phy_provider->of_xlate(phy_provider->dev, &args);
52733f434d2SAxel Lin 
52833f434d2SAxel Lin out_put_module:
529ff764963SKishon Vijay Abraham I 	module_put(phy_provider->owner);
530ff764963SKishon Vijay Abraham I 
53133f434d2SAxel Lin out_unlock:
532ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
533ff764963SKishon Vijay Abraham I 	of_node_put(args.np);
534ff764963SKishon Vijay Abraham I 
535ff764963SKishon Vijay Abraham I 	return phy;
536ff764963SKishon Vijay Abraham I }
537ff764963SKishon Vijay Abraham I 
538ff764963SKishon Vijay Abraham I /**
5390b3f3b2cSKamil Debski  * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
5400b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
5410b3f3b2cSKamil Debski  * @con_id: name of the phy from device's point of view
5420b3f3b2cSKamil Debski  *
5430b3f3b2cSKamil Debski  * Returns the phy driver, after getting a refcount to it; or
5440b3f3b2cSKamil Debski  * -ENODEV if there is no such phy. The caller is responsible for
5450b3f3b2cSKamil Debski  * calling phy_put() to release that count.
5460b3f3b2cSKamil Debski  */
5470b3f3b2cSKamil Debski struct phy *of_phy_get(struct device_node *np, const char *con_id)
5480b3f3b2cSKamil Debski {
5490b3f3b2cSKamil Debski 	struct phy *phy = NULL;
5500b3f3b2cSKamil Debski 	int index = 0;
5510b3f3b2cSKamil Debski 
5520b3f3b2cSKamil Debski 	if (con_id)
5530b3f3b2cSKamil Debski 		index = of_property_match_string(np, "phy-names", con_id);
5540b3f3b2cSKamil Debski 
5550b3f3b2cSKamil Debski 	phy = _of_phy_get(np, index);
5560b3f3b2cSKamil Debski 	if (IS_ERR(phy))
5570b3f3b2cSKamil Debski 		return phy;
5580b3f3b2cSKamil Debski 
5590b3f3b2cSKamil Debski 	if (!try_module_get(phy->ops->owner))
5600b3f3b2cSKamil Debski 		return ERR_PTR(-EPROBE_DEFER);
5610b3f3b2cSKamil Debski 
5620b3f3b2cSKamil Debski 	get_device(&phy->dev);
5630b3f3b2cSKamil Debski 
5640b3f3b2cSKamil Debski 	return phy;
5650b3f3b2cSKamil Debski }
5660b3f3b2cSKamil Debski EXPORT_SYMBOL_GPL(of_phy_get);
5670b3f3b2cSKamil Debski 
5680b3f3b2cSKamil Debski /**
569*987351e1SAlexandre Torgue  * of_phy_put() - release the PHY
570*987351e1SAlexandre Torgue  * @phy: the phy returned by of_phy_get()
571ff764963SKishon Vijay Abraham I  *
572*987351e1SAlexandre Torgue  * Releases a refcount the caller received from of_phy_get().
573ff764963SKishon Vijay Abraham I  */
574*987351e1SAlexandre Torgue void of_phy_put(struct phy *phy)
575ff764963SKishon Vijay Abraham I {
57604c2facaSAndrew Lunn 	if (!phy || IS_ERR(phy))
577ff764963SKishon Vijay Abraham I 		return;
578ff764963SKishon Vijay Abraham I 
579fec06b2bSKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
580fec06b2bSKishon Vijay Abraham I 	if (phy->ops->release)
581fec06b2bSKishon Vijay Abraham I 		phy->ops->release(phy);
582fec06b2bSKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
583fec06b2bSKishon Vijay Abraham I 
584ff764963SKishon Vijay Abraham I 	module_put(phy->ops->owner);
585ff764963SKishon Vijay Abraham I 	put_device(&phy->dev);
586ff764963SKishon Vijay Abraham I }
587*987351e1SAlexandre Torgue EXPORT_SYMBOL_GPL(of_phy_put);
588*987351e1SAlexandre Torgue 
589*987351e1SAlexandre Torgue /**
590*987351e1SAlexandre Torgue  * phy_put() - release the PHY
591*987351e1SAlexandre Torgue  * @dev: device that wants to release this phy
592*987351e1SAlexandre Torgue  * @phy: the phy returned by phy_get()
593*987351e1SAlexandre Torgue  *
594*987351e1SAlexandre Torgue  * Releases a refcount the caller received from phy_get().
595*987351e1SAlexandre Torgue  */
596*987351e1SAlexandre Torgue void phy_put(struct device *dev, struct phy *phy)
597*987351e1SAlexandre Torgue {
598*987351e1SAlexandre Torgue 	device_link_remove(dev, &phy->dev);
599*987351e1SAlexandre Torgue 	of_phy_put(phy);
600*987351e1SAlexandre Torgue }
601ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_put);
602ff764963SKishon Vijay Abraham I 
603ff764963SKishon Vijay Abraham I /**
604ff764963SKishon Vijay Abraham I  * devm_phy_put() - release the PHY
605ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
606ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
607ff764963SKishon Vijay Abraham I  *
608ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_put
609ff764963SKishon Vijay Abraham I  * to release the phy.
610ff764963SKishon Vijay Abraham I  */
611ff764963SKishon Vijay Abraham I void devm_phy_put(struct device *dev, struct phy *phy)
612ff764963SKishon Vijay Abraham I {
613ff764963SKishon Vijay Abraham I 	int r;
614ff764963SKishon Vijay Abraham I 
61504c2facaSAndrew Lunn 	if (!phy)
61604c2facaSAndrew Lunn 		return;
61704c2facaSAndrew Lunn 
618ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
619ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
620ff764963SKishon Vijay Abraham I }
621ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_put);
622ff764963SKishon Vijay Abraham I 
623ff764963SKishon Vijay Abraham I /**
624ff764963SKishon Vijay Abraham I  * of_phy_simple_xlate() - returns the phy instance from phy provider
625ff764963SKishon Vijay Abraham I  * @dev: the PHY provider device
626ff764963SKishon Vijay Abraham I  * @args: of_phandle_args (not used here)
627ff764963SKishon Vijay Abraham I  *
628ff764963SKishon Vijay Abraham I  * Intended to be used by phy provider for the common case where #phy-cells is
629ff764963SKishon Vijay Abraham I  * 0. For other cases where #phy-cells is greater than '0', the phy provider
630ff764963SKishon Vijay Abraham I  * should provide a custom of_xlate function that reads the *args* and returns
631ff764963SKishon Vijay Abraham I  * the appropriate phy.
632ff764963SKishon Vijay Abraham I  */
633ff764963SKishon Vijay Abraham I struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
634ff764963SKishon Vijay Abraham I 	*args)
635ff764963SKishon Vijay Abraham I {
636ff764963SKishon Vijay Abraham I 	struct phy *phy;
637ff764963SKishon Vijay Abraham I 	struct class_dev_iter iter;
638ff764963SKishon Vijay Abraham I 
639ff764963SKishon Vijay Abraham I 	class_dev_iter_init(&iter, phy_class, NULL, NULL);
640ff764963SKishon Vijay Abraham I 	while ((dev = class_dev_iter_next(&iter))) {
641ff764963SKishon Vijay Abraham I 		phy = to_phy(dev);
642491e0490SKishon Vijay Abraham I 		if (args->np != phy->dev.of_node)
643ff764963SKishon Vijay Abraham I 			continue;
644ff764963SKishon Vijay Abraham I 
645ff764963SKishon Vijay Abraham I 		class_dev_iter_exit(&iter);
646ff764963SKishon Vijay Abraham I 		return phy;
647ff764963SKishon Vijay Abraham I 	}
648ff764963SKishon Vijay Abraham I 
649ff764963SKishon Vijay Abraham I 	class_dev_iter_exit(&iter);
650ff764963SKishon Vijay Abraham I 	return ERR_PTR(-ENODEV);
651ff764963SKishon Vijay Abraham I }
652ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
653ff764963SKishon Vijay Abraham I 
654ff764963SKishon Vijay Abraham I /**
655ff764963SKishon Vijay Abraham I  * phy_get() - lookup and obtain a reference to a phy.
656ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
657ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or the name of the controller
658ff764963SKishon Vijay Abraham I  * port for non-dt case
659ff764963SKishon Vijay Abraham I  *
660ff764963SKishon Vijay Abraham I  * Returns the phy driver, after getting a refcount to it; or
661ff764963SKishon Vijay Abraham I  * -ENODEV if there is no such phy.  The caller is responsible for
662ff764963SKishon Vijay Abraham I  * calling phy_put() to release that count.
663ff764963SKishon Vijay Abraham I  */
664ff764963SKishon Vijay Abraham I struct phy *phy_get(struct device *dev, const char *string)
665ff764963SKishon Vijay Abraham I {
666ff764963SKishon Vijay Abraham I 	int index = 0;
667d18c9604SKishon Vijay Abraham I 	struct phy *phy;
668*987351e1SAlexandre Torgue 	struct device_link *link;
669ff764963SKishon Vijay Abraham I 
670ff764963SKishon Vijay Abraham I 	if (string == NULL) {
671ff764963SKishon Vijay Abraham I 		dev_WARN(dev, "missing string\n");
672ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EINVAL);
673ff764963SKishon Vijay Abraham I 	}
674ff764963SKishon Vijay Abraham I 
675ff764963SKishon Vijay Abraham I 	if (dev->of_node) {
676ff764963SKishon Vijay Abraham I 		index = of_property_match_string(dev->of_node, "phy-names",
677ff764963SKishon Vijay Abraham I 			string);
6780b3f3b2cSKamil Debski 		phy = _of_phy_get(dev->of_node, index);
679ff764963SKishon Vijay Abraham I 	} else {
680b7bc15b9SHeikki Krogerus 		phy = phy_find(dev, string);
681f40037fdSHans de Goede 	}
682f40037fdSHans de Goede 	if (IS_ERR(phy))
683ff764963SKishon Vijay Abraham I 		return phy;
684ff764963SKishon Vijay Abraham I 
685ff764963SKishon Vijay Abraham I 	if (!try_module_get(phy->ops->owner))
686ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EPROBE_DEFER);
687ff764963SKishon Vijay Abraham I 
688ff764963SKishon Vijay Abraham I 	get_device(&phy->dev);
689ff764963SKishon Vijay Abraham I 
690*987351e1SAlexandre Torgue 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
691*987351e1SAlexandre Torgue 	if (!link) {
692*987351e1SAlexandre Torgue 		dev_err(dev, "failed to create device link to %s\n",
693*987351e1SAlexandre Torgue 			dev_name(phy->dev.parent));
694*987351e1SAlexandre Torgue 		return ERR_PTR(-EINVAL);
695*987351e1SAlexandre Torgue 	}
696*987351e1SAlexandre Torgue 
697ff764963SKishon Vijay Abraham I 	return phy;
698ff764963SKishon Vijay Abraham I }
699ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_get);
700ff764963SKishon Vijay Abraham I 
701ff764963SKishon Vijay Abraham I /**
702788a4d56SAndrew Lunn  * phy_optional_get() - lookup and obtain a reference to an optional phy.
703788a4d56SAndrew Lunn  * @dev: device that requests this phy
704788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or the name of the controller
705788a4d56SAndrew Lunn  * port for non-dt case
706788a4d56SAndrew Lunn  *
707788a4d56SAndrew Lunn  * Returns the phy driver, after getting a refcount to it; or
708788a4d56SAndrew Lunn  * NULL if there is no such phy.  The caller is responsible for
709788a4d56SAndrew Lunn  * calling phy_put() to release that count.
710788a4d56SAndrew Lunn  */
711788a4d56SAndrew Lunn struct phy *phy_optional_get(struct device *dev, const char *string)
712788a4d56SAndrew Lunn {
713788a4d56SAndrew Lunn 	struct phy *phy = phy_get(dev, string);
714788a4d56SAndrew Lunn 
715f83be4c3SAxel Lin 	if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
716788a4d56SAndrew Lunn 		phy = NULL;
717788a4d56SAndrew Lunn 
718788a4d56SAndrew Lunn 	return phy;
719788a4d56SAndrew Lunn }
720788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(phy_optional_get);
721788a4d56SAndrew Lunn 
722788a4d56SAndrew Lunn /**
723ff764963SKishon Vijay Abraham I  * devm_phy_get() - lookup and obtain a reference to a phy.
724ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
725ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or phy device name
726ff764963SKishon Vijay Abraham I  * for non-dt case
727ff764963SKishon Vijay Abraham I  *
728ff764963SKishon Vijay Abraham I  * Gets the phy using phy_get(), and associates a device with it using
729ff764963SKishon Vijay Abraham I  * devres. On driver detach, release function is invoked on the devres data,
730ff764963SKishon Vijay Abraham I  * then, devres data is freed.
731ff764963SKishon Vijay Abraham I  */
732ff764963SKishon Vijay Abraham I struct phy *devm_phy_get(struct device *dev, const char *string)
733ff764963SKishon Vijay Abraham I {
734ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
735ff764963SKishon Vijay Abraham I 
736ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
737ff764963SKishon Vijay Abraham I 	if (!ptr)
738ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
739ff764963SKishon Vijay Abraham I 
740ff764963SKishon Vijay Abraham I 	phy = phy_get(dev, string);
741ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
742ff764963SKishon Vijay Abraham I 		*ptr = phy;
743ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
744ff764963SKishon Vijay Abraham I 	} else {
745ff764963SKishon Vijay Abraham I 		devres_free(ptr);
746ff764963SKishon Vijay Abraham I 	}
747ff764963SKishon Vijay Abraham I 
748ff764963SKishon Vijay Abraham I 	return phy;
749ff764963SKishon Vijay Abraham I }
750ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_get);
751ff764963SKishon Vijay Abraham I 
752ff764963SKishon Vijay Abraham I /**
753788a4d56SAndrew Lunn  * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
754788a4d56SAndrew Lunn  * @dev: device that requests this phy
755788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or phy device name
756788a4d56SAndrew Lunn  * for non-dt case
757788a4d56SAndrew Lunn  *
758788a4d56SAndrew Lunn  * Gets the phy using phy_get(), and associates a device with it using
759788a4d56SAndrew Lunn  * devres. On driver detach, release function is invoked on the devres
760788a4d56SAndrew Lunn  * data, then, devres data is freed. This differs to devm_phy_get() in
761788a4d56SAndrew Lunn  * that if the phy does not exist, it is not considered an error and
762788a4d56SAndrew Lunn  * -ENODEV will not be returned. Instead the NULL phy is returned,
763788a4d56SAndrew Lunn  * which can be passed to all other phy consumer calls.
764788a4d56SAndrew Lunn  */
765788a4d56SAndrew Lunn struct phy *devm_phy_optional_get(struct device *dev, const char *string)
766788a4d56SAndrew Lunn {
767788a4d56SAndrew Lunn 	struct phy *phy = devm_phy_get(dev, string);
768788a4d56SAndrew Lunn 
769f83be4c3SAxel Lin 	if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
770788a4d56SAndrew Lunn 		phy = NULL;
771788a4d56SAndrew Lunn 
772788a4d56SAndrew Lunn 	return phy;
773788a4d56SAndrew Lunn }
774788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(devm_phy_optional_get);
775788a4d56SAndrew Lunn 
776788a4d56SAndrew Lunn /**
777b5d682f4SKamil Debski  * devm_of_phy_get() - lookup and obtain a reference to a phy.
778b5d682f4SKamil Debski  * @dev: device that requests this phy
779b5d682f4SKamil Debski  * @np: node containing the phy
780b5d682f4SKamil Debski  * @con_id: name of the phy from device's point of view
781b5d682f4SKamil Debski  *
782b5d682f4SKamil Debski  * Gets the phy using of_phy_get(), and associates a device with it using
783b5d682f4SKamil Debski  * devres. On driver detach, release function is invoked on the devres data,
784b5d682f4SKamil Debski  * then, devres data is freed.
785b5d682f4SKamil Debski  */
786b5d682f4SKamil Debski struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
787b5d682f4SKamil Debski 			    const char *con_id)
788b5d682f4SKamil Debski {
789b5d682f4SKamil Debski 	struct phy **ptr, *phy;
790*987351e1SAlexandre Torgue 	struct device_link *link;
791b5d682f4SKamil Debski 
792b5d682f4SKamil Debski 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
793b5d682f4SKamil Debski 	if (!ptr)
794b5d682f4SKamil Debski 		return ERR_PTR(-ENOMEM);
795b5d682f4SKamil Debski 
796b5d682f4SKamil Debski 	phy = of_phy_get(np, con_id);
797b5d682f4SKamil Debski 	if (!IS_ERR(phy)) {
798b5d682f4SKamil Debski 		*ptr = phy;
799b5d682f4SKamil Debski 		devres_add(dev, ptr);
800b5d682f4SKamil Debski 	} else {
801b5d682f4SKamil Debski 		devres_free(ptr);
802*987351e1SAlexandre Torgue 		return phy;
803*987351e1SAlexandre Torgue 	}
804*987351e1SAlexandre Torgue 
805*987351e1SAlexandre Torgue 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
806*987351e1SAlexandre Torgue 	if (!link) {
807*987351e1SAlexandre Torgue 		dev_err(dev, "failed to create device link to %s\n",
808*987351e1SAlexandre Torgue 			dev_name(phy->dev.parent));
809*987351e1SAlexandre Torgue 		return ERR_PTR(-EINVAL);
810b5d682f4SKamil Debski 	}
811b5d682f4SKamil Debski 
812b5d682f4SKamil Debski 	return phy;
813b5d682f4SKamil Debski }
814b5d682f4SKamil Debski EXPORT_SYMBOL_GPL(devm_of_phy_get);
815b5d682f4SKamil Debski 
816b5d682f4SKamil Debski /**
8176be109b3SArun Ramamurthy  * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
8186be109b3SArun Ramamurthy  * @dev: device that requests this phy
8196be109b3SArun Ramamurthy  * @np: node containing the phy
8206be109b3SArun Ramamurthy  * @index: index of the phy
8216be109b3SArun Ramamurthy  *
82270874462SChunfeng Yun  * Gets the phy using _of_phy_get(), then gets a refcount to it,
82370874462SChunfeng Yun  * and associates a device with it using devres. On driver detach,
82470874462SChunfeng Yun  * release function is invoked on the devres data,
8256be109b3SArun Ramamurthy  * then, devres data is freed.
8266be109b3SArun Ramamurthy  *
8276be109b3SArun Ramamurthy  */
8286be109b3SArun Ramamurthy struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
8296be109b3SArun Ramamurthy 				     int index)
8306be109b3SArun Ramamurthy {
8316be109b3SArun Ramamurthy 	struct phy **ptr, *phy;
832*987351e1SAlexandre Torgue 	struct device_link *link;
8336be109b3SArun Ramamurthy 
8346be109b3SArun Ramamurthy 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
8356be109b3SArun Ramamurthy 	if (!ptr)
8366be109b3SArun Ramamurthy 		return ERR_PTR(-ENOMEM);
8376be109b3SArun Ramamurthy 
8386be109b3SArun Ramamurthy 	phy = _of_phy_get(np, index);
83970874462SChunfeng Yun 	if (IS_ERR(phy)) {
84070874462SChunfeng Yun 		devres_free(ptr);
84170874462SChunfeng Yun 		return phy;
84270874462SChunfeng Yun 	}
84370874462SChunfeng Yun 
84470874462SChunfeng Yun 	if (!try_module_get(phy->ops->owner)) {
84570874462SChunfeng Yun 		devres_free(ptr);
84670874462SChunfeng Yun 		return ERR_PTR(-EPROBE_DEFER);
84770874462SChunfeng Yun 	}
84870874462SChunfeng Yun 
84970874462SChunfeng Yun 	get_device(&phy->dev);
85070874462SChunfeng Yun 
8516be109b3SArun Ramamurthy 	*ptr = phy;
8526be109b3SArun Ramamurthy 	devres_add(dev, ptr);
8536be109b3SArun Ramamurthy 
854*987351e1SAlexandre Torgue 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
855*987351e1SAlexandre Torgue 	if (!link) {
856*987351e1SAlexandre Torgue 		dev_err(dev, "failed to create device link to %s\n",
857*987351e1SAlexandre Torgue 			dev_name(phy->dev.parent));
858*987351e1SAlexandre Torgue 		return ERR_PTR(-EINVAL);
859*987351e1SAlexandre Torgue 	}
860*987351e1SAlexandre Torgue 
8616be109b3SArun Ramamurthy 	return phy;
8626be109b3SArun Ramamurthy }
8636be109b3SArun Ramamurthy EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
8646be109b3SArun Ramamurthy 
8656be109b3SArun Ramamurthy /**
866ff764963SKishon Vijay Abraham I  * phy_create() - create a new phy
867ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
868f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
869ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
870ff764963SKishon Vijay Abraham I  *
871ff764963SKishon Vijay Abraham I  * Called to create a phy using phy framework.
872ff764963SKishon Vijay Abraham I  */
873f0ed8176SKishon Vijay Abraham I struct phy *phy_create(struct device *dev, struct device_node *node,
874dbc98635SHeikki Krogerus 		       const struct phy_ops *ops)
875ff764963SKishon Vijay Abraham I {
876ff764963SKishon Vijay Abraham I 	int ret;
877ff764963SKishon Vijay Abraham I 	int id;
878ff764963SKishon Vijay Abraham I 	struct phy *phy;
879ff764963SKishon Vijay Abraham I 
88052797d29SDan Carpenter 	if (WARN_ON(!dev))
88152797d29SDan Carpenter 		return ERR_PTR(-EINVAL);
882ff764963SKishon Vijay Abraham I 
883ff764963SKishon Vijay Abraham I 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
88452797d29SDan Carpenter 	if (!phy)
88552797d29SDan Carpenter 		return ERR_PTR(-ENOMEM);
886ff764963SKishon Vijay Abraham I 
887ff764963SKishon Vijay Abraham I 	id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
888ff764963SKishon Vijay Abraham I 	if (id < 0) {
889ff764963SKishon Vijay Abraham I 		dev_err(dev, "unable to get id\n");
890ff764963SKishon Vijay Abraham I 		ret = id;
89152797d29SDan Carpenter 		goto free_phy;
892ff764963SKishon Vijay Abraham I 	}
893ff764963SKishon Vijay Abraham I 
894ff764963SKishon Vijay Abraham I 	device_initialize(&phy->dev);
895ff764963SKishon Vijay Abraham I 	mutex_init(&phy->mutex);
896ff764963SKishon Vijay Abraham I 
897ff764963SKishon Vijay Abraham I 	phy->dev.class = phy_class;
898ff764963SKishon Vijay Abraham I 	phy->dev.parent = dev;
899f0ed8176SKishon Vijay Abraham I 	phy->dev.of_node = node ?: dev->of_node;
900ff764963SKishon Vijay Abraham I 	phy->id = id;
901ff764963SKishon Vijay Abraham I 	phy->ops = ops;
902ff764963SKishon Vijay Abraham I 
903ff764963SKishon Vijay Abraham I 	ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
904ff764963SKishon Vijay Abraham I 	if (ret)
90552797d29SDan Carpenter 		goto put_dev;
906ff764963SKishon Vijay Abraham I 
90787006dd6SDmitry Torokhov 	/* phy-supply */
90887006dd6SDmitry Torokhov 	phy->pwr = regulator_get_optional(&phy->dev, "phy");
90987006dd6SDmitry Torokhov 	if (IS_ERR(phy->pwr)) {
91087006dd6SDmitry Torokhov 		ret = PTR_ERR(phy->pwr);
91187006dd6SDmitry Torokhov 		if (ret == -EPROBE_DEFER)
91287006dd6SDmitry Torokhov 			goto put_dev;
91387006dd6SDmitry Torokhov 
91487006dd6SDmitry Torokhov 		phy->pwr = NULL;
91587006dd6SDmitry Torokhov 	}
91687006dd6SDmitry Torokhov 
917ff764963SKishon Vijay Abraham I 	ret = device_add(&phy->dev);
918ff764963SKishon Vijay Abraham I 	if (ret)
91952797d29SDan Carpenter 		goto put_dev;
920ff764963SKishon Vijay Abraham I 
921ff764963SKishon Vijay Abraham I 	if (pm_runtime_enabled(dev)) {
922ff764963SKishon Vijay Abraham I 		pm_runtime_enable(&phy->dev);
923ff764963SKishon Vijay Abraham I 		pm_runtime_no_callbacks(&phy->dev);
924ff764963SKishon Vijay Abraham I 	}
925ff764963SKishon Vijay Abraham I 
926ff764963SKishon Vijay Abraham I 	return phy;
927ff764963SKishon Vijay Abraham I 
92852797d29SDan Carpenter put_dev:
929e73b49f1SRoger Quadros 	put_device(&phy->dev);  /* calls phy_release() which frees resources */
930e73b49f1SRoger Quadros 	return ERR_PTR(ret);
931e73b49f1SRoger Quadros 
93252797d29SDan Carpenter free_phy:
933ff764963SKishon Vijay Abraham I 	kfree(phy);
934ff764963SKishon Vijay Abraham I 	return ERR_PTR(ret);
935ff764963SKishon Vijay Abraham I }
936ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_create);
937ff764963SKishon Vijay Abraham I 
938ff764963SKishon Vijay Abraham I /**
939ff764963SKishon Vijay Abraham I  * devm_phy_create() - create a new phy
940ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
941f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
942ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
943ff764963SKishon Vijay Abraham I  *
944ff764963SKishon Vijay Abraham I  * Creates a new PHY device adding it to the PHY class.
945ff764963SKishon Vijay Abraham I  * While at that, it also associates the device with the phy using devres.
946ff764963SKishon Vijay Abraham I  * On driver detach, release function is invoked on the devres data,
947ff764963SKishon Vijay Abraham I  * then, devres data is freed.
948ff764963SKishon Vijay Abraham I  */
949f0ed8176SKishon Vijay Abraham I struct phy *devm_phy_create(struct device *dev, struct device_node *node,
950dbc98635SHeikki Krogerus 			    const struct phy_ops *ops)
951ff764963SKishon Vijay Abraham I {
952ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
953ff764963SKishon Vijay Abraham I 
954ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
955ff764963SKishon Vijay Abraham I 	if (!ptr)
956ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
957ff764963SKishon Vijay Abraham I 
958dbc98635SHeikki Krogerus 	phy = phy_create(dev, node, ops);
959ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
960ff764963SKishon Vijay Abraham I 		*ptr = phy;
961ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
962ff764963SKishon Vijay Abraham I 	} else {
963ff764963SKishon Vijay Abraham I 		devres_free(ptr);
964ff764963SKishon Vijay Abraham I 	}
965ff764963SKishon Vijay Abraham I 
966ff764963SKishon Vijay Abraham I 	return phy;
967ff764963SKishon Vijay Abraham I }
968ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_create);
969ff764963SKishon Vijay Abraham I 
970ff764963SKishon Vijay Abraham I /**
971ff764963SKishon Vijay Abraham I  * phy_destroy() - destroy the phy
972ff764963SKishon Vijay Abraham I  * @phy: the phy to be destroyed
973ff764963SKishon Vijay Abraham I  *
974ff764963SKishon Vijay Abraham I  * Called to destroy the phy.
975ff764963SKishon Vijay Abraham I  */
976ff764963SKishon Vijay Abraham I void phy_destroy(struct phy *phy)
977ff764963SKishon Vijay Abraham I {
978ff764963SKishon Vijay Abraham I 	pm_runtime_disable(&phy->dev);
979ff764963SKishon Vijay Abraham I 	device_unregister(&phy->dev);
980ff764963SKishon Vijay Abraham I }
981ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_destroy);
982ff764963SKishon Vijay Abraham I 
983ff764963SKishon Vijay Abraham I /**
984ff764963SKishon Vijay Abraham I  * devm_phy_destroy() - destroy the PHY
985ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
986ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
987ff764963SKishon Vijay Abraham I  *
988ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_destroy
989ff764963SKishon Vijay Abraham I  * to destroy the phy.
990ff764963SKishon Vijay Abraham I  */
991ff764963SKishon Vijay Abraham I void devm_phy_destroy(struct device *dev, struct phy *phy)
992ff764963SKishon Vijay Abraham I {
993ff764963SKishon Vijay Abraham I 	int r;
994ff764963SKishon Vijay Abraham I 
995ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
996ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
997ff764963SKishon Vijay Abraham I }
998ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_destroy);
999ff764963SKishon Vijay Abraham I 
1000ff764963SKishon Vijay Abraham I /**
1001ff764963SKishon Vijay Abraham I  * __of_phy_provider_register() - create/register phy provider with the framework
1002ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
10031140f7c8SThierry Reding  * @children: device node containing children (if different from dev->of_node)
1004ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
1005ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
1006ff764963SKishon Vijay Abraham I  *
1007ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
1008ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
1009ff764963SKishon Vijay Abraham I  * phy provider.
10101140f7c8SThierry Reding  *
10111140f7c8SThierry Reding  * If the PHY provider doesn't nest children directly but uses a separate
10121140f7c8SThierry Reding  * child node to contain the individual children, the @children parameter
10131140f7c8SThierry Reding  * can be used to override the default. If NULL, the default (dev->of_node)
10141140f7c8SThierry Reding  * will be used. If non-NULL, the device node must be a child (or further
10151140f7c8SThierry Reding  * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
10161140f7c8SThierry Reding  * error code is returned.
1017ff764963SKishon Vijay Abraham I  */
1018ff764963SKishon Vijay Abraham I struct phy_provider *__of_phy_provider_register(struct device *dev,
10191140f7c8SThierry Reding 	struct device_node *children, struct module *owner,
10201140f7c8SThierry Reding 	struct phy * (*of_xlate)(struct device *dev,
1021ff764963SKishon Vijay Abraham I 				 struct of_phandle_args *args))
1022ff764963SKishon Vijay Abraham I {
1023ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
1024ff764963SKishon Vijay Abraham I 
10251140f7c8SThierry Reding 	/*
10261140f7c8SThierry Reding 	 * If specified, the device node containing the children must itself
10271140f7c8SThierry Reding 	 * be the provider's device node or a child (or further descendant)
10281140f7c8SThierry Reding 	 * thereof.
10291140f7c8SThierry Reding 	 */
10301140f7c8SThierry Reding 	if (children) {
10311140f7c8SThierry Reding 		struct device_node *parent = of_node_get(children), *next;
10321140f7c8SThierry Reding 
10331140f7c8SThierry Reding 		while (parent) {
10341140f7c8SThierry Reding 			if (parent == dev->of_node)
10351140f7c8SThierry Reding 				break;
10361140f7c8SThierry Reding 
10371140f7c8SThierry Reding 			next = of_get_parent(parent);
10381140f7c8SThierry Reding 			of_node_put(parent);
10391140f7c8SThierry Reding 			parent = next;
10401140f7c8SThierry Reding 		}
10411140f7c8SThierry Reding 
10421140f7c8SThierry Reding 		if (!parent)
10431140f7c8SThierry Reding 			return ERR_PTR(-EINVAL);
10441140f7c8SThierry Reding 
10451140f7c8SThierry Reding 		of_node_put(parent);
10461140f7c8SThierry Reding 	} else {
10471140f7c8SThierry Reding 		children = dev->of_node;
10481140f7c8SThierry Reding 	}
10491140f7c8SThierry Reding 
1050ff764963SKishon Vijay Abraham I 	phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
1051ff764963SKishon Vijay Abraham I 	if (!phy_provider)
1052ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
1053ff764963SKishon Vijay Abraham I 
1054ff764963SKishon Vijay Abraham I 	phy_provider->dev = dev;
10551140f7c8SThierry Reding 	phy_provider->children = of_node_get(children);
1056ff764963SKishon Vijay Abraham I 	phy_provider->owner = owner;
1057ff764963SKishon Vijay Abraham I 	phy_provider->of_xlate = of_xlate;
1058ff764963SKishon Vijay Abraham I 
1059ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
1060ff764963SKishon Vijay Abraham I 	list_add_tail(&phy_provider->list, &phy_provider_list);
1061ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
1062ff764963SKishon Vijay Abraham I 
1063ff764963SKishon Vijay Abraham I 	return phy_provider;
1064ff764963SKishon Vijay Abraham I }
1065ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__of_phy_provider_register);
1066ff764963SKishon Vijay Abraham I 
1067ff764963SKishon Vijay Abraham I /**
1068ff764963SKishon Vijay Abraham I  * __devm_of_phy_provider_register() - create/register phy provider with the
1069ff764963SKishon Vijay Abraham I  * framework
1070ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
1071ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
1072ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
1073ff764963SKishon Vijay Abraham I  *
1074ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
1075ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
1076ff764963SKishon Vijay Abraham I  * phy provider. While at that, it also associates the device with the
1077ff764963SKishon Vijay Abraham I  * phy provider using devres. On driver detach, release function is invoked
1078ff764963SKishon Vijay Abraham I  * on the devres data, then, devres data is freed.
1079ff764963SKishon Vijay Abraham I  */
1080ff764963SKishon Vijay Abraham I struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
10811140f7c8SThierry Reding 	struct device_node *children, struct module *owner,
10821140f7c8SThierry Reding 	struct phy * (*of_xlate)(struct device *dev,
1083ff764963SKishon Vijay Abraham I 				 struct of_phandle_args *args))
1084ff764963SKishon Vijay Abraham I {
1085ff764963SKishon Vijay Abraham I 	struct phy_provider **ptr, *phy_provider;
1086ff764963SKishon Vijay Abraham I 
1087ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
1088ff764963SKishon Vijay Abraham I 	if (!ptr)
1089ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
1090ff764963SKishon Vijay Abraham I 
10911140f7c8SThierry Reding 	phy_provider = __of_phy_provider_register(dev, children, owner,
10921140f7c8SThierry Reding 						  of_xlate);
1093ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy_provider)) {
1094ff764963SKishon Vijay Abraham I 		*ptr = phy_provider;
1095ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
1096ff764963SKishon Vijay Abraham I 	} else {
1097ff764963SKishon Vijay Abraham I 		devres_free(ptr);
1098ff764963SKishon Vijay Abraham I 	}
1099ff764963SKishon Vijay Abraham I 
1100ff764963SKishon Vijay Abraham I 	return phy_provider;
1101ff764963SKishon Vijay Abraham I }
1102ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
1103ff764963SKishon Vijay Abraham I 
1104ff764963SKishon Vijay Abraham I /**
1105ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister() - unregister phy provider from the framework
1106ff764963SKishon Vijay Abraham I  * @phy_provider: phy provider returned by of_phy_provider_register()
1107ff764963SKishon Vijay Abraham I  *
1108ff764963SKishon Vijay Abraham I  * Removes the phy_provider created using of_phy_provider_register().
1109ff764963SKishon Vijay Abraham I  */
1110ff764963SKishon Vijay Abraham I void of_phy_provider_unregister(struct phy_provider *phy_provider)
1111ff764963SKishon Vijay Abraham I {
1112ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider))
1113ff764963SKishon Vijay Abraham I 		return;
1114ff764963SKishon Vijay Abraham I 
1115ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
1116ff764963SKishon Vijay Abraham I 	list_del(&phy_provider->list);
11171140f7c8SThierry Reding 	of_node_put(phy_provider->children);
1118ff764963SKishon Vijay Abraham I 	kfree(phy_provider);
1119ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
1120ff764963SKishon Vijay Abraham I }
1121ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
1122ff764963SKishon Vijay Abraham I 
1123ff764963SKishon Vijay Abraham I /**
1124ff764963SKishon Vijay Abraham I  * devm_of_phy_provider_unregister() - remove phy provider from the framework
1125ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
1126ff764963SKishon Vijay Abraham I  *
1127ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy provider and invokes
1128ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister to unregister the phy provider.
1129ff764963SKishon Vijay Abraham I  */
1130ff764963SKishon Vijay Abraham I void devm_of_phy_provider_unregister(struct device *dev,
1131ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider) {
1132ff764963SKishon Vijay Abraham I 	int r;
1133ff764963SKishon Vijay Abraham I 
1134ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
1135ff764963SKishon Vijay Abraham I 		phy_provider);
1136ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
1137ff764963SKishon Vijay Abraham I }
1138ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
1139ff764963SKishon Vijay Abraham I 
1140ff764963SKishon Vijay Abraham I /**
1141ff764963SKishon Vijay Abraham I  * phy_release() - release the phy
1142ff764963SKishon Vijay Abraham I  * @dev: the dev member within phy
1143ff764963SKishon Vijay Abraham I  *
1144ff764963SKishon Vijay Abraham I  * When the last reference to the device is removed, it is called
1145ff764963SKishon Vijay Abraham I  * from the embedded kobject as release method.
1146ff764963SKishon Vijay Abraham I  */
1147ff764963SKishon Vijay Abraham I static void phy_release(struct device *dev)
1148ff764963SKishon Vijay Abraham I {
1149ff764963SKishon Vijay Abraham I 	struct phy *phy;
1150ff764963SKishon Vijay Abraham I 
1151ff764963SKishon Vijay Abraham I 	phy = to_phy(dev);
1152ff764963SKishon Vijay Abraham I 	dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
11533be88125SRoger Quadros 	regulator_put(phy->pwr);
1154e73b49f1SRoger Quadros 	ida_simple_remove(&phy_ida, phy->id);
1155ff764963SKishon Vijay Abraham I 	kfree(phy);
1156ff764963SKishon Vijay Abraham I }
1157ff764963SKishon Vijay Abraham I 
1158ff764963SKishon Vijay Abraham I static int __init phy_core_init(void)
1159ff764963SKishon Vijay Abraham I {
1160ff764963SKishon Vijay Abraham I 	phy_class = class_create(THIS_MODULE, "phy");
1161ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_class)) {
1162ff764963SKishon Vijay Abraham I 		pr_err("failed to create phy class --> %ld\n",
1163ff764963SKishon Vijay Abraham I 			PTR_ERR(phy_class));
1164ff764963SKishon Vijay Abraham I 		return PTR_ERR(phy_class);
1165ff764963SKishon Vijay Abraham I 	}
1166ff764963SKishon Vijay Abraham I 
1167ff764963SKishon Vijay Abraham I 	phy_class->dev_release = phy_release;
1168ff764963SKishon Vijay Abraham I 
1169ff764963SKishon Vijay Abraham I 	return 0;
1170ff764963SKishon Vijay Abraham I }
1171cc013c28SPaul Gortmaker device_initcall(phy_core_init);
1172