Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 14 | #include <linux/err.h> |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 15 | #include <linux/device.h> |
Kishon Vijay Abraham I | 410219d | 2012-06-22 17:02:47 +0530 | [diff] [blame] | 16 | #include <linux/slab.h> |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 17 | |
| 18 | #include <linux/usb/otg.h> |
| 19 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 20 | static LIST_HEAD(phy_list); |
Kishon Vijay Abraham I | b4a83e4 | 2013-01-25 08:03:21 +0530 | [diff] [blame] | 21 | static LIST_HEAD(phy_bind_list); |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 22 | static DEFINE_SPINLOCK(phy_lock); |
| 23 | |
| 24 | static struct usb_phy *__usb_find_phy(struct list_head *list, |
| 25 | enum usb_phy_type type) |
| 26 | { |
| 27 | struct usb_phy *phy = NULL; |
| 28 | |
| 29 | list_for_each_entry(phy, list, head) { |
| 30 | if (phy->type != type) |
| 31 | continue; |
| 32 | |
| 33 | return phy; |
| 34 | } |
| 35 | |
| 36 | return ERR_PTR(-ENODEV); |
| 37 | } |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 38 | |
Kishon Vijay Abraham I | 0fa4fab | 2013-01-25 08:03:22 +0530 | [diff] [blame^] | 39 | static struct usb_phy *__usb_find_phy_dev(struct device *dev, |
| 40 | struct list_head *list, u8 index) |
| 41 | { |
| 42 | struct usb_phy_bind *phy_bind = NULL; |
| 43 | |
| 44 | list_for_each_entry(phy_bind, list, list) { |
| 45 | if (!(strcmp(phy_bind->dev_name, dev_name(dev))) && |
| 46 | phy_bind->index == index) { |
| 47 | if (phy_bind->phy) |
| 48 | return phy_bind->phy; |
| 49 | else |
| 50 | return ERR_PTR(-EPROBE_DEFER); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return ERR_PTR(-ENODEV); |
| 55 | } |
| 56 | |
Kishon Vijay Abraham I | 410219d | 2012-06-22 17:02:47 +0530 | [diff] [blame] | 57 | static void devm_usb_phy_release(struct device *dev, void *res) |
| 58 | { |
| 59 | struct usb_phy *phy = *(struct usb_phy **)res; |
| 60 | |
| 61 | usb_put_phy(phy); |
| 62 | } |
| 63 | |
| 64 | static int devm_usb_phy_match(struct device *dev, void *res, void *match_data) |
| 65 | { |
| 66 | return res == match_data; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * devm_usb_get_phy - find the USB PHY |
| 71 | * @dev - device that requests this phy |
| 72 | * @type - the type of the phy the controller requires |
| 73 | * |
| 74 | * Gets the phy using usb_get_phy(), and associates a device with it using |
| 75 | * devres. On driver detach, release function is invoked on the devres data, |
| 76 | * then, devres data is freed. |
| 77 | * |
| 78 | * For use by USB host and peripheral drivers. |
| 79 | */ |
| 80 | struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type) |
| 81 | { |
| 82 | struct usb_phy **ptr, *phy; |
| 83 | |
| 84 | ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); |
| 85 | if (!ptr) |
| 86 | return NULL; |
| 87 | |
| 88 | phy = usb_get_phy(type); |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 89 | if (!IS_ERR(phy)) { |
Kishon Vijay Abraham I | 410219d | 2012-06-22 17:02:47 +0530 | [diff] [blame] | 90 | *ptr = phy; |
| 91 | devres_add(dev, ptr); |
| 92 | } else |
| 93 | devres_free(ptr); |
| 94 | |
| 95 | return phy; |
| 96 | } |
| 97 | EXPORT_SYMBOL(devm_usb_get_phy); |
| 98 | |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 99 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 100 | * usb_get_phy - find the USB PHY |
| 101 | * @type - the type of the phy the controller requires |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 102 | * |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 103 | * Returns the phy driver, after getting a refcount to it; or |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 104 | * -ENODEV if there is no such phy. The caller is responsible for |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 105 | * calling usb_put_phy() to release that count. |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 106 | * |
| 107 | * For use by USB host and peripheral drivers. |
| 108 | */ |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 109 | struct usb_phy *usb_get_phy(enum usb_phy_type type) |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 110 | { |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 111 | struct usb_phy *phy = NULL; |
| 112 | unsigned long flags; |
| 113 | |
| 114 | spin_lock_irqsave(&phy_lock, flags); |
| 115 | |
| 116 | phy = __usb_find_phy(&phy_list, type); |
| 117 | if (IS_ERR(phy)) { |
| 118 | pr_err("unable to find transceiver of type %s\n", |
| 119 | usb_phy_type_string(type)); |
Kishon Vijay Abraham I | f8ecf82 | 2012-07-02 12:20:24 +0530 | [diff] [blame] | 120 | goto err0; |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | get_device(phy->dev); |
| 124 | |
Kishon Vijay Abraham I | f8ecf82 | 2012-07-02 12:20:24 +0530 | [diff] [blame] | 125 | err0: |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 126 | spin_unlock_irqrestore(&phy_lock, flags); |
| 127 | |
Heikki Krogerus | 7a8a3a9 | 2012-02-13 13:24:04 +0200 | [diff] [blame] | 128 | return phy; |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 129 | } |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 130 | EXPORT_SYMBOL(usb_get_phy); |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
Kishon Vijay Abraham I | 0fa4fab | 2013-01-25 08:03:22 +0530 | [diff] [blame^] | 133 | * usb_get_phy_dev - find the USB PHY |
| 134 | * @dev - device that requests this phy |
| 135 | * @index - the index of the phy |
| 136 | * |
| 137 | * Returns the phy driver, after getting a refcount to it; or |
| 138 | * -ENODEV if there is no such phy. The caller is responsible for |
| 139 | * calling usb_put_phy() to release that count. |
| 140 | * |
| 141 | * For use by USB host and peripheral drivers. |
| 142 | */ |
| 143 | struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index) |
| 144 | { |
| 145 | struct usb_phy *phy = NULL; |
| 146 | unsigned long flags; |
| 147 | |
| 148 | spin_lock_irqsave(&phy_lock, flags); |
| 149 | |
| 150 | phy = __usb_find_phy_dev(dev, &phy_bind_list, index); |
| 151 | if (IS_ERR(phy)) { |
| 152 | pr_err("unable to find transceiver\n"); |
| 153 | goto err0; |
| 154 | } |
| 155 | |
| 156 | get_device(phy->dev); |
| 157 | |
| 158 | err0: |
| 159 | spin_unlock_irqrestore(&phy_lock, flags); |
| 160 | |
| 161 | return phy; |
| 162 | } |
| 163 | EXPORT_SYMBOL(usb_get_phy_dev); |
| 164 | |
| 165 | /** |
| 166 | * devm_usb_get_phy_dev - find the USB PHY using device ptr and index |
| 167 | * @dev - device that requests this phy |
| 168 | * @index - the index of the phy |
| 169 | * |
| 170 | * Gets the phy using usb_get_phy_dev(), and associates a device with it using |
| 171 | * devres. On driver detach, release function is invoked on the devres data, |
| 172 | * then, devres data is freed. |
| 173 | * |
| 174 | * For use by USB host and peripheral drivers. |
| 175 | */ |
| 176 | struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index) |
| 177 | { |
| 178 | struct usb_phy **ptr, *phy; |
| 179 | |
| 180 | ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL); |
| 181 | if (!ptr) |
| 182 | return NULL; |
| 183 | |
| 184 | phy = usb_get_phy_dev(dev, index); |
| 185 | if (!IS_ERR(phy)) { |
| 186 | *ptr = phy; |
| 187 | devres_add(dev, ptr); |
| 188 | } else |
| 189 | devres_free(ptr); |
| 190 | |
| 191 | return phy; |
| 192 | } |
| 193 | EXPORT_SYMBOL(devm_usb_get_phy_dev); |
| 194 | |
| 195 | /** |
Kishon Vijay Abraham I | 410219d | 2012-06-22 17:02:47 +0530 | [diff] [blame] | 196 | * devm_usb_put_phy - release the USB PHY |
| 197 | * @dev - device that wants to release this phy |
| 198 | * @phy - the phy returned by devm_usb_get_phy() |
| 199 | * |
| 200 | * destroys the devres associated with this phy and invokes usb_put_phy |
| 201 | * to release the phy. |
| 202 | * |
| 203 | * For use by USB host and peripheral drivers. |
| 204 | */ |
| 205 | void devm_usb_put_phy(struct device *dev, struct usb_phy *phy) |
| 206 | { |
| 207 | int r; |
| 208 | |
| 209 | r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy); |
| 210 | dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n"); |
| 211 | } |
| 212 | EXPORT_SYMBOL(devm_usb_put_phy); |
| 213 | |
| 214 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 215 | * usb_put_phy - release the USB PHY |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 216 | * @x: the phy returned by usb_get_phy() |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 217 | * |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 218 | * Releases a refcount the caller received from usb_get_phy(). |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 219 | * |
| 220 | * For use by USB host and peripheral drivers. |
| 221 | */ |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 222 | void usb_put_phy(struct usb_phy *x) |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 223 | { |
Robert Jarzmik | ecf85e4 | 2009-04-21 20:33:10 -0700 | [diff] [blame] | 224 | if (x) |
| 225 | put_device(x->dev); |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 226 | } |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 227 | EXPORT_SYMBOL(usb_put_phy); |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 228 | |
| 229 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 230 | * usb_add_phy - declare the USB PHY |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 231 | * @x: the USB phy to be used; or NULL |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 232 | * @type - the type of this PHY |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 233 | * |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 234 | * This call is exclusively for use by phy drivers, which |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 235 | * coordinate the activities of drivers for host and peripheral |
| 236 | * controllers, and in some cases for VBUS current regulation. |
| 237 | */ |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 238 | int usb_add_phy(struct usb_phy *x, enum usb_phy_type type) |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 239 | { |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 240 | int ret = 0; |
| 241 | unsigned long flags; |
| 242 | struct usb_phy *phy; |
| 243 | |
Shubhrajyoti D | df6791d | 2012-08-07 19:56:30 +0530 | [diff] [blame] | 244 | if (x->type != USB_PHY_TYPE_UNDEFINED) { |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 245 | dev_err(x->dev, "not accepting initialized PHY %s\n", x->label); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | spin_lock_irqsave(&phy_lock, flags); |
| 250 | |
| 251 | list_for_each_entry(phy, &phy_list, head) { |
| 252 | if (phy->type == type) { |
| 253 | ret = -EBUSY; |
| 254 | dev_err(x->dev, "transceiver type %s already exists\n", |
| 255 | usb_phy_type_string(type)); |
| 256 | goto out; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | x->type = type; |
| 261 | list_add_tail(&x->head, &phy_list); |
| 262 | |
| 263 | out: |
| 264 | spin_unlock_irqrestore(&phy_lock, flags); |
| 265 | return ret; |
Tony Lindgren | 3cb22d6 | 2008-11-24 12:02:21 -0800 | [diff] [blame] | 266 | } |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 267 | EXPORT_SYMBOL(usb_add_phy); |
Anatolij Gustschin | 3dacdf1 | 2011-04-15 16:18:38 +0200 | [diff] [blame] | 268 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 269 | /** |
Kishon Vijay Abraham I | 0fa4fab | 2013-01-25 08:03:22 +0530 | [diff] [blame^] | 270 | * usb_add_phy_dev - declare the USB PHY |
| 271 | * @x: the USB phy to be used; or NULL |
| 272 | * |
| 273 | * This call is exclusively for use by phy drivers, which |
| 274 | * coordinate the activities of drivers for host and peripheral |
| 275 | * controllers, and in some cases for VBUS current regulation. |
| 276 | */ |
| 277 | int usb_add_phy_dev(struct usb_phy *x) |
| 278 | { |
| 279 | struct usb_phy_bind *phy_bind; |
| 280 | unsigned long flags; |
| 281 | |
| 282 | if (!x->dev) { |
| 283 | dev_err(x->dev, "no device provided for PHY\n"); |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
| 287 | spin_lock_irqsave(&phy_lock, flags); |
| 288 | list_for_each_entry(phy_bind, &phy_bind_list, list) |
| 289 | if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev)))) |
| 290 | phy_bind->phy = x; |
| 291 | |
| 292 | list_add_tail(&x->head, &phy_list); |
| 293 | |
| 294 | spin_unlock_irqrestore(&phy_lock, flags); |
| 295 | return 0; |
| 296 | } |
| 297 | EXPORT_SYMBOL(usb_add_phy_dev); |
| 298 | |
| 299 | /** |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 300 | * usb_remove_phy - remove the OTG PHY |
| 301 | * @x: the USB OTG PHY to be removed; |
| 302 | * |
| 303 | * This reverts the effects of usb_add_phy |
| 304 | */ |
| 305 | void usb_remove_phy(struct usb_phy *x) |
| 306 | { |
| 307 | unsigned long flags; |
Kishon Vijay Abraham I | 0fa4fab | 2013-01-25 08:03:22 +0530 | [diff] [blame^] | 308 | struct usb_phy_bind *phy_bind; |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 309 | |
| 310 | spin_lock_irqsave(&phy_lock, flags); |
Kishon Vijay Abraham I | 0fa4fab | 2013-01-25 08:03:22 +0530 | [diff] [blame^] | 311 | if (x) { |
| 312 | list_for_each_entry(phy_bind, &phy_bind_list, list) |
| 313 | if (phy_bind->phy == x) |
| 314 | phy_bind->phy = NULL; |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 315 | list_del(&x->head); |
Kishon Vijay Abraham I | 0fa4fab | 2013-01-25 08:03:22 +0530 | [diff] [blame^] | 316 | } |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 317 | spin_unlock_irqrestore(&phy_lock, flags); |
| 318 | } |
| 319 | EXPORT_SYMBOL(usb_remove_phy); |
| 320 | |
Kishon Vijay Abraham I | b4a83e4 | 2013-01-25 08:03:21 +0530 | [diff] [blame] | 321 | /** |
| 322 | * usb_bind_phy - bind the phy and the controller that uses the phy |
| 323 | * @dev_name: the device name of the device that will bind to the phy |
| 324 | * @index: index to specify the port number |
| 325 | * @phy_dev_name: the device name of the phy |
| 326 | * |
| 327 | * Fills the phy_bind structure with the dev_name and phy_dev_name. This will |
| 328 | * be used when the phy driver registers the phy and when the controller |
| 329 | * requests this phy. |
| 330 | * |
| 331 | * To be used by platform specific initialization code. |
| 332 | */ |
| 333 | int __init usb_bind_phy(const char *dev_name, u8 index, |
| 334 | const char *phy_dev_name) |
| 335 | { |
| 336 | struct usb_phy_bind *phy_bind; |
| 337 | unsigned long flags; |
| 338 | |
| 339 | phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL); |
| 340 | if (!phy_bind) { |
| 341 | pr_err("phy_bind(): No memory for phy_bind"); |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | |
| 345 | phy_bind->dev_name = dev_name; |
| 346 | phy_bind->phy_dev_name = phy_dev_name; |
| 347 | phy_bind->index = index; |
| 348 | |
| 349 | spin_lock_irqsave(&phy_lock, flags); |
| 350 | list_add_tail(&phy_bind->list, &phy_bind_list); |
| 351 | spin_unlock_irqrestore(&phy_lock, flags); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(usb_bind_phy); |
| 356 | |
Anatolij Gustschin | 3dacdf1 | 2011-04-15 16:18:38 +0200 | [diff] [blame] | 357 | const char *otg_state_string(enum usb_otg_state state) |
| 358 | { |
| 359 | switch (state) { |
| 360 | case OTG_STATE_A_IDLE: |
| 361 | return "a_idle"; |
| 362 | case OTG_STATE_A_WAIT_VRISE: |
| 363 | return "a_wait_vrise"; |
| 364 | case OTG_STATE_A_WAIT_BCON: |
| 365 | return "a_wait_bcon"; |
| 366 | case OTG_STATE_A_HOST: |
| 367 | return "a_host"; |
| 368 | case OTG_STATE_A_SUSPEND: |
| 369 | return "a_suspend"; |
| 370 | case OTG_STATE_A_PERIPHERAL: |
| 371 | return "a_peripheral"; |
| 372 | case OTG_STATE_A_WAIT_VFALL: |
| 373 | return "a_wait_vfall"; |
| 374 | case OTG_STATE_A_VBUS_ERR: |
| 375 | return "a_vbus_err"; |
| 376 | case OTG_STATE_B_IDLE: |
| 377 | return "b_idle"; |
| 378 | case OTG_STATE_B_SRP_INIT: |
| 379 | return "b_srp_init"; |
| 380 | case OTG_STATE_B_PERIPHERAL: |
| 381 | return "b_peripheral"; |
| 382 | case OTG_STATE_B_WAIT_ACON: |
| 383 | return "b_wait_acon"; |
| 384 | case OTG_STATE_B_HOST: |
| 385 | return "b_host"; |
| 386 | default: |
| 387 | return "UNDEFINED"; |
| 388 | } |
| 389 | } |
| 390 | EXPORT_SYMBOL(otg_state_string); |