phy-google-usb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-google-usb.c - Google USB PHY driver
  4. *
  5. * Copyright (C) 2025, Google LLC
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/cleanup.h>
  9. #include <linux/clk.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/reset.h>
  20. #include <linux/usb/typec_mux.h>
  21. #define USBCS_USB2PHY_CFG19_OFFSET 0x0
  22. #define USBCS_USB2PHY_CFG19_PHY_CFG_PLL_FB_DIV GENMASK(19, 8)
  23. #define USBCS_USB2PHY_CFG21_OFFSET 0x8
  24. #define USBCS_USB2PHY_CFG21_PHY_ENABLE BIT(12)
  25. #define USBCS_USB2PHY_CFG21_REF_FREQ_SEL GENMASK(15, 13)
  26. #define USBCS_USB2PHY_CFG21_PHY_TX_DIG_BYPASS_SEL BIT(19)
  27. #define USBCS_PHY_CFG1_OFFSET 0x28
  28. #define USBCS_PHY_CFG1_SYS_VBUSVALID BIT(17)
  29. enum google_usb_phy_id {
  30. GOOGLE_USB2_PHY,
  31. GOOGLE_USB_PHY_NUM,
  32. };
  33. struct google_usb_phy_instance {
  34. struct google_usb_phy *parent;
  35. unsigned int index;
  36. struct phy *phy;
  37. unsigned int num_clks;
  38. struct clk_bulk_data *clks;
  39. unsigned int num_rsts;
  40. struct reset_control_bulk_data *rsts;
  41. };
  42. struct google_usb_phy {
  43. struct device *dev;
  44. struct regmap *usb_cfg_regmap;
  45. unsigned int usb2_cfg_offset;
  46. void __iomem *usbdp_top_base;
  47. struct google_usb_phy_instance *insts;
  48. /*
  49. * Protect phy registers from concurrent access, specifically via
  50. * google_usb_set_orientation callback.
  51. */
  52. struct mutex phy_mutex;
  53. struct typec_switch_dev *sw;
  54. enum typec_orientation orientation;
  55. };
  56. static void set_vbus_valid(struct google_usb_phy *gphy)
  57. {
  58. u32 reg;
  59. if (gphy->orientation == TYPEC_ORIENTATION_NONE) {
  60. reg = readl(gphy->usbdp_top_base + USBCS_PHY_CFG1_OFFSET);
  61. reg &= ~USBCS_PHY_CFG1_SYS_VBUSVALID;
  62. writel(reg, gphy->usbdp_top_base + USBCS_PHY_CFG1_OFFSET);
  63. } else {
  64. reg = readl(gphy->usbdp_top_base + USBCS_PHY_CFG1_OFFSET);
  65. reg |= USBCS_PHY_CFG1_SYS_VBUSVALID;
  66. writel(reg, gphy->usbdp_top_base + USBCS_PHY_CFG1_OFFSET);
  67. }
  68. }
  69. static int google_usb_set_orientation(struct typec_switch_dev *sw,
  70. enum typec_orientation orientation)
  71. {
  72. struct google_usb_phy *gphy = typec_switch_get_drvdata(sw);
  73. dev_dbg(gphy->dev, "set orientation %d\n", orientation);
  74. gphy->orientation = orientation;
  75. if (pm_runtime_suspended(gphy->dev))
  76. return 0;
  77. guard(mutex)(&gphy->phy_mutex);
  78. set_vbus_valid(gphy);
  79. return 0;
  80. }
  81. static int google_usb2_phy_init(struct phy *_phy)
  82. {
  83. struct google_usb_phy_instance *inst = phy_get_drvdata(_phy);
  84. struct google_usb_phy *gphy = inst->parent;
  85. u32 reg;
  86. int ret;
  87. dev_dbg(gphy->dev, "initializing usb2 phy\n");
  88. guard(mutex)(&gphy->phy_mutex);
  89. regmap_read(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG21_OFFSET, &reg);
  90. reg &= ~USBCS_USB2PHY_CFG21_PHY_TX_DIG_BYPASS_SEL;
  91. reg &= ~USBCS_USB2PHY_CFG21_REF_FREQ_SEL;
  92. reg |= FIELD_PREP(USBCS_USB2PHY_CFG21_REF_FREQ_SEL, 0);
  93. regmap_write(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG21_OFFSET, reg);
  94. regmap_read(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG19_OFFSET, &reg);
  95. reg &= ~USBCS_USB2PHY_CFG19_PHY_CFG_PLL_FB_DIV;
  96. reg |= FIELD_PREP(USBCS_USB2PHY_CFG19_PHY_CFG_PLL_FB_DIV, 368);
  97. regmap_write(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG19_OFFSET, reg);
  98. set_vbus_valid(gphy);
  99. ret = clk_bulk_prepare_enable(inst->num_clks, inst->clks);
  100. if (ret)
  101. return ret;
  102. ret = reset_control_bulk_deassert(inst->num_rsts, inst->rsts);
  103. if (ret) {
  104. clk_bulk_disable_unprepare(inst->num_clks, inst->clks);
  105. return ret;
  106. }
  107. regmap_read(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG21_OFFSET, &reg);
  108. reg |= USBCS_USB2PHY_CFG21_PHY_ENABLE;
  109. regmap_write(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG21_OFFSET, reg);
  110. return 0;
  111. }
  112. static int google_usb2_phy_exit(struct phy *_phy)
  113. {
  114. struct google_usb_phy_instance *inst = phy_get_drvdata(_phy);
  115. struct google_usb_phy *gphy = inst->parent;
  116. u32 reg;
  117. dev_dbg(gphy->dev, "exiting usb2 phy\n");
  118. guard(mutex)(&gphy->phy_mutex);
  119. regmap_read(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG21_OFFSET, &reg);
  120. reg &= ~USBCS_USB2PHY_CFG21_PHY_ENABLE;
  121. regmap_write(gphy->usb_cfg_regmap, gphy->usb2_cfg_offset + USBCS_USB2PHY_CFG21_OFFSET, reg);
  122. reset_control_bulk_assert(inst->num_rsts, inst->rsts);
  123. clk_bulk_disable_unprepare(inst->num_clks, inst->clks);
  124. return 0;
  125. }
  126. static const struct phy_ops google_usb2_phy_ops = {
  127. .init = google_usb2_phy_init,
  128. .exit = google_usb2_phy_exit,
  129. };
  130. static struct phy *google_usb_phy_xlate(struct device *dev,
  131. const struct of_phandle_args *args)
  132. {
  133. struct google_usb_phy *gphy = dev_get_drvdata(dev);
  134. if (args->args[0] >= GOOGLE_USB_PHY_NUM) {
  135. dev_err(dev, "invalid PHY index requested from DT\n");
  136. return ERR_PTR(-ENODEV);
  137. }
  138. return gphy->insts[args->args[0]].phy;
  139. }
  140. static int google_usb_phy_probe(struct platform_device *pdev)
  141. {
  142. struct typec_switch_desc sw_desc = { };
  143. struct google_usb_phy_instance *inst;
  144. struct phy_provider *phy_provider;
  145. struct device *dev = &pdev->dev;
  146. struct google_usb_phy *gphy;
  147. struct phy *phy;
  148. u32 args[1];
  149. int ret;
  150. gphy = devm_kzalloc(dev, sizeof(*gphy), GFP_KERNEL);
  151. if (!gphy)
  152. return -ENOMEM;
  153. dev_set_drvdata(dev, gphy);
  154. gphy->dev = dev;
  155. ret = devm_mutex_init(dev, &gphy->phy_mutex);
  156. if (ret)
  157. return ret;
  158. gphy->usb_cfg_regmap =
  159. syscon_regmap_lookup_by_phandle_args(dev->of_node,
  160. "google,usb-cfg-csr",
  161. ARRAY_SIZE(args), args);
  162. if (IS_ERR(gphy->usb_cfg_regmap)) {
  163. return dev_err_probe(dev, PTR_ERR(gphy->usb_cfg_regmap),
  164. "invalid usb cfg csr\n");
  165. }
  166. gphy->usb2_cfg_offset = args[0];
  167. gphy->usbdp_top_base = devm_platform_ioremap_resource_byname(pdev,
  168. "usbdp_top");
  169. if (IS_ERR(gphy->usbdp_top_base))
  170. return dev_err_probe(dev, PTR_ERR(gphy->usbdp_top_base),
  171. "invalid usbdp top\n");
  172. gphy->insts = devm_kcalloc(dev, GOOGLE_USB_PHY_NUM, sizeof(*gphy->insts), GFP_KERNEL);
  173. if (!gphy->insts)
  174. return -ENOMEM;
  175. inst = &gphy->insts[GOOGLE_USB2_PHY];
  176. inst->parent = gphy;
  177. inst->index = GOOGLE_USB2_PHY;
  178. phy = devm_phy_create(dev, NULL, &google_usb2_phy_ops);
  179. if (IS_ERR(phy))
  180. return dev_err_probe(dev, PTR_ERR(phy),
  181. "failed to create usb2 phy instance\n");
  182. inst->phy = phy;
  183. phy_set_drvdata(phy, inst);
  184. inst->num_clks = 2;
  185. inst->clks = devm_kcalloc(dev, inst->num_clks, sizeof(*inst->clks), GFP_KERNEL);
  186. if (!inst->clks)
  187. return -ENOMEM;
  188. inst->clks[0].id = "usb2";
  189. inst->clks[1].id = "usb2_apb";
  190. ret = devm_clk_bulk_get(dev, inst->num_clks, inst->clks);
  191. if (ret)
  192. return dev_err_probe(dev, ret, "failed to get u2 phy clks\n");
  193. inst->num_rsts = 2;
  194. inst->rsts = devm_kcalloc(dev, inst->num_rsts, sizeof(*inst->rsts), GFP_KERNEL);
  195. if (!inst->rsts)
  196. return -ENOMEM;
  197. inst->rsts[0].id = "usb2";
  198. inst->rsts[1].id = "usb2_apb";
  199. ret = devm_reset_control_bulk_get_exclusive(dev, inst->num_rsts, inst->rsts);
  200. if (ret)
  201. return dev_err_probe(dev, ret, "failed to get u2 phy resets\n");
  202. phy_provider = devm_of_phy_provider_register(dev, google_usb_phy_xlate);
  203. if (IS_ERR(phy_provider))
  204. return dev_err_probe(dev, PTR_ERR(phy_provider),
  205. "failed to register phy provider\n");
  206. pm_runtime_enable(dev);
  207. sw_desc.fwnode = dev_fwnode(dev);
  208. sw_desc.drvdata = gphy;
  209. sw_desc.name = fwnode_get_name(dev_fwnode(dev));
  210. sw_desc.set = google_usb_set_orientation;
  211. gphy->sw = typec_switch_register(dev, &sw_desc);
  212. if (IS_ERR(gphy->sw))
  213. return dev_err_probe(dev, PTR_ERR(gphy->sw),
  214. "failed to register typec switch\n");
  215. return 0;
  216. }
  217. static void google_usb_phy_remove(struct platform_device *pdev)
  218. {
  219. struct google_usb_phy *gphy = dev_get_drvdata(&pdev->dev);
  220. typec_switch_unregister(gphy->sw);
  221. pm_runtime_disable(&pdev->dev);
  222. }
  223. static const struct of_device_id google_usb_phy_of_match[] = {
  224. {
  225. .compatible = "google,lga-usb-phy",
  226. },
  227. { }
  228. };
  229. MODULE_DEVICE_TABLE(of, google_usb_phy_of_match);
  230. static struct platform_driver google_usb_phy = {
  231. .probe = google_usb_phy_probe,
  232. .remove = google_usb_phy_remove,
  233. .driver = {
  234. .name = "google-usb-phy",
  235. .of_match_table = google_usb_phy_of_match,
  236. }
  237. };
  238. module_platform_driver(google_usb_phy);
  239. MODULE_LICENSE("GPL");
  240. MODULE_DESCRIPTION("Google USB phy driver");