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>
25 static struct class *phy_class;
26 static DEFINE_MUTEX(phy_provider_mutex);
27 static LIST_HEAD(phy_provider_list);
28 static DEFINE_IDA(phy_ida);
30 static void devm_phy_release(struct device *dev, void *res)
32 struct phy *phy = *(struct phy **)res;
37 static void devm_phy_provider_release(struct device *dev, void *res)
39 struct phy_provider *phy_provider = *(struct phy_provider **)res;
41 of_phy_provider_unregister(phy_provider);
44 static void devm_phy_consume(struct device *dev, void *res)
46 struct phy *phy = *(struct phy **)res;
51 static int devm_phy_match(struct device *dev, void *res, void *match_data)
53 return res == match_data;
56 static struct phy *phy_lookup(struct device *device, const char *port)
61 struct phy_consumer *consumers;
62 struct class_dev_iter iter;
64 class_dev_iter_init(&iter, phy_class, NULL, NULL);
65 while ((dev = class_dev_iter_next(&iter))) {
70 count = phy->init_data->num_consumers;
71 consumers = phy->init_data->consumers;
73 if (!strcmp(consumers->dev_name, dev_name(device)) &&
74 !strcmp(consumers->port, port)) {
75 class_dev_iter_exit(&iter);
82 class_dev_iter_exit(&iter);
83 return ERR_PTR(-ENODEV);
86 static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
88 struct phy_provider *phy_provider;
89 struct device_node *child;
91 list_for_each_entry(phy_provider, &phy_provider_list, list) {
92 if (phy_provider->dev->of_node == node)
95 for_each_child_of_node(phy_provider->dev->of_node, child)
100 return ERR_PTR(-EPROBE_DEFER);
103 int phy_pm_runtime_get(struct phy *phy)
107 if (!pm_runtime_enabled(&phy->dev))
110 ret = pm_runtime_get(&phy->dev);
111 if (ret < 0 && ret != -EINPROGRESS)
112 pm_runtime_put_noidle(&phy->dev);
116 EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
118 int phy_pm_runtime_get_sync(struct phy *phy)
122 if (!pm_runtime_enabled(&phy->dev))
125 ret = pm_runtime_get_sync(&phy->dev);
127 pm_runtime_put_sync(&phy->dev);
131 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
133 int phy_pm_runtime_put(struct phy *phy)
135 if (!pm_runtime_enabled(&phy->dev))
138 return pm_runtime_put(&phy->dev);
140 EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
142 int phy_pm_runtime_put_sync(struct phy *phy)
144 if (!pm_runtime_enabled(&phy->dev))
147 return pm_runtime_put_sync(&phy->dev);
149 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
151 void phy_pm_runtime_allow(struct phy *phy)
153 if (!pm_runtime_enabled(&phy->dev))
156 pm_runtime_allow(&phy->dev);
158 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
160 void phy_pm_runtime_forbid(struct phy *phy)
162 if (!pm_runtime_enabled(&phy->dev))
165 pm_runtime_forbid(&phy->dev);
167 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
169 int phy_init(struct phy *phy)
176 ret = phy_pm_runtime_get_sync(phy);
177 if (ret < 0 && ret != -ENOTSUPP)
180 mutex_lock(&phy->mutex);
181 if (phy->init_count == 0 && phy->ops->init) {
182 ret = phy->ops->init(phy);
184 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
188 ret = 0; /* Override possible ret == -ENOTSUPP */
193 mutex_unlock(&phy->mutex);
194 phy_pm_runtime_put(phy);
197 EXPORT_SYMBOL_GPL(phy_init);
199 int phy_exit(struct phy *phy)
206 ret = phy_pm_runtime_get_sync(phy);
207 if (ret < 0 && ret != -ENOTSUPP)
210 mutex_lock(&phy->mutex);
211 if (phy->init_count == 1 && phy->ops->exit) {
212 ret = phy->ops->exit(phy);
214 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
221 mutex_unlock(&phy->mutex);
222 phy_pm_runtime_put(phy);
225 EXPORT_SYMBOL_GPL(phy_exit);
227 int phy_power_on(struct phy *phy)
234 ret = phy_pm_runtime_get_sync(phy);
235 if (ret < 0 && ret != -ENOTSUPP)
238 mutex_lock(&phy->mutex);
239 if (phy->power_count == 0 && phy->ops->power_on) {
240 ret = phy->ops->power_on(phy);
242 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
246 ret = 0; /* Override possible ret == -ENOTSUPP */
249 mutex_unlock(&phy->mutex);
253 mutex_unlock(&phy->mutex);
254 phy_pm_runtime_put_sync(phy);
258 EXPORT_SYMBOL_GPL(phy_power_on);
260 int phy_power_off(struct phy *phy)
267 mutex_lock(&phy->mutex);
268 if (phy->power_count == 1 && phy->ops->power_off) {
269 ret = phy->ops->power_off(phy);
271 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
272 mutex_unlock(&phy->mutex);
277 mutex_unlock(&phy->mutex);
278 phy_pm_runtime_put(phy);
282 EXPORT_SYMBOL_GPL(phy_power_off);
285 * of_phy_get() - lookup and obtain a reference to a phy by phandle
286 * @dev: device that requests this phy
287 * @index: the index of the phy
289 * Returns the phy associated with the given phandle value,
290 * after getting a refcount to it or -ENODEV if there is no such phy or
291 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
292 * not yet loaded. This function uses of_xlate call back function provided
293 * while registering the phy_provider to find the phy instance.
295 static struct phy *of_phy_get(struct device *dev, int index)
298 struct phy_provider *phy_provider;
299 struct phy *phy = NULL;
300 struct of_phandle_args args;
302 ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
305 dev_dbg(dev, "failed to get phy in %s node\n",
306 dev->of_node->full_name);
307 return ERR_PTR(-ENODEV);
310 mutex_lock(&phy_provider_mutex);
311 phy_provider = of_phy_provider_lookup(args.np);
312 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
313 phy = ERR_PTR(-EPROBE_DEFER);
317 phy = phy_provider->of_xlate(phy_provider->dev, &args);
318 module_put(phy_provider->owner);
321 mutex_unlock(&phy_provider_mutex);
322 of_node_put(args.np);
328 * phy_put() - release the PHY
329 * @phy: the phy returned by phy_get()
331 * Releases a refcount the caller received from phy_get().
333 void phy_put(struct phy *phy)
335 if (!phy || IS_ERR(phy))
338 module_put(phy->ops->owner);
339 put_device(&phy->dev);
341 EXPORT_SYMBOL_GPL(phy_put);
344 * devm_phy_put() - release the PHY
345 * @dev: device that wants to release this phy
346 * @phy: the phy returned by devm_phy_get()
348 * destroys the devres associated with this phy and invokes phy_put
349 * to release the phy.
351 void devm_phy_put(struct device *dev, struct phy *phy)
358 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
359 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
361 EXPORT_SYMBOL_GPL(devm_phy_put);
364 * of_phy_simple_xlate() - returns the phy instance from phy provider
365 * @dev: the PHY provider device
366 * @args: of_phandle_args (not used here)
368 * Intended to be used by phy provider for the common case where #phy-cells is
369 * 0. For other cases where #phy-cells is greater than '0', the phy provider
370 * should provide a custom of_xlate function that reads the *args* and returns
371 * the appropriate phy.
373 struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
377 struct class_dev_iter iter;
378 struct device_node *node = dev->of_node;
379 struct device_node *child;
381 class_dev_iter_init(&iter, phy_class, NULL, NULL);
382 while ((dev = class_dev_iter_next(&iter))) {
384 if (node != phy->dev.of_node) {
385 for_each_child_of_node(node, child) {
386 if (child == phy->dev.of_node)
393 class_dev_iter_exit(&iter);
397 class_dev_iter_exit(&iter);
398 return ERR_PTR(-ENODEV);
400 EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
403 * phy_get() - lookup and obtain a reference to a phy.
404 * @dev: device that requests this phy
405 * @string: the phy name as given in the dt data or the name of the controller
406 * port for non-dt case
408 * Returns the phy driver, after getting a refcount to it; or
409 * -ENODEV if there is no such phy. The caller is responsible for
410 * calling phy_put() to release that count.
412 struct phy *phy_get(struct device *dev, const char *string)
417 if (string == NULL) {
418 dev_WARN(dev, "missing string\n");
419 return ERR_PTR(-EINVAL);
423 index = of_property_match_string(dev->of_node, "phy-names",
425 phy = of_phy_get(dev, index);
427 phy = phy_lookup(dev, string);
432 if (!try_module_get(phy->ops->owner))
433 return ERR_PTR(-EPROBE_DEFER);
435 get_device(&phy->dev);
439 EXPORT_SYMBOL_GPL(phy_get);
442 * phy_optional_get() - lookup and obtain a reference to an optional phy.
443 * @dev: device that requests this phy
444 * @string: the phy name as given in the dt data or the name of the controller
445 * port for non-dt case
447 * Returns the phy driver, after getting a refcount to it; or
448 * NULL if there is no such phy. The caller is responsible for
449 * calling phy_put() to release that count.
451 struct phy *phy_optional_get(struct device *dev, const char *string)
453 struct phy *phy = phy_get(dev, string);
455 if (PTR_ERR(phy) == -ENODEV)
460 EXPORT_SYMBOL_GPL(phy_optional_get);
463 * devm_phy_get() - lookup and obtain a reference to a phy.
464 * @dev: device that requests this phy
465 * @string: the phy name as given in the dt data or phy device name
468 * Gets the phy using phy_get(), and associates a device with it using
469 * devres. On driver detach, release function is invoked on the devres data,
470 * then, devres data is freed.
472 struct phy *devm_phy_get(struct device *dev, const char *string)
474 struct phy **ptr, *phy;
476 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
478 return ERR_PTR(-ENOMEM);
480 phy = phy_get(dev, string);
483 devres_add(dev, ptr);
490 EXPORT_SYMBOL_GPL(devm_phy_get);
493 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
494 * @dev: device that requests this phy
495 * @string: the phy name as given in the dt data or phy device name
498 * Gets the phy using phy_get(), and associates a device with it using
499 * devres. On driver detach, release function is invoked on the devres
500 * data, then, devres data is freed. This differs to devm_phy_get() in
501 * that if the phy does not exist, it is not considered an error and
502 * -ENODEV will not be returned. Instead the NULL phy is returned,
503 * which can be passed to all other phy consumer calls.
505 struct phy *devm_phy_optional_get(struct device *dev, const char *string)
507 struct phy *phy = devm_phy_get(dev, string);
509 if (PTR_ERR(phy) == -ENODEV)
514 EXPORT_SYMBOL_GPL(devm_phy_optional_get);
517 * phy_create() - create a new phy
518 * @dev: device that is creating the new phy
519 * @node: device node of the phy
520 * @ops: function pointers for performing phy operations
521 * @init_data: contains the list of PHY consumers or NULL
523 * Called to create a phy using phy framework.
525 struct phy *phy_create(struct device *dev, struct device_node *node,
526 const struct phy_ops *ops,
527 struct phy_init_data *init_data)
534 return ERR_PTR(-EINVAL);
536 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
538 return ERR_PTR(-ENOMEM);
540 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
542 dev_err(dev, "unable to get id\n");
547 device_initialize(&phy->dev);
548 mutex_init(&phy->mutex);
550 phy->dev.class = phy_class;
551 phy->dev.parent = dev;
552 phy->dev.of_node = node ?: dev->of_node;
555 phy->init_data = init_data;
557 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
561 ret = device_add(&phy->dev);
565 if (pm_runtime_enabled(dev)) {
566 pm_runtime_enable(&phy->dev);
567 pm_runtime_no_callbacks(&phy->dev);
573 put_device(&phy->dev); /* calls phy_release() which frees resources */
580 EXPORT_SYMBOL_GPL(phy_create);
583 * devm_phy_create() - create a new phy
584 * @dev: device that is creating the new phy
585 * @node: device node of the phy
586 * @ops: function pointers for performing phy operations
587 * @init_data: contains the list of PHY consumers or NULL
589 * Creates a new PHY device adding it to the PHY class.
590 * While at that, it also associates the device with the phy using devres.
591 * On driver detach, release function is invoked on the devres data,
592 * then, devres data is freed.
594 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
595 const struct phy_ops *ops,
596 struct phy_init_data *init_data)
598 struct phy **ptr, *phy;
600 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
602 return ERR_PTR(-ENOMEM);
604 phy = phy_create(dev, node, ops, init_data);
607 devres_add(dev, ptr);
614 EXPORT_SYMBOL_GPL(devm_phy_create);
617 * phy_destroy() - destroy the phy
618 * @phy: the phy to be destroyed
620 * Called to destroy the phy.
622 void phy_destroy(struct phy *phy)
624 pm_runtime_disable(&phy->dev);
625 device_unregister(&phy->dev);
627 EXPORT_SYMBOL_GPL(phy_destroy);
630 * devm_phy_destroy() - destroy the PHY
631 * @dev: device that wants to release this phy
632 * @phy: the phy returned by devm_phy_get()
634 * destroys the devres associated with this phy and invokes phy_destroy
635 * to destroy the phy.
637 void devm_phy_destroy(struct device *dev, struct phy *phy)
641 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
642 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
644 EXPORT_SYMBOL_GPL(devm_phy_destroy);
647 * __of_phy_provider_register() - create/register phy provider with the framework
648 * @dev: struct device of the phy provider
649 * @owner: the module owner containing of_xlate
650 * @of_xlate: function pointer to obtain phy instance from phy provider
652 * Creates struct phy_provider from dev and of_xlate function pointer.
653 * This is used in the case of dt boot for finding the phy instance from
656 struct phy_provider *__of_phy_provider_register(struct device *dev,
657 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
658 struct of_phandle_args *args))
660 struct phy_provider *phy_provider;
662 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
664 return ERR_PTR(-ENOMEM);
666 phy_provider->dev = dev;
667 phy_provider->owner = owner;
668 phy_provider->of_xlate = of_xlate;
670 mutex_lock(&phy_provider_mutex);
671 list_add_tail(&phy_provider->list, &phy_provider_list);
672 mutex_unlock(&phy_provider_mutex);
676 EXPORT_SYMBOL_GPL(__of_phy_provider_register);
679 * __devm_of_phy_provider_register() - create/register phy provider with the
681 * @dev: struct device of the phy provider
682 * @owner: the module owner containing of_xlate
683 * @of_xlate: function pointer to obtain phy instance from phy provider
685 * Creates struct phy_provider from dev and of_xlate function pointer.
686 * This is used in the case of dt boot for finding the phy instance from
687 * phy provider. While at that, it also associates the device with the
688 * phy provider using devres. On driver detach, release function is invoked
689 * on the devres data, then, devres data is freed.
691 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
692 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
693 struct of_phandle_args *args))
695 struct phy_provider **ptr, *phy_provider;
697 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
699 return ERR_PTR(-ENOMEM);
701 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
702 if (!IS_ERR(phy_provider)) {
704 devres_add(dev, ptr);
711 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
714 * of_phy_provider_unregister() - unregister phy provider from the framework
715 * @phy_provider: phy provider returned by of_phy_provider_register()
717 * Removes the phy_provider created using of_phy_provider_register().
719 void of_phy_provider_unregister(struct phy_provider *phy_provider)
721 if (IS_ERR(phy_provider))
724 mutex_lock(&phy_provider_mutex);
725 list_del(&phy_provider->list);
727 mutex_unlock(&phy_provider_mutex);
729 EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
732 * devm_of_phy_provider_unregister() - remove phy provider from the framework
733 * @dev: struct device of the phy provider
735 * destroys the devres associated with this phy provider and invokes
736 * of_phy_provider_unregister to unregister the phy provider.
738 void devm_of_phy_provider_unregister(struct device *dev,
739 struct phy_provider *phy_provider) {
742 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
744 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
746 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
749 * phy_release() - release the phy
750 * @dev: the dev member within phy
752 * When the last reference to the device is removed, it is called
753 * from the embedded kobject as release method.
755 static void phy_release(struct device *dev)
760 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
761 ida_simple_remove(&phy_ida, phy->id);
765 static int __init phy_core_init(void)
767 phy_class = class_create(THIS_MODULE, "phy");
768 if (IS_ERR(phy_class)) {
769 pr_err("failed to create phy class --> %ld\n",
771 return PTR_ERR(phy_class);
774 phy_class->dev_release = phy_release;
778 module_init(phy_core_init);
780 static void __exit phy_core_exit(void)
782 class_destroy(phy_class);
784 module_exit(phy_core_exit);
786 MODULE_DESCRIPTION("Generic PHY Framework");
787 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
788 MODULE_LICENSE("GPL v2");