phy.c 13 KB

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