phy-qcom-8x16-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2015, Linaro Limited
  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 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/extcon.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reboot.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/reset.h>
  26. #include <linux/slab.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/ulpi.h>
  29. #define HSPHY_AHBBURST 0x0090
  30. #define HSPHY_AHBMODE 0x0098
  31. #define HSPHY_GENCONFIG 0x009c
  32. #define HSPHY_GENCONFIG_2 0x00a0
  33. #define HSPHY_USBCMD 0x0140
  34. #define HSPHY_ULPI_VIEWPORT 0x0170
  35. #define HSPHY_CTRL 0x0240
  36. #define HSPHY_TXFIFO_IDLE_FORCE_DIS BIT(4)
  37. #define HSPHY_SESS_VLD_CTRL_EN BIT(7)
  38. #define HSPHY_POR_ASSERT BIT(0)
  39. #define HSPHY_RETEN BIT(1)
  40. #define HSPHY_SESS_VLD_CTRL BIT(25)
  41. #define ULPI_PWR_CLK_MNG_REG 0x88
  42. #define ULPI_PWR_OTG_COMP_DISABLE BIT(0)
  43. #define ULPI_MISC_A 0x96
  44. #define ULPI_MISC_A_VBUSVLDEXTSEL BIT(1)
  45. #define ULPI_MISC_A_VBUSVLDEXT BIT(0)
  46. #define HSPHY_3P3_MIN 3050000 /* uV */
  47. #define HSPHY_3P3_MAX 3300000 /* uV */
  48. #define HSPHY_1P8_MIN 1800000 /* uV */
  49. #define HSPHY_1P8_MAX 1800000 /* uV */
  50. #define HSPHY_VDD_MIN 5
  51. #define HSPHY_VDD_MAX 7
  52. struct phy_8x16 {
  53. struct usb_phy phy;
  54. void __iomem *regs;
  55. struct clk *core_clk;
  56. struct clk *iface_clk;
  57. struct regulator *v3p3;
  58. struct regulator *v1p8;
  59. struct regulator *vdd;
  60. struct reset_control *phy_reset;
  61. struct extcon_dev *vbus_edev;
  62. struct notifier_block vbus_notify;
  63. struct gpio_desc *switch_gpio;
  64. struct notifier_block reboot_notify;
  65. };
  66. static int phy_8x16_regulators_enable(struct phy_8x16 *qphy)
  67. {
  68. int ret;
  69. ret = regulator_set_voltage(qphy->vdd, HSPHY_VDD_MIN, HSPHY_VDD_MAX);
  70. if (ret)
  71. return ret;
  72. ret = regulator_enable(qphy->vdd);
  73. if (ret)
  74. return ret;
  75. ret = regulator_set_voltage(qphy->v3p3, HSPHY_3P3_MIN, HSPHY_3P3_MAX);
  76. if (ret)
  77. goto off_vdd;
  78. ret = regulator_enable(qphy->v3p3);
  79. if (ret)
  80. goto off_vdd;
  81. ret = regulator_set_voltage(qphy->v1p8, HSPHY_1P8_MIN, HSPHY_1P8_MAX);
  82. if (ret)
  83. goto off_3p3;
  84. ret = regulator_enable(qphy->v1p8);
  85. if (ret)
  86. goto off_3p3;
  87. return 0;
  88. off_3p3:
  89. regulator_disable(qphy->v3p3);
  90. off_vdd:
  91. regulator_disable(qphy->vdd);
  92. return ret;
  93. }
  94. static void phy_8x16_regulators_disable(struct phy_8x16 *qphy)
  95. {
  96. regulator_disable(qphy->v1p8);
  97. regulator_disable(qphy->v3p3);
  98. regulator_disable(qphy->vdd);
  99. }
  100. static int phy_8x16_notify_connect(struct usb_phy *phy,
  101. enum usb_device_speed speed)
  102. {
  103. struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
  104. u32 val;
  105. val = ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT;
  106. usb_phy_io_write(&qphy->phy, val, ULPI_SET(ULPI_MISC_A));
  107. val = readl(qphy->regs + HSPHY_USBCMD);
  108. val |= HSPHY_SESS_VLD_CTRL;
  109. writel(val, qphy->regs + HSPHY_USBCMD);
  110. return 0;
  111. }
  112. static int phy_8x16_notify_disconnect(struct usb_phy *phy,
  113. enum usb_device_speed speed)
  114. {
  115. struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
  116. u32 val;
  117. val = ULPI_MISC_A_VBUSVLDEXT | ULPI_MISC_A_VBUSVLDEXTSEL;
  118. usb_phy_io_write(&qphy->phy, val, ULPI_CLR(ULPI_MISC_A));
  119. val = readl(qphy->regs + HSPHY_USBCMD);
  120. val &= ~HSPHY_SESS_VLD_CTRL;
  121. writel(val, qphy->regs + HSPHY_USBCMD);
  122. return 0;
  123. }
  124. static int phy_8x16_vbus_on(struct phy_8x16 *qphy)
  125. {
  126. phy_8x16_notify_connect(&qphy->phy, USB_SPEED_UNKNOWN);
  127. /* Switch D+/D- lines to Device connector */
  128. gpiod_set_value_cansleep(qphy->switch_gpio, 0);
  129. return 0;
  130. }
  131. static int phy_8x16_vbus_off(struct phy_8x16 *qphy)
  132. {
  133. phy_8x16_notify_disconnect(&qphy->phy, USB_SPEED_UNKNOWN);
  134. /* Switch D+/D- lines to USB HUB */
  135. gpiod_set_value_cansleep(qphy->switch_gpio, 1);
  136. return 0;
  137. }
  138. static int phy_8x16_vbus_notify(struct notifier_block *nb, unsigned long event,
  139. void *ptr)
  140. {
  141. struct phy_8x16 *qphy = container_of(nb, struct phy_8x16, vbus_notify);
  142. if (event)
  143. phy_8x16_vbus_on(qphy);
  144. else
  145. phy_8x16_vbus_off(qphy);
  146. return NOTIFY_DONE;
  147. }
  148. static int phy_8x16_init(struct usb_phy *phy)
  149. {
  150. struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
  151. u32 val, init[] = {0x44, 0x6B, 0x24, 0x13};
  152. u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
  153. int idx, state;
  154. for (idx = 0; idx < ARRAY_SIZE(init); idx++)
  155. usb_phy_io_write(phy, init[idx], addr + idx);
  156. reset_control_reset(qphy->phy_reset);
  157. /* Assert USB HSPHY_POR */
  158. val = readl(qphy->regs + HSPHY_CTRL);
  159. val |= HSPHY_POR_ASSERT;
  160. writel(val, qphy->regs + HSPHY_CTRL);
  161. /*
  162. * wait for minimum 10 microseconds as suggested in HPG.
  163. * Use a slightly larger value since the exact value didn't
  164. * work 100% of the time.
  165. */
  166. usleep_range(12, 15);
  167. /* Deassert USB HSPHY_POR */
  168. val = readl(qphy->regs + HSPHY_CTRL);
  169. val &= ~HSPHY_POR_ASSERT;
  170. writel(val, qphy->regs + HSPHY_CTRL);
  171. usleep_range(10, 15);
  172. writel(0x00, qphy->regs + HSPHY_AHBBURST);
  173. writel(0x08, qphy->regs + HSPHY_AHBMODE);
  174. /* workaround for rx buffer collision issue */
  175. val = readl(qphy->regs + HSPHY_GENCONFIG);
  176. val &= ~HSPHY_TXFIFO_IDLE_FORCE_DIS;
  177. writel(val, qphy->regs + HSPHY_GENCONFIG);
  178. val = readl(qphy->regs + HSPHY_GENCONFIG_2);
  179. val |= HSPHY_SESS_VLD_CTRL_EN;
  180. writel(val, qphy->regs + HSPHY_GENCONFIG_2);
  181. val = ULPI_PWR_OTG_COMP_DISABLE;
  182. usb_phy_io_write(phy, val, ULPI_SET(ULPI_PWR_CLK_MNG_REG));
  183. state = extcon_get_cable_state_(qphy->vbus_edev, EXTCON_USB);
  184. if (state)
  185. phy_8x16_vbus_on(qphy);
  186. else
  187. phy_8x16_vbus_off(qphy);
  188. val = usb_phy_io_read(&qphy->phy, ULPI_FUNC_CTRL);
  189. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  190. val |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  191. usb_phy_io_write(&qphy->phy, val, ULPI_FUNC_CTRL);
  192. return 0;
  193. }
  194. static void phy_8x16_shutdown(struct usb_phy *phy)
  195. {
  196. u32 val;
  197. /* Put the controller in non-driving mode */
  198. val = usb_phy_io_read(phy, ULPI_FUNC_CTRL);
  199. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  200. val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  201. usb_phy_io_write(phy, val, ULPI_FUNC_CTRL);
  202. }
  203. static int phy_8x16_read_devicetree(struct phy_8x16 *qphy)
  204. {
  205. struct regulator_bulk_data regs[3];
  206. struct device *dev = qphy->phy.dev;
  207. int ret;
  208. qphy->core_clk = devm_clk_get(dev, "core");
  209. if (IS_ERR(qphy->core_clk))
  210. return PTR_ERR(qphy->core_clk);
  211. qphy->iface_clk = devm_clk_get(dev, "iface");
  212. if (IS_ERR(qphy->iface_clk))
  213. return PTR_ERR(qphy->iface_clk);
  214. regs[0].supply = "v3p3";
  215. regs[1].supply = "v1p8";
  216. regs[2].supply = "vddcx";
  217. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(regs), regs);
  218. if (ret)
  219. return ret;
  220. qphy->v3p3 = regs[0].consumer;
  221. qphy->v1p8 = regs[1].consumer;
  222. qphy->vdd = regs[2].consumer;
  223. qphy->phy_reset = devm_reset_control_get(dev, "phy");
  224. if (IS_ERR(qphy->phy_reset))
  225. return PTR_ERR(qphy->phy_reset);
  226. qphy->switch_gpio = devm_gpiod_get_optional(dev, "switch",
  227. GPIOD_OUT_LOW);
  228. if (IS_ERR(qphy->switch_gpio))
  229. return PTR_ERR(qphy->switch_gpio);
  230. return 0;
  231. }
  232. static int phy_8x16_reboot_notify(struct notifier_block *this,
  233. unsigned long code, void *unused)
  234. {
  235. struct phy_8x16 *qphy;
  236. qphy = container_of(this, struct phy_8x16, reboot_notify);
  237. /*
  238. * Ensure that D+/D- lines are routed to uB connector, so
  239. * we could load bootloader/kernel at next reboot_notify
  240. */
  241. gpiod_set_value_cansleep(qphy->switch_gpio, 0);
  242. return NOTIFY_DONE;
  243. }
  244. static int phy_8x16_probe(struct platform_device *pdev)
  245. {
  246. struct phy_8x16 *qphy;
  247. struct resource *res;
  248. struct usb_phy *phy;
  249. int ret;
  250. qphy = devm_kzalloc(&pdev->dev, sizeof(*qphy), GFP_KERNEL);
  251. if (!qphy)
  252. return -ENOMEM;
  253. platform_set_drvdata(pdev, qphy);
  254. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. if (!res)
  256. return -EINVAL;
  257. qphy->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  258. if (!qphy->regs)
  259. return -ENOMEM;
  260. phy = &qphy->phy;
  261. phy->dev = &pdev->dev;
  262. phy->label = dev_name(&pdev->dev);
  263. phy->init = phy_8x16_init;
  264. phy->shutdown = phy_8x16_shutdown;
  265. phy->notify_connect = phy_8x16_notify_connect;
  266. phy->notify_disconnect = phy_8x16_notify_disconnect;
  267. phy->io_priv = qphy->regs + HSPHY_ULPI_VIEWPORT;
  268. phy->io_ops = &ulpi_viewport_access_ops;
  269. phy->type = USB_PHY_TYPE_USB2;
  270. ret = phy_8x16_read_devicetree(qphy);
  271. if (ret < 0)
  272. return ret;
  273. qphy->vbus_edev = extcon_get_edev_by_phandle(phy->dev, 0);
  274. if (IS_ERR(qphy->vbus_edev))
  275. return PTR_ERR(qphy->vbus_edev);
  276. ret = clk_set_rate(qphy->core_clk, INT_MAX);
  277. if (ret < 0)
  278. dev_dbg(phy->dev, "Can't boost core clock\n");
  279. ret = clk_prepare_enable(qphy->core_clk);
  280. if (ret < 0)
  281. return ret;
  282. ret = clk_prepare_enable(qphy->iface_clk);
  283. if (ret < 0)
  284. goto off_core;
  285. ret = phy_8x16_regulators_enable(qphy);
  286. if (0 && ret)
  287. goto off_clks;
  288. qphy->vbus_notify.notifier_call = phy_8x16_vbus_notify;
  289. ret = extcon_register_notifier(qphy->vbus_edev, EXTCON_USB,
  290. &qphy->vbus_notify);
  291. if (ret < 0)
  292. goto off_power;
  293. ret = usb_add_phy_dev(&qphy->phy);
  294. if (ret)
  295. goto off_extcon;
  296. qphy->reboot_notify.notifier_call = phy_8x16_reboot_notify;
  297. register_reboot_notifier(&qphy->reboot_notify);
  298. return 0;
  299. off_extcon:
  300. extcon_unregister_notifier(qphy->vbus_edev, EXTCON_USB,
  301. &qphy->vbus_notify);
  302. off_power:
  303. phy_8x16_regulators_disable(qphy);
  304. off_clks:
  305. clk_disable_unprepare(qphy->iface_clk);
  306. off_core:
  307. clk_disable_unprepare(qphy->core_clk);
  308. return ret;
  309. }
  310. static int phy_8x16_remove(struct platform_device *pdev)
  311. {
  312. struct phy_8x16 *qphy = platform_get_drvdata(pdev);
  313. unregister_reboot_notifier(&qphy->reboot_notify);
  314. extcon_unregister_notifier(qphy->vbus_edev, EXTCON_USB,
  315. &qphy->vbus_notify);
  316. /*
  317. * Ensure that D+/D- lines are routed to uB connector, so
  318. * we could load bootloader/kernel at next reboot_notify
  319. */
  320. gpiod_set_value_cansleep(qphy->switch_gpio, 0);
  321. usb_remove_phy(&qphy->phy);
  322. clk_disable_unprepare(qphy->iface_clk);
  323. clk_disable_unprepare(qphy->core_clk);
  324. phy_8x16_regulators_disable(qphy);
  325. return 0;
  326. }
  327. static const struct of_device_id phy_8x16_dt_match[] = {
  328. { .compatible = "qcom,usb-8x16-phy" },
  329. { }
  330. };
  331. MODULE_DEVICE_TABLE(of, phy_8x16_dt_match);
  332. static struct platform_driver phy_8x16_driver = {
  333. .probe = phy_8x16_probe,
  334. .remove = phy_8x16_remove,
  335. .driver = {
  336. .name = "phy-qcom-8x16-usb",
  337. .of_match_table = phy_8x16_dt_match,
  338. },
  339. };
  340. module_platform_driver(phy_8x16_driver);
  341. MODULE_LICENSE("GPL v2");
  342. MODULE_DESCRIPTION("Qualcomm APQ8016/MSM8916 chipsets USB transceiver driver");