1 // SPDX-License-Identifier: GPL-2.0+
3 * A wrapper for multiple PHYs which passes all phy_* function calls to
4 * multiple (actual) PHY devices. This is comes handy when initializing
5 * all PHYs on a HCD and to keep them all in the same state.
7 * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
10 #include <linux/device.h>
11 #include <linux/list.h>
12 #include <linux/phy/phy.h>
17 struct usb_phy_roothub {
19 struct list_head list;
22 static int usb_phy_roothub_add_phy(struct device *dev, int index,
23 struct list_head *list)
25 struct usb_phy_roothub *roothub_entry;
26 struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
28 if (IS_ERR_OR_NULL(phy)) {
29 if (!phy || PTR_ERR(phy) == -ENODEV)
35 roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
39 INIT_LIST_HEAD(&roothub_entry->list);
41 roothub_entry->phy = phy;
43 list_add_tail(&roothub_entry->list, list);
48 struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
50 struct usb_phy_roothub *phy_roothub;
53 num_phys = of_count_phandle_with_args(dev->of_node, "phys",
58 phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
60 return ERR_PTR(-ENOMEM);
62 INIT_LIST_HEAD(&phy_roothub->list);
64 for (i = 0; i < num_phys; i++) {
65 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
72 EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
74 int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
76 struct usb_phy_roothub *roothub_entry;
77 struct list_head *head;
83 head = &phy_roothub->list;
85 list_for_each_entry(roothub_entry, head, list) {
86 err = phy_init(roothub_entry->phy);
94 list_for_each_entry_continue_reverse(roothub_entry, head, list)
95 phy_exit(roothub_entry->phy);
99 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
101 int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
103 struct usb_phy_roothub *roothub_entry;
104 struct list_head *head;
110 head = &phy_roothub->list;
112 list_for_each_entry(roothub_entry, head, list) {
113 err = phy_exit(roothub_entry->phy);
120 EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
122 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
124 struct usb_phy_roothub *roothub_entry;
125 struct list_head *head;
131 head = &phy_roothub->list;
133 list_for_each_entry(roothub_entry, head, list) {
134 err = phy_power_on(roothub_entry->phy);
142 list_for_each_entry_continue_reverse(roothub_entry, head, list)
143 phy_power_off(roothub_entry->phy);
147 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
149 void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
151 struct usb_phy_roothub *roothub_entry;
156 list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
157 phy_power_off(roothub_entry->phy);
159 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);