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;
58 static struct phy *phy_lookup(struct device *device, const char *port)
63 struct phy_consumer *consumers;
64 struct class_dev_iter iter;
66 class_dev_iter_init(&iter, phy_class, NULL, NULL);
67 while ((dev = class_dev_iter_next(&iter))) {
72 count = phy->init_data->num_consumers;
73 consumers = phy->init_data->consumers;
75 if (!strcmp(consumers->dev_name, dev_name(device)) &&
76 !strcmp(consumers->port, port)) {
77 class_dev_iter_exit(&iter);
84 class_dev_iter_exit(&iter);
85 return ERR_PTR(-ENODEV);
89 * phy_create_lookup() - allocate and register PHY/device association
90 * @phy: the phy of the association
91 * @con_id: connection ID string on device
92 * @dev_id: the device of the association
94 * Creates and registers phy_lookup entry.
96 int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
98 struct phy_lookup *pl;
100 if (!phy || !dev_id || !con_id)
103 pl = kzalloc(sizeof(*pl), GFP_KERNEL);
111 mutex_lock(&phy_provider_mutex);
112 list_add_tail(&pl->node, &phys);
113 mutex_unlock(&phy_provider_mutex);
117 EXPORT_SYMBOL_GPL(phy_create_lookup);
120 * phy_remove_lookup() - find and remove PHY/device association
121 * @phy: the phy of the association
122 * @con_id: connection ID string on device
123 * @dev_id: the device of the association
125 * Finds and unregisters phy_lookup entry that was created with
126 * phy_create_lookup().
128 void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
130 struct phy_lookup *pl;
132 if (!phy || !dev_id || !con_id)
135 mutex_lock(&phy_provider_mutex);
136 list_for_each_entry(pl, &phys, node)
137 if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
138 !strcmp(pl->con_id, con_id)) {
143 mutex_unlock(&phy_provider_mutex);
145 EXPORT_SYMBOL_GPL(phy_remove_lookup);
147 static struct phy *phy_find(struct device *dev, const char *con_id)
149 const char *dev_id = dev_name(dev);
150 struct phy_lookup *p, *pl = NULL;
153 mutex_lock(&phy_provider_mutex);
154 list_for_each_entry(p, &phys, node)
155 if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
159 mutex_unlock(&phy_provider_mutex);
161 phy = pl ? pl->phy : ERR_PTR(-ENODEV);
163 /* fall-back to the old lookup method for now */
165 phy = phy_lookup(dev, con_id);
169 static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
171 struct phy_provider *phy_provider;
172 struct device_node *child;
174 list_for_each_entry(phy_provider, &phy_provider_list, list) {
175 if (phy_provider->dev->of_node == node)
178 for_each_child_of_node(phy_provider->dev->of_node, child)
183 return ERR_PTR(-EPROBE_DEFER);
186 int phy_pm_runtime_get(struct phy *phy)
190 if (!pm_runtime_enabled(&phy->dev))
193 ret = pm_runtime_get(&phy->dev);
194 if (ret < 0 && ret != -EINPROGRESS)
195 pm_runtime_put_noidle(&phy->dev);
199 EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
201 int phy_pm_runtime_get_sync(struct phy *phy)
205 if (!pm_runtime_enabled(&phy->dev))
208 ret = pm_runtime_get_sync(&phy->dev);
210 pm_runtime_put_sync(&phy->dev);
214 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
216 int phy_pm_runtime_put(struct phy *phy)
218 if (!pm_runtime_enabled(&phy->dev))
221 return pm_runtime_put(&phy->dev);
223 EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
225 int phy_pm_runtime_put_sync(struct phy *phy)
227 if (!pm_runtime_enabled(&phy->dev))
230 return pm_runtime_put_sync(&phy->dev);
232 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
234 void phy_pm_runtime_allow(struct phy *phy)
236 if (!pm_runtime_enabled(&phy->dev))
239 pm_runtime_allow(&phy->dev);
241 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
243 void phy_pm_runtime_forbid(struct phy *phy)
245 if (!pm_runtime_enabled(&phy->dev))
248 pm_runtime_forbid(&phy->dev);
250 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
252 int phy_init(struct phy *phy)
259 ret = phy_pm_runtime_get_sync(phy);
260 if (ret < 0 && ret != -ENOTSUPP)
263 mutex_lock(&phy->mutex);
264 if (phy->init_count == 0 && phy->ops->init) {
265 ret = phy->ops->init(phy);
267 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
271 ret = 0; /* Override possible ret == -ENOTSUPP */
276 mutex_unlock(&phy->mutex);
277 phy_pm_runtime_put(phy);
280 EXPORT_SYMBOL_GPL(phy_init);
282 int phy_exit(struct phy *phy)
289 ret = phy_pm_runtime_get_sync(phy);
290 if (ret < 0 && ret != -ENOTSUPP)
293 mutex_lock(&phy->mutex);
294 if (phy->init_count == 1 && phy->ops->exit) {
295 ret = phy->ops->exit(phy);
297 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
304 mutex_unlock(&phy->mutex);
305 phy_pm_runtime_put(phy);
308 EXPORT_SYMBOL_GPL(phy_exit);
310 int phy_power_on(struct phy *phy)
318 ret = regulator_enable(phy->pwr);
323 ret = phy_pm_runtime_get_sync(phy);
324 if (ret < 0 && ret != -ENOTSUPP)
327 mutex_lock(&phy->mutex);
328 if (phy->power_count == 0 && phy->ops->power_on) {
329 ret = phy->ops->power_on(phy);
331 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
335 ret = 0; /* Override possible ret == -ENOTSUPP */
338 mutex_unlock(&phy->mutex);
342 mutex_unlock(&phy->mutex);
343 phy_pm_runtime_put_sync(phy);
345 regulator_disable(phy->pwr);
349 EXPORT_SYMBOL_GPL(phy_power_on);
351 int phy_power_off(struct phy *phy)
358 mutex_lock(&phy->mutex);
359 if (phy->power_count == 1 && phy->ops->power_off) {
360 ret = phy->ops->power_off(phy);
362 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
363 mutex_unlock(&phy->mutex);
368 mutex_unlock(&phy->mutex);
369 phy_pm_runtime_put(phy);
372 regulator_disable(phy->pwr);
376 EXPORT_SYMBOL_GPL(phy_power_off);
379 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
380 * @np: device_node for which to get the phy
381 * @index: the index of the phy
383 * Returns the phy associated with the given phandle value,
384 * after getting a refcount to it or -ENODEV if there is no such phy or
385 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
386 * not yet loaded. This function uses of_xlate call back function provided
387 * while registering the phy_provider to find the phy instance.
389 static struct phy *_of_phy_get(struct device_node *np, int index)
392 struct phy_provider *phy_provider;
393 struct phy *phy = NULL;
394 struct of_phandle_args args;
396 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
399 return ERR_PTR(-ENODEV);
401 mutex_lock(&phy_provider_mutex);
402 phy_provider = of_phy_provider_lookup(args.np);
403 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
404 phy = ERR_PTR(-EPROBE_DEFER);
408 phy = phy_provider->of_xlate(phy_provider->dev, &args);
409 module_put(phy_provider->owner);
412 mutex_unlock(&phy_provider_mutex);
413 of_node_put(args.np);
419 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
420 * @np: device_node for which to get the phy
421 * @con_id: name of the phy from device's point of view
423 * Returns the phy driver, after getting a refcount to it; or
424 * -ENODEV if there is no such phy. The caller is responsible for
425 * calling phy_put() to release that count.
427 struct phy *of_phy_get(struct device_node *np, const char *con_id)
429 struct phy *phy = NULL;
433 index = of_property_match_string(np, "phy-names", con_id);
435 phy = _of_phy_get(np, index);
439 if (!try_module_get(phy->ops->owner))
440 return ERR_PTR(-EPROBE_DEFER);
442 get_device(&phy->dev);
446 EXPORT_SYMBOL_GPL(of_phy_get);
449 * phy_put() - release the PHY
450 * @phy: the phy returned by phy_get()
452 * Releases a refcount the caller received from phy_get().
454 void phy_put(struct phy *phy)
456 if (!phy || IS_ERR(phy))
459 module_put(phy->ops->owner);
460 put_device(&phy->dev);
462 EXPORT_SYMBOL_GPL(phy_put);
465 * devm_phy_put() - release the PHY
466 * @dev: device that wants to release this phy
467 * @phy: the phy returned by devm_phy_get()
469 * destroys the devres associated with this phy and invokes phy_put
470 * to release the phy.
472 void devm_phy_put(struct device *dev, struct phy *phy)
479 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
480 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
482 EXPORT_SYMBOL_GPL(devm_phy_put);
485 * of_phy_simple_xlate() - returns the phy instance from phy provider
486 * @dev: the PHY provider device
487 * @args: of_phandle_args (not used here)
489 * Intended to be used by phy provider for the common case where #phy-cells is
490 * 0. For other cases where #phy-cells is greater than '0', the phy provider
491 * should provide a custom of_xlate function that reads the *args* and returns
492 * the appropriate phy.
494 struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
498 struct class_dev_iter iter;
500 class_dev_iter_init(&iter, phy_class, NULL, NULL);
501 while ((dev = class_dev_iter_next(&iter))) {
503 if (args->np != phy->dev.of_node)
506 class_dev_iter_exit(&iter);
510 class_dev_iter_exit(&iter);
511 return ERR_PTR(-ENODEV);
513 EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
516 * phy_get() - lookup and obtain a reference to a phy.
517 * @dev: device that requests this phy
518 * @string: the phy name as given in the dt data or the name of the controller
519 * port for non-dt case
521 * Returns the phy driver, after getting a refcount to it; or
522 * -ENODEV if there is no such phy. The caller is responsible for
523 * calling phy_put() to release that count.
525 struct phy *phy_get(struct device *dev, const char *string)
530 if (string == NULL) {
531 dev_WARN(dev, "missing string\n");
532 return ERR_PTR(-EINVAL);
536 index = of_property_match_string(dev->of_node, "phy-names",
538 phy = _of_phy_get(dev->of_node, index);
540 phy = phy_find(dev, string);
545 if (!try_module_get(phy->ops->owner))
546 return ERR_PTR(-EPROBE_DEFER);
548 get_device(&phy->dev);
552 EXPORT_SYMBOL_GPL(phy_get);
555 * phy_optional_get() - lookup and obtain a reference to an optional phy.
556 * @dev: device that requests this phy
557 * @string: the phy name as given in the dt data or the name of the controller
558 * port for non-dt case
560 * Returns the phy driver, after getting a refcount to it; or
561 * NULL if there is no such phy. The caller is responsible for
562 * calling phy_put() to release that count.
564 struct phy *phy_optional_get(struct device *dev, const char *string)
566 struct phy *phy = phy_get(dev, string);
568 if (PTR_ERR(phy) == -ENODEV)
573 EXPORT_SYMBOL_GPL(phy_optional_get);
576 * devm_phy_get() - lookup and obtain a reference to a phy.
577 * @dev: device that requests this phy
578 * @string: the phy name as given in the dt data or phy device name
581 * Gets the phy using phy_get(), and associates a device with it using
582 * devres. On driver detach, release function is invoked on the devres data,
583 * then, devres data is freed.
585 struct phy *devm_phy_get(struct device *dev, const char *string)
587 struct phy **ptr, *phy;
589 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
591 return ERR_PTR(-ENOMEM);
593 phy = phy_get(dev, string);
596 devres_add(dev, ptr);
603 EXPORT_SYMBOL_GPL(devm_phy_get);
606 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
607 * @dev: device that requests this phy
608 * @string: the phy name as given in the dt data or phy device name
611 * Gets the phy using phy_get(), and associates a device with it using
612 * devres. On driver detach, release function is invoked on the devres
613 * data, then, devres data is freed. This differs to devm_phy_get() in
614 * that if the phy does not exist, it is not considered an error and
615 * -ENODEV will not be returned. Instead the NULL phy is returned,
616 * which can be passed to all other phy consumer calls.
618 struct phy *devm_phy_optional_get(struct device *dev, const char *string)
620 struct phy *phy = devm_phy_get(dev, string);
622 if (PTR_ERR(phy) == -ENODEV)
627 EXPORT_SYMBOL_GPL(devm_phy_optional_get);
630 * devm_of_phy_get() - lookup and obtain a reference to a phy.
631 * @dev: device that requests this phy
632 * @np: node containing the phy
633 * @con_id: name of the phy from device's point of view
635 * Gets the phy using of_phy_get(), and associates a device with it using
636 * devres. On driver detach, release function is invoked on the devres data,
637 * then, devres data is freed.
639 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
642 struct phy **ptr, *phy;
644 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
646 return ERR_PTR(-ENOMEM);
648 phy = of_phy_get(np, con_id);
651 devres_add(dev, ptr);
658 EXPORT_SYMBOL_GPL(devm_of_phy_get);
661 * phy_create() - create a new phy
662 * @dev: device that is creating the new phy
663 * @node: device node of the phy
664 * @ops: function pointers for performing phy operations
665 * @init_data: contains the list of PHY consumers or NULL
667 * Called to create a phy using phy framework.
669 struct phy *phy_create(struct device *dev, struct device_node *node,
670 const struct phy_ops *ops,
671 struct phy_init_data *init_data)
678 return ERR_PTR(-EINVAL);
680 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
682 return ERR_PTR(-ENOMEM);
684 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
686 dev_err(dev, "unable to get id\n");
692 phy->pwr = regulator_get_optional(dev, "phy");
693 if (IS_ERR(phy->pwr)) {
694 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
701 device_initialize(&phy->dev);
702 mutex_init(&phy->mutex);
704 phy->dev.class = phy_class;
705 phy->dev.parent = dev;
706 phy->dev.of_node = node ?: dev->of_node;
709 phy->init_data = init_data;
711 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
715 ret = device_add(&phy->dev);
719 if (pm_runtime_enabled(dev)) {
720 pm_runtime_enable(&phy->dev);
721 pm_runtime_no_callbacks(&phy->dev);
727 put_device(&phy->dev); /* calls phy_release() which frees resources */
731 ida_simple_remove(&phy_ida, phy->id);
737 EXPORT_SYMBOL_GPL(phy_create);
740 * devm_phy_create() - create a new phy
741 * @dev: device that is creating the new phy
742 * @node: device node of the phy
743 * @ops: function pointers for performing phy operations
744 * @init_data: contains the list of PHY consumers or NULL
746 * Creates a new PHY device adding it to the PHY class.
747 * While at that, it also associates the device with the phy using devres.
748 * On driver detach, release function is invoked on the devres data,
749 * then, devres data is freed.
751 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
752 const struct phy_ops *ops,
753 struct phy_init_data *init_data)
755 struct phy **ptr, *phy;
757 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
759 return ERR_PTR(-ENOMEM);
761 phy = phy_create(dev, node, ops, init_data);
764 devres_add(dev, ptr);
771 EXPORT_SYMBOL_GPL(devm_phy_create);
774 * phy_destroy() - destroy the phy
775 * @phy: the phy to be destroyed
777 * Called to destroy the phy.
779 void phy_destroy(struct phy *phy)
781 pm_runtime_disable(&phy->dev);
782 device_unregister(&phy->dev);
784 EXPORT_SYMBOL_GPL(phy_destroy);
787 * devm_phy_destroy() - destroy the PHY
788 * @dev: device that wants to release this phy
789 * @phy: the phy returned by devm_phy_get()
791 * destroys the devres associated with this phy and invokes phy_destroy
792 * to destroy the phy.
794 void devm_phy_destroy(struct device *dev, struct phy *phy)
798 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
799 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
801 EXPORT_SYMBOL_GPL(devm_phy_destroy);
804 * __of_phy_provider_register() - create/register phy provider with the framework
805 * @dev: struct device of the phy provider
806 * @owner: the module owner containing of_xlate
807 * @of_xlate: function pointer to obtain phy instance from phy provider
809 * Creates struct phy_provider from dev and of_xlate function pointer.
810 * This is used in the case of dt boot for finding the phy instance from
813 struct phy_provider *__of_phy_provider_register(struct device *dev,
814 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
815 struct of_phandle_args *args))
817 struct phy_provider *phy_provider;
819 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
821 return ERR_PTR(-ENOMEM);
823 phy_provider->dev = dev;
824 phy_provider->owner = owner;
825 phy_provider->of_xlate = of_xlate;
827 mutex_lock(&phy_provider_mutex);
828 list_add_tail(&phy_provider->list, &phy_provider_list);
829 mutex_unlock(&phy_provider_mutex);
833 EXPORT_SYMBOL_GPL(__of_phy_provider_register);
836 * __devm_of_phy_provider_register() - create/register phy provider with the
838 * @dev: struct device of the phy provider
839 * @owner: the module owner containing of_xlate
840 * @of_xlate: function pointer to obtain phy instance from phy provider
842 * Creates struct phy_provider from dev and of_xlate function pointer.
843 * This is used in the case of dt boot for finding the phy instance from
844 * phy provider. While at that, it also associates the device with the
845 * phy provider using devres. On driver detach, release function is invoked
846 * on the devres data, then, devres data is freed.
848 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
849 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
850 struct of_phandle_args *args))
852 struct phy_provider **ptr, *phy_provider;
854 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
856 return ERR_PTR(-ENOMEM);
858 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
859 if (!IS_ERR(phy_provider)) {
861 devres_add(dev, ptr);
868 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
871 * of_phy_provider_unregister() - unregister phy provider from the framework
872 * @phy_provider: phy provider returned by of_phy_provider_register()
874 * Removes the phy_provider created using of_phy_provider_register().
876 void of_phy_provider_unregister(struct phy_provider *phy_provider)
878 if (IS_ERR(phy_provider))
881 mutex_lock(&phy_provider_mutex);
882 list_del(&phy_provider->list);
884 mutex_unlock(&phy_provider_mutex);
886 EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
889 * devm_of_phy_provider_unregister() - remove phy provider from the framework
890 * @dev: struct device of the phy provider
892 * destroys the devres associated with this phy provider and invokes
893 * of_phy_provider_unregister to unregister the phy provider.
895 void devm_of_phy_provider_unregister(struct device *dev,
896 struct phy_provider *phy_provider) {
899 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
901 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
903 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
906 * phy_release() - release the phy
907 * @dev: the dev member within phy
909 * When the last reference to the device is removed, it is called
910 * from the embedded kobject as release method.
912 static void phy_release(struct device *dev)
917 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
918 regulator_put(phy->pwr);
919 ida_simple_remove(&phy_ida, phy->id);
923 static int __init phy_core_init(void)
925 phy_class = class_create(THIS_MODULE, "phy");
926 if (IS_ERR(phy_class)) {
927 pr_err("failed to create phy class --> %ld\n",
929 return PTR_ERR(phy_class);
932 phy_class->dev_release = phy_release;
936 module_init(phy_core_init);
938 static void __exit phy_core_exit(void)
940 class_destroy(phy_class);
942 module_exit(phy_core_exit);
944 MODULE_DESCRIPTION("Generic PHY Framework");
945 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
946 MODULE_LICENSE("GPL v2");