dsi_io_v2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <mach/clk.h>
  20. #include "dsi_v2.h"
  21. #include "dsi_io_v2.h"
  22. #include "dsi_host_v2.h"
  23. struct msm_dsi_io_private {
  24. struct clk *dsi_byte_clk;
  25. struct clk *dsi_esc_clk;
  26. struct clk *dsi_pixel_clk;
  27. struct clk *dsi_ahb_clk;
  28. struct clk *dsi_clk;
  29. int msm_dsi_clk_on;
  30. int msm_dsi_ahb_clk_on;
  31. };
  32. static struct msm_dsi_io_private *dsi_io_private;
  33. #define DSI_VDDA_VOLTAGE 1200000
  34. void msm_dsi_ahb_ctrl(int enable)
  35. {
  36. if (enable) {
  37. dsi_io_private->msm_dsi_ahb_clk_on++;
  38. if (dsi_io_private->msm_dsi_ahb_clk_on == 1)
  39. clk_enable(dsi_io_private->dsi_ahb_clk);
  40. } else {
  41. dsi_io_private->msm_dsi_ahb_clk_on--;
  42. if (dsi_io_private->msm_dsi_ahb_clk_on == 0)
  43. clk_disable(dsi_io_private->dsi_ahb_clk);
  44. }
  45. }
  46. int msm_dsi_io_init(struct platform_device *pdev, struct dss_module_power *mp)
  47. {
  48. int rc;
  49. if (!dsi_io_private) {
  50. dsi_io_private = kzalloc(sizeof(struct msm_dsi_io_private),
  51. GFP_KERNEL);
  52. if (!dsi_io_private) {
  53. pr_err("fail to alloc dsi io private data structure\n");
  54. return -ENOMEM;
  55. }
  56. }
  57. rc = msm_dsi_clk_init(pdev);
  58. if (rc) {
  59. pr_err("fail to initialize DSI clock\n");
  60. return rc;
  61. }
  62. rc = msm_dss_config_vreg(&pdev->dev, mp->vreg_config,
  63. mp->num_vreg, 1);
  64. if (rc) {
  65. pr_err("fail to initialize DSI regulator\n");
  66. return rc;
  67. }
  68. return 0;
  69. }
  70. void msm_dsi_io_deinit(struct platform_device *pdev,
  71. struct dss_module_power *mp)
  72. {
  73. if (dsi_io_private) {
  74. msm_dsi_clk_deinit();
  75. msm_dss_config_vreg(&pdev->dev, mp->vreg_config,
  76. mp->num_vreg, 0);
  77. kfree(dsi_io_private);
  78. dsi_io_private = NULL;
  79. }
  80. }
  81. int msm_dsi_clk_init(struct platform_device *dev)
  82. {
  83. int rc = 0;
  84. dsi_io_private->dsi_clk = clk_get(&dev->dev, "dsi_clk");
  85. if (IS_ERR(dsi_io_private->dsi_clk)) {
  86. pr_err("can't find dsi core_clk\n");
  87. rc = PTR_ERR(dsi_io_private->dsi_clk);
  88. dsi_io_private->dsi_clk = NULL;
  89. return rc;
  90. }
  91. dsi_io_private->dsi_byte_clk = clk_get(&dev->dev, "byte_clk");
  92. if (IS_ERR(dsi_io_private->dsi_byte_clk)) {
  93. pr_err("can't find dsi byte_clk\n");
  94. rc = PTR_ERR(dsi_io_private->dsi_byte_clk);
  95. dsi_io_private->dsi_byte_clk = NULL;
  96. return rc;
  97. }
  98. dsi_io_private->dsi_esc_clk = clk_get(&dev->dev, "esc_clk");
  99. if (IS_ERR(dsi_io_private->dsi_esc_clk)) {
  100. pr_err("can't find dsi esc_clk\n");
  101. rc = PTR_ERR(dsi_io_private->dsi_esc_clk);
  102. dsi_io_private->dsi_esc_clk = NULL;
  103. return rc;
  104. }
  105. dsi_io_private->dsi_pixel_clk = clk_get(&dev->dev, "pixel_clk");
  106. if (IS_ERR(dsi_io_private->dsi_pixel_clk)) {
  107. pr_err("can't find dsi pixel\n");
  108. rc = PTR_ERR(dsi_io_private->dsi_pixel_clk);
  109. dsi_io_private->dsi_pixel_clk = NULL;
  110. return rc;
  111. }
  112. dsi_io_private->dsi_ahb_clk = clk_get(&dev->dev, "iface_clk");
  113. if (IS_ERR(dsi_io_private->dsi_ahb_clk)) {
  114. pr_err("can't find dsi iface_clk\n");
  115. rc = PTR_ERR(dsi_io_private->dsi_ahb_clk);
  116. dsi_io_private->dsi_ahb_clk = NULL;
  117. return rc;
  118. }
  119. clk_prepare(dsi_io_private->dsi_ahb_clk);
  120. return 0;
  121. }
  122. void msm_dsi_clk_deinit(void)
  123. {
  124. if (dsi_io_private->dsi_clk) {
  125. clk_put(dsi_io_private->dsi_clk);
  126. dsi_io_private->dsi_clk = NULL;
  127. }
  128. if (dsi_io_private->dsi_byte_clk) {
  129. clk_put(dsi_io_private->dsi_byte_clk);
  130. dsi_io_private->dsi_byte_clk = NULL;
  131. }
  132. if (dsi_io_private->dsi_esc_clk) {
  133. clk_put(dsi_io_private->dsi_esc_clk);
  134. dsi_io_private->dsi_esc_clk = NULL;
  135. }
  136. if (dsi_io_private->dsi_pixel_clk) {
  137. clk_put(dsi_io_private->dsi_pixel_clk);
  138. dsi_io_private->dsi_pixel_clk = NULL;
  139. }
  140. if (dsi_io_private->dsi_ahb_clk) {
  141. clk_unprepare(dsi_io_private->dsi_ahb_clk);
  142. clk_put(dsi_io_private->dsi_ahb_clk);
  143. dsi_io_private->dsi_ahb_clk = NULL;
  144. }
  145. }
  146. int msm_dsi_prepare_clocks(void)
  147. {
  148. clk_prepare(dsi_io_private->dsi_clk);
  149. clk_prepare(dsi_io_private->dsi_byte_clk);
  150. clk_prepare(dsi_io_private->dsi_esc_clk);
  151. clk_prepare(dsi_io_private->dsi_pixel_clk);
  152. return 0;
  153. }
  154. int msm_dsi_unprepare_clocks(void)
  155. {
  156. clk_unprepare(dsi_io_private->dsi_clk);
  157. clk_unprepare(dsi_io_private->dsi_esc_clk);
  158. clk_unprepare(dsi_io_private->dsi_byte_clk);
  159. clk_unprepare(dsi_io_private->dsi_pixel_clk);
  160. return 0;
  161. }
  162. int msm_dsi_clk_set_rate(unsigned long esc_rate,
  163. unsigned long dsi_rate,
  164. unsigned long byte_rate,
  165. unsigned long pixel_rate)
  166. {
  167. int rc;
  168. rc = clk_set_rate(dsi_io_private->dsi_clk, dsi_rate);
  169. if (rc) {
  170. pr_err("dsi_esc_clk - clk_set_rate failed =%d\n", rc);
  171. return rc;
  172. }
  173. rc = clk_set_rate(dsi_io_private->dsi_esc_clk, esc_rate);
  174. if (rc) {
  175. pr_err("dsi_esc_clk - clk_set_rate failed =%d\n", rc);
  176. return rc;
  177. }
  178. rc = clk_set_rate(dsi_io_private->dsi_byte_clk, byte_rate);
  179. if (rc) {
  180. pr_err("dsi_byte_clk - clk_set_rate faile = %dd\n", rc);
  181. return rc;
  182. }
  183. rc = clk_set_rate(dsi_io_private->dsi_pixel_clk, pixel_rate);
  184. if (rc) {
  185. pr_err("dsi_pixel_clk - clk_set_rate failed = %d\n", rc);
  186. return rc;
  187. }
  188. return 0;
  189. }
  190. int msm_dsi_clk_enable(void)
  191. {
  192. if (dsi_io_private->msm_dsi_clk_on) {
  193. pr_debug("dsi_clks on already\n");
  194. return 0;
  195. }
  196. clk_enable(dsi_io_private->dsi_clk);
  197. clk_enable(dsi_io_private->dsi_esc_clk);
  198. clk_enable(dsi_io_private->dsi_byte_clk);
  199. clk_enable(dsi_io_private->dsi_pixel_clk);
  200. dsi_io_private->msm_dsi_clk_on = 1;
  201. return 0;
  202. }
  203. int msm_dsi_clk_disable(void)
  204. {
  205. if (dsi_io_private->msm_dsi_clk_on == 0) {
  206. pr_debug("mdss_dsi_clks already OFF\n");
  207. return 0;
  208. }
  209. clk_disable(dsi_io_private->dsi_clk);
  210. clk_disable(dsi_io_private->dsi_byte_clk);
  211. clk_disable(dsi_io_private->dsi_esc_clk);
  212. clk_disable(dsi_io_private->dsi_pixel_clk);
  213. dsi_io_private->msm_dsi_clk_on = 0;
  214. return 0;
  215. }
  216. static void msm_dsi_phy_strength_init(unsigned char *ctrl_base,
  217. struct mdss_dsi_phy_ctrl *pd)
  218. {
  219. MIPI_OUTP(ctrl_base + DSI_DSIPHY_STRENGTH_CTRL_0, pd->strength[0]);
  220. MIPI_OUTP(ctrl_base + DSI_DSIPHY_STRENGTH_CTRL_2, pd->strength[1]);
  221. }
  222. static void msm_dsi_phy_ctrl_init(unsigned char *ctrl_base,
  223. struct mdss_panel_data *pdata)
  224. {
  225. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CTRL_0, 0x5f);
  226. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CTRL_3, 0x10);
  227. }
  228. static void msm_dsi_phy_regulator_init(unsigned char *ctrl_base,
  229. struct mdss_dsi_phy_ctrl *pd)
  230. {
  231. MIPI_OUTP(ctrl_base + DSI_DSIPHY_LDO_CNTRL, 0x25);
  232. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CTRL_0, pd->regulator[0]);
  233. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CTRL_1, pd->regulator[1]);
  234. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CTRL_2, pd->regulator[2]);
  235. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CTRL_3, pd->regulator[3]);
  236. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CTRL_4, pd->regulator[4]);
  237. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CAL_PWR_CFG,
  238. pd->regulator[5]);
  239. }
  240. static int msm_dsi_phy_calibration(unsigned char *ctrl_base)
  241. {
  242. int i = 0, term_cnt = 5000, ret = 0, cal_busy;
  243. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_SW_CFG2, 0x0);
  244. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_HW_CFG1, 0x5a);
  245. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_HW_CFG3, 0x10);
  246. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_HW_CFG4, 0x01);
  247. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_HW_CFG0, 0x01);
  248. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_HW_TRIGGER, 0x01);
  249. usleep_range(5000, 5000); /*per DSI controller spec*/
  250. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CAL_HW_TRIGGER, 0x00);
  251. cal_busy = MIPI_INP(ctrl_base + DSI_DSIPHY_REGULATOR_CAL_STATUS0);
  252. while (cal_busy & 0x10) {
  253. i++;
  254. if (i > term_cnt) {
  255. ret = -EINVAL;
  256. pr_err("msm_dsi_phy_calibration error\n");
  257. break;
  258. }
  259. cal_busy = MIPI_INP(ctrl_base +
  260. DSI_DSIPHY_REGULATOR_CAL_STATUS0);
  261. }
  262. return ret;
  263. }
  264. static void msm_dsi_phy_lane_init(unsigned char *ctrl_base,
  265. struct mdss_dsi_phy_ctrl *pd)
  266. {
  267. int ln, index;
  268. /*CFG0, CFG1, CFG2, TEST_DATAPATH, TEST_STR0, TEST_STR1*/
  269. for (ln = 0; ln < 5; ln++) {
  270. unsigned char *off = ctrl_base + 0x0300 + (ln * 0x40);
  271. index = ln * 6;
  272. MIPI_OUTP(off, pd->lanecfg[index]);
  273. MIPI_OUTP(off + 4, pd->lanecfg[index + 1]);
  274. MIPI_OUTP(off + 8, pd->lanecfg[index + 2]);
  275. MIPI_OUTP(off + 12, pd->lanecfg[index + 3]);
  276. MIPI_OUTP(off + 20, pd->lanecfg[index + 4]);
  277. MIPI_OUTP(off + 24, pd->lanecfg[index + 5]);
  278. }
  279. wmb();
  280. }
  281. static void msm_dsi_phy_timing_init(unsigned char *ctrl_base,
  282. struct mdss_dsi_phy_ctrl *pd)
  283. {
  284. int i, off = DSI_DSIPHY_TIMING_CTRL_0;
  285. for (i = 0; i < 12; i++) {
  286. MIPI_OUTP(ctrl_base + off, pd->timing[i]);
  287. off += 4;
  288. }
  289. wmb();
  290. }
  291. static void msm_dsi_phy_bist_init(unsigned char *ctrl_base,
  292. struct mdss_dsi_phy_ctrl *pd)
  293. {
  294. MIPI_OUTP(ctrl_base + DSI_DSIPHY_BIST_CTRL4, pd->bistctrl[4]);
  295. MIPI_OUTP(ctrl_base + DSI_DSIPHY_BIST_CTRL1, pd->bistctrl[1]);
  296. MIPI_OUTP(ctrl_base + DSI_DSIPHY_BIST_CTRL0, pd->bistctrl[0]);
  297. MIPI_OUTP(ctrl_base + DSI_DSIPHY_BIST_CTRL4, 0);
  298. wmb();
  299. }
  300. int msm_dsi_phy_init(unsigned char *ctrl_base,
  301. struct mdss_panel_data *pdata)
  302. {
  303. struct mdss_dsi_phy_ctrl *pd;
  304. pd = &(pdata->panel_info.mipi.dsi_phy_db);
  305. msm_dsi_phy_strength_init(ctrl_base, pd);
  306. msm_dsi_phy_ctrl_init(ctrl_base, pdata);
  307. msm_dsi_phy_regulator_init(ctrl_base, pd);
  308. msm_dsi_phy_calibration(ctrl_base);
  309. msm_dsi_phy_lane_init(ctrl_base, pd);
  310. msm_dsi_phy_timing_init(ctrl_base, pd);
  311. msm_dsi_phy_bist_init(ctrl_base, pd);
  312. return 0;
  313. }
  314. void msm_dsi_phy_sw_reset(unsigned char *ctrl_base)
  315. {
  316. /* start phy sw reset */
  317. MIPI_OUTP(ctrl_base + DSI_PHY_SW_RESET, 0x0001);
  318. udelay(1000); /*per DSI controller spec*/
  319. wmb();
  320. /* end phy sw reset */
  321. MIPI_OUTP(ctrl_base + DSI_PHY_SW_RESET, 0x0000);
  322. udelay(100); /*per DSI controller spec*/
  323. wmb();
  324. }
  325. void msm_dsi_phy_off(unsigned char *ctrl_base)
  326. {
  327. MIPI_OUTP(ctrl_base + DSI_DSIPHY_PLL_CTRL_5, 0x05f);
  328. MIPI_OUTP(ctrl_base + DSI_DSIPHY_REGULATOR_CTRL_0, 0x02);
  329. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CTRL_0, 0x00);
  330. MIPI_OUTP(ctrl_base + DSI_DSIPHY_CTRL_1, 0x7f);
  331. MIPI_OUTP(ctrl_base + DSI_CLK_CTRL, 0);
  332. }