phy-qcom-qusb2.c 20 KB

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