stm32_ltdc.c 14 KB

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