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
))) {
67 count
= phy
->init_data
->num_consumers
;
68 consumers
= phy
->init_data
->consumers
;
70 if (!strcmp(consumers
->dev_name
, dev_name(device
)) &&
71 !strcmp(consumers
->port
, port
)) {
72 class_dev_iter_exit(&iter
);
79 class_dev_iter_exit(&iter
);
80 return ERR_PTR(-ENODEV
);
83 static struct phy_provider
*of_phy_provider_lookup(struct device_node
*node
)
85 struct phy_provider
*phy_provider
;
87 list_for_each_entry(phy_provider
, &phy_provider_list
, list
) {
88 if (phy_provider
->dev
->of_node
== node
)
92 return ERR_PTR(-EPROBE_DEFER
);
95 int phy_pm_runtime_get(struct phy
*phy
)
97 if (!pm_runtime_enabled(&phy
->dev
))
100 return pm_runtime_get(&phy
->dev
);
102 EXPORT_SYMBOL_GPL(phy_pm_runtime_get
);
104 int phy_pm_runtime_get_sync(struct phy
*phy
)
106 if (!pm_runtime_enabled(&phy
->dev
))
109 return pm_runtime_get_sync(&phy
->dev
);
111 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync
);
113 int phy_pm_runtime_put(struct phy
*phy
)
115 if (!pm_runtime_enabled(&phy
->dev
))
118 return pm_runtime_put(&phy
->dev
);
120 EXPORT_SYMBOL_GPL(phy_pm_runtime_put
);
122 int phy_pm_runtime_put_sync(struct phy
*phy
)
124 if (!pm_runtime_enabled(&phy
->dev
))
127 return pm_runtime_put_sync(&phy
->dev
);
129 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync
);
131 void phy_pm_runtime_allow(struct phy
*phy
)
133 if (!pm_runtime_enabled(&phy
->dev
))
136 pm_runtime_allow(&phy
->dev
);
138 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow
);
140 void phy_pm_runtime_forbid(struct phy
*phy
)
142 if (!pm_runtime_enabled(&phy
->dev
))
145 pm_runtime_forbid(&phy
->dev
);
147 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid
);
149 int phy_init(struct phy
*phy
)
153 ret
= phy_pm_runtime_get_sync(phy
);
154 if (ret
< 0 && ret
!= -ENOTSUPP
)
157 mutex_lock(&phy
->mutex
);
158 if (phy
->init_count
++ == 0 && phy
->ops
->init
) {
159 ret
= phy
->ops
->init(phy
);
161 dev_err(&phy
->dev
, "phy init failed --> %d\n", ret
);
167 mutex_unlock(&phy
->mutex
);
168 phy_pm_runtime_put(phy
);
171 EXPORT_SYMBOL_GPL(phy_init
);
173 int phy_exit(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
->exit
) {
183 ret
= phy
->ops
->exit(phy
);
185 dev_err(&phy
->dev
, "phy exit failed --> %d\n", ret
);
191 mutex_unlock(&phy
->mutex
);
192 phy_pm_runtime_put(phy
);
195 EXPORT_SYMBOL_GPL(phy_exit
);
197 int phy_power_on(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
->power_count
++ == 0 && phy
->ops
->power_on
) {
207 ret
= phy
->ops
->power_on(phy
);
209 dev_err(&phy
->dev
, "phy poweron failed --> %d\n", ret
);
215 mutex_unlock(&phy
->mutex
);
219 EXPORT_SYMBOL_GPL(phy_power_on
);
221 int phy_power_off(struct phy
*phy
)
225 mutex_lock(&phy
->mutex
);
226 if (--phy
->power_count
== 0 && phy
->ops
->power_off
) {
227 ret
= phy
->ops
->power_off(phy
);
229 dev_err(&phy
->dev
, "phy poweroff failed --> %d\n", ret
);
235 mutex_unlock(&phy
->mutex
);
236 phy_pm_runtime_put(phy
);
240 EXPORT_SYMBOL_GPL(phy_power_off
);
243 * of_phy_get() - lookup and obtain a reference to a phy by phandle
244 * @dev: device that requests this phy
245 * @index: the index of the phy
247 * Returns the phy associated with the given phandle value,
248 * after getting a refcount to it or -ENODEV if there is no such phy or
249 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
250 * not yet loaded. This function uses of_xlate call back function provided
251 * while registering the phy_provider to find the phy instance.
253 static struct phy
*of_phy_get(struct device
*dev
, int index
)
256 struct phy_provider
*phy_provider
;
257 struct phy
*phy
= NULL
;
258 struct of_phandle_args args
;
260 ret
= of_parse_phandle_with_args(dev
->of_node
, "phys", "#phy-cells",
263 dev_dbg(dev
, "failed to get phy in %s node\n",
264 dev
->of_node
->full_name
);
265 return ERR_PTR(-ENODEV
);
268 mutex_lock(&phy_provider_mutex
);
269 phy_provider
= of_phy_provider_lookup(args
.np
);
270 if (IS_ERR(phy_provider
) || !try_module_get(phy_provider
->owner
)) {
271 phy
= ERR_PTR(-EPROBE_DEFER
);
275 phy
= phy_provider
->of_xlate(phy_provider
->dev
, &args
);
276 module_put(phy_provider
->owner
);
279 mutex_unlock(&phy_provider_mutex
);
280 of_node_put(args
.np
);
286 * phy_put() - release the PHY
287 * @phy: the phy returned by phy_get()
289 * Releases a refcount the caller received from phy_get().
291 void phy_put(struct phy
*phy
)
296 module_put(phy
->ops
->owner
);
297 put_device(&phy
->dev
);
299 EXPORT_SYMBOL_GPL(phy_put
);
302 * devm_phy_put() - release the PHY
303 * @dev: device that wants to release this phy
304 * @phy: the phy returned by devm_phy_get()
306 * destroys the devres associated with this phy and invokes phy_put
307 * to release the phy.
309 void devm_phy_put(struct device
*dev
, struct phy
*phy
)
313 r
= devres_destroy(dev
, devm_phy_release
, devm_phy_match
, phy
);
314 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
316 EXPORT_SYMBOL_GPL(devm_phy_put
);
319 * of_phy_simple_xlate() - returns the phy instance from phy provider
320 * @dev: the PHY provider device
321 * @args: of_phandle_args (not used here)
323 * Intended to be used by phy provider for the common case where #phy-cells is
324 * 0. For other cases where #phy-cells is greater than '0', the phy provider
325 * should provide a custom of_xlate function that reads the *args* and returns
326 * the appropriate phy.
328 struct phy
*of_phy_simple_xlate(struct device
*dev
, struct of_phandle_args
332 struct class_dev_iter iter
;
333 struct device_node
*node
= dev
->of_node
;
335 class_dev_iter_init(&iter
, phy_class
, NULL
, NULL
);
336 while ((dev
= class_dev_iter_next(&iter
))) {
338 if (node
!= phy
->dev
.of_node
)
341 class_dev_iter_exit(&iter
);
345 class_dev_iter_exit(&iter
);
346 return ERR_PTR(-ENODEV
);
348 EXPORT_SYMBOL_GPL(of_phy_simple_xlate
);
351 * phy_get() - lookup and obtain a reference to a phy.
352 * @dev: device that requests this phy
353 * @string: the phy name as given in the dt data or the name of the controller
354 * port for non-dt case
356 * Returns the phy driver, after getting a refcount to it; or
357 * -ENODEV if there is no such phy. The caller is responsible for
358 * calling phy_put() to release that count.
360 struct phy
*phy_get(struct device
*dev
, const char *string
)
363 struct phy
*phy
= NULL
;
365 if (string
== NULL
) {
366 dev_WARN(dev
, "missing string\n");
367 return ERR_PTR(-EINVAL
);
371 index
= of_property_match_string(dev
->of_node
, "phy-names",
373 phy
= of_phy_get(dev
, index
);
375 dev_err(dev
, "unable to find phy\n");
379 phy
= phy_lookup(dev
, string
);
381 dev_err(dev
, "unable to find phy\n");
386 if (!try_module_get(phy
->ops
->owner
))
387 return ERR_PTR(-EPROBE_DEFER
);
389 get_device(&phy
->dev
);
393 EXPORT_SYMBOL_GPL(phy_get
);
396 * devm_phy_get() - lookup and obtain a reference to a phy.
397 * @dev: device that requests this phy
398 * @string: the phy name as given in the dt data or phy device name
401 * Gets the phy using phy_get(), and associates a device with it using
402 * devres. On driver detach, release function is invoked on the devres data,
403 * then, devres data is freed.
405 struct phy
*devm_phy_get(struct device
*dev
, const char *string
)
407 struct phy
**ptr
, *phy
;
409 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
411 return ERR_PTR(-ENOMEM
);
413 phy
= phy_get(dev
, string
);
416 devres_add(dev
, ptr
);
423 EXPORT_SYMBOL_GPL(devm_phy_get
);
426 * phy_create() - create a new phy
427 * @dev: device that is creating the new phy
428 * @ops: function pointers for performing phy operations
429 * @init_data: contains the list of PHY consumers or NULL
431 * Called to create a phy using phy framework.
433 struct phy
*phy_create(struct device
*dev
, const struct phy_ops
*ops
,
434 struct phy_init_data
*init_data
)
441 return ERR_PTR(-EINVAL
);
443 phy
= kzalloc(sizeof(*phy
), GFP_KERNEL
);
445 return ERR_PTR(-ENOMEM
);
447 id
= ida_simple_get(&phy_ida
, 0, 0, GFP_KERNEL
);
449 dev_err(dev
, "unable to get id\n");
454 device_initialize(&phy
->dev
);
455 mutex_init(&phy
->mutex
);
457 phy
->dev
.class = phy_class
;
458 phy
->dev
.parent
= dev
;
459 phy
->dev
.of_node
= dev
->of_node
;
462 phy
->init_data
= init_data
;
464 ret
= dev_set_name(&phy
->dev
, "phy-%s.%d", dev_name(dev
), id
);
468 ret
= device_add(&phy
->dev
);
472 if (pm_runtime_enabled(dev
)) {
473 pm_runtime_enable(&phy
->dev
);
474 pm_runtime_no_callbacks(&phy
->dev
);
480 put_device(&phy
->dev
);
481 ida_remove(&phy_ida
, phy
->id
);
486 EXPORT_SYMBOL_GPL(phy_create
);
489 * devm_phy_create() - create a new phy
490 * @dev: device that is creating the new phy
491 * @ops: function pointers for performing phy operations
492 * @init_data: contains the list of PHY consumers or NULL
494 * Creates a new PHY device adding it to the PHY class.
495 * While at that, it also associates the device with the phy using devres.
496 * On driver detach, release function is invoked on the devres data,
497 * then, devres data is freed.
499 struct phy
*devm_phy_create(struct device
*dev
, const struct phy_ops
*ops
,
500 struct phy_init_data
*init_data
)
502 struct phy
**ptr
, *phy
;
504 ptr
= devres_alloc(devm_phy_consume
, sizeof(*ptr
), GFP_KERNEL
);
506 return ERR_PTR(-ENOMEM
);
508 phy
= phy_create(dev
, ops
, init_data
);
511 devres_add(dev
, ptr
);
518 EXPORT_SYMBOL_GPL(devm_phy_create
);
521 * phy_destroy() - destroy the phy
522 * @phy: the phy to be destroyed
524 * Called to destroy the phy.
526 void phy_destroy(struct phy
*phy
)
528 pm_runtime_disable(&phy
->dev
);
529 device_unregister(&phy
->dev
);
531 EXPORT_SYMBOL_GPL(phy_destroy
);
534 * devm_phy_destroy() - destroy the PHY
535 * @dev: device that wants to release this phy
536 * @phy: the phy returned by devm_phy_get()
538 * destroys the devres associated with this phy and invokes phy_destroy
539 * to destroy the phy.
541 void devm_phy_destroy(struct device
*dev
, struct phy
*phy
)
545 r
= devres_destroy(dev
, devm_phy_consume
, devm_phy_match
, phy
);
546 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
548 EXPORT_SYMBOL_GPL(devm_phy_destroy
);
551 * __of_phy_provider_register() - create/register phy provider with the framework
552 * @dev: struct device of the phy provider
553 * @owner: the module owner containing of_xlate
554 * @of_xlate: function pointer to obtain phy instance from phy provider
556 * Creates struct phy_provider from dev and of_xlate function pointer.
557 * This is used in the case of dt boot for finding the phy instance from
560 struct phy_provider
*__of_phy_provider_register(struct device
*dev
,
561 struct module
*owner
, struct phy
* (*of_xlate
)(struct device
*dev
,
562 struct of_phandle_args
*args
))
564 struct phy_provider
*phy_provider
;
566 phy_provider
= kzalloc(sizeof(*phy_provider
), GFP_KERNEL
);
568 return ERR_PTR(-ENOMEM
);
570 phy_provider
->dev
= dev
;
571 phy_provider
->owner
= owner
;
572 phy_provider
->of_xlate
= of_xlate
;
574 mutex_lock(&phy_provider_mutex
);
575 list_add_tail(&phy_provider
->list
, &phy_provider_list
);
576 mutex_unlock(&phy_provider_mutex
);
580 EXPORT_SYMBOL_GPL(__of_phy_provider_register
);
583 * __devm_of_phy_provider_register() - create/register phy provider with the
585 * @dev: struct device of the phy provider
586 * @owner: the module owner containing of_xlate
587 * @of_xlate: function pointer to obtain phy instance from phy provider
589 * Creates struct phy_provider from dev and of_xlate function pointer.
590 * This is used in the case of dt boot for finding the phy instance from
591 * phy provider. While at that, it also associates the device with the
592 * phy provider using devres. On driver detach, release function is invoked
593 * on the devres data, then, devres data is freed.
595 struct phy_provider
*__devm_of_phy_provider_register(struct device
*dev
,
596 struct module
*owner
, struct phy
* (*of_xlate
)(struct device
*dev
,
597 struct of_phandle_args
*args
))
599 struct phy_provider
**ptr
, *phy_provider
;
601 ptr
= devres_alloc(devm_phy_provider_release
, sizeof(*ptr
), GFP_KERNEL
);
603 return ERR_PTR(-ENOMEM
);
605 phy_provider
= __of_phy_provider_register(dev
, owner
, of_xlate
);
606 if (!IS_ERR(phy_provider
)) {
608 devres_add(dev
, ptr
);
615 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register
);
618 * of_phy_provider_unregister() - unregister phy provider from the framework
619 * @phy_provider: phy provider returned by of_phy_provider_register()
621 * Removes the phy_provider created using of_phy_provider_register().
623 void of_phy_provider_unregister(struct phy_provider
*phy_provider
)
625 if (IS_ERR(phy_provider
))
628 mutex_lock(&phy_provider_mutex
);
629 list_del(&phy_provider
->list
);
631 mutex_unlock(&phy_provider_mutex
);
633 EXPORT_SYMBOL_GPL(of_phy_provider_unregister
);
636 * devm_of_phy_provider_unregister() - remove phy provider from the framework
637 * @dev: struct device of the phy provider
639 * destroys the devres associated with this phy provider and invokes
640 * of_phy_provider_unregister to unregister the phy provider.
642 void devm_of_phy_provider_unregister(struct device
*dev
,
643 struct phy_provider
*phy_provider
) {
646 r
= devres_destroy(dev
, devm_phy_provider_release
, devm_phy_match
,
648 dev_WARN_ONCE(dev
, r
, "couldn't find PHY provider device resource\n");
650 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister
);
653 * phy_release() - release the phy
654 * @dev: the dev member within phy
656 * When the last reference to the device is removed, it is called
657 * from the embedded kobject as release method.
659 static void phy_release(struct device
*dev
)
664 dev_vdbg(dev
, "releasing '%s'\n", dev_name(dev
));
665 ida_remove(&phy_ida
, phy
->id
);
669 static int __init
phy_core_init(void)
671 phy_class
= class_create(THIS_MODULE
, "phy");
672 if (IS_ERR(phy_class
)) {
673 pr_err("failed to create phy class --> %ld\n",
675 return PTR_ERR(phy_class
);
678 phy_class
->dev_release
= phy_release
;
682 module_init(phy_core_init
);
684 static void __exit
phy_core_exit(void)
686 class_destroy(phy_class
);
688 module_exit(phy_core_exit
);
690 MODULE_DESCRIPTION("Generic PHY Framework");
691 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
692 MODULE_LICENSE("GPL v2");