otg.c 11 KB

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