phy.c 11 KB

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