pty: Repair TIOCGPTPEER
[cris-mirror.git] / drivers / usb / phy / phy.c
blob032f5afaad4b1be30e64b0cb1f7644f9ba696b00
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 struct phy_devm {
26 struct usb_phy *phy;
27 struct notifier_block *nb;
30 static struct usb_phy *__usb_find_phy(struct list_head *list,
31 enum usb_phy_type type)
33 struct usb_phy *phy = NULL;
35 list_for_each_entry(phy, list, head) {
36 if (phy->type != type)
37 continue;
39 return phy;
42 return ERR_PTR(-ENODEV);
45 static struct usb_phy *__usb_find_phy_dev(struct device *dev,
46 struct list_head *list, u8 index)
48 struct usb_phy_bind *phy_bind = NULL;
50 list_for_each_entry(phy_bind, list, list) {
51 if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
52 phy_bind->index == index) {
53 if (phy_bind->phy)
54 return phy_bind->phy;
55 else
56 return ERR_PTR(-EPROBE_DEFER);
60 return ERR_PTR(-ENODEV);
63 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
65 struct usb_phy *phy;
67 if (!of_device_is_available(node))
68 return ERR_PTR(-ENODEV);
70 list_for_each_entry(phy, &phy_list, head) {
71 if (node != phy->dev->of_node)
72 continue;
74 return phy;
77 return ERR_PTR(-EPROBE_DEFER);
80 static void devm_usb_phy_release(struct device *dev, void *res)
82 struct usb_phy *phy = *(struct usb_phy **)res;
84 usb_put_phy(phy);
87 static void devm_usb_phy_release2(struct device *dev, void *_res)
89 struct phy_devm *res = _res;
91 if (res->nb)
92 usb_unregister_notifier(res->phy, res->nb);
93 usb_put_phy(res->phy);
96 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
98 struct usb_phy **phy = res;
100 return *phy == match_data;
103 static int usb_add_extcon(struct usb_phy *x)
105 int ret;
107 if (of_property_read_bool(x->dev->of_node, "extcon")) {
108 x->edev = extcon_get_edev_by_phandle(x->dev, 0);
109 if (IS_ERR(x->edev))
110 return PTR_ERR(x->edev);
112 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
113 if (IS_ERR(x->id_edev)) {
114 x->id_edev = NULL;
115 dev_info(x->dev, "No separate ID extcon device\n");
118 if (x->vbus_nb.notifier_call) {
119 ret = devm_extcon_register_notifier(x->dev, x->edev,
120 EXTCON_USB,
121 &x->vbus_nb);
122 if (ret < 0) {
123 dev_err(x->dev,
124 "register VBUS notifier failed\n");
125 return ret;
129 if (x->id_nb.notifier_call) {
130 struct extcon_dev *id_ext;
132 if (x->id_edev)
133 id_ext = x->id_edev;
134 else
135 id_ext = x->edev;
137 ret = devm_extcon_register_notifier(x->dev, id_ext,
138 EXTCON_USB_HOST,
139 &x->id_nb);
140 if (ret < 0) {
141 dev_err(x->dev,
142 "register ID notifier failed\n");
143 return ret;
148 return 0;
152 * devm_usb_get_phy - find the USB PHY
153 * @dev - device that requests this phy
154 * @type - the type of the phy the controller requires
156 * Gets the phy using usb_get_phy(), and associates a device with it using
157 * devres. On driver detach, release function is invoked on the devres data,
158 * then, devres data is freed.
160 * For use by USB host and peripheral drivers.
162 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
164 struct usb_phy **ptr, *phy;
166 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
167 if (!ptr)
168 return ERR_PTR(-ENOMEM);
170 phy = usb_get_phy(type);
171 if (!IS_ERR(phy)) {
172 *ptr = phy;
173 devres_add(dev, ptr);
174 } else
175 devres_free(ptr);
177 return phy;
179 EXPORT_SYMBOL_GPL(devm_usb_get_phy);
182 * usb_get_phy - find the USB PHY
183 * @type - the type of the phy the controller requires
185 * Returns the phy driver, after getting a refcount to it; or
186 * -ENODEV if there is no such phy. The caller is responsible for
187 * calling usb_put_phy() to release that count.
189 * For use by USB host and peripheral drivers.
191 struct usb_phy *usb_get_phy(enum usb_phy_type type)
193 struct usb_phy *phy = NULL;
194 unsigned long flags;
196 spin_lock_irqsave(&phy_lock, flags);
198 phy = __usb_find_phy(&phy_list, type);
199 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
200 pr_debug("PHY: unable to find transceiver of type %s\n",
201 usb_phy_type_string(type));
202 if (!IS_ERR(phy))
203 phy = ERR_PTR(-ENODEV);
205 goto err0;
208 get_device(phy->dev);
210 err0:
211 spin_unlock_irqrestore(&phy_lock, flags);
213 return phy;
215 EXPORT_SYMBOL_GPL(usb_get_phy);
218 * devm_usb_get_phy_by_node - find the USB PHY by device_node
219 * @dev - device that requests this phy
220 * @node - the device_node for the phy device.
221 * @nb - a notifier_block to register with the phy.
223 * Returns the phy driver associated with the given device_node,
224 * after getting a refcount to it, -ENODEV if there is no such phy or
225 * -EPROBE_DEFER if the device is not yet loaded. While at that, it
226 * also associates the device with
227 * the phy using devres. On driver detach, release function is invoked
228 * on the devres data, then, devres data is freed.
230 * For use by peripheral drivers for devices related to a phy,
231 * such as a charger.
233 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
234 struct device_node *node,
235 struct notifier_block *nb)
237 struct usb_phy *phy = ERR_PTR(-ENOMEM);
238 struct phy_devm *ptr;
239 unsigned long flags;
241 ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
242 if (!ptr) {
243 dev_dbg(dev, "failed to allocate memory for devres\n");
244 goto err0;
247 spin_lock_irqsave(&phy_lock, flags);
249 phy = __of_usb_find_phy(node);
250 if (IS_ERR(phy)) {
251 devres_free(ptr);
252 goto err1;
255 if (!try_module_get(phy->dev->driver->owner)) {
256 phy = ERR_PTR(-ENODEV);
257 devres_free(ptr);
258 goto err1;
260 if (nb)
261 usb_register_notifier(phy, nb);
262 ptr->phy = phy;
263 ptr->nb = nb;
264 devres_add(dev, ptr);
266 get_device(phy->dev);
268 err1:
269 spin_unlock_irqrestore(&phy_lock, flags);
271 err0:
273 return phy;
275 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
278 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
279 * @dev - device that requests this phy
280 * @phandle - name of the property holding the phy phandle value
281 * @index - the index of the phy
283 * Returns the phy driver associated with the given phandle value,
284 * after getting a refcount to it, -ENODEV if there is no such phy or
285 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
286 * not yet loaded. While at that, it also associates the device with
287 * the phy using devres. On driver detach, release function is invoked
288 * on the devres data, then, devres data is freed.
290 * For use by USB host and peripheral drivers.
292 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
293 const char *phandle, u8 index)
295 struct device_node *node;
296 struct usb_phy *phy;
298 if (!dev->of_node) {
299 dev_dbg(dev, "device does not have a device node entry\n");
300 return ERR_PTR(-EINVAL);
303 node = of_parse_phandle(dev->of_node, phandle, index);
304 if (!node) {
305 dev_dbg(dev, "failed to get %s phandle in %s node\n", phandle,
306 dev->of_node->full_name);
307 return ERR_PTR(-ENODEV);
309 phy = devm_usb_get_phy_by_node(dev, node, NULL);
310 of_node_put(node);
311 return phy;
313 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
316 * usb_get_phy_dev - find the USB PHY
317 * @dev - device that requests this phy
318 * @index - the index of the phy
320 * Returns the phy driver, after getting a refcount to it; or
321 * -ENODEV if there is no such phy. The caller is responsible for
322 * calling usb_put_phy() to release that count.
324 * For use by USB host and peripheral drivers.
326 struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
328 struct usb_phy *phy = NULL;
329 unsigned long flags;
331 spin_lock_irqsave(&phy_lock, flags);
333 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
334 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
335 dev_dbg(dev, "unable to find transceiver\n");
336 if (!IS_ERR(phy))
337 phy = ERR_PTR(-ENODEV);
339 goto err0;
342 get_device(phy->dev);
344 err0:
345 spin_unlock_irqrestore(&phy_lock, flags);
347 return phy;
349 EXPORT_SYMBOL_GPL(usb_get_phy_dev);
352 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
353 * @dev - device that requests this phy
354 * @index - the index of the phy
356 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
357 * devres. On driver detach, release function is invoked on the devres data,
358 * then, devres data is freed.
360 * For use by USB host and peripheral drivers.
362 struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
364 struct usb_phy **ptr, *phy;
366 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
367 if (!ptr)
368 return NULL;
370 phy = usb_get_phy_dev(dev, index);
371 if (!IS_ERR(phy)) {
372 *ptr = phy;
373 devres_add(dev, ptr);
374 } else
375 devres_free(ptr);
377 return phy;
379 EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
382 * devm_usb_put_phy - release the USB PHY
383 * @dev - device that wants to release this phy
384 * @phy - the phy returned by devm_usb_get_phy()
386 * destroys the devres associated with this phy and invokes usb_put_phy
387 * to release the phy.
389 * For use by USB host and peripheral drivers.
391 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
393 int r;
395 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
396 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
398 EXPORT_SYMBOL_GPL(devm_usb_put_phy);
401 * usb_put_phy - release the USB PHY
402 * @x: the phy returned by usb_get_phy()
404 * Releases a refcount the caller received from usb_get_phy().
406 * For use by USB host and peripheral drivers.
408 void usb_put_phy(struct usb_phy *x)
410 if (x) {
411 struct module *owner = x->dev->driver->owner;
413 put_device(x->dev);
414 module_put(owner);
417 EXPORT_SYMBOL_GPL(usb_put_phy);
420 * usb_add_phy - declare the USB PHY
421 * @x: the USB phy to be used; or NULL
422 * @type - the type of this PHY
424 * This call is exclusively for use by phy drivers, which
425 * coordinate the activities of drivers for host and peripheral
426 * controllers, and in some cases for VBUS current regulation.
428 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
430 int ret = 0;
431 unsigned long flags;
432 struct usb_phy *phy;
434 if (x->type != USB_PHY_TYPE_UNDEFINED) {
435 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
436 return -EINVAL;
439 ret = usb_add_extcon(x);
440 if (ret)
441 return ret;
443 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
445 spin_lock_irqsave(&phy_lock, flags);
447 list_for_each_entry(phy, &phy_list, head) {
448 if (phy->type == type) {
449 ret = -EBUSY;
450 dev_err(x->dev, "transceiver type %s already exists\n",
451 usb_phy_type_string(type));
452 goto out;
456 x->type = type;
457 list_add_tail(&x->head, &phy_list);
459 out:
460 spin_unlock_irqrestore(&phy_lock, flags);
461 return ret;
463 EXPORT_SYMBOL_GPL(usb_add_phy);
466 * usb_add_phy_dev - declare the USB PHY
467 * @x: the USB phy to be used; or NULL
469 * This call is exclusively for use by phy drivers, which
470 * coordinate the activities of drivers for host and peripheral
471 * controllers, and in some cases for VBUS current regulation.
473 int usb_add_phy_dev(struct usb_phy *x)
475 struct usb_phy_bind *phy_bind;
476 unsigned long flags;
477 int ret;
479 if (!x->dev) {
480 dev_err(x->dev, "no device provided for PHY\n");
481 return -EINVAL;
484 ret = usb_add_extcon(x);
485 if (ret)
486 return ret;
488 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
490 spin_lock_irqsave(&phy_lock, flags);
491 list_for_each_entry(phy_bind, &phy_bind_list, list)
492 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
493 phy_bind->phy = x;
495 list_add_tail(&x->head, &phy_list);
497 spin_unlock_irqrestore(&phy_lock, flags);
498 return 0;
500 EXPORT_SYMBOL_GPL(usb_add_phy_dev);
503 * usb_remove_phy - remove the OTG PHY
504 * @x: the USB OTG PHY to be removed;
506 * This reverts the effects of usb_add_phy
508 void usb_remove_phy(struct usb_phy *x)
510 unsigned long flags;
511 struct usb_phy_bind *phy_bind;
513 spin_lock_irqsave(&phy_lock, flags);
514 if (x) {
515 list_for_each_entry(phy_bind, &phy_bind_list, list)
516 if (phy_bind->phy == x)
517 phy_bind->phy = NULL;
518 list_del(&x->head);
520 spin_unlock_irqrestore(&phy_lock, flags);
522 EXPORT_SYMBOL_GPL(usb_remove_phy);
525 * usb_bind_phy - bind the phy and the controller that uses the phy
526 * @dev_name: the device name of the device that will bind to the phy
527 * @index: index to specify the port number
528 * @phy_dev_name: the device name of the phy
530 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
531 * be used when the phy driver registers the phy and when the controller
532 * requests this phy.
534 * To be used by platform specific initialization code.
536 int usb_bind_phy(const char *dev_name, u8 index,
537 const char *phy_dev_name)
539 struct usb_phy_bind *phy_bind;
540 unsigned long flags;
542 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
543 if (!phy_bind)
544 return -ENOMEM;
546 phy_bind->dev_name = dev_name;
547 phy_bind->phy_dev_name = phy_dev_name;
548 phy_bind->index = index;
550 spin_lock_irqsave(&phy_lock, flags);
551 list_add_tail(&phy_bind->list, &phy_bind_list);
552 spin_unlock_irqrestore(&phy_lock, flags);
554 return 0;
556 EXPORT_SYMBOL_GPL(usb_bind_phy);
559 * usb_phy_set_event - set event to phy event
560 * @x: the phy returned by usb_get_phy();
562 * This sets event to phy event
564 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
566 x->last_event = event;
568 EXPORT_SYMBOL_GPL(usb_phy_set_event);