blob: 330857a8b3189689ba3a6f6439bd28ce1e08417e [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>
Sascha Hauerf909ef62009-01-15 15:21:00 +010029#include <linux/clk.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010030#include <linux/platform_device.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070031#include <linux/dma-mapping.h>
Juergen Beisert72330b02008-12-16 11:44:07 +010032#include <linux/io.h>
Sascha Hauerf909ef62009-01-15 15:21:00 +010033#include <linux/math64.h>
Sascha Hauer7c2f891c2005-05-01 08:59:24 -070034
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/imxfb.h>
Sascha Hauerf497d012009-03-18 11:29:31 +010036#include <mach/hardware.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
Juergen Beisert72330b02008-12-16 11:44:07 +010043#define DRIVER_NAME "imx-fb"
44
45#define LCDC_SSA 0x00
46
47#define LCDC_SIZE 0x04
48#define SIZE_XMAX(x) ((((x) >> 4) & 0x3f) << 20)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010049
50#ifdef CONFIG_ARCH_MX1
Juergen Beisert72330b02008-12-16 11:44:07 +010051#define SIZE_YMAX(y) ((y) & 0x1ff)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010052#else
53#define SIZE_YMAX(y) ((y) & 0x3ff)
54#endif
Juergen Beisert72330b02008-12-16 11:44:07 +010055
56#define LCDC_VPW 0x08
57#define VPW_VPW(x) ((x) & 0x3ff)
58
59#define LCDC_CPOS 0x0C
60#define CPOS_CC1 (1<<31)
61#define CPOS_CC0 (1<<30)
62#define CPOS_OP (1<<28)
63#define CPOS_CXP(x) (((x) & 3ff) << 16)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010064
65#ifdef CONFIG_ARCH_MX1
Juergen Beisert72330b02008-12-16 11:44:07 +010066#define CPOS_CYP(y) ((y) & 0x1ff)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010067#else
68#define CPOS_CYP(y) ((y) & 0x3ff)
69#endif
Juergen Beisert72330b02008-12-16 11:44:07 +010070
71#define LCDC_LCWHB 0x10
72#define LCWHB_BK_EN (1<<31)
73#define LCWHB_CW(w) (((w) & 0x1f) << 24)
74#define LCWHB_CH(h) (((h) & 0x1f) << 16)
75#define LCWHB_BD(x) ((x) & 0xff)
76
77#define LCDC_LCHCC 0x14
Sascha Hauer1d0f9872009-01-26 17:29:10 +010078
79#ifdef CONFIG_ARCH_MX1
Juergen Beisert72330b02008-12-16 11:44:07 +010080#define LCHCC_CUR_COL_R(r) (((r) & 0x1f) << 11)
81#define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 5)
82#define LCHCC_CUR_COL_B(b) ((b) & 0x1f)
Sascha Hauer1d0f9872009-01-26 17:29:10 +010083#else
84#define LCHCC_CUR_COL_R(r) (((r) & 0x3f) << 12)
85#define LCHCC_CUR_COL_G(g) (((g) & 0x3f) << 6)
86#define LCHCC_CUR_COL_B(b) ((b) & 0x3f)
87#endif
Juergen Beisert72330b02008-12-16 11:44:07 +010088
89#define LCDC_PCR 0x18
90
91#define LCDC_HCR 0x1C
92#define HCR_H_WIDTH(x) (((x) & 0x3f) << 26)
93#define HCR_H_WAIT_1(x) (((x) & 0xff) << 8)
94#define HCR_H_WAIT_2(x) ((x) & 0xff)
95
96#define LCDC_VCR 0x20
97#define VCR_V_WIDTH(x) (((x) & 0x3f) << 26)
98#define VCR_V_WAIT_1(x) (((x) & 0xff) << 8)
99#define VCR_V_WAIT_2(x) ((x) & 0xff)
100
101#define LCDC_POS 0x24
102#define POS_POS(x) ((x) & 1f)
103
104#define LCDC_LSCR1 0x28
105/* bit fields in imxfb.h */
106
107#define LCDC_PWMR 0x2C
108/* bit fields in imxfb.h */
109
110#define LCDC_DMACR 0x30
111/* bit fields in imxfb.h */
112
113#define LCDC_RMCR 0x34
Sascha Hauer1d0f9872009-01-26 17:29:10 +0100114
115#ifdef CONFIG_ARCH_MX1
Juergen Beisert72330b02008-12-16 11:44:07 +0100116#define RMCR_LCDC_EN (1<<1)
Sascha Hauer1d0f9872009-01-26 17:29:10 +0100117#else
118#define RMCR_LCDC_EN 0
119#endif
120
Juergen Beisert72330b02008-12-16 11:44:07 +0100121#define RMCR_SELF_REF (1<<0)
122
123#define LCDC_LCDICR 0x38
124#define LCDICR_INT_SYN (1<<2)
125#define LCDICR_INT_CON (1)
126
127#define LCDC_LCDISR 0x40
128#define LCDISR_UDR_ERR (1<<3)
129#define LCDISR_ERR_RES (1<<2)
130#define LCDISR_EOF (1<<1)
131#define LCDISR_BOF (1<<0)
132
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100133/*
134 * These are the bitfields for each
135 * display depth that we support.
136 */
137struct imxfb_rgb {
138 struct fb_bitfield red;
139 struct fb_bitfield green;
140 struct fb_bitfield blue;
141 struct fb_bitfield transp;
142};
143
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100144struct imxfb_info {
145 struct platform_device *pdev;
146 void __iomem *regs;
Sascha Hauerf909ef62009-01-15 15:21:00 +0100147 struct clk *clk;
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100148
Sascha Hauer24b9baf2008-12-16 11:44:08 +0100149 u_int max_bpp;
150 u_int max_xres;
151 u_int max_yres;
152
153 /*
154 * These are the addresses we mapped
155 * the framebuffer memory region to.
156 */
157 dma_addr_t map_dma;
158 u_char *map_cpu;
159 u_int map_size;
160
161 u_char *screen_cpu;
162 dma_addr_t screen_dma;
163 u_int palette_size;
164
165 dma_addr_t dbar1;
166 dma_addr_t dbar2;
167
168 u_int pcr;
169 u_int pwmr;
170 u_int lscr1;
171 u_int dmacr;
172 u_int cmap_inverse:1,
173 cmap_static:1,
174 unused:30;
175
176 void (*lcd_power)(int);
177 void (*backlight_power)(int);
178};
179
180#define IMX_NAME "IMX"
181
182/*
183 * Minimum X and Y resolutions
184 */
185#define MIN_XRES 64
186#define MIN_YRES 64
187
Sascha Hauer15122222009-01-26 17:31:02 +0100188/* Actually this really is 18bit support, the lowest 2 bits of each colour
189 * are unused in hardware. We claim to have 24bit support to make software
190 * like X work, which does not support 18bit.
191 */
192static struct imxfb_rgb def_rgb_18 = {
193 .red = {.offset = 16, .length = 8,},
194 .green = {.offset = 8, .length = 8,},
195 .blue = {.offset = 0, .length = 8,},
196 .transp = {.offset = 0, .length = 0,},
197};
198
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100199static struct imxfb_rgb def_rgb_16_tft = {
200 .red = {.offset = 11, .length = 5,},
201 .green = {.offset = 5, .length = 6,},
202 .blue = {.offset = 0, .length = 5,},
203 .transp = {.offset = 0, .length = 0,},
204};
205
206static struct imxfb_rgb def_rgb_16_stn = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100207 .red = {.offset = 8, .length = 4,},
208 .green = {.offset = 4, .length = 4,},
209 .blue = {.offset = 0, .length = 4,},
210 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700211};
212
213static struct imxfb_rgb def_rgb_8 = {
Sascha Hauer66c87192008-12-16 11:44:08 +0100214 .red = {.offset = 0, .length = 8,},
215 .green = {.offset = 0, .length = 8,},
216 .blue = {.offset = 0, .length = 8,},
217 .transp = {.offset = 0, .length = 0,},
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700218};
219
Sascha Hauer66c87192008-12-16 11:44:08 +0100220static int imxfb_activate_var(struct fb_var_screeninfo *var,
221 struct fb_info *info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700222
223static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
224{
225 chan &= 0xffff;
226 chan >>= 16 - bf->length;
227 return chan << bf->offset;
228}
229
Sascha Hauer66c87192008-12-16 11:44:08 +0100230static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
231 u_int trans, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700232{
233 struct imxfb_info *fbi = info->par;
234 u_int val, ret = 1;
235
236#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
237 if (regno < fbi->palette_size) {
238 val = (CNVT_TOHW(red, 4) << 8) |
239 (CNVT_TOHW(green,4) << 4) |
240 CNVT_TOHW(blue, 4);
241
Juergen Beisert72330b02008-12-16 11:44:07 +0100242 writel(val, fbi->regs + 0x800 + (regno << 2));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700243 ret = 0;
244 }
245 return ret;
246}
247
Sascha Hauer66c87192008-12-16 11:44:08 +0100248static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700249 u_int trans, struct fb_info *info)
250{
251 struct imxfb_info *fbi = info->par;
252 unsigned int val;
253 int ret = 1;
254
255 /*
256 * If inverse mode was selected, invert all the colours
257 * rather than the register number. The register number
258 * is what you poke into the framebuffer to produce the
259 * colour you requested.
260 */
261 if (fbi->cmap_inverse) {
262 red = 0xffff - red;
263 green = 0xffff - green;
264 blue = 0xffff - blue;
265 }
266
267 /*
268 * If greyscale is true, then we convert the RGB value
269 * to greyscale no mater what visual we are using.
270 */
271 if (info->var.grayscale)
272 red = green = blue = (19595 * red + 38470 * green +
273 7471 * blue) >> 16;
274
275 switch (info->fix.visual) {
276 case FB_VISUAL_TRUECOLOR:
277 /*
278 * 12 or 16-bit True Colour. We encode the RGB value
279 * according to the RGB bitfield information.
280 */
281 if (regno < 16) {
282 u32 *pal = info->pseudo_palette;
283
284 val = chan_to_field(red, &info->var.red);
285 val |= chan_to_field(green, &info->var.green);
286 val |= chan_to_field(blue, &info->var.blue);
287
288 pal[regno] = val;
289 ret = 0;
290 }
291 break;
292
293 case FB_VISUAL_STATIC_PSEUDOCOLOR:
294 case FB_VISUAL_PSEUDOCOLOR:
295 ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
296 break;
297 }
298
299 return ret;
300}
301
302/*
303 * imxfb_check_var():
304 * Round up in the following order: bits_per_pixel, xres,
305 * yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
306 * bitfields, horizontal timing, vertical timing.
307 */
Sascha Hauer66c87192008-12-16 11:44:08 +0100308static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700309{
310 struct imxfb_info *fbi = info->par;
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100311 struct imxfb_rgb *rgb;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700312
313 if (var->xres < MIN_XRES)
314 var->xres = MIN_XRES;
315 if (var->yres < MIN_YRES)
316 var->yres = MIN_YRES;
317 if (var->xres > fbi->max_xres)
318 var->xres = fbi->max_xres;
319 if (var->yres > fbi->max_yres)
320 var->yres = fbi->max_yres;
321 var->xres_virtual = max(var->xres_virtual, var->xres);
322 var->yres_virtual = max(var->yres_virtual, var->yres);
323
324 pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
325 switch (var->bits_per_pixel) {
Sascha Hauer15122222009-01-26 17:31:02 +0100326 case 32:
327 rgb = &def_rgb_18;
328 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700329 case 16:
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100330 default:
Sascha Hauer4d1e4e52009-03-26 12:38:26 +0100331 if (fbi->pcr & PCR_TFT)
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100332 rgb = &def_rgb_16_tft;
333 else
334 rgb = &def_rgb_16_stn;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700335 break;
336 case 8:
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100337 rgb = &def_rgb_8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700338 break;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700339 }
340
341 /*
342 * Copy the RGB parameters for this display
343 * from the machine specific parameters.
344 */
Sascha Hauer80eee6b2008-12-16 11:44:09 +0100345 var->red = rgb->red;
346 var->green = rgb->green;
347 var->blue = rgb->blue;
348 var->transp = rgb->transp;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700349
350 pr_debug("RGBT length = %d:%d:%d:%d\n",
351 var->red.length, var->green.length, var->blue.length,
352 var->transp.length);
353
354 pr_debug("RGBT offset = %d:%d:%d:%d\n",
355 var->red.offset, var->green.offset, var->blue.offset,
356 var->transp.offset);
357
358 return 0;
359}
360
361/*
362 * imxfb_set_par():
363 * Set the user defined part of the display for the specified console
364 */
365static int imxfb_set_par(struct fb_info *info)
366{
367 struct imxfb_info *fbi = info->par;
368 struct fb_var_screeninfo *var = &info->var;
369
Sascha Hauer15122222009-01-26 17:31:02 +0100370 if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700371 info->fix.visual = FB_VISUAL_TRUECOLOR;
372 else if (!fbi->cmap_static)
373 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
374 else {
375 /*
376 * Some people have weird ideas about wanting static
377 * pseudocolor maps. I suspect their user space
378 * applications are broken.
379 */
380 info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
381 }
382
Sascha Hauer66c87192008-12-16 11:44:08 +0100383 info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700384 fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;
385
386 imxfb_activate_var(var, info);
387
388 return 0;
389}
390
391static void imxfb_enable_controller(struct imxfb_info *fbi)
392{
393 pr_debug("Enabling LCD controller\n");
394
Juergen Beisert72330b02008-12-16 11:44:07 +0100395 writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
396
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700397 /* physical screen start address */
Juergen Beisert72330b02008-12-16 11:44:07 +0100398 writel(VPW_VPW(fbi->max_xres * fbi->max_bpp / 8 / 4),
399 fbi->regs + LCDC_VPW);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700400
Juergen Beisert72330b02008-12-16 11:44:07 +0100401 /* panning offset 0 (0 pixel offset) */
402 writel(0x00000000, fbi->regs + LCDC_POS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700403
404 /* disable hardware cursor */
Juergen Beisert72330b02008-12-16 11:44:07 +0100405 writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
406 fbi->regs + LCDC_CPOS);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700407
Juergen Beisert72330b02008-12-16 11:44:07 +0100408 writel(RMCR_LCDC_EN, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700409
Sascha Hauerf909ef62009-01-15 15:21:00 +0100410 clk_enable(fbi->clk);
411
Sascha Hauer66c87192008-12-16 11:44:08 +0100412 if (fbi->backlight_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700413 fbi->backlight_power(1);
Sascha Hauer66c87192008-12-16 11:44:08 +0100414 if (fbi->lcd_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700415 fbi->lcd_power(1);
416}
417
418static void imxfb_disable_controller(struct imxfb_info *fbi)
419{
420 pr_debug("Disabling LCD controller\n");
421
Sascha Hauer66c87192008-12-16 11:44:08 +0100422 if (fbi->backlight_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700423 fbi->backlight_power(0);
Sascha Hauer66c87192008-12-16 11:44:08 +0100424 if (fbi->lcd_power)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700425 fbi->lcd_power(0);
426
Sascha Hauerf909ef62009-01-15 15:21:00 +0100427 clk_disable(fbi->clk);
428
Juergen Beisert72330b02008-12-16 11:44:07 +0100429 writel(0, fbi->regs + LCDC_RMCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700430}
431
432static int imxfb_blank(int blank, struct fb_info *info)
433{
434 struct imxfb_info *fbi = info->par;
435
436 pr_debug("imxfb_blank: blank=%d\n", blank);
437
438 switch (blank) {
439 case FB_BLANK_POWERDOWN:
440 case FB_BLANK_VSYNC_SUSPEND:
441 case FB_BLANK_HSYNC_SUSPEND:
442 case FB_BLANK_NORMAL:
443 imxfb_disable_controller(fbi);
444 break;
445
446 case FB_BLANK_UNBLANK:
447 imxfb_enable_controller(fbi);
448 break;
449 }
450 return 0;
451}
452
453static struct fb_ops imxfb_ops = {
454 .owner = THIS_MODULE,
455 .fb_check_var = imxfb_check_var,
456 .fb_set_par = imxfb_set_par,
457 .fb_setcolreg = imxfb_setcolreg,
458 .fb_fillrect = cfb_fillrect,
459 .fb_copyarea = cfb_copyarea,
460 .fb_imageblit = cfb_imageblit,
461 .fb_blank = imxfb_blank,
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700462};
463
464/*
465 * imxfb_activate_var():
466 * Configures LCD Controller based on entries in var parameter. Settings are
467 * only written to the controller if changes were made.
468 */
469static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
470{
471 struct imxfb_info *fbi = info->par;
Sascha Hauerf909ef62009-01-15 15:21:00 +0100472 unsigned int pcr, lcd_clk;
473 unsigned long long tmp;
474
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700475 pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
476 var->xres, var->hsync_len,
477 var->left_margin, var->right_margin);
478 pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
479 var->yres, var->vsync_len,
480 var->upper_margin, var->lower_margin);
481
482#if DEBUG_VAR
483 if (var->xres < 16 || var->xres > 1024)
484 printk(KERN_ERR "%s: invalid xres %d\n",
485 info->fix.id, var->xres);
486 if (var->hsync_len < 1 || var->hsync_len > 64)
487 printk(KERN_ERR "%s: invalid hsync_len %d\n",
488 info->fix.id, var->hsync_len);
489 if (var->left_margin > 255)
490 printk(KERN_ERR "%s: invalid left_margin %d\n",
491 info->fix.id, var->left_margin);
492 if (var->right_margin > 255)
493 printk(KERN_ERR "%s: invalid right_margin %d\n",
494 info->fix.id, var->right_margin);
495 if (var->yres < 1 || var->yres > 511)
496 printk(KERN_ERR "%s: invalid yres %d\n",
497 info->fix.id, var->yres);
498 if (var->vsync_len > 100)
499 printk(KERN_ERR "%s: invalid vsync_len %d\n",
500 info->fix.id, var->vsync_len);
501 if (var->upper_margin > 63)
502 printk(KERN_ERR "%s: invalid upper_margin %d\n",
503 info->fix.id, var->upper_margin);
504 if (var->lower_margin > 255)
505 printk(KERN_ERR "%s: invalid lower_margin %d\n",
506 info->fix.id, var->lower_margin);
507#endif
508
Sascha Hauer7e8549b2009-02-14 16:29:38 +0100509 writel(HCR_H_WIDTH(var->hsync_len - 1) |
510 HCR_H_WAIT_1(var->right_margin - 1) |
511 HCR_H_WAIT_2(var->left_margin - 3),
Juergen Beisert72330b02008-12-16 11:44:07 +0100512 fbi->regs + LCDC_HCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700513
Juergen Beisert72330b02008-12-16 11:44:07 +0100514 writel(VCR_V_WIDTH(var->vsync_len) |
Sascha Hauerd6ed5752008-12-16 11:44:08 +0100515 VCR_V_WAIT_1(var->lower_margin) |
516 VCR_V_WAIT_2(var->upper_margin),
Juergen Beisert72330b02008-12-16 11:44:07 +0100517 fbi->regs + LCDC_VCR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700518
Juergen Beisert72330b02008-12-16 11:44:07 +0100519 writel(SIZE_XMAX(var->xres) | SIZE_YMAX(var->yres),
520 fbi->regs + LCDC_SIZE);
Sascha Hauerf909ef62009-01-15 15:21:00 +0100521
522 lcd_clk = clk_get_rate(fbi->clk);
523 tmp = var->pixclock * (unsigned long long)lcd_clk;
524 do_div(tmp, 1000000);
525 if (do_div(tmp, 1000000) > 500000)
526 tmp++;
527 pcr = (unsigned int)tmp;
528 if (--pcr > 0x3F) {
529 pcr = 0x3F;
530 printk(KERN_WARNING "Must limit pixel clock to %uHz\n",
531 lcd_clk / pcr);
532 }
533
Sascha Hauerf497d012009-03-18 11:29:31 +0100534 switch (var->bits_per_pixel) {
535 case 32:
536 pcr |= PCR_BPIX_18;
537 break;
538 case 16:
539 default:
540 if (cpu_is_mx1())
541 pcr |= PCR_BPIX_12;
542 else
543 pcr |= PCR_BPIX_16;
544 break;
545 case 8:
546 pcr |= PCR_BPIX_8;
547 break;
548 }
549
Sascha Hauerf909ef62009-01-15 15:21:00 +0100550 /* add sync polarities */
Sascha Hauerf497d012009-03-18 11:29:31 +0100551 pcr |= fbi->pcr & ~(0x3f | (7 << 25));
Sascha Hauerf909ef62009-01-15 15:21:00 +0100552
553 writel(pcr, fbi->regs + LCDC_PCR);
Juergen Beisert72330b02008-12-16 11:44:07 +0100554 writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
555 writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
556 writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700557
558 return 0;
559}
560
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700561#ifdef CONFIG_PM
562/*
563 * Power management hooks. Note that we won't be called from IRQ context,
564 * unlike the blank functions above, so we may sleep.
565 */
Russell King3ae5eae2005-11-09 22:32:44 +0000566static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700567{
Russell King3ae5eae2005-11-09 22:32:44 +0000568 struct imxfb_info *fbi = platform_get_drvdata(dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100569
570 pr_debug("%s\n", __func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700571
Russell King9480e302005-10-28 09:52:56 -0700572 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700573 return 0;
574}
575
Russell King3ae5eae2005-11-09 22:32:44 +0000576static int imxfb_resume(struct platform_device *dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700577{
Russell King3ae5eae2005-11-09 22:32:44 +0000578 struct imxfb_info *fbi = platform_get_drvdata(dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100579
580 pr_debug("%s\n", __func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700581
Russell King9480e302005-10-28 09:52:56 -0700582 imxfb_enable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700583 return 0;
584}
585#else
586#define imxfb_suspend NULL
587#define imxfb_resume NULL
588#endif
589
Juergen Beisert72330b02008-12-16 11:44:07 +0100590static int __init imxfb_init_fbinfo(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700591{
Sascha Hauer27889272008-12-16 11:44:09 +0100592 struct imx_fb_platform_data *pdata = pdev->dev.platform_data;
Juergen Beisert72330b02008-12-16 11:44:07 +0100593 struct fb_info *info = dev_get_drvdata(&pdev->dev);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700594 struct imxfb_info *fbi = info->par;
595
Harvey Harrison5ae12172008-04-28 02:15:47 -0700596 pr_debug("%s\n",__func__);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700597
Sascha Hauer66c87192008-12-16 11:44:08 +0100598 info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700599 if (!info->pseudo_palette)
600 return -ENOMEM;
601
602 memset(fbi, 0, sizeof(struct imxfb_info));
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700603
604 strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));
605
Sascha Hauer66c87192008-12-16 11:44:08 +0100606 info->fix.type = FB_TYPE_PACKED_PIXELS;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700607 info->fix.type_aux = 0;
608 info->fix.xpanstep = 0;
609 info->fix.ypanstep = 0;
610 info->fix.ywrapstep = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100611 info->fix.accel = FB_ACCEL_NONE;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700612
613 info->var.nonstd = 0;
614 info->var.activate = FB_ACTIVATE_NOW;
615 info->var.height = -1;
616 info->var.width = -1;
617 info->var.accel_flags = 0;
Sascha Hauer66c87192008-12-16 11:44:08 +0100618 info->var.vmode = FB_VMODE_NONINTERLACED;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700619
620 info->fbops = &imxfb_ops;
Sascha Hauer66c87192008-12-16 11:44:08 +0100621 info->flags = FBINFO_FLAG_DEFAULT |
622 FBINFO_READS_FAST;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700623
Sascha Hauer27889272008-12-16 11:44:09 +0100624 fbi->max_xres = pdata->xres;
625 info->var.xres = pdata->xres;
626 info->var.xres_virtual = pdata->xres;
627 fbi->max_yres = pdata->yres;
628 info->var.yres = pdata->yres;
629 info->var.yres_virtual = pdata->yres;
630 fbi->max_bpp = pdata->bpp;
631 info->var.bits_per_pixel = pdata->bpp;
632 info->var.nonstd = pdata->nonstd;
633 info->var.pixclock = pdata->pixclock;
634 info->var.hsync_len = pdata->hsync_len;
635 info->var.left_margin = pdata->left_margin;
636 info->var.right_margin = pdata->right_margin;
637 info->var.vsync_len = pdata->vsync_len;
638 info->var.upper_margin = pdata->upper_margin;
639 info->var.lower_margin = pdata->lower_margin;
640 info->var.sync = pdata->sync;
641 info->var.grayscale = pdata->cmap_greyscale;
642 fbi->cmap_inverse = pdata->cmap_inverse;
643 fbi->cmap_static = pdata->cmap_static;
644 fbi->pcr = pdata->pcr;
645 fbi->lscr1 = pdata->lscr1;
646 fbi->dmacr = pdata->dmacr;
647 fbi->pwmr = pdata->pwmr;
648 fbi->lcd_power = pdata->lcd_power;
649 fbi->backlight_power = pdata->backlight_power;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700650 info->fix.smem_len = fbi->max_xres * fbi->max_yres *
651 fbi->max_bpp / 8;
652
653 return 0;
654}
655
Russell King3ae5eae2005-11-09 22:32:44 +0000656static int __init imxfb_probe(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700657{
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700658 struct imxfb_info *fbi;
659 struct fb_info *info;
Sascha Hauer27889272008-12-16 11:44:09 +0100660 struct imx_fb_platform_data *pdata;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700661 struct resource *res;
662 int ret;
663
Sascha Hauerd6b51502009-06-29 11:41:09 +0200664 dev_info(&pdev->dev, "i.MX Framebuffer driver\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700665
666 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sascha Hauer66c87192008-12-16 11:44:08 +0100667 if (!res)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700668 return -ENODEV;
669
Sascha Hauer27889272008-12-16 11:44:09 +0100670 pdata = pdev->dev.platform_data;
671 if (!pdata) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000672 dev_err(&pdev->dev,"No platform_data available\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700673 return -ENOMEM;
674 }
675
Russell King3ae5eae2005-11-09 22:32:44 +0000676 info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100677 if (!info)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700678 return -ENOMEM;
679
680 fbi = info->par;
681
Russell King3ae5eae2005-11-09 22:32:44 +0000682 platform_set_drvdata(pdev, info);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700683
Juergen Beisert72330b02008-12-16 11:44:07 +0100684 ret = imxfb_init_fbinfo(pdev);
Sascha Hauer66c87192008-12-16 11:44:08 +0100685 if (ret < 0)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700686 goto failed_init;
687
Juergen Beisert72330b02008-12-16 11:44:07 +0100688 res = request_mem_region(res->start, resource_size(res),
689 DRIVER_NAME);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700690 if (!res) {
691 ret = -EBUSY;
Juergen Beisert72330b02008-12-16 11:44:07 +0100692 goto failed_req;
693 }
694
Sascha Hauerf909ef62009-01-15 15:21:00 +0100695 fbi->clk = clk_get(&pdev->dev, NULL);
696 if (IS_ERR(fbi->clk)) {
697 ret = PTR_ERR(fbi->clk);;
698 dev_err(&pdev->dev, "unable to get clock: %d\n", ret);
699 goto failed_getclock;
700 }
701
Juergen Beisert72330b02008-12-16 11:44:07 +0100702 fbi->regs = ioremap(res->start, resource_size(res));
703 if (fbi->regs == NULL) {
Sascha Hauerd6b51502009-06-29 11:41:09 +0200704 dev_err(&pdev->dev, "Cannot map frame buffer registers\n");
Juergen Beisert72330b02008-12-16 11:44:07 +0100705 goto failed_ioremap;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700706 }
707
Sascha Hauer27889272008-12-16 11:44:09 +0100708 if (!pdata->fixed_screen_cpu) {
Juergen Beisert72330b02008-12-16 11:44:07 +0100709 fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
710 fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
711 fbi->map_size, &fbi->map_dma, GFP_KERNEL);
712
713 if (!fbi->map_cpu) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000714 dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700715 ret = -ENOMEM;
716 goto failed_map;
717 }
Juergen Beisert72330b02008-12-16 11:44:07 +0100718
719 info->screen_base = fbi->map_cpu;
720 fbi->screen_cpu = fbi->map_cpu;
721 fbi->screen_dma = fbi->map_dma;
722 info->fix.smem_start = fbi->screen_dma;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700723 } else {
724 /* Fixed framebuffer mapping enables location of the screen in eSRAM */
Sascha Hauer27889272008-12-16 11:44:09 +0100725 fbi->map_cpu = pdata->fixed_screen_cpu;
726 fbi->map_dma = pdata->fixed_screen_dma;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700727 info->screen_base = fbi->map_cpu;
728 fbi->screen_cpu = fbi->map_cpu;
729 fbi->screen_dma = fbi->map_dma;
730 info->fix.smem_start = fbi->screen_dma;
731 }
732
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100733 if (pdata->init) {
734 ret = pdata->init(fbi->pdev);
735 if (ret)
736 goto failed_platform_init;
737 }
738
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700739 /*
740 * This makes sure that our colour bitfield
741 * descriptors are correctly initialised.
742 */
743 imxfb_check_var(&info->var, info);
744
Sascha Hauer66c87192008-12-16 11:44:08 +0100745 ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700746 if (ret < 0)
747 goto failed_cmap;
748
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700749 imxfb_set_par(info);
750 ret = register_framebuffer(info);
751 if (ret < 0) {
Pavel Pisaf99c8922006-01-07 10:44:32 +0000752 dev_err(&pdev->dev, "failed to register framebuffer\n");
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700753 goto failed_register;
754 }
755
756 imxfb_enable_controller(fbi);
757
758 return 0;
759
760failed_register:
761 fb_dealloc_cmap(&info->cmap);
762failed_cmap:
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100763 if (pdata->exit)
764 pdata->exit(fbi->pdev);
765failed_platform_init:
Sascha Hauer27889272008-12-16 11:44:09 +0100766 if (!pdata->fixed_screen_cpu)
Russell King3ae5eae2005-11-09 22:32:44 +0000767 dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
Juergen Beisert72330b02008-12-16 11:44:07 +0100768 fbi->map_dma);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700769failed_map:
Sascha Hauerf909ef62009-01-15 15:21:00 +0100770 clk_put(fbi->clk);
771failed_getclock:
Juergen Beisert72330b02008-12-16 11:44:07 +0100772 iounmap(fbi->regs);
773failed_ioremap:
Sascha Hauerd6b51502009-06-29 11:41:09 +0200774 release_mem_region(res->start, resource_size(res));
Juergen Beisert72330b02008-12-16 11:44:07 +0100775failed_req:
776 kfree(info->pseudo_palette);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700777failed_init:
Russell King3ae5eae2005-11-09 22:32:44 +0000778 platform_set_drvdata(pdev, NULL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700779 framebuffer_release(info);
780 return ret;
781}
782
Juergen Beisert72330b02008-12-16 11:44:07 +0100783static int __devexit imxfb_remove(struct platform_device *pdev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700784{
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100785 struct imx_fb_platform_data *pdata;
Russell King3ae5eae2005-11-09 22:32:44 +0000786 struct fb_info *info = platform_get_drvdata(pdev);
Sascha Hauer772a9e62005-07-17 20:15:36 +0100787 struct imxfb_info *fbi = info->par;
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700788 struct resource *res;
789
790 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
791
Sascha Hauer772a9e62005-07-17 20:15:36 +0100792 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700793
794 unregister_framebuffer(info);
795
Sascha Hauerc0b90a32009-01-15 15:37:22 +0100796 pdata = pdev->dev.platform_data;
797 if (pdata->exit)
798 pdata->exit(fbi->pdev);
799
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700800 fb_dealloc_cmap(&info->cmap);
801 kfree(info->pseudo_palette);
802 framebuffer_release(info);
803
Juergen Beisert72330b02008-12-16 11:44:07 +0100804 iounmap(fbi->regs);
Sascha Hauerd6b51502009-06-29 11:41:09 +0200805 release_mem_region(res->start, resource_size(res));
Sascha Hauerf909ef62009-01-15 15:21:00 +0100806 clk_disable(fbi->clk);
807 clk_put(fbi->clk);
808
Russell King3ae5eae2005-11-09 22:32:44 +0000809 platform_set_drvdata(pdev, NULL);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700810
811 return 0;
812}
813
Russell King3ae5eae2005-11-09 22:32:44 +0000814void imxfb_shutdown(struct platform_device * dev)
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700815{
Russell King3ae5eae2005-11-09 22:32:44 +0000816 struct fb_info *info = platform_get_drvdata(dev);
Sascha Hauer772a9e62005-07-17 20:15:36 +0100817 struct imxfb_info *fbi = info->par;
818 imxfb_disable_controller(fbi);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700819}
820
Russell King3ae5eae2005-11-09 22:32:44 +0000821static struct platform_driver imxfb_driver = {
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700822 .suspend = imxfb_suspend,
823 .resume = imxfb_resume,
Juergen Beisert72330b02008-12-16 11:44:07 +0100824 .remove = __devexit_p(imxfb_remove),
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700825 .shutdown = imxfb_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +0000826 .driver = {
Juergen Beisert72330b02008-12-16 11:44:07 +0100827 .name = DRIVER_NAME,
Russell King3ae5eae2005-11-09 22:32:44 +0000828 },
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700829};
830
831int __init imxfb_init(void)
832{
Juergen Beisert72330b02008-12-16 11:44:07 +0100833 return platform_driver_probe(&imxfb_driver, imxfb_probe);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700834}
835
836static void __exit imxfb_cleanup(void)
837{
Russell King3ae5eae2005-11-09 22:32:44 +0000838 platform_driver_unregister(&imxfb_driver);
Sascha Hauer7c2f891c2005-05-01 08:59:24 -0700839}
840
841module_init(imxfb_init);
842module_exit(imxfb_cleanup);
843
844MODULE_DESCRIPTION("Motorola i.MX framebuffer driver");
845MODULE_AUTHOR("Sascha Hauer, Pengutronix");
846MODULE_LICENSE("GPL");