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;
90 struct device_node *child;
92 list_for_each_entry(phy_provider, &phy_provider_list, list) {
93 if (phy_provider->dev->of_node == node)
96 for_each_child_of_node(phy_provider->dev->of_node, child)
101 return ERR_PTR(-EPROBE_DEFER);
104 int phy_pm_runtime_get(struct phy *phy)
108 if (!pm_runtime_enabled(&phy->dev))
111 ret = pm_runtime_get(&phy->dev);
112 if (ret < 0 && ret != -EINPROGRESS)
113 pm_runtime_put_noidle(&phy->dev);
117 EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
119 int phy_pm_runtime_get_sync(struct phy *phy)
123 if (!pm_runtime_enabled(&phy->dev))
126 ret = pm_runtime_get_sync(&phy->dev);
128 pm_runtime_put_sync(&phy->dev);
132 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
134 int phy_pm_runtime_put(struct phy *phy)
136 if (!pm_runtime_enabled(&phy->dev))
139 return pm_runtime_put(&phy->dev);
141 EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
143 int phy_pm_runtime_put_sync(struct phy *phy)
145 if (!pm_runtime_enabled(&phy->dev))
148 return pm_runtime_put_sync(&phy->dev);
150 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
152 void phy_pm_runtime_allow(struct phy *phy)
154 if (!pm_runtime_enabled(&phy->dev))
157 pm_runtime_allow(&phy->dev);
159 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
161 void phy_pm_runtime_forbid(struct phy *phy)
163 if (!pm_runtime_enabled(&phy->dev))
166 pm_runtime_forbid(&phy->dev);
168 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
170 int phy_init(struct phy *phy)
177 ret = phy_pm_runtime_get_sync(phy);
178 if (ret < 0 && ret != -ENOTSUPP)
181 mutex_lock(&phy->mutex);
182 if (phy->init_count == 0 && phy->ops->init) {
183 ret = phy->ops->init(phy);
185 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
189 ret = 0; /* Override possible ret == -ENOTSUPP */
194 mutex_unlock(&phy->mutex);
195 phy_pm_runtime_put(phy);
198 EXPORT_SYMBOL_GPL(phy_init);
200 int phy_exit(struct phy *phy)
207 ret = phy_pm_runtime_get_sync(phy);
208 if (ret < 0 && ret != -ENOTSUPP)
211 mutex_lock(&phy->mutex);
212 if (phy->init_count == 1 && phy->ops->exit) {
213 ret = phy->ops->exit(phy);
215 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
222 mutex_unlock(&phy->mutex);
223 phy_pm_runtime_put(phy);
226 EXPORT_SYMBOL_GPL(phy_exit);
228 int phy_power_on(struct phy *phy)
236 ret = regulator_enable(phy->pwr);
241 ret = phy_pm_runtime_get_sync(phy);
242 if (ret < 0 && ret != -ENOTSUPP)
245 mutex_lock(&phy->mutex);
246 if (phy->power_count == 0 && phy->ops->power_on) {
247 ret = phy->ops->power_on(phy);
249 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
253 ret = 0; /* Override possible ret == -ENOTSUPP */
256 mutex_unlock(&phy->mutex);
260 mutex_unlock(&phy->mutex);
261 phy_pm_runtime_put_sync(phy);
263 regulator_disable(phy->pwr);
267 EXPORT_SYMBOL_GPL(phy_power_on);
269 int phy_power_off(struct phy *phy)
276 mutex_lock(&phy->mutex);
277 if (phy->power_count == 1 && phy->ops->power_off) {
278 ret = phy->ops->power_off(phy);
280 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
281 mutex_unlock(&phy->mutex);
286 mutex_unlock(&phy->mutex);
287 phy_pm_runtime_put(phy);
290 regulator_disable(phy->pwr);
294 EXPORT_SYMBOL_GPL(phy_power_off);
297 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
298 * @np: device_node for which to get the phy
299 * @index: the index of the phy
301 * Returns the phy associated with the given phandle value,
302 * after getting a refcount to it or -ENODEV if there is no such phy or
303 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
304 * not yet loaded. This function uses of_xlate call back function provided
305 * while registering the phy_provider to find the phy instance.
307 static struct phy *_of_phy_get(struct device_node *np, int index)
310 struct phy_provider *phy_provider;
311 struct phy *phy = NULL;
312 struct of_phandle_args args;
314 ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
317 return ERR_PTR(-ENODEV);
319 mutex_lock(&phy_provider_mutex);
320 phy_provider = of_phy_provider_lookup(args.np);
321 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
322 phy = ERR_PTR(-EPROBE_DEFER);
326 phy = phy_provider->of_xlate(phy_provider->dev, &args);
327 module_put(phy_provider->owner);
330 mutex_unlock(&phy_provider_mutex);
331 of_node_put(args.np);
337 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
338 * @np: device_node for which to get the phy
339 * @con_id: name of the phy from device's point of view
341 * Returns the phy driver, after getting a refcount to it; or
342 * -ENODEV if there is no such phy. The caller is responsible for
343 * calling phy_put() to release that count.
345 struct phy *of_phy_get(struct device_node *np, const char *con_id)
347 struct phy *phy = NULL;
351 index = of_property_match_string(np, "phy-names", con_id);
353 phy = _of_phy_get(np, index);
357 if (!try_module_get(phy->ops->owner))
358 return ERR_PTR(-EPROBE_DEFER);
360 get_device(&phy->dev);
364 EXPORT_SYMBOL_GPL(of_phy_get);
367 * phy_put() - release the PHY
368 * @phy: the phy returned by phy_get()
370 * Releases a refcount the caller received from phy_get().
372 void phy_put(struct phy *phy)
374 if (!phy || IS_ERR(phy))
377 module_put(phy->ops->owner);
378 put_device(&phy->dev);
380 EXPORT_SYMBOL_GPL(phy_put);
383 * devm_phy_put() - release the PHY
384 * @dev: device that wants to release this phy
385 * @phy: the phy returned by devm_phy_get()
387 * destroys the devres associated with this phy and invokes phy_put
388 * to release the phy.
390 void devm_phy_put(struct device *dev, struct phy *phy)
397 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
398 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
400 EXPORT_SYMBOL_GPL(devm_phy_put);
403 * of_phy_simple_xlate() - returns the phy instance from phy provider
404 * @dev: the PHY provider device
405 * @args: of_phandle_args (not used here)
407 * Intended to be used by phy provider for the common case where #phy-cells is
408 * 0. For other cases where #phy-cells is greater than '0', the phy provider
409 * should provide a custom of_xlate function that reads the *args* and returns
410 * the appropriate phy.
412 struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
416 struct class_dev_iter iter;
418 class_dev_iter_init(&iter, phy_class, NULL, NULL);
419 while ((dev = class_dev_iter_next(&iter))) {
421 if (args->np != phy->dev.of_node)
424 class_dev_iter_exit(&iter);
428 class_dev_iter_exit(&iter);
429 return ERR_PTR(-ENODEV);
431 EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
434 * phy_get() - lookup and obtain a reference to a phy.
435 * @dev: device that requests this phy
436 * @string: the phy name as given in the dt data or the name of the controller
437 * port for non-dt case
439 * Returns the phy driver, after getting a refcount to it; or
440 * -ENODEV if there is no such phy. The caller is responsible for
441 * calling phy_put() to release that count.
443 struct phy *phy_get(struct device *dev, const char *string)
448 if (string == NULL) {
449 dev_WARN(dev, "missing string\n");
450 return ERR_PTR(-EINVAL);
454 index = of_property_match_string(dev->of_node, "phy-names",
456 phy = _of_phy_get(dev->of_node, index);
458 phy = phy_lookup(dev, string);
463 if (!try_module_get(phy->ops->owner))
464 return ERR_PTR(-EPROBE_DEFER);
466 get_device(&phy->dev);
470 EXPORT_SYMBOL_GPL(phy_get);
473 * phy_optional_get() - lookup and obtain a reference to an optional phy.
474 * @dev: device that requests this phy
475 * @string: the phy name as given in the dt data or the name of the controller
476 * port for non-dt case
478 * Returns the phy driver, after getting a refcount to it; or
479 * NULL if there is no such phy. The caller is responsible for
480 * calling phy_put() to release that count.
482 struct phy *phy_optional_get(struct device *dev, const char *string)
484 struct phy *phy = phy_get(dev, string);
486 if (PTR_ERR(phy) == -ENODEV)
491 EXPORT_SYMBOL_GPL(phy_optional_get);
494 * devm_phy_get() - lookup and obtain a reference to a phy.
495 * @dev: device that requests this phy
496 * @string: the phy name as given in the dt data or phy device name
499 * Gets the phy using phy_get(), and associates a device with it using
500 * devres. On driver detach, release function is invoked on the devres data,
501 * then, devres data is freed.
503 struct phy *devm_phy_get(struct device *dev, const char *string)
505 struct phy **ptr, *phy;
507 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
509 return ERR_PTR(-ENOMEM);
511 phy = phy_get(dev, string);
514 devres_add(dev, ptr);
521 EXPORT_SYMBOL_GPL(devm_phy_get);
524 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
525 * @dev: device that requests this phy
526 * @string: the phy name as given in the dt data or phy device name
529 * Gets the phy using phy_get(), and associates a device with it using
530 * devres. On driver detach, release function is invoked on the devres
531 * data, then, devres data is freed. This differs to devm_phy_get() in
532 * that if the phy does not exist, it is not considered an error and
533 * -ENODEV will not be returned. Instead the NULL phy is returned,
534 * which can be passed to all other phy consumer calls.
536 struct phy *devm_phy_optional_get(struct device *dev, const char *string)
538 struct phy *phy = devm_phy_get(dev, string);
540 if (PTR_ERR(phy) == -ENODEV)
545 EXPORT_SYMBOL_GPL(devm_phy_optional_get);
548 * devm_of_phy_get() - lookup and obtain a reference to a phy.
549 * @dev: device that requests this phy
550 * @np: node containing the phy
551 * @con_id: name of the phy from device's point of view
553 * Gets the phy using of_phy_get(), and associates a device with it using
554 * devres. On driver detach, release function is invoked on the devres data,
555 * then, devres data is freed.
557 struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
560 struct phy **ptr, *phy;
562 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
564 return ERR_PTR(-ENOMEM);
566 phy = of_phy_get(np, con_id);
569 devres_add(dev, ptr);
576 EXPORT_SYMBOL_GPL(devm_of_phy_get);
579 * phy_create() - create a new phy
580 * @dev: device that is creating the new phy
581 * @node: device node of the phy
582 * @ops: function pointers for performing phy operations
583 * @init_data: contains the list of PHY consumers or NULL
585 * Called to create a phy using phy framework.
587 struct phy *phy_create(struct device *dev, struct device_node *node,
588 const struct phy_ops *ops,
589 struct phy_init_data *init_data)
596 return ERR_PTR(-EINVAL);
598 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
600 return ERR_PTR(-ENOMEM);
602 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
604 dev_err(dev, "unable to get id\n");
610 phy->pwr = regulator_get_optional(dev, "phy");
611 if (IS_ERR(phy->pwr)) {
612 if (PTR_ERR(phy->pwr) == -EPROBE_DEFER) {
619 device_initialize(&phy->dev);
620 mutex_init(&phy->mutex);
622 phy->dev.class = phy_class;
623 phy->dev.parent = dev;
624 phy->dev.of_node = node ?: dev->of_node;
627 phy->init_data = init_data;
629 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
633 ret = device_add(&phy->dev);
637 if (pm_runtime_enabled(dev)) {
638 pm_runtime_enable(&phy->dev);
639 pm_runtime_no_callbacks(&phy->dev);
645 put_device(&phy->dev); /* calls phy_release() which frees resources */
649 ida_simple_remove(&phy_ida, phy->id);
655 EXPORT_SYMBOL_GPL(phy_create);
658 * devm_phy_create() - create a new phy
659 * @dev: device that is creating the new phy
660 * @node: device node of the phy
661 * @ops: function pointers for performing phy operations
662 * @init_data: contains the list of PHY consumers or NULL
664 * Creates a new PHY device adding it to the PHY class.
665 * While at that, it also associates the device with the phy using devres.
666 * On driver detach, release function is invoked on the devres data,
667 * then, devres data is freed.
669 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
670 const struct phy_ops *ops,
671 struct phy_init_data *init_data)
673 struct phy **ptr, *phy;
675 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
677 return ERR_PTR(-ENOMEM);
679 phy = phy_create(dev, node, ops, init_data);
682 devres_add(dev, ptr);
689 EXPORT_SYMBOL_GPL(devm_phy_create);
692 * phy_destroy() - destroy the phy
693 * @phy: the phy to be destroyed
695 * Called to destroy the phy.
697 void phy_destroy(struct phy *phy)
699 pm_runtime_disable(&phy->dev);
700 device_unregister(&phy->dev);
702 EXPORT_SYMBOL_GPL(phy_destroy);
705 * devm_phy_destroy() - destroy the PHY
706 * @dev: device that wants to release this phy
707 * @phy: the phy returned by devm_phy_get()
709 * destroys the devres associated with this phy and invokes phy_destroy
710 * to destroy the phy.
712 void devm_phy_destroy(struct device *dev, struct phy *phy)
716 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
717 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
719 EXPORT_SYMBOL_GPL(devm_phy_destroy);
722 * __of_phy_provider_register() - create/register phy provider with the framework
723 * @dev: struct device of the phy provider
724 * @owner: the module owner containing of_xlate
725 * @of_xlate: function pointer to obtain phy instance from phy provider
727 * Creates struct phy_provider from dev and of_xlate function pointer.
728 * This is used in the case of dt boot for finding the phy instance from
731 struct phy_provider *__of_phy_provider_register(struct device *dev,
732 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
733 struct of_phandle_args *args))
735 struct phy_provider *phy_provider;
737 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
739 return ERR_PTR(-ENOMEM);
741 phy_provider->dev = dev;
742 phy_provider->owner = owner;
743 phy_provider->of_xlate = of_xlate;
745 mutex_lock(&phy_provider_mutex);
746 list_add_tail(&phy_provider->list, &phy_provider_list);
747 mutex_unlock(&phy_provider_mutex);
751 EXPORT_SYMBOL_GPL(__of_phy_provider_register);
754 * __devm_of_phy_provider_register() - create/register phy provider with the
756 * @dev: struct device of the phy provider
757 * @owner: the module owner containing of_xlate
758 * @of_xlate: function pointer to obtain phy instance from phy provider
760 * Creates struct phy_provider from dev and of_xlate function pointer.
761 * This is used in the case of dt boot for finding the phy instance from
762 * phy provider. While at that, it also associates the device with the
763 * phy provider using devres. On driver detach, release function is invoked
764 * on the devres data, then, devres data is freed.
766 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
767 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
768 struct of_phandle_args *args))
770 struct phy_provider **ptr, *phy_provider;
772 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
774 return ERR_PTR(-ENOMEM);
776 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
777 if (!IS_ERR(phy_provider)) {
779 devres_add(dev, ptr);
786 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
789 * of_phy_provider_unregister() - unregister phy provider from the framework
790 * @phy_provider: phy provider returned by of_phy_provider_register()
792 * Removes the phy_provider created using of_phy_provider_register().
794 void of_phy_provider_unregister(struct phy_provider *phy_provider)
796 if (IS_ERR(phy_provider))
799 mutex_lock(&phy_provider_mutex);
800 list_del(&phy_provider->list);
802 mutex_unlock(&phy_provider_mutex);
804 EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
807 * devm_of_phy_provider_unregister() - remove phy provider from the framework
808 * @dev: struct device of the phy provider
810 * destroys the devres associated with this phy provider and invokes
811 * of_phy_provider_unregister to unregister the phy provider.
813 void devm_of_phy_provider_unregister(struct device *dev,
814 struct phy_provider *phy_provider) {
817 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
819 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
821 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
824 * phy_release() - release the phy
825 * @dev: the dev member within phy
827 * When the last reference to the device is removed, it is called
828 * from the embedded kobject as release method.
830 static void phy_release(struct device *dev)
835 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
836 regulator_put(phy->pwr);
837 ida_simple_remove(&phy_ida, phy->id);
841 static int __init phy_core_init(void)
843 phy_class = class_create(THIS_MODULE, "phy");
844 if (IS_ERR(phy_class)) {
845 pr_err("failed to create phy class --> %ld\n",
847 return PTR_ERR(phy_class);
850 phy_class->dev_release = phy_release;
854 module_init(phy_core_init);
856 static void __exit phy_core_exit(void)
858 class_destroy(phy_class);
860 module_exit(phy_core_exit);
862 MODULE_DESCRIPTION("Generic PHY Framework");
863 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
864 MODULE_LICENSE("GPL v2");