xref: /OK3568_Linux_fs/kernel/drivers/phy/phy-core.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * phy-core.c  --  Generic Phy framework.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Kishon Vijay Abraham I <kishon@ti.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/export.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/err.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/phy/phy.h>
18*4882a593Smuzhiyun #include <linux/idr.h>
19*4882a593Smuzhiyun #include <linux/pm_runtime.h>
20*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static struct class *phy_class;
23*4882a593Smuzhiyun static DEFINE_MUTEX(phy_provider_mutex);
24*4882a593Smuzhiyun static LIST_HEAD(phy_provider_list);
25*4882a593Smuzhiyun static LIST_HEAD(phys);
26*4882a593Smuzhiyun static DEFINE_IDA(phy_ida);
27*4882a593Smuzhiyun 
devm_phy_release(struct device * dev,void * res)28*4882a593Smuzhiyun static void devm_phy_release(struct device *dev, void *res)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct phy *phy = *(struct phy **)res;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	phy_put(dev, phy);
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
devm_phy_provider_release(struct device * dev,void * res)35*4882a593Smuzhiyun static void devm_phy_provider_release(struct device *dev, void *res)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct phy_provider *phy_provider = *(struct phy_provider **)res;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	of_phy_provider_unregister(phy_provider);
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
devm_phy_consume(struct device * dev,void * res)42*4882a593Smuzhiyun static void devm_phy_consume(struct device *dev, void *res)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct phy *phy = *(struct phy **)res;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	phy_destroy(phy);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
devm_phy_match(struct device * dev,void * res,void * match_data)49*4882a593Smuzhiyun static int devm_phy_match(struct device *dev, void *res, void *match_data)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct phy **phy = res;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	return *phy == match_data;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun  * phy_create_lookup() - allocate and register PHY/device association
58*4882a593Smuzhiyun  * @phy: the phy of the association
59*4882a593Smuzhiyun  * @con_id: connection ID string on device
60*4882a593Smuzhiyun  * @dev_id: the device of the association
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Creates and registers phy_lookup entry.
63*4882a593Smuzhiyun  */
phy_create_lookup(struct phy * phy,const char * con_id,const char * dev_id)64*4882a593Smuzhiyun int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct phy_lookup *pl;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (!phy || !dev_id || !con_id)
69*4882a593Smuzhiyun 		return -EINVAL;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	pl = kzalloc(sizeof(*pl), GFP_KERNEL);
72*4882a593Smuzhiyun 	if (!pl)
73*4882a593Smuzhiyun 		return -ENOMEM;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	pl->dev_id = dev_id;
76*4882a593Smuzhiyun 	pl->con_id = con_id;
77*4882a593Smuzhiyun 	pl->phy = phy;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	mutex_lock(&phy_provider_mutex);
80*4882a593Smuzhiyun 	list_add_tail(&pl->node, &phys);
81*4882a593Smuzhiyun 	mutex_unlock(&phy_provider_mutex);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	return 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_create_lookup);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun  * phy_remove_lookup() - find and remove PHY/device association
89*4882a593Smuzhiyun  * @phy: the phy of the association
90*4882a593Smuzhiyun  * @con_id: connection ID string on device
91*4882a593Smuzhiyun  * @dev_id: the device of the association
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * Finds and unregisters phy_lookup entry that was created with
94*4882a593Smuzhiyun  * phy_create_lookup().
95*4882a593Smuzhiyun  */
phy_remove_lookup(struct phy * phy,const char * con_id,const char * dev_id)96*4882a593Smuzhiyun void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	struct phy_lookup *pl;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (!phy || !dev_id || !con_id)
101*4882a593Smuzhiyun 		return;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	mutex_lock(&phy_provider_mutex);
104*4882a593Smuzhiyun 	list_for_each_entry(pl, &phys, node)
105*4882a593Smuzhiyun 		if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
106*4882a593Smuzhiyun 		    !strcmp(pl->con_id, con_id)) {
107*4882a593Smuzhiyun 			list_del(&pl->node);
108*4882a593Smuzhiyun 			kfree(pl);
109*4882a593Smuzhiyun 			break;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 	mutex_unlock(&phy_provider_mutex);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_remove_lookup);
114*4882a593Smuzhiyun 
phy_find(struct device * dev,const char * con_id)115*4882a593Smuzhiyun static struct phy *phy_find(struct device *dev, const char *con_id)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	const char *dev_id = dev_name(dev);
118*4882a593Smuzhiyun 	struct phy_lookup *p, *pl = NULL;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	mutex_lock(&phy_provider_mutex);
121*4882a593Smuzhiyun 	list_for_each_entry(p, &phys, node)
122*4882a593Smuzhiyun 		if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
123*4882a593Smuzhiyun 			pl = p;
124*4882a593Smuzhiyun 			break;
125*4882a593Smuzhiyun 		}
126*4882a593Smuzhiyun 	mutex_unlock(&phy_provider_mutex);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	return pl ? pl->phy : ERR_PTR(-ENODEV);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
of_phy_provider_lookup(struct device_node * node)131*4882a593Smuzhiyun static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	struct phy_provider *phy_provider;
134*4882a593Smuzhiyun 	struct device_node *child;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	list_for_each_entry(phy_provider, &phy_provider_list, list) {
137*4882a593Smuzhiyun 		if (phy_provider->dev->of_node == node)
138*4882a593Smuzhiyun 			return phy_provider;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 		for_each_child_of_node(phy_provider->children, child)
141*4882a593Smuzhiyun 			if (child == node)
142*4882a593Smuzhiyun 				return phy_provider;
143*4882a593Smuzhiyun 	}
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	return ERR_PTR(-EPROBE_DEFER);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
phy_pm_runtime_get(struct phy * phy)148*4882a593Smuzhiyun int phy_pm_runtime_get(struct phy *phy)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	int ret;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!phy)
153*4882a593Smuzhiyun 		return 0;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (!pm_runtime_enabled(&phy->dev))
156*4882a593Smuzhiyun 		return -ENOTSUPP;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	ret = pm_runtime_get(&phy->dev);
159*4882a593Smuzhiyun 	if (ret < 0 && ret != -EINPROGRESS)
160*4882a593Smuzhiyun 		pm_runtime_put_noidle(&phy->dev);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	return ret;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
165*4882a593Smuzhiyun 
phy_pm_runtime_get_sync(struct phy * phy)166*4882a593Smuzhiyun int phy_pm_runtime_get_sync(struct phy *phy)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	int ret;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (!phy)
171*4882a593Smuzhiyun 		return 0;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	if (!pm_runtime_enabled(&phy->dev))
174*4882a593Smuzhiyun 		return -ENOTSUPP;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	ret = pm_runtime_get_sync(&phy->dev);
177*4882a593Smuzhiyun 	if (ret < 0)
178*4882a593Smuzhiyun 		pm_runtime_put_sync(&phy->dev);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	return ret;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
183*4882a593Smuzhiyun 
phy_pm_runtime_put(struct phy * phy)184*4882a593Smuzhiyun int phy_pm_runtime_put(struct phy *phy)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	if (!phy)
187*4882a593Smuzhiyun 		return 0;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (!pm_runtime_enabled(&phy->dev))
190*4882a593Smuzhiyun 		return -ENOTSUPP;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	return pm_runtime_put(&phy->dev);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
195*4882a593Smuzhiyun 
phy_pm_runtime_put_sync(struct phy * phy)196*4882a593Smuzhiyun int phy_pm_runtime_put_sync(struct phy *phy)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	if (!phy)
199*4882a593Smuzhiyun 		return 0;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (!pm_runtime_enabled(&phy->dev))
202*4882a593Smuzhiyun 		return -ENOTSUPP;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	return pm_runtime_put_sync(&phy->dev);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
207*4882a593Smuzhiyun 
phy_pm_runtime_allow(struct phy * phy)208*4882a593Smuzhiyun void phy_pm_runtime_allow(struct phy *phy)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	if (!phy)
211*4882a593Smuzhiyun 		return;
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (!pm_runtime_enabled(&phy->dev))
214*4882a593Smuzhiyun 		return;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	pm_runtime_allow(&phy->dev);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
219*4882a593Smuzhiyun 
phy_pm_runtime_forbid(struct phy * phy)220*4882a593Smuzhiyun void phy_pm_runtime_forbid(struct phy *phy)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	if (!phy)
223*4882a593Smuzhiyun 		return;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (!pm_runtime_enabled(&phy->dev))
226*4882a593Smuzhiyun 		return;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	pm_runtime_forbid(&phy->dev);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
231*4882a593Smuzhiyun 
phy_init(struct phy * phy)232*4882a593Smuzhiyun int phy_init(struct phy *phy)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	int ret;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (!phy)
237*4882a593Smuzhiyun 		return 0;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	ret = phy_pm_runtime_get_sync(phy);
240*4882a593Smuzhiyun 	if (ret < 0 && ret != -ENOTSUPP)
241*4882a593Smuzhiyun 		return ret;
242*4882a593Smuzhiyun 	ret = 0; /* Override possible ret == -ENOTSUPP */
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
245*4882a593Smuzhiyun 	if (phy->init_count == 0 && phy->ops->init) {
246*4882a593Smuzhiyun 		ret = phy->ops->init(phy);
247*4882a593Smuzhiyun 		if (ret < 0) {
248*4882a593Smuzhiyun 			dev_err(&phy->dev, "phy init failed --> %d\n", ret);
249*4882a593Smuzhiyun 			goto out;
250*4882a593Smuzhiyun 		}
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 	++phy->init_count;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun out:
255*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
256*4882a593Smuzhiyun 	phy_pm_runtime_put(phy);
257*4882a593Smuzhiyun 	return ret;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_init);
260*4882a593Smuzhiyun 
phy_exit(struct phy * phy)261*4882a593Smuzhiyun int phy_exit(struct phy *phy)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	int ret;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	if (!phy)
266*4882a593Smuzhiyun 		return 0;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	ret = phy_pm_runtime_get_sync(phy);
269*4882a593Smuzhiyun 	if (ret < 0 && ret != -ENOTSUPP)
270*4882a593Smuzhiyun 		return ret;
271*4882a593Smuzhiyun 	ret = 0; /* Override possible ret == -ENOTSUPP */
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
274*4882a593Smuzhiyun 	if (phy->init_count == 1 && phy->ops->exit) {
275*4882a593Smuzhiyun 		ret = phy->ops->exit(phy);
276*4882a593Smuzhiyun 		if (ret < 0) {
277*4882a593Smuzhiyun 			dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
278*4882a593Smuzhiyun 			goto out;
279*4882a593Smuzhiyun 		}
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 	--phy->init_count;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun out:
284*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
285*4882a593Smuzhiyun 	phy_pm_runtime_put(phy);
286*4882a593Smuzhiyun 	return ret;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_exit);
289*4882a593Smuzhiyun 
phy_power_on(struct phy * phy)290*4882a593Smuzhiyun int phy_power_on(struct phy *phy)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	int ret = 0;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	if (!phy)
295*4882a593Smuzhiyun 		goto out;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if (phy->pwr) {
298*4882a593Smuzhiyun 		ret = regulator_enable(phy->pwr);
299*4882a593Smuzhiyun 		if (ret)
300*4882a593Smuzhiyun 			goto out;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	ret = phy_pm_runtime_get_sync(phy);
304*4882a593Smuzhiyun 	if (ret < 0 && ret != -ENOTSUPP)
305*4882a593Smuzhiyun 		goto err_pm_sync;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	ret = 0; /* Override possible ret == -ENOTSUPP */
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
310*4882a593Smuzhiyun 	if (phy->power_count == 0 && phy->ops->power_on) {
311*4882a593Smuzhiyun 		ret = phy->ops->power_on(phy);
312*4882a593Smuzhiyun 		if (ret < 0) {
313*4882a593Smuzhiyun 			dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
314*4882a593Smuzhiyun 			goto err_pwr_on;
315*4882a593Smuzhiyun 		}
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 	++phy->power_count;
318*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
319*4882a593Smuzhiyun 	return 0;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun err_pwr_on:
322*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
323*4882a593Smuzhiyun 	phy_pm_runtime_put_sync(phy);
324*4882a593Smuzhiyun err_pm_sync:
325*4882a593Smuzhiyun 	if (phy->pwr)
326*4882a593Smuzhiyun 		regulator_disable(phy->pwr);
327*4882a593Smuzhiyun out:
328*4882a593Smuzhiyun 	return ret;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_power_on);
331*4882a593Smuzhiyun 
phy_power_off(struct phy * phy)332*4882a593Smuzhiyun int phy_power_off(struct phy *phy)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	int ret;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (!phy)
337*4882a593Smuzhiyun 		return 0;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
340*4882a593Smuzhiyun 	if (phy->power_count == 1 && phy->ops->power_off) {
341*4882a593Smuzhiyun 		ret =  phy->ops->power_off(phy);
342*4882a593Smuzhiyun 		if (ret < 0) {
343*4882a593Smuzhiyun 			dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
344*4882a593Smuzhiyun 			mutex_unlock(&phy->mutex);
345*4882a593Smuzhiyun 			return ret;
346*4882a593Smuzhiyun 		}
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 	--phy->power_count;
349*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
350*4882a593Smuzhiyun 	phy_pm_runtime_put(phy);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (phy->pwr)
353*4882a593Smuzhiyun 		regulator_disable(phy->pwr);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	return 0;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_power_off);
358*4882a593Smuzhiyun 
phy_set_mode_ext(struct phy * phy,enum phy_mode mode,int submode)359*4882a593Smuzhiyun int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun 	int ret;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	if (!phy || !phy->ops->set_mode)
364*4882a593Smuzhiyun 		return 0;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
367*4882a593Smuzhiyun 	ret = phy->ops->set_mode(phy, mode, submode);
368*4882a593Smuzhiyun 	if (!ret)
369*4882a593Smuzhiyun 		phy->attrs.mode = mode;
370*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	return ret;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_set_mode_ext);
375*4882a593Smuzhiyun 
phy_reset(struct phy * phy)376*4882a593Smuzhiyun int phy_reset(struct phy *phy)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun 	int ret;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	if (!phy || !phy->ops->reset)
381*4882a593Smuzhiyun 		return 0;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	ret = phy_pm_runtime_get_sync(phy);
384*4882a593Smuzhiyun 	if (ret < 0 && ret != -ENOTSUPP)
385*4882a593Smuzhiyun 		return ret;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
388*4882a593Smuzhiyun 	ret = phy->ops->reset(phy);
389*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	phy_pm_runtime_put(phy);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	return ret;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_reset);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun /**
398*4882a593Smuzhiyun  * phy_calibrate() - Tunes the phy hw parameters for current configuration
399*4882a593Smuzhiyun  * @phy: the phy returned by phy_get()
400*4882a593Smuzhiyun  *
401*4882a593Smuzhiyun  * Used to calibrate phy hardware, typically by adjusting some parameters in
402*4882a593Smuzhiyun  * runtime, which are otherwise lost after host controller reset and cannot
403*4882a593Smuzhiyun  * be applied in phy_init() or phy_power_on().
404*4882a593Smuzhiyun  *
405*4882a593Smuzhiyun  * Returns: 0 if successful, an negative error code otherwise
406*4882a593Smuzhiyun  */
phy_calibrate(struct phy * phy)407*4882a593Smuzhiyun int phy_calibrate(struct phy *phy)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	int ret;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	if (!phy || !phy->ops->calibrate)
412*4882a593Smuzhiyun 		return 0;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
415*4882a593Smuzhiyun 	ret = phy->ops->calibrate(phy);
416*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	return ret;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_calibrate);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /**
423*4882a593Smuzhiyun  * phy_configure() - Changes the phy parameters
424*4882a593Smuzhiyun  * @phy: the phy returned by phy_get()
425*4882a593Smuzhiyun  * @opts: New configuration to apply
426*4882a593Smuzhiyun  *
427*4882a593Smuzhiyun  * Used to change the PHY parameters. phy_init() must have been called
428*4882a593Smuzhiyun  * on the phy. The configuration will be applied on the current phy
429*4882a593Smuzhiyun  * mode, that can be changed using phy_set_mode().
430*4882a593Smuzhiyun  *
431*4882a593Smuzhiyun  * Returns: 0 if successful, an negative error code otherwise
432*4882a593Smuzhiyun  */
phy_configure(struct phy * phy,union phy_configure_opts * opts)433*4882a593Smuzhiyun int phy_configure(struct phy *phy, union phy_configure_opts *opts)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	int ret;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	if (!phy)
438*4882a593Smuzhiyun 		return -EINVAL;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (!phy->ops->configure)
441*4882a593Smuzhiyun 		return -EOPNOTSUPP;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
444*4882a593Smuzhiyun 	ret = phy->ops->configure(phy, opts);
445*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	return ret;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_configure);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /**
452*4882a593Smuzhiyun  * phy_validate() - Checks the phy parameters
453*4882a593Smuzhiyun  * @phy: the phy returned by phy_get()
454*4882a593Smuzhiyun  * @mode: phy_mode the configuration is applicable to.
455*4882a593Smuzhiyun  * @submode: PHY submode the configuration is applicable to.
456*4882a593Smuzhiyun  * @opts: Configuration to check
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  * Used to check that the current set of parameters can be handled by
459*4882a593Smuzhiyun  * the phy. Implementations are free to tune the parameters passed as
460*4882a593Smuzhiyun  * arguments if needed by some implementation detail or
461*4882a593Smuzhiyun  * constraints. It will not change any actual configuration of the
462*4882a593Smuzhiyun  * PHY, so calling it as many times as deemed fit will have no side
463*4882a593Smuzhiyun  * effect.
464*4882a593Smuzhiyun  *
465*4882a593Smuzhiyun  * Returns: 0 if successful, an negative error code otherwise
466*4882a593Smuzhiyun  */
phy_validate(struct phy * phy,enum phy_mode mode,int submode,union phy_configure_opts * opts)467*4882a593Smuzhiyun int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
468*4882a593Smuzhiyun 		 union phy_configure_opts *opts)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	int ret;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	if (!phy)
473*4882a593Smuzhiyun 		return -EINVAL;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	if (!phy->ops->validate)
476*4882a593Smuzhiyun 		return -EOPNOTSUPP;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
479*4882a593Smuzhiyun 	ret = phy->ops->validate(phy, mode, submode, opts);
480*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	return ret;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_validate);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun /**
487*4882a593Smuzhiyun  * _of_phy_get() - lookup and obtain a reference to a phy by phandle
488*4882a593Smuzhiyun  * @np: device_node for which to get the phy
489*4882a593Smuzhiyun  * @index: the index of the phy
490*4882a593Smuzhiyun  *
491*4882a593Smuzhiyun  * Returns the phy associated with the given phandle value,
492*4882a593Smuzhiyun  * after getting a refcount to it or -ENODEV if there is no such phy or
493*4882a593Smuzhiyun  * -EPROBE_DEFER if there is a phandle to the phy, but the device is
494*4882a593Smuzhiyun  * not yet loaded. This function uses of_xlate call back function provided
495*4882a593Smuzhiyun  * while registering the phy_provider to find the phy instance.
496*4882a593Smuzhiyun  */
_of_phy_get(struct device_node * np,int index)497*4882a593Smuzhiyun static struct phy *_of_phy_get(struct device_node *np, int index)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun 	int ret;
500*4882a593Smuzhiyun 	struct phy_provider *phy_provider;
501*4882a593Smuzhiyun 	struct phy *phy = NULL;
502*4882a593Smuzhiyun 	struct of_phandle_args args;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
505*4882a593Smuzhiyun 		index, &args);
506*4882a593Smuzhiyun 	if (ret)
507*4882a593Smuzhiyun 		return ERR_PTR(-ENODEV);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	/* This phy type handled by the usb-phy subsystem for now */
510*4882a593Smuzhiyun 	if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
511*4882a593Smuzhiyun 		return ERR_PTR(-ENODEV);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	mutex_lock(&phy_provider_mutex);
514*4882a593Smuzhiyun 	phy_provider = of_phy_provider_lookup(args.np);
515*4882a593Smuzhiyun 	if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
516*4882a593Smuzhiyun 		phy = ERR_PTR(-EPROBE_DEFER);
517*4882a593Smuzhiyun 		goto out_unlock;
518*4882a593Smuzhiyun 	}
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	if (!of_device_is_available(args.np)) {
521*4882a593Smuzhiyun 		dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
522*4882a593Smuzhiyun 		phy = ERR_PTR(-ENODEV);
523*4882a593Smuzhiyun 		goto out_put_module;
524*4882a593Smuzhiyun 	}
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	phy = phy_provider->of_xlate(phy_provider->dev, &args);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun out_put_module:
529*4882a593Smuzhiyun 	module_put(phy_provider->owner);
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun out_unlock:
532*4882a593Smuzhiyun 	mutex_unlock(&phy_provider_mutex);
533*4882a593Smuzhiyun 	of_node_put(args.np);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	return phy;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun /**
539*4882a593Smuzhiyun  * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
540*4882a593Smuzhiyun  * @np: device_node for which to get the phy
541*4882a593Smuzhiyun  * @con_id: name of the phy from device's point of view
542*4882a593Smuzhiyun  *
543*4882a593Smuzhiyun  * Returns the phy driver, after getting a refcount to it; or
544*4882a593Smuzhiyun  * -ENODEV if there is no such phy. The caller is responsible for
545*4882a593Smuzhiyun  * calling phy_put() to release that count.
546*4882a593Smuzhiyun  */
of_phy_get(struct device_node * np,const char * con_id)547*4882a593Smuzhiyun struct phy *of_phy_get(struct device_node *np, const char *con_id)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun 	struct phy *phy = NULL;
550*4882a593Smuzhiyun 	int index = 0;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (con_id)
553*4882a593Smuzhiyun 		index = of_property_match_string(np, "phy-names", con_id);
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	phy = _of_phy_get(np, index);
556*4882a593Smuzhiyun 	if (IS_ERR(phy))
557*4882a593Smuzhiyun 		return phy;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	if (!try_module_get(phy->ops->owner))
560*4882a593Smuzhiyun 		return ERR_PTR(-EPROBE_DEFER);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	get_device(&phy->dev);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	return phy;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_phy_get);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun /**
569*4882a593Smuzhiyun  * of_phy_put() - release the PHY
570*4882a593Smuzhiyun  * @phy: the phy returned by of_phy_get()
571*4882a593Smuzhiyun  *
572*4882a593Smuzhiyun  * Releases a refcount the caller received from of_phy_get().
573*4882a593Smuzhiyun  */
of_phy_put(struct phy * phy)574*4882a593Smuzhiyun void of_phy_put(struct phy *phy)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun 	if (!phy || IS_ERR(phy))
577*4882a593Smuzhiyun 		return;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	mutex_lock(&phy->mutex);
580*4882a593Smuzhiyun 	if (phy->ops->release)
581*4882a593Smuzhiyun 		phy->ops->release(phy);
582*4882a593Smuzhiyun 	mutex_unlock(&phy->mutex);
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	module_put(phy->ops->owner);
585*4882a593Smuzhiyun 	put_device(&phy->dev);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_phy_put);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun /**
590*4882a593Smuzhiyun  * phy_put() - release the PHY
591*4882a593Smuzhiyun  * @dev: device that wants to release this phy
592*4882a593Smuzhiyun  * @phy: the phy returned by phy_get()
593*4882a593Smuzhiyun  *
594*4882a593Smuzhiyun  * Releases a refcount the caller received from phy_get().
595*4882a593Smuzhiyun  */
phy_put(struct device * dev,struct phy * phy)596*4882a593Smuzhiyun void phy_put(struct device *dev, struct phy *phy)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun 	device_link_remove(dev, &phy->dev);
599*4882a593Smuzhiyun 	of_phy_put(phy);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_put);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun  * devm_phy_put() - release the PHY
605*4882a593Smuzhiyun  * @dev: device that wants to release this phy
606*4882a593Smuzhiyun  * @phy: the phy returned by devm_phy_get()
607*4882a593Smuzhiyun  *
608*4882a593Smuzhiyun  * destroys the devres associated with this phy and invokes phy_put
609*4882a593Smuzhiyun  * to release the phy.
610*4882a593Smuzhiyun  */
devm_phy_put(struct device * dev,struct phy * phy)611*4882a593Smuzhiyun void devm_phy_put(struct device *dev, struct phy *phy)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	int r;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (!phy)
616*4882a593Smuzhiyun 		return;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
619*4882a593Smuzhiyun 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_phy_put);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun /**
624*4882a593Smuzhiyun  * of_phy_simple_xlate() - returns the phy instance from phy provider
625*4882a593Smuzhiyun  * @dev: the PHY provider device
626*4882a593Smuzhiyun  * @args: of_phandle_args (not used here)
627*4882a593Smuzhiyun  *
628*4882a593Smuzhiyun  * Intended to be used by phy provider for the common case where #phy-cells is
629*4882a593Smuzhiyun  * 0. For other cases where #phy-cells is greater than '0', the phy provider
630*4882a593Smuzhiyun  * should provide a custom of_xlate function that reads the *args* and returns
631*4882a593Smuzhiyun  * the appropriate phy.
632*4882a593Smuzhiyun  */
of_phy_simple_xlate(struct device * dev,struct of_phandle_args * args)633*4882a593Smuzhiyun struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
634*4882a593Smuzhiyun 	*args)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun 	struct phy *phy;
637*4882a593Smuzhiyun 	struct class_dev_iter iter;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	class_dev_iter_init(&iter, phy_class, NULL, NULL);
640*4882a593Smuzhiyun 	while ((dev = class_dev_iter_next(&iter))) {
641*4882a593Smuzhiyun 		phy = to_phy(dev);
642*4882a593Smuzhiyun 		if (args->np != phy->dev.of_node)
643*4882a593Smuzhiyun 			continue;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 		class_dev_iter_exit(&iter);
646*4882a593Smuzhiyun 		return phy;
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	class_dev_iter_exit(&iter);
650*4882a593Smuzhiyun 	return ERR_PTR(-ENODEV);
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun /**
655*4882a593Smuzhiyun  * phy_get() - lookup and obtain a reference to a phy.
656*4882a593Smuzhiyun  * @dev: device that requests this phy
657*4882a593Smuzhiyun  * @string: the phy name as given in the dt data or the name of the controller
658*4882a593Smuzhiyun  * port for non-dt case
659*4882a593Smuzhiyun  *
660*4882a593Smuzhiyun  * Returns the phy driver, after getting a refcount to it; or
661*4882a593Smuzhiyun  * -ENODEV if there is no such phy.  The caller is responsible for
662*4882a593Smuzhiyun  * calling phy_put() to release that count.
663*4882a593Smuzhiyun  */
phy_get(struct device * dev,const char * string)664*4882a593Smuzhiyun struct phy *phy_get(struct device *dev, const char *string)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun 	int index = 0;
667*4882a593Smuzhiyun 	struct phy *phy;
668*4882a593Smuzhiyun 	struct device_link *link;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	if (string == NULL) {
671*4882a593Smuzhiyun 		dev_WARN(dev, "missing string\n");
672*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	if (dev->of_node) {
676*4882a593Smuzhiyun 		index = of_property_match_string(dev->of_node, "phy-names",
677*4882a593Smuzhiyun 			string);
678*4882a593Smuzhiyun 		phy = _of_phy_get(dev->of_node, index);
679*4882a593Smuzhiyun 	} else {
680*4882a593Smuzhiyun 		phy = phy_find(dev, string);
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 	if (IS_ERR(phy))
683*4882a593Smuzhiyun 		return phy;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (!try_module_get(phy->ops->owner))
686*4882a593Smuzhiyun 		return ERR_PTR(-EPROBE_DEFER);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	get_device(&phy->dev);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
691*4882a593Smuzhiyun 	if (!link)
692*4882a593Smuzhiyun 		dev_dbg(dev, "failed to create device link to %s\n",
693*4882a593Smuzhiyun 			dev_name(phy->dev.parent));
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	return phy;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_get);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun /**
700*4882a593Smuzhiyun  * phy_optional_get() - lookup and obtain a reference to an optional phy.
701*4882a593Smuzhiyun  * @dev: device that requests this phy
702*4882a593Smuzhiyun  * @string: the phy name as given in the dt data or the name of the controller
703*4882a593Smuzhiyun  * port for non-dt case
704*4882a593Smuzhiyun  *
705*4882a593Smuzhiyun  * Returns the phy driver, after getting a refcount to it; or
706*4882a593Smuzhiyun  * NULL if there is no such phy.  The caller is responsible for
707*4882a593Smuzhiyun  * calling phy_put() to release that count.
708*4882a593Smuzhiyun  */
phy_optional_get(struct device * dev,const char * string)709*4882a593Smuzhiyun struct phy *phy_optional_get(struct device *dev, const char *string)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	struct phy *phy = phy_get(dev, string);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	if (PTR_ERR(phy) == -ENODEV)
714*4882a593Smuzhiyun 		phy = NULL;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	return phy;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_optional_get);
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /**
721*4882a593Smuzhiyun  * devm_phy_get() - lookup and obtain a reference to a phy.
722*4882a593Smuzhiyun  * @dev: device that requests this phy
723*4882a593Smuzhiyun  * @string: the phy name as given in the dt data or phy device name
724*4882a593Smuzhiyun  * for non-dt case
725*4882a593Smuzhiyun  *
726*4882a593Smuzhiyun  * Gets the phy using phy_get(), and associates a device with it using
727*4882a593Smuzhiyun  * devres. On driver detach, release function is invoked on the devres data,
728*4882a593Smuzhiyun  * then, devres data is freed.
729*4882a593Smuzhiyun  */
devm_phy_get(struct device * dev,const char * string)730*4882a593Smuzhiyun struct phy *devm_phy_get(struct device *dev, const char *string)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	struct phy **ptr, *phy;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
735*4882a593Smuzhiyun 	if (!ptr)
736*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	phy = phy_get(dev, string);
739*4882a593Smuzhiyun 	if (!IS_ERR(phy)) {
740*4882a593Smuzhiyun 		*ptr = phy;
741*4882a593Smuzhiyun 		devres_add(dev, ptr);
742*4882a593Smuzhiyun 	} else {
743*4882a593Smuzhiyun 		devres_free(ptr);
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	return phy;
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_phy_get);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun /**
751*4882a593Smuzhiyun  * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
752*4882a593Smuzhiyun  * @dev: device that requests this phy
753*4882a593Smuzhiyun  * @string: the phy name as given in the dt data or phy device name
754*4882a593Smuzhiyun  * for non-dt case
755*4882a593Smuzhiyun  *
756*4882a593Smuzhiyun  * Gets the phy using phy_get(), and associates a device with it using
757*4882a593Smuzhiyun  * devres. On driver detach, release function is invoked on the devres
758*4882a593Smuzhiyun  * data, then, devres data is freed. This differs to devm_phy_get() in
759*4882a593Smuzhiyun  * that if the phy does not exist, it is not considered an error and
760*4882a593Smuzhiyun  * -ENODEV will not be returned. Instead the NULL phy is returned,
761*4882a593Smuzhiyun  * which can be passed to all other phy consumer calls.
762*4882a593Smuzhiyun  */
devm_phy_optional_get(struct device * dev,const char * string)763*4882a593Smuzhiyun struct phy *devm_phy_optional_get(struct device *dev, const char *string)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun 	struct phy *phy = devm_phy_get(dev, string);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (PTR_ERR(phy) == -ENODEV)
768*4882a593Smuzhiyun 		phy = NULL;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	return phy;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_phy_optional_get);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun /**
775*4882a593Smuzhiyun  * devm_of_phy_get() - lookup and obtain a reference to a phy.
776*4882a593Smuzhiyun  * @dev: device that requests this phy
777*4882a593Smuzhiyun  * @np: node containing the phy
778*4882a593Smuzhiyun  * @con_id: name of the phy from device's point of view
779*4882a593Smuzhiyun  *
780*4882a593Smuzhiyun  * Gets the phy using of_phy_get(), and associates a device with it using
781*4882a593Smuzhiyun  * devres. On driver detach, release function is invoked on the devres data,
782*4882a593Smuzhiyun  * then, devres data is freed.
783*4882a593Smuzhiyun  */
devm_of_phy_get(struct device * dev,struct device_node * np,const char * con_id)784*4882a593Smuzhiyun struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
785*4882a593Smuzhiyun 			    const char *con_id)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	struct phy **ptr, *phy;
788*4882a593Smuzhiyun 	struct device_link *link;
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
791*4882a593Smuzhiyun 	if (!ptr)
792*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	phy = of_phy_get(np, con_id);
795*4882a593Smuzhiyun 	if (!IS_ERR(phy)) {
796*4882a593Smuzhiyun 		*ptr = phy;
797*4882a593Smuzhiyun 		devres_add(dev, ptr);
798*4882a593Smuzhiyun 	} else {
799*4882a593Smuzhiyun 		devres_free(ptr);
800*4882a593Smuzhiyun 		return phy;
801*4882a593Smuzhiyun 	}
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
804*4882a593Smuzhiyun 	if (!link)
805*4882a593Smuzhiyun 		dev_dbg(dev, "failed to create device link to %s\n",
806*4882a593Smuzhiyun 			dev_name(phy->dev.parent));
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	return phy;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_of_phy_get);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun /**
813*4882a593Smuzhiyun  * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
814*4882a593Smuzhiyun  * @dev: device that requests this phy
815*4882a593Smuzhiyun  * @np: node containing the phy
816*4882a593Smuzhiyun  * @index: index of the phy
817*4882a593Smuzhiyun  *
818*4882a593Smuzhiyun  * Gets the phy using _of_phy_get(), then gets a refcount to it,
819*4882a593Smuzhiyun  * and associates a device with it using devres. On driver detach,
820*4882a593Smuzhiyun  * release function is invoked on the devres data,
821*4882a593Smuzhiyun  * then, devres data is freed.
822*4882a593Smuzhiyun  *
823*4882a593Smuzhiyun  */
devm_of_phy_get_by_index(struct device * dev,struct device_node * np,int index)824*4882a593Smuzhiyun struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
825*4882a593Smuzhiyun 				     int index)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	struct phy **ptr, *phy;
828*4882a593Smuzhiyun 	struct device_link *link;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
831*4882a593Smuzhiyun 	if (!ptr)
832*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	phy = _of_phy_get(np, index);
835*4882a593Smuzhiyun 	if (IS_ERR(phy)) {
836*4882a593Smuzhiyun 		devres_free(ptr);
837*4882a593Smuzhiyun 		return phy;
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	if (!try_module_get(phy->ops->owner)) {
841*4882a593Smuzhiyun 		devres_free(ptr);
842*4882a593Smuzhiyun 		return ERR_PTR(-EPROBE_DEFER);
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 
845*4882a593Smuzhiyun 	get_device(&phy->dev);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	*ptr = phy;
848*4882a593Smuzhiyun 	devres_add(dev, ptr);
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
851*4882a593Smuzhiyun 	if (!link)
852*4882a593Smuzhiyun 		dev_dbg(dev, "failed to create device link to %s\n",
853*4882a593Smuzhiyun 			dev_name(phy->dev.parent));
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	return phy;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun /**
860*4882a593Smuzhiyun  * phy_create() - create a new phy
861*4882a593Smuzhiyun  * @dev: device that is creating the new phy
862*4882a593Smuzhiyun  * @node: device node of the phy
863*4882a593Smuzhiyun  * @ops: function pointers for performing phy operations
864*4882a593Smuzhiyun  *
865*4882a593Smuzhiyun  * Called to create a phy using phy framework.
866*4882a593Smuzhiyun  */
phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)867*4882a593Smuzhiyun struct phy *phy_create(struct device *dev, struct device_node *node,
868*4882a593Smuzhiyun 		       const struct phy_ops *ops)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun 	int ret;
871*4882a593Smuzhiyun 	int id;
872*4882a593Smuzhiyun 	struct phy *phy;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	if (WARN_ON(!dev))
875*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	phy = kzalloc(sizeof(*phy), GFP_KERNEL);
878*4882a593Smuzhiyun 	if (!phy)
879*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
882*4882a593Smuzhiyun 	if (id < 0) {
883*4882a593Smuzhiyun 		dev_err(dev, "unable to get id\n");
884*4882a593Smuzhiyun 		ret = id;
885*4882a593Smuzhiyun 		goto free_phy;
886*4882a593Smuzhiyun 	}
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	device_initialize(&phy->dev);
889*4882a593Smuzhiyun 	mutex_init(&phy->mutex);
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	phy->dev.class = phy_class;
892*4882a593Smuzhiyun 	phy->dev.parent = dev;
893*4882a593Smuzhiyun 	phy->dev.of_node = node ?: dev->of_node;
894*4882a593Smuzhiyun 	phy->id = id;
895*4882a593Smuzhiyun 	phy->ops = ops;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
898*4882a593Smuzhiyun 	if (ret)
899*4882a593Smuzhiyun 		goto put_dev;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	/* phy-supply */
902*4882a593Smuzhiyun 	phy->pwr = regulator_get_optional(&phy->dev, "phy");
903*4882a593Smuzhiyun 	if (IS_ERR(phy->pwr)) {
904*4882a593Smuzhiyun 		ret = PTR_ERR(phy->pwr);
905*4882a593Smuzhiyun 		if (ret == -EPROBE_DEFER)
906*4882a593Smuzhiyun 			goto put_dev;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 		phy->pwr = NULL;
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	ret = device_add(&phy->dev);
912*4882a593Smuzhiyun 	if (ret)
913*4882a593Smuzhiyun 		goto put_dev;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	if (pm_runtime_enabled(dev)) {
916*4882a593Smuzhiyun 		pm_runtime_enable(&phy->dev);
917*4882a593Smuzhiyun 		pm_runtime_no_callbacks(&phy->dev);
918*4882a593Smuzhiyun 	}
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	return phy;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun put_dev:
923*4882a593Smuzhiyun 	put_device(&phy->dev);  /* calls phy_release() which frees resources */
924*4882a593Smuzhiyun 	return ERR_PTR(ret);
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun free_phy:
927*4882a593Smuzhiyun 	kfree(phy);
928*4882a593Smuzhiyun 	return ERR_PTR(ret);
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_create);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun /**
933*4882a593Smuzhiyun  * devm_phy_create() - create a new phy
934*4882a593Smuzhiyun  * @dev: device that is creating the new phy
935*4882a593Smuzhiyun  * @node: device node of the phy
936*4882a593Smuzhiyun  * @ops: function pointers for performing phy operations
937*4882a593Smuzhiyun  *
938*4882a593Smuzhiyun  * Creates a new PHY device adding it to the PHY class.
939*4882a593Smuzhiyun  * While at that, it also associates the device with the phy using devres.
940*4882a593Smuzhiyun  * On driver detach, release function is invoked on the devres data,
941*4882a593Smuzhiyun  * then, devres data is freed.
942*4882a593Smuzhiyun  */
devm_phy_create(struct device * dev,struct device_node * node,const struct phy_ops * ops)943*4882a593Smuzhiyun struct phy *devm_phy_create(struct device *dev, struct device_node *node,
944*4882a593Smuzhiyun 			    const struct phy_ops *ops)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun 	struct phy **ptr, *phy;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
949*4882a593Smuzhiyun 	if (!ptr)
950*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	phy = phy_create(dev, node, ops);
953*4882a593Smuzhiyun 	if (!IS_ERR(phy)) {
954*4882a593Smuzhiyun 		*ptr = phy;
955*4882a593Smuzhiyun 		devres_add(dev, ptr);
956*4882a593Smuzhiyun 	} else {
957*4882a593Smuzhiyun 		devres_free(ptr);
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	return phy;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_phy_create);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun /**
965*4882a593Smuzhiyun  * phy_destroy() - destroy the phy
966*4882a593Smuzhiyun  * @phy: the phy to be destroyed
967*4882a593Smuzhiyun  *
968*4882a593Smuzhiyun  * Called to destroy the phy.
969*4882a593Smuzhiyun  */
phy_destroy(struct phy * phy)970*4882a593Smuzhiyun void phy_destroy(struct phy *phy)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	pm_runtime_disable(&phy->dev);
973*4882a593Smuzhiyun 	device_unregister(&phy->dev);
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(phy_destroy);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun /**
978*4882a593Smuzhiyun  * devm_phy_destroy() - destroy the PHY
979*4882a593Smuzhiyun  * @dev: device that wants to release this phy
980*4882a593Smuzhiyun  * @phy: the phy returned by devm_phy_get()
981*4882a593Smuzhiyun  *
982*4882a593Smuzhiyun  * destroys the devres associated with this phy and invokes phy_destroy
983*4882a593Smuzhiyun  * to destroy the phy.
984*4882a593Smuzhiyun  */
devm_phy_destroy(struct device * dev,struct phy * phy)985*4882a593Smuzhiyun void devm_phy_destroy(struct device *dev, struct phy *phy)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	int r;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
990*4882a593Smuzhiyun 	dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_phy_destroy);
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun /**
995*4882a593Smuzhiyun  * __of_phy_provider_register() - create/register phy provider with the framework
996*4882a593Smuzhiyun  * @dev: struct device of the phy provider
997*4882a593Smuzhiyun  * @children: device node containing children (if different from dev->of_node)
998*4882a593Smuzhiyun  * @owner: the module owner containing of_xlate
999*4882a593Smuzhiyun  * @of_xlate: function pointer to obtain phy instance from phy provider
1000*4882a593Smuzhiyun  *
1001*4882a593Smuzhiyun  * Creates struct phy_provider from dev and of_xlate function pointer.
1002*4882a593Smuzhiyun  * This is used in the case of dt boot for finding the phy instance from
1003*4882a593Smuzhiyun  * phy provider.
1004*4882a593Smuzhiyun  *
1005*4882a593Smuzhiyun  * If the PHY provider doesn't nest children directly but uses a separate
1006*4882a593Smuzhiyun  * child node to contain the individual children, the @children parameter
1007*4882a593Smuzhiyun  * can be used to override the default. If NULL, the default (dev->of_node)
1008*4882a593Smuzhiyun  * will be used. If non-NULL, the device node must be a child (or further
1009*4882a593Smuzhiyun  * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
1010*4882a593Smuzhiyun  * error code is returned.
1011*4882a593Smuzhiyun  */
__of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,struct of_phandle_args * args))1012*4882a593Smuzhiyun struct phy_provider *__of_phy_provider_register(struct device *dev,
1013*4882a593Smuzhiyun 	struct device_node *children, struct module *owner,
1014*4882a593Smuzhiyun 	struct phy * (*of_xlate)(struct device *dev,
1015*4882a593Smuzhiyun 				 struct of_phandle_args *args))
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	struct phy_provider *phy_provider;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	/*
1020*4882a593Smuzhiyun 	 * If specified, the device node containing the children must itself
1021*4882a593Smuzhiyun 	 * be the provider's device node or a child (or further descendant)
1022*4882a593Smuzhiyun 	 * thereof.
1023*4882a593Smuzhiyun 	 */
1024*4882a593Smuzhiyun 	if (children) {
1025*4882a593Smuzhiyun 		struct device_node *parent = of_node_get(children), *next;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 		while (parent) {
1028*4882a593Smuzhiyun 			if (parent == dev->of_node)
1029*4882a593Smuzhiyun 				break;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 			next = of_get_parent(parent);
1032*4882a593Smuzhiyun 			of_node_put(parent);
1033*4882a593Smuzhiyun 			parent = next;
1034*4882a593Smuzhiyun 		}
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 		if (!parent)
1037*4882a593Smuzhiyun 			return ERR_PTR(-EINVAL);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 		of_node_put(parent);
1040*4882a593Smuzhiyun 	} else {
1041*4882a593Smuzhiyun 		children = dev->of_node;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
1045*4882a593Smuzhiyun 	if (!phy_provider)
1046*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	phy_provider->dev = dev;
1049*4882a593Smuzhiyun 	phy_provider->children = of_node_get(children);
1050*4882a593Smuzhiyun 	phy_provider->owner = owner;
1051*4882a593Smuzhiyun 	phy_provider->of_xlate = of_xlate;
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	mutex_lock(&phy_provider_mutex);
1054*4882a593Smuzhiyun 	list_add_tail(&phy_provider->list, &phy_provider_list);
1055*4882a593Smuzhiyun 	mutex_unlock(&phy_provider_mutex);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	return phy_provider;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__of_phy_provider_register);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun /**
1062*4882a593Smuzhiyun  * __devm_of_phy_provider_register() - create/register phy provider with the
1063*4882a593Smuzhiyun  * framework
1064*4882a593Smuzhiyun  * @dev: struct device of the phy provider
1065*4882a593Smuzhiyun  * @children: device node containing children (if different from dev->of_node)
1066*4882a593Smuzhiyun  * @owner: the module owner containing of_xlate
1067*4882a593Smuzhiyun  * @of_xlate: function pointer to obtain phy instance from phy provider
1068*4882a593Smuzhiyun  *
1069*4882a593Smuzhiyun  * Creates struct phy_provider from dev and of_xlate function pointer.
1070*4882a593Smuzhiyun  * This is used in the case of dt boot for finding the phy instance from
1071*4882a593Smuzhiyun  * phy provider. While at that, it also associates the device with the
1072*4882a593Smuzhiyun  * phy provider using devres. On driver detach, release function is invoked
1073*4882a593Smuzhiyun  * on the devres data, then, devres data is freed.
1074*4882a593Smuzhiyun  */
__devm_of_phy_provider_register(struct device * dev,struct device_node * children,struct module * owner,struct phy * (* of_xlate)(struct device * dev,struct of_phandle_args * args))1075*4882a593Smuzhiyun struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
1076*4882a593Smuzhiyun 	struct device_node *children, struct module *owner,
1077*4882a593Smuzhiyun 	struct phy * (*of_xlate)(struct device *dev,
1078*4882a593Smuzhiyun 				 struct of_phandle_args *args))
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun 	struct phy_provider **ptr, *phy_provider;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
1083*4882a593Smuzhiyun 	if (!ptr)
1084*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	phy_provider = __of_phy_provider_register(dev, children, owner,
1087*4882a593Smuzhiyun 						  of_xlate);
1088*4882a593Smuzhiyun 	if (!IS_ERR(phy_provider)) {
1089*4882a593Smuzhiyun 		*ptr = phy_provider;
1090*4882a593Smuzhiyun 		devres_add(dev, ptr);
1091*4882a593Smuzhiyun 	} else {
1092*4882a593Smuzhiyun 		devres_free(ptr);
1093*4882a593Smuzhiyun 	}
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	return phy_provider;
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun /**
1100*4882a593Smuzhiyun  * of_phy_provider_unregister() - unregister phy provider from the framework
1101*4882a593Smuzhiyun  * @phy_provider: phy provider returned by of_phy_provider_register()
1102*4882a593Smuzhiyun  *
1103*4882a593Smuzhiyun  * Removes the phy_provider created using of_phy_provider_register().
1104*4882a593Smuzhiyun  */
of_phy_provider_unregister(struct phy_provider * phy_provider)1105*4882a593Smuzhiyun void of_phy_provider_unregister(struct phy_provider *phy_provider)
1106*4882a593Smuzhiyun {
1107*4882a593Smuzhiyun 	if (IS_ERR(phy_provider))
1108*4882a593Smuzhiyun 		return;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	mutex_lock(&phy_provider_mutex);
1111*4882a593Smuzhiyun 	list_del(&phy_provider->list);
1112*4882a593Smuzhiyun 	of_node_put(phy_provider->children);
1113*4882a593Smuzhiyun 	kfree(phy_provider);
1114*4882a593Smuzhiyun 	mutex_unlock(&phy_provider_mutex);
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun /**
1119*4882a593Smuzhiyun  * devm_of_phy_provider_unregister() - remove phy provider from the framework
1120*4882a593Smuzhiyun  * @dev: struct device of the phy provider
1121*4882a593Smuzhiyun  * @phy_provider: phy provider returned by of_phy_provider_register()
1122*4882a593Smuzhiyun  *
1123*4882a593Smuzhiyun  * destroys the devres associated with this phy provider and invokes
1124*4882a593Smuzhiyun  * of_phy_provider_unregister to unregister the phy provider.
1125*4882a593Smuzhiyun  */
devm_of_phy_provider_unregister(struct device * dev,struct phy_provider * phy_provider)1126*4882a593Smuzhiyun void devm_of_phy_provider_unregister(struct device *dev,
1127*4882a593Smuzhiyun 	struct phy_provider *phy_provider)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun 	int r;
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
1132*4882a593Smuzhiyun 		phy_provider);
1133*4882a593Smuzhiyun 	dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun /**
1138*4882a593Smuzhiyun  * phy_release() - release the phy
1139*4882a593Smuzhiyun  * @dev: the dev member within phy
1140*4882a593Smuzhiyun  *
1141*4882a593Smuzhiyun  * When the last reference to the device is removed, it is called
1142*4882a593Smuzhiyun  * from the embedded kobject as release method.
1143*4882a593Smuzhiyun  */
phy_release(struct device * dev)1144*4882a593Smuzhiyun static void phy_release(struct device *dev)
1145*4882a593Smuzhiyun {
1146*4882a593Smuzhiyun 	struct phy *phy;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	phy = to_phy(dev);
1149*4882a593Smuzhiyun 	dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
1150*4882a593Smuzhiyun 	regulator_put(phy->pwr);
1151*4882a593Smuzhiyun 	ida_simple_remove(&phy_ida, phy->id);
1152*4882a593Smuzhiyun 	kfree(phy);
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun 
phy_core_init(void)1155*4882a593Smuzhiyun static int __init phy_core_init(void)
1156*4882a593Smuzhiyun {
1157*4882a593Smuzhiyun 	phy_class = class_create(THIS_MODULE, "phy");
1158*4882a593Smuzhiyun 	if (IS_ERR(phy_class)) {
1159*4882a593Smuzhiyun 		pr_err("failed to create phy class --> %ld\n",
1160*4882a593Smuzhiyun 			PTR_ERR(phy_class));
1161*4882a593Smuzhiyun 		return PTR_ERR(phy_class);
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	phy_class->dev_release = phy_release;
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun device_initcall(phy_core_init);
1169