blob: 1bccf5ad0c7da91b37a8363099d2dedc880b41b2 [file] [log] [blame]
Sascha Hauer7c2f891c2005-05-01 08:59:24 -07001/*
2 * linux/drivers/video/imxfb.c
3 *
4 * Freescale i.MX Frame Buffer device driver
5 *
6 * Copyright (C) 2004 Sascha Hauer, Pengutronix
7 * Based on acornfb.c Copyright (C) Russell King.
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
11 * more details.
12 *
13 * Please direct your questions and comments on this driver to the following
14 * email address:
15 *
16 * linux-arm-kernel@lists.arm.linux.org.uk
17 */
18
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070019
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070020#include <linux/module.h>
21#include <linux/kernel.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070022#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/interrupt.h>
25#include <linux/slab.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070026#include <linux/mm.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070027#include <linux/fb.h>
28#include <linux/delay.h>
29#include <linux/init.h>
30#include <linux/ioport.h>
31#include <linux/cpufreq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070033#include <linux/dma-mapping.h>
Juergen Beisert72330b02008-12-16 11:44:07 +010034#include <linux/io.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070035
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/imxfb.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070037
38/*
39 * Complain if VAR is out of range.
40 */
41#define DEBUG_VAR 1
42
43#include "imxfb.h"
44
Juergen Beisert72330b02008-12-16 11:44:07 +010045#define DRIVER_NAME "imx-fb"
46
47#define LCDC_SSA 0x00
48
49#define LCDC_SIZE 0x04
50#define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
51#define SIZE_YMAX(y) ((y) & 0x1ff)
52
53#define LCDC_VPW 0x08
54#define VPW_VPW(x) ((x) & 0x3ff)
55
56#define LCDC_CPOS 0x0C
57#define CPOS_CC1 (1<<31)
58#define CPOS_CC0 (1<<30)
59#define CPOS_OP (1<<28)
60#define CPOS_CXP(x) (((x) & 3ff) << 16)
61#define CPOS_CYP(y) ((y) & 0x1ff)
62
63#define LCDC_LCWHB 0x10
64#define LCWHB_BK_EN (1<<31)
65#define LCWHB_CW(w) (((w) & 0x1f) << 24)
66#define LCWHB_CH(h) (((h) & 0x1f) << 16)
67#define LCWHB_BD(x) ((x) & 0xff)
68
69#define LCDC_LCHCC 0x14
70#define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
71#define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
72#define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
73
74#define LCDC_PCR 0x18
75
76#define LCDC_HCR 0x1C
77#define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
78#define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
79#define HCR_H_WAIT_2(x) ((x) & 0xff)
80
81#define LCDC_VCR 0x20
82#define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
83#define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
84#define VCR_V_WAIT_2(x) ((x) & 0xff)
85
86#define LCDC_POS 0x24
87#define POS_POS(x) ((x) & 1f)
88
89#define LCDC_LSCR1 0x28
90/* bit fields in imxfb.h */
91
92#define LCDC_PWMR 0x2C
93/* bit fields in imxfb.h */
94
95#define LCDC_DMACR 0x30
96/* bit fields in imxfb.h */
97
98#define LCDC_RMCR 0x34
99#define RMCR_LCDC_EN (1<<1)
100#define RMCR_SELF_REF (1<<0)
101
102#define LCDC_LCDICR 0x38
103#define LCDICR_INT_SYN (1<<2)
104#define LCDICR_INT_CON (1)
105
106#define LCDC_LCDISR 0x40
107#define LCDISR_UDR_ERR (1<<3)
108#define LCDISR_ERR_RES (1<<2)
109#define LCDISR_EOF (1<<1)
110#define LCDISR_BOF (1<<0)
111
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700112static struct imxfb_rgb def_rgb_16 = {
113 .red = { .offset = 8, .length = 4, },
114 .green = { .offset = 4, .length = 4, },
115 .blue = { .offset = 0, .length = 4, },
116 .transp = { .offset = 0, .length = 0, },
117};
118
119static struct imxfb_rgb def_rgb_8 = {
120 .red = { .offset = 0, .length = 8, },
121 .green = { .offset = 0, .length = 8, },
122 .blue = { .offset = 0, .length = 8, },
123 .transp = { .offset = 0, .length = 0, },
124};
125
126static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info);
127
128static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
129{
130 chan &= 0xffff;
131 chan >>= 16 - bf->length;
132 return chan << bf->offset;
133}
134
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700135static int
136imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
137 u_int trans, struct fb_info *info)
138{
139 struct imxfb_info *fbi = info->par;
140 u_int val, ret = 1;
141
142#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
143 if (regno < fbi->palette_size) {
144 val = (CNVT_TOHW(red, 4) << 8) |
145 (CNVT_TOHW(green,4) << 4) |
146 CNVT_TOHW(blue, 4);
147
Juergen Beisert72330b02008-12-16 11:44:07 +0100148 writel(val, fbi->regs + 0x800 + (regno << 2));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700149 ret = 0;
150 }
151 return ret;
152}
153
154static int
155imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
156 u_int trans, struct fb_info *info)
157{
158 struct imxfb_info *fbi = info->par;
159 unsigned int val;
160 int ret = 1;
161
162 /*
163 * If inverse mode was selected, invert all the colours
164 * rather than the register number. The register number
165 * is what you poke into the framebuffer to produce the
166 * colour you requested.
167 */
168 if (fbi->cmap_inverse) {
169 red = 0xffff - red;
170 green = 0xffff - green;
171 blue = 0xffff - blue;
172 }
173
174 /*
175 * If greyscale is true, then we convert the RGB value
176 * to greyscale no mater what visual we are using.
177 */
178 if (info->var.grayscale)
179 red = green = blue = (19595 * red + 38470 * green +
180 7471 * blue) >> 16;
181
182 switch (info->fix.visual) {
183 case FB_VISUAL_TRUECOLOR:
184 /*
185 * 12 or 16-bit True Colour. We encode the RGB value
186 * according to the RGB bitfield information.
187 */
188 if (regno < 16) {
189 u32 *pal = info->pseudo_palette;
190
191 val = chan_to_field(red, &info->var.red);
192 val |= chan_to_field(green, &info->var.green);
193 val |= chan_to_field(blue, &info->var.blue);
194
195 pal[regno] = val;
196 ret = 0;
197 }
198 break;
199
200 case FB_VISUAL_STATIC_PSEUDOCOLOR:
201 case FB_VISUAL_PSEUDOCOLOR:
202 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
203 break;
204 }
205
206 return ret;
207}
208
209/*
210 * imxfb_check_var():
211 * Round up in the following order: bits_per_pixel, xres,
212 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
213 * bitfields, horizontal timing, vertical timing.
214 */
215static int
216imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
217{
218 struct imxfb_info *fbi = info->par;
219 int rgbidx;
220
221 if (var->xres < MIN_XRES)
222 var->xres = MIN_XRES;
223 if (var->yres < MIN_YRES)
224 var->yres = MIN_YRES;
225 if (var->xres > fbi->max_xres)
226 var->xres = fbi->max_xres;
227 if (var->yres > fbi->max_yres)
228 var->yres = fbi->max_yres;
229 var->xres_virtual = max(var->xres_virtual, var->xres);
230 var->yres_virtual = max(var->yres_virtual, var->yres);
231
232 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
233 switch (var->bits_per_pixel) {
234 case 16:
235 rgbidx = RGB_16;
236 break;
237 case 8:
238 rgbidx = RGB_8;
239 break;
240 default:
241 rgbidx = RGB_16;
242 }
243
244 /*
245 * Copy the RGB parameters for this display
246 * from the machine specific parameters.
247 */
248 var->red = fbi->rgb[rgbidx]->red;
249 var->green = fbi->rgb[rgbidx]->green;
250 var->blue = fbi->rgb[rgbidx]->blue;
251 var->transp = fbi->rgb[rgbidx]->transp;
252
253 pr_debug("RGBT length = %d:%d:%d:%d\n",
254 var->red.length, var->green.length, var->blue.length,
255 var->transp.length);
256
257 pr_debug("RGBT offset = %d:%d:%d:%d\n",
258 var->red.offset, var->green.offset, var->blue.offset,
259 var->transp.offset);
260
261 return 0;
262}
263
264/*
265 * imxfb_set_par():
266 * Set the user defined part of the display for the specified console
267 */
268static int imxfb_set_par(struct fb_info *info)
269{
270 struct imxfb_info *fbi = info->par;
271 struct fb_var_screeninfo *var = &info->var;
272
273 pr_debug("set_par\n");
274
275 if (var->bits_per_pixel == 16)
276 info->fix.visual = FB_VISUAL_TRUECOLOR;
277 else if (!fbi->cmap_static)
278 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
279 else {
280 /*
281 * Some people have weird ideas about wanting static
282 * pseudocolor maps. I suspect their user space
283 * applications are broken.
284 */
285 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
286 }
287
288 info->fix.line_length = var->xres_virtual *
289 var->bits_per_pixel / 8;
290 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
291
292 imxfb_activate_var(var, info);
293
294 return 0;
295}
296
297static void imxfb_enable_controller(struct imxfb_info *fbi)
298{
299 pr_debug("Enabling LCD controller\n");
300
301 /* initialize LCDC */
Juergen Beisert72330b02008-12-16 11:44:07 +0100302 writel(readl(fbi->regs + LCDC_RMCR) & ~RMCR_LCDC_EN,
303 fbi->regs + LCDC_RMCR); /* just to be safe... */
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700304
Juergen Beisert72330b02008-12-16 11:44:07 +0100305 writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
306
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700307 /* physical screen start address */
Juergen Beisert72330b02008-12-16 11:44:07 +0100308 writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
309 fbi->regs + LCDC_VPW);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700310
Juergen Beisert72330b02008-12-16 11:44:07 +0100311 /* panning offset 0 (0 pixel offset) */
312 writel(0x00000000, fbi->regs + LCDC_POS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700313
314 /* disable hardware cursor */
Juergen Beisert72330b02008-12-16 11:44:07 +0100315 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
316 fbi->regs + LCDC_CPOS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700317
Juergen Beisert72330b02008-12-16 11:44:07 +0100318 writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700319
320 if(fbi->backlight_power)
321 fbi->backlight_power(1);
322 if(fbi->lcd_power)
323 fbi->lcd_power(1);
324}
325
326static void imxfb_disable_controller(struct imxfb_info *fbi)
327{
328 pr_debug("Disabling LCD controller\n");
329
330 if(fbi->backlight_power)
331 fbi->backlight_power(0);
332 if(fbi->lcd_power)
333 fbi->lcd_power(0);
334
Juergen Beisert72330b02008-12-16 11:44:07 +0100335 writel(0, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700336}
337
338static int imxfb_blank(int blank, struct fb_info *info)
339{
340 struct imxfb_info *fbi = info->par;
341
342 pr_debug("imxfb_blank: blank=%d\n", blank);
343
344 switch (blank) {
345 case FB_BLANK_POWERDOWN:
346 case FB_BLANK_VSYNC_SUSPEND:
347 case FB_BLANK_HSYNC_SUSPEND:
348 case FB_BLANK_NORMAL:
349 imxfb_disable_controller(fbi);
350 break;
351
352 case FB_BLANK_UNBLANK:
353 imxfb_enable_controller(fbi);
354 break;
355 }
356 return 0;
357}
358
359static struct fb_ops imxfb_ops = {
360 .owner = THIS_MODULE,
361 .fb_check_var = imxfb_check_var,
362 .fb_set_par = imxfb_set_par,
363 .fb_setcolreg = imxfb_setcolreg,
364 .fb_fillrect = cfb_fillrect,
365 .fb_copyarea = cfb_copyarea,
366 .fb_imageblit = cfb_imageblit,
367 .fb_blank = imxfb_blank,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700368};
369
370/*
371 * imxfb_activate_var():
372 * Configures LCD Controller based on entries in var parameter. Settings are
373 * only written to the controller if changes were made.
374 */
375static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
376{
377 struct imxfb_info *fbi = info->par;
378 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
379 var->xres, var->hsync_len,
380 var->left_margin, var->right_margin);
381 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
382 var->yres, var->vsync_len,
383 var->upper_margin, var->lower_margin);
384
385#if DEBUG_VAR
386 if (var->xres < 16 || var->xres > 1024)
387 printk(KERN_ERR "%s: invalid xres %d\n",
388 info->fix.id, var->xres);
389 if (var->hsync_len < 1 || var->hsync_len > 64)
390 printk(KERN_ERR "%s: invalid hsync_len %d\n",
391 info->fix.id, var->hsync_len);
392 if (var->left_margin > 255)
393 printk(KERN_ERR "%s: invalid left_margin %d\n",
394 info->fix.id, var->left_margin);
395 if (var->right_margin > 255)
396 printk(KERN_ERR "%s: invalid right_margin %d\n",
397 info->fix.id, var->right_margin);
398 if (var->yres < 1 || var->yres > 511)
399 printk(KERN_ERR "%s: invalid yres %d\n",
400 info->fix.id, var->yres);
401 if (var->vsync_len > 100)
402 printk(KERN_ERR "%s: invalid vsync_len %d\n",
403 info->fix.id, var->vsync_len);
404 if (var->upper_margin > 63)
405 printk(KERN_ERR "%s: invalid upper_margin %d\n",
406 info->fix.id, var->upper_margin);
407 if (var->lower_margin > 255)
408 printk(KERN_ERR "%s: invalid lower_margin %d\n",
409 info->fix.id, var->lower_margin);
410#endif
411
Juergen Beisert72330b02008-12-16 11:44:07 +0100412 writel(HCR_H_WIDTH(var->hsync_len) |
413 HCR_H_WAIT_1(var->left_margin) |
414 HCR_H_WAIT_2(var->right_margin),
415 fbi->regs + LCDC_HCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700416
Juergen Beisert72330b02008-12-16 11:44:07 +0100417 writel(VCR_V_WIDTH(var->vsync_len) |
418 VCR_V_WAIT_1(var->upper_margin) |
419 VCR_V_WAIT_2(var->lower_margin),
420 fbi->regs + LCDC_VCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700421
Juergen Beisert72330b02008-12-16 11:44:07 +0100422 writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
423 fbi->regs + LCDC_SIZE);
424 writel(fbi->pcr, fbi->regs + LCDC_PCR);
425 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
426 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
427 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700428
429 return 0;
430}
431
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700432#ifdef CONFIG_PM
433/*
434 * Power management hooks. Note that we won't be called from IRQ context,
435 * unlike the blank functions above, so we may sleep.
436 */
Russell King3ae5eae2005-11-09 22:32:44 +0000437static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700438{
Russell King3ae5eae2005-11-09 22:32:44 +0000439 struct imxfb_info *fbi = platform_get_drvdata(dev);
Harvey Harrison5ae12172008-04-28 02:15:47 -0700440 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700441
Russell King9480e302005-10-28 09:52:56 -0700442 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700443 return 0;
444}
445
Russell King3ae5eae2005-11-09 22:32:44 +0000446static int imxfb_resume(struct platform_device *dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700447{
Russell King3ae5eae2005-11-09 22:32:44 +0000448 struct imxfb_info *fbi = platform_get_drvdata(dev);
Harvey Harrison5ae12172008-04-28 02:15:47 -0700449 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700450
Russell King9480e302005-10-28 09:52:56 -0700451 imxfb_enable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700452 return 0;
453}
454#else
455#define imxfb_suspend NULL
456#define imxfb_resume NULL
457#endif
458
Juergen Beisert72330b02008-12-16 11:44:07 +0100459static int __init imxfb_init_fbinfo(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700460{
Juergen Beisert72330b02008-12-16 11:44:07 +0100461 struct imxfb_mach_info *inf = pdev->dev.platform_data;
462 struct fb_info *info = dev_get_drvdata(&pdev->dev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700463 struct imxfb_info *fbi = info->par;
464
Harvey Harrison5ae12172008-04-28 02:15:47 -0700465 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700466
467 info->pseudo_palette = kmalloc( sizeof(u32) * 16, GFP_KERNEL);
468 if (!info->pseudo_palette)
469 return -ENOMEM;
470
471 memset(fbi, 0, sizeof(struct imxfb_info));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700472
473 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
474
475 info->fix.type = FB_TYPE_PACKED_PIXELS;
476 info->fix.type_aux = 0;
477 info->fix.xpanstep = 0;
478 info->fix.ypanstep = 0;
479 info->fix.ywrapstep = 0;
480 info->fix.accel = FB_ACCEL_NONE;
481
482 info->var.nonstd = 0;
483 info->var.activate = FB_ACTIVATE_NOW;
484 info->var.height = -1;
485 info->var.width = -1;
486 info->var.accel_flags = 0;
487 info->var.vmode = FB_VMODE_NONINTERLACED;
488
489 info->fbops = &imxfb_ops;
Pavel Pisa9da505d2007-10-16 01:29:44 -0700490 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_READS_FAST;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700491
492 fbi->rgb[RGB_16] = &def_rgb_16;
493 fbi->rgb[RGB_8] = &def_rgb_8;
494
495 fbi->max_xres = inf->xres;
496 info->var.xres = inf->xres;
497 info->var.xres_virtual = inf->xres;
498 fbi->max_yres = inf->yres;
499 info->var.yres = inf->yres;
500 info->var.yres_virtual = inf->yres;
501 fbi->max_bpp = inf->bpp;
502 info->var.bits_per_pixel = inf->bpp;
Pavel Pisa9da505d2007-10-16 01:29:44 -0700503 info->var.nonstd = inf->nonstd;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700504 info->var.pixclock = inf->pixclock;
505 info->var.hsync_len = inf->hsync_len;
506 info->var.left_margin = inf->left_margin;
507 info->var.right_margin = inf->right_margin;
508 info->var.vsync_len = inf->vsync_len;
509 info->var.upper_margin = inf->upper_margin;
510 info->var.lower_margin = inf->lower_margin;
511 info->var.sync = inf->sync;
512 info->var.grayscale = inf->cmap_greyscale;
513 fbi->cmap_inverse = inf->cmap_inverse;
Sascha Hauer90e16dd2007-05-23 13:57:51 -0700514 fbi->cmap_static = inf->cmap_static;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700515 fbi->pcr = inf->pcr;
516 fbi->lscr1 = inf->lscr1;
Sascha Hauer772a9e62005-07-17 20:15:36 +0100517 fbi->dmacr = inf->dmacr;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700518 fbi->pwmr = inf->pwmr;
519 fbi->lcd_power = inf->lcd_power;
520 fbi->backlight_power = inf->backlight_power;
521 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
522 fbi->max_bpp / 8;
523
524 return 0;
525}
526
Russell King3ae5eae2005-11-09 22:32:44 +0000527static int __init imxfb_probe(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700528{
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700529 struct imxfb_info *fbi;
530 struct fb_info *info;
531 struct imxfb_mach_info *inf;
532 struct resource *res;
533 int ret;
534
535 printk("i.MX Framebuffer driver\n");
536
537 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
538 if(!res)
539 return -ENODEV;
540
Russell King3ae5eae2005-11-09 22:32:44 +0000541 inf = pdev->dev.platform_data;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700542 if(!inf) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000543 dev_err(&pdev->dev,"No platform_data available\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700544 return -ENOMEM;
545 }
546
Russell King3ae5eae2005-11-09 22:32:44 +0000547 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700548 if(!info)
549 return -ENOMEM;
550
551 fbi = info->par;
552
Russell King3ae5eae2005-11-09 22:32:44 +0000553 platform_set_drvdata(pdev, info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700554
Juergen Beisert72330b02008-12-16 11:44:07 +0100555 ret = imxfb_init_fbinfo(pdev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700556 if( ret < 0 )
557 goto failed_init;
558
Juergen Beisert72330b02008-12-16 11:44:07 +0100559 res = request_mem_region(res->start, resource_size(res),
560 DRIVER_NAME);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700561 if (!res) {
562 ret = -EBUSY;
Juergen Beisert72330b02008-12-16 11:44:07 +0100563 goto failed_req;
564 }
565
566 fbi->regs = ioremap(res->start, resource_size(res));
567 if (fbi->regs == NULL) {
568 printk(KERN_ERR"Cannot map frame buffer registers\n");
569 goto failed_ioremap;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700570 }
571
572 if (!inf->fixed_screen_cpu) {
Juergen Beisert72330b02008-12-16 11:44:07 +0100573 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
574 fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
575 fbi->map_size, &fbi->map_dma, GFP_KERNEL);
576
577 if (!fbi->map_cpu) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000578 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700579 ret = -ENOMEM;
580 goto failed_map;
581 }
Juergen Beisert72330b02008-12-16 11:44:07 +0100582
583 info->screen_base = fbi->map_cpu;
584 fbi->screen_cpu = fbi->map_cpu;
585 fbi->screen_dma = fbi->map_dma;
586 info->fix.smem_start = fbi->screen_dma;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700587 } else {
588 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
589 fbi->map_cpu = inf->fixed_screen_cpu;
590 fbi->map_dma = inf->fixed_screen_dma;
591 info->screen_base = fbi->map_cpu;
592 fbi->screen_cpu = fbi->map_cpu;
593 fbi->screen_dma = fbi->map_dma;
594 info->fix.smem_start = fbi->screen_dma;
595 }
596
597 /*
598 * This makes sure that our colour bitfield
599 * descriptors are correctly initialised.
600 */
601 imxfb_check_var(&info->var, info);
602
603 ret = fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
604 if (ret < 0)
605 goto failed_cmap;
606
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700607 imxfb_set_par(info);
608 ret = register_framebuffer(info);
609 if (ret < 0) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000610 dev_err(&pdev->dev, "failed to register framebuffer\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700611 goto failed_register;
612 }
613
614 imxfb_enable_controller(fbi);
615
616 return 0;
617
618failed_register:
619 fb_dealloc_cmap(&info->cmap);
620failed_cmap:
621 if (!inf->fixed_screen_cpu)
Russell King3ae5eae2005-11-09 22:32:44 +0000622 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
Juergen Beisert72330b02008-12-16 11:44:07 +0100623 fbi->map_dma);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700624failed_map:
Juergen Beisert72330b02008-12-16 11:44:07 +0100625 iounmap(fbi->regs);
626failed_ioremap:
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700627 release_mem_region(res->start, res->end - res->start);
Juergen Beisert72330b02008-12-16 11:44:07 +0100628failed_req:
629 kfree(info->pseudo_palette);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700630failed_init:
Russell King3ae5eae2005-11-09 22:32:44 +0000631 platform_set_drvdata(pdev, NULL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700632 framebuffer_release(info);
633 return ret;
634}
635
Juergen Beisert72330b02008-12-16 11:44:07 +0100636static int __devexit imxfb_remove(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700637{
Russell King3ae5eae2005-11-09 22:32:44 +0000638 struct fb_info *info = platform_get_drvdata(pdev);
Sascha Hauer772a9e62005-07-17 20:15:36 +0100639 struct imxfb_info *fbi = info->par;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700640 struct resource *res;
641
642 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
643
Sascha Hauer772a9e62005-07-17 20:15:36 +0100644 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700645
646 unregister_framebuffer(info);
647
648 fb_dealloc_cmap(&info->cmap);
649 kfree(info->pseudo_palette);
650 framebuffer_release(info);
651
Juergen Beisert72330b02008-12-16 11:44:07 +0100652 iounmap(fbi->regs);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700653 release_mem_region(res->start, res->end - res->start + 1);
Russell King3ae5eae2005-11-09 22:32:44 +0000654 platform_set_drvdata(pdev, NULL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700655
656 return 0;
657}
658
Russell King3ae5eae2005-11-09 22:32:44 +0000659void imxfb_shutdown(struct platform_device * dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700660{
Russell King3ae5eae2005-11-09 22:32:44 +0000661 struct fb_info *info = platform_get_drvdata(dev);
Sascha Hauer772a9e62005-07-17 20:15:36 +0100662 struct imxfb_info *fbi = info->par;
663 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700664}
665
Russell King3ae5eae2005-11-09 22:32:44 +0000666static struct platform_driver imxfb_driver = {
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700667 .suspend = imxfb_suspend,
668 .resume = imxfb_resume,
Juergen Beisert72330b02008-12-16 11:44:07 +0100669 .remove = __devexit_p(imxfb_remove),
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700670 .shutdown = imxfb_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000671 .driver = {
Juergen Beisert72330b02008-12-16 11:44:07 +0100672 .name = DRIVER_NAME,
Russell King3ae5eae2005-11-09 22:32:44 +0000673 },
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700674};
675
676int __init imxfb_init(void)
677{
Juergen Beisert72330b02008-12-16 11:44:07 +0100678 return platform_driver_probe(&imxfb_driver, imxfb_probe);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700679}
680
681static void __exit imxfb_cleanup(void)
682{
Russell King3ae5eae2005-11-09 22:32:44 +0000683 platform_driver_unregister(&imxfb_driver);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700684}
685
686module_init(imxfb_init);
687module_exit(imxfb_cleanup);
688
689MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
690MODULE_AUTHOR("Sascha Hauer, Pengutronix");
691MODULE_LICENSE("GPL");