phy-qcom-usb-hs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * Copyright (C) 2016 Linaro Ltd
  4. */
  5. #include <linux/module.h>
  6. #include <linux/ulpi/driver.h>
  7. #include <linux/ulpi/regs.h>
  8. #include <linux/clk.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <linux/of_device.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/reset.h>
  13. #include <linux/extcon.h>
  14. #include <linux/notifier.h>
  15. #define ULPI_PWR_CLK_MNG_REG 0x88
  16. # define ULPI_PWR_OTG_COMP_DISABLE BIT(0)
  17. #define ULPI_MISC_A 0x96
  18. # define ULPI_MISC_A_VBUSVLDEXTSEL BIT(1)
  19. # define ULPI_MISC_A_VBUSVLDEXT BIT(0)
  20. struct ulpi_seq {
  21. u8 addr;
  22. u8 val;
  23. };
  24. struct qcom_usb_hs_phy {
  25. struct ulpi *ulpi;
  26. struct phy *phy;
  27. struct clk *ref_clk;
  28. struct clk *sleep_clk;
  29. struct regulator *v1p8;
  30. struct regulator *v3p3;
  31. struct reset_control *reset;
  32. struct ulpi_seq *init_seq;
  33. struct extcon_dev *vbus_edev;
  34. struct notifier_block vbus_notify;
  35. };
  36. static int qcom_usb_hs_phy_set_mode(struct phy *phy,
  37. enum phy_mode mode, int submode)
  38. {
  39. struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
  40. u8 addr;
  41. int ret;
  42. if (!uphy->vbus_edev) {
  43. u8 val = 0;
  44. switch (mode) {
  45. case PHY_MODE_USB_OTG:
  46. case PHY_MODE_USB_HOST:
  47. val |= ULPI_INT_IDGRD;
  48. fallthrough;
  49. case PHY_MODE_USB_DEVICE:
  50. val |= ULPI_INT_SESS_VALID;
  51. default:
  52. break;
  53. }
  54. ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_RISE, val);
  55. if (ret)
  56. return ret;
  57. ret = ulpi_write(uphy->ulpi, ULPI_USB_INT_EN_FALL, val);
  58. } else {
  59. switch (mode) {
  60. case PHY_MODE_USB_OTG:
  61. case PHY_MODE_USB_DEVICE:
  62. addr = ULPI_SET(ULPI_MISC_A);
  63. break;
  64. case PHY_MODE_USB_HOST:
  65. addr = ULPI_CLR(ULPI_MISC_A);
  66. break;
  67. default:
  68. return -EINVAL;
  69. }
  70. ret = ulpi_write(uphy->ulpi, ULPI_SET(ULPI_PWR_CLK_MNG_REG),
  71. ULPI_PWR_OTG_COMP_DISABLE);
  72. if (ret)
  73. return ret;
  74. ret = ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXTSEL);
  75. }
  76. return ret;
  77. }
  78. static int
  79. qcom_usb_hs_phy_vbus_notifier(struct notifier_block *nb, unsigned long event,
  80. void *ptr)
  81. {
  82. struct qcom_usb_hs_phy *uphy;
  83. u8 addr;
  84. uphy = container_of(nb, struct qcom_usb_hs_phy, vbus_notify);
  85. if (event)
  86. addr = ULPI_SET(ULPI_MISC_A);
  87. else
  88. addr = ULPI_CLR(ULPI_MISC_A);
  89. return ulpi_write(uphy->ulpi, addr, ULPI_MISC_A_VBUSVLDEXT);
  90. }
  91. static int qcom_usb_hs_phy_power_on(struct phy *phy)
  92. {
  93. struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
  94. struct ulpi *ulpi = uphy->ulpi;
  95. const struct ulpi_seq *seq;
  96. int ret, state;
  97. ret = clk_prepare_enable(uphy->ref_clk);
  98. if (ret)
  99. return ret;
  100. ret = clk_prepare_enable(uphy->sleep_clk);
  101. if (ret)
  102. goto err_sleep;
  103. ret = regulator_set_load(uphy->v1p8, 50000);
  104. if (ret < 0)
  105. goto err_1p8;
  106. ret = regulator_enable(uphy->v1p8);
  107. if (ret)
  108. goto err_1p8;
  109. ret = regulator_set_voltage_triplet(uphy->v3p3, 3050000, 3300000,
  110. 3300000);
  111. if (ret)
  112. goto err_3p3;
  113. ret = regulator_set_load(uphy->v3p3, 50000);
  114. if (ret < 0)
  115. goto err_3p3;
  116. ret = regulator_enable(uphy->v3p3);
  117. if (ret)
  118. goto err_3p3;
  119. for (seq = uphy->init_seq; seq->addr; seq++) {
  120. ret = ulpi_write(ulpi, ULPI_EXT_VENDOR_SPECIFIC + seq->addr,
  121. seq->val);
  122. if (ret)
  123. goto err_ulpi;
  124. }
  125. if (uphy->reset) {
  126. ret = reset_control_reset(uphy->reset);
  127. if (ret)
  128. goto err_ulpi;
  129. }
  130. if (uphy->vbus_edev) {
  131. state = extcon_get_state(uphy->vbus_edev, EXTCON_USB);
  132. /* setup initial state */
  133. qcom_usb_hs_phy_vbus_notifier(&uphy->vbus_notify, state,
  134. uphy->vbus_edev);
  135. ret = extcon_register_notifier(uphy->vbus_edev, EXTCON_USB,
  136. &uphy->vbus_notify);
  137. if (ret)
  138. goto err_ulpi;
  139. }
  140. return 0;
  141. err_ulpi:
  142. regulator_disable(uphy->v3p3);
  143. err_3p3:
  144. regulator_disable(uphy->v1p8);
  145. err_1p8:
  146. clk_disable_unprepare(uphy->sleep_clk);
  147. err_sleep:
  148. clk_disable_unprepare(uphy->ref_clk);
  149. return ret;
  150. }
  151. static int qcom_usb_hs_phy_power_off(struct phy *phy)
  152. {
  153. struct qcom_usb_hs_phy *uphy = phy_get_drvdata(phy);
  154. if (uphy->vbus_edev)
  155. extcon_unregister_notifier(uphy->vbus_edev, EXTCON_USB,
  156. &uphy->vbus_notify);
  157. regulator_disable(uphy->v3p3);
  158. regulator_disable(uphy->v1p8);
  159. clk_disable_unprepare(uphy->sleep_clk);
  160. clk_disable_unprepare(uphy->ref_clk);
  161. return 0;
  162. }
  163. static const struct phy_ops qcom_usb_hs_phy_ops = {
  164. .power_on = qcom_usb_hs_phy_power_on,
  165. .power_off = qcom_usb_hs_phy_power_off,
  166. .set_mode = qcom_usb_hs_phy_set_mode,
  167. .owner = THIS_MODULE,
  168. };
  169. static int qcom_usb_hs_phy_probe(struct ulpi *ulpi)
  170. {
  171. struct qcom_usb_hs_phy *uphy;
  172. struct phy_provider *p;
  173. struct clk *clk;
  174. struct regulator *reg;
  175. struct reset_control *reset;
  176. int size;
  177. int ret;
  178. uphy = devm_kzalloc(&ulpi->dev, sizeof(*uphy), GFP_KERNEL);
  179. if (!uphy)
  180. return -ENOMEM;
  181. ulpi_set_drvdata(ulpi, uphy);
  182. uphy->ulpi = ulpi;
  183. size = of_property_count_u8_elems(ulpi->dev.of_node, "qcom,init-seq");
  184. if (size < 0)
  185. size = 0;
  186. uphy->init_seq = devm_kmalloc_array(&ulpi->dev, (size / 2) + 1,
  187. sizeof(*uphy->init_seq), GFP_KERNEL);
  188. if (!uphy->init_seq)
  189. return -ENOMEM;
  190. ret = of_property_read_u8_array(ulpi->dev.of_node, "qcom,init-seq",
  191. (u8 *)uphy->init_seq, size);
  192. if (ret && size)
  193. return ret;
  194. /* NUL terminate */
  195. uphy->init_seq[size / 2].addr = uphy->init_seq[size / 2].val = 0;
  196. uphy->ref_clk = clk = devm_clk_get(&ulpi->dev, "ref");
  197. if (IS_ERR(clk))
  198. return PTR_ERR(clk);
  199. uphy->sleep_clk = clk = devm_clk_get(&ulpi->dev, "sleep");
  200. if (IS_ERR(clk))
  201. return PTR_ERR(clk);
  202. uphy->v1p8 = reg = devm_regulator_get(&ulpi->dev, "v1p8");
  203. if (IS_ERR(reg))
  204. return PTR_ERR(reg);
  205. uphy->v3p3 = reg = devm_regulator_get(&ulpi->dev, "v3p3");
  206. if (IS_ERR(reg))
  207. return PTR_ERR(reg);
  208. uphy->reset = reset = devm_reset_control_get(&ulpi->dev, "por");
  209. if (IS_ERR(reset)) {
  210. if (PTR_ERR(reset) == -EPROBE_DEFER)
  211. return PTR_ERR(reset);
  212. uphy->reset = NULL;
  213. }
  214. uphy->phy = devm_phy_create(&ulpi->dev, ulpi->dev.of_node,
  215. &qcom_usb_hs_phy_ops);
  216. if (IS_ERR(uphy->phy))
  217. return PTR_ERR(uphy->phy);
  218. uphy->vbus_edev = extcon_get_edev_by_phandle(&ulpi->dev, 0);
  219. if (IS_ERR(uphy->vbus_edev)) {
  220. if (PTR_ERR(uphy->vbus_edev) != -ENODEV)
  221. return PTR_ERR(uphy->vbus_edev);
  222. uphy->vbus_edev = NULL;
  223. }
  224. uphy->vbus_notify.notifier_call = qcom_usb_hs_phy_vbus_notifier;
  225. phy_set_drvdata(uphy->phy, uphy);
  226. p = devm_of_phy_provider_register(&ulpi->dev, of_phy_simple_xlate);
  227. return PTR_ERR_OR_ZERO(p);
  228. }
  229. static const struct of_device_id qcom_usb_hs_phy_match[] = {
  230. { .compatible = "qcom,usb-hs-phy", },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(of, qcom_usb_hs_phy_match);
  234. static struct ulpi_driver qcom_usb_hs_phy_driver = {
  235. .probe = qcom_usb_hs_phy_probe,
  236. .driver = {
  237. .name = "qcom_usb_hs_phy",
  238. .of_match_table = qcom_usb_hs_phy_match,
  239. },
  240. };
  241. module_ulpi_driver(qcom_usb_hs_phy_driver);
  242. MODULE_DESCRIPTION("Qualcomm USB HS phy");
  243. MODULE_LICENSE("GPL v2");