2 * phy-core.c -- Generic Phy framework.
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/export.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
21 #include <linux/phy/phy.h>
22 #include <linux/idr.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regulator/consumer.h>
26 static struct class *phy_class
;
27 static DEFINE_MUTEX(phy_provider_mutex
);
28 static LIST_HEAD(phy_provider_list
);
29 static DEFINE_IDA(phy_ida
);
31 static void devm_phy_release(struct device
*dev
, void *res
)
33 struct phy
*phy
= *(struct phy
**)res
;
38 static void devm_phy_provider_release(struct device
*dev
, void *res
)
40 struct phy_provider
*phy_provider
= *(struct phy_provider
**)res
;
42 of_phy_provider_unregister(phy_provider
);
45 static void devm_phy_consume(struct device
*dev
, void *res
)
47 struct phy
*phy
= *(struct phy
**)res
;
52 static int devm_phy_match(struct device
*dev
, void *res
, void *match_data
)
54 return res
== match_data
;
57 static struct phy
*phy_lookup(struct device
*device
, const char *port
)
62 struct phy_consumer
*consumers
;
63 struct class_dev_iter iter
;
65 class_dev_iter_init(&iter
, phy_class
, NULL
, NULL
);
66 while ((dev
= class_dev_iter_next(&iter
))) {
71 count
= phy
->init_data
->num_consumers
;
72 consumers
= phy
->init_data
->consumers
;
74 if (!strcmp(consumers
->dev_name
, dev_name(device
)) &&
75 !strcmp(consumers
->port
, port
)) {
76 class_dev_iter_exit(&iter
);
83 class_dev_iter_exit(&iter
);
84 return ERR_PTR(-ENODEV
);
87 static struct phy_provider
*of_phy_provider_lookup(struct device_node
*node
)
89 struct phy_provider
*phy_provider
;
91 list_for_each_entry(phy_provider
, &phy_provider_list
, list
) {
92 if (phy_provider
->dev
->of_node
== node
)
96 return ERR_PTR(-EPROBE_DEFER
);
99 int phy_pm_runtime_get(struct phy
*phy
)
103 if (!pm_runtime_enabled(&phy
->dev
))
106 ret
= pm_runtime_get(&phy
->dev
);
107 if (ret
< 0 && ret
!= -EINPROGRESS
)
108 pm_runtime_put_noidle(&phy
->dev
);
112 EXPORT_SYMBOL_GPL(phy_pm_runtime_get
);
114 int phy_pm_runtime_get_sync(struct phy
*phy
)
118 if (!pm_runtime_enabled(&phy
->dev
))
121 ret
= pm_runtime_get_sync(&phy
->dev
);
123 pm_runtime_put_sync(&phy
->dev
);
127 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync
);
129 int phy_pm_runtime_put(struct phy
*phy
)
131 if (!pm_runtime_enabled(&phy
->dev
))
134 return pm_runtime_put(&phy
->dev
);
136 EXPORT_SYMBOL_GPL(phy_pm_runtime_put
);
138 int phy_pm_runtime_put_sync(struct phy
*phy
)
140 if (!pm_runtime_enabled(&phy
->dev
))
143 return pm_runtime_put_sync(&phy
->dev
);
145 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync
);
147 void phy_pm_runtime_allow(struct phy
*phy
)
149 if (!pm_runtime_enabled(&phy
->dev
))
152 pm_runtime_allow(&phy
->dev
);
154 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow
);
156 void phy_pm_runtime_forbid(struct phy
*phy
)
158 if (!pm_runtime_enabled(&phy
->dev
))
161 pm_runtime_forbid(&phy
->dev
);
163 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid
);
165 int phy_init(struct phy
*phy
)
172 ret
= phy_pm_runtime_get_sync(phy
);
173 if (ret
< 0 && ret
!= -ENOTSUPP
)
176 mutex_lock(&phy
->mutex
);
177 if (phy
->init_count
== 0 && phy
->ops
->init
) {
178 ret
= phy
->ops
->init(phy
);
180 dev_err(&phy
->dev
, "phy init failed --> %d\n", ret
);
184 ret
= 0; /* Override possible ret == -ENOTSUPP */
189 mutex_unlock(&phy
->mutex
);
190 phy_pm_runtime_put(phy
);
193 EXPORT_SYMBOL_GPL(phy_init
);
195 int phy_exit(struct phy
*phy
)
202 ret
= phy_pm_runtime_get_sync(phy
);
203 if (ret
< 0 && ret
!= -ENOTSUPP
)
206 mutex_lock(&phy
->mutex
);
207 if (phy
->init_count
== 1 && phy
->ops
->exit
) {
208 ret
= phy
->ops
->exit(phy
);
210 dev_err(&phy
->dev
, "phy exit failed --> %d\n", ret
);
217 mutex_unlock(&phy
->mutex
);
218 phy_pm_runtime_put(phy
);
221 EXPORT_SYMBOL_GPL(phy_exit
);
223 int phy_power_on(struct phy
*phy
)
231 ret
= regulator_enable(phy
->pwr
);
236 ret
= phy_pm_runtime_get_sync(phy
);
237 if (ret
< 0 && ret
!= -ENOTSUPP
)
240 mutex_lock(&phy
->mutex
);
241 if (phy
->power_count
== 0 && phy
->ops
->power_on
) {
242 ret
= phy
->ops
->power_on(phy
);
244 dev_err(&phy
->dev
, "phy poweron failed --> %d\n", ret
);
248 ret
= 0; /* Override possible ret == -ENOTSUPP */
251 mutex_unlock(&phy
->mutex
);
255 mutex_unlock(&phy
->mutex
);
256 phy_pm_runtime_put_sync(phy
);
258 regulator_disable(phy
->pwr
);
262 EXPORT_SYMBOL_GPL(phy_power_on
);
264 int phy_power_off(struct phy
*phy
)
271 mutex_lock(&phy
->mutex
);
272 if (phy
->power_count
== 1 && phy
->ops
->power_off
) {
273 ret
= phy
->ops
->power_off(phy
);
275 dev_err(&phy
->dev
, "phy poweroff failed --> %d\n", ret
);
276 mutex_unlock(&phy
->mutex
);
281 mutex_unlock(&phy
->mutex
);
282 phy_pm_runtime_put(phy
);
285 regulator_disable(phy
->pwr
);
289 EXPORT_SYMBOL_GPL(phy_power_off
);
292 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
293 * @np: device_node for which to get the phy
294 * @index: the index of the phy
296 * Returns the phy associated with the given phandle value,
297 * after getting a refcount to it or -ENODEV if there is no such phy or
298 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
299 * not yet loaded. This function uses of_xlate call back function provided
300 * while registering the phy_provider to find the phy instance.
302 static struct phy
*_of_phy_get(struct device_node
*np
, int index
)
305 struct phy_provider
*phy_provider
;
306 struct phy
*phy
= NULL
;
307 struct of_phandle_args args
;
309 ret
= of_parse_phandle_with_args(np
, "phys", "#phy-cells",
312 return ERR_PTR(-ENODEV
);
314 mutex_lock(&phy_provider_mutex
);
315 phy_provider
= of_phy_provider_lookup(args
.np
);
316 if (IS_ERR(phy_provider
) || !try_module_get(phy_provider
->owner
)) {
317 phy
= ERR_PTR(-EPROBE_DEFER
);
321 phy
= phy_provider
->of_xlate(phy_provider
->dev
, &args
);
322 module_put(phy_provider
->owner
);
325 mutex_unlock(&phy_provider_mutex
);
326 of_node_put(args
.np
);
332 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
333 * @np: device_node for which to get the phy
334 * @con_id: name of the phy from device's point of view
336 * Returns the phy driver, after getting a refcount to it; or
337 * -ENODEV if there is no such phy. The caller is responsible for
338 * calling phy_put() to release that count.
340 struct phy
*of_phy_get(struct device_node
*np
, const char *con_id
)
342 struct phy
*phy
= NULL
;
346 index
= of_property_match_string(np
, "phy-names", con_id
);
348 phy
= _of_phy_get(np
, index
);
352 if (!try_module_get(phy
->ops
->owner
))
353 return ERR_PTR(-EPROBE_DEFER
);
355 get_device(&phy
->dev
);
359 EXPORT_SYMBOL_GPL(of_phy_get
);
362 * phy_put() - release the PHY
363 * @phy: the phy returned by phy_get()
365 * Releases a refcount the caller received from phy_get().
367 void phy_put(struct phy
*phy
)
369 if (!phy
|| IS_ERR(phy
))
372 module_put(phy
->ops
->owner
);
373 put_device(&phy
->dev
);
375 EXPORT_SYMBOL_GPL(phy_put
);
378 * devm_phy_put() - release the PHY
379 * @dev: device that wants to release this phy
380 * @phy: the phy returned by devm_phy_get()
382 * destroys the devres associated with this phy and invokes phy_put
383 * to release the phy.
385 void devm_phy_put(struct device
*dev
, struct phy
*phy
)
392 r
= devres_destroy(dev
, devm_phy_release
, devm_phy_match
, phy
);
393 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
395 EXPORT_SYMBOL_GPL(devm_phy_put
);
398 * of_phy_simple_xlate() - returns the phy instance from phy provider
399 * @dev: the PHY provider device
400 * @args: of_phandle_args (not used here)
402 * Intended to be used by phy provider for the common case where #phy-cells is
403 * 0. For other cases where #phy-cells is greater than '0', the phy provider
404 * should provide a custom of_xlate function that reads the *args* and returns
405 * the appropriate phy.
407 struct phy
*of_phy_simple_xlate(struct device
*dev
, struct of_phandle_args
411 struct class_dev_iter iter
;
412 struct device_node
*node
= dev
->of_node
;
414 class_dev_iter_init(&iter
, phy_class
, NULL
, NULL
);
415 while ((dev
= class_dev_iter_next(&iter
))) {
417 if (node
!= phy
->dev
.of_node
)
420 class_dev_iter_exit(&iter
);
424 class_dev_iter_exit(&iter
);
425 return ERR_PTR(-ENODEV
);
427 EXPORT_SYMBOL_GPL(of_phy_simple_xlate
);
430 * phy_get() - lookup and obtain a reference to a phy.
431 * @dev: device that requests this phy
432 * @string: the phy name as given in the dt data or the name of the controller
433 * port for non-dt case
435 * Returns the phy driver, after getting a refcount to it; or
436 * -ENODEV if there is no such phy. The caller is responsible for
437 * calling phy_put() to release that count.
439 struct phy
*phy_get(struct device
*dev
, const char *string
)
444 if (string
== NULL
) {
445 dev_WARN(dev
, "missing string\n");
446 return ERR_PTR(-EINVAL
);
450 index
= of_property_match_string(dev
->of_node
, "phy-names",
452 phy
= _of_phy_get(dev
->of_node
, index
);
454 phy
= phy_lookup(dev
, string
);
459 if (!try_module_get(phy
->ops
->owner
))
460 return ERR_PTR(-EPROBE_DEFER
);
462 get_device(&phy
->dev
);
466 EXPORT_SYMBOL_GPL(phy_get
);
469 * phy_optional_get() - lookup and obtain a reference to an optional phy.
470 * @dev: device that requests this phy
471 * @string: the phy name as given in the dt data or the name of the controller
472 * port for non-dt case
474 * Returns the phy driver, after getting a refcount to it; or
475 * NULL if there is no such phy. The caller is responsible for
476 * calling phy_put() to release that count.
478 struct phy
*phy_optional_get(struct device
*dev
, const char *string
)
480 struct phy
*phy
= phy_get(dev
, string
);
482 if (PTR_ERR(phy
) == -ENODEV
)
487 EXPORT_SYMBOL_GPL(phy_optional_get
);
490 * devm_phy_get() - lookup and obtain a reference to a phy.
491 * @dev: device that requests this phy
492 * @string: the phy name as given in the dt data or phy device name
495 * Gets the phy using phy_get(), and associates a device with it using
496 * devres. On driver detach, release function is invoked on the devres data,
497 * then, devres data is freed.
499 struct phy
*devm_phy_get(struct device
*dev
, const char *string
)
501 struct phy
**ptr
, *phy
;
503 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
505 return ERR_PTR(-ENOMEM
);
507 phy
= phy_get(dev
, string
);
510 devres_add(dev
, ptr
);
517 EXPORT_SYMBOL_GPL(devm_phy_get
);
520 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
521 * @dev: device that requests this phy
522 * @string: the phy name as given in the dt data or phy device name
525 * Gets the phy using phy_get(), and associates a device with it using
526 * devres. On driver detach, release function is invoked on the devres
527 * data, then, devres data is freed. This differs to devm_phy_get() in
528 * that if the phy does not exist, it is not considered an error and
529 * -ENODEV will not be returned. Instead the NULL phy is returned,
530 * which can be passed to all other phy consumer calls.
532 struct phy
*devm_phy_optional_get(struct device
*dev
, const char *string
)
534 struct phy
*phy
= devm_phy_get(dev
, string
);
536 if (PTR_ERR(phy
) == -ENODEV
)
541 EXPORT_SYMBOL_GPL(devm_phy_optional_get
);
544 * devm_of_phy_get() - lookup and obtain a reference to a phy.
545 * @dev: device that requests this phy
546 * @np: node containing the phy
547 * @con_id: name of the phy from device's point of view
549 * Gets the phy using of_phy_get(), and associates a device with it using
550 * devres. On driver detach, release function is invoked on the devres data,
551 * then, devres data is freed.
553 struct phy
*devm_of_phy_get(struct device
*dev
, struct device_node
*np
,
556 struct phy
**ptr
, *phy
;
558 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
560 return ERR_PTR(-ENOMEM
);
562 phy
= of_phy_get(np
, con_id
);
565 devres_add(dev
, ptr
);
572 EXPORT_SYMBOL_GPL(devm_of_phy_get
);
575 * phy_create() - create a new phy
576 * @dev: device that is creating the new phy
577 * @ops: function pointers for performing phy operations
578 * @init_data: contains the list of PHY consumers or NULL
580 * Called to create a phy using phy framework.
582 struct phy
*phy_create(struct device
*dev
, const struct phy_ops
*ops
,
583 struct phy_init_data
*init_data
)
590 return ERR_PTR(-EINVAL
);
592 phy
= kzalloc(sizeof(*phy
), GFP_KERNEL
);
594 return ERR_PTR(-ENOMEM
);
596 id
= ida_simple_get(&phy_ida
, 0, 0, GFP_KERNEL
);
598 dev_err(dev
, "unable to get id\n");
604 phy
->pwr
= regulator_get_optional(dev
, "phy");
605 if (IS_ERR(phy
->pwr
)) {
606 if (PTR_ERR(phy
->pwr
) == -EPROBE_DEFER
) {
613 device_initialize(&phy
->dev
);
614 mutex_init(&phy
->mutex
);
616 phy
->dev
.class = phy_class
;
617 phy
->dev
.parent
= dev
;
618 phy
->dev
.of_node
= dev
->of_node
;
621 phy
->init_data
= init_data
;
623 ret
= dev_set_name(&phy
->dev
, "phy-%s.%d", dev_name(dev
), id
);
627 ret
= device_add(&phy
->dev
);
631 if (pm_runtime_enabled(dev
)) {
632 pm_runtime_enable(&phy
->dev
);
633 pm_runtime_no_callbacks(&phy
->dev
);
639 put_device(&phy
->dev
); /* calls phy_release() which frees resources */
643 ida_simple_remove(&phy_ida
, phy
->id
);
649 EXPORT_SYMBOL_GPL(phy_create
);
652 * devm_phy_create() - create a new phy
653 * @dev: device that is creating the new phy
654 * @ops: function pointers for performing phy operations
655 * @init_data: contains the list of PHY consumers or NULL
657 * Creates a new PHY device adding it to the PHY class.
658 * While at that, it also associates the device with the phy using devres.
659 * On driver detach, release function is invoked on the devres data,
660 * then, devres data is freed.
662 struct phy
*devm_phy_create(struct device
*dev
, const struct phy_ops
*ops
,
663 struct phy_init_data
*init_data
)
665 struct phy
**ptr
, *phy
;
667 ptr
= devres_alloc(devm_phy_consume
, sizeof(*ptr
), GFP_KERNEL
);
669 return ERR_PTR(-ENOMEM
);
671 phy
= phy_create(dev
, ops
, init_data
);
674 devres_add(dev
, ptr
);
681 EXPORT_SYMBOL_GPL(devm_phy_create
);
684 * phy_destroy() - destroy the phy
685 * @phy: the phy to be destroyed
687 * Called to destroy the phy.
689 void phy_destroy(struct phy
*phy
)
691 pm_runtime_disable(&phy
->dev
);
692 device_unregister(&phy
->dev
);
694 EXPORT_SYMBOL_GPL(phy_destroy
);
697 * devm_phy_destroy() - destroy the PHY
698 * @dev: device that wants to release this phy
699 * @phy: the phy returned by devm_phy_get()
701 * destroys the devres associated with this phy and invokes phy_destroy
702 * to destroy the phy.
704 void devm_phy_destroy(struct device
*dev
, struct phy
*phy
)
708 r
= devres_destroy(dev
, devm_phy_consume
, devm_phy_match
, phy
);
709 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
711 EXPORT_SYMBOL_GPL(devm_phy_destroy
);
714 * __of_phy_provider_register() - create/register phy provider with the framework
715 * @dev: struct device of the phy provider
716 * @owner: the module owner containing of_xlate
717 * @of_xlate: function pointer to obtain phy instance from phy provider
719 * Creates struct phy_provider from dev and of_xlate function pointer.
720 * This is used in the case of dt boot for finding the phy instance from
723 struct phy_provider
*__of_phy_provider_register(struct device
*dev
,
724 struct module
*owner
, struct phy
* (*of_xlate
)(struct device
*dev
,
725 struct of_phandle_args
*args
))
727 struct phy_provider
*phy_provider
;
729 phy_provider
= kzalloc(sizeof(*phy_provider
), GFP_KERNEL
);
731 return ERR_PTR(-ENOMEM
);
733 phy_provider
->dev
= dev
;
734 phy_provider
->owner
= owner
;
735 phy_provider
->of_xlate
= of_xlate
;
737 mutex_lock(&phy_provider_mutex
);
738 list_add_tail(&phy_provider
->list
, &phy_provider_list
);
739 mutex_unlock(&phy_provider_mutex
);
743 EXPORT_SYMBOL_GPL(__of_phy_provider_register
);
746 * __devm_of_phy_provider_register() - create/register phy provider with the
748 * @dev: struct device of the phy provider
749 * @owner: the module owner containing of_xlate
750 * @of_xlate: function pointer to obtain phy instance from phy provider
752 * Creates struct phy_provider from dev and of_xlate function pointer.
753 * This is used in the case of dt boot for finding the phy instance from
754 * phy provider. While at that, it also associates the device with the
755 * phy provider using devres. On driver detach, release function is invoked
756 * on the devres data, then, devres data is freed.
758 struct phy_provider
*__devm_of_phy_provider_register(struct device
*dev
,
759 struct module
*owner
, struct phy
* (*of_xlate
)(struct device
*dev
,
760 struct of_phandle_args
*args
))
762 struct phy_provider
**ptr
, *phy_provider
;
764 ptr
= devres_alloc(devm_phy_provider_release
, sizeof(*ptr
), GFP_KERNEL
);
766 return ERR_PTR(-ENOMEM
);
768 phy_provider
= __of_phy_provider_register(dev
, owner
, of_xlate
);
769 if (!IS_ERR(phy_provider
)) {
771 devres_add(dev
, ptr
);
778 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register
);
781 * of_phy_provider_unregister() - unregister phy provider from the framework
782 * @phy_provider: phy provider returned by of_phy_provider_register()
784 * Removes the phy_provider created using of_phy_provider_register().
786 void of_phy_provider_unregister(struct phy_provider
*phy_provider
)
788 if (IS_ERR(phy_provider
))
791 mutex_lock(&phy_provider_mutex
);
792 list_del(&phy_provider
->list
);
794 mutex_unlock(&phy_provider_mutex
);
796 EXPORT_SYMBOL_GPL(of_phy_provider_unregister
);
799 * devm_of_phy_provider_unregister() - remove phy provider from the framework
800 * @dev: struct device of the phy provider
802 * destroys the devres associated with this phy provider and invokes
803 * of_phy_provider_unregister to unregister the phy provider.
805 void devm_of_phy_provider_unregister(struct device
*dev
,
806 struct phy_provider
*phy_provider
) {
809 r
= devres_destroy(dev
, devm_phy_provider_release
, devm_phy_match
,
811 dev_WARN_ONCE(dev
, r
, "couldn't find PHY provider device resource\n");
813 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister
);
816 * phy_release() - release the phy
817 * @dev: the dev member within phy
819 * When the last reference to the device is removed, it is called
820 * from the embedded kobject as release method.
822 static void phy_release(struct device
*dev
)
827 dev_vdbg(dev
, "releasing '%s'\n", dev_name(dev
));
828 regulator_put(phy
->pwr
);
829 ida_simple_remove(&phy_ida
, phy
->id
);
833 static int __init
phy_core_init(void)
835 phy_class
= class_create(THIS_MODULE
, "phy");
836 if (IS_ERR(phy_class
)) {
837 pr_err("failed to create phy class --> %ld\n",
839 return PTR_ERR(phy_class
);
842 phy_class
->dev_release
= phy_release
;
846 module_init(phy_core_init
);
848 static void __exit
phy_core_exit(void)
850 class_destroy(phy_class
);
852 module_exit(phy_core_exit
);
854 MODULE_DESCRIPTION("Generic PHY Framework");
855 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
856 MODULE_LICENSE("GPL v2");