PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / phy / phy-core.c
blob5f5b0f4be5be3da4e3146f4479d416306f4de9e9
1 /*
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>
20 #include <linux/of.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;
34 phy_put(phy);
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;
48 phy_destroy(phy);
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)
58 unsigned int count;
59 struct phy *phy;
60 struct device *dev;
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))) {
66 phy = to_phy(dev);
67 count = phy->init_data->num_consumers;
68 consumers = phy->init_data->consumers;
69 while (count--) {
70 if (!strcmp(consumers->dev_name, dev_name(device)) &&
71 !strcmp(consumers->port, port)) {
72 class_dev_iter_exit(&iter);
73 return phy;
75 consumers++;
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)
89 return phy_provider;
92 return ERR_PTR(-EPROBE_DEFER);
95 int phy_pm_runtime_get(struct phy *phy)
97 int ret;
99 if (!pm_runtime_enabled(&phy->dev))
100 return -ENOTSUPP;
102 ret = pm_runtime_get(&phy->dev);
103 if (ret < 0 && ret != -EINPROGRESS)
104 pm_runtime_put_noidle(&phy->dev);
106 return ret;
108 EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
110 int phy_pm_runtime_get_sync(struct phy *phy)
112 int ret;
114 if (!pm_runtime_enabled(&phy->dev))
115 return -ENOTSUPP;
117 ret = pm_runtime_get_sync(&phy->dev);
118 if (ret < 0)
119 pm_runtime_put_sync(&phy->dev);
121 return ret;
123 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
125 int phy_pm_runtime_put(struct phy *phy)
127 if (!pm_runtime_enabled(&phy->dev))
128 return -ENOTSUPP;
130 return pm_runtime_put(&phy->dev);
132 EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
134 int phy_pm_runtime_put_sync(struct phy *phy)
136 if (!pm_runtime_enabled(&phy->dev))
137 return -ENOTSUPP;
139 return pm_runtime_put_sync(&phy->dev);
141 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
143 void phy_pm_runtime_allow(struct phy *phy)
145 if (!pm_runtime_enabled(&phy->dev))
146 return;
148 pm_runtime_allow(&phy->dev);
150 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
152 void phy_pm_runtime_forbid(struct phy *phy)
154 if (!pm_runtime_enabled(&phy->dev))
155 return;
157 pm_runtime_forbid(&phy->dev);
159 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
161 int phy_init(struct phy *phy)
163 int ret;
165 if (!phy)
166 return 0;
168 ret = phy_pm_runtime_get_sync(phy);
169 if (ret < 0 && ret != -ENOTSUPP)
170 return ret;
172 mutex_lock(&phy->mutex);
173 if (phy->init_count == 0 && phy->ops->init) {
174 ret = phy->ops->init(phy);
175 if (ret < 0) {
176 dev_err(&phy->dev, "phy init failed --> %d\n", ret);
177 goto out;
180 ++phy->init_count;
182 out:
183 mutex_unlock(&phy->mutex);
184 phy_pm_runtime_put(phy);
185 return ret;
187 EXPORT_SYMBOL_GPL(phy_init);
189 int phy_exit(struct phy *phy)
191 int ret;
193 if (!phy)
194 return 0;
196 ret = phy_pm_runtime_get_sync(phy);
197 if (ret < 0 && ret != -ENOTSUPP)
198 return ret;
200 mutex_lock(&phy->mutex);
201 if (phy->init_count == 1 && phy->ops->exit) {
202 ret = phy->ops->exit(phy);
203 if (ret < 0) {
204 dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
205 goto out;
208 --phy->init_count;
210 out:
211 mutex_unlock(&phy->mutex);
212 phy_pm_runtime_put(phy);
213 return ret;
215 EXPORT_SYMBOL_GPL(phy_exit);
217 int phy_power_on(struct phy *phy)
219 int ret;
221 if (!phy)
222 return 0;
224 ret = phy_pm_runtime_get_sync(phy);
225 if (ret < 0 && ret != -ENOTSUPP)
226 return ret;
228 mutex_lock(&phy->mutex);
229 if (phy->power_count == 0 && phy->ops->power_on) {
230 ret = phy->ops->power_on(phy);
231 if (ret < 0) {
232 dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
233 goto out;
236 ++phy->power_count;
237 mutex_unlock(&phy->mutex);
238 return 0;
240 out:
241 mutex_unlock(&phy->mutex);
242 phy_pm_runtime_put_sync(phy);
244 return ret;
246 EXPORT_SYMBOL_GPL(phy_power_on);
248 int phy_power_off(struct phy *phy)
250 int ret;
252 if (!phy)
253 return 0;
255 mutex_lock(&phy->mutex);
256 if (phy->power_count == 1 && phy->ops->power_off) {
257 ret = phy->ops->power_off(phy);
258 if (ret < 0) {
259 dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
260 mutex_unlock(&phy->mutex);
261 return ret;
264 --phy->power_count;
265 mutex_unlock(&phy->mutex);
266 phy_pm_runtime_put(phy);
268 return 0;
270 EXPORT_SYMBOL_GPL(phy_power_off);
273 * of_phy_get() - lookup and obtain a reference to a phy by phandle
274 * @dev: device that requests this phy
275 * @index: the index of the phy
277 * Returns the phy associated with the given phandle value,
278 * after getting a refcount to it or -ENODEV if there is no such phy or
279 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
280 * not yet loaded. This function uses of_xlate call back function provided
281 * while registering the phy_provider to find the phy instance.
283 static struct phy *of_phy_get(struct device *dev, int index)
285 int ret;
286 struct phy_provider *phy_provider;
287 struct phy *phy = NULL;
288 struct of_phandle_args args;
290 ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
291 index, &args);
292 if (ret) {
293 dev_dbg(dev, "failed to get phy in %s node\n",
294 dev->of_node->full_name);
295 return ERR_PTR(-ENODEV);
298 mutex_lock(&phy_provider_mutex);
299 phy_provider = of_phy_provider_lookup(args.np);
300 if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
301 phy = ERR_PTR(-EPROBE_DEFER);
302 goto err0;
305 phy = phy_provider->of_xlate(phy_provider->dev, &args);
306 module_put(phy_provider->owner);
308 err0:
309 mutex_unlock(&phy_provider_mutex);
310 of_node_put(args.np);
312 return phy;
316 * phy_put() - release the PHY
317 * @phy: the phy returned by phy_get()
319 * Releases a refcount the caller received from phy_get().
321 void phy_put(struct phy *phy)
323 if (!phy || IS_ERR(phy))
324 return;
326 module_put(phy->ops->owner);
327 put_device(&phy->dev);
329 EXPORT_SYMBOL_GPL(phy_put);
332 * devm_phy_put() - release the PHY
333 * @dev: device that wants to release this phy
334 * @phy: the phy returned by devm_phy_get()
336 * destroys the devres associated with this phy and invokes phy_put
337 * to release the phy.
339 void devm_phy_put(struct device *dev, struct phy *phy)
341 int r;
343 if (!phy)
344 return;
346 r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
347 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
349 EXPORT_SYMBOL_GPL(devm_phy_put);
352 * of_phy_simple_xlate() - returns the phy instance from phy provider
353 * @dev: the PHY provider device
354 * @args: of_phandle_args (not used here)
356 * Intended to be used by phy provider for the common case where #phy-cells is
357 * 0. For other cases where #phy-cells is greater than '0', the phy provider
358 * should provide a custom of_xlate function that reads the *args* and returns
359 * the appropriate phy.
361 struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
362 *args)
364 struct phy *phy;
365 struct class_dev_iter iter;
366 struct device_node *node = dev->of_node;
368 class_dev_iter_init(&iter, phy_class, NULL, NULL);
369 while ((dev = class_dev_iter_next(&iter))) {
370 phy = to_phy(dev);
371 if (node != phy->dev.of_node)
372 continue;
374 class_dev_iter_exit(&iter);
375 return phy;
378 class_dev_iter_exit(&iter);
379 return ERR_PTR(-ENODEV);
381 EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
384 * phy_get() - lookup and obtain a reference to a phy.
385 * @dev: device that requests this phy
386 * @string: the phy name as given in the dt data or the name of the controller
387 * port for non-dt case
389 * Returns the phy driver, after getting a refcount to it; or
390 * -ENODEV if there is no such phy. The caller is responsible for
391 * calling phy_put() to release that count.
393 struct phy *phy_get(struct device *dev, const char *string)
395 int index = 0;
396 struct phy *phy;
398 if (string == NULL) {
399 dev_WARN(dev, "missing string\n");
400 return ERR_PTR(-EINVAL);
403 if (dev->of_node) {
404 index = of_property_match_string(dev->of_node, "phy-names",
405 string);
406 phy = of_phy_get(dev, index);
407 if (IS_ERR(phy)) {
408 dev_err(dev, "unable to find phy\n");
409 return phy;
411 } else {
412 phy = phy_lookup(dev, string);
413 if (IS_ERR(phy)) {
414 dev_err(dev, "unable to find phy\n");
415 return phy;
419 if (!try_module_get(phy->ops->owner))
420 return ERR_PTR(-EPROBE_DEFER);
422 get_device(&phy->dev);
424 return phy;
426 EXPORT_SYMBOL_GPL(phy_get);
429 * phy_optional_get() - lookup and obtain a reference to an optional phy.
430 * @dev: device that requests this phy
431 * @string: the phy name as given in the dt data or the name of the controller
432 * port for non-dt case
434 * Returns the phy driver, after getting a refcount to it; or
435 * NULL if there is no such phy. The caller is responsible for
436 * calling phy_put() to release that count.
438 struct phy *phy_optional_get(struct device *dev, const char *string)
440 struct phy *phy = phy_get(dev, string);
442 if (PTR_ERR(phy) == -ENODEV)
443 phy = NULL;
445 return phy;
447 EXPORT_SYMBOL_GPL(phy_optional_get);
450 * devm_phy_get() - lookup and obtain a reference to a phy.
451 * @dev: device that requests this phy
452 * @string: the phy name as given in the dt data or phy device name
453 * for non-dt case
455 * Gets the phy using phy_get(), and associates a device with it using
456 * devres. On driver detach, release function is invoked on the devres data,
457 * then, devres data is freed.
459 struct phy *devm_phy_get(struct device *dev, const char *string)
461 struct phy **ptr, *phy;
463 ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
464 if (!ptr)
465 return ERR_PTR(-ENOMEM);
467 phy = phy_get(dev, string);
468 if (!IS_ERR(phy)) {
469 *ptr = phy;
470 devres_add(dev, ptr);
471 } else {
472 devres_free(ptr);
475 return phy;
477 EXPORT_SYMBOL_GPL(devm_phy_get);
480 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
481 * @dev: device that requests this phy
482 * @string: the phy name as given in the dt data or phy device name
483 * for non-dt case
485 * Gets the phy using phy_get(), and associates a device with it using
486 * devres. On driver detach, release function is invoked on the devres
487 * data, then, devres data is freed. This differs to devm_phy_get() in
488 * that if the phy does not exist, it is not considered an error and
489 * -ENODEV will not be returned. Instead the NULL phy is returned,
490 * which can be passed to all other phy consumer calls.
492 struct phy *devm_phy_optional_get(struct device *dev, const char *string)
494 struct phy *phy = devm_phy_get(dev, string);
496 if (PTR_ERR(phy) == -ENODEV)
497 phy = NULL;
499 return phy;
501 EXPORT_SYMBOL_GPL(devm_phy_optional_get);
504 * phy_create() - create a new phy
505 * @dev: device that is creating the new phy
506 * @ops: function pointers for performing phy operations
507 * @init_data: contains the list of PHY consumers or NULL
509 * Called to create a phy using phy framework.
511 struct phy *phy_create(struct device *dev, const struct phy_ops *ops,
512 struct phy_init_data *init_data)
514 int ret;
515 int id;
516 struct phy *phy;
518 if (WARN_ON(!dev))
519 return ERR_PTR(-EINVAL);
521 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
522 if (!phy)
523 return ERR_PTR(-ENOMEM);
525 id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
526 if (id < 0) {
527 dev_err(dev, "unable to get id\n");
528 ret = id;
529 goto free_phy;
532 device_initialize(&phy->dev);
533 mutex_init(&phy->mutex);
535 phy->dev.class = phy_class;
536 phy->dev.parent = dev;
537 phy->dev.of_node = dev->of_node;
538 phy->id = id;
539 phy->ops = ops;
540 phy->init_data = init_data;
542 ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
543 if (ret)
544 goto put_dev;
546 ret = device_add(&phy->dev);
547 if (ret)
548 goto put_dev;
550 if (pm_runtime_enabled(dev)) {
551 pm_runtime_enable(&phy->dev);
552 pm_runtime_no_callbacks(&phy->dev);
555 return phy;
557 put_dev:
558 put_device(&phy->dev);
559 ida_remove(&phy_ida, phy->id);
560 free_phy:
561 kfree(phy);
562 return ERR_PTR(ret);
564 EXPORT_SYMBOL_GPL(phy_create);
567 * devm_phy_create() - create a new phy
568 * @dev: device that is creating the new phy
569 * @ops: function pointers for performing phy operations
570 * @init_data: contains the list of PHY consumers or NULL
572 * Creates a new PHY device adding it to the PHY class.
573 * While at that, it also associates the device with the phy using devres.
574 * On driver detach, release function is invoked on the devres data,
575 * then, devres data is freed.
577 struct phy *devm_phy_create(struct device *dev, const struct phy_ops *ops,
578 struct phy_init_data *init_data)
580 struct phy **ptr, *phy;
582 ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
583 if (!ptr)
584 return ERR_PTR(-ENOMEM);
586 phy = phy_create(dev, ops, init_data);
587 if (!IS_ERR(phy)) {
588 *ptr = phy;
589 devres_add(dev, ptr);
590 } else {
591 devres_free(ptr);
594 return phy;
596 EXPORT_SYMBOL_GPL(devm_phy_create);
599 * phy_destroy() - destroy the phy
600 * @phy: the phy to be destroyed
602 * Called to destroy the phy.
604 void phy_destroy(struct phy *phy)
606 pm_runtime_disable(&phy->dev);
607 device_unregister(&phy->dev);
609 EXPORT_SYMBOL_GPL(phy_destroy);
612 * devm_phy_destroy() - destroy the PHY
613 * @dev: device that wants to release this phy
614 * @phy: the phy returned by devm_phy_get()
616 * destroys the devres associated with this phy and invokes phy_destroy
617 * to destroy the phy.
619 void devm_phy_destroy(struct device *dev, struct phy *phy)
621 int r;
623 r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
624 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
626 EXPORT_SYMBOL_GPL(devm_phy_destroy);
629 * __of_phy_provider_register() - create/register phy provider with the framework
630 * @dev: struct device of the phy provider
631 * @owner: the module owner containing of_xlate
632 * @of_xlate: function pointer to obtain phy instance from phy provider
634 * Creates struct phy_provider from dev and of_xlate function pointer.
635 * This is used in the case of dt boot for finding the phy instance from
636 * phy provider.
638 struct phy_provider *__of_phy_provider_register(struct device *dev,
639 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
640 struct of_phandle_args *args))
642 struct phy_provider *phy_provider;
644 phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
645 if (!phy_provider)
646 return ERR_PTR(-ENOMEM);
648 phy_provider->dev = dev;
649 phy_provider->owner = owner;
650 phy_provider->of_xlate = of_xlate;
652 mutex_lock(&phy_provider_mutex);
653 list_add_tail(&phy_provider->list, &phy_provider_list);
654 mutex_unlock(&phy_provider_mutex);
656 return phy_provider;
658 EXPORT_SYMBOL_GPL(__of_phy_provider_register);
661 * __devm_of_phy_provider_register() - create/register phy provider with the
662 * framework
663 * @dev: struct device of the phy provider
664 * @owner: the module owner containing of_xlate
665 * @of_xlate: function pointer to obtain phy instance from phy provider
667 * Creates struct phy_provider from dev and of_xlate function pointer.
668 * This is used in the case of dt boot for finding the phy instance from
669 * phy provider. While at that, it also associates the device with the
670 * phy provider using devres. On driver detach, release function is invoked
671 * on the devres data, then, devres data is freed.
673 struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
674 struct module *owner, struct phy * (*of_xlate)(struct device *dev,
675 struct of_phandle_args *args))
677 struct phy_provider **ptr, *phy_provider;
679 ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
680 if (!ptr)
681 return ERR_PTR(-ENOMEM);
683 phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
684 if (!IS_ERR(phy_provider)) {
685 *ptr = phy_provider;
686 devres_add(dev, ptr);
687 } else {
688 devres_free(ptr);
691 return phy_provider;
693 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
696 * of_phy_provider_unregister() - unregister phy provider from the framework
697 * @phy_provider: phy provider returned by of_phy_provider_register()
699 * Removes the phy_provider created using of_phy_provider_register().
701 void of_phy_provider_unregister(struct phy_provider *phy_provider)
703 if (IS_ERR(phy_provider))
704 return;
706 mutex_lock(&phy_provider_mutex);
707 list_del(&phy_provider->list);
708 kfree(phy_provider);
709 mutex_unlock(&phy_provider_mutex);
711 EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
714 * devm_of_phy_provider_unregister() - remove phy provider from the framework
715 * @dev: struct device of the phy provider
717 * destroys the devres associated with this phy provider and invokes
718 * of_phy_provider_unregister to unregister the phy provider.
720 void devm_of_phy_provider_unregister(struct device *dev,
721 struct phy_provider *phy_provider) {
722 int r;
724 r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
725 phy_provider);
726 dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
728 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
731 * phy_release() - release the phy
732 * @dev: the dev member within phy
734 * When the last reference to the device is removed, it is called
735 * from the embedded kobject as release method.
737 static void phy_release(struct device *dev)
739 struct phy *phy;
741 phy = to_phy(dev);
742 dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
743 ida_remove(&phy_ida, phy->id);
744 kfree(phy);
747 static int __init phy_core_init(void)
749 phy_class = class_create(THIS_MODULE, "phy");
750 if (IS_ERR(phy_class)) {
751 pr_err("failed to create phy class --> %ld\n",
752 PTR_ERR(phy_class));
753 return PTR_ERR(phy_class);
756 phy_class->dev_release = phy_release;
758 return 0;
760 module_init(phy_core_init);
762 static void __exit phy_core_exit(void)
764 class_destroy(phy_class);
766 module_exit(phy_core_exit);
768 MODULE_DESCRIPTION("Generic PHY Framework");
769 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
770 MODULE_LICENSE("GPL v2");