]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/atmel_lcdfb.c
atmel_lcdfb: validate display timings
[linux-2.6-omap-h63xx.git] / drivers / video / atmel_lcdfb.c
1 /*
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>
19
20 #include <asm/arch/board.h>
21 #include <asm/arch/cpu.h>
22 #include <asm/arch/gpio.h>
23
24 #include <video/atmel_lcdc.h>
25
26 #define lcdc_readl(sinfo, reg)          __raw_readl((sinfo)->mmio+(reg))
27 #define lcdc_writel(sinfo, reg, val)    __raw_writel((val), (sinfo)->mmio+(reg))
28
29 /* configurable parameters */
30 #define ATMEL_LCDC_CVAL_DEFAULT         0xc8
31 #define ATMEL_LCDC_DMA_BURST_LEN        8
32
33 #if defined(CONFIG_ARCH_AT91SAM9263) || defined(CONFIG_ARCH_AT91CAP9)
34 #define ATMEL_LCDC_FIFO_SIZE            2048
35 #else
36 #define ATMEL_LCDC_FIFO_SIZE            512
37 #endif
38
39 #if defined(CONFIG_ARCH_AT91)
40 #define ATMEL_LCDFB_FBINFO_DEFAULT      FBINFO_DEFAULT
41
42 static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
43                                         struct fb_var_screeninfo *var)
44 {
45
46 }
47 #elif defined(CONFIG_AVR32)
48 #define ATMEL_LCDFB_FBINFO_DEFAULT      (FBINFO_DEFAULT \
49                                         | FBINFO_PARTIAL_PAN_OK \
50                                         | FBINFO_HWACCEL_XPAN \
51                                         | FBINFO_HWACCEL_YPAN)
52
53 static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
54                                      struct fb_var_screeninfo *var)
55 {
56         u32 dma2dcfg;
57         u32 pixeloff;
58
59         pixeloff = (var->xoffset * var->bits_per_pixel) & 0x1f;
60
61         dma2dcfg = ((var->xres_virtual - var->xres) * var->bits_per_pixel) / 8;
62         dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
63         lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
64
65         /* Update configuration */
66         lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
67                     lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
68                     | ATMEL_LCDC_DMAUPDT);
69 }
70 #endif
71
72
73 static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
74         .type           = FB_TYPE_PACKED_PIXELS,
75         .visual         = FB_VISUAL_TRUECOLOR,
76         .xpanstep       = 0,
77         .ypanstep       = 0,
78         .ywrapstep      = 0,
79         .accel          = FB_ACCEL_NONE,
80 };
81
82 static unsigned long compute_hozval(unsigned long xres, unsigned long lcdcon2)
83 {
84         unsigned long value;
85
86         if (!(cpu_is_at91sam9261() || cpu_is_at32ap7000()))
87                 return xres;
88
89         value = xres;
90         if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
91                 /* STN display */
92                 if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
93                         value *= 3;
94                 }
95                 if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
96                    || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
97                       && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
98                         value = DIV_ROUND_UP(value, 4);
99                 else
100                         value = DIV_ROUND_UP(value, 8);
101         }
102
103         return value;
104 }
105
106 static void atmel_lcdfb_update_dma(struct fb_info *info,
107                                struct fb_var_screeninfo *var)
108 {
109         struct atmel_lcdfb_info *sinfo = info->par;
110         struct fb_fix_screeninfo *fix = &info->fix;
111         unsigned long dma_addr;
112
113         dma_addr = (fix->smem_start + var->yoffset * fix->line_length
114                     + var->xoffset * var->bits_per_pixel / 8);
115
116         dma_addr &= ~3UL;
117
118         /* Set framebuffer DMA base address and pixel offset */
119         lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
120
121         atmel_lcdfb_update_dma2d(sinfo, var);
122 }
123
124 static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
125 {
126         struct fb_info *info = sinfo->info;
127
128         dma_free_writecombine(info->device, info->fix.smem_len,
129                                 info->screen_base, info->fix.smem_start);
130 }
131
132 /**
133  *      atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
134  *      @sinfo: the frame buffer to allocate memory for
135  */
136 static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
137 {
138         struct fb_info *info = sinfo->info;
139         struct fb_var_screeninfo *var = &info->var;
140
141         info->fix.smem_len = (var->xres_virtual * var->yres_virtual
142                             * ((var->bits_per_pixel + 7) / 8));
143
144         info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
145                                         (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
146
147         if (!info->screen_base) {
148                 return -ENOMEM;
149         }
150
151         return 0;
152 }
153
154 /**
155  *      atmel_lcdfb_check_var - Validates a var passed in.
156  *      @var: frame buffer variable screen structure
157  *      @info: frame buffer structure that represents a single frame buffer
158  *
159  *      Checks to see if the hardware supports the state requested by
160  *      var passed in. This function does not alter the hardware
161  *      state!!!  This means the data stored in struct fb_info and
162  *      struct atmel_lcdfb_info do not change. This includes the var
163  *      inside of struct fb_info.  Do NOT change these. This function
164  *      can be called on its own if we intent to only test a mode and
165  *      not actually set it. The stuff in modedb.c is a example of
166  *      this. If the var passed in is slightly off by what the
167  *      hardware can support then we alter the var PASSED in to what
168  *      we can do. If the hardware doesn't support mode change a
169  *      -EINVAL will be returned by the upper layers. You don't need
170  *      to implement this function then. If you hardware doesn't
171  *      support changing the resolution then this function is not
172  *      needed. In this case the driver would just provide a var that
173  *      represents the static state the screen is in.
174  *
175  *      Returns negative errno on error, or zero on success.
176  */
177 static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
178                              struct fb_info *info)
179 {
180         struct device *dev = info->device;
181         struct atmel_lcdfb_info *sinfo = info->par;
182         unsigned long clk_value_khz;
183
184         clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
185
186         dev_dbg(dev, "%s:\n", __func__);
187         dev_dbg(dev, "  resolution: %ux%u\n", var->xres, var->yres);
188         dev_dbg(dev, "  pixclk:     %lu KHz\n", PICOS2KHZ(var->pixclock));
189         dev_dbg(dev, "  bpp:        %u\n", var->bits_per_pixel);
190         dev_dbg(dev, "  clk:        %lu KHz\n", clk_value_khz);
191
192         if ((PICOS2KHZ(var->pixclock) * var->bits_per_pixel / 8) > clk_value_khz) {
193                 dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
194                 return -EINVAL;
195         }
196
197         /* Force same alignment for each line */
198         var->xres = (var->xres + 3) & ~3UL;
199         var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
200
201         var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
202         var->transp.msb_right = 0;
203         var->transp.offset = var->transp.length = 0;
204         var->xoffset = var->yoffset = 0;
205
206         /* Saturate vertical and horizontal timings at maximum values */
207         var->vsync_len = min_t(u32, var->vsync_len,
208                         (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
209         var->upper_margin = min_t(u32, var->upper_margin,
210                         ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
211         var->lower_margin = min_t(u32, var->lower_margin,
212                         ATMEL_LCDC_VFP);
213         var->right_margin = min_t(u32, var->right_margin,
214                         (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
215         var->hsync_len = min_t(u32, var->hsync_len,
216                         (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
217         var->left_margin = min_t(u32, var->left_margin,
218                         ATMEL_LCDC_HBP + 1);
219
220         /* Some parameters can't be zero */
221         var->vsync_len = max_t(u32, var->vsync_len, 1);
222         var->right_margin = max_t(u32, var->right_margin, 1);
223         var->hsync_len = max_t(u32, var->hsync_len, 1);
224         var->left_margin = max_t(u32, var->left_margin, 1);
225
226         switch (var->bits_per_pixel) {
227         case 1:
228         case 2:
229         case 4:
230         case 8:
231                 var->red.offset = var->green.offset = var->blue.offset = 0;
232                 var->red.length = var->green.length = var->blue.length
233                         = var->bits_per_pixel;
234                 break;
235         case 15:
236         case 16:
237                 var->red.offset = 0;
238                 var->green.offset = 5;
239                 var->blue.offset = 10;
240                 var->red.length = var->green.length = var->blue.length = 5;
241                 break;
242         case 32:
243                 var->transp.offset = 24;
244                 var->transp.length = 8;
245                 /* fall through */
246         case 24:
247                 var->red.offset = 0;
248                 var->green.offset = 8;
249                 var->blue.offset = 16;
250                 var->red.length = var->green.length = var->blue.length = 8;
251                 break;
252         default:
253                 dev_err(dev, "color depth %d not supported\n",
254                                         var->bits_per_pixel);
255                 return -EINVAL;
256         }
257
258         return 0;
259 }
260
261 /**
262  *      atmel_lcdfb_set_par - Alters the hardware state.
263  *      @info: frame buffer structure that represents a single frame buffer
264  *
265  *      Using the fb_var_screeninfo in fb_info we set the resolution
266  *      of the this particular framebuffer. This function alters the
267  *      par AND the fb_fix_screeninfo stored in fb_info. It doesn't
268  *      not alter var in fb_info since we are using that data. This
269  *      means we depend on the data in var inside fb_info to be
270  *      supported by the hardware.  atmel_lcdfb_check_var is always called
271  *      before atmel_lcdfb_set_par to ensure this.  Again if you can't
272  *      change the resolution you don't need this function.
273  *
274  */
275 static int atmel_lcdfb_set_par(struct fb_info *info)
276 {
277         struct atmel_lcdfb_info *sinfo = info->par;
278         unsigned long hozval_linesz;
279         unsigned long value;
280         unsigned long clk_value_khz;
281         unsigned long bits_per_line;
282
283         dev_dbg(info->device, "%s:\n", __func__);
284         dev_dbg(info->device, "  * resolution: %ux%u (%ux%u virtual)\n",
285                  info->var.xres, info->var.yres,
286                  info->var.xres_virtual, info->var.yres_virtual);
287
288         /* Turn off the LCD controller and the DMA controller */
289         lcdc_writel(sinfo, ATMEL_LCDC_PWRCON, sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
290
291         /* Wait for the LCDC core to become idle */
292         while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
293                 msleep(10);
294
295         lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
296
297         if (info->var.bits_per_pixel == 1)
298                 info->fix.visual = FB_VISUAL_MONO01;
299         else if (info->var.bits_per_pixel <= 8)
300                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
301         else
302                 info->fix.visual = FB_VISUAL_TRUECOLOR;
303
304         bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
305         info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
306
307         /* Re-initialize the DMA engine... */
308         dev_dbg(info->device, "  * update DMA engine\n");
309         atmel_lcdfb_update_dma(info, &info->var);
310
311         /* ...set frame size and burst length = 8 words (?) */
312         value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
313         value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
314         lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
315
316         /* Now, the LCDC core... */
317
318         /* Set pixel clock */
319         clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
320
321         value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
322
323         value = (value / 2) - 1;
324         dev_dbg(info->device, "  * programming CLKVAL = 0x%08lx\n", value);
325
326         if (value <= 0) {
327                 dev_notice(info->device, "Bypassing pixel clock divider\n");
328                 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
329         } else {
330                 lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, value << ATMEL_LCDC_CLKVAL_OFFSET);
331                 info->var.pixclock = KHZ2PICOS(clk_value_khz / (2 * (value + 1)));
332                 dev_dbg(info->device, "  updated pixclk:     %lu KHz\n",
333                                         PICOS2KHZ(info->var.pixclock));
334         }
335
336
337         /* Initialize control register 2 */
338         value = sinfo->default_lcdcon2;
339
340         if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
341                 value |= ATMEL_LCDC_INVLINE_INVERTED;
342         if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
343                 value |= ATMEL_LCDC_INVFRAME_INVERTED;
344
345         switch (info->var.bits_per_pixel) {
346                 case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
347                 case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
348                 case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
349                 case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
350                 case 15: /* fall through */
351                 case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
352                 case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
353                 case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
354                 default: BUG(); break;
355         }
356         dev_dbg(info->device, "  * LCDCON2 = %08lx\n", value);
357         lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
358
359         /* Vertical timing */
360         value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
361         value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
362         value |= info->var.lower_margin;
363         dev_dbg(info->device, "  * LCDTIM1 = %08lx\n", value);
364         lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
365
366         /* Horizontal timing */
367         value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
368         value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
369         value |= (info->var.left_margin - 1);
370         dev_dbg(info->device, "  * LCDTIM2 = %08lx\n", value);
371         lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
372
373         /* Horizontal value (aka line size) */
374         hozval_linesz = compute_hozval(info->var.xres,
375                                         lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2));
376
377         /* Display size */
378         value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
379         value |= info->var.yres - 1;
380         dev_dbg(info->device, "  * LCDFRMCFG = %08lx\n", value);
381         lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
382
383         /* FIFO Threshold: Use formula from data sheet */
384         value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
385         lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
386
387         /* Toggle LCD_MODE every frame */
388         lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
389
390         /* Disable all interrupts */
391         lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
392
393         /* Set contrast */
394         value = ATMEL_LCDC_PS_DIV8 | ATMEL_LCDC_POL_POSITIVE | ATMEL_LCDC_ENA_PWMENABLE;
395         lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, value);
396         lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
397         /* ...wait for DMA engine to become idle... */
398         while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
399                 msleep(10);
400
401         dev_dbg(info->device, "  * re-enable DMA engine\n");
402         /* ...and enable it with updated configuration */
403         lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
404
405         dev_dbg(info->device, "  * re-enable LCDC core\n");
406         lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
407                 (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR);
408
409         dev_dbg(info->device, "  * DONE\n");
410
411         return 0;
412 }
413
414 static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
415 {
416         chan &= 0xffff;
417         chan >>= 16 - bf->length;
418         return chan << bf->offset;
419 }
420
421 /**
422  *      atmel_lcdfb_setcolreg - Optional function. Sets a color register.
423  *      @regno: Which register in the CLUT we are programming
424  *      @red: The red value which can be up to 16 bits wide
425  *      @green: The green value which can be up to 16 bits wide
426  *      @blue:  The blue value which can be up to 16 bits wide.
427  *      @transp: If supported the alpha value which can be up to 16 bits wide.
428  *      @info: frame buffer info structure
429  *
430  *      Set a single color register. The values supplied have a 16 bit
431  *      magnitude which needs to be scaled in this function for the hardware.
432  *      Things to take into consideration are how many color registers, if
433  *      any, are supported with the current color visual. With truecolor mode
434  *      no color palettes are supported. Here a psuedo palette is created
435  *      which we store the value in pseudo_palette in struct fb_info. For
436  *      pseudocolor mode we have a limited color palette. To deal with this
437  *      we can program what color is displayed for a particular pixel value.
438  *      DirectColor is similar in that we can program each color field. If
439  *      we have a static colormap we don't need to implement this function.
440  *
441  *      Returns negative errno on error, or zero on success. In an
442  *      ideal world, this would have been the case, but as it turns
443  *      out, the other drivers return 1 on failure, so that's what
444  *      we're going to do.
445  */
446 static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
447                              unsigned int green, unsigned int blue,
448                              unsigned int transp, struct fb_info *info)
449 {
450         struct atmel_lcdfb_info *sinfo = info->par;
451         unsigned int val;
452         u32 *pal;
453         int ret = 1;
454
455         if (info->var.grayscale)
456                 red = green = blue = (19595 * red + 38470 * green
457                                       + 7471 * blue) >> 16;
458
459         switch (info->fix.visual) {
460         case FB_VISUAL_TRUECOLOR:
461                 if (regno < 16) {
462                         pal = info->pseudo_palette;
463
464                         val  = chan_to_field(red, &info->var.red);
465                         val |= chan_to_field(green, &info->var.green);
466                         val |= chan_to_field(blue, &info->var.blue);
467
468                         pal[regno] = val;
469                         ret = 0;
470                 }
471                 break;
472
473         case FB_VISUAL_PSEUDOCOLOR:
474                 if (regno < 256) {
475                         val  = ((red   >> 11) & 0x001f);
476                         val |= ((green >>  6) & 0x03e0);
477                         val |= ((blue  >>  1) & 0x7c00);
478
479                         /*
480                          * TODO: intensity bit. Maybe something like
481                          *   ~(red[10] ^ green[10] ^ blue[10]) & 1
482                          */
483
484                         lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
485                         ret = 0;
486                 }
487                 break;
488
489         case FB_VISUAL_MONO01:
490                 if (regno < 2) {
491                         val = (regno == 0) ? 0x00 : 0x1F;
492                         lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
493                         ret = 0;
494                 }
495                 break;
496
497         }
498
499         return ret;
500 }
501
502 static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
503                                struct fb_info *info)
504 {
505         dev_dbg(info->device, "%s\n", __func__);
506
507         atmel_lcdfb_update_dma(info, var);
508
509         return 0;
510 }
511
512 static struct fb_ops atmel_lcdfb_ops = {
513         .owner          = THIS_MODULE,
514         .fb_check_var   = atmel_lcdfb_check_var,
515         .fb_set_par     = atmel_lcdfb_set_par,
516         .fb_setcolreg   = atmel_lcdfb_setcolreg,
517         .fb_pan_display = atmel_lcdfb_pan_display,
518         .fb_fillrect    = cfb_fillrect,
519         .fb_copyarea    = cfb_copyarea,
520         .fb_imageblit   = cfb_imageblit,
521 };
522
523 static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
524 {
525         struct fb_info *info = dev_id;
526         struct atmel_lcdfb_info *sinfo = info->par;
527         u32 status;
528
529         status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
530         lcdc_writel(sinfo, ATMEL_LCDC_IDR, status);
531         return IRQ_HANDLED;
532 }
533
534 static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
535 {
536         struct fb_info *info = sinfo->info;
537         int ret = 0;
538
539         memset_io(info->screen_base, 0, info->fix.smem_len);
540         info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
541
542         dev_info(info->device,
543                "%luKiB frame buffer at %08lx (mapped at %p)\n",
544                (unsigned long)info->fix.smem_len / 1024,
545                (unsigned long)info->fix.smem_start,
546                info->screen_base);
547
548         /* Allocate colormap */
549         ret = fb_alloc_cmap(&info->cmap, 256, 0);
550         if (ret < 0)
551                 dev_err(info->device, "Alloc color map failed\n");
552
553         return ret;
554 }
555
556 static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
557 {
558         if (sinfo->bus_clk)
559                 clk_enable(sinfo->bus_clk);
560         clk_enable(sinfo->lcdc_clk);
561 }
562
563 static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
564 {
565         if (sinfo->bus_clk)
566                 clk_disable(sinfo->bus_clk);
567         clk_disable(sinfo->lcdc_clk);
568 }
569
570
571 static int __init atmel_lcdfb_probe(struct platform_device *pdev)
572 {
573         struct device *dev = &pdev->dev;
574         struct fb_info *info;
575         struct atmel_lcdfb_info *sinfo;
576         struct atmel_lcdfb_info *pdata_sinfo;
577         struct resource *regs = NULL;
578         struct resource *map = NULL;
579         int ret;
580
581         dev_dbg(dev, "%s BEGIN\n", __func__);
582
583         ret = -ENOMEM;
584         info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
585         if (!info) {
586                 dev_err(dev, "cannot allocate memory\n");
587                 goto out;
588         }
589
590         sinfo = info->par;
591
592         if (dev->platform_data) {
593                 pdata_sinfo = (struct atmel_lcdfb_info *)dev->platform_data;
594                 sinfo->default_bpp = pdata_sinfo->default_bpp;
595                 sinfo->default_dmacon = pdata_sinfo->default_dmacon;
596                 sinfo->default_lcdcon2 = pdata_sinfo->default_lcdcon2;
597                 sinfo->default_monspecs = pdata_sinfo->default_monspecs;
598                 sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
599                 sinfo->guard_time = pdata_sinfo->guard_time;
600         } else {
601                 dev_err(dev, "cannot get default configuration\n");
602                 goto free_info;
603         }
604         sinfo->info = info;
605         sinfo->pdev = pdev;
606
607         strcpy(info->fix.id, sinfo->pdev->name);
608         info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
609         info->pseudo_palette = sinfo->pseudo_palette;
610         info->fbops = &atmel_lcdfb_ops;
611
612         memcpy(&info->monspecs, sinfo->default_monspecs, sizeof(info->monspecs));
613         info->fix = atmel_lcdfb_fix;
614
615         /* Enable LCDC Clocks */
616         if (cpu_is_at91sam9261() || cpu_is_at32ap7000()) {
617                 sinfo->bus_clk = clk_get(dev, "hck1");
618                 if (IS_ERR(sinfo->bus_clk)) {
619                         ret = PTR_ERR(sinfo->bus_clk);
620                         goto free_info;
621                 }
622         }
623         sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
624         if (IS_ERR(sinfo->lcdc_clk)) {
625                 ret = PTR_ERR(sinfo->lcdc_clk);
626                 goto put_bus_clk;
627         }
628         atmel_lcdfb_start_clock(sinfo);
629
630         ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
631                         info->monspecs.modedb_len, info->monspecs.modedb,
632                         sinfo->default_bpp);
633         if (!ret) {
634                 dev_err(dev, "no suitable video mode found\n");
635                 goto stop_clk;
636         }
637
638
639         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
640         if (!regs) {
641                 dev_err(dev, "resources unusable\n");
642                 ret = -ENXIO;
643                 goto stop_clk;
644         }
645
646         sinfo->irq_base = platform_get_irq(pdev, 0);
647         if (sinfo->irq_base < 0) {
648                 dev_err(dev, "unable to get irq\n");
649                 ret = sinfo->irq_base;
650                 goto stop_clk;
651         }
652
653         /* Initialize video memory */
654         map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
655         if (map) {
656                 /* use a pre-allocated memory buffer */
657                 info->fix.smem_start = map->start;
658                 info->fix.smem_len = map->end - map->start + 1;
659                 if (!request_mem_region(info->fix.smem_start,
660                                         info->fix.smem_len, pdev->name)) {
661                         ret = -EBUSY;
662                         goto stop_clk;
663                 }
664
665                 info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
666                 if (!info->screen_base)
667                         goto release_intmem;
668         } else {
669                 /* alocate memory buffer */
670                 ret = atmel_lcdfb_alloc_video_memory(sinfo);
671                 if (ret < 0) {
672                         dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
673                         goto stop_clk;
674                 }
675         }
676
677         /* LCDC registers */
678         info->fix.mmio_start = regs->start;
679         info->fix.mmio_len = regs->end - regs->start + 1;
680
681         if (!request_mem_region(info->fix.mmio_start,
682                                 info->fix.mmio_len, pdev->name)) {
683                 ret = -EBUSY;
684                 goto free_fb;
685         }
686
687         sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
688         if (!sinfo->mmio) {
689                 dev_err(dev, "cannot map LCDC registers\n");
690                 goto release_mem;
691         }
692
693         /* interrupt */
694         ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
695         if (ret) {
696                 dev_err(dev, "request_irq failed: %d\n", ret);
697                 goto unmap_mmio;
698         }
699
700         ret = atmel_lcdfb_init_fbinfo(sinfo);
701         if (ret < 0) {
702                 dev_err(dev, "init fbinfo failed: %d\n", ret);
703                 goto unregister_irqs;
704         }
705
706         /*
707          * This makes sure that our colour bitfield
708          * descriptors are correctly initialised.
709          */
710         atmel_lcdfb_check_var(&info->var, info);
711
712         ret = fb_set_var(info, &info->var);
713         if (ret) {
714                 dev_warn(dev, "unable to set display parameters\n");
715                 goto free_cmap;
716         }
717
718         dev_set_drvdata(dev, info);
719
720         /*
721          * Tell the world that we're ready to go
722          */
723         ret = register_framebuffer(info);
724         if (ret < 0) {
725                 dev_err(dev, "failed to register framebuffer device: %d\n", ret);
726                 goto free_cmap;
727         }
728
729         /* Power up the LCDC screen */
730         if (sinfo->atmel_lcdfb_power_control)
731                 sinfo->atmel_lcdfb_power_control(1);
732
733         dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %lu\n",
734                        info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
735
736         return 0;
737
738
739 free_cmap:
740         fb_dealloc_cmap(&info->cmap);
741 unregister_irqs:
742         free_irq(sinfo->irq_base, info);
743 unmap_mmio:
744         iounmap(sinfo->mmio);
745 release_mem:
746         release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
747 free_fb:
748         if (map)
749                 iounmap(info->screen_base);
750         else
751                 atmel_lcdfb_free_video_memory(sinfo);
752
753 release_intmem:
754         if (map)
755                 release_mem_region(info->fix.smem_start, info->fix.smem_len);
756 stop_clk:
757         atmel_lcdfb_stop_clock(sinfo);
758         clk_put(sinfo->lcdc_clk);
759 put_bus_clk:
760         if (sinfo->bus_clk)
761                 clk_put(sinfo->bus_clk);
762 free_info:
763         framebuffer_release(info);
764 out:
765         dev_dbg(dev, "%s FAILED\n", __func__);
766         return ret;
767 }
768
769 static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
770 {
771         struct device *dev = &pdev->dev;
772         struct fb_info *info = dev_get_drvdata(dev);
773         struct atmel_lcdfb_info *sinfo = info->par;
774
775         if (!sinfo)
776                 return 0;
777
778         if (sinfo->atmel_lcdfb_power_control)
779                 sinfo->atmel_lcdfb_power_control(0);
780         unregister_framebuffer(info);
781         atmel_lcdfb_stop_clock(sinfo);
782         clk_put(sinfo->lcdc_clk);
783         if (sinfo->bus_clk)
784                 clk_put(sinfo->bus_clk);
785         fb_dealloc_cmap(&info->cmap);
786         free_irq(sinfo->irq_base, info);
787         iounmap(sinfo->mmio);
788         release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
789         if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
790                 iounmap(info->screen_base);
791                 release_mem_region(info->fix.smem_start, info->fix.smem_len);
792         } else {
793                 atmel_lcdfb_free_video_memory(sinfo);
794         }
795
796         dev_set_drvdata(dev, NULL);
797         framebuffer_release(info);
798
799         return 0;
800 }
801
802 static struct platform_driver atmel_lcdfb_driver = {
803         .remove         = __exit_p(atmel_lcdfb_remove),
804         .driver         = {
805                 .name   = "atmel_lcdfb",
806                 .owner  = THIS_MODULE,
807         },
808 };
809
810 static int __init atmel_lcdfb_init(void)
811 {
812         return platform_driver_probe(&atmel_lcdfb_driver, atmel_lcdfb_probe);
813 }
814
815 static void __exit atmel_lcdfb_exit(void)
816 {
817         platform_driver_unregister(&atmel_lcdfb_driver);
818 }
819
820 module_init(atmel_lcdfb_init);
821 module_exit(atmel_lcdfb_exit);
822
823 MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
824 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
825 MODULE_LICENSE("GPL");