]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/otg/otg.c
8e756d95a28f029b0db9c417d9b1dbdb6fa7df2e
[karo-tx-linux.git] / drivers / usb / otg / otg.c
1 /*
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>
13 #include <linux/export.h>
14 #include <linux/err.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17
18 #include <linux/usb/otg.h>
19
20 static LIST_HEAD(phy_list);
21 static LIST_HEAD(phy_bind_list);
22 static DEFINE_SPINLOCK(phy_lock);
23
24 static 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 }
38
39 static void devm_usb_phy_release(struct device *dev, void *res)
40 {
41         struct usb_phy *phy = *(struct usb_phy **)res;
42
43         usb_put_phy(phy);
44 }
45
46 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
47 {
48         return res == match_data;
49 }
50
51 /**
52  * devm_usb_get_phy - find the USB PHY
53  * @dev - device that requests this phy
54  * @type - the type of the phy the controller requires
55  *
56  * Gets the phy using usb_get_phy(), and associates a device with it using
57  * devres. On driver detach, release function is invoked on the devres data,
58  * then, devres data is freed.
59  *
60  * For use by USB host and peripheral drivers.
61  */
62 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
63 {
64         struct usb_phy **ptr, *phy;
65
66         ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
67         if (!ptr)
68                 return NULL;
69
70         phy = usb_get_phy(type);
71         if (!IS_ERR(phy)) {
72                 *ptr = phy;
73                 devres_add(dev, ptr);
74         } else
75                 devres_free(ptr);
76
77         return phy;
78 }
79 EXPORT_SYMBOL(devm_usb_get_phy);
80
81 /**
82  * usb_get_phy - find the USB PHY
83  * @type - the type of the phy the controller requires
84  *
85  * Returns the phy driver, after getting a refcount to it; or
86  * -ENODEV if there is no such phy.  The caller is responsible for
87  * calling usb_put_phy() to release that count.
88  *
89  * For use by USB host and peripheral drivers.
90  */
91 struct usb_phy *usb_get_phy(enum usb_phy_type type)
92 {
93         struct usb_phy  *phy = NULL;
94         unsigned long   flags;
95
96         spin_lock_irqsave(&phy_lock, flags);
97
98         phy = __usb_find_phy(&phy_list, type);
99         if (IS_ERR(phy)) {
100                 pr_err("unable to find transceiver of type %s\n",
101                         usb_phy_type_string(type));
102                 goto err0;
103         }
104
105         get_device(phy->dev);
106
107 err0:
108         spin_unlock_irqrestore(&phy_lock, flags);
109
110         return phy;
111 }
112 EXPORT_SYMBOL(usb_get_phy);
113
114 /**
115  * devm_usb_put_phy - release the USB PHY
116  * @dev - device that wants to release this phy
117  * @phy - the phy returned by devm_usb_get_phy()
118  *
119  * destroys the devres associated with this phy and invokes usb_put_phy
120  * to release the phy.
121  *
122  * For use by USB host and peripheral drivers.
123  */
124 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
125 {
126         int r;
127
128         r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
129         dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
130 }
131 EXPORT_SYMBOL(devm_usb_put_phy);
132
133 /**
134  * usb_put_phy - release the USB PHY
135  * @x: the phy returned by usb_get_phy()
136  *
137  * Releases a refcount the caller received from usb_get_phy().
138  *
139  * For use by USB host and peripheral drivers.
140  */
141 void usb_put_phy(struct usb_phy *x)
142 {
143         if (x)
144                 put_device(x->dev);
145 }
146 EXPORT_SYMBOL(usb_put_phy);
147
148 /**
149  * usb_add_phy - declare the USB PHY
150  * @x: the USB phy to be used; or NULL
151  * @type - the type of this PHY
152  *
153  * This call is exclusively for use by phy drivers, which
154  * coordinate the activities of drivers for host and peripheral
155  * controllers, and in some cases for VBUS current regulation.
156  */
157 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
158 {
159         int             ret = 0;
160         unsigned long   flags;
161         struct usb_phy  *phy;
162
163         if (x->type != USB_PHY_TYPE_UNDEFINED) {
164                 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
165                 return -EINVAL;
166         }
167
168         spin_lock_irqsave(&phy_lock, flags);
169
170         list_for_each_entry(phy, &phy_list, head) {
171                 if (phy->type == type) {
172                         ret = -EBUSY;
173                         dev_err(x->dev, "transceiver type %s already exists\n",
174                                                 usb_phy_type_string(type));
175                         goto out;
176                 }
177         }
178
179         x->type = type;
180         list_add_tail(&x->head, &phy_list);
181
182 out:
183         spin_unlock_irqrestore(&phy_lock, flags);
184         return ret;
185 }
186 EXPORT_SYMBOL(usb_add_phy);
187
188 /**
189  * usb_remove_phy - remove the OTG PHY
190  * @x: the USB OTG PHY to be removed;
191  *
192  * This reverts the effects of usb_add_phy
193  */
194 void usb_remove_phy(struct usb_phy *x)
195 {
196         unsigned long   flags;
197
198         spin_lock_irqsave(&phy_lock, flags);
199         if (x)
200                 list_del(&x->head);
201         spin_unlock_irqrestore(&phy_lock, flags);
202 }
203 EXPORT_SYMBOL(usb_remove_phy);
204
205 /**
206  * usb_bind_phy - bind the phy and the controller that uses the phy
207  * @dev_name: the device name of the device that will bind to the phy
208  * @index: index to specify the port number
209  * @phy_dev_name: the device name of the phy
210  *
211  * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
212  * be used when the phy driver registers the phy and when the controller
213  * requests this phy.
214  *
215  * To be used by platform specific initialization code.
216  */
217 int __init usb_bind_phy(const char *dev_name, u8 index,
218                                 const char *phy_dev_name)
219 {
220         struct usb_phy_bind *phy_bind;
221         unsigned long flags;
222
223         phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
224         if (!phy_bind) {
225                 pr_err("phy_bind(): No memory for phy_bind");
226                 return -ENOMEM;
227         }
228
229         phy_bind->dev_name = dev_name;
230         phy_bind->phy_dev_name = phy_dev_name;
231         phy_bind->index = index;
232
233         spin_lock_irqsave(&phy_lock, flags);
234         list_add_tail(&phy_bind->list, &phy_bind_list);
235         spin_unlock_irqrestore(&phy_lock, flags);
236
237         return 0;
238 }
239 EXPORT_SYMBOL_GPL(usb_bind_phy);
240
241 const char *otg_state_string(enum usb_otg_state state)
242 {
243         switch (state) {
244         case OTG_STATE_A_IDLE:
245                 return "a_idle";
246         case OTG_STATE_A_WAIT_VRISE:
247                 return "a_wait_vrise";
248         case OTG_STATE_A_WAIT_BCON:
249                 return "a_wait_bcon";
250         case OTG_STATE_A_HOST:
251                 return "a_host";
252         case OTG_STATE_A_SUSPEND:
253                 return "a_suspend";
254         case OTG_STATE_A_PERIPHERAL:
255                 return "a_peripheral";
256         case OTG_STATE_A_WAIT_VFALL:
257                 return "a_wait_vfall";
258         case OTG_STATE_A_VBUS_ERR:
259                 return "a_vbus_err";
260         case OTG_STATE_B_IDLE:
261                 return "b_idle";
262         case OTG_STATE_B_SRP_INIT:
263                 return "b_srp_init";
264         case OTG_STATE_B_PERIPHERAL:
265                 return "b_peripheral";
266         case OTG_STATE_B_WAIT_ACON:
267                 return "b_wait_acon";
268         case OTG_STATE_B_HOST:
269                 return "b_host";
270         default:
271                 return "UNDEFINED";
272         }
273 }
274 EXPORT_SYMBOL(otg_state_string);