phy.c 12 KB

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