phy-lgm-usb.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel LGM USB PHY driver
  4. *
  5. * Copyright (C) 2020 Intel Corporation.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/delay.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/reset.h>
  15. #include <linux/usb/phy.h>
  16. #include <linux/workqueue.h>
  17. #define CTRL1_OFFSET 0x14
  18. #define SRAM_EXT_LD_DONE BIT(25)
  19. #define SRAM_INIT_DONE BIT(26)
  20. #define TCPC_OFFSET 0x1014
  21. #define TCPC_MUX_CTL GENMASK(1, 0)
  22. #define MUX_NC 0
  23. #define MUX_USB 1
  24. #define MUX_DP 2
  25. #define MUX_USBDP 3
  26. #define TCPC_FLIPPED BIT(2)
  27. #define TCPC_LOW_POWER_EN BIT(3)
  28. #define TCPC_VALID BIT(4)
  29. #define TCPC_CONN \
  30. (TCPC_VALID | FIELD_PREP(TCPC_MUX_CTL, MUX_USB))
  31. #define TCPC_DISCONN \
  32. (TCPC_VALID | FIELD_PREP(TCPC_MUX_CTL, MUX_NC) | TCPC_LOW_POWER_EN)
  33. static const char *const PHY_RESETS[] = { "phy31", "phy", };
  34. static const char *const CTL_RESETS[] = { "apb", "ctrl", };
  35. struct tca_apb {
  36. struct reset_control *resets[ARRAY_SIZE(PHY_RESETS)];
  37. struct regulator *vbus;
  38. struct work_struct wk;
  39. struct usb_phy phy;
  40. bool regulator_enabled;
  41. bool phy_initialized;
  42. bool connected;
  43. };
  44. static int get_flipped(struct tca_apb *ta, bool *flipped)
  45. {
  46. union extcon_property_value property;
  47. int ret;
  48. ret = extcon_get_property(ta->phy.edev, EXTCON_USB_HOST,
  49. EXTCON_PROP_USB_TYPEC_POLARITY, &property);
  50. if (ret) {
  51. dev_err(ta->phy.dev, "no polarity property from extcon\n");
  52. return ret;
  53. }
  54. *flipped = property.intval;
  55. return 0;
  56. }
  57. static int phy_init(struct usb_phy *phy)
  58. {
  59. struct tca_apb *ta = container_of(phy, struct tca_apb, phy);
  60. void __iomem *ctrl1 = phy->io_priv + CTRL1_OFFSET;
  61. int val, ret, i;
  62. if (ta->phy_initialized)
  63. return 0;
  64. for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++)
  65. reset_control_deassert(ta->resets[i]);
  66. ret = readl_poll_timeout(ctrl1, val, val & SRAM_INIT_DONE, 10, 10 * 1000);
  67. if (ret) {
  68. dev_err(ta->phy.dev, "SRAM init failed, 0x%x\n", val);
  69. return ret;
  70. }
  71. writel(readl(ctrl1) | SRAM_EXT_LD_DONE, ctrl1);
  72. ta->phy_initialized = true;
  73. if (!ta->phy.edev) {
  74. writel(TCPC_CONN, ta->phy.io_priv + TCPC_OFFSET);
  75. return phy->set_vbus(phy, true);
  76. }
  77. schedule_work(&ta->wk);
  78. return ret;
  79. }
  80. static void phy_shutdown(struct usb_phy *phy)
  81. {
  82. struct tca_apb *ta = container_of(phy, struct tca_apb, phy);
  83. int i;
  84. if (!ta->phy_initialized)
  85. return;
  86. ta->phy_initialized = false;
  87. flush_work(&ta->wk);
  88. ta->phy.set_vbus(&ta->phy, false);
  89. ta->connected = false;
  90. writel(TCPC_DISCONN, ta->phy.io_priv + TCPC_OFFSET);
  91. for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++)
  92. reset_control_assert(ta->resets[i]);
  93. }
  94. static int phy_set_vbus(struct usb_phy *phy, int on)
  95. {
  96. struct tca_apb *ta = container_of(phy, struct tca_apb, phy);
  97. int ret;
  98. if (!!on == ta->regulator_enabled)
  99. return 0;
  100. if (on)
  101. ret = regulator_enable(ta->vbus);
  102. else
  103. ret = regulator_disable(ta->vbus);
  104. if (!ret)
  105. ta->regulator_enabled = on;
  106. dev_dbg(ta->phy.dev, "set vbus: %d\n", on);
  107. return ret;
  108. }
  109. static void tca_work(struct work_struct *work)
  110. {
  111. struct tca_apb *ta = container_of(work, struct tca_apb, wk);
  112. bool connected;
  113. bool flipped = false;
  114. u32 val;
  115. int ret;
  116. ret = get_flipped(ta, &flipped);
  117. if (ret)
  118. return;
  119. connected = extcon_get_state(ta->phy.edev, EXTCON_USB_HOST);
  120. if (connected == ta->connected)
  121. return;
  122. ta->connected = connected;
  123. if (connected) {
  124. val = TCPC_CONN;
  125. if (flipped)
  126. val |= TCPC_FLIPPED;
  127. dev_dbg(ta->phy.dev, "connected%s\n", flipped ? " flipped" : "");
  128. } else {
  129. val = TCPC_DISCONN;
  130. dev_dbg(ta->phy.dev, "disconnected\n");
  131. }
  132. writel(val, ta->phy.io_priv + TCPC_OFFSET);
  133. ret = ta->phy.set_vbus(&ta->phy, connected);
  134. if (ret)
  135. dev_err(ta->phy.dev, "failed to set VBUS\n");
  136. }
  137. static int id_notifier(struct notifier_block *nb, unsigned long event, void *ptr)
  138. {
  139. struct tca_apb *ta = container_of(nb, struct tca_apb, phy.id_nb);
  140. if (ta->phy_initialized)
  141. schedule_work(&ta->wk);
  142. return NOTIFY_DONE;
  143. }
  144. static int vbus_notifier(struct notifier_block *nb, unsigned long evnt, void *ptr)
  145. {
  146. return NOTIFY_DONE;
  147. }
  148. static int phy_probe(struct platform_device *pdev)
  149. {
  150. struct reset_control *resets[ARRAY_SIZE(CTL_RESETS)];
  151. struct device *dev = &pdev->dev;
  152. struct usb_phy *phy;
  153. struct tca_apb *ta;
  154. int i;
  155. ta = devm_kzalloc(dev, sizeof(*ta), GFP_KERNEL);
  156. if (!ta)
  157. return -ENOMEM;
  158. platform_set_drvdata(pdev, ta);
  159. INIT_WORK(&ta->wk, tca_work);
  160. phy = &ta->phy;
  161. phy->dev = dev;
  162. phy->label = dev_name(dev);
  163. phy->type = USB_PHY_TYPE_USB3;
  164. phy->init = phy_init;
  165. phy->shutdown = phy_shutdown;
  166. phy->set_vbus = phy_set_vbus;
  167. phy->id_nb.notifier_call = id_notifier;
  168. phy->vbus_nb.notifier_call = vbus_notifier;
  169. phy->io_priv = devm_platform_ioremap_resource(pdev, 0);
  170. if (IS_ERR(phy->io_priv))
  171. return PTR_ERR(phy->io_priv);
  172. ta->vbus = devm_regulator_get(dev, "vbus");
  173. if (IS_ERR(ta->vbus))
  174. return PTR_ERR(ta->vbus);
  175. for (i = 0; i < ARRAY_SIZE(CTL_RESETS); i++) {
  176. resets[i] = devm_reset_control_get_exclusive(dev, CTL_RESETS[i]);
  177. if (IS_ERR(resets[i])) {
  178. dev_err(dev, "%s reset not found\n", CTL_RESETS[i]);
  179. return PTR_ERR(resets[i]);
  180. }
  181. }
  182. for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++) {
  183. ta->resets[i] = devm_reset_control_get_exclusive(dev, PHY_RESETS[i]);
  184. if (IS_ERR(ta->resets[i])) {
  185. dev_err(dev, "%s reset not found\n", PHY_RESETS[i]);
  186. return PTR_ERR(ta->resets[i]);
  187. }
  188. }
  189. for (i = 0; i < ARRAY_SIZE(CTL_RESETS); i++)
  190. reset_control_assert(resets[i]);
  191. for (i = 0; i < ARRAY_SIZE(PHY_RESETS); i++)
  192. reset_control_assert(ta->resets[i]);
  193. /*
  194. * Out-of-band reset of the controller after PHY reset will cause
  195. * controller malfunctioning, so we should use in-band controller
  196. * reset only and leave the controller de-asserted here.
  197. */
  198. for (i = 0; i < ARRAY_SIZE(CTL_RESETS); i++)
  199. reset_control_deassert(resets[i]);
  200. /* Need to wait at least 20us after de-assert the controller */
  201. usleep_range(20, 100);
  202. return usb_add_phy_dev(phy);
  203. }
  204. static int phy_remove(struct platform_device *pdev)
  205. {
  206. struct tca_apb *ta = platform_get_drvdata(pdev);
  207. usb_remove_phy(&ta->phy);
  208. return 0;
  209. }
  210. static const struct of_device_id intel_usb_phy_dt_ids[] = {
  211. { .compatible = "intel,lgm-usb-phy" },
  212. { }
  213. };
  214. MODULE_DEVICE_TABLE(of, intel_usb_phy_dt_ids);
  215. static struct platform_driver lgm_phy_driver = {
  216. .driver = {
  217. .name = "lgm-usb-phy",
  218. .of_match_table = intel_usb_phy_dt_ids,
  219. },
  220. .probe = phy_probe,
  221. .remove = phy_remove,
  222. };
  223. module_platform_driver(lgm_phy_driver);
  224. MODULE_DESCRIPTION("Intel LGM USB PHY driver");
  225. MODULE_AUTHOR("Li Yin <yin1.li@intel.com>");
  226. MODULE_AUTHOR("Vadivel Murugan R <vadivel.muruganx.ramuthevar@linux.intel.com>");
  227. MODULE_LICENSE("GPL v2");