blob: ae8ec26468f029cfd36cd3d17ba5b02210ac6041 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stelian Pop39cf4802008-05-09 21:57:18 +02002/*
3 * Driver for AT91/AT32 LCD Controller
4 *
5 * Copyright (C) 2007 Atmel Corporation
Stelian Pop39cf4802008-05-09 21:57:18 +02006 */
7
8#include <common.h>
Simon Glass9dc89a02016-05-05 07:28:20 -06009#include <atmel_lcd.h>
10#include <dm.h>
Simon Glassd63ec262016-05-05 07:28:19 -060011#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <part.h>
Simon Glass9dc89a02016-05-05 07:28:20 -060014#include <video.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020015#include <asm/io.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020016#include <asm/arch/gpio.h>
17#include <asm/arch/clk.h>
18#include <lcd.h>
Nikita Kiryanov0b29a892015-02-03 13:32:27 +020019#include <bmp_layout.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020020#include <atmel_lcdc.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Stelian Pop39cf4802008-05-09 21:57:18 +020022
Simon Glass9dc89a02016-05-05 07:28:20 -060023DECLARE_GLOBAL_DATA_PTR;
24
25#ifdef CONFIG_DM_VIDEO
26enum {
27 /* Maximum LCD size we support */
28 LCD_MAX_WIDTH = 1366,
29 LCD_MAX_HEIGHT = 768,
30 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
31};
32#endif
33
34struct atmel_fb_priv {
35 struct display_timing timing;
36};
37
Stelian Pop39cf4802008-05-09 21:57:18 +020038/* configurable parameters */
39#define ATMEL_LCDC_CVAL_DEFAULT 0xc8
40#define ATMEL_LCDC_DMA_BURST_LEN 8
Mark Jackson6bbced62009-06-29 15:59:10 +010041#ifndef ATMEL_LCDC_GUARD_TIME
42#define ATMEL_LCDC_GUARD_TIME 1
43#endif
Stelian Pop39cf4802008-05-09 21:57:18 +020044
Bo Shenc6941e12015-01-16 10:55:46 +080045#if defined(CONFIG_AT91SAM9263)
Stelian Pop39cf4802008-05-09 21:57:18 +020046#define ATMEL_LCDC_FIFO_SIZE 2048
47#else
48#define ATMEL_LCDC_FIFO_SIZE 512
49#endif
50
51#define lcdc_readl(mmio, reg) __raw_readl((mmio)+(reg))
52#define lcdc_writel(mmio, reg, val) __raw_writel((val), (mmio)+(reg))
53
Simon Glass9dc89a02016-05-05 07:28:20 -060054#ifndef CONFIG_DM_VIDEO
Nikita Kiryanov38b55082015-02-03 13:32:21 +020055ushort *configuration_get_cmap(void)
56{
57 return (ushort *)(panel_info.mmio + ATMEL_LCDC_LUT(0));
58}
59
Nikita Kiryanovb3d12e92015-02-03 13:32:22 +020060#if defined(CONFIG_BMP_16BPP) && defined(CONFIG_ATMEL_LCD_BGR555)
61void fb_put_word(uchar **fb, uchar **from)
62{
63 *(*fb)++ = (((*from)[0] & 0x1f) << 2) | ((*from)[1] & 0x03);
64 *(*fb)++ = ((*from)[0] & 0xe0) | (((*from)[1] & 0x7c) >> 2);
65 *from += 2;
66}
67#endif
68
Nikita Kiryanova02e9482015-02-03 13:32:24 +020069#ifdef CONFIG_LCD_LOGO
70#include <bmp_logo.h>
71void lcd_logo_set_cmap(void)
72{
73 int i;
74 uint lut_entry;
75 ushort colreg;
76 uint *cmap = (uint *)configuration_get_cmap();
77
78 for (i = 0; i < BMP_LOGO_COLORS; ++i) {
79 colreg = bmp_logo_palette[i];
80#ifdef CONFIG_ATMEL_LCD_BGR555
81 lut_entry = ((colreg & 0x000F) << 11) |
82 ((colreg & 0x00F0) << 2) |
83 ((colreg & 0x0F00) >> 7);
84#else
85 lut_entry = ((colreg & 0x000F) << 1) |
86 ((colreg & 0x00F0) << 3) |
87 ((colreg & 0x0F00) << 4);
88#endif
89 *(cmap + BMP_LOGO_OFFSET) = lut_entry;
90 cmap++;
91 }
92}
93#endif
94
Stelian Pop39cf4802008-05-09 21:57:18 +020095void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
96{
97#if defined(CONFIG_ATMEL_LCD_BGR555)
98 lcdc_writel(panel_info.mmio, ATMEL_LCDC_LUT(regno),
99 (red >> 3) | ((green & 0xf8) << 2) | ((blue & 0xf8) << 7));
100#else
101 lcdc_writel(panel_info.mmio, ATMEL_LCDC_LUT(regno),
102 (blue >> 3) | ((green & 0xfc) << 3) | ((red & 0xf8) << 8));
103#endif
104}
105
Simon Glass1c3dbe52015-05-13 07:02:27 -0600106void lcd_set_cmap(struct bmp_image *bmp, unsigned colors)
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200107{
108 int i;
109
110 for (i = 0; i < colors; ++i) {
Simon Glass1c3dbe52015-05-13 07:02:27 -0600111 struct bmp_color_table_entry cte = bmp->color_table[i];
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200112 lcd_setcolreg(i, cte.red, cte.green, cte.blue);
113 }
114}
Simon Glass9dc89a02016-05-05 07:28:20 -0600115#endif
Nikita Kiryanov0b29a892015-02-03 13:32:27 +0200116
Simon Glassd63ec262016-05-05 07:28:19 -0600117static void atmel_fb_init(ulong addr, struct display_timing *timing, int bpix,
118 bool tft, bool cont_pol_low, ulong lcdbase)
Stelian Pop39cf4802008-05-09 21:57:18 +0200119{
120 unsigned long value;
Simon Glassd63ec262016-05-05 07:28:19 -0600121 void *reg = (void *)addr;
Stelian Pop39cf4802008-05-09 21:57:18 +0200122
123 /* Turn off the LCD controller and the DMA controller */
Simon Glassd63ec262016-05-05 07:28:19 -0600124 lcdc_writel(reg, ATMEL_LCDC_PWRCON,
Mark Jackson6bbced62009-06-29 15:59:10 +0100125 ATMEL_LCDC_GUARD_TIME << ATMEL_LCDC_GUARDT_OFFSET);
Stelian Pop39cf4802008-05-09 21:57:18 +0200126
127 /* Wait for the LCDC core to become idle */
Simon Glassd63ec262016-05-05 07:28:19 -0600128 while (lcdc_readl(reg, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
Stelian Pop39cf4802008-05-09 21:57:18 +0200129 udelay(10);
130
Simon Glassd63ec262016-05-05 07:28:19 -0600131 lcdc_writel(reg, ATMEL_LCDC_DMACON, 0);
Stelian Pop39cf4802008-05-09 21:57:18 +0200132
133 /* Reset LCDC DMA */
Simon Glassd63ec262016-05-05 07:28:19 -0600134 lcdc_writel(reg, ATMEL_LCDC_DMACON, ATMEL_LCDC_DMARST);
Stelian Pop39cf4802008-05-09 21:57:18 +0200135
136 /* ...set frame size and burst length = 8 words (?) */
Simon Glassd63ec262016-05-05 07:28:19 -0600137 value = (timing->hactive.typ * timing->vactive.typ *
138 (1 << bpix)) / 32;
Stelian Pop39cf4802008-05-09 21:57:18 +0200139 value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
Simon Glassd63ec262016-05-05 07:28:19 -0600140 lcdc_writel(reg, ATMEL_LCDC_DMAFRMCFG, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200141
142 /* Set pixel clock */
Simon Glassd63ec262016-05-05 07:28:19 -0600143 value = get_lcdc_clk_rate(0) / timing->pixelclock.typ;
144 if (get_lcdc_clk_rate(0) % timing->pixelclock.typ)
Stelian Pop39cf4802008-05-09 21:57:18 +0200145 value++;
146 value = (value / 2) - 1;
147
148 if (!value) {
Simon Glassd63ec262016-05-05 07:28:19 -0600149 lcdc_writel(reg, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
Stelian Pop39cf4802008-05-09 21:57:18 +0200150 } else
Simon Glassd63ec262016-05-05 07:28:19 -0600151 lcdc_writel(reg, ATMEL_LCDC_LCDCON1,
Stelian Pop39cf4802008-05-09 21:57:18 +0200152 value << ATMEL_LCDC_CLKVAL_OFFSET);
153
154 /* Initialize control register 2 */
155 value = ATMEL_LCDC_MEMOR_LITTLE | ATMEL_LCDC_CLKMOD_ALWAYSACTIVE;
Simon Glassd63ec262016-05-05 07:28:19 -0600156 if (tft)
Stelian Pop39cf4802008-05-09 21:57:18 +0200157 value |= ATMEL_LCDC_DISTYPE_TFT;
158
Simon Glassd63ec262016-05-05 07:28:19 -0600159 if (!(timing->flags & DISPLAY_FLAGS_HSYNC_HIGH))
160 value |= ATMEL_LCDC_INVLINE_INVERTED;
161 if (!(timing->flags & DISPLAY_FLAGS_VSYNC_HIGH))
162 value |= ATMEL_LCDC_INVFRAME_INVERTED;
163 value |= bpix << 5;
164 lcdc_writel(reg, ATMEL_LCDC_LCDCON2, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200165
166 /* Vertical timing */
Simon Glassd63ec262016-05-05 07:28:19 -0600167 value = (timing->vsync_len.typ - 1) << ATMEL_LCDC_VPW_OFFSET;
168 value |= timing->vback_porch.typ << ATMEL_LCDC_VBP_OFFSET;
169 value |= timing->vfront_porch.typ;
170 /* Magic! (Datasheet says "Bit 31 must be written to 1") */
171 value |= 1U << 31;
172 lcdc_writel(reg, ATMEL_LCDC_TIM1, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200173
174 /* Horizontal timing */
Simon Glassd63ec262016-05-05 07:28:19 -0600175 value = (timing->hfront_porch.typ - 1) << ATMEL_LCDC_HFP_OFFSET;
176 value |= (timing->hsync_len.typ - 1) << ATMEL_LCDC_HPW_OFFSET;
177 value |= (timing->hback_porch.typ - 1);
178 lcdc_writel(reg, ATMEL_LCDC_TIM2, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200179
180 /* Display size */
Simon Glassd63ec262016-05-05 07:28:19 -0600181 value = (timing->hactive.typ - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
182 value |= timing->vactive.typ - 1;
183 lcdc_writel(reg, ATMEL_LCDC_LCDFRMCFG, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200184
185 /* FIFO Threshold: Use formula from data sheet */
186 value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
Simon Glassd63ec262016-05-05 07:28:19 -0600187 lcdc_writel(reg, ATMEL_LCDC_FIFO, value);
Stelian Pop39cf4802008-05-09 21:57:18 +0200188
189 /* Toggle LCD_MODE every frame */
Simon Glassd63ec262016-05-05 07:28:19 -0600190 lcdc_writel(reg, ATMEL_LCDC_MVAL, 0);
Stelian Pop39cf4802008-05-09 21:57:18 +0200191
192 /* Disable all interrupts */
Simon Glassd63ec262016-05-05 07:28:19 -0600193 lcdc_writel(reg, ATMEL_LCDC_IDR, ~0UL);
Stelian Pop39cf4802008-05-09 21:57:18 +0200194
195 /* Set contrast */
196 value = ATMEL_LCDC_PS_DIV8 |
Stelian Pop39cf4802008-05-09 21:57:18 +0200197 ATMEL_LCDC_ENA_PWMENABLE;
Simon Glassd63ec262016-05-05 07:28:19 -0600198 if (!cont_pol_low)
Alexander Steincdfcedb2010-07-20 08:55:40 +0200199 value |= ATMEL_LCDC_POL_POSITIVE;
Simon Glassd63ec262016-05-05 07:28:19 -0600200 lcdc_writel(reg, ATMEL_LCDC_CONTRAST_CTR, value);
201 lcdc_writel(reg, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
Stelian Pop39cf4802008-05-09 21:57:18 +0200202
203 /* Set framebuffer DMA base address and pixel offset */
Simon Glassd63ec262016-05-05 07:28:19 -0600204 lcdc_writel(reg, ATMEL_LCDC_DMABADDR1, lcdbase);
Stelian Pop39cf4802008-05-09 21:57:18 +0200205
Simon Glassd63ec262016-05-05 07:28:19 -0600206 lcdc_writel(reg, ATMEL_LCDC_DMACON, ATMEL_LCDC_DMAEN);
207 lcdc_writel(reg, ATMEL_LCDC_PWRCON,
Mark Jackson6bbced62009-06-29 15:59:10 +0100208 (ATMEL_LCDC_GUARD_TIME << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR);
Stelian Pop39cf4802008-05-09 21:57:18 +0200209}
210
Simon Glass9dc89a02016-05-05 07:28:20 -0600211#ifndef CONFIG_DM_VIDEO
Simon Glassd63ec262016-05-05 07:28:19 -0600212void lcd_ctrl_init(void *lcdbase)
213{
214 struct display_timing timing;
215
216 timing.flags = 0;
217 if (!(panel_info.vl_sync & ATMEL_LCDC_INVLINE_INVERTED))
218 timing.flags |= DISPLAY_FLAGS_HSYNC_HIGH;
219 if (!(panel_info.vl_sync & ATMEL_LCDC_INVFRAME_INVERTED))
220 timing.flags |= DISPLAY_FLAGS_VSYNC_LOW;
221 timing.pixelclock.typ = panel_info.vl_clk;
222
223 timing.hactive.typ = panel_info.vl_col;
224 timing.hfront_porch.typ = panel_info.vl_right_margin;
225 timing.hback_porch.typ = panel_info.vl_left_margin;
226 timing.hsync_len.typ = panel_info.vl_hsync_len;
227
228 timing.vactive.typ = panel_info.vl_row;
229 timing.vfront_porch.typ = panel_info.vl_clk;
230 timing.vback_porch.typ = panel_info.vl_clk;
231 timing.vsync_len.typ = panel_info.vl_clk;
232
233 atmel_fb_init(panel_info.mmio, &timing, panel_info.vl_bpix,
234 panel_info.vl_tft, panel_info.vl_cont_pol_low,
235 (ulong)lcdbase);
236}
237
Stelian Pop39cf4802008-05-09 21:57:18 +0200238ulong calc_fbsize(void)
239{
240 return ((panel_info.vl_col * panel_info.vl_row *
241 NBITS(panel_info.vl_bpix)) / 8) + PAGE_SIZE;
242}
Simon Glass9dc89a02016-05-05 07:28:20 -0600243#endif
244
245#ifdef CONFIG_DM_VIDEO
246static int atmel_fb_lcd_probe(struct udevice *dev)
247{
248 struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
249 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
250 struct atmel_fb_priv *priv = dev_get_priv(dev);
251 struct display_timing *timing = &priv->timing;
252
253 /*
254 * For now some values are hard-coded. We could use the device tree
255 * bindings in simple-framebuffer.txt to specify the format/bpp and
256 * some Atmel-specific binding for tft and cont_pol_low.
257 */
258 atmel_fb_init(ATMEL_BASE_LCDC, timing, VIDEO_BPP16, true, false,
259 uc_plat->base);
260 uc_priv->xsize = timing->hactive.typ;
261 uc_priv->ysize = timing->vactive.typ;
262 uc_priv->bpix = VIDEO_BPP16;
263 video_set_flush_dcache(dev, true);
264 debug("LCD frame buffer at %lx, size %x, %dx%d pixels\n", uc_plat->base,
265 uc_plat->size, uc_priv->xsize, uc_priv->ysize);
266
267 return 0;
268}
269
270static int atmel_fb_ofdata_to_platdata(struct udevice *dev)
271{
272 struct atmel_lcd_platdata *plat = dev_get_platdata(dev);
273 struct atmel_fb_priv *priv = dev_get_priv(dev);
274 struct display_timing *timing = &priv->timing;
275 const void *blob = gd->fdt_blob;
276
Simon Glasse160f7d2017-01-17 16:52:55 -0700277 if (fdtdec_decode_display_timing(blob, dev_of_offset(dev),
Simon Glass9dc89a02016-05-05 07:28:20 -0600278 plat->timing_index, timing)) {
279 debug("%s: Failed to decode display timing\n", __func__);
280 return -EINVAL;
281 }
282
283 return 0;
284}
285
286static int atmel_fb_lcd_bind(struct udevice *dev)
287{
288 struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
289
290 uc_plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
291 (1 << VIDEO_BPP16) / 8;
292 debug("%s: Frame buffer size %x\n", __func__, uc_plat->size);
293
294 return 0;
295}
296
297static const struct udevice_id atmel_fb_lcd_ids[] = {
298 { .compatible = "atmel,at91sam9g45-lcdc" },
299 { }
300};
301
302U_BOOT_DRIVER(atmel_fb) = {
303 .name = "atmel_fb",
304 .id = UCLASS_VIDEO,
305 .of_match = atmel_fb_lcd_ids,
306 .bind = atmel_fb_lcd_bind,
307 .ofdata_to_platdata = atmel_fb_ofdata_to_platdata,
308 .probe = atmel_fb_lcd_probe,
309 .platdata_auto_alloc_size = sizeof(struct atmel_lcd_platdata),
310 .priv_auto_alloc_size = sizeof(struct atmel_fb_priv),
311};
312#endif