blob: 0d7214df1237690998db6734df0cacbd56bd219c [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>
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +080023#include <video/of_display_timing.h>
Nicolas Ferre14340582007-05-10 22:23:26 -070024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/cpu.h>
Russell King60e89722011-07-26 10:56:19 +010026#include <asm/gpio.h>
Nicolas Ferre14340582007-05-10 22:23:26 -070027
28#include <video/atmel_lcdc.h>
29
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +080030struct atmel_lcdfb_config {
31 bool have_alt_pixclock;
32 bool have_hozval;
33 bool have_intensity_bit;
34};
35
36 /* LCD Controller info data structure, stored in device platform_data */
37struct atmel_lcdfb_info {
38 spinlock_t lock;
39 struct fb_info *info;
40 void __iomem *mmio;
41 int irq_base;
42 struct work_struct task;
43
44 unsigned int smem_len;
45 struct platform_device *pdev;
46 struct clk *bus_clk;
47 struct clk *lcdc_clk;
48
49 struct backlight_device *backlight;
50 u8 bl_power;
51 u8 saved_lcdcon;
52
53 u32 pseudo_palette[16];
54 bool have_intensity_bit;
55
56 struct atmel_lcdfb_pdata pdata;
57
58 struct atmel_lcdfb_config *config;
59};
60
Nicolas Ferre14340582007-05-10 22:23:26 -070061#define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
62#define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
63
64/* configurable parameters */
65#define ATMEL_LCDC_CVAL_DEFAULT 0xc8
Nicolas Ferre53b74792009-05-28 14:34:36 -070066#define ATMEL_LCDC_DMA_BURST_LEN 8 /* words */
67#define ATMEL_LCDC_FIFO_SIZE 512 /* words */
Nicolas Ferre14340582007-05-10 22:23:26 -070068
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +010069static struct atmel_lcdfb_config at91sam9261_config = {
70 .have_hozval = true,
71 .have_intensity_bit = true,
72};
73
74static struct atmel_lcdfb_config at91sam9263_config = {
75 .have_intensity_bit = true,
76};
77
78static struct atmel_lcdfb_config at91sam9g10_config = {
79 .have_hozval = true,
80};
81
82static struct atmel_lcdfb_config at91sam9g45_config = {
83 .have_alt_pixclock = true,
84};
85
86static struct atmel_lcdfb_config at91sam9g45es_config = {
87};
88
89static struct atmel_lcdfb_config at91sam9rl_config = {
90 .have_intensity_bit = true,
91};
92
93static struct atmel_lcdfb_config at32ap_config = {
94 .have_hozval = true,
95};
96
97static const struct platform_device_id atmel_lcdfb_devtypes[] = {
98 {
99 .name = "at91sam9261-lcdfb",
100 .driver_data = (unsigned long)&at91sam9261_config,
101 }, {
102 .name = "at91sam9263-lcdfb",
103 .driver_data = (unsigned long)&at91sam9263_config,
104 }, {
105 .name = "at91sam9g10-lcdfb",
106 .driver_data = (unsigned long)&at91sam9g10_config,
107 }, {
108 .name = "at91sam9g45-lcdfb",
109 .driver_data = (unsigned long)&at91sam9g45_config,
110 }, {
111 .name = "at91sam9g45es-lcdfb",
112 .driver_data = (unsigned long)&at91sam9g45es_config,
113 }, {
114 .name = "at91sam9rl-lcdfb",
115 .driver_data = (unsigned long)&at91sam9rl_config,
116 }, {
117 .name = "at32ap-lcdfb",
118 .driver_data = (unsigned long)&at32ap_config,
119 }, {
120 /* terminator */
121 }
122};
123
124static struct atmel_lcdfb_config *
125atmel_lcdfb_get_config(struct platform_device *pdev)
126{
127 unsigned long data;
128
129 data = platform_get_device_id(pdev)->driver_data;
130
131 return (struct atmel_lcdfb_config *)data;
132}
133
Nicolas Ferre14340582007-05-10 22:23:26 -0700134#if defined(CONFIG_ARCH_AT91)
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -0700135#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
136 | FBINFO_PARTIAL_PAN_OK \
137 | FBINFO_HWACCEL_YPAN)
Nicolas Ferre14340582007-05-10 22:23:26 -0700138
139static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200140 struct fb_var_screeninfo *var,
141 struct fb_info *info)
Nicolas Ferre14340582007-05-10 22:23:26 -0700142{
143
144}
145#elif defined(CONFIG_AVR32)
146#define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
147 | FBINFO_PARTIAL_PAN_OK \
148 | FBINFO_HWACCEL_XPAN \
149 | FBINFO_HWACCEL_YPAN)
150
151static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200152 struct fb_var_screeninfo *var,
153 struct fb_info *info)
Nicolas Ferre14340582007-05-10 22:23:26 -0700154{
155 u32 dma2dcfg;
156 u32 pixeloff;
157
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200158 pixeloff = (var->xoffset * info->var.bits_per_pixel) & 0x1f;
Nicolas Ferre14340582007-05-10 22:23:26 -0700159
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200160 dma2dcfg = (info->var.xres_virtual - info->var.xres)
161 * info->var.bits_per_pixel / 8;
Nicolas Ferre14340582007-05-10 22:23:26 -0700162 dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
163 lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
164
165 /* Update configuration */
166 lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
167 lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
168 | ATMEL_LCDC_DMAUPDT);
169}
170#endif
171
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000172static u32 contrast_ctr = ATMEL_LCDC_PS_DIV8
David Brownella9a84c32008-02-06 01:39:26 -0800173 | ATMEL_LCDC_POL_POSITIVE
174 | ATMEL_LCDC_ENA_PWMENABLE;
175
176#ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
177
178/* some bl->props field just changed */
179static int atmel_bl_update_status(struct backlight_device *bl)
180{
181 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
182 int power = sinfo->bl_power;
183 int brightness = bl->props.brightness;
184
185 /* REVISIT there may be a meaningful difference between
186 * fb_blank and power ... there seem to be some cases
187 * this doesn't handle correctly.
188 */
189 if (bl->props.fb_blank != sinfo->bl_power)
190 power = bl->props.fb_blank;
191 else if (bl->props.power != sinfo->bl_power)
192 power = bl->props.power;
193
194 if (brightness < 0 && power == FB_BLANK_UNBLANK)
195 brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
196 else if (power != FB_BLANK_UNBLANK)
197 brightness = 0;
198
199 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness);
Alexander Steinacfdc2e2011-10-05 09:59:58 +0200200 if (contrast_ctr & ATMEL_LCDC_POL_POSITIVE)
201 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
David Brownella9a84c32008-02-06 01:39:26 -0800202 brightness ? contrast_ctr : 0);
Alexander Steinacfdc2e2011-10-05 09:59:58 +0200203 else
204 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
David Brownella9a84c32008-02-06 01:39:26 -0800205
206 bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
207
208 return 0;
209}
210
211static int atmel_bl_get_brightness(struct backlight_device *bl)
212{
213 struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
214
215 return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
216}
217
Lionel Debrouxacc24722010-11-16 14:14:02 +0100218static const struct backlight_ops atmel_lcdc_bl_ops = {
David Brownella9a84c32008-02-06 01:39:26 -0800219 .update_status = atmel_bl_update_status,
220 .get_brightness = atmel_bl_get_brightness,
221};
222
223static void init_backlight(struct atmel_lcdfb_info *sinfo)
224{
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500225 struct backlight_properties props;
David Brownella9a84c32008-02-06 01:39:26 -0800226 struct backlight_device *bl;
227
228 sinfo->bl_power = FB_BLANK_UNBLANK;
229
230 if (sinfo->backlight)
231 return;
232
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500233 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700234 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500235 props.max_brightness = 0xff;
236 bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
237 &atmel_lcdc_bl_ops, &props);
Julien Brunelcf7b9a12008-11-19 15:36:07 -0800238 if (IS_ERR(bl)) {
David Brownella9a84c32008-02-06 01:39:26 -0800239 dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
240 PTR_ERR(bl));
241 return;
242 }
243 sinfo->backlight = bl;
244
245 bl->props.power = FB_BLANK_UNBLANK;
246 bl->props.fb_blank = FB_BLANK_UNBLANK;
David Brownella9a84c32008-02-06 01:39:26 -0800247 bl->props.brightness = atmel_bl_get_brightness(bl);
248}
249
250static void exit_backlight(struct atmel_lcdfb_info *sinfo)
251{
Richard Genoud56c21b52013-05-31 15:49:35 +0000252 if (!sinfo->backlight)
253 return;
254
255 if (sinfo->backlight->ops) {
256 sinfo->backlight->props.power = FB_BLANK_POWERDOWN;
257 sinfo->backlight->ops->update_status(sinfo->backlight);
258 }
259 backlight_device_unregister(sinfo->backlight);
David Brownella9a84c32008-02-06 01:39:26 -0800260}
261
262#else
263
264static void init_backlight(struct atmel_lcdfb_info *sinfo)
265{
266 dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
267}
268
269static void exit_backlight(struct atmel_lcdfb_info *sinfo)
270{
271}
272
273#endif
274
275static void init_contrast(struct atmel_lcdfb_info *sinfo)
276{
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800277 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
278
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000279 /* contrast pwm can be 'inverted' */
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800280 if (pdata->lcdcon_pol_negative)
Andreas Bießmann7cdcdb62011-02-11 15:19:43 +0000281 contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE);
282
David Brownella9a84c32008-02-06 01:39:26 -0800283 /* have some default contrast/backlight settings */
284 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
285 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
286
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800287 if (pdata->lcdcon_is_backlight)
David Brownella9a84c32008-02-06 01:39:26 -0800288 init_backlight(sinfo);
289}
290
Nicolas Ferre14340582007-05-10 22:23:26 -0700291
292static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
293 .type = FB_TYPE_PACKED_PIXELS,
294 .visual = FB_VISUAL_TRUECOLOR,
295 .xpanstep = 0,
Haavard Skinnemoene730d8b0a2008-08-12 15:08:56 -0700296 .ypanstep = 1,
Nicolas Ferre14340582007-05-10 22:23:26 -0700297 .ywrapstep = 0,
298 .accel = FB_ACCEL_NONE,
299};
300
Johan Hovold934a50b2013-02-07 16:31:57 +0100301static unsigned long compute_hozval(struct atmel_lcdfb_info *sinfo,
302 unsigned long xres)
Nicolas Ferre250a2692007-07-21 04:37:59 -0700303{
Johan Hovold934a50b2013-02-07 16:31:57 +0100304 unsigned long lcdcon2;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700305 unsigned long value;
306
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100307 if (!sinfo->config->have_hozval)
Nicolas Ferre250a2692007-07-21 04:37:59 -0700308 return xres;
309
Johan Hovold934a50b2013-02-07 16:31:57 +0100310 lcdcon2 = lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700311 value = xres;
312 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
313 /* STN display */
314 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
315 value *= 3;
316 }
317 if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
318 || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
319 && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
320 value = DIV_ROUND_UP(value, 4);
321 else
322 value = DIV_ROUND_UP(value, 8);
323 }
324
325 return value;
326}
Nicolas Ferre14340582007-05-10 22:23:26 -0700327
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700328static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
329{
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800330 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
331
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700332 /* Turn off the LCD controller and the DMA controller */
333 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800334 pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700335
336 /* Wait for the LCDC core to become idle */
337 while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
338 msleep(10);
339
340 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
341}
342
343static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
344{
345 atmel_lcdfb_stop_nowait(sinfo);
346
347 /* Wait for DMA engine to become idle... */
348 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
349 msleep(10);
350}
351
352static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
353{
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800354 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
355
356 lcdc_writel(sinfo, ATMEL_LCDC_DMACON, pdata->default_dmacon);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700357 lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800358 (pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700359 | ATMEL_LCDC_PWR);
360}
361
Nicolas Ferre14340582007-05-10 22:23:26 -0700362static void atmel_lcdfb_update_dma(struct fb_info *info,
363 struct fb_var_screeninfo *var)
364{
365 struct atmel_lcdfb_info *sinfo = info->par;
366 struct fb_fix_screeninfo *fix = &info->fix;
367 unsigned long dma_addr;
368
369 dma_addr = (fix->smem_start + var->yoffset * fix->line_length
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200370 + var->xoffset * info->var.bits_per_pixel / 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700371
372 dma_addr &= ~3UL;
373
374 /* Set framebuffer DMA base address and pixel offset */
375 lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
376
Laurent Pinchartb3e9c122011-05-25 11:34:52 +0200377 atmel_lcdfb_update_dma2d(sinfo, var, info);
Nicolas Ferre14340582007-05-10 22:23:26 -0700378}
379
380static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
381{
382 struct fb_info *info = sinfo->info;
383
384 dma_free_writecombine(info->device, info->fix.smem_len,
385 info->screen_base, info->fix.smem_start);
386}
387
388/**
389 * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
390 * @sinfo: the frame buffer to allocate memory for
Krzysztof Helt1d01e832009-07-08 22:26:16 +0200391 *
392 * This function is called only from the atmel_lcdfb_probe()
393 * so no locking by fb_info->mm_lock around smem_len setting is needed.
Nicolas Ferre14340582007-05-10 22:23:26 -0700394 */
395static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
396{
397 struct fb_info *info = sinfo->info;
398 struct fb_var_screeninfo *var = &info->var;
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700399 unsigned int smem_len;
Nicolas Ferre14340582007-05-10 22:23:26 -0700400
Haavard Skinnemoenea757ac2008-08-12 15:08:57 -0700401 smem_len = (var->xres_virtual * var->yres_virtual
402 * ((var->bits_per_pixel + 7) / 8));
403 info->fix.smem_len = max(smem_len, sinfo->smem_len);
Nicolas Ferre14340582007-05-10 22:23:26 -0700404
405 info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
406 (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
407
408 if (!info->screen_base) {
409 return -ENOMEM;
410 }
411
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -0700412 memset(info->screen_base, 0, info->fix.smem_len);
413
Nicolas Ferre14340582007-05-10 22:23:26 -0700414 return 0;
415}
416
Nicolas Ferre968910b2008-07-23 21:31:34 -0700417static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
418 struct fb_info *info)
419{
420 struct fb_videomode varfbmode;
421 const struct fb_videomode *fbmode = NULL;
422
423 fb_var_to_videomode(&varfbmode, var);
424 fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
425 if (fbmode)
426 fb_videomode_to_var(var, fbmode);
427 return fbmode;
428}
429
430
Nicolas Ferre14340582007-05-10 22:23:26 -0700431/**
432 * atmel_lcdfb_check_var - Validates a var passed in.
433 * @var: frame buffer variable screen structure
434 * @info: frame buffer structure that represents a single frame buffer
435 *
436 * Checks to see if the hardware supports the state requested by
437 * var passed in. This function does not alter the hardware
438 * state!!! This means the data stored in struct fb_info and
439 * struct atmel_lcdfb_info do not change. This includes the var
440 * inside of struct fb_info. Do NOT change these. This function
441 * can be called on its own if we intent to only test a mode and
442 * not actually set it. The stuff in modedb.c is a example of
443 * this. If the var passed in is slightly off by what the
444 * hardware can support then we alter the var PASSED in to what
445 * we can do. If the hardware doesn't support mode change a
446 * -EINVAL will be returned by the upper layers. You don't need
447 * to implement this function then. If you hardware doesn't
448 * support changing the resolution then this function is not
449 * needed. In this case the driver would just provide a var that
450 * represents the static state the screen is in.
451 *
452 * Returns negative errno on error, or zero on success.
453 */
454static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
455 struct fb_info *info)
456{
457 struct device *dev = info->device;
458 struct atmel_lcdfb_info *sinfo = info->par;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800459 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
Nicolas Ferre14340582007-05-10 22:23:26 -0700460 unsigned long clk_value_khz;
461
462 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
463
464 dev_dbg(dev, "%s:\n", __func__);
Nicolas Ferre968910b2008-07-23 21:31:34 -0700465
466 if (!(var->pixclock && var->bits_per_pixel)) {
467 /* choose a suitable mode if possible */
468 if (!atmel_lcdfb_choose_mode(var, info)) {
469 dev_err(dev, "needed value not specified\n");
470 return -EINVAL;
471 }
472 }
473
Nicolas Ferre14340582007-05-10 22:23:26 -0700474 dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
475 dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
476 dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
477 dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
478
Ben Nizette97b9a5a2009-06-16 15:34:24 -0700479 if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700480 dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
481 return -EINVAL;
482 }
483
Nicolas Ferre968910b2008-07-23 21:31:34 -0700484 /* Do not allow to have real resoulution larger than virtual */
485 if (var->xres > var->xres_virtual)
486 var->xres_virtual = var->xres;
487
488 if (var->yres > var->yres_virtual)
489 var->yres_virtual = var->yres;
490
Nicolas Ferre14340582007-05-10 22:23:26 -0700491 /* Force same alignment for each line */
492 var->xres = (var->xres + 3) & ~3UL;
493 var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
494
495 var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
496 var->transp.msb_right = 0;
497 var->transp.offset = var->transp.length = 0;
498 var->xoffset = var->yoffset = 0;
499
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700500 if (info->fix.smem_len) {
501 unsigned int smem_len = (var->xres_virtual * var->yres_virtual
502 * ((var->bits_per_pixel + 7) / 8));
Richard Genoud65ac0572013-05-31 14:28:38 +0000503 if (smem_len > info->fix.smem_len) {
504 dev_err(dev, "Frame buffer is too small (%u) for screen size (need at least %u)\n",
505 info->fix.smem_len, smem_len);
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700506 return -EINVAL;
Richard Genoud65ac0572013-05-31 14:28:38 +0000507 }
Stanislaw Gruszkaf928ac0a2008-10-15 22:03:43 -0700508 }
509
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800510 /* Saturate vertical and horizontal timings at maximum values */
511 var->vsync_len = min_t(u32, var->vsync_len,
512 (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
513 var->upper_margin = min_t(u32, var->upper_margin,
514 ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
515 var->lower_margin = min_t(u32, var->lower_margin,
516 ATMEL_LCDC_VFP);
517 var->right_margin = min_t(u32, var->right_margin,
Florian Tobias Schandinat6b3cbe42012-01-11 22:26:59 +0000518 (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
Haavard Skinnemoen162b3a02008-02-06 01:39:11 -0800519 var->hsync_len = min_t(u32, var->hsync_len,
520 (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
521 var->left_margin = min_t(u32, var->left_margin,
522 ATMEL_LCDC_HBP + 1);
523
524 /* Some parameters can't be zero */
525 var->vsync_len = max_t(u32, var->vsync_len, 1);
526 var->right_margin = max_t(u32, var->right_margin, 1);
527 var->hsync_len = max_t(u32, var->hsync_len, 1);
528 var->left_margin = max_t(u32, var->left_margin, 1);
529
Nicolas Ferre14340582007-05-10 22:23:26 -0700530 switch (var->bits_per_pixel) {
Nicolas Ferre250a2692007-07-21 04:37:59 -0700531 case 1:
Nicolas Ferre14340582007-05-10 22:23:26 -0700532 case 2:
533 case 4:
534 case 8:
535 var->red.offset = var->green.offset = var->blue.offset = 0;
536 var->red.length = var->green.length = var->blue.length
537 = var->bits_per_pixel;
538 break;
Nicolas Ferre14340582007-05-10 22:23:26 -0700539 case 16:
Johan Hovolda79eac72013-02-05 14:35:11 +0100540 /* Older SOCs use IBGR:555 rather than BGR:565. */
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100541 if (sinfo->config->have_intensity_bit)
Johan Hovolda79eac72013-02-05 14:35:11 +0100542 var->green.length = 5;
543 else
544 var->green.length = 6;
545
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800546 if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
Johan Hovolda79eac72013-02-05 14:35:11 +0100547 /* RGB:5X5 mode */
548 var->red.offset = var->green.length + 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700549 var->blue.offset = 0;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700550 } else {
Johan Hovolda79eac72013-02-05 14:35:11 +0100551 /* BGR:5X5 mode */
Nicolas Ferrefd085802008-04-28 02:15:21 -0700552 var->red.offset = 0;
Johan Hovolda79eac72013-02-05 14:35:11 +0100553 var->blue.offset = var->green.length + 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700554 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700555 var->green.offset = 5;
Nicolas Ferrefd085802008-04-28 02:15:21 -0700556 var->red.length = var->blue.length = 5;
Nicolas Ferre14340582007-05-10 22:23:26 -0700557 break;
Nicolas Ferre14340582007-05-10 22:23:26 -0700558 case 32:
Haavard Skinnemoen4440e0e2007-07-21 04:38:02 -0700559 var->transp.offset = 24;
560 var->transp.length = 8;
561 /* fall through */
562 case 24:
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800563 if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
Nicolas Ferrefd085802008-04-28 02:15:21 -0700564 /* RGB:888 mode */
565 var->red.offset = 16;
566 var->blue.offset = 0;
567 } else {
568 /* BGR:888 mode */
569 var->red.offset = 0;
570 var->blue.offset = 16;
571 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700572 var->green.offset = 8;
Nicolas Ferre14340582007-05-10 22:23:26 -0700573 var->red.length = var->green.length = var->blue.length = 8;
574 break;
575 default:
576 dev_err(dev, "color depth %d not supported\n",
577 var->bits_per_pixel);
578 return -EINVAL;
579 }
580
581 return 0;
582}
583
Nicolas Ferred22579b2008-07-23 21:31:20 -0700584/*
585 * LCD reset sequence
586 */
587static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
588{
589 might_sleep();
590
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700591 atmel_lcdfb_stop(sinfo);
592 atmel_lcdfb_start(sinfo);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700593}
594
Nicolas Ferre14340582007-05-10 22:23:26 -0700595/**
596 * atmel_lcdfb_set_par - Alters the hardware state.
597 * @info: frame buffer structure that represents a single frame buffer
598 *
599 * Using the fb_var_screeninfo in fb_info we set the resolution
600 * of the this particular framebuffer. This function alters the
601 * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
602 * not alter var in fb_info since we are using that data. This
603 * means we depend on the data in var inside fb_info to be
604 * supported by the hardware. atmel_lcdfb_check_var is always called
605 * before atmel_lcdfb_set_par to ensure this. Again if you can't
606 * change the resolution you don't need this function.
607 *
608 */
609static int atmel_lcdfb_set_par(struct fb_info *info)
610{
611 struct atmel_lcdfb_info *sinfo = info->par;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800612 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700613 unsigned long hozval_linesz;
Nicolas Ferre14340582007-05-10 22:23:26 -0700614 unsigned long value;
615 unsigned long clk_value_khz;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700616 unsigned long bits_per_line;
Nicolas Ferre431861c2009-11-11 14:26:35 -0800617 unsigned long pix_factor = 2;
Nicolas Ferre14340582007-05-10 22:23:26 -0700618
Nicolas Ferred22579b2008-07-23 21:31:20 -0700619 might_sleep();
620
Nicolas Ferre14340582007-05-10 22:23:26 -0700621 dev_dbg(info->device, "%s:\n", __func__);
622 dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
623 info->var.xres, info->var.yres,
624 info->var.xres_virtual, info->var.yres_virtual);
625
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700626 atmel_lcdfb_stop_nowait(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700627
Nicolas Ferre250a2692007-07-21 04:37:59 -0700628 if (info->var.bits_per_pixel == 1)
629 info->fix.visual = FB_VISUAL_MONO01;
630 else if (info->var.bits_per_pixel <= 8)
Nicolas Ferre14340582007-05-10 22:23:26 -0700631 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
632 else
633 info->fix.visual = FB_VISUAL_TRUECOLOR;
634
Nicolas Ferre250a2692007-07-21 04:37:59 -0700635 bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
636 info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
Nicolas Ferre14340582007-05-10 22:23:26 -0700637
638 /* Re-initialize the DMA engine... */
639 dev_dbg(info->device, " * update DMA engine\n");
640 atmel_lcdfb_update_dma(info, &info->var);
641
642 /* ...set frame size and burst length = 8 words (?) */
643 value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
644 value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
645 lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
646
647 /* Now, the LCDC core... */
648
649 /* Set pixel clock */
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100650 if (sinfo->config->have_alt_pixclock)
Nicolas Ferre431861c2009-11-11 14:26:35 -0800651 pix_factor = 1;
652
Nicolas Ferre14340582007-05-10 22:23:26 -0700653 clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
654
Nicolas Ferre250a2692007-07-21 04:37:59 -0700655 value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
Nicolas Ferre14340582007-05-10 22:23:26 -0700656
Nicolas Ferre431861c2009-11-11 14:26:35 -0800657 if (value < pix_factor) {
Nicolas Ferre14340582007-05-10 22:23:26 -0700658 dev_notice(info->device, "Bypassing pixel clock divider\n");
659 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700660 } else {
Nicolas Ferre431861c2009-11-11 14:26:35 -0800661 value = (value / pix_factor) - 1;
Nicolas Ferrebaf63322008-05-12 14:02:25 -0700662 dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n",
663 value);
664 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
665 value << ATMEL_LCDC_CLKVAL_OFFSET);
Nicolas Ferre431861c2009-11-11 14:26:35 -0800666 info->var.pixclock =
667 KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
Nicolas Ferre250a2692007-07-21 04:37:59 -0700668 dev_dbg(info->device, " updated pixclk: %lu KHz\n",
669 PICOS2KHZ(info->var.pixclock));
670 }
671
Nicolas Ferre14340582007-05-10 22:23:26 -0700672
673 /* Initialize control register 2 */
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800674 value = pdata->default_lcdcon2;
Nicolas Ferre14340582007-05-10 22:23:26 -0700675
676 if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
677 value |= ATMEL_LCDC_INVLINE_INVERTED;
678 if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
679 value |= ATMEL_LCDC_INVFRAME_INVERTED;
680
681 switch (info->var.bits_per_pixel) {
682 case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
683 case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
684 case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
685 case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
686 case 15: /* fall through */
687 case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
688 case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
689 case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
690 default: BUG(); break;
691 }
692 dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
693 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
694
695 /* Vertical timing */
696 value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
697 value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
698 value |= info->var.lower_margin;
699 dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
700 lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
701
702 /* Horizontal timing */
Florian Tobias Schandinat6b3cbe42012-01-11 22:26:59 +0000703 value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700704 value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
705 value |= (info->var.left_margin - 1);
706 dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
707 lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
708
Nicolas Ferre250a2692007-07-21 04:37:59 -0700709 /* Horizontal value (aka line size) */
Johan Hovold934a50b2013-02-07 16:31:57 +0100710 hozval_linesz = compute_hozval(sinfo, info->var.xres);
Nicolas Ferre250a2692007-07-21 04:37:59 -0700711
Nicolas Ferre14340582007-05-10 22:23:26 -0700712 /* Display size */
Nicolas Ferre250a2692007-07-21 04:37:59 -0700713 value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
Nicolas Ferre14340582007-05-10 22:23:26 -0700714 value |= info->var.yres - 1;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700715 dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value);
Nicolas Ferre14340582007-05-10 22:23:26 -0700716 lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
717
718 /* FIFO Threshold: Use formula from data sheet */
719 value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
720 lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
721
722 /* Toggle LCD_MODE every frame */
723 lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
724
725 /* Disable all interrupts */
726 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700727 /* Enable FIFO & DMA errors */
728 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
Nicolas Ferre14340582007-05-10 22:23:26 -0700729
Nicolas Ferre14340582007-05-10 22:23:26 -0700730 /* ...wait for DMA engine to become idle... */
731 while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
732 msleep(10);
733
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -0700734 atmel_lcdfb_start(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -0700735
736 dev_dbg(info->device, " * DONE\n");
737
738 return 0;
739}
740
741static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
742{
743 chan &= 0xffff;
744 chan >>= 16 - bf->length;
745 return chan << bf->offset;
746}
747
748/**
749 * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
750 * @regno: Which register in the CLUT we are programming
751 * @red: The red value which can be up to 16 bits wide
752 * @green: The green value which can be up to 16 bits wide
753 * @blue: The blue value which can be up to 16 bits wide.
754 * @transp: If supported the alpha value which can be up to 16 bits wide.
755 * @info: frame buffer info structure
756 *
757 * Set a single color register. The values supplied have a 16 bit
758 * magnitude which needs to be scaled in this function for the hardware.
759 * Things to take into consideration are how many color registers, if
760 * any, are supported with the current color visual. With truecolor mode
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300761 * no color palettes are supported. Here a pseudo palette is created
Nicolas Ferre14340582007-05-10 22:23:26 -0700762 * which we store the value in pseudo_palette in struct fb_info. For
763 * pseudocolor mode we have a limited color palette. To deal with this
764 * we can program what color is displayed for a particular pixel value.
765 * DirectColor is similar in that we can program each color field. If
766 * we have a static colormap we don't need to implement this function.
767 *
768 * Returns negative errno on error, or zero on success. In an
769 * ideal world, this would have been the case, but as it turns
770 * out, the other drivers return 1 on failure, so that's what
771 * we're going to do.
772 */
773static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
774 unsigned int green, unsigned int blue,
775 unsigned int transp, struct fb_info *info)
776{
777 struct atmel_lcdfb_info *sinfo = info->par;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800778 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
Nicolas Ferre14340582007-05-10 22:23:26 -0700779 unsigned int val;
780 u32 *pal;
781 int ret = 1;
782
783 if (info->var.grayscale)
784 red = green = blue = (19595 * red + 38470 * green
785 + 7471 * blue) >> 16;
786
787 switch (info->fix.visual) {
788 case FB_VISUAL_TRUECOLOR:
789 if (regno < 16) {
790 pal = info->pseudo_palette;
791
792 val = chan_to_field(red, &info->var.red);
793 val |= chan_to_field(green, &info->var.green);
794 val |= chan_to_field(blue, &info->var.blue);
795
796 pal[regno] = val;
797 ret = 0;
798 }
799 break;
800
801 case FB_VISUAL_PSEUDOCOLOR:
802 if (regno < 256) {
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100803 if (sinfo->config->have_intensity_bit) {
Peter Korsgaard5d67b892011-10-13 16:45:52 +0200804 /* old style I+BGR:555 */
805 val = ((red >> 11) & 0x001f);
806 val |= ((green >> 6) & 0x03e0);
807 val |= ((blue >> 1) & 0x7c00);
Nicolas Ferre14340582007-05-10 22:23:26 -0700808
Peter Korsgaard5d67b892011-10-13 16:45:52 +0200809 /*
810 * TODO: intensity bit. Maybe something like
811 * ~(red[10] ^ green[10] ^ blue[10]) & 1
812 */
813 } else {
814 /* new style BGR:565 / RGB:565 */
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800815 if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
Peter Korsgaard5d67b892011-10-13 16:45:52 +0200816 val = ((blue >> 11) & 0x001f);
817 val |= ((red >> 0) & 0xf800);
818 } else {
819 val = ((red >> 11) & 0x001f);
820 val |= ((blue >> 0) & 0xf800);
821 }
822
823 val |= ((green >> 5) & 0x07e0);
824 }
Nicolas Ferre14340582007-05-10 22:23:26 -0700825
826 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
827 ret = 0;
828 }
829 break;
Nicolas Ferre250a2692007-07-21 04:37:59 -0700830
831 case FB_VISUAL_MONO01:
832 if (regno < 2) {
833 val = (regno == 0) ? 0x00 : 0x1F;
834 lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
835 ret = 0;
836 }
837 break;
838
Nicolas Ferre14340582007-05-10 22:23:26 -0700839 }
840
841 return ret;
842}
843
844static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
845 struct fb_info *info)
846{
847 dev_dbg(info->device, "%s\n", __func__);
848
849 atmel_lcdfb_update_dma(info, var);
850
851 return 0;
852}
853
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000854static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info)
855{
856 struct atmel_lcdfb_info *sinfo = info->par;
857
858 switch (blank_mode) {
859 case FB_BLANK_UNBLANK:
860 case FB_BLANK_NORMAL:
861 atmel_lcdfb_start(sinfo);
862 break;
863 case FB_BLANK_VSYNC_SUSPEND:
864 case FB_BLANK_HSYNC_SUSPEND:
865 break;
866 case FB_BLANK_POWERDOWN:
867 atmel_lcdfb_stop(sinfo);
868 break;
869 default:
870 return -EINVAL;
871 }
872
873 /* let fbcon do a soft blank for us */
874 return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
875}
876
Nicolas Ferre14340582007-05-10 22:23:26 -0700877static struct fb_ops atmel_lcdfb_ops = {
878 .owner = THIS_MODULE,
879 .fb_check_var = atmel_lcdfb_check_var,
880 .fb_set_par = atmel_lcdfb_set_par,
881 .fb_setcolreg = atmel_lcdfb_setcolreg,
Andreas Bießmannbed7bdd2011-02-11 15:19:44 +0000882 .fb_blank = atmel_lcdfb_blank,
Nicolas Ferre14340582007-05-10 22:23:26 -0700883 .fb_pan_display = atmel_lcdfb_pan_display,
884 .fb_fillrect = cfb_fillrect,
885 .fb_copyarea = cfb_copyarea,
886 .fb_imageblit = cfb_imageblit,
887};
888
889static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
890{
891 struct fb_info *info = dev_id;
892 struct atmel_lcdfb_info *sinfo = info->par;
893 u32 status;
894
895 status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
Nicolas Ferred22579b2008-07-23 21:31:20 -0700896 if (status & ATMEL_LCDC_UFLWI) {
897 dev_warn(info->device, "FIFO underflow %#x\n", status);
898 /* reset DMA and FIFO to avoid screen shifting */
899 schedule_work(&sinfo->task);
900 }
901 lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
Nicolas Ferre14340582007-05-10 22:23:26 -0700902 return IRQ_HANDLED;
903}
904
Nicolas Ferred22579b2008-07-23 21:31:20 -0700905/*
906 * LCD controller task (to reset the LCD)
907 */
908static void atmel_lcdfb_task(struct work_struct *work)
909{
910 struct atmel_lcdfb_info *sinfo =
911 container_of(work, struct atmel_lcdfb_info, task);
912
913 atmel_lcdfb_reset(sinfo);
914}
915
Nicolas Ferre14340582007-05-10 22:23:26 -0700916static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
917{
918 struct fb_info *info = sinfo->info;
919 int ret = 0;
920
Nicolas Ferre14340582007-05-10 22:23:26 -0700921 info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
922
923 dev_info(info->device,
924 "%luKiB frame buffer at %08lx (mapped at %p)\n",
925 (unsigned long)info->fix.smem_len / 1024,
926 (unsigned long)info->fix.smem_start,
927 info->screen_base);
928
929 /* Allocate colormap */
930 ret = fb_alloc_cmap(&info->cmap, 256, 0);
931 if (ret < 0)
932 dev_err(info->device, "Alloc color map failed\n");
933
934 return ret;
935}
936
937static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
938{
Boris BREZILLON6e665fb2013-07-18 09:40:25 +0200939 clk_prepare_enable(sinfo->bus_clk);
940 clk_prepare_enable(sinfo->lcdc_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -0700941}
942
943static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
944{
Boris BREZILLON6e665fb2013-07-18 09:40:25 +0200945 clk_disable_unprepare(sinfo->bus_clk);
946 clk_disable_unprepare(sinfo->lcdc_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -0700947}
948
949
950static int __init atmel_lcdfb_probe(struct platform_device *pdev)
951{
952 struct device *dev = &pdev->dev;
953 struct fb_info *info;
954 struct atmel_lcdfb_info *sinfo;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800955 struct atmel_lcdfb_pdata *pdata;
Nicolas Ferre968910b2008-07-23 21:31:34 -0700956 struct fb_videomode fbmode;
Nicolas Ferre14340582007-05-10 22:23:26 -0700957 struct resource *regs = NULL;
958 struct resource *map = NULL;
959 int ret;
960
961 dev_dbg(dev, "%s BEGIN\n", __func__);
962
963 ret = -ENOMEM;
964 info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
965 if (!info) {
966 dev_err(dev, "cannot allocate memory\n");
967 goto out;
968 }
969
970 sinfo = info->par;
971
Jingoo Hana5d58be2013-09-17 14:06:39 +0900972 if (dev_get_platdata(dev)) {
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800973 pdata = dev_get_platdata(dev);
974 sinfo->pdata = *pdata;
Nicolas Ferre14340582007-05-10 22:23:26 -0700975 } else {
976 dev_err(dev, "cannot get default configuration\n");
977 goto free_info;
978 }
979 sinfo->info = info;
980 sinfo->pdev = pdev;
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +0100981 sinfo->config = atmel_lcdfb_get_config(pdev);
982 if (!sinfo->config)
983 goto free_info;
Nicolas Ferre14340582007-05-10 22:23:26 -0700984
985 strcpy(info->fix.id, sinfo->pdev->name);
986 info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
987 info->pseudo_palette = sinfo->pseudo_palette;
988 info->fbops = &atmel_lcdfb_ops;
989
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +0800990 memcpy(&info->monspecs, pdata->default_monspecs, sizeof(info->monspecs));
Nicolas Ferre14340582007-05-10 22:23:26 -0700991 info->fix = atmel_lcdfb_fix;
992
993 /* Enable LCDC Clocks */
Johan Hovold557b7d52013-02-07 16:31:56 +0100994 sinfo->bus_clk = clk_get(dev, "hclk");
995 if (IS_ERR(sinfo->bus_clk)) {
996 ret = PTR_ERR(sinfo->bus_clk);
997 goto free_info;
Nicolas Ferre14340582007-05-10 22:23:26 -0700998 }
999 sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
1000 if (IS_ERR(sinfo->lcdc_clk)) {
1001 ret = PTR_ERR(sinfo->lcdc_clk);
1002 goto put_bus_clk;
1003 }
1004 atmel_lcdfb_start_clock(sinfo);
1005
1006 ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
1007 info->monspecs.modedb_len, info->monspecs.modedb,
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001008 pdata->default_bpp);
Nicolas Ferre14340582007-05-10 22:23:26 -07001009 if (!ret) {
1010 dev_err(dev, "no suitable video mode found\n");
1011 goto stop_clk;
1012 }
1013
1014
1015 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1016 if (!regs) {
1017 dev_err(dev, "resources unusable\n");
1018 ret = -ENXIO;
1019 goto stop_clk;
1020 }
1021
1022 sinfo->irq_base = platform_get_irq(pdev, 0);
1023 if (sinfo->irq_base < 0) {
1024 dev_err(dev, "unable to get irq\n");
1025 ret = sinfo->irq_base;
1026 goto stop_clk;
1027 }
1028
1029 /* Initialize video memory */
1030 map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1031 if (map) {
1032 /* use a pre-allocated memory buffer */
1033 info->fix.smem_start = map->start;
Joe Perches28f65c112011-06-09 09:13:32 -07001034 info->fix.smem_len = resource_size(map);
Nicolas Ferre14340582007-05-10 22:23:26 -07001035 if (!request_mem_region(info->fix.smem_start,
1036 info->fix.smem_len, pdev->name)) {
1037 ret = -EBUSY;
1038 goto stop_clk;
1039 }
1040
1041 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
Peter Senna Tschudin130320b2012-09-18 14:07:55 +02001042 if (!info->screen_base) {
1043 ret = -ENOMEM;
Nicolas Ferre14340582007-05-10 22:23:26 -07001044 goto release_intmem;
Peter Senna Tschudin130320b2012-09-18 14:07:55 +02001045 }
Haavard Skinnemoen01d3a5e2008-04-28 02:15:19 -07001046
1047 /*
1048 * Don't clear the framebuffer -- someone may have set
1049 * up a splash image.
1050 */
Nicolas Ferre14340582007-05-10 22:23:26 -07001051 } else {
Masanari Iidaff0c2642012-07-22 00:23:15 +09001052 /* allocate memory buffer */
Nicolas Ferre14340582007-05-10 22:23:26 -07001053 ret = atmel_lcdfb_alloc_video_memory(sinfo);
1054 if (ret < 0) {
1055 dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
1056 goto stop_clk;
1057 }
1058 }
1059
1060 /* LCDC registers */
1061 info->fix.mmio_start = regs->start;
Joe Perches28f65c112011-06-09 09:13:32 -07001062 info->fix.mmio_len = resource_size(regs);
Nicolas Ferre14340582007-05-10 22:23:26 -07001063
1064 if (!request_mem_region(info->fix.mmio_start,
1065 info->fix.mmio_len, pdev->name)) {
1066 ret = -EBUSY;
1067 goto free_fb;
1068 }
1069
1070 sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
1071 if (!sinfo->mmio) {
1072 dev_err(dev, "cannot map LCDC registers\n");
Peter Senna Tschudin130320b2012-09-18 14:07:55 +02001073 ret = -ENOMEM;
Nicolas Ferre14340582007-05-10 22:23:26 -07001074 goto release_mem;
1075 }
1076
David Brownella9a84c32008-02-06 01:39:26 -08001077 /* Initialize PWM for contrast or backlight ("off") */
1078 init_contrast(sinfo);
1079
Nicolas Ferre14340582007-05-10 22:23:26 -07001080 /* interrupt */
1081 ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
1082 if (ret) {
1083 dev_err(dev, "request_irq failed: %d\n", ret);
1084 goto unmap_mmio;
1085 }
1086
Nicolas Ferred22579b2008-07-23 21:31:20 -07001087 /* Some operations on the LCDC might sleep and
1088 * require a preemptible task context */
1089 INIT_WORK(&sinfo->task, atmel_lcdfb_task);
1090
Nicolas Ferre14340582007-05-10 22:23:26 -07001091 ret = atmel_lcdfb_init_fbinfo(sinfo);
1092 if (ret < 0) {
1093 dev_err(dev, "init fbinfo failed: %d\n", ret);
1094 goto unregister_irqs;
1095 }
1096
1097 /*
1098 * This makes sure that our colour bitfield
1099 * descriptors are correctly initialised.
1100 */
1101 atmel_lcdfb_check_var(&info->var, info);
1102
1103 ret = fb_set_var(info, &info->var);
1104 if (ret) {
1105 dev_warn(dev, "unable to set display parameters\n");
1106 goto free_cmap;
1107 }
1108
1109 dev_set_drvdata(dev, info);
1110
1111 /*
1112 * Tell the world that we're ready to go
1113 */
1114 ret = register_framebuffer(info);
1115 if (ret < 0) {
1116 dev_err(dev, "failed to register framebuffer device: %d\n", ret);
Sachin Kamata36bf192013-09-20 12:02:10 +05301117 goto free_cmap;
Nicolas Ferre14340582007-05-10 22:23:26 -07001118 }
1119
Nicolas Ferre968910b2008-07-23 21:31:34 -07001120 /* add selected videomode to modelist */
1121 fb_var_to_videomode(&fbmode, &info->var);
1122 fb_add_videomode(&fbmode, &info->modelist);
1123
Nicolas Ferre14340582007-05-10 22:23:26 -07001124 /* Power up the LCDC screen */
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001125 if (pdata->atmel_lcdfb_power_control)
1126 pdata->atmel_lcdfb_power_control(1);
Nicolas Ferre14340582007-05-10 22:23:26 -07001127
Claudio Scordino93f6ced2009-10-09 12:20:21 +02001128 dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
Nicolas Ferre14340582007-05-10 22:23:26 -07001129 info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
1130
1131 return 0;
1132
Nicolas Ferre14340582007-05-10 22:23:26 -07001133free_cmap:
1134 fb_dealloc_cmap(&info->cmap);
1135unregister_irqs:
Nicolas Ferred22579b2008-07-23 21:31:20 -07001136 cancel_work_sync(&sinfo->task);
Nicolas Ferre14340582007-05-10 22:23:26 -07001137 free_irq(sinfo->irq_base, info);
1138unmap_mmio:
David Brownella9a84c32008-02-06 01:39:26 -08001139 exit_backlight(sinfo);
Nicolas Ferre14340582007-05-10 22:23:26 -07001140 iounmap(sinfo->mmio);
1141release_mem:
1142 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1143free_fb:
1144 if (map)
1145 iounmap(info->screen_base);
1146 else
1147 atmel_lcdfb_free_video_memory(sinfo);
1148
1149release_intmem:
1150 if (map)
1151 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1152stop_clk:
1153 atmel_lcdfb_stop_clock(sinfo);
1154 clk_put(sinfo->lcdc_clk);
1155put_bus_clk:
Johan Hovold557b7d52013-02-07 16:31:56 +01001156 clk_put(sinfo->bus_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -07001157free_info:
1158 framebuffer_release(info);
1159out:
1160 dev_dbg(dev, "%s FAILED\n", __func__);
1161 return ret;
1162}
1163
1164static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
1165{
1166 struct device *dev = &pdev->dev;
1167 struct fb_info *info = dev_get_drvdata(dev);
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001168 struct atmel_lcdfb_info *sinfo;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001169 struct atmel_lcdfb_pdata *pdata;
Nicolas Ferre14340582007-05-10 22:23:26 -07001170
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001171 if (!info || !info->par)
Nicolas Ferre14340582007-05-10 22:23:26 -07001172 return 0;
Stanislaw Gruszka34a35bd2008-09-05 14:00:22 -07001173 sinfo = info->par;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001174 pdata = &sinfo->pdata;
Nicolas Ferre14340582007-05-10 22:23:26 -07001175
Nicolas Ferred22579b2008-07-23 21:31:20 -07001176 cancel_work_sync(&sinfo->task);
David Brownella9a84c32008-02-06 01:39:26 -08001177 exit_backlight(sinfo);
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001178 if (pdata->atmel_lcdfb_power_control)
1179 pdata->atmel_lcdfb_power_control(0);
Nicolas Ferre14340582007-05-10 22:23:26 -07001180 unregister_framebuffer(info);
1181 atmel_lcdfb_stop_clock(sinfo);
1182 clk_put(sinfo->lcdc_clk);
Johan Hovold557b7d52013-02-07 16:31:56 +01001183 clk_put(sinfo->bus_clk);
Nicolas Ferre14340582007-05-10 22:23:26 -07001184 fb_dealloc_cmap(&info->cmap);
1185 free_irq(sinfo->irq_base, info);
1186 iounmap(sinfo->mmio);
1187 release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1188 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
1189 iounmap(info->screen_base);
1190 release_mem_region(info->fix.smem_start, info->fix.smem_len);
1191 } else {
1192 atmel_lcdfb_free_video_memory(sinfo);
1193 }
1194
Nicolas Ferre14340582007-05-10 22:23:26 -07001195 framebuffer_release(info);
1196
1197 return 0;
1198}
1199
David Brownellcf19a372008-04-28 02:15:20 -07001200#ifdef CONFIG_PM
1201
1202static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1203{
1204 struct fb_info *info = platform_get_drvdata(pdev);
1205 struct atmel_lcdfb_info *sinfo = info->par;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001206 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
David Brownellcf19a372008-04-28 02:15:20 -07001207
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001208 /*
1209 * We don't want to handle interrupts while the clock is
1210 * stopped. It may take forever.
1211 */
1212 lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
1213
Hubert Feurstein9f106502012-01-09 17:23:57 +01001214 sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_CTR);
David Brownellcf19a372008-04-28 02:15:20 -07001215 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001216 if (pdata->atmel_lcdfb_power_control)
1217 pdata->atmel_lcdfb_power_control(0);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001218
1219 atmel_lcdfb_stop(sinfo);
David Brownellcf19a372008-04-28 02:15:20 -07001220 atmel_lcdfb_stop_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001221
David Brownellcf19a372008-04-28 02:15:20 -07001222 return 0;
1223}
1224
1225static int atmel_lcdfb_resume(struct platform_device *pdev)
1226{
1227 struct fb_info *info = platform_get_drvdata(pdev);
1228 struct atmel_lcdfb_info *sinfo = info->par;
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001229 struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
David Brownellcf19a372008-04-28 02:15:20 -07001230
1231 atmel_lcdfb_start_clock(sinfo);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001232 atmel_lcdfb_start(sinfo);
Jean-Christophe PLAGNIOL-VILLARD8af2c282013-03-28 22:53:42 +08001233 if (pdata->atmel_lcdfb_power_control)
1234 pdata->atmel_lcdfb_power_control(1);
David Brownellcf19a372008-04-28 02:15:20 -07001235 lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
Haavard Skinnemoen3aa04f12008-09-13 02:33:23 -07001236
1237 /* Enable FIFO & DMA errors */
1238 lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
1239 | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
1240
David Brownellcf19a372008-04-28 02:15:20 -07001241 return 0;
1242}
1243
1244#else
1245#define atmel_lcdfb_suspend NULL
1246#define atmel_lcdfb_resume NULL
1247#endif
1248
Nicolas Ferre14340582007-05-10 22:23:26 -07001249static struct platform_driver atmel_lcdfb_driver = {
1250 .remove = __exit_p(atmel_lcdfb_remove),
David Brownellcf19a372008-04-28 02:15:20 -07001251 .suspend = atmel_lcdfb_suspend,
1252 .resume = atmel_lcdfb_resume,
Johan Hovoldbbd44f6b2013-02-07 16:31:58 +01001253 .id_table = atmel_lcdfb_devtypes,
Nicolas Ferre14340582007-05-10 22:23:26 -07001254 .driver = {
1255 .name = "atmel_lcdfb",
1256 .owner = THIS_MODULE,
1257 },
1258};
1259
Fabio Porcedda3ccbf892013-03-15 14:02:38 +01001260module_platform_driver_probe(atmel_lcdfb_driver, atmel_lcdfb_probe);
Nicolas Ferre14340582007-05-10 22:23:26 -07001261
1262MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
Nicolas Ferre8f4c79c2008-01-14 00:55:13 -08001263MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
Nicolas Ferre14340582007-05-10 22:23:26 -07001264MODULE_LICENSE("GPL");