ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / video / arcfb.c
blobc22e8d39a2cb1371ff88bc6840cacbbbc8123c37
1 /*
2 * linux/drivers/video/arcfb.c -- FB driver for Arc monochrome LCD board
4 * Copyright (C) 2005, Jaya Kumar <jayalk@intworks.biz>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
8 * more details.
10 * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
12 * This driver was written to be used with the Arc LCD board. Arc uses a
13 * set of KS108 chips that control individual 64x64 LCD matrices. The board
14 * can be paneled in a variety of setups such as 2x1=128x64, 4x4=256x256 and
15 * so on. The interface between the board and the host is TTL based GPIO. The
16 * GPIO requirements are 8 writable data lines and 4+n lines for control. On a
17 * GPIO-less system, the board can be tested by connecting the respective sigs
18 * up to a parallel port connector. The driver requires the IO addresses for
19 * data and control GPIO at load time. It is unable to probe for the
20 * existence of the LCD so it must be told at load time whether it should
21 * be enabled or not.
23 * Todo:
24 * - testing with 4x4
25 * - testing with interrupt hw
27 * General notes:
28 * - User must set tuhold. It's in microseconds. According to the 108 spec,
29 * the hold time is supposed to be at least 1 microsecond.
30 * - User must set num_cols=x num_rows=y, eg: x=2 means 128
31 * - User must set arcfb_enable=1 to enable it
32 * - User must set dio_addr=0xIOADDR cio_addr=0xIOADDR
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/string.h>
40 #include <linux/mm.h>
41 #include <linux/vmalloc.h>
42 #include <linux/delay.h>
43 #include <linux/interrupt.h>
44 #include <linux/fb.h>
45 #include <linux/init.h>
46 #include <linux/arcfb.h>
47 #include <linux/platform_device.h>
49 #include <linux/uaccess.h>
51 #define floor8(a) (a&(~0x07))
52 #define floorXres(a,xres) (a&(~(xres - 1)))
53 #define iceil8(a) (((int)((a+7)/8))*8)
54 #define ceil64(a) (a|0x3F)
55 #define ceilXres(a,xres) (a|(xres - 1))
57 /* ks108 chipset specific defines and code */
59 #define KS_SET_DPY_START_LINE 0xC0
60 #define KS_SET_PAGE_NUM 0xB8
61 #define KS_SET_X 0x40
62 #define KS_CEHI 0x01
63 #define KS_CELO 0x00
64 #define KS_SEL_CMD 0x08
65 #define KS_SEL_DATA 0x00
66 #define KS_DPY_ON 0x3F
67 #define KS_DPY_OFF 0x3E
68 #define KS_INTACK 0x40
69 #define KS_CLRINT 0x02
71 struct arcfb_par {
72 unsigned long dio_addr;
73 unsigned long cio_addr;
74 unsigned long c2io_addr;
75 atomic_t ref_count;
76 unsigned char cslut[9];
77 struct fb_info *info;
78 unsigned int irq;
79 spinlock_t lock;
82 static struct fb_fix_screeninfo arcfb_fix __devinitdata = {
83 .id = "arcfb",
84 .type = FB_TYPE_PACKED_PIXELS,
85 .visual = FB_VISUAL_MONO01,
86 .xpanstep = 0,
87 .ypanstep = 1,
88 .ywrapstep = 0,
89 .accel = FB_ACCEL_NONE,
92 static struct fb_var_screeninfo arcfb_var __devinitdata = {
93 .xres = 128,
94 .yres = 64,
95 .xres_virtual = 128,
96 .yres_virtual = 64,
97 .bits_per_pixel = 1,
98 .nonstd = 1,
101 static unsigned long num_cols;
102 static unsigned long num_rows;
103 static unsigned long dio_addr;
104 static unsigned long cio_addr;
105 static unsigned long c2io_addr;
106 static unsigned long splashval;
107 static unsigned long tuhold;
108 static unsigned int nosplash;
109 static unsigned int arcfb_enable;
110 static unsigned int irq;
112 static DECLARE_WAIT_QUEUE_HEAD(arcfb_waitq);
114 static void ks108_writeb_ctl(struct arcfb_par *par,
115 unsigned int chipindex, unsigned char value)
117 unsigned char chipselval = par->cslut[chipindex];
119 outb(chipselval|KS_CEHI|KS_SEL_CMD, par->cio_addr);
120 outb(value, par->dio_addr);
121 udelay(tuhold);
122 outb(chipselval|KS_CELO|KS_SEL_CMD, par->cio_addr);
125 static void ks108_writeb_mainctl(struct arcfb_par *par, unsigned char value)
128 outb(value, par->cio_addr);
129 udelay(tuhold);
132 static unsigned char ks108_readb_ctl2(struct arcfb_par *par)
134 return inb(par->c2io_addr);
137 static void ks108_writeb_data(struct arcfb_par *par,
138 unsigned int chipindex, unsigned char value)
140 unsigned char chipselval = par->cslut[chipindex];
142 outb(chipselval|KS_CEHI|KS_SEL_DATA, par->cio_addr);
143 outb(value, par->dio_addr);
144 udelay(tuhold);
145 outb(chipselval|KS_CELO|KS_SEL_DATA, par->cio_addr);
148 static void ks108_set_start_line(struct arcfb_par *par,
149 unsigned int chipindex, unsigned char y)
151 ks108_writeb_ctl(par, chipindex, KS_SET_DPY_START_LINE|y);
154 static void ks108_set_yaddr(struct arcfb_par *par,
155 unsigned int chipindex, unsigned char y)
157 ks108_writeb_ctl(par, chipindex, KS_SET_PAGE_NUM|y);
160 static void ks108_set_xaddr(struct arcfb_par *par,
161 unsigned int chipindex, unsigned char x)
163 ks108_writeb_ctl(par, chipindex, KS_SET_X|x);
166 static void ks108_clear_lcd(struct arcfb_par *par, unsigned int chipindex)
168 int i,j;
170 for (i = 0; i <= 8; i++) {
171 ks108_set_yaddr(par, chipindex, i);
172 ks108_set_xaddr(par, chipindex, 0);
173 for (j = 0; j < 64; j++) {
174 ks108_writeb_data(par, chipindex,
175 (unsigned char) splashval);
180 /* main arcfb functions */
182 static int arcfb_open(struct fb_info *info, int user)
184 struct arcfb_par *par = info->par;
186 atomic_inc(&par->ref_count);
187 return 0;
190 static int arcfb_release(struct fb_info *info, int user)
192 struct arcfb_par *par = info->par;
193 int count = atomic_read(&par->ref_count);
195 if (!count)
196 return -EINVAL;
197 atomic_dec(&par->ref_count);
198 return 0;
201 static int arcfb_pan_display(struct fb_var_screeninfo *var,
202 struct fb_info *info)
204 int i;
205 struct arcfb_par *par = info->par;
207 if ((var->vmode & FB_VMODE_YWRAP) && (var->yoffset < 64)
208 && (info->var.yres <= 64)) {
209 for (i = 0; i < num_cols; i++) {
210 ks108_set_start_line(par, i, var->yoffset);
212 info->var.yoffset = var->yoffset;
213 return 0;
216 return -EINVAL;
219 static irqreturn_t arcfb_interrupt(int vec, void *dev_instance)
221 struct fb_info *info = dev_instance;
222 unsigned char ctl2status;
223 struct arcfb_par *par = info->par;
225 ctl2status = ks108_readb_ctl2(par);
227 if (!(ctl2status & KS_INTACK)) /* not arc generated interrupt */
228 return IRQ_NONE;
230 ks108_writeb_mainctl(par, KS_CLRINT);
232 spin_lock(&par->lock);
233 if (waitqueue_active(&arcfb_waitq)) {
234 wake_up(&arcfb_waitq);
236 spin_unlock(&par->lock);
238 return IRQ_HANDLED;
242 * here we handle a specific page on the lcd. the complexity comes from
243 * the fact that the fb is laidout in 8xX vertical columns. we extract
244 * each write of 8 vertical pixels. then we shift out as we move along
245 * X. That's what rightshift does. bitmask selects the desired input bit.
247 static void arcfb_lcd_update_page(struct arcfb_par *par, unsigned int upper,
248 unsigned int left, unsigned int right, unsigned int distance)
250 unsigned char *src;
251 unsigned int xindex, yindex, chipindex, linesize;
252 int i;
253 unsigned char val;
254 unsigned char bitmask, rightshift;
256 xindex = left >> 6;
257 yindex = upper >> 6;
258 chipindex = (xindex + (yindex*num_cols));
260 ks108_set_yaddr(par, chipindex, upper/8);
262 linesize = par->info->var.xres/8;
263 src = (unsigned char __force *) par->info->screen_base + (left/8) +
264 (upper * linesize);
265 ks108_set_xaddr(par, chipindex, left);
267 bitmask=1;
268 rightshift=0;
269 while (left <= right) {
270 val = 0;
271 for (i = 0; i < 8; i++) {
272 if ( i > rightshift) {
273 val |= (*(src + (i*linesize)) & bitmask)
274 << (i - rightshift);
275 } else {
276 val |= (*(src + (i*linesize)) & bitmask)
277 >> (rightshift - i);
280 ks108_writeb_data(par, chipindex, val);
281 left++;
282 if (bitmask == 0x80) {
283 bitmask = 1;
284 src++;
285 rightshift=0;
286 } else {
287 bitmask <<= 1;
288 rightshift++;
294 * here we handle the entire vertical page of the update. we write across
295 * lcd chips. update_page uses the upper/left values to decide which
296 * chip to select for the right. upper is needed for setting the page
297 * desired for the write.
299 static void arcfb_lcd_update_vert(struct arcfb_par *par, unsigned int top,
300 unsigned int bottom, unsigned int left, unsigned int right)
302 unsigned int distance, upper, lower;
304 distance = (bottom - top) + 1;
305 upper = top;
306 lower = top + 7;
308 while (distance > 0) {
309 distance -= 8;
310 arcfb_lcd_update_page(par, upper, left, right, 8);
311 upper = lower + 1;
312 lower = upper + 7;
317 * here we handle horizontal blocks for the update. update_vert will
318 * handle spaning multiple pages. we break out each horizontal
319 * block in to individual blocks no taller than 64 pixels.
321 static void arcfb_lcd_update_horiz(struct arcfb_par *par, unsigned int left,
322 unsigned int right, unsigned int top, unsigned int h)
324 unsigned int distance, upper, lower;
326 distance = h;
327 upper = floor8(top);
328 lower = min(upper + distance - 1, ceil64(upper));
330 while (distance > 0) {
331 distance -= ((lower - upper) + 1 );
332 arcfb_lcd_update_vert(par, upper, lower, left, right);
333 upper = lower + 1;
334 lower = min(upper + distance - 1, ceil64(upper));
339 * here we start the process of spliting out the fb update into
340 * individual blocks of pixels. we end up spliting into 64x64 blocks
341 * and finally down to 64x8 pages.
343 static void arcfb_lcd_update(struct arcfb_par *par, unsigned int dx,
344 unsigned int dy, unsigned int w, unsigned int h)
346 unsigned int left, right, distance, y;
348 /* align the request first */
349 y = floor8(dy);
350 h += dy - y;
351 h = iceil8(h);
353 distance = w;
354 left = dx;
355 right = min(left + w - 1, ceil64(left));
357 while (distance > 0) {
358 arcfb_lcd_update_horiz(par, left, right, y, h);
359 distance -= ((right - left) + 1);
360 left = right + 1;
361 right = min(left + distance - 1, ceil64(left));
365 static void arcfb_fillrect(struct fb_info *info,
366 const struct fb_fillrect *rect)
368 struct arcfb_par *par = info->par;
370 sys_fillrect(info, rect);
372 /* update the physical lcd */
373 arcfb_lcd_update(par, rect->dx, rect->dy, rect->width, rect->height);
376 static void arcfb_copyarea(struct fb_info *info,
377 const struct fb_copyarea *area)
379 struct arcfb_par *par = info->par;
381 sys_copyarea(info, area);
383 /* update the physical lcd */
384 arcfb_lcd_update(par, area->dx, area->dy, area->width, area->height);
387 static void arcfb_imageblit(struct fb_info *info, const struct fb_image *image)
389 struct arcfb_par *par = info->par;
391 sys_imageblit(info, image);
393 /* update the physical lcd */
394 arcfb_lcd_update(par, image->dx, image->dy, image->width,
395 image->height);
398 static int arcfb_ioctl(struct fb_info *info,
399 unsigned int cmd, unsigned long arg)
401 void __user *argp = (void __user *)arg;
402 struct arcfb_par *par = info->par;
403 unsigned long flags;
405 switch (cmd) {
406 case FBIO_WAITEVENT:
408 DEFINE_WAIT(wait);
409 /* illegal to wait on arc if no irq will occur */
410 if (!par->irq)
411 return -EINVAL;
413 /* wait until the Arc has generated an interrupt
414 * which will wake us up */
415 spin_lock_irqsave(&par->lock, flags);
416 prepare_to_wait(&arcfb_waitq, &wait,
417 TASK_INTERRUPTIBLE);
418 spin_unlock_irqrestore(&par->lock, flags);
419 schedule();
420 finish_wait(&arcfb_waitq, &wait);
422 case FBIO_GETCONTROL2:
424 unsigned char ctl2;
426 ctl2 = ks108_readb_ctl2(info->par);
427 if (copy_to_user(argp, &ctl2, sizeof(ctl2)))
428 return -EFAULT;
429 return 0;
431 default:
432 return -EINVAL;
437 * this is the access path from userspace. they can seek and write to
438 * the fb. it's inefficient for them to do anything less than 64*8
439 * writes since we update the lcd in each write() anyway.
441 static ssize_t arcfb_write(struct fb_info *info, const char __user *buf,
442 size_t count, loff_t *ppos)
444 /* modded from epson 1355 */
446 unsigned long p;
447 int err=-EINVAL;
448 unsigned int fbmemlength,x,y,w,h, bitppos, startpos, endpos, bitcount;
449 struct arcfb_par *par;
450 unsigned int xres;
452 p = *ppos;
453 par = info->par;
454 xres = info->var.xres;
455 fbmemlength = (xres * info->var.yres)/8;
457 if (p > fbmemlength)
458 return -ENOSPC;
460 err = 0;
461 if ((count + p) > fbmemlength) {
462 count = fbmemlength - p;
463 err = -ENOSPC;
466 if (count) {
467 char *base_addr;
469 base_addr = (char __force *)info->screen_base;
470 count -= copy_from_user(base_addr + p, buf, count);
471 *ppos += count;
472 err = -EFAULT;
476 bitppos = p*8;
477 startpos = floorXres(bitppos, xres);
478 endpos = ceilXres((bitppos + (count*8)), xres);
479 bitcount = endpos - startpos;
481 x = startpos % xres;
482 y = startpos / xres;
483 w = xres;
484 h = bitcount / xres;
485 arcfb_lcd_update(par, x, y, w, h);
487 if (count)
488 return count;
489 return err;
492 static struct fb_ops arcfb_ops = {
493 .owner = THIS_MODULE,
494 .fb_open = arcfb_open,
495 .fb_read = fb_sys_read,
496 .fb_write = arcfb_write,
497 .fb_release = arcfb_release,
498 .fb_pan_display = arcfb_pan_display,
499 .fb_fillrect = arcfb_fillrect,
500 .fb_copyarea = arcfb_copyarea,
501 .fb_imageblit = arcfb_imageblit,
502 .fb_ioctl = arcfb_ioctl,
505 static int __devinit arcfb_probe(struct platform_device *dev)
507 struct fb_info *info;
508 int retval = -ENOMEM;
509 int videomemorysize;
510 unsigned char *videomemory;
511 struct arcfb_par *par;
512 int i;
514 videomemorysize = (((64*64)*num_cols)*num_rows)/8;
516 /* We need a flat backing store for the Arc's
517 less-flat actual paged framebuffer */
518 videomemory = vzalloc(videomemorysize);
519 if (!videomemory)
520 return retval;
522 info = framebuffer_alloc(sizeof(struct arcfb_par), &dev->dev);
523 if (!info)
524 goto err;
526 info->screen_base = (char __iomem *)videomemory;
527 info->fbops = &arcfb_ops;
529 info->var = arcfb_var;
530 info->fix = arcfb_fix;
531 par = info->par;
532 par->info = info;
534 if (!dio_addr || !cio_addr || !c2io_addr) {
535 printk(KERN_WARNING "no IO addresses supplied\n");
536 goto err1;
538 par->dio_addr = dio_addr;
539 par->cio_addr = cio_addr;
540 par->c2io_addr = c2io_addr;
541 par->cslut[0] = 0x00;
542 par->cslut[1] = 0x06;
543 info->flags = FBINFO_FLAG_DEFAULT;
544 spin_lock_init(&par->lock);
545 retval = register_framebuffer(info);
546 if (retval < 0)
547 goto err1;
548 platform_set_drvdata(dev, info);
549 if (irq) {
550 par->irq = irq;
551 if (request_irq(par->irq, &arcfb_interrupt, IRQF_SHARED,
552 "arcfb", info)) {
553 printk(KERN_INFO
554 "arcfb: Failed req IRQ %d\n", par->irq);
555 goto err1;
558 printk(KERN_INFO
559 "fb%d: Arc frame buffer device, using %dK of video memory\n",
560 info->node, videomemorysize >> 10);
562 /* this inits the lcd but doesn't clear dirty pixels */
563 for (i = 0; i < num_cols * num_rows; i++) {
564 ks108_writeb_ctl(par, i, KS_DPY_OFF);
565 ks108_set_start_line(par, i, 0);
566 ks108_set_yaddr(par, i, 0);
567 ks108_set_xaddr(par, i, 0);
568 ks108_writeb_ctl(par, i, KS_DPY_ON);
571 /* if we were told to splash the screen, we just clear it */
572 if (!nosplash) {
573 for (i = 0; i < num_cols * num_rows; i++) {
574 printk(KERN_INFO "fb%d: splashing lcd %d\n",
575 info->node, i);
576 ks108_set_start_line(par, i, 0);
577 ks108_clear_lcd(par, i);
581 return 0;
582 err1:
583 framebuffer_release(info);
584 err:
585 vfree(videomemory);
586 return retval;
589 static int __devexit arcfb_remove(struct platform_device *dev)
591 struct fb_info *info = platform_get_drvdata(dev);
593 if (info) {
594 unregister_framebuffer(info);
595 vfree((void __force *)info->screen_base);
596 framebuffer_release(info);
598 return 0;
601 static struct platform_driver arcfb_driver = {
602 .probe = arcfb_probe,
603 .remove = __devexit_p(arcfb_remove),
604 .driver = {
605 .name = "arcfb",
609 static struct platform_device *arcfb_device;
611 static int __init arcfb_init(void)
613 int ret;
615 if (!arcfb_enable)
616 return -ENXIO;
618 ret = platform_driver_register(&arcfb_driver);
619 if (!ret) {
620 arcfb_device = platform_device_alloc("arcfb", 0);
621 if (arcfb_device) {
622 ret = platform_device_add(arcfb_device);
623 } else {
624 ret = -ENOMEM;
626 if (ret) {
627 platform_device_put(arcfb_device);
628 platform_driver_unregister(&arcfb_driver);
631 return ret;
635 static void __exit arcfb_exit(void)
637 platform_device_unregister(arcfb_device);
638 platform_driver_unregister(&arcfb_driver);
641 module_param(num_cols, ulong, 0);
642 MODULE_PARM_DESC(num_cols, "Num horiz panels, eg: 2 = 128 bit wide");
643 module_param(num_rows, ulong, 0);
644 MODULE_PARM_DESC(num_rows, "Num vert panels, eg: 1 = 64 bit high");
645 module_param(nosplash, uint, 0);
646 MODULE_PARM_DESC(nosplash, "Disable doing the splash screen");
647 module_param(arcfb_enable, uint, 0);
648 MODULE_PARM_DESC(arcfb_enable, "Enable communication with Arc board");
649 module_param(dio_addr, ulong, 0);
650 MODULE_PARM_DESC(dio_addr, "IO address for data, eg: 0x480");
651 module_param(cio_addr, ulong, 0);
652 MODULE_PARM_DESC(cio_addr, "IO address for control, eg: 0x400");
653 module_param(c2io_addr, ulong, 0);
654 MODULE_PARM_DESC(c2io_addr, "IO address for secondary control, eg: 0x408");
655 module_param(splashval, ulong, 0);
656 MODULE_PARM_DESC(splashval, "Splash pattern: 0xFF is black, 0x00 is green");
657 module_param(tuhold, ulong, 0);
658 MODULE_PARM_DESC(tuhold, "Time to hold between strobing data to Arc board");
659 module_param(irq, uint, 0);
660 MODULE_PARM_DESC(irq, "IRQ for the Arc board");
662 module_init(arcfb_init);
663 module_exit(arcfb_exit);
665 MODULE_DESCRIPTION("fbdev driver for Arc monochrome LCD board");
666 MODULE_AUTHOR("Jaya Kumar");
667 MODULE_LICENSE("GPL");