stm32_ltdc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2017
  3. *
  4. * Authors: Philippe Cornu <philippe.cornu@st.com>
  5. * Yannick Fertre <yannick.fertre@st.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <panel.h>
  13. #include <video.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/gpio.h>
  16. #include <dm/device-internal.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct stm32_ltdc_priv {
  19. void __iomem *regs;
  20. struct display_timing timing;
  21. enum video_log2_bpp l2bpp;
  22. u32 bg_col_argb;
  23. u32 crop_x, crop_y, crop_w, crop_h;
  24. u32 alpha;
  25. };
  26. /* LTDC main registers */
  27. #define LTDC_IDR 0x00 /* IDentification */
  28. #define LTDC_LCR 0x04 /* Layer Count */
  29. #define LTDC_SSCR 0x08 /* Synchronization Size Configuration */
  30. #define LTDC_BPCR 0x0C /* Back Porch Configuration */
  31. #define LTDC_AWCR 0x10 /* Active Width Configuration */
  32. #define LTDC_TWCR 0x14 /* Total Width Configuration */
  33. #define LTDC_GCR 0x18 /* Global Control */
  34. #define LTDC_GC1R 0x1C /* Global Configuration 1 */
  35. #define LTDC_GC2R 0x20 /* Global Configuration 2 */
  36. #define LTDC_SRCR 0x24 /* Shadow Reload Configuration */
  37. #define LTDC_GACR 0x28 /* GAmma Correction */
  38. #define LTDC_BCCR 0x2C /* Background Color Configuration */
  39. #define LTDC_IER 0x34 /* Interrupt Enable */
  40. #define LTDC_ISR 0x38 /* Interrupt Status */
  41. #define LTDC_ICR 0x3C /* Interrupt Clear */
  42. #define LTDC_LIPCR 0x40 /* Line Interrupt Position Conf. */
  43. #define LTDC_CPSR 0x44 /* Current Position Status */
  44. #define LTDC_CDSR 0x48 /* Current Display Status */
  45. /* LTDC layer 1 registers */
  46. #define LTDC_L1LC1R 0x80 /* L1 Layer Configuration 1 */
  47. #define LTDC_L1LC2R 0x84 /* L1 Layer Configuration 2 */
  48. #define LTDC_L1CR 0x84 /* L1 Control */
  49. #define LTDC_L1WHPCR 0x88 /* L1 Window Hor Position Config */
  50. #define LTDC_L1WVPCR 0x8C /* L1 Window Vert Position Config */
  51. #define LTDC_L1CKCR 0x90 /* L1 Color Keying Configuration */
  52. #define LTDC_L1PFCR 0x94 /* L1 Pixel Format Configuration */
  53. #define LTDC_L1CACR 0x98 /* L1 Constant Alpha Config */
  54. #define LTDC_L1DCCR 0x9C /* L1 Default Color Configuration */
  55. #define LTDC_L1BFCR 0xA0 /* L1 Blend Factors Configuration */
  56. #define LTDC_L1FBBCR 0xA4 /* L1 FrameBuffer Bus Control */
  57. #define LTDC_L1AFBCR 0xA8 /* L1 AuxFB Control */
  58. #define LTDC_L1CFBAR 0xAC /* L1 Color FrameBuffer Address */
  59. #define LTDC_L1CFBLR 0xB0 /* L1 Color FrameBuffer Length */
  60. #define LTDC_L1CFBLNR 0xB4 /* L1 Color FrameBuffer Line Nb */
  61. #define LTDC_L1AFBAR 0xB8 /* L1 AuxFB Address */
  62. #define LTDC_L1AFBLR 0xBC /* L1 AuxFB Length */
  63. #define LTDC_L1AFBLNR 0xC0 /* L1 AuxFB Line Number */
  64. #define LTDC_L1CLUTWR 0xC4 /* L1 CLUT Write */
  65. /* Bit definitions */
  66. #define SSCR_VSH GENMASK(10, 0) /* Vertical Synchronization Height */
  67. #define SSCR_HSW GENMASK(27, 16) /* Horizontal Synchronization Width */
  68. #define BPCR_AVBP GENMASK(10, 0) /* Accumulated Vertical Back Porch */
  69. #define BPCR_AHBP GENMASK(27, 16) /* Accumulated Horizontal Back Porch */
  70. #define AWCR_AAH GENMASK(10, 0) /* Accumulated Active Height */
  71. #define AWCR_AAW GENMASK(27, 16) /* Accumulated Active Width */
  72. #define TWCR_TOTALH GENMASK(10, 0) /* TOTAL Height */
  73. #define TWCR_TOTALW GENMASK(27, 16) /* TOTAL Width */
  74. #define GCR_LTDCEN BIT(0) /* LTDC ENable */
  75. #define GCR_DEN BIT(16) /* Dither ENable */
  76. #define GCR_PCPOL BIT(28) /* Pixel Clock POLarity-Inverted */
  77. #define GCR_DEPOL BIT(29) /* Data Enable POLarity-High */
  78. #define GCR_VSPOL BIT(30) /* Vertical Synchro POLarity-High */
  79. #define GCR_HSPOL BIT(31) /* Horizontal Synchro POLarity-High */
  80. #define GC1R_WBCH GENMASK(3, 0) /* Width of Blue CHannel output */
  81. #define GC1R_WGCH GENMASK(7, 4) /* Width of Green Channel output */
  82. #define GC1R_WRCH GENMASK(11, 8) /* Width of Red Channel output */
  83. #define GC1R_PBEN BIT(12) /* Precise Blending ENable */
  84. #define GC1R_DT GENMASK(15, 14) /* Dithering Technique */
  85. #define GC1R_GCT GENMASK(19, 17) /* Gamma Correction Technique */
  86. #define GC1R_SHREN BIT(21) /* SHadow Registers ENabled */
  87. #define GC1R_BCP BIT(22) /* Background Colour Programmable */
  88. #define GC1R_BBEN BIT(23) /* Background Blending ENabled */
  89. #define GC1R_LNIP BIT(24) /* Line Number IRQ Position */
  90. #define GC1R_TP BIT(25) /* Timing Programmable */
  91. #define GC1R_IPP BIT(26) /* IRQ Polarity Programmable */
  92. #define GC1R_SPP BIT(27) /* Sync Polarity Programmable */
  93. #define GC1R_DWP BIT(28) /* Dither Width Programmable */
  94. #define GC1R_STREN BIT(29) /* STatus Registers ENabled */
  95. #define GC1R_BMEN BIT(31) /* Blind Mode ENabled */
  96. #define GC2R_EDCA BIT(0) /* External Display Control Ability */
  97. #define GC2R_STSAEN BIT(1) /* Slave Timing Sync Ability ENabled */
  98. #define GC2R_DVAEN BIT(2) /* Dual-View Ability ENabled */
  99. #define GC2R_DPAEN BIT(3) /* Dual-Port Ability ENabled */
  100. #define GC2R_BW GENMASK(6, 4) /* Bus Width (log2 of nb of bytes) */
  101. #define GC2R_EDCEN BIT(7) /* External Display Control ENabled */
  102. #define SRCR_IMR BIT(0) /* IMmediate Reload */
  103. #define SRCR_VBR BIT(1) /* Vertical Blanking Reload */
  104. #define LXCR_LEN BIT(0) /* Layer ENable */
  105. #define LXCR_COLKEN BIT(1) /* Color Keying Enable */
  106. #define LXCR_CLUTEN BIT(4) /* Color Look-Up Table ENable */
  107. #define LXWHPCR_WHSTPOS GENMASK(11, 0) /* Window Horizontal StarT POSition */
  108. #define LXWHPCR_WHSPPOS GENMASK(27, 16) /* Window Horizontal StoP POSition */
  109. #define LXWVPCR_WVSTPOS GENMASK(10, 0) /* Window Vertical StarT POSition */
  110. #define LXWVPCR_WVSPPOS GENMASK(26, 16) /* Window Vertical StoP POSition */
  111. #define LXPFCR_PF GENMASK(2, 0) /* Pixel Format */
  112. #define LXCACR_CONSTA GENMASK(7, 0) /* CONSTant Alpha */
  113. #define LXBFCR_BF2 GENMASK(2, 0) /* Blending Factor 2 */
  114. #define LXBFCR_BF1 GENMASK(10, 8) /* Blending Factor 1 */
  115. #define LXCFBLR_CFBLL GENMASK(12, 0) /* Color Frame Buffer Line Length */
  116. #define LXCFBLR_CFBP GENMASK(28, 16) /* Color Frame Buffer Pitch in bytes */
  117. #define LXCFBLNR_CFBLN GENMASK(10, 0) /* Color Frame Buffer Line Number */
  118. #define BF1_PAXCA 0x600 /* Pixel Alpha x Constant Alpha */
  119. #define BF2_1PAXCA 0x007 /* 1 - (Pixel Alpha x Constant Alpha) */
  120. enum stm32_ltdc_pix_fmt {
  121. PF_ARGB8888 = 0,
  122. PF_RGB888,
  123. PF_RGB565,
  124. PF_ARGB1555,
  125. PF_ARGB4444,
  126. PF_L8,
  127. PF_AL44,
  128. PF_AL88
  129. };
  130. /* TODO add more color format support */
  131. static u32 stm32_ltdc_get_pixel_format(enum video_log2_bpp l2bpp)
  132. {
  133. enum stm32_ltdc_pix_fmt pf;
  134. switch (l2bpp) {
  135. case VIDEO_BPP16:
  136. pf = PF_RGB565;
  137. break;
  138. case VIDEO_BPP1:
  139. case VIDEO_BPP2:
  140. case VIDEO_BPP4:
  141. case VIDEO_BPP8:
  142. case VIDEO_BPP32:
  143. default:
  144. debug("%s: warning %dbpp not supported yet, %dbpp instead\n",
  145. __func__, VNBITS(l2bpp), VNBITS(VIDEO_BPP16));
  146. pf = PF_RGB565;
  147. break;
  148. }
  149. debug("%s: %d bpp -> ltdc pf %d\n", __func__, VNBITS(l2bpp), pf);
  150. return (u32)pf;
  151. }
  152. static void stm32_ltdc_enable(struct stm32_ltdc_priv *priv)
  153. {
  154. /* Reload configuration immediately & enable LTDC */
  155. setbits_le32(priv->regs + LTDC_SRCR, SRCR_IMR);
  156. setbits_le32(priv->regs + LTDC_GCR, GCR_LTDCEN);
  157. }
  158. static void stm32_ltdc_set_mode(struct stm32_ltdc_priv *priv)
  159. {
  160. void __iomem *regs = priv->regs;
  161. struct display_timing *timing = &priv->timing;
  162. u32 hsync, vsync, acc_hbp, acc_vbp, acc_act_w, acc_act_h;
  163. u32 total_w, total_h;
  164. u32 val;
  165. /* Convert video timings to ltdc timings */
  166. hsync = timing->hsync_len.typ - 1;
  167. vsync = timing->vsync_len.typ - 1;
  168. acc_hbp = hsync + timing->hback_porch.typ;
  169. acc_vbp = vsync + timing->vback_porch.typ;
  170. acc_act_w = acc_hbp + timing->hactive.typ;
  171. acc_act_h = acc_vbp + timing->vactive.typ;
  172. total_w = acc_act_w + timing->hfront_porch.typ;
  173. total_h = acc_act_h + timing->vfront_porch.typ;
  174. /* Synchronization sizes */
  175. val = (hsync << 16) | vsync;
  176. clrsetbits_le32(regs + LTDC_SSCR, SSCR_VSH | SSCR_HSW, val);
  177. /* Accumulated back porch */
  178. val = (acc_hbp << 16) | acc_vbp;
  179. clrsetbits_le32(regs + LTDC_BPCR, BPCR_AVBP | BPCR_AHBP, val);
  180. /* Accumulated active width */
  181. val = (acc_act_w << 16) | acc_act_h;
  182. clrsetbits_le32(regs + LTDC_AWCR, AWCR_AAW | AWCR_AAH, val);
  183. /* Total width & height */
  184. val = (total_w << 16) | total_h;
  185. clrsetbits_le32(regs + LTDC_TWCR, TWCR_TOTALH | TWCR_TOTALW, val);
  186. /* Signal polarities */
  187. val = 0;
  188. debug("%s: timing->flags 0x%08x\n", __func__, timing->flags);
  189. if (timing->flags & DISPLAY_FLAGS_HSYNC_HIGH)
  190. val |= GCR_HSPOL;
  191. if (timing->flags & DISPLAY_FLAGS_VSYNC_HIGH)
  192. val |= GCR_VSPOL;
  193. if (timing->flags & DISPLAY_FLAGS_DE_HIGH)
  194. val |= GCR_DEPOL;
  195. if (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  196. val |= GCR_PCPOL;
  197. clrsetbits_le32(regs + LTDC_GCR,
  198. GCR_HSPOL | GCR_VSPOL | GCR_DEPOL | GCR_PCPOL, val);
  199. /* Overall background color */
  200. writel(priv->bg_col_argb, priv->regs + LTDC_BCCR);
  201. }
  202. static void stm32_ltdc_set_layer1(struct stm32_ltdc_priv *priv, ulong fb_addr)
  203. {
  204. void __iomem *regs = priv->regs;
  205. u32 x0, x1, y0, y1;
  206. u32 pitch_in_bytes;
  207. u32 line_length;
  208. u32 bus_width;
  209. u32 val, tmp, bpp;
  210. x0 = priv->crop_x;
  211. x1 = priv->crop_x + priv->crop_w - 1;
  212. y0 = priv->crop_y;
  213. y1 = priv->crop_y + priv->crop_h - 1;
  214. /* Horizontal start and stop position */
  215. tmp = (readl(regs + LTDC_BPCR) & BPCR_AHBP) >> 16;
  216. val = ((x1 + 1 + tmp) << 16) + (x0 + 1 + tmp);
  217. clrsetbits_le32(regs + LTDC_L1WHPCR, LXWHPCR_WHSTPOS | LXWHPCR_WHSPPOS,
  218. val);
  219. /* Vertical start & stop position */
  220. tmp = readl(regs + LTDC_BPCR) & BPCR_AVBP;
  221. val = ((y1 + 1 + tmp) << 16) + (y0 + 1 + tmp);
  222. clrsetbits_le32(regs + LTDC_L1WVPCR, LXWVPCR_WVSTPOS | LXWVPCR_WVSPPOS,
  223. val);
  224. /* Layer background color */
  225. writel(priv->bg_col_argb, regs + LTDC_L1DCCR);
  226. /* Color frame buffer pitch in bytes & line length */
  227. bpp = VNBITS(priv->l2bpp);
  228. pitch_in_bytes = priv->crop_w * (bpp >> 3);
  229. bus_width = 8 << ((readl(regs + LTDC_GC2R) & GC2R_BW) >> 4);
  230. line_length = ((bpp >> 3) * priv->crop_w) + (bus_width >> 3) - 1;
  231. val = (pitch_in_bytes << 16) | line_length;
  232. clrsetbits_le32(regs + LTDC_L1CFBLR, LXCFBLR_CFBLL | LXCFBLR_CFBP, val);
  233. /* Pixel format */
  234. val = stm32_ltdc_get_pixel_format(priv->l2bpp);
  235. clrsetbits_le32(regs + LTDC_L1PFCR, LXPFCR_PF, val);
  236. /* Constant alpha value */
  237. clrsetbits_le32(regs + LTDC_L1CACR, LXCACR_CONSTA, priv->alpha);
  238. /* Blending factors */
  239. clrsetbits_le32(regs + LTDC_L1BFCR, LXBFCR_BF2 | LXBFCR_BF1,
  240. BF1_PAXCA | BF2_1PAXCA);
  241. /* Frame buffer line number */
  242. clrsetbits_le32(regs + LTDC_L1CFBLNR, LXCFBLNR_CFBLN, priv->crop_h);
  243. /* Frame buffer address */
  244. writel(fb_addr, regs + LTDC_L1CFBAR);
  245. /* Enable layer 1 */
  246. setbits_le32(priv->regs + LTDC_L1CR, LXCR_LEN);
  247. }
  248. static int stm32_ltdc_probe(struct udevice *dev)
  249. {
  250. struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
  251. struct video_priv *uc_priv = dev_get_uclass_priv(dev);
  252. struct stm32_ltdc_priv *priv = dev_get_priv(dev);
  253. struct udevice *panel;
  254. struct clk pclk, pxclk;
  255. int ret;
  256. priv->regs = (void *)dev_read_addr(dev);
  257. if ((fdt_addr_t)priv->regs == FDT_ADDR_T_NONE) {
  258. debug("%s: ltdc dt register address error\n", __func__);
  259. return -EINVAL;
  260. }
  261. ret = uclass_first_device(UCLASS_PANEL, &panel);
  262. if (ret) {
  263. debug("%s: panel device error %d\n", __func__, ret);
  264. return ret;
  265. }
  266. ret = panel_enable_backlight(panel);
  267. if (ret) {
  268. debug("%s: panel %s enable backlight error %d\n",
  269. __func__, panel->name, ret);
  270. return ret;
  271. }
  272. ret = fdtdec_decode_display_timing(gd->fdt_blob, dev_of_offset(dev),
  273. 0, &priv->timing);
  274. if (ret) {
  275. debug("%s: decode display timing error %d\n", __func__, ret);
  276. return -EINVAL;
  277. }
  278. ret = clk_get_by_name(dev, "pclk", &pclk);
  279. if (ret) {
  280. debug("%s: peripheral clock get error %d\n", __func__, ret);
  281. return ret;
  282. }
  283. ret = clk_enable(&pclk);
  284. if (ret) {
  285. debug("%s: peripheral clock enable error %d\n", __func__, ret);
  286. return ret;
  287. }
  288. /* Verify pixel clock value if any & inform user accordingly */
  289. ret = clk_get_by_name(dev, "pxclk", &pxclk);
  290. if (!ret) {
  291. if (clk_get_rate(&pxclk) != priv->timing.pixelclock.typ)
  292. printf("Warning: please adjust ltdc pixel clock\n");
  293. }
  294. /* TODO Below parameters are hard-coded for the moment... */
  295. priv->l2bpp = VIDEO_BPP16;
  296. priv->bg_col_argb = 0xFFFFFFFF; /* white no transparency */
  297. priv->crop_x = 0;
  298. priv->crop_y = 0;
  299. priv->crop_w = priv->timing.hactive.typ;
  300. priv->crop_h = priv->timing.vactive.typ;
  301. priv->alpha = 0xFF;
  302. debug("%s: %dx%d %dbpp frame buffer at 0x%lx\n", __func__,
  303. priv->timing.hactive.typ, priv->timing.vactive.typ,
  304. VNBITS(priv->l2bpp), uc_plat->base);
  305. debug("%s: crop %d,%d %dx%d bg 0x%08x alpha %d\n", __func__,
  306. priv->crop_x, priv->crop_y, priv->crop_w, priv->crop_h,
  307. priv->bg_col_argb, priv->alpha);
  308. /* Configure & start LTDC */
  309. stm32_ltdc_set_mode(priv);
  310. stm32_ltdc_set_layer1(priv, uc_plat->base);
  311. stm32_ltdc_enable(priv);
  312. uc_priv->xsize = priv->timing.hactive.typ;
  313. uc_priv->ysize = priv->timing.vactive.typ;
  314. uc_priv->bpix = priv->l2bpp;
  315. video_set_flush_dcache(dev, true);
  316. return 0;
  317. }
  318. static int stm32_ltdc_bind(struct udevice *dev)
  319. {
  320. struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
  321. uc_plat->size = CONFIG_VIDEO_STM32_MAX_XRES *
  322. CONFIG_VIDEO_STM32_MAX_YRES *
  323. (CONFIG_VIDEO_STM32_MAX_BPP >> 3);
  324. debug("%s: frame buffer max size %d bytes\n", __func__, uc_plat->size);
  325. return 0;
  326. }
  327. static const struct udevice_id stm32_ltdc_ids[] = {
  328. { .compatible = "st,stm32-ltdc" },
  329. { }
  330. };
  331. U_BOOT_DRIVER(stm32_ltdc) = {
  332. .name = "stm32_ltdc",
  333. .id = UCLASS_VIDEO,
  334. .of_match = stm32_ltdc_ids,
  335. .probe = stm32_ltdc_probe,
  336. .bind = stm32_ltdc_bind,
  337. .priv_auto_alloc_size = sizeof(struct stm32_ltdc_priv),
  338. };