rk_mipi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
  4. * Author: Eric Gao <eric.gao@rock-chips.com>
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <display.h>
  9. #include <dm.h>
  10. #include <fdtdec.h>
  11. #include <panel.h>
  12. #include <regmap.h>
  13. #include "rk_mipi.h"
  14. #include <syscon.h>
  15. #include <asm/gpio.h>
  16. #include <asm/hardware.h>
  17. #include <asm/io.h>
  18. #include <dm/uclass-internal.h>
  19. #include <linux/kernel.h>
  20. #include <asm/arch/clock.h>
  21. #include <asm/arch/cru_rk3399.h>
  22. #include <asm/arch/grf_rk3399.h>
  23. #include <asm/arch/rockchip_mipi_dsi.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. int rk_mipi_read_timing(struct udevice *dev,
  26. struct display_timing *timing)
  27. {
  28. int ret;
  29. ret = fdtdec_decode_display_timing(gd->fdt_blob, dev_of_offset(dev),
  30. 0, timing);
  31. if (ret) {
  32. debug("%s: Failed to decode display timing (ret=%d)\n",
  33. __func__, ret);
  34. return -EINVAL;
  35. }
  36. return 0;
  37. }
  38. /*
  39. * Register write function used only for mipi dsi controller.
  40. * Parameter:
  41. * @regs: mipi controller address
  42. * @reg: combination of regaddr(16bit)|bitswidth(8bit)|offset(8bit) you can
  43. * use define in rk_mipi.h directly for this parameter
  44. * @val: value that will be write to specified bits of register
  45. */
  46. static void rk_mipi_dsi_write(uintptr_t regs, u32 reg, u32 val)
  47. {
  48. u32 dat;
  49. u32 mask;
  50. u32 offset = (reg >> OFFSET_SHIFT) & 0xff;
  51. u32 bits = (reg >> BITS_SHIFT) & 0xff;
  52. uintptr_t addr = (reg >> ADDR_SHIFT) + regs;
  53. /* Mask for specifiled bits,the corresponding bits will be clear */
  54. mask = ~((0xffffffff << offset) & (0xffffffff >> (32 - offset - bits)));
  55. /* Make sure val in the available range */
  56. val &= ~(0xffffffff << bits);
  57. /* Get register's original val */
  58. dat = readl(addr);
  59. /* Clear specified bits */
  60. dat &= mask;
  61. /* Fill specified bits */
  62. dat |= val << offset;
  63. writel(dat, addr);
  64. }
  65. int rk_mipi_dsi_enable(struct udevice *dev,
  66. const struct display_timing *timing)
  67. {
  68. int node, timing_node;
  69. int val;
  70. struct rk_mipi_priv *priv = dev_get_priv(dev);
  71. uintptr_t regs = priv->regs;
  72. u32 txbyte_clk = priv->txbyte_clk;
  73. u32 txesc_clk = priv->txesc_clk;
  74. txesc_clk = txbyte_clk/(txbyte_clk/txesc_clk + 1);
  75. /* Set Display timing parameter */
  76. rk_mipi_dsi_write(regs, VID_HSA_TIME, timing->hsync_len.typ);
  77. rk_mipi_dsi_write(regs, VID_HBP_TIME, timing->hback_porch.typ);
  78. rk_mipi_dsi_write(regs, VID_HLINE_TIME, (timing->hsync_len.typ
  79. + timing->hback_porch.typ + timing->hactive.typ
  80. + timing->hfront_porch.typ));
  81. rk_mipi_dsi_write(regs, VID_VSA_LINES, timing->vsync_len.typ);
  82. rk_mipi_dsi_write(regs, VID_VBP_LINES, timing->vback_porch.typ);
  83. rk_mipi_dsi_write(regs, VID_VFP_LINES, timing->vfront_porch.typ);
  84. rk_mipi_dsi_write(regs, VID_ACTIVE_LINES, timing->vactive.typ);
  85. /* Set Signal Polarity */
  86. val = (timing->flags & DISPLAY_FLAGS_HSYNC_LOW) ? 1 : 0;
  87. rk_mipi_dsi_write(regs, HSYNC_ACTIVE_LOW, val);
  88. val = (timing->flags & DISPLAY_FLAGS_VSYNC_LOW) ? 1 : 0;
  89. rk_mipi_dsi_write(regs, VSYNC_ACTIVE_LOW, val);
  90. val = (timing->flags & DISPLAY_FLAGS_DE_LOW) ? 1 : 0;
  91. rk_mipi_dsi_write(regs, DISPLAY_FLAGS_DE_LOW, val);
  92. val = (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) ? 1 : 0;
  93. rk_mipi_dsi_write(regs, COLORM_ACTIVE_LOW, val);
  94. /* Set video mode */
  95. rk_mipi_dsi_write(regs, CMD_VIDEO_MODE, VIDEO_MODE);
  96. /* Set video mode transmission type as burst mode */
  97. rk_mipi_dsi_write(regs, VID_MODE_TYPE, BURST_MODE);
  98. /* Set pix num in a video package */
  99. rk_mipi_dsi_write(regs, VID_PKT_SIZE, 0x4b0);
  100. /* Set dpi color coding depth 24 bit */
  101. timing_node = fdt_subnode_offset(gd->fdt_blob, dev_of_offset(dev),
  102. "display-timings");
  103. node = fdt_first_subnode(gd->fdt_blob, timing_node);
  104. val = fdtdec_get_int(gd->fdt_blob, node, "bits-per-pixel", -1);
  105. switch (val) {
  106. case 16:
  107. rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_16BIT_CFG_1);
  108. break;
  109. case 24:
  110. rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT);
  111. break;
  112. case 30:
  113. rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_30BIT);
  114. break;
  115. default:
  116. rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT);
  117. }
  118. /* Enable low power mode */
  119. rk_mipi_dsi_write(regs, LP_CMD_EN, 1);
  120. rk_mipi_dsi_write(regs, LP_HFP_EN, 1);
  121. rk_mipi_dsi_write(regs, LP_VACT_EN, 1);
  122. rk_mipi_dsi_write(regs, LP_VFP_EN, 1);
  123. rk_mipi_dsi_write(regs, LP_VBP_EN, 1);
  124. rk_mipi_dsi_write(regs, LP_VSA_EN, 1);
  125. /* Division for timeout counter clk */
  126. rk_mipi_dsi_write(regs, TO_CLK_DIVISION, 0x0a);
  127. /* Tx esc clk division from txbyte clk */
  128. rk_mipi_dsi_write(regs, TX_ESC_CLK_DIVISION, txbyte_clk/txesc_clk);
  129. /* Timeout count for hs<->lp transation between Line period */
  130. rk_mipi_dsi_write(regs, HSTX_TO_CNT, 0x3e8);
  131. /* Phy State transfer timing */
  132. rk_mipi_dsi_write(regs, PHY_STOP_WAIT_TIME, 32);
  133. rk_mipi_dsi_write(regs, PHY_TXREQUESTCLKHS, 1);
  134. rk_mipi_dsi_write(regs, PHY_HS2LP_TIME, 0x14);
  135. rk_mipi_dsi_write(regs, PHY_LP2HS_TIME, 0x10);
  136. rk_mipi_dsi_write(regs, MAX_RD_TIME, 0x2710);
  137. /* Power on */
  138. rk_mipi_dsi_write(regs, SHUTDOWNZ, 1);
  139. return 0;
  140. }
  141. /* rk mipi dphy write function. It is used to write test data to dphy */
  142. static void rk_mipi_phy_write(uintptr_t regs, unsigned char test_code,
  143. unsigned char *test_data, unsigned char size)
  144. {
  145. int i = 0;
  146. /* Write Test code */
  147. rk_mipi_dsi_write(regs, PHY_TESTCLK, 1);
  148. rk_mipi_dsi_write(regs, PHY_TESTDIN, test_code);
  149. rk_mipi_dsi_write(regs, PHY_TESTEN, 1);
  150. rk_mipi_dsi_write(regs, PHY_TESTCLK, 0);
  151. rk_mipi_dsi_write(regs, PHY_TESTEN, 0);
  152. /* Write Test data */
  153. for (i = 0; i < size; i++) {
  154. rk_mipi_dsi_write(regs, PHY_TESTCLK, 0);
  155. rk_mipi_dsi_write(regs, PHY_TESTDIN, test_data[i]);
  156. rk_mipi_dsi_write(regs, PHY_TESTCLK, 1);
  157. }
  158. }
  159. /*
  160. * Mipi dphy config function. Calculate the suitable prediv, feedback div,
  161. * fsfreqrang value ,cap ,lpf and so on according to the given pix clk rate,
  162. * and then enable phy.
  163. */
  164. int rk_mipi_phy_enable(struct udevice *dev)
  165. {
  166. int i;
  167. struct rk_mipi_priv *priv = dev_get_priv(dev);
  168. uintptr_t regs = priv->regs;
  169. u64 fbdiv;
  170. u64 prediv = 1;
  171. u32 max_fbdiv = 512;
  172. u32 max_prediv, min_prediv;
  173. u64 ddr_clk = priv->phy_clk;
  174. u32 refclk = priv->ref_clk;
  175. u32 remain = refclk;
  176. unsigned char test_data[2] = {0};
  177. int freq_rang[][2] = {
  178. {90, 0x01}, {100, 0x10}, {110, 0x20}, {130, 0x01},
  179. {140, 0x11}, {150, 0x21}, {170, 0x02}, {180, 0x12},
  180. {200, 0x22}, {220, 0x03}, {240, 0x13}, {250, 0x23},
  181. {270, 0x04}, {300, 0x14}, {330, 0x05}, {360, 0x15},
  182. {400, 0x25}, {450, 0x06}, {500, 0x16}, {550, 0x07},
  183. {600, 0x17}, {650, 0x08}, {700, 0x18}, {750, 0x09},
  184. {800, 0x19}, {850, 0x29}, {900, 0x39}, {950, 0x0a},
  185. {1000, 0x1a}, {1050, 0x2a}, {1100, 0x3a}, {1150, 0x0b},
  186. {1200, 0x1b}, {1250, 0x2b}, {1300, 0x3b}, {1350, 0x0c},
  187. {1400, 0x1c}, {1450, 0x2c}, {1500, 0x3c}
  188. };
  189. /* Shutdown mode */
  190. rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 0);
  191. rk_mipi_dsi_write(regs, PHY_RSTZ, 0);
  192. rk_mipi_dsi_write(regs, PHY_TESTCLR, 1);
  193. /* Pll locking */
  194. rk_mipi_dsi_write(regs, PHY_TESTCLR, 0);
  195. /* config cp and lfp */
  196. test_data[0] = 0x80 | (ddr_clk / (200 * MHz)) << 3 | 0x3;
  197. rk_mipi_phy_write(regs, CODE_PLL_VCORANGE_VCOCAP, test_data, 1);
  198. test_data[0] = 0x8;
  199. rk_mipi_phy_write(regs, CODE_PLL_CPCTRL, test_data, 1);
  200. test_data[0] = 0x80 | 0x40;
  201. rk_mipi_phy_write(regs, CODE_PLL_LPF_CP, test_data, 1);
  202. /* select the suitable value for fsfreqrang reg */
  203. for (i = 0; i < ARRAY_SIZE(freq_rang); i++) {
  204. if (ddr_clk / (MHz) >= freq_rang[i][0])
  205. break;
  206. }
  207. if (i == ARRAY_SIZE(freq_rang)) {
  208. debug("%s: Dphy freq out of range!\n", __func__);
  209. return -EINVAL;
  210. }
  211. test_data[0] = freq_rang[i][1] << 1;
  212. rk_mipi_phy_write(regs, CODE_HS_RX_LANE0, test_data, 1);
  213. /*
  214. * Calculate the best ddrclk and it's corresponding div value. If the
  215. * given pixelclock is great than 250M, ddrclk will be fix 1500M.
  216. * Otherwise,
  217. * it's equal to ddr_clk= pixclk * 6. 40MHz >= refclk / prediv >= 5MHz
  218. * according to spec.
  219. */
  220. max_prediv = (refclk / (5 * MHz));
  221. min_prediv = ((refclk / (40 * MHz)) ? (refclk / (40 * MHz) + 1) : 1);
  222. debug("%s: DEBUG: max_prediv=%u, min_prediv=%u\n", __func__, max_prediv,
  223. min_prediv);
  224. if (max_prediv < min_prediv) {
  225. debug("%s: Invalid refclk value\n", __func__);
  226. return -EINVAL;
  227. }
  228. /* Calculate the best refclk and feedback division value for dphy pll */
  229. for (i = min_prediv; i < max_prediv; i++) {
  230. if ((ddr_clk * i % refclk < remain) &&
  231. (ddr_clk * i / refclk) < max_fbdiv) {
  232. prediv = i;
  233. remain = ddr_clk * i % refclk;
  234. }
  235. }
  236. fbdiv = ddr_clk * prediv / refclk;
  237. ddr_clk = refclk * fbdiv / prediv;
  238. priv->phy_clk = ddr_clk;
  239. debug("%s: DEBUG: refclk=%u, refclk=%llu, fbdiv=%llu, phyclk=%llu\n",
  240. __func__, refclk, prediv, fbdiv, ddr_clk);
  241. /* config prediv and feedback reg */
  242. test_data[0] = prediv - 1;
  243. rk_mipi_phy_write(regs, CODE_PLL_INPUT_DIV_RAT, test_data, 1);
  244. test_data[0] = (fbdiv - 1) & 0x1f;
  245. rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1);
  246. test_data[0] = (fbdiv - 1) >> 5 | 0x80;
  247. rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1);
  248. test_data[0] = 0x30;
  249. rk_mipi_phy_write(regs, CODE_PLL_INPUT_LOOP_DIV_RAT, test_data, 1);
  250. /* rest config */
  251. test_data[0] = 0x4d;
  252. rk_mipi_phy_write(regs, CODE_BANDGAP_BIAS_CTRL, test_data, 1);
  253. test_data[0] = 0x3d;
  254. rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1);
  255. test_data[0] = 0xdf;
  256. rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1);
  257. test_data[0] = 0x7;
  258. rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1);
  259. test_data[0] = 0x80 | 0x7;
  260. rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1);
  261. test_data[0] = 0x80 | 15;
  262. rk_mipi_phy_write(regs, CODE_HSTXDATALANEREQUSETSTATETIME,
  263. test_data, 1);
  264. test_data[0] = 0x80 | 85;
  265. rk_mipi_phy_write(regs, CODE_HSTXDATALANEPREPARESTATETIME,
  266. test_data, 1);
  267. test_data[0] = 0x40 | 10;
  268. rk_mipi_phy_write(regs, CODE_HSTXDATALANEHSZEROSTATETIME,
  269. test_data, 1);
  270. /* enter into stop mode */
  271. rk_mipi_dsi_write(regs, N_LANES, 0x03);
  272. rk_mipi_dsi_write(regs, PHY_ENABLECLK, 1);
  273. rk_mipi_dsi_write(regs, PHY_FORCEPLL, 1);
  274. rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 1);
  275. rk_mipi_dsi_write(regs, PHY_RSTZ, 1);
  276. return 0;
  277. }