2 * linux/drivers/video/amba-clcd.c
4 * Copyright (C) 2001 ARM Limited, by David A Rusling
5 * Updated to 2.5, Deep Blue Solutions Ltd.
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
11 * ARM PrimeCell PL110 Color LCD Controller
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/list.h>
25 #include <asm/hardware/amba.h>
26 #include <asm/hardware/clock.h>
28 #include <asm/hardware/amba_clcd.h>
30 #define to_clcd(info) container_of(info, struct clcd_fb, fb)
32 /* This is limited to 16 characters when displayed by X startup */
33 static const char *clcd_name = "CLCD FB";
36 * Unfortunately, the enable/disable functions may be called either from
37 * process or IRQ context, and we _need_ to delay. This is _not_ good.
39 static inline void clcdfb_sleep(unsigned int ms)
48 static inline void clcdfb_set_start(struct clcd_fb *fb)
50 unsigned long ustart = fb->fb.fix.smem_start;
53 ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
54 lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
56 writel(ustart, fb->regs + CLCD_UBAS);
57 writel(lstart, fb->regs + CLCD_LBAS);
60 static void clcdfb_disable(struct clcd_fb *fb)
64 if (fb->board->disable)
65 fb->board->disable(fb);
67 val = readl(fb->regs + CLCD_CNTL);
68 if (val & CNTL_LCDPWR) {
70 writel(val, fb->regs + CLCD_CNTL);
74 if (val & CNTL_LCDEN) {
76 writel(val, fb->regs + CLCD_CNTL);
80 * Disable CLCD clock source.
85 static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
88 * Enable the CLCD clock source.
93 * Bring up by first enabling..
96 writel(cntl, fb->regs + CLCD_CNTL);
101 * and now apply power.
104 writel(cntl, fb->regs + CLCD_CNTL);
107 * finally, enable the interface.
109 if (fb->board->enable)
110 fb->board->enable(fb);
114 clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
118 memset(&var->transp, 0, sizeof(var->transp));
119 memset(&var->red, 0, sizeof(var->red));
120 memset(&var->green, 0, sizeof(var->green));
121 memset(&var->blue, 0, sizeof(var->blue));
123 switch (var->bits_per_pixel) {
128 var->red.length = var->bits_per_pixel;
130 var->green.length = var->bits_per_pixel;
131 var->green.offset = 0;
132 var->blue.length = var->bits_per_pixel;
133 var->blue.offset = 0;
137 var->green.length = 5;
138 var->blue.length = 5;
139 if (fb->panel->cntl & CNTL_BGR) {
140 var->red.offset = 10;
141 var->green.offset = 5;
142 var->blue.offset = 0;
145 var->green.offset = 5;
146 var->blue.offset = 10;
150 if (fb->panel->cntl & CNTL_LCDTFT) {
152 var->green.length = 8;
153 var->blue.length = 8;
155 if (fb->panel->cntl & CNTL_BGR) {
156 var->red.offset = 16;
157 var->green.offset = 8;
158 var->blue.offset = 0;
161 var->green.offset = 8;
162 var->blue.offset = 16;
174 static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
176 struct clcd_fb *fb = to_clcd(info);
179 if (fb->board->check)
180 ret = fb->board->check(fb, var);
182 ret = clcdfb_set_bitfields(fb, var);
187 static int clcdfb_set_par(struct fb_info *info)
189 struct clcd_fb *fb = to_clcd(info);
190 struct clcd_regs regs;
192 fb->fb.fix.line_length = fb->fb.var.xres_virtual *
193 fb->fb.var.bits_per_pixel / 8;
195 if (fb->fb.var.bits_per_pixel <= 8)
196 fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
198 fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
200 fb->board->decode(fb, ®s);
204 writel(regs.tim0, fb->regs + CLCD_TIM0);
205 writel(regs.tim1, fb->regs + CLCD_TIM1);
206 writel(regs.tim2, fb->regs + CLCD_TIM2);
207 writel(regs.tim3, fb->regs + CLCD_TIM3);
209 clcdfb_set_start(fb);
211 clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
213 fb->clcd_cntl = regs.cntl;
215 clcdfb_enable(fb, regs.cntl);
218 printk(KERN_INFO "CLCD: Registers set to\n"
219 KERN_INFO " %08x %08x %08x %08x\n"
220 KERN_INFO " %08x %08x %08x %08x\n",
221 readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
222 readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
223 readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
224 readl(fb->regs + CLCD_IENB), readl(fb->regs + CLCD_CNTL));
230 static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
232 unsigned int mask = (1 << bf->length) - 1;
234 return (val >> (16 - bf->length) & mask) << bf->offset;
238 * Set a single color register. The values supplied have a 16 bit
239 * magnitude. Return != 0 for invalid regno.
242 clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
243 unsigned int blue, unsigned int transp, struct fb_info *info)
245 struct clcd_fb *fb = to_clcd(info);
248 fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
249 convert_bitfield(blue, &fb->fb.var.blue) |
250 convert_bitfield(green, &fb->fb.var.green) |
251 convert_bitfield(red, &fb->fb.var.red);
253 if (fb->fb.var.bits_per_pixel == 8 && regno < 256) {
254 int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
255 u32 val, mask, newval;
257 newval = (red >> 11) & 0x001f;
258 newval |= (green >> 6) & 0x03e0;
259 newval |= (blue >> 1) & 0x7c00;
262 * 3.2.11: if we're configured for big endian
263 * byte order, the palette entries are swapped.
265 if (fb->clcd_cntl & CNTL_BEBO)
275 val = readl(fb->regs + hw_reg) & mask;
276 writel(val | newval, fb->regs + hw_reg);
283 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
284 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
285 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
286 * to e.g. a video mode which doesn't support it. Implements VESA suspend
287 * and powerdown modes on hardware that supports disabling hsync/vsync:
288 * blank_mode == 2: suspend vsync
289 * blank_mode == 3: suspend hsync
290 * blank_mode == 4: powerdown
292 static int clcdfb_blank(int blank_mode, struct fb_info *info)
294 struct clcd_fb *fb = to_clcd(info);
296 if (blank_mode != 0) {
299 clcdfb_enable(fb, fb->clcd_cntl);
304 static int clcdfb_mmap(struct fb_info *info, struct file *file,
305 struct vm_area_struct *vma)
307 struct clcd_fb *fb = to_clcd(info);
308 unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
311 len = info->fix.smem_len;
313 if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
315 ret = fb->board->mmap(fb, vma);
320 static struct fb_ops clcdfb_ops = {
321 .owner = THIS_MODULE,
322 .fb_check_var = clcdfb_check_var,
323 .fb_set_par = clcdfb_set_par,
324 .fb_setcolreg = clcdfb_setcolreg,
325 .fb_blank = clcdfb_blank,
326 .fb_fillrect = cfb_fillrect,
327 .fb_copyarea = cfb_copyarea,
328 .fb_imageblit = cfb_imageblit,
329 .fb_cursor = soft_cursor,
330 .fb_mmap = clcdfb_mmap,
333 static int clcdfb_register(struct clcd_fb *fb)
337 fb->clk = clk_get(&fb->dev->dev, "CLCDCLK");
338 if (IS_ERR(fb->clk)) {
339 ret = PTR_ERR(fb->clk);
343 ret = clk_use(fb->clk);
347 fb->fb.fix.mmio_start = fb->dev->res.start;
348 fb->fb.fix.mmio_len = SZ_4K;
350 fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
352 printk(KERN_ERR "CLCD: unable to remap registers\n");
357 fb->fb.fbops = &clcdfb_ops;
358 fb->fb.flags = FBINFO_FLAG_DEFAULT;
359 fb->fb.pseudo_palette = fb->cmap;
361 strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
362 fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
363 fb->fb.fix.type_aux = 0;
364 fb->fb.fix.xpanstep = 0;
365 fb->fb.fix.ypanstep = 0;
366 fb->fb.fix.ywrapstep = 0;
367 fb->fb.fix.accel = FB_ACCEL_NONE;
369 fb->fb.var.xres = fb->panel->mode.xres;
370 fb->fb.var.yres = fb->panel->mode.yres;
371 fb->fb.var.xres_virtual = fb->panel->mode.xres;
372 fb->fb.var.yres_virtual = fb->panel->mode.yres;
373 fb->fb.var.bits_per_pixel = fb->panel->bpp;
374 fb->fb.var.grayscale = fb->panel->grayscale;
375 fb->fb.var.pixclock = fb->panel->mode.pixclock;
376 fb->fb.var.left_margin = fb->panel->mode.left_margin;
377 fb->fb.var.right_margin = fb->panel->mode.right_margin;
378 fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
379 fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
380 fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
381 fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
382 fb->fb.var.sync = fb->panel->mode.sync;
383 fb->fb.var.vmode = fb->panel->mode.vmode;
384 fb->fb.var.activate = FB_ACTIVATE_NOW;
385 fb->fb.var.nonstd = 0;
386 fb->fb.var.height = fb->panel->height;
387 fb->fb.var.width = fb->panel->width;
388 fb->fb.var.accel_flags = 0;
390 fb->fb.monspecs.hfmin = 0;
391 fb->fb.monspecs.hfmax = 100000;
392 fb->fb.monspecs.vfmin = 0;
393 fb->fb.monspecs.vfmax = 400;
394 fb->fb.monspecs.dclkmin = 1000000;
395 fb->fb.monspecs.dclkmax = 100000000;
398 * Make sure that the bitfields are set appropriately.
400 clcdfb_set_bitfields(fb, &fb->fb.var);
403 * Allocate colourmap.
405 fb_alloc_cmap(&fb->fb.cmap, 256, 0);
408 * Ensure interrupts are disabled.
410 writel(0, fb->regs + CLCD_IENB);
412 fb_set_var(&fb->fb, &fb->fb.var);
414 printk(KERN_INFO "CLCD: %s hardware, %s display\n",
415 fb->board->name, fb->panel->mode.name);
417 ret = register_framebuffer(&fb->fb);
421 printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
432 static int clcdfb_probe(struct amba_device *dev, void *id)
434 struct clcd_board *board = dev->dev.platform_data;
441 ret = amba_request_regions(dev, NULL);
443 printk(KERN_ERR "CLCD: unable to reserve regs region\n");
447 fb = (struct clcd_fb *) kmalloc(sizeof(struct clcd_fb), GFP_KERNEL);
449 printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
453 memset(fb, 0, sizeof(struct clcd_fb));
458 ret = fb->board->setup(fb);
462 ret = clcdfb_register(fb);
464 amba_set_drvdata(dev, fb);
468 fb->board->remove(fb);
472 amba_release_regions(dev);
477 static int clcdfb_remove(struct amba_device *dev)
479 struct clcd_fb *fb = amba_get_drvdata(dev);
481 amba_set_drvdata(dev, NULL);
484 unregister_framebuffer(&fb->fb);
489 fb->board->remove(fb);
493 amba_release_regions(dev);
498 static struct amba_id clcdfb_id_table[] = {
506 static struct amba_driver clcd_driver = {
508 .name = "clcd-pl110",
510 .probe = clcdfb_probe,
511 .remove = clcdfb_remove,
512 .id_table = clcdfb_id_table,
515 int __init amba_clcdfb_init(void)
517 if (fb_get_options("ambafb", NULL))
520 return amba_driver_register(&clcd_driver);
523 module_init(amba_clcdfb_init);
525 static void __exit amba_clcdfb_exit(void)
527 amba_driver_unregister(&clcd_driver);
530 module_exit(amba_clcdfb_exit);
532 MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
533 MODULE_LICENSE("GPL");