phy-qcom-m31-eusb2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2024-2025 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset.h>
  16. #include <linux/slab.h>
  17. #include <linux/regulator/consumer.h>
  18. #define USB_PHY_UTMI_CTRL0 (0x3c)
  19. #define SLEEPM BIT(0)
  20. #define USB_PHY_UTMI_CTRL5 (0x50)
  21. #define POR BIT(1)
  22. #define USB_PHY_HS_PHY_CTRL_COMMON0 (0x54)
  23. #define PHY_ENABLE BIT(0)
  24. #define SIDDQ_SEL BIT(1)
  25. #define SIDDQ BIT(2)
  26. #define FSEL GENMASK(6, 4)
  27. #define FSEL_38_4_MHZ_VAL (0x6)
  28. #define USB_PHY_HS_PHY_CTRL2 (0x64)
  29. #define USB2_SUSPEND_N BIT(2)
  30. #define USB2_SUSPEND_N_SEL BIT(3)
  31. #define USB_PHY_CFG0 (0x94)
  32. #define UTMI_PHY_CMN_CTRL_OVERRIDE_EN BIT(1)
  33. #define USB_PHY_CFG1 (0x154)
  34. #define PLL_EN BIT(0)
  35. #define USB_PHY_FSEL_SEL (0xb8)
  36. #define FSEL_SEL BIT(0)
  37. #define USB_PHY_XCFGI_39_32 (0x16c)
  38. #define HSTX_PE GENMASK(3, 2)
  39. #define USB_PHY_XCFGI_71_64 (0x17c)
  40. #define HSTX_SWING GENMASK(3, 0)
  41. #define USB_PHY_XCFGI_31_24 (0x168)
  42. #define HSTX_SLEW GENMASK(2, 0)
  43. #define USB_PHY_XCFGI_7_0 (0x15c)
  44. #define PLL_LOCK_TIME GENMASK(1, 0)
  45. #define M31_EUSB_PHY_INIT_CFG(o, b, v) \
  46. { \
  47. .off = o, \
  48. .mask = b, \
  49. .val = v, \
  50. }
  51. struct m31_phy_tbl_entry {
  52. u32 off;
  53. u32 mask;
  54. u32 val;
  55. };
  56. struct m31_eusb2_priv_data {
  57. const struct m31_phy_tbl_entry *setup_seq;
  58. unsigned int setup_seq_nregs;
  59. const struct m31_phy_tbl_entry *override_seq;
  60. unsigned int override_seq_nregs;
  61. const struct m31_phy_tbl_entry *reset_seq;
  62. unsigned int reset_seq_nregs;
  63. unsigned int fsel;
  64. };
  65. static const struct m31_phy_tbl_entry m31_eusb2_setup_tbl[] = {
  66. M31_EUSB_PHY_INIT_CFG(USB_PHY_CFG0, UTMI_PHY_CMN_CTRL_OVERRIDE_EN, 1),
  67. M31_EUSB_PHY_INIT_CFG(USB_PHY_UTMI_CTRL5, POR, 1),
  68. M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL_COMMON0, PHY_ENABLE, 1),
  69. M31_EUSB_PHY_INIT_CFG(USB_PHY_CFG1, PLL_EN, 1),
  70. M31_EUSB_PHY_INIT_CFG(USB_PHY_FSEL_SEL, FSEL_SEL, 1),
  71. };
  72. static const struct m31_phy_tbl_entry m31_eusb_phy_override_tbl[] = {
  73. M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_39_32, HSTX_PE, 0),
  74. M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_71_64, HSTX_SWING, 7),
  75. M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_31_24, HSTX_SLEW, 0),
  76. M31_EUSB_PHY_INIT_CFG(USB_PHY_XCFGI_7_0, PLL_LOCK_TIME, 0),
  77. };
  78. static const struct m31_phy_tbl_entry m31_eusb_phy_reset_tbl[] = {
  79. M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL2, USB2_SUSPEND_N_SEL, 1),
  80. M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL2, USB2_SUSPEND_N, 1),
  81. M31_EUSB_PHY_INIT_CFG(USB_PHY_UTMI_CTRL0, SLEEPM, 1),
  82. M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL_COMMON0, SIDDQ_SEL, 1),
  83. M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL_COMMON0, SIDDQ, 0),
  84. M31_EUSB_PHY_INIT_CFG(USB_PHY_UTMI_CTRL5, POR, 0),
  85. M31_EUSB_PHY_INIT_CFG(USB_PHY_HS_PHY_CTRL2, USB2_SUSPEND_N_SEL, 0),
  86. M31_EUSB_PHY_INIT_CFG(USB_PHY_CFG0, UTMI_PHY_CMN_CTRL_OVERRIDE_EN, 0),
  87. };
  88. static const struct regulator_bulk_data m31_eusb_phy_vregs[] = {
  89. { .supply = "vdd" },
  90. { .supply = "vdda12" },
  91. };
  92. #define M31_EUSB_NUM_VREGS ARRAY_SIZE(m31_eusb_phy_vregs)
  93. struct m31eusb2_phy {
  94. struct phy *phy;
  95. void __iomem *base;
  96. const struct m31_eusb2_priv_data *data;
  97. enum phy_mode mode;
  98. struct regulator_bulk_data *vregs;
  99. struct clk *clk;
  100. struct reset_control *reset;
  101. struct phy *repeater;
  102. };
  103. static int m31eusb2_phy_write_readback(void __iomem *base, u32 offset,
  104. const u32 mask, u32 val)
  105. {
  106. u32 write_val;
  107. u32 tmp;
  108. tmp = readl(base + offset);
  109. tmp &= ~mask;
  110. write_val = tmp | val;
  111. writel(write_val, base + offset);
  112. tmp = readl(base + offset);
  113. tmp &= mask;
  114. if (tmp != val) {
  115. pr_err("write: %x to offset: %x FAILED\n", val, offset);
  116. return -EINVAL;
  117. }
  118. return 0;
  119. }
  120. static int m31eusb2_phy_write_sequence(struct m31eusb2_phy *phy,
  121. const struct m31_phy_tbl_entry *tbl,
  122. int num)
  123. {
  124. int i;
  125. int ret;
  126. for (i = 0 ; i < num; i++, tbl++) {
  127. dev_dbg(&phy->phy->dev, "Offset:%x BitMask:%x Value:%x",
  128. tbl->off, tbl->mask, tbl->val);
  129. ret = m31eusb2_phy_write_readback(phy->base,
  130. tbl->off, tbl->mask,
  131. tbl->val << __ffs(tbl->mask));
  132. if (ret < 0)
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. static int m31eusb2_phy_set_mode(struct phy *uphy, enum phy_mode mode, int submode)
  138. {
  139. struct m31eusb2_phy *phy = phy_get_drvdata(uphy);
  140. phy->mode = mode;
  141. return phy_set_mode_ext(phy->repeater, mode, submode);
  142. }
  143. static int m31eusb2_phy_init(struct phy *uphy)
  144. {
  145. struct m31eusb2_phy *phy = phy_get_drvdata(uphy);
  146. const struct m31_eusb2_priv_data *data = phy->data;
  147. int ret;
  148. ret = regulator_bulk_enable(M31_EUSB_NUM_VREGS, phy->vregs);
  149. if (ret) {
  150. dev_err(&uphy->dev, "failed to enable regulator, %d\n", ret);
  151. return ret;
  152. }
  153. ret = phy_init(phy->repeater);
  154. if (ret) {
  155. dev_err(&uphy->dev, "repeater init failed. %d\n", ret);
  156. goto disable_vreg;
  157. }
  158. ret = clk_prepare_enable(phy->clk);
  159. if (ret) {
  160. dev_err(&uphy->dev, "failed to enable ref clock, %d\n", ret);
  161. goto disable_repeater;
  162. }
  163. /* Perform phy reset */
  164. reset_control_assert(phy->reset);
  165. udelay(5);
  166. reset_control_deassert(phy->reset);
  167. m31eusb2_phy_write_sequence(phy, data->setup_seq, data->setup_seq_nregs);
  168. m31eusb2_phy_write_readback(phy->base,
  169. USB_PHY_HS_PHY_CTRL_COMMON0, FSEL,
  170. FIELD_PREP(FSEL, data->fsel));
  171. m31eusb2_phy_write_sequence(phy, data->override_seq, data->override_seq_nregs);
  172. m31eusb2_phy_write_sequence(phy, data->reset_seq, data->reset_seq_nregs);
  173. return 0;
  174. disable_repeater:
  175. phy_exit(phy->repeater);
  176. disable_vreg:
  177. regulator_bulk_disable(M31_EUSB_NUM_VREGS, phy->vregs);
  178. return 0;
  179. }
  180. static int m31eusb2_phy_exit(struct phy *uphy)
  181. {
  182. struct m31eusb2_phy *phy = phy_get_drvdata(uphy);
  183. clk_disable_unprepare(phy->clk);
  184. regulator_bulk_disable(M31_EUSB_NUM_VREGS, phy->vregs);
  185. phy_exit(phy->repeater);
  186. return 0;
  187. }
  188. static const struct phy_ops m31eusb2_phy_gen_ops = {
  189. .init = m31eusb2_phy_init,
  190. .exit = m31eusb2_phy_exit,
  191. .set_mode = m31eusb2_phy_set_mode,
  192. .owner = THIS_MODULE,
  193. };
  194. static int m31eusb2_phy_probe(struct platform_device *pdev)
  195. {
  196. struct phy_provider *phy_provider;
  197. const struct m31_eusb2_priv_data *data;
  198. struct device *dev = &pdev->dev;
  199. struct m31eusb2_phy *phy;
  200. int ret;
  201. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  202. if (!phy)
  203. return -ENOMEM;
  204. data = device_get_match_data(dev);
  205. if (!data)
  206. return -EINVAL;
  207. phy->data = data;
  208. phy->base = devm_platform_ioremap_resource(pdev, 0);
  209. if (IS_ERR(phy->base))
  210. return PTR_ERR(phy->base);
  211. phy->reset = devm_reset_control_get_exclusive(dev, NULL);
  212. if (IS_ERR(phy->reset))
  213. return PTR_ERR(phy->reset);
  214. phy->clk = devm_clk_get(dev, NULL);
  215. if (IS_ERR(phy->clk))
  216. return dev_err_probe(dev, PTR_ERR(phy->clk),
  217. "failed to get clk\n");
  218. phy->phy = devm_phy_create(dev, NULL, &m31eusb2_phy_gen_ops);
  219. if (IS_ERR(phy->phy))
  220. return dev_err_probe(dev, PTR_ERR(phy->phy),
  221. "failed to create phy\n");
  222. ret = devm_regulator_bulk_get_const(dev, M31_EUSB_NUM_VREGS,
  223. m31_eusb_phy_vregs, &phy->vregs);
  224. if (ret)
  225. return dev_err_probe(dev, ret,
  226. "failed to get regulator supplies\n");
  227. phy_set_drvdata(phy->phy, phy);
  228. phy->repeater = devm_of_phy_get_by_index(dev, dev->of_node, 0);
  229. if (IS_ERR(phy->repeater))
  230. return dev_err_probe(dev, PTR_ERR(phy->repeater),
  231. "failed to get repeater\n");
  232. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  233. return PTR_ERR_OR_ZERO(phy_provider);
  234. }
  235. static const struct m31_eusb2_priv_data m31_eusb_v1_data = {
  236. .setup_seq = m31_eusb2_setup_tbl,
  237. .setup_seq_nregs = ARRAY_SIZE(m31_eusb2_setup_tbl),
  238. .override_seq = m31_eusb_phy_override_tbl,
  239. .override_seq_nregs = ARRAY_SIZE(m31_eusb_phy_override_tbl),
  240. .reset_seq = m31_eusb_phy_reset_tbl,
  241. .reset_seq_nregs = ARRAY_SIZE(m31_eusb_phy_reset_tbl),
  242. .fsel = FSEL_38_4_MHZ_VAL,
  243. };
  244. static const struct of_device_id m31eusb2_phy_id_table[] = {
  245. { .compatible = "qcom,sm8750-m31-eusb2-phy", .data = &m31_eusb_v1_data },
  246. { },
  247. };
  248. MODULE_DEVICE_TABLE(of, m31eusb2_phy_id_table);
  249. static struct platform_driver m31eusb2_phy_driver = {
  250. .probe = m31eusb2_phy_probe,
  251. .driver = {
  252. .name = "qcom-m31eusb2-phy",
  253. .of_match_table = m31eusb2_phy_id_table,
  254. },
  255. };
  256. module_platform_driver(m31eusb2_phy_driver);
  257. MODULE_AUTHOR("Wesley Cheng <quic_wcheng@quicinc.com>");
  258. MODULE_DESCRIPTION("eUSB2 Qualcomm M31 HSPHY driver");
  259. MODULE_LICENSE("GPL");