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
);
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
)
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
) {
51 return ERR_PTR(-EPROBE_DEFER
);
55 return ERR_PTR(-ENODEV
);
58 static struct usb_phy
*__of_usb_find_phy(struct device_node
*node
)
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
)
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
;
82 static int devm_usb_phy_match(struct device
*dev
, void *res
, void *match_data
)
84 return res
== match_data
;
88 * devm_usb_get_phy - find the USB PHY
89 * @dev - device that requests this phy
90 * @type - the type of the phy the controller requires
92 * Gets the phy using usb_get_phy(), and associates a device with it using
93 * devres. On driver detach, release function is invoked on the devres data,
94 * then, devres data is freed.
96 * For use by USB host and peripheral drivers.
98 struct usb_phy
*devm_usb_get_phy(struct device
*dev
, enum usb_phy_type type
)
100 struct usb_phy
**ptr
, *phy
;
102 ptr
= devres_alloc(devm_usb_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
104 return ERR_PTR(-ENOMEM
);
106 phy
= usb_get_phy(type
);
109 devres_add(dev
, ptr
);
115 EXPORT_SYMBOL_GPL(devm_usb_get_phy
);
118 * usb_get_phy - find the USB PHY
119 * @type - the type of the phy the controller requires
121 * Returns the phy driver, after getting a refcount to it; or
122 * -ENODEV if there is no such phy. The caller is responsible for
123 * calling usb_put_phy() to release that count.
125 * For use by USB host and peripheral drivers.
127 struct usb_phy
*usb_get_phy(enum usb_phy_type type
)
129 struct usb_phy
*phy
= NULL
;
132 spin_lock_irqsave(&phy_lock
, flags
);
134 phy
= __usb_find_phy(&phy_list
, type
);
135 if (IS_ERR(phy
) || !try_module_get(phy
->dev
->driver
->owner
)) {
136 pr_debug("PHY: unable to find transceiver of type %s\n",
137 usb_phy_type_string(type
));
139 phy
= ERR_PTR(-ENODEV
);
144 get_device(phy
->dev
);
147 spin_unlock_irqrestore(&phy_lock
, flags
);
151 EXPORT_SYMBOL_GPL(usb_get_phy
);
154 * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
155 * @dev - device that requests this phy
156 * @phandle - name of the property holding the phy phandle value
157 * @index - the index of the phy
159 * Returns the phy driver associated with the given phandle value,
160 * after getting a refcount to it, -ENODEV if there is no such phy or
161 * -EPROBE_DEFER if there is a phandle to the phy, but the device is
162 * not yet loaded. While at that, it also associates the device with
163 * the phy using devres. On driver detach, release function is invoked
164 * on the devres data, then, devres data is freed.
166 * For use by USB host and peripheral drivers.
168 struct usb_phy
*devm_usb_get_phy_by_phandle(struct device
*dev
,
169 const char *phandle
, u8 index
)
171 struct usb_phy
*phy
= ERR_PTR(-ENOMEM
), **ptr
;
173 struct device_node
*node
;
176 dev_dbg(dev
, "device does not have a device node entry\n");
177 return ERR_PTR(-EINVAL
);
180 node
= of_parse_phandle(dev
->of_node
, phandle
, index
);
182 dev_dbg(dev
, "failed to get %s phandle in %s node\n", phandle
,
183 dev
->of_node
->full_name
);
184 return ERR_PTR(-ENODEV
);
187 ptr
= devres_alloc(devm_usb_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
189 dev_dbg(dev
, "failed to allocate memory for devres\n");
193 spin_lock_irqsave(&phy_lock
, flags
);
195 phy
= __of_usb_find_phy(node
);
201 if (!try_module_get(phy
->dev
->driver
->owner
)) {
202 phy
= ERR_PTR(-ENODEV
);
208 devres_add(dev
, ptr
);
210 get_device(phy
->dev
);
213 spin_unlock_irqrestore(&phy_lock
, flags
);
220 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle
);
223 * usb_get_phy_dev - find the USB PHY
224 * @dev - device that requests this phy
225 * @index - the index of the phy
227 * Returns the phy driver, after getting a refcount to it; or
228 * -ENODEV if there is no such phy. The caller is responsible for
229 * calling usb_put_phy() to release that count.
231 * For use by USB host and peripheral drivers.
233 struct usb_phy
*usb_get_phy_dev(struct device
*dev
, u8 index
)
235 struct usb_phy
*phy
= NULL
;
238 spin_lock_irqsave(&phy_lock
, flags
);
240 phy
= __usb_find_phy_dev(dev
, &phy_bind_list
, index
);
241 if (IS_ERR(phy
) || !try_module_get(phy
->dev
->driver
->owner
)) {
242 dev_dbg(dev
, "unable to find transceiver\n");
244 phy
= ERR_PTR(-ENODEV
);
249 get_device(phy
->dev
);
252 spin_unlock_irqrestore(&phy_lock
, flags
);
256 EXPORT_SYMBOL_GPL(usb_get_phy_dev
);
259 * devm_usb_get_phy_dev - find the USB PHY using device ptr and index
260 * @dev - device that requests this phy
261 * @index - the index of the phy
263 * Gets the phy using usb_get_phy_dev(), and associates a device with it using
264 * devres. On driver detach, release function is invoked on the devres data,
265 * then, devres data is freed.
267 * For use by USB host and peripheral drivers.
269 struct usb_phy
*devm_usb_get_phy_dev(struct device
*dev
, u8 index
)
271 struct usb_phy
**ptr
, *phy
;
273 ptr
= devres_alloc(devm_usb_phy_release
, sizeof(*ptr
), GFP_KERNEL
);
277 phy
= usb_get_phy_dev(dev
, index
);
280 devres_add(dev
, ptr
);
286 EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev
);
289 * devm_usb_put_phy - release the USB PHY
290 * @dev - device that wants to release this phy
291 * @phy - the phy returned by devm_usb_get_phy()
293 * destroys the devres associated with this phy and invokes usb_put_phy
294 * to release the phy.
296 * For use by USB host and peripheral drivers.
298 void devm_usb_put_phy(struct device
*dev
, struct usb_phy
*phy
)
302 r
= devres_destroy(dev
, devm_usb_phy_release
, devm_usb_phy_match
, phy
);
303 dev_WARN_ONCE(dev
, r
, "couldn't find PHY resource\n");
305 EXPORT_SYMBOL_GPL(devm_usb_put_phy
);
308 * usb_put_phy - release the USB PHY
309 * @x: the phy returned by usb_get_phy()
311 * Releases a refcount the caller received from usb_get_phy().
313 * For use by USB host and peripheral drivers.
315 void usb_put_phy(struct usb_phy
*x
)
318 struct module
*owner
= x
->dev
->driver
->owner
;
324 EXPORT_SYMBOL_GPL(usb_put_phy
);
327 * usb_add_phy - declare the USB PHY
328 * @x: the USB phy to be used; or NULL
329 * @type - the type of this PHY
331 * This call is exclusively for use by phy drivers, which
332 * coordinate the activities of drivers for host and peripheral
333 * controllers, and in some cases for VBUS current regulation.
335 int usb_add_phy(struct usb_phy
*x
, enum usb_phy_type type
)
341 if (x
->type
!= USB_PHY_TYPE_UNDEFINED
) {
342 dev_err(x
->dev
, "not accepting initialized PHY %s\n", x
->label
);
346 ATOMIC_INIT_NOTIFIER_HEAD(&x
->notifier
);
348 spin_lock_irqsave(&phy_lock
, flags
);
350 list_for_each_entry(phy
, &phy_list
, head
) {
351 if (phy
->type
== type
) {
353 dev_err(x
->dev
, "transceiver type %s already exists\n",
354 usb_phy_type_string(type
));
360 list_add_tail(&x
->head
, &phy_list
);
363 spin_unlock_irqrestore(&phy_lock
, flags
);
366 EXPORT_SYMBOL_GPL(usb_add_phy
);
369 * usb_add_phy_dev - declare the USB PHY
370 * @x: the USB phy to be used; or NULL
372 * This call is exclusively for use by phy drivers, which
373 * coordinate the activities of drivers for host and peripheral
374 * controllers, and in some cases for VBUS current regulation.
376 int usb_add_phy_dev(struct usb_phy
*x
)
378 struct usb_phy_bind
*phy_bind
;
382 dev_err(x
->dev
, "no device provided for PHY\n");
386 ATOMIC_INIT_NOTIFIER_HEAD(&x
->notifier
);
388 spin_lock_irqsave(&phy_lock
, flags
);
389 list_for_each_entry(phy_bind
, &phy_bind_list
, list
)
390 if (!(strcmp(phy_bind
->phy_dev_name
, dev_name(x
->dev
))))
393 list_add_tail(&x
->head
, &phy_list
);
395 spin_unlock_irqrestore(&phy_lock
, flags
);
398 EXPORT_SYMBOL_GPL(usb_add_phy_dev
);
401 * usb_remove_phy - remove the OTG PHY
402 * @x: the USB OTG PHY to be removed;
404 * This reverts the effects of usb_add_phy
406 void usb_remove_phy(struct usb_phy
*x
)
409 struct usb_phy_bind
*phy_bind
;
411 spin_lock_irqsave(&phy_lock
, flags
);
413 list_for_each_entry(phy_bind
, &phy_bind_list
, list
)
414 if (phy_bind
->phy
== x
)
415 phy_bind
->phy
= NULL
;
418 spin_unlock_irqrestore(&phy_lock
, flags
);
420 EXPORT_SYMBOL_GPL(usb_remove_phy
);
423 * usb_bind_phy - bind the phy and the controller that uses the phy
424 * @dev_name: the device name of the device that will bind to the phy
425 * @index: index to specify the port number
426 * @phy_dev_name: the device name of the phy
428 * Fills the phy_bind structure with the dev_name and phy_dev_name. This will
429 * be used when the phy driver registers the phy and when the controller
432 * To be used by platform specific initialization code.
434 int usb_bind_phy(const char *dev_name
, u8 index
,
435 const char *phy_dev_name
)
437 struct usb_phy_bind
*phy_bind
;
440 phy_bind
= kzalloc(sizeof(*phy_bind
), GFP_KERNEL
);
444 phy_bind
->dev_name
= dev_name
;
445 phy_bind
->phy_dev_name
= phy_dev_name
;
446 phy_bind
->index
= index
;
448 spin_lock_irqsave(&phy_lock
, flags
);
449 list_add_tail(&phy_bind
->list
, &phy_bind_list
);
450 spin_unlock_irqrestore(&phy_lock
, flags
);
454 EXPORT_SYMBOL_GPL(usb_bind_phy
);
457 * usb_phy_set_event - set event to phy event
458 * @x: the phy returned by usb_get_phy();
460 * This sets event to phy event
462 void usb_phy_set_event(struct usb_phy
*x
, unsigned long event
)
464 x
->last_event
= event
;
466 EXPORT_SYMBOL_GPL(usb_phy_set_event
);