xref: /openbmc/u-boot/drivers/video/stm32/stm32_ltdc.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
272719d2fSPhilippe CORNU /*
3c4c33e9dSyannick fertre  * Copyright (C) 2017-2018 STMicroelectronics - All Rights Reserved
4c4c33e9dSyannick fertre  * Author(s): Philippe Cornu <philippe.cornu@st.com> for STMicroelectronics.
5c4c33e9dSyannick fertre  *	      Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
672719d2fSPhilippe CORNU  */
772719d2fSPhilippe CORNU 
872719d2fSPhilippe CORNU #include <common.h>
972719d2fSPhilippe CORNU #include <clk.h>
1072719d2fSPhilippe CORNU #include <dm.h>
1172719d2fSPhilippe CORNU #include <panel.h>
12c0fb2fc0Syannick fertre #include <reset.h>
1372719d2fSPhilippe CORNU #include <video.h>
1472719d2fSPhilippe CORNU #include <asm/io.h>
1572719d2fSPhilippe CORNU #include <asm/arch/gpio.h>
1672719d2fSPhilippe CORNU #include <dm/device-internal.h>
1772719d2fSPhilippe CORNU 
1872719d2fSPhilippe CORNU DECLARE_GLOBAL_DATA_PTR;
1972719d2fSPhilippe CORNU 
2072719d2fSPhilippe CORNU struct stm32_ltdc_priv {
2172719d2fSPhilippe CORNU 	void __iomem *regs;
2272719d2fSPhilippe CORNU 	struct display_timing timing;
2372719d2fSPhilippe CORNU 	enum video_log2_bpp l2bpp;
2472719d2fSPhilippe CORNU 	u32 bg_col_argb;
2572719d2fSPhilippe CORNU 	u32 crop_x, crop_y, crop_w, crop_h;
2672719d2fSPhilippe CORNU 	u32 alpha;
2772719d2fSPhilippe CORNU };
2872719d2fSPhilippe CORNU 
2972719d2fSPhilippe CORNU /* LTDC main registers */
3072719d2fSPhilippe CORNU #define LTDC_IDR	0x00	/* IDentification */
3172719d2fSPhilippe CORNU #define LTDC_LCR	0x04	/* Layer Count */
3272719d2fSPhilippe CORNU #define LTDC_SSCR	0x08	/* Synchronization Size Configuration */
3372719d2fSPhilippe CORNU #define LTDC_BPCR	0x0C	/* Back Porch Configuration */
3472719d2fSPhilippe CORNU #define LTDC_AWCR	0x10	/* Active Width Configuration */
3572719d2fSPhilippe CORNU #define LTDC_TWCR	0x14	/* Total Width Configuration */
3672719d2fSPhilippe CORNU #define LTDC_GCR	0x18	/* Global Control */
3772719d2fSPhilippe CORNU #define LTDC_GC1R	0x1C	/* Global Configuration 1 */
3872719d2fSPhilippe CORNU #define LTDC_GC2R	0x20	/* Global Configuration 2 */
3972719d2fSPhilippe CORNU #define LTDC_SRCR	0x24	/* Shadow Reload Configuration */
4072719d2fSPhilippe CORNU #define LTDC_GACR	0x28	/* GAmma Correction */
4172719d2fSPhilippe CORNU #define LTDC_BCCR	0x2C	/* Background Color Configuration */
4272719d2fSPhilippe CORNU #define LTDC_IER	0x34	/* Interrupt Enable */
4372719d2fSPhilippe CORNU #define LTDC_ISR	0x38	/* Interrupt Status */
4472719d2fSPhilippe CORNU #define LTDC_ICR	0x3C	/* Interrupt Clear */
4572719d2fSPhilippe CORNU #define LTDC_LIPCR	0x40	/* Line Interrupt Position Conf. */
4672719d2fSPhilippe CORNU #define LTDC_CPSR	0x44	/* Current Position Status */
4772719d2fSPhilippe CORNU #define LTDC_CDSR	0x48	/* Current Display Status */
4872719d2fSPhilippe CORNU 
4972719d2fSPhilippe CORNU /* LTDC layer 1 registers */
5072719d2fSPhilippe CORNU #define LTDC_L1LC1R	0x80	/* L1 Layer Configuration 1 */
5172719d2fSPhilippe CORNU #define LTDC_L1LC2R	0x84	/* L1 Layer Configuration 2 */
5272719d2fSPhilippe CORNU #define LTDC_L1CR	0x84	/* L1 Control */
5372719d2fSPhilippe CORNU #define LTDC_L1WHPCR	0x88	/* L1 Window Hor Position Config */
5472719d2fSPhilippe CORNU #define LTDC_L1WVPCR	0x8C	/* L1 Window Vert Position Config */
5572719d2fSPhilippe CORNU #define LTDC_L1CKCR	0x90	/* L1 Color Keying Configuration */
5672719d2fSPhilippe CORNU #define LTDC_L1PFCR	0x94	/* L1 Pixel Format Configuration */
5772719d2fSPhilippe CORNU #define LTDC_L1CACR	0x98	/* L1 Constant Alpha Config */
5872719d2fSPhilippe CORNU #define LTDC_L1DCCR	0x9C	/* L1 Default Color Configuration */
5972719d2fSPhilippe CORNU #define LTDC_L1BFCR	0xA0	/* L1 Blend Factors Configuration */
6072719d2fSPhilippe CORNU #define LTDC_L1FBBCR	0xA4	/* L1 FrameBuffer Bus Control */
6172719d2fSPhilippe CORNU #define LTDC_L1AFBCR	0xA8	/* L1 AuxFB Control */
6272719d2fSPhilippe CORNU #define LTDC_L1CFBAR	0xAC	/* L1 Color FrameBuffer Address */
6372719d2fSPhilippe CORNU #define LTDC_L1CFBLR	0xB0	/* L1 Color FrameBuffer Length */
6472719d2fSPhilippe CORNU #define LTDC_L1CFBLNR	0xB4	/* L1 Color FrameBuffer Line Nb */
6572719d2fSPhilippe CORNU #define LTDC_L1AFBAR	0xB8	/* L1 AuxFB Address */
6672719d2fSPhilippe CORNU #define LTDC_L1AFBLR	0xBC	/* L1 AuxFB Length */
6772719d2fSPhilippe CORNU #define LTDC_L1AFBLNR	0xC0	/* L1 AuxFB Line Number */
6872719d2fSPhilippe CORNU #define LTDC_L1CLUTWR	0xC4	/* L1 CLUT Write */
6972719d2fSPhilippe CORNU 
7072719d2fSPhilippe CORNU /* Bit definitions */
7172719d2fSPhilippe CORNU #define SSCR_VSH	GENMASK(10, 0)	/* Vertical Synchronization Height */
7272719d2fSPhilippe CORNU #define SSCR_HSW	GENMASK(27, 16)	/* Horizontal Synchronization Width */
7372719d2fSPhilippe CORNU 
7472719d2fSPhilippe CORNU #define BPCR_AVBP	GENMASK(10, 0)	/* Accumulated Vertical Back Porch */
7572719d2fSPhilippe CORNU #define BPCR_AHBP	GENMASK(27, 16)	/* Accumulated Horizontal Back Porch */
7672719d2fSPhilippe CORNU 
7772719d2fSPhilippe CORNU #define AWCR_AAH	GENMASK(10, 0)	/* Accumulated Active Height */
7872719d2fSPhilippe CORNU #define AWCR_AAW	GENMASK(27, 16)	/* Accumulated Active Width */
7972719d2fSPhilippe CORNU 
8072719d2fSPhilippe CORNU #define TWCR_TOTALH	GENMASK(10, 0)	/* TOTAL Height */
8172719d2fSPhilippe CORNU #define TWCR_TOTALW	GENMASK(27, 16)	/* TOTAL Width */
8272719d2fSPhilippe CORNU 
8372719d2fSPhilippe CORNU #define GCR_LTDCEN	BIT(0)		/* LTDC ENable */
8472719d2fSPhilippe CORNU #define GCR_DEN		BIT(16)		/* Dither ENable */
8572719d2fSPhilippe CORNU #define GCR_PCPOL	BIT(28)		/* Pixel Clock POLarity-Inverted */
8672719d2fSPhilippe CORNU #define GCR_DEPOL	BIT(29)		/* Data Enable POLarity-High */
8772719d2fSPhilippe CORNU #define GCR_VSPOL	BIT(30)		/* Vertical Synchro POLarity-High */
8872719d2fSPhilippe CORNU #define GCR_HSPOL	BIT(31)		/* Horizontal Synchro POLarity-High */
8972719d2fSPhilippe CORNU 
9072719d2fSPhilippe CORNU #define GC1R_WBCH	GENMASK(3, 0)	/* Width of Blue CHannel output */
9172719d2fSPhilippe CORNU #define GC1R_WGCH	GENMASK(7, 4)	/* Width of Green Channel output */
9272719d2fSPhilippe CORNU #define GC1R_WRCH	GENMASK(11, 8)	/* Width of Red Channel output */
9372719d2fSPhilippe CORNU #define GC1R_PBEN	BIT(12)		/* Precise Blending ENable */
9472719d2fSPhilippe CORNU #define GC1R_DT		GENMASK(15, 14)	/* Dithering Technique */
9572719d2fSPhilippe CORNU #define GC1R_GCT	GENMASK(19, 17)	/* Gamma Correction Technique */
9672719d2fSPhilippe CORNU #define GC1R_SHREN	BIT(21)		/* SHadow Registers ENabled */
9772719d2fSPhilippe CORNU #define GC1R_BCP	BIT(22)		/* Background Colour Programmable */
9872719d2fSPhilippe CORNU #define GC1R_BBEN	BIT(23)		/* Background Blending ENabled */
9972719d2fSPhilippe CORNU #define GC1R_LNIP	BIT(24)		/* Line Number IRQ Position */
10072719d2fSPhilippe CORNU #define GC1R_TP		BIT(25)		/* Timing Programmable */
10172719d2fSPhilippe CORNU #define GC1R_IPP	BIT(26)		/* IRQ Polarity Programmable */
10272719d2fSPhilippe CORNU #define GC1R_SPP	BIT(27)		/* Sync Polarity Programmable */
10372719d2fSPhilippe CORNU #define GC1R_DWP	BIT(28)		/* Dither Width Programmable */
10472719d2fSPhilippe CORNU #define GC1R_STREN	BIT(29)		/* STatus Registers ENabled */
10572719d2fSPhilippe CORNU #define GC1R_BMEN	BIT(31)		/* Blind Mode ENabled */
10672719d2fSPhilippe CORNU 
10772719d2fSPhilippe CORNU #define GC2R_EDCA	BIT(0)		/* External Display Control Ability  */
10872719d2fSPhilippe CORNU #define GC2R_STSAEN	BIT(1)		/* Slave Timing Sync Ability ENabled */
10972719d2fSPhilippe CORNU #define GC2R_DVAEN	BIT(2)		/* Dual-View Ability ENabled */
11072719d2fSPhilippe CORNU #define GC2R_DPAEN	BIT(3)		/* Dual-Port Ability ENabled */
11172719d2fSPhilippe CORNU #define GC2R_BW		GENMASK(6, 4)	/* Bus Width (log2 of nb of bytes) */
11272719d2fSPhilippe CORNU #define GC2R_EDCEN	BIT(7)		/* External Display Control ENabled */
11372719d2fSPhilippe CORNU 
11472719d2fSPhilippe CORNU #define SRCR_IMR	BIT(0)		/* IMmediate Reload */
11572719d2fSPhilippe CORNU #define SRCR_VBR	BIT(1)		/* Vertical Blanking Reload */
11672719d2fSPhilippe CORNU 
11772719d2fSPhilippe CORNU #define LXCR_LEN	BIT(0)		/* Layer ENable */
11872719d2fSPhilippe CORNU #define LXCR_COLKEN	BIT(1)		/* Color Keying Enable */
11972719d2fSPhilippe CORNU #define LXCR_CLUTEN	BIT(4)		/* Color Look-Up Table ENable */
12072719d2fSPhilippe CORNU 
12172719d2fSPhilippe CORNU #define LXWHPCR_WHSTPOS	GENMASK(11, 0)	/* Window Horizontal StarT POSition */
12272719d2fSPhilippe CORNU #define LXWHPCR_WHSPPOS	GENMASK(27, 16)	/* Window Horizontal StoP POSition */
12372719d2fSPhilippe CORNU 
12472719d2fSPhilippe CORNU #define LXWVPCR_WVSTPOS	GENMASK(10, 0)	/* Window Vertical StarT POSition */
12572719d2fSPhilippe CORNU #define LXWVPCR_WVSPPOS	GENMASK(26, 16)	/* Window Vertical StoP POSition */
12672719d2fSPhilippe CORNU 
12772719d2fSPhilippe CORNU #define LXPFCR_PF	GENMASK(2, 0)	/* Pixel Format */
12872719d2fSPhilippe CORNU 
12972719d2fSPhilippe CORNU #define LXCACR_CONSTA	GENMASK(7, 0)	/* CONSTant Alpha */
13072719d2fSPhilippe CORNU 
13172719d2fSPhilippe CORNU #define LXBFCR_BF2	GENMASK(2, 0)	/* Blending Factor 2 */
13272719d2fSPhilippe CORNU #define LXBFCR_BF1	GENMASK(10, 8)	/* Blending Factor 1 */
13372719d2fSPhilippe CORNU 
13472719d2fSPhilippe CORNU #define LXCFBLR_CFBLL	GENMASK(12, 0)	/* Color Frame Buffer Line Length */
13572719d2fSPhilippe CORNU #define LXCFBLR_CFBP	GENMASK(28, 16)	/* Color Frame Buffer Pitch in bytes */
13672719d2fSPhilippe CORNU 
13772719d2fSPhilippe CORNU #define LXCFBLNR_CFBLN	GENMASK(10, 0)	/* Color Frame Buffer Line Number */
13872719d2fSPhilippe CORNU 
13972719d2fSPhilippe CORNU #define BF1_PAXCA	0x600		/* Pixel Alpha x Constant Alpha */
140e6194ce6Syannick fertre #define BF1_CA		0x400		/* Constant Alpha */
14172719d2fSPhilippe CORNU #define BF2_1PAXCA	0x007		/* 1 - (Pixel Alpha x Constant Alpha) */
142e6194ce6Syannick fertre #define BF2_1CA		0x005		/* 1 - Constant Alpha */
14372719d2fSPhilippe CORNU 
14472719d2fSPhilippe CORNU enum stm32_ltdc_pix_fmt {
14572719d2fSPhilippe CORNU 	PF_ARGB8888 = 0,
14672719d2fSPhilippe CORNU 	PF_RGB888,
14772719d2fSPhilippe CORNU 	PF_RGB565,
14872719d2fSPhilippe CORNU 	PF_ARGB1555,
14972719d2fSPhilippe CORNU 	PF_ARGB4444,
15072719d2fSPhilippe CORNU 	PF_L8,
15172719d2fSPhilippe CORNU 	PF_AL44,
15272719d2fSPhilippe CORNU 	PF_AL88
15372719d2fSPhilippe CORNU };
15472719d2fSPhilippe CORNU 
15572719d2fSPhilippe CORNU /* TODO add more color format support */
stm32_ltdc_get_pixel_format(enum video_log2_bpp l2bpp)15672719d2fSPhilippe CORNU static u32 stm32_ltdc_get_pixel_format(enum video_log2_bpp l2bpp)
15772719d2fSPhilippe CORNU {
15872719d2fSPhilippe CORNU 	enum stm32_ltdc_pix_fmt pf;
15972719d2fSPhilippe CORNU 
16072719d2fSPhilippe CORNU 	switch (l2bpp) {
16172719d2fSPhilippe CORNU 	case VIDEO_BPP16:
16272719d2fSPhilippe CORNU 		pf = PF_RGB565;
16372719d2fSPhilippe CORNU 		break;
16472719d2fSPhilippe CORNU 
165e6194ce6Syannick fertre 	case VIDEO_BPP32:
166e6194ce6Syannick fertre 		pf = PF_ARGB8888;
167e6194ce6Syannick fertre 		break;
168e6194ce6Syannick fertre 
169e6194ce6Syannick fertre 	case VIDEO_BPP8:
170e6194ce6Syannick fertre 		pf = PF_L8;
171e6194ce6Syannick fertre 		break;
172e6194ce6Syannick fertre 
17372719d2fSPhilippe CORNU 	case VIDEO_BPP1:
17472719d2fSPhilippe CORNU 	case VIDEO_BPP2:
17572719d2fSPhilippe CORNU 	case VIDEO_BPP4:
17672719d2fSPhilippe CORNU 	default:
17772719d2fSPhilippe CORNU 		debug("%s: warning %dbpp not supported yet, %dbpp instead\n",
17872719d2fSPhilippe CORNU 		      __func__, VNBITS(l2bpp), VNBITS(VIDEO_BPP16));
17972719d2fSPhilippe CORNU 		pf = PF_RGB565;
18072719d2fSPhilippe CORNU 		break;
18172719d2fSPhilippe CORNU 	}
18272719d2fSPhilippe CORNU 
18372719d2fSPhilippe CORNU 	debug("%s: %d bpp -> ltdc pf %d\n", __func__, VNBITS(l2bpp), pf);
18472719d2fSPhilippe CORNU 
18572719d2fSPhilippe CORNU 	return (u32)pf;
18672719d2fSPhilippe CORNU }
18772719d2fSPhilippe CORNU 
has_alpha(u32 fmt)188e6194ce6Syannick fertre static bool has_alpha(u32 fmt)
189e6194ce6Syannick fertre {
190e6194ce6Syannick fertre 	switch (fmt) {
191e6194ce6Syannick fertre 	case PF_ARGB8888:
192e6194ce6Syannick fertre 	case PF_ARGB1555:
193e6194ce6Syannick fertre 	case PF_ARGB4444:
194e6194ce6Syannick fertre 	case PF_AL44:
195e6194ce6Syannick fertre 	case PF_AL88:
196e6194ce6Syannick fertre 		return true;
197e6194ce6Syannick fertre 	case PF_RGB888:
198e6194ce6Syannick fertre 	case PF_RGB565:
199e6194ce6Syannick fertre 	case PF_L8:
200e6194ce6Syannick fertre 	default:
201e6194ce6Syannick fertre 		return false;
202e6194ce6Syannick fertre 	}
203e6194ce6Syannick fertre }
204e6194ce6Syannick fertre 
stm32_ltdc_enable(struct stm32_ltdc_priv * priv)20572719d2fSPhilippe CORNU static void stm32_ltdc_enable(struct stm32_ltdc_priv *priv)
20672719d2fSPhilippe CORNU {
20772719d2fSPhilippe CORNU 	/* Reload configuration immediately & enable LTDC */
20872719d2fSPhilippe CORNU 	setbits_le32(priv->regs + LTDC_SRCR, SRCR_IMR);
20972719d2fSPhilippe CORNU 	setbits_le32(priv->regs + LTDC_GCR, GCR_LTDCEN);
21072719d2fSPhilippe CORNU }
21172719d2fSPhilippe CORNU 
stm32_ltdc_set_mode(struct stm32_ltdc_priv * priv)21272719d2fSPhilippe CORNU static void stm32_ltdc_set_mode(struct stm32_ltdc_priv *priv)
21372719d2fSPhilippe CORNU {
21472719d2fSPhilippe CORNU 	void __iomem *regs = priv->regs;
21572719d2fSPhilippe CORNU 	struct display_timing *timing = &priv->timing;
21672719d2fSPhilippe CORNU 	u32 hsync, vsync, acc_hbp, acc_vbp, acc_act_w, acc_act_h;
21772719d2fSPhilippe CORNU 	u32 total_w, total_h;
21872719d2fSPhilippe CORNU 	u32 val;
21972719d2fSPhilippe CORNU 
22072719d2fSPhilippe CORNU 	/* Convert video timings to ltdc timings */
22172719d2fSPhilippe CORNU 	hsync = timing->hsync_len.typ - 1;
22272719d2fSPhilippe CORNU 	vsync = timing->vsync_len.typ - 1;
22372719d2fSPhilippe CORNU 	acc_hbp = hsync + timing->hback_porch.typ;
22472719d2fSPhilippe CORNU 	acc_vbp = vsync + timing->vback_porch.typ;
22572719d2fSPhilippe CORNU 	acc_act_w = acc_hbp + timing->hactive.typ;
22672719d2fSPhilippe CORNU 	acc_act_h = acc_vbp + timing->vactive.typ;
22772719d2fSPhilippe CORNU 	total_w = acc_act_w + timing->hfront_porch.typ;
22872719d2fSPhilippe CORNU 	total_h = acc_act_h + timing->vfront_porch.typ;
22972719d2fSPhilippe CORNU 
23072719d2fSPhilippe CORNU 	/* Synchronization sizes */
23172719d2fSPhilippe CORNU 	val = (hsync << 16) | vsync;
23272719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_SSCR, SSCR_VSH | SSCR_HSW, val);
23372719d2fSPhilippe CORNU 
23472719d2fSPhilippe CORNU 	/* Accumulated back porch */
23572719d2fSPhilippe CORNU 	val = (acc_hbp << 16) | acc_vbp;
23672719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_BPCR, BPCR_AVBP | BPCR_AHBP, val);
23772719d2fSPhilippe CORNU 
23872719d2fSPhilippe CORNU 	/* Accumulated active width */
23972719d2fSPhilippe CORNU 	val = (acc_act_w << 16) | acc_act_h;
24072719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_AWCR, AWCR_AAW | AWCR_AAH, val);
24172719d2fSPhilippe CORNU 
24272719d2fSPhilippe CORNU 	/* Total width & height */
24372719d2fSPhilippe CORNU 	val = (total_w << 16) | total_h;
24472719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_TWCR, TWCR_TOTALH | TWCR_TOTALW, val);
24572719d2fSPhilippe CORNU 
24675fa711aSyannick fertre 	setbits_le32(regs + LTDC_LIPCR, acc_act_h + 1);
24775fa711aSyannick fertre 
24872719d2fSPhilippe CORNU 	/* Signal polarities */
24972719d2fSPhilippe CORNU 	val = 0;
25072719d2fSPhilippe CORNU 	debug("%s: timing->flags 0x%08x\n", __func__, timing->flags);
25172719d2fSPhilippe CORNU 	if (timing->flags & DISPLAY_FLAGS_HSYNC_HIGH)
25272719d2fSPhilippe CORNU 		val |= GCR_HSPOL;
25372719d2fSPhilippe CORNU 	if (timing->flags & DISPLAY_FLAGS_VSYNC_HIGH)
25472719d2fSPhilippe CORNU 		val |= GCR_VSPOL;
25572719d2fSPhilippe CORNU 	if (timing->flags & DISPLAY_FLAGS_DE_HIGH)
25672719d2fSPhilippe CORNU 		val |= GCR_DEPOL;
25772719d2fSPhilippe CORNU 	if (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
25872719d2fSPhilippe CORNU 		val |= GCR_PCPOL;
25972719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_GCR,
26072719d2fSPhilippe CORNU 			GCR_HSPOL | GCR_VSPOL | GCR_DEPOL | GCR_PCPOL, val);
26172719d2fSPhilippe CORNU 
26272719d2fSPhilippe CORNU 	/* Overall background color */
26372719d2fSPhilippe CORNU 	writel(priv->bg_col_argb, priv->regs + LTDC_BCCR);
26472719d2fSPhilippe CORNU }
26572719d2fSPhilippe CORNU 
stm32_ltdc_set_layer1(struct stm32_ltdc_priv * priv,ulong fb_addr)26672719d2fSPhilippe CORNU static void stm32_ltdc_set_layer1(struct stm32_ltdc_priv *priv, ulong fb_addr)
26772719d2fSPhilippe CORNU {
26872719d2fSPhilippe CORNU 	void __iomem *regs = priv->regs;
26972719d2fSPhilippe CORNU 	u32 x0, x1, y0, y1;
27072719d2fSPhilippe CORNU 	u32 pitch_in_bytes;
27172719d2fSPhilippe CORNU 	u32 line_length;
27272719d2fSPhilippe CORNU 	u32 bus_width;
27372719d2fSPhilippe CORNU 	u32 val, tmp, bpp;
274e6194ce6Syannick fertre 	u32 format;
27572719d2fSPhilippe CORNU 
27672719d2fSPhilippe CORNU 	x0 = priv->crop_x;
27772719d2fSPhilippe CORNU 	x1 = priv->crop_x + priv->crop_w - 1;
27872719d2fSPhilippe CORNU 	y0 = priv->crop_y;
27972719d2fSPhilippe CORNU 	y1 = priv->crop_y + priv->crop_h - 1;
28072719d2fSPhilippe CORNU 
28172719d2fSPhilippe CORNU 	/* Horizontal start and stop position */
28272719d2fSPhilippe CORNU 	tmp = (readl(regs + LTDC_BPCR) & BPCR_AHBP) >> 16;
28372719d2fSPhilippe CORNU 	val = ((x1 + 1 + tmp) << 16) + (x0 + 1 + tmp);
28472719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_L1WHPCR, LXWHPCR_WHSTPOS | LXWHPCR_WHSPPOS,
28572719d2fSPhilippe CORNU 			val);
28672719d2fSPhilippe CORNU 
28772719d2fSPhilippe CORNU 	/* Vertical start & stop position */
28872719d2fSPhilippe CORNU 	tmp = readl(regs + LTDC_BPCR) & BPCR_AVBP;
28972719d2fSPhilippe CORNU 	val = ((y1 + 1 + tmp) << 16) + (y0 + 1 + tmp);
29072719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_L1WVPCR, LXWVPCR_WVSTPOS | LXWVPCR_WVSPPOS,
29172719d2fSPhilippe CORNU 			val);
29272719d2fSPhilippe CORNU 
29372719d2fSPhilippe CORNU 	/* Layer background color */
29472719d2fSPhilippe CORNU 	writel(priv->bg_col_argb, regs + LTDC_L1DCCR);
29572719d2fSPhilippe CORNU 
29672719d2fSPhilippe CORNU 	/* Color frame buffer pitch in bytes & line length */
29772719d2fSPhilippe CORNU 	bpp = VNBITS(priv->l2bpp);
29872719d2fSPhilippe CORNU 	pitch_in_bytes = priv->crop_w * (bpp >> 3);
29972719d2fSPhilippe CORNU 	bus_width = 8 << ((readl(regs + LTDC_GC2R) & GC2R_BW) >> 4);
30072719d2fSPhilippe CORNU 	line_length = ((bpp >> 3) * priv->crop_w) + (bus_width >> 3) - 1;
30172719d2fSPhilippe CORNU 	val = (pitch_in_bytes << 16) | line_length;
30272719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_L1CFBLR, LXCFBLR_CFBLL | LXCFBLR_CFBP, val);
30372719d2fSPhilippe CORNU 
30472719d2fSPhilippe CORNU 	/* Pixel format */
305e6194ce6Syannick fertre 	format = stm32_ltdc_get_pixel_format(priv->l2bpp);
306e6194ce6Syannick fertre 	clrsetbits_le32(regs + LTDC_L1PFCR, LXPFCR_PF, format);
30772719d2fSPhilippe CORNU 
30872719d2fSPhilippe CORNU 	/* Constant alpha value */
30972719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_L1CACR, LXCACR_CONSTA, priv->alpha);
31072719d2fSPhilippe CORNU 
311e6194ce6Syannick fertre 	/* Specifies the blending factors : with or without pixel alpha */
312e6194ce6Syannick fertre 	/* Manage hw-specific capabilities */
313e6194ce6Syannick fertre 	val = has_alpha(format) ? BF1_PAXCA | BF2_1PAXCA : BF1_CA | BF2_1CA;
314e6194ce6Syannick fertre 
31572719d2fSPhilippe CORNU 	/* Blending factors */
316e6194ce6Syannick fertre 	clrsetbits_le32(regs + LTDC_L1BFCR, LXBFCR_BF2 | LXBFCR_BF1, val);
31772719d2fSPhilippe CORNU 
31872719d2fSPhilippe CORNU 	/* Frame buffer line number */
31972719d2fSPhilippe CORNU 	clrsetbits_le32(regs + LTDC_L1CFBLNR, LXCFBLNR_CFBLN, priv->crop_h);
32072719d2fSPhilippe CORNU 
32172719d2fSPhilippe CORNU 	/* Frame buffer address */
32272719d2fSPhilippe CORNU 	writel(fb_addr, regs + LTDC_L1CFBAR);
32372719d2fSPhilippe CORNU 
32472719d2fSPhilippe CORNU 	/* Enable layer 1 */
32572719d2fSPhilippe CORNU 	setbits_le32(priv->regs + LTDC_L1CR, LXCR_LEN);
32672719d2fSPhilippe CORNU }
32772719d2fSPhilippe CORNU 
stm32_ltdc_probe(struct udevice * dev)32872719d2fSPhilippe CORNU static int stm32_ltdc_probe(struct udevice *dev)
32972719d2fSPhilippe CORNU {
33072719d2fSPhilippe CORNU 	struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
33172719d2fSPhilippe CORNU 	struct video_priv *uc_priv = dev_get_uclass_priv(dev);
33272719d2fSPhilippe CORNU 	struct stm32_ltdc_priv *priv = dev_get_priv(dev);
33372719d2fSPhilippe CORNU 	struct udevice *panel;
3342a0e8784Syannick fertre 	struct clk pclk;
335c0fb2fc0Syannick fertre 	struct reset_ctl rst;
3362a0e8784Syannick fertre 	int rate, ret;
33772719d2fSPhilippe CORNU 
33872719d2fSPhilippe CORNU 	priv->regs = (void *)dev_read_addr(dev);
33972719d2fSPhilippe CORNU 	if ((fdt_addr_t)priv->regs == FDT_ADDR_T_NONE) {
34072719d2fSPhilippe CORNU 		debug("%s: ltdc dt register address error\n", __func__);
34172719d2fSPhilippe CORNU 		return -EINVAL;
34272719d2fSPhilippe CORNU 	}
34372719d2fSPhilippe CORNU 
3442a0e8784Syannick fertre 	ret = clk_get_by_index(dev, 0, &pclk);
34572719d2fSPhilippe CORNU 	if (ret) {
3462a0e8784Syannick fertre 		debug("%s: peripheral clock get error %d\n", __func__, ret);
3472a0e8784Syannick fertre 		return ret;
3482a0e8784Syannick fertre 	}
3492a0e8784Syannick fertre 
3502a0e8784Syannick fertre 	ret = clk_enable(&pclk);
3512a0e8784Syannick fertre 	if (ret) {
3522a0e8784Syannick fertre 		debug("%s: peripheral clock enable error %d\n",
3532a0e8784Syannick fertre 		      __func__, ret);
35472719d2fSPhilippe CORNU 		return ret;
35572719d2fSPhilippe CORNU 	}
35672719d2fSPhilippe CORNU 
357c0fb2fc0Syannick fertre 	ret = reset_get_by_index(dev, 0, &rst);
358c0fb2fc0Syannick fertre 	if (ret) {
359c0fb2fc0Syannick fertre 		debug("%s: missing ltdc hardware reset\n", __func__);
360c0fb2fc0Syannick fertre 		return -ENODEV;
361c0fb2fc0Syannick fertre 	}
362c0fb2fc0Syannick fertre 
363c0fb2fc0Syannick fertre 	/* Reset */
364c0fb2fc0Syannick fertre 	reset_deassert(&rst);
365c0fb2fc0Syannick fertre 
3662a0e8784Syannick fertre 	ret = uclass_first_device(UCLASS_PANEL, &panel);
3672a0e8784Syannick fertre 	if (ret) {
3682a0e8784Syannick fertre 		debug("%s: panel device error %d\n", __func__, ret);
3692a0e8784Syannick fertre 		return ret;
3702a0e8784Syannick fertre 	}
3712a0e8784Syannick fertre 
37272719d2fSPhilippe CORNU 	ret = panel_enable_backlight(panel);
37372719d2fSPhilippe CORNU 	if (ret) {
37472719d2fSPhilippe CORNU 		debug("%s: panel %s enable backlight error %d\n",
37572719d2fSPhilippe CORNU 		      __func__, panel->name, ret);
37672719d2fSPhilippe CORNU 		return ret;
37772719d2fSPhilippe CORNU 	}
37872719d2fSPhilippe CORNU 
3792a0e8784Syannick fertre 	ret = fdtdec_decode_display_timing(gd->fdt_blob,
3802a0e8784Syannick fertre 					   dev_of_offset(dev), 0,
3812a0e8784Syannick fertre 					   &priv->timing);
38272719d2fSPhilippe CORNU 	if (ret) {
3832a0e8784Syannick fertre 		debug("%s: decode display timing error %d\n",
3842a0e8784Syannick fertre 		      __func__, ret);
38572719d2fSPhilippe CORNU 		return -EINVAL;
38672719d2fSPhilippe CORNU 	}
38772719d2fSPhilippe CORNU 
3882a0e8784Syannick fertre 	rate = clk_set_rate(&pclk, priv->timing.pixelclock.typ);
3892a0e8784Syannick fertre 	if (rate < 0) {
3902a0e8784Syannick fertre 		debug("%s: fail to set pixel clock %d hz %d hz\n",
3912a0e8784Syannick fertre 		      __func__, priv->timing.pixelclock.typ, rate);
3922a0e8784Syannick fertre 		return rate;
39372719d2fSPhilippe CORNU 	}
39472719d2fSPhilippe CORNU 
3952a0e8784Syannick fertre 	debug("%s: Set pixel clock req %d hz get %d hz\n", __func__,
3962a0e8784Syannick fertre 	      priv->timing.pixelclock.typ, rate);
39772719d2fSPhilippe CORNU 
39872719d2fSPhilippe CORNU 	/* TODO Below parameters are hard-coded for the moment... */
39972719d2fSPhilippe CORNU 	priv->l2bpp = VIDEO_BPP16;
40072719d2fSPhilippe CORNU 	priv->bg_col_argb = 0xFFFFFFFF; /* white no transparency */
40172719d2fSPhilippe CORNU 	priv->crop_x = 0;
40272719d2fSPhilippe CORNU 	priv->crop_y = 0;
40372719d2fSPhilippe CORNU 	priv->crop_w = priv->timing.hactive.typ;
40472719d2fSPhilippe CORNU 	priv->crop_h = priv->timing.vactive.typ;
40572719d2fSPhilippe CORNU 	priv->alpha = 0xFF;
40672719d2fSPhilippe CORNU 
40772719d2fSPhilippe CORNU 	debug("%s: %dx%d %dbpp frame buffer at 0x%lx\n", __func__,
40872719d2fSPhilippe CORNU 	      priv->timing.hactive.typ, priv->timing.vactive.typ,
40972719d2fSPhilippe CORNU 	      VNBITS(priv->l2bpp), uc_plat->base);
41072719d2fSPhilippe CORNU 	debug("%s: crop %d,%d %dx%d bg 0x%08x alpha %d\n", __func__,
41172719d2fSPhilippe CORNU 	      priv->crop_x, priv->crop_y, priv->crop_w, priv->crop_h,
41272719d2fSPhilippe CORNU 	      priv->bg_col_argb, priv->alpha);
41372719d2fSPhilippe CORNU 
41472719d2fSPhilippe CORNU 	/* Configure & start LTDC */
41572719d2fSPhilippe CORNU 	stm32_ltdc_set_mode(priv);
41672719d2fSPhilippe CORNU 	stm32_ltdc_set_layer1(priv, uc_plat->base);
41772719d2fSPhilippe CORNU 	stm32_ltdc_enable(priv);
41872719d2fSPhilippe CORNU 
41972719d2fSPhilippe CORNU 	uc_priv->xsize = priv->timing.hactive.typ;
42072719d2fSPhilippe CORNU 	uc_priv->ysize = priv->timing.vactive.typ;
42172719d2fSPhilippe CORNU 	uc_priv->bpix = priv->l2bpp;
42272719d2fSPhilippe CORNU 
42372719d2fSPhilippe CORNU 	video_set_flush_dcache(dev, true);
42472719d2fSPhilippe CORNU 
42572719d2fSPhilippe CORNU 	return 0;
42672719d2fSPhilippe CORNU }
42772719d2fSPhilippe CORNU 
stm32_ltdc_bind(struct udevice * dev)42872719d2fSPhilippe CORNU static int stm32_ltdc_bind(struct udevice *dev)
42972719d2fSPhilippe CORNU {
43072719d2fSPhilippe CORNU 	struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
43172719d2fSPhilippe CORNU 
43272719d2fSPhilippe CORNU 	uc_plat->size = CONFIG_VIDEO_STM32_MAX_XRES *
43372719d2fSPhilippe CORNU 			CONFIG_VIDEO_STM32_MAX_YRES *
43472719d2fSPhilippe CORNU 			(CONFIG_VIDEO_STM32_MAX_BPP >> 3);
43572719d2fSPhilippe CORNU 	debug("%s: frame buffer max size %d bytes\n", __func__, uc_plat->size);
43672719d2fSPhilippe CORNU 
43772719d2fSPhilippe CORNU 	return 0;
43872719d2fSPhilippe CORNU }
43972719d2fSPhilippe CORNU 
44072719d2fSPhilippe CORNU static const struct udevice_id stm32_ltdc_ids[] = {
44172719d2fSPhilippe CORNU 	{ .compatible = "st,stm32-ltdc" },
44272719d2fSPhilippe CORNU 	{ }
44372719d2fSPhilippe CORNU };
44472719d2fSPhilippe CORNU 
44572719d2fSPhilippe CORNU U_BOOT_DRIVER(stm32_ltdc) = {
446c4c33e9dSyannick fertre 	.name			= "stm32_display",
44772719d2fSPhilippe CORNU 	.id			= UCLASS_VIDEO,
44872719d2fSPhilippe CORNU 	.of_match		= stm32_ltdc_ids,
44972719d2fSPhilippe CORNU 	.probe			= stm32_ltdc_probe,
45072719d2fSPhilippe CORNU 	.bind			= stm32_ltdc_bind,
45172719d2fSPhilippe CORNU 	.priv_auto_alloc_size	= sizeof(struct stm32_ltdc_priv),
45272719d2fSPhilippe CORNU };
453