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 LIST_HEAD(phys
);
30 static DEFINE_IDA(phy_ida
);
32 static void devm_phy_release(struct device
*dev
, void *res
)
34 struct phy
*phy
= *(struct phy
**)res
;
39 static void devm_phy_provider_release(struct device
*dev
, void *res
)
41 struct phy_provider
*phy_provider
= *(struct phy_provider
**)res
;
43 of_phy_provider_unregister(phy_provider
);
46 static void devm_phy_consume(struct device
*dev
, void *res
)
48 struct phy
*phy
= *(struct phy
**)res
;
53 static int devm_phy_match(struct device
*dev
, void *res
, void *match_data
)
55 return res
== match_data
;
59 * phy_create_lookup() - allocate and register PHY/device association
60 * @phy: the phy of the association
61 * @con_id: connection ID string on device
62 * @dev_id: the device of the association
64 * Creates and registers phy_lookup entry.
66 int phy_create_lookup(struct phy
*phy
, const char *con_id
, const char *dev_id
)
68 struct phy_lookup
*pl
;
70 if (!phy
|| !dev_id
|| !con_id
)
73 pl
= kzalloc(sizeof(*pl
), GFP_KERNEL
);
81 mutex_lock(&phy_provider_mutex
);
82 list_add_tail(&pl
->node
, &phys
);
83 mutex_unlock(&phy_provider_mutex
);
87 EXPORT_SYMBOL_GPL(phy_create_lookup
);
90 * phy_remove_lookup() - find and remove PHY/device association
91 * @phy: the phy of the association
92 * @con_id: connection ID string on device
93 * @dev_id: the device of the association
95 * Finds and unregisters phy_lookup entry that was created with
96 * phy_create_lookup().
98 void phy_remove_lookup(struct phy
*phy
, const char *con_id
, const char *dev_id
)
100 struct phy_lookup
*pl
;
102 if (!phy
|| !dev_id
|| !con_id
)
105 mutex_lock(&phy_provider_mutex
);
106 list_for_each_entry(pl
, &phys
, node
)
107 if (pl
->phy
== phy
&& !strcmp(pl
->dev_id
, dev_id
) &&
108 !strcmp(pl
->con_id
, con_id
)) {
113 mutex_unlock(&phy_provider_mutex
);
115 EXPORT_SYMBOL_GPL(phy_remove_lookup
);
117 static struct phy
*phy_find(struct device
*dev
, const char *con_id
)
119 const char *dev_id
= dev_name(dev
);
120 struct phy_lookup
*p
, *pl
= NULL
;
122 mutex_lock(&phy_provider_mutex
);
123 list_for_each_entry(p
, &phys
, node
)
124 if (!strcmp(p
->dev_id
, dev_id
) && !strcmp(p
->con_id
, con_id
)) {
128 mutex_unlock(&phy_provider_mutex
);
130 return pl
? pl
->phy
: ERR_PTR(-ENODEV
);
133 static struct phy_provider
*of_phy_provider_lookup(struct device_node
*node
)
135 struct phy_provider
*phy_provider
;
136 struct device_node
*child
;
138 list_for_each_entry(phy_provider
, &phy_provider_list
, list
) {
139 if (phy_provider
->dev
->of_node
== node
)
142 for_each_child_of_node(phy_provider
->dev
->of_node
, child
)
147 return ERR_PTR(-EPROBE_DEFER
);
150 int phy_pm_runtime_get(struct phy
*phy
)
154 if (!pm_runtime_enabled(&phy
->dev
))
157 ret
= pm_runtime_get(&phy
->dev
);
158 if (ret
< 0 && ret
!= -EINPROGRESS
)
159 pm_runtime_put_noidle(&phy
->dev
);
163 EXPORT_SYMBOL_GPL(phy_pm_runtime_get
);
165 int phy_pm_runtime_get_sync(struct phy
*phy
)
169 if (!pm_runtime_enabled(&phy
->dev
))
172 ret
= pm_runtime_get_sync(&phy
->dev
);
174 pm_runtime_put_sync(&phy
->dev
);
178 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync
);
180 int phy_pm_runtime_put(struct phy
*phy
)
182 if (!pm_runtime_enabled(&phy
->dev
))
185 return pm_runtime_put(&phy
->dev
);
187 EXPORT_SYMBOL_GPL(phy_pm_runtime_put
);
189 int phy_pm_runtime_put_sync(struct phy
*phy
)
191 if (!pm_runtime_enabled(&phy
->dev
))
194 return pm_runtime_put_sync(&phy
->dev
);
196 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync
);
198 void phy_pm_runtime_allow(struct phy
*phy
)
200 if (!pm_runtime_enabled(&phy
->dev
))
203 pm_runtime_allow(&phy
->dev
);
205 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow
);
207 void phy_pm_runtime_forbid(struct phy
*phy
)
209 if (!pm_runtime_enabled(&phy
->dev
))
212 pm_runtime_forbid(&phy
->dev
);
214 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid
);
216 int phy_init(struct phy
*phy
)
223 ret
= phy_pm_runtime_get_sync(phy
);
224 if (ret
< 0 && ret
!= -ENOTSUPP
)
227 mutex_lock(&phy
->mutex
);
228 if (phy
->init_count
== 0 && phy
->ops
->init
) {
229 ret
= phy
->ops
->init(phy
);
231 dev_err(&phy
->dev
, "phy init failed --> %d\n", ret
);
235 ret
= 0; /* Override possible ret == -ENOTSUPP */
240 mutex_unlock(&phy
->mutex
);
241 phy_pm_runtime_put(phy
);
244 EXPORT_SYMBOL_GPL(phy_init
);
246 int phy_exit(struct phy
*phy
)
253 ret
= phy_pm_runtime_get_sync(phy
);
254 if (ret
< 0 && ret
!= -ENOTSUPP
)
257 mutex_lock(&phy
->mutex
);
258 if (phy
->init_count
== 1 && phy
->ops
->exit
) {
259 ret
= phy
->ops
->exit(phy
);
261 dev_err(&phy
->dev
, "phy exit failed --> %d\n", ret
);
268 mutex_unlock(&phy
->mutex
);
269 phy_pm_runtime_put(phy
);
272 EXPORT_SYMBOL_GPL(phy_exit
);
274 int phy_power_on(struct phy
*phy
)
282 ret
= regulator_enable(phy
->pwr
);
287 ret
= phy_pm_runtime_get_sync(phy
);
288 if (ret
< 0 && ret
!= -ENOTSUPP
)
291 mutex_lock(&phy
->mutex
);
292 if (phy
->power_count
== 0 && phy
->ops
->power_on
) {
293 ret
= phy
->ops
->power_on(phy
);
295 dev_err(&phy
->dev
, "phy poweron failed --> %d\n", ret
);
299 ret
= 0; /* Override possible ret == -ENOTSUPP */
302 mutex_unlock(&phy
->mutex
);
306 mutex_unlock(&phy
->mutex
);
307 phy_pm_runtime_put_sync(phy
);
309 regulator_disable(phy
->pwr
);
313 EXPORT_SYMBOL_GPL(phy_power_on
);
315 int phy_power_off(struct phy
*phy
)
322 mutex_lock(&phy
->mutex
);
323 if (phy
->power_count
== 1 && phy
->ops
->power_off
) {
324 ret
= phy
->ops
->power_off(phy
);
326 dev_err(&phy
->dev
, "phy poweroff failed --> %d\n", ret
);
327 mutex_unlock(&phy
->mutex
);
332 mutex_unlock(&phy
->mutex
);
333 phy_pm_runtime_put(phy
);
336 regulator_disable(phy
->pwr
);
340 EXPORT_SYMBOL_GPL(phy_power_off
);
343 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
344 * @np: device_node for which to get the phy
345 * @index: the index of the phy
347 * Returns the phy associated with the given phandle value,
348 * after getting a refcount to it or -ENODEV if there is no such phy or
349 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
350 * not yet loaded. This function uses of_xlate call back function provided
351 * while registering the phy_provider to find the phy instance.
353 static struct phy
*_of_phy_get(struct device_node
*np
, int index
)
356 struct phy_provider
*phy_provider
;
357 struct phy
*phy
= NULL
;
358 struct of_phandle_args args
;
360 ret
= of_parse_phandle_with_args(np
, "phys", "#phy-cells",
363 return ERR_PTR(-ENODEV
);
365 mutex_lock(&phy_provider_mutex
);
366 phy_provider
= of_phy_provider_lookup(args
.np
);
367 if (IS_ERR(phy_provider
) || !try_module_get(phy_provider
->owner
)) {
368 phy
= ERR_PTR(-EPROBE_DEFER
);
372 phy
= phy_provider
->of_xlate(phy_provider
->dev
, &args
);
373 module_put(phy_provider
->owner
);
376 mutex_unlock(&phy_provider_mutex
);
377 of_node_put(args
.np
);
383 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
384 * @np: device_node for which to get the phy
385 * @con_id: name of the phy from device's point of view
387 * Returns the phy driver, after getting a refcount to it; or
388 * -ENODEV if there is no such phy. The caller is responsible for
389 * calling phy_put() to release that count.
391 struct phy
*of_phy_get(struct device_node
*np
, const char *con_id
)
393 struct phy
*phy
= NULL
;
397 index
= of_property_match_string(np
, "phy-names", con_id
);
399 phy
= _of_phy_get(np
, index
);
403 if (!try_module_get(phy
->ops
->owner
))
404 return ERR_PTR(-EPROBE_DEFER
);
406 get_device(&phy
->dev
);
410 EXPORT_SYMBOL_GPL(of_phy_get
);
413 * phy_put() - release the PHY
414 * @phy: the phy returned by phy_get()
416 * Releases a refcount the caller received from phy_get().
418 void phy_put(struct phy
*phy
)
420 if (!phy
|| IS_ERR(phy
))
423 module_put(phy
->ops
->owner
);
424 put_device(&phy
->dev
);
426 EXPORT_SYMBOL_GPL(phy_put
);
429 * devm_phy_put() - release the PHY
430 * @dev: device that wants to release this phy
431 * @phy: the phy returned by devm_phy_get()
433 * destroys the devres associated with this phy and invokes phy_put
434 * to release the phy.
436 void devm_phy_put(struct device
*dev
, struct phy
*phy
)
443 r
= devres_destroy(dev
, devm_phy_release
, devm_phy_match
, phy
);
444 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
446 EXPORT_SYMBOL_GPL(devm_phy_put
);
449 * of_phy_simple_xlate() - returns the phy instance from phy provider
450 * @dev: the PHY provider device
451 * @args: of_phandle_args (not used here)
453 * Intended to be used by phy provider for the common case where #phy-cells is
454 * 0. For other cases where #phy-cells is greater than '0', the phy provider
455 * should provide a custom of_xlate function that reads the *args* and returns
456 * the appropriate phy.
458 struct phy
*of_phy_simple_xlate(struct device
*dev
, struct of_phandle_args
462 struct class_dev_iter iter
;
464 class_dev_iter_init(&iter
, phy_class
, NULL
, NULL
);
465 while ((dev
= class_dev_iter_next(&iter
))) {
467 if (args
->np
!= phy
->dev
.of_node
)
470 class_dev_iter_exit(&iter
);
474 class_dev_iter_exit(&iter
);
475 return ERR_PTR(-ENODEV
);
477 EXPORT_SYMBOL_GPL(of_phy_simple_xlate
);
480 * phy_get() - lookup and obtain a reference to a phy.
481 * @dev: device that requests this phy
482 * @string: the phy name as given in the dt data or the name of the controller
483 * port for non-dt case
485 * Returns the phy driver, after getting a refcount to it; or
486 * -ENODEV if there is no such phy. The caller is responsible for
487 * calling phy_put() to release that count.
489 struct phy
*phy_get(struct device
*dev
, const char *string
)
494 if (string
== NULL
) {
495 dev_WARN(dev
, "missing string\n");
496 return ERR_PTR(-EINVAL
);
500 index
= of_property_match_string(dev
->of_node
, "phy-names",
502 phy
= _of_phy_get(dev
->of_node
, index
);
504 phy
= phy_find(dev
, string
);
509 if (!try_module_get(phy
->ops
->owner
))
510 return ERR_PTR(-EPROBE_DEFER
);
512 get_device(&phy
->dev
);
516 EXPORT_SYMBOL_GPL(phy_get
);
519 * phy_optional_get() - lookup and obtain a reference to an optional phy.
520 * @dev: device that requests this phy
521 * @string: the phy name as given in the dt data or the name of the controller
522 * port for non-dt case
524 * Returns the phy driver, after getting a refcount to it; or
525 * NULL if there is no such phy. The caller is responsible for
526 * calling phy_put() to release that count.
528 struct phy
*phy_optional_get(struct device
*dev
, const char *string
)
530 struct phy
*phy
= phy_get(dev
, string
);
532 if (PTR_ERR(phy
) == -ENODEV
)
537 EXPORT_SYMBOL_GPL(phy_optional_get
);
540 * devm_phy_get() - lookup and obtain a reference to a phy.
541 * @dev: device that requests this phy
542 * @string: the phy name as given in the dt data or phy device name
545 * Gets the phy using phy_get(), and associates a device with it using
546 * devres. On driver detach, release function is invoked on the devres data,
547 * then, devres data is freed.
549 struct phy
*devm_phy_get(struct device
*dev
, const char *string
)
551 struct phy
**ptr
, *phy
;
553 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
555 return ERR_PTR(-ENOMEM
);
557 phy
= phy_get(dev
, string
);
560 devres_add(dev
, ptr
);
567 EXPORT_SYMBOL_GPL(devm_phy_get
);
570 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
571 * @dev: device that requests this phy
572 * @string: the phy name as given in the dt data or phy device name
575 * Gets the phy using phy_get(), and associates a device with it using
576 * devres. On driver detach, release function is invoked on the devres
577 * data, then, devres data is freed. This differs to devm_phy_get() in
578 * that if the phy does not exist, it is not considered an error and
579 * -ENODEV will not be returned. Instead the NULL phy is returned,
580 * which can be passed to all other phy consumer calls.
582 struct phy
*devm_phy_optional_get(struct device
*dev
, const char *string
)
584 struct phy
*phy
= devm_phy_get(dev
, string
);
586 if (PTR_ERR(phy
) == -ENODEV
)
591 EXPORT_SYMBOL_GPL(devm_phy_optional_get
);
594 * devm_of_phy_get() - lookup and obtain a reference to a phy.
595 * @dev: device that requests this phy
596 * @np: node containing the phy
597 * @con_id: name of the phy from device's point of view
599 * Gets the phy using of_phy_get(), and associates a device with it using
600 * devres. On driver detach, release function is invoked on the devres data,
601 * then, devres data is freed.
603 struct phy
*devm_of_phy_get(struct device
*dev
, struct device_node
*np
,
606 struct phy
**ptr
, *phy
;
608 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
610 return ERR_PTR(-ENOMEM
);
612 phy
= of_phy_get(np
, con_id
);
615 devres_add(dev
, ptr
);
622 EXPORT_SYMBOL_GPL(devm_of_phy_get
);
625 * phy_create() - create a new phy
626 * @dev: device that is creating the new phy
627 * @node: device node of the phy
628 * @ops: function pointers for performing phy operations
630 * Called to create a phy using phy framework.
632 struct phy
*phy_create(struct device
*dev
, struct device_node
*node
,
633 const struct phy_ops
*ops
)
640 return ERR_PTR(-EINVAL
);
642 phy
= kzalloc(sizeof(*phy
), GFP_KERNEL
);
644 return ERR_PTR(-ENOMEM
);
646 id
= ida_simple_get(&phy_ida
, 0, 0, GFP_KERNEL
);
648 dev_err(dev
, "unable to get id\n");
654 phy
->pwr
= regulator_get_optional(dev
, "phy");
655 if (IS_ERR(phy
->pwr
)) {
656 if (PTR_ERR(phy
->pwr
) == -EPROBE_DEFER
) {
663 device_initialize(&phy
->dev
);
664 mutex_init(&phy
->mutex
);
666 phy
->dev
.class = phy_class
;
667 phy
->dev
.parent
= dev
;
668 phy
->dev
.of_node
= node
?: dev
->of_node
;
672 ret
= dev_set_name(&phy
->dev
, "phy-%s.%d", dev_name(dev
), id
);
676 ret
= device_add(&phy
->dev
);
680 if (pm_runtime_enabled(dev
)) {
681 pm_runtime_enable(&phy
->dev
);
682 pm_runtime_no_callbacks(&phy
->dev
);
688 put_device(&phy
->dev
); /* calls phy_release() which frees resources */
692 ida_simple_remove(&phy_ida
, phy
->id
);
698 EXPORT_SYMBOL_GPL(phy_create
);
701 * devm_phy_create() - create a new phy
702 * @dev: device that is creating the new phy
703 * @node: device node of the phy
704 * @ops: function pointers for performing phy operations
706 * Creates a new PHY device adding it to the PHY class.
707 * While at that, it also associates the device with the phy using devres.
708 * On driver detach, release function is invoked on the devres data,
709 * then, devres data is freed.
711 struct phy
*devm_phy_create(struct device
*dev
, struct device_node
*node
,
712 const struct phy_ops
*ops
)
714 struct phy
**ptr
, *phy
;
716 ptr
= devres_alloc(devm_phy_consume
, sizeof(*ptr
), GFP_KERNEL
);
718 return ERR_PTR(-ENOMEM
);
720 phy
= phy_create(dev
, node
, ops
);
723 devres_add(dev
, ptr
);
730 EXPORT_SYMBOL_GPL(devm_phy_create
);
733 * phy_destroy() - destroy the phy
734 * @phy: the phy to be destroyed
736 * Called to destroy the phy.
738 void phy_destroy(struct phy
*phy
)
740 pm_runtime_disable(&phy
->dev
);
741 device_unregister(&phy
->dev
);
743 EXPORT_SYMBOL_GPL(phy_destroy
);
746 * devm_phy_destroy() - destroy the PHY
747 * @dev: device that wants to release this phy
748 * @phy: the phy returned by devm_phy_get()
750 * destroys the devres associated with this phy and invokes phy_destroy
751 * to destroy the phy.
753 void devm_phy_destroy(struct device
*dev
, struct phy
*phy
)
757 r
= devres_destroy(dev
, devm_phy_consume
, devm_phy_match
, phy
);
758 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
760 EXPORT_SYMBOL_GPL(devm_phy_destroy
);
763 * __of_phy_provider_register() - create/register phy provider with the framework
764 * @dev: struct device of the phy provider
765 * @owner: the module owner containing of_xlate
766 * @of_xlate: function pointer to obtain phy instance from phy provider
768 * Creates struct phy_provider from dev and of_xlate function pointer.
769 * This is used in the case of dt boot for finding the phy instance from
772 struct phy_provider
*__of_phy_provider_register(struct device
*dev
,
773 struct module
*owner
, struct phy
* (*of_xlate
)(struct device
*dev
,
774 struct of_phandle_args
*args
))
776 struct phy_provider
*phy_provider
;
778 phy_provider
= kzalloc(sizeof(*phy_provider
), GFP_KERNEL
);
780 return ERR_PTR(-ENOMEM
);
782 phy_provider
->dev
= dev
;
783 phy_provider
->owner
= owner
;
784 phy_provider
->of_xlate
= of_xlate
;
786 mutex_lock(&phy_provider_mutex
);
787 list_add_tail(&phy_provider
->list
, &phy_provider_list
);
788 mutex_unlock(&phy_provider_mutex
);
792 EXPORT_SYMBOL_GPL(__of_phy_provider_register
);
795 * __devm_of_phy_provider_register() - create/register phy provider with the
797 * @dev: struct device of the phy provider
798 * @owner: the module owner containing of_xlate
799 * @of_xlate: function pointer to obtain phy instance from phy provider
801 * Creates struct phy_provider from dev and of_xlate function pointer.
802 * This is used in the case of dt boot for finding the phy instance from
803 * phy provider. While at that, it also associates the device with the
804 * phy provider using devres. On driver detach, release function is invoked
805 * on the devres data, then, devres data is freed.
807 struct phy_provider
*__devm_of_phy_provider_register(struct device
*dev
,
808 struct module
*owner
, struct phy
* (*of_xlate
)(struct device
*dev
,
809 struct of_phandle_args
*args
))
811 struct phy_provider
**ptr
, *phy_provider
;
813 ptr
= devres_alloc(devm_phy_provider_release
, sizeof(*ptr
), GFP_KERNEL
);
815 return ERR_PTR(-ENOMEM
);
817 phy_provider
= __of_phy_provider_register(dev
, owner
, of_xlate
);
818 if (!IS_ERR(phy_provider
)) {
820 devres_add(dev
, ptr
);
827 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register
);
830 * of_phy_provider_unregister() - unregister phy provider from the framework
831 * @phy_provider: phy provider returned by of_phy_provider_register()
833 * Removes the phy_provider created using of_phy_provider_register().
835 void of_phy_provider_unregister(struct phy_provider
*phy_provider
)
837 if (IS_ERR(phy_provider
))
840 mutex_lock(&phy_provider_mutex
);
841 list_del(&phy_provider
->list
);
843 mutex_unlock(&phy_provider_mutex
);
845 EXPORT_SYMBOL_GPL(of_phy_provider_unregister
);
848 * devm_of_phy_provider_unregister() - remove phy provider from the framework
849 * @dev: struct device of the phy provider
851 * destroys the devres associated with this phy provider and invokes
852 * of_phy_provider_unregister to unregister the phy provider.
854 void devm_of_phy_provider_unregister(struct device
*dev
,
855 struct phy_provider
*phy_provider
) {
858 r
= devres_destroy(dev
, devm_phy_provider_release
, devm_phy_match
,
860 dev_WARN_ONCE(dev
, r
, "couldn't find PHY provider device resource\n");
862 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister
);
865 * phy_release() - release the phy
866 * @dev: the dev member within phy
868 * When the last reference to the device is removed, it is called
869 * from the embedded kobject as release method.
871 static void phy_release(struct device
*dev
)
876 dev_vdbg(dev
, "releasing '%s'\n", dev_name(dev
));
877 regulator_put(phy
->pwr
);
878 ida_simple_remove(&phy_ida
, phy
->id
);
882 static int __init
phy_core_init(void)
884 phy_class
= class_create(THIS_MODULE
, "phy");
885 if (IS_ERR(phy_class
)) {
886 pr_err("failed to create phy class --> %ld\n",
888 return PTR_ERR(phy_class
);
891 phy_class
->dev_release
= phy_release
;
895 module_init(phy_core_init
);
897 static void __exit
phy_core_exit(void)
899 class_destroy(phy_class
);
901 module_exit(phy_core_exit
);
903 MODULE_DESCRIPTION("Generic PHY Framework");
904 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
905 MODULE_LICENSE("GPL v2");