2 * otg.c -- USB OTG utility code
4 * Copyright (C) 2004 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.
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>
18 #include <linux/usb/otg.h>
20 static LIST_HEAD(phy_list);
21 static DEFINE_SPINLOCK(phy_lock);
23 static struct usb_phy *__usb_find_phy(struct list_head *list,
24 enum usb_phy_type type)
26 struct usb_phy *phy = NULL;
28 list_for_each_entry(phy, list, head) {
29 if (phy->type != type)
35 return ERR_PTR(-ENODEV);
38 static void devm_usb_phy_release(struct device *dev, void *res)
40 struct usb_phy *phy = *(struct usb_phy **)res;
45 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
47 return res == match_data;
51 * devm_usb_get_phy - find the USB PHY
52 * @dev - device that requests this phy
53 * @type - the type of the phy the controller requires
55 * Gets the phy using usb_get_phy(), and associates a device with it using
56 * devres. On driver detach, release function is invoked on the devres data,
57 * then, devres data is freed.
59 * For use by USB host and peripheral drivers.
61 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
63 struct usb_phy **ptr, *phy;
65 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
69 phy = usb_get_phy(type);
78 EXPORT_SYMBOL(devm_usb_get_phy);
81 * usb_get_phy - find the USB PHY
82 * @type - the type of the phy the controller requires
84 * Returns the phy driver, after getting a refcount to it; or
85 * -ENODEV if there is no such phy. The caller is responsible for
86 * calling usb_put_phy() to release that count.
88 * For use by USB host and peripheral drivers.
90 struct usb_phy *usb_get_phy(enum usb_phy_type type)
92 struct usb_phy *phy = NULL;
95 spin_lock_irqsave(&phy_lock, flags);
97 phy = __usb_find_phy(&phy_list, type);
99 pr_err("unable to find transceiver of type %s\n",
100 usb_phy_type_string(type));
104 get_device(phy->dev);
107 spin_unlock_irqrestore(&phy_lock, flags);
111 EXPORT_SYMBOL(usb_get_phy);
114 * devm_usb_put_phy - release the USB PHY
115 * @dev - device that wants to release this phy
116 * @phy - the phy returned by devm_usb_get_phy()
118 * destroys the devres associated with this phy and invokes usb_put_phy
119 * to release the phy.
121 * For use by USB host and peripheral drivers.
123 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
127 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
128 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
130 EXPORT_SYMBOL(devm_usb_put_phy);
133 * usb_put_phy - release the USB PHY
134 * @x: the phy returned by usb_get_phy()
136 * Releases a refcount the caller received from usb_get_phy().
138 * For use by USB host and peripheral drivers.
140 void usb_put_phy(struct usb_phy *x)
145 EXPORT_SYMBOL(usb_put_phy);
148 * usb_add_phy - declare the USB PHY
149 * @x: the USB phy to be used; or NULL
150 * @type - the type of this PHY
152 * This call is exclusively for use by phy drivers, which
153 * coordinate the activities of drivers for host and peripheral
154 * controllers, and in some cases for VBUS current regulation.
156 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
162 if (x->type != USB_PHY_TYPE_UNDEFINED) {
163 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
167 spin_lock_irqsave(&phy_lock, flags);
169 list_for_each_entry(phy, &phy_list, head) {
170 if (phy->type == type) {
172 dev_err(x->dev, "transceiver type %s already exists\n",
173 usb_phy_type_string(type));
179 list_add_tail(&x->head, &phy_list);
182 spin_unlock_irqrestore(&phy_lock, flags);
185 EXPORT_SYMBOL(usb_add_phy);
188 * usb_remove_phy - remove the OTG PHY
189 * @x: the USB OTG PHY to be removed;
191 * This reverts the effects of usb_add_phy
193 void usb_remove_phy(struct usb_phy *x)
197 spin_lock_irqsave(&phy_lock, flags);
200 spin_unlock_irqrestore(&phy_lock, flags);
202 EXPORT_SYMBOL(usb_remove_phy);
204 const char *otg_state_string(enum usb_otg_state state)
207 case OTG_STATE_A_IDLE:
209 case OTG_STATE_A_WAIT_VRISE:
210 return "a_wait_vrise";
211 case OTG_STATE_A_WAIT_BCON:
212 return "a_wait_bcon";
213 case OTG_STATE_A_HOST:
215 case OTG_STATE_A_SUSPEND:
217 case OTG_STATE_A_PERIPHERAL:
218 return "a_peripheral";
219 case OTG_STATE_A_WAIT_VFALL:
220 return "a_wait_vfall";
221 case OTG_STATE_A_VBUS_ERR:
223 case OTG_STATE_B_IDLE:
225 case OTG_STATE_B_SRP_INIT:
227 case OTG_STATE_B_PERIPHERAL:
228 return "b_peripheral";
229 case OTG_STATE_B_WAIT_ACON:
230 return "b_wait_acon";
231 case OTG_STATE_B_HOST:
237 EXPORT_SYMBOL(otg_state_string);