lcdc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Timing controller driver for Allwinner SoCs.
  4. *
  5. * (C) Copyright 2013-2014 Luc Verhaegen <libv@skynet.be>
  6. * (C) Copyright 2014-2015 Hans de Goede <hdegoede@redhat.com>
  7. * (C) Copyright 2017 Jernej Skrabec <jernej.skrabec@siol.net>
  8. */
  9. #include <common.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch/lcdc.h>
  12. #include <asm/io.h>
  13. static int lcdc_get_clk_delay(const struct display_timing *mode, int tcon)
  14. {
  15. int delay;
  16. delay = mode->vfront_porch.typ + mode->vsync_len.typ +
  17. mode->vback_porch.typ;
  18. if (mode->flags & DISPLAY_FLAGS_INTERLACED)
  19. delay /= 2;
  20. if (tcon == 1)
  21. delay -= 2;
  22. return (delay > 30) ? 30 : delay;
  23. }
  24. void lcdc_init(struct sunxi_lcdc_reg * const lcdc)
  25. {
  26. /* Init lcdc */
  27. writel(0, &lcdc->ctrl); /* Disable tcon */
  28. writel(0, &lcdc->int0); /* Disable all interrupts */
  29. /* Disable tcon0 dot clock */
  30. clrbits_le32(&lcdc->tcon0_dclk, SUNXI_LCDC_TCON0_DCLK_ENABLE);
  31. /* Set all io lines to tristate */
  32. writel(0xffffffff, &lcdc->tcon0_io_tristate);
  33. writel(0xffffffff, &lcdc->tcon1_io_tristate);
  34. }
  35. void lcdc_enable(struct sunxi_lcdc_reg * const lcdc, int depth)
  36. {
  37. setbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_TCON_ENABLE);
  38. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  39. setbits_le32(&lcdc->tcon0_lvds_intf, SUNXI_LCDC_TCON0_LVDS_INTF_ENABLE);
  40. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0);
  41. #ifdef CONFIG_SUNXI_GEN_SUN6I
  42. udelay(2); /* delay at least 1200 ns */
  43. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_EN_MB);
  44. udelay(2); /* delay at least 1200 ns */
  45. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_DRVC);
  46. if (depth == 18)
  47. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_DRVD(0x7));
  48. else
  49. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_DRVD(0xf));
  50. #else
  51. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
  52. udelay(2); /* delay at least 1200 ns */
  53. setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT1);
  54. udelay(1); /* delay at least 120 ns */
  55. setbits_le32(&lcdc->lvds_ana1, SUNXI_LCDC_LVDS_ANA1_INIT2);
  56. setbits_le32(&lcdc->lvds_ana0, SUNXI_LCDC_LVDS_ANA0_UPDATE);
  57. #endif
  58. #endif
  59. }
  60. void lcdc_tcon0_mode_set(struct sunxi_lcdc_reg * const lcdc,
  61. const struct display_timing *mode,
  62. int clk_div, bool for_ext_vga_dac,
  63. int depth, int dclk_phase)
  64. {
  65. int bp, clk_delay, total, val;
  66. #ifndef CONFIG_SUNXI_DE2
  67. /* Use tcon0 */
  68. clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
  69. SUNXI_LCDC_CTRL_IO_MAP_TCON0);
  70. #endif
  71. clk_delay = lcdc_get_clk_delay(mode, 0);
  72. writel(SUNXI_LCDC_TCON0_CTRL_ENABLE |
  73. SUNXI_LCDC_TCON0_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon0_ctrl);
  74. writel(SUNXI_LCDC_TCON0_DCLK_ENABLE |
  75. SUNXI_LCDC_TCON0_DCLK_DIV(clk_div), &lcdc->tcon0_dclk);
  76. writel(SUNXI_LCDC_X(mode->hactive.typ) |
  77. SUNXI_LCDC_Y(mode->vactive.typ), &lcdc->tcon0_timing_active);
  78. bp = mode->hsync_len.typ + mode->hback_porch.typ;
  79. total = mode->hactive.typ + mode->hfront_porch.typ + bp;
  80. writel(SUNXI_LCDC_TCON0_TIMING_H_TOTAL(total) |
  81. SUNXI_LCDC_TCON0_TIMING_H_BP(bp), &lcdc->tcon0_timing_h);
  82. bp = mode->vsync_len.typ + mode->vback_porch.typ;
  83. total = mode->vactive.typ + mode->vfront_porch.typ + bp;
  84. writel(SUNXI_LCDC_TCON0_TIMING_V_TOTAL(total) |
  85. SUNXI_LCDC_TCON0_TIMING_V_BP(bp), &lcdc->tcon0_timing_v);
  86. #if defined(CONFIG_VIDEO_LCD_IF_PARALLEL) || defined(CONFIG_VIDEO_DE2)
  87. writel(SUNXI_LCDC_X(mode->hsync_len.typ) |
  88. SUNXI_LCDC_Y(mode->vsync_len.typ), &lcdc->tcon0_timing_sync);
  89. writel(0, &lcdc->tcon0_hv_intf);
  90. writel(0, &lcdc->tcon0_cpu_intf);
  91. #endif
  92. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  93. val = (depth == 18) ? 1 : 0;
  94. writel(SUNXI_LCDC_TCON0_LVDS_INTF_BITWIDTH(val) |
  95. SUNXI_LCDC_TCON0_LVDS_CLK_SEL_TCON0, &lcdc->tcon0_lvds_intf);
  96. #endif
  97. if (depth == 18 || depth == 16) {
  98. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[0]);
  99. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[1]);
  100. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[2]);
  101. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[3]);
  102. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[4]);
  103. writel(SUNXI_LCDC_TCON0_FRM_SEED, &lcdc->tcon0_frm_seed[5]);
  104. writel(SUNXI_LCDC_TCON0_FRM_TAB0, &lcdc->tcon0_frm_table[0]);
  105. writel(SUNXI_LCDC_TCON0_FRM_TAB1, &lcdc->tcon0_frm_table[1]);
  106. writel(SUNXI_LCDC_TCON0_FRM_TAB2, &lcdc->tcon0_frm_table[2]);
  107. writel(SUNXI_LCDC_TCON0_FRM_TAB3, &lcdc->tcon0_frm_table[3]);
  108. writel(((depth == 18) ?
  109. SUNXI_LCDC_TCON0_FRM_CTRL_RGB666 :
  110. SUNXI_LCDC_TCON0_FRM_CTRL_RGB565),
  111. &lcdc->tcon0_frm_ctrl);
  112. }
  113. val = SUNXI_LCDC_TCON0_IO_POL_DCLK_PHASE(dclk_phase);
  114. if (mode->flags & DISPLAY_FLAGS_HSYNC_LOW)
  115. val |= SUNXI_LCDC_TCON_HSYNC_MASK;
  116. if (mode->flags & DISPLAY_FLAGS_VSYNC_LOW)
  117. val |= SUNXI_LCDC_TCON_VSYNC_MASK;
  118. #ifdef CONFIG_VIDEO_VGA_VIA_LCD_FORCE_SYNC_ACTIVE_HIGH
  119. if (for_ext_vga_dac)
  120. val = 0;
  121. #endif
  122. writel(val, &lcdc->tcon0_io_polarity);
  123. writel(0, &lcdc->tcon0_io_tristate);
  124. }
  125. void lcdc_tcon1_mode_set(struct sunxi_lcdc_reg * const lcdc,
  126. const struct display_timing *mode,
  127. bool ext_hvsync, bool is_composite)
  128. {
  129. int bp, clk_delay, total, val, yres;
  130. #ifndef CONFIG_SUNXI_DE2
  131. /* Use tcon1 */
  132. clrsetbits_le32(&lcdc->ctrl, SUNXI_LCDC_CTRL_IO_MAP_MASK,
  133. SUNXI_LCDC_CTRL_IO_MAP_TCON1);
  134. #endif
  135. clk_delay = lcdc_get_clk_delay(mode, 1);
  136. writel(SUNXI_LCDC_TCON1_CTRL_ENABLE |
  137. ((mode->flags & DISPLAY_FLAGS_INTERLACED) ?
  138. SUNXI_LCDC_TCON1_CTRL_INTERLACE_ENABLE : 0) |
  139. SUNXI_LCDC_TCON1_CTRL_CLK_DELAY(clk_delay), &lcdc->tcon1_ctrl);
  140. yres = mode->vactive.typ;
  141. if (mode->flags & DISPLAY_FLAGS_INTERLACED)
  142. yres /= 2;
  143. writel(SUNXI_LCDC_X(mode->hactive.typ) | SUNXI_LCDC_Y(yres),
  144. &lcdc->tcon1_timing_source);
  145. writel(SUNXI_LCDC_X(mode->hactive.typ) | SUNXI_LCDC_Y(yres),
  146. &lcdc->tcon1_timing_scale);
  147. writel(SUNXI_LCDC_X(mode->hactive.typ) | SUNXI_LCDC_Y(yres),
  148. &lcdc->tcon1_timing_out);
  149. bp = mode->hsync_len.typ + mode->hback_porch.typ;
  150. total = mode->hactive.typ + mode->hfront_porch.typ + bp;
  151. writel(SUNXI_LCDC_TCON1_TIMING_H_TOTAL(total) |
  152. SUNXI_LCDC_TCON1_TIMING_H_BP(bp), &lcdc->tcon1_timing_h);
  153. bp = mode->vsync_len.typ + mode->vback_porch.typ;
  154. total = mode->vactive.typ + mode->vfront_porch.typ + bp;
  155. if (!(mode->flags & DISPLAY_FLAGS_INTERLACED))
  156. total *= 2;
  157. writel(SUNXI_LCDC_TCON1_TIMING_V_TOTAL(total) |
  158. SUNXI_LCDC_TCON1_TIMING_V_BP(bp), &lcdc->tcon1_timing_v);
  159. writel(SUNXI_LCDC_X(mode->hsync_len.typ) |
  160. SUNXI_LCDC_Y(mode->vsync_len.typ), &lcdc->tcon1_timing_sync);
  161. if (ext_hvsync) {
  162. val = 0;
  163. if (mode->flags & DISPLAY_FLAGS_HSYNC_HIGH)
  164. val |= SUNXI_LCDC_TCON_HSYNC_MASK;
  165. if (mode->flags & DISPLAY_FLAGS_VSYNC_HIGH)
  166. val |= SUNXI_LCDC_TCON_VSYNC_MASK;
  167. writel(val, &lcdc->tcon1_io_polarity);
  168. clrbits_le32(&lcdc->tcon1_io_tristate,
  169. SUNXI_LCDC_TCON_VSYNC_MASK |
  170. SUNXI_LCDC_TCON_HSYNC_MASK);
  171. }
  172. #ifdef CONFIG_MACH_SUN5I
  173. if (is_composite)
  174. clrsetbits_le32(&lcdc->mux_ctrl, SUNXI_LCDC_MUX_CTRL_SRC0_MASK,
  175. SUNXI_LCDC_MUX_CTRL_SRC0(1));
  176. #endif
  177. }
  178. void lcdc_pll_set(struct sunxi_ccm_reg *ccm, int tcon, int dotclock,
  179. int *clk_div, int *clk_double, bool is_composite)
  180. {
  181. int value, n, m, min_m, max_m, diff;
  182. int best_n = 0, best_m = 0, best_diff = 0x0FFFFFFF;
  183. int best_double = 0;
  184. bool use_mipi_pll = false;
  185. if (tcon == 0) {
  186. #if defined(CONFIG_VIDEO_LCD_IF_PARALLEL) || defined(CONFIG_SUNXI_DE2)
  187. min_m = 6;
  188. max_m = 127;
  189. #endif
  190. #ifdef CONFIG_VIDEO_LCD_IF_LVDS
  191. min_m = 7;
  192. max_m = 7;
  193. #endif
  194. } else {
  195. min_m = 1;
  196. max_m = 15;
  197. }
  198. /*
  199. * Find the lowest divider resulting in a matching clock, if there
  200. * is no match, pick the closest lower clock, as monitors tend to
  201. * not sync to higher frequencies.
  202. */
  203. for (m = min_m; m <= max_m; m++) {
  204. #ifndef CONFIG_SUNXI_DE2
  205. n = (m * dotclock) / 3000;
  206. if ((n >= 9) && (n <= 127)) {
  207. value = (3000 * n) / m;
  208. diff = dotclock - value;
  209. if (diff < best_diff) {
  210. best_diff = diff;
  211. best_m = m;
  212. best_n = n;
  213. best_double = 0;
  214. }
  215. }
  216. /* These are just duplicates */
  217. if (!(m & 1))
  218. continue;
  219. #endif
  220. /* No double clock on DE2 */
  221. n = (m * dotclock) / 6000;
  222. if ((n >= 9) && (n <= 127)) {
  223. value = (6000 * n) / m;
  224. diff = dotclock - value;
  225. if (diff < best_diff) {
  226. best_diff = diff;
  227. best_m = m;
  228. best_n = n;
  229. best_double = 1;
  230. }
  231. }
  232. }
  233. #ifdef CONFIG_MACH_SUN6I
  234. /*
  235. * Use the MIPI pll if we've been unable to find any matching setting
  236. * for PLL3, this happens with high dotclocks because of min_m = 6.
  237. */
  238. if (tcon == 0 && best_n == 0) {
  239. use_mipi_pll = true;
  240. best_m = 6; /* Minimum m for tcon0 */
  241. }
  242. if (use_mipi_pll) {
  243. clock_set_pll3(297000000); /* Fix the video pll at 297 MHz */
  244. clock_set_mipi_pll(best_m * dotclock * 1000);
  245. debug("dotclock: %dkHz = %dkHz via mipi pll\n",
  246. dotclock, clock_get_mipi_pll() / best_m / 1000);
  247. } else
  248. #endif
  249. {
  250. clock_set_pll3(best_n * 3000000);
  251. debug("dotclock: %dkHz = %dkHz: (%d * 3MHz * %d) / %d\n",
  252. dotclock,
  253. (best_double + 1) * clock_get_pll3() / best_m / 1000,
  254. best_double + 1, best_n, best_m);
  255. }
  256. if (tcon == 0) {
  257. u32 pll;
  258. if (use_mipi_pll)
  259. pll = CCM_LCD_CH0_CTRL_MIPI_PLL;
  260. else if (best_double)
  261. pll = CCM_LCD_CH0_CTRL_PLL3_2X;
  262. else
  263. pll = CCM_LCD_CH0_CTRL_PLL3;
  264. #ifndef CONFIG_SUNXI_DE2
  265. writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST | pll,
  266. &ccm->lcd0_ch0_clk_cfg);
  267. #else
  268. writel(CCM_LCD_CH0_CTRL_GATE | CCM_LCD_CH0_CTRL_RST | pll,
  269. &ccm->lcd0_clk_cfg);
  270. #endif
  271. }
  272. #ifndef CONFIG_SUNXI_DE2
  273. else {
  274. writel(CCM_LCD_CH1_CTRL_GATE |
  275. (best_double ? CCM_LCD_CH1_CTRL_PLL3_2X :
  276. CCM_LCD_CH1_CTRL_PLL3) |
  277. CCM_LCD_CH1_CTRL_M(best_m), &ccm->lcd0_ch1_clk_cfg);
  278. if (is_composite)
  279. setbits_le32(&ccm->lcd0_ch1_clk_cfg,
  280. CCM_LCD_CH1_CTRL_HALF_SCLK1);
  281. }
  282. #endif
  283. *clk_div = best_m;
  284. *clk_double = best_double;
  285. }