xref: /openbmc/linux/drivers/phy/phy-core.c (revision f1b8d3358af77fc453d6b781f40ee7342a230672)
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 
32987351e1SAlexandre 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 
232*f1b8d335SJules Maselbas /**
233*f1b8d335SJules Maselbas  * phy_init - phy internal initialization before phy operation
234*f1b8d335SJules Maselbas  * @phy: the phy returned by phy_get()
235*f1b8d335SJules Maselbas  *
236*f1b8d335SJules Maselbas  * Used to allow phy's driver to perform phy internal initialization,
237*f1b8d335SJules Maselbas  * such as PLL block powering, clock initialization or anything that's
238*f1b8d335SJules Maselbas  * is required by the phy to perform the start of operation.
239*f1b8d335SJules Maselbas  * Must be called before phy_power_on().
240*f1b8d335SJules Maselbas  *
241*f1b8d335SJules Maselbas  * Return: %0 if successful, a negative error code otherwise
242*f1b8d335SJules Maselbas  */
243ff764963SKishon Vijay Abraham I int phy_init(struct phy *phy)
244ff764963SKishon Vijay Abraham I {
245ff764963SKishon Vijay Abraham I 	int ret;
246ff764963SKishon Vijay Abraham I 
24704c2facaSAndrew Lunn 	if (!phy)
24804c2facaSAndrew Lunn 		return 0;
24904c2facaSAndrew Lunn 
250ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
251ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
252ff764963SKishon Vijay Abraham I 		return ret;
253736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
254ff764963SKishon Vijay Abraham I 
255ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
256637d378cSKishon Vijay Abraham I 	if (phy->init_count == 0 && phy->ops->init) {
257ff764963SKishon Vijay Abraham I 		ret = phy->ops->init(phy);
258ff764963SKishon Vijay Abraham I 		if (ret < 0) {
259ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy init failed --> %d\n", ret);
260ff764963SKishon Vijay Abraham I 			goto out;
261ff764963SKishon Vijay Abraham I 		}
262ff764963SKishon Vijay Abraham I 	}
263637d378cSKishon Vijay Abraham I 	++phy->init_count;
264ff764963SKishon Vijay Abraham I 
265ff764963SKishon Vijay Abraham I out:
266ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
267ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
268ff764963SKishon Vijay Abraham I 	return ret;
269ff764963SKishon Vijay Abraham I }
270ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_init);
271ff764963SKishon Vijay Abraham I 
272*f1b8d335SJules Maselbas /**
273*f1b8d335SJules Maselbas  * phy_exit - Phy internal un-initialization
274*f1b8d335SJules Maselbas  * @phy: the phy returned by phy_get()
275*f1b8d335SJules Maselbas  *
276*f1b8d335SJules Maselbas  * Must be called after phy_power_off().
277*f1b8d335SJules Maselbas  *
278*f1b8d335SJules Maselbas  * Return: %0 if successful, a negative error code otherwise
279*f1b8d335SJules Maselbas  */
280ff764963SKishon Vijay Abraham I int phy_exit(struct phy *phy)
281ff764963SKishon Vijay Abraham I {
282ff764963SKishon Vijay Abraham I 	int ret;
283ff764963SKishon Vijay Abraham I 
28404c2facaSAndrew Lunn 	if (!phy)
28504c2facaSAndrew Lunn 		return 0;
28604c2facaSAndrew Lunn 
287ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
288ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
289ff764963SKishon Vijay Abraham I 		return ret;
290736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
291ff764963SKishon Vijay Abraham I 
292ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
293637d378cSKishon Vijay Abraham I 	if (phy->init_count == 1 && phy->ops->exit) {
294ff764963SKishon Vijay Abraham I 		ret = phy->ops->exit(phy);
295ff764963SKishon Vijay Abraham I 		if (ret < 0) {
296ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
297ff764963SKishon Vijay Abraham I 			goto out;
298ff764963SKishon Vijay Abraham I 		}
299ff764963SKishon Vijay Abraham I 	}
300637d378cSKishon Vijay Abraham I 	--phy->init_count;
301ff764963SKishon Vijay Abraham I 
302ff764963SKishon Vijay Abraham I out:
303ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
304ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
305ff764963SKishon Vijay Abraham I 	return ret;
306ff764963SKishon Vijay Abraham I }
307ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_exit);
308ff764963SKishon Vijay Abraham I 
309*f1b8d335SJules Maselbas /**
310*f1b8d335SJules Maselbas  * phy_power_on - Enable the phy and enter proper operation
311*f1b8d335SJules Maselbas  * @phy: the phy returned by phy_get()
312*f1b8d335SJules Maselbas  *
313*f1b8d335SJules Maselbas  * Must be called after phy_init().
314*f1b8d335SJules Maselbas  *
315*f1b8d335SJules Maselbas  * Return: %0 if successful, a negative error code otherwise
316*f1b8d335SJules Maselbas  */
317ff764963SKishon Vijay Abraham I int phy_power_on(struct phy *phy)
318ff764963SKishon Vijay Abraham I {
319b82fcabeSShawn Lin 	int ret = 0;
320ff764963SKishon Vijay Abraham I 
32104c2facaSAndrew Lunn 	if (!phy)
322b82fcabeSShawn Lin 		goto out;
32304c2facaSAndrew Lunn 
3243be88125SRoger Quadros 	if (phy->pwr) {
3253be88125SRoger Quadros 		ret = regulator_enable(phy->pwr);
3263be88125SRoger Quadros 		if (ret)
327b82fcabeSShawn Lin 			goto out;
3283be88125SRoger Quadros 	}
3293be88125SRoger Quadros 
330ff764963SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
331ff764963SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
332b82fcabeSShawn Lin 		goto err_pm_sync;
333b82fcabeSShawn Lin 
334736b67a3SAxel Lin 	ret = 0; /* Override possible ret == -ENOTSUPP */
335ff764963SKishon Vijay Abraham I 
336ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
337637d378cSKishon Vijay Abraham I 	if (phy->power_count == 0 && phy->ops->power_on) {
338ff764963SKishon Vijay Abraham I 		ret = phy->ops->power_on(phy);
339ff764963SKishon Vijay Abraham I 		if (ret < 0) {
340ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
341b82fcabeSShawn Lin 			goto err_pwr_on;
342ff764963SKishon Vijay Abraham I 		}
343ff764963SKishon Vijay Abraham I 	}
344637d378cSKishon Vijay Abraham I 	++phy->power_count;
345637d378cSKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
346637d378cSKishon Vijay Abraham I 	return 0;
347ff764963SKishon Vijay Abraham I 
348b82fcabeSShawn Lin err_pwr_on:
349ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
350637d378cSKishon Vijay Abraham I 	phy_pm_runtime_put_sync(phy);
351b82fcabeSShawn Lin err_pm_sync:
3523be88125SRoger Quadros 	if (phy->pwr)
3533be88125SRoger Quadros 		regulator_disable(phy->pwr);
354b82fcabeSShawn Lin out:
355ff764963SKishon Vijay Abraham I 	return ret;
356ff764963SKishon Vijay Abraham I }
357ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_on);
358ff764963SKishon Vijay Abraham I 
359*f1b8d335SJules Maselbas /**
360*f1b8d335SJules Maselbas  * phy_power_off - Disable the phy.
361*f1b8d335SJules Maselbas  * @phy: the phy returned by phy_get()
362*f1b8d335SJules Maselbas  *
363*f1b8d335SJules Maselbas  * Must be called before phy_exit().
364*f1b8d335SJules Maselbas  *
365*f1b8d335SJules Maselbas  * Return: %0 if successful, a negative error code otherwise
366*f1b8d335SJules Maselbas  */
367ff764963SKishon Vijay Abraham I int phy_power_off(struct phy *phy)
368ff764963SKishon Vijay Abraham I {
369d18c9604SKishon Vijay Abraham I 	int ret;
370ff764963SKishon Vijay Abraham I 
37104c2facaSAndrew Lunn 	if (!phy)
37204c2facaSAndrew Lunn 		return 0;
37304c2facaSAndrew Lunn 
374ff764963SKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
375637d378cSKishon Vijay Abraham I 	if (phy->power_count == 1 && phy->ops->power_off) {
376ff764963SKishon Vijay Abraham I 		ret =  phy->ops->power_off(phy);
377ff764963SKishon Vijay Abraham I 		if (ret < 0) {
378ff764963SKishon Vijay Abraham I 			dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
379637d378cSKishon Vijay Abraham I 			mutex_unlock(&phy->mutex);
380637d378cSKishon Vijay Abraham I 			return ret;
381ff764963SKishon Vijay Abraham I 		}
382ff764963SKishon Vijay Abraham I 	}
383637d378cSKishon Vijay Abraham I 	--phy->power_count;
384ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
385ff764963SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
386ff764963SKishon Vijay Abraham I 
3873be88125SRoger Quadros 	if (phy->pwr)
3883be88125SRoger Quadros 		regulator_disable(phy->pwr);
3893be88125SRoger Quadros 
390637d378cSKishon Vijay Abraham I 	return 0;
391ff764963SKishon Vijay Abraham I }
392ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_power_off);
393ff764963SKishon Vijay Abraham I 
39479a5a18aSGrygorii Strashko int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
395300eb013SDavid Lechner {
396300eb013SDavid Lechner 	int ret;
397300eb013SDavid Lechner 
398300eb013SDavid Lechner 	if (!phy || !phy->ops->set_mode)
399300eb013SDavid Lechner 		return 0;
400300eb013SDavid Lechner 
401300eb013SDavid Lechner 	mutex_lock(&phy->mutex);
40279a5a18aSGrygorii Strashko 	ret = phy->ops->set_mode(phy, mode, submode);
4033b3cd24aSManu Gautam 	if (!ret)
4043b3cd24aSManu Gautam 		phy->attrs.mode = mode;
405300eb013SDavid Lechner 	mutex_unlock(&phy->mutex);
406300eb013SDavid Lechner 
407300eb013SDavid Lechner 	return ret;
408300eb013SDavid Lechner }
40979a5a18aSGrygorii Strashko EXPORT_SYMBOL_GPL(phy_set_mode_ext);
410300eb013SDavid Lechner 
4116c172e73SSteen Hegelund int phy_set_media(struct phy *phy, enum phy_media media)
4126c172e73SSteen Hegelund {
4136c172e73SSteen Hegelund 	int ret;
4146c172e73SSteen Hegelund 
4156c172e73SSteen Hegelund 	if (!phy || !phy->ops->set_media)
4166c172e73SSteen Hegelund 		return 0;
4176c172e73SSteen Hegelund 
4186c172e73SSteen Hegelund 	mutex_lock(&phy->mutex);
4196c172e73SSteen Hegelund 	ret = phy->ops->set_media(phy, media);
4206c172e73SSteen Hegelund 	mutex_unlock(&phy->mutex);
4216c172e73SSteen Hegelund 
4226c172e73SSteen Hegelund 	return ret;
4236c172e73SSteen Hegelund }
4246c172e73SSteen Hegelund EXPORT_SYMBOL_GPL(phy_set_media);
4256c172e73SSteen Hegelund 
4266c172e73SSteen Hegelund int phy_set_speed(struct phy *phy, int speed)
4276c172e73SSteen Hegelund {
4286c172e73SSteen Hegelund 	int ret;
4296c172e73SSteen Hegelund 
4306c172e73SSteen Hegelund 	if (!phy || !phy->ops->set_speed)
4316c172e73SSteen Hegelund 		return 0;
4326c172e73SSteen Hegelund 
4336c172e73SSteen Hegelund 	mutex_lock(&phy->mutex);
4346c172e73SSteen Hegelund 	ret = phy->ops->set_speed(phy, speed);
4356c172e73SSteen Hegelund 	mutex_unlock(&phy->mutex);
4366c172e73SSteen Hegelund 
4376c172e73SSteen Hegelund 	return ret;
4386c172e73SSteen Hegelund }
4396c172e73SSteen Hegelund EXPORT_SYMBOL_GPL(phy_set_speed);
4406c172e73SSteen Hegelund 
441cac18ecbSRandy Li int phy_reset(struct phy *phy)
442cac18ecbSRandy Li {
443cac18ecbSRandy Li 	int ret;
444cac18ecbSRandy Li 
445cac18ecbSRandy Li 	if (!phy || !phy->ops->reset)
446cac18ecbSRandy Li 		return 0;
447cac18ecbSRandy Li 
4484df614c4SKishon Vijay Abraham I 	ret = phy_pm_runtime_get_sync(phy);
4494df614c4SKishon Vijay Abraham I 	if (ret < 0 && ret != -ENOTSUPP)
4504df614c4SKishon Vijay Abraham I 		return ret;
4514df614c4SKishon Vijay Abraham I 
452cac18ecbSRandy Li 	mutex_lock(&phy->mutex);
453cac18ecbSRandy Li 	ret = phy->ops->reset(phy);
454cac18ecbSRandy Li 	mutex_unlock(&phy->mutex);
455cac18ecbSRandy Li 
4564df614c4SKishon Vijay Abraham I 	phy_pm_runtime_put(phy);
4574df614c4SKishon Vijay Abraham I 
458cac18ecbSRandy Li 	return ret;
459cac18ecbSRandy Li }
460cac18ecbSRandy Li EXPORT_SYMBOL_GPL(phy_reset);
461cac18ecbSRandy Li 
462bbae18f0SMarek Szyprowski /**
463bbae18f0SMarek Szyprowski  * phy_calibrate() - Tunes the phy hw parameters for current configuration
464bbae18f0SMarek Szyprowski  * @phy: the phy returned by phy_get()
465bbae18f0SMarek Szyprowski  *
466bbae18f0SMarek Szyprowski  * Used to calibrate phy hardware, typically by adjusting some parameters in
467bbae18f0SMarek Szyprowski  * runtime, which are otherwise lost after host controller reset and cannot
468bbae18f0SMarek Szyprowski  * be applied in phy_init() or phy_power_on().
469bbae18f0SMarek Szyprowski  *
470bbae18f0SMarek Szyprowski  * Returns: 0 if successful, an negative error code otherwise
471bbae18f0SMarek Szyprowski  */
47236914111SAndrzej Pietrasiewicz int phy_calibrate(struct phy *phy)
47336914111SAndrzej Pietrasiewicz {
47436914111SAndrzej Pietrasiewicz 	int ret;
47536914111SAndrzej Pietrasiewicz 
47636914111SAndrzej Pietrasiewicz 	if (!phy || !phy->ops->calibrate)
47736914111SAndrzej Pietrasiewicz 		return 0;
47836914111SAndrzej Pietrasiewicz 
47936914111SAndrzej Pietrasiewicz 	mutex_lock(&phy->mutex);
48036914111SAndrzej Pietrasiewicz 	ret = phy->ops->calibrate(phy);
48136914111SAndrzej Pietrasiewicz 	mutex_unlock(&phy->mutex);
48236914111SAndrzej Pietrasiewicz 
48336914111SAndrzej Pietrasiewicz 	return ret;
48436914111SAndrzej Pietrasiewicz }
48536914111SAndrzej Pietrasiewicz EXPORT_SYMBOL_GPL(phy_calibrate);
48636914111SAndrzej Pietrasiewicz 
487ff764963SKishon Vijay Abraham I /**
488aeaac93dSMaxime Ripard  * phy_configure() - Changes the phy parameters
489aeaac93dSMaxime Ripard  * @phy: the phy returned by phy_get()
490aeaac93dSMaxime Ripard  * @opts: New configuration to apply
491aeaac93dSMaxime Ripard  *
492aeaac93dSMaxime Ripard  * Used to change the PHY parameters. phy_init() must have been called
493aeaac93dSMaxime Ripard  * on the phy. The configuration will be applied on the current phy
494aeaac93dSMaxime Ripard  * mode, that can be changed using phy_set_mode().
495aeaac93dSMaxime Ripard  *
496aeaac93dSMaxime Ripard  * Returns: 0 if successful, an negative error code otherwise
497aeaac93dSMaxime Ripard  */
498aeaac93dSMaxime Ripard int phy_configure(struct phy *phy, union phy_configure_opts *opts)
499aeaac93dSMaxime Ripard {
500aeaac93dSMaxime Ripard 	int ret;
501aeaac93dSMaxime Ripard 
502aeaac93dSMaxime Ripard 	if (!phy)
503aeaac93dSMaxime Ripard 		return -EINVAL;
504aeaac93dSMaxime Ripard 
505aeaac93dSMaxime Ripard 	if (!phy->ops->configure)
506aeaac93dSMaxime Ripard 		return -EOPNOTSUPP;
507aeaac93dSMaxime Ripard 
508aeaac93dSMaxime Ripard 	mutex_lock(&phy->mutex);
509aeaac93dSMaxime Ripard 	ret = phy->ops->configure(phy, opts);
510aeaac93dSMaxime Ripard 	mutex_unlock(&phy->mutex);
511aeaac93dSMaxime Ripard 
512aeaac93dSMaxime Ripard 	return ret;
513aeaac93dSMaxime Ripard }
514aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_configure);
515aeaac93dSMaxime Ripard 
516aeaac93dSMaxime Ripard /**
517aeaac93dSMaxime Ripard  * phy_validate() - Checks the phy parameters
518aeaac93dSMaxime Ripard  * @phy: the phy returned by phy_get()
519aeaac93dSMaxime Ripard  * @mode: phy_mode the configuration is applicable to.
520aeaac93dSMaxime Ripard  * @submode: PHY submode the configuration is applicable to.
521aeaac93dSMaxime Ripard  * @opts: Configuration to check
522aeaac93dSMaxime Ripard  *
523aeaac93dSMaxime Ripard  * Used to check that the current set of parameters can be handled by
524aeaac93dSMaxime Ripard  * the phy. Implementations are free to tune the parameters passed as
525aeaac93dSMaxime Ripard  * arguments if needed by some implementation detail or
526aeaac93dSMaxime Ripard  * constraints. It will not change any actual configuration of the
527aeaac93dSMaxime Ripard  * PHY, so calling it as many times as deemed fit will have no side
528aeaac93dSMaxime Ripard  * effect.
529aeaac93dSMaxime Ripard  *
530aeaac93dSMaxime Ripard  * Returns: 0 if successful, an negative error code otherwise
531aeaac93dSMaxime Ripard  */
532aeaac93dSMaxime Ripard int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
533aeaac93dSMaxime Ripard 		 union phy_configure_opts *opts)
534aeaac93dSMaxime Ripard {
535aeaac93dSMaxime Ripard 	int ret;
536aeaac93dSMaxime Ripard 
537aeaac93dSMaxime Ripard 	if (!phy)
538aeaac93dSMaxime Ripard 		return -EINVAL;
539aeaac93dSMaxime Ripard 
540aeaac93dSMaxime Ripard 	if (!phy->ops->validate)
541aeaac93dSMaxime Ripard 		return -EOPNOTSUPP;
542aeaac93dSMaxime Ripard 
543aeaac93dSMaxime Ripard 	mutex_lock(&phy->mutex);
544aeaac93dSMaxime Ripard 	ret = phy->ops->validate(phy, mode, submode, opts);
545aeaac93dSMaxime Ripard 	mutex_unlock(&phy->mutex);
546aeaac93dSMaxime Ripard 
547aeaac93dSMaxime Ripard 	return ret;
548aeaac93dSMaxime Ripard }
549aeaac93dSMaxime Ripard EXPORT_SYMBOL_GPL(phy_validate);
550aeaac93dSMaxime Ripard 
551aeaac93dSMaxime Ripard /**
5520b3f3b2cSKamil Debski  * _of_phy_get() - lookup and obtain a reference to a phy by phandle
5530b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
554ff764963SKishon Vijay Abraham I  * @index: the index of the phy
555ff764963SKishon Vijay Abraham I  *
556ff764963SKishon Vijay Abraham I  * Returns the phy associated with the given phandle value,
557ff764963SKishon Vijay Abraham I  * after getting a refcount to it or -ENODEV if there is no such phy or
558ff764963SKishon Vijay Abraham I  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
559ff764963SKishon Vijay Abraham I  * not yet loaded. This function uses of_xlate call back function provided
560ff764963SKishon Vijay Abraham I  * while registering the phy_provider to find the phy instance.
561ff764963SKishon Vijay Abraham I  */
5620b3f3b2cSKamil Debski static struct phy *_of_phy_get(struct device_node *np, int index)
563ff764963SKishon Vijay Abraham I {
564ff764963SKishon Vijay Abraham I 	int ret;
565ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
566ff764963SKishon Vijay Abraham I 	struct phy *phy = NULL;
567ff764963SKishon Vijay Abraham I 	struct of_phandle_args args;
568ff764963SKishon Vijay Abraham I 
5690b3f3b2cSKamil Debski 	ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
570ff764963SKishon Vijay Abraham I 		index, &args);
5710b3f3b2cSKamil Debski 	if (ret)
572ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENODEV);
573ff764963SKishon Vijay Abraham I 
574b7563e27SArnd Bergmann 	/* This phy type handled by the usb-phy subsystem for now */
575b7563e27SArnd Bergmann 	if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
576b7563e27SArnd Bergmann 		return ERR_PTR(-ENODEV);
577b7563e27SArnd Bergmann 
578ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
579ff764963SKishon Vijay Abraham I 	phy_provider = of_phy_provider_lookup(args.np);
580ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
581ff764963SKishon Vijay Abraham I 		phy = ERR_PTR(-EPROBE_DEFER);
58233f434d2SAxel Lin 		goto out_unlock;
58333f434d2SAxel Lin 	}
58433f434d2SAxel Lin 
58533f434d2SAxel Lin 	if (!of_device_is_available(args.np)) {
58633f434d2SAxel Lin 		dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
58733f434d2SAxel Lin 		phy = ERR_PTR(-ENODEV);
58833f434d2SAxel Lin 		goto out_put_module;
589ff764963SKishon Vijay Abraham I 	}
590ff764963SKishon Vijay Abraham I 
591ff764963SKishon Vijay Abraham I 	phy = phy_provider->of_xlate(phy_provider->dev, &args);
59233f434d2SAxel Lin 
59333f434d2SAxel Lin out_put_module:
594ff764963SKishon Vijay Abraham I 	module_put(phy_provider->owner);
595ff764963SKishon Vijay Abraham I 
59633f434d2SAxel Lin out_unlock:
597ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
598ff764963SKishon Vijay Abraham I 	of_node_put(args.np);
599ff764963SKishon Vijay Abraham I 
600ff764963SKishon Vijay Abraham I 	return phy;
601ff764963SKishon Vijay Abraham I }
602ff764963SKishon Vijay Abraham I 
603ff764963SKishon Vijay Abraham I /**
6040b3f3b2cSKamil Debski  * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
6050b3f3b2cSKamil Debski  * @np: device_node for which to get the phy
6060b3f3b2cSKamil Debski  * @con_id: name of the phy from device's point of view
6070b3f3b2cSKamil Debski  *
6080b3f3b2cSKamil Debski  * Returns the phy driver, after getting a refcount to it; or
6090b3f3b2cSKamil Debski  * -ENODEV if there is no such phy. The caller is responsible for
6100b3f3b2cSKamil Debski  * calling phy_put() to release that count.
6110b3f3b2cSKamil Debski  */
6120b3f3b2cSKamil Debski struct phy *of_phy_get(struct device_node *np, const char *con_id)
6130b3f3b2cSKamil Debski {
6140b3f3b2cSKamil Debski 	struct phy *phy = NULL;
6150b3f3b2cSKamil Debski 	int index = 0;
6160b3f3b2cSKamil Debski 
6170b3f3b2cSKamil Debski 	if (con_id)
6180b3f3b2cSKamil Debski 		index = of_property_match_string(np, "phy-names", con_id);
6190b3f3b2cSKamil Debski 
6200b3f3b2cSKamil Debski 	phy = _of_phy_get(np, index);
6210b3f3b2cSKamil Debski 	if (IS_ERR(phy))
6220b3f3b2cSKamil Debski 		return phy;
6230b3f3b2cSKamil Debski 
6240b3f3b2cSKamil Debski 	if (!try_module_get(phy->ops->owner))
6250b3f3b2cSKamil Debski 		return ERR_PTR(-EPROBE_DEFER);
6260b3f3b2cSKamil Debski 
6270b3f3b2cSKamil Debski 	get_device(&phy->dev);
6280b3f3b2cSKamil Debski 
6290b3f3b2cSKamil Debski 	return phy;
6300b3f3b2cSKamil Debski }
6310b3f3b2cSKamil Debski EXPORT_SYMBOL_GPL(of_phy_get);
6320b3f3b2cSKamil Debski 
6330b3f3b2cSKamil Debski /**
634987351e1SAlexandre Torgue  * of_phy_put() - release the PHY
635987351e1SAlexandre Torgue  * @phy: the phy returned by of_phy_get()
636ff764963SKishon Vijay Abraham I  *
637987351e1SAlexandre Torgue  * Releases a refcount the caller received from of_phy_get().
638ff764963SKishon Vijay Abraham I  */
639987351e1SAlexandre Torgue void of_phy_put(struct phy *phy)
640ff764963SKishon Vijay Abraham I {
64104c2facaSAndrew Lunn 	if (!phy || IS_ERR(phy))
642ff764963SKishon Vijay Abraham I 		return;
643ff764963SKishon Vijay Abraham I 
644fec06b2bSKishon Vijay Abraham I 	mutex_lock(&phy->mutex);
645fec06b2bSKishon Vijay Abraham I 	if (phy->ops->release)
646fec06b2bSKishon Vijay Abraham I 		phy->ops->release(phy);
647fec06b2bSKishon Vijay Abraham I 	mutex_unlock(&phy->mutex);
648fec06b2bSKishon Vijay Abraham I 
649ff764963SKishon Vijay Abraham I 	module_put(phy->ops->owner);
650ff764963SKishon Vijay Abraham I 	put_device(&phy->dev);
651ff764963SKishon Vijay Abraham I }
652987351e1SAlexandre Torgue EXPORT_SYMBOL_GPL(of_phy_put);
653987351e1SAlexandre Torgue 
654987351e1SAlexandre Torgue /**
655987351e1SAlexandre Torgue  * phy_put() - release the PHY
656987351e1SAlexandre Torgue  * @dev: device that wants to release this phy
657987351e1SAlexandre Torgue  * @phy: the phy returned by phy_get()
658987351e1SAlexandre Torgue  *
659987351e1SAlexandre Torgue  * Releases a refcount the caller received from phy_get().
660987351e1SAlexandre Torgue  */
661987351e1SAlexandre Torgue void phy_put(struct device *dev, struct phy *phy)
662987351e1SAlexandre Torgue {
663987351e1SAlexandre Torgue 	device_link_remove(dev, &phy->dev);
664987351e1SAlexandre Torgue 	of_phy_put(phy);
665987351e1SAlexandre Torgue }
666ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_put);
667ff764963SKishon Vijay Abraham I 
668ff764963SKishon Vijay Abraham I /**
669ff764963SKishon Vijay Abraham I  * devm_phy_put() - release the PHY
670ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
671ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
672ff764963SKishon Vijay Abraham I  *
673ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_put
674ff764963SKishon Vijay Abraham I  * to release the phy.
675ff764963SKishon Vijay Abraham I  */
676ff764963SKishon Vijay Abraham I void devm_phy_put(struct device *dev, struct phy *phy)
677ff764963SKishon Vijay Abraham I {
678ff764963SKishon Vijay Abraham I 	int r;
679ff764963SKishon Vijay Abraham I 
68004c2facaSAndrew Lunn 	if (!phy)
68104c2facaSAndrew Lunn 		return;
68204c2facaSAndrew Lunn 
683ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
684ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
685ff764963SKishon Vijay Abraham I }
686ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_put);
687ff764963SKishon Vijay Abraham I 
688ff764963SKishon Vijay Abraham I /**
689ff764963SKishon Vijay Abraham I  * of_phy_simple_xlate() - returns the phy instance from phy provider
690ff764963SKishon Vijay Abraham I  * @dev: the PHY provider device
691ff764963SKishon Vijay Abraham I  * @args: of_phandle_args (not used here)
692ff764963SKishon Vijay Abraham I  *
693ff764963SKishon Vijay Abraham I  * Intended to be used by phy provider for the common case where #phy-cells is
694ff764963SKishon Vijay Abraham I  * 0. For other cases where #phy-cells is greater than '0', the phy provider
695ff764963SKishon Vijay Abraham I  * should provide a custom of_xlate function that reads the *args* and returns
696ff764963SKishon Vijay Abraham I  * the appropriate phy.
697ff764963SKishon Vijay Abraham I  */
698ff764963SKishon Vijay Abraham I struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
699ff764963SKishon Vijay Abraham I 	*args)
700ff764963SKishon Vijay Abraham I {
701ff764963SKishon Vijay Abraham I 	struct phy *phy;
702ff764963SKishon Vijay Abraham I 	struct class_dev_iter iter;
703ff764963SKishon Vijay Abraham I 
704ff764963SKishon Vijay Abraham I 	class_dev_iter_init(&iter, phy_class, NULL, NULL);
705ff764963SKishon Vijay Abraham I 	while ((dev = class_dev_iter_next(&iter))) {
706ff764963SKishon Vijay Abraham I 		phy = to_phy(dev);
707491e0490SKishon Vijay Abraham I 		if (args->np != phy->dev.of_node)
708ff764963SKishon Vijay Abraham I 			continue;
709ff764963SKishon Vijay Abraham I 
710ff764963SKishon Vijay Abraham I 		class_dev_iter_exit(&iter);
711ff764963SKishon Vijay Abraham I 		return phy;
712ff764963SKishon Vijay Abraham I 	}
713ff764963SKishon Vijay Abraham I 
714ff764963SKishon Vijay Abraham I 	class_dev_iter_exit(&iter);
715ff764963SKishon Vijay Abraham I 	return ERR_PTR(-ENODEV);
716ff764963SKishon Vijay Abraham I }
717ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
718ff764963SKishon Vijay Abraham I 
719ff764963SKishon Vijay Abraham I /**
720ff764963SKishon Vijay Abraham I  * phy_get() - lookup and obtain a reference to a phy.
721ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
722ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or the name of the controller
723ff764963SKishon Vijay Abraham I  * port for non-dt case
724ff764963SKishon Vijay Abraham I  *
725ff764963SKishon Vijay Abraham I  * Returns the phy driver, after getting a refcount to it; or
726ff764963SKishon Vijay Abraham I  * -ENODEV if there is no such phy.  The caller is responsible for
727ff764963SKishon Vijay Abraham I  * calling phy_put() to release that count.
728ff764963SKishon Vijay Abraham I  */
729ff764963SKishon Vijay Abraham I struct phy *phy_get(struct device *dev, const char *string)
730ff764963SKishon Vijay Abraham I {
731ff764963SKishon Vijay Abraham I 	int index = 0;
732d18c9604SKishon Vijay Abraham I 	struct phy *phy;
733987351e1SAlexandre Torgue 	struct device_link *link;
734ff764963SKishon Vijay Abraham I 
7358a917813SRob Herring 	if (dev->of_node) {
7368a917813SRob Herring 		if (string)
7378a917813SRob Herring 			index = of_property_match_string(dev->of_node, "phy-names",
7388a917813SRob Herring 				string);
7398a917813SRob Herring 		else
7408a917813SRob Herring 			index = 0;
7418a917813SRob Herring 		phy = _of_phy_get(dev->of_node, index);
7428a917813SRob Herring 	} else {
743ff764963SKishon Vijay Abraham I 		if (string == NULL) {
744ff764963SKishon Vijay Abraham I 			dev_WARN(dev, "missing string\n");
745ff764963SKishon Vijay Abraham I 			return ERR_PTR(-EINVAL);
746ff764963SKishon Vijay Abraham I 		}
747b7bc15b9SHeikki Krogerus 		phy = phy_find(dev, string);
748f40037fdSHans de Goede 	}
749f40037fdSHans de Goede 	if (IS_ERR(phy))
750ff764963SKishon Vijay Abraham I 		return phy;
751ff764963SKishon Vijay Abraham I 
752ff764963SKishon Vijay Abraham I 	if (!try_module_get(phy->ops->owner))
753ff764963SKishon Vijay Abraham I 		return ERR_PTR(-EPROBE_DEFER);
754ff764963SKishon Vijay Abraham I 
755ff764963SKishon Vijay Abraham I 	get_device(&phy->dev);
756ff764963SKishon Vijay Abraham I 
757987351e1SAlexandre Torgue 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
7581d7cb11eSKishon Vijay Abraham I 	if (!link)
7591d7cb11eSKishon Vijay Abraham I 		dev_dbg(dev, "failed to create device link to %s\n",
760987351e1SAlexandre Torgue 			dev_name(phy->dev.parent));
761987351e1SAlexandre Torgue 
762ff764963SKishon Vijay Abraham I 	return phy;
763ff764963SKishon Vijay Abraham I }
764ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_get);
765ff764963SKishon Vijay Abraham I 
766ff764963SKishon Vijay Abraham I /**
767788a4d56SAndrew Lunn  * phy_optional_get() - lookup and obtain a reference to an optional phy.
768788a4d56SAndrew Lunn  * @dev: device that requests this phy
769788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or the name of the controller
770788a4d56SAndrew Lunn  * port for non-dt case
771788a4d56SAndrew Lunn  *
772788a4d56SAndrew Lunn  * Returns the phy driver, after getting a refcount to it; or
773788a4d56SAndrew Lunn  * NULL if there is no such phy.  The caller is responsible for
774788a4d56SAndrew Lunn  * calling phy_put() to release that count.
775788a4d56SAndrew Lunn  */
776788a4d56SAndrew Lunn struct phy *phy_optional_get(struct device *dev, const char *string)
777788a4d56SAndrew Lunn {
778788a4d56SAndrew Lunn 	struct phy *phy = phy_get(dev, string);
779788a4d56SAndrew Lunn 
78045586c70SMasahiro Yamada 	if (PTR_ERR(phy) == -ENODEV)
781788a4d56SAndrew Lunn 		phy = NULL;
782788a4d56SAndrew Lunn 
783788a4d56SAndrew Lunn 	return phy;
784788a4d56SAndrew Lunn }
785788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(phy_optional_get);
786788a4d56SAndrew Lunn 
787788a4d56SAndrew Lunn /**
788ff764963SKishon Vijay Abraham I  * devm_phy_get() - lookup and obtain a reference to a phy.
789ff764963SKishon Vijay Abraham I  * @dev: device that requests this phy
790ff764963SKishon Vijay Abraham I  * @string: the phy name as given in the dt data or phy device name
791ff764963SKishon Vijay Abraham I  * for non-dt case
792ff764963SKishon Vijay Abraham I  *
793ff764963SKishon Vijay Abraham I  * Gets the phy using phy_get(), and associates a device with it using
794ff764963SKishon Vijay Abraham I  * devres. On driver detach, release function is invoked on the devres data,
795ff764963SKishon Vijay Abraham I  * then, devres data is freed.
796ff764963SKishon Vijay Abraham I  */
797ff764963SKishon Vijay Abraham I struct phy *devm_phy_get(struct device *dev, const char *string)
798ff764963SKishon Vijay Abraham I {
799ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
800ff764963SKishon Vijay Abraham I 
801ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
802ff764963SKishon Vijay Abraham I 	if (!ptr)
803ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
804ff764963SKishon Vijay Abraham I 
805ff764963SKishon Vijay Abraham I 	phy = phy_get(dev, string);
806ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
807ff764963SKishon Vijay Abraham I 		*ptr = phy;
808ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
809ff764963SKishon Vijay Abraham I 	} else {
810ff764963SKishon Vijay Abraham I 		devres_free(ptr);
811ff764963SKishon Vijay Abraham I 	}
812ff764963SKishon Vijay Abraham I 
813ff764963SKishon Vijay Abraham I 	return phy;
814ff764963SKishon Vijay Abraham I }
815ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_get);
816ff764963SKishon Vijay Abraham I 
817ff764963SKishon Vijay Abraham I /**
818788a4d56SAndrew Lunn  * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
819788a4d56SAndrew Lunn  * @dev: device that requests this phy
820788a4d56SAndrew Lunn  * @string: the phy name as given in the dt data or phy device name
821788a4d56SAndrew Lunn  * for non-dt case
822788a4d56SAndrew Lunn  *
823788a4d56SAndrew Lunn  * Gets the phy using phy_get(), and associates a device with it using
824788a4d56SAndrew Lunn  * devres. On driver detach, release function is invoked on the devres
825788a4d56SAndrew Lunn  * data, then, devres data is freed. This differs to devm_phy_get() in
826788a4d56SAndrew Lunn  * that if the phy does not exist, it is not considered an error and
827788a4d56SAndrew Lunn  * -ENODEV will not be returned. Instead the NULL phy is returned,
828788a4d56SAndrew Lunn  * which can be passed to all other phy consumer calls.
829788a4d56SAndrew Lunn  */
830788a4d56SAndrew Lunn struct phy *devm_phy_optional_get(struct device *dev, const char *string)
831788a4d56SAndrew Lunn {
832788a4d56SAndrew Lunn 	struct phy *phy = devm_phy_get(dev, string);
833788a4d56SAndrew Lunn 
83445586c70SMasahiro Yamada 	if (PTR_ERR(phy) == -ENODEV)
835788a4d56SAndrew Lunn 		phy = NULL;
836788a4d56SAndrew Lunn 
837788a4d56SAndrew Lunn 	return phy;
838788a4d56SAndrew Lunn }
839788a4d56SAndrew Lunn EXPORT_SYMBOL_GPL(devm_phy_optional_get);
840788a4d56SAndrew Lunn 
841788a4d56SAndrew Lunn /**
842b5d682f4SKamil Debski  * devm_of_phy_get() - lookup and obtain a reference to a phy.
843b5d682f4SKamil Debski  * @dev: device that requests this phy
844b5d682f4SKamil Debski  * @np: node containing the phy
845b5d682f4SKamil Debski  * @con_id: name of the phy from device's point of view
846b5d682f4SKamil Debski  *
847b5d682f4SKamil Debski  * Gets the phy using of_phy_get(), and associates a device with it using
848b5d682f4SKamil Debski  * devres. On driver detach, release function is invoked on the devres data,
849b5d682f4SKamil Debski  * then, devres data is freed.
850b5d682f4SKamil Debski  */
851b5d682f4SKamil Debski struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
852b5d682f4SKamil Debski 			    const char *con_id)
853b5d682f4SKamil Debski {
854b5d682f4SKamil Debski 	struct phy **ptr, *phy;
855987351e1SAlexandre Torgue 	struct device_link *link;
856b5d682f4SKamil Debski 
857b5d682f4SKamil Debski 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
858b5d682f4SKamil Debski 	if (!ptr)
859b5d682f4SKamil Debski 		return ERR_PTR(-ENOMEM);
860b5d682f4SKamil Debski 
861b5d682f4SKamil Debski 	phy = of_phy_get(np, con_id);
862b5d682f4SKamil Debski 	if (!IS_ERR(phy)) {
863b5d682f4SKamil Debski 		*ptr = phy;
864b5d682f4SKamil Debski 		devres_add(dev, ptr);
865b5d682f4SKamil Debski 	} else {
866b5d682f4SKamil Debski 		devres_free(ptr);
867987351e1SAlexandre Torgue 		return phy;
868987351e1SAlexandre Torgue 	}
869987351e1SAlexandre Torgue 
870987351e1SAlexandre Torgue 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
8711d7cb11eSKishon Vijay Abraham I 	if (!link)
8721d7cb11eSKishon Vijay Abraham I 		dev_dbg(dev, "failed to create device link to %s\n",
873987351e1SAlexandre Torgue 			dev_name(phy->dev.parent));
874b5d682f4SKamil Debski 
875b5d682f4SKamil Debski 	return phy;
876b5d682f4SKamil Debski }
877b5d682f4SKamil Debski EXPORT_SYMBOL_GPL(devm_of_phy_get);
878b5d682f4SKamil Debski 
879b5d682f4SKamil Debski /**
8806be109b3SArun Ramamurthy  * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
8816be109b3SArun Ramamurthy  * @dev: device that requests this phy
8826be109b3SArun Ramamurthy  * @np: node containing the phy
8836be109b3SArun Ramamurthy  * @index: index of the phy
8846be109b3SArun Ramamurthy  *
88570874462SChunfeng Yun  * Gets the phy using _of_phy_get(), then gets a refcount to it,
88670874462SChunfeng Yun  * and associates a device with it using devres. On driver detach,
88770874462SChunfeng Yun  * release function is invoked on the devres data,
8886be109b3SArun Ramamurthy  * then, devres data is freed.
8896be109b3SArun Ramamurthy  *
8906be109b3SArun Ramamurthy  */
8916be109b3SArun Ramamurthy struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
8926be109b3SArun Ramamurthy 				     int index)
8936be109b3SArun Ramamurthy {
8946be109b3SArun Ramamurthy 	struct phy **ptr, *phy;
895987351e1SAlexandre Torgue 	struct device_link *link;
8966be109b3SArun Ramamurthy 
8976be109b3SArun Ramamurthy 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
8986be109b3SArun Ramamurthy 	if (!ptr)
8996be109b3SArun Ramamurthy 		return ERR_PTR(-ENOMEM);
9006be109b3SArun Ramamurthy 
9016be109b3SArun Ramamurthy 	phy = _of_phy_get(np, index);
90270874462SChunfeng Yun 	if (IS_ERR(phy)) {
90370874462SChunfeng Yun 		devres_free(ptr);
90470874462SChunfeng Yun 		return phy;
90570874462SChunfeng Yun 	}
90670874462SChunfeng Yun 
90770874462SChunfeng Yun 	if (!try_module_get(phy->ops->owner)) {
90870874462SChunfeng Yun 		devres_free(ptr);
90970874462SChunfeng Yun 		return ERR_PTR(-EPROBE_DEFER);
91070874462SChunfeng Yun 	}
91170874462SChunfeng Yun 
91270874462SChunfeng Yun 	get_device(&phy->dev);
91370874462SChunfeng Yun 
9146be109b3SArun Ramamurthy 	*ptr = phy;
9156be109b3SArun Ramamurthy 	devres_add(dev, ptr);
9166be109b3SArun Ramamurthy 
917987351e1SAlexandre Torgue 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
9181d7cb11eSKishon Vijay Abraham I 	if (!link)
9191d7cb11eSKishon Vijay Abraham I 		dev_dbg(dev, "failed to create device link to %s\n",
920987351e1SAlexandre Torgue 			dev_name(phy->dev.parent));
921987351e1SAlexandre Torgue 
9226be109b3SArun Ramamurthy 	return phy;
9236be109b3SArun Ramamurthy }
9246be109b3SArun Ramamurthy EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
9256be109b3SArun Ramamurthy 
9266be109b3SArun Ramamurthy /**
927ff764963SKishon Vijay Abraham I  * phy_create() - create a new phy
928ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
929f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
930ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
931ff764963SKishon Vijay Abraham I  *
932ff764963SKishon Vijay Abraham I  * Called to create a phy using phy framework.
933ff764963SKishon Vijay Abraham I  */
934f0ed8176SKishon Vijay Abraham I struct phy *phy_create(struct device *dev, struct device_node *node,
935dbc98635SHeikki Krogerus 		       const struct phy_ops *ops)
936ff764963SKishon Vijay Abraham I {
937ff764963SKishon Vijay Abraham I 	int ret;
938ff764963SKishon Vijay Abraham I 	int id;
939ff764963SKishon Vijay Abraham I 	struct phy *phy;
940ff764963SKishon Vijay Abraham I 
94152797d29SDan Carpenter 	if (WARN_ON(!dev))
94252797d29SDan Carpenter 		return ERR_PTR(-EINVAL);
943ff764963SKishon Vijay Abraham I 
944ff764963SKishon Vijay Abraham I 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
94552797d29SDan Carpenter 	if (!phy)
94652797d29SDan Carpenter 		return ERR_PTR(-ENOMEM);
947ff764963SKishon Vijay Abraham I 
948ff764963SKishon Vijay Abraham I 	id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
949ff764963SKishon Vijay Abraham I 	if (id < 0) {
950ff764963SKishon Vijay Abraham I 		dev_err(dev, "unable to get id\n");
951ff764963SKishon Vijay Abraham I 		ret = id;
95252797d29SDan Carpenter 		goto free_phy;
953ff764963SKishon Vijay Abraham I 	}
954ff764963SKishon Vijay Abraham I 
955ff764963SKishon Vijay Abraham I 	device_initialize(&phy->dev);
956ff764963SKishon Vijay Abraham I 	mutex_init(&phy->mutex);
957ff764963SKishon Vijay Abraham I 
958ff764963SKishon Vijay Abraham I 	phy->dev.class = phy_class;
959ff764963SKishon Vijay Abraham I 	phy->dev.parent = dev;
960f0ed8176SKishon Vijay Abraham I 	phy->dev.of_node = node ?: dev->of_node;
961ff764963SKishon Vijay Abraham I 	phy->id = id;
962ff764963SKishon Vijay Abraham I 	phy->ops = ops;
963ff764963SKishon Vijay Abraham I 
964ff764963SKishon Vijay Abraham I 	ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
965ff764963SKishon Vijay Abraham I 	if (ret)
96652797d29SDan Carpenter 		goto put_dev;
967ff764963SKishon Vijay Abraham I 
96887006dd6SDmitry Torokhov 	/* phy-supply */
96987006dd6SDmitry Torokhov 	phy->pwr = regulator_get_optional(&phy->dev, "phy");
97087006dd6SDmitry Torokhov 	if (IS_ERR(phy->pwr)) {
97187006dd6SDmitry Torokhov 		ret = PTR_ERR(phy->pwr);
97287006dd6SDmitry Torokhov 		if (ret == -EPROBE_DEFER)
97387006dd6SDmitry Torokhov 			goto put_dev;
97487006dd6SDmitry Torokhov 
97587006dd6SDmitry Torokhov 		phy->pwr = NULL;
97687006dd6SDmitry Torokhov 	}
97787006dd6SDmitry Torokhov 
978ff764963SKishon Vijay Abraham I 	ret = device_add(&phy->dev);
979ff764963SKishon Vijay Abraham I 	if (ret)
98052797d29SDan Carpenter 		goto put_dev;
981ff764963SKishon Vijay Abraham I 
982ff764963SKishon Vijay Abraham I 	if (pm_runtime_enabled(dev)) {
983ff764963SKishon Vijay Abraham I 		pm_runtime_enable(&phy->dev);
984ff764963SKishon Vijay Abraham I 		pm_runtime_no_callbacks(&phy->dev);
985ff764963SKishon Vijay Abraham I 	}
986ff764963SKishon Vijay Abraham I 
987ff764963SKishon Vijay Abraham I 	return phy;
988ff764963SKishon Vijay Abraham I 
98952797d29SDan Carpenter put_dev:
990e73b49f1SRoger Quadros 	put_device(&phy->dev);  /* calls phy_release() which frees resources */
991e73b49f1SRoger Quadros 	return ERR_PTR(ret);
992e73b49f1SRoger Quadros 
99352797d29SDan Carpenter free_phy:
994ff764963SKishon Vijay Abraham I 	kfree(phy);
995ff764963SKishon Vijay Abraham I 	return ERR_PTR(ret);
996ff764963SKishon Vijay Abraham I }
997ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_create);
998ff764963SKishon Vijay Abraham I 
999ff764963SKishon Vijay Abraham I /**
1000ff764963SKishon Vijay Abraham I  * devm_phy_create() - create a new phy
1001ff764963SKishon Vijay Abraham I  * @dev: device that is creating the new phy
1002f0ed8176SKishon Vijay Abraham I  * @node: device node of the phy
1003ff764963SKishon Vijay Abraham I  * @ops: function pointers for performing phy operations
1004ff764963SKishon Vijay Abraham I  *
1005ff764963SKishon Vijay Abraham I  * Creates a new PHY device adding it to the PHY class.
1006ff764963SKishon Vijay Abraham I  * While at that, it also associates the device with the phy using devres.
1007ff764963SKishon Vijay Abraham I  * On driver detach, release function is invoked on the devres data,
1008ff764963SKishon Vijay Abraham I  * then, devres data is freed.
1009ff764963SKishon Vijay Abraham I  */
1010f0ed8176SKishon Vijay Abraham I struct phy *devm_phy_create(struct device *dev, struct device_node *node,
1011dbc98635SHeikki Krogerus 			    const struct phy_ops *ops)
1012ff764963SKishon Vijay Abraham I {
1013ff764963SKishon Vijay Abraham I 	struct phy **ptr, *phy;
1014ff764963SKishon Vijay Abraham I 
1015ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
1016ff764963SKishon Vijay Abraham I 	if (!ptr)
1017ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
1018ff764963SKishon Vijay Abraham I 
1019dbc98635SHeikki Krogerus 	phy = phy_create(dev, node, ops);
1020ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy)) {
1021ff764963SKishon Vijay Abraham I 		*ptr = phy;
1022ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
1023ff764963SKishon Vijay Abraham I 	} else {
1024ff764963SKishon Vijay Abraham I 		devres_free(ptr);
1025ff764963SKishon Vijay Abraham I 	}
1026ff764963SKishon Vijay Abraham I 
1027ff764963SKishon Vijay Abraham I 	return phy;
1028ff764963SKishon Vijay Abraham I }
1029ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_create);
1030ff764963SKishon Vijay Abraham I 
1031ff764963SKishon Vijay Abraham I /**
1032ff764963SKishon Vijay Abraham I  * phy_destroy() - destroy the phy
1033ff764963SKishon Vijay Abraham I  * @phy: the phy to be destroyed
1034ff764963SKishon Vijay Abraham I  *
1035ff764963SKishon Vijay Abraham I  * Called to destroy the phy.
1036ff764963SKishon Vijay Abraham I  */
1037ff764963SKishon Vijay Abraham I void phy_destroy(struct phy *phy)
1038ff764963SKishon Vijay Abraham I {
1039ff764963SKishon Vijay Abraham I 	pm_runtime_disable(&phy->dev);
1040ff764963SKishon Vijay Abraham I 	device_unregister(&phy->dev);
1041ff764963SKishon Vijay Abraham I }
1042ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(phy_destroy);
1043ff764963SKishon Vijay Abraham I 
1044ff764963SKishon Vijay Abraham I /**
1045ff764963SKishon Vijay Abraham I  * devm_phy_destroy() - destroy the PHY
1046ff764963SKishon Vijay Abraham I  * @dev: device that wants to release this phy
1047ff764963SKishon Vijay Abraham I  * @phy: the phy returned by devm_phy_get()
1048ff764963SKishon Vijay Abraham I  *
1049ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy and invokes phy_destroy
1050ff764963SKishon Vijay Abraham I  * to destroy the phy.
1051ff764963SKishon Vijay Abraham I  */
1052ff764963SKishon Vijay Abraham I void devm_phy_destroy(struct device *dev, struct phy *phy)
1053ff764963SKishon Vijay Abraham I {
1054ff764963SKishon Vijay Abraham I 	int r;
1055ff764963SKishon Vijay Abraham I 
1056ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
1057ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
1058ff764963SKishon Vijay Abraham I }
1059ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_phy_destroy);
1060ff764963SKishon Vijay Abraham I 
1061ff764963SKishon Vijay Abraham I /**
1062ff764963SKishon Vijay Abraham I  * __of_phy_provider_register() - create/register phy provider with the framework
1063ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
10641140f7c8SThierry Reding  * @children: device node containing children (if different from dev->of_node)
1065ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
1066ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
1067ff764963SKishon Vijay Abraham I  *
1068ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
1069ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
1070ff764963SKishon Vijay Abraham I  * phy provider.
10711140f7c8SThierry Reding  *
10721140f7c8SThierry Reding  * If the PHY provider doesn't nest children directly but uses a separate
10731140f7c8SThierry Reding  * child node to contain the individual children, the @children parameter
10741140f7c8SThierry Reding  * can be used to override the default. If NULL, the default (dev->of_node)
10751140f7c8SThierry Reding  * will be used. If non-NULL, the device node must be a child (or further
10761140f7c8SThierry Reding  * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
10771140f7c8SThierry Reding  * error code is returned.
1078ff764963SKishon Vijay Abraham I  */
1079ff764963SKishon Vijay Abraham I struct phy_provider *__of_phy_provider_register(struct device *dev,
10801140f7c8SThierry Reding 	struct device_node *children, struct module *owner,
10811140f7c8SThierry Reding 	struct phy * (*of_xlate)(struct device *dev,
1082ff764963SKishon Vijay Abraham I 				 struct of_phandle_args *args))
1083ff764963SKishon Vijay Abraham I {
1084ff764963SKishon Vijay Abraham I 	struct phy_provider *phy_provider;
1085ff764963SKishon Vijay Abraham I 
10861140f7c8SThierry Reding 	/*
10871140f7c8SThierry Reding 	 * If specified, the device node containing the children must itself
10881140f7c8SThierry Reding 	 * be the provider's device node or a child (or further descendant)
10891140f7c8SThierry Reding 	 * thereof.
10901140f7c8SThierry Reding 	 */
10911140f7c8SThierry Reding 	if (children) {
10921140f7c8SThierry Reding 		struct device_node *parent = of_node_get(children), *next;
10931140f7c8SThierry Reding 
10941140f7c8SThierry Reding 		while (parent) {
10951140f7c8SThierry Reding 			if (parent == dev->of_node)
10961140f7c8SThierry Reding 				break;
10971140f7c8SThierry Reding 
10981140f7c8SThierry Reding 			next = of_get_parent(parent);
10991140f7c8SThierry Reding 			of_node_put(parent);
11001140f7c8SThierry Reding 			parent = next;
11011140f7c8SThierry Reding 		}
11021140f7c8SThierry Reding 
11031140f7c8SThierry Reding 		if (!parent)
11041140f7c8SThierry Reding 			return ERR_PTR(-EINVAL);
11051140f7c8SThierry Reding 
11061140f7c8SThierry Reding 		of_node_put(parent);
11071140f7c8SThierry Reding 	} else {
11081140f7c8SThierry Reding 		children = dev->of_node;
11091140f7c8SThierry Reding 	}
11101140f7c8SThierry Reding 
1111ff764963SKishon Vijay Abraham I 	phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
1112ff764963SKishon Vijay Abraham I 	if (!phy_provider)
1113ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
1114ff764963SKishon Vijay Abraham I 
1115ff764963SKishon Vijay Abraham I 	phy_provider->dev = dev;
11161140f7c8SThierry Reding 	phy_provider->children = of_node_get(children);
1117ff764963SKishon Vijay Abraham I 	phy_provider->owner = owner;
1118ff764963SKishon Vijay Abraham I 	phy_provider->of_xlate = of_xlate;
1119ff764963SKishon Vijay Abraham I 
1120ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
1121ff764963SKishon Vijay Abraham I 	list_add_tail(&phy_provider->list, &phy_provider_list);
1122ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
1123ff764963SKishon Vijay Abraham I 
1124ff764963SKishon Vijay Abraham I 	return phy_provider;
1125ff764963SKishon Vijay Abraham I }
1126ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__of_phy_provider_register);
1127ff764963SKishon Vijay Abraham I 
1128ff764963SKishon Vijay Abraham I /**
1129ff764963SKishon Vijay Abraham I  * __devm_of_phy_provider_register() - create/register phy provider with the
1130ff764963SKishon Vijay Abraham I  * framework
1131ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
1132aad075c1SVinod Koul  * @children: device node containing children (if different from dev->of_node)
1133ff764963SKishon Vijay Abraham I  * @owner: the module owner containing of_xlate
1134ff764963SKishon Vijay Abraham I  * @of_xlate: function pointer to obtain phy instance from phy provider
1135ff764963SKishon Vijay Abraham I  *
1136ff764963SKishon Vijay Abraham I  * Creates struct phy_provider from dev and of_xlate function pointer.
1137ff764963SKishon Vijay Abraham I  * This is used in the case of dt boot for finding the phy instance from
1138ff764963SKishon Vijay Abraham I  * phy provider. While at that, it also associates the device with the
1139ff764963SKishon Vijay Abraham I  * phy provider using devres. On driver detach, release function is invoked
1140ff764963SKishon Vijay Abraham I  * on the devres data, then, devres data is freed.
1141ff764963SKishon Vijay Abraham I  */
1142ff764963SKishon Vijay Abraham I struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
11431140f7c8SThierry Reding 	struct device_node *children, struct module *owner,
11441140f7c8SThierry Reding 	struct phy * (*of_xlate)(struct device *dev,
1145ff764963SKishon Vijay Abraham I 				 struct of_phandle_args *args))
1146ff764963SKishon Vijay Abraham I {
1147ff764963SKishon Vijay Abraham I 	struct phy_provider **ptr, *phy_provider;
1148ff764963SKishon Vijay Abraham I 
1149ff764963SKishon Vijay Abraham I 	ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
1150ff764963SKishon Vijay Abraham I 	if (!ptr)
1151ff764963SKishon Vijay Abraham I 		return ERR_PTR(-ENOMEM);
1152ff764963SKishon Vijay Abraham I 
11531140f7c8SThierry Reding 	phy_provider = __of_phy_provider_register(dev, children, owner,
11541140f7c8SThierry Reding 						  of_xlate);
1155ff764963SKishon Vijay Abraham I 	if (!IS_ERR(phy_provider)) {
1156ff764963SKishon Vijay Abraham I 		*ptr = phy_provider;
1157ff764963SKishon Vijay Abraham I 		devres_add(dev, ptr);
1158ff764963SKishon Vijay Abraham I 	} else {
1159ff764963SKishon Vijay Abraham I 		devres_free(ptr);
1160ff764963SKishon Vijay Abraham I 	}
1161ff764963SKishon Vijay Abraham I 
1162ff764963SKishon Vijay Abraham I 	return phy_provider;
1163ff764963SKishon Vijay Abraham I }
1164ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
1165ff764963SKishon Vijay Abraham I 
1166ff764963SKishon Vijay Abraham I /**
1167ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister() - unregister phy provider from the framework
1168ff764963SKishon Vijay Abraham I  * @phy_provider: phy provider returned by of_phy_provider_register()
1169ff764963SKishon Vijay Abraham I  *
1170ff764963SKishon Vijay Abraham I  * Removes the phy_provider created using of_phy_provider_register().
1171ff764963SKishon Vijay Abraham I  */
1172ff764963SKishon Vijay Abraham I void of_phy_provider_unregister(struct phy_provider *phy_provider)
1173ff764963SKishon Vijay Abraham I {
1174ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_provider))
1175ff764963SKishon Vijay Abraham I 		return;
1176ff764963SKishon Vijay Abraham I 
1177ff764963SKishon Vijay Abraham I 	mutex_lock(&phy_provider_mutex);
1178ff764963SKishon Vijay Abraham I 	list_del(&phy_provider->list);
11791140f7c8SThierry Reding 	of_node_put(phy_provider->children);
1180ff764963SKishon Vijay Abraham I 	kfree(phy_provider);
1181ff764963SKishon Vijay Abraham I 	mutex_unlock(&phy_provider_mutex);
1182ff764963SKishon Vijay Abraham I }
1183ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
1184ff764963SKishon Vijay Abraham I 
1185ff764963SKishon Vijay Abraham I /**
1186ff764963SKishon Vijay Abraham I  * devm_of_phy_provider_unregister() - remove phy provider from the framework
1187ff764963SKishon Vijay Abraham I  * @dev: struct device of the phy provider
1188aad075c1SVinod Koul  * @phy_provider: phy provider returned by of_phy_provider_register()
1189ff764963SKishon Vijay Abraham I  *
1190ff764963SKishon Vijay Abraham I  * destroys the devres associated with this phy provider and invokes
1191ff764963SKishon Vijay Abraham I  * of_phy_provider_unregister to unregister the phy provider.
1192ff764963SKishon Vijay Abraham I  */
1193ff764963SKishon Vijay Abraham I void devm_of_phy_provider_unregister(struct device *dev,
1194b555f35fSVinod Koul 	struct phy_provider *phy_provider)
1195b555f35fSVinod Koul {
1196ff764963SKishon Vijay Abraham I 	int r;
1197ff764963SKishon Vijay Abraham I 
1198ff764963SKishon Vijay Abraham I 	r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
1199ff764963SKishon Vijay Abraham I 		phy_provider);
1200ff764963SKishon Vijay Abraham I 	dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
1201ff764963SKishon Vijay Abraham I }
1202ff764963SKishon Vijay Abraham I EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
1203ff764963SKishon Vijay Abraham I 
1204ff764963SKishon Vijay Abraham I /**
1205ff764963SKishon Vijay Abraham I  * phy_release() - release the phy
1206ff764963SKishon Vijay Abraham I  * @dev: the dev member within phy
1207ff764963SKishon Vijay Abraham I  *
1208ff764963SKishon Vijay Abraham I  * When the last reference to the device is removed, it is called
1209ff764963SKishon Vijay Abraham I  * from the embedded kobject as release method.
1210ff764963SKishon Vijay Abraham I  */
1211ff764963SKishon Vijay Abraham I static void phy_release(struct device *dev)
1212ff764963SKishon Vijay Abraham I {
1213ff764963SKishon Vijay Abraham I 	struct phy *phy;
1214ff764963SKishon Vijay Abraham I 
1215ff764963SKishon Vijay Abraham I 	phy = to_phy(dev);
1216ff764963SKishon Vijay Abraham I 	dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
12173be88125SRoger Quadros 	regulator_put(phy->pwr);
1218e73b49f1SRoger Quadros 	ida_simple_remove(&phy_ida, phy->id);
1219ff764963SKishon Vijay Abraham I 	kfree(phy);
1220ff764963SKishon Vijay Abraham I }
1221ff764963SKishon Vijay Abraham I 
1222ff764963SKishon Vijay Abraham I static int __init phy_core_init(void)
1223ff764963SKishon Vijay Abraham I {
1224ff764963SKishon Vijay Abraham I 	phy_class = class_create(THIS_MODULE, "phy");
1225ff764963SKishon Vijay Abraham I 	if (IS_ERR(phy_class)) {
1226ff764963SKishon Vijay Abraham I 		pr_err("failed to create phy class --> %ld\n",
1227ff764963SKishon Vijay Abraham I 			PTR_ERR(phy_class));
1228ff764963SKishon Vijay Abraham I 		return PTR_ERR(phy_class);
1229ff764963SKishon Vijay Abraham I 	}
1230ff764963SKishon Vijay Abraham I 
1231ff764963SKishon Vijay Abraham I 	phy_class->dev_release = phy_release;
1232ff764963SKishon Vijay Abraham I 
1233ff764963SKishon Vijay Abraham I 	return 0;
1234ff764963SKishon Vijay Abraham I }
1235cc013c28SPaul Gortmaker device_initcall(phy_core_init);
1236