blob: 0fa4d8c1b1e8fb61a41d237a8d6bc7d4439aee1d [file] [log] [blame]
Tony Lindgren3cb22d62008-11-24 12:02:21 -08001/*
2 * otg.c -- USB OTG utility code
3 *
4 * Copyright (C) 2004 Texas Instruments
5 *
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.
10 */
11
12#include <linux/kernel.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040013#include <linux/export.h>
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053014#include <linux/err.h>
Tony Lindgren3cb22d62008-11-24 12:02:21 -080015#include <linux/device.h>
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +053016#include <linux/slab.h>
Tony Lindgren3cb22d62008-11-24 12:02:21 -080017
18#include <linux/usb/otg.h>
19
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053020static LIST_HEAD(phy_list);
21static DEFINE_SPINLOCK(phy_lock);
22
23static struct usb_phy *__usb_find_phy(struct list_head *list,
24 enum usb_phy_type type)
25{
26 struct usb_phy *phy = NULL;
27
28 list_for_each_entry(phy, list, head) {
29 if (phy->type != type)
30 continue;
31
32 return phy;
33 }
34
35 return ERR_PTR(-ENODEV);
36}
Tony Lindgren3cb22d62008-11-24 12:02:21 -080037
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +053038static void devm_usb_phy_release(struct device *dev, void *res)
39{
40 struct usb_phy *phy = *(struct usb_phy **)res;
41
42 usb_put_phy(phy);
43}
44
45static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
46{
47 return res == match_data;
48}
49
50/**
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
54 *
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.
58 *
59 * For use by USB host and peripheral drivers.
60 */
61struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
62{
63 struct usb_phy **ptr, *phy;
64
65 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
66 if (!ptr)
67 return NULL;
68
69 phy = usb_get_phy(type);
70 if (phy) {
71 *ptr = phy;
72 devres_add(dev, ptr);
73 } else
74 devres_free(ptr);
75
76 return phy;
77}
78EXPORT_SYMBOL(devm_usb_get_phy);
79
Tony Lindgren3cb22d62008-11-24 12:02:21 -080080/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053081 * usb_get_phy - find the USB PHY
82 * @type - the type of the phy the controller requires
Tony Lindgren3cb22d62008-11-24 12:02:21 -080083 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +053084 * Returns the phy driver, after getting a refcount to it; or
85 * null if there is no such phy. The caller is responsible for
86 * calling usb_put_phy() to release that count.
Tony Lindgren3cb22d62008-11-24 12:02:21 -080087 *
88 * For use by USB host and peripheral drivers.
89 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053090struct usb_phy *usb_get_phy(enum usb_phy_type type)
Tony Lindgren3cb22d62008-11-24 12:02:21 -080091{
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +053092 struct usb_phy *phy = NULL;
93 unsigned long flags;
94
95 spin_lock_irqsave(&phy_lock, flags);
96
97 phy = __usb_find_phy(&phy_list, type);
98 if (IS_ERR(phy)) {
99 pr_err("unable to find transceiver of type %s\n",
100 usb_phy_type_string(type));
101 return phy;
102 }
103
104 get_device(phy->dev);
105
106 spin_unlock_irqrestore(&phy_lock, flags);
107
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200108 return phy;
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800109}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530110EXPORT_SYMBOL(usb_get_phy);
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800111
112/**
Kishon Vijay Abraham I410219d2012-06-22 17:02:47 +0530113 * devm_usb_put_phy - release the USB PHY
114 * @dev - device that wants to release this phy
115 * @phy - the phy returned by devm_usb_get_phy()
116 *
117 * destroys the devres associated with this phy and invokes usb_put_phy
118 * to release the phy.
119 *
120 * For use by USB host and peripheral drivers.
121 */
122void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
123{
124 int r;
125
126 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
127 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
128}
129EXPORT_SYMBOL(devm_usb_put_phy);
130
131/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530132 * usb_put_phy - release the USB PHY
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530133 * @x: the phy returned by usb_get_phy()
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800134 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530135 * Releases a refcount the caller received from usb_get_phy().
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800136 *
137 * For use by USB host and peripheral drivers.
138 */
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530139void usb_put_phy(struct usb_phy *x)
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800140{
Robert Jarzmikecf85e42009-04-21 20:33:10 -0700141 if (x)
142 put_device(x->dev);
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800143}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530144EXPORT_SYMBOL(usb_put_phy);
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800145
146/**
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530147 * usb_add_phy - declare the USB PHY
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530148 * @x: the USB phy to be used; or NULL
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530149 * @type - the type of this PHY
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800150 *
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530151 * This call is exclusively for use by phy drivers, which
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800152 * coordinate the activities of drivers for host and peripheral
153 * controllers, and in some cases for VBUS current regulation.
154 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530155int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800156{
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530157 int ret = 0;
158 unsigned long flags;
159 struct usb_phy *phy;
160
161 if (x && x->type != USB_PHY_TYPE_UNDEFINED) {
162 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
163 return -EINVAL;
164 }
165
166 spin_lock_irqsave(&phy_lock, flags);
167
168 list_for_each_entry(phy, &phy_list, head) {
169 if (phy->type == type) {
170 ret = -EBUSY;
171 dev_err(x->dev, "transceiver type %s already exists\n",
172 usb_phy_type_string(type));
173 goto out;
174 }
175 }
176
177 x->type = type;
178 list_add_tail(&x->head, &phy_list);
179
180out:
181 spin_unlock_irqrestore(&phy_lock, flags);
182 return ret;
Tony Lindgren3cb22d62008-11-24 12:02:21 -0800183}
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530184EXPORT_SYMBOL(usb_add_phy);
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200185
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530186/**
187 * usb_remove_phy - remove the OTG PHY
188 * @x: the USB OTG PHY to be removed;
189 *
190 * This reverts the effects of usb_add_phy
191 */
192void usb_remove_phy(struct usb_phy *x)
193{
194 unsigned long flags;
195
196 spin_lock_irqsave(&phy_lock, flags);
197 if (x)
198 list_del(&x->head);
199 spin_unlock_irqrestore(&phy_lock, flags);
200}
201EXPORT_SYMBOL(usb_remove_phy);
202
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200203const char *otg_state_string(enum usb_otg_state state)
204{
205 switch (state) {
206 case OTG_STATE_A_IDLE:
207 return "a_idle";
208 case OTG_STATE_A_WAIT_VRISE:
209 return "a_wait_vrise";
210 case OTG_STATE_A_WAIT_BCON:
211 return "a_wait_bcon";
212 case OTG_STATE_A_HOST:
213 return "a_host";
214 case OTG_STATE_A_SUSPEND:
215 return "a_suspend";
216 case OTG_STATE_A_PERIPHERAL:
217 return "a_peripheral";
218 case OTG_STATE_A_WAIT_VFALL:
219 return "a_wait_vfall";
220 case OTG_STATE_A_VBUS_ERR:
221 return "a_vbus_err";
222 case OTG_STATE_B_IDLE:
223 return "b_idle";
224 case OTG_STATE_B_SRP_INIT:
225 return "b_srp_init";
226 case OTG_STATE_B_PERIPHERAL:
227 return "b_peripheral";
228 case OTG_STATE_B_WAIT_ACON:
229 return "b_wait_acon";
230 case OTG_STATE_B_HOST:
231 return "b_host";
232 default:
233 return "UNDEFINED";
234 }
235}
236EXPORT_SYMBOL(otg_state_string);