phy-qcom-qusb2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/nvmem-consumer.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/reset.h>
  20. #include <linux/slab.h>
  21. #include <dt-bindings/phy/phy-qcom-qusb2.h>
  22. #define QUSB2PHY_PLL_TEST 0x04
  23. #define CLK_REF_SEL BIT(7)
  24. #define QUSB2PHY_PLL_TUNE 0x08
  25. #define QUSB2PHY_PLL_USER_CTL1 0x0c
  26. #define QUSB2PHY_PLL_USER_CTL2 0x10
  27. #define QUSB2PHY_PLL_AUTOPGM_CTL1 0x1c
  28. #define QUSB2PHY_PLL_PWR_CTRL 0x18
  29. /* QUSB2PHY_PLL_STATUS register bits */
  30. #define PLL_LOCKED BIT(5)
  31. /* QUSB2PHY_PLL_COMMON_STATUS_ONE register bits */
  32. #define CORE_READY_STATUS BIT(0)
  33. /* QUSB2PHY_PORT_POWERDOWN register bits */
  34. #define CLAMP_N_EN BIT(5)
  35. #define FREEZIO_N BIT(1)
  36. #define POWER_DOWN BIT(0)
  37. /* QUSB2PHY_PWR_CTRL1 register bits */
  38. #define PWR_CTRL1_VREF_SUPPLY_TRIM BIT(5)
  39. #define PWR_CTRL1_CLAMP_N_EN BIT(1)
  40. #define QUSB2PHY_REFCLK_ENABLE BIT(0)
  41. #define PHY_CLK_SCHEME_SEL BIT(0)
  42. /* QUSB2PHY_INTR_CTRL register bits */
  43. #define DMSE_INTR_HIGH_SEL BIT(4)
  44. #define DPSE_INTR_HIGH_SEL BIT(3)
  45. #define CHG_DET_INTR_EN BIT(2)
  46. #define DMSE_INTR_EN BIT(1)
  47. #define DPSE_INTR_EN BIT(0)
  48. /* QUSB2PHY_PLL_CORE_INPUT_OVERRIDE register bits */
  49. #define CORE_PLL_EN_FROM_RESET BIT(4)
  50. #define CORE_RESET BIT(5)
  51. #define CORE_RESET_MUX BIT(6)
  52. /* QUSB2PHY_IMP_CTRL1 register bits */
  53. #define IMP_RES_OFFSET_MASK GENMASK(5, 0)
  54. #define IMP_RES_OFFSET_SHIFT 0x0
  55. /* QUSB2PHY_PORT_TUNE1 register bits */
  56. #define HSTX_TRIM_MASK GENMASK(7, 4)
  57. #define HSTX_TRIM_SHIFT 0x4
  58. #define PREEMPH_WIDTH_HALF_BIT BIT(2)
  59. #define PREEMPHASIS_EN_MASK GENMASK(1, 0)
  60. #define PREEMPHASIS_EN_SHIFT 0x0
  61. #define QUSB2PHY_PLL_ANALOG_CONTROLS_TWO 0x04
  62. #define QUSB2PHY_PLL_CLOCK_INVERTERS 0x18c
  63. #define QUSB2PHY_PLL_CMODE 0x2c
  64. #define QUSB2PHY_PLL_LOCK_DELAY 0x184
  65. #define QUSB2PHY_PLL_DIGITAL_TIMERS_TWO 0xb4
  66. #define QUSB2PHY_PLL_BIAS_CONTROL_1 0x194
  67. #define QUSB2PHY_PLL_BIAS_CONTROL_2 0x198
  68. #define QUSB2PHY_PWR_CTRL2 0x214
  69. #define QUSB2PHY_IMP_CTRL1 0x220
  70. #define QUSB2PHY_IMP_CTRL2 0x224
  71. #define QUSB2PHY_CHG_CTRL2 0x23c
  72. struct qusb2_phy_init_tbl {
  73. unsigned int offset;
  74. unsigned int val;
  75. /*
  76. * register part of layout ?
  77. * if yes, then offset gives index in the reg-layout
  78. */
  79. int in_layout;
  80. };
  81. #define QUSB2_PHY_INIT_CFG(o, v) \
  82. { \
  83. .offset = o, \
  84. .val = v, \
  85. }
  86. #define QUSB2_PHY_INIT_CFG_L(o, v) \
  87. { \
  88. .offset = o, \
  89. .val = v, \
  90. .in_layout = 1, \
  91. }
  92. /* set of registers with offsets different per-PHY */
  93. enum qusb2phy_reg_layout {
  94. QUSB2PHY_PLL_CORE_INPUT_OVERRIDE,
  95. QUSB2PHY_PLL_STATUS,
  96. QUSB2PHY_PORT_TUNE1,
  97. QUSB2PHY_PORT_TUNE2,
  98. QUSB2PHY_PORT_TUNE3,
  99. QUSB2PHY_PORT_TUNE4,
  100. QUSB2PHY_PORT_TUNE5,
  101. QUSB2PHY_PORT_TEST1,
  102. QUSB2PHY_PORT_TEST2,
  103. QUSB2PHY_PORT_POWERDOWN,
  104. QUSB2PHY_INTR_CTRL,
  105. };
  106. static const unsigned int msm8996_regs_layout[] = {
  107. [QUSB2PHY_PLL_STATUS] = 0x38,
  108. [QUSB2PHY_PORT_TUNE1] = 0x80,
  109. [QUSB2PHY_PORT_TUNE2] = 0x84,
  110. [QUSB2PHY_PORT_TUNE3] = 0x88,
  111. [QUSB2PHY_PORT_TUNE4] = 0x8c,
  112. [QUSB2PHY_PORT_TUNE5] = 0x90,
  113. [QUSB2PHY_PORT_TEST1] = 0xb8,
  114. [QUSB2PHY_PORT_TEST2] = 0x9c,
  115. [QUSB2PHY_PORT_POWERDOWN] = 0xb4,
  116. [QUSB2PHY_INTR_CTRL] = 0xbc,
  117. };
  118. static const struct qusb2_phy_init_tbl msm8996_init_tbl[] = {
  119. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0xf8),
  120. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0xb3),
  121. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0x83),
  122. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0xc0),
  123. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_TUNE, 0x30),
  124. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL1, 0x79),
  125. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL2, 0x21),
  126. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TEST2, 0x14),
  127. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_AUTOPGM_CTL1, 0x9f),
  128. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_PWR_CTRL, 0x00),
  129. };
  130. static const unsigned int sdm845_regs_layout[] = {
  131. [QUSB2PHY_PLL_CORE_INPUT_OVERRIDE] = 0xa8,
  132. [QUSB2PHY_PLL_STATUS] = 0x1a0,
  133. [QUSB2PHY_PORT_TUNE1] = 0x240,
  134. [QUSB2PHY_PORT_TUNE2] = 0x244,
  135. [QUSB2PHY_PORT_TUNE3] = 0x248,
  136. [QUSB2PHY_PORT_TUNE4] = 0x24c,
  137. [QUSB2PHY_PORT_TUNE5] = 0x250,
  138. [QUSB2PHY_PORT_TEST1] = 0x254,
  139. [QUSB2PHY_PORT_TEST2] = 0x258,
  140. [QUSB2PHY_PORT_POWERDOWN] = 0x210,
  141. [QUSB2PHY_INTR_CTRL] = 0x230,
  142. };
  143. static const struct qusb2_phy_init_tbl sdm845_init_tbl[] = {
  144. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_ANALOG_CONTROLS_TWO, 0x03),
  145. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CLOCK_INVERTERS, 0x7c),
  146. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_CMODE, 0x80),
  147. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_LOCK_DELAY, 0x0a),
  148. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_DIGITAL_TIMERS_TWO, 0x19),
  149. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_1, 0x40),
  150. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_BIAS_CONTROL_2, 0x20),
  151. QUSB2_PHY_INIT_CFG(QUSB2PHY_PWR_CTRL2, 0x21),
  152. QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL1, 0x0),
  153. QUSB2_PHY_INIT_CFG(QUSB2PHY_IMP_CTRL2, 0x58),
  154. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE1, 0x30),
  155. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE2, 0x29),
  156. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE3, 0xca),
  157. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE4, 0x04),
  158. QUSB2_PHY_INIT_CFG_L(QUSB2PHY_PORT_TUNE5, 0x03),
  159. QUSB2_PHY_INIT_CFG(QUSB2PHY_CHG_CTRL2, 0x0),
  160. };
  161. struct qusb2_phy_cfg {
  162. const struct qusb2_phy_init_tbl *tbl;
  163. /* number of entries in the table */
  164. unsigned int tbl_num;
  165. /* offset to PHY_CLK_SCHEME register in TCSR map */
  166. unsigned int clk_scheme_offset;
  167. /* array of registers with different offsets */
  168. const unsigned int *regs;
  169. unsigned int mask_core_ready;
  170. unsigned int disable_ctrl;
  171. unsigned int autoresume_en;
  172. /* true if PHY has PLL_TEST register to select clk_scheme */
  173. bool has_pll_test;
  174. /* true if TUNE1 register must be updated by fused value, else TUNE2 */
  175. bool update_tune1_with_efuse;
  176. /* true if PHY has PLL_CORE_INPUT_OVERRIDE register to reset PLL */
  177. bool has_pll_override;
  178. };
  179. static const struct qusb2_phy_cfg msm8996_phy_cfg = {
  180. .tbl = msm8996_init_tbl,
  181. .tbl_num = ARRAY_SIZE(msm8996_init_tbl),
  182. .regs = msm8996_regs_layout,
  183. .has_pll_test = true,
  184. .disable_ctrl = (CLAMP_N_EN | FREEZIO_N | POWER_DOWN),
  185. .mask_core_ready = PLL_LOCKED,
  186. .autoresume_en = BIT(3),
  187. };
  188. static const struct qusb2_phy_cfg sdm845_phy_cfg = {
  189. .tbl = sdm845_init_tbl,
  190. .tbl_num = ARRAY_SIZE(sdm845_init_tbl),
  191. .regs = sdm845_regs_layout,
  192. .disable_ctrl = (PWR_CTRL1_VREF_SUPPLY_TRIM | PWR_CTRL1_CLAMP_N_EN |
  193. POWER_DOWN),
  194. .mask_core_ready = CORE_READY_STATUS,
  195. .has_pll_override = true,
  196. .autoresume_en = BIT(0),
  197. .update_tune1_with_efuse = true,
  198. };
  199. static const char * const qusb2_phy_vreg_names[] = {
  200. "vdda-pll", "vdda-phy-dpdm",
  201. };
  202. #define QUSB2_NUM_VREGS ARRAY_SIZE(qusb2_phy_vreg_names)
  203. /**
  204. * struct qusb2_phy - structure holding qusb2 phy attributes
  205. *
  206. * @phy: generic phy
  207. * @base: iomapped memory space for qubs2 phy
  208. *
  209. * @cfg_ahb_clk: AHB2PHY interface clock
  210. * @ref_clk: phy reference clock
  211. * @iface_clk: phy interface clock
  212. * @phy_reset: phy reset control
  213. * @vregs: regulator supplies bulk data
  214. *
  215. * @tcsr: TCSR syscon register map
  216. * @cell: nvmem cell containing phy tuning value
  217. *
  218. * @override_imp_res_offset: PHY should use different rescode offset
  219. * @imp_res_offset_value: rescode offset to be updated in IMP_CTRL1 register
  220. * @override_hstx_trim: PHY should use different HSTX o/p current value
  221. * @hstx_trim_value: HSTX_TRIM value to be updated in TUNE1 register
  222. * @override_preemphasis: PHY should use different pre-amphasis amplitude
  223. * @preemphasis_level: Amplitude Pre-Emphasis to be updated in TUNE1 register
  224. * @override_preemphasis_width: PHY should use different pre-emphasis duration
  225. * @preemphasis_width: half/full-width Pre-Emphasis updated via TUNE1
  226. *
  227. * @cfg: phy config data
  228. * @has_se_clk_scheme: indicate if PHY has single-ended ref clock scheme
  229. * @phy_initialized: indicate if PHY has been initialized
  230. * @mode: current PHY mode
  231. */
  232. struct qusb2_phy {
  233. struct phy *phy;
  234. void __iomem *base;
  235. struct clk *cfg_ahb_clk;
  236. struct clk *ref_clk;
  237. struct clk *iface_clk;
  238. struct reset_control *phy_reset;
  239. struct regulator_bulk_data vregs[QUSB2_NUM_VREGS];
  240. struct regmap *tcsr;
  241. struct nvmem_cell *cell;
  242. bool override_imp_res_offset;
  243. u8 imp_res_offset_value;
  244. bool override_hstx_trim;
  245. u8 hstx_trim_value;
  246. bool override_preemphasis;
  247. u8 preemphasis_level;
  248. bool override_preemphasis_width;
  249. u8 preemphasis_width;
  250. const struct qusb2_phy_cfg *cfg;
  251. bool has_se_clk_scheme;
  252. bool phy_initialized;
  253. enum phy_mode mode;
  254. };
  255. static inline void qusb2_write_mask(void __iomem *base, u32 offset,
  256. u32 val, u32 mask)
  257. {
  258. u32 reg;
  259. reg = readl(base + offset);
  260. reg &= ~mask;
  261. reg |= val & mask;
  262. writel(reg, base + offset);
  263. /* Ensure above write is completed */
  264. readl(base + offset);
  265. }
  266. static inline void qusb2_setbits(void __iomem *base, u32 offset, u32 val)
  267. {
  268. u32 reg;
  269. reg = readl(base + offset);
  270. reg |= val;
  271. writel(reg, base + offset);
  272. /* Ensure above write is completed */
  273. readl(base + offset);
  274. }
  275. static inline void qusb2_clrbits(void __iomem *base, u32 offset, u32 val)
  276. {
  277. u32 reg;
  278. reg = readl(base + offset);
  279. reg &= ~val;
  280. writel(reg, base + offset);
  281. /* Ensure above write is completed */
  282. readl(base + offset);
  283. }
  284. static inline
  285. void qcom_qusb2_phy_configure(void __iomem *base,
  286. const unsigned int *regs,
  287. const struct qusb2_phy_init_tbl tbl[], int num)
  288. {
  289. int i;
  290. for (i = 0; i < num; i++) {
  291. if (tbl[i].in_layout)
  292. writel(tbl[i].val, base + regs[tbl[i].offset]);
  293. else
  294. writel(tbl[i].val, base + tbl[i].offset);
  295. }
  296. }
  297. /*
  298. * Update board specific PHY tuning override values if specified from
  299. * device tree.
  300. */
  301. static void qusb2_phy_override_phy_params(struct qusb2_phy *qphy)
  302. {
  303. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  304. if (qphy->override_imp_res_offset)
  305. qusb2_write_mask(qphy->base, QUSB2PHY_IMP_CTRL1,
  306. qphy->imp_res_offset_value << IMP_RES_OFFSET_SHIFT,
  307. IMP_RES_OFFSET_MASK);
  308. if (qphy->override_hstx_trim)
  309. qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
  310. qphy->hstx_trim_value << HSTX_TRIM_SHIFT,
  311. HSTX_TRIM_MASK);
  312. if (qphy->override_preemphasis)
  313. qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
  314. qphy->preemphasis_level << PREEMPHASIS_EN_SHIFT,
  315. PREEMPHASIS_EN_MASK);
  316. if (qphy->override_preemphasis_width) {
  317. if (qphy->preemphasis_width ==
  318. QUSB2_V2_PREEMPHASIS_WIDTH_HALF_BIT)
  319. qusb2_setbits(qphy->base,
  320. cfg->regs[QUSB2PHY_PORT_TUNE1],
  321. PREEMPH_WIDTH_HALF_BIT);
  322. else
  323. qusb2_clrbits(qphy->base,
  324. cfg->regs[QUSB2PHY_PORT_TUNE1],
  325. PREEMPH_WIDTH_HALF_BIT);
  326. }
  327. }
  328. /*
  329. * Fetches HS Tx tuning value from nvmem and sets the
  330. * QUSB2PHY_PORT_TUNE1/2 register.
  331. * For error case, skip setting the value and use the default value.
  332. */
  333. static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy)
  334. {
  335. struct device *dev = &qphy->phy->dev;
  336. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  337. u8 *val;
  338. /* efuse register is optional */
  339. if (!qphy->cell)
  340. return;
  341. /*
  342. * Read efuse register having TUNE2/1 parameter's high nibble.
  343. * If efuse register shows value as 0x0 (indicating value is not
  344. * fused), or if we fail to find a valid efuse register setting,
  345. * then use default value for high nibble that we have already
  346. * set while configuring the phy.
  347. */
  348. val = nvmem_cell_read(qphy->cell, NULL);
  349. if (IS_ERR(val) || !val[0]) {
  350. dev_dbg(dev, "failed to read a valid hs-tx trim value\n");
  351. return;
  352. }
  353. /* Fused TUNE1/2 value is the higher nibble only */
  354. if (cfg->update_tune1_with_efuse)
  355. qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
  356. val[0] << HSTX_TRIM_SHIFT,
  357. HSTX_TRIM_MASK);
  358. else
  359. qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE2],
  360. val[0] << HSTX_TRIM_SHIFT,
  361. HSTX_TRIM_MASK);
  362. }
  363. static int qusb2_phy_set_mode(struct phy *phy, enum phy_mode mode)
  364. {
  365. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  366. qphy->mode = mode;
  367. return 0;
  368. }
  369. static int __maybe_unused qusb2_phy_runtime_suspend(struct device *dev)
  370. {
  371. struct qusb2_phy *qphy = dev_get_drvdata(dev);
  372. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  373. u32 intr_mask;
  374. dev_vdbg(dev, "Suspending QUSB2 Phy, mode:%d\n", qphy->mode);
  375. if (!qphy->phy_initialized) {
  376. dev_vdbg(dev, "PHY not initialized, bailing out\n");
  377. return 0;
  378. }
  379. /*
  380. * Enable DP/DM interrupts to detect line state changes based on current
  381. * speed. In other words, enable the triggers _opposite_ of what the
  382. * current D+/D- levels are e.g. if currently D+ high, D- low
  383. * (HS 'J'/Suspend), configure the mask to trigger on D+ low OR D- high
  384. */
  385. intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
  386. switch (qphy->mode) {
  387. case PHY_MODE_USB_HOST_HS:
  388. case PHY_MODE_USB_HOST_FS:
  389. case PHY_MODE_USB_DEVICE_HS:
  390. case PHY_MODE_USB_DEVICE_FS:
  391. intr_mask |= DMSE_INTR_HIGH_SEL;
  392. break;
  393. case PHY_MODE_USB_HOST_LS:
  394. case PHY_MODE_USB_DEVICE_LS:
  395. intr_mask |= DPSE_INTR_HIGH_SEL;
  396. break;
  397. default:
  398. /* No device connected, enable both DP/DM high interrupt */
  399. intr_mask |= DMSE_INTR_HIGH_SEL;
  400. intr_mask |= DPSE_INTR_HIGH_SEL;
  401. break;
  402. }
  403. writel(intr_mask, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
  404. /* hold core PLL into reset */
  405. if (cfg->has_pll_override) {
  406. qusb2_setbits(qphy->base,
  407. cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
  408. CORE_PLL_EN_FROM_RESET | CORE_RESET |
  409. CORE_RESET_MUX);
  410. }
  411. /* enable phy auto-resume only if device is connected on bus */
  412. if (qphy->mode != PHY_MODE_INVALID) {
  413. qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
  414. cfg->autoresume_en);
  415. /* Autoresume bit has to be toggled in order to enable it */
  416. qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
  417. cfg->autoresume_en);
  418. }
  419. if (!qphy->has_se_clk_scheme)
  420. clk_disable_unprepare(qphy->ref_clk);
  421. clk_disable_unprepare(qphy->cfg_ahb_clk);
  422. clk_disable_unprepare(qphy->iface_clk);
  423. return 0;
  424. }
  425. static int __maybe_unused qusb2_phy_runtime_resume(struct device *dev)
  426. {
  427. struct qusb2_phy *qphy = dev_get_drvdata(dev);
  428. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  429. int ret;
  430. dev_vdbg(dev, "Resuming QUSB2 phy, mode:%d\n", qphy->mode);
  431. if (!qphy->phy_initialized) {
  432. dev_vdbg(dev, "PHY not initialized, bailing out\n");
  433. return 0;
  434. }
  435. ret = clk_prepare_enable(qphy->iface_clk);
  436. if (ret) {
  437. dev_err(dev, "failed to enable iface_clk, %d\n", ret);
  438. return ret;
  439. }
  440. ret = clk_prepare_enable(qphy->cfg_ahb_clk);
  441. if (ret) {
  442. dev_err(dev, "failed to enable cfg ahb clock, %d\n", ret);
  443. goto disable_iface_clk;
  444. }
  445. if (!qphy->has_se_clk_scheme) {
  446. ret = clk_prepare_enable(qphy->ref_clk);
  447. if (ret) {
  448. dev_err(dev, "failed to enable ref clk, %d\n", ret);
  449. goto disable_ahb_clk;
  450. }
  451. }
  452. writel(0x0, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
  453. /* bring core PLL out of reset */
  454. if (cfg->has_pll_override) {
  455. qusb2_clrbits(qphy->base,
  456. cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
  457. CORE_RESET | CORE_RESET_MUX);
  458. }
  459. return 0;
  460. disable_ahb_clk:
  461. clk_disable_unprepare(qphy->cfg_ahb_clk);
  462. disable_iface_clk:
  463. clk_disable_unprepare(qphy->iface_clk);
  464. return ret;
  465. }
  466. static int qusb2_phy_init(struct phy *phy)
  467. {
  468. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  469. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  470. unsigned int val = 0;
  471. unsigned int clk_scheme;
  472. int ret;
  473. dev_vdbg(&phy->dev, "%s(): Initializing QUSB2 phy\n", __func__);
  474. /* turn on regulator supplies */
  475. ret = regulator_bulk_enable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  476. if (ret)
  477. return ret;
  478. ret = clk_prepare_enable(qphy->iface_clk);
  479. if (ret) {
  480. dev_err(&phy->dev, "failed to enable iface_clk, %d\n", ret);
  481. goto poweroff_phy;
  482. }
  483. /* enable ahb interface clock to program phy */
  484. ret = clk_prepare_enable(qphy->cfg_ahb_clk);
  485. if (ret) {
  486. dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
  487. goto disable_iface_clk;
  488. }
  489. /* Perform phy reset */
  490. ret = reset_control_assert(qphy->phy_reset);
  491. if (ret) {
  492. dev_err(&phy->dev, "failed to assert phy_reset, %d\n", ret);
  493. goto disable_ahb_clk;
  494. }
  495. /* 100 us delay to keep PHY in reset mode */
  496. usleep_range(100, 150);
  497. ret = reset_control_deassert(qphy->phy_reset);
  498. if (ret) {
  499. dev_err(&phy->dev, "failed to de-assert phy_reset, %d\n", ret);
  500. goto disable_ahb_clk;
  501. }
  502. /* Disable the PHY */
  503. qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
  504. qphy->cfg->disable_ctrl);
  505. if (cfg->has_pll_test) {
  506. /* save reset value to override reference clock scheme later */
  507. val = readl(qphy->base + QUSB2PHY_PLL_TEST);
  508. }
  509. qcom_qusb2_phy_configure(qphy->base, cfg->regs, cfg->tbl,
  510. cfg->tbl_num);
  511. /* Override board specific PHY tuning values */
  512. qusb2_phy_override_phy_params(qphy);
  513. /* Set efuse value for tuning the PHY */
  514. qusb2_phy_set_tune2_param(qphy);
  515. /* Enable the PHY */
  516. qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
  517. POWER_DOWN);
  518. /* Required to get phy pll lock successfully */
  519. usleep_range(150, 160);
  520. /* Default is single-ended clock on msm8996 */
  521. qphy->has_se_clk_scheme = true;
  522. /*
  523. * read TCSR_PHY_CLK_SCHEME register to check if single-ended
  524. * clock scheme is selected. If yes, then disable differential
  525. * ref_clk and use single-ended clock, otherwise use differential
  526. * ref_clk only.
  527. */
  528. if (qphy->tcsr) {
  529. ret = regmap_read(qphy->tcsr, qphy->cfg->clk_scheme_offset,
  530. &clk_scheme);
  531. if (ret) {
  532. dev_err(&phy->dev, "failed to read clk scheme reg\n");
  533. goto assert_phy_reset;
  534. }
  535. /* is it a differential clock scheme ? */
  536. if (!(clk_scheme & PHY_CLK_SCHEME_SEL)) {
  537. dev_vdbg(&phy->dev, "%s(): select differential clk\n",
  538. __func__);
  539. qphy->has_se_clk_scheme = false;
  540. } else {
  541. dev_vdbg(&phy->dev, "%s(): select single-ended clk\n",
  542. __func__);
  543. }
  544. }
  545. if (!qphy->has_se_clk_scheme) {
  546. ret = clk_prepare_enable(qphy->ref_clk);
  547. if (ret) {
  548. dev_err(&phy->dev, "failed to enable ref clk, %d\n",
  549. ret);
  550. goto assert_phy_reset;
  551. }
  552. }
  553. if (cfg->has_pll_test) {
  554. if (!qphy->has_se_clk_scheme)
  555. val &= ~CLK_REF_SEL;
  556. else
  557. val |= CLK_REF_SEL;
  558. writel(val, qphy->base + QUSB2PHY_PLL_TEST);
  559. /* ensure above write is through */
  560. readl(qphy->base + QUSB2PHY_PLL_TEST);
  561. }
  562. /* Required to get phy pll lock successfully */
  563. usleep_range(100, 110);
  564. val = readb(qphy->base + cfg->regs[QUSB2PHY_PLL_STATUS]);
  565. if (!(val & cfg->mask_core_ready)) {
  566. dev_err(&phy->dev,
  567. "QUSB2PHY pll lock failed: status reg = %x\n", val);
  568. ret = -EBUSY;
  569. goto disable_ref_clk;
  570. }
  571. qphy->phy_initialized = true;
  572. return 0;
  573. disable_ref_clk:
  574. if (!qphy->has_se_clk_scheme)
  575. clk_disable_unprepare(qphy->ref_clk);
  576. assert_phy_reset:
  577. reset_control_assert(qphy->phy_reset);
  578. disable_ahb_clk:
  579. clk_disable_unprepare(qphy->cfg_ahb_clk);
  580. disable_iface_clk:
  581. clk_disable_unprepare(qphy->iface_clk);
  582. poweroff_phy:
  583. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  584. return ret;
  585. }
  586. static int qusb2_phy_exit(struct phy *phy)
  587. {
  588. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  589. /* Disable the PHY */
  590. qusb2_setbits(qphy->base, qphy->cfg->regs[QUSB2PHY_PORT_POWERDOWN],
  591. qphy->cfg->disable_ctrl);
  592. if (!qphy->has_se_clk_scheme)
  593. clk_disable_unprepare(qphy->ref_clk);
  594. reset_control_assert(qphy->phy_reset);
  595. clk_disable_unprepare(qphy->cfg_ahb_clk);
  596. clk_disable_unprepare(qphy->iface_clk);
  597. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  598. qphy->phy_initialized = false;
  599. return 0;
  600. }
  601. static const struct phy_ops qusb2_phy_gen_ops = {
  602. .init = qusb2_phy_init,
  603. .exit = qusb2_phy_exit,
  604. .set_mode = qusb2_phy_set_mode,
  605. .owner = THIS_MODULE,
  606. };
  607. static const struct of_device_id qusb2_phy_of_match_table[] = {
  608. {
  609. .compatible = "qcom,msm8996-qusb2-phy",
  610. .data = &msm8996_phy_cfg,
  611. }, {
  612. .compatible = "qcom,sdm845-qusb2-phy",
  613. .data = &sdm845_phy_cfg,
  614. },
  615. { },
  616. };
  617. MODULE_DEVICE_TABLE(of, qusb2_phy_of_match_table);
  618. static const struct dev_pm_ops qusb2_phy_pm_ops = {
  619. SET_RUNTIME_PM_OPS(qusb2_phy_runtime_suspend,
  620. qusb2_phy_runtime_resume, NULL)
  621. };
  622. static int qusb2_phy_probe(struct platform_device *pdev)
  623. {
  624. struct device *dev = &pdev->dev;
  625. struct qusb2_phy *qphy;
  626. struct phy_provider *phy_provider;
  627. struct phy *generic_phy;
  628. struct resource *res;
  629. int ret, i;
  630. int num;
  631. u32 value;
  632. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  633. if (!qphy)
  634. return -ENOMEM;
  635. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  636. qphy->base = devm_ioremap_resource(dev, res);
  637. if (IS_ERR(qphy->base))
  638. return PTR_ERR(qphy->base);
  639. qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb");
  640. if (IS_ERR(qphy->cfg_ahb_clk)) {
  641. ret = PTR_ERR(qphy->cfg_ahb_clk);
  642. if (ret != -EPROBE_DEFER)
  643. dev_err(dev, "failed to get cfg ahb clk, %d\n", ret);
  644. return ret;
  645. }
  646. qphy->ref_clk = devm_clk_get(dev, "ref");
  647. if (IS_ERR(qphy->ref_clk)) {
  648. ret = PTR_ERR(qphy->ref_clk);
  649. if (ret != -EPROBE_DEFER)
  650. dev_err(dev, "failed to get ref clk, %d\n", ret);
  651. return ret;
  652. }
  653. qphy->iface_clk = devm_clk_get(dev, "iface");
  654. if (IS_ERR(qphy->iface_clk)) {
  655. ret = PTR_ERR(qphy->iface_clk);
  656. if (ret == -EPROBE_DEFER)
  657. return ret;
  658. qphy->iface_clk = NULL;
  659. dev_dbg(dev, "failed to get iface clk, %d\n", ret);
  660. }
  661. qphy->phy_reset = devm_reset_control_get_by_index(&pdev->dev, 0);
  662. if (IS_ERR(qphy->phy_reset)) {
  663. dev_err(dev, "failed to get phy core reset\n");
  664. return PTR_ERR(qphy->phy_reset);
  665. }
  666. num = ARRAY_SIZE(qphy->vregs);
  667. for (i = 0; i < num; i++)
  668. qphy->vregs[i].supply = qusb2_phy_vreg_names[i];
  669. ret = devm_regulator_bulk_get(dev, num, qphy->vregs);
  670. if (ret) {
  671. dev_err(dev, "failed to get regulator supplies\n");
  672. return ret;
  673. }
  674. /* Get the specific init parameters of QMP phy */
  675. qphy->cfg = of_device_get_match_data(dev);
  676. qphy->tcsr = syscon_regmap_lookup_by_phandle(dev->of_node,
  677. "qcom,tcsr-syscon");
  678. if (IS_ERR(qphy->tcsr)) {
  679. dev_dbg(dev, "failed to lookup TCSR regmap\n");
  680. qphy->tcsr = NULL;
  681. }
  682. qphy->cell = devm_nvmem_cell_get(dev, NULL);
  683. if (IS_ERR(qphy->cell)) {
  684. if (PTR_ERR(qphy->cell) == -EPROBE_DEFER)
  685. return -EPROBE_DEFER;
  686. qphy->cell = NULL;
  687. dev_dbg(dev, "failed to lookup tune2 hstx trim value\n");
  688. }
  689. if (!of_property_read_u32(dev->of_node, "qcom,imp-res-offset-value",
  690. &value)) {
  691. qphy->imp_res_offset_value = (u8)value;
  692. qphy->override_imp_res_offset = true;
  693. }
  694. if (!of_property_read_u32(dev->of_node, "qcom,hstx-trim-value",
  695. &value)) {
  696. qphy->hstx_trim_value = (u8)value;
  697. qphy->override_hstx_trim = true;
  698. }
  699. if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-level",
  700. &value)) {
  701. qphy->preemphasis_level = (u8)value;
  702. qphy->override_preemphasis = true;
  703. }
  704. if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-width",
  705. &value)) {
  706. qphy->preemphasis_width = (u8)value;
  707. qphy->override_preemphasis_width = true;
  708. }
  709. pm_runtime_set_active(dev);
  710. pm_runtime_enable(dev);
  711. /*
  712. * Prevent runtime pm from being ON by default. Users can enable
  713. * it using power/control in sysfs.
  714. */
  715. pm_runtime_forbid(dev);
  716. generic_phy = devm_phy_create(dev, NULL, &qusb2_phy_gen_ops);
  717. if (IS_ERR(generic_phy)) {
  718. ret = PTR_ERR(generic_phy);
  719. dev_err(dev, "failed to create phy, %d\n", ret);
  720. pm_runtime_disable(dev);
  721. return ret;
  722. }
  723. qphy->phy = generic_phy;
  724. dev_set_drvdata(dev, qphy);
  725. phy_set_drvdata(generic_phy, qphy);
  726. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  727. if (!IS_ERR(phy_provider))
  728. dev_info(dev, "Registered Qcom-QUSB2 phy\n");
  729. else
  730. pm_runtime_disable(dev);
  731. return PTR_ERR_OR_ZERO(phy_provider);
  732. }
  733. static struct platform_driver qusb2_phy_driver = {
  734. .probe = qusb2_phy_probe,
  735. .driver = {
  736. .name = "qcom-qusb2-phy",
  737. .pm = &qusb2_phy_pm_ops,
  738. .of_match_table = qusb2_phy_of_match_table,
  739. },
  740. };
  741. module_platform_driver(qusb2_phy_driver);
  742. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  743. MODULE_DESCRIPTION("Qualcomm QUSB2 PHY driver");
  744. MODULE_LICENSE("GPL v2");