xref: /openbmc/linux/drivers/phy/phy-core.c (revision aeaac93ddb28eeacc0dff9c12cb338eb1de7481d)
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 
1441140f7c8SThierry Reding 		for_each_child_of_node(phy_provider->children, 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 
1568866df25SManu Gautam 	if (!phy)
1578866df25SManu Gautam 		return 0;
1588866df25SManu Gautam 
159ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
160ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
161ff764963SKishon Vijay Abraham I 
162cedb7f89SFelipe Balbi 	ret = pm_runtime_get(&phy->dev);
163cedb7f89SFelipe Balbi 	if (ret < 0 && ret != -EINPROGRESS)
164cedb7f89SFelipe Balbi 		pm_runtime_put_noidle(&phy->dev);
165cedb7f89SFelipe Balbi 
166cedb7f89SFelipe Balbi 	return ret;
167ff764963SKishon Vijay Abraham I }
168ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
169ff764963SKishon Vijay Abraham I 
170ff764963SKishon Vijay Abraham I int phy_pm_runtime_get_sync(struct phy *phy)
171ff764963SKishon Vijay Abraham I {
172cedb7f89SFelipe Balbi 	int ret;
173cedb7f89SFelipe Balbi 
1748866df25SManu Gautam 	if (!phy)
1758866df25SManu Gautam 		return 0;
1768866df25SManu Gautam 
177ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
178ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
179ff764963SKishon Vijay Abraham I 
180cedb7f89SFelipe Balbi 	ret = pm_runtime_get_sync(&phy->dev);
181cedb7f89SFelipe Balbi 	if (ret < 0)
182cedb7f89SFelipe Balbi 		pm_runtime_put_sync(&phy->dev);
183cedb7f89SFelipe Balbi 
184cedb7f89SFelipe Balbi 	return ret;
185ff764963SKishon Vijay Abraham I }
186ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
187ff764963SKishon Vijay Abraham I 
188ff764963SKishon Vijay Abraham I int phy_pm_runtime_put(struct phy *phy)
189ff764963SKishon Vijay Abraham I {
1908866df25SManu Gautam 	if (!phy)
1918866df25SManu Gautam 		return 0;
1928866df25SManu Gautam 
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(&phy->dev);
197ff764963SKishon Vijay Abraham I }
198ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
199ff764963SKishon Vijay Abraham I 
200ff764963SKishon Vijay Abraham I int phy_pm_runtime_put_sync(struct phy *phy)
201ff764963SKishon Vijay Abraham I {
2028866df25SManu Gautam 	if (!phy)
2038866df25SManu Gautam 		return 0;
2048866df25SManu Gautam 
205ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
206ff764963SKishon Vijay Abraham I 		return -ENOTSUPP;
207ff764963SKishon Vijay Abraham I 
208ff764963SKishon Vijay Abraham I 	return pm_runtime_put_sync(&phy->dev);
209ff764963SKishon Vijay Abraham I }
210ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
211ff764963SKishon Vijay Abraham I 
212ff764963SKishon Vijay Abraham I void phy_pm_runtime_allow(struct phy *phy)
213ff764963SKishon Vijay Abraham I {
2148866df25SManu Gautam 	if (!phy)
2158866df25SManu Gautam 		return;
2168866df25SManu Gautam 
217ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
218ff764963SKishon Vijay Abraham I 		return;
219ff764963SKishon Vijay Abraham I 
220ff764963SKishon Vijay Abraham I 	pm_runtime_allow(&phy->dev);
221ff764963SKishon Vijay Abraham I }
222ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
223ff764963SKishon Vijay Abraham I 
224ff764963SKishon Vijay Abraham I void phy_pm_runtime_forbid(struct phy *phy)
225ff764963SKishon Vijay Abraham I {
2268866df25SManu Gautam 	if (!phy)
2278866df25SManu Gautam 		return;
2288866df25SManu Gautam 
229ff764963SKishon Vijay Abraham I 	if (!pm_runtime_enabled(&phy->dev))
230ff764963SKishon Vijay Abraham I 		return;
231ff764963SKishon Vijay Abraham I 
232ff764963SKishon Vijay Abraham I 	pm_runtime_forbid(&phy->dev);
233ff764963SKishon Vijay Abraham I }
234ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
235ff764963SKishon Vijay Abraham I 
236ff764963SKishon Vijay Abraham I int phy_init(struct phy *phy)
237ff764963SKishon Vijay Abraham I {
238ff764963SKishon Vijay Abraham I 	int ret;
239ff764963SKishon Vijay Abraham I 
24004c2facaSAndrew Lunn 	if (!phy)
24104c2facaSAndrew Lunn 		return 0;
24204c2facaSAndrew Lunn 
243ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
244ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
245ff764963SKishon Vijay Abraham I 		return ret;
246736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
247ff764963SKishon Vijay Abraham I 
248ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
249637d378cSKishon Vijay Abraham I 	if (phy->init_count == 0 && phy->ops->init) {
250ff764963SKishon Vijay Abraham I 		ret = phy->ops->init(phy);
251ff764963SKishon Vijay Abraham I 		if (ret < 0) {
252ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy init failed --> %d\n", ret);
253ff764963SKishon Vijay Abraham I 			goto out;
254ff764963SKishon Vijay Abraham I 		}
255ff764963SKishon Vijay Abraham I 	}
256637d378cSKishon Vijay Abraham I 	++phy->init_count;
257ff764963SKishon Vijay Abraham I 
258ff764963SKishon Vijay Abraham I out:
259ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
260ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
261ff764963SKishon Vijay Abraham I 	return ret;
262ff764963SKishon Vijay Abraham I }
263ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_init);
264ff764963SKishon Vijay Abraham I 
265ff764963SKishon Vijay Abraham I int phy_exit(struct phy *phy)
266ff764963SKishon Vijay Abraham I {
267ff764963SKishon Vijay Abraham I 	int ret;
268ff764963SKishon Vijay Abraham I 
26904c2facaSAndrew Lunn 	if (!phy)
27004c2facaSAndrew Lunn 		return 0;
27104c2facaSAndrew Lunn 
272ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
273ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
274ff764963SKishon Vijay Abraham I 		return ret;
275736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
276ff764963SKishon Vijay Abraham I 
277ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
278637d378cSKishon Vijay Abraham I 	if (phy->init_count == 1 && phy->ops->exit) {
279ff764963SKishon Vijay Abraham I 		ret = phy->ops->exit(phy);
280ff764963SKishon Vijay Abraham I 		if (ret < 0) {
281ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
282ff764963SKishon Vijay Abraham I 			goto out;
283ff764963SKishon Vijay Abraham I 		}
284ff764963SKishon Vijay Abraham I 	}
285637d378cSKishon Vijay Abraham I 	--phy->init_count;
286ff764963SKishon Vijay Abraham I 
287ff764963SKishon Vijay Abraham I out:
288ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
289ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
290ff764963SKishon Vijay Abraham I 	return ret;
291ff764963SKishon Vijay Abraham I }
292ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_exit);
293ff764963SKishon Vijay Abraham I 
294ff764963SKishon Vijay Abraham I int phy_power_on(struct phy *phy)
295ff764963SKishon Vijay Abraham I {
296b82fcabeSShawn Lin 	int ret = 0;
297ff764963SKishon Vijay Abraham I 
29804c2facaSAndrew Lunn 	if (!phy)
299b82fcabeSShawn Lin 		goto out;
30004c2facaSAndrew Lunn 
3013be88125SRoger Quadros 	if (phy->pwr) {
3023be88125SRoger Quadros 		ret = regulator_enable(phy->pwr);
3033be88125SRoger Quadros 		if (ret)
304b82fcabeSShawn Lin 			goto out;
3053be88125SRoger Quadros 	}
3063be88125SRoger Quadros 
307ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
308ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
309b82fcabeSShawn Lin 		goto err_pm_sync;
310b82fcabeSShawn Lin 
311736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
312ff764963SKishon Vijay Abraham I 
313ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
314637d378cSKishon Vijay Abraham I 	if (phy->power_count == 0 && phy->ops->power_on) {
315ff764963SKishon Vijay Abraham I 		ret = phy->ops->power_on(phy);
316ff764963SKishon Vijay Abraham I 		if (ret < 0) {
317ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
318b82fcabeSShawn Lin 			goto err_pwr_on;
319ff764963SKishon Vijay Abraham I 		}
320ff764963SKishon Vijay Abraham I 	}
321637d378cSKishon Vijay Abraham I 	++phy->power_count;
322637d378cSKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
323637d378cSKishon Vijay Abraham I 	return 0;
324ff764963SKishon Vijay Abraham I 
325b82fcabeSShawn Lin err_pwr_on:
326ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
327637d378cSKishon Vijay Abraham I 	phy_pm_runtime_put_sync(phy);
328b82fcabeSShawn Lin err_pm_sync:
3293be88125SRoger Quadros 	if (phy->pwr)
3303be88125SRoger Quadros 		regulator_disable(phy->pwr);
331b82fcabeSShawn Lin out:
332ff764963SKishon Vijay Abraham I 	return ret;
333ff764963SKishon Vijay Abraham I }
334ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_on);
335ff764963SKishon Vijay Abraham I 
336ff764963SKishon Vijay Abraham I int phy_power_off(struct phy *phy)
337ff764963SKishon Vijay Abraham I {
338d18c9604SKishon Vijay Abraham I 	int ret;
339ff764963SKishon Vijay Abraham I 
34004c2facaSAndrew Lunn 	if (!phy)
34104c2facaSAndrew Lunn 		return 0;
34204c2facaSAndrew Lunn 
343ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
344637d378cSKishon Vijay Abraham I 	if (phy->power_count == 1 && phy->ops->power_off) {
345ff764963SKishon Vijay Abraham I 		ret =  phy->ops->power_off(phy);
346ff764963SKishon Vijay Abraham I 		if (ret < 0) {
347ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
348637d378cSKishon Vijay Abraham I 			mutex_unlock(&phy->mutex);
349637d378cSKishon Vijay Abraham I 			return ret;
350ff764963SKishon Vijay Abraham I 		}
351ff764963SKishon Vijay Abraham I 	}
352637d378cSKishon Vijay Abraham I 	--phy->power_count;
353ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
354ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
355ff764963SKishon Vijay Abraham I 
3563be88125SRoger Quadros 	if (phy->pwr)
3573be88125SRoger Quadros 		regulator_disable(phy->pwr);
3583be88125SRoger Quadros 
359637d378cSKishon Vijay Abraham I 	return 0;
360ff764963SKishon Vijay Abraham I }
361ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_off);
362ff764963SKishon Vijay Abraham I 
36379a5a18aSGrygorii Strashko int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
364300eb013SDavid Lechner {
365300eb013SDavid Lechner 	int ret;
366300eb013SDavid Lechner 
367300eb013SDavid Lechner 	if (!phy || !phy->ops->set_mode)
368300eb013SDavid Lechner 		return 0;
369300eb013SDavid Lechner 
370300eb013SDavid Lechner 	mutex_lock(&phy->mutex);
37179a5a18aSGrygorii Strashko 	ret = phy->ops->set_mode(phy, mode, submode);
3723b3cd24aSManu Gautam 	if (!ret)
3733b3cd24aSManu Gautam 		phy->attrs.mode = mode;
374300eb013SDavid Lechner 	mutex_unlock(&phy->mutex);
375300eb013SDavid Lechner 
376300eb013SDavid Lechner 	return ret;
377300eb013SDavid Lechner }
37879a5a18aSGrygorii Strashko EXPORT_SYMBOL_GPL(phy_set_mode_ext);
379300eb013SDavid Lechner 
380cac18ecbSRandy Li int phy_reset(struct phy *phy)
381cac18ecbSRandy Li {
382cac18ecbSRandy Li 	int ret;
383cac18ecbSRandy Li 
384cac18ecbSRandy Li 	if (!phy || !phy->ops->reset)
385cac18ecbSRandy Li 		return 0;
386cac18ecbSRandy Li 
387cac18ecbSRandy Li 	mutex_lock(&phy->mutex);
388cac18ecbSRandy Li 	ret = phy->ops->reset(phy);
389cac18ecbSRandy Li 	mutex_unlock(&phy->mutex);
390cac18ecbSRandy Li 
391cac18ecbSRandy Li 	return ret;
392cac18ecbSRandy Li }
393cac18ecbSRandy Li EXPORT_SYMBOL_GPL(phy_reset);
394cac18ecbSRandy Li 
39536914111SAndrzej Pietrasiewicz int phy_calibrate(struct phy *phy)
39636914111SAndrzej Pietrasiewicz {
39736914111SAndrzej Pietrasiewicz 	int ret;
39836914111SAndrzej Pietrasiewicz 
39936914111SAndrzej Pietrasiewicz 	if (!phy || !phy->ops->calibrate)
40036914111SAndrzej Pietrasiewicz 		return 0;
40136914111SAndrzej Pietrasiewicz 
40236914111SAndrzej Pietrasiewicz 	mutex_lock(&phy->mutex);
40336914111SAndrzej Pietrasiewicz 	ret = phy->ops->calibrate(phy);
40436914111SAndrzej Pietrasiewicz 	mutex_unlock(&phy->mutex);
40536914111SAndrzej Pietrasiewicz 
40636914111SAndrzej Pietrasiewicz 	return ret;
40736914111SAndrzej Pietrasiewicz }
40836914111SAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(phy_calibrate);
40936914111SAndrzej Pietrasiewicz 
410ff764963SKishon Vijay Abraham I /**
411*aeaac93dSMaxime Ripard  * phy_configure() - Changes the phy parameters
412*aeaac93dSMaxime Ripard  * @phy: the phy returned by phy_get()
413*aeaac93dSMaxime Ripard  * @opts: New configuration to apply
414*aeaac93dSMaxime Ripard  *
415*aeaac93dSMaxime Ripard  * Used to change the PHY parameters. phy_init() must have been called
416*aeaac93dSMaxime Ripard  * on the phy. The configuration will be applied on the current phy
417*aeaac93dSMaxime Ripard  * mode, that can be changed using phy_set_mode().
418*aeaac93dSMaxime Ripard  *
419*aeaac93dSMaxime Ripard  * Returns: 0 if successful, an negative error code otherwise
420*aeaac93dSMaxime Ripard  */
421*aeaac93dSMaxime Ripard int phy_configure(struct phy *phy, union phy_configure_opts *opts)
422*aeaac93dSMaxime Ripard {
423*aeaac93dSMaxime Ripard 	int ret;
424*aeaac93dSMaxime Ripard 
425*aeaac93dSMaxime Ripard 	if (!phy)
426*aeaac93dSMaxime Ripard 		return -EINVAL;
427*aeaac93dSMaxime Ripard 
428*aeaac93dSMaxime Ripard 	if (!phy->ops->configure)
429*aeaac93dSMaxime Ripard 		return -EOPNOTSUPP;
430*aeaac93dSMaxime Ripard 
431*aeaac93dSMaxime Ripard 	mutex_lock(&phy->mutex);
432*aeaac93dSMaxime Ripard 	ret = phy->ops->configure(phy, opts);
433*aeaac93dSMaxime Ripard 	mutex_unlock(&phy->mutex);
434*aeaac93dSMaxime Ripard 
435*aeaac93dSMaxime Ripard 	return ret;
436*aeaac93dSMaxime Ripard }
437*aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_configure);
438*aeaac93dSMaxime Ripard 
439*aeaac93dSMaxime Ripard /**
440*aeaac93dSMaxime Ripard  * phy_validate() - Checks the phy parameters
441*aeaac93dSMaxime Ripard  * @phy: the phy returned by phy_get()
442*aeaac93dSMaxime Ripard  * @mode: phy_mode the configuration is applicable to.
443*aeaac93dSMaxime Ripard  * @submode: PHY submode the configuration is applicable to.
444*aeaac93dSMaxime Ripard  * @opts: Configuration to check
445*aeaac93dSMaxime Ripard  *
446*aeaac93dSMaxime Ripard  * Used to check that the current set of parameters can be handled by
447*aeaac93dSMaxime Ripard  * the phy. Implementations are free to tune the parameters passed as
448*aeaac93dSMaxime Ripard  * arguments if needed by some implementation detail or
449*aeaac93dSMaxime Ripard  * constraints. It will not change any actual configuration of the
450*aeaac93dSMaxime Ripard  * PHY, so calling it as many times as deemed fit will have no side
451*aeaac93dSMaxime Ripard  * effect.
452*aeaac93dSMaxime Ripard  *
453*aeaac93dSMaxime Ripard  * Returns: 0 if successful, an negative error code otherwise
454*aeaac93dSMaxime Ripard  */
455*aeaac93dSMaxime Ripard int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
456*aeaac93dSMaxime Ripard 		 union phy_configure_opts *opts)
457*aeaac93dSMaxime Ripard {
458*aeaac93dSMaxime Ripard 	int ret;
459*aeaac93dSMaxime Ripard 
460*aeaac93dSMaxime Ripard 	if (!phy)
461*aeaac93dSMaxime Ripard 		return -EINVAL;
462*aeaac93dSMaxime Ripard 
463*aeaac93dSMaxime Ripard 	if (!phy->ops->validate)
464*aeaac93dSMaxime Ripard 		return -EOPNOTSUPP;
465*aeaac93dSMaxime Ripard 
466*aeaac93dSMaxime Ripard 	mutex_lock(&phy->mutex);
467*aeaac93dSMaxime Ripard 	ret = phy->ops->validate(phy, mode, submode, opts);
468*aeaac93dSMaxime Ripard 	mutex_unlock(&phy->mutex);
469*aeaac93dSMaxime Ripard 
470*aeaac93dSMaxime Ripard 	return ret;
471*aeaac93dSMaxime Ripard }
472*aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_validate);
473*aeaac93dSMaxime Ripard 
474*aeaac93dSMaxime Ripard /**
4750b3f3b2cSKamil Debski  * _of_phy_get() - lookup and obtain a reference to a phy by phandle
4760b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
477ff764963SKishon Vijay Abraham I  * @index: the index of the phy
478ff764963SKishon Vijay Abraham I  *
479ff764963SKishon Vijay Abraham I  * Returns the phy associated with the given phandle value,
480ff764963SKishon Vijay Abraham I  * after getting a refcount to it or -ENODEV if there is no such phy or
481ff764963SKishon Vijay Abraham I  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
482ff764963SKishon Vijay Abraham I  * not yet loaded. This function uses of_xlate call back function provided
483ff764963SKishon Vijay Abraham I  * while registering the phy_provider to find the phy instance.
484ff764963SKishon Vijay Abraham I  */
4850b3f3b2cSKamil Debski static struct phy *_of_phy_get(struct device_node *np, int index)
486ff764963SKishon Vijay Abraham I {
487ff764963SKishon Vijay Abraham I 	int ret;
488ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
489ff764963SKishon Vijay Abraham I 	struct phy *phy = NULL;
490ff764963SKishon Vijay Abraham I 	struct of_phandle_args args;
491ff764963SKishon Vijay Abraham I 
4920b3f3b2cSKamil Debski 	ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
493ff764963SKishon Vijay Abraham I 		index, &args);
4940b3f3b2cSKamil Debski 	if (ret)
495ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENODEV);
496ff764963SKishon Vijay Abraham I 
497b7563e27SArnd Bergmann 	/* This phy type handled by the usb-phy subsystem for now */
498b7563e27SArnd Bergmann 	if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
499b7563e27SArnd Bergmann 		return ERR_PTR(-ENODEV);
500b7563e27SArnd Bergmann 
501ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
502ff764963SKishon Vijay Abraham I 	phy_provider = of_phy_provider_lookup(args.np);
503ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
504ff764963SKishon Vijay Abraham I 		phy = ERR_PTR(-EPROBE_DEFER);
50533f434d2SAxel Lin 		goto out_unlock;
50633f434d2SAxel Lin 	}
50733f434d2SAxel Lin 
50833f434d2SAxel Lin 	if (!of_device_is_available(args.np)) {
50933f434d2SAxel Lin 		dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
51033f434d2SAxel Lin 		phy = ERR_PTR(-ENODEV);
51133f434d2SAxel Lin 		goto out_put_module;
512ff764963SKishon Vijay Abraham I 	}
513ff764963SKishon Vijay Abraham I 
514ff764963SKishon Vijay Abraham I 	phy = phy_provider->of_xlate(phy_provider->dev, &args);
51533f434d2SAxel Lin 
51633f434d2SAxel Lin out_put_module:
517ff764963SKishon Vijay Abraham I 	module_put(phy_provider->owner);
518ff764963SKishon Vijay Abraham I 
51933f434d2SAxel Lin out_unlock:
520ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
521ff764963SKishon Vijay Abraham I 	of_node_put(args.np);
522ff764963SKishon Vijay Abraham I 
523ff764963SKishon Vijay Abraham I 	return phy;
524ff764963SKishon Vijay Abraham I }
525ff764963SKishon Vijay Abraham I 
526ff764963SKishon Vijay Abraham I /**
5270b3f3b2cSKamil Debski  * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
5280b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
5290b3f3b2cSKamil Debski  * @con_id: name of the phy from device's point of view
5300b3f3b2cSKamil Debski  *
5310b3f3b2cSKamil Debski  * Returns the phy driver, after getting a refcount to it; or
5320b3f3b2cSKamil Debski  * -ENODEV if there is no such phy. The caller is responsible for
5330b3f3b2cSKamil Debski  * calling phy_put() to release that count.
5340b3f3b2cSKamil Debski  */
5350b3f3b2cSKamil Debski struct phy *of_phy_get(struct device_node *np, const char *con_id)
5360b3f3b2cSKamil Debski {
5370b3f3b2cSKamil Debski 	struct phy *phy = NULL;
5380b3f3b2cSKamil Debski 	int index = 0;
5390b3f3b2cSKamil Debski 
5400b3f3b2cSKamil Debski 	if (con_id)
5410b3f3b2cSKamil Debski 		index = of_property_match_string(np, "phy-names", con_id);
5420b3f3b2cSKamil Debski 
5430b3f3b2cSKamil Debski 	phy = _of_phy_get(np, index);
5440b3f3b2cSKamil Debski 	if (IS_ERR(phy))
5450b3f3b2cSKamil Debski 		return phy;
5460b3f3b2cSKamil Debski 
5470b3f3b2cSKamil Debski 	if (!try_module_get(phy->ops->owner))
5480b3f3b2cSKamil Debski 		return ERR_PTR(-EPROBE_DEFER);
5490b3f3b2cSKamil Debski 
5500b3f3b2cSKamil Debski 	get_device(&phy->dev);
5510b3f3b2cSKamil Debski 
5520b3f3b2cSKamil Debski 	return phy;
5530b3f3b2cSKamil Debski }
5540b3f3b2cSKamil Debski EXPORT_SYMBOL_GPL(of_phy_get);
5550b3f3b2cSKamil Debski 
5560b3f3b2cSKamil Debski /**
557ff764963SKishon Vijay Abraham I  * phy_put() - release the PHY
558ff764963SKishon Vijay Abraham I  * @phy: the phy returned by phy_get()
559ff764963SKishon Vijay Abraham I  *
560ff764963SKishon Vijay Abraham I  * Releases a refcount the caller received from phy_get().
561ff764963SKishon Vijay Abraham I  */
562ff764963SKishon Vijay Abraham I void phy_put(struct phy *phy)
563ff764963SKishon Vijay Abraham I {
56404c2facaSAndrew Lunn 	if (!phy || IS_ERR(phy))
565ff764963SKishon Vijay Abraham I 		return;
566ff764963SKishon Vijay Abraham I 
567ff764963SKishon Vijay Abraham I 	module_put(phy->ops->owner);
568ff764963SKishon Vijay Abraham I 	put_device(&phy->dev);
569ff764963SKishon Vijay Abraham I }
570ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_put);
571ff764963SKishon Vijay Abraham I 
572ff764963SKishon Vijay Abraham I /**
573ff764963SKishon Vijay Abraham I  * devm_phy_put() - release the PHY
574ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
575ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
576ff764963SKishon Vijay Abraham I  *
577ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_put
578ff764963SKishon Vijay Abraham I  * to release the phy.
579ff764963SKishon Vijay Abraham I  */
580ff764963SKishon Vijay Abraham I void devm_phy_put(struct device *dev, struct phy *phy)
581ff764963SKishon Vijay Abraham I {
582ff764963SKishon Vijay Abraham I 	int r;
583ff764963SKishon Vijay Abraham I 
58404c2facaSAndrew Lunn 	if (!phy)
58504c2facaSAndrew Lunn 		return;
58604c2facaSAndrew Lunn 
587ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
588ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
589ff764963SKishon Vijay Abraham I }
590ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_put);
591ff764963SKishon Vijay Abraham I 
592ff764963SKishon Vijay Abraham I /**
593ff764963SKishon Vijay Abraham I  * of_phy_simple_xlate() - returns the phy instance from phy provider
594ff764963SKishon Vijay Abraham I  * @dev: the PHY provider device
595ff764963SKishon Vijay Abraham I  * @args: of_phandle_args (not used here)
596ff764963SKishon Vijay Abraham I  *
597ff764963SKishon Vijay Abraham I  * Intended to be used by phy provider for the common case where #phy-cells is
598ff764963SKishon Vijay Abraham I  * 0. For other cases where #phy-cells is greater than '0', the phy provider
599ff764963SKishon Vijay Abraham I  * should provide a custom of_xlate function that reads the *args* and returns
600ff764963SKishon Vijay Abraham I  * the appropriate phy.
601ff764963SKishon Vijay Abraham I  */
602ff764963SKishon Vijay Abraham I struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
603ff764963SKishon Vijay Abraham I 	*args)
604ff764963SKishon Vijay Abraham I {
605ff764963SKishon Vijay Abraham I 	struct phy *phy;
606ff764963SKishon Vijay Abraham I 	struct class_dev_iter iter;
607ff764963SKishon Vijay Abraham I 
608ff764963SKishon Vijay Abraham I 	class_dev_iter_init(&iter, phy_class, NULL, NULL);
609ff764963SKishon Vijay Abraham I 	while ((dev = class_dev_iter_next(&iter))) {
610ff764963SKishon Vijay Abraham I 		phy = to_phy(dev);
611491e0490SKishon Vijay Abraham I 		if (args->np != phy->dev.of_node)
612ff764963SKishon Vijay Abraham I 			continue;
613ff764963SKishon Vijay Abraham I 
614ff764963SKishon Vijay Abraham I 		class_dev_iter_exit(&iter);
615ff764963SKishon Vijay Abraham I 		return phy;
616ff764963SKishon Vijay Abraham I 	}
617ff764963SKishon Vijay Abraham I 
618ff764963SKishon Vijay Abraham I 	class_dev_iter_exit(&iter);
619ff764963SKishon Vijay Abraham I 	return ERR_PTR(-ENODEV);
620ff764963SKishon Vijay Abraham I }
621ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
622ff764963SKishon Vijay Abraham I 
623ff764963SKishon Vijay Abraham I /**
624ff764963SKishon Vijay Abraham I  * phy_get() - lookup and obtain a reference to a phy.
625ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
626ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or the name of the controller
627ff764963SKishon Vijay Abraham I  * port for non-dt case
628ff764963SKishon Vijay Abraham I  *
629ff764963SKishon Vijay Abraham I  * Returns the phy driver, after getting a refcount to it; or
630ff764963SKishon Vijay Abraham I  * -ENODEV if there is no such phy.  The caller is responsible for
631ff764963SKishon Vijay Abraham I  * calling phy_put() to release that count.
632ff764963SKishon Vijay Abraham I  */
633ff764963SKishon Vijay Abraham I struct phy *phy_get(struct device *dev, const char *string)
634ff764963SKishon Vijay Abraham I {
635ff764963SKishon Vijay Abraham I 	int index = 0;
636d18c9604SKishon Vijay Abraham I 	struct phy *phy;
637ff764963SKishon Vijay Abraham I 
638ff764963SKishon Vijay Abraham I 	if (string == NULL) {
639ff764963SKishon Vijay Abraham I 		dev_WARN(dev, "missing string\n");
640ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EINVAL);
641ff764963SKishon Vijay Abraham I 	}
642ff764963SKishon Vijay Abraham I 
643ff764963SKishon Vijay Abraham I 	if (dev->of_node) {
644ff764963SKishon Vijay Abraham I 		index = of_property_match_string(dev->of_node, "phy-names",
645ff764963SKishon Vijay Abraham I 			string);
6460b3f3b2cSKamil Debski 		phy = _of_phy_get(dev->of_node, index);
647ff764963SKishon Vijay Abraham I 	} else {
648b7bc15b9SHeikki Krogerus 		phy = phy_find(dev, string);
649f40037fdSHans de Goede 	}
650f40037fdSHans de Goede 	if (IS_ERR(phy))
651ff764963SKishon Vijay Abraham I 		return phy;
652ff764963SKishon Vijay Abraham I 
653ff764963SKishon Vijay Abraham I 	if (!try_module_get(phy->ops->owner))
654ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EPROBE_DEFER);
655ff764963SKishon Vijay Abraham I 
656ff764963SKishon Vijay Abraham I 	get_device(&phy->dev);
657ff764963SKishon Vijay Abraham I 
658ff764963SKishon Vijay Abraham I 	return phy;
659ff764963SKishon Vijay Abraham I }
660ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_get);
661ff764963SKishon Vijay Abraham I 
662ff764963SKishon Vijay Abraham I /**
663788a4d56SAndrew Lunn  * phy_optional_get() - lookup and obtain a reference to an optional phy.
664788a4d56SAndrew Lunn  * @dev: device that requests this phy
665788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or the name of the controller
666788a4d56SAndrew Lunn  * port for non-dt case
667788a4d56SAndrew Lunn  *
668788a4d56SAndrew Lunn  * Returns the phy driver, after getting a refcount to it; or
669788a4d56SAndrew Lunn  * NULL if there is no such phy.  The caller is responsible for
670788a4d56SAndrew Lunn  * calling phy_put() to release that count.
671788a4d56SAndrew Lunn  */
672788a4d56SAndrew Lunn struct phy *phy_optional_get(struct device *dev, const char *string)
673788a4d56SAndrew Lunn {
674788a4d56SAndrew Lunn 	struct phy *phy = phy_get(dev, string);
675788a4d56SAndrew Lunn 
676f83be4c3SAxel Lin 	if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
677788a4d56SAndrew Lunn 		phy = NULL;
678788a4d56SAndrew Lunn 
679788a4d56SAndrew Lunn 	return phy;
680788a4d56SAndrew Lunn }
681788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(phy_optional_get);
682788a4d56SAndrew Lunn 
683788a4d56SAndrew Lunn /**
684ff764963SKishon Vijay Abraham I  * devm_phy_get() - lookup and obtain a reference to a phy.
685ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
686ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or phy device name
687ff764963SKishon Vijay Abraham I  * for non-dt case
688ff764963SKishon Vijay Abraham I  *
689ff764963SKishon Vijay Abraham I  * Gets the phy using phy_get(), and associates a device with it using
690ff764963SKishon Vijay Abraham I  * devres. On driver detach, release function is invoked on the devres data,
691ff764963SKishon Vijay Abraham I  * then, devres data is freed.
692ff764963SKishon Vijay Abraham I  */
693ff764963SKishon Vijay Abraham I struct phy *devm_phy_get(struct device *dev, const char *string)
694ff764963SKishon Vijay Abraham I {
695ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
696ff764963SKishon Vijay Abraham I 
697ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
698ff764963SKishon Vijay Abraham I 	if (!ptr)
699ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
700ff764963SKishon Vijay Abraham I 
701ff764963SKishon Vijay Abraham I 	phy = phy_get(dev, string);
702ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
703ff764963SKishon Vijay Abraham I 		*ptr = phy;
704ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
705ff764963SKishon Vijay Abraham I 	} else {
706ff764963SKishon Vijay Abraham I 		devres_free(ptr);
707ff764963SKishon Vijay Abraham I 	}
708ff764963SKishon Vijay Abraham I 
709ff764963SKishon Vijay Abraham I 	return phy;
710ff764963SKishon Vijay Abraham I }
711ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_get);
712ff764963SKishon Vijay Abraham I 
713ff764963SKishon Vijay Abraham I /**
714788a4d56SAndrew Lunn  * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
715788a4d56SAndrew Lunn  * @dev: device that requests this phy
716788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or phy device name
717788a4d56SAndrew Lunn  * for non-dt case
718788a4d56SAndrew Lunn  *
719788a4d56SAndrew Lunn  * Gets the phy using phy_get(), and associates a device with it using
720788a4d56SAndrew Lunn  * devres. On driver detach, release function is invoked on the devres
721788a4d56SAndrew Lunn  * data, then, devres data is freed. This differs to devm_phy_get() in
722788a4d56SAndrew Lunn  * that if the phy does not exist, it is not considered an error and
723788a4d56SAndrew Lunn  * -ENODEV will not be returned. Instead the NULL phy is returned,
724788a4d56SAndrew Lunn  * which can be passed to all other phy consumer calls.
725788a4d56SAndrew Lunn  */
726788a4d56SAndrew Lunn struct phy *devm_phy_optional_get(struct device *dev, const char *string)
727788a4d56SAndrew Lunn {
728788a4d56SAndrew Lunn 	struct phy *phy = devm_phy_get(dev, string);
729788a4d56SAndrew Lunn 
730f83be4c3SAxel Lin 	if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
731788a4d56SAndrew Lunn 		phy = NULL;
732788a4d56SAndrew Lunn 
733788a4d56SAndrew Lunn 	return phy;
734788a4d56SAndrew Lunn }
735788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(devm_phy_optional_get);
736788a4d56SAndrew Lunn 
737788a4d56SAndrew Lunn /**
738b5d682f4SKamil Debski  * devm_of_phy_get() - lookup and obtain a reference to a phy.
739b5d682f4SKamil Debski  * @dev: device that requests this phy
740b5d682f4SKamil Debski  * @np: node containing the phy
741b5d682f4SKamil Debski  * @con_id: name of the phy from device's point of view
742b5d682f4SKamil Debski  *
743b5d682f4SKamil Debski  * Gets the phy using of_phy_get(), and associates a device with it using
744b5d682f4SKamil Debski  * devres. On driver detach, release function is invoked on the devres data,
745b5d682f4SKamil Debski  * then, devres data is freed.
746b5d682f4SKamil Debski  */
747b5d682f4SKamil Debski struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
748b5d682f4SKamil Debski 			    const char *con_id)
749b5d682f4SKamil Debski {
750b5d682f4SKamil Debski 	struct phy **ptr, *phy;
751b5d682f4SKamil Debski 
752b5d682f4SKamil Debski 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
753b5d682f4SKamil Debski 	if (!ptr)
754b5d682f4SKamil Debski 		return ERR_PTR(-ENOMEM);
755b5d682f4SKamil Debski 
756b5d682f4SKamil Debski 	phy = of_phy_get(np, con_id);
757b5d682f4SKamil Debski 	if (!IS_ERR(phy)) {
758b5d682f4SKamil Debski 		*ptr = phy;
759b5d682f4SKamil Debski 		devres_add(dev, ptr);
760b5d682f4SKamil Debski 	} else {
761b5d682f4SKamil Debski 		devres_free(ptr);
762b5d682f4SKamil Debski 	}
763b5d682f4SKamil Debski 
764b5d682f4SKamil Debski 	return phy;
765b5d682f4SKamil Debski }
766b5d682f4SKamil Debski EXPORT_SYMBOL_GPL(devm_of_phy_get);
767b5d682f4SKamil Debski 
768b5d682f4SKamil Debski /**
7696be109b3SArun Ramamurthy  * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
7706be109b3SArun Ramamurthy  * @dev: device that requests this phy
7716be109b3SArun Ramamurthy  * @np: node containing the phy
7726be109b3SArun Ramamurthy  * @index: index of the phy
7736be109b3SArun Ramamurthy  *
77470874462SChunfeng Yun  * Gets the phy using _of_phy_get(), then gets a refcount to it,
77570874462SChunfeng Yun  * and associates a device with it using devres. On driver detach,
77670874462SChunfeng Yun  * release function is invoked on the devres data,
7776be109b3SArun Ramamurthy  * then, devres data is freed.
7786be109b3SArun Ramamurthy  *
7796be109b3SArun Ramamurthy  */
7806be109b3SArun Ramamurthy struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
7816be109b3SArun Ramamurthy 				     int index)
7826be109b3SArun Ramamurthy {
7836be109b3SArun Ramamurthy 	struct phy **ptr, *phy;
7846be109b3SArun Ramamurthy 
7856be109b3SArun Ramamurthy 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
7866be109b3SArun Ramamurthy 	if (!ptr)
7876be109b3SArun Ramamurthy 		return ERR_PTR(-ENOMEM);
7886be109b3SArun Ramamurthy 
7896be109b3SArun Ramamurthy 	phy = _of_phy_get(np, index);
79070874462SChunfeng Yun 	if (IS_ERR(phy)) {
79170874462SChunfeng Yun 		devres_free(ptr);
79270874462SChunfeng Yun 		return phy;
79370874462SChunfeng Yun 	}
79470874462SChunfeng Yun 
79570874462SChunfeng Yun 	if (!try_module_get(phy->ops->owner)) {
79670874462SChunfeng Yun 		devres_free(ptr);
79770874462SChunfeng Yun 		return ERR_PTR(-EPROBE_DEFER);
79870874462SChunfeng Yun 	}
79970874462SChunfeng Yun 
80070874462SChunfeng Yun 	get_device(&phy->dev);
80170874462SChunfeng Yun 
8026be109b3SArun Ramamurthy 	*ptr = phy;
8036be109b3SArun Ramamurthy 	devres_add(dev, ptr);
8046be109b3SArun Ramamurthy 
8056be109b3SArun Ramamurthy 	return phy;
8066be109b3SArun Ramamurthy }
8076be109b3SArun Ramamurthy EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
8086be109b3SArun Ramamurthy 
8096be109b3SArun Ramamurthy /**
810ff764963SKishon Vijay Abraham I  * phy_create() - create a new phy
811ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
812f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
813ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
814ff764963SKishon Vijay Abraham I  *
815ff764963SKishon Vijay Abraham I  * Called to create a phy using phy framework.
816ff764963SKishon Vijay Abraham I  */
817f0ed8176SKishon Vijay Abraham I struct phy *phy_create(struct device *dev, struct device_node *node,
818dbc98635SHeikki Krogerus 		       const struct phy_ops *ops)
819ff764963SKishon Vijay Abraham I {
820ff764963SKishon Vijay Abraham I 	int ret;
821ff764963SKishon Vijay Abraham I 	int id;
822ff764963SKishon Vijay Abraham I 	struct phy *phy;
823ff764963SKishon Vijay Abraham I 
82452797d29SDan Carpenter 	if (WARN_ON(!dev))
82552797d29SDan Carpenter 		return ERR_PTR(-EINVAL);
826ff764963SKishon Vijay Abraham I 
827ff764963SKishon Vijay Abraham I 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
82852797d29SDan Carpenter 	if (!phy)
82952797d29SDan Carpenter 		return ERR_PTR(-ENOMEM);
830ff764963SKishon Vijay Abraham I 
831ff764963SKishon Vijay Abraham I 	id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
832ff764963SKishon Vijay Abraham I 	if (id < 0) {
833ff764963SKishon Vijay Abraham I 		dev_err(dev, "unable to get id\n");
834ff764963SKishon Vijay Abraham I 		ret = id;
83552797d29SDan Carpenter 		goto free_phy;
836ff764963SKishon Vijay Abraham I 	}
837ff764963SKishon Vijay Abraham I 
838ff764963SKishon Vijay Abraham I 	device_initialize(&phy->dev);
839ff764963SKishon Vijay Abraham I 	mutex_init(&phy->mutex);
840ff764963SKishon Vijay Abraham I 
841ff764963SKishon Vijay Abraham I 	phy->dev.class = phy_class;
842ff764963SKishon Vijay Abraham I 	phy->dev.parent = dev;
843f0ed8176SKishon Vijay Abraham I 	phy->dev.of_node = node ?: dev->of_node;
844ff764963SKishon Vijay Abraham I 	phy->id = id;
845ff764963SKishon Vijay Abraham I 	phy->ops = ops;
846ff764963SKishon Vijay Abraham I 
847ff764963SKishon Vijay Abraham I 	ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
848ff764963SKishon Vijay Abraham I 	if (ret)
84952797d29SDan Carpenter 		goto put_dev;
850ff764963SKishon Vijay Abraham I 
85187006dd6SDmitry Torokhov 	/* phy-supply */
85287006dd6SDmitry Torokhov 	phy->pwr = regulator_get_optional(&phy->dev, "phy");
85387006dd6SDmitry Torokhov 	if (IS_ERR(phy->pwr)) {
85487006dd6SDmitry Torokhov 		ret = PTR_ERR(phy->pwr);
85587006dd6SDmitry Torokhov 		if (ret == -EPROBE_DEFER)
85687006dd6SDmitry Torokhov 			goto put_dev;
85787006dd6SDmitry Torokhov 
85887006dd6SDmitry Torokhov 		phy->pwr = NULL;
85987006dd6SDmitry Torokhov 	}
86087006dd6SDmitry Torokhov 
861ff764963SKishon Vijay Abraham I 	ret = device_add(&phy->dev);
862ff764963SKishon Vijay Abraham I 	if (ret)
86352797d29SDan Carpenter 		goto put_dev;
864ff764963SKishon Vijay Abraham I 
865ff764963SKishon Vijay Abraham I 	if (pm_runtime_enabled(dev)) {
866ff764963SKishon Vijay Abraham I 		pm_runtime_enable(&phy->dev);
867ff764963SKishon Vijay Abraham I 		pm_runtime_no_callbacks(&phy->dev);
868ff764963SKishon Vijay Abraham I 	}
869ff764963SKishon Vijay Abraham I 
870ff764963SKishon Vijay Abraham I 	return phy;
871ff764963SKishon Vijay Abraham I 
87252797d29SDan Carpenter put_dev:
873e73b49f1SRoger Quadros 	put_device(&phy->dev);  /* calls phy_release() which frees resources */
874e73b49f1SRoger Quadros 	return ERR_PTR(ret);
875e73b49f1SRoger Quadros 
87652797d29SDan Carpenter free_phy:
877ff764963SKishon Vijay Abraham I 	kfree(phy);
878ff764963SKishon Vijay Abraham I 	return ERR_PTR(ret);
879ff764963SKishon Vijay Abraham I }
880ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_create);
881ff764963SKishon Vijay Abraham I 
882ff764963SKishon Vijay Abraham I /**
883ff764963SKishon Vijay Abraham I  * devm_phy_create() - create a new phy
884ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
885f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
886ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
887ff764963SKishon Vijay Abraham I  *
888ff764963SKishon Vijay Abraham I  * Creates a new PHY device adding it to the PHY class.
889ff764963SKishon Vijay Abraham I  * While at that, it also associates the device with the phy using devres.
890ff764963SKishon Vijay Abraham I  * On driver detach, release function is invoked on the devres data,
891ff764963SKishon Vijay Abraham I  * then, devres data is freed.
892ff764963SKishon Vijay Abraham I  */
893f0ed8176SKishon Vijay Abraham I struct phy *devm_phy_create(struct device *dev, struct device_node *node,
894dbc98635SHeikki Krogerus 			    const struct phy_ops *ops)
895ff764963SKishon Vijay Abraham I {
896ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
897ff764963SKishon Vijay Abraham I 
898ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
899ff764963SKishon Vijay Abraham I 	if (!ptr)
900ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
901ff764963SKishon Vijay Abraham I 
902dbc98635SHeikki Krogerus 	phy = phy_create(dev, node, ops);
903ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
904ff764963SKishon Vijay Abraham I 		*ptr = phy;
905ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
906ff764963SKishon Vijay Abraham I 	} else {
907ff764963SKishon Vijay Abraham I 		devres_free(ptr);
908ff764963SKishon Vijay Abraham I 	}
909ff764963SKishon Vijay Abraham I 
910ff764963SKishon Vijay Abraham I 	return phy;
911ff764963SKishon Vijay Abraham I }
912ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_create);
913ff764963SKishon Vijay Abraham I 
914ff764963SKishon Vijay Abraham I /**
915ff764963SKishon Vijay Abraham I  * phy_destroy() - destroy the phy
916ff764963SKishon Vijay Abraham I  * @phy: the phy to be destroyed
917ff764963SKishon Vijay Abraham I  *
918ff764963SKishon Vijay Abraham I  * Called to destroy the phy.
919ff764963SKishon Vijay Abraham I  */
920ff764963SKishon Vijay Abraham I void phy_destroy(struct phy *phy)
921ff764963SKishon Vijay Abraham I {
922ff764963SKishon Vijay Abraham I 	pm_runtime_disable(&phy->dev);
923ff764963SKishon Vijay Abraham I 	device_unregister(&phy->dev);
924ff764963SKishon Vijay Abraham I }
925ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_destroy);
926ff764963SKishon Vijay Abraham I 
927ff764963SKishon Vijay Abraham I /**
928ff764963SKishon Vijay Abraham I  * devm_phy_destroy() - destroy the PHY
929ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
930ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
931ff764963SKishon Vijay Abraham I  *
932ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_destroy
933ff764963SKishon Vijay Abraham I  * to destroy the phy.
934ff764963SKishon Vijay Abraham I  */
935ff764963SKishon Vijay Abraham I void devm_phy_destroy(struct device *dev, struct phy *phy)
936ff764963SKishon Vijay Abraham I {
937ff764963SKishon Vijay Abraham I 	int r;
938ff764963SKishon Vijay Abraham I 
939ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
940ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
941ff764963SKishon Vijay Abraham I }
942ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_destroy);
943ff764963SKishon Vijay Abraham I 
944ff764963SKishon Vijay Abraham I /**
945ff764963SKishon Vijay Abraham I  * __of_phy_provider_register() - create/register phy provider with the framework
946ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
9471140f7c8SThierry Reding  * @children: device node containing children (if different from dev->of_node)
948ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
949ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
950ff764963SKishon Vijay Abraham I  *
951ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
952ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
953ff764963SKishon Vijay Abraham I  * phy provider.
9541140f7c8SThierry Reding  *
9551140f7c8SThierry Reding  * If the PHY provider doesn't nest children directly but uses a separate
9561140f7c8SThierry Reding  * child node to contain the individual children, the @children parameter
9571140f7c8SThierry Reding  * can be used to override the default. If NULL, the default (dev->of_node)
9581140f7c8SThierry Reding  * will be used. If non-NULL, the device node must be a child (or further
9591140f7c8SThierry Reding  * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
9601140f7c8SThierry Reding  * error code is returned.
961ff764963SKishon Vijay Abraham I  */
962ff764963SKishon Vijay Abraham I struct phy_provider *__of_phy_provider_register(struct device *dev,
9631140f7c8SThierry Reding 	struct device_node *children, struct module *owner,
9641140f7c8SThierry Reding 	struct phy * (*of_xlate)(struct device *dev,
965ff764963SKishon Vijay Abraham I 				 struct of_phandle_args *args))
966ff764963SKishon Vijay Abraham I {
967ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
968ff764963SKishon Vijay Abraham I 
9691140f7c8SThierry Reding 	/*
9701140f7c8SThierry Reding 	 * If specified, the device node containing the children must itself
9711140f7c8SThierry Reding 	 * be the provider's device node or a child (or further descendant)
9721140f7c8SThierry Reding 	 * thereof.
9731140f7c8SThierry Reding 	 */
9741140f7c8SThierry Reding 	if (children) {
9751140f7c8SThierry Reding 		struct device_node *parent = of_node_get(children), *next;
9761140f7c8SThierry Reding 
9771140f7c8SThierry Reding 		while (parent) {
9781140f7c8SThierry Reding 			if (parent == dev->of_node)
9791140f7c8SThierry Reding 				break;
9801140f7c8SThierry Reding 
9811140f7c8SThierry Reding 			next = of_get_parent(parent);
9821140f7c8SThierry Reding 			of_node_put(parent);
9831140f7c8SThierry Reding 			parent = next;
9841140f7c8SThierry Reding 		}
9851140f7c8SThierry Reding 
9861140f7c8SThierry Reding 		if (!parent)
9871140f7c8SThierry Reding 			return ERR_PTR(-EINVAL);
9881140f7c8SThierry Reding 
9891140f7c8SThierry Reding 		of_node_put(parent);
9901140f7c8SThierry Reding 	} else {
9911140f7c8SThierry Reding 		children = dev->of_node;
9921140f7c8SThierry Reding 	}
9931140f7c8SThierry Reding 
994ff764963SKishon Vijay Abraham I 	phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
995ff764963SKishon Vijay Abraham I 	if (!phy_provider)
996ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
997ff764963SKishon Vijay Abraham I 
998ff764963SKishon Vijay Abraham I 	phy_provider->dev = dev;
9991140f7c8SThierry Reding 	phy_provider->children = of_node_get(children);
1000ff764963SKishon Vijay Abraham I 	phy_provider->owner = owner;
1001ff764963SKishon Vijay Abraham I 	phy_provider->of_xlate = of_xlate;
1002ff764963SKishon Vijay Abraham I 
1003ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
1004ff764963SKishon Vijay Abraham I 	list_add_tail(&phy_provider->list, &phy_provider_list);
1005ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
1006ff764963SKishon Vijay Abraham I 
1007ff764963SKishon Vijay Abraham I 	return phy_provider;
1008ff764963SKishon Vijay Abraham I }
1009ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__of_phy_provider_register);
1010ff764963SKishon Vijay Abraham I 
1011ff764963SKishon Vijay Abraham I /**
1012ff764963SKishon Vijay Abraham I  * __devm_of_phy_provider_register() - create/register phy provider with the
1013ff764963SKishon Vijay Abraham I  * framework
1014ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
1015ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
1016ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
1017ff764963SKishon Vijay Abraham I  *
1018ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
1019ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
1020ff764963SKishon Vijay Abraham I  * phy provider. While at that, it also associates the device with the
1021ff764963SKishon Vijay Abraham I  * phy provider using devres. On driver detach, release function is invoked
1022ff764963SKishon Vijay Abraham I  * on the devres data, then, devres data is freed.
1023ff764963SKishon Vijay Abraham I  */
1024ff764963SKishon Vijay Abraham I struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
10251140f7c8SThierry Reding 	struct device_node *children, struct module *owner,
10261140f7c8SThierry Reding 	struct phy * (*of_xlate)(struct device *dev,
1027ff764963SKishon Vijay Abraham I 				 struct of_phandle_args *args))
1028ff764963SKishon Vijay Abraham I {
1029ff764963SKishon Vijay Abraham I 	struct phy_provider **ptr, *phy_provider;
1030ff764963SKishon Vijay Abraham I 
1031ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
1032ff764963SKishon Vijay Abraham I 	if (!ptr)
1033ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
1034ff764963SKishon Vijay Abraham I 
10351140f7c8SThierry Reding 	phy_provider = __of_phy_provider_register(dev, children, owner,
10361140f7c8SThierry Reding 						  of_xlate);
1037ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy_provider)) {
1038ff764963SKishon Vijay Abraham I 		*ptr = phy_provider;
1039ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
1040ff764963SKishon Vijay Abraham I 	} else {
1041ff764963SKishon Vijay Abraham I 		devres_free(ptr);
1042ff764963SKishon Vijay Abraham I 	}
1043ff764963SKishon Vijay Abraham I 
1044ff764963SKishon Vijay Abraham I 	return phy_provider;
1045ff764963SKishon Vijay Abraham I }
1046ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
1047ff764963SKishon Vijay Abraham I 
1048ff764963SKishon Vijay Abraham I /**
1049ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister() - unregister phy provider from the framework
1050ff764963SKishon Vijay Abraham I  * @phy_provider: phy provider returned by of_phy_provider_register()
1051ff764963SKishon Vijay Abraham I  *
1052ff764963SKishon Vijay Abraham I  * Removes the phy_provider created using of_phy_provider_register().
1053ff764963SKishon Vijay Abraham I  */
1054ff764963SKishon Vijay Abraham I void of_phy_provider_unregister(struct phy_provider *phy_provider)
1055ff764963SKishon Vijay Abraham I {
1056ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider))
1057ff764963SKishon Vijay Abraham I 		return;
1058ff764963SKishon Vijay Abraham I 
1059ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
1060ff764963SKishon Vijay Abraham I 	list_del(&phy_provider->list);
10611140f7c8SThierry Reding 	of_node_put(phy_provider->children);
1062ff764963SKishon Vijay Abraham I 	kfree(phy_provider);
1063ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
1064ff764963SKishon Vijay Abraham I }
1065ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
1066ff764963SKishon Vijay Abraham I 
1067ff764963SKishon Vijay Abraham I /**
1068ff764963SKishon Vijay Abraham I  * devm_of_phy_provider_unregister() - remove phy provider from the framework
1069ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
1070ff764963SKishon Vijay Abraham I  *
1071ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy provider and invokes
1072ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister to unregister the phy provider.
1073ff764963SKishon Vijay Abraham I  */
1074ff764963SKishon Vijay Abraham I void devm_of_phy_provider_unregister(struct device *dev,
1075ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider) {
1076ff764963SKishon Vijay Abraham I 	int r;
1077ff764963SKishon Vijay Abraham I 
1078ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
1079ff764963SKishon Vijay Abraham I 		phy_provider);
1080ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
1081ff764963SKishon Vijay Abraham I }
1082ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
1083ff764963SKishon Vijay Abraham I 
1084ff764963SKishon Vijay Abraham I /**
1085ff764963SKishon Vijay Abraham I  * phy_release() - release the phy
1086ff764963SKishon Vijay Abraham I  * @dev: the dev member within phy
1087ff764963SKishon Vijay Abraham I  *
1088ff764963SKishon Vijay Abraham I  * When the last reference to the device is removed, it is called
1089ff764963SKishon Vijay Abraham I  * from the embedded kobject as release method.
1090ff764963SKishon Vijay Abraham I  */
1091ff764963SKishon Vijay Abraham I static void phy_release(struct device *dev)
1092ff764963SKishon Vijay Abraham I {
1093ff764963SKishon Vijay Abraham I 	struct phy *phy;
1094ff764963SKishon Vijay Abraham I 
1095ff764963SKishon Vijay Abraham I 	phy = to_phy(dev);
1096ff764963SKishon Vijay Abraham I 	dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
10973be88125SRoger Quadros 	regulator_put(phy->pwr);
1098e73b49f1SRoger Quadros 	ida_simple_remove(&phy_ida, phy->id);
1099ff764963SKishon Vijay Abraham I 	kfree(phy);
1100ff764963SKishon Vijay Abraham I }
1101ff764963SKishon Vijay Abraham I 
1102ff764963SKishon Vijay Abraham I static int __init phy_core_init(void)
1103ff764963SKishon Vijay Abraham I {
1104ff764963SKishon Vijay Abraham I 	phy_class = class_create(THIS_MODULE, "phy");
1105ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_class)) {
1106ff764963SKishon Vijay Abraham I 		pr_err("failed to create phy class --> %ld\n",
1107ff764963SKishon Vijay Abraham I 			PTR_ERR(phy_class));
1108ff764963SKishon Vijay Abraham I 		return PTR_ERR(phy_class);
1109ff764963SKishon Vijay Abraham I 	}
1110ff764963SKishon Vijay Abraham I 
1111ff764963SKishon Vijay Abraham I 	phy_class->dev_release = phy_release;
1112ff764963SKishon Vijay Abraham I 
1113ff764963SKishon Vijay Abraham I 	return 0;
1114ff764963SKishon Vijay Abraham I }
1115ff764963SKishon Vijay Abraham I module_init(phy_core_init);
1116ff764963SKishon Vijay Abraham I 
1117ff764963SKishon Vijay Abraham I static void __exit phy_core_exit(void)
1118ff764963SKishon Vijay Abraham I {
1119ff764963SKishon Vijay Abraham I 	class_destroy(phy_class);
1120ff764963SKishon Vijay Abraham I }
1121ff764963SKishon Vijay Abraham I module_exit(phy_core_exit);
1122ff764963SKishon Vijay Abraham I 
1123ff764963SKishon Vijay Abraham I MODULE_DESCRIPTION("Generic PHY Framework");
1124ff764963SKishon Vijay Abraham I MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
1125ff764963SKishon Vijay Abraham I MODULE_LICENSE("GPL v2");
1126