NFS: Don't clear desc->pg_moreio in nfs_do_recoalesce()
[linux/fpc-iii.git] / drivers / usb / phy / phy.c
blobd1cd6b50f5204898f0ed11785eecc73d1d6f5ebd
1 /*
2 * phy.c -- USB phy handling
4 * Copyright (C) 2004-2013 Texas Instruments
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.
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>
19 #include <linux/usb/phy.h>
21 static LIST_HEAD(phy_list);
22 static LIST_HEAD(phy_bind_list);
23 static DEFINE_SPINLOCK(phy_lock);
25 static struct usb_phy *__usb_find_phy(struct list_head *list,
26 enum usb_phy_type type)
28 struct usb_phy *phy = NULL;
30 list_for_each_entry(phy, list, head) {
31 if (phy->type != type)
32 continue;
34 return phy;
37 return ERR_PTR(-ENODEV);
40 static struct usb_phy *__usb_find_phy_dev(struct device *dev,
41 struct list_head *list, u8 index)
43 struct usb_phy_bind *phy_bind = NULL;
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);
55 return ERR_PTR(-ENODEV);
58 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
60 struct usb_phy *phy;
62 if (!of_device_is_available(node))
63 return ERR_PTR(-ENODEV);
65 list_for_each_entry(phy, &phy_list, head) {
66 if (node != phy->dev->of_node)
67 continue;
69 return phy;
72 return ERR_PTR(-EPROBE_DEFER);
75 static void devm_usb_phy_release(struct device *dev, void *res)
77 struct usb_phy *phy = *(struct usb_phy **)res;
79 usb_put_phy(phy);
82 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
84 struct usb_phy **phy = res;
86 return *phy == match_data;
89 /**
90 * devm_usb_get_phy - find the USB PHY
91 * @dev - device that requests this phy
92 * @type - the type of the phy the controller requires
94 * Gets the phy using usb_get_phy(), and associates a device with it using
95 * devres. On driver detach, release function is invoked on the devres data,
96 * then, devres data is freed.
98 * For use by USB host and peripheral drivers.
100 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
102 struct usb_phy **ptr, *phy;
104 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
105 if (!ptr)
106 return ERR_PTR(-ENOMEM);
108 phy = usb_get_phy(type);
109 if (!IS_ERR(phy)) {
110 *ptr = phy;
111 devres_add(dev, ptr);
112 } else
113 devres_free(ptr);
115 return phy;
117 EXPORT_SYMBOL_GPL(devm_usb_get_phy);
120 * usb_get_phy - find the USB PHY
121 * @type - the type of the phy the controller requires
123 * Returns the phy driver, after getting a refcount to it; or
124 * -ENODEV if there is no such phy. The caller is responsible for
125 * calling usb_put_phy() to release that count.
127 * For use by USB host and peripheral drivers.
129 struct usb_phy *usb_get_phy(enum usb_phy_type type)
131 struct usb_phy *phy = NULL;
132 unsigned long flags;
134 spin_lock_irqsave(&phy_lock, flags);
136 phy = __usb_find_phy(&phy_list, type);
137 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
138 pr_debug("PHY: unable to find transceiver of type %s\n",
139 usb_phy_type_string(type));
140 if (!IS_ERR(phy))
141 phy = ERR_PTR(-ENODEV);
143 goto err0;
146 get_device(phy->dev);
148 err0:
149 spin_unlock_irqrestore(&phy_lock, flags);
151 return phy;
153 EXPORT_SYMBOL_GPL(usb_get_phy);
156 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
157 * @dev - device that requests this phy
158 * @phandle - name of the property holding the phy phandle value
159 * @index - the index of the phy
161 * Returns the phy driver associated with the given phandle value,
162 * after getting a refcount to it, -ENODEV if there is no such phy or
163 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
164 * not yet loaded. While at that, it also associates the device with
165 * the phy using devres. On driver detach, release function is invoked
166 * on the devres data, then, devres data is freed.
168 * For use by USB host and peripheral drivers.
170 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
171 const char *phandle, u8 index)
173 struct usb_phy *phy = ERR_PTR(-ENOMEM), **ptr;
174 unsigned long flags;
175 struct device_node *node;
177 if (!dev->of_node) {
178 dev_dbg(dev, "device does not have a device node entry\n");
179 return ERR_PTR(-EINVAL);
182 node = of_parse_phandle(dev->of_node, phandle, index);
183 if (!node) {
184 dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
185 dev->of_node->full_name);
186 return ERR_PTR(-ENODEV);
189 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
190 if (!ptr) {
191 dev_dbg(dev, "failed to allocate memory for devres\n");
192 goto err0;
195 spin_lock_irqsave(&phy_lock, flags);
197 phy = __of_usb_find_phy(node);
198 if (IS_ERR(phy)) {
199 devres_free(ptr);
200 goto err1;
203 if (!try_module_get(phy->dev->driver->owner)) {
204 phy = ERR_PTR(-ENODEV);
205 devres_free(ptr);
206 goto err1;
209 *ptr = phy;
210 devres_add(dev, ptr);
212 get_device(phy->dev);
214 err1:
215 spin_unlock_irqrestore(&phy_lock, flags);
217 err0:
218 of_node_put(node);
220 return phy;
222 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
225 * usb_get_phy_dev - find the USB PHY
226 * @dev - device that requests this phy
227 * @index - the index of the phy
229 * Returns the phy driver, after getting a refcount to it; or
230 * -ENODEV if there is no such phy. The caller is responsible for
231 * calling usb_put_phy() to release that count.
233 * For use by USB host and peripheral drivers.
235 struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
237 struct usb_phy *phy = NULL;
238 unsigned long flags;
240 spin_lock_irqsave(&phy_lock, flags);
242 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
243 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
244 dev_dbg(dev, "unable to find transceiver\n");
245 if (!IS_ERR(phy))
246 phy = ERR_PTR(-ENODEV);
248 goto err0;
251 get_device(phy->dev);
253 err0:
254 spin_unlock_irqrestore(&phy_lock, flags);
256 return phy;
258 EXPORT_SYMBOL_GPL(usb_get_phy_dev);
261 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
262 * @dev - device that requests this phy
263 * @index - the index of the phy
265 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
266 * devres. On driver detach, release function is invoked on the devres data,
267 * then, devres data is freed.
269 * For use by USB host and peripheral drivers.
271 struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
273 struct usb_phy **ptr, *phy;
275 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
276 if (!ptr)
277 return NULL;
279 phy = usb_get_phy_dev(dev, index);
280 if (!IS_ERR(phy)) {
281 *ptr = phy;
282 devres_add(dev, ptr);
283 } else
284 devres_free(ptr);
286 return phy;
288 EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
291 * devm_usb_put_phy - release the USB PHY
292 * @dev - device that wants to release this phy
293 * @phy - the phy returned by devm_usb_get_phy()
295 * destroys the devres associated with this phy and invokes usb_put_phy
296 * to release the phy.
298 * For use by USB host and peripheral drivers.
300 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
302 int r;
304 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
305 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
307 EXPORT_SYMBOL_GPL(devm_usb_put_phy);
310 * usb_put_phy - release the USB PHY
311 * @x: the phy returned by usb_get_phy()
313 * Releases a refcount the caller received from usb_get_phy().
315 * For use by USB host and peripheral drivers.
317 void usb_put_phy(struct usb_phy *x)
319 if (x) {
320 struct module *owner = x->dev->driver->owner;
322 put_device(x->dev);
323 module_put(owner);
326 EXPORT_SYMBOL_GPL(usb_put_phy);
329 * usb_add_phy - declare the USB PHY
330 * @x: the USB phy to be used; or NULL
331 * @type - the type of this PHY
333 * This call is exclusively for use by phy drivers, which
334 * coordinate the activities of drivers for host and peripheral
335 * controllers, and in some cases for VBUS current regulation.
337 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
339 int ret = 0;
340 unsigned long flags;
341 struct usb_phy *phy;
343 if (x->type != USB_PHY_TYPE_UNDEFINED) {
344 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
345 return -EINVAL;
348 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
350 spin_lock_irqsave(&phy_lock, flags);
352 list_for_each_entry(phy, &phy_list, head) {
353 if (phy->type == type) {
354 ret = -EBUSY;
355 dev_err(x->dev, "transceiver type %s already exists\n",
356 usb_phy_type_string(type));
357 goto out;
361 x->type = type;
362 list_add_tail(&x->head, &phy_list);
364 out:
365 spin_unlock_irqrestore(&phy_lock, flags);
366 return ret;
368 EXPORT_SYMBOL_GPL(usb_add_phy);
371 * usb_add_phy_dev - declare the USB PHY
372 * @x: the USB phy to be used; or NULL
374 * This call is exclusively for use by phy drivers, which
375 * coordinate the activities of drivers for host and peripheral
376 * controllers, and in some cases for VBUS current regulation.
378 int usb_add_phy_dev(struct usb_phy *x)
380 struct usb_phy_bind *phy_bind;
381 unsigned long flags;
383 if (!x->dev) {
384 dev_err(x->dev, "no device provided for PHY\n");
385 return -EINVAL;
388 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
390 spin_lock_irqsave(&phy_lock, flags);
391 list_for_each_entry(phy_bind, &phy_bind_list, list)
392 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
393 phy_bind->phy = x;
395 list_add_tail(&x->head, &phy_list);
397 spin_unlock_irqrestore(&phy_lock, flags);
398 return 0;
400 EXPORT_SYMBOL_GPL(usb_add_phy_dev);
403 * usb_remove_phy - remove the OTG PHY
404 * @x: the USB OTG PHY to be removed;
406 * This reverts the effects of usb_add_phy
408 void usb_remove_phy(struct usb_phy *x)
410 unsigned long flags;
411 struct usb_phy_bind *phy_bind;
413 spin_lock_irqsave(&phy_lock, flags);
414 if (x) {
415 list_for_each_entry(phy_bind, &phy_bind_list, list)
416 if (phy_bind->phy == x)
417 phy_bind->phy = NULL;
418 list_del(&x->head);
420 spin_unlock_irqrestore(&phy_lock, flags);
422 EXPORT_SYMBOL_GPL(usb_remove_phy);
425 * usb_bind_phy - bind the phy and the controller that uses the phy
426 * @dev_name: the device name of the device that will bind to the phy
427 * @index: index to specify the port number
428 * @phy_dev_name: the device name of the phy
430 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
431 * be used when the phy driver registers the phy and when the controller
432 * requests this phy.
434 * To be used by platform specific initialization code.
436 int usb_bind_phy(const char *dev_name, u8 index,
437 const char *phy_dev_name)
439 struct usb_phy_bind *phy_bind;
440 unsigned long flags;
442 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
443 if (!phy_bind)
444 return -ENOMEM;
446 phy_bind->dev_name = dev_name;
447 phy_bind->phy_dev_name = phy_dev_name;
448 phy_bind->index = index;
450 spin_lock_irqsave(&phy_lock, flags);
451 list_add_tail(&phy_bind->list, &phy_bind_list);
452 spin_unlock_irqrestore(&phy_lock, flags);
454 return 0;
456 EXPORT_SYMBOL_GPL(usb_bind_phy);
459 * usb_phy_set_event - set event to phy event
460 * @x: the phy returned by usb_get_phy();
462 * This sets event to phy event
464 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
466 x->last_event = event;
468 EXPORT_SYMBOL_GPL(usb_phy_set_event);