phy-qcom-qusb2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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_init(struct phy *phy)
  168. {
  169. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  170. unsigned int val;
  171. unsigned int clk_scheme;
  172. int ret;
  173. dev_vdbg(&phy->dev, "%s(): Initializing QUSB2 phy\n", __func__);
  174. /* turn on regulator supplies */
  175. ret = regulator_bulk_enable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  176. if (ret)
  177. return ret;
  178. ret = clk_prepare_enable(qphy->iface_clk);
  179. if (ret) {
  180. dev_err(&phy->dev, "failed to enable iface_clk, %d\n", ret);
  181. goto poweroff_phy;
  182. }
  183. /* enable ahb interface clock to program phy */
  184. ret = clk_prepare_enable(qphy->cfg_ahb_clk);
  185. if (ret) {
  186. dev_err(&phy->dev, "failed to enable cfg ahb clock, %d\n", ret);
  187. goto disable_iface_clk;
  188. }
  189. /* Perform phy reset */
  190. ret = reset_control_assert(qphy->phy_reset);
  191. if (ret) {
  192. dev_err(&phy->dev, "failed to assert phy_reset, %d\n", ret);
  193. goto disable_ahb_clk;
  194. }
  195. /* 100 us delay to keep PHY in reset mode */
  196. usleep_range(100, 150);
  197. ret = reset_control_deassert(qphy->phy_reset);
  198. if (ret) {
  199. dev_err(&phy->dev, "failed to de-assert phy_reset, %d\n", ret);
  200. goto disable_ahb_clk;
  201. }
  202. /* Disable the PHY */
  203. qusb2_setbits(qphy->base, QUSB2PHY_PORT_POWERDOWN,
  204. CLAMP_N_EN | FREEZIO_N | POWER_DOWN);
  205. /* save reset value to override reference clock scheme later */
  206. val = readl(qphy->base + QUSB2PHY_PLL_TEST);
  207. qcom_qusb2_phy_configure(qphy->base, qphy->cfg->tbl,
  208. qphy->cfg->tbl_num);
  209. /* Set efuse value for tuning the PHY */
  210. qusb2_phy_set_tune2_param(qphy);
  211. /* Enable the PHY */
  212. qusb2_clrbits(qphy->base, QUSB2PHY_PORT_POWERDOWN, POWER_DOWN);
  213. /* Required to get phy pll lock successfully */
  214. usleep_range(150, 160);
  215. /* Default is single-ended clock on msm8996 */
  216. qphy->has_se_clk_scheme = true;
  217. /*
  218. * read TCSR_PHY_CLK_SCHEME register to check if single-ended
  219. * clock scheme is selected. If yes, then disable differential
  220. * ref_clk and use single-ended clock, otherwise use differential
  221. * ref_clk only.
  222. */
  223. if (qphy->tcsr) {
  224. ret = regmap_read(qphy->tcsr, qphy->cfg->clk_scheme_offset,
  225. &clk_scheme);
  226. if (ret) {
  227. dev_err(&phy->dev, "failed to read clk scheme reg\n");
  228. goto assert_phy_reset;
  229. }
  230. /* is it a differential clock scheme ? */
  231. if (!(clk_scheme & PHY_CLK_SCHEME_SEL)) {
  232. dev_vdbg(&phy->dev, "%s(): select differential clk\n",
  233. __func__);
  234. qphy->has_se_clk_scheme = false;
  235. } else {
  236. dev_vdbg(&phy->dev, "%s(): select single-ended clk\n",
  237. __func__);
  238. }
  239. }
  240. if (!qphy->has_se_clk_scheme) {
  241. val &= ~CLK_REF_SEL;
  242. ret = clk_prepare_enable(qphy->ref_clk);
  243. if (ret) {
  244. dev_err(&phy->dev, "failed to enable ref clk, %d\n",
  245. ret);
  246. goto assert_phy_reset;
  247. }
  248. } else {
  249. val |= CLK_REF_SEL;
  250. }
  251. writel(val, qphy->base + QUSB2PHY_PLL_TEST);
  252. /* ensure above write is through */
  253. readl(qphy->base + QUSB2PHY_PLL_TEST);
  254. /* Required to get phy pll lock successfully */
  255. usleep_range(100, 110);
  256. val = readb(qphy->base + QUSB2PHY_PLL_STATUS);
  257. if (!(val & PLL_LOCKED)) {
  258. dev_err(&phy->dev,
  259. "QUSB2PHY pll lock failed: status reg = %x\n", val);
  260. ret = -EBUSY;
  261. goto disable_ref_clk;
  262. }
  263. return 0;
  264. disable_ref_clk:
  265. if (!qphy->has_se_clk_scheme)
  266. clk_disable_unprepare(qphy->ref_clk);
  267. assert_phy_reset:
  268. reset_control_assert(qphy->phy_reset);
  269. disable_ahb_clk:
  270. clk_disable_unprepare(qphy->cfg_ahb_clk);
  271. disable_iface_clk:
  272. clk_disable_unprepare(qphy->iface_clk);
  273. poweroff_phy:
  274. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  275. return ret;
  276. }
  277. static int qusb2_phy_exit(struct phy *phy)
  278. {
  279. struct qusb2_phy *qphy = phy_get_drvdata(phy);
  280. /* Disable the PHY */
  281. qusb2_setbits(qphy->base, QUSB2PHY_PORT_POWERDOWN,
  282. CLAMP_N_EN | FREEZIO_N | POWER_DOWN);
  283. if (!qphy->has_se_clk_scheme)
  284. clk_disable_unprepare(qphy->ref_clk);
  285. reset_control_assert(qphy->phy_reset);
  286. clk_disable_unprepare(qphy->cfg_ahb_clk);
  287. clk_disable_unprepare(qphy->iface_clk);
  288. regulator_bulk_disable(ARRAY_SIZE(qphy->vregs), qphy->vregs);
  289. return 0;
  290. }
  291. static const struct phy_ops qusb2_phy_gen_ops = {
  292. .init = qusb2_phy_init,
  293. .exit = qusb2_phy_exit,
  294. .owner = THIS_MODULE,
  295. };
  296. static const struct of_device_id qusb2_phy_of_match_table[] = {
  297. {
  298. .compatible = "qcom,msm8996-qusb2-phy",
  299. .data = &msm8996_phy_cfg,
  300. },
  301. { },
  302. };
  303. MODULE_DEVICE_TABLE(of, qusb2_phy_of_match_table);
  304. static int qusb2_phy_probe(struct platform_device *pdev)
  305. {
  306. struct device *dev = &pdev->dev;
  307. struct qusb2_phy *qphy;
  308. struct phy_provider *phy_provider;
  309. struct phy *generic_phy;
  310. struct resource *res;
  311. int ret, i;
  312. int num;
  313. qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
  314. if (!qphy)
  315. return -ENOMEM;
  316. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  317. qphy->base = devm_ioremap_resource(dev, res);
  318. if (IS_ERR(qphy->base))
  319. return PTR_ERR(qphy->base);
  320. qphy->cfg_ahb_clk = devm_clk_get(dev, "cfg_ahb");
  321. if (IS_ERR(qphy->cfg_ahb_clk)) {
  322. ret = PTR_ERR(qphy->cfg_ahb_clk);
  323. if (ret != -EPROBE_DEFER)
  324. dev_err(dev, "failed to get cfg ahb clk, %d\n", ret);
  325. return ret;
  326. }
  327. qphy->ref_clk = devm_clk_get(dev, "ref");
  328. if (IS_ERR(qphy->ref_clk)) {
  329. ret = PTR_ERR(qphy->ref_clk);
  330. if (ret != -EPROBE_DEFER)
  331. dev_err(dev, "failed to get ref clk, %d\n", ret);
  332. return ret;
  333. }
  334. qphy->iface_clk = devm_clk_get(dev, "iface");
  335. if (IS_ERR(qphy->iface_clk)) {
  336. ret = PTR_ERR(qphy->iface_clk);
  337. if (ret == -EPROBE_DEFER)
  338. return ret;
  339. qphy->iface_clk = NULL;
  340. dev_dbg(dev, "failed to get iface clk, %d\n", ret);
  341. }
  342. qphy->phy_reset = devm_reset_control_get_by_index(&pdev->dev, 0);
  343. if (IS_ERR(qphy->phy_reset)) {
  344. dev_err(dev, "failed to get phy core reset\n");
  345. return PTR_ERR(qphy->phy_reset);
  346. }
  347. num = ARRAY_SIZE(qphy->vregs);
  348. for (i = 0; i < num; i++)
  349. qphy->vregs[i].supply = qusb2_phy_vreg_names[i];
  350. ret = devm_regulator_bulk_get(dev, num, qphy->vregs);
  351. if (ret) {
  352. dev_err(dev, "failed to get regulator supplies\n");
  353. return ret;
  354. }
  355. /* Get the specific init parameters of QMP phy */
  356. qphy->cfg = of_device_get_match_data(dev);
  357. qphy->tcsr = syscon_regmap_lookup_by_phandle(dev->of_node,
  358. "qcom,tcsr-syscon");
  359. if (IS_ERR(qphy->tcsr)) {
  360. dev_dbg(dev, "failed to lookup TCSR regmap\n");
  361. qphy->tcsr = NULL;
  362. }
  363. qphy->cell = devm_nvmem_cell_get(dev, NULL);
  364. if (IS_ERR(qphy->cell)) {
  365. if (PTR_ERR(qphy->cell) == -EPROBE_DEFER)
  366. return -EPROBE_DEFER;
  367. qphy->cell = NULL;
  368. dev_dbg(dev, "failed to lookup tune2 hstx trim value\n");
  369. }
  370. generic_phy = devm_phy_create(dev, NULL, &qusb2_phy_gen_ops);
  371. if (IS_ERR(generic_phy)) {
  372. ret = PTR_ERR(generic_phy);
  373. dev_err(dev, "failed to create phy, %d\n", ret);
  374. return ret;
  375. }
  376. qphy->phy = generic_phy;
  377. dev_set_drvdata(dev, qphy);
  378. phy_set_drvdata(generic_phy, qphy);
  379. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  380. if (!IS_ERR(phy_provider))
  381. dev_info(dev, "Registered Qcom-QUSB2 phy\n");
  382. return PTR_ERR_OR_ZERO(phy_provider);
  383. }
  384. static struct platform_driver qusb2_phy_driver = {
  385. .probe = qusb2_phy_probe,
  386. .driver = {
  387. .name = "qcom-qusb2-phy",
  388. .of_match_table = qusb2_phy_of_match_table,
  389. },
  390. };
  391. module_platform_driver(qusb2_phy_driver);
  392. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  393. MODULE_DESCRIPTION("Qualcomm QUSB2 PHY driver");
  394. MODULE_LICENSE("GPL v2");