blob: 5f79d8e2caab76219edc887e936f94045d154cf7 [file] [log] [blame]
Sascha Hauercedf8602013-02-27 15:16:28 +01001/*
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
21static LIST_HEAD(phy_list);
22static LIST_HEAD(phy_bind_list);
23static DEFINE_SPINLOCK(phy_lock);
24
25static 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
37 return ERR_PTR(-ENODEV);
38}
39
40static 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
58static struct usb_phy *__of_usb_find_phy(struct device_node *node)
59{
60 struct usb_phy *phy;
61
62 list_for_each_entry(phy, &phy_list, head) {
63 if (node != phy->dev->of_node)
64 continue;
65
66 return phy;
67 }
68
69 return ERR_PTR(-ENODEV);
70}
71
72static void devm_usb_phy_release(struct device *dev, void *res)
73{
74 struct usb_phy *phy = *(struct usb_phy **)res;
75
76 usb_put_phy(phy);
77}
78
79static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
80{
Axel Lin99cecd32015-03-12 09:15:28 +080081 struct usb_phy **phy = res;
82
83 return *phy == match_data;
Sascha Hauercedf8602013-02-27 15:16:28 +010084}
85
86/**
87 * devm_usb_get_phy - find the USB PHY
88 * @dev - device that requests this phy
89 * @type - the type of the phy the controller requires
90 *
91 * Gets the phy using usb_get_phy(), and associates a device with it using
92 * devres. On driver detach, release function is invoked on the devres data,
93 * then, devres data is freed.
94 *
95 * For use by USB host and peripheral drivers.
96 */
97struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
98{
99 struct usb_phy **ptr, *phy;
100
101 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
102 if (!ptr)
103 return NULL;
104
105 phy = usb_get_phy(type);
106 if (!IS_ERR(phy)) {
107 *ptr = phy;
108 devres_add(dev, ptr);
109 } else
110 devres_free(ptr);
111
112 return phy;
113}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200114EXPORT_SYMBOL_GPL(devm_usb_get_phy);
Sascha Hauercedf8602013-02-27 15:16:28 +0100115
116/**
117 * usb_get_phy - find the USB PHY
118 * @type - the type of the phy the controller requires
119 *
120 * Returns the phy driver, after getting a refcount to it; or
121 * -ENODEV if there is no such phy. The caller is responsible for
122 * calling usb_put_phy() to release that count.
123 *
124 * For use by USB host and peripheral drivers.
125 */
126struct usb_phy *usb_get_phy(enum usb_phy_type type)
127{
128 struct usb_phy *phy = NULL;
129 unsigned long flags;
130
131 spin_lock_irqsave(&phy_lock, flags);
132
133 phy = __usb_find_phy(&phy_list, type);
134 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
135 pr_err("unable to find transceiver of type %s\n",
136 usb_phy_type_string(type));
137 goto err0;
138 }
139
140 get_device(phy->dev);
141
142err0:
143 spin_unlock_irqrestore(&phy_lock, flags);
144
145 return phy;
146}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200147EXPORT_SYMBOL_GPL(usb_get_phy);
Sascha Hauercedf8602013-02-27 15:16:28 +0100148
149 /**
150 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
151 * @dev - device that requests this phy
152 * @phandle - name of the property holding the phy phandle value
153 * @index - the index of the phy
154 *
155 * Returns the phy driver associated with the given phandle value,
156 * after getting a refcount to it, -ENODEV if there is no such phy or
157 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
158 * not yet loaded. While at that, it also associates the device with
159 * the phy using devres. On driver detach, release function is invoked
160 * on the devres data, then, devres data is freed.
161 *
162 * For use by USB host and peripheral drivers.
163 */
164struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
165 const char *phandle, u8 index)
166{
167 struct usb_phy *phy = ERR_PTR(-ENOMEM), **ptr;
168 unsigned long flags;
169 struct device_node *node;
170
171 if (!dev->of_node) {
172 dev_dbg(dev, "device does not have a device node entry\n");
173 return ERR_PTR(-EINVAL);
174 }
175
176 node = of_parse_phandle(dev->of_node, phandle, index);
177 if (!node) {
178 dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
179 dev->of_node->full_name);
180 return ERR_PTR(-ENODEV);
181 }
182
183 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
184 if (!ptr) {
185 dev_dbg(dev, "failed to allocate memory for devres\n");
186 goto err0;
187 }
188
189 spin_lock_irqsave(&phy_lock, flags);
190
191 phy = __of_usb_find_phy(node);
192 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
193 phy = ERR_PTR(-EPROBE_DEFER);
194 devres_free(ptr);
195 goto err1;
196 }
197
198 *ptr = phy;
199 devres_add(dev, ptr);
200
201 get_device(phy->dev);
202
203err1:
204 spin_unlock_irqrestore(&phy_lock, flags);
205
206err0:
207 of_node_put(node);
208
209 return phy;
210}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200211EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
Sascha Hauercedf8602013-02-27 15:16:28 +0100212
213/**
214 * usb_get_phy_dev - find the USB PHY
215 * @dev - device that requests this phy
216 * @index - the index of the phy
217 *
218 * Returns the phy driver, after getting a refcount to it; or
219 * -ENODEV if there is no such phy. The caller is responsible for
220 * calling usb_put_phy() to release that count.
221 *
222 * For use by USB host and peripheral drivers.
223 */
224struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
225{
226 struct usb_phy *phy = NULL;
227 unsigned long flags;
228
229 spin_lock_irqsave(&phy_lock, flags);
230
231 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
232 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
233 pr_err("unable to find transceiver\n");
234 goto err0;
235 }
236
237 get_device(phy->dev);
238
239err0:
240 spin_unlock_irqrestore(&phy_lock, flags);
241
242 return phy;
243}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200244EXPORT_SYMBOL_GPL(usb_get_phy_dev);
Sascha Hauercedf8602013-02-27 15:16:28 +0100245
246/**
247 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
248 * @dev - device that requests this phy
249 * @index - the index of the phy
250 *
251 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
252 * devres. On driver detach, release function is invoked on the devres data,
253 * then, devres data is freed.
254 *
255 * For use by USB host and peripheral drivers.
256 */
257struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
258{
259 struct usb_phy **ptr, *phy;
260
261 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
262 if (!ptr)
263 return NULL;
264
265 phy = usb_get_phy_dev(dev, index);
266 if (!IS_ERR(phy)) {
267 *ptr = phy;
268 devres_add(dev, ptr);
269 } else
270 devres_free(ptr);
271
272 return phy;
273}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200274EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
Sascha Hauercedf8602013-02-27 15:16:28 +0100275
276/**
277 * devm_usb_put_phy - release the USB PHY
278 * @dev - device that wants to release this phy
279 * @phy - the phy returned by devm_usb_get_phy()
280 *
281 * destroys the devres associated with this phy and invokes usb_put_phy
282 * to release the phy.
283 *
284 * For use by USB host and peripheral drivers.
285 */
286void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
287{
288 int r;
289
290 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
291 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
292}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200293EXPORT_SYMBOL_GPL(devm_usb_put_phy);
Sascha Hauercedf8602013-02-27 15:16:28 +0100294
295/**
296 * usb_put_phy - release the USB PHY
297 * @x: the phy returned by usb_get_phy()
298 *
299 * Releases a refcount the caller received from usb_get_phy().
300 *
301 * For use by USB host and peripheral drivers.
302 */
303void usb_put_phy(struct usb_phy *x)
304{
305 if (x) {
306 struct module *owner = x->dev->driver->owner;
307
308 put_device(x->dev);
309 module_put(owner);
310 }
311}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200312EXPORT_SYMBOL_GPL(usb_put_phy);
Sascha Hauercedf8602013-02-27 15:16:28 +0100313
314/**
315 * usb_add_phy - declare the USB PHY
316 * @x: the USB phy to be used; or NULL
317 * @type - the type of this PHY
318 *
319 * This call is exclusively for use by phy drivers, which
320 * coordinate the activities of drivers for host and peripheral
321 * controllers, and in some cases for VBUS current regulation.
322 */
323int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
324{
325 int ret = 0;
326 unsigned long flags;
327 struct usb_phy *phy;
328
329 if (x->type != USB_PHY_TYPE_UNDEFINED) {
330 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
331 return -EINVAL;
332 }
333
334 spin_lock_irqsave(&phy_lock, flags);
335
336 list_for_each_entry(phy, &phy_list, head) {
337 if (phy->type == type) {
338 ret = -EBUSY;
339 dev_err(x->dev, "transceiver type %s already exists\n",
340 usb_phy_type_string(type));
341 goto out;
342 }
343 }
344
345 x->type = type;
346 list_add_tail(&x->head, &phy_list);
347
348out:
349 spin_unlock_irqrestore(&phy_lock, flags);
350 return ret;
351}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200352EXPORT_SYMBOL_GPL(usb_add_phy);
Sascha Hauercedf8602013-02-27 15:16:28 +0100353
354/**
355 * usb_add_phy_dev - declare the USB PHY
356 * @x: the USB phy to be used; or NULL
357 *
358 * This call is exclusively for use by phy drivers, which
359 * coordinate the activities of drivers for host and peripheral
360 * controllers, and in some cases for VBUS current regulation.
361 */
362int usb_add_phy_dev(struct usb_phy *x)
363{
364 struct usb_phy_bind *phy_bind;
365 unsigned long flags;
366
367 if (!x->dev) {
368 dev_err(x->dev, "no device provided for PHY\n");
369 return -EINVAL;
370 }
371
372 spin_lock_irqsave(&phy_lock, flags);
373 list_for_each_entry(phy_bind, &phy_bind_list, list)
374 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
375 phy_bind->phy = x;
376
377 list_add_tail(&x->head, &phy_list);
378
379 spin_unlock_irqrestore(&phy_lock, flags);
380 return 0;
381}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200382EXPORT_SYMBOL_GPL(usb_add_phy_dev);
Sascha Hauercedf8602013-02-27 15:16:28 +0100383
384/**
385 * usb_remove_phy - remove the OTG PHY
386 * @x: the USB OTG PHY to be removed;
387 *
388 * This reverts the effects of usb_add_phy
389 */
390void usb_remove_phy(struct usb_phy *x)
391{
392 unsigned long flags;
393 struct usb_phy_bind *phy_bind;
394
395 spin_lock_irqsave(&phy_lock, flags);
396 if (x) {
397 list_for_each_entry(phy_bind, &phy_bind_list, list)
398 if (phy_bind->phy == x)
399 phy_bind->phy = NULL;
400 list_del(&x->head);
401 }
402 spin_unlock_irqrestore(&phy_lock, flags);
403}
Felipe Balbi110ff6d02013-03-07 10:49:27 +0200404EXPORT_SYMBOL_GPL(usb_remove_phy);
Sascha Hauercedf8602013-02-27 15:16:28 +0100405
406/**
407 * usb_bind_phy - bind the phy and the controller that uses the phy
408 * @dev_name: the device name of the device that will bind to the phy
409 * @index: index to specify the port number
410 * @phy_dev_name: the device name of the phy
411 *
412 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
413 * be used when the phy driver registers the phy and when the controller
414 * requests this phy.
415 *
416 * To be used by platform specific initialization code.
417 */
Denis Efremov19d8ced2013-04-18 17:13:31 +0400418int usb_bind_phy(const char *dev_name, u8 index,
Sascha Hauercedf8602013-02-27 15:16:28 +0100419 const char *phy_dev_name)
420{
421 struct usb_phy_bind *phy_bind;
422 unsigned long flags;
423
424 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
425 if (!phy_bind) {
426 pr_err("phy_bind(): No memory for phy_bind");
427 return -ENOMEM;
428 }
429
430 phy_bind->dev_name = dev_name;
431 phy_bind->phy_dev_name = phy_dev_name;
432 phy_bind->index = index;
433
434 spin_lock_irqsave(&phy_lock, flags);
435 list_add_tail(&phy_bind->list, &phy_bind_list);
436 spin_unlock_irqrestore(&phy_lock, flags);
437
438 return 0;
439}
440EXPORT_SYMBOL_GPL(usb_bind_phy);