blob: 4ac48d9ee66584a797393fd5e836ee82a5024957 [file] [log] [blame]
Nicolas Ferre14340582007-05-10 22:23:26 -07001/*
2 * Driver for AT91/AT32 LCD Controller
3 *
4 * Copyright (C) 2007 Atmel Corporation
5 *
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
8 * more details.
9 */
10
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>
16#include <linux/fb.h>
17#include <linux/init.h>
18#include <linux/delay.h>
David Brownella9a84c32008-02-06 01:39:26 -080019#include <linux/backlight.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Nicolas Ferre14340582007-05-10 22:23:26 -070021
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/board.h>
23#include <mach/cpu.h>
Russell King60e89722011-07-26 10:56:19 +010024#include <asm/gpio.h>
Nicolas Ferre14340582007-05-10 22:23:26 -070025
26#include <video/atmel_lcdc.h>
27
28#define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
29#define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
30
31/* configurable parameters */
32#define ATMEL_LCDC_CVAL_DEFAULT 0xc8
Nicolas Ferre53b74792009-05-28 14:34:36 -070033#define ATMEL_LCDC_DMA_BURST_LEN 8 /* words */
34#define ATMEL_LCDC_FIFO_SIZE 512 /* words */
Nicolas Ferre14340582007-05-10 22:23:26 -070035
36#if defined(CONFIG_ARCH_AT91)
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -070037#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
38 | FBINFO_PARTIAL_PAN_OK \
39 | FBINFO_HWACCEL_YPAN)
Nicolas Ferre14340582007-05-10 22:23:26 -070040
41static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
Laurent Pinchartb3e9c122011-05-25 11:34:52 +020042 struct fb_var_screeninfo *var,
43 struct fb_info *info)
Nicolas Ferre14340582007-05-10 22:23:26 -070044{
45
46}
47#elif defined(CONFIG_AVR32)
48#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
49 | FBINFO_PARTIAL_PAN_OK \
50 | FBINFO_HWACCEL_XPAN \
51 | FBINFO_HWACCEL_YPAN)
52
53static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
Laurent Pinchartb3e9c122011-05-25 11:34:52 +020054 struct fb_var_screeninfo *var,
55 struct fb_info *info)
Nicolas Ferre14340582007-05-10 22:23:26 -070056{
57 u32 dma2dcfg;
58 u32 pixeloff;
59
Laurent Pinchartb3e9c122011-05-25 11:34:52 +020060 pixeloff = (var->xoffset * info->var.bits_per_pixel) & 0x1f;
Nicolas Ferre14340582007-05-10 22:23:26 -070061
Laurent Pinchartb3e9c122011-05-25 11:34:52 +020062 dma2dcfg = (info->var.xres_virtual - info->var.xres)
63 * info->var.bits_per_pixel / 8;
Nicolas Ferre14340582007-05-10 22:23:26 -070064 dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
65 lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
66
67 /* Update configuration */
68 lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
69 lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
70 | ATMEL_LCDC_DMAUPDT);
71}
72#endif
73
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +000074static u32 contrast_ctr = ATMEL_LCDC_PS_DIV8
David Brownella9a84c32008-02-06 01:39:26 -080075 | ATMEL_LCDC_POL_POSITIVE
76 | ATMEL_LCDC_ENA_PWMENABLE;
77
78#ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
79
80/* some bl->props field just changed */
81static int atmel_bl_update_status(struct backlight_device *bl)
82{
83 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
84 int power = sinfo->bl_power;
85 int brightness = bl->props.brightness;
86
87 /* REVISIT there may be a meaningful difference between
88 * fb_blank and power ... there seem to be some cases
89 * this doesn't handle correctly.
90 */
91 if (bl->props.fb_blank != sinfo->bl_power)
92 power = bl->props.fb_blank;
93 else if (bl->props.power != sinfo->bl_power)
94 power = bl->props.power;
95
96 if (brightness < 0 && power == FB_BLANK_UNBLANK)
97 brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
98 else if (power != FB_BLANK_UNBLANK)
99 brightness = 0;
100
101 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness);
102 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
103 brightness ? contrast_ctr : 0);
104
105 bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
106
107 return 0;
108}
109
110static int atmel_bl_get_brightness(struct backlight_device *bl)
111{
112 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
113
114 return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
115}
116
Lionel Debrouxacc24722010-11-16 14:14:02 +0100117static const struct backlight_ops atmel_lcdc_bl_ops = {
David Brownella9a84c32008-02-06 01:39:26 -0800118 .update_status = atmel_bl_update_status,
119 .get_brightness = atmel_bl_get_brightness,
120};
121
122static void init_backlight(struct atmel_lcdfb_info *sinfo)
123{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500124 struct backlight_properties props;
David Brownella9a84c32008-02-06 01:39:26 -0800125 struct backlight_device *bl;
126
127 sinfo->bl_power = FB_BLANK_UNBLANK;
128
129 if (sinfo->backlight)
130 return;
131
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500132 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700133 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500134 props.max_brightness = 0xff;
135 bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
136 &atmel_lcdc_bl_ops, &props);
Julien Brunelcf7b9a12008-11-19 15:36:07 -0800137 if (IS_ERR(bl)) {
David Brownella9a84c32008-02-06 01:39:26 -0800138 dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
139 PTR_ERR(bl));
140 return;
141 }
142 sinfo->backlight = bl;
143
144 bl->props.power = FB_BLANK_UNBLANK;
145 bl->props.fb_blank = FB_BLANK_UNBLANK;
David Brownella9a84c32008-02-06 01:39:26 -0800146 bl->props.brightness = atmel_bl_get_brightness(bl);
147}
148
149static void exit_backlight(struct atmel_lcdfb_info *sinfo)
150{
151 if (sinfo->backlight)
152 backlight_device_unregister(sinfo->backlight);
153}
154
155#else
156
157static void init_backlight(struct atmel_lcdfb_info *sinfo)
158{
159 dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
160}
161
162static void exit_backlight(struct atmel_lcdfb_info *sinfo)
163{
164}
165
166#endif
167
168static void init_contrast(struct atmel_lcdfb_info *sinfo)
169{
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000170 /* contrast pwm can be 'inverted' */
171 if (sinfo->lcdcon_pol_negative)
172 contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE);
173
David Brownella9a84c32008-02-06 01:39:26 -0800174 /* have some default contrast/backlight settings */
175 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
176 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
177
178 if (sinfo->lcdcon_is_backlight)
179 init_backlight(sinfo);
180}
181
Nicolas Ferre14340582007-05-10 22:23:26 -0700182
183static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
184 .type = FB_TYPE_PACKED_PIXELS,
185 .visual = FB_VISUAL_TRUECOLOR,
186 .xpanstep = 0,
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -0700187 .ypanstep = 1,
Nicolas Ferre14340582007-05-10 22:23:26 -0700188 .ywrapstep = 0,
189 .accel = FB_ACCEL_NONE,
190};
191
Nicolas Ferre250a2692007-07-21 04:37:59 -0700192static unsigned long compute_hozval(unsigned long xres, unsigned long lcdcon2)
193{
194 unsigned long value;
195
Nicolas Ferre915190f2009-07-21 11:31:29 +0100196 if (!(cpu_is_at91sam9261() || cpu_is_at91sam9g10()
197 || cpu_is_at32ap7000()))
Nicolas Ferre250a2692007-07-21 04:37:59 -0700198 return xres;
199
200 value = xres;
201 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
202 /* STN display */
203 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
204 value *= 3;
205 }
206 if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
207 || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
208 && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
209 value = DIV_ROUND_UP(value, 4);
210 else
211 value = DIV_ROUND_UP(value, 8);
212 }
213
214 return value;
215}
Nicolas Ferre14340582007-05-10 22:23:26 -0700216
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700217static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
218{
219 /* Turn off the LCD controller and the DMA controller */
220 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
221 sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
222
223 /* Wait for the LCDC core to become idle */
224 while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
225 msleep(10);
226
227 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
228}
229
230static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
231{
232 atmel_lcdfb_stop_nowait(sinfo);
233
234 /* Wait for DMA engine to become idle... */
235 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
236 msleep(10);
237}
238
239static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
240{
241 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
242 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
243 (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
244 | ATMEL_LCDC_PWR);
245}
246
Nicolas Ferre14340582007-05-10 22:23:26 -0700247static void atmel_lcdfb_update_dma(struct fb_info *info,
248 struct fb_var_screeninfo *var)
249{
250 struct atmel_lcdfb_info *sinfo = info->par;
251 struct fb_fix_screeninfo *fix = &info->fix;
252 unsigned long dma_addr;
253
254 dma_addr = (fix->smem_start + var->yoffset * fix->line_length
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200255 + var->xoffset * info->var.bits_per_pixel / 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700256
257 dma_addr &= ~3UL;
258
259 /* Set framebuffer DMA base address and pixel offset */
260 lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
261
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200262 atmel_lcdfb_update_dma2d(sinfo, var, info);
Nicolas Ferre14340582007-05-10 22:23:26 -0700263}
264
265static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
266{
267 struct fb_info *info = sinfo->info;
268
269 dma_free_writecombine(info->device, info->fix.smem_len,
270 info->screen_base, info->fix.smem_start);
271}
272
273/**
274 * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
275 * @sinfo: the frame buffer to allocate memory for
Krzysztof Helt1d01e832009-07-08 22:26:16 +0200276 *
277 * This function is called only from the atmel_lcdfb_probe()
278 * so no locking by fb_info->mm_lock around smem_len setting is needed.
Nicolas Ferre14340582007-05-10 22:23:26 -0700279 */
280static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
281{
282 struct fb_info *info = sinfo->info;
283 struct fb_var_screeninfo *var = &info->var;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700284 unsigned int smem_len;
Nicolas Ferre14340582007-05-10 22:23:26 -0700285
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700286 smem_len = (var->xres_virtual * var->yres_virtual
287 * ((var->bits_per_pixel + 7) / 8));
288 info->fix.smem_len = max(smem_len, sinfo->smem_len);
Nicolas Ferre14340582007-05-10 22:23:26 -0700289
290 info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
291 (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
292
293 if (!info->screen_base) {
294 return -ENOMEM;
295 }
296
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -0700297 memset(info->screen_base, 0, info->fix.smem_len);
298
Nicolas Ferre14340582007-05-10 22:23:26 -0700299 return 0;
300}
301
Nicolas Ferre968910b2008-07-23 21:31:34 -0700302static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
303 struct fb_info *info)
304{
305 struct fb_videomode varfbmode;
306 const struct fb_videomode *fbmode = NULL;
307
308 fb_var_to_videomode(&varfbmode, var);
309 fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
310 if (fbmode)
311 fb_videomode_to_var(var, fbmode);
312 return fbmode;
313}
314
315
Nicolas Ferre14340582007-05-10 22:23:26 -0700316/**
317 * atmel_lcdfb_check_var - Validates a var passed in.
318 * @var: frame buffer variable screen structure
319 * @info: frame buffer structure that represents a single frame buffer
320 *
321 * Checks to see if the hardware supports the state requested by
322 * var passed in. This function does not alter the hardware
323 * state!!! This means the data stored in struct fb_info and
324 * struct atmel_lcdfb_info do not change. This includes the var
325 * inside of struct fb_info. Do NOT change these. This function
326 * can be called on its own if we intent to only test a mode and
327 * not actually set it. The stuff in modedb.c is a example of
328 * this. If the var passed in is slightly off by what the
329 * hardware can support then we alter the var PASSED in to what
330 * we can do. If the hardware doesn't support mode change a
331 * -EINVAL will be returned by the upper layers. You don't need
332 * to implement this function then. If you hardware doesn't
333 * support changing the resolution then this function is not
334 * needed. In this case the driver would just provide a var that
335 * represents the static state the screen is in.
336 *
337 * Returns negative errno on error, or zero on success.
338 */
339static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
340 struct fb_info *info)
341{
342 struct device *dev = info->device;
343 struct atmel_lcdfb_info *sinfo = info->par;
344 unsigned long clk_value_khz;
345
346 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
347
348 dev_dbg(dev, "%s:\n", __func__);
Nicolas Ferre968910b2008-07-23 21:31:34 -0700349
350 if (!(var->pixclock && var->bits_per_pixel)) {
351 /* choose a suitable mode if possible */
352 if (!atmel_lcdfb_choose_mode(var, info)) {
353 dev_err(dev, "needed value not specified\n");
354 return -EINVAL;
355 }
356 }
357
Nicolas Ferre14340582007-05-10 22:23:26 -0700358 dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
359 dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
360 dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
361 dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
362
Ben Nizette97b9a5a2009-06-16 15:34:24 -0700363 if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700364 dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
365 return -EINVAL;
366 }
367
Nicolas Ferre968910b2008-07-23 21:31:34 -0700368 /* Do not allow to have real resoulution larger than virtual */
369 if (var->xres > var->xres_virtual)
370 var->xres_virtual = var->xres;
371
372 if (var->yres > var->yres_virtual)
373 var->yres_virtual = var->yres;
374
Nicolas Ferre14340582007-05-10 22:23:26 -0700375 /* Force same alignment for each line */
376 var->xres = (var->xres + 3) & ~3UL;
377 var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
378
379 var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
380 var->transp.msb_right = 0;
381 var->transp.offset = var->transp.length = 0;
382 var->xoffset = var->yoffset = 0;
383
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700384 if (info->fix.smem_len) {
385 unsigned int smem_len = (var->xres_virtual * var->yres_virtual
386 * ((var->bits_per_pixel + 7) / 8));
387 if (smem_len > info->fix.smem_len)
388 return -EINVAL;
389 }
390
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800391 /* Saturate vertical and horizontal timings at maximum values */
392 var->vsync_len = min_t(u32, var->vsync_len,
393 (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
394 var->upper_margin = min_t(u32, var->upper_margin,
395 ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
396 var->lower_margin = min_t(u32, var->lower_margin,
397 ATMEL_LCDC_VFP);
398 var->right_margin = min_t(u32, var->right_margin,
399 (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
400 var->hsync_len = min_t(u32, var->hsync_len,
401 (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
402 var->left_margin = min_t(u32, var->left_margin,
403 ATMEL_LCDC_HBP + 1);
404
405 /* Some parameters can't be zero */
406 var->vsync_len = max_t(u32, var->vsync_len, 1);
407 var->right_margin = max_t(u32, var->right_margin, 1);
408 var->hsync_len = max_t(u32, var->hsync_len, 1);
409 var->left_margin = max_t(u32, var->left_margin, 1);
410
Nicolas Ferre14340582007-05-10 22:23:26 -0700411 switch (var->bits_per_pixel) {
Nicolas Ferre250a2692007-07-21 04:37:59 -0700412 case 1:
Nicolas Ferre14340582007-05-10 22:23:26 -0700413 case 2:
414 case 4:
415 case 8:
416 var->red.offset = var->green.offset = var->blue.offset = 0;
417 var->red.length = var->green.length = var->blue.length
418 = var->bits_per_pixel;
419 break;
420 case 15:
421 case 16:
Nicolas Ferrefd085802008-04-28 02:15:21 -0700422 if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
423 /* RGB:565 mode */
424 var->red.offset = 11;
425 var->blue.offset = 0;
426 var->green.length = 6;
Guillaume GARDETfbd03a12008-08-29 10:11:24 +0100427 } else if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB555) {
428 var->red.offset = 10;
429 var->blue.offset = 0;
430 var->green.length = 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700431 } else {
432 /* BGR:555 mode */
433 var->red.offset = 0;
434 var->blue.offset = 10;
435 var->green.length = 5;
436 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700437 var->green.offset = 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700438 var->red.length = var->blue.length = 5;
Nicolas Ferre14340582007-05-10 22:23:26 -0700439 break;
Nicolas Ferre14340582007-05-10 22:23:26 -0700440 case 32:
Haavard Skinnemoen4440e0e2007-07-21 04:38:02 -0700441 var->transp.offset = 24;
442 var->transp.length = 8;
443 /* fall through */
444 case 24:
Nicolas Ferrefd085802008-04-28 02:15:21 -0700445 if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
446 /* RGB:888 mode */
447 var->red.offset = 16;
448 var->blue.offset = 0;
449 } else {
450 /* BGR:888 mode */
451 var->red.offset = 0;
452 var->blue.offset = 16;
453 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700454 var->green.offset = 8;
Nicolas Ferre14340582007-05-10 22:23:26 -0700455 var->red.length = var->green.length = var->blue.length = 8;
456 break;
457 default:
458 dev_err(dev, "color depth %d not supported\n",
459 var->bits_per_pixel);
460 return -EINVAL;
461 }
462
463 return 0;
464}
465
Nicolas Ferred22579b2008-07-23 21:31:20 -0700466/*
467 * LCD reset sequence
468 */
469static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
470{
471 might_sleep();
472
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700473 atmel_lcdfb_stop(sinfo);
474 atmel_lcdfb_start(sinfo);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700475}
476
Nicolas Ferre14340582007-05-10 22:23:26 -0700477/**
478 * atmel_lcdfb_set_par - Alters the hardware state.
479 * @info: frame buffer structure that represents a single frame buffer
480 *
481 * Using the fb_var_screeninfo in fb_info we set the resolution
482 * of the this particular framebuffer. This function alters the
483 * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
484 * not alter var in fb_info since we are using that data. This
485 * means we depend on the data in var inside fb_info to be
486 * supported by the hardware. atmel_lcdfb_check_var is always called
487 * before atmel_lcdfb_set_par to ensure this. Again if you can't
488 * change the resolution you don't need this function.
489 *
490 */
491static int atmel_lcdfb_set_par(struct fb_info *info)
492{
493 struct atmel_lcdfb_info *sinfo = info->par;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700494 unsigned long hozval_linesz;
Nicolas Ferre14340582007-05-10 22:23:26 -0700495 unsigned long value;
496 unsigned long clk_value_khz;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700497 unsigned long bits_per_line;
Nicolas Ferre431861c2009-11-11 14:26:35 -0800498 unsigned long pix_factor = 2;
Nicolas Ferre14340582007-05-10 22:23:26 -0700499
Nicolas Ferred22579b2008-07-23 21:31:20 -0700500 might_sleep();
501
Nicolas Ferre14340582007-05-10 22:23:26 -0700502 dev_dbg(info->device, "%s:\n", __func__);
503 dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
504 info->var.xres, info->var.yres,
505 info->var.xres_virtual, info->var.yres_virtual);
506
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700507 atmel_lcdfb_stop_nowait(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700508
Nicolas Ferre250a2692007-07-21 04:37:59 -0700509 if (info->var.bits_per_pixel == 1)
510 info->fix.visual = FB_VISUAL_MONO01;
511 else if (info->var.bits_per_pixel <= 8)
Nicolas Ferre14340582007-05-10 22:23:26 -0700512 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
513 else
514 info->fix.visual = FB_VISUAL_TRUECOLOR;
515
Nicolas Ferre250a2692007-07-21 04:37:59 -0700516 bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
517 info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700518
519 /* Re-initialize the DMA engine... */
520 dev_dbg(info->device, " * update DMA engine\n");
521 atmel_lcdfb_update_dma(info, &info->var);
522
523 /* ...set frame size and burst length = 8 words (?) */
524 value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
525 value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
526 lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
527
528 /* Now, the LCDC core... */
529
530 /* Set pixel clock */
Nicolas Ferre431861c2009-11-11 14:26:35 -0800531 if (cpu_is_at91sam9g45() && !cpu_is_at91sam9g45es())
532 pix_factor = 1;
533
Nicolas Ferre14340582007-05-10 22:23:26 -0700534 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
535
Nicolas Ferre250a2692007-07-21 04:37:59 -0700536 value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
Nicolas Ferre14340582007-05-10 22:23:26 -0700537
Nicolas Ferre431861c2009-11-11 14:26:35 -0800538 if (value < pix_factor) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700539 dev_notice(info->device, "Bypassing pixel clock divider\n");
540 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700541 } else {
Nicolas Ferre431861c2009-11-11 14:26:35 -0800542 value = (value / pix_factor) - 1;
Nicolas Ferrebaf63322008-05-12 14:02:25 -0700543 dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n",
544 value);
545 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
546 value << ATMEL_LCDC_CLKVAL_OFFSET);
Nicolas Ferre431861c2009-11-11 14:26:35 -0800547 info->var.pixclock =
548 KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
Nicolas Ferre250a2692007-07-21 04:37:59 -0700549 dev_dbg(info->device, " updated pixclk: %lu KHz\n",
550 PICOS2KHZ(info->var.pixclock));
551 }
552
Nicolas Ferre14340582007-05-10 22:23:26 -0700553
554 /* Initialize control register 2 */
555 value = sinfo->default_lcdcon2;
556
557 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
558 value |= ATMEL_LCDC_INVLINE_INVERTED;
559 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
560 value |= ATMEL_LCDC_INVFRAME_INVERTED;
561
562 switch (info->var.bits_per_pixel) {
563 case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
564 case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
565 case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
566 case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
567 case 15: /* fall through */
568 case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
569 case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
570 case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
571 default: BUG(); break;
572 }
573 dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
574 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
575
576 /* Vertical timing */
577 value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
578 value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
579 value |= info->var.lower_margin;
580 dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
581 lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
582
583 /* Horizontal timing */
584 value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
585 value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
586 value |= (info->var.left_margin - 1);
587 dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
588 lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
589
Nicolas Ferre250a2692007-07-21 04:37:59 -0700590 /* Horizontal value (aka line size) */
591 hozval_linesz = compute_hozval(info->var.xres,
592 lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2));
593
Nicolas Ferre14340582007-05-10 22:23:26 -0700594 /* Display size */
Nicolas Ferre250a2692007-07-21 04:37:59 -0700595 value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700596 value |= info->var.yres - 1;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700597 dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value);
Nicolas Ferre14340582007-05-10 22:23:26 -0700598 lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
599
600 /* FIFO Threshold: Use formula from data sheet */
601 value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
602 lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
603
604 /* Toggle LCD_MODE every frame */
605 lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
606
607 /* Disable all interrupts */
608 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700609 /* Enable FIFO & DMA errors */
610 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
Nicolas Ferre14340582007-05-10 22:23:26 -0700611
Nicolas Ferre14340582007-05-10 22:23:26 -0700612 /* ...wait for DMA engine to become idle... */
613 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
614 msleep(10);
615
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700616 atmel_lcdfb_start(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700617
618 dev_dbg(info->device, " * DONE\n");
619
620 return 0;
621}
622
623static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
624{
625 chan &= 0xffff;
626 chan >>= 16 - bf->length;
627 return chan << bf->offset;
628}
629
630/**
631 * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
632 * @regno: Which register in the CLUT we are programming
633 * @red: The red value which can be up to 16 bits wide
634 * @green: The green value which can be up to 16 bits wide
635 * @blue: The blue value which can be up to 16 bits wide.
636 * @transp: If supported the alpha value which can be up to 16 bits wide.
637 * @info: frame buffer info structure
638 *
639 * Set a single color register. The values supplied have a 16 bit
640 * magnitude which needs to be scaled in this function for the hardware.
641 * Things to take into consideration are how many color registers, if
642 * any, are supported with the current color visual. With truecolor mode
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300643 * no color palettes are supported. Here a pseudo palette is created
Nicolas Ferre14340582007-05-10 22:23:26 -0700644 * which we store the value in pseudo_palette in struct fb_info. For
645 * pseudocolor mode we have a limited color palette. To deal with this
646 * we can program what color is displayed for a particular pixel value.
647 * DirectColor is similar in that we can program each color field. If
648 * we have a static colormap we don't need to implement this function.
649 *
650 * Returns negative errno on error, or zero on success. In an
651 * ideal world, this would have been the case, but as it turns
652 * out, the other drivers return 1 on failure, so that's what
653 * we're going to do.
654 */
655static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
656 unsigned int green, unsigned int blue,
657 unsigned int transp, struct fb_info *info)
658{
659 struct atmel_lcdfb_info *sinfo = info->par;
660 unsigned int val;
661 u32 *pal;
662 int ret = 1;
663
664 if (info->var.grayscale)
665 red = green = blue = (19595 * red + 38470 * green
666 + 7471 * blue) >> 16;
667
668 switch (info->fix.visual) {
669 case FB_VISUAL_TRUECOLOR:
670 if (regno < 16) {
671 pal = info->pseudo_palette;
672
673 val = chan_to_field(red, &info->var.red);
674 val |= chan_to_field(green, &info->var.green);
675 val |= chan_to_field(blue, &info->var.blue);
676
677 pal[regno] = val;
678 ret = 0;
679 }
680 break;
681
682 case FB_VISUAL_PSEUDOCOLOR:
683 if (regno < 256) {
684 val = ((red >> 11) & 0x001f);
685 val |= ((green >> 6) & 0x03e0);
686 val |= ((blue >> 1) & 0x7c00);
687
688 /*
689 * TODO: intensity bit. Maybe something like
690 * ~(red[10] ^ green[10] ^ blue[10]) & 1
691 */
692
693 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
694 ret = 0;
695 }
696 break;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700697
698 case FB_VISUAL_MONO01:
699 if (regno < 2) {
700 val = (regno == 0) ? 0x00 : 0x1F;
701 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
702 ret = 0;
703 }
704 break;
705
Nicolas Ferre14340582007-05-10 22:23:26 -0700706 }
707
708 return ret;
709}
710
711static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
712 struct fb_info *info)
713{
714 dev_dbg(info->device, "%s\n", __func__);
715
716 atmel_lcdfb_update_dma(info, var);
717
718 return 0;
719}
720
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000721static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info)
722{
723 struct atmel_lcdfb_info *sinfo = info->par;
724
725 switch (blank_mode) {
726 case FB_BLANK_UNBLANK:
727 case FB_BLANK_NORMAL:
728 atmel_lcdfb_start(sinfo);
729 break;
730 case FB_BLANK_VSYNC_SUSPEND:
731 case FB_BLANK_HSYNC_SUSPEND:
732 break;
733 case FB_BLANK_POWERDOWN:
734 atmel_lcdfb_stop(sinfo);
735 break;
736 default:
737 return -EINVAL;
738 }
739
740 /* let fbcon do a soft blank for us */
741 return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
742}
743
Nicolas Ferre14340582007-05-10 22:23:26 -0700744static struct fb_ops atmel_lcdfb_ops = {
745 .owner = THIS_MODULE,
746 .fb_check_var = atmel_lcdfb_check_var,
747 .fb_set_par = atmel_lcdfb_set_par,
748 .fb_setcolreg = atmel_lcdfb_setcolreg,
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000749 .fb_blank = atmel_lcdfb_blank,
Nicolas Ferre14340582007-05-10 22:23:26 -0700750 .fb_pan_display = atmel_lcdfb_pan_display,
751 .fb_fillrect = cfb_fillrect,
752 .fb_copyarea = cfb_copyarea,
753 .fb_imageblit = cfb_imageblit,
754};
755
756static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
757{
758 struct fb_info *info = dev_id;
759 struct atmel_lcdfb_info *sinfo = info->par;
760 u32 status;
761
762 status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700763 if (status & ATMEL_LCDC_UFLWI) {
764 dev_warn(info->device, "FIFO underflow %#x\n", status);
765 /* reset DMA and FIFO to avoid screen shifting */
766 schedule_work(&sinfo->task);
767 }
768 lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
Nicolas Ferre14340582007-05-10 22:23:26 -0700769 return IRQ_HANDLED;
770}
771
Nicolas Ferred22579b2008-07-23 21:31:20 -0700772/*
773 * LCD controller task (to reset the LCD)
774 */
775static void atmel_lcdfb_task(struct work_struct *work)
776{
777 struct atmel_lcdfb_info *sinfo =
778 container_of(work, struct atmel_lcdfb_info, task);
779
780 atmel_lcdfb_reset(sinfo);
781}
782
Nicolas Ferre14340582007-05-10 22:23:26 -0700783static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
784{
785 struct fb_info *info = sinfo->info;
786 int ret = 0;
787
Nicolas Ferre14340582007-05-10 22:23:26 -0700788 info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
789
790 dev_info(info->device,
791 "%luKiB frame buffer at %08lx (mapped at %p)\n",
792 (unsigned long)info->fix.smem_len / 1024,
793 (unsigned long)info->fix.smem_start,
794 info->screen_base);
795
796 /* Allocate colormap */
797 ret = fb_alloc_cmap(&info->cmap, 256, 0);
798 if (ret < 0)
799 dev_err(info->device, "Alloc color map failed\n");
800
801 return ret;
802}
803
804static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
805{
806 if (sinfo->bus_clk)
807 clk_enable(sinfo->bus_clk);
808 clk_enable(sinfo->lcdc_clk);
809}
810
811static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
812{
813 if (sinfo->bus_clk)
814 clk_disable(sinfo->bus_clk);
815 clk_disable(sinfo->lcdc_clk);
816}
817
818
819static int __init atmel_lcdfb_probe(struct platform_device *pdev)
820{
821 struct device *dev = &pdev->dev;
822 struct fb_info *info;
823 struct atmel_lcdfb_info *sinfo;
824 struct atmel_lcdfb_info *pdata_sinfo;
Nicolas Ferre968910b2008-07-23 21:31:34 -0700825 struct fb_videomode fbmode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700826 struct resource *regs = NULL;
827 struct resource *map = NULL;
828 int ret;
829
830 dev_dbg(dev, "%s BEGIN\n", __func__);
831
832 ret = -ENOMEM;
833 info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
834 if (!info) {
835 dev_err(dev, "cannot allocate memory\n");
836 goto out;
837 }
838
839 sinfo = info->par;
840
841 if (dev->platform_data) {
842 pdata_sinfo = (struct atmel_lcdfb_info *)dev->platform_data;
843 sinfo->default_bpp = pdata_sinfo->default_bpp;
844 sinfo->default_dmacon = pdata_sinfo->default_dmacon;
845 sinfo->default_lcdcon2 = pdata_sinfo->default_lcdcon2;
846 sinfo->default_monspecs = pdata_sinfo->default_monspecs;
847 sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
848 sinfo->guard_time = pdata_sinfo->guard_time;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700849 sinfo->smem_len = pdata_sinfo->smem_len;
David Brownella9a84c32008-02-06 01:39:26 -0800850 sinfo->lcdcon_is_backlight = pdata_sinfo->lcdcon_is_backlight;
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000851 sinfo->lcdcon_pol_negative = pdata_sinfo->lcdcon_pol_negative;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700852 sinfo->lcd_wiring_mode = pdata_sinfo->lcd_wiring_mode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700853 } else {
854 dev_err(dev, "cannot get default configuration\n");
855 goto free_info;
856 }
857 sinfo->info = info;
858 sinfo->pdev = pdev;
859
860 strcpy(info->fix.id, sinfo->pdev->name);
861 info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
862 info->pseudo_palette = sinfo->pseudo_palette;
863 info->fbops = &atmel_lcdfb_ops;
864
865 memcpy(&info->monspecs, sinfo->default_monspecs, sizeof(info->monspecs));
866 info->fix = atmel_lcdfb_fix;
867
868 /* Enable LCDC Clocks */
Nicolas Ferre915190f2009-07-21 11:31:29 +0100869 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()
870 || cpu_is_at32ap7000()) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700871 sinfo->bus_clk = clk_get(dev, "hck1");
872 if (IS_ERR(sinfo->bus_clk)) {
873 ret = PTR_ERR(sinfo->bus_clk);
874 goto free_info;
875 }
876 }
877 sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
878 if (IS_ERR(sinfo->lcdc_clk)) {
879 ret = PTR_ERR(sinfo->lcdc_clk);
880 goto put_bus_clk;
881 }
882 atmel_lcdfb_start_clock(sinfo);
883
884 ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
885 info->monspecs.modedb_len, info->monspecs.modedb,
886 sinfo->default_bpp);
887 if (!ret) {
888 dev_err(dev, "no suitable video mode found\n");
889 goto stop_clk;
890 }
891
892
893 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
894 if (!regs) {
895 dev_err(dev, "resources unusable\n");
896 ret = -ENXIO;
897 goto stop_clk;
898 }
899
900 sinfo->irq_base = platform_get_irq(pdev, 0);
901 if (sinfo->irq_base < 0) {
902 dev_err(dev, "unable to get irq\n");
903 ret = sinfo->irq_base;
904 goto stop_clk;
905 }
906
907 /* Initialize video memory */
908 map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
909 if (map) {
910 /* use a pre-allocated memory buffer */
911 info->fix.smem_start = map->start;
Joe Perches28f65c112011-06-09 09:13:32 -0700912 info->fix.smem_len = resource_size(map);
Nicolas Ferre14340582007-05-10 22:23:26 -0700913 if (!request_mem_region(info->fix.smem_start,
914 info->fix.smem_len, pdev->name)) {
915 ret = -EBUSY;
916 goto stop_clk;
917 }
918
919 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
920 if (!info->screen_base)
921 goto release_intmem;
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -0700922
923 /*
924 * Don't clear the framebuffer -- someone may have set
925 * up a splash image.
926 */
Nicolas Ferre14340582007-05-10 22:23:26 -0700927 } else {
928 /* alocate memory buffer */
929 ret = atmel_lcdfb_alloc_video_memory(sinfo);
930 if (ret < 0) {
931 dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
932 goto stop_clk;
933 }
934 }
935
936 /* LCDC registers */
937 info->fix.mmio_start = regs->start;
Joe Perches28f65c112011-06-09 09:13:32 -0700938 info->fix.mmio_len = resource_size(regs);
Nicolas Ferre14340582007-05-10 22:23:26 -0700939
940 if (!request_mem_region(info->fix.mmio_start,
941 info->fix.mmio_len, pdev->name)) {
942 ret = -EBUSY;
943 goto free_fb;
944 }
945
946 sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
947 if (!sinfo->mmio) {
948 dev_err(dev, "cannot map LCDC registers\n");
949 goto release_mem;
950 }
951
David Brownella9a84c32008-02-06 01:39:26 -0800952 /* Initialize PWM for contrast or backlight ("off") */
953 init_contrast(sinfo);
954
Nicolas Ferre14340582007-05-10 22:23:26 -0700955 /* interrupt */
956 ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
957 if (ret) {
958 dev_err(dev, "request_irq failed: %d\n", ret);
959 goto unmap_mmio;
960 }
961
Nicolas Ferred22579b2008-07-23 21:31:20 -0700962 /* Some operations on the LCDC might sleep and
963 * require a preemptible task context */
964 INIT_WORK(&sinfo->task, atmel_lcdfb_task);
965
Nicolas Ferre14340582007-05-10 22:23:26 -0700966 ret = atmel_lcdfb_init_fbinfo(sinfo);
967 if (ret < 0) {
968 dev_err(dev, "init fbinfo failed: %d\n", ret);
969 goto unregister_irqs;
970 }
971
972 /*
973 * This makes sure that our colour bitfield
974 * descriptors are correctly initialised.
975 */
976 atmel_lcdfb_check_var(&info->var, info);
977
978 ret = fb_set_var(info, &info->var);
979 if (ret) {
980 dev_warn(dev, "unable to set display parameters\n");
981 goto free_cmap;
982 }
983
984 dev_set_drvdata(dev, info);
985
986 /*
987 * Tell the world that we're ready to go
988 */
989 ret = register_framebuffer(info);
990 if (ret < 0) {
991 dev_err(dev, "failed to register framebuffer device: %d\n", ret);
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -0700992 goto reset_drvdata;
Nicolas Ferre14340582007-05-10 22:23:26 -0700993 }
994
Nicolas Ferre968910b2008-07-23 21:31:34 -0700995 /* add selected videomode to modelist */
996 fb_var_to_videomode(&fbmode, &info->var);
997 fb_add_videomode(&fbmode, &info->modelist);
998
Nicolas Ferre14340582007-05-10 22:23:26 -0700999 /* Power up the LCDC screen */
1000 if (sinfo->atmel_lcdfb_power_control)
1001 sinfo->atmel_lcdfb_power_control(1);
1002
Claudio Scordino93f6ced2009-10-09 12:20:21 +02001003 dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
Nicolas Ferre14340582007-05-10 22:23:26 -07001004 info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
1005
1006 return 0;
1007
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001008reset_drvdata:
1009 dev_set_drvdata(dev, NULL);
Nicolas Ferre14340582007-05-10 22:23:26 -07001010free_cmap:
1011 fb_dealloc_cmap(&info->cmap);
1012unregister_irqs:
Nicolas Ferred22579b2008-07-23 21:31:20 -07001013 cancel_work_sync(&sinfo->task);
Nicolas Ferre14340582007-05-10 22:23:26 -07001014 free_irq(sinfo->irq_base, info);
1015unmap_mmio:
David Brownella9a84c32008-02-06 01:39:26 -08001016 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001017 iounmap(sinfo->mmio);
1018release_mem:
1019 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1020free_fb:
1021 if (map)
1022 iounmap(info->screen_base);
1023 else
1024 atmel_lcdfb_free_video_memory(sinfo);
1025
1026release_intmem:
1027 if (map)
1028 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1029stop_clk:
1030 atmel_lcdfb_stop_clock(sinfo);
1031 clk_put(sinfo->lcdc_clk);
1032put_bus_clk:
1033 if (sinfo->bus_clk)
1034 clk_put(sinfo->bus_clk);
1035free_info:
1036 framebuffer_release(info);
1037out:
1038 dev_dbg(dev, "%s FAILED\n", __func__);
1039 return ret;
1040}
1041
1042static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
1043{
1044 struct device *dev = &pdev->dev;
1045 struct fb_info *info = dev_get_drvdata(dev);
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001046 struct atmel_lcdfb_info *sinfo;
Nicolas Ferre14340582007-05-10 22:23:26 -07001047
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001048 if (!info || !info->par)
Nicolas Ferre14340582007-05-10 22:23:26 -07001049 return 0;
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001050 sinfo = info->par;
Nicolas Ferre14340582007-05-10 22:23:26 -07001051
Nicolas Ferred22579b2008-07-23 21:31:20 -07001052 cancel_work_sync(&sinfo->task);
David Brownella9a84c32008-02-06 01:39:26 -08001053 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001054 if (sinfo->atmel_lcdfb_power_control)
1055 sinfo->atmel_lcdfb_power_control(0);
1056 unregister_framebuffer(info);
1057 atmel_lcdfb_stop_clock(sinfo);
1058 clk_put(sinfo->lcdc_clk);
1059 if (sinfo->bus_clk)
1060 clk_put(sinfo->bus_clk);
1061 fb_dealloc_cmap(&info->cmap);
1062 free_irq(sinfo->irq_base, info);
1063 iounmap(sinfo->mmio);
1064 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1065 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
1066 iounmap(info->screen_base);
1067 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1068 } else {
1069 atmel_lcdfb_free_video_memory(sinfo);
1070 }
1071
1072 dev_set_drvdata(dev, NULL);
1073 framebuffer_release(info);
1074
1075 return 0;
1076}
1077
David Brownellcf19a372008-04-28 02:15:20 -07001078#ifdef CONFIG_PM
1079
1080static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1081{
1082 struct fb_info *info = platform_get_drvdata(pdev);
1083 struct atmel_lcdfb_info *sinfo = info->par;
1084
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001085 /*
1086 * We don't want to handle interrupts while the clock is
1087 * stopped. It may take forever.
1088 */
1089 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
1090
David Brownellcf19a372008-04-28 02:15:20 -07001091 sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
1092 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
1093 if (sinfo->atmel_lcdfb_power_control)
1094 sinfo->atmel_lcdfb_power_control(0);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001095
1096 atmel_lcdfb_stop(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001097 atmel_lcdfb_stop_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001098
David Brownellcf19a372008-04-28 02:15:20 -07001099 return 0;
1100}
1101
1102static int atmel_lcdfb_resume(struct platform_device *pdev)
1103{
1104 struct fb_info *info = platform_get_drvdata(pdev);
1105 struct atmel_lcdfb_info *sinfo = info->par;
1106
1107 atmel_lcdfb_start_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001108 atmel_lcdfb_start(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001109 if (sinfo->atmel_lcdfb_power_control)
1110 sinfo->atmel_lcdfb_power_control(1);
1111 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001112
1113 /* Enable FIFO & DMA errors */
1114 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
1115 | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
1116
David Brownellcf19a372008-04-28 02:15:20 -07001117 return 0;
1118}
1119
1120#else
1121#define atmel_lcdfb_suspend NULL
1122#define atmel_lcdfb_resume NULL
1123#endif
1124
Nicolas Ferre14340582007-05-10 22:23:26 -07001125static struct platform_driver atmel_lcdfb_driver = {
1126 .remove = __exit_p(atmel_lcdfb_remove),
David Brownellcf19a372008-04-28 02:15:20 -07001127 .suspend = atmel_lcdfb_suspend,
1128 .resume = atmel_lcdfb_resume,
David Brownella9a84c32008-02-06 01:39:26 -08001129
Nicolas Ferre14340582007-05-10 22:23:26 -07001130 .driver = {
1131 .name = "atmel_lcdfb",
1132 .owner = THIS_MODULE,
1133 },
1134};
1135
1136static int __init atmel_lcdfb_init(void)
1137{
1138 return platform_driver_probe(&atmel_lcdfb_driver, atmel_lcdfb_probe);
1139}
1140
1141static void __exit atmel_lcdfb_exit(void)
1142{
1143 platform_driver_unregister(&atmel_lcdfb_driver);
1144}
1145
1146module_init(atmel_lcdfb_init);
1147module_exit(atmel_lcdfb_exit);
1148
1149MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
Nicolas Ferre8f4c79c2008-01-14 00:55:13 -08001150MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
Nicolas Ferre14340582007-05-10 22:23:26 -07001151MODULE_LICENSE("GPL");