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>
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
);
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
)
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
) {
56 return ERR_PTR(-EPROBE_DEFER
);
60 return ERR_PTR(-ENODEV
);
63 static struct usb_phy
*__of_usb_find_phy(struct device_node
*node
)
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
)
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
;
87 static void devm_usb_phy_release2(struct device
*dev
, void *_res
)
89 struct phy_devm
*res
= _res
;
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
)
107 if (of_property_read_bool(x
->dev
->of_node
, "extcon")) {
108 x
->edev
= extcon_get_edev_by_phandle(x
->dev
, 0);
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
)) {
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
,
124 "register VBUS notifier failed\n");
129 if (x
->id_nb
.notifier_call
) {
130 struct extcon_dev
*id_ext
;
137 ret
= devm_extcon_register_notifier(x
->dev
, id_ext
,
142 "register ID notifier failed\n");
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
);
168 return ERR_PTR(-ENOMEM
);
170 phy
= usb_get_phy(type
);
173 devres_add(dev
, ptr
);
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
;
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
));
203 phy
= ERR_PTR(-ENODEV
);
208 get_device(phy
->dev
);
211 spin_unlock_irqrestore(&phy_lock
, flags
);
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,
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
;
241 ptr
= devres_alloc(devm_usb_phy_release2
, sizeof(*ptr
), GFP_KERNEL
);
243 dev_dbg(dev
, "failed to allocate memory for devres\n");
247 spin_lock_irqsave(&phy_lock
, flags
);
249 phy
= __of_usb_find_phy(node
);
255 if (!try_module_get(phy
->dev
->driver
->owner
)) {
256 phy
= ERR_PTR(-ENODEV
);
261 usb_register_notifier(phy
, nb
);
264 devres_add(dev
, ptr
);
266 get_device(phy
->dev
);
269 spin_unlock_irqrestore(&phy_lock
, flags
);
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
;
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
);
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
);
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
;
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");
337 phy
= ERR_PTR(-ENODEV
);
342 get_device(phy
->dev
);
345 spin_unlock_irqrestore(&phy_lock
, flags
);
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
);
370 phy
= usb_get_phy_dev(dev
, index
);
373 devres_add(dev
, ptr
);
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
)
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
)
411 struct module
*owner
= x
->dev
->driver
->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
)
434 if (x
->type
!= USB_PHY_TYPE_UNDEFINED
) {
435 dev_err(x
->dev
, "not accepting initialized PHY %s\n", x
->label
);
439 ret
= usb_add_extcon(x
);
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
) {
450 dev_err(x
->dev
, "transceiver type %s already exists\n",
451 usb_phy_type_string(type
));
457 list_add_tail(&x
->head
, &phy_list
);
460 spin_unlock_irqrestore(&phy_lock
, flags
);
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
;
480 dev_err(x
->dev
, "no device provided for PHY\n");
484 ret
= usb_add_extcon(x
);
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
))))
495 list_add_tail(&x
->head
, &phy_list
);
497 spin_unlock_irqrestore(&phy_lock
, flags
);
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
)
511 struct usb_phy_bind
*phy_bind
;
513 spin_lock_irqsave(&phy_lock
, flags
);
515 list_for_each_entry(phy_bind
, &phy_bind_list
, list
)
516 if (phy_bind
->phy
== x
)
517 phy_bind
->phy
= NULL
;
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
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
;
542 phy_bind
= kzalloc(sizeof(*phy_bind
), GFP_KERNEL
);
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
);
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
);