1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * phy-core.c -- Generic Phy framework.
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
7 * Author: Kishon Vijay Abraham I <kishon@ti.com>
10 #include <linux/kernel.h>
11 #include <linux/export.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/debugfs.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
18 #include <linux/phy/phy.h>
19 #include <linux/idr.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
23 static void phy_release(struct device
*dev
);
24 static const struct class phy_class
= {
26 .dev_release
= phy_release
,
29 static struct dentry
*phy_debugfs_root
;
30 static DEFINE_MUTEX(phy_provider_mutex
);
31 static LIST_HEAD(phy_provider_list
);
32 static LIST_HEAD(phys
);
33 static DEFINE_IDA(phy_ida
);
35 static void devm_phy_release(struct device
*dev
, void *res
)
37 struct phy
*phy
= *(struct phy
**)res
;
42 static void devm_phy_provider_release(struct device
*dev
, void *res
)
44 struct phy_provider
*phy_provider
= *(struct phy_provider
**)res
;
46 of_phy_provider_unregister(phy_provider
);
49 static void devm_phy_consume(struct device
*dev
, void *res
)
51 struct phy
*phy
= *(struct phy
**)res
;
56 static int devm_phy_match(struct device
*dev
, void *res
, void *match_data
)
58 struct phy
**phy
= res
;
60 return *phy
== match_data
;
64 * phy_create_lookup() - allocate and register PHY/device association
65 * @phy: the phy of the association
66 * @con_id: connection ID string on device
67 * @dev_id: the device of the association
69 * Creates and registers phy_lookup entry.
71 int phy_create_lookup(struct phy
*phy
, const char *con_id
, const char *dev_id
)
73 struct phy_lookup
*pl
;
75 if (!phy
|| !dev_id
|| !con_id
)
78 pl
= kzalloc(sizeof(*pl
), GFP_KERNEL
);
86 mutex_lock(&phy_provider_mutex
);
87 list_add_tail(&pl
->node
, &phys
);
88 mutex_unlock(&phy_provider_mutex
);
92 EXPORT_SYMBOL_GPL(phy_create_lookup
);
95 * phy_remove_lookup() - find and remove PHY/device association
96 * @phy: the phy of the association
97 * @con_id: connection ID string on device
98 * @dev_id: the device of the association
100 * Finds and unregisters phy_lookup entry that was created with
101 * phy_create_lookup().
103 void phy_remove_lookup(struct phy
*phy
, const char *con_id
, const char *dev_id
)
105 struct phy_lookup
*pl
;
107 if (!phy
|| !dev_id
|| !con_id
)
110 mutex_lock(&phy_provider_mutex
);
111 list_for_each_entry(pl
, &phys
, node
)
112 if (pl
->phy
== phy
&& !strcmp(pl
->dev_id
, dev_id
) &&
113 !strcmp(pl
->con_id
, con_id
)) {
118 mutex_unlock(&phy_provider_mutex
);
120 EXPORT_SYMBOL_GPL(phy_remove_lookup
);
122 static struct phy
*phy_find(struct device
*dev
, const char *con_id
)
124 const char *dev_id
= dev_name(dev
);
125 struct phy_lookup
*p
, *pl
= NULL
;
127 mutex_lock(&phy_provider_mutex
);
128 list_for_each_entry(p
, &phys
, node
)
129 if (!strcmp(p
->dev_id
, dev_id
) && !strcmp(p
->con_id
, con_id
)) {
133 mutex_unlock(&phy_provider_mutex
);
135 return pl
? pl
->phy
: ERR_PTR(-ENODEV
);
138 static struct phy_provider
*of_phy_provider_lookup(struct device_node
*node
)
140 struct phy_provider
*phy_provider
;
141 struct device_node
*child
;
143 list_for_each_entry(phy_provider
, &phy_provider_list
, list
) {
144 if (phy_provider
->dev
->of_node
== node
)
147 for_each_child_of_node(phy_provider
->children
, child
)
152 return ERR_PTR(-EPROBE_DEFER
);
155 int phy_pm_runtime_get(struct phy
*phy
)
162 if (!pm_runtime_enabled(&phy
->dev
))
165 ret
= pm_runtime_get(&phy
->dev
);
166 if (ret
< 0 && ret
!= -EINPROGRESS
)
167 pm_runtime_put_noidle(&phy
->dev
);
171 EXPORT_SYMBOL_GPL(phy_pm_runtime_get
);
173 int phy_pm_runtime_get_sync(struct phy
*phy
)
180 if (!pm_runtime_enabled(&phy
->dev
))
183 ret
= pm_runtime_get_sync(&phy
->dev
);
185 pm_runtime_put_sync(&phy
->dev
);
189 EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync
);
191 int phy_pm_runtime_put(struct phy
*phy
)
196 if (!pm_runtime_enabled(&phy
->dev
))
199 return pm_runtime_put(&phy
->dev
);
201 EXPORT_SYMBOL_GPL(phy_pm_runtime_put
);
203 int phy_pm_runtime_put_sync(struct phy
*phy
)
208 if (!pm_runtime_enabled(&phy
->dev
))
211 return pm_runtime_put_sync(&phy
->dev
);
213 EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync
);
215 void phy_pm_runtime_allow(struct phy
*phy
)
220 if (!pm_runtime_enabled(&phy
->dev
))
223 pm_runtime_allow(&phy
->dev
);
225 EXPORT_SYMBOL_GPL(phy_pm_runtime_allow
);
227 void phy_pm_runtime_forbid(struct phy
*phy
)
232 if (!pm_runtime_enabled(&phy
->dev
))
235 pm_runtime_forbid(&phy
->dev
);
237 EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid
);
240 * phy_init - phy internal initialization before phy operation
241 * @phy: the phy returned by phy_get()
243 * Used to allow phy's driver to perform phy internal initialization,
244 * such as PLL block powering, clock initialization or anything that's
245 * is required by the phy to perform the start of operation.
246 * Must be called before phy_power_on().
248 * Return: %0 if successful, a negative error code otherwise
250 int phy_init(struct phy
*phy
)
257 ret
= phy_pm_runtime_get_sync(phy
);
258 if (ret
< 0 && ret
!= -ENOTSUPP
)
260 ret
= 0; /* Override possible ret == -ENOTSUPP */
262 mutex_lock(&phy
->mutex
);
263 if (phy
->power_count
> phy
->init_count
)
264 dev_warn(&phy
->dev
, "phy_power_on was called before phy_init\n");
266 if (phy
->init_count
== 0 && phy
->ops
->init
) {
267 ret
= phy
->ops
->init(phy
);
269 dev_err(&phy
->dev
, "phy init failed --> %d\n", ret
);
276 mutex_unlock(&phy
->mutex
);
277 phy_pm_runtime_put(phy
);
280 EXPORT_SYMBOL_GPL(phy_init
);
283 * phy_exit - Phy internal un-initialization
284 * @phy: the phy returned by phy_get()
286 * Must be called after phy_power_off().
288 * Return: %0 if successful, a negative error code otherwise
290 int phy_exit(struct phy
*phy
)
297 ret
= phy_pm_runtime_get_sync(phy
);
298 if (ret
< 0 && ret
!= -ENOTSUPP
)
300 ret
= 0; /* Override possible ret == -ENOTSUPP */
302 mutex_lock(&phy
->mutex
);
303 if (phy
->init_count
== 1 && phy
->ops
->exit
) {
304 ret
= phy
->ops
->exit(phy
);
306 dev_err(&phy
->dev
, "phy exit failed --> %d\n", ret
);
313 mutex_unlock(&phy
->mutex
);
314 phy_pm_runtime_put(phy
);
317 EXPORT_SYMBOL_GPL(phy_exit
);
320 * phy_power_on - Enable the phy and enter proper operation
321 * @phy: the phy returned by phy_get()
323 * Must be called after phy_init().
325 * Return: %0 if successful, a negative error code otherwise
327 int phy_power_on(struct phy
*phy
)
335 ret
= regulator_enable(phy
->pwr
);
340 ret
= phy_pm_runtime_get_sync(phy
);
341 if (ret
< 0 && ret
!= -ENOTSUPP
)
344 ret
= 0; /* Override possible ret == -ENOTSUPP */
346 mutex_lock(&phy
->mutex
);
347 if (phy
->power_count
== 0 && phy
->ops
->power_on
) {
348 ret
= phy
->ops
->power_on(phy
);
350 dev_err(&phy
->dev
, "phy poweron failed --> %d\n", ret
);
355 mutex_unlock(&phy
->mutex
);
359 mutex_unlock(&phy
->mutex
);
360 phy_pm_runtime_put_sync(phy
);
363 regulator_disable(phy
->pwr
);
367 EXPORT_SYMBOL_GPL(phy_power_on
);
370 * phy_power_off - Disable the phy.
371 * @phy: the phy returned by phy_get()
373 * Must be called before phy_exit().
375 * Return: %0 if successful, a negative error code otherwise
377 int phy_power_off(struct phy
*phy
)
384 mutex_lock(&phy
->mutex
);
385 if (phy
->power_count
== 1 && phy
->ops
->power_off
) {
386 ret
= phy
->ops
->power_off(phy
);
388 dev_err(&phy
->dev
, "phy poweroff failed --> %d\n", ret
);
389 mutex_unlock(&phy
->mutex
);
394 mutex_unlock(&phy
->mutex
);
395 phy_pm_runtime_put(phy
);
398 regulator_disable(phy
->pwr
);
402 EXPORT_SYMBOL_GPL(phy_power_off
);
404 int phy_set_mode_ext(struct phy
*phy
, enum phy_mode mode
, int submode
)
408 if (!phy
|| !phy
->ops
->set_mode
)
411 mutex_lock(&phy
->mutex
);
412 ret
= phy
->ops
->set_mode(phy
, mode
, submode
);
414 phy
->attrs
.mode
= mode
;
415 mutex_unlock(&phy
->mutex
);
419 EXPORT_SYMBOL_GPL(phy_set_mode_ext
);
421 int phy_set_media(struct phy
*phy
, enum phy_media media
)
425 if (!phy
|| !phy
->ops
->set_media
)
428 mutex_lock(&phy
->mutex
);
429 ret
= phy
->ops
->set_media(phy
, media
);
430 mutex_unlock(&phy
->mutex
);
434 EXPORT_SYMBOL_GPL(phy_set_media
);
436 int phy_set_speed(struct phy
*phy
, int speed
)
440 if (!phy
|| !phy
->ops
->set_speed
)
443 mutex_lock(&phy
->mutex
);
444 ret
= phy
->ops
->set_speed(phy
, speed
);
445 mutex_unlock(&phy
->mutex
);
449 EXPORT_SYMBOL_GPL(phy_set_speed
);
451 int phy_reset(struct phy
*phy
)
455 if (!phy
|| !phy
->ops
->reset
)
458 ret
= phy_pm_runtime_get_sync(phy
);
459 if (ret
< 0 && ret
!= -ENOTSUPP
)
462 mutex_lock(&phy
->mutex
);
463 ret
= phy
->ops
->reset(phy
);
464 mutex_unlock(&phy
->mutex
);
466 phy_pm_runtime_put(phy
);
470 EXPORT_SYMBOL_GPL(phy_reset
);
473 * phy_calibrate() - Tunes the phy hw parameters for current configuration
474 * @phy: the phy returned by phy_get()
476 * Used to calibrate phy hardware, typically by adjusting some parameters in
477 * runtime, which are otherwise lost after host controller reset and cannot
478 * be applied in phy_init() or phy_power_on().
480 * Return: %0 if successful, a negative error code otherwise
482 int phy_calibrate(struct phy
*phy
)
486 if (!phy
|| !phy
->ops
->calibrate
)
489 mutex_lock(&phy
->mutex
);
490 ret
= phy
->ops
->calibrate(phy
);
491 mutex_unlock(&phy
->mutex
);
495 EXPORT_SYMBOL_GPL(phy_calibrate
);
498 * phy_notify_connect() - phy connect notification
499 * @phy: the phy returned by phy_get()
500 * @port: the port index for connect
502 * If the phy needs to get connection status, the callback can be used.
503 * Returns: %0 if successful, a negative error code otherwise
505 int phy_notify_connect(struct phy
*phy
, int port
)
509 if (!phy
|| !phy
->ops
->connect
)
512 mutex_lock(&phy
->mutex
);
513 ret
= phy
->ops
->connect(phy
, port
);
514 mutex_unlock(&phy
->mutex
);
518 EXPORT_SYMBOL_GPL(phy_notify_connect
);
521 * phy_notify_disconnect() - phy disconnect notification
522 * @phy: the phy returned by phy_get()
523 * @port: the port index for disconnect
525 * If the phy needs to get connection status, the callback can be used.
527 * Returns: %0 if successful, a negative error code otherwise
529 int phy_notify_disconnect(struct phy
*phy
, int port
)
533 if (!phy
|| !phy
->ops
->disconnect
)
536 mutex_lock(&phy
->mutex
);
537 ret
= phy
->ops
->disconnect(phy
, port
);
538 mutex_unlock(&phy
->mutex
);
542 EXPORT_SYMBOL_GPL(phy_notify_disconnect
);
545 * phy_configure() - Changes the phy parameters
546 * @phy: the phy returned by phy_get()
547 * @opts: New configuration to apply
549 * Used to change the PHY parameters. phy_init() must have been called
550 * on the phy. The configuration will be applied on the current phy
551 * mode, that can be changed using phy_set_mode().
553 * Return: %0 if successful, a negative error code otherwise
555 int phy_configure(struct phy
*phy
, union phy_configure_opts
*opts
)
562 if (!phy
->ops
->configure
)
565 mutex_lock(&phy
->mutex
);
566 ret
= phy
->ops
->configure(phy
, opts
);
567 mutex_unlock(&phy
->mutex
);
571 EXPORT_SYMBOL_GPL(phy_configure
);
574 * phy_validate() - Checks the phy parameters
575 * @phy: the phy returned by phy_get()
576 * @mode: phy_mode the configuration is applicable to.
577 * @submode: PHY submode the configuration is applicable to.
578 * @opts: Configuration to check
580 * Used to check that the current set of parameters can be handled by
581 * the phy. Implementations are free to tune the parameters passed as
582 * arguments if needed by some implementation detail or
583 * constraints. It will not change any actual configuration of the
584 * PHY, so calling it as many times as deemed fit will have no side
587 * Return: %0 if successful, a negative error code otherwise
589 int phy_validate(struct phy
*phy
, enum phy_mode mode
, int submode
,
590 union phy_configure_opts
*opts
)
597 if (!phy
->ops
->validate
)
600 mutex_lock(&phy
->mutex
);
601 ret
= phy
->ops
->validate(phy
, mode
, submode
, opts
);
602 mutex_unlock(&phy
->mutex
);
606 EXPORT_SYMBOL_GPL(phy_validate
);
609 * _of_phy_get() - lookup and obtain a reference to a phy by phandle
610 * @np: device_node for which to get the phy
611 * @index: the index of the phy
613 * Returns the phy associated with the given phandle value,
614 * after getting a refcount to it or -ENODEV if there is no such phy or
615 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
616 * not yet loaded. This function uses of_xlate call back function provided
617 * while registering the phy_provider to find the phy instance.
619 static struct phy
*_of_phy_get(struct device_node
*np
, int index
)
622 struct phy_provider
*phy_provider
;
623 struct phy
*phy
= NULL
;
624 struct of_phandle_args args
;
626 ret
= of_parse_phandle_with_args(np
, "phys", "#phy-cells",
629 return ERR_PTR(-ENODEV
);
631 /* This phy type handled by the usb-phy subsystem for now */
632 if (of_device_is_compatible(args
.np
, "usb-nop-xceiv"))
633 return ERR_PTR(-ENODEV
);
635 mutex_lock(&phy_provider_mutex
);
636 phy_provider
= of_phy_provider_lookup(args
.np
);
637 if (IS_ERR(phy_provider
) || !try_module_get(phy_provider
->owner
)) {
638 phy
= ERR_PTR(-EPROBE_DEFER
);
642 if (!of_device_is_available(args
.np
)) {
643 dev_warn(phy_provider
->dev
, "Requested PHY is disabled\n");
644 phy
= ERR_PTR(-ENODEV
);
648 phy
= phy_provider
->of_xlate(phy_provider
->dev
, &args
);
651 module_put(phy_provider
->owner
);
654 mutex_unlock(&phy_provider_mutex
);
655 of_node_put(args
.np
);
661 * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
662 * @np: device_node for which to get the phy
663 * @con_id: name of the phy from device's point of view
665 * Returns the phy driver, after getting a refcount to it; or
666 * -ENODEV if there is no such phy. The caller is responsible for
667 * calling of_phy_put() to release that count.
669 struct phy
*of_phy_get(struct device_node
*np
, const char *con_id
)
671 struct phy
*phy
= NULL
;
675 index
= of_property_match_string(np
, "phy-names", con_id
);
677 phy
= _of_phy_get(np
, index
);
681 if (!try_module_get(phy
->ops
->owner
))
682 return ERR_PTR(-EPROBE_DEFER
);
684 get_device(&phy
->dev
);
688 EXPORT_SYMBOL_GPL(of_phy_get
);
691 * of_phy_put() - release the PHY
692 * @phy: the phy returned by of_phy_get()
694 * Releases a refcount the caller received from of_phy_get().
696 void of_phy_put(struct phy
*phy
)
698 if (!phy
|| IS_ERR(phy
))
701 mutex_lock(&phy
->mutex
);
702 if (phy
->ops
->release
)
703 phy
->ops
->release(phy
);
704 mutex_unlock(&phy
->mutex
);
706 module_put(phy
->ops
->owner
);
707 put_device(&phy
->dev
);
709 EXPORT_SYMBOL_GPL(of_phy_put
);
712 * phy_put() - release the PHY
713 * @dev: device that wants to release this phy
714 * @phy: the phy returned by phy_get()
716 * Releases a refcount the caller received from phy_get().
718 void phy_put(struct device
*dev
, struct phy
*phy
)
720 device_link_remove(dev
, &phy
->dev
);
723 EXPORT_SYMBOL_GPL(phy_put
);
726 * devm_phy_put() - release the PHY
727 * @dev: device that wants to release this phy
728 * @phy: the phy returned by devm_phy_get()
730 * destroys the devres associated with this phy and invokes phy_put
731 * to release the phy.
733 void devm_phy_put(struct device
*dev
, struct phy
*phy
)
740 r
= devres_destroy(dev
, devm_phy_release
, devm_phy_match
, phy
);
741 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
743 EXPORT_SYMBOL_GPL(devm_phy_put
);
746 * of_phy_simple_xlate() - returns the phy instance from phy provider
747 * @dev: the PHY provider device
748 * @args: of_phandle_args (not used here)
750 * Intended to be used by phy provider for the common case where #phy-cells is
751 * 0. For other cases where #phy-cells is greater than '0', the phy provider
752 * should provide a custom of_xlate function that reads the *args* and returns
753 * the appropriate phy.
755 struct phy
*of_phy_simple_xlate(struct device
*dev
,
756 const struct of_phandle_args
*args
)
759 struct class_dev_iter iter
;
761 class_dev_iter_init(&iter
, &phy_class
, NULL
, NULL
);
762 while ((dev
= class_dev_iter_next(&iter
))) {
764 if (args
->np
!= phy
->dev
.of_node
)
767 class_dev_iter_exit(&iter
);
771 class_dev_iter_exit(&iter
);
772 return ERR_PTR(-ENODEV
);
774 EXPORT_SYMBOL_GPL(of_phy_simple_xlate
);
777 * phy_get() - lookup and obtain a reference to a phy.
778 * @dev: device that requests this phy
779 * @string: the phy name as given in the dt data or the name of the controller
780 * port for non-dt case
782 * Returns the phy driver, after getting a refcount to it; or
783 * -ENODEV if there is no such phy. The caller is responsible for
784 * calling phy_put() to release that count.
786 struct phy
*phy_get(struct device
*dev
, const char *string
)
790 struct device_link
*link
;
794 index
= of_property_match_string(dev
->of_node
, "phy-names",
798 phy
= _of_phy_get(dev
->of_node
, index
);
800 if (string
== NULL
) {
801 dev_WARN(dev
, "missing string\n");
802 return ERR_PTR(-EINVAL
);
804 phy
= phy_find(dev
, string
);
809 if (!try_module_get(phy
->ops
->owner
))
810 return ERR_PTR(-EPROBE_DEFER
);
812 get_device(&phy
->dev
);
814 link
= device_link_add(dev
, &phy
->dev
, DL_FLAG_STATELESS
);
816 dev_dbg(dev
, "failed to create device link to %s\n",
817 dev_name(phy
->dev
.parent
));
821 EXPORT_SYMBOL_GPL(phy_get
);
824 * devm_phy_get() - lookup and obtain a reference to a phy.
825 * @dev: device that requests this phy
826 * @string: the phy name as given in the dt data or phy device name
829 * Gets the phy using phy_get(), and associates a device with it using
830 * devres. On driver detach, release function is invoked on the devres data,
831 * then, devres data is freed.
833 struct phy
*devm_phy_get(struct device
*dev
, const char *string
)
835 struct phy
**ptr
, *phy
;
837 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
839 return ERR_PTR(-ENOMEM
);
841 phy
= phy_get(dev
, string
);
844 devres_add(dev
, ptr
);
851 EXPORT_SYMBOL_GPL(devm_phy_get
);
854 * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
855 * @dev: device that requests this phy
856 * @string: the phy name as given in the dt data or phy device name
859 * Gets the phy using phy_get(), and associates a device with it using
860 * devres. On driver detach, release function is invoked on the devres
861 * data, then, devres data is freed. This differs to devm_phy_get() in
862 * that if the phy does not exist, it is not considered an error and
863 * -ENODEV will not be returned. Instead the NULL phy is returned,
864 * which can be passed to all other phy consumer calls.
866 struct phy
*devm_phy_optional_get(struct device
*dev
, const char *string
)
868 struct phy
*phy
= devm_phy_get(dev
, string
);
870 if (PTR_ERR(phy
) == -ENODEV
)
875 EXPORT_SYMBOL_GPL(devm_phy_optional_get
);
878 * devm_of_phy_get() - lookup and obtain a reference to a phy.
879 * @dev: device that requests this phy
880 * @np: node containing the phy
881 * @con_id: name of the phy from device's point of view
883 * Gets the phy using of_phy_get(), and associates a device with it using
884 * devres. On driver detach, release function is invoked on the devres data,
885 * then, devres data is freed.
887 struct phy
*devm_of_phy_get(struct device
*dev
, struct device_node
*np
,
890 struct phy
**ptr
, *phy
;
891 struct device_link
*link
;
893 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
895 return ERR_PTR(-ENOMEM
);
897 phy
= of_phy_get(np
, con_id
);
900 devres_add(dev
, ptr
);
906 link
= device_link_add(dev
, &phy
->dev
, DL_FLAG_STATELESS
);
908 dev_dbg(dev
, "failed to create device link to %s\n",
909 dev_name(phy
->dev
.parent
));
913 EXPORT_SYMBOL_GPL(devm_of_phy_get
);
916 * devm_of_phy_optional_get() - lookup and obtain a reference to an optional
918 * @dev: device that requests this phy
919 * @np: node containing the phy
920 * @con_id: name of the phy from device's point of view
922 * Gets the phy using of_phy_get(), and associates a device with it using
923 * devres. On driver detach, release function is invoked on the devres data,
924 * then, devres data is freed. This differs to devm_of_phy_get() in
925 * that if the phy does not exist, it is not considered an error and
926 * -ENODEV will not be returned. Instead the NULL phy is returned,
927 * which can be passed to all other phy consumer calls.
929 struct phy
*devm_of_phy_optional_get(struct device
*dev
, struct device_node
*np
,
932 struct phy
*phy
= devm_of_phy_get(dev
, np
, con_id
);
934 if (PTR_ERR(phy
) == -ENODEV
)
938 dev_err_probe(dev
, PTR_ERR(phy
), "failed to get PHY %pOF:%s",
943 EXPORT_SYMBOL_GPL(devm_of_phy_optional_get
);
946 * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
947 * @dev: device that requests this phy
948 * @np: node containing the phy
949 * @index: index of the phy
951 * Gets the phy using _of_phy_get(), then gets a refcount to it,
952 * and associates a device with it using devres. On driver detach,
953 * release function is invoked on the devres data,
954 * then, devres data is freed.
957 struct phy
*devm_of_phy_get_by_index(struct device
*dev
, struct device_node
*np
,
960 struct phy
**ptr
, *phy
;
961 struct device_link
*link
;
963 ptr
= devres_alloc(devm_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
965 return ERR_PTR(-ENOMEM
);
967 phy
= _of_phy_get(np
, index
);
973 if (!try_module_get(phy
->ops
->owner
)) {
975 return ERR_PTR(-EPROBE_DEFER
);
978 get_device(&phy
->dev
);
981 devres_add(dev
, ptr
);
983 link
= device_link_add(dev
, &phy
->dev
, DL_FLAG_STATELESS
);
985 dev_dbg(dev
, "failed to create device link to %s\n",
986 dev_name(phy
->dev
.parent
));
990 EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index
);
993 * phy_create() - create a new phy
994 * @dev: device that is creating the new phy
995 * @node: device node of the phy
996 * @ops: function pointers for performing phy operations
998 * Called to create a phy using phy framework.
1000 struct phy
*phy_create(struct device
*dev
, struct device_node
*node
,
1001 const struct phy_ops
*ops
)
1008 return ERR_PTR(-EINVAL
);
1010 phy
= kzalloc(sizeof(*phy
), GFP_KERNEL
);
1012 return ERR_PTR(-ENOMEM
);
1014 id
= ida_alloc(&phy_ida
, GFP_KERNEL
);
1016 dev_err(dev
, "unable to get id\n");
1021 device_initialize(&phy
->dev
);
1022 mutex_init(&phy
->mutex
);
1024 phy
->dev
.class = &phy_class
;
1025 phy
->dev
.parent
= dev
;
1026 phy
->dev
.of_node
= node
?: dev
->of_node
;
1030 ret
= dev_set_name(&phy
->dev
, "phy-%s.%d", dev_name(dev
), id
);
1035 phy
->pwr
= regulator_get_optional(&phy
->dev
, "phy");
1036 if (IS_ERR(phy
->pwr
)) {
1037 ret
= PTR_ERR(phy
->pwr
);
1038 if (ret
== -EPROBE_DEFER
)
1044 ret
= device_add(&phy
->dev
);
1048 if (pm_runtime_enabled(dev
)) {
1049 pm_runtime_enable(&phy
->dev
);
1050 pm_runtime_no_callbacks(&phy
->dev
);
1053 phy
->debugfs
= debugfs_create_dir(dev_name(&phy
->dev
), phy_debugfs_root
);
1058 put_device(&phy
->dev
); /* calls phy_release() which frees resources */
1059 return ERR_PTR(ret
);
1063 return ERR_PTR(ret
);
1065 EXPORT_SYMBOL_GPL(phy_create
);
1068 * devm_phy_create() - create a new phy
1069 * @dev: device that is creating the new phy
1070 * @node: device node of the phy
1071 * @ops: function pointers for performing phy operations
1073 * Creates a new PHY device adding it to the PHY class.
1074 * While at that, it also associates the device with the phy using devres.
1075 * On driver detach, release function is invoked on the devres data,
1076 * then, devres data is freed.
1078 struct phy
*devm_phy_create(struct device
*dev
, struct device_node
*node
,
1079 const struct phy_ops
*ops
)
1081 struct phy
**ptr
, *phy
;
1083 ptr
= devres_alloc(devm_phy_consume
, sizeof(*ptr
), GFP_KERNEL
);
1085 return ERR_PTR(-ENOMEM
);
1087 phy
= phy_create(dev
, node
, ops
);
1090 devres_add(dev
, ptr
);
1097 EXPORT_SYMBOL_GPL(devm_phy_create
);
1100 * phy_destroy() - destroy the phy
1101 * @phy: the phy to be destroyed
1103 * Called to destroy the phy.
1105 void phy_destroy(struct phy
*phy
)
1107 pm_runtime_disable(&phy
->dev
);
1108 device_unregister(&phy
->dev
);
1110 EXPORT_SYMBOL_GPL(phy_destroy
);
1113 * devm_phy_destroy() - destroy the PHY
1114 * @dev: device that wants to release this phy
1115 * @phy: the phy returned by devm_phy_get()
1117 * destroys the devres associated with this phy and invokes phy_destroy
1118 * to destroy the phy.
1120 void devm_phy_destroy(struct device
*dev
, struct phy
*phy
)
1124 r
= devres_destroy(dev
, devm_phy_consume
, devm_phy_match
, phy
);
1125 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
1127 EXPORT_SYMBOL_GPL(devm_phy_destroy
);
1130 * __of_phy_provider_register() - create/register phy provider with the framework
1131 * @dev: struct device of the phy provider
1132 * @children: device node containing children (if different from dev->of_node)
1133 * @owner: the module owner containing of_xlate
1134 * @of_xlate: function pointer to obtain phy instance from phy provider
1136 * Creates struct phy_provider from dev and of_xlate function pointer.
1137 * This is used in the case of dt boot for finding the phy instance from
1140 * If the PHY provider doesn't nest children directly but uses a separate
1141 * child node to contain the individual children, the @children parameter
1142 * can be used to override the default. If NULL, the default (dev->of_node)
1143 * will be used. If non-NULL, the device node must be a child (or further
1144 * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
1145 * error code is returned.
1147 struct phy_provider
*__of_phy_provider_register(struct device
*dev
,
1148 struct device_node
*children
, struct module
*owner
,
1149 struct phy
* (*of_xlate
)(struct device
*dev
,
1150 const struct of_phandle_args
*args
))
1152 struct phy_provider
*phy_provider
;
1155 * If specified, the device node containing the children must itself
1156 * be the provider's device node or a child (or further descendant)
1160 struct device_node
*parent
= of_node_get(children
), *next
;
1163 if (parent
== dev
->of_node
)
1166 next
= of_get_parent(parent
);
1167 of_node_put(parent
);
1172 return ERR_PTR(-EINVAL
);
1174 of_node_put(parent
);
1176 children
= dev
->of_node
;
1179 phy_provider
= kzalloc(sizeof(*phy_provider
), GFP_KERNEL
);
1181 return ERR_PTR(-ENOMEM
);
1183 phy_provider
->dev
= dev
;
1184 phy_provider
->children
= of_node_get(children
);
1185 phy_provider
->owner
= owner
;
1186 phy_provider
->of_xlate
= of_xlate
;
1188 mutex_lock(&phy_provider_mutex
);
1189 list_add_tail(&phy_provider
->list
, &phy_provider_list
);
1190 mutex_unlock(&phy_provider_mutex
);
1192 return phy_provider
;
1194 EXPORT_SYMBOL_GPL(__of_phy_provider_register
);
1197 * __devm_of_phy_provider_register() - create/register phy provider with the
1199 * @dev: struct device of the phy provider
1200 * @children: device node containing children (if different from dev->of_node)
1201 * @owner: the module owner containing of_xlate
1202 * @of_xlate: function pointer to obtain phy instance from phy provider
1204 * Creates struct phy_provider from dev and of_xlate function pointer.
1205 * This is used in the case of dt boot for finding the phy instance from
1206 * phy provider. While at that, it also associates the device with the
1207 * phy provider using devres. On driver detach, release function is invoked
1208 * on the devres data, then, devres data is freed.
1210 struct phy_provider
*__devm_of_phy_provider_register(struct device
*dev
,
1211 struct device_node
*children
, struct module
*owner
,
1212 struct phy
* (*of_xlate
)(struct device
*dev
,
1213 const struct of_phandle_args
*args
))
1215 struct phy_provider
**ptr
, *phy_provider
;
1217 ptr
= devres_alloc(devm_phy_provider_release
, sizeof(*ptr
), GFP_KERNEL
);
1219 return ERR_PTR(-ENOMEM
);
1221 phy_provider
= __of_phy_provider_register(dev
, children
, owner
,
1223 if (!IS_ERR(phy_provider
)) {
1224 *ptr
= phy_provider
;
1225 devres_add(dev
, ptr
);
1230 return phy_provider
;
1232 EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register
);
1235 * of_phy_provider_unregister() - unregister phy provider from the framework
1236 * @phy_provider: phy provider returned by of_phy_provider_register()
1238 * Removes the phy_provider created using of_phy_provider_register().
1240 void of_phy_provider_unregister(struct phy_provider
*phy_provider
)
1242 if (IS_ERR(phy_provider
))
1245 mutex_lock(&phy_provider_mutex
);
1246 list_del(&phy_provider
->list
);
1247 of_node_put(phy_provider
->children
);
1248 kfree(phy_provider
);
1249 mutex_unlock(&phy_provider_mutex
);
1251 EXPORT_SYMBOL_GPL(of_phy_provider_unregister
);
1254 * devm_of_phy_provider_unregister() - remove phy provider from the framework
1255 * @dev: struct device of the phy provider
1256 * @phy_provider: phy provider returned by of_phy_provider_register()
1258 * destroys the devres associated with this phy provider and invokes
1259 * of_phy_provider_unregister to unregister the phy provider.
1261 void devm_of_phy_provider_unregister(struct device
*dev
,
1262 struct phy_provider
*phy_provider
)
1266 r
= devres_destroy(dev
, devm_phy_provider_release
, devm_phy_match
,
1268 dev_WARN_ONCE(dev
, r
, "couldn't find PHY provider device resource\n");
1270 EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister
);
1273 * phy_release() - release the phy
1274 * @dev: the dev member within phy
1276 * When the last reference to the device is removed, it is called
1277 * from the embedded kobject as release method.
1279 static void phy_release(struct device
*dev
)
1284 dev_vdbg(dev
, "releasing '%s'\n", dev_name(dev
));
1285 debugfs_remove_recursive(phy
->debugfs
);
1286 regulator_put(phy
->pwr
);
1287 ida_free(&phy_ida
, phy
->id
);
1291 static int __init
phy_core_init(void)
1295 err
= class_register(&phy_class
);
1297 pr_err("failed to register phy class");
1301 phy_debugfs_root
= debugfs_create_dir("phy", NULL
);
1305 device_initcall(phy_core_init
);
1307 static void __exit
phy_core_exit(void)
1309 debugfs_remove_recursive(phy_debugfs_root
);
1310 class_unregister(&phy_class
);
1312 module_exit(phy_core_exit
);