blob: 3f7d6dc3408db19b3cb2b14661d4ad0310c74704 [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>
Paul Gortmaker355b2002011-07-03 16:17:28 -040021#include <linux/module.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080022#include <linux/platform_data/atmel.h>
Nicolas Ferre14340582007-05-10 22:23:26 -070023
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/cpu.h>
Russell King60e89722011-07-26 10:56:19 +010025#include <asm/gpio.h>
Nicolas Ferre14340582007-05-10 22:23:26 -070026
27#include <video/atmel_lcdc.h>
28
29#define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
30#define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
31
32/* configurable parameters */
33#define ATMEL_LCDC_CVAL_DEFAULT 0xc8
Nicolas Ferre53b74792009-05-28 14:34:36 -070034#define ATMEL_LCDC_DMA_BURST_LEN 8 /* words */
35#define ATMEL_LCDC_FIFO_SIZE 512 /* words */
Nicolas Ferre14340582007-05-10 22:23:26 -070036
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +010037struct atmel_lcdfb_config {
38 bool have_alt_pixclock;
39 bool have_hozval;
40 bool have_intensity_bit;
41};
42
43static struct atmel_lcdfb_config at91sam9261_config = {
44 .have_hozval = true,
45 .have_intensity_bit = true,
46};
47
48static struct atmel_lcdfb_config at91sam9263_config = {
49 .have_intensity_bit = true,
50};
51
52static struct atmel_lcdfb_config at91sam9g10_config = {
53 .have_hozval = true,
54};
55
56static struct atmel_lcdfb_config at91sam9g45_config = {
57 .have_alt_pixclock = true,
58};
59
60static struct atmel_lcdfb_config at91sam9g45es_config = {
61};
62
63static struct atmel_lcdfb_config at91sam9rl_config = {
64 .have_intensity_bit = true,
65};
66
67static struct atmel_lcdfb_config at32ap_config = {
68 .have_hozval = true,
69};
70
71static const struct platform_device_id atmel_lcdfb_devtypes[] = {
72 {
73 .name = "at91sam9261-lcdfb",
74 .driver_data = (unsigned long)&at91sam9261_config,
75 }, {
76 .name = "at91sam9263-lcdfb",
77 .driver_data = (unsigned long)&at91sam9263_config,
78 }, {
79 .name = "at91sam9g10-lcdfb",
80 .driver_data = (unsigned long)&at91sam9g10_config,
81 }, {
82 .name = "at91sam9g45-lcdfb",
83 .driver_data = (unsigned long)&at91sam9g45_config,
84 }, {
85 .name = "at91sam9g45es-lcdfb",
86 .driver_data = (unsigned long)&at91sam9g45es_config,
87 }, {
88 .name = "at91sam9rl-lcdfb",
89 .driver_data = (unsigned long)&at91sam9rl_config,
90 }, {
91 .name = "at32ap-lcdfb",
92 .driver_data = (unsigned long)&at32ap_config,
93 }, {
94 /* terminator */
95 }
96};
97
98static struct atmel_lcdfb_config *
99atmel_lcdfb_get_config(struct platform_device *pdev)
100{
101 unsigned long data;
102
103 data = platform_get_device_id(pdev)->driver_data;
104
105 return (struct atmel_lcdfb_config *)data;
106}
107
Nicolas Ferre14340582007-05-10 22:23:26 -0700108#if defined(CONFIG_ARCH_AT91)
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -0700109#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
110 | FBINFO_PARTIAL_PAN_OK \
111 | FBINFO_HWACCEL_YPAN)
Nicolas Ferre14340582007-05-10 22:23:26 -0700112
113static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200114 struct fb_var_screeninfo *var,
115 struct fb_info *info)
Nicolas Ferre14340582007-05-10 22:23:26 -0700116{
117
118}
119#elif defined(CONFIG_AVR32)
120#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
121 | FBINFO_PARTIAL_PAN_OK \
122 | FBINFO_HWACCEL_XPAN \
123 | FBINFO_HWACCEL_YPAN)
124
125static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200126 struct fb_var_screeninfo *var,
127 struct fb_info *info)
Nicolas Ferre14340582007-05-10 22:23:26 -0700128{
129 u32 dma2dcfg;
130 u32 pixeloff;
131
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200132 pixeloff = (var->xoffset * info->var.bits_per_pixel) & 0x1f;
Nicolas Ferre14340582007-05-10 22:23:26 -0700133
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200134 dma2dcfg = (info->var.xres_virtual - info->var.xres)
135 * info->var.bits_per_pixel / 8;
Nicolas Ferre14340582007-05-10 22:23:26 -0700136 dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
137 lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
138
139 /* Update configuration */
140 lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
141 lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
142 | ATMEL_LCDC_DMAUPDT);
143}
144#endif
145
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000146static u32 contrast_ctr = ATMEL_LCDC_PS_DIV8
David Brownella9a84c32008-02-06 01:39:26 -0800147 | ATMEL_LCDC_POL_POSITIVE
148 | ATMEL_LCDC_ENA_PWMENABLE;
149
150#ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
151
152/* some bl->props field just changed */
153static int atmel_bl_update_status(struct backlight_device *bl)
154{
155 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
156 int power = sinfo->bl_power;
157 int brightness = bl->props.brightness;
158
159 /* REVISIT there may be a meaningful difference between
160 * fb_blank and power ... there seem to be some cases
161 * this doesn't handle correctly.
162 */
163 if (bl->props.fb_blank != sinfo->bl_power)
164 power = bl->props.fb_blank;
165 else if (bl->props.power != sinfo->bl_power)
166 power = bl->props.power;
167
168 if (brightness < 0 && power == FB_BLANK_UNBLANK)
169 brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
170 else if (power != FB_BLANK_UNBLANK)
171 brightness = 0;
172
173 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness);
Alexander Steinacfdc2e2011-10-05 09:59:58 +0200174 if (contrast_ctr & ATMEL_LCDC_POL_POSITIVE)
175 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
David Brownella9a84c32008-02-06 01:39:26 -0800176 brightness ? contrast_ctr : 0);
Alexander Steinacfdc2e2011-10-05 09:59:58 +0200177 else
178 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
David Brownella9a84c32008-02-06 01:39:26 -0800179
180 bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
181
182 return 0;
183}
184
185static int atmel_bl_get_brightness(struct backlight_device *bl)
186{
187 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
188
189 return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
190}
191
Lionel Debrouxacc24722010-11-16 14:14:02 +0100192static const struct backlight_ops atmel_lcdc_bl_ops = {
David Brownella9a84c32008-02-06 01:39:26 -0800193 .update_status = atmel_bl_update_status,
194 .get_brightness = atmel_bl_get_brightness,
195};
196
197static void init_backlight(struct atmel_lcdfb_info *sinfo)
198{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500199 struct backlight_properties props;
David Brownella9a84c32008-02-06 01:39:26 -0800200 struct backlight_device *bl;
201
202 sinfo->bl_power = FB_BLANK_UNBLANK;
203
204 if (sinfo->backlight)
205 return;
206
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500207 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700208 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500209 props.max_brightness = 0xff;
210 bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
211 &atmel_lcdc_bl_ops, &props);
Julien Brunelcf7b9a12008-11-19 15:36:07 -0800212 if (IS_ERR(bl)) {
David Brownella9a84c32008-02-06 01:39:26 -0800213 dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
214 PTR_ERR(bl));
215 return;
216 }
217 sinfo->backlight = bl;
218
219 bl->props.power = FB_BLANK_UNBLANK;
220 bl->props.fb_blank = FB_BLANK_UNBLANK;
David Brownella9a84c32008-02-06 01:39:26 -0800221 bl->props.brightness = atmel_bl_get_brightness(bl);
222}
223
224static void exit_backlight(struct atmel_lcdfb_info *sinfo)
225{
Richard Genoud56c21b52013-05-31 15:49:35 +0000226 if (!sinfo->backlight)
227 return;
228
229 if (sinfo->backlight->ops) {
230 sinfo->backlight->props.power = FB_BLANK_POWERDOWN;
231 sinfo->backlight->ops->update_status(sinfo->backlight);
232 }
233 backlight_device_unregister(sinfo->backlight);
David Brownella9a84c32008-02-06 01:39:26 -0800234}
235
236#else
237
238static void init_backlight(struct atmel_lcdfb_info *sinfo)
239{
240 dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
241}
242
243static void exit_backlight(struct atmel_lcdfb_info *sinfo)
244{
245}
246
247#endif
248
249static void init_contrast(struct atmel_lcdfb_info *sinfo)
250{
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000251 /* contrast pwm can be 'inverted' */
252 if (sinfo->lcdcon_pol_negative)
253 contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE);
254
David Brownella9a84c32008-02-06 01:39:26 -0800255 /* have some default contrast/backlight settings */
256 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
257 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
258
259 if (sinfo->lcdcon_is_backlight)
260 init_backlight(sinfo);
261}
262
Nicolas Ferre14340582007-05-10 22:23:26 -0700263
264static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
265 .type = FB_TYPE_PACKED_PIXELS,
266 .visual = FB_VISUAL_TRUECOLOR,
267 .xpanstep = 0,
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -0700268 .ypanstep = 1,
Nicolas Ferre14340582007-05-10 22:23:26 -0700269 .ywrapstep = 0,
270 .accel = FB_ACCEL_NONE,
271};
272
Johan Hovold934a50b2013-02-07 16:31:57 +0100273static unsigned long compute_hozval(struct atmel_lcdfb_info *sinfo,
274 unsigned long xres)
Nicolas Ferre250a2692007-07-21 04:37:59 -0700275{
Johan Hovold934a50b2013-02-07 16:31:57 +0100276 unsigned long lcdcon2;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700277 unsigned long value;
278
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100279 if (!sinfo->config->have_hozval)
Nicolas Ferre250a2692007-07-21 04:37:59 -0700280 return xres;
281
Johan Hovold934a50b2013-02-07 16:31:57 +0100282 lcdcon2 = lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700283 value = xres;
284 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
285 /* STN display */
286 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
287 value *= 3;
288 }
289 if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
290 || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
291 && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
292 value = DIV_ROUND_UP(value, 4);
293 else
294 value = DIV_ROUND_UP(value, 8);
295 }
296
297 return value;
298}
Nicolas Ferre14340582007-05-10 22:23:26 -0700299
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700300static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
301{
302 /* Turn off the LCD controller and the DMA controller */
303 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
304 sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
305
306 /* Wait for the LCDC core to become idle */
307 while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
308 msleep(10);
309
310 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
311}
312
313static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
314{
315 atmel_lcdfb_stop_nowait(sinfo);
316
317 /* Wait for DMA engine to become idle... */
318 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
319 msleep(10);
320}
321
322static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
323{
324 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
325 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
326 (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
327 | ATMEL_LCDC_PWR);
328}
329
Nicolas Ferre14340582007-05-10 22:23:26 -0700330static void atmel_lcdfb_update_dma(struct fb_info *info,
331 struct fb_var_screeninfo *var)
332{
333 struct atmel_lcdfb_info *sinfo = info->par;
334 struct fb_fix_screeninfo *fix = &info->fix;
335 unsigned long dma_addr;
336
337 dma_addr = (fix->smem_start + var->yoffset * fix->line_length
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200338 + var->xoffset * info->var.bits_per_pixel / 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700339
340 dma_addr &= ~3UL;
341
342 /* Set framebuffer DMA base address and pixel offset */
343 lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
344
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200345 atmel_lcdfb_update_dma2d(sinfo, var, info);
Nicolas Ferre14340582007-05-10 22:23:26 -0700346}
347
348static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
349{
350 struct fb_info *info = sinfo->info;
351
352 dma_free_writecombine(info->device, info->fix.smem_len,
353 info->screen_base, info->fix.smem_start);
354}
355
356/**
357 * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
358 * @sinfo: the frame buffer to allocate memory for
Krzysztof Helt1d01e832009-07-08 22:26:16 +0200359 *
360 * This function is called only from the atmel_lcdfb_probe()
361 * so no locking by fb_info->mm_lock around smem_len setting is needed.
Nicolas Ferre14340582007-05-10 22:23:26 -0700362 */
363static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
364{
365 struct fb_info *info = sinfo->info;
366 struct fb_var_screeninfo *var = &info->var;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700367 unsigned int smem_len;
Nicolas Ferre14340582007-05-10 22:23:26 -0700368
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700369 smem_len = (var->xres_virtual * var->yres_virtual
370 * ((var->bits_per_pixel + 7) / 8));
371 info->fix.smem_len = max(smem_len, sinfo->smem_len);
Nicolas Ferre14340582007-05-10 22:23:26 -0700372
373 info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
374 (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
375
376 if (!info->screen_base) {
377 return -ENOMEM;
378 }
379
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -0700380 memset(info->screen_base, 0, info->fix.smem_len);
381
Nicolas Ferre14340582007-05-10 22:23:26 -0700382 return 0;
383}
384
Nicolas Ferre968910b2008-07-23 21:31:34 -0700385static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
386 struct fb_info *info)
387{
388 struct fb_videomode varfbmode;
389 const struct fb_videomode *fbmode = NULL;
390
391 fb_var_to_videomode(&varfbmode, var);
392 fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
393 if (fbmode)
394 fb_videomode_to_var(var, fbmode);
395 return fbmode;
396}
397
398
Nicolas Ferre14340582007-05-10 22:23:26 -0700399/**
400 * atmel_lcdfb_check_var - Validates a var passed in.
401 * @var: frame buffer variable screen structure
402 * @info: frame buffer structure that represents a single frame buffer
403 *
404 * Checks to see if the hardware supports the state requested by
405 * var passed in. This function does not alter the hardware
406 * state!!! This means the data stored in struct fb_info and
407 * struct atmel_lcdfb_info do not change. This includes the var
408 * inside of struct fb_info. Do NOT change these. This function
409 * can be called on its own if we intent to only test a mode and
410 * not actually set it. The stuff in modedb.c is a example of
411 * this. If the var passed in is slightly off by what the
412 * hardware can support then we alter the var PASSED in to what
413 * we can do. If the hardware doesn't support mode change a
414 * -EINVAL will be returned by the upper layers. You don't need
415 * to implement this function then. If you hardware doesn't
416 * support changing the resolution then this function is not
417 * needed. In this case the driver would just provide a var that
418 * represents the static state the screen is in.
419 *
420 * Returns negative errno on error, or zero on success.
421 */
422static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
423 struct fb_info *info)
424{
425 struct device *dev = info->device;
426 struct atmel_lcdfb_info *sinfo = info->par;
427 unsigned long clk_value_khz;
428
429 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
430
431 dev_dbg(dev, "%s:\n", __func__);
Nicolas Ferre968910b2008-07-23 21:31:34 -0700432
433 if (!(var->pixclock && var->bits_per_pixel)) {
434 /* choose a suitable mode if possible */
435 if (!atmel_lcdfb_choose_mode(var, info)) {
436 dev_err(dev, "needed value not specified\n");
437 return -EINVAL;
438 }
439 }
440
Nicolas Ferre14340582007-05-10 22:23:26 -0700441 dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
442 dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
443 dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
444 dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
445
Ben Nizette97b9a5a2009-06-16 15:34:24 -0700446 if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700447 dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
448 return -EINVAL;
449 }
450
Nicolas Ferre968910b2008-07-23 21:31:34 -0700451 /* Do not allow to have real resoulution larger than virtual */
452 if (var->xres > var->xres_virtual)
453 var->xres_virtual = var->xres;
454
455 if (var->yres > var->yres_virtual)
456 var->yres_virtual = var->yres;
457
Nicolas Ferre14340582007-05-10 22:23:26 -0700458 /* Force same alignment for each line */
459 var->xres = (var->xres + 3) & ~3UL;
460 var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
461
462 var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
463 var->transp.msb_right = 0;
464 var->transp.offset = var->transp.length = 0;
465 var->xoffset = var->yoffset = 0;
466
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700467 if (info->fix.smem_len) {
468 unsigned int smem_len = (var->xres_virtual * var->yres_virtual
469 * ((var->bits_per_pixel + 7) / 8));
Richard Genoud65ac0572013-05-31 14:28:38 +0000470 if (smem_len > info->fix.smem_len) {
471 dev_err(dev, "Frame buffer is too small (%u) for screen size (need at least %u)\n",
472 info->fix.smem_len, smem_len);
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700473 return -EINVAL;
Richard Genoud65ac0572013-05-31 14:28:38 +0000474 }
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700475 }
476
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800477 /* Saturate vertical and horizontal timings at maximum values */
478 var->vsync_len = min_t(u32, var->vsync_len,
479 (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
480 var->upper_margin = min_t(u32, var->upper_margin,
481 ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
482 var->lower_margin = min_t(u32, var->lower_margin,
483 ATMEL_LCDC_VFP);
484 var->right_margin = min_t(u32, var->right_margin,
Florian Tobias Schandinat6b3cbe42012-01-11 22:26:59 +0000485 (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800486 var->hsync_len = min_t(u32, var->hsync_len,
487 (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
488 var->left_margin = min_t(u32, var->left_margin,
489 ATMEL_LCDC_HBP + 1);
490
491 /* Some parameters can't be zero */
492 var->vsync_len = max_t(u32, var->vsync_len, 1);
493 var->right_margin = max_t(u32, var->right_margin, 1);
494 var->hsync_len = max_t(u32, var->hsync_len, 1);
495 var->left_margin = max_t(u32, var->left_margin, 1);
496
Nicolas Ferre14340582007-05-10 22:23:26 -0700497 switch (var->bits_per_pixel) {
Nicolas Ferre250a2692007-07-21 04:37:59 -0700498 case 1:
Nicolas Ferre14340582007-05-10 22:23:26 -0700499 case 2:
500 case 4:
501 case 8:
502 var->red.offset = var->green.offset = var->blue.offset = 0;
503 var->red.length = var->green.length = var->blue.length
504 = var->bits_per_pixel;
505 break;
Nicolas Ferre14340582007-05-10 22:23:26 -0700506 case 16:
Johan Hovolda79eac72013-02-05 14:35:11 +0100507 /* Older SOCs use IBGR:555 rather than BGR:565. */
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100508 if (sinfo->config->have_intensity_bit)
Johan Hovolda79eac72013-02-05 14:35:11 +0100509 var->green.length = 5;
510 else
511 var->green.length = 6;
512
Nicolas Ferrefd085802008-04-28 02:15:21 -0700513 if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
Johan Hovolda79eac72013-02-05 14:35:11 +0100514 /* RGB:5X5 mode */
515 var->red.offset = var->green.length + 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700516 var->blue.offset = 0;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700517 } else {
Johan Hovolda79eac72013-02-05 14:35:11 +0100518 /* BGR:5X5 mode */
Nicolas Ferrefd085802008-04-28 02:15:21 -0700519 var->red.offset = 0;
Johan Hovolda79eac72013-02-05 14:35:11 +0100520 var->blue.offset = var->green.length + 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700521 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700522 var->green.offset = 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700523 var->red.length = var->blue.length = 5;
Nicolas Ferre14340582007-05-10 22:23:26 -0700524 break;
Nicolas Ferre14340582007-05-10 22:23:26 -0700525 case 32:
Haavard Skinnemoen4440e0e2007-07-21 04:38:02 -0700526 var->transp.offset = 24;
527 var->transp.length = 8;
528 /* fall through */
529 case 24:
Nicolas Ferrefd085802008-04-28 02:15:21 -0700530 if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
531 /* RGB:888 mode */
532 var->red.offset = 16;
533 var->blue.offset = 0;
534 } else {
535 /* BGR:888 mode */
536 var->red.offset = 0;
537 var->blue.offset = 16;
538 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700539 var->green.offset = 8;
Nicolas Ferre14340582007-05-10 22:23:26 -0700540 var->red.length = var->green.length = var->blue.length = 8;
541 break;
542 default:
543 dev_err(dev, "color depth %d not supported\n",
544 var->bits_per_pixel);
545 return -EINVAL;
546 }
547
548 return 0;
549}
550
Nicolas Ferred22579b2008-07-23 21:31:20 -0700551/*
552 * LCD reset sequence
553 */
554static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
555{
556 might_sleep();
557
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700558 atmel_lcdfb_stop(sinfo);
559 atmel_lcdfb_start(sinfo);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700560}
561
Nicolas Ferre14340582007-05-10 22:23:26 -0700562/**
563 * atmel_lcdfb_set_par - Alters the hardware state.
564 * @info: frame buffer structure that represents a single frame buffer
565 *
566 * Using the fb_var_screeninfo in fb_info we set the resolution
567 * of the this particular framebuffer. This function alters the
568 * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
569 * not alter var in fb_info since we are using that data. This
570 * means we depend on the data in var inside fb_info to be
571 * supported by the hardware. atmel_lcdfb_check_var is always called
572 * before atmel_lcdfb_set_par to ensure this. Again if you can't
573 * change the resolution you don't need this function.
574 *
575 */
576static int atmel_lcdfb_set_par(struct fb_info *info)
577{
578 struct atmel_lcdfb_info *sinfo = info->par;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700579 unsigned long hozval_linesz;
Nicolas Ferre14340582007-05-10 22:23:26 -0700580 unsigned long value;
581 unsigned long clk_value_khz;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700582 unsigned long bits_per_line;
Nicolas Ferre431861c2009-11-11 14:26:35 -0800583 unsigned long pix_factor = 2;
Nicolas Ferre14340582007-05-10 22:23:26 -0700584
Nicolas Ferred22579b2008-07-23 21:31:20 -0700585 might_sleep();
586
Nicolas Ferre14340582007-05-10 22:23:26 -0700587 dev_dbg(info->device, "%s:\n", __func__);
588 dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
589 info->var.xres, info->var.yres,
590 info->var.xres_virtual, info->var.yres_virtual);
591
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700592 atmel_lcdfb_stop_nowait(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700593
Nicolas Ferre250a2692007-07-21 04:37:59 -0700594 if (info->var.bits_per_pixel == 1)
595 info->fix.visual = FB_VISUAL_MONO01;
596 else if (info->var.bits_per_pixel <= 8)
Nicolas Ferre14340582007-05-10 22:23:26 -0700597 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
598 else
599 info->fix.visual = FB_VISUAL_TRUECOLOR;
600
Nicolas Ferre250a2692007-07-21 04:37:59 -0700601 bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
602 info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700603
604 /* Re-initialize the DMA engine... */
605 dev_dbg(info->device, " * update DMA engine\n");
606 atmel_lcdfb_update_dma(info, &info->var);
607
608 /* ...set frame size and burst length = 8 words (?) */
609 value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
610 value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
611 lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
612
613 /* Now, the LCDC core... */
614
615 /* Set pixel clock */
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100616 if (sinfo->config->have_alt_pixclock)
Nicolas Ferre431861c2009-11-11 14:26:35 -0800617 pix_factor = 1;
618
Nicolas Ferre14340582007-05-10 22:23:26 -0700619 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
620
Nicolas Ferre250a2692007-07-21 04:37:59 -0700621 value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
Nicolas Ferre14340582007-05-10 22:23:26 -0700622
Nicolas Ferre431861c2009-11-11 14:26:35 -0800623 if (value < pix_factor) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700624 dev_notice(info->device, "Bypassing pixel clock divider\n");
625 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700626 } else {
Nicolas Ferre431861c2009-11-11 14:26:35 -0800627 value = (value / pix_factor) - 1;
Nicolas Ferrebaf63322008-05-12 14:02:25 -0700628 dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n",
629 value);
630 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
631 value << ATMEL_LCDC_CLKVAL_OFFSET);
Nicolas Ferre431861c2009-11-11 14:26:35 -0800632 info->var.pixclock =
633 KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
Nicolas Ferre250a2692007-07-21 04:37:59 -0700634 dev_dbg(info->device, " updated pixclk: %lu KHz\n",
635 PICOS2KHZ(info->var.pixclock));
636 }
637
Nicolas Ferre14340582007-05-10 22:23:26 -0700638
639 /* Initialize control register 2 */
640 value = sinfo->default_lcdcon2;
641
642 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
643 value |= ATMEL_LCDC_INVLINE_INVERTED;
644 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
645 value |= ATMEL_LCDC_INVFRAME_INVERTED;
646
647 switch (info->var.bits_per_pixel) {
648 case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
649 case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
650 case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
651 case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
652 case 15: /* fall through */
653 case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
654 case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
655 case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
656 default: BUG(); break;
657 }
658 dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
659 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
660
661 /* Vertical timing */
662 value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
663 value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
664 value |= info->var.lower_margin;
665 dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
666 lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
667
668 /* Horizontal timing */
Florian Tobias Schandinat6b3cbe42012-01-11 22:26:59 +0000669 value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700670 value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
671 value |= (info->var.left_margin - 1);
672 dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
673 lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
674
Nicolas Ferre250a2692007-07-21 04:37:59 -0700675 /* Horizontal value (aka line size) */
Johan Hovold934a50b2013-02-07 16:31:57 +0100676 hozval_linesz = compute_hozval(sinfo, info->var.xres);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700677
Nicolas Ferre14340582007-05-10 22:23:26 -0700678 /* Display size */
Nicolas Ferre250a2692007-07-21 04:37:59 -0700679 value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700680 value |= info->var.yres - 1;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700681 dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value);
Nicolas Ferre14340582007-05-10 22:23:26 -0700682 lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
683
684 /* FIFO Threshold: Use formula from data sheet */
685 value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
686 lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
687
688 /* Toggle LCD_MODE every frame */
689 lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
690
691 /* Disable all interrupts */
692 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700693 /* Enable FIFO & DMA errors */
694 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
Nicolas Ferre14340582007-05-10 22:23:26 -0700695
Nicolas Ferre14340582007-05-10 22:23:26 -0700696 /* ...wait for DMA engine to become idle... */
697 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
698 msleep(10);
699
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700700 atmel_lcdfb_start(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700701
702 dev_dbg(info->device, " * DONE\n");
703
704 return 0;
705}
706
707static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
708{
709 chan &= 0xffff;
710 chan >>= 16 - bf->length;
711 return chan << bf->offset;
712}
713
714/**
715 * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
716 * @regno: Which register in the CLUT we are programming
717 * @red: The red value which can be up to 16 bits wide
718 * @green: The green value which can be up to 16 bits wide
719 * @blue: The blue value which can be up to 16 bits wide.
720 * @transp: If supported the alpha value which can be up to 16 bits wide.
721 * @info: frame buffer info structure
722 *
723 * Set a single color register. The values supplied have a 16 bit
724 * magnitude which needs to be scaled in this function for the hardware.
725 * Things to take into consideration are how many color registers, if
726 * any, are supported with the current color visual. With truecolor mode
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300727 * no color palettes are supported. Here a pseudo palette is created
Nicolas Ferre14340582007-05-10 22:23:26 -0700728 * which we store the value in pseudo_palette in struct fb_info. For
729 * pseudocolor mode we have a limited color palette. To deal with this
730 * we can program what color is displayed for a particular pixel value.
731 * DirectColor is similar in that we can program each color field. If
732 * we have a static colormap we don't need to implement this function.
733 *
734 * Returns negative errno on error, or zero on success. In an
735 * ideal world, this would have been the case, but as it turns
736 * out, the other drivers return 1 on failure, so that's what
737 * we're going to do.
738 */
739static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
740 unsigned int green, unsigned int blue,
741 unsigned int transp, struct fb_info *info)
742{
743 struct atmel_lcdfb_info *sinfo = info->par;
744 unsigned int val;
745 u32 *pal;
746 int ret = 1;
747
748 if (info->var.grayscale)
749 red = green = blue = (19595 * red + 38470 * green
750 + 7471 * blue) >> 16;
751
752 switch (info->fix.visual) {
753 case FB_VISUAL_TRUECOLOR:
754 if (regno < 16) {
755 pal = info->pseudo_palette;
756
757 val = chan_to_field(red, &info->var.red);
758 val |= chan_to_field(green, &info->var.green);
759 val |= chan_to_field(blue, &info->var.blue);
760
761 pal[regno] = val;
762 ret = 0;
763 }
764 break;
765
766 case FB_VISUAL_PSEUDOCOLOR:
767 if (regno < 256) {
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100768 if (sinfo->config->have_intensity_bit) {
Peter Korsgaard5d67b892011-10-13 16:45:52 +0200769 /* old style I+BGR:555 */
770 val = ((red >> 11) & 0x001f);
771 val |= ((green >> 6) & 0x03e0);
772 val |= ((blue >> 1) & 0x7c00);
Nicolas Ferre14340582007-05-10 22:23:26 -0700773
Peter Korsgaard5d67b892011-10-13 16:45:52 +0200774 /*
775 * TODO: intensity bit. Maybe something like
776 * ~(red[10] ^ green[10] ^ blue[10]) & 1
777 */
778 } else {
779 /* new style BGR:565 / RGB:565 */
780 if (sinfo->lcd_wiring_mode ==
781 ATMEL_LCDC_WIRING_RGB) {
782 val = ((blue >> 11) & 0x001f);
783 val |= ((red >> 0) & 0xf800);
784 } else {
785 val = ((red >> 11) & 0x001f);
786 val |= ((blue >> 0) & 0xf800);
787 }
788
789 val |= ((green >> 5) & 0x07e0);
790 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700791
792 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
793 ret = 0;
794 }
795 break;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700796
797 case FB_VISUAL_MONO01:
798 if (regno < 2) {
799 val = (regno == 0) ? 0x00 : 0x1F;
800 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
801 ret = 0;
802 }
803 break;
804
Nicolas Ferre14340582007-05-10 22:23:26 -0700805 }
806
807 return ret;
808}
809
810static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
811 struct fb_info *info)
812{
813 dev_dbg(info->device, "%s\n", __func__);
814
815 atmel_lcdfb_update_dma(info, var);
816
817 return 0;
818}
819
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000820static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info)
821{
822 struct atmel_lcdfb_info *sinfo = info->par;
823
824 switch (blank_mode) {
825 case FB_BLANK_UNBLANK:
826 case FB_BLANK_NORMAL:
827 atmel_lcdfb_start(sinfo);
828 break;
829 case FB_BLANK_VSYNC_SUSPEND:
830 case FB_BLANK_HSYNC_SUSPEND:
831 break;
832 case FB_BLANK_POWERDOWN:
833 atmel_lcdfb_stop(sinfo);
834 break;
835 default:
836 return -EINVAL;
837 }
838
839 /* let fbcon do a soft blank for us */
840 return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
841}
842
Nicolas Ferre14340582007-05-10 22:23:26 -0700843static struct fb_ops atmel_lcdfb_ops = {
844 .owner = THIS_MODULE,
845 .fb_check_var = atmel_lcdfb_check_var,
846 .fb_set_par = atmel_lcdfb_set_par,
847 .fb_setcolreg = atmel_lcdfb_setcolreg,
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000848 .fb_blank = atmel_lcdfb_blank,
Nicolas Ferre14340582007-05-10 22:23:26 -0700849 .fb_pan_display = atmel_lcdfb_pan_display,
850 .fb_fillrect = cfb_fillrect,
851 .fb_copyarea = cfb_copyarea,
852 .fb_imageblit = cfb_imageblit,
853};
854
855static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
856{
857 struct fb_info *info = dev_id;
858 struct atmel_lcdfb_info *sinfo = info->par;
859 u32 status;
860
861 status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700862 if (status & ATMEL_LCDC_UFLWI) {
863 dev_warn(info->device, "FIFO underflow %#x\n", status);
864 /* reset DMA and FIFO to avoid screen shifting */
865 schedule_work(&sinfo->task);
866 }
867 lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
Nicolas Ferre14340582007-05-10 22:23:26 -0700868 return IRQ_HANDLED;
869}
870
Nicolas Ferred22579b2008-07-23 21:31:20 -0700871/*
872 * LCD controller task (to reset the LCD)
873 */
874static void atmel_lcdfb_task(struct work_struct *work)
875{
876 struct atmel_lcdfb_info *sinfo =
877 container_of(work, struct atmel_lcdfb_info, task);
878
879 atmel_lcdfb_reset(sinfo);
880}
881
Nicolas Ferre14340582007-05-10 22:23:26 -0700882static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
883{
884 struct fb_info *info = sinfo->info;
885 int ret = 0;
886
Nicolas Ferre14340582007-05-10 22:23:26 -0700887 info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
888
889 dev_info(info->device,
890 "%luKiB frame buffer at %08lx (mapped at %p)\n",
891 (unsigned long)info->fix.smem_len / 1024,
892 (unsigned long)info->fix.smem_start,
893 info->screen_base);
894
895 /* Allocate colormap */
896 ret = fb_alloc_cmap(&info->cmap, 256, 0);
897 if (ret < 0)
898 dev_err(info->device, "Alloc color map failed\n");
899
900 return ret;
901}
902
903static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
904{
Boris BREZILLON6e665fb2013-07-18 09:40:25 +0200905 clk_prepare_enable(sinfo->bus_clk);
906 clk_prepare_enable(sinfo->lcdc_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -0700907}
908
909static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
910{
Boris BREZILLON6e665fb2013-07-18 09:40:25 +0200911 clk_disable_unprepare(sinfo->bus_clk);
912 clk_disable_unprepare(sinfo->lcdc_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -0700913}
914
915
916static int __init atmel_lcdfb_probe(struct platform_device *pdev)
917{
918 struct device *dev = &pdev->dev;
919 struct fb_info *info;
920 struct atmel_lcdfb_info *sinfo;
921 struct atmel_lcdfb_info *pdata_sinfo;
Nicolas Ferre968910b2008-07-23 21:31:34 -0700922 struct fb_videomode fbmode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700923 struct resource *regs = NULL;
924 struct resource *map = NULL;
925 int ret;
926
927 dev_dbg(dev, "%s BEGIN\n", __func__);
928
929 ret = -ENOMEM;
930 info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
931 if (!info) {
932 dev_err(dev, "cannot allocate memory\n");
933 goto out;
934 }
935
936 sinfo = info->par;
937
Jingoo Hana5d58be2013-09-17 14:06:39 +0900938 if (dev_get_platdata(dev)) {
939 pdata_sinfo = dev_get_platdata(dev);
Nicolas Ferre14340582007-05-10 22:23:26 -0700940 sinfo->default_bpp = pdata_sinfo->default_bpp;
941 sinfo->default_dmacon = pdata_sinfo->default_dmacon;
942 sinfo->default_lcdcon2 = pdata_sinfo->default_lcdcon2;
943 sinfo->default_monspecs = pdata_sinfo->default_monspecs;
944 sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
945 sinfo->guard_time = pdata_sinfo->guard_time;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700946 sinfo->smem_len = pdata_sinfo->smem_len;
David Brownella9a84c32008-02-06 01:39:26 -0800947 sinfo->lcdcon_is_backlight = pdata_sinfo->lcdcon_is_backlight;
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000948 sinfo->lcdcon_pol_negative = pdata_sinfo->lcdcon_pol_negative;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700949 sinfo->lcd_wiring_mode = pdata_sinfo->lcd_wiring_mode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700950 } else {
951 dev_err(dev, "cannot get default configuration\n");
952 goto free_info;
953 }
954 sinfo->info = info;
955 sinfo->pdev = pdev;
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100956 sinfo->config = atmel_lcdfb_get_config(pdev);
957 if (!sinfo->config)
958 goto free_info;
Nicolas Ferre14340582007-05-10 22:23:26 -0700959
960 strcpy(info->fix.id, sinfo->pdev->name);
961 info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
962 info->pseudo_palette = sinfo->pseudo_palette;
963 info->fbops = &atmel_lcdfb_ops;
964
965 memcpy(&info->monspecs, sinfo->default_monspecs, sizeof(info->monspecs));
966 info->fix = atmel_lcdfb_fix;
967
968 /* Enable LCDC Clocks */
Johan Hovold557b7d52013-02-07 16:31:56 +0100969 sinfo->bus_clk = clk_get(dev, "hclk");
970 if (IS_ERR(sinfo->bus_clk)) {
971 ret = PTR_ERR(sinfo->bus_clk);
972 goto free_info;
Nicolas Ferre14340582007-05-10 22:23:26 -0700973 }
974 sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
975 if (IS_ERR(sinfo->lcdc_clk)) {
976 ret = PTR_ERR(sinfo->lcdc_clk);
977 goto put_bus_clk;
978 }
979 atmel_lcdfb_start_clock(sinfo);
980
981 ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
982 info->monspecs.modedb_len, info->monspecs.modedb,
983 sinfo->default_bpp);
984 if (!ret) {
985 dev_err(dev, "no suitable video mode found\n");
986 goto stop_clk;
987 }
988
989
990 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
991 if (!regs) {
992 dev_err(dev, "resources unusable\n");
993 ret = -ENXIO;
994 goto stop_clk;
995 }
996
997 sinfo->irq_base = platform_get_irq(pdev, 0);
998 if (sinfo->irq_base < 0) {
999 dev_err(dev, "unable to get irq\n");
1000 ret = sinfo->irq_base;
1001 goto stop_clk;
1002 }
1003
1004 /* Initialize video memory */
1005 map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1006 if (map) {
1007 /* use a pre-allocated memory buffer */
1008 info->fix.smem_start = map->start;
Joe Perches28f65c112011-06-09 09:13:32 -07001009 info->fix.smem_len = resource_size(map);
Nicolas Ferre14340582007-05-10 22:23:26 -07001010 if (!request_mem_region(info->fix.smem_start,
1011 info->fix.smem_len, pdev->name)) {
1012 ret = -EBUSY;
1013 goto stop_clk;
1014 }
1015
1016 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
Peter Senna Tschudin130320b2012-09-18 14:07:55 +02001017 if (!info->screen_base) {
1018 ret = -ENOMEM;
Nicolas Ferre14340582007-05-10 22:23:26 -07001019 goto release_intmem;
Peter Senna Tschudin130320b2012-09-18 14:07:55 +02001020 }
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -07001021
1022 /*
1023 * Don't clear the framebuffer -- someone may have set
1024 * up a splash image.
1025 */
Nicolas Ferre14340582007-05-10 22:23:26 -07001026 } else {
Masanari Iidaff0c2642012-07-22 00:23:15 +09001027 /* allocate memory buffer */
Nicolas Ferre14340582007-05-10 22:23:26 -07001028 ret = atmel_lcdfb_alloc_video_memory(sinfo);
1029 if (ret < 0) {
1030 dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
1031 goto stop_clk;
1032 }
1033 }
1034
1035 /* LCDC registers */
1036 info->fix.mmio_start = regs->start;
Joe Perches28f65c112011-06-09 09:13:32 -07001037 info->fix.mmio_len = resource_size(regs);
Nicolas Ferre14340582007-05-10 22:23:26 -07001038
1039 if (!request_mem_region(info->fix.mmio_start,
1040 info->fix.mmio_len, pdev->name)) {
1041 ret = -EBUSY;
1042 goto free_fb;
1043 }
1044
1045 sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
1046 if (!sinfo->mmio) {
1047 dev_err(dev, "cannot map LCDC registers\n");
Peter Senna Tschudin130320b2012-09-18 14:07:55 +02001048 ret = -ENOMEM;
Nicolas Ferre14340582007-05-10 22:23:26 -07001049 goto release_mem;
1050 }
1051
David Brownella9a84c32008-02-06 01:39:26 -08001052 /* Initialize PWM for contrast or backlight ("off") */
1053 init_contrast(sinfo);
1054
Nicolas Ferre14340582007-05-10 22:23:26 -07001055 /* interrupt */
1056 ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
1057 if (ret) {
1058 dev_err(dev, "request_irq failed: %d\n", ret);
1059 goto unmap_mmio;
1060 }
1061
Nicolas Ferred22579b2008-07-23 21:31:20 -07001062 /* Some operations on the LCDC might sleep and
1063 * require a preemptible task context */
1064 INIT_WORK(&sinfo->task, atmel_lcdfb_task);
1065
Nicolas Ferre14340582007-05-10 22:23:26 -07001066 ret = atmel_lcdfb_init_fbinfo(sinfo);
1067 if (ret < 0) {
1068 dev_err(dev, "init fbinfo failed: %d\n", ret);
1069 goto unregister_irqs;
1070 }
1071
1072 /*
1073 * This makes sure that our colour bitfield
1074 * descriptors are correctly initialised.
1075 */
1076 atmel_lcdfb_check_var(&info->var, info);
1077
1078 ret = fb_set_var(info, &info->var);
1079 if (ret) {
1080 dev_warn(dev, "unable to set display parameters\n");
1081 goto free_cmap;
1082 }
1083
1084 dev_set_drvdata(dev, info);
1085
1086 /*
1087 * Tell the world that we're ready to go
1088 */
1089 ret = register_framebuffer(info);
1090 if (ret < 0) {
1091 dev_err(dev, "failed to register framebuffer device: %d\n", ret);
Sachin Kamata36bf192013-09-20 12:02:10 +05301092 goto free_cmap;
Nicolas Ferre14340582007-05-10 22:23:26 -07001093 }
1094
Nicolas Ferre968910b2008-07-23 21:31:34 -07001095 /* add selected videomode to modelist */
1096 fb_var_to_videomode(&fbmode, &info->var);
1097 fb_add_videomode(&fbmode, &info->modelist);
1098
Nicolas Ferre14340582007-05-10 22:23:26 -07001099 /* Power up the LCDC screen */
1100 if (sinfo->atmel_lcdfb_power_control)
1101 sinfo->atmel_lcdfb_power_control(1);
1102
Claudio Scordino93f6ced2009-10-09 12:20:21 +02001103 dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
Nicolas Ferre14340582007-05-10 22:23:26 -07001104 info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
1105
1106 return 0;
1107
Nicolas Ferre14340582007-05-10 22:23:26 -07001108free_cmap:
1109 fb_dealloc_cmap(&info->cmap);
1110unregister_irqs:
Nicolas Ferred22579b2008-07-23 21:31:20 -07001111 cancel_work_sync(&sinfo->task);
Nicolas Ferre14340582007-05-10 22:23:26 -07001112 free_irq(sinfo->irq_base, info);
1113unmap_mmio:
David Brownella9a84c32008-02-06 01:39:26 -08001114 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001115 iounmap(sinfo->mmio);
1116release_mem:
1117 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1118free_fb:
1119 if (map)
1120 iounmap(info->screen_base);
1121 else
1122 atmel_lcdfb_free_video_memory(sinfo);
1123
1124release_intmem:
1125 if (map)
1126 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1127stop_clk:
1128 atmel_lcdfb_stop_clock(sinfo);
1129 clk_put(sinfo->lcdc_clk);
1130put_bus_clk:
Johan Hovold557b7d52013-02-07 16:31:56 +01001131 clk_put(sinfo->bus_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -07001132free_info:
1133 framebuffer_release(info);
1134out:
1135 dev_dbg(dev, "%s FAILED\n", __func__);
1136 return ret;
1137}
1138
1139static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
1140{
1141 struct device *dev = &pdev->dev;
1142 struct fb_info *info = dev_get_drvdata(dev);
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001143 struct atmel_lcdfb_info *sinfo;
Nicolas Ferre14340582007-05-10 22:23:26 -07001144
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001145 if (!info || !info->par)
Nicolas Ferre14340582007-05-10 22:23:26 -07001146 return 0;
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001147 sinfo = info->par;
Nicolas Ferre14340582007-05-10 22:23:26 -07001148
Nicolas Ferred22579b2008-07-23 21:31:20 -07001149 cancel_work_sync(&sinfo->task);
David Brownella9a84c32008-02-06 01:39:26 -08001150 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001151 if (sinfo->atmel_lcdfb_power_control)
1152 sinfo->atmel_lcdfb_power_control(0);
1153 unregister_framebuffer(info);
1154 atmel_lcdfb_stop_clock(sinfo);
1155 clk_put(sinfo->lcdc_clk);
Johan Hovold557b7d52013-02-07 16:31:56 +01001156 clk_put(sinfo->bus_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -07001157 fb_dealloc_cmap(&info->cmap);
1158 free_irq(sinfo->irq_base, info);
1159 iounmap(sinfo->mmio);
1160 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1161 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
1162 iounmap(info->screen_base);
1163 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1164 } else {
1165 atmel_lcdfb_free_video_memory(sinfo);
1166 }
1167
Nicolas Ferre14340582007-05-10 22:23:26 -07001168 framebuffer_release(info);
1169
1170 return 0;
1171}
1172
David Brownellcf19a372008-04-28 02:15:20 -07001173#ifdef CONFIG_PM
1174
1175static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1176{
1177 struct fb_info *info = platform_get_drvdata(pdev);
1178 struct atmel_lcdfb_info *sinfo = info->par;
1179
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001180 /*
1181 * We don't want to handle interrupts while the clock is
1182 * stopped. It may take forever.
1183 */
1184 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
1185
Hubert Feurstein9f106502012-01-09 17:23:57 +01001186 sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_CTR);
David Brownellcf19a372008-04-28 02:15:20 -07001187 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
1188 if (sinfo->atmel_lcdfb_power_control)
1189 sinfo->atmel_lcdfb_power_control(0);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001190
1191 atmel_lcdfb_stop(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001192 atmel_lcdfb_stop_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001193
David Brownellcf19a372008-04-28 02:15:20 -07001194 return 0;
1195}
1196
1197static int atmel_lcdfb_resume(struct platform_device *pdev)
1198{
1199 struct fb_info *info = platform_get_drvdata(pdev);
1200 struct atmel_lcdfb_info *sinfo = info->par;
1201
1202 atmel_lcdfb_start_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001203 atmel_lcdfb_start(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001204 if (sinfo->atmel_lcdfb_power_control)
1205 sinfo->atmel_lcdfb_power_control(1);
1206 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001207
1208 /* Enable FIFO & DMA errors */
1209 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
1210 | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
1211
David Brownellcf19a372008-04-28 02:15:20 -07001212 return 0;
1213}
1214
1215#else
1216#define atmel_lcdfb_suspend NULL
1217#define atmel_lcdfb_resume NULL
1218#endif
1219
Nicolas Ferre14340582007-05-10 22:23:26 -07001220static struct platform_driver atmel_lcdfb_driver = {
1221 .remove = __exit_p(atmel_lcdfb_remove),
David Brownellcf19a372008-04-28 02:15:20 -07001222 .suspend = atmel_lcdfb_suspend,
1223 .resume = atmel_lcdfb_resume,
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +01001224 .id_table = atmel_lcdfb_devtypes,
Nicolas Ferre14340582007-05-10 22:23:26 -07001225 .driver = {
1226 .name = "atmel_lcdfb",
1227 .owner = THIS_MODULE,
1228 },
1229};
1230
Fabio Porcedda3ccbf892013-03-15 14:02:38 +01001231module_platform_driver_probe(atmel_lcdfb_driver, atmel_lcdfb_probe);
Nicolas Ferre14340582007-05-10 22:23:26 -07001232
1233MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
Nicolas Ferre8f4c79c2008-01-14 00:55:13 -08001234MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
Nicolas Ferre14340582007-05-10 22:23:26 -07001235MODULE_LICENSE("GPL");