phy.c 11 KB

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