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