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