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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_bulk_data regulator[3];
  58. struct reset_control *phy_reset;
  59. struct extcon_dev *vbus_edev;
  60. struct notifier_block vbus_notify;
  61. struct gpio_desc *switch_gpio;
  62. struct notifier_block reboot_notify;
  63. };
  64. static int phy_8x16_notify_connect(struct usb_phy *phy,
  65. enum usb_device_speed speed)
  66. {
  67. struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
  68. u32 val;
  69. val = ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT;
  70. usb_phy_io_write(&qphy->phy, val, ULPI_SET(ULPI_MISC_A));
  71. val = readl(qphy->regs + HSPHY_USBCMD);
  72. val |= HSPHY_SESS_VLD_CTRL;
  73. writel(val, qphy->regs + HSPHY_USBCMD);
  74. return 0;
  75. }
  76. static int phy_8x16_notify_disconnect(struct usb_phy *phy,
  77. enum usb_device_speed speed)
  78. {
  79. struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
  80. u32 val;
  81. val = ULPI_MISC_A_VBUSVLDEXT | ULPI_MISC_A_VBUSVLDEXTSEL;
  82. usb_phy_io_write(&qphy->phy, val, ULPI_CLR(ULPI_MISC_A));
  83. val = readl(qphy->regs + HSPHY_USBCMD);
  84. val &= ~HSPHY_SESS_VLD_CTRL;
  85. writel(val, qphy->regs + HSPHY_USBCMD);
  86. return 0;
  87. }
  88. static int phy_8x16_vbus_on(struct phy_8x16 *qphy)
  89. {
  90. phy_8x16_notify_connect(&qphy->phy, USB_SPEED_UNKNOWN);
  91. /* Switch D+/D- lines to Device connector */
  92. gpiod_set_value_cansleep(qphy->switch_gpio, 0);
  93. return 0;
  94. }
  95. static int phy_8x16_vbus_off(struct phy_8x16 *qphy)
  96. {
  97. phy_8x16_notify_disconnect(&qphy->phy, USB_SPEED_UNKNOWN);
  98. /* Switch D+/D- lines to USB HUB */
  99. gpiod_set_value_cansleep(qphy->switch_gpio, 1);
  100. return 0;
  101. }
  102. static int phy_8x16_vbus_notify(struct notifier_block *nb, unsigned long event,
  103. void *ptr)
  104. {
  105. struct phy_8x16 *qphy = container_of(nb, struct phy_8x16, vbus_notify);
  106. if (event)
  107. phy_8x16_vbus_on(qphy);
  108. else
  109. phy_8x16_vbus_off(qphy);
  110. return NOTIFY_DONE;
  111. }
  112. static int phy_8x16_init(struct usb_phy *phy)
  113. {
  114. struct phy_8x16 *qphy = container_of(phy, struct phy_8x16, phy);
  115. u32 val, init[] = {0x44, 0x6B, 0x24, 0x13};
  116. u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
  117. int idx, state;
  118. for (idx = 0; idx < ARRAY_SIZE(init); idx++)
  119. usb_phy_io_write(phy, init[idx], addr + idx);
  120. reset_control_reset(qphy->phy_reset);
  121. /* Assert USB HSPHY_POR */
  122. val = readl(qphy->regs + HSPHY_CTRL);
  123. val |= HSPHY_POR_ASSERT;
  124. writel(val, qphy->regs + HSPHY_CTRL);
  125. /*
  126. * wait for minimum 10 microseconds as suggested in HPG.
  127. * Use a slightly larger value since the exact value didn't
  128. * work 100% of the time.
  129. */
  130. usleep_range(12, 15);
  131. /* Deassert USB HSPHY_POR */
  132. val = readl(qphy->regs + HSPHY_CTRL);
  133. val &= ~HSPHY_POR_ASSERT;
  134. writel(val, qphy->regs + HSPHY_CTRL);
  135. usleep_range(10, 15);
  136. writel(0x00, qphy->regs + HSPHY_AHBBURST);
  137. writel(0x08, qphy->regs + HSPHY_AHBMODE);
  138. /* workaround for rx buffer collision issue */
  139. val = readl(qphy->regs + HSPHY_GENCONFIG);
  140. val &= ~HSPHY_TXFIFO_IDLE_FORCE_DIS;
  141. writel(val, qphy->regs + HSPHY_GENCONFIG);
  142. val = readl(qphy->regs + HSPHY_GENCONFIG_2);
  143. val |= HSPHY_SESS_VLD_CTRL_EN;
  144. writel(val, qphy->regs + HSPHY_GENCONFIG_2);
  145. val = ULPI_PWR_OTG_COMP_DISABLE;
  146. usb_phy_io_write(phy, val, ULPI_SET(ULPI_PWR_CLK_MNG_REG));
  147. state = extcon_get_cable_state_(qphy->vbus_edev, EXTCON_USB);
  148. if (state)
  149. phy_8x16_vbus_on(qphy);
  150. else
  151. phy_8x16_vbus_off(qphy);
  152. val = usb_phy_io_read(&qphy->phy, ULPI_FUNC_CTRL);
  153. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  154. val |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  155. usb_phy_io_write(&qphy->phy, val, ULPI_FUNC_CTRL);
  156. return 0;
  157. }
  158. static void phy_8x16_shutdown(struct usb_phy *phy)
  159. {
  160. u32 val;
  161. /* Put the controller in non-driving mode */
  162. val = usb_phy_io_read(phy, ULPI_FUNC_CTRL);
  163. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  164. val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  165. usb_phy_io_write(phy, val, ULPI_FUNC_CTRL);
  166. }
  167. static int phy_8x16_read_devicetree(struct phy_8x16 *qphy)
  168. {
  169. struct device *dev = qphy->phy.dev;
  170. int ret;
  171. qphy->core_clk = devm_clk_get(dev, "core");
  172. if (IS_ERR(qphy->core_clk))
  173. return PTR_ERR(qphy->core_clk);
  174. qphy->iface_clk = devm_clk_get(dev, "iface");
  175. if (IS_ERR(qphy->iface_clk))
  176. return PTR_ERR(qphy->iface_clk);
  177. qphy->regulator[0].supply = "v3p3";
  178. qphy->regulator[1].supply = "v1p8";
  179. qphy->regulator[2].supply = "vddcx";
  180. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(qphy->regulator),
  181. qphy->regulator);
  182. if (ret)
  183. return ret;
  184. qphy->phy_reset = devm_reset_control_get(dev, "phy");
  185. if (IS_ERR(qphy->phy_reset))
  186. return PTR_ERR(qphy->phy_reset);
  187. qphy->switch_gpio = devm_gpiod_get_optional(dev, "switch",
  188. GPIOD_OUT_LOW);
  189. return PTR_ERR_OR_ZERO(qphy->switch_gpio);
  190. }
  191. static int phy_8x16_reboot_notify(struct notifier_block *this,
  192. unsigned long code, void *unused)
  193. {
  194. struct phy_8x16 *qphy;
  195. qphy = container_of(this, struct phy_8x16, reboot_notify);
  196. /*
  197. * Ensure that D+/D- lines are routed to uB connector, so
  198. * we could load bootloader/kernel at next reboot_notify
  199. */
  200. gpiod_set_value_cansleep(qphy->switch_gpio, 0);
  201. return NOTIFY_DONE;
  202. }
  203. static int phy_8x16_probe(struct platform_device *pdev)
  204. {
  205. struct phy_8x16 *qphy;
  206. struct resource *res;
  207. struct usb_phy *phy;
  208. int ret;
  209. qphy = devm_kzalloc(&pdev->dev, sizeof(*qphy), GFP_KERNEL);
  210. if (!qphy)
  211. return -ENOMEM;
  212. platform_set_drvdata(pdev, qphy);
  213. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. if (!res)
  215. return -EINVAL;
  216. qphy->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  217. if (!qphy->regs)
  218. return -ENOMEM;
  219. phy = &qphy->phy;
  220. phy->dev = &pdev->dev;
  221. phy->label = dev_name(&pdev->dev);
  222. phy->init = phy_8x16_init;
  223. phy->shutdown = phy_8x16_shutdown;
  224. phy->notify_connect = phy_8x16_notify_connect;
  225. phy->notify_disconnect = phy_8x16_notify_disconnect;
  226. phy->io_priv = qphy->regs + HSPHY_ULPI_VIEWPORT;
  227. phy->io_ops = &ulpi_viewport_access_ops;
  228. phy->type = USB_PHY_TYPE_USB2;
  229. ret = phy_8x16_read_devicetree(qphy);
  230. if (ret < 0)
  231. return ret;
  232. qphy->vbus_edev = extcon_get_edev_by_phandle(phy->dev, 0);
  233. if (IS_ERR(qphy->vbus_edev))
  234. return PTR_ERR(qphy->vbus_edev);
  235. ret = clk_set_rate(qphy->core_clk, INT_MAX);
  236. if (ret < 0)
  237. dev_dbg(phy->dev, "Can't boost core clock\n");
  238. ret = clk_prepare_enable(qphy->core_clk);
  239. if (ret < 0)
  240. return ret;
  241. ret = clk_prepare_enable(qphy->iface_clk);
  242. if (ret < 0)
  243. goto off_core;
  244. ret = regulator_bulk_enable(ARRAY_SIZE(qphy->regulator),
  245. qphy->regulator);
  246. if (WARN_ON(ret))
  247. goto off_clks;
  248. qphy->vbus_notify.notifier_call = phy_8x16_vbus_notify;
  249. ret = extcon_register_notifier(qphy->vbus_edev, EXTCON_USB,
  250. &qphy->vbus_notify);
  251. if (ret < 0)
  252. goto off_power;
  253. ret = usb_add_phy_dev(&qphy->phy);
  254. if (ret)
  255. goto off_extcon;
  256. qphy->reboot_notify.notifier_call = phy_8x16_reboot_notify;
  257. register_reboot_notifier(&qphy->reboot_notify);
  258. return 0;
  259. off_extcon:
  260. extcon_unregister_notifier(qphy->vbus_edev, EXTCON_USB,
  261. &qphy->vbus_notify);
  262. off_power:
  263. regulator_bulk_disable(ARRAY_SIZE(qphy->regulator), qphy->regulator);
  264. off_clks:
  265. clk_disable_unprepare(qphy->iface_clk);
  266. off_core:
  267. clk_disable_unprepare(qphy->core_clk);
  268. return ret;
  269. }
  270. static int phy_8x16_remove(struct platform_device *pdev)
  271. {
  272. struct phy_8x16 *qphy = platform_get_drvdata(pdev);
  273. unregister_reboot_notifier(&qphy->reboot_notify);
  274. extcon_unregister_notifier(qphy->vbus_edev, EXTCON_USB,
  275. &qphy->vbus_notify);
  276. /*
  277. * Ensure that D+/D- lines are routed to uB connector, so
  278. * we could load bootloader/kernel at next reboot_notify
  279. */
  280. gpiod_set_value_cansleep(qphy->switch_gpio, 0);
  281. usb_remove_phy(&qphy->phy);
  282. clk_disable_unprepare(qphy->iface_clk);
  283. clk_disable_unprepare(qphy->core_clk);
  284. regulator_bulk_disable(ARRAY_SIZE(qphy->regulator), qphy->regulator);
  285. return 0;
  286. }
  287. static const struct of_device_id phy_8x16_dt_match[] = {
  288. { .compatible = "qcom,usb-8x16-phy" },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(of, phy_8x16_dt_match);
  292. static struct platform_driver phy_8x16_driver = {
  293. .probe = phy_8x16_probe,
  294. .remove = phy_8x16_remove,
  295. .driver = {
  296. .name = "phy-qcom-8x16-usb",
  297. .of_match_table = phy_8x16_dt_match,
  298. },
  299. };
  300. module_platform_driver(phy_8x16_driver);
  301. MODULE_LICENSE("GPL v2");
  302. MODULE_DESCRIPTION("Qualcomm APQ8016/MSM8916 chipsets USB transceiver driver");