Commit | Line | Data |
---|---|---|
cedf8602 SH |
1 | /* |
2 | * phy.c -- USB phy handling | |
3 | * | |
4 | * Copyright (C) 2004-2013 Texas Instruments | |
5 | * | |
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 | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/export.h> | |
13 | #include <linux/err.h> | |
14 | #include <linux/device.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/of.h> | |
18 | ||
19 | #include <linux/usb/phy.h> | |
20 | ||
21 | static LIST_HEAD(phy_list); | |
22 | static LIST_HEAD(phy_bind_list); | |
23 | static DEFINE_SPINLOCK(phy_lock); | |
24 | ||
25 | static struct usb_phy *__usb_find_phy(struct list_head *list, | |
26 | enum usb_phy_type type) | |
27 | { | |
28 | struct usb_phy *phy = NULL; | |
29 | ||
30 | list_for_each_entry(phy, list, head) { | |
31 | if (phy->type != type) | |
32 | continue; | |
33 | ||
34 | return phy; | |
35 | } | |
36 | ||
0df8fc37 | 37 | return ERR_PTR(-ENODEV); |
cedf8602 SH |
38 | } |
39 | ||
40 | static struct usb_phy *__usb_find_phy_dev(struct device *dev, | |
41 | struct list_head *list, u8 index) | |
42 | { | |
43 | struct usb_phy_bind *phy_bind = NULL; | |
44 | ||
45 | list_for_each_entry(phy_bind, list, list) { | |
46 | if (!(strcmp(phy_bind->dev_name, dev_name(dev))) && | |
47 | phy_bind->index == index) { | |
48 | if (phy_bind->phy) | |
49 | return phy_bind->phy; | |
50 | else | |
51 | return ERR_PTR(-EPROBE_DEFER); | |
52 | } | |
53 | } | |
54 | ||
55 | return ERR_PTR(-ENODEV); | |
56 | } | |
57 | ||
58 | static struct usb_phy *__of_usb_find_phy(struct device_node *node) | |
59 | { | |
60 | struct usb_phy *phy; | |
61 | ||
c818a94c TR |
62 | if (!of_device_is_available(node)) |
63 | return ERR_PTR(-ENODEV); | |
64 | ||
cedf8602 SH |
65 | list_for_each_entry(phy, &phy_list, head) { |
66 | if (node != phy->dev->of_node) | |
67 | continue; | |
68 | ||
69 | return phy; | |
70 | } | |
71 | ||
9c9d8249 | 72 | return ERR_PTR(-EPROBE_DEFER); |
cedf8602 SH |
73 | } |
74 | ||
75 | static void devm_usb_phy_release(struct device *dev, void *res) | |
76 | { | |
77 | struct usb_phy *phy = *(struct usb_phy **)res; | |
78 | ||
79 | usb_put_phy(phy); | |
80 | } | |
81 | ||
82 | static int devm_usb_phy_match(struct device *dev, void *res, void *match_data) | |
83 | { | |
84 | return res == match_data; | |
85 | } | |
86 | ||
87 | /** | |
88 | * devm_usb_get_phy - find the USB PHY | |
89 | * @dev - device that requests this phy | |
90 | * @type - the type of the phy the controller requires | |
91 | * | |
92 | * Gets the phy using usb_get_phy(), and associates a device with it using | |
93 | * devres. On driver detach, release function is invoked on the devres data, | |
94 | * then, devres data is freed. | |
95 | * | |
96 | * For use by USB host and peripheral drivers. | |
97 | */ | |
98 | struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type) | |
99 | { | |
100 | struct usb_phy **ptr, *phy; | |
101 | ||
102 | ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); | |
103 | if (!ptr) | |
7f4d1e7b | 104 | return ERR_PTR(-ENOMEM); |
cedf8602 SH |
105 | |
106 | phy = usb_get_phy(type); | |
107 | if (!IS_ERR(phy)) { | |
108 | *ptr = phy; | |
109 | devres_add(dev, ptr); | |
110 | } else | |
111 | devres_free(ptr); | |
112 | ||
113 | return phy; | |
114 | } | |
110ff6d0 | 115 | EXPORT_SYMBOL_GPL(devm_usb_get_phy); |
cedf8602 SH |
116 | |
117 | /** | |
118 | * usb_get_phy - find the USB PHY | |
119 | * @type - the type of the phy the controller requires | |
120 | * | |
121 | * Returns the phy driver, after getting a refcount to it; or | |
122 | * -ENODEV if there is no such phy. The caller is responsible for | |
123 | * calling usb_put_phy() to release that count. | |
124 | * | |
125 | * For use by USB host and peripheral drivers. | |
126 | */ | |
127 | struct usb_phy *usb_get_phy(enum usb_phy_type type) | |
128 | { | |
129 | struct usb_phy *phy = NULL; | |
130 | unsigned long flags; | |
131 | ||
132 | spin_lock_irqsave(&phy_lock, flags); | |
133 | ||
134 | phy = __usb_find_phy(&phy_list, type); | |
135 | if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { | |
e9d123a5 | 136 | pr_debug("PHY: unable to find transceiver of type %s\n", |
cedf8602 | 137 | usb_phy_type_string(type)); |
9dc9cb0c MK |
138 | if (!IS_ERR(phy)) |
139 | phy = ERR_PTR(-ENODEV); | |
140 | ||
cedf8602 SH |
141 | goto err0; |
142 | } | |
143 | ||
144 | get_device(phy->dev); | |
145 | ||
146 | err0: | |
147 | spin_unlock_irqrestore(&phy_lock, flags); | |
148 | ||
149 | return phy; | |
150 | } | |
110ff6d0 | 151 | EXPORT_SYMBOL_GPL(usb_get_phy); |
cedf8602 | 152 | |
fb1928de | 153 | /** |
cedf8602 SH |
154 | * devm_usb_get_phy_by_phandle - find the USB PHY by phandle |
155 | * @dev - device that requests this phy | |
156 | * @phandle - name of the property holding the phy phandle value | |
157 | * @index - the index of the phy | |
158 | * | |
159 | * Returns the phy driver associated with the given phandle value, | |
160 | * after getting a refcount to it, -ENODEV if there is no such phy or | |
161 | * -EPROBE_DEFER if there is a phandle to the phy, but the device is | |
162 | * not yet loaded. While at that, it also associates the device with | |
163 | * the phy using devres. On driver detach, release function is invoked | |
164 | * on the devres data, then, devres data is freed. | |
165 | * | |
166 | * For use by USB host and peripheral drivers. | |
167 | */ | |
168 | struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev, | |
169 | const char *phandle, u8 index) | |
170 | { | |
171 | struct usb_phy *phy = ERR_PTR(-ENOMEM), **ptr; | |
172 | unsigned long flags; | |
173 | struct device_node *node; | |
174 | ||
175 | if (!dev->of_node) { | |
176 | dev_dbg(dev, "device does not have a device node entry\n"); | |
177 | return ERR_PTR(-EINVAL); | |
178 | } | |
179 | ||
180 | node = of_parse_phandle(dev->of_node, phandle, index); | |
181 | if (!node) { | |
182 | dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle, | |
183 | dev->of_node->full_name); | |
184 | return ERR_PTR(-ENODEV); | |
185 | } | |
186 | ||
187 | ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); | |
188 | if (!ptr) { | |
189 | dev_dbg(dev, "failed to allocate memory for devres\n"); | |
190 | goto err0; | |
191 | } | |
192 | ||
193 | spin_lock_irqsave(&phy_lock, flags); | |
194 | ||
195 | phy = __of_usb_find_phy(node); | |
c818a94c TR |
196 | if (IS_ERR(phy)) { |
197 | devres_free(ptr); | |
198 | goto err1; | |
199 | } | |
1290a958 | 200 | |
c818a94c TR |
201 | if (!try_module_get(phy->dev->driver->owner)) { |
202 | phy = ERR_PTR(-ENODEV); | |
cedf8602 SH |
203 | devres_free(ptr); |
204 | goto err1; | |
205 | } | |
206 | ||
207 | *ptr = phy; | |
208 | devres_add(dev, ptr); | |
209 | ||
210 | get_device(phy->dev); | |
211 | ||
212 | err1: | |
213 | spin_unlock_irqrestore(&phy_lock, flags); | |
214 | ||
215 | err0: | |
216 | of_node_put(node); | |
217 | ||
218 | return phy; | |
219 | } | |
110ff6d0 | 220 | EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle); |
cedf8602 SH |
221 | |
222 | /** | |
223 | * usb_get_phy_dev - find the USB PHY | |
224 | * @dev - device that requests this phy | |
225 | * @index - the index of the phy | |
226 | * | |
227 | * Returns the phy driver, after getting a refcount to it; or | |
228 | * -ENODEV if there is no such phy. The caller is responsible for | |
229 | * calling usb_put_phy() to release that count. | |
230 | * | |
231 | * For use by USB host and peripheral drivers. | |
232 | */ | |
233 | struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index) | |
234 | { | |
235 | struct usb_phy *phy = NULL; | |
236 | unsigned long flags; | |
237 | ||
238 | spin_lock_irqsave(&phy_lock, flags); | |
239 | ||
240 | phy = __usb_find_phy_dev(dev, &phy_bind_list, index); | |
241 | if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) { | |
e9d123a5 | 242 | dev_dbg(dev, "unable to find transceiver\n"); |
2c4e3dbf AS |
243 | if (!IS_ERR(phy)) |
244 | phy = ERR_PTR(-ENODEV); | |
245 | ||
cedf8602 SH |
246 | goto err0; |
247 | } | |
248 | ||
249 | get_device(phy->dev); | |
250 | ||
251 | err0: | |
252 | spin_unlock_irqrestore(&phy_lock, flags); | |
253 | ||
254 | return phy; | |
255 | } | |
110ff6d0 | 256 | EXPORT_SYMBOL_GPL(usb_get_phy_dev); |
cedf8602 SH |
257 | |
258 | /** | |
259 | * devm_usb_get_phy_dev - find the USB PHY using device ptr and index | |
260 | * @dev - device that requests this phy | |
261 | * @index - the index of the phy | |
262 | * | |
263 | * Gets the phy using usb_get_phy_dev(), and associates a device with it using | |
264 | * devres. On driver detach, release function is invoked on the devres data, | |
265 | * then, devres data is freed. | |
266 | * | |
267 | * For use by USB host and peripheral drivers. | |
268 | */ | |
269 | struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index) | |
270 | { | |
271 | struct usb_phy **ptr, *phy; | |
272 | ||
273 | ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); | |
274 | if (!ptr) | |
275 | return NULL; | |
276 | ||
277 | phy = usb_get_phy_dev(dev, index); | |
278 | if (!IS_ERR(phy)) { | |
279 | *ptr = phy; | |
280 | devres_add(dev, ptr); | |
281 | } else | |
282 | devres_free(ptr); | |
283 | ||
284 | return phy; | |
285 | } | |
110ff6d0 | 286 | EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev); |
cedf8602 SH |
287 | |
288 | /** | |
289 | * devm_usb_put_phy - release the USB PHY | |
290 | * @dev - device that wants to release this phy | |
291 | * @phy - the phy returned by devm_usb_get_phy() | |
292 | * | |
293 | * destroys the devres associated with this phy and invokes usb_put_phy | |
294 | * to release the phy. | |
295 | * | |
296 | * For use by USB host and peripheral drivers. | |
297 | */ | |
298 | void devm_usb_put_phy(struct device *dev, struct usb_phy *phy) | |
299 | { | |
300 | int r; | |
301 | ||
302 | r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy); | |
303 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); | |
304 | } | |
110ff6d0 | 305 | EXPORT_SYMBOL_GPL(devm_usb_put_phy); |
cedf8602 SH |
306 | |
307 | /** | |
308 | * usb_put_phy - release the USB PHY | |
309 | * @x: the phy returned by usb_get_phy() | |
310 | * | |
311 | * Releases a refcount the caller received from usb_get_phy(). | |
312 | * | |
313 | * For use by USB host and peripheral drivers. | |
314 | */ | |
315 | void usb_put_phy(struct usb_phy *x) | |
316 | { | |
317 | if (x) { | |
318 | struct module *owner = x->dev->driver->owner; | |
319 | ||
320 | put_device(x->dev); | |
321 | module_put(owner); | |
322 | } | |
323 | } | |
110ff6d0 | 324 | EXPORT_SYMBOL_GPL(usb_put_phy); |
cedf8602 SH |
325 | |
326 | /** | |
327 | * usb_add_phy - declare the USB PHY | |
328 | * @x: the USB phy to be used; or NULL | |
329 | * @type - the type of this PHY | |
330 | * | |
331 | * This call is exclusively for use by phy drivers, which | |
332 | * coordinate the activities of drivers for host and peripheral | |
333 | * controllers, and in some cases for VBUS current regulation. | |
334 | */ | |
335 | int usb_add_phy(struct usb_phy *x, enum usb_phy_type type) | |
336 | { | |
337 | int ret = 0; | |
338 | unsigned long flags; | |
339 | struct usb_phy *phy; | |
340 | ||
341 | if (x->type != USB_PHY_TYPE_UNDEFINED) { | |
342 | dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); | |
343 | return -EINVAL; | |
344 | } | |
345 | ||
31e32227 NZ |
346 | ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); |
347 | ||
cedf8602 SH |
348 | spin_lock_irqsave(&phy_lock, flags); |
349 | ||
350 | list_for_each_entry(phy, &phy_list, head) { | |
351 | if (phy->type == type) { | |
352 | ret = -EBUSY; | |
353 | dev_err(x->dev, "transceiver type %s already exists\n", | |
354 | usb_phy_type_string(type)); | |
355 | goto out; | |
356 | } | |
357 | } | |
358 | ||
359 | x->type = type; | |
360 | list_add_tail(&x->head, &phy_list); | |
361 | ||
362 | out: | |
363 | spin_unlock_irqrestore(&phy_lock, flags); | |
364 | return ret; | |
365 | } | |
110ff6d0 | 366 | EXPORT_SYMBOL_GPL(usb_add_phy); |
cedf8602 SH |
367 | |
368 | /** | |
369 | * usb_add_phy_dev - declare the USB PHY | |
370 | * @x: the USB phy to be used; or NULL | |
371 | * | |
372 | * This call is exclusively for use by phy drivers, which | |
373 | * coordinate the activities of drivers for host and peripheral | |
374 | * controllers, and in some cases for VBUS current regulation. | |
375 | */ | |
376 | int usb_add_phy_dev(struct usb_phy *x) | |
377 | { | |
378 | struct usb_phy_bind *phy_bind; | |
379 | unsigned long flags; | |
380 | ||
381 | if (!x->dev) { | |
382 | dev_err(x->dev, "no device provided for PHY\n"); | |
383 | return -EINVAL; | |
384 | } | |
385 | ||
31e32227 NZ |
386 | ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier); |
387 | ||
cedf8602 SH |
388 | spin_lock_irqsave(&phy_lock, flags); |
389 | list_for_each_entry(phy_bind, &phy_bind_list, list) | |
390 | if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev)))) | |
391 | phy_bind->phy = x; | |
392 | ||
393 | list_add_tail(&x->head, &phy_list); | |
394 | ||
395 | spin_unlock_irqrestore(&phy_lock, flags); | |
396 | return 0; | |
397 | } | |
110ff6d0 | 398 | EXPORT_SYMBOL_GPL(usb_add_phy_dev); |
cedf8602 SH |
399 | |
400 | /** | |
401 | * usb_remove_phy - remove the OTG PHY | |
402 | * @x: the USB OTG PHY to be removed; | |
403 | * | |
404 | * This reverts the effects of usb_add_phy | |
405 | */ | |
406 | void usb_remove_phy(struct usb_phy *x) | |
407 | { | |
408 | unsigned long flags; | |
409 | struct usb_phy_bind *phy_bind; | |
410 | ||
411 | spin_lock_irqsave(&phy_lock, flags); | |
412 | if (x) { | |
413 | list_for_each_entry(phy_bind, &phy_bind_list, list) | |
414 | if (phy_bind->phy == x) | |
415 | phy_bind->phy = NULL; | |
416 | list_del(&x->head); | |
417 | } | |
418 | spin_unlock_irqrestore(&phy_lock, flags); | |
419 | } | |
110ff6d0 | 420 | EXPORT_SYMBOL_GPL(usb_remove_phy); |
cedf8602 SH |
421 | |
422 | /** | |
423 | * usb_bind_phy - bind the phy and the controller that uses the phy | |
424 | * @dev_name: the device name of the device that will bind to the phy | |
425 | * @index: index to specify the port number | |
426 | * @phy_dev_name: the device name of the phy | |
427 | * | |
428 | * Fills the phy_bind structure with the dev_name and phy_dev_name. This will | |
429 | * be used when the phy driver registers the phy and when the controller | |
430 | * requests this phy. | |
431 | * | |
432 | * To be used by platform specific initialization code. | |
433 | */ | |
19d8cedd | 434 | int usb_bind_phy(const char *dev_name, u8 index, |
cedf8602 SH |
435 | const char *phy_dev_name) |
436 | { | |
437 | struct usb_phy_bind *phy_bind; | |
438 | unsigned long flags; | |
439 | ||
440 | phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL); | |
e9d123a5 | 441 | if (!phy_bind) |
cedf8602 | 442 | return -ENOMEM; |
cedf8602 SH |
443 | |
444 | phy_bind->dev_name = dev_name; | |
445 | phy_bind->phy_dev_name = phy_dev_name; | |
446 | phy_bind->index = index; | |
447 | ||
448 | spin_lock_irqsave(&phy_lock, flags); | |
449 | list_add_tail(&phy_bind->list, &phy_bind_list); | |
450 | spin_unlock_irqrestore(&phy_lock, flags); | |
451 | ||
452 | return 0; | |
453 | } | |
454 | EXPORT_SYMBOL_GPL(usb_bind_phy); | |
df9f7b31 KR |
455 | |
456 | /** | |
457 | * usb_phy_set_event - set event to phy event | |
458 | * @x: the phy returned by usb_get_phy(); | |
459 | * | |
460 | * This sets event to phy event | |
461 | */ | |
462 | void usb_phy_set_event(struct usb_phy *x, unsigned long event) | |
463 | { | |
464 | x->last_event = event; | |
465 | } | |
466 | EXPORT_SYMBOL_GPL(usb_phy_set_event); |