Commit 5d3c28b5a42df5ceaa854901ba2cccb76883c77e

Authored by Kishon Vijay Abraham I
Committed by Felipe Balbi
1 parent 0fa4fab4ee

usb: otg: add device tree support to otg library

Added an API devm_usb_get_phy_by_phandle(), to get usb phy by passing a
device node phandle value. This function will return a pointer to
the phy on success, -EPROBE_DEFER if there is a device_node for the phandle,
but the phy has not been added, or a ERR_PTR() otherwise.

Cc: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>

Showing 2 changed files with 88 additions and 0 deletions Inline Diff

drivers/usb/otg/otg.c
1 /* 1 /*
2 * otg.c -- USB OTG utility code 2 * otg.c -- USB OTG utility code
3 * 3 *
4 * Copyright (C) 2004 Texas Instruments 4 * Copyright (C) 2004 Texas Instruments
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 */ 10 */
11 11
12 #include <linux/kernel.h> 12 #include <linux/kernel.h>
13 #include <linux/export.h> 13 #include <linux/export.h>
14 #include <linux/err.h> 14 #include <linux/err.h>
15 #include <linux/device.h> 15 #include <linux/device.h>
16 #include <linux/module.h>
16 #include <linux/slab.h> 17 #include <linux/slab.h>
18 #include <linux/of.h>
17 19
18 #include <linux/usb/otg.h> 20 #include <linux/usb/otg.h>
19 21
20 static LIST_HEAD(phy_list); 22 static LIST_HEAD(phy_list);
21 static LIST_HEAD(phy_bind_list); 23 static LIST_HEAD(phy_bind_list);
22 static DEFINE_SPINLOCK(phy_lock); 24 static DEFINE_SPINLOCK(phy_lock);
23 25
24 static struct usb_phy *__usb_find_phy(struct list_head *list, 26 static struct usb_phy *__usb_find_phy(struct list_head *list,
25 enum usb_phy_type type) 27 enum usb_phy_type type)
26 { 28 {
27 struct usb_phy *phy = NULL; 29 struct usb_phy *phy = NULL;
28 30
29 list_for_each_entry(phy, list, head) { 31 list_for_each_entry(phy, list, head) {
30 if (phy->type != type) 32 if (phy->type != type)
31 continue; 33 continue;
32 34
33 return phy; 35 return phy;
34 } 36 }
35 37
36 return ERR_PTR(-ENODEV); 38 return ERR_PTR(-ENODEV);
37 } 39 }
38 40
39 static struct usb_phy *__usb_find_phy_dev(struct device *dev, 41 static struct usb_phy *__usb_find_phy_dev(struct device *dev,
40 struct list_head *list, u8 index) 42 struct list_head *list, u8 index)
41 { 43 {
42 struct usb_phy_bind *phy_bind = NULL; 44 struct usb_phy_bind *phy_bind = NULL;
43 45
44 list_for_each_entry(phy_bind, list, list) { 46 list_for_each_entry(phy_bind, list, list) {
45 if (!(strcmp(phy_bind->dev_name, dev_name(dev))) && 47 if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
46 phy_bind->index == index) { 48 phy_bind->index == index) {
47 if (phy_bind->phy) 49 if (phy_bind->phy)
48 return phy_bind->phy; 50 return phy_bind->phy;
49 else 51 else
50 return ERR_PTR(-EPROBE_DEFER); 52 return ERR_PTR(-EPROBE_DEFER);
51 } 53 }
52 } 54 }
53 55
54 return ERR_PTR(-ENODEV); 56 return ERR_PTR(-ENODEV);
55 } 57 }
56 58
59 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
60 {
61 struct usb_phy *phy;
62
63 list_for_each_entry(phy, &phy_list, head) {
64 if (node != phy->dev->of_node)
65 continue;
66
67 return phy;
68 }
69
70 return ERR_PTR(-ENODEV);
71 }
72
57 static void devm_usb_phy_release(struct device *dev, void *res) 73 static void devm_usb_phy_release(struct device *dev, void *res)
58 { 74 {
59 struct usb_phy *phy = *(struct usb_phy **)res; 75 struct usb_phy *phy = *(struct usb_phy **)res;
60 76
61 usb_put_phy(phy); 77 usb_put_phy(phy);
62 } 78 }
63 79
64 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data) 80 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
65 { 81 {
66 return res == match_data; 82 return res == match_data;
67 } 83 }
68 84
69 /** 85 /**
70 * devm_usb_get_phy - find the USB PHY 86 * devm_usb_get_phy - find the USB PHY
71 * @dev - device that requests this phy 87 * @dev - device that requests this phy
72 * @type - the type of the phy the controller requires 88 * @type - the type of the phy the controller requires
73 * 89 *
74 * Gets the phy using usb_get_phy(), and associates a device with it using 90 * Gets the phy using usb_get_phy(), and associates a device with it using
75 * devres. On driver detach, release function is invoked on the devres data, 91 * devres. On driver detach, release function is invoked on the devres data,
76 * then, devres data is freed. 92 * then, devres data is freed.
77 * 93 *
78 * For use by USB host and peripheral drivers. 94 * For use by USB host and peripheral drivers.
79 */ 95 */
80 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type) 96 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
81 { 97 {
82 struct usb_phy **ptr, *phy; 98 struct usb_phy **ptr, *phy;
83 99
84 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); 100 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
85 if (!ptr) 101 if (!ptr)
86 return NULL; 102 return NULL;
87 103
88 phy = usb_get_phy(type); 104 phy = usb_get_phy(type);
89 if (!IS_ERR(phy)) { 105 if (!IS_ERR(phy)) {
90 *ptr = phy; 106 *ptr = phy;
91 devres_add(dev, ptr); 107 devres_add(dev, ptr);
92 } else 108 } else
93 devres_free(ptr); 109 devres_free(ptr);
94 110
95 return phy; 111 return phy;
96 } 112 }
97 EXPORT_SYMBOL(devm_usb_get_phy); 113 EXPORT_SYMBOL(devm_usb_get_phy);
98 114
99 /** 115 /**
100 * usb_get_phy - find the USB PHY 116 * usb_get_phy - find the USB PHY
101 * @type - the type of the phy the controller requires 117 * @type - the type of the phy the controller requires
102 * 118 *
103 * Returns the phy driver, after getting a refcount to it; or 119 * Returns the phy driver, after getting a refcount to it; or
104 * -ENODEV if there is no such phy. The caller is responsible for 120 * -ENODEV if there is no such phy. The caller is responsible for
105 * calling usb_put_phy() to release that count. 121 * calling usb_put_phy() to release that count.
106 * 122 *
107 * For use by USB host and peripheral drivers. 123 * For use by USB host and peripheral drivers.
108 */ 124 */
109 struct usb_phy *usb_get_phy(enum usb_phy_type type) 125 struct usb_phy *usb_get_phy(enum usb_phy_type type)
110 { 126 {
111 struct usb_phy *phy = NULL; 127 struct usb_phy *phy = NULL;
112 unsigned long flags; 128 unsigned long flags;
113 129
114 spin_lock_irqsave(&phy_lock, flags); 130 spin_lock_irqsave(&phy_lock, flags);
115 131
116 phy = __usb_find_phy(&phy_list, type); 132 phy = __usb_find_phy(&phy_list, type);
117 if (IS_ERR(phy)) { 133 if (IS_ERR(phy)) {
118 pr_err("unable to find transceiver of type %s\n", 134 pr_err("unable to find transceiver of type %s\n",
119 usb_phy_type_string(type)); 135 usb_phy_type_string(type));
120 goto err0; 136 goto err0;
121 } 137 }
122 138
123 get_device(phy->dev); 139 get_device(phy->dev);
124 140
125 err0: 141 err0:
126 spin_unlock_irqrestore(&phy_lock, flags); 142 spin_unlock_irqrestore(&phy_lock, flags);
127 143
128 return phy; 144 return phy;
129 } 145 }
130 EXPORT_SYMBOL(usb_get_phy); 146 EXPORT_SYMBOL(usb_get_phy);
147
148 /**
149 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
150 * @dev - device that requests this phy
151 * @phandle - name of the property holding the phy phandle value
152 * @index - the index of the phy
153 *
154 * Returns the phy driver associated with the given phandle value,
155 * after getting a refcount to it, -ENODEV if there is no such phy or
156 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
157 * not yet loaded. While at that, it also associates the device with
158 * the phy using devres. On driver detach, release function is invoked
159 * on the devres data, then, devres data is freed.
160 *
161 * For use by USB host and peripheral drivers.
162 */
163 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
164 const char *phandle, u8 index)
165 {
166 struct usb_phy *phy = ERR_PTR(-ENOMEM), **ptr;
167 unsigned long flags;
168 struct device_node *node;
169
170 if (!dev->of_node) {
171 dev_dbg(dev, "device does not have a device node entry\n");
172 return ERR_PTR(-EINVAL);
173 }
174
175 node = of_parse_phandle(dev->of_node, phandle, index);
176 if (!node) {
177 dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
178 dev->of_node->full_name);
179 return ERR_PTR(-ENODEV);
180 }
181
182 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
183 if (!ptr) {
184 dev_dbg(dev, "failed to allocate memory for devres\n");
185 goto err0;
186 }
187
188 spin_lock_irqsave(&phy_lock, flags);
189
190 phy = __of_usb_find_phy(node);
191 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
192 phy = ERR_PTR(-EPROBE_DEFER);
193 devres_free(ptr);
194 goto err1;
195 }
196
197 *ptr = phy;
198 devres_add(dev, ptr);
199
200 get_device(phy->dev);
201
202 err1:
203 spin_unlock_irqrestore(&phy_lock, flags);
204
205 err0:
206 of_node_put(node);
207
208 return phy;
209 }
210 EXPORT_SYMBOL(devm_usb_get_phy_by_phandle);
131 211
132 /** 212 /**
133 * usb_get_phy_dev - find the USB PHY 213 * usb_get_phy_dev - find the USB PHY
134 * @dev - device that requests this phy 214 * @dev - device that requests this phy
135 * @index - the index of the phy 215 * @index - the index of the phy
136 * 216 *
137 * Returns the phy driver, after getting a refcount to it; or 217 * Returns the phy driver, after getting a refcount to it; or
138 * -ENODEV if there is no such phy. The caller is responsible for 218 * -ENODEV if there is no such phy. The caller is responsible for
139 * calling usb_put_phy() to release that count. 219 * calling usb_put_phy() to release that count.
140 * 220 *
141 * For use by USB host and peripheral drivers. 221 * For use by USB host and peripheral drivers.
142 */ 222 */
143 struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index) 223 struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
144 { 224 {
145 struct usb_phy *phy = NULL; 225 struct usb_phy *phy = NULL;
146 unsigned long flags; 226 unsigned long flags;
147 227
148 spin_lock_irqsave(&phy_lock, flags); 228 spin_lock_irqsave(&phy_lock, flags);
149 229
150 phy = __usb_find_phy_dev(dev, &phy_bind_list, index); 230 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
151 if (IS_ERR(phy)) { 231 if (IS_ERR(phy)) {
152 pr_err("unable to find transceiver\n"); 232 pr_err("unable to find transceiver\n");
153 goto err0; 233 goto err0;
154 } 234 }
155 235
156 get_device(phy->dev); 236 get_device(phy->dev);
157 237
158 err0: 238 err0:
159 spin_unlock_irqrestore(&phy_lock, flags); 239 spin_unlock_irqrestore(&phy_lock, flags);
160 240
161 return phy; 241 return phy;
162 } 242 }
163 EXPORT_SYMBOL(usb_get_phy_dev); 243 EXPORT_SYMBOL(usb_get_phy_dev);
164 244
165 /** 245 /**
166 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index 246 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
167 * @dev - device that requests this phy 247 * @dev - device that requests this phy
168 * @index - the index of the phy 248 * @index - the index of the phy
169 * 249 *
170 * Gets the phy using usb_get_phy_dev(), and associates a device with it using 250 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
171 * devres. On driver detach, release function is invoked on the devres data, 251 * devres. On driver detach, release function is invoked on the devres data,
172 * then, devres data is freed. 252 * then, devres data is freed.
173 * 253 *
174 * For use by USB host and peripheral drivers. 254 * For use by USB host and peripheral drivers.
175 */ 255 */
176 struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index) 256 struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
177 { 257 {
178 struct usb_phy **ptr, *phy; 258 struct usb_phy **ptr, *phy;
179 259
180 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); 260 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
181 if (!ptr) 261 if (!ptr)
182 return NULL; 262 return NULL;
183 263
184 phy = usb_get_phy_dev(dev, index); 264 phy = usb_get_phy_dev(dev, index);
185 if (!IS_ERR(phy)) { 265 if (!IS_ERR(phy)) {
186 *ptr = phy; 266 *ptr = phy;
187 devres_add(dev, ptr); 267 devres_add(dev, ptr);
188 } else 268 } else
189 devres_free(ptr); 269 devres_free(ptr);
190 270
191 return phy; 271 return phy;
192 } 272 }
193 EXPORT_SYMBOL(devm_usb_get_phy_dev); 273 EXPORT_SYMBOL(devm_usb_get_phy_dev);
194 274
195 /** 275 /**
196 * devm_usb_put_phy - release the USB PHY 276 * devm_usb_put_phy - release the USB PHY
197 * @dev - device that wants to release this phy 277 * @dev - device that wants to release this phy
198 * @phy - the phy returned by devm_usb_get_phy() 278 * @phy - the phy returned by devm_usb_get_phy()
199 * 279 *
200 * destroys the devres associated with this phy and invokes usb_put_phy 280 * destroys the devres associated with this phy and invokes usb_put_phy
201 * to release the phy. 281 * to release the phy.
202 * 282 *
203 * For use by USB host and peripheral drivers. 283 * For use by USB host and peripheral drivers.
204 */ 284 */
205 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy) 285 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
206 { 286 {
207 int r; 287 int r;
208 288
209 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy); 289 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
210 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); 290 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
211 } 291 }
212 EXPORT_SYMBOL(devm_usb_put_phy); 292 EXPORT_SYMBOL(devm_usb_put_phy);
213 293
214 /** 294 /**
215 * usb_put_phy - release the USB PHY 295 * usb_put_phy - release the USB PHY
216 * @x: the phy returned by usb_get_phy() 296 * @x: the phy returned by usb_get_phy()
217 * 297 *
218 * Releases a refcount the caller received from usb_get_phy(). 298 * Releases a refcount the caller received from usb_get_phy().
219 * 299 *
220 * For use by USB host and peripheral drivers. 300 * For use by USB host and peripheral drivers.
221 */ 301 */
222 void usb_put_phy(struct usb_phy *x) 302 void usb_put_phy(struct usb_phy *x)
223 { 303 {
224 if (x) 304 if (x)
225 put_device(x->dev); 305 put_device(x->dev);
226 } 306 }
227 EXPORT_SYMBOL(usb_put_phy); 307 EXPORT_SYMBOL(usb_put_phy);
228 308
229 /** 309 /**
230 * usb_add_phy - declare the USB PHY 310 * usb_add_phy - declare the USB PHY
231 * @x: the USB phy to be used; or NULL 311 * @x: the USB phy to be used; or NULL
232 * @type - the type of this PHY 312 * @type - the type of this PHY
233 * 313 *
234 * This call is exclusively for use by phy drivers, which 314 * This call is exclusively for use by phy drivers, which
235 * coordinate the activities of drivers for host and peripheral 315 * coordinate the activities of drivers for host and peripheral
236 * controllers, and in some cases for VBUS current regulation. 316 * controllers, and in some cases for VBUS current regulation.
237 */ 317 */
238 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type) 318 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
239 { 319 {
240 int ret = 0; 320 int ret = 0;
241 unsigned long flags; 321 unsigned long flags;
242 struct usb_phy *phy; 322 struct usb_phy *phy;
243 323
244 if (x->type != USB_PHY_TYPE_UNDEFINED) { 324 if (x->type != USB_PHY_TYPE_UNDEFINED) {
245 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); 325 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
246 return -EINVAL; 326 return -EINVAL;
247 } 327 }
248 328
249 spin_lock_irqsave(&phy_lock, flags); 329 spin_lock_irqsave(&phy_lock, flags);
250 330
251 list_for_each_entry(phy, &phy_list, head) { 331 list_for_each_entry(phy, &phy_list, head) {
252 if (phy->type == type) { 332 if (phy->type == type) {
253 ret = -EBUSY; 333 ret = -EBUSY;
254 dev_err(x->dev, "transceiver type %s already exists\n", 334 dev_err(x->dev, "transceiver type %s already exists\n",
255 usb_phy_type_string(type)); 335 usb_phy_type_string(type));
256 goto out; 336 goto out;
257 } 337 }
258 } 338 }
259 339
260 x->type = type; 340 x->type = type;
261 list_add_tail(&x->head, &phy_list); 341 list_add_tail(&x->head, &phy_list);
262 342
263 out: 343 out:
264 spin_unlock_irqrestore(&phy_lock, flags); 344 spin_unlock_irqrestore(&phy_lock, flags);
265 return ret; 345 return ret;
266 } 346 }
267 EXPORT_SYMBOL(usb_add_phy); 347 EXPORT_SYMBOL(usb_add_phy);
268 348
269 /** 349 /**
270 * usb_add_phy_dev - declare the USB PHY 350 * usb_add_phy_dev - declare the USB PHY
271 * @x: the USB phy to be used; or NULL 351 * @x: the USB phy to be used; or NULL
272 * 352 *
273 * This call is exclusively for use by phy drivers, which 353 * This call is exclusively for use by phy drivers, which
274 * coordinate the activities of drivers for host and peripheral 354 * coordinate the activities of drivers for host and peripheral
275 * controllers, and in some cases for VBUS current regulation. 355 * controllers, and in some cases for VBUS current regulation.
276 */ 356 */
277 int usb_add_phy_dev(struct usb_phy *x) 357 int usb_add_phy_dev(struct usb_phy *x)
278 { 358 {
279 struct usb_phy_bind *phy_bind; 359 struct usb_phy_bind *phy_bind;
280 unsigned long flags; 360 unsigned long flags;
281 361
282 if (!x->dev) { 362 if (!x->dev) {
283 dev_err(x->dev, "no device provided for PHY\n"); 363 dev_err(x->dev, "no device provided for PHY\n");
284 return -EINVAL; 364 return -EINVAL;
285 } 365 }
286 366
287 spin_lock_irqsave(&phy_lock, flags); 367 spin_lock_irqsave(&phy_lock, flags);
288 list_for_each_entry(phy_bind, &phy_bind_list, list) 368 list_for_each_entry(phy_bind, &phy_bind_list, list)
289 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev)))) 369 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
290 phy_bind->phy = x; 370 phy_bind->phy = x;
291 371
292 list_add_tail(&x->head, &phy_list); 372 list_add_tail(&x->head, &phy_list);
293 373
294 spin_unlock_irqrestore(&phy_lock, flags); 374 spin_unlock_irqrestore(&phy_lock, flags);
295 return 0; 375 return 0;
296 } 376 }
297 EXPORT_SYMBOL(usb_add_phy_dev); 377 EXPORT_SYMBOL(usb_add_phy_dev);
298 378
299 /** 379 /**
300 * usb_remove_phy - remove the OTG PHY 380 * usb_remove_phy - remove the OTG PHY
301 * @x: the USB OTG PHY to be removed; 381 * @x: the USB OTG PHY to be removed;
302 * 382 *
303 * This reverts the effects of usb_add_phy 383 * This reverts the effects of usb_add_phy
304 */ 384 */
305 void usb_remove_phy(struct usb_phy *x) 385 void usb_remove_phy(struct usb_phy *x)
306 { 386 {
307 unsigned long flags; 387 unsigned long flags;
308 struct usb_phy_bind *phy_bind; 388 struct usb_phy_bind *phy_bind;
309 389
310 spin_lock_irqsave(&phy_lock, flags); 390 spin_lock_irqsave(&phy_lock, flags);
311 if (x) { 391 if (x) {
312 list_for_each_entry(phy_bind, &phy_bind_list, list) 392 list_for_each_entry(phy_bind, &phy_bind_list, list)
313 if (phy_bind->phy == x) 393 if (phy_bind->phy == x)
314 phy_bind->phy = NULL; 394 phy_bind->phy = NULL;
315 list_del(&x->head); 395 list_del(&x->head);
316 } 396 }
317 spin_unlock_irqrestore(&phy_lock, flags); 397 spin_unlock_irqrestore(&phy_lock, flags);
318 } 398 }
319 EXPORT_SYMBOL(usb_remove_phy); 399 EXPORT_SYMBOL(usb_remove_phy);
320 400
321 /** 401 /**
322 * usb_bind_phy - bind the phy and the controller that uses the phy 402 * usb_bind_phy - bind the phy and the controller that uses the phy
323 * @dev_name: the device name of the device that will bind to the phy 403 * @dev_name: the device name of the device that will bind to the phy
324 * @index: index to specify the port number 404 * @index: index to specify the port number
325 * @phy_dev_name: the device name of the phy 405 * @phy_dev_name: the device name of the phy
326 * 406 *
327 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will 407 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
328 * be used when the phy driver registers the phy and when the controller 408 * be used when the phy driver registers the phy and when the controller
329 * requests this phy. 409 * requests this phy.
330 * 410 *
331 * To be used by platform specific initialization code. 411 * To be used by platform specific initialization code.
332 */ 412 */
333 int __init usb_bind_phy(const char *dev_name, u8 index, 413 int __init usb_bind_phy(const char *dev_name, u8 index,
334 const char *phy_dev_name) 414 const char *phy_dev_name)
335 { 415 {
336 struct usb_phy_bind *phy_bind; 416 struct usb_phy_bind *phy_bind;
337 unsigned long flags; 417 unsigned long flags;
338 418
339 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL); 419 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
340 if (!phy_bind) { 420 if (!phy_bind) {
341 pr_err("phy_bind(): No memory for phy_bind"); 421 pr_err("phy_bind(): No memory for phy_bind");
342 return -ENOMEM; 422 return -ENOMEM;
343 } 423 }
344 424
345 phy_bind->dev_name = dev_name; 425 phy_bind->dev_name = dev_name;
346 phy_bind->phy_dev_name = phy_dev_name; 426 phy_bind->phy_dev_name = phy_dev_name;
347 phy_bind->index = index; 427 phy_bind->index = index;
348 428
349 spin_lock_irqsave(&phy_lock, flags); 429 spin_lock_irqsave(&phy_lock, flags);
350 list_add_tail(&phy_bind->list, &phy_bind_list); 430 list_add_tail(&phy_bind->list, &phy_bind_list);
351 spin_unlock_irqrestore(&phy_lock, flags); 431 spin_unlock_irqrestore(&phy_lock, flags);
352 432
353 return 0; 433 return 0;
354 } 434 }
355 EXPORT_SYMBOL_GPL(usb_bind_phy); 435 EXPORT_SYMBOL_GPL(usb_bind_phy);
356 436
357 const char *otg_state_string(enum usb_otg_state state) 437 const char *otg_state_string(enum usb_otg_state state)
358 { 438 {
359 switch (state) { 439 switch (state) {
360 case OTG_STATE_A_IDLE: 440 case OTG_STATE_A_IDLE:
361 return "a_idle"; 441 return "a_idle";
362 case OTG_STATE_A_WAIT_VRISE: 442 case OTG_STATE_A_WAIT_VRISE:
363 return "a_wait_vrise"; 443 return "a_wait_vrise";
364 case OTG_STATE_A_WAIT_BCON: 444 case OTG_STATE_A_WAIT_BCON:
365 return "a_wait_bcon"; 445 return "a_wait_bcon";
366 case OTG_STATE_A_HOST: 446 case OTG_STATE_A_HOST:
367 return "a_host"; 447 return "a_host";
368 case OTG_STATE_A_SUSPEND: 448 case OTG_STATE_A_SUSPEND:
369 return "a_suspend"; 449 return "a_suspend";
370 case OTG_STATE_A_PERIPHERAL: 450 case OTG_STATE_A_PERIPHERAL:
371 return "a_peripheral"; 451 return "a_peripheral";
372 case OTG_STATE_A_WAIT_VFALL: 452 case OTG_STATE_A_WAIT_VFALL:
373 return "a_wait_vfall"; 453 return "a_wait_vfall";
374 case OTG_STATE_A_VBUS_ERR: 454 case OTG_STATE_A_VBUS_ERR:
375 return "a_vbus_err"; 455 return "a_vbus_err";
376 case OTG_STATE_B_IDLE: 456 case OTG_STATE_B_IDLE:
377 return "b_idle"; 457 return "b_idle";
378 case OTG_STATE_B_SRP_INIT: 458 case OTG_STATE_B_SRP_INIT:
379 return "b_srp_init"; 459 return "b_srp_init";
380 case OTG_STATE_B_PERIPHERAL: 460 case OTG_STATE_B_PERIPHERAL:
381 return "b_peripheral"; 461 return "b_peripheral";
382 case OTG_STATE_B_WAIT_ACON: 462 case OTG_STATE_B_WAIT_ACON:
383 return "b_wait_acon"; 463 return "b_wait_acon";
384 case OTG_STATE_B_HOST: 464 case OTG_STATE_B_HOST:
385 return "b_host"; 465 return "b_host";
386 default: 466 default:
387 return "UNDEFINED"; 467 return "UNDEFINED";
388 } 468 }
389 } 469 }
390 EXPORT_SYMBOL(otg_state_string); 470 EXPORT_SYMBOL(otg_state_string);
391 471
include/linux/usb/phy.h
1 /* USB OTG (On The Go) defines */ 1 /* USB OTG (On The Go) defines */
2 /* 2 /*
3 * 3 *
4 * These APIs may be used between USB controllers. USB device drivers 4 * These APIs may be used between USB controllers. USB device drivers
5 * (for either host or peripheral roles) don't use these calls; they 5 * (for either host or peripheral roles) don't use these calls; they
6 * continue to use just usb_device and usb_gadget. 6 * continue to use just usb_device and usb_gadget.
7 */ 7 */
8 8
9 #ifndef __LINUX_USB_PHY_H 9 #ifndef __LINUX_USB_PHY_H
10 #define __LINUX_USB_PHY_H 10 #define __LINUX_USB_PHY_H
11 11
12 #include <linux/notifier.h> 12 #include <linux/notifier.h>
13 #include <linux/usb.h> 13 #include <linux/usb.h>
14 14
15 enum usb_phy_events { 15 enum usb_phy_events {
16 USB_EVENT_NONE, /* no events or cable disconnected */ 16 USB_EVENT_NONE, /* no events or cable disconnected */
17 USB_EVENT_VBUS, /* vbus valid event */ 17 USB_EVENT_VBUS, /* vbus valid event */
18 USB_EVENT_ID, /* id was grounded */ 18 USB_EVENT_ID, /* id was grounded */
19 USB_EVENT_CHARGER, /* usb dedicated charger */ 19 USB_EVENT_CHARGER, /* usb dedicated charger */
20 USB_EVENT_ENUMERATED, /* gadget driver enumerated */ 20 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
21 }; 21 };
22 22
23 /* associate a type with PHY */ 23 /* associate a type with PHY */
24 enum usb_phy_type { 24 enum usb_phy_type {
25 USB_PHY_TYPE_UNDEFINED, 25 USB_PHY_TYPE_UNDEFINED,
26 USB_PHY_TYPE_USB2, 26 USB_PHY_TYPE_USB2,
27 USB_PHY_TYPE_USB3, 27 USB_PHY_TYPE_USB3,
28 }; 28 };
29 29
30 /* OTG defines lots of enumeration states before device reset */ 30 /* OTG defines lots of enumeration states before device reset */
31 enum usb_otg_state { 31 enum usb_otg_state {
32 OTG_STATE_UNDEFINED = 0, 32 OTG_STATE_UNDEFINED = 0,
33 33
34 /* single-role peripheral, and dual-role default-b */ 34 /* single-role peripheral, and dual-role default-b */
35 OTG_STATE_B_IDLE, 35 OTG_STATE_B_IDLE,
36 OTG_STATE_B_SRP_INIT, 36 OTG_STATE_B_SRP_INIT,
37 OTG_STATE_B_PERIPHERAL, 37 OTG_STATE_B_PERIPHERAL,
38 38
39 /* extra dual-role default-b states */ 39 /* extra dual-role default-b states */
40 OTG_STATE_B_WAIT_ACON, 40 OTG_STATE_B_WAIT_ACON,
41 OTG_STATE_B_HOST, 41 OTG_STATE_B_HOST,
42 42
43 /* dual-role default-a */ 43 /* dual-role default-a */
44 OTG_STATE_A_IDLE, 44 OTG_STATE_A_IDLE,
45 OTG_STATE_A_WAIT_VRISE, 45 OTG_STATE_A_WAIT_VRISE,
46 OTG_STATE_A_WAIT_BCON, 46 OTG_STATE_A_WAIT_BCON,
47 OTG_STATE_A_HOST, 47 OTG_STATE_A_HOST,
48 OTG_STATE_A_SUSPEND, 48 OTG_STATE_A_SUSPEND,
49 OTG_STATE_A_PERIPHERAL, 49 OTG_STATE_A_PERIPHERAL,
50 OTG_STATE_A_WAIT_VFALL, 50 OTG_STATE_A_WAIT_VFALL,
51 OTG_STATE_A_VBUS_ERR, 51 OTG_STATE_A_VBUS_ERR,
52 }; 52 };
53 53
54 struct usb_phy; 54 struct usb_phy;
55 struct usb_otg; 55 struct usb_otg;
56 56
57 /* for transceivers connected thru an ULPI interface, the user must 57 /* for transceivers connected thru an ULPI interface, the user must
58 * provide access ops 58 * provide access ops
59 */ 59 */
60 struct usb_phy_io_ops { 60 struct usb_phy_io_ops {
61 int (*read)(struct usb_phy *x, u32 reg); 61 int (*read)(struct usb_phy *x, u32 reg);
62 int (*write)(struct usb_phy *x, u32 val, u32 reg); 62 int (*write)(struct usb_phy *x, u32 val, u32 reg);
63 }; 63 };
64 64
65 struct usb_phy { 65 struct usb_phy {
66 struct device *dev; 66 struct device *dev;
67 const char *label; 67 const char *label;
68 unsigned int flags; 68 unsigned int flags;
69 69
70 enum usb_phy_type type; 70 enum usb_phy_type type;
71 enum usb_otg_state state; 71 enum usb_otg_state state;
72 enum usb_phy_events last_event; 72 enum usb_phy_events last_event;
73 73
74 struct usb_otg *otg; 74 struct usb_otg *otg;
75 75
76 struct device *io_dev; 76 struct device *io_dev;
77 struct usb_phy_io_ops *io_ops; 77 struct usb_phy_io_ops *io_ops;
78 void __iomem *io_priv; 78 void __iomem *io_priv;
79 79
80 /* for notification of usb_phy_events */ 80 /* for notification of usb_phy_events */
81 struct atomic_notifier_head notifier; 81 struct atomic_notifier_head notifier;
82 82
83 /* to pass extra port status to the root hub */ 83 /* to pass extra port status to the root hub */
84 u16 port_status; 84 u16 port_status;
85 u16 port_change; 85 u16 port_change;
86 86
87 /* to support controllers that have multiple transceivers */ 87 /* to support controllers that have multiple transceivers */
88 struct list_head head; 88 struct list_head head;
89 89
90 /* initialize/shutdown the OTG controller */ 90 /* initialize/shutdown the OTG controller */
91 int (*init)(struct usb_phy *x); 91 int (*init)(struct usb_phy *x);
92 void (*shutdown)(struct usb_phy *x); 92 void (*shutdown)(struct usb_phy *x);
93 93
94 /* effective for B devices, ignored for A-peripheral */ 94 /* effective for B devices, ignored for A-peripheral */
95 int (*set_power)(struct usb_phy *x, 95 int (*set_power)(struct usb_phy *x,
96 unsigned mA); 96 unsigned mA);
97 97
98 /* for non-OTG B devices: set transceiver into suspend mode */ 98 /* for non-OTG B devices: set transceiver into suspend mode */
99 int (*set_suspend)(struct usb_phy *x, 99 int (*set_suspend)(struct usb_phy *x,
100 int suspend); 100 int suspend);
101 101
102 /* notify phy connect status change */ 102 /* notify phy connect status change */
103 int (*notify_connect)(struct usb_phy *x, 103 int (*notify_connect)(struct usb_phy *x,
104 enum usb_device_speed speed); 104 enum usb_device_speed speed);
105 int (*notify_disconnect)(struct usb_phy *x, 105 int (*notify_disconnect)(struct usb_phy *x,
106 enum usb_device_speed speed); 106 enum usb_device_speed speed);
107 }; 107 };
108 108
109 /** 109 /**
110 * struct usb_phy_bind - represent the binding for the phy 110 * struct usb_phy_bind - represent the binding for the phy
111 * @dev_name: the device name of the device that will bind to the phy 111 * @dev_name: the device name of the device that will bind to the phy
112 * @phy_dev_name: the device name of the phy 112 * @phy_dev_name: the device name of the phy
113 * @index: used if a single controller uses multiple phys 113 * @index: used if a single controller uses multiple phys
114 * @phy: reference to the phy 114 * @phy: reference to the phy
115 * @list: to maintain a linked list of the binding information 115 * @list: to maintain a linked list of the binding information
116 */ 116 */
117 struct usb_phy_bind { 117 struct usb_phy_bind {
118 const char *dev_name; 118 const char *dev_name;
119 const char *phy_dev_name; 119 const char *phy_dev_name;
120 u8 index; 120 u8 index;
121 struct usb_phy *phy; 121 struct usb_phy *phy;
122 struct list_head list; 122 struct list_head list;
123 }; 123 };
124 124
125 /* for board-specific init logic */ 125 /* for board-specific init logic */
126 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type); 126 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
127 extern int usb_add_phy_dev(struct usb_phy *); 127 extern int usb_add_phy_dev(struct usb_phy *);
128 extern void usb_remove_phy(struct usb_phy *); 128 extern void usb_remove_phy(struct usb_phy *);
129 129
130 /* helpers for direct access thru low-level io interface */ 130 /* helpers for direct access thru low-level io interface */
131 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg) 131 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
132 { 132 {
133 if (x->io_ops && x->io_ops->read) 133 if (x->io_ops && x->io_ops->read)
134 return x->io_ops->read(x, reg); 134 return x->io_ops->read(x, reg);
135 135
136 return -EINVAL; 136 return -EINVAL;
137 } 137 }
138 138
139 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg) 139 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
140 { 140 {
141 if (x->io_ops && x->io_ops->write) 141 if (x->io_ops && x->io_ops->write)
142 return x->io_ops->write(x, val, reg); 142 return x->io_ops->write(x, val, reg);
143 143
144 return -EINVAL; 144 return -EINVAL;
145 } 145 }
146 146
147 static inline int 147 static inline int
148 usb_phy_init(struct usb_phy *x) 148 usb_phy_init(struct usb_phy *x)
149 { 149 {
150 if (x->init) 150 if (x->init)
151 return x->init(x); 151 return x->init(x);
152 152
153 return 0; 153 return 0;
154 } 154 }
155 155
156 static inline void 156 static inline void
157 usb_phy_shutdown(struct usb_phy *x) 157 usb_phy_shutdown(struct usb_phy *x)
158 { 158 {
159 if (x->shutdown) 159 if (x->shutdown)
160 x->shutdown(x); 160 x->shutdown(x);
161 } 161 }
162 162
163 /* for usb host and peripheral controller drivers */ 163 /* for usb host and peripheral controller drivers */
164 #ifdef CONFIG_USB_OTG_UTILS 164 #ifdef CONFIG_USB_OTG_UTILS
165 extern struct usb_phy *usb_get_phy(enum usb_phy_type type); 165 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
166 extern struct usb_phy *devm_usb_get_phy(struct device *dev, 166 extern struct usb_phy *devm_usb_get_phy(struct device *dev,
167 enum usb_phy_type type); 167 enum usb_phy_type type);
168 extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index); 168 extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
169 extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index); 169 extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
170 extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
171 const char *phandle, u8 index);
170 extern void usb_put_phy(struct usb_phy *); 172 extern void usb_put_phy(struct usb_phy *);
171 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x); 173 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
172 extern int usb_bind_phy(const char *dev_name, u8 index, 174 extern int usb_bind_phy(const char *dev_name, u8 index,
173 const char *phy_dev_name); 175 const char *phy_dev_name);
174 #else 176 #else
175 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type) 177 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
176 { 178 {
177 return NULL; 179 return NULL;
178 } 180 }
179 181
180 static inline struct usb_phy *devm_usb_get_phy(struct device *dev, 182 static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
181 enum usb_phy_type type) 183 enum usb_phy_type type)
182 { 184 {
183 return NULL; 185 return NULL;
184 } 186 }
185 187
186 static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index) 188 static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
187 { 189 {
188 return NULL; 190 return NULL;
189 } 191 }
190 192
191 static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index) 193 static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
194 {
195 return NULL;
196 }
197
198 static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
199 const char *phandle, u8 index)
192 { 200 {
193 return NULL; 201 return NULL;
194 } 202 }
195 203
196 static inline void usb_put_phy(struct usb_phy *x) 204 static inline void usb_put_phy(struct usb_phy *x)
197 { 205 {
198 } 206 }
199 207
200 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x) 208 static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
201 { 209 {
202 } 210 }
203 211
204 static inline int usb_bind_phy(const char *dev_name, u8 index, 212 static inline int usb_bind_phy(const char *dev_name, u8 index,
205 const char *phy_dev_name) 213 const char *phy_dev_name)
206 { 214 {
207 return -EOPNOTSUPP; 215 return -EOPNOTSUPP;
208 } 216 }
209 #endif 217 #endif
210 218
211 static inline int 219 static inline int
212 usb_phy_set_power(struct usb_phy *x, unsigned mA) 220 usb_phy_set_power(struct usb_phy *x, unsigned mA)
213 { 221 {
214 if (x && x->set_power) 222 if (x && x->set_power)
215 return x->set_power(x, mA); 223 return x->set_power(x, mA);
216 return 0; 224 return 0;
217 } 225 }
218 226
219 /* Context: can sleep */ 227 /* Context: can sleep */
220 static inline int 228 static inline int
221 usb_phy_set_suspend(struct usb_phy *x, int suspend) 229 usb_phy_set_suspend(struct usb_phy *x, int suspend)
222 { 230 {
223 if (x->set_suspend != NULL) 231 if (x->set_suspend != NULL)
224 return x->set_suspend(x, suspend); 232 return x->set_suspend(x, suspend);
225 else 233 else
226 return 0; 234 return 0;
227 } 235 }
228 236
229 static inline int 237 static inline int
230 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed) 238 usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
231 { 239 {
232 if (x->notify_connect) 240 if (x->notify_connect)
233 return x->notify_connect(x, speed); 241 return x->notify_connect(x, speed);
234 else 242 else
235 return 0; 243 return 0;
236 } 244 }
237 245
238 static inline int 246 static inline int
239 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed) 247 usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
240 { 248 {
241 if (x->notify_disconnect) 249 if (x->notify_disconnect)
242 return x->notify_disconnect(x, speed); 250 return x->notify_disconnect(x, speed);
243 else 251 else
244 return 0; 252 return 0;
245 } 253 }
246 254
247 /* notifiers */ 255 /* notifiers */
248 static inline int 256 static inline int
249 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb) 257 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
250 { 258 {
251 return atomic_notifier_chain_register(&x->notifier, nb); 259 return atomic_notifier_chain_register(&x->notifier, nb);
252 } 260 }
253 261
254 static inline void 262 static inline void
255 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb) 263 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
256 { 264 {
257 atomic_notifier_chain_unregister(&x->notifier, nb); 265 atomic_notifier_chain_unregister(&x->notifier, nb);
258 } 266 }
259 267
260 static inline const char *usb_phy_type_string(enum usb_phy_type type) 268 static inline const char *usb_phy_type_string(enum usb_phy_type type)
261 { 269 {
262 switch (type) { 270 switch (type) {
263 case USB_PHY_TYPE_USB2: 271 case USB_PHY_TYPE_USB2:
264 return "USB2 PHY"; 272 return "USB2 PHY";
265 case USB_PHY_TYPE_USB3: 273 case USB_PHY_TYPE_USB3:
266 return "USB3 PHY"; 274 return "USB3 PHY";
267 default: 275 default:
268 return "UNKNOWN PHY TYPE"; 276 return "UNKNOWN PHY TYPE";
269 } 277 }
270 } 278 }
271 #endif /* __LINUX_USB_PHY_H */ 279 #endif /* __LINUX_USB_PHY_H */
272 280