phy-qcom-usb-hs.c 6.7 KB

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