dsi_phy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. const unsigned long bit_rate, const unsigned long esc_rate)
  49. {
  50. s32 ui, lpx;
  51. s32 tmax, tmin;
  52. s32 pcnt0 = 10;
  53. s32 pcnt1 = (bit_rate > 1200000000) ? 15 : 10;
  54. s32 pcnt2 = 10;
  55. s32 pcnt3 = (bit_rate > 180000000) ? 10 : 40;
  56. s32 coeff = 1000; /* Precision, should avoid overflow */
  57. s32 temp;
  58. if (!bit_rate || !esc_rate)
  59. return -EINVAL;
  60. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  61. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  62. tmax = S_DIV_ROUND_UP(95 * coeff, ui) - 2;
  63. tmin = S_DIV_ROUND_UP(38 * coeff, ui) - 2;
  64. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, true);
  65. temp = lpx / ui;
  66. if (temp & 0x1)
  67. timing->hs_rqst = temp;
  68. else
  69. timing->hs_rqst = max_t(s32, 0, temp - 2);
  70. /* Calculate clk_zero after clk_prepare and hs_rqst */
  71. dsi_dphy_timing_calc_clk_zero(timing, ui, coeff, pcnt2);
  72. temp = 105 * coeff + 12 * ui - 20 * coeff;
  73. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  74. tmin = S_DIV_ROUND_UP(60 * coeff, ui) - 2;
  75. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  76. temp = 85 * coeff + 6 * ui;
  77. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  78. temp = 40 * coeff + 4 * ui;
  79. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  80. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, true);
  81. tmax = 255;
  82. temp = ((timing->hs_prepare >> 1) + 1) * 2 * ui + 2 * ui;
  83. temp = 145 * coeff + 10 * ui - temp;
  84. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  85. timing->hs_zero = linear_inter(tmax, tmin, pcnt2, 24, true);
  86. temp = 105 * coeff + 12 * ui - 20 * coeff;
  87. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  88. temp = 60 * coeff + 4 * ui;
  89. tmin = DIV_ROUND_UP(temp, ui) - 2;
  90. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  91. tmax = 255;
  92. tmin = S_DIV_ROUND_UP(100 * coeff, ui) - 2;
  93. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, true);
  94. tmax = 63;
  95. temp = ((timing->hs_exit >> 1) + 1) * 2 * ui;
  96. temp = 60 * coeff + 52 * ui - 24 * ui - temp;
  97. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  98. timing->clk_post = linear_inter(tmax, tmin, pcnt2, 0, false);
  99. tmax = 63;
  100. temp = ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  101. temp += ((timing->clk_zero >> 1) + 1) * 2 * ui;
  102. temp += 8 * ui + lpx;
  103. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  104. if (tmin > tmax) {
  105. temp = linear_inter(2 * tmax, tmin, pcnt2, 0, false);
  106. timing->clk_pre = temp >> 1;
  107. } else {
  108. timing->clk_pre = linear_inter(tmax, tmin, pcnt2, 0, false);
  109. }
  110. timing->ta_go = 3;
  111. timing->ta_sure = 0;
  112. timing->ta_get = 4;
  113. DBG("PHY timings: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  114. timing->clk_pre, timing->clk_post, timing->clk_zero,
  115. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  116. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  117. timing->hs_rqst);
  118. return 0;
  119. }
  120. void msm_dsi_phy_set_src_pll(struct msm_dsi_phy *phy, int pll_id, u32 reg,
  121. u32 bit_mask)
  122. {
  123. int phy_id = phy->id;
  124. u32 val;
  125. if ((phy_id >= DSI_MAX) || (pll_id >= DSI_MAX))
  126. return;
  127. val = dsi_phy_read(phy->base + reg);
  128. if (phy->cfg->src_pll_truthtable[phy_id][pll_id])
  129. dsi_phy_write(phy->base + reg, val | bit_mask);
  130. else
  131. dsi_phy_write(phy->base + reg, val & (~bit_mask));
  132. }
  133. static int dsi_phy_regulator_init(struct msm_dsi_phy *phy)
  134. {
  135. struct regulator_bulk_data *s = phy->supplies;
  136. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  137. struct device *dev = &phy->pdev->dev;
  138. int num = phy->cfg->reg_cfg.num;
  139. int i, ret;
  140. for (i = 0; i < num; i++)
  141. s[i].supply = regs[i].name;
  142. ret = devm_regulator_bulk_get(dev, num, s);
  143. if (ret < 0) {
  144. dev_err(dev, "%s: failed to init regulator, ret=%d\n",
  145. __func__, ret);
  146. return ret;
  147. }
  148. for (i = 0; i < num; i++) {
  149. if (regulator_can_change_voltage(s[i].consumer)) {
  150. ret = regulator_set_voltage(s[i].consumer,
  151. regs[i].min_voltage, regs[i].max_voltage);
  152. if (ret < 0) {
  153. dev_err(dev,
  154. "regulator %d set voltage failed, %d\n",
  155. i, ret);
  156. return ret;
  157. }
  158. }
  159. }
  160. return 0;
  161. }
  162. static void dsi_phy_regulator_disable(struct msm_dsi_phy *phy)
  163. {
  164. struct regulator_bulk_data *s = phy->supplies;
  165. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  166. int num = phy->cfg->reg_cfg.num;
  167. int i;
  168. DBG("");
  169. for (i = num - 1; i >= 0; i--)
  170. if (regs[i].disable_load >= 0)
  171. regulator_set_load(s[i].consumer, regs[i].disable_load);
  172. regulator_bulk_disable(num, s);
  173. }
  174. static int dsi_phy_regulator_enable(struct msm_dsi_phy *phy)
  175. {
  176. struct regulator_bulk_data *s = phy->supplies;
  177. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  178. struct device *dev = &phy->pdev->dev;
  179. int num = phy->cfg->reg_cfg.num;
  180. int ret, i;
  181. DBG("");
  182. for (i = 0; i < num; i++) {
  183. if (regs[i].enable_load >= 0) {
  184. ret = regulator_set_load(s[i].consumer,
  185. regs[i].enable_load);
  186. if (ret < 0) {
  187. dev_err(dev,
  188. "regulator %d set op mode failed, %d\n",
  189. i, ret);
  190. goto fail;
  191. }
  192. }
  193. }
  194. ret = regulator_bulk_enable(num, s);
  195. if (ret < 0) {
  196. dev_err(dev, "regulator enable failed, %d\n", ret);
  197. goto fail;
  198. }
  199. return 0;
  200. fail:
  201. for (i--; i >= 0; i--)
  202. regulator_set_load(s[i].consumer, regs[i].disable_load);
  203. return ret;
  204. }
  205. static int dsi_phy_enable_resource(struct msm_dsi_phy *phy)
  206. {
  207. struct device *dev = &phy->pdev->dev;
  208. int ret;
  209. pm_runtime_get_sync(dev);
  210. ret = clk_prepare_enable(phy->ahb_clk);
  211. if (ret) {
  212. dev_err(dev, "%s: can't enable ahb clk, %d\n", __func__, ret);
  213. pm_runtime_put_sync(dev);
  214. }
  215. return ret;
  216. }
  217. static void dsi_phy_disable_resource(struct msm_dsi_phy *phy)
  218. {
  219. clk_disable_unprepare(phy->ahb_clk);
  220. pm_runtime_put_sync(&phy->pdev->dev);
  221. }
  222. static const struct of_device_id dsi_phy_dt_match[] = {
  223. #ifdef CONFIG_DRM_MSM_DSI_28NM_PHY
  224. { .compatible = "qcom,dsi-phy-28nm-hpm",
  225. .data = &dsi_phy_28nm_hpm_cfgs },
  226. { .compatible = "qcom,dsi-phy-28nm-lp",
  227. .data = &dsi_phy_28nm_lp_cfgs },
  228. #endif
  229. #ifdef CONFIG_DRM_MSM_DSI_20NM_PHY
  230. { .compatible = "qcom,dsi-phy-20nm",
  231. .data = &dsi_phy_20nm_cfgs },
  232. #endif
  233. {}
  234. };
  235. static int dsi_phy_driver_probe(struct platform_device *pdev)
  236. {
  237. struct msm_dsi_phy *phy;
  238. struct device *dev = &pdev->dev;
  239. const struct of_device_id *match;
  240. int ret;
  241. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  242. if (!phy)
  243. return -ENOMEM;
  244. match = of_match_node(dsi_phy_dt_match, dev->of_node);
  245. if (!match)
  246. return -ENODEV;
  247. phy->cfg = match->data;
  248. phy->pdev = pdev;
  249. ret = of_property_read_u32(dev->of_node,
  250. "qcom,dsi-phy-index", &phy->id);
  251. if (ret) {
  252. dev_err(dev, "%s: PHY index not specified, %d\n",
  253. __func__, ret);
  254. goto fail;
  255. }
  256. phy->regulator_ldo_mode = of_property_read_bool(dev->of_node,
  257. "qcom,dsi-phy-regulator-ldo-mode");
  258. phy->base = msm_ioremap(pdev, "dsi_phy", "DSI_PHY");
  259. if (IS_ERR(phy->base)) {
  260. dev_err(dev, "%s: failed to map phy base\n", __func__);
  261. ret = -ENOMEM;
  262. goto fail;
  263. }
  264. phy->reg_base = msm_ioremap(pdev, "dsi_phy_regulator",
  265. "DSI_PHY_REG");
  266. if (IS_ERR(phy->reg_base)) {
  267. dev_err(dev, "%s: failed to map phy regulator base\n",
  268. __func__);
  269. ret = -ENOMEM;
  270. goto fail;
  271. }
  272. ret = dsi_phy_regulator_init(phy);
  273. if (ret) {
  274. dev_err(dev, "%s: failed to init regulator\n", __func__);
  275. goto fail;
  276. }
  277. phy->ahb_clk = devm_clk_get(dev, "iface_clk");
  278. if (IS_ERR(phy->ahb_clk)) {
  279. dev_err(dev, "%s: Unable to get ahb clk\n", __func__);
  280. ret = PTR_ERR(phy->ahb_clk);
  281. goto fail;
  282. }
  283. /* PLL init will call into clk_register which requires
  284. * register access, so we need to enable power and ahb clock.
  285. */
  286. ret = dsi_phy_enable_resource(phy);
  287. if (ret)
  288. goto fail;
  289. phy->pll = msm_dsi_pll_init(pdev, phy->cfg->type, phy->id);
  290. if (!phy->pll)
  291. dev_info(dev,
  292. "%s: pll init failed, need separate pll clk driver\n",
  293. __func__);
  294. dsi_phy_disable_resource(phy);
  295. platform_set_drvdata(pdev, phy);
  296. return 0;
  297. fail:
  298. return ret;
  299. }
  300. static int dsi_phy_driver_remove(struct platform_device *pdev)
  301. {
  302. struct msm_dsi_phy *phy = platform_get_drvdata(pdev);
  303. if (phy && phy->pll) {
  304. msm_dsi_pll_destroy(phy->pll);
  305. phy->pll = NULL;
  306. }
  307. platform_set_drvdata(pdev, NULL);
  308. return 0;
  309. }
  310. static struct platform_driver dsi_phy_platform_driver = {
  311. .probe = dsi_phy_driver_probe,
  312. .remove = dsi_phy_driver_remove,
  313. .driver = {
  314. .name = "msm_dsi_phy",
  315. .of_match_table = dsi_phy_dt_match,
  316. },
  317. };
  318. void __init msm_dsi_phy_driver_register(void)
  319. {
  320. platform_driver_register(&dsi_phy_platform_driver);
  321. }
  322. void __exit msm_dsi_phy_driver_unregister(void)
  323. {
  324. platform_driver_unregister(&dsi_phy_platform_driver);
  325. }
  326. int msm_dsi_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
  327. const unsigned long bit_rate, const unsigned long esc_rate)
  328. {
  329. struct device *dev = &phy->pdev->dev;
  330. int ret;
  331. if (!phy || !phy->cfg->ops.enable)
  332. return -EINVAL;
  333. ret = dsi_phy_regulator_enable(phy);
  334. if (ret) {
  335. dev_err(dev, "%s: regulator enable failed, %d\n",
  336. __func__, ret);
  337. return ret;
  338. }
  339. ret = phy->cfg->ops.enable(phy, src_pll_id, bit_rate, esc_rate);
  340. if (ret) {
  341. dev_err(dev, "%s: phy enable failed, %d\n", __func__, ret);
  342. dsi_phy_regulator_disable(phy);
  343. return ret;
  344. }
  345. return 0;
  346. }
  347. void msm_dsi_phy_disable(struct msm_dsi_phy *phy)
  348. {
  349. if (!phy || !phy->cfg->ops.disable)
  350. return;
  351. phy->cfg->ops.disable(phy);
  352. dsi_phy_regulator_disable(phy);
  353. }
  354. void msm_dsi_phy_get_clk_pre_post(struct msm_dsi_phy *phy,
  355. u32 *clk_pre, u32 *clk_post)
  356. {
  357. if (!phy)
  358. return;
  359. if (clk_pre)
  360. *clk_pre = phy->timing.clk_pre;
  361. if (clk_post)
  362. *clk_post = phy->timing.clk_post;
  363. }
  364. struct msm_dsi_pll *msm_dsi_phy_get_pll(struct msm_dsi_phy *phy)
  365. {
  366. if (!phy)
  367. return NULL;
  368. return phy->pll;
  369. }