dsi_phy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2015, 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/platform_device.h>
  14. #include "dsi_phy.h"
  15. #define S_DIV_ROUND_UP(n, d) \
  16. (((n) >= 0) ? (((n) + (d) - 1) / (d)) : (((n) - (d) + 1) / (d)))
  17. static inline s32 linear_inter(s32 tmax, s32 tmin, s32 percent,
  18. s32 min_result, bool even)
  19. {
  20. s32 v;
  21. v = (tmax - tmin) * percent;
  22. v = S_DIV_ROUND_UP(v, 100) + tmin;
  23. if (even && (v & 0x1))
  24. return max_t(s32, min_result, v - 1);
  25. else
  26. return max_t(s32, min_result, v);
  27. }
  28. static void dsi_dphy_timing_calc_clk_zero(struct msm_dsi_dphy_timing *timing,
  29. s32 ui, s32 coeff, s32 pcnt)
  30. {
  31. s32 tmax, tmin, clk_z;
  32. s32 temp;
  33. /* reset */
  34. temp = 300 * coeff - ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  35. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  36. if (tmin > 255) {
  37. tmax = 511;
  38. clk_z = linear_inter(2 * tmin, tmin, pcnt, 0, true);
  39. } else {
  40. tmax = 255;
  41. clk_z = linear_inter(tmax, tmin, pcnt, 0, true);
  42. }
  43. /* adjust */
  44. temp = (timing->hs_rqst + timing->clk_prepare + clk_z) & 0x7;
  45. timing->clk_zero = clk_z + 8 - temp;
  46. }
  47. int msm_dsi_dphy_timing_calc(struct msm_dsi_dphy_timing *timing,
  48. struct msm_dsi_phy_clk_request *clk_req)
  49. {
  50. const unsigned long bit_rate = clk_req->bitclk_rate;
  51. const unsigned long esc_rate = clk_req->escclk_rate;
  52. s32 ui, lpx;
  53. s32 tmax, tmin;
  54. s32 pcnt0 = 10;
  55. s32 pcnt1 = (bit_rate > 1200000000) ? 15 : 10;
  56. s32 pcnt2 = 10;
  57. s32 pcnt3 = (bit_rate > 180000000) ? 10 : 40;
  58. s32 coeff = 1000; /* Precision, should avoid overflow */
  59. s32 temp;
  60. if (!bit_rate || !esc_rate)
  61. return -EINVAL;
  62. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  63. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  64. tmax = S_DIV_ROUND_UP(95 * coeff, ui) - 2;
  65. tmin = S_DIV_ROUND_UP(38 * coeff, ui) - 2;
  66. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, true);
  67. temp = lpx / ui;
  68. if (temp & 0x1)
  69. timing->hs_rqst = temp;
  70. else
  71. timing->hs_rqst = max_t(s32, 0, temp - 2);
  72. /* Calculate clk_zero after clk_prepare and hs_rqst */
  73. dsi_dphy_timing_calc_clk_zero(timing, ui, coeff, pcnt2);
  74. temp = 105 * coeff + 12 * ui - 20 * coeff;
  75. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  76. tmin = S_DIV_ROUND_UP(60 * coeff, ui) - 2;
  77. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  78. temp = 85 * coeff + 6 * ui;
  79. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  80. temp = 40 * coeff + 4 * ui;
  81. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  82. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, true);
  83. tmax = 255;
  84. temp = ((timing->hs_prepare >> 1) + 1) * 2 * ui + 2 * ui;
  85. temp = 145 * coeff + 10 * ui - temp;
  86. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  87. timing->hs_zero = linear_inter(tmax, tmin, pcnt2, 24, true);
  88. temp = 105 * coeff + 12 * ui - 20 * coeff;
  89. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  90. temp = 60 * coeff + 4 * ui;
  91. tmin = DIV_ROUND_UP(temp, ui) - 2;
  92. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  93. tmax = 255;
  94. tmin = S_DIV_ROUND_UP(100 * coeff, ui) - 2;
  95. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, true);
  96. tmax = 63;
  97. temp = ((timing->hs_exit >> 1) + 1) * 2 * ui;
  98. temp = 60 * coeff + 52 * ui - 24 * ui - temp;
  99. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  100. timing->shared_timings.clk_post = linear_inter(tmax, tmin, pcnt2, 0,
  101. false);
  102. tmax = 63;
  103. temp = ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  104. temp += ((timing->clk_zero >> 1) + 1) * 2 * ui;
  105. temp += 8 * ui + lpx;
  106. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  107. if (tmin > tmax) {
  108. temp = linear_inter(2 * tmax, tmin, pcnt2, 0, false);
  109. timing->shared_timings.clk_pre = temp >> 1;
  110. timing->shared_timings.clk_pre_inc_by_2 = true;
  111. } else {
  112. timing->shared_timings.clk_pre =
  113. linear_inter(tmax, tmin, pcnt2, 0, false);
  114. timing->shared_timings.clk_pre_inc_by_2 = false;
  115. }
  116. timing->ta_go = 3;
  117. timing->ta_sure = 0;
  118. timing->ta_get = 4;
  119. DBG("PHY timings: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  120. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  121. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  122. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  123. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  124. timing->hs_rqst);
  125. return 0;
  126. }
  127. void msm_dsi_phy_set_src_pll(struct msm_dsi_phy *phy, int pll_id, u32 reg,
  128. u32 bit_mask)
  129. {
  130. int phy_id = phy->id;
  131. u32 val;
  132. if ((phy_id >= DSI_MAX) || (pll_id >= DSI_MAX))
  133. return;
  134. val = dsi_phy_read(phy->base + reg);
  135. if (phy->cfg->src_pll_truthtable[phy_id][pll_id])
  136. dsi_phy_write(phy->base + reg, val | bit_mask);
  137. else
  138. dsi_phy_write(phy->base + reg, val & (~bit_mask));
  139. }
  140. static int dsi_phy_regulator_init(struct msm_dsi_phy *phy)
  141. {
  142. struct regulator_bulk_data *s = phy->supplies;
  143. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  144. struct device *dev = &phy->pdev->dev;
  145. int num = phy->cfg->reg_cfg.num;
  146. int i, ret;
  147. for (i = 0; i < num; i++)
  148. s[i].supply = regs[i].name;
  149. ret = devm_regulator_bulk_get(dev, num, s);
  150. if (ret < 0) {
  151. dev_err(dev, "%s: failed to init regulator, ret=%d\n",
  152. __func__, ret);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static void dsi_phy_regulator_disable(struct msm_dsi_phy *phy)
  158. {
  159. struct regulator_bulk_data *s = phy->supplies;
  160. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  161. int num = phy->cfg->reg_cfg.num;
  162. int i;
  163. DBG("");
  164. for (i = num - 1; i >= 0; i--)
  165. if (regs[i].disable_load >= 0)
  166. regulator_set_load(s[i].consumer, regs[i].disable_load);
  167. regulator_bulk_disable(num, s);
  168. }
  169. static int dsi_phy_regulator_enable(struct msm_dsi_phy *phy)
  170. {
  171. struct regulator_bulk_data *s = phy->supplies;
  172. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  173. struct device *dev = &phy->pdev->dev;
  174. int num = phy->cfg->reg_cfg.num;
  175. int ret, i;
  176. DBG("");
  177. for (i = 0; i < num; i++) {
  178. if (regs[i].enable_load >= 0) {
  179. ret = regulator_set_load(s[i].consumer,
  180. regs[i].enable_load);
  181. if (ret < 0) {
  182. dev_err(dev,
  183. "regulator %d set op mode failed, %d\n",
  184. i, ret);
  185. goto fail;
  186. }
  187. }
  188. }
  189. ret = regulator_bulk_enable(num, s);
  190. if (ret < 0) {
  191. dev_err(dev, "regulator enable failed, %d\n", ret);
  192. goto fail;
  193. }
  194. return 0;
  195. fail:
  196. for (i--; i >= 0; i--)
  197. regulator_set_load(s[i].consumer, regs[i].disable_load);
  198. return ret;
  199. }
  200. static int dsi_phy_enable_resource(struct msm_dsi_phy *phy)
  201. {
  202. struct device *dev = &phy->pdev->dev;
  203. int ret;
  204. pm_runtime_get_sync(dev);
  205. ret = clk_prepare_enable(phy->ahb_clk);
  206. if (ret) {
  207. dev_err(dev, "%s: can't enable ahb clk, %d\n", __func__, ret);
  208. pm_runtime_put_sync(dev);
  209. }
  210. return ret;
  211. }
  212. static void dsi_phy_disable_resource(struct msm_dsi_phy *phy)
  213. {
  214. clk_disable_unprepare(phy->ahb_clk);
  215. pm_runtime_put_sync(&phy->pdev->dev);
  216. }
  217. static const struct of_device_id dsi_phy_dt_match[] = {
  218. #ifdef CONFIG_DRM_MSM_DSI_28NM_PHY
  219. { .compatible = "qcom,dsi-phy-28nm-hpm",
  220. .data = &dsi_phy_28nm_hpm_cfgs },
  221. { .compatible = "qcom,dsi-phy-28nm-lp",
  222. .data = &dsi_phy_28nm_lp_cfgs },
  223. #endif
  224. #ifdef CONFIG_DRM_MSM_DSI_20NM_PHY
  225. { .compatible = "qcom,dsi-phy-20nm",
  226. .data = &dsi_phy_20nm_cfgs },
  227. #endif
  228. #ifdef CONFIG_DRM_MSM_DSI_28NM_8960_PHY
  229. { .compatible = "qcom,dsi-phy-28nm-8960",
  230. .data = &dsi_phy_28nm_8960_cfgs },
  231. #endif
  232. {}
  233. };
  234. /*
  235. * Currently, we only support one SoC for each PHY type. When we have multiple
  236. * SoCs for the same PHY, we can try to make the index searching a bit more
  237. * clever.
  238. */
  239. static int dsi_phy_get_id(struct msm_dsi_phy *phy)
  240. {
  241. struct platform_device *pdev = phy->pdev;
  242. const struct msm_dsi_phy_cfg *cfg = phy->cfg;
  243. struct resource *res;
  244. int i;
  245. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_phy");
  246. if (!res)
  247. return -EINVAL;
  248. for (i = 0; i < cfg->num_dsi_phy; i++) {
  249. if (cfg->io_start[i] == res->start)
  250. return i;
  251. }
  252. return -EINVAL;
  253. }
  254. int msm_dsi_phy_init_common(struct msm_dsi_phy *phy)
  255. {
  256. struct platform_device *pdev = phy->pdev;
  257. int ret = 0;
  258. phy->reg_base = msm_ioremap(pdev, "dsi_phy_regulator",
  259. "DSI_PHY_REG");
  260. if (IS_ERR(phy->reg_base)) {
  261. dev_err(&pdev->dev, "%s: failed to map phy regulator base\n",
  262. __func__);
  263. ret = -ENOMEM;
  264. goto fail;
  265. }
  266. fail:
  267. return ret;
  268. }
  269. static int dsi_phy_driver_probe(struct platform_device *pdev)
  270. {
  271. struct msm_dsi_phy *phy;
  272. struct device *dev = &pdev->dev;
  273. const struct of_device_id *match;
  274. int ret;
  275. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  276. if (!phy)
  277. return -ENOMEM;
  278. match = of_match_node(dsi_phy_dt_match, dev->of_node);
  279. if (!match)
  280. return -ENODEV;
  281. phy->cfg = match->data;
  282. phy->pdev = pdev;
  283. phy->id = dsi_phy_get_id(phy);
  284. if (phy->id < 0) {
  285. ret = phy->id;
  286. dev_err(dev, "%s: couldn't identify PHY index, %d\n",
  287. __func__, ret);
  288. goto fail;
  289. }
  290. phy->regulator_ldo_mode = of_property_read_bool(dev->of_node,
  291. "qcom,dsi-phy-regulator-ldo-mode");
  292. phy->base = msm_ioremap(pdev, "dsi_phy", "DSI_PHY");
  293. if (IS_ERR(phy->base)) {
  294. dev_err(dev, "%s: failed to map phy base\n", __func__);
  295. ret = -ENOMEM;
  296. goto fail;
  297. }
  298. ret = dsi_phy_regulator_init(phy);
  299. if (ret) {
  300. dev_err(dev, "%s: failed to init regulator\n", __func__);
  301. goto fail;
  302. }
  303. phy->ahb_clk = devm_clk_get(dev, "iface_clk");
  304. if (IS_ERR(phy->ahb_clk)) {
  305. dev_err(dev, "%s: Unable to get ahb clk\n", __func__);
  306. ret = PTR_ERR(phy->ahb_clk);
  307. goto fail;
  308. }
  309. if (phy->cfg->ops.init) {
  310. ret = phy->cfg->ops.init(phy);
  311. if (ret)
  312. goto fail;
  313. }
  314. /* PLL init will call into clk_register which requires
  315. * register access, so we need to enable power and ahb clock.
  316. */
  317. ret = dsi_phy_enable_resource(phy);
  318. if (ret)
  319. goto fail;
  320. phy->pll = msm_dsi_pll_init(pdev, phy->cfg->type, phy->id);
  321. if (!phy->pll)
  322. dev_info(dev,
  323. "%s: pll init failed, need separate pll clk driver\n",
  324. __func__);
  325. dsi_phy_disable_resource(phy);
  326. platform_set_drvdata(pdev, phy);
  327. return 0;
  328. fail:
  329. return ret;
  330. }
  331. static int dsi_phy_driver_remove(struct platform_device *pdev)
  332. {
  333. struct msm_dsi_phy *phy = platform_get_drvdata(pdev);
  334. if (phy && phy->pll) {
  335. msm_dsi_pll_destroy(phy->pll);
  336. phy->pll = NULL;
  337. }
  338. platform_set_drvdata(pdev, NULL);
  339. return 0;
  340. }
  341. static struct platform_driver dsi_phy_platform_driver = {
  342. .probe = dsi_phy_driver_probe,
  343. .remove = dsi_phy_driver_remove,
  344. .driver = {
  345. .name = "msm_dsi_phy",
  346. .of_match_table = dsi_phy_dt_match,
  347. },
  348. };
  349. void __init msm_dsi_phy_driver_register(void)
  350. {
  351. platform_driver_register(&dsi_phy_platform_driver);
  352. }
  353. void __exit msm_dsi_phy_driver_unregister(void)
  354. {
  355. platform_driver_unregister(&dsi_phy_platform_driver);
  356. }
  357. int msm_dsi_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
  358. struct msm_dsi_phy_clk_request *clk_req)
  359. {
  360. struct device *dev = &phy->pdev->dev;
  361. int ret;
  362. if (!phy || !phy->cfg->ops.enable)
  363. return -EINVAL;
  364. ret = dsi_phy_enable_resource(phy);
  365. if (ret) {
  366. dev_err(dev, "%s: resource enable failed, %d\n",
  367. __func__, ret);
  368. goto res_en_fail;
  369. }
  370. ret = dsi_phy_regulator_enable(phy);
  371. if (ret) {
  372. dev_err(dev, "%s: regulator enable failed, %d\n",
  373. __func__, ret);
  374. goto reg_en_fail;
  375. }
  376. ret = phy->cfg->ops.enable(phy, src_pll_id, clk_req);
  377. if (ret) {
  378. dev_err(dev, "%s: phy enable failed, %d\n", __func__, ret);
  379. goto phy_en_fail;
  380. }
  381. /*
  382. * Resetting DSI PHY silently changes its PLL registers to reset status,
  383. * which will confuse clock driver and result in wrong output rate of
  384. * link clocks. Restore PLL status if its PLL is being used as clock
  385. * source.
  386. */
  387. if (phy->usecase != MSM_DSI_PHY_SLAVE) {
  388. ret = msm_dsi_pll_restore_state(phy->pll);
  389. if (ret) {
  390. dev_err(dev, "%s: failed to restore pll state, %d\n",
  391. __func__, ret);
  392. goto pll_restor_fail;
  393. }
  394. }
  395. return 0;
  396. pll_restor_fail:
  397. if (phy->cfg->ops.disable)
  398. phy->cfg->ops.disable(phy);
  399. phy_en_fail:
  400. dsi_phy_regulator_disable(phy);
  401. reg_en_fail:
  402. dsi_phy_disable_resource(phy);
  403. res_en_fail:
  404. return ret;
  405. }
  406. void msm_dsi_phy_disable(struct msm_dsi_phy *phy)
  407. {
  408. if (!phy || !phy->cfg->ops.disable)
  409. return;
  410. /* Save PLL status if it is a clock source */
  411. if (phy->usecase != MSM_DSI_PHY_SLAVE)
  412. msm_dsi_pll_save_state(phy->pll);
  413. phy->cfg->ops.disable(phy);
  414. dsi_phy_regulator_disable(phy);
  415. dsi_phy_disable_resource(phy);
  416. }
  417. void msm_dsi_phy_get_shared_timings(struct msm_dsi_phy *phy,
  418. struct msm_dsi_phy_shared_timings *shared_timings)
  419. {
  420. memcpy(shared_timings, &phy->timing.shared_timings,
  421. sizeof(*shared_timings));
  422. }
  423. struct msm_dsi_pll *msm_dsi_phy_get_pll(struct msm_dsi_phy *phy)
  424. {
  425. if (!phy)
  426. return NULL;
  427. return phy->pll;
  428. }
  429. void msm_dsi_phy_set_usecase(struct msm_dsi_phy *phy,
  430. enum msm_dsi_phy_usecase uc)
  431. {
  432. if (phy)
  433. phy->usecase = uc;
  434. }