phy-qcom-qusb2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright (c) 2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/module.h>
  20. #include <linux/nvmem-consumer.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regmap.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/reset.h>
  28. #include <linux/slab.h>
  29. #define QUSB2PHY_PLL_TEST 0x04
  30. #define CLK_REF_SEL BIT(7)
  31. #define QUSB2PHY_PLL_TUNE 0x08
  32. #define QUSB2PHY_PLL_USER_CTL1 0x0c
  33. #define QUSB2PHY_PLL_USER_CTL2 0x10
  34. #define QUSB2PHY_PLL_AUTOPGM_CTL1 0x1c
  35. #define QUSB2PHY_PLL_PWR_CTRL 0x18
  36. #define QUSB2PHY_PLL_STATUS 0x38
  37. #define PLL_LOCKED BIT(5)
  38. #define QUSB2PHY_PORT_TUNE1 0x80
  39. #define QUSB2PHY_PORT_TUNE2 0x84
  40. #define QUSB2PHY_PORT_TUNE3 0x88
  41. #define QUSB2PHY_PORT_TUNE4 0x8c
  42. #define QUSB2PHY_PORT_TUNE5 0x90
  43. #define QUSB2PHY_PORT_TEST2 0x9c
  44. #define QUSB2PHY_PORT_POWERDOWN 0xb4
  45. #define CLAMP_N_EN BIT(5)
  46. #define FREEZIO_N BIT(1)
  47. #define POWER_DOWN BIT(0)
  48. #define QUSB2PHY_REFCLK_ENABLE BIT(0)
  49. #define PHY_CLK_SCHEME_SEL BIT(0)
  50. struct qusb2_phy_init_tbl {
  51. unsigned int offset;
  52. unsigned int val;
  53. };
  54. #define QUSB2_PHY_INIT_CFG(o, v) \
  55. { \
  56. .offset = o, \
  57. .val = v, \
  58. }
  59. static const struct qusb2_phy_init_tbl msm8996_init_tbl[] = {
  60. QUSB2_PHY_INIT_CFG(QUSB2PHY_PORT_TUNE1, 0xf8),
  61. QUSB2_PHY_INIT_CFG(QUSB2PHY_PORT_TUNE2, 0xb3),
  62. QUSB2_PHY_INIT_CFG(QUSB2PHY_PORT_TUNE3, 0x83),
  63. QUSB2_PHY_INIT_CFG(QUSB2PHY_PORT_TUNE4, 0xc0),
  64. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_TUNE, 0x30),
  65. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL1, 0x79),
  66. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_USER_CTL2, 0x21),
  67. QUSB2_PHY_INIT_CFG(QUSB2PHY_PORT_TEST2, 0x14),
  68. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_AUTOPGM_CTL1, 0x9f),
  69. QUSB2_PHY_INIT_CFG(QUSB2PHY_PLL_PWR_CTRL, 0x00),
  70. };
  71. struct qusb2_phy_cfg {
  72. const struct qusb2_phy_init_tbl *tbl;
  73. /* number of entries in the table */
  74. unsigned int tbl_num;
  75. /* offset to PHY_CLK_SCHEME register in TCSR map */
  76. unsigned int clk_scheme_offset;
  77. };
  78. static const struct qusb2_phy_cfg msm8996_phy_cfg = {
  79. .tbl = msm8996_init_tbl,
  80. .tbl_num = ARRAY_SIZE(msm8996_init_tbl),
  81. };
  82. static const char * const qusb2_phy_vreg_names[] = {
  83. "vdda-pll", "vdda-phy-dpdm",
  84. };
  85. #define QUSB2_NUM_VREGS ARRAY_SIZE(qusb2_phy_vreg_names)
  86. /**
  87. * struct qusb2_phy - structure holding qusb2 phy attributes
  88. *
  89. * @phy: generic phy
  90. * @base: iomapped memory space for qubs2 phy
  91. *
  92. * @cfg_ahb_clk: AHB2PHY interface clock
  93. * @ref_clk: phy reference clock
  94. * @iface_clk: phy interface clock
  95. * @phy_reset: phy reset control
  96. * @vregs: regulator supplies bulk data
  97. *
  98. * @tcsr: TCSR syscon register map
  99. * @cell: nvmem cell containing phy tuning value
  100. *
  101. * @cfg: phy config data
  102. * @has_se_clk_scheme: indicate if PHY has single-ended ref clock scheme
  103. */
  104. struct qusb2_phy {
  105. struct phy *phy;
  106. void __iomem *base;
  107. struct clk *cfg_ahb_clk;
  108. struct clk *ref_clk;
  109. struct clk *iface_clk;
  110. struct reset_control *phy_reset;
  111. struct regulator_bulk_data vregs[QUSB2_NUM_VREGS];
  112. struct regmap *tcsr;
  113. struct nvmem_cell *cell;
  114. const struct qusb2_phy_cfg *cfg;
  115. bool has_se_clk_scheme;
  116. };
  117. static inline void qusb2_setbits(void __iomem *base, u32 offset, u32 val)
  118. {
  119. u32 reg;
  120. reg = readl(base + offset);
  121. reg |= val;
  122. writel(reg, base + offset);
  123. /* Ensure above write is completed */
  124. readl(base + offset);
  125. }
  126. static inline void qusb2_clrbits(void __iomem *base, u32 offset, u32 val)
  127. {
  128. u32 reg;
  129. reg = readl(base + offset);
  130. reg &= ~val;
  131. writel(reg, base + offset);
  132. /* Ensure above write is completed */
  133. readl(base + offset);
  134. }
  135. static inline
  136. void qcom_qusb2_phy_configure(void __iomem *base,
  137. const struct qusb2_phy_init_tbl tbl[], int num)
  138. {
  139. int i;
  140. for (i = 0; i < num; i++)
  141. writel(tbl[i].val, base + tbl[i].offset);
  142. }
  143. /*
  144. * Fetches HS Tx tuning value from nvmem and sets the
  145. * QUSB2PHY_PORT_TUNE2 register.
  146. * For error case, skip setting the value and use the default value.
  147. */
  148. static void qusb2_phy_set_tune2_param(struct qusb2_phy *qphy)
  149. {
  150. struct device *dev = &qphy->phy->dev;
  151. u8 *val;
  152. /*
  153. * Read efuse register having TUNE2 parameter's high nibble.
  154. * If efuse register shows value as 0x0, or if we fail to find
  155. * a valid efuse register settings, then use default value
  156. * as 0xB for high nibble that we have already set while
  157. * configuring phy.
  158. */
  159. val = nvmem_cell_read(qphy->cell, NULL);
  160. if (IS_ERR(val) || !val[0]) {
  161. dev_dbg(dev, "failed to read a valid hs-tx trim value\n");
  162. return;
  163. }
  164. /* Fused TUNE2 value is the higher nibble only */
  165. qusb2_setbits(qphy->base, QUSB2PHY_PORT_TUNE2, val[0] << 0x4);
  166. }
  167. static int qusb2_phy_poweron(struct phy *phy)
  168. {
  169. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  170. int num = ARRAY_SIZE(qphy->vregs);
  171. int ret;
  172. dev_vdbg(&phy->dev, "%s(): Powering-on QUSB2 phy\n", __func__);
  173. /* turn on regulator supplies */
  174. ret = regulator_bulk_enable(num, qphy->vregs);
  175. if (ret)
  176. return ret;
  177. ret = clk_prepare_enable(qphy->iface_clk);
  178. if (ret) {
  179. dev_err(&phy->dev, "failed to enable iface_clk, %d\n", ret);
  180. regulator_bulk_disable(num, qphy->vregs);
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. static int qusb2_phy_poweroff(struct phy *phy)
  186. {
  187. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  188. clk_disable_unprepare(qphy->iface_clk);
  189. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  190. return 0;
  191. }
  192. static int qusb2_phy_init(struct phy *phy)
  193. {
  194. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  195. unsigned int val;
  196. unsigned int clk_scheme;
  197. int ret;
  198. dev_vdbg(&phy->dev, "%s(): Initializing QUSB2 phy\n", __func__);
  199. /* enable ahb interface clock to program phy */
  200. ret = clk_prepare_enable(qphy->cfg_ahb_clk);
  201. if (ret) {
  202. dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
  203. return ret;
  204. }
  205. /* Perform phy reset */
  206. ret = reset_control_assert(qphy->phy_reset);
  207. if (ret) {
  208. dev_err(&phy->dev, "failed to assert phy_reset, %d\n", ret);
  209. goto disable_ahb_clk;
  210. }
  211. /* 100 us delay to keep PHY in reset mode */
  212. usleep_range(100, 150);
  213. ret = reset_control_deassert(qphy->phy_reset);
  214. if (ret) {
  215. dev_err(&phy->dev, "failed to de-assert phy_reset, %d\n", ret);
  216. goto disable_ahb_clk;
  217. }
  218. /* Disable the PHY */
  219. qusb2_setbits(qphy->base, QUSB2PHY_PORT_POWERDOWN,
  220. CLAMP_N_EN | FREEZIO_N | POWER_DOWN);
  221. /* save reset value to override reference clock scheme later */
  222. val = readl(qphy->base + QUSB2PHY_PLL_TEST);
  223. qcom_qusb2_phy_configure(qphy->base, qphy->cfg->tbl,
  224. qphy->cfg->tbl_num);
  225. /* Set efuse value for tuning the PHY */
  226. qusb2_phy_set_tune2_param(qphy);
  227. /* Enable the PHY */
  228. qusb2_clrbits(qphy->base, QUSB2PHY_PORT_POWERDOWN, POWER_DOWN);
  229. /* Required to get phy pll lock successfully */
  230. usleep_range(150, 160);
  231. /* Default is single-ended clock on msm8996 */
  232. qphy->has_se_clk_scheme = true;
  233. /*
  234. * read TCSR_PHY_CLK_SCHEME register to check if single-ended
  235. * clock scheme is selected. If yes, then disable differential
  236. * ref_clk and use single-ended clock, otherwise use differential
  237. * ref_clk only.
  238. */
  239. if (qphy->tcsr) {
  240. ret = regmap_read(qphy->tcsr, qphy->cfg->clk_scheme_offset,
  241. &clk_scheme);
  242. if (ret) {
  243. dev_err(&phy->dev, "failed to read clk scheme reg\n");
  244. goto assert_phy_reset;
  245. }
  246. /* is it a differential clock scheme ? */
  247. if (!(clk_scheme & PHY_CLK_SCHEME_SEL)) {
  248. dev_vdbg(&phy->dev, "%s(): select differential clk\n",
  249. __func__);
  250. qphy->has_se_clk_scheme = false;
  251. } else {
  252. dev_vdbg(&phy->dev, "%s(): select single-ended clk\n",
  253. __func__);
  254. }
  255. }
  256. if (!qphy->has_se_clk_scheme) {
  257. val &= ~CLK_REF_SEL;
  258. ret = clk_prepare_enable(qphy->ref_clk);
  259. if (ret) {
  260. dev_err(&phy->dev, "failed to enable ref clk, %d\n",
  261. ret);
  262. goto assert_phy_reset;
  263. }
  264. } else {
  265. val |= CLK_REF_SEL;
  266. }
  267. writel(val, qphy->base + QUSB2PHY_PLL_TEST);
  268. /* ensure above write is through */
  269. readl(qphy->base + QUSB2PHY_PLL_TEST);
  270. /* Required to get phy pll lock successfully */
  271. usleep_range(100, 110);
  272. val = readb(qphy->base + QUSB2PHY_PLL_STATUS);
  273. if (!(val & PLL_LOCKED)) {
  274. dev_err(&phy->dev,
  275. "QUSB2PHY pll lock failed: status reg = %x\n", val);
  276. ret = -EBUSY;
  277. goto disable_ref_clk;
  278. }
  279. return 0;
  280. disable_ref_clk:
  281. if (!qphy->has_se_clk_scheme)
  282. clk_disable_unprepare(qphy->ref_clk);
  283. assert_phy_reset:
  284. reset_control_assert(qphy->phy_reset);
  285. disable_ahb_clk:
  286. clk_disable_unprepare(qphy->cfg_ahb_clk);
  287. return ret;
  288. }
  289. static int qusb2_phy_exit(struct phy *phy)
  290. {
  291. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  292. /* Disable the PHY */
  293. qusb2_setbits(qphy->base, QUSB2PHY_PORT_POWERDOWN,
  294. CLAMP_N_EN | FREEZIO_N | POWER_DOWN);
  295. if (!qphy->has_se_clk_scheme)
  296. clk_disable_unprepare(qphy->ref_clk);
  297. reset_control_assert(qphy->phy_reset);
  298. clk_disable_unprepare(qphy->cfg_ahb_clk);
  299. return 0;
  300. }
  301. static const struct phy_ops qusb2_phy_gen_ops = {
  302. .init = qusb2_phy_init,
  303. .exit = qusb2_phy_exit,
  304. .power_on = qusb2_phy_poweron,
  305. .power_off = qusb2_phy_poweroff,
  306. .owner = THIS_MODULE,
  307. };
  308. static const struct of_device_id qusb2_phy_of_match_table[] = {
  309. {
  310. .compatible = "qcom,msm8996-qusb2-phy",
  311. .data = &msm8996_phy_cfg,
  312. },
  313. { },
  314. };
  315. MODULE_DEVICE_TABLE(of, qusb2_phy_of_match_table);
  316. static int qusb2_phy_probe(struct platform_device *pdev)
  317. {
  318. struct device *dev = &pdev->dev;
  319. struct qusb2_phy *qphy;
  320. struct phy_provider *phy_provider;
  321. struct phy *generic_phy;
  322. struct resource *res;
  323. int ret, i;
  324. int num;
  325. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  326. if (!qphy)
  327. return -ENOMEM;
  328. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  329. qphy->base = devm_ioremap_resource(dev, res);
  330. if (IS_ERR(qphy->base))
  331. return PTR_ERR(qphy->base);
  332. qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb");
  333. if (IS_ERR(qphy->cfg_ahb_clk)) {
  334. ret = PTR_ERR(qphy->cfg_ahb_clk);
  335. if (ret != -EPROBE_DEFER)
  336. dev_err(dev, "failed to get cfg ahb clk, %d\n", ret);
  337. return ret;
  338. }
  339. qphy->ref_clk = devm_clk_get(dev, "ref");
  340. if (IS_ERR(qphy->ref_clk)) {
  341. ret = PTR_ERR(qphy->ref_clk);
  342. if (ret != -EPROBE_DEFER)
  343. dev_err(dev, "failed to get ref clk, %d\n", ret);
  344. return ret;
  345. }
  346. qphy->iface_clk = devm_clk_get(dev, "iface");
  347. if (IS_ERR(qphy->iface_clk)) {
  348. ret = PTR_ERR(qphy->iface_clk);
  349. if (ret == -EPROBE_DEFER)
  350. return ret;
  351. qphy->iface_clk = NULL;
  352. dev_dbg(dev, "failed to get iface clk, %d\n", ret);
  353. }
  354. qphy->phy_reset = devm_reset_control_get_by_index(&pdev->dev, 0);
  355. if (IS_ERR(qphy->phy_reset)) {
  356. dev_err(dev, "failed to get phy core reset\n");
  357. return PTR_ERR(qphy->phy_reset);
  358. }
  359. num = ARRAY_SIZE(qphy->vregs);
  360. for (i = 0; i < num; i++)
  361. qphy->vregs[i].supply = qusb2_phy_vreg_names[i];
  362. ret = devm_regulator_bulk_get(dev, num, qphy->vregs);
  363. if (ret) {
  364. dev_err(dev, "failed to get regulator supplies\n");
  365. return ret;
  366. }
  367. /* Get the specific init parameters of QMP phy */
  368. qphy->cfg = of_device_get_match_data(dev);
  369. qphy->tcsr = syscon_regmap_lookup_by_phandle(dev->of_node,
  370. "qcom,tcsr-syscon");
  371. if (IS_ERR(qphy->tcsr)) {
  372. dev_dbg(dev, "failed to lookup TCSR regmap\n");
  373. qphy->tcsr = NULL;
  374. }
  375. qphy->cell = devm_nvmem_cell_get(dev, NULL);
  376. if (IS_ERR(qphy->cell)) {
  377. if (PTR_ERR(qphy->cell) == -EPROBE_DEFER)
  378. return -EPROBE_DEFER;
  379. qphy->cell = NULL;
  380. dev_dbg(dev, "failed to lookup tune2 hstx trim value\n");
  381. }
  382. generic_phy = devm_phy_create(dev, NULL, &qusb2_phy_gen_ops);
  383. if (IS_ERR(generic_phy)) {
  384. ret = PTR_ERR(generic_phy);
  385. dev_err(dev, "failed to create phy, %d\n", ret);
  386. return ret;
  387. }
  388. qphy->phy = generic_phy;
  389. dev_set_drvdata(dev, qphy);
  390. phy_set_drvdata(generic_phy, qphy);
  391. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  392. if (!IS_ERR(phy_provider))
  393. dev_info(dev, "Registered Qcom-QUSB2 phy\n");
  394. return PTR_ERR_OR_ZERO(phy_provider);
  395. }
  396. static struct platform_driver qusb2_phy_driver = {
  397. .probe = qusb2_phy_probe,
  398. .driver = {
  399. .name = "qcom-qusb2-phy",
  400. .of_match_table = qusb2_phy_of_match_table,
  401. },
  402. };
  403. module_platform_driver(qusb2_phy_driver);
  404. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  405. MODULE_DESCRIPTION("Qualcomm QUSB2 PHY driver");
  406. MODULE_LICENSE("GPL v2");