2 * Driver for AT91/AT32 LCD Controller
4 * Copyright (C) 2007 Atmel Corporation
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/interrupt.h>
15 #include <linux/clk.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/backlight.h>
21 #include <mach/board.h>
23 #include <mach/gpio.h>
25 #include <video/atmel_lcdc.h>
27 #define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
28 #define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
30 /* configurable parameters */
31 #define ATMEL_LCDC_CVAL_DEFAULT 0xc8
32 #define ATMEL_LCDC_DMA_BURST_LEN 8
34 #if defined(CONFIG_ARCH_AT91SAM9263) || defined(CONFIG_ARCH_AT91CAP9) || \
35 defined(CONFIG_ARCH_AT91SAM9RL)
36 #define ATMEL_LCDC_FIFO_SIZE 2048
38 #define ATMEL_LCDC_FIFO_SIZE 512
41 #if defined(CONFIG_ARCH_AT91)
42 #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
43 | FBINFO_PARTIAL_PAN_OK \
44 | FBINFO_HWACCEL_YPAN)
46 static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info
*sinfo
,
47 struct fb_var_screeninfo
*var
)
51 #elif defined(CONFIG_AVR32)
52 #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
53 | FBINFO_PARTIAL_PAN_OK \
54 | FBINFO_HWACCEL_XPAN \
55 | FBINFO_HWACCEL_YPAN)
57 static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info
*sinfo
,
58 struct fb_var_screeninfo
*var
)
63 pixeloff
= (var
->xoffset
* var
->bits_per_pixel
) & 0x1f;
65 dma2dcfg
= ((var
->xres_virtual
- var
->xres
) * var
->bits_per_pixel
) / 8;
66 dma2dcfg
|= pixeloff
<< ATMEL_LCDC_PIXELOFF_OFFSET
;
67 lcdc_writel(sinfo
, ATMEL_LCDC_DMA2DCFG
, dma2dcfg
);
69 /* Update configuration */
70 lcdc_writel(sinfo
, ATMEL_LCDC_DMACON
,
71 lcdc_readl(sinfo
, ATMEL_LCDC_DMACON
)
72 | ATMEL_LCDC_DMAUPDT
);
76 static const u32 contrast_ctr
= ATMEL_LCDC_PS_DIV8
77 | ATMEL_LCDC_POL_POSITIVE
78 | ATMEL_LCDC_ENA_PWMENABLE
;
80 #ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
82 /* some bl->props field just changed */
83 static int atmel_bl_update_status(struct backlight_device
*bl
)
85 struct atmel_lcdfb_info
*sinfo
= bl_get_data(bl
);
86 int power
= sinfo
->bl_power
;
87 int brightness
= bl
->props
.brightness
;
89 /* REVISIT there may be a meaningful difference between
90 * fb_blank and power ... there seem to be some cases
91 * this doesn't handle correctly.
93 if (bl
->props
.fb_blank
!= sinfo
->bl_power
)
94 power
= bl
->props
.fb_blank
;
95 else if (bl
->props
.power
!= sinfo
->bl_power
)
96 power
= bl
->props
.power
;
98 if (brightness
< 0 && power
== FB_BLANK_UNBLANK
)
99 brightness
= lcdc_readl(sinfo
, ATMEL_LCDC_CONTRAST_VAL
);
100 else if (power
!= FB_BLANK_UNBLANK
)
103 lcdc_writel(sinfo
, ATMEL_LCDC_CONTRAST_VAL
, brightness
);
104 lcdc_writel(sinfo
, ATMEL_LCDC_CONTRAST_CTR
,
105 brightness
? contrast_ctr
: 0);
107 bl
->props
.fb_blank
= bl
->props
.power
= sinfo
->bl_power
= power
;
112 static int atmel_bl_get_brightness(struct backlight_device
*bl
)
114 struct atmel_lcdfb_info
*sinfo
= bl_get_data(bl
);
116 return lcdc_readl(sinfo
, ATMEL_LCDC_CONTRAST_VAL
);
119 static struct backlight_ops atmel_lcdc_bl_ops
= {
120 .update_status
= atmel_bl_update_status
,
121 .get_brightness
= atmel_bl_get_brightness
,
124 static void init_backlight(struct atmel_lcdfb_info
*sinfo
)
126 struct backlight_device
*bl
;
128 sinfo
->bl_power
= FB_BLANK_UNBLANK
;
130 if (sinfo
->backlight
)
133 bl
= backlight_device_register("backlight", &sinfo
->pdev
->dev
,
134 sinfo
, &atmel_lcdc_bl_ops
);
135 if (IS_ERR(sinfo
->backlight
)) {
136 dev_err(&sinfo
->pdev
->dev
, "error %ld on backlight register\n",
140 sinfo
->backlight
= bl
;
142 bl
->props
.power
= FB_BLANK_UNBLANK
;
143 bl
->props
.fb_blank
= FB_BLANK_UNBLANK
;
144 bl
->props
.max_brightness
= 0xff;
145 bl
->props
.brightness
= atmel_bl_get_brightness(bl
);
148 static void exit_backlight(struct atmel_lcdfb_info
*sinfo
)
150 if (sinfo
->backlight
)
151 backlight_device_unregister(sinfo
->backlight
);
156 static void init_backlight(struct atmel_lcdfb_info
*sinfo
)
158 dev_warn(&sinfo
->pdev
->dev
, "backlight control is not available\n");
161 static void exit_backlight(struct atmel_lcdfb_info
*sinfo
)
167 static void init_contrast(struct atmel_lcdfb_info
*sinfo
)
169 /* have some default contrast/backlight settings */
170 lcdc_writel(sinfo
, ATMEL_LCDC_CONTRAST_CTR
, contrast_ctr
);
171 lcdc_writel(sinfo
, ATMEL_LCDC_CONTRAST_VAL
, ATMEL_LCDC_CVAL_DEFAULT
);
173 if (sinfo
->lcdcon_is_backlight
)
174 init_backlight(sinfo
);
178 static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata
= {
179 .type
= FB_TYPE_PACKED_PIXELS
,
180 .visual
= FB_VISUAL_TRUECOLOR
,
184 .accel
= FB_ACCEL_NONE
,
187 static unsigned long compute_hozval(unsigned long xres
, unsigned long lcdcon2
)
191 if (!(cpu_is_at91sam9261() || cpu_is_at32ap7000()))
195 if ((lcdcon2
& ATMEL_LCDC_DISTYPE
) != ATMEL_LCDC_DISTYPE_TFT
) {
197 if ((lcdcon2
& ATMEL_LCDC_DISTYPE
) == ATMEL_LCDC_DISTYPE_STNCOLOR
) {
200 if ( (lcdcon2
& ATMEL_LCDC_IFWIDTH
) == ATMEL_LCDC_IFWIDTH_4
201 || ( (lcdcon2
& ATMEL_LCDC_IFWIDTH
) == ATMEL_LCDC_IFWIDTH_8
202 && (lcdcon2
& ATMEL_LCDC_SCANMOD
) == ATMEL_LCDC_SCANMOD_DUAL
))
203 value
= DIV_ROUND_UP(value
, 4);
205 value
= DIV_ROUND_UP(value
, 8);
211 static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info
*sinfo
)
213 /* Turn off the LCD controller and the DMA controller */
214 lcdc_writel(sinfo
, ATMEL_LCDC_PWRCON
,
215 sinfo
->guard_time
<< ATMEL_LCDC_GUARDT_OFFSET
);
217 /* Wait for the LCDC core to become idle */
218 while (lcdc_readl(sinfo
, ATMEL_LCDC_PWRCON
) & ATMEL_LCDC_BUSY
)
221 lcdc_writel(sinfo
, ATMEL_LCDC_DMACON
, 0);
224 static void atmel_lcdfb_stop(struct atmel_lcdfb_info
*sinfo
)
226 atmel_lcdfb_stop_nowait(sinfo
);
228 /* Wait for DMA engine to become idle... */
229 while (lcdc_readl(sinfo
, ATMEL_LCDC_DMACON
) & ATMEL_LCDC_DMABUSY
)
233 static void atmel_lcdfb_start(struct atmel_lcdfb_info
*sinfo
)
235 lcdc_writel(sinfo
, ATMEL_LCDC_DMACON
, sinfo
->default_dmacon
);
236 lcdc_writel(sinfo
, ATMEL_LCDC_PWRCON
,
237 (sinfo
->guard_time
<< ATMEL_LCDC_GUARDT_OFFSET
)
241 static void atmel_lcdfb_update_dma(struct fb_info
*info
,
242 struct fb_var_screeninfo
*var
)
244 struct atmel_lcdfb_info
*sinfo
= info
->par
;
245 struct fb_fix_screeninfo
*fix
= &info
->fix
;
246 unsigned long dma_addr
;
248 dma_addr
= (fix
->smem_start
+ var
->yoffset
* fix
->line_length
249 + var
->xoffset
* var
->bits_per_pixel
/ 8);
253 /* Set framebuffer DMA base address and pixel offset */
254 lcdc_writel(sinfo
, ATMEL_LCDC_DMABADDR1
, dma_addr
);
256 atmel_lcdfb_update_dma2d(sinfo
, var
);
259 static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info
*sinfo
)
261 struct fb_info
*info
= sinfo
->info
;
263 dma_free_writecombine(info
->device
, info
->fix
.smem_len
,
264 info
->screen_base
, info
->fix
.smem_start
);
268 * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
269 * @sinfo: the frame buffer to allocate memory for
271 static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info
*sinfo
)
273 struct fb_info
*info
= sinfo
->info
;
274 struct fb_var_screeninfo
*var
= &info
->var
;
275 unsigned int smem_len
;
277 smem_len
= (var
->xres_virtual
* var
->yres_virtual
278 * ((var
->bits_per_pixel
+ 7) / 8));
279 info
->fix
.smem_len
= max(smem_len
, sinfo
->smem_len
);
281 info
->screen_base
= dma_alloc_writecombine(info
->device
, info
->fix
.smem_len
,
282 (dma_addr_t
*)&info
->fix
.smem_start
, GFP_KERNEL
);
284 if (!info
->screen_base
) {
288 memset(info
->screen_base
, 0, info
->fix
.smem_len
);
293 static const struct fb_videomode
*atmel_lcdfb_choose_mode(struct fb_var_screeninfo
*var
,
294 struct fb_info
*info
)
296 struct fb_videomode varfbmode
;
297 const struct fb_videomode
*fbmode
= NULL
;
299 fb_var_to_videomode(&varfbmode
, var
);
300 fbmode
= fb_find_nearest_mode(&varfbmode
, &info
->modelist
);
302 fb_videomode_to_var(var
, fbmode
);
308 * atmel_lcdfb_check_var - Validates a var passed in.
309 * @var: frame buffer variable screen structure
310 * @info: frame buffer structure that represents a single frame buffer
312 * Checks to see if the hardware supports the state requested by
313 * var passed in. This function does not alter the hardware
314 * state!!! This means the data stored in struct fb_info and
315 * struct atmel_lcdfb_info do not change. This includes the var
316 * inside of struct fb_info. Do NOT change these. This function
317 * can be called on its own if we intent to only test a mode and
318 * not actually set it. The stuff in modedb.c is a example of
319 * this. If the var passed in is slightly off by what the
320 * hardware can support then we alter the var PASSED in to what
321 * we can do. If the hardware doesn't support mode change a
322 * -EINVAL will be returned by the upper layers. You don't need
323 * to implement this function then. If you hardware doesn't
324 * support changing the resolution then this function is not
325 * needed. In this case the driver would just provide a var that
326 * represents the static state the screen is in.
328 * Returns negative errno on error, or zero on success.
330 static int atmel_lcdfb_check_var(struct fb_var_screeninfo
*var
,
331 struct fb_info
*info
)
333 struct device
*dev
= info
->device
;
334 struct atmel_lcdfb_info
*sinfo
= info
->par
;
335 unsigned long clk_value_khz
;
337 clk_value_khz
= clk_get_rate(sinfo
->lcdc_clk
) / 1000;
339 dev_dbg(dev
, "%s:\n", __func__
);
341 if (!(var
->pixclock
&& var
->bits_per_pixel
)) {
342 /* choose a suitable mode if possible */
343 if (!atmel_lcdfb_choose_mode(var
, info
)) {
344 dev_err(dev
, "needed value not specified\n");
349 dev_dbg(dev
, " resolution: %ux%u\n", var
->xres
, var
->yres
);
350 dev_dbg(dev
, " pixclk: %lu KHz\n", PICOS2KHZ(var
->pixclock
));
351 dev_dbg(dev
, " bpp: %u\n", var
->bits_per_pixel
);
352 dev_dbg(dev
, " clk: %lu KHz\n", clk_value_khz
);
354 if ((PICOS2KHZ(var
->pixclock
) * var
->bits_per_pixel
/ 8) > clk_value_khz
) {
355 dev_err(dev
, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var
->pixclock
));
359 /* Do not allow to have real resoulution larger than virtual */
360 if (var
->xres
> var
->xres_virtual
)
361 var
->xres_virtual
= var
->xres
;
363 if (var
->yres
> var
->yres_virtual
)
364 var
->yres_virtual
= var
->yres
;
366 /* Force same alignment for each line */
367 var
->xres
= (var
->xres
+ 3) & ~3UL;
368 var
->xres_virtual
= (var
->xres_virtual
+ 3) & ~3UL;
370 var
->red
.msb_right
= var
->green
.msb_right
= var
->blue
.msb_right
= 0;
371 var
->transp
.msb_right
= 0;
372 var
->transp
.offset
= var
->transp
.length
= 0;
373 var
->xoffset
= var
->yoffset
= 0;
375 /* Saturate vertical and horizontal timings at maximum values */
376 var
->vsync_len
= min_t(u32
, var
->vsync_len
,
377 (ATMEL_LCDC_VPW
>> ATMEL_LCDC_VPW_OFFSET
) + 1);
378 var
->upper_margin
= min_t(u32
, var
->upper_margin
,
379 ATMEL_LCDC_VBP
>> ATMEL_LCDC_VBP_OFFSET
);
380 var
->lower_margin
= min_t(u32
, var
->lower_margin
,
382 var
->right_margin
= min_t(u32
, var
->right_margin
,
383 (ATMEL_LCDC_HFP
>> ATMEL_LCDC_HFP_OFFSET
) + 1);
384 var
->hsync_len
= min_t(u32
, var
->hsync_len
,
385 (ATMEL_LCDC_HPW
>> ATMEL_LCDC_HPW_OFFSET
) + 1);
386 var
->left_margin
= min_t(u32
, var
->left_margin
,
389 /* Some parameters can't be zero */
390 var
->vsync_len
= max_t(u32
, var
->vsync_len
, 1);
391 var
->right_margin
= max_t(u32
, var
->right_margin
, 1);
392 var
->hsync_len
= max_t(u32
, var
->hsync_len
, 1);
393 var
->left_margin
= max_t(u32
, var
->left_margin
, 1);
395 switch (var
->bits_per_pixel
) {
400 var
->red
.offset
= var
->green
.offset
= var
->blue
.offset
= 0;
401 var
->red
.length
= var
->green
.length
= var
->blue
.length
402 = var
->bits_per_pixel
;
406 if (sinfo
->lcd_wiring_mode
== ATMEL_LCDC_WIRING_RGB
) {
408 var
->red
.offset
= 11;
409 var
->blue
.offset
= 0;
410 var
->green
.length
= 6;
414 var
->blue
.offset
= 10;
415 var
->green
.length
= 5;
417 var
->green
.offset
= 5;
418 var
->red
.length
= var
->blue
.length
= 5;
421 var
->transp
.offset
= 24;
422 var
->transp
.length
= 8;
425 if (sinfo
->lcd_wiring_mode
== ATMEL_LCDC_WIRING_RGB
) {
427 var
->red
.offset
= 16;
428 var
->blue
.offset
= 0;
432 var
->blue
.offset
= 16;
434 var
->green
.offset
= 8;
435 var
->red
.length
= var
->green
.length
= var
->blue
.length
= 8;
438 dev_err(dev
, "color depth %d not supported\n",
439 var
->bits_per_pixel
);
449 static void atmel_lcdfb_reset(struct atmel_lcdfb_info
*sinfo
)
453 atmel_lcdfb_stop(sinfo
);
454 atmel_lcdfb_start(sinfo
);
458 * atmel_lcdfb_set_par - Alters the hardware state.
459 * @info: frame buffer structure that represents a single frame buffer
461 * Using the fb_var_screeninfo in fb_info we set the resolution
462 * of the this particular framebuffer. This function alters the
463 * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
464 * not alter var in fb_info since we are using that data. This
465 * means we depend on the data in var inside fb_info to be
466 * supported by the hardware. atmel_lcdfb_check_var is always called
467 * before atmel_lcdfb_set_par to ensure this. Again if you can't
468 * change the resolution you don't need this function.
471 static int atmel_lcdfb_set_par(struct fb_info
*info
)
473 struct atmel_lcdfb_info
*sinfo
= info
->par
;
474 unsigned long hozval_linesz
;
476 unsigned long clk_value_khz
;
477 unsigned long bits_per_line
;
481 dev_dbg(info
->device
, "%s:\n", __func__
);
482 dev_dbg(info
->device
, " * resolution: %ux%u (%ux%u virtual)\n",
483 info
->var
.xres
, info
->var
.yres
,
484 info
->var
.xres_virtual
, info
->var
.yres_virtual
);
486 atmel_lcdfb_stop_nowait(sinfo
);
488 if (info
->var
.bits_per_pixel
== 1)
489 info
->fix
.visual
= FB_VISUAL_MONO01
;
490 else if (info
->var
.bits_per_pixel
<= 8)
491 info
->fix
.visual
= FB_VISUAL_PSEUDOCOLOR
;
493 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
495 bits_per_line
= info
->var
.xres_virtual
* info
->var
.bits_per_pixel
;
496 info
->fix
.line_length
= DIV_ROUND_UP(bits_per_line
, 8);
498 /* Re-initialize the DMA engine... */
499 dev_dbg(info
->device
, " * update DMA engine\n");
500 atmel_lcdfb_update_dma(info
, &info
->var
);
502 /* ...set frame size and burst length = 8 words (?) */
503 value
= (info
->var
.yres
* info
->var
.xres
* info
->var
.bits_per_pixel
) / 32;
504 value
|= ((ATMEL_LCDC_DMA_BURST_LEN
- 1) << ATMEL_LCDC_BLENGTH_OFFSET
);
505 lcdc_writel(sinfo
, ATMEL_LCDC_DMAFRMCFG
, value
);
507 /* Now, the LCDC core... */
509 /* Set pixel clock */
510 clk_value_khz
= clk_get_rate(sinfo
->lcdc_clk
) / 1000;
512 value
= DIV_ROUND_UP(clk_value_khz
, PICOS2KHZ(info
->var
.pixclock
));
515 dev_notice(info
->device
, "Bypassing pixel clock divider\n");
516 lcdc_writel(sinfo
, ATMEL_LCDC_LCDCON1
, ATMEL_LCDC_BYPASS
);
518 value
= (value
/ 2) - 1;
519 dev_dbg(info
->device
, " * programming CLKVAL = 0x%08lx\n",
521 lcdc_writel(sinfo
, ATMEL_LCDC_LCDCON1
,
522 value
<< ATMEL_LCDC_CLKVAL_OFFSET
);
523 info
->var
.pixclock
= KHZ2PICOS(clk_value_khz
/ (2 * (value
+ 1)));
524 dev_dbg(info
->device
, " updated pixclk: %lu KHz\n",
525 PICOS2KHZ(info
->var
.pixclock
));
529 /* Initialize control register 2 */
530 value
= sinfo
->default_lcdcon2
;
532 if (!(info
->var
.sync
& FB_SYNC_HOR_HIGH_ACT
))
533 value
|= ATMEL_LCDC_INVLINE_INVERTED
;
534 if (!(info
->var
.sync
& FB_SYNC_VERT_HIGH_ACT
))
535 value
|= ATMEL_LCDC_INVFRAME_INVERTED
;
537 switch (info
->var
.bits_per_pixel
) {
538 case 1: value
|= ATMEL_LCDC_PIXELSIZE_1
; break;
539 case 2: value
|= ATMEL_LCDC_PIXELSIZE_2
; break;
540 case 4: value
|= ATMEL_LCDC_PIXELSIZE_4
; break;
541 case 8: value
|= ATMEL_LCDC_PIXELSIZE_8
; break;
542 case 15: /* fall through */
543 case 16: value
|= ATMEL_LCDC_PIXELSIZE_16
; break;
544 case 24: value
|= ATMEL_LCDC_PIXELSIZE_24
; break;
545 case 32: value
|= ATMEL_LCDC_PIXELSIZE_32
; break;
546 default: BUG(); break;
548 dev_dbg(info
->device
, " * LCDCON2 = %08lx\n", value
);
549 lcdc_writel(sinfo
, ATMEL_LCDC_LCDCON2
, value
);
551 /* Vertical timing */
552 value
= (info
->var
.vsync_len
- 1) << ATMEL_LCDC_VPW_OFFSET
;
553 value
|= info
->var
.upper_margin
<< ATMEL_LCDC_VBP_OFFSET
;
554 value
|= info
->var
.lower_margin
;
555 dev_dbg(info
->device
, " * LCDTIM1 = %08lx\n", value
);
556 lcdc_writel(sinfo
, ATMEL_LCDC_TIM1
, value
);
558 /* Horizontal timing */
559 value
= (info
->var
.right_margin
- 1) << ATMEL_LCDC_HFP_OFFSET
;
560 value
|= (info
->var
.hsync_len
- 1) << ATMEL_LCDC_HPW_OFFSET
;
561 value
|= (info
->var
.left_margin
- 1);
562 dev_dbg(info
->device
, " * LCDTIM2 = %08lx\n", value
);
563 lcdc_writel(sinfo
, ATMEL_LCDC_TIM2
, value
);
565 /* Horizontal value (aka line size) */
566 hozval_linesz
= compute_hozval(info
->var
.xres
,
567 lcdc_readl(sinfo
, ATMEL_LCDC_LCDCON2
));
570 value
= (hozval_linesz
- 1) << ATMEL_LCDC_HOZVAL_OFFSET
;
571 value
|= info
->var
.yres
- 1;
572 dev_dbg(info
->device
, " * LCDFRMCFG = %08lx\n", value
);
573 lcdc_writel(sinfo
, ATMEL_LCDC_LCDFRMCFG
, value
);
575 /* FIFO Threshold: Use formula from data sheet */
576 value
= ATMEL_LCDC_FIFO_SIZE
- (2 * ATMEL_LCDC_DMA_BURST_LEN
+ 3);
577 lcdc_writel(sinfo
, ATMEL_LCDC_FIFO
, value
);
579 /* Toggle LCD_MODE every frame */
580 lcdc_writel(sinfo
, ATMEL_LCDC_MVAL
, 0);
582 /* Disable all interrupts */
583 lcdc_writel(sinfo
, ATMEL_LCDC_IDR
, ~0UL);
584 /* Enable FIFO & DMA errors */
585 lcdc_writel(sinfo
, ATMEL_LCDC_IER
, ATMEL_LCDC_UFLWI
| ATMEL_LCDC_OWRI
| ATMEL_LCDC_MERI
);
587 /* ...wait for DMA engine to become idle... */
588 while (lcdc_readl(sinfo
, ATMEL_LCDC_DMACON
) & ATMEL_LCDC_DMABUSY
)
591 atmel_lcdfb_start(sinfo
);
593 dev_dbg(info
->device
, " * DONE\n");
598 static inline unsigned int chan_to_field(unsigned int chan
, const struct fb_bitfield
*bf
)
601 chan
>>= 16 - bf
->length
;
602 return chan
<< bf
->offset
;
606 * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
607 * @regno: Which register in the CLUT we are programming
608 * @red: The red value which can be up to 16 bits wide
609 * @green: The green value which can be up to 16 bits wide
610 * @blue: The blue value which can be up to 16 bits wide.
611 * @transp: If supported the alpha value which can be up to 16 bits wide.
612 * @info: frame buffer info structure
614 * Set a single color register. The values supplied have a 16 bit
615 * magnitude which needs to be scaled in this function for the hardware.
616 * Things to take into consideration are how many color registers, if
617 * any, are supported with the current color visual. With truecolor mode
618 * no color palettes are supported. Here a psuedo palette is created
619 * which we store the value in pseudo_palette in struct fb_info. For
620 * pseudocolor mode we have a limited color palette. To deal with this
621 * we can program what color is displayed for a particular pixel value.
622 * DirectColor is similar in that we can program each color field. If
623 * we have a static colormap we don't need to implement this function.
625 * Returns negative errno on error, or zero on success. In an
626 * ideal world, this would have been the case, but as it turns
627 * out, the other drivers return 1 on failure, so that's what
630 static int atmel_lcdfb_setcolreg(unsigned int regno
, unsigned int red
,
631 unsigned int green
, unsigned int blue
,
632 unsigned int transp
, struct fb_info
*info
)
634 struct atmel_lcdfb_info
*sinfo
= info
->par
;
639 if (info
->var
.grayscale
)
640 red
= green
= blue
= (19595 * red
+ 38470 * green
641 + 7471 * blue
) >> 16;
643 switch (info
->fix
.visual
) {
644 case FB_VISUAL_TRUECOLOR
:
646 pal
= info
->pseudo_palette
;
648 val
= chan_to_field(red
, &info
->var
.red
);
649 val
|= chan_to_field(green
, &info
->var
.green
);
650 val
|= chan_to_field(blue
, &info
->var
.blue
);
657 case FB_VISUAL_PSEUDOCOLOR
:
659 val
= ((red
>> 11) & 0x001f);
660 val
|= ((green
>> 6) & 0x03e0);
661 val
|= ((blue
>> 1) & 0x7c00);
664 * TODO: intensity bit. Maybe something like
665 * ~(red[10] ^ green[10] ^ blue[10]) & 1
668 lcdc_writel(sinfo
, ATMEL_LCDC_LUT(regno
), val
);
673 case FB_VISUAL_MONO01
:
675 val
= (regno
== 0) ? 0x00 : 0x1F;
676 lcdc_writel(sinfo
, ATMEL_LCDC_LUT(regno
), val
);
686 static int atmel_lcdfb_pan_display(struct fb_var_screeninfo
*var
,
687 struct fb_info
*info
)
689 dev_dbg(info
->device
, "%s\n", __func__
);
691 atmel_lcdfb_update_dma(info
, var
);
696 static struct fb_ops atmel_lcdfb_ops
= {
697 .owner
= THIS_MODULE
,
698 .fb_check_var
= atmel_lcdfb_check_var
,
699 .fb_set_par
= atmel_lcdfb_set_par
,
700 .fb_setcolreg
= atmel_lcdfb_setcolreg
,
701 .fb_pan_display
= atmel_lcdfb_pan_display
,
702 .fb_fillrect
= cfb_fillrect
,
703 .fb_copyarea
= cfb_copyarea
,
704 .fb_imageblit
= cfb_imageblit
,
707 static irqreturn_t
atmel_lcdfb_interrupt(int irq
, void *dev_id
)
709 struct fb_info
*info
= dev_id
;
710 struct atmel_lcdfb_info
*sinfo
= info
->par
;
713 status
= lcdc_readl(sinfo
, ATMEL_LCDC_ISR
);
714 if (status
& ATMEL_LCDC_UFLWI
) {
715 dev_warn(info
->device
, "FIFO underflow %#x\n", status
);
716 /* reset DMA and FIFO to avoid screen shifting */
717 schedule_work(&sinfo
->task
);
719 lcdc_writel(sinfo
, ATMEL_LCDC_ICR
, status
);
724 * LCD controller task (to reset the LCD)
726 static void atmel_lcdfb_task(struct work_struct
*work
)
728 struct atmel_lcdfb_info
*sinfo
=
729 container_of(work
, struct atmel_lcdfb_info
, task
);
731 atmel_lcdfb_reset(sinfo
);
734 static int __init
atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info
*sinfo
)
736 struct fb_info
*info
= sinfo
->info
;
739 info
->var
.activate
|= FB_ACTIVATE_FORCE
| FB_ACTIVATE_NOW
;
741 dev_info(info
->device
,
742 "%luKiB frame buffer at %08lx (mapped at %p)\n",
743 (unsigned long)info
->fix
.smem_len
/ 1024,
744 (unsigned long)info
->fix
.smem_start
,
747 /* Allocate colormap */
748 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
750 dev_err(info
->device
, "Alloc color map failed\n");
755 static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info
*sinfo
)
758 clk_enable(sinfo
->bus_clk
);
759 clk_enable(sinfo
->lcdc_clk
);
762 static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info
*sinfo
)
765 clk_disable(sinfo
->bus_clk
);
766 clk_disable(sinfo
->lcdc_clk
);
770 static int __init
atmel_lcdfb_probe(struct platform_device
*pdev
)
772 struct device
*dev
= &pdev
->dev
;
773 struct fb_info
*info
;
774 struct atmel_lcdfb_info
*sinfo
;
775 struct atmel_lcdfb_info
*pdata_sinfo
;
776 struct fb_videomode fbmode
;
777 struct resource
*regs
= NULL
;
778 struct resource
*map
= NULL
;
781 dev_dbg(dev
, "%s BEGIN\n", __func__
);
784 info
= framebuffer_alloc(sizeof(struct atmel_lcdfb_info
), dev
);
786 dev_err(dev
, "cannot allocate memory\n");
792 if (dev
->platform_data
) {
793 pdata_sinfo
= (struct atmel_lcdfb_info
*)dev
->platform_data
;
794 sinfo
->default_bpp
= pdata_sinfo
->default_bpp
;
795 sinfo
->default_dmacon
= pdata_sinfo
->default_dmacon
;
796 sinfo
->default_lcdcon2
= pdata_sinfo
->default_lcdcon2
;
797 sinfo
->default_monspecs
= pdata_sinfo
->default_monspecs
;
798 sinfo
->atmel_lcdfb_power_control
= pdata_sinfo
->atmel_lcdfb_power_control
;
799 sinfo
->guard_time
= pdata_sinfo
->guard_time
;
800 sinfo
->smem_len
= pdata_sinfo
->smem_len
;
801 sinfo
->lcdcon_is_backlight
= pdata_sinfo
->lcdcon_is_backlight
;
802 sinfo
->lcd_wiring_mode
= pdata_sinfo
->lcd_wiring_mode
;
804 dev_err(dev
, "cannot get default configuration\n");
810 strcpy(info
->fix
.id
, sinfo
->pdev
->name
);
811 info
->flags
= ATMEL_LCDFB_FBINFO_DEFAULT
;
812 info
->pseudo_palette
= sinfo
->pseudo_palette
;
813 info
->fbops
= &atmel_lcdfb_ops
;
815 memcpy(&info
->monspecs
, sinfo
->default_monspecs
, sizeof(info
->monspecs
));
816 info
->fix
= atmel_lcdfb_fix
;
818 /* Enable LCDC Clocks */
819 if (cpu_is_at91sam9261() || cpu_is_at32ap7000()) {
820 sinfo
->bus_clk
= clk_get(dev
, "hck1");
821 if (IS_ERR(sinfo
->bus_clk
)) {
822 ret
= PTR_ERR(sinfo
->bus_clk
);
826 sinfo
->lcdc_clk
= clk_get(dev
, "lcdc_clk");
827 if (IS_ERR(sinfo
->lcdc_clk
)) {
828 ret
= PTR_ERR(sinfo
->lcdc_clk
);
831 atmel_lcdfb_start_clock(sinfo
);
833 ret
= fb_find_mode(&info
->var
, info
, NULL
, info
->monspecs
.modedb
,
834 info
->monspecs
.modedb_len
, info
->monspecs
.modedb
,
837 dev_err(dev
, "no suitable video mode found\n");
842 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
844 dev_err(dev
, "resources unusable\n");
849 sinfo
->irq_base
= platform_get_irq(pdev
, 0);
850 if (sinfo
->irq_base
< 0) {
851 dev_err(dev
, "unable to get irq\n");
852 ret
= sinfo
->irq_base
;
856 /* Initialize video memory */
857 map
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
859 /* use a pre-allocated memory buffer */
860 info
->fix
.smem_start
= map
->start
;
861 info
->fix
.smem_len
= map
->end
- map
->start
+ 1;
862 if (!request_mem_region(info
->fix
.smem_start
,
863 info
->fix
.smem_len
, pdev
->name
)) {
868 info
->screen_base
= ioremap(info
->fix
.smem_start
, info
->fix
.smem_len
);
869 if (!info
->screen_base
)
873 * Don't clear the framebuffer -- someone may have set
877 /* alocate memory buffer */
878 ret
= atmel_lcdfb_alloc_video_memory(sinfo
);
880 dev_err(dev
, "cannot allocate framebuffer: %d\n", ret
);
886 info
->fix
.mmio_start
= regs
->start
;
887 info
->fix
.mmio_len
= regs
->end
- regs
->start
+ 1;
889 if (!request_mem_region(info
->fix
.mmio_start
,
890 info
->fix
.mmio_len
, pdev
->name
)) {
895 sinfo
->mmio
= ioremap(info
->fix
.mmio_start
, info
->fix
.mmio_len
);
897 dev_err(dev
, "cannot map LCDC registers\n");
901 /* Initialize PWM for contrast or backlight ("off") */
902 init_contrast(sinfo
);
905 ret
= request_irq(sinfo
->irq_base
, atmel_lcdfb_interrupt
, 0, pdev
->name
, info
);
907 dev_err(dev
, "request_irq failed: %d\n", ret
);
911 /* Some operations on the LCDC might sleep and
912 * require a preemptible task context */
913 INIT_WORK(&sinfo
->task
, atmel_lcdfb_task
);
915 ret
= atmel_lcdfb_init_fbinfo(sinfo
);
917 dev_err(dev
, "init fbinfo failed: %d\n", ret
);
918 goto unregister_irqs
;
922 * This makes sure that our colour bitfield
923 * descriptors are correctly initialised.
925 atmel_lcdfb_check_var(&info
->var
, info
);
927 ret
= fb_set_var(info
, &info
->var
);
929 dev_warn(dev
, "unable to set display parameters\n");
933 dev_set_drvdata(dev
, info
);
936 * Tell the world that we're ready to go
938 ret
= register_framebuffer(info
);
940 dev_err(dev
, "failed to register framebuffer device: %d\n", ret
);
944 /* add selected videomode to modelist */
945 fb_var_to_videomode(&fbmode
, &info
->var
);
946 fb_add_videomode(&fbmode
, &info
->modelist
);
948 /* Power up the LCDC screen */
949 if (sinfo
->atmel_lcdfb_power_control
)
950 sinfo
->atmel_lcdfb_power_control(1);
952 dev_info(dev
, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %lu\n",
953 info
->node
, info
->fix
.mmio_start
, sinfo
->mmio
, sinfo
->irq_base
);
958 dev_set_drvdata(dev
, NULL
);
960 fb_dealloc_cmap(&info
->cmap
);
962 cancel_work_sync(&sinfo
->task
);
963 free_irq(sinfo
->irq_base
, info
);
965 exit_backlight(sinfo
);
966 iounmap(sinfo
->mmio
);
968 release_mem_region(info
->fix
.mmio_start
, info
->fix
.mmio_len
);
971 iounmap(info
->screen_base
);
973 atmel_lcdfb_free_video_memory(sinfo
);
977 release_mem_region(info
->fix
.smem_start
, info
->fix
.smem_len
);
979 atmel_lcdfb_stop_clock(sinfo
);
980 clk_put(sinfo
->lcdc_clk
);
983 clk_put(sinfo
->bus_clk
);
985 framebuffer_release(info
);
987 dev_dbg(dev
, "%s FAILED\n", __func__
);
991 static int __exit
atmel_lcdfb_remove(struct platform_device
*pdev
)
993 struct device
*dev
= &pdev
->dev
;
994 struct fb_info
*info
= dev_get_drvdata(dev
);
995 struct atmel_lcdfb_info
*sinfo
;
997 if (!info
|| !info
->par
)
1001 cancel_work_sync(&sinfo
->task
);
1002 exit_backlight(sinfo
);
1003 if (sinfo
->atmel_lcdfb_power_control
)
1004 sinfo
->atmel_lcdfb_power_control(0);
1005 unregister_framebuffer(info
);
1006 atmel_lcdfb_stop_clock(sinfo
);
1007 clk_put(sinfo
->lcdc_clk
);
1009 clk_put(sinfo
->bus_clk
);
1010 fb_dealloc_cmap(&info
->cmap
);
1011 free_irq(sinfo
->irq_base
, info
);
1012 iounmap(sinfo
->mmio
);
1013 release_mem_region(info
->fix
.mmio_start
, info
->fix
.mmio_len
);
1014 if (platform_get_resource(pdev
, IORESOURCE_MEM
, 1)) {
1015 iounmap(info
->screen_base
);
1016 release_mem_region(info
->fix
.smem_start
, info
->fix
.smem_len
);
1018 atmel_lcdfb_free_video_memory(sinfo
);
1021 dev_set_drvdata(dev
, NULL
);
1022 framebuffer_release(info
);
1029 static int atmel_lcdfb_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1031 struct fb_info
*info
= platform_get_drvdata(pdev
);
1032 struct atmel_lcdfb_info
*sinfo
= info
->par
;
1035 * We don't want to handle interrupts while the clock is
1036 * stopped. It may take forever.
1038 lcdc_writel(sinfo
, ATMEL_LCDC_IDR
, ~0UL);
1040 sinfo
->saved_lcdcon
= lcdc_readl(sinfo
, ATMEL_LCDC_CONTRAST_VAL
);
1041 lcdc_writel(sinfo
, ATMEL_LCDC_CONTRAST_CTR
, 0);
1042 if (sinfo
->atmel_lcdfb_power_control
)
1043 sinfo
->atmel_lcdfb_power_control(0);
1045 atmel_lcdfb_stop(sinfo
);
1046 atmel_lcdfb_stop_clock(sinfo
);
1051 static int atmel_lcdfb_resume(struct platform_device
*pdev
)
1053 struct fb_info
*info
= platform_get_drvdata(pdev
);
1054 struct atmel_lcdfb_info
*sinfo
= info
->par
;
1056 atmel_lcdfb_start_clock(sinfo
);
1057 atmel_lcdfb_start(sinfo
);
1058 if (sinfo
->atmel_lcdfb_power_control
)
1059 sinfo
->atmel_lcdfb_power_control(1);
1060 lcdc_writel(sinfo
, ATMEL_LCDC_CONTRAST_CTR
, sinfo
->saved_lcdcon
);
1062 /* Enable FIFO & DMA errors */
1063 lcdc_writel(sinfo
, ATMEL_LCDC_IER
, ATMEL_LCDC_UFLWI
1064 | ATMEL_LCDC_OWRI
| ATMEL_LCDC_MERI
);
1070 #define atmel_lcdfb_suspend NULL
1071 #define atmel_lcdfb_resume NULL
1074 static struct platform_driver atmel_lcdfb_driver
= {
1075 .remove
= __exit_p(atmel_lcdfb_remove
),
1076 .suspend
= atmel_lcdfb_suspend
,
1077 .resume
= atmel_lcdfb_resume
,
1080 .name
= "atmel_lcdfb",
1081 .owner
= THIS_MODULE
,
1085 static int __init
atmel_lcdfb_init(void)
1087 return platform_driver_probe(&atmel_lcdfb_driver
, atmel_lcdfb_probe
);
1090 static void __exit
atmel_lcdfb_exit(void)
1092 platform_driver_unregister(&atmel_lcdfb_driver
);
1095 module_init(atmel_lcdfb_init
);
1096 module_exit(atmel_lcdfb_exit
);
1098 MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
1099 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1100 MODULE_LICENSE("GPL");