phy.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * A wrapper for multiple PHYs which passes all phy_* function calls to
  4. * multiple (actual) PHY devices. This is comes handy when initializing
  5. * all PHYs on a HCD and to keep them all in the same state.
  6. *
  7. * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/list.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/of.h>
  13. #include "phy.h"
  14. struct usb_phy_roothub {
  15. struct phy *phy;
  16. struct list_head list;
  17. };
  18. /* Allocate the roothub_entry by specific name of phy */
  19. static int usb_phy_roothub_add_phy_by_name(struct device *dev, const char *name,
  20. struct list_head *list)
  21. {
  22. struct usb_phy_roothub *roothub_entry;
  23. struct phy *phy;
  24. phy = devm_of_phy_get(dev, dev->of_node, name);
  25. if (IS_ERR(phy))
  26. return PTR_ERR(phy);
  27. roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
  28. if (!roothub_entry)
  29. return -ENOMEM;
  30. INIT_LIST_HEAD(&roothub_entry->list);
  31. roothub_entry->phy = phy;
  32. list_add_tail(&roothub_entry->list, list);
  33. return 0;
  34. }
  35. static int usb_phy_roothub_add_phy(struct device *dev, int index,
  36. struct list_head *list)
  37. {
  38. struct usb_phy_roothub *roothub_entry;
  39. struct phy *phy;
  40. phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
  41. if (IS_ERR(phy)) {
  42. if (PTR_ERR(phy) == -ENODEV)
  43. return 0;
  44. else
  45. return PTR_ERR(phy);
  46. }
  47. roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
  48. if (!roothub_entry)
  49. return -ENOMEM;
  50. INIT_LIST_HEAD(&roothub_entry->list);
  51. roothub_entry->phy = phy;
  52. list_add_tail(&roothub_entry->list, list);
  53. return 0;
  54. }
  55. struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
  56. {
  57. struct usb_phy_roothub *phy_roothub;
  58. int i, num_phys, err;
  59. if (!IS_ENABLED(CONFIG_GENERIC_PHY))
  60. return NULL;
  61. num_phys = of_count_phandle_with_args(dev->of_node, "phys",
  62. "#phy-cells");
  63. if (num_phys <= 0)
  64. return NULL;
  65. phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
  66. if (!phy_roothub)
  67. return ERR_PTR(-ENOMEM);
  68. INIT_LIST_HEAD(&phy_roothub->list);
  69. if (!usb_phy_roothub_add_phy_by_name(dev, "usb2-phy", &phy_roothub->list))
  70. return phy_roothub;
  71. for (i = 0; i < num_phys; i++) {
  72. err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
  73. if (err)
  74. return ERR_PTR(err);
  75. }
  76. return phy_roothub;
  77. }
  78. EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
  79. /**
  80. * usb_phy_roothub_alloc_usb3_phy - alloc the roothub
  81. * @dev: the device of the host controller
  82. *
  83. * Allocate the usb phy roothub if the host use a generic usb3-phy.
  84. *
  85. * Return: On success, a pointer to the usb_phy_roothub. Otherwise,
  86. * %NULL if no use usb3 phy or %-ENOMEM if out of memory.
  87. */
  88. struct usb_phy_roothub *usb_phy_roothub_alloc_usb3_phy(struct device *dev)
  89. {
  90. struct usb_phy_roothub *phy_roothub;
  91. int num_phys;
  92. if (!IS_ENABLED(CONFIG_GENERIC_PHY))
  93. return NULL;
  94. num_phys = of_count_phandle_with_args(dev->of_node, "phys",
  95. "#phy-cells");
  96. if (num_phys <= 0)
  97. return NULL;
  98. phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
  99. if (!phy_roothub)
  100. return ERR_PTR(-ENOMEM);
  101. INIT_LIST_HEAD(&phy_roothub->list);
  102. if (!usb_phy_roothub_add_phy_by_name(dev, "usb3-phy", &phy_roothub->list))
  103. return phy_roothub;
  104. return NULL;
  105. }
  106. EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc_usb3_phy);
  107. int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
  108. {
  109. struct usb_phy_roothub *roothub_entry;
  110. struct list_head *head;
  111. int err;
  112. if (!phy_roothub)
  113. return 0;
  114. head = &phy_roothub->list;
  115. list_for_each_entry(roothub_entry, head, list) {
  116. err = phy_init(roothub_entry->phy);
  117. if (err)
  118. goto err_exit_phys;
  119. }
  120. return 0;
  121. err_exit_phys:
  122. list_for_each_entry_continue_reverse(roothub_entry, head, list)
  123. phy_exit(roothub_entry->phy);
  124. return err;
  125. }
  126. EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
  127. int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
  128. {
  129. struct usb_phy_roothub *roothub_entry;
  130. struct list_head *head;
  131. int err, ret = 0;
  132. if (!phy_roothub)
  133. return 0;
  134. head = &phy_roothub->list;
  135. list_for_each_entry(roothub_entry, head, list) {
  136. err = phy_exit(roothub_entry->phy);
  137. if (err)
  138. ret = err;
  139. }
  140. return ret;
  141. }
  142. EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
  143. int usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub,
  144. enum phy_mode mode)
  145. {
  146. struct usb_phy_roothub *roothub_entry;
  147. struct list_head *head;
  148. int err;
  149. if (!phy_roothub)
  150. return 0;
  151. head = &phy_roothub->list;
  152. list_for_each_entry(roothub_entry, head, list) {
  153. err = phy_set_mode(roothub_entry->phy, mode);
  154. if (err)
  155. goto err_out;
  156. }
  157. return 0;
  158. err_out:
  159. list_for_each_entry_continue_reverse(roothub_entry, head, list)
  160. phy_power_off(roothub_entry->phy);
  161. return err;
  162. }
  163. EXPORT_SYMBOL_GPL(usb_phy_roothub_set_mode);
  164. int usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub)
  165. {
  166. struct usb_phy_roothub *roothub_entry;
  167. struct list_head *head;
  168. int err;
  169. if (!phy_roothub)
  170. return 0;
  171. head = &phy_roothub->list;
  172. list_for_each_entry(roothub_entry, head, list) {
  173. err = phy_calibrate(roothub_entry->phy);
  174. if (err)
  175. return err;
  176. }
  177. return 0;
  178. }
  179. EXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate);
  180. /**
  181. * usb_phy_roothub_notify_connect() - connect notification
  182. * @phy_roothub: the phy of roothub, if the host use a generic phy.
  183. * @port: the port index for connect
  184. *
  185. * If the phy needs to get connection status, the callback can be used.
  186. * Returns: %0 if successful, a negative error code otherwise
  187. */
  188. int usb_phy_roothub_notify_connect(struct usb_phy_roothub *phy_roothub, int port)
  189. {
  190. struct usb_phy_roothub *roothub_entry;
  191. struct list_head *head;
  192. int err;
  193. if (!phy_roothub)
  194. return 0;
  195. head = &phy_roothub->list;
  196. list_for_each_entry(roothub_entry, head, list) {
  197. err = phy_notify_connect(roothub_entry->phy, port);
  198. if (err)
  199. return err;
  200. }
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_connect);
  204. /**
  205. * usb_phy_roothub_notify_disconnect() - disconnect notification
  206. * @phy_roothub: the phy of roothub, if the host use a generic phy.
  207. * @port: the port index for disconnect
  208. *
  209. * If the phy needs to get connection status, the callback can be used.
  210. * Returns: %0 if successful, a negative error code otherwise
  211. */
  212. int usb_phy_roothub_notify_disconnect(struct usb_phy_roothub *phy_roothub, int port)
  213. {
  214. struct usb_phy_roothub *roothub_entry;
  215. struct list_head *head;
  216. int err;
  217. if (!phy_roothub)
  218. return 0;
  219. head = &phy_roothub->list;
  220. list_for_each_entry(roothub_entry, head, list) {
  221. err = phy_notify_disconnect(roothub_entry->phy, port);
  222. if (err)
  223. return err;
  224. }
  225. return 0;
  226. }
  227. EXPORT_SYMBOL_GPL(usb_phy_roothub_notify_disconnect);
  228. int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
  229. {
  230. struct usb_phy_roothub *roothub_entry;
  231. struct list_head *head;
  232. int err;
  233. if (!phy_roothub)
  234. return 0;
  235. head = &phy_roothub->list;
  236. list_for_each_entry(roothub_entry, head, list) {
  237. err = phy_power_on(roothub_entry->phy);
  238. if (err)
  239. goto err_out;
  240. }
  241. return 0;
  242. err_out:
  243. list_for_each_entry_continue_reverse(roothub_entry, head, list)
  244. phy_power_off(roothub_entry->phy);
  245. return err;
  246. }
  247. EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
  248. void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
  249. {
  250. struct usb_phy_roothub *roothub_entry;
  251. if (!phy_roothub)
  252. return;
  253. list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
  254. phy_power_off(roothub_entry->phy);
  255. }
  256. EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
  257. int usb_phy_roothub_suspend(struct device *controller_dev,
  258. struct usb_phy_roothub *phy_roothub)
  259. {
  260. usb_phy_roothub_power_off(phy_roothub);
  261. /* keep the PHYs initialized so the device can wake up the system */
  262. if (device_may_wakeup(controller_dev))
  263. return 0;
  264. return usb_phy_roothub_exit(phy_roothub);
  265. }
  266. EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
  267. int usb_phy_roothub_resume(struct device *controller_dev,
  268. struct usb_phy_roothub *phy_roothub)
  269. {
  270. int err;
  271. /* if the device can't wake up the system _exit was called */
  272. if (!device_may_wakeup(controller_dev)) {
  273. err = usb_phy_roothub_init(phy_roothub);
  274. if (err)
  275. return err;
  276. }
  277. err = usb_phy_roothub_power_on(phy_roothub);
  278. /* undo _init if _power_on failed */
  279. if (err && !device_may_wakeup(controller_dev))
  280. usb_phy_roothub_exit(phy_roothub);
  281. return err;
  282. }
  283. EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);