blob: 8bfee6aecc8bc9308a9d7dd3d2444183131043cf [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>
24#include <mach/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);
Alexander Steinacfdc2e2011-10-05 09:59:58 +0200102 if (contrast_ctr & ATMEL_LCDC_POL_POSITIVE)
103 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
David Brownella9a84c32008-02-06 01:39:26 -0800104 brightness ? contrast_ctr : 0);
Alexander Steinacfdc2e2011-10-05 09:59:58 +0200105 else
106 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
David Brownella9a84c32008-02-06 01:39:26 -0800107
108 bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
109
110 return 0;
111}
112
113static int atmel_bl_get_brightness(struct backlight_device *bl)
114{
115 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
116
117 return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
118}
119
Lionel Debrouxacc24722010-11-16 14:14:02 +0100120static const struct backlight_ops atmel_lcdc_bl_ops = {
David Brownella9a84c32008-02-06 01:39:26 -0800121 .update_status = atmel_bl_update_status,
122 .get_brightness = atmel_bl_get_brightness,
123};
124
125static void init_backlight(struct atmel_lcdfb_info *sinfo)
126{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500127 struct backlight_properties props;
David Brownella9a84c32008-02-06 01:39:26 -0800128 struct backlight_device *bl;
129
130 sinfo->bl_power = FB_BLANK_UNBLANK;
131
132 if (sinfo->backlight)
133 return;
134
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500135 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700136 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500137 props.max_brightness = 0xff;
138 bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
139 &atmel_lcdc_bl_ops, &props);
Julien Brunelcf7b9a12008-11-19 15:36:07 -0800140 if (IS_ERR(bl)) {
David Brownella9a84c32008-02-06 01:39:26 -0800141 dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
142 PTR_ERR(bl));
143 return;
144 }
145 sinfo->backlight = bl;
146
147 bl->props.power = FB_BLANK_UNBLANK;
148 bl->props.fb_blank = FB_BLANK_UNBLANK;
David Brownella9a84c32008-02-06 01:39:26 -0800149 bl->props.brightness = atmel_bl_get_brightness(bl);
150}
151
152static void exit_backlight(struct atmel_lcdfb_info *sinfo)
153{
154 if (sinfo->backlight)
155 backlight_device_unregister(sinfo->backlight);
156}
157
158#else
159
160static void init_backlight(struct atmel_lcdfb_info *sinfo)
161{
162 dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
163}
164
165static void exit_backlight(struct atmel_lcdfb_info *sinfo)
166{
167}
168
169#endif
170
171static void init_contrast(struct atmel_lcdfb_info *sinfo)
172{
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000173 /* contrast pwm can be 'inverted' */
174 if (sinfo->lcdcon_pol_negative)
175 contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE);
176
David Brownella9a84c32008-02-06 01:39:26 -0800177 /* have some default contrast/backlight settings */
178 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
179 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
180
181 if (sinfo->lcdcon_is_backlight)
182 init_backlight(sinfo);
183}
184
Nicolas Ferre14340582007-05-10 22:23:26 -0700185
186static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
187 .type = FB_TYPE_PACKED_PIXELS,
188 .visual = FB_VISUAL_TRUECOLOR,
189 .xpanstep = 0,
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -0700190 .ypanstep = 1,
Nicolas Ferre14340582007-05-10 22:23:26 -0700191 .ywrapstep = 0,
192 .accel = FB_ACCEL_NONE,
193};
194
Nicolas Ferre250a2692007-07-21 04:37:59 -0700195static unsigned long compute_hozval(unsigned long xres, unsigned long lcdcon2)
196{
197 unsigned long value;
198
Nicolas Ferre915190f2009-07-21 11:31:29 +0100199 if (!(cpu_is_at91sam9261() || cpu_is_at91sam9g10()
200 || cpu_is_at32ap7000()))
Nicolas Ferre250a2692007-07-21 04:37:59 -0700201 return xres;
202
203 value = xres;
204 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
205 /* STN display */
206 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
207 value *= 3;
208 }
209 if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
210 || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
211 && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
212 value = DIV_ROUND_UP(value, 4);
213 else
214 value = DIV_ROUND_UP(value, 8);
215 }
216
217 return value;
218}
Nicolas Ferre14340582007-05-10 22:23:26 -0700219
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700220static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
221{
222 /* Turn off the LCD controller and the DMA controller */
223 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
224 sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
225
226 /* Wait for the LCDC core to become idle */
227 while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
228 msleep(10);
229
230 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
231}
232
233static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
234{
235 atmel_lcdfb_stop_nowait(sinfo);
236
237 /* Wait for DMA engine to become idle... */
238 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
239 msleep(10);
240}
241
242static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
243{
244 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
245 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
246 (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
247 | ATMEL_LCDC_PWR);
248}
249
Nicolas Ferre14340582007-05-10 22:23:26 -0700250static void atmel_lcdfb_update_dma(struct fb_info *info,
251 struct fb_var_screeninfo *var)
252{
253 struct atmel_lcdfb_info *sinfo = info->par;
254 struct fb_fix_screeninfo *fix = &info->fix;
255 unsigned long dma_addr;
256
257 dma_addr = (fix->smem_start + var->yoffset * fix->line_length
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200258 + var->xoffset * info->var.bits_per_pixel / 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700259
260 dma_addr &= ~3UL;
261
262 /* Set framebuffer DMA base address and pixel offset */
263 lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
264
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200265 atmel_lcdfb_update_dma2d(sinfo, var, info);
Nicolas Ferre14340582007-05-10 22:23:26 -0700266}
267
268static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
269{
270 struct fb_info *info = sinfo->info;
271
272 dma_free_writecombine(info->device, info->fix.smem_len,
273 info->screen_base, info->fix.smem_start);
274}
275
276/**
277 * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
278 * @sinfo: the frame buffer to allocate memory for
Krzysztof Helt1d01e832009-07-08 22:26:16 +0200279 *
280 * This function is called only from the atmel_lcdfb_probe()
281 * so no locking by fb_info->mm_lock around smem_len setting is needed.
Nicolas Ferre14340582007-05-10 22:23:26 -0700282 */
283static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
284{
285 struct fb_info *info = sinfo->info;
286 struct fb_var_screeninfo *var = &info->var;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700287 unsigned int smem_len;
Nicolas Ferre14340582007-05-10 22:23:26 -0700288
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700289 smem_len = (var->xres_virtual * var->yres_virtual
290 * ((var->bits_per_pixel + 7) / 8));
291 info->fix.smem_len = max(smem_len, sinfo->smem_len);
Nicolas Ferre14340582007-05-10 22:23:26 -0700292
293 info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
294 (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
295
296 if (!info->screen_base) {
297 return -ENOMEM;
298 }
299
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -0700300 memset(info->screen_base, 0, info->fix.smem_len);
301
Nicolas Ferre14340582007-05-10 22:23:26 -0700302 return 0;
303}
304
Nicolas Ferre968910b2008-07-23 21:31:34 -0700305static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
306 struct fb_info *info)
307{
308 struct fb_videomode varfbmode;
309 const struct fb_videomode *fbmode = NULL;
310
311 fb_var_to_videomode(&varfbmode, var);
312 fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
313 if (fbmode)
314 fb_videomode_to_var(var, fbmode);
315 return fbmode;
316}
317
318
Nicolas Ferre14340582007-05-10 22:23:26 -0700319/**
320 * atmel_lcdfb_check_var - Validates a var passed in.
321 * @var: frame buffer variable screen structure
322 * @info: frame buffer structure that represents a single frame buffer
323 *
324 * Checks to see if the hardware supports the state requested by
325 * var passed in. This function does not alter the hardware
326 * state!!! This means the data stored in struct fb_info and
327 * struct atmel_lcdfb_info do not change. This includes the var
328 * inside of struct fb_info. Do NOT change these. This function
329 * can be called on its own if we intent to only test a mode and
330 * not actually set it. The stuff in modedb.c is a example of
331 * this. If the var passed in is slightly off by what the
332 * hardware can support then we alter the var PASSED in to what
333 * we can do. If the hardware doesn't support mode change a
334 * -EINVAL will be returned by the upper layers. You don't need
335 * to implement this function then. If you hardware doesn't
336 * support changing the resolution then this function is not
337 * needed. In this case the driver would just provide a var that
338 * represents the static state the screen is in.
339 *
340 * Returns negative errno on error, or zero on success.
341 */
342static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
343 struct fb_info *info)
344{
345 struct device *dev = info->device;
346 struct atmel_lcdfb_info *sinfo = info->par;
347 unsigned long clk_value_khz;
348
349 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
350
351 dev_dbg(dev, "%s:\n", __func__);
Nicolas Ferre968910b2008-07-23 21:31:34 -0700352
353 if (!(var->pixclock && var->bits_per_pixel)) {
354 /* choose a suitable mode if possible */
355 if (!atmel_lcdfb_choose_mode(var, info)) {
356 dev_err(dev, "needed value not specified\n");
357 return -EINVAL;
358 }
359 }
360
Nicolas Ferre14340582007-05-10 22:23:26 -0700361 dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
362 dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
363 dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
364 dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
365
Ben Nizette97b9a5a2009-06-16 15:34:24 -0700366 if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700367 dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
368 return -EINVAL;
369 }
370
Nicolas Ferre968910b2008-07-23 21:31:34 -0700371 /* Do not allow to have real resoulution larger than virtual */
372 if (var->xres > var->xres_virtual)
373 var->xres_virtual = var->xres;
374
375 if (var->yres > var->yres_virtual)
376 var->yres_virtual = var->yres;
377
Nicolas Ferre14340582007-05-10 22:23:26 -0700378 /* Force same alignment for each line */
379 var->xres = (var->xres + 3) & ~3UL;
380 var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
381
382 var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
383 var->transp.msb_right = 0;
384 var->transp.offset = var->transp.length = 0;
385 var->xoffset = var->yoffset = 0;
386
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700387 if (info->fix.smem_len) {
388 unsigned int smem_len = (var->xres_virtual * var->yres_virtual
389 * ((var->bits_per_pixel + 7) / 8));
390 if (smem_len > info->fix.smem_len)
391 return -EINVAL;
392 }
393
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800394 /* Saturate vertical and horizontal timings at maximum values */
395 var->vsync_len = min_t(u32, var->vsync_len,
396 (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
397 var->upper_margin = min_t(u32, var->upper_margin,
398 ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
399 var->lower_margin = min_t(u32, var->lower_margin,
400 ATMEL_LCDC_VFP);
401 var->right_margin = min_t(u32, var->right_margin,
Alexander Stein5d910422011-10-05 09:59:57 +0200402 (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 2);
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800403 var->hsync_len = min_t(u32, var->hsync_len,
404 (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
405 var->left_margin = min_t(u32, var->left_margin,
406 ATMEL_LCDC_HBP + 1);
407
408 /* Some parameters can't be zero */
409 var->vsync_len = max_t(u32, var->vsync_len, 1);
410 var->right_margin = max_t(u32, var->right_margin, 1);
411 var->hsync_len = max_t(u32, var->hsync_len, 1);
412 var->left_margin = max_t(u32, var->left_margin, 1);
413
Nicolas Ferre14340582007-05-10 22:23:26 -0700414 switch (var->bits_per_pixel) {
Nicolas Ferre250a2692007-07-21 04:37:59 -0700415 case 1:
Nicolas Ferre14340582007-05-10 22:23:26 -0700416 case 2:
417 case 4:
418 case 8:
419 var->red.offset = var->green.offset = var->blue.offset = 0;
420 var->red.length = var->green.length = var->blue.length
421 = var->bits_per_pixel;
422 break;
423 case 15:
424 case 16:
Nicolas Ferrefd085802008-04-28 02:15:21 -0700425 if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
426 /* RGB:565 mode */
427 var->red.offset = 11;
428 var->blue.offset = 0;
429 var->green.length = 6;
Guillaume GARDETfbd03a12008-08-29 10:11:24 +0100430 } else if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB555) {
431 var->red.offset = 10;
432 var->blue.offset = 0;
433 var->green.length = 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700434 } else {
435 /* BGR:555 mode */
436 var->red.offset = 0;
437 var->blue.offset = 10;
438 var->green.length = 5;
439 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700440 var->green.offset = 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700441 var->red.length = var->blue.length = 5;
Nicolas Ferre14340582007-05-10 22:23:26 -0700442 break;
Nicolas Ferre14340582007-05-10 22:23:26 -0700443 case 32:
Haavard Skinnemoen4440e0e2007-07-21 04:38:02 -0700444 var->transp.offset = 24;
445 var->transp.length = 8;
446 /* fall through */
447 case 24:
Nicolas Ferrefd085802008-04-28 02:15:21 -0700448 if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
449 /* RGB:888 mode */
450 var->red.offset = 16;
451 var->blue.offset = 0;
452 } else {
453 /* BGR:888 mode */
454 var->red.offset = 0;
455 var->blue.offset = 16;
456 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700457 var->green.offset = 8;
Nicolas Ferre14340582007-05-10 22:23:26 -0700458 var->red.length = var->green.length = var->blue.length = 8;
459 break;
460 default:
461 dev_err(dev, "color depth %d not supported\n",
462 var->bits_per_pixel);
463 return -EINVAL;
464 }
465
466 return 0;
467}
468
Nicolas Ferred22579b2008-07-23 21:31:20 -0700469/*
470 * LCD reset sequence
471 */
472static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
473{
474 might_sleep();
475
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700476 atmel_lcdfb_stop(sinfo);
477 atmel_lcdfb_start(sinfo);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700478}
479
Nicolas Ferre14340582007-05-10 22:23:26 -0700480/**
481 * atmel_lcdfb_set_par - Alters the hardware state.
482 * @info: frame buffer structure that represents a single frame buffer
483 *
484 * Using the fb_var_screeninfo in fb_info we set the resolution
485 * of the this particular framebuffer. This function alters the
486 * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
487 * not alter var in fb_info since we are using that data. This
488 * means we depend on the data in var inside fb_info to be
489 * supported by the hardware. atmel_lcdfb_check_var is always called
490 * before atmel_lcdfb_set_par to ensure this. Again if you can't
491 * change the resolution you don't need this function.
492 *
493 */
494static int atmel_lcdfb_set_par(struct fb_info *info)
495{
496 struct atmel_lcdfb_info *sinfo = info->par;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700497 unsigned long hozval_linesz;
Nicolas Ferre14340582007-05-10 22:23:26 -0700498 unsigned long value;
499 unsigned long clk_value_khz;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700500 unsigned long bits_per_line;
Nicolas Ferre431861c2009-11-11 14:26:35 -0800501 unsigned long pix_factor = 2;
Nicolas Ferre14340582007-05-10 22:23:26 -0700502
Nicolas Ferred22579b2008-07-23 21:31:20 -0700503 might_sleep();
504
Nicolas Ferre14340582007-05-10 22:23:26 -0700505 dev_dbg(info->device, "%s:\n", __func__);
506 dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
507 info->var.xres, info->var.yres,
508 info->var.xres_virtual, info->var.yres_virtual);
509
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700510 atmel_lcdfb_stop_nowait(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700511
Nicolas Ferre250a2692007-07-21 04:37:59 -0700512 if (info->var.bits_per_pixel == 1)
513 info->fix.visual = FB_VISUAL_MONO01;
514 else if (info->var.bits_per_pixel <= 8)
Nicolas Ferre14340582007-05-10 22:23:26 -0700515 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
516 else
517 info->fix.visual = FB_VISUAL_TRUECOLOR;
518
Nicolas Ferre250a2692007-07-21 04:37:59 -0700519 bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
520 info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700521
522 /* Re-initialize the DMA engine... */
523 dev_dbg(info->device, " * update DMA engine\n");
524 atmel_lcdfb_update_dma(info, &info->var);
525
526 /* ...set frame size and burst length = 8 words (?) */
527 value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
528 value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
529 lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
530
531 /* Now, the LCDC core... */
532
533 /* Set pixel clock */
Nicolas Ferre431861c2009-11-11 14:26:35 -0800534 if (cpu_is_at91sam9g45() && !cpu_is_at91sam9g45es())
535 pix_factor = 1;
536
Nicolas Ferre14340582007-05-10 22:23:26 -0700537 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
538
Nicolas Ferre250a2692007-07-21 04:37:59 -0700539 value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
Nicolas Ferre14340582007-05-10 22:23:26 -0700540
Nicolas Ferre431861c2009-11-11 14:26:35 -0800541 if (value < pix_factor) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700542 dev_notice(info->device, "Bypassing pixel clock divider\n");
543 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700544 } else {
Nicolas Ferre431861c2009-11-11 14:26:35 -0800545 value = (value / pix_factor) - 1;
Nicolas Ferrebaf63322008-05-12 14:02:25 -0700546 dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n",
547 value);
548 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
549 value << ATMEL_LCDC_CLKVAL_OFFSET);
Nicolas Ferre431861c2009-11-11 14:26:35 -0800550 info->var.pixclock =
551 KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
Nicolas Ferre250a2692007-07-21 04:37:59 -0700552 dev_dbg(info->device, " updated pixclk: %lu KHz\n",
553 PICOS2KHZ(info->var.pixclock));
554 }
555
Nicolas Ferre14340582007-05-10 22:23:26 -0700556
557 /* Initialize control register 2 */
558 value = sinfo->default_lcdcon2;
559
560 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
561 value |= ATMEL_LCDC_INVLINE_INVERTED;
562 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
563 value |= ATMEL_LCDC_INVFRAME_INVERTED;
564
565 switch (info->var.bits_per_pixel) {
566 case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
567 case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
568 case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
569 case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
570 case 15: /* fall through */
571 case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
572 case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
573 case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
574 default: BUG(); break;
575 }
576 dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
577 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
578
579 /* Vertical timing */
580 value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
581 value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
582 value |= info->var.lower_margin;
583 dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
584 lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
585
586 /* Horizontal timing */
Alexander Stein5d910422011-10-05 09:59:57 +0200587 value = (info->var.right_margin - 2) << ATMEL_LCDC_HFP_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700588 value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
589 value |= (info->var.left_margin - 1);
590 dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
591 lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
592
Nicolas Ferre250a2692007-07-21 04:37:59 -0700593 /* Horizontal value (aka line size) */
594 hozval_linesz = compute_hozval(info->var.xres,
595 lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2));
596
Nicolas Ferre14340582007-05-10 22:23:26 -0700597 /* Display size */
Nicolas Ferre250a2692007-07-21 04:37:59 -0700598 value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700599 value |= info->var.yres - 1;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700600 dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value);
Nicolas Ferre14340582007-05-10 22:23:26 -0700601 lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
602
603 /* FIFO Threshold: Use formula from data sheet */
604 value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
605 lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
606
607 /* Toggle LCD_MODE every frame */
608 lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
609
610 /* Disable all interrupts */
611 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700612 /* Enable FIFO & DMA errors */
613 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
Nicolas Ferre14340582007-05-10 22:23:26 -0700614
Nicolas Ferre14340582007-05-10 22:23:26 -0700615 /* ...wait for DMA engine to become idle... */
616 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
617 msleep(10);
618
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700619 atmel_lcdfb_start(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700620
621 dev_dbg(info->device, " * DONE\n");
622
623 return 0;
624}
625
626static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
627{
628 chan &= 0xffff;
629 chan >>= 16 - bf->length;
630 return chan << bf->offset;
631}
632
633/**
634 * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
635 * @regno: Which register in the CLUT we are programming
636 * @red: The red value which can be up to 16 bits wide
637 * @green: The green value which can be up to 16 bits wide
638 * @blue: The blue value which can be up to 16 bits wide.
639 * @transp: If supported the alpha value which can be up to 16 bits wide.
640 * @info: frame buffer info structure
641 *
642 * Set a single color register. The values supplied have a 16 bit
643 * magnitude which needs to be scaled in this function for the hardware.
644 * Things to take into consideration are how many color registers, if
645 * any, are supported with the current color visual. With truecolor mode
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300646 * no color palettes are supported. Here a pseudo palette is created
Nicolas Ferre14340582007-05-10 22:23:26 -0700647 * which we store the value in pseudo_palette in struct fb_info. For
648 * pseudocolor mode we have a limited color palette. To deal with this
649 * we can program what color is displayed for a particular pixel value.
650 * DirectColor is similar in that we can program each color field. If
651 * we have a static colormap we don't need to implement this function.
652 *
653 * Returns negative errno on error, or zero on success. In an
654 * ideal world, this would have been the case, but as it turns
655 * out, the other drivers return 1 on failure, so that's what
656 * we're going to do.
657 */
658static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
659 unsigned int green, unsigned int blue,
660 unsigned int transp, struct fb_info *info)
661{
662 struct atmel_lcdfb_info *sinfo = info->par;
663 unsigned int val;
664 u32 *pal;
665 int ret = 1;
666
667 if (info->var.grayscale)
668 red = green = blue = (19595 * red + 38470 * green
669 + 7471 * blue) >> 16;
670
671 switch (info->fix.visual) {
672 case FB_VISUAL_TRUECOLOR:
673 if (regno < 16) {
674 pal = info->pseudo_palette;
675
676 val = chan_to_field(red, &info->var.red);
677 val |= chan_to_field(green, &info->var.green);
678 val |= chan_to_field(blue, &info->var.blue);
679
680 pal[regno] = val;
681 ret = 0;
682 }
683 break;
684
685 case FB_VISUAL_PSEUDOCOLOR:
686 if (regno < 256) {
687 val = ((red >> 11) & 0x001f);
688 val |= ((green >> 6) & 0x03e0);
689 val |= ((blue >> 1) & 0x7c00);
690
691 /*
692 * TODO: intensity bit. Maybe something like
693 * ~(red[10] ^ green[10] ^ blue[10]) & 1
694 */
695
696 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
697 ret = 0;
698 }
699 break;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700700
701 case FB_VISUAL_MONO01:
702 if (regno < 2) {
703 val = (regno == 0) ? 0x00 : 0x1F;
704 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
705 ret = 0;
706 }
707 break;
708
Nicolas Ferre14340582007-05-10 22:23:26 -0700709 }
710
711 return ret;
712}
713
714static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
715 struct fb_info *info)
716{
717 dev_dbg(info->device, "%s\n", __func__);
718
719 atmel_lcdfb_update_dma(info, var);
720
721 return 0;
722}
723
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000724static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info)
725{
726 struct atmel_lcdfb_info *sinfo = info->par;
727
728 switch (blank_mode) {
729 case FB_BLANK_UNBLANK:
730 case FB_BLANK_NORMAL:
731 atmel_lcdfb_start(sinfo);
732 break;
733 case FB_BLANK_VSYNC_SUSPEND:
734 case FB_BLANK_HSYNC_SUSPEND:
735 break;
736 case FB_BLANK_POWERDOWN:
737 atmel_lcdfb_stop(sinfo);
738 break;
739 default:
740 return -EINVAL;
741 }
742
743 /* let fbcon do a soft blank for us */
744 return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
745}
746
Nicolas Ferre14340582007-05-10 22:23:26 -0700747static struct fb_ops atmel_lcdfb_ops = {
748 .owner = THIS_MODULE,
749 .fb_check_var = atmel_lcdfb_check_var,
750 .fb_set_par = atmel_lcdfb_set_par,
751 .fb_setcolreg = atmel_lcdfb_setcolreg,
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000752 .fb_blank = atmel_lcdfb_blank,
Nicolas Ferre14340582007-05-10 22:23:26 -0700753 .fb_pan_display = atmel_lcdfb_pan_display,
754 .fb_fillrect = cfb_fillrect,
755 .fb_copyarea = cfb_copyarea,
756 .fb_imageblit = cfb_imageblit,
757};
758
759static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
760{
761 struct fb_info *info = dev_id;
762 struct atmel_lcdfb_info *sinfo = info->par;
763 u32 status;
764
765 status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700766 if (status & ATMEL_LCDC_UFLWI) {
767 dev_warn(info->device, "FIFO underflow %#x\n", status);
768 /* reset DMA and FIFO to avoid screen shifting */
769 schedule_work(&sinfo->task);
770 }
771 lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
Nicolas Ferre14340582007-05-10 22:23:26 -0700772 return IRQ_HANDLED;
773}
774
Nicolas Ferred22579b2008-07-23 21:31:20 -0700775/*
776 * LCD controller task (to reset the LCD)
777 */
778static void atmel_lcdfb_task(struct work_struct *work)
779{
780 struct atmel_lcdfb_info *sinfo =
781 container_of(work, struct atmel_lcdfb_info, task);
782
783 atmel_lcdfb_reset(sinfo);
784}
785
Nicolas Ferre14340582007-05-10 22:23:26 -0700786static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
787{
788 struct fb_info *info = sinfo->info;
789 int ret = 0;
790
Nicolas Ferre14340582007-05-10 22:23:26 -0700791 info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
792
793 dev_info(info->device,
794 "%luKiB frame buffer at %08lx (mapped at %p)\n",
795 (unsigned long)info->fix.smem_len / 1024,
796 (unsigned long)info->fix.smem_start,
797 info->screen_base);
798
799 /* Allocate colormap */
800 ret = fb_alloc_cmap(&info->cmap, 256, 0);
801 if (ret < 0)
802 dev_err(info->device, "Alloc color map failed\n");
803
804 return ret;
805}
806
807static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
808{
809 if (sinfo->bus_clk)
810 clk_enable(sinfo->bus_clk);
811 clk_enable(sinfo->lcdc_clk);
812}
813
814static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
815{
816 if (sinfo->bus_clk)
817 clk_disable(sinfo->bus_clk);
818 clk_disable(sinfo->lcdc_clk);
819}
820
821
822static int __init atmel_lcdfb_probe(struct platform_device *pdev)
823{
824 struct device *dev = &pdev->dev;
825 struct fb_info *info;
826 struct atmel_lcdfb_info *sinfo;
827 struct atmel_lcdfb_info *pdata_sinfo;
Nicolas Ferre968910b2008-07-23 21:31:34 -0700828 struct fb_videomode fbmode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700829 struct resource *regs = NULL;
830 struct resource *map = NULL;
831 int ret;
832
833 dev_dbg(dev, "%s BEGIN\n", __func__);
834
835 ret = -ENOMEM;
836 info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
837 if (!info) {
838 dev_err(dev, "cannot allocate memory\n");
839 goto out;
840 }
841
842 sinfo = info->par;
843
844 if (dev->platform_data) {
845 pdata_sinfo = (struct atmel_lcdfb_info *)dev->platform_data;
846 sinfo->default_bpp = pdata_sinfo->default_bpp;
847 sinfo->default_dmacon = pdata_sinfo->default_dmacon;
848 sinfo->default_lcdcon2 = pdata_sinfo->default_lcdcon2;
849 sinfo->default_monspecs = pdata_sinfo->default_monspecs;
850 sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
851 sinfo->guard_time = pdata_sinfo->guard_time;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700852 sinfo->smem_len = pdata_sinfo->smem_len;
David Brownella9a84c32008-02-06 01:39:26 -0800853 sinfo->lcdcon_is_backlight = pdata_sinfo->lcdcon_is_backlight;
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000854 sinfo->lcdcon_pol_negative = pdata_sinfo->lcdcon_pol_negative;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700855 sinfo->lcd_wiring_mode = pdata_sinfo->lcd_wiring_mode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700856 } else {
857 dev_err(dev, "cannot get default configuration\n");
858 goto free_info;
859 }
860 sinfo->info = info;
861 sinfo->pdev = pdev;
862
863 strcpy(info->fix.id, sinfo->pdev->name);
864 info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
865 info->pseudo_palette = sinfo->pseudo_palette;
866 info->fbops = &atmel_lcdfb_ops;
867
868 memcpy(&info->monspecs, sinfo->default_monspecs, sizeof(info->monspecs));
869 info->fix = atmel_lcdfb_fix;
870
871 /* Enable LCDC Clocks */
Nicolas Ferre915190f2009-07-21 11:31:29 +0100872 if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()
873 || cpu_is_at32ap7000()) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700874 sinfo->bus_clk = clk_get(dev, "hck1");
875 if (IS_ERR(sinfo->bus_clk)) {
876 ret = PTR_ERR(sinfo->bus_clk);
877 goto free_info;
878 }
879 }
880 sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
881 if (IS_ERR(sinfo->lcdc_clk)) {
882 ret = PTR_ERR(sinfo->lcdc_clk);
883 goto put_bus_clk;
884 }
885 atmel_lcdfb_start_clock(sinfo);
886
887 ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
888 info->monspecs.modedb_len, info->monspecs.modedb,
889 sinfo->default_bpp);
890 if (!ret) {
891 dev_err(dev, "no suitable video mode found\n");
892 goto stop_clk;
893 }
894
895
896 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
897 if (!regs) {
898 dev_err(dev, "resources unusable\n");
899 ret = -ENXIO;
900 goto stop_clk;
901 }
902
903 sinfo->irq_base = platform_get_irq(pdev, 0);
904 if (sinfo->irq_base < 0) {
905 dev_err(dev, "unable to get irq\n");
906 ret = sinfo->irq_base;
907 goto stop_clk;
908 }
909
910 /* Initialize video memory */
911 map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
912 if (map) {
913 /* use a pre-allocated memory buffer */
914 info->fix.smem_start = map->start;
Joe Perches28f65c12011-06-09 09:13:32 -0700915 info->fix.smem_len = resource_size(map);
Nicolas Ferre14340582007-05-10 22:23:26 -0700916 if (!request_mem_region(info->fix.smem_start,
917 info->fix.smem_len, pdev->name)) {
918 ret = -EBUSY;
919 goto stop_clk;
920 }
921
922 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
923 if (!info->screen_base)
924 goto release_intmem;
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -0700925
926 /*
927 * Don't clear the framebuffer -- someone may have set
928 * up a splash image.
929 */
Nicolas Ferre14340582007-05-10 22:23:26 -0700930 } else {
931 /* alocate memory buffer */
932 ret = atmel_lcdfb_alloc_video_memory(sinfo);
933 if (ret < 0) {
934 dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
935 goto stop_clk;
936 }
937 }
938
939 /* LCDC registers */
940 info->fix.mmio_start = regs->start;
Joe Perches28f65c12011-06-09 09:13:32 -0700941 info->fix.mmio_len = resource_size(regs);
Nicolas Ferre14340582007-05-10 22:23:26 -0700942
943 if (!request_mem_region(info->fix.mmio_start,
944 info->fix.mmio_len, pdev->name)) {
945 ret = -EBUSY;
946 goto free_fb;
947 }
948
949 sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
950 if (!sinfo->mmio) {
951 dev_err(dev, "cannot map LCDC registers\n");
952 goto release_mem;
953 }
954
David Brownella9a84c32008-02-06 01:39:26 -0800955 /* Initialize PWM for contrast or backlight ("off") */
956 init_contrast(sinfo);
957
Nicolas Ferre14340582007-05-10 22:23:26 -0700958 /* interrupt */
959 ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
960 if (ret) {
961 dev_err(dev, "request_irq failed: %d\n", ret);
962 goto unmap_mmio;
963 }
964
Nicolas Ferred22579b2008-07-23 21:31:20 -0700965 /* Some operations on the LCDC might sleep and
966 * require a preemptible task context */
967 INIT_WORK(&sinfo->task, atmel_lcdfb_task);
968
Nicolas Ferre14340582007-05-10 22:23:26 -0700969 ret = atmel_lcdfb_init_fbinfo(sinfo);
970 if (ret < 0) {
971 dev_err(dev, "init fbinfo failed: %d\n", ret);
972 goto unregister_irqs;
973 }
974
975 /*
976 * This makes sure that our colour bitfield
977 * descriptors are correctly initialised.
978 */
979 atmel_lcdfb_check_var(&info->var, info);
980
981 ret = fb_set_var(info, &info->var);
982 if (ret) {
983 dev_warn(dev, "unable to set display parameters\n");
984 goto free_cmap;
985 }
986
987 dev_set_drvdata(dev, info);
988
989 /*
990 * Tell the world that we're ready to go
991 */
992 ret = register_framebuffer(info);
993 if (ret < 0) {
994 dev_err(dev, "failed to register framebuffer device: %d\n", ret);
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -0700995 goto reset_drvdata;
Nicolas Ferre14340582007-05-10 22:23:26 -0700996 }
997
Nicolas Ferre968910b2008-07-23 21:31:34 -0700998 /* add selected videomode to modelist */
999 fb_var_to_videomode(&fbmode, &info->var);
1000 fb_add_videomode(&fbmode, &info->modelist);
1001
Nicolas Ferre14340582007-05-10 22:23:26 -07001002 /* Power up the LCDC screen */
1003 if (sinfo->atmel_lcdfb_power_control)
1004 sinfo->atmel_lcdfb_power_control(1);
1005
Claudio Scordino93f6ced2009-10-09 12:20:21 +02001006 dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
Nicolas Ferre14340582007-05-10 22:23:26 -07001007 info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
1008
1009 return 0;
1010
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001011reset_drvdata:
1012 dev_set_drvdata(dev, NULL);
Nicolas Ferre14340582007-05-10 22:23:26 -07001013free_cmap:
1014 fb_dealloc_cmap(&info->cmap);
1015unregister_irqs:
Nicolas Ferred22579b2008-07-23 21:31:20 -07001016 cancel_work_sync(&sinfo->task);
Nicolas Ferre14340582007-05-10 22:23:26 -07001017 free_irq(sinfo->irq_base, info);
1018unmap_mmio:
David Brownella9a84c32008-02-06 01:39:26 -08001019 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001020 iounmap(sinfo->mmio);
1021release_mem:
1022 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1023free_fb:
1024 if (map)
1025 iounmap(info->screen_base);
1026 else
1027 atmel_lcdfb_free_video_memory(sinfo);
1028
1029release_intmem:
1030 if (map)
1031 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1032stop_clk:
1033 atmel_lcdfb_stop_clock(sinfo);
1034 clk_put(sinfo->lcdc_clk);
1035put_bus_clk:
1036 if (sinfo->bus_clk)
1037 clk_put(sinfo->bus_clk);
1038free_info:
1039 framebuffer_release(info);
1040out:
1041 dev_dbg(dev, "%s FAILED\n", __func__);
1042 return ret;
1043}
1044
1045static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
1046{
1047 struct device *dev = &pdev->dev;
1048 struct fb_info *info = dev_get_drvdata(dev);
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001049 struct atmel_lcdfb_info *sinfo;
Nicolas Ferre14340582007-05-10 22:23:26 -07001050
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001051 if (!info || !info->par)
Nicolas Ferre14340582007-05-10 22:23:26 -07001052 return 0;
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001053 sinfo = info->par;
Nicolas Ferre14340582007-05-10 22:23:26 -07001054
Nicolas Ferred22579b2008-07-23 21:31:20 -07001055 cancel_work_sync(&sinfo->task);
David Brownella9a84c32008-02-06 01:39:26 -08001056 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001057 if (sinfo->atmel_lcdfb_power_control)
1058 sinfo->atmel_lcdfb_power_control(0);
1059 unregister_framebuffer(info);
1060 atmel_lcdfb_stop_clock(sinfo);
1061 clk_put(sinfo->lcdc_clk);
1062 if (sinfo->bus_clk)
1063 clk_put(sinfo->bus_clk);
1064 fb_dealloc_cmap(&info->cmap);
1065 free_irq(sinfo->irq_base, info);
1066 iounmap(sinfo->mmio);
1067 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1068 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
1069 iounmap(info->screen_base);
1070 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1071 } else {
1072 atmel_lcdfb_free_video_memory(sinfo);
1073 }
1074
1075 dev_set_drvdata(dev, NULL);
1076 framebuffer_release(info);
1077
1078 return 0;
1079}
1080
David Brownellcf19a372008-04-28 02:15:20 -07001081#ifdef CONFIG_PM
1082
1083static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1084{
1085 struct fb_info *info = platform_get_drvdata(pdev);
1086 struct atmel_lcdfb_info *sinfo = info->par;
1087
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001088 /*
1089 * We don't want to handle interrupts while the clock is
1090 * stopped. It may take forever.
1091 */
1092 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
1093
David Brownellcf19a372008-04-28 02:15:20 -07001094 sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
1095 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
1096 if (sinfo->atmel_lcdfb_power_control)
1097 sinfo->atmel_lcdfb_power_control(0);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001098
1099 atmel_lcdfb_stop(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001100 atmel_lcdfb_stop_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001101
David Brownellcf19a372008-04-28 02:15:20 -07001102 return 0;
1103}
1104
1105static int atmel_lcdfb_resume(struct platform_device *pdev)
1106{
1107 struct fb_info *info = platform_get_drvdata(pdev);
1108 struct atmel_lcdfb_info *sinfo = info->par;
1109
1110 atmel_lcdfb_start_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001111 atmel_lcdfb_start(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001112 if (sinfo->atmel_lcdfb_power_control)
1113 sinfo->atmel_lcdfb_power_control(1);
1114 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001115
1116 /* Enable FIFO & DMA errors */
1117 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
1118 | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
1119
David Brownellcf19a372008-04-28 02:15:20 -07001120 return 0;
1121}
1122
1123#else
1124#define atmel_lcdfb_suspend NULL
1125#define atmel_lcdfb_resume NULL
1126#endif
1127
Nicolas Ferre14340582007-05-10 22:23:26 -07001128static struct platform_driver atmel_lcdfb_driver = {
1129 .remove = __exit_p(atmel_lcdfb_remove),
David Brownellcf19a372008-04-28 02:15:20 -07001130 .suspend = atmel_lcdfb_suspend,
1131 .resume = atmel_lcdfb_resume,
David Brownella9a84c32008-02-06 01:39:26 -08001132
Nicolas Ferre14340582007-05-10 22:23:26 -07001133 .driver = {
1134 .name = "atmel_lcdfb",
1135 .owner = THIS_MODULE,
1136 },
1137};
1138
1139static int __init atmel_lcdfb_init(void)
1140{
1141 return platform_driver_probe(&atmel_lcdfb_driver, atmel_lcdfb_probe);
1142}
1143
1144static void __exit atmel_lcdfb_exit(void)
1145{
1146 platform_driver_unregister(&atmel_lcdfb_driver);
1147}
1148
1149module_init(atmel_lcdfb_init);
1150module_exit(atmel_lcdfb_exit);
1151
1152MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
Nicolas Ferre8f4c79c2008-01-14 00:55:13 -08001153MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
Nicolas Ferre14340582007-05-10 22:23:26 -07001154MODULE_LICENSE("GPL");