blob: 5c61286d7ff5d0ac1cc01d05692401170f7c0d49 [file] [log] [blame]
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001/*
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07002 * Freescale i.MX Frame Buffer device driver
3 *
4 * Copyright (C) 2004 Sascha Hauer, Pengutronix
5 * Based on acornfb.c Copyright (C) Russell King.
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 * Please direct your questions and comments on this driver to the following
12 * email address:
13 *
14 * linux-arm-kernel@lists.arm.linux.org.uk
15 */
16
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070017#include <linux/module.h>
18#include <linux/kernel.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070019#include <linux/errno.h>
20#include <linux/string.h>
21#include <linux/interrupt.h>
22#include <linux/slab.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070023#include <linux/mm.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070024#include <linux/fb.h>
25#include <linux/delay.h>
26#include <linux/init.h>
27#include <linux/ioport.h>
28#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010029#include <linux/platform_device.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070030#include <linux/dma-mapping.h>
Juergen Beisert72330b02008-12-16 11:44:07 +010031#include <linux/io.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070032
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/imxfb.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070034
35/*
36 * Complain if VAR is out of range.
37 */
38#define DEBUG_VAR 1
39
Juergen Beisert72330b02008-12-16 11:44:07 +010040#define DRIVER_NAME "imx-fb"
41
42#define LCDC_SSA 0x00
43
44#define LCDC_SIZE 0x04
45#define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
46#define SIZE_YMAX(y) ((y) & 0x1ff)
47
48#define LCDC_VPW 0x08
49#define VPW_VPW(x) ((x) & 0x3ff)
50
51#define LCDC_CPOS 0x0C
52#define CPOS_CC1 (1<<31)
53#define CPOS_CC0 (1<<30)
54#define CPOS_OP (1<<28)
55#define CPOS_CXP(x) (((x) & 3ff) << 16)
56#define CPOS_CYP(y) ((y) & 0x1ff)
57
58#define LCDC_LCWHB 0x10
59#define LCWHB_BK_EN (1<<31)
60#define LCWHB_CW(w) (((w) & 0x1f) << 24)
61#define LCWHB_CH(h) (((h) & 0x1f) << 16)
62#define LCWHB_BD(x) ((x) & 0xff)
63
64#define LCDC_LCHCC 0x14
65#define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
66#define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
67#define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
68
69#define LCDC_PCR 0x18
70
71#define LCDC_HCR 0x1C
72#define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
73#define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
74#define HCR_H_WAIT_2(x) ((x) & 0xff)
75
76#define LCDC_VCR 0x20
77#define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
78#define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
79#define VCR_V_WAIT_2(x) ((x) & 0xff)
80
81#define LCDC_POS 0x24
82#define POS_POS(x) ((x) & 1f)
83
84#define LCDC_LSCR1 0x28
85/* bit fields in imxfb.h */
86
87#define LCDC_PWMR 0x2C
88/* bit fields in imxfb.h */
89
90#define LCDC_DMACR 0x30
91/* bit fields in imxfb.h */
92
93#define LCDC_RMCR 0x34
94#define RMCR_LCDC_EN (1<<1)
95#define RMCR_SELF_REF (1<<0)
96
97#define LCDC_LCDICR 0x38
98#define LCDICR_INT_SYN (1<<2)
99#define LCDICR_INT_CON (1)
100
101#define LCDC_LCDISR 0x40
102#define LCDISR_UDR_ERR (1<<3)
103#define LCDISR_ERR_RES (1<<2)
104#define LCDISR_EOF (1<<1)
105#define LCDISR_BOF (1<<0)
106
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100107/*
108 * These are the bitfields for each
109 * display depth that we support.
110 */
111struct imxfb_rgb {
112 struct fb_bitfield red;
113 struct fb_bitfield green;
114 struct fb_bitfield blue;
115 struct fb_bitfield transp;
116};
117
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100118struct imxfb_info {
119 struct platform_device *pdev;
120 void __iomem *regs;
121
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100122 u_int max_bpp;
123 u_int max_xres;
124 u_int max_yres;
125
126 /*
127 * These are the addresses we mapped
128 * the framebuffer memory region to.
129 */
130 dma_addr_t map_dma;
131 u_char *map_cpu;
132 u_int map_size;
133
134 u_char *screen_cpu;
135 dma_addr_t screen_dma;
136 u_int palette_size;
137
138 dma_addr_t dbar1;
139 dma_addr_t dbar2;
140
141 u_int pcr;
142 u_int pwmr;
143 u_int lscr1;
144 u_int dmacr;
145 u_int cmap_inverse:1,
146 cmap_static:1,
147 unused:30;
148
149 void (*lcd_power)(int);
150 void (*backlight_power)(int);
151};
152
153#define IMX_NAME "IMX"
154
155/*
156 * Minimum X and Y resolutions
157 */
158#define MIN_XRES 64
159#define MIN_YRES 64
160
Sascha Hauer15122222009-01-26 17:31:02 +0100161/* Actually this really is 18bit support, the lowest 2 bits of each colour
162 * are unused in hardware. We claim to have 24bit support to make software
163 * like X work, which does not support 18bit.
164 */
165static struct imxfb_rgb def_rgb_18 = {
166 .red = {.offset = 16, .length = 8,},
167 .green = {.offset = 8, .length = 8,},
168 .blue = {.offset = 0, .length = 8,},
169 .transp = {.offset = 0, .length = 0,},
170};
171
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100172static struct imxfb_rgb def_rgb_16_tft = {
173 .red = {.offset = 11, .length = 5,},
174 .green = {.offset = 5, .length = 6,},
175 .blue = {.offset = 0, .length = 5,},
176 .transp = {.offset = 0, .length = 0,},
177};
178
179static struct imxfb_rgb def_rgb_16_stn = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100180 .red = {.offset = 8, .length = 4,},
181 .green = {.offset = 4, .length = 4,},
182 .blue = {.offset = 0, .length = 4,},
183 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700184};
185
186static struct imxfb_rgb def_rgb_8 = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100187 .red = {.offset = 0, .length = 8,},
188 .green = {.offset = 0, .length = 8,},
189 .blue = {.offset = 0, .length = 8,},
190 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700191};
192
Sascha Hauer66c87192008-12-16 11:44:08 +0100193static int imxfb_activate_var(struct fb_var_screeninfo *var,
194 struct fb_info *info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700195
196static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
197{
198 chan &= 0xffff;
199 chan >>= 16 - bf->length;
200 return chan << bf->offset;
201}
202
Sascha Hauer66c87192008-12-16 11:44:08 +0100203static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
204 u_int trans, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700205{
206 struct imxfb_info *fbi = info->par;
207 u_int val, ret = 1;
208
209#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
210 if (regno < fbi->palette_size) {
211 val = (CNVT_TOHW(red, 4) << 8) |
212 (CNVT_TOHW(green,4) << 4) |
213 CNVT_TOHW(blue, 4);
214
Juergen Beisert72330b02008-12-16 11:44:07 +0100215 writel(val, fbi->regs + 0x800 + (regno << 2));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700216 ret = 0;
217 }
218 return ret;
219}
220
Sascha Hauer66c87192008-12-16 11:44:08 +0100221static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700222 u_int trans, struct fb_info *info)
223{
224 struct imxfb_info *fbi = info->par;
225 unsigned int val;
226 int ret = 1;
227
228 /*
229 * If inverse mode was selected, invert all the colours
230 * rather than the register number. The register number
231 * is what you poke into the framebuffer to produce the
232 * colour you requested.
233 */
234 if (fbi->cmap_inverse) {
235 red = 0xffff - red;
236 green = 0xffff - green;
237 blue = 0xffff - blue;
238 }
239
240 /*
241 * If greyscale is true, then we convert the RGB value
242 * to greyscale no mater what visual we are using.
243 */
244 if (info->var.grayscale)
245 red = green = blue = (19595 * red + 38470 * green +
246 7471 * blue) >> 16;
247
248 switch (info->fix.visual) {
249 case FB_VISUAL_TRUECOLOR:
250 /*
251 * 12 or 16-bit True Colour. We encode the RGB value
252 * according to the RGB bitfield information.
253 */
254 if (regno < 16) {
255 u32 *pal = info->pseudo_palette;
256
257 val = chan_to_field(red, &info->var.red);
258 val |= chan_to_field(green, &info->var.green);
259 val |= chan_to_field(blue, &info->var.blue);
260
261 pal[regno] = val;
262 ret = 0;
263 }
264 break;
265
266 case FB_VISUAL_STATIC_PSEUDOCOLOR:
267 case FB_VISUAL_PSEUDOCOLOR:
268 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
269 break;
270 }
271
272 return ret;
273}
274
275/*
276 * imxfb_check_var():
277 * Round up in the following order: bits_per_pixel, xres,
278 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
279 * bitfields, horizontal timing, vertical timing.
280 */
Sascha Hauer66c87192008-12-16 11:44:08 +0100281static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700282{
283 struct imxfb_info *fbi = info->par;
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100284 struct imxfb_rgb *rgb;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700285
286 if (var->xres < MIN_XRES)
287 var->xres = MIN_XRES;
288 if (var->yres < MIN_YRES)
289 var->yres = MIN_YRES;
290 if (var->xres > fbi->max_xres)
291 var->xres = fbi->max_xres;
292 if (var->yres > fbi->max_yres)
293 var->yres = fbi->max_yres;
294 var->xres_virtual = max(var->xres_virtual, var->xres);
295 var->yres_virtual = max(var->yres_virtual, var->yres);
296
297 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
298 switch (var->bits_per_pixel) {
Sascha Hauer15122222009-01-26 17:31:02 +0100299 case 32:
300 rgb = &def_rgb_18;
301 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700302 case 16:
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100303 default:
304 if (readl(fbi->regs + LCDC_PCR) & PCR_TFT)
305 rgb = &def_rgb_16_tft;
306 else
307 rgb = &def_rgb_16_stn;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700308 break;
309 case 8:
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100310 rgb = &def_rgb_8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700311 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700312 }
313
314 /*
315 * Copy the RGB parameters for this display
316 * from the machine specific parameters.
317 */
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100318 var->red = rgb->red;
319 var->green = rgb->green;
320 var->blue = rgb->blue;
321 var->transp = rgb->transp;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700322
323 pr_debug("RGBT length = %d:%d:%d:%d\n",
324 var->red.length, var->green.length, var->blue.length,
325 var->transp.length);
326
327 pr_debug("RGBT offset = %d:%d:%d:%d\n",
328 var->red.offset, var->green.offset, var->blue.offset,
329 var->transp.offset);
330
331 return 0;
332}
333
334/*
335 * imxfb_set_par():
336 * Set the user defined part of the display for the specified console
337 */
338static int imxfb_set_par(struct fb_info *info)
339{
340 struct imxfb_info *fbi = info->par;
341 struct fb_var_screeninfo *var = &info->var;
342
Sascha Hauer15122222009-01-26 17:31:02 +0100343 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700344 info->fix.visual = FB_VISUAL_TRUECOLOR;
345 else if (!fbi->cmap_static)
346 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
347 else {
348 /*
349 * Some people have weird ideas about wanting static
350 * pseudocolor maps. I suspect their user space
351 * applications are broken.
352 */
353 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
354 }
355
Sascha Hauer66c87192008-12-16 11:44:08 +0100356 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700357 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
358
359 imxfb_activate_var(var, info);
360
361 return 0;
362}
363
364static void imxfb_enable_controller(struct imxfb_info *fbi)
365{
366 pr_debug("Enabling LCD controller\n");
367
368 /* initialize LCDC */
Juergen Beisert72330b02008-12-16 11:44:07 +0100369 writel(readl(fbi->regs + LCDC_RMCR) & ~RMCR_LCDC_EN,
370 fbi->regs + LCDC_RMCR); /* just to be safe... */
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700371
Juergen Beisert72330b02008-12-16 11:44:07 +0100372 writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
373
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700374 /* physical screen start address */
Juergen Beisert72330b02008-12-16 11:44:07 +0100375 writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
376 fbi->regs + LCDC_VPW);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700377
Juergen Beisert72330b02008-12-16 11:44:07 +0100378 /* panning offset 0 (0 pixel offset) */
379 writel(0x00000000, fbi->regs + LCDC_POS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700380
381 /* disable hardware cursor */
Juergen Beisert72330b02008-12-16 11:44:07 +0100382 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
383 fbi->regs + LCDC_CPOS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700384
Juergen Beisert72330b02008-12-16 11:44:07 +0100385 writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700386
Sascha Hauer66c87192008-12-16 11:44:08 +0100387 if (fbi->backlight_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700388 fbi->backlight_power(1);
Sascha Hauer66c87192008-12-16 11:44:08 +0100389 if (fbi->lcd_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700390 fbi->lcd_power(1);
391}
392
393static void imxfb_disable_controller(struct imxfb_info *fbi)
394{
395 pr_debug("Disabling LCD controller\n");
396
Sascha Hauer66c87192008-12-16 11:44:08 +0100397 if (fbi->backlight_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700398 fbi->backlight_power(0);
Sascha Hauer66c87192008-12-16 11:44:08 +0100399 if (fbi->lcd_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700400 fbi->lcd_power(0);
401
Juergen Beisert72330b02008-12-16 11:44:07 +0100402 writel(0, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700403}
404
405static int imxfb_blank(int blank, struct fb_info *info)
406{
407 struct imxfb_info *fbi = info->par;
408
409 pr_debug("imxfb_blank: blank=%d\n", blank);
410
411 switch (blank) {
412 case FB_BLANK_POWERDOWN:
413 case FB_BLANK_VSYNC_SUSPEND:
414 case FB_BLANK_HSYNC_SUSPEND:
415 case FB_BLANK_NORMAL:
416 imxfb_disable_controller(fbi);
417 break;
418
419 case FB_BLANK_UNBLANK:
420 imxfb_enable_controller(fbi);
421 break;
422 }
423 return 0;
424}
425
426static struct fb_ops imxfb_ops = {
427 .owner = THIS_MODULE,
428 .fb_check_var = imxfb_check_var,
429 .fb_set_par = imxfb_set_par,
430 .fb_setcolreg = imxfb_setcolreg,
431 .fb_fillrect = cfb_fillrect,
432 .fb_copyarea = cfb_copyarea,
433 .fb_imageblit = cfb_imageblit,
434 .fb_blank = imxfb_blank,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700435};
436
437/*
438 * imxfb_activate_var():
439 * Configures LCD Controller based on entries in var parameter. Settings are
440 * only written to the controller if changes were made.
441 */
442static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
443{
444 struct imxfb_info *fbi = info->par;
445 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
446 var->xres, var->hsync_len,
447 var->left_margin, var->right_margin);
448 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
449 var->yres, var->vsync_len,
450 var->upper_margin, var->lower_margin);
451
452#if DEBUG_VAR
453 if (var->xres < 16 || var->xres > 1024)
454 printk(KERN_ERR "%s: invalid xres %d\n",
455 info->fix.id, var->xres);
456 if (var->hsync_len < 1 || var->hsync_len > 64)
457 printk(KERN_ERR "%s: invalid hsync_len %d\n",
458 info->fix.id, var->hsync_len);
459 if (var->left_margin > 255)
460 printk(KERN_ERR "%s: invalid left_margin %d\n",
461 info->fix.id, var->left_margin);
462 if (var->right_margin > 255)
463 printk(KERN_ERR "%s: invalid right_margin %d\n",
464 info->fix.id, var->right_margin);
465 if (var->yres < 1 || var->yres > 511)
466 printk(KERN_ERR "%s: invalid yres %d\n",
467 info->fix.id, var->yres);
468 if (var->vsync_len > 100)
469 printk(KERN_ERR "%s: invalid vsync_len %d\n",
470 info->fix.id, var->vsync_len);
471 if (var->upper_margin > 63)
472 printk(KERN_ERR "%s: invalid upper_margin %d\n",
473 info->fix.id, var->upper_margin);
474 if (var->lower_margin > 255)
475 printk(KERN_ERR "%s: invalid lower_margin %d\n",
476 info->fix.id, var->lower_margin);
477#endif
478
Juergen Beisert72330b02008-12-16 11:44:07 +0100479 writel(HCR_H_WIDTH(var->hsync_len) |
Sascha Hauerd6ed5752008-12-16 11:44:08 +0100480 HCR_H_WAIT_1(var->right_margin) |
481 HCR_H_WAIT_2(var->left_margin),
Juergen Beisert72330b02008-12-16 11:44:07 +0100482 fbi->regs + LCDC_HCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700483
Juergen Beisert72330b02008-12-16 11:44:07 +0100484 writel(VCR_V_WIDTH(var->vsync_len) |
Sascha Hauerd6ed5752008-12-16 11:44:08 +0100485 VCR_V_WAIT_1(var->lower_margin) |
486 VCR_V_WAIT_2(var->upper_margin),
Juergen Beisert72330b02008-12-16 11:44:07 +0100487 fbi->regs + LCDC_VCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700488
Juergen Beisert72330b02008-12-16 11:44:07 +0100489 writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
490 fbi->regs + LCDC_SIZE);
491 writel(fbi->pcr, fbi->regs + LCDC_PCR);
492 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
493 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
494 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700495
496 return 0;
497}
498
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700499#ifdef CONFIG_PM
500/*
501 * Power management hooks. Note that we won't be called from IRQ context,
502 * unlike the blank functions above, so we may sleep.
503 */
Russell King3ae5eae2005-11-09 22:32:44 +0000504static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700505{
Russell King3ae5eae2005-11-09 22:32:44 +0000506 struct imxfb_info *fbi = platform_get_drvdata(dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100507
508 pr_debug("%s\n", __func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700509
Russell King9480e302005-10-28 09:52:56 -0700510 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700511 return 0;
512}
513
Russell King3ae5eae2005-11-09 22:32:44 +0000514static int imxfb_resume(struct platform_device *dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700515{
Russell King3ae5eae2005-11-09 22:32:44 +0000516 struct imxfb_info *fbi = platform_get_drvdata(dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100517
518 pr_debug("%s\n", __func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700519
Russell King9480e302005-10-28 09:52:56 -0700520 imxfb_enable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700521 return 0;
522}
523#else
524#define imxfb_suspend NULL
525#define imxfb_resume NULL
526#endif
527
Juergen Beisert72330b02008-12-16 11:44:07 +0100528static int __init imxfb_init_fbinfo(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700529{
Sascha Hauer27889272008-12-16 11:44:09 +0100530 struct imx_fb_platform_data *pdata = pdev->dev.platform_data;
Juergen Beisert72330b02008-12-16 11:44:07 +0100531 struct fb_info *info = dev_get_drvdata(&pdev->dev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700532 struct imxfb_info *fbi = info->par;
533
Harvey Harrison5ae12172008-04-28 02:15:47 -0700534 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700535
Sascha Hauer66c87192008-12-16 11:44:08 +0100536 info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700537 if (!info->pseudo_palette)
538 return -ENOMEM;
539
540 memset(fbi, 0, sizeof(struct imxfb_info));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700541
542 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
543
Sascha Hauer66c87192008-12-16 11:44:08 +0100544 info->fix.type = FB_TYPE_PACKED_PIXELS;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700545 info->fix.type_aux = 0;
546 info->fix.xpanstep = 0;
547 info->fix.ypanstep = 0;
548 info->fix.ywrapstep = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100549 info->fix.accel = FB_ACCEL_NONE;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700550
551 info->var.nonstd = 0;
552 info->var.activate = FB_ACTIVATE_NOW;
553 info->var.height = -1;
554 info->var.width = -1;
555 info->var.accel_flags = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100556 info->var.vmode = FB_VMODE_NONINTERLACED;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700557
558 info->fbops = &imxfb_ops;
Sascha Hauer66c87192008-12-16 11:44:08 +0100559 info->flags = FBINFO_FLAG_DEFAULT |
560 FBINFO_READS_FAST;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700561
Sascha Hauer27889272008-12-16 11:44:09 +0100562 fbi->max_xres = pdata->xres;
563 info->var.xres = pdata->xres;
564 info->var.xres_virtual = pdata->xres;
565 fbi->max_yres = pdata->yres;
566 info->var.yres = pdata->yres;
567 info->var.yres_virtual = pdata->yres;
568 fbi->max_bpp = pdata->bpp;
569 info->var.bits_per_pixel = pdata->bpp;
570 info->var.nonstd = pdata->nonstd;
571 info->var.pixclock = pdata->pixclock;
572 info->var.hsync_len = pdata->hsync_len;
573 info->var.left_margin = pdata->left_margin;
574 info->var.right_margin = pdata->right_margin;
575 info->var.vsync_len = pdata->vsync_len;
576 info->var.upper_margin = pdata->upper_margin;
577 info->var.lower_margin = pdata->lower_margin;
578 info->var.sync = pdata->sync;
579 info->var.grayscale = pdata->cmap_greyscale;
580 fbi->cmap_inverse = pdata->cmap_inverse;
581 fbi->cmap_static = pdata->cmap_static;
582 fbi->pcr = pdata->pcr;
583 fbi->lscr1 = pdata->lscr1;
584 fbi->dmacr = pdata->dmacr;
585 fbi->pwmr = pdata->pwmr;
586 fbi->lcd_power = pdata->lcd_power;
587 fbi->backlight_power = pdata->backlight_power;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700588 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
589 fbi->max_bpp / 8;
590
591 return 0;
592}
593
Russell King3ae5eae2005-11-09 22:32:44 +0000594static int __init imxfb_probe(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700595{
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700596 struct imxfb_info *fbi;
597 struct fb_info *info;
Sascha Hauer27889272008-12-16 11:44:09 +0100598 struct imx_fb_platform_data *pdata;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700599 struct resource *res;
600 int ret;
601
602 printk("i.MX Framebuffer driver\n");
603
604 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sascha Hauer66c87192008-12-16 11:44:08 +0100605 if (!res)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700606 return -ENODEV;
607
Sascha Hauer27889272008-12-16 11:44:09 +0100608 pdata = pdev->dev.platform_data;
609 if (!pdata) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000610 dev_err(&pdev->dev,"No platform_data available\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700611 return -ENOMEM;
612 }
613
Russell King3ae5eae2005-11-09 22:32:44 +0000614 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100615 if (!info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700616 return -ENOMEM;
617
618 fbi = info->par;
619
Russell King3ae5eae2005-11-09 22:32:44 +0000620 platform_set_drvdata(pdev, info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700621
Juergen Beisert72330b02008-12-16 11:44:07 +0100622 ret = imxfb_init_fbinfo(pdev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100623 if (ret < 0)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700624 goto failed_init;
625
Juergen Beisert72330b02008-12-16 11:44:07 +0100626 res = request_mem_region(res->start, resource_size(res),
627 DRIVER_NAME);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700628 if (!res) {
629 ret = -EBUSY;
Juergen Beisert72330b02008-12-16 11:44:07 +0100630 goto failed_req;
631 }
632
633 fbi->regs = ioremap(res->start, resource_size(res));
634 if (fbi->regs == NULL) {
635 printk(KERN_ERR"Cannot map frame buffer registers\n");
636 goto failed_ioremap;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700637 }
638
Sascha Hauer27889272008-12-16 11:44:09 +0100639 if (!pdata->fixed_screen_cpu) {
Juergen Beisert72330b02008-12-16 11:44:07 +0100640 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
641 fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
642 fbi->map_size, &fbi->map_dma, GFP_KERNEL);
643
644 if (!fbi->map_cpu) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000645 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700646 ret = -ENOMEM;
647 goto failed_map;
648 }
Juergen Beisert72330b02008-12-16 11:44:07 +0100649
650 info->screen_base = fbi->map_cpu;
651 fbi->screen_cpu = fbi->map_cpu;
652 fbi->screen_dma = fbi->map_dma;
653 info->fix.smem_start = fbi->screen_dma;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700654 } else {
655 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
Sascha Hauer27889272008-12-16 11:44:09 +0100656 fbi->map_cpu = pdata->fixed_screen_cpu;
657 fbi->map_dma = pdata->fixed_screen_dma;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700658 info->screen_base = fbi->map_cpu;
659 fbi->screen_cpu = fbi->map_cpu;
660 fbi->screen_dma = fbi->map_dma;
661 info->fix.smem_start = fbi->screen_dma;
662 }
663
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100664 if (pdata->init) {
665 ret = pdata->init(fbi->pdev);
666 if (ret)
667 goto failed_platform_init;
668 }
669
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700670 /*
671 * This makes sure that our colour bitfield
672 * descriptors are correctly initialised.
673 */
674 imxfb_check_var(&info->var, info);
675
Sascha Hauer66c87192008-12-16 11:44:08 +0100676 ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700677 if (ret < 0)
678 goto failed_cmap;
679
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700680 imxfb_set_par(info);
681 ret = register_framebuffer(info);
682 if (ret < 0) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000683 dev_err(&pdev->dev, "failed to register framebuffer\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700684 goto failed_register;
685 }
686
687 imxfb_enable_controller(fbi);
688
689 return 0;
690
691failed_register:
692 fb_dealloc_cmap(&info->cmap);
693failed_cmap:
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100694 if (pdata->exit)
695 pdata->exit(fbi->pdev);
696failed_platform_init:
Sascha Hauer27889272008-12-16 11:44:09 +0100697 if (!pdata->fixed_screen_cpu)
Russell King3ae5eae2005-11-09 22:32:44 +0000698 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
Juergen Beisert72330b02008-12-16 11:44:07 +0100699 fbi->map_dma);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700700failed_map:
Juergen Beisert72330b02008-12-16 11:44:07 +0100701 iounmap(fbi->regs);
702failed_ioremap:
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700703 release_mem_region(res->start, res->end - res->start);
Juergen Beisert72330b02008-12-16 11:44:07 +0100704failed_req:
705 kfree(info->pseudo_palette);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700706failed_init:
Russell King3ae5eae2005-11-09 22:32:44 +0000707 platform_set_drvdata(pdev, NULL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700708 framebuffer_release(info);
709 return ret;
710}
711
Juergen Beisert72330b02008-12-16 11:44:07 +0100712static int __devexit imxfb_remove(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700713{
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100714 struct imx_fb_platform_data *pdata;
Russell King3ae5eae2005-11-09 22:32:44 +0000715 struct fb_info *info = platform_get_drvdata(pdev);
Sascha Hauer772a9e632005-07-17 20:15:36 +0100716 struct imxfb_info *fbi = info->par;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700717 struct resource *res;
718
719 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
720
Sascha Hauer772a9e632005-07-17 20:15:36 +0100721 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700722
723 unregister_framebuffer(info);
724
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100725 pdata = pdev->dev.platform_data;
726 if (pdata->exit)
727 pdata->exit(fbi->pdev);
728
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700729 fb_dealloc_cmap(&info->cmap);
730 kfree(info->pseudo_palette);
731 framebuffer_release(info);
732
Juergen Beisert72330b02008-12-16 11:44:07 +0100733 iounmap(fbi->regs);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700734 release_mem_region(res->start, res->end - res->start + 1);
Russell King3ae5eae2005-11-09 22:32:44 +0000735 platform_set_drvdata(pdev, NULL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700736
737 return 0;
738}
739
Russell King3ae5eae2005-11-09 22:32:44 +0000740void imxfb_shutdown(struct platform_device * dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700741{
Russell King3ae5eae2005-11-09 22:32:44 +0000742 struct fb_info *info = platform_get_drvdata(dev);
Sascha Hauer772a9e632005-07-17 20:15:36 +0100743 struct imxfb_info *fbi = info->par;
744 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700745}
746
Russell King3ae5eae2005-11-09 22:32:44 +0000747static struct platform_driver imxfb_driver = {
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700748 .suspend = imxfb_suspend,
749 .resume = imxfb_resume,
Juergen Beisert72330b02008-12-16 11:44:07 +0100750 .remove = __devexit_p(imxfb_remove),
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700751 .shutdown = imxfb_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000752 .driver = {
Juergen Beisert72330b02008-12-16 11:44:07 +0100753 .name = DRIVER_NAME,
Russell King3ae5eae2005-11-09 22:32:44 +0000754 },
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700755};
756
757int __init imxfb_init(void)
758{
Juergen Beisert72330b02008-12-16 11:44:07 +0100759 return platform_driver_probe(&imxfb_driver, imxfb_probe);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700760}
761
762static void __exit imxfb_cleanup(void)
763{
Russell King3ae5eae2005-11-09 22:32:44 +0000764 platform_driver_unregister(&imxfb_driver);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700765}
766
767module_init(imxfb_init);
768module_exit(imxfb_cleanup);
769
770MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
771MODULE_AUTHOR("Sascha Hauer, Pengutronix");
772MODULE_LICENSE("GPL");