phy-qcom-qusb2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. };
  198. static const char * const qusb2_phy_vreg_names[] = {
  199. "vdda-pll", "vdda-phy-dpdm",
  200. };
  201. #define QUSB2_NUM_VREGS ARRAY_SIZE(qusb2_phy_vreg_names)
  202. /**
  203. * struct qusb2_phy - structure holding qusb2 phy attributes
  204. *
  205. * @phy: generic phy
  206. * @base: iomapped memory space for qubs2 phy
  207. *
  208. * @cfg_ahb_clk: AHB2PHY interface clock
  209. * @ref_clk: phy reference clock
  210. * @iface_clk: phy interface clock
  211. * @phy_reset: phy reset control
  212. * @vregs: regulator supplies bulk data
  213. *
  214. * @tcsr: TCSR syscon register map
  215. * @cell: nvmem cell containing phy tuning value
  216. *
  217. * @override_imp_res_offset: PHY should use different rescode offset
  218. * @imp_res_offset_value: rescode offset to be updated in IMP_CTRL1 register
  219. * @override_hstx_trim: PHY should use different HSTX o/p current value
  220. * @hstx_trim_value: HSTX_TRIM value to be updated in TUNE1 register
  221. * @override_preemphasis: PHY should use different pre-amphasis amplitude
  222. * @preemphasis_level: Amplitude Pre-Emphasis to be updated in TUNE1 register
  223. * @override_preemphasis_width: PHY should use different pre-emphasis duration
  224. * @preemphasis_width: half/full-width Pre-Emphasis updated via TUNE1
  225. *
  226. * @cfg: phy config data
  227. * @has_se_clk_scheme: indicate if PHY has single-ended ref clock scheme
  228. * @phy_initialized: indicate if PHY has been initialized
  229. * @mode: current PHY mode
  230. */
  231. struct qusb2_phy {
  232. struct phy *phy;
  233. void __iomem *base;
  234. struct clk *cfg_ahb_clk;
  235. struct clk *ref_clk;
  236. struct clk *iface_clk;
  237. struct reset_control *phy_reset;
  238. struct regulator_bulk_data vregs[QUSB2_NUM_VREGS];
  239. struct regmap *tcsr;
  240. struct nvmem_cell *cell;
  241. bool override_imp_res_offset;
  242. u8 imp_res_offset_value;
  243. bool override_hstx_trim;
  244. u8 hstx_trim_value;
  245. bool override_preemphasis;
  246. u8 preemphasis_level;
  247. bool override_preemphasis_width;
  248. u8 preemphasis_width;
  249. const struct qusb2_phy_cfg *cfg;
  250. bool has_se_clk_scheme;
  251. bool phy_initialized;
  252. enum phy_mode mode;
  253. };
  254. static inline void qusb2_write_mask(void __iomem *base, u32 offset,
  255. u32 val, u32 mask)
  256. {
  257. u32 reg;
  258. reg = readl(base + offset);
  259. reg &= ~mask;
  260. reg |= val & mask;
  261. writel(reg, base + offset);
  262. /* Ensure above write is completed */
  263. readl(base + offset);
  264. }
  265. static inline void qusb2_setbits(void __iomem *base, u32 offset, u32 val)
  266. {
  267. u32 reg;
  268. reg = readl(base + offset);
  269. reg |= val;
  270. writel(reg, base + offset);
  271. /* Ensure above write is completed */
  272. readl(base + offset);
  273. }
  274. static inline void qusb2_clrbits(void __iomem *base, u32 offset, u32 val)
  275. {
  276. u32 reg;
  277. reg = readl(base + offset);
  278. reg &= ~val;
  279. writel(reg, base + offset);
  280. /* Ensure above write is completed */
  281. readl(base + offset);
  282. }
  283. static inline
  284. void qcom_qusb2_phy_configure(void __iomem *base,
  285. const unsigned int *regs,
  286. const struct qusb2_phy_init_tbl tbl[], int num)
  287. {
  288. int i;
  289. for (i = 0; i < num; i++) {
  290. if (tbl[i].in_layout)
  291. writel(tbl[i].val, base + regs[tbl[i].offset]);
  292. else
  293. writel(tbl[i].val, base + tbl[i].offset);
  294. }
  295. }
  296. /*
  297. * Update board specific PHY tuning override values if specified from
  298. * device tree.
  299. */
  300. static void qusb2_phy_override_phy_params(struct qusb2_phy *qphy)
  301. {
  302. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  303. if (qphy->override_imp_res_offset)
  304. qusb2_write_mask(qphy->base, QUSB2PHY_IMP_CTRL1,
  305. qphy->imp_res_offset_value << IMP_RES_OFFSET_SHIFT,
  306. IMP_RES_OFFSET_MASK);
  307. if (qphy->override_hstx_trim)
  308. qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
  309. qphy->hstx_trim_value << HSTX_TRIM_SHIFT,
  310. HSTX_TRIM_MASK);
  311. if (qphy->override_preemphasis)
  312. qusb2_write_mask(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
  313. qphy->preemphasis_level << PREEMPHASIS_EN_SHIFT,
  314. PREEMPHASIS_EN_MASK);
  315. if (qphy->override_preemphasis_width) {
  316. if (qphy->preemphasis_width ==
  317. QUSB2_V2_PREEMPHASIS_WIDTH_HALF_BIT)
  318. qusb2_setbits(qphy->base,
  319. cfg->regs[QUSB2PHY_PORT_TUNE1],
  320. PREEMPH_WIDTH_HALF_BIT);
  321. else
  322. qusb2_clrbits(qphy->base,
  323. cfg->regs[QUSB2PHY_PORT_TUNE1],
  324. PREEMPH_WIDTH_HALF_BIT);
  325. }
  326. }
  327. /*
  328. * Fetches HS Tx tuning value from nvmem and sets the
  329. * QUSB2PHY_PORT_TUNE1/2 register.
  330. * For error case, skip setting the value and use the default value.
  331. */
  332. static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy)
  333. {
  334. struct device *dev = &qphy->phy->dev;
  335. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  336. u8 *val;
  337. /* efuse register is optional */
  338. if (!qphy->cell)
  339. return;
  340. /*
  341. * Read efuse register having TUNE2/1 parameter's high nibble.
  342. * If efuse register shows value as 0x0, or if we fail to find
  343. * a valid efuse register settings, then use default value
  344. * as 0xB for high nibble that we have already set while
  345. * configuring phy.
  346. */
  347. val = nvmem_cell_read(qphy->cell, NULL);
  348. if (IS_ERR(val) || !val[0]) {
  349. dev_dbg(dev, "failed to read a valid hs-tx trim value\n");
  350. return;
  351. }
  352. /* Fused TUNE1/2 value is the higher nibble only */
  353. if (cfg->update_tune1_with_efuse)
  354. qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE1],
  355. val[0] << 0x4);
  356. else
  357. qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TUNE2],
  358. val[0] << 0x4);
  359. }
  360. static int qusb2_phy_set_mode(struct phy *phy, enum phy_mode mode)
  361. {
  362. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  363. qphy->mode = mode;
  364. return 0;
  365. }
  366. static int __maybe_unused qusb2_phy_runtime_suspend(struct device *dev)
  367. {
  368. struct qusb2_phy *qphy = dev_get_drvdata(dev);
  369. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  370. u32 intr_mask;
  371. dev_vdbg(dev, "Suspending QUSB2 Phy, mode:%d\n", qphy->mode);
  372. if (!qphy->phy_initialized) {
  373. dev_vdbg(dev, "PHY not initialized, bailing out\n");
  374. return 0;
  375. }
  376. /*
  377. * Enable DP/DM interrupts to detect line state changes based on current
  378. * speed. In other words, enable the triggers _opposite_ of what the
  379. * current D+/D- levels are e.g. if currently D+ high, D- low
  380. * (HS 'J'/Suspend), configure the mask to trigger on D+ low OR D- high
  381. */
  382. intr_mask = DPSE_INTR_EN | DMSE_INTR_EN;
  383. switch (qphy->mode) {
  384. case PHY_MODE_USB_HOST_HS:
  385. case PHY_MODE_USB_HOST_FS:
  386. case PHY_MODE_USB_DEVICE_HS:
  387. case PHY_MODE_USB_DEVICE_FS:
  388. intr_mask |= DMSE_INTR_HIGH_SEL;
  389. break;
  390. case PHY_MODE_USB_HOST_LS:
  391. case PHY_MODE_USB_DEVICE_LS:
  392. intr_mask |= DPSE_INTR_HIGH_SEL;
  393. break;
  394. default:
  395. /* No device connected, enable both DP/DM high interrupt */
  396. intr_mask |= DMSE_INTR_HIGH_SEL;
  397. intr_mask |= DPSE_INTR_HIGH_SEL;
  398. break;
  399. }
  400. writel(intr_mask, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
  401. /* hold core PLL into reset */
  402. if (cfg->has_pll_override) {
  403. qusb2_setbits(qphy->base,
  404. cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
  405. CORE_PLL_EN_FROM_RESET | CORE_RESET |
  406. CORE_RESET_MUX);
  407. }
  408. /* enable phy auto-resume only if device is connected on bus */
  409. if (qphy->mode != PHY_MODE_INVALID) {
  410. qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
  411. cfg->autoresume_en);
  412. /* Autoresume bit has to be toggled in order to enable it */
  413. qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_TEST1],
  414. cfg->autoresume_en);
  415. }
  416. if (!qphy->has_se_clk_scheme)
  417. clk_disable_unprepare(qphy->ref_clk);
  418. clk_disable_unprepare(qphy->cfg_ahb_clk);
  419. clk_disable_unprepare(qphy->iface_clk);
  420. return 0;
  421. }
  422. static int __maybe_unused qusb2_phy_runtime_resume(struct device *dev)
  423. {
  424. struct qusb2_phy *qphy = dev_get_drvdata(dev);
  425. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  426. int ret;
  427. dev_vdbg(dev, "Resuming QUSB2 phy, mode:%d\n", qphy->mode);
  428. if (!qphy->phy_initialized) {
  429. dev_vdbg(dev, "PHY not initialized, bailing out\n");
  430. return 0;
  431. }
  432. ret = clk_prepare_enable(qphy->iface_clk);
  433. if (ret) {
  434. dev_err(dev, "failed to enable iface_clk, %d\n", ret);
  435. return ret;
  436. }
  437. ret = clk_prepare_enable(qphy->cfg_ahb_clk);
  438. if (ret) {
  439. dev_err(dev, "failed to enable cfg ahb clock, %d\n", ret);
  440. goto disable_iface_clk;
  441. }
  442. if (!qphy->has_se_clk_scheme) {
  443. clk_prepare_enable(qphy->ref_clk);
  444. if (ret) {
  445. dev_err(dev, "failed to enable ref clk, %d\n", ret);
  446. goto disable_ahb_clk;
  447. }
  448. }
  449. writel(0x0, qphy->base + cfg->regs[QUSB2PHY_INTR_CTRL]);
  450. /* bring core PLL out of reset */
  451. if (cfg->has_pll_override) {
  452. qusb2_clrbits(qphy->base,
  453. cfg->regs[QUSB2PHY_PLL_CORE_INPUT_OVERRIDE],
  454. CORE_RESET | CORE_RESET_MUX);
  455. }
  456. return 0;
  457. disable_ahb_clk:
  458. clk_disable_unprepare(qphy->cfg_ahb_clk);
  459. disable_iface_clk:
  460. clk_disable_unprepare(qphy->iface_clk);
  461. return ret;
  462. }
  463. static int qusb2_phy_init(struct phy *phy)
  464. {
  465. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  466. const struct qusb2_phy_cfg *cfg = qphy->cfg;
  467. unsigned int val = 0;
  468. unsigned int clk_scheme;
  469. int ret;
  470. dev_vdbg(&phy->dev, "%s(): Initializing QUSB2 phy\n", __func__);
  471. /* turn on regulator supplies */
  472. ret = regulator_bulk_enable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  473. if (ret)
  474. return ret;
  475. ret = clk_prepare_enable(qphy->iface_clk);
  476. if (ret) {
  477. dev_err(&phy->dev, "failed to enable iface_clk, %d\n", ret);
  478. goto poweroff_phy;
  479. }
  480. /* enable ahb interface clock to program phy */
  481. ret = clk_prepare_enable(qphy->cfg_ahb_clk);
  482. if (ret) {
  483. dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
  484. goto disable_iface_clk;
  485. }
  486. /* Perform phy reset */
  487. ret = reset_control_assert(qphy->phy_reset);
  488. if (ret) {
  489. dev_err(&phy->dev, "failed to assert phy_reset, %d\n", ret);
  490. goto disable_ahb_clk;
  491. }
  492. /* 100 us delay to keep PHY in reset mode */
  493. usleep_range(100, 150);
  494. ret = reset_control_deassert(qphy->phy_reset);
  495. if (ret) {
  496. dev_err(&phy->dev, "failed to de-assert phy_reset, %d\n", ret);
  497. goto disable_ahb_clk;
  498. }
  499. /* Disable the PHY */
  500. qusb2_setbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
  501. qphy->cfg->disable_ctrl);
  502. if (cfg->has_pll_test) {
  503. /* save reset value to override reference clock scheme later */
  504. val = readl(qphy->base + QUSB2PHY_PLL_TEST);
  505. }
  506. qcom_qusb2_phy_configure(qphy->base, cfg->regs, cfg->tbl,
  507. cfg->tbl_num);
  508. /* Override board specific PHY tuning values */
  509. qusb2_phy_override_phy_params(qphy);
  510. /* Set efuse value for tuning the PHY */
  511. qusb2_phy_set_tune2_param(qphy);
  512. /* Enable the PHY */
  513. qusb2_clrbits(qphy->base, cfg->regs[QUSB2PHY_PORT_POWERDOWN],
  514. POWER_DOWN);
  515. /* Required to get phy pll lock successfully */
  516. usleep_range(150, 160);
  517. /* Default is single-ended clock on msm8996 */
  518. qphy->has_se_clk_scheme = true;
  519. /*
  520. * read TCSR_PHY_CLK_SCHEME register to check if single-ended
  521. * clock scheme is selected. If yes, then disable differential
  522. * ref_clk and use single-ended clock, otherwise use differential
  523. * ref_clk only.
  524. */
  525. if (qphy->tcsr) {
  526. ret = regmap_read(qphy->tcsr, qphy->cfg->clk_scheme_offset,
  527. &clk_scheme);
  528. if (ret) {
  529. dev_err(&phy->dev, "failed to read clk scheme reg\n");
  530. goto assert_phy_reset;
  531. }
  532. /* is it a differential clock scheme ? */
  533. if (!(clk_scheme & PHY_CLK_SCHEME_SEL)) {
  534. dev_vdbg(&phy->dev, "%s(): select differential clk\n",
  535. __func__);
  536. qphy->has_se_clk_scheme = false;
  537. } else {
  538. dev_vdbg(&phy->dev, "%s(): select single-ended clk\n",
  539. __func__);
  540. }
  541. }
  542. if (!qphy->has_se_clk_scheme) {
  543. ret = clk_prepare_enable(qphy->ref_clk);
  544. if (ret) {
  545. dev_err(&phy->dev, "failed to enable ref clk, %d\n",
  546. ret);
  547. goto assert_phy_reset;
  548. }
  549. }
  550. if (cfg->has_pll_test) {
  551. if (!qphy->has_se_clk_scheme)
  552. val &= ~CLK_REF_SEL;
  553. else
  554. val |= CLK_REF_SEL;
  555. writel(val, qphy->base + QUSB2PHY_PLL_TEST);
  556. /* ensure above write is through */
  557. readl(qphy->base + QUSB2PHY_PLL_TEST);
  558. }
  559. /* Required to get phy pll lock successfully */
  560. usleep_range(100, 110);
  561. val = readb(qphy->base + cfg->regs[QUSB2PHY_PLL_STATUS]);
  562. if (!(val & cfg->mask_core_ready)) {
  563. dev_err(&phy->dev,
  564. "QUSB2PHY pll lock failed: status reg = %x\n", val);
  565. ret = -EBUSY;
  566. goto disable_ref_clk;
  567. }
  568. qphy->phy_initialized = true;
  569. return 0;
  570. disable_ref_clk:
  571. if (!qphy->has_se_clk_scheme)
  572. clk_disable_unprepare(qphy->ref_clk);
  573. assert_phy_reset:
  574. reset_control_assert(qphy->phy_reset);
  575. disable_ahb_clk:
  576. clk_disable_unprepare(qphy->cfg_ahb_clk);
  577. disable_iface_clk:
  578. clk_disable_unprepare(qphy->iface_clk);
  579. poweroff_phy:
  580. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  581. return ret;
  582. }
  583. static int qusb2_phy_exit(struct phy *phy)
  584. {
  585. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  586. /* Disable the PHY */
  587. qusb2_setbits(qphy->base, qphy->cfg->regs[QUSB2PHY_PORT_POWERDOWN],
  588. qphy->cfg->disable_ctrl);
  589. if (!qphy->has_se_clk_scheme)
  590. clk_disable_unprepare(qphy->ref_clk);
  591. reset_control_assert(qphy->phy_reset);
  592. clk_disable_unprepare(qphy->cfg_ahb_clk);
  593. clk_disable_unprepare(qphy->iface_clk);
  594. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  595. qphy->phy_initialized = false;
  596. return 0;
  597. }
  598. static const struct phy_ops qusb2_phy_gen_ops = {
  599. .init = qusb2_phy_init,
  600. .exit = qusb2_phy_exit,
  601. .set_mode = qusb2_phy_set_mode,
  602. .owner = THIS_MODULE,
  603. };
  604. static const struct of_device_id qusb2_phy_of_match_table[] = {
  605. {
  606. .compatible = "qcom,msm8996-qusb2-phy",
  607. .data = &msm8996_phy_cfg,
  608. }, {
  609. .compatible = "qcom,sdm845-qusb2-phy",
  610. .data = &sdm845_phy_cfg,
  611. },
  612. { },
  613. };
  614. MODULE_DEVICE_TABLE(of, qusb2_phy_of_match_table);
  615. static const struct dev_pm_ops qusb2_phy_pm_ops = {
  616. SET_RUNTIME_PM_OPS(qusb2_phy_runtime_suspend,
  617. qusb2_phy_runtime_resume, NULL)
  618. };
  619. static int qusb2_phy_probe(struct platform_device *pdev)
  620. {
  621. struct device *dev = &pdev->dev;
  622. struct qusb2_phy *qphy;
  623. struct phy_provider *phy_provider;
  624. struct phy *generic_phy;
  625. struct resource *res;
  626. int ret, i;
  627. int num;
  628. u32 value;
  629. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  630. if (!qphy)
  631. return -ENOMEM;
  632. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  633. qphy->base = devm_ioremap_resource(dev, res);
  634. if (IS_ERR(qphy->base))
  635. return PTR_ERR(qphy->base);
  636. qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb");
  637. if (IS_ERR(qphy->cfg_ahb_clk)) {
  638. ret = PTR_ERR(qphy->cfg_ahb_clk);
  639. if (ret != -EPROBE_DEFER)
  640. dev_err(dev, "failed to get cfg ahb clk, %d\n", ret);
  641. return ret;
  642. }
  643. qphy->ref_clk = devm_clk_get(dev, "ref");
  644. if (IS_ERR(qphy->ref_clk)) {
  645. ret = PTR_ERR(qphy->ref_clk);
  646. if (ret != -EPROBE_DEFER)
  647. dev_err(dev, "failed to get ref clk, %d\n", ret);
  648. return ret;
  649. }
  650. qphy->iface_clk = devm_clk_get(dev, "iface");
  651. if (IS_ERR(qphy->iface_clk)) {
  652. ret = PTR_ERR(qphy->iface_clk);
  653. if (ret == -EPROBE_DEFER)
  654. return ret;
  655. qphy->iface_clk = NULL;
  656. dev_dbg(dev, "failed to get iface clk, %d\n", ret);
  657. }
  658. qphy->phy_reset = devm_reset_control_get_by_index(&pdev->dev, 0);
  659. if (IS_ERR(qphy->phy_reset)) {
  660. dev_err(dev, "failed to get phy core reset\n");
  661. return PTR_ERR(qphy->phy_reset);
  662. }
  663. num = ARRAY_SIZE(qphy->vregs);
  664. for (i = 0; i < num; i++)
  665. qphy->vregs[i].supply = qusb2_phy_vreg_names[i];
  666. ret = devm_regulator_bulk_get(dev, num, qphy->vregs);
  667. if (ret) {
  668. dev_err(dev, "failed to get regulator supplies\n");
  669. return ret;
  670. }
  671. /* Get the specific init parameters of QMP phy */
  672. qphy->cfg = of_device_get_match_data(dev);
  673. qphy->tcsr = syscon_regmap_lookup_by_phandle(dev->of_node,
  674. "qcom,tcsr-syscon");
  675. if (IS_ERR(qphy->tcsr)) {
  676. dev_dbg(dev, "failed to lookup TCSR regmap\n");
  677. qphy->tcsr = NULL;
  678. }
  679. qphy->cell = devm_nvmem_cell_get(dev, NULL);
  680. if (IS_ERR(qphy->cell)) {
  681. if (PTR_ERR(qphy->cell) == -EPROBE_DEFER)
  682. return -EPROBE_DEFER;
  683. qphy->cell = NULL;
  684. dev_dbg(dev, "failed to lookup tune2 hstx trim value\n");
  685. }
  686. if (!of_property_read_u32(dev->of_node, "qcom,imp-res-offset-value",
  687. &value)) {
  688. qphy->imp_res_offset_value = (u8)value;
  689. qphy->override_imp_res_offset = true;
  690. }
  691. if (!of_property_read_u32(dev->of_node, "qcom,hstx-trim-value",
  692. &value)) {
  693. qphy->hstx_trim_value = (u8)value;
  694. qphy->override_hstx_trim = true;
  695. }
  696. if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-level",
  697. &value)) {
  698. qphy->preemphasis_level = (u8)value;
  699. qphy->override_preemphasis = true;
  700. }
  701. if (!of_property_read_u32(dev->of_node, "qcom,preemphasis-width",
  702. &value)) {
  703. qphy->preemphasis_width = (u8)value;
  704. qphy->override_preemphasis_width = true;
  705. }
  706. pm_runtime_set_active(dev);
  707. pm_runtime_enable(dev);
  708. /*
  709. * Prevent runtime pm from being ON by default. Users can enable
  710. * it using power/control in sysfs.
  711. */
  712. pm_runtime_forbid(dev);
  713. generic_phy = devm_phy_create(dev, NULL, &qusb2_phy_gen_ops);
  714. if (IS_ERR(generic_phy)) {
  715. ret = PTR_ERR(generic_phy);
  716. dev_err(dev, "failed to create phy, %d\n", ret);
  717. pm_runtime_disable(dev);
  718. return ret;
  719. }
  720. qphy->phy = generic_phy;
  721. dev_set_drvdata(dev, qphy);
  722. phy_set_drvdata(generic_phy, qphy);
  723. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  724. if (!IS_ERR(phy_provider))
  725. dev_info(dev, "Registered Qcom-QUSB2 phy\n");
  726. else
  727. pm_runtime_disable(dev);
  728. return PTR_ERR_OR_ZERO(phy_provider);
  729. }
  730. static struct platform_driver qusb2_phy_driver = {
  731. .probe = qusb2_phy_probe,
  732. .driver = {
  733. .name = "qcom-qusb2-phy",
  734. .pm = &qusb2_phy_pm_ops,
  735. .of_match_table = qusb2_phy_of_match_table,
  736. },
  737. };
  738. module_platform_driver(qusb2_phy_driver);
  739. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  740. MODULE_DESCRIPTION("Qualcomm QUSB2 PHY driver");
  741. MODULE_LICENSE("GPL v2");