phy.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. static int usb_phy_roothub_add_phy(struct device *dev, int index,
  19. struct list_head *list)
  20. {
  21. struct usb_phy_roothub *roothub_entry;
  22. struct phy *phy;
  23. phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
  24. if (IS_ERR(phy)) {
  25. if (PTR_ERR(phy) == -ENODEV)
  26. return 0;
  27. else
  28. return PTR_ERR(phy);
  29. }
  30. roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
  31. if (!roothub_entry)
  32. return -ENOMEM;
  33. INIT_LIST_HEAD(&roothub_entry->list);
  34. roothub_entry->phy = phy;
  35. list_add_tail(&roothub_entry->list, list);
  36. return 0;
  37. }
  38. struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
  39. {
  40. struct usb_phy_roothub *phy_roothub;
  41. int i, num_phys, err;
  42. if (!IS_ENABLED(CONFIG_GENERIC_PHY))
  43. return NULL;
  44. num_phys = of_count_phandle_with_args(dev->of_node, "phys",
  45. "#phy-cells");
  46. if (num_phys <= 0)
  47. return NULL;
  48. phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
  49. if (!phy_roothub)
  50. return ERR_PTR(-ENOMEM);
  51. INIT_LIST_HEAD(&phy_roothub->list);
  52. for (i = 0; i < num_phys; i++) {
  53. err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
  54. if (err)
  55. return ERR_PTR(err);
  56. }
  57. return phy_roothub;
  58. }
  59. EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
  60. int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
  61. {
  62. struct usb_phy_roothub *roothub_entry;
  63. struct list_head *head;
  64. int err;
  65. if (!phy_roothub)
  66. return 0;
  67. head = &phy_roothub->list;
  68. list_for_each_entry(roothub_entry, head, list) {
  69. err = phy_init(roothub_entry->phy);
  70. if (err)
  71. goto err_exit_phys;
  72. }
  73. return 0;
  74. err_exit_phys:
  75. list_for_each_entry_continue_reverse(roothub_entry, head, list)
  76. phy_exit(roothub_entry->phy);
  77. return err;
  78. }
  79. EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
  80. int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
  81. {
  82. struct usb_phy_roothub *roothub_entry;
  83. struct list_head *head;
  84. int err, ret = 0;
  85. if (!phy_roothub)
  86. return 0;
  87. head = &phy_roothub->list;
  88. list_for_each_entry(roothub_entry, head, list) {
  89. err = phy_exit(roothub_entry->phy);
  90. if (err)
  91. ret = err;
  92. }
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
  96. int usb_phy_roothub_set_mode(struct usb_phy_roothub *phy_roothub,
  97. enum phy_mode mode)
  98. {
  99. struct usb_phy_roothub *roothub_entry;
  100. struct list_head *head;
  101. int err;
  102. if (!phy_roothub)
  103. return 0;
  104. head = &phy_roothub->list;
  105. list_for_each_entry(roothub_entry, head, list) {
  106. err = phy_set_mode(roothub_entry->phy, mode);
  107. if (err)
  108. goto err_out;
  109. }
  110. return 0;
  111. err_out:
  112. list_for_each_entry_continue_reverse(roothub_entry, head, list)
  113. phy_power_off(roothub_entry->phy);
  114. return err;
  115. }
  116. EXPORT_SYMBOL_GPL(usb_phy_roothub_set_mode);
  117. int usb_phy_roothub_calibrate(struct usb_phy_roothub *phy_roothub)
  118. {
  119. struct usb_phy_roothub *roothub_entry;
  120. struct list_head *head;
  121. int err;
  122. if (!phy_roothub)
  123. return 0;
  124. head = &phy_roothub->list;
  125. list_for_each_entry(roothub_entry, head, list) {
  126. err = phy_calibrate(roothub_entry->phy);
  127. if (err)
  128. return err;
  129. }
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(usb_phy_roothub_calibrate);
  133. int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
  134. {
  135. struct usb_phy_roothub *roothub_entry;
  136. struct list_head *head;
  137. int err;
  138. if (!phy_roothub)
  139. return 0;
  140. head = &phy_roothub->list;
  141. list_for_each_entry(roothub_entry, head, list) {
  142. err = phy_power_on(roothub_entry->phy);
  143. if (err)
  144. goto err_out;
  145. }
  146. return 0;
  147. err_out:
  148. list_for_each_entry_continue_reverse(roothub_entry, head, list)
  149. phy_power_off(roothub_entry->phy);
  150. return err;
  151. }
  152. EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
  153. void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
  154. {
  155. struct usb_phy_roothub *roothub_entry;
  156. if (!phy_roothub)
  157. return;
  158. list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
  159. phy_power_off(roothub_entry->phy);
  160. }
  161. EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
  162. int usb_phy_roothub_suspend(struct device *controller_dev,
  163. struct usb_phy_roothub *phy_roothub)
  164. {
  165. usb_phy_roothub_power_off(phy_roothub);
  166. /* keep the PHYs initialized so the device can wake up the system */
  167. if (device_may_wakeup(controller_dev))
  168. return 0;
  169. return usb_phy_roothub_exit(phy_roothub);
  170. }
  171. EXPORT_SYMBOL_GPL(usb_phy_roothub_suspend);
  172. int usb_phy_roothub_resume(struct device *controller_dev,
  173. struct usb_phy_roothub *phy_roothub)
  174. {
  175. int err;
  176. /* if the device can't wake up the system _exit was called */
  177. if (!device_may_wakeup(controller_dev)) {
  178. err = usb_phy_roothub_init(phy_roothub);
  179. if (err)
  180. return err;
  181. }
  182. err = usb_phy_roothub_power_on(phy_roothub);
  183. /* undo _init if _power_on failed */
  184. if (err && !device_may_wakeup(controller_dev))
  185. usb_phy_roothub_exit(phy_roothub);
  186. return err;
  187. }
  188. EXPORT_SYMBOL_GPL(usb_phy_roothub_resume);