phy-snps-eusb2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2023, Linaro Limited
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regulator/consumer.h>
  13. #include <linux/reset.h>
  14. #define EXYNOS_USB_PHY_HS_PHY_CTRL_RST (0x0)
  15. #define USB_PHY_RST_MASK GENMASK(1, 0)
  16. #define UTMI_PORT_RST_MASK GENMASK(5, 4)
  17. #define EXYNOS_USB_PHY_HS_PHY_CTRL_COMMON (0x4)
  18. #define RPTR_MODE BIT(10)
  19. #define FSEL_20_MHZ_VAL (0x1)
  20. #define FSEL_24_MHZ_VAL (0x2)
  21. #define FSEL_26_MHZ_VAL (0x3)
  22. #define FSEL_48_MHZ_VAL (0x2)
  23. #define EXYNOS_USB_PHY_CFG_PLLCFG0 (0x8)
  24. #define PHY_CFG_PLL_FB_DIV_19_8_MASK GENMASK(19, 8)
  25. #define DIV_19_8_19_2_MHZ_VAL (0x170)
  26. #define DIV_19_8_20_MHZ_VAL (0x160)
  27. #define DIV_19_8_24_MHZ_VAL (0x120)
  28. #define DIV_19_8_26_MHZ_VAL (0x107)
  29. #define DIV_19_8_48_MHZ_VAL (0x120)
  30. #define EXYNOS_USB_PHY_CFG_PLLCFG1 (0xc)
  31. #define EXYNOS_PHY_CFG_PLL_FB_DIV_11_8_MASK GENMASK(11, 8)
  32. #define EXYNOS_DIV_11_8_19_2_MHZ_VAL (0x0)
  33. #define EXYNOS_DIV_11_8_20_MHZ_VAL (0x0)
  34. #define EXYNOS_DIV_11_8_24_MHZ_VAL (0x0)
  35. #define EXYNOS_DIV_11_8_26_MHZ_VAL (0x0)
  36. #define EXYNOS_DIV_11_8_48_MHZ_VAL (0x1)
  37. #define EXYNOS_PHY_CFG_TX (0x14)
  38. #define EXYNOS_PHY_CFG_TX_FSLS_VREF_TUNE_MASK GENMASK(2, 1)
  39. #define EXYNOS_USB_PHY_UTMI_TESTSE (0x20)
  40. #define TEST_IDDQ BIT(6)
  41. #define QCOM_USB_PHY_UTMI_CTRL0 (0x3c)
  42. #define SLEEPM BIT(0)
  43. #define OPMODE_MASK GENMASK(4, 3)
  44. #define OPMODE_NONDRIVING BIT(3)
  45. #define QCOM_USB_PHY_UTMI_CTRL5 (0x50)
  46. #define POR BIT(1)
  47. #define QCOM_USB_PHY_HS_PHY_CTRL_COMMON0 (0x54)
  48. #define PHY_ENABLE BIT(0)
  49. #define SIDDQ_SEL BIT(1)
  50. #define SIDDQ BIT(2)
  51. #define RETENABLEN BIT(3)
  52. #define FSEL_MASK GENMASK(6, 4)
  53. #define FSEL_19_2_MHZ_VAL (0x0)
  54. #define FSEL_38_4_MHZ_VAL (0x4)
  55. #define QCOM_USB_PHY_CFG_CTRL_1 (0x58)
  56. #define PHY_CFG_PLL_CPBIAS_CNTRL_MASK GENMASK(7, 1)
  57. #define QCOM_USB_PHY_CFG_CTRL_2 (0x5c)
  58. #define PHY_CFG_PLL_FB_DIV_7_0_MASK GENMASK(7, 0)
  59. #define DIV_7_0_19_2_MHZ_VAL (0x90)
  60. #define DIV_7_0_38_4_MHZ_VAL (0xc8)
  61. #define QCOM_USB_PHY_CFG_CTRL_3 (0x60)
  62. #define PHY_CFG_PLL_FB_DIV_11_8_MASK GENMASK(3, 0)
  63. #define DIV_11_8_19_2_MHZ_VAL (0x1)
  64. #define DIV_11_8_38_4_MHZ_VAL (0x0)
  65. #define PHY_CFG_PLL_REF_DIV GENMASK(7, 4)
  66. #define PLL_REF_DIV_VAL (0x0)
  67. #define QCOM_USB_PHY_HS_PHY_CTRL2 (0x64)
  68. #define VBUSVLDEXT0 BIT(0)
  69. #define USB2_SUSPEND_N BIT(2)
  70. #define USB2_SUSPEND_N_SEL BIT(3)
  71. #define VBUS_DET_EXT_SEL BIT(4)
  72. #define QCOM_USB_PHY_CFG_CTRL_4 (0x68)
  73. #define PHY_CFG_PLL_GMP_CNTRL_MASK GENMASK(1, 0)
  74. #define PHY_CFG_PLL_INT_CNTRL_MASK GENMASK(7, 2)
  75. #define QCOM_USB_PHY_CFG_CTRL_5 (0x6c)
  76. #define PHY_CFG_PLL_PROP_CNTRL_MASK GENMASK(4, 0)
  77. #define PHY_CFG_PLL_VREF_TUNE_MASK GENMASK(7, 6)
  78. #define QCOM_USB_PHY_CFG_CTRL_6 (0x70)
  79. #define PHY_CFG_PLL_VCO_CNTRL_MASK GENMASK(2, 0)
  80. #define QCOM_USB_PHY_CFG_CTRL_7 (0x74)
  81. #define QCOM_USB_PHY_CFG_CTRL_8 (0x78)
  82. #define PHY_CFG_TX_FSLS_VREF_TUNE_MASK GENMASK(1, 0)
  83. #define PHY_CFG_TX_FSLS_VREG_BYPASS BIT(2)
  84. #define PHY_CFG_TX_HS_VREF_TUNE_MASK GENMASK(5, 3)
  85. #define PHY_CFG_TX_HS_XV_TUNE_MASK GENMASK(7, 6)
  86. #define QCOM_USB_PHY_CFG_CTRL_9 (0x7c)
  87. #define PHY_CFG_TX_PREEMP_TUNE_MASK GENMASK(2, 0)
  88. #define PHY_CFG_TX_RES_TUNE_MASK GENMASK(4, 3)
  89. #define PHY_CFG_TX_RISE_TUNE_MASK GENMASK(6, 5)
  90. #define PHY_CFG_RCAL_BYPASS BIT(7)
  91. #define QCOM_USB_PHY_CFG_CTRL_10 (0x80)
  92. #define QCOM_USB_PHY_CFG0 (0x94)
  93. #define DATAPATH_CTRL_OVERRIDE_EN BIT(0)
  94. #define CMN_CTRL_OVERRIDE_EN BIT(1)
  95. #define QCOM_UTMI_PHY_CMN_CTRL0 (0x98)
  96. #define TESTBURNIN BIT(6)
  97. #define QCOM_USB_PHY_FSEL_SEL (0xb8)
  98. #define FSEL_SEL BIT(0)
  99. #define QCOM_USB_PHY_APB_ACCESS_CMD (0x130)
  100. #define RW_ACCESS BIT(0)
  101. #define APB_START_CMD BIT(1)
  102. #define APB_LOGIC_RESET BIT(2)
  103. #define QCOM_USB_PHY_APB_ACCESS_STATUS (0x134)
  104. #define ACCESS_DONE BIT(0)
  105. #define TIMED_OUT BIT(1)
  106. #define ACCESS_ERROR BIT(2)
  107. #define ACCESS_IN_PROGRESS BIT(3)
  108. #define QCOM_USB_PHY_APB_ADDRESS (0x138)
  109. #define APB_REG_ADDR_MASK GENMASK(7, 0)
  110. #define QCOM_USB_PHY_APB_WRDATA_LSB (0x13c)
  111. #define APB_REG_WRDATA_7_0_MASK GENMASK(3, 0)
  112. #define QCOM_USB_PHY_APB_WRDATA_MSB (0x140)
  113. #define APB_REG_WRDATA_15_8_MASK GENMASK(7, 4)
  114. #define QCOM_USB_PHY_APB_RDDATA_LSB (0x144)
  115. #define APB_REG_RDDATA_7_0_MASK GENMASK(3, 0)
  116. #define QCOM_USB_PHY_APB_RDDATA_MSB (0x148)
  117. #define APB_REG_RDDATA_15_8_MASK GENMASK(7, 4)
  118. static const char * const eusb2_hsphy_vreg_names[] = {
  119. "vdd", "vdda12",
  120. };
  121. #define EUSB2_NUM_VREGS ARRAY_SIZE(eusb2_hsphy_vreg_names)
  122. struct snps_eusb2_phy_drvdata {
  123. int (*phy_init)(struct phy *p);
  124. const char * const *clk_names;
  125. int num_clks;
  126. };
  127. struct snps_eusb2_hsphy {
  128. struct phy *phy;
  129. void __iomem *base;
  130. struct clk *ref_clk;
  131. struct clk_bulk_data *clks;
  132. struct reset_control *phy_reset;
  133. struct regulator_bulk_data vregs[EUSB2_NUM_VREGS];
  134. enum phy_mode mode;
  135. struct phy *repeater;
  136. const struct snps_eusb2_phy_drvdata *data;
  137. };
  138. static int snps_eusb2_hsphy_set_mode(struct phy *p, enum phy_mode mode, int submode)
  139. {
  140. struct snps_eusb2_hsphy *phy = phy_get_drvdata(p);
  141. phy->mode = mode;
  142. return phy_set_mode_ext(phy->repeater, mode, submode);
  143. }
  144. static void snps_eusb2_hsphy_write_mask(void __iomem *base, u32 offset,
  145. u32 mask, u32 val)
  146. {
  147. u32 reg;
  148. reg = readl_relaxed(base + offset);
  149. reg &= ~mask;
  150. reg |= val & mask;
  151. writel_relaxed(reg, base + offset);
  152. /* Ensure above write is completed */
  153. readl_relaxed(base + offset);
  154. }
  155. static void qcom_eusb2_default_parameters(struct snps_eusb2_hsphy *phy)
  156. {
  157. /* default parameters: tx pre-emphasis */
  158. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_9,
  159. PHY_CFG_TX_PREEMP_TUNE_MASK,
  160. FIELD_PREP(PHY_CFG_TX_PREEMP_TUNE_MASK, 0));
  161. /* tx rise/fall time */
  162. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_9,
  163. PHY_CFG_TX_RISE_TUNE_MASK,
  164. FIELD_PREP(PHY_CFG_TX_RISE_TUNE_MASK, 0x2));
  165. /* source impedance adjustment */
  166. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_9,
  167. PHY_CFG_TX_RES_TUNE_MASK,
  168. FIELD_PREP(PHY_CFG_TX_RES_TUNE_MASK, 0x1));
  169. /* dc voltage level adjustement */
  170. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_8,
  171. PHY_CFG_TX_HS_VREF_TUNE_MASK,
  172. FIELD_PREP(PHY_CFG_TX_HS_VREF_TUNE_MASK, 0x3));
  173. /* transmitter HS crossover adjustement */
  174. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_8,
  175. PHY_CFG_TX_HS_XV_TUNE_MASK,
  176. FIELD_PREP(PHY_CFG_TX_HS_XV_TUNE_MASK, 0x0));
  177. }
  178. struct snps_eusb2_ref_clk {
  179. unsigned long freq;
  180. u32 fsel_val;
  181. u32 div_7_0_val;
  182. u32 div_11_8_val;
  183. };
  184. static const struct snps_eusb2_ref_clk exynos_eusb2_ref_clk[] = {
  185. { 19200000, FSEL_19_2_MHZ_VAL, DIV_19_8_19_2_MHZ_VAL, EXYNOS_DIV_11_8_19_2_MHZ_VAL },
  186. { 20000000, FSEL_20_MHZ_VAL, DIV_19_8_20_MHZ_VAL, EXYNOS_DIV_11_8_20_MHZ_VAL },
  187. { 24000000, FSEL_24_MHZ_VAL, DIV_19_8_24_MHZ_VAL, EXYNOS_DIV_11_8_24_MHZ_VAL },
  188. { 26000000, FSEL_26_MHZ_VAL, DIV_19_8_26_MHZ_VAL, EXYNOS_DIV_11_8_26_MHZ_VAL },
  189. { 48000000, FSEL_48_MHZ_VAL, DIV_19_8_48_MHZ_VAL, EXYNOS_DIV_11_8_48_MHZ_VAL },
  190. };
  191. static int exynos_eusb2_ref_clk_init(struct snps_eusb2_hsphy *phy)
  192. {
  193. const struct snps_eusb2_ref_clk *config = NULL;
  194. unsigned long ref_clk_freq = clk_get_rate(phy->ref_clk);
  195. for (int i = 0; i < ARRAY_SIZE(exynos_eusb2_ref_clk); i++) {
  196. if (exynos_eusb2_ref_clk[i].freq == ref_clk_freq) {
  197. config = &exynos_eusb2_ref_clk[i];
  198. break;
  199. }
  200. }
  201. if (!config) {
  202. dev_err(&phy->phy->dev, "unsupported ref_clk_freq: %lu\n", ref_clk_freq);
  203. return -EINVAL;
  204. }
  205. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_HS_PHY_CTRL_COMMON,
  206. FSEL_MASK,
  207. FIELD_PREP(FSEL_MASK, config->fsel_val));
  208. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_CFG_PLLCFG0,
  209. PHY_CFG_PLL_FB_DIV_19_8_MASK,
  210. FIELD_PREP(PHY_CFG_PLL_FB_DIV_19_8_MASK,
  211. config->div_7_0_val));
  212. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_CFG_PLLCFG1,
  213. EXYNOS_PHY_CFG_PLL_FB_DIV_11_8_MASK,
  214. config->div_11_8_val);
  215. return 0;
  216. }
  217. static const struct snps_eusb2_ref_clk qcom_eusb2_ref_clk[] = {
  218. { 19200000, FSEL_19_2_MHZ_VAL, DIV_7_0_19_2_MHZ_VAL, DIV_11_8_19_2_MHZ_VAL },
  219. { 38400000, FSEL_38_4_MHZ_VAL, DIV_7_0_38_4_MHZ_VAL, DIV_11_8_38_4_MHZ_VAL },
  220. };
  221. static int qcom_eusb2_ref_clk_init(struct snps_eusb2_hsphy *phy)
  222. {
  223. const struct snps_eusb2_ref_clk *config = NULL;
  224. unsigned long ref_clk_freq = clk_get_rate(phy->ref_clk);
  225. for (int i = 0; i < ARRAY_SIZE(qcom_eusb2_ref_clk); i++) {
  226. if (qcom_eusb2_ref_clk[i].freq == ref_clk_freq) {
  227. config = &qcom_eusb2_ref_clk[i];
  228. break;
  229. }
  230. }
  231. if (!config) {
  232. dev_err(&phy->phy->dev, "unsupported ref_clk_freq: %lu\n", ref_clk_freq);
  233. return -EINVAL;
  234. }
  235. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL_COMMON0,
  236. FSEL_MASK,
  237. FIELD_PREP(FSEL_MASK, config->fsel_val));
  238. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_2,
  239. PHY_CFG_PLL_FB_DIV_7_0_MASK,
  240. config->div_7_0_val);
  241. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_3,
  242. PHY_CFG_PLL_FB_DIV_11_8_MASK,
  243. config->div_11_8_val);
  244. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_3,
  245. PHY_CFG_PLL_REF_DIV, PLL_REF_DIV_VAL);
  246. return 0;
  247. }
  248. static int exynos_snps_eusb2_hsphy_init(struct phy *p)
  249. {
  250. struct snps_eusb2_hsphy *phy = phy_get_drvdata(p);
  251. int ret;
  252. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_HS_PHY_CTRL_RST,
  253. USB_PHY_RST_MASK | UTMI_PORT_RST_MASK,
  254. USB_PHY_RST_MASK | UTMI_PORT_RST_MASK);
  255. fsleep(50); /* required after holding phy in reset */
  256. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_HS_PHY_CTRL_COMMON,
  257. RPTR_MODE, RPTR_MODE);
  258. /* update ref_clk related registers */
  259. ret = exynos_eusb2_ref_clk_init(phy);
  260. if (ret)
  261. return ret;
  262. /* default parameter: tx fsls-vref */
  263. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_PHY_CFG_TX,
  264. EXYNOS_PHY_CFG_TX_FSLS_VREF_TUNE_MASK,
  265. FIELD_PREP(EXYNOS_PHY_CFG_TX_FSLS_VREF_TUNE_MASK, 0x0));
  266. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_UTMI_TESTSE,
  267. TEST_IDDQ, 0);
  268. fsleep(10); /* required after releasing test_iddq */
  269. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_HS_PHY_CTRL_RST,
  270. USB_PHY_RST_MASK, 0);
  271. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_HS_PHY_CTRL_COMMON,
  272. PHY_ENABLE, PHY_ENABLE);
  273. snps_eusb2_hsphy_write_mask(phy->base, EXYNOS_USB_PHY_HS_PHY_CTRL_RST,
  274. UTMI_PORT_RST_MASK, 0);
  275. return 0;
  276. }
  277. static const char * const exynos_eusb2_hsphy_clock_names[] = {
  278. "ref", "bus", "ctrl",
  279. };
  280. static const struct snps_eusb2_phy_drvdata exynos2200_snps_eusb2_phy = {
  281. .phy_init = exynos_snps_eusb2_hsphy_init,
  282. .clk_names = exynos_eusb2_hsphy_clock_names,
  283. .num_clks = ARRAY_SIZE(exynos_eusb2_hsphy_clock_names),
  284. };
  285. static int qcom_snps_eusb2_hsphy_init(struct phy *p)
  286. {
  287. struct snps_eusb2_hsphy *phy = phy_get_drvdata(p);
  288. int ret;
  289. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG0,
  290. CMN_CTRL_OVERRIDE_EN, CMN_CTRL_OVERRIDE_EN);
  291. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_UTMI_CTRL5, POR, POR);
  292. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL_COMMON0,
  293. PHY_ENABLE | RETENABLEN, PHY_ENABLE | RETENABLEN);
  294. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_APB_ACCESS_CMD,
  295. APB_LOGIC_RESET, APB_LOGIC_RESET);
  296. snps_eusb2_hsphy_write_mask(phy->base, QCOM_UTMI_PHY_CMN_CTRL0, TESTBURNIN, 0);
  297. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_FSEL_SEL,
  298. FSEL_SEL, FSEL_SEL);
  299. /* update ref_clk related registers */
  300. ret = qcom_eusb2_ref_clk_init(phy);
  301. if (ret)
  302. return ret;
  303. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_1,
  304. PHY_CFG_PLL_CPBIAS_CNTRL_MASK,
  305. FIELD_PREP(PHY_CFG_PLL_CPBIAS_CNTRL_MASK, 0x0));
  306. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_4,
  307. PHY_CFG_PLL_INT_CNTRL_MASK,
  308. FIELD_PREP(PHY_CFG_PLL_INT_CNTRL_MASK, 0x8));
  309. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_4,
  310. PHY_CFG_PLL_GMP_CNTRL_MASK,
  311. FIELD_PREP(PHY_CFG_PLL_GMP_CNTRL_MASK, 0x1));
  312. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_5,
  313. PHY_CFG_PLL_PROP_CNTRL_MASK,
  314. FIELD_PREP(PHY_CFG_PLL_PROP_CNTRL_MASK, 0x10));
  315. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_6,
  316. PHY_CFG_PLL_VCO_CNTRL_MASK,
  317. FIELD_PREP(PHY_CFG_PLL_VCO_CNTRL_MASK, 0x0));
  318. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG_CTRL_5,
  319. PHY_CFG_PLL_VREF_TUNE_MASK,
  320. FIELD_PREP(PHY_CFG_PLL_VREF_TUNE_MASK, 0x1));
  321. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL2,
  322. VBUS_DET_EXT_SEL, VBUS_DET_EXT_SEL);
  323. /* set default parameters */
  324. qcom_eusb2_default_parameters(phy);
  325. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL2,
  326. USB2_SUSPEND_N_SEL | USB2_SUSPEND_N,
  327. USB2_SUSPEND_N_SEL | USB2_SUSPEND_N);
  328. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_UTMI_CTRL0, SLEEPM, SLEEPM);
  329. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL_COMMON0,
  330. SIDDQ_SEL, SIDDQ_SEL);
  331. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL_COMMON0,
  332. SIDDQ, 0);
  333. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_UTMI_CTRL5, POR, 0);
  334. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_HS_PHY_CTRL2,
  335. USB2_SUSPEND_N_SEL, 0);
  336. snps_eusb2_hsphy_write_mask(phy->base, QCOM_USB_PHY_CFG0,
  337. CMN_CTRL_OVERRIDE_EN, 0);
  338. return 0;
  339. }
  340. static const char * const qcom_eusb2_hsphy_clock_names[] = {
  341. "ref",
  342. };
  343. static const struct snps_eusb2_phy_drvdata sm8550_snps_eusb2_phy = {
  344. .phy_init = qcom_snps_eusb2_hsphy_init,
  345. .clk_names = qcom_eusb2_hsphy_clock_names,
  346. .num_clks = ARRAY_SIZE(qcom_eusb2_hsphy_clock_names),
  347. };
  348. static int snps_eusb2_hsphy_init(struct phy *p)
  349. {
  350. struct snps_eusb2_hsphy *phy = phy_get_drvdata(p);
  351. int ret;
  352. ret = regulator_bulk_enable(ARRAY_SIZE(phy->vregs), phy->vregs);
  353. if (ret)
  354. return ret;
  355. ret = phy_init(phy->repeater);
  356. if (ret) {
  357. dev_err(&p->dev, "repeater init failed: %d\n", ret);
  358. goto disable_vreg;
  359. }
  360. ret = clk_bulk_prepare_enable(phy->data->num_clks, phy->clks);
  361. if (ret) {
  362. dev_err(&p->dev, "failed to enable ref clock: %d\n", ret);
  363. goto exit_repeater;
  364. }
  365. ret = reset_control_assert(phy->phy_reset);
  366. if (ret) {
  367. dev_err(&p->dev, "failed to assert phy_reset: %d\n", ret);
  368. goto disable_clks;
  369. }
  370. usleep_range(100, 150);
  371. ret = reset_control_deassert(phy->phy_reset);
  372. if (ret) {
  373. dev_err(&p->dev, "failed to de-assert phy_reset: %d\n", ret);
  374. goto disable_clks;
  375. }
  376. ret = phy->data->phy_init(p);
  377. if (ret)
  378. goto disable_clks;
  379. return 0;
  380. disable_clks:
  381. clk_bulk_disable_unprepare(phy->data->num_clks, phy->clks);
  382. exit_repeater:
  383. phy_exit(phy->repeater);
  384. disable_vreg:
  385. regulator_bulk_disable(ARRAY_SIZE(phy->vregs), phy->vregs);
  386. return ret;
  387. }
  388. static int snps_eusb2_hsphy_exit(struct phy *p)
  389. {
  390. struct snps_eusb2_hsphy *phy = phy_get_drvdata(p);
  391. clk_bulk_disable_unprepare(phy->data->num_clks, phy->clks);
  392. regulator_bulk_disable(ARRAY_SIZE(phy->vregs), phy->vregs);
  393. phy_exit(phy->repeater);
  394. return 0;
  395. }
  396. static const struct phy_ops snps_eusb2_hsphy_ops = {
  397. .init = snps_eusb2_hsphy_init,
  398. .exit = snps_eusb2_hsphy_exit,
  399. .set_mode = snps_eusb2_hsphy_set_mode,
  400. .owner = THIS_MODULE,
  401. };
  402. static int snps_eusb2_hsphy_probe(struct platform_device *pdev)
  403. {
  404. struct device *dev = &pdev->dev;
  405. struct device_node *np = dev->of_node;
  406. struct snps_eusb2_hsphy *phy;
  407. struct phy_provider *phy_provider;
  408. struct phy *generic_phy;
  409. int ret, i;
  410. int num;
  411. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  412. if (!phy)
  413. return -ENOMEM;
  414. phy->data = device_get_match_data(dev);
  415. if (!phy->data)
  416. return -EINVAL;
  417. phy->base = devm_platform_ioremap_resource(pdev, 0);
  418. if (IS_ERR(phy->base))
  419. return PTR_ERR(phy->base);
  420. phy->phy_reset = devm_reset_control_get_optional_exclusive(dev, NULL);
  421. if (IS_ERR(phy->phy_reset))
  422. return PTR_ERR(phy->phy_reset);
  423. phy->clks = devm_kcalloc(dev, phy->data->num_clks, sizeof(*phy->clks),
  424. GFP_KERNEL);
  425. if (!phy->clks)
  426. return -ENOMEM;
  427. for (i = 0; i < phy->data->num_clks; ++i)
  428. phy->clks[i].id = phy->data->clk_names[i];
  429. ret = devm_clk_bulk_get(dev, phy->data->num_clks, phy->clks);
  430. if (ret)
  431. return dev_err_probe(dev, ret,
  432. "failed to get phy clock(s)\n");
  433. phy->ref_clk = NULL;
  434. for (i = 0; i < phy->data->num_clks; ++i) {
  435. if (!strcmp(phy->clks[i].id, "ref")) {
  436. phy->ref_clk = phy->clks[i].clk;
  437. break;
  438. }
  439. }
  440. if (IS_ERR_OR_NULL(phy->ref_clk)) {
  441. ret = phy->ref_clk ? PTR_ERR(phy->ref_clk) : -ENOENT;
  442. return dev_err_probe(dev, ret,
  443. "failed to get ref clk\n");
  444. }
  445. num = ARRAY_SIZE(phy->vregs);
  446. for (i = 0; i < num; i++)
  447. phy->vregs[i].supply = eusb2_hsphy_vreg_names[i];
  448. ret = devm_regulator_bulk_get(dev, num, phy->vregs);
  449. if (ret)
  450. return dev_err_probe(dev, ret,
  451. "failed to get regulator supplies\n");
  452. phy->repeater = devm_of_phy_optional_get(dev, np, NULL);
  453. if (IS_ERR(phy->repeater))
  454. return dev_err_probe(dev, PTR_ERR(phy->repeater),
  455. "failed to get repeater\n");
  456. generic_phy = devm_phy_create(dev, NULL, &snps_eusb2_hsphy_ops);
  457. if (IS_ERR(generic_phy)) {
  458. dev_err(dev, "failed to create phy: %d\n", ret);
  459. return PTR_ERR(generic_phy);
  460. }
  461. dev_set_drvdata(dev, phy);
  462. phy_set_drvdata(generic_phy, phy);
  463. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  464. if (IS_ERR(phy_provider))
  465. return PTR_ERR(phy_provider);
  466. return 0;
  467. }
  468. static const struct of_device_id snps_eusb2_hsphy_of_match_table[] = {
  469. {
  470. .compatible = "qcom,sm8550-snps-eusb2-phy",
  471. .data = &sm8550_snps_eusb2_phy,
  472. }, {
  473. .compatible = "samsung,exynos2200-eusb2-phy",
  474. .data = &exynos2200_snps_eusb2_phy,
  475. }, {
  476. /* sentinel */
  477. }
  478. };
  479. MODULE_DEVICE_TABLE(of, snps_eusb2_hsphy_of_match_table);
  480. static struct platform_driver snps_eusb2_hsphy_driver = {
  481. .probe = snps_eusb2_hsphy_probe,
  482. .driver = {
  483. .name = "snps-eusb2-hsphy",
  484. .of_match_table = snps_eusb2_hsphy_of_match_table,
  485. },
  486. };
  487. module_platform_driver(snps_eusb2_hsphy_driver);
  488. MODULE_DESCRIPTION("Synopsys eUSB2 HS PHY driver");
  489. MODULE_LICENSE("GPL");