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;
90 list_for_each_entry(phy_provider, &phy_provider_list, list) {
91 if (phy_provider->dev->of_node == node)
95 return ERR_PTR(-EPROBE_DEFER);
98 int phy_pm_runtime_get(struct phy *phy)
102 if (!pm_runtime_enabled(&phy->dev))
105 ret = pm_runtime_get(&phy->dev);
106 if (ret < 0 && ret != -EINPROGRESS)
107 pm_runtime_put_noidle(&phy->dev);
111 EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
113 int phy_pm_runtime_get_sync(struct phy *phy)
117 if (!pm_runtime_enabled(&phy->dev))
120 ret = pm_runtime_get_sync(&phy->dev);
122 pm_runtime_put_sync(&phy->dev);
126 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
128 int phy_pm_runtime_put(struct phy *phy)
130 if (!pm_runtime_enabled(&phy->dev))
133 return pm_runtime_put(&phy->dev);
135 EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
137 int phy_pm_runtime_put_sync(struct phy *phy)
139 if (!pm_runtime_enabled(&phy->dev))
142 return pm_runtime_put_sync(&phy->dev);
144 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
146 void phy_pm_runtime_allow(struct phy *phy)
148 if (!pm_runtime_enabled(&phy->dev))
151 pm_runtime_allow(&phy->dev);
153 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
155 void phy_pm_runtime_forbid(struct phy *phy)
157 if (!pm_runtime_enabled(&phy->dev))
160 pm_runtime_forbid(&phy->dev);
162 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
164 int phy_init(struct phy *phy)
171 ret = phy_pm_runtime_get_sync(phy);
172 if (ret < 0 && ret != -ENOTSUPP)
175 mutex_lock(&phy->mutex);
176 if (phy->init_count == 0 && phy->ops->init) {
177 ret = phy->ops->init(phy);
179 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
183 ret = 0; /* Override possible ret == -ENOTSUPP */
188 mutex_unlock(&phy->mutex);
189 phy_pm_runtime_put(phy);
192 EXPORT_SYMBOL_GPL(phy_init);
194 int phy_exit(struct phy *phy)
201 ret = phy_pm_runtime_get_sync(phy);
202 if (ret < 0 && ret != -ENOTSUPP)
205 mutex_lock(&phy->mutex);
206 if (phy->init_count == 1 && phy->ops->exit) {
207 ret = phy->ops->exit(phy);
209 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
216 mutex_unlock(&phy->mutex);
217 phy_pm_runtime_put(phy);
220 EXPORT_SYMBOL_GPL(phy_exit);
222 int phy_power_on(struct phy *phy)
229 ret = phy_pm_runtime_get_sync(phy);
230 if (ret < 0 && ret != -ENOTSUPP)
233 mutex_lock(&phy->mutex);
234 if (phy->power_count == 0 && phy->ops->power_on) {
235 ret = phy->ops->power_on(phy);
237 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
241 ret = 0; /* Override possible ret == -ENOTSUPP */
244 mutex_unlock(&phy->mutex);
248 mutex_unlock(&phy->mutex);
249 phy_pm_runtime_put_sync(phy);
253 EXPORT_SYMBOL_GPL(phy_power_on);
255 int phy_power_off(struct phy *phy)
262 mutex_lock(&phy->mutex);
263 if (phy->power_count == 1 && phy->ops->power_off) {
264 ret = phy->ops->power_off(phy);
266 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
267 mutex_unlock(&phy->mutex);
272 mutex_unlock(&phy->mutex);
273 phy_pm_runtime_put(phy);
277 EXPORT_SYMBOL_GPL(phy_power_off);
280 * of_phy_get() - lookup and obtain a reference to a phy by phandle
281 * @dev: device that requests this phy
282 * @index: the index of the phy
284 * Returns the phy associated with the given phandle value,
285 * after getting a refcount to it or -ENODEV if there is no such phy or
286 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
287 * not yet loaded. This function uses of_xlate call back function provided
288 * while registering the phy_provider to find the phy instance.
290 static struct phy *of_phy_get(struct device *dev, int index)
293 struct phy_provider *phy_provider;
294 struct phy *phy = NULL;
295 struct of_phandle_args args;
297 ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
300 dev_dbg(dev, "failed to get phy in %s node\n",
301 dev->of_node->full_name);
302 return ERR_PTR(-ENODEV);
305 mutex_lock(&phy_provider_mutex);
306 phy_provider = of_phy_provider_lookup(args.np);
307 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
308 phy = ERR_PTR(-EPROBE_DEFER);
312 phy = phy_provider->of_xlate(phy_provider->dev, &args);
313 module_put(phy_provider->owner);
316 mutex_unlock(&phy_provider_mutex);
317 of_node_put(args.np);
323 * phy_put() - release the PHY
324 * @phy: the phy returned by phy_get()
326 * Releases a refcount the caller received from phy_get().
328 void phy_put(struct phy *phy)
330 if (!phy || IS_ERR(phy))
333 module_put(phy->ops->owner);
334 put_device(&phy->dev);
336 EXPORT_SYMBOL_GPL(phy_put);
339 * devm_phy_put() - release the PHY
340 * @dev: device that wants to release this phy
341 * @phy: the phy returned by devm_phy_get()
343 * destroys the devres associated with this phy and invokes phy_put
344 * to release the phy.
346 void devm_phy_put(struct device *dev, struct phy *phy)
353 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
354 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
356 EXPORT_SYMBOL_GPL(devm_phy_put);
359 * of_phy_simple_xlate() - returns the phy instance from phy provider
360 * @dev: the PHY provider device
361 * @args: of_phandle_args (not used here)
363 * Intended to be used by phy provider for the common case where #phy-cells is
364 * 0. For other cases where #phy-cells is greater than '0', the phy provider
365 * should provide a custom of_xlate function that reads the *args* and returns
366 * the appropriate phy.
368 struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
372 struct class_dev_iter iter;
373 struct device_node *node = dev->of_node;
374 struct device_node *child;
376 class_dev_iter_init(&iter, phy_class, NULL, NULL);
377 while ((dev = class_dev_iter_next(&iter))) {
379 if (node != phy->dev.of_node) {
380 for_each_child_of_node(node, child) {
381 if (child == phy->dev.of_node)
388 class_dev_iter_exit(&iter);
392 class_dev_iter_exit(&iter);
393 return ERR_PTR(-ENODEV);
395 EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
398 * phy_get() - lookup and obtain a reference to a phy.
399 * @dev: device that requests this phy
400 * @string: the phy name as given in the dt data or the name of the controller
401 * port for non-dt case
403 * Returns the phy driver, after getting a refcount to it; or
404 * -ENODEV if there is no such phy. The caller is responsible for
405 * calling phy_put() to release that count.
407 struct phy *phy_get(struct device *dev, const char *string)
412 if (string == NULL) {
413 dev_WARN(dev, "missing string\n");
414 return ERR_PTR(-EINVAL);
418 index = of_property_match_string(dev->of_node, "phy-names",
420 phy = of_phy_get(dev, index);
422 phy = phy_lookup(dev, string);
427 if (!try_module_get(phy->ops->owner))
428 return ERR_PTR(-EPROBE_DEFER);
430 get_device(&phy->dev);
434 EXPORT_SYMBOL_GPL(phy_get);
437 * phy_optional_get() - lookup and obtain a reference to an optional phy.
438 * @dev: device that requests this phy
439 * @string: the phy name as given in the dt data or the name of the controller
440 * port for non-dt case
442 * Returns the phy driver, after getting a refcount to it; or
443 * NULL if there is no such phy. The caller is responsible for
444 * calling phy_put() to release that count.
446 struct phy *phy_optional_get(struct device *dev, const char *string)
448 struct phy *phy = phy_get(dev, string);
450 if (PTR_ERR(phy) == -ENODEV)
455 EXPORT_SYMBOL_GPL(phy_optional_get);
458 * devm_phy_get() - lookup and obtain a reference to a phy.
459 * @dev: device that requests this phy
460 * @string: the phy name as given in the dt data or phy device name
463 * Gets the phy using phy_get(), and associates a device with it using
464 * devres. On driver detach, release function is invoked on the devres data,
465 * then, devres data is freed.
467 struct phy *devm_phy_get(struct device *dev, const char *string)
469 struct phy **ptr, *phy;
471 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
473 return ERR_PTR(-ENOMEM);
475 phy = phy_get(dev, string);
478 devres_add(dev, ptr);
485 EXPORT_SYMBOL_GPL(devm_phy_get);
488 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
489 * @dev: device that requests this phy
490 * @string: the phy name as given in the dt data or phy device name
493 * Gets the phy using phy_get(), and associates a device with it using
494 * devres. On driver detach, release function is invoked on the devres
495 * data, then, devres data is freed. This differs to devm_phy_get() in
496 * that if the phy does not exist, it is not considered an error and
497 * -ENODEV will not be returned. Instead the NULL phy is returned,
498 * which can be passed to all other phy consumer calls.
500 struct phy *devm_phy_optional_get(struct device *dev, const char *string)
502 struct phy *phy = devm_phy_get(dev, string);
504 if (PTR_ERR(phy) == -ENODEV)
509 EXPORT_SYMBOL_GPL(devm_phy_optional_get);
512 * phy_create() - create a new phy
513 * @dev: device that is creating the new phy
514 * @node: device node of the phy
515 * @ops: function pointers for performing phy operations
516 * @init_data: contains the list of PHY consumers or NULL
518 * Called to create a phy using phy framework.
520 struct phy *phy_create(struct device *dev, struct device_node *node,
521 const struct phy_ops *ops,
522 struct phy_init_data *init_data)
529 return ERR_PTR(-EINVAL);
531 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
533 return ERR_PTR(-ENOMEM);
535 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
537 dev_err(dev, "unable to get id\n");
542 device_initialize(&phy->dev);
543 mutex_init(&phy->mutex);
545 phy->dev.class = phy_class;
546 phy->dev.parent = dev;
547 phy->dev.of_node = node ?: dev->of_node;
550 phy->init_data = init_data;
552 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
556 ret = device_add(&phy->dev);
560 if (pm_runtime_enabled(dev)) {
561 pm_runtime_enable(&phy->dev);
562 pm_runtime_no_callbacks(&phy->dev);
568 put_device(&phy->dev); /* calls phy_release() which frees resources */
575 EXPORT_SYMBOL_GPL(phy_create);
578 * devm_phy_create() - create a new phy
579 * @dev: device that is creating the new phy
580 * @node: device node of the phy
581 * @ops: function pointers for performing phy operations
582 * @init_data: contains the list of PHY consumers or NULL
584 * Creates a new PHY device adding it to the PHY class.
585 * While at that, it also associates the device with the phy using devres.
586 * On driver detach, release function is invoked on the devres data,
587 * then, devres data is freed.
589 struct phy *devm_phy_create(struct device *dev, struct device_node *node,
590 const struct phy_ops *ops,
591 struct phy_init_data *init_data)
593 struct phy **ptr, *phy;
595 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
597 return ERR_PTR(-ENOMEM);
599 phy = phy_create(dev, node, ops, init_data);
602 devres_add(dev, ptr);
609 EXPORT_SYMBOL_GPL(devm_phy_create);
612 * phy_destroy() - destroy the phy
613 * @phy: the phy to be destroyed
615 * Called to destroy the phy.
617 void phy_destroy(struct phy *phy)
619 pm_runtime_disable(&phy->dev);
620 device_unregister(&phy->dev);
622 EXPORT_SYMBOL_GPL(phy_destroy);
625 * devm_phy_destroy() - destroy the PHY
626 * @dev: device that wants to release this phy
627 * @phy: the phy returned by devm_phy_get()
629 * destroys the devres associated with this phy and invokes phy_destroy
630 * to destroy the phy.
632 void devm_phy_destroy(struct device *dev, struct phy *phy)
636 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
637 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
639 EXPORT_SYMBOL_GPL(devm_phy_destroy);
642 * __of_phy_provider_register() - create/register phy provider with the framework
643 * @dev: struct device of the phy provider
644 * @owner: the module owner containing of_xlate
645 * @of_xlate: function pointer to obtain phy instance from phy provider
647 * Creates struct phy_provider from dev and of_xlate function pointer.
648 * This is used in the case of dt boot for finding the phy instance from
651 struct phy_provider *__of_phy_provider_register(struct device *dev,
652 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
653 struct of_phandle_args *args))
655 struct phy_provider *phy_provider;
657 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
659 return ERR_PTR(-ENOMEM);
661 phy_provider->dev = dev;
662 phy_provider->owner = owner;
663 phy_provider->of_xlate = of_xlate;
665 mutex_lock(&phy_provider_mutex);
666 list_add_tail(&phy_provider->list, &phy_provider_list);
667 mutex_unlock(&phy_provider_mutex);
671 EXPORT_SYMBOL_GPL(__of_phy_provider_register);
674 * __devm_of_phy_provider_register() - create/register phy provider with the
676 * @dev: struct device of the phy provider
677 * @owner: the module owner containing of_xlate
678 * @of_xlate: function pointer to obtain phy instance from phy provider
680 * Creates struct phy_provider from dev and of_xlate function pointer.
681 * This is used in the case of dt boot for finding the phy instance from
682 * phy provider. While at that, it also associates the device with the
683 * phy provider using devres. On driver detach, release function is invoked
684 * on the devres data, then, devres data is freed.
686 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
687 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
688 struct of_phandle_args *args))
690 struct phy_provider **ptr, *phy_provider;
692 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
694 return ERR_PTR(-ENOMEM);
696 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
697 if (!IS_ERR(phy_provider)) {
699 devres_add(dev, ptr);
706 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
709 * of_phy_provider_unregister() - unregister phy provider from the framework
710 * @phy_provider: phy provider returned by of_phy_provider_register()
712 * Removes the phy_provider created using of_phy_provider_register().
714 void of_phy_provider_unregister(struct phy_provider *phy_provider)
716 if (IS_ERR(phy_provider))
719 mutex_lock(&phy_provider_mutex);
720 list_del(&phy_provider->list);
722 mutex_unlock(&phy_provider_mutex);
724 EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
727 * devm_of_phy_provider_unregister() - remove phy provider from the framework
728 * @dev: struct device of the phy provider
730 * destroys the devres associated with this phy provider and invokes
731 * of_phy_provider_unregister to unregister the phy provider.
733 void devm_of_phy_provider_unregister(struct device *dev,
734 struct phy_provider *phy_provider) {
737 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
739 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
741 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
744 * phy_release() - release the phy
745 * @dev: the dev member within phy
747 * When the last reference to the device is removed, it is called
748 * from the embedded kobject as release method.
750 static void phy_release(struct device *dev)
755 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
756 ida_simple_remove(&phy_ida, phy->id);
760 static int __init phy_core_init(void)
762 phy_class = class_create(THIS_MODULE, "phy");
763 if (IS_ERR(phy_class)) {
764 pr_err("failed to create phy class --> %ld\n",
766 return PTR_ERR(phy_class);
769 phy_class->dev_release = phy_release;
773 module_init(phy_core_init);
775 static void __exit phy_core_exit(void)
777 class_destroy(phy_class);
779 module_exit(phy_core_exit);
781 MODULE_DESCRIPTION("Generic PHY Framework");
782 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
783 MODULE_LICENSE("GPL v2");