blob: 4bb4333c9653f484419d98468a492ad8dadc006e [file] [log] [blame]
Tony Lindgren3cb22d62008-11-24 12:02:21 -08001/*
2 * otg.c -- USB OTG utility code
3 *
4 * Copyright (C) 2004 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
12#include <linux/kernel.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040013#include <linux/export.h>
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053014#include <linux/err.h>
Tony Lindgren3cb22d62008-11-24 12:02:21 -080015#include <linux/device.h>
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +053016#include <linux/slab.h>
Tony Lindgren3cb22d62008-11-24 12:02:21 -080017
18#include <linux/usb/otg.h>
19
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053020static LIST_HEAD(phy_list);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +053021static LIST_HEAD(phy_bind_list);
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053022static DEFINE_SPINLOCK(phy_lock);
23
24static struct usb_phy *__usb_find_phy(struct list_head *list,
25 enum usb_phy_type type)
26{
27 struct usb_phy *phy = NULL;
28
29 list_for_each_entry(phy, list, head) {
30 if (phy->type != type)
31 continue;
32
33 return phy;
34 }
35
36 return ERR_PTR(-ENODEV);
37}
Tony Lindgren3cb22d62008-11-24 12:02:21 -080038
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +053039static struct usb_phy *__usb_find_phy_dev(struct device *dev,
40 struct list_head *list, u8 index)
41{
42 struct usb_phy_bind *phy_bind = NULL;
43
44 list_for_each_entry(phy_bind, list, list) {
45 if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
46 phy_bind->index == index) {
47 if (phy_bind->phy)
48 return phy_bind->phy;
49 else
50 return ERR_PTR(-EPROBE_DEFER);
51 }
52 }
53
54 return ERR_PTR(-ENODEV);
55}
56
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +053057static void devm_usb_phy_release(struct device *dev, void *res)
58{
59 struct usb_phy *phy = *(struct usb_phy **)res;
60
61 usb_put_phy(phy);
62}
63
64static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
65{
66 return res == match_data;
67}
68
69/**
70 * devm_usb_get_phy - find the USB PHY
71 * @dev - device that requests this phy
72 * @type - the type of the phy the controller requires
73 *
74 * 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,
76 * then, devres data is freed.
77 *
78 * For use by USB host and peripheral drivers.
79 */
80struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
81{
82 struct usb_phy **ptr, *phy;
83
84 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
85 if (!ptr)
86 return NULL;
87
88 phy = usb_get_phy(type);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053089 if (!IS_ERR(phy)) {
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +053090 *ptr = phy;
91 devres_add(dev, ptr);
92 } else
93 devres_free(ptr);
94
95 return phy;
96}
97EXPORT_SYMBOL(devm_usb_get_phy);
98
Tony Lindgren3cb22d62008-11-24 12:02:21 -080099/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530100 * usb_get_phy - find the USB PHY
101 * @type - the type of the phy the controller requires
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800102 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530103 * Returns the phy driver, after getting a refcount to it; or
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530104 * -ENODEV if there is no such phy. The caller is responsible for
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530105 * calling usb_put_phy() to release that count.
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800106 *
107 * For use by USB host and peripheral drivers.
108 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530109struct usb_phy *usb_get_phy(enum usb_phy_type type)
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800110{
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530111 struct usb_phy *phy = NULL;
112 unsigned long flags;
113
114 spin_lock_irqsave(&phy_lock, flags);
115
116 phy = __usb_find_phy(&phy_list, type);
117 if (IS_ERR(phy)) {
118 pr_err("unable to find transceiver of type %s\n",
119 usb_phy_type_string(type));
Kishon Vijay Abraham If8ecf822012-07-02 12:20:24 +0530120 goto err0;
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530121 }
122
123 get_device(phy->dev);
124
Kishon Vijay Abraham If8ecf822012-07-02 12:20:24 +0530125err0:
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530126 spin_unlock_irqrestore(&phy_lock, flags);
127
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200128 return phy;
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800129}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530130EXPORT_SYMBOL(usb_get_phy);
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800131
132/**
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530133 * usb_get_phy_dev - find the USB PHY
134 * @dev - device that requests this phy
135 * @index - the index of the phy
136 *
137 * Returns the phy driver, after getting a refcount to it; or
138 * -ENODEV if there is no such phy. The caller is responsible for
139 * calling usb_put_phy() to release that count.
140 *
141 * For use by USB host and peripheral drivers.
142 */
143struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
144{
145 struct usb_phy *phy = NULL;
146 unsigned long flags;
147
148 spin_lock_irqsave(&phy_lock, flags);
149
150 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
151 if (IS_ERR(phy)) {
152 pr_err("unable to find transceiver\n");
153 goto err0;
154 }
155
156 get_device(phy->dev);
157
158err0:
159 spin_unlock_irqrestore(&phy_lock, flags);
160
161 return phy;
162}
163EXPORT_SYMBOL(usb_get_phy_dev);
164
165/**
166 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
167 * @dev - device that requests this phy
168 * @index - the index of the phy
169 *
170 * 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,
172 * then, devres data is freed.
173 *
174 * For use by USB host and peripheral drivers.
175 */
176struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
177{
178 struct usb_phy **ptr, *phy;
179
180 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
181 if (!ptr)
182 return NULL;
183
184 phy = usb_get_phy_dev(dev, index);
185 if (!IS_ERR(phy)) {
186 *ptr = phy;
187 devres_add(dev, ptr);
188 } else
189 devres_free(ptr);
190
191 return phy;
192}
193EXPORT_SYMBOL(devm_usb_get_phy_dev);
194
195/**
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +0530196 * devm_usb_put_phy - release the USB PHY
197 * @dev - device that wants to release this phy
198 * @phy - the phy returned by devm_usb_get_phy()
199 *
200 * destroys the devres associated with this phy and invokes usb_put_phy
201 * to release the phy.
202 *
203 * For use by USB host and peripheral drivers.
204 */
205void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
206{
207 int r;
208
209 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");
211}
212EXPORT_SYMBOL(devm_usb_put_phy);
213
214/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530215 * usb_put_phy - release the USB PHY
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530216 * @x: the phy returned by usb_get_phy()
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800217 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530218 * Releases a refcount the caller received from usb_get_phy().
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800219 *
220 * For use by USB host and peripheral drivers.
221 */
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530222void usb_put_phy(struct usb_phy *x)
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800223{
Robert Jarzmikecf85e42009-04-21 20:33:10 -0700224 if (x)
225 put_device(x->dev);
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800226}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530227EXPORT_SYMBOL(usb_put_phy);
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800228
229/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530230 * usb_add_phy - declare the USB PHY
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530231 * @x: the USB phy to be used; or NULL
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530232 * @type - the type of this PHY
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800233 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530234 * This call is exclusively for use by phy drivers, which
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800235 * coordinate the activities of drivers for host and peripheral
236 * controllers, and in some cases for VBUS current regulation.
237 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530238int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800239{
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530240 int ret = 0;
241 unsigned long flags;
242 struct usb_phy *phy;
243
Shubhrajyoti Ddf6791d2012-08-07 19:56:30 +0530244 if (x->type != USB_PHY_TYPE_UNDEFINED) {
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530245 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
246 return -EINVAL;
247 }
248
249 spin_lock_irqsave(&phy_lock, flags);
250
251 list_for_each_entry(phy, &phy_list, head) {
252 if (phy->type == type) {
253 ret = -EBUSY;
254 dev_err(x->dev, "transceiver type %s already exists\n",
255 usb_phy_type_string(type));
256 goto out;
257 }
258 }
259
260 x->type = type;
261 list_add_tail(&x->head, &phy_list);
262
263out:
264 spin_unlock_irqrestore(&phy_lock, flags);
265 return ret;
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800266}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530267EXPORT_SYMBOL(usb_add_phy);
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200268
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530269/**
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530270 * usb_add_phy_dev - declare the USB PHY
271 * @x: the USB phy to be used; or NULL
272 *
273 * This call is exclusively for use by phy drivers, which
274 * coordinate the activities of drivers for host and peripheral
275 * controllers, and in some cases for VBUS current regulation.
276 */
277int usb_add_phy_dev(struct usb_phy *x)
278{
279 struct usb_phy_bind *phy_bind;
280 unsigned long flags;
281
282 if (!x->dev) {
283 dev_err(x->dev, "no device provided for PHY\n");
284 return -EINVAL;
285 }
286
287 spin_lock_irqsave(&phy_lock, flags);
288 list_for_each_entry(phy_bind, &phy_bind_list, list)
289 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
290 phy_bind->phy = x;
291
292 list_add_tail(&x->head, &phy_list);
293
294 spin_unlock_irqrestore(&phy_lock, flags);
295 return 0;
296}
297EXPORT_SYMBOL(usb_add_phy_dev);
298
299/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530300 * usb_remove_phy - remove the OTG PHY
301 * @x: the USB OTG PHY to be removed;
302 *
303 * This reverts the effects of usb_add_phy
304 */
305void usb_remove_phy(struct usb_phy *x)
306{
307 unsigned long flags;
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530308 struct usb_phy_bind *phy_bind;
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530309
310 spin_lock_irqsave(&phy_lock, flags);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530311 if (x) {
312 list_for_each_entry(phy_bind, &phy_bind_list, list)
313 if (phy_bind->phy == x)
314 phy_bind->phy = NULL;
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530315 list_del(&x->head);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530316 }
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530317 spin_unlock_irqrestore(&phy_lock, flags);
318}
319EXPORT_SYMBOL(usb_remove_phy);
320
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530321/**
322 * 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
324 * @index: index to specify the port number
325 * @phy_dev_name: the device name of the phy
326 *
327 * 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
329 * requests this phy.
330 *
331 * To be used by platform specific initialization code.
332 */
333int __init usb_bind_phy(const char *dev_name, u8 index,
334 const char *phy_dev_name)
335{
336 struct usb_phy_bind *phy_bind;
337 unsigned long flags;
338
339 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
340 if (!phy_bind) {
341 pr_err("phy_bind(): No memory for phy_bind");
342 return -ENOMEM;
343 }
344
345 phy_bind->dev_name = dev_name;
346 phy_bind->phy_dev_name = phy_dev_name;
347 phy_bind->index = index;
348
349 spin_lock_irqsave(&phy_lock, flags);
350 list_add_tail(&phy_bind->list, &phy_bind_list);
351 spin_unlock_irqrestore(&phy_lock, flags);
352
353 return 0;
354}
355EXPORT_SYMBOL_GPL(usb_bind_phy);
356
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200357const char *otg_state_string(enum usb_otg_state state)
358{
359 switch (state) {
360 case OTG_STATE_A_IDLE:
361 return "a_idle";
362 case OTG_STATE_A_WAIT_VRISE:
363 return "a_wait_vrise";
364 case OTG_STATE_A_WAIT_BCON:
365 return "a_wait_bcon";
366 case OTG_STATE_A_HOST:
367 return "a_host";
368 case OTG_STATE_A_SUSPEND:
369 return "a_suspend";
370 case OTG_STATE_A_PERIPHERAL:
371 return "a_peripheral";
372 case OTG_STATE_A_WAIT_VFALL:
373 return "a_wait_vfall";
374 case OTG_STATE_A_VBUS_ERR:
375 return "a_vbus_err";
376 case OTG_STATE_B_IDLE:
377 return "b_idle";
378 case OTG_STATE_B_SRP_INIT:
379 return "b_srp_init";
380 case OTG_STATE_B_PERIPHERAL:
381 return "b_peripheral";
382 case OTG_STATE_B_WAIT_ACON:
383 return "b_wait_acon";
384 case OTG_STATE_B_HOST:
385 return "b_host";
386 default:
387 return "UNDEFINED";
388 }
389}
390EXPORT_SYMBOL(otg_state_string);