]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/usb/core/phy.c
usb: core: split usb_phy_roothub_{init,alloc}
[linux.git] / drivers / usb / core / phy.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
6  *
7  * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
8  */
9
10 #include <linux/device.h>
11 #include <linux/list.h>
12 #include <linux/phy/phy.h>
13 #include <linux/of.h>
14
15 #include "phy.h"
16
17 struct usb_phy_roothub {
18         struct phy              *phy;
19         struct list_head        list;
20 };
21
22 static int usb_phy_roothub_add_phy(struct device *dev, int index,
23                                    struct list_head *list)
24 {
25         struct usb_phy_roothub *roothub_entry;
26         struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
27
28         if (IS_ERR_OR_NULL(phy)) {
29                 if (!phy || PTR_ERR(phy) == -ENODEV)
30                         return 0;
31                 else
32                         return PTR_ERR(phy);
33         }
34
35         roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
36         if (!roothub_entry)
37                 return -ENOMEM;
38
39         INIT_LIST_HEAD(&roothub_entry->list);
40
41         roothub_entry->phy = phy;
42
43         list_add_tail(&roothub_entry->list, list);
44
45         return 0;
46 }
47
48 struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
49 {
50         struct usb_phy_roothub *phy_roothub;
51         int i, num_phys, err;
52
53         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
54                                               "#phy-cells");
55         if (num_phys <= 0)
56                 return NULL;
57
58         phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
59         if (!phy_roothub)
60                 return ERR_PTR(-ENOMEM);
61
62         INIT_LIST_HEAD(&phy_roothub->list);
63
64         for (i = 0; i < num_phys; i++) {
65                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
66                 if (err)
67                         return ERR_PTR(err);
68         }
69
70         return phy_roothub;
71 }
72 EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
73
74 int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
75 {
76         struct usb_phy_roothub *roothub_entry;
77         struct list_head *head;
78         int err;
79
80         if (!phy_roothub)
81                 return 0;
82
83         head = &phy_roothub->list;
84
85         list_for_each_entry(roothub_entry, head, list) {
86                 err = phy_init(roothub_entry->phy);
87                 if (err)
88                         goto err_exit_phys;
89         }
90
91         return 0;
92
93 err_exit_phys:
94         list_for_each_entry_continue_reverse(roothub_entry, head, list)
95                 phy_exit(roothub_entry->phy);
96
97         return err;
98 }
99 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
100
101 int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
102 {
103         struct usb_phy_roothub *roothub_entry;
104         struct list_head *head;
105         int err, ret = 0;
106
107         if (!phy_roothub)
108                 return 0;
109
110         head = &phy_roothub->list;
111
112         list_for_each_entry(roothub_entry, head, list) {
113                 err = phy_exit(roothub_entry->phy);
114                 if (err)
115                         ret = err;
116         }
117
118         return ret;
119 }
120 EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
121
122 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
123 {
124         struct usb_phy_roothub *roothub_entry;
125         struct list_head *head;
126         int err;
127
128         if (!phy_roothub)
129                 return 0;
130
131         head = &phy_roothub->list;
132
133         list_for_each_entry(roothub_entry, head, list) {
134                 err = phy_power_on(roothub_entry->phy);
135                 if (err)
136                         goto err_out;
137         }
138
139         return 0;
140
141 err_out:
142         list_for_each_entry_continue_reverse(roothub_entry, head, list)
143                 phy_power_off(roothub_entry->phy);
144
145         return err;
146 }
147 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
148
149 void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
150 {
151         struct usb_phy_roothub *roothub_entry;
152
153         if (!phy_roothub)
154                 return;
155
156         list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
157                 phy_power_off(roothub_entry->phy);
158 }
159 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);