fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / video / arcfb.c
1 /*
2  * linux/drivers/video/arcfb.c -- FB driver for Arc monochrome LCD board
3  *
4  * Copyright (C) 2005, Jaya Kumar <jayalk@intworks.biz>
5  * http://www.intworks.biz/arclcd
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  * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
12  *
13  * This driver was written to be used with the Arc LCD board. Arc uses a
14  * set of KS108 chips that control individual 64x64 LCD matrices. The board
15  * can be paneled in a variety of setups such as 2x1=128x64, 4x4=256x256 and
16  * so on. The interface between the board and the host is TTL based GPIO. The
17  * GPIO requirements are 8 writable data lines and 4+n lines for control. On a
18  * GPIO-less system, the board can be tested by connecting the respective sigs
19  * up to a parallel port connector. The driver requires the IO addresses for
20  * data and control GPIO at load time. It is unable to probe for the
21  * existence of the LCD so it must be told at load time whether it should
22  * be enabled or not.
23  *
24  * Todo:
25  * - testing with 4x4
26  * - testing with interrupt hw
27  *
28  * General notes:
29  * - User must set tuhold. It's in microseconds. According to the 108 spec,
30  *   the hold time is supposed to be at least 1 microsecond.
31  * - User must set num_cols=x num_rows=y, eg: x=2 means 128
32  * - User must set arcfb_enable=1 to enable it
33  * - User must set dio_addr=0xIOADDR cio_addr=0xIOADDR
34  *
35  */
36
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/string.h>
41 #include <linux/mm.h>
42 #include <linux/slab.h>
43 #include <linux/vmalloc.h>
44 #include <linux/delay.h>
45 #include <linux/interrupt.h>
46 #include <linux/fb.h>
47 #include <linux/init.h>
48 #include <linux/arcfb.h>
49 #include <linux/platform_device.h>
50
51 #include <asm/uaccess.h>
52
53 #define floor8(a) (a&(~0x07))
54 #define floorXres(a,xres) (a&(~(xres - 1)))
55 #define iceil8(a) (((int)((a+7)/8))*8)
56 #define ceil64(a) (a|0x3F)
57 #define ceilXres(a,xres) (a|(xres - 1))
58
59 /* ks108 chipset specific defines and code */
60
61 #define KS_SET_DPY_START_LINE   0xC0
62 #define KS_SET_PAGE_NUM         0xB8
63 #define KS_SET_X                0x40
64 #define KS_CEHI                 0x01
65 #define KS_CELO                 0x00
66 #define KS_SEL_CMD              0x08
67 #define KS_SEL_DATA             0x00
68 #define KS_DPY_ON               0x3F
69 #define KS_DPY_OFF              0x3E
70 #define KS_INTACK               0x40
71 #define KS_CLRINT               0x02
72
73 struct arcfb_par {
74         unsigned long dio_addr;
75         unsigned long cio_addr;
76         unsigned long c2io_addr;
77         atomic_t ref_count;
78         unsigned char cslut[9];
79         struct fb_info *info;
80         unsigned int irq;
81         spinlock_t lock;
82 };
83
84 static struct fb_fix_screeninfo arcfb_fix __initdata = {
85         .id =           "arcfb",
86         .type =         FB_TYPE_PACKED_PIXELS,
87         .visual =       FB_VISUAL_MONO01,
88         .xpanstep =     0,
89         .ypanstep =     1,
90         .ywrapstep =    0,
91         .accel =        FB_ACCEL_NONE,
92 };
93
94 static struct fb_var_screeninfo arcfb_var __initdata = {
95         .xres           = 128,
96         .yres           = 64,
97         .xres_virtual   = 128,
98         .yres_virtual   = 64,
99         .bits_per_pixel = 1,
100         .nonstd         = 1,
101 };
102
103 static unsigned long num_cols;
104 static unsigned long num_rows;
105 static unsigned long dio_addr;
106 static unsigned long cio_addr;
107 static unsigned long c2io_addr;
108 static unsigned long splashval;
109 static unsigned long tuhold;
110 static unsigned int nosplash;
111 static unsigned int arcfb_enable;
112 static unsigned int irq;
113
114 static DECLARE_WAIT_QUEUE_HEAD(arcfb_waitq);
115
116 static void ks108_writeb_ctl(struct arcfb_par *par,
117                                 unsigned int chipindex, unsigned char value)
118 {
119         unsigned char chipselval = par->cslut[chipindex];
120
121         outb(chipselval|KS_CEHI|KS_SEL_CMD, par->cio_addr);
122         outb(value, par->dio_addr);
123         udelay(tuhold);
124         outb(chipselval|KS_CELO|KS_SEL_CMD, par->cio_addr);
125 }
126
127 static void ks108_writeb_mainctl(struct arcfb_par *par, unsigned char value)
128 {
129
130         outb(value, par->cio_addr);
131         udelay(tuhold);
132 }
133
134 static unsigned char ks108_readb_ctl2(struct arcfb_par *par)
135 {
136         return inb(par->c2io_addr);
137 }
138
139 static void ks108_writeb_data(struct arcfb_par *par,
140                                 unsigned int chipindex, unsigned char value)
141 {
142         unsigned char chipselval = par->cslut[chipindex];
143
144         outb(chipselval|KS_CEHI|KS_SEL_DATA, par->cio_addr);
145         outb(value, par->dio_addr);
146         udelay(tuhold);
147         outb(chipselval|KS_CELO|KS_SEL_DATA, par->cio_addr);
148 }
149
150 static void ks108_set_start_line(struct arcfb_par *par,
151                                 unsigned int chipindex, unsigned char y)
152 {
153         ks108_writeb_ctl(par, chipindex, KS_SET_DPY_START_LINE|y);
154 }
155
156 static void ks108_set_yaddr(struct arcfb_par *par,
157                                 unsigned int chipindex, unsigned char y)
158 {
159         ks108_writeb_ctl(par, chipindex, KS_SET_PAGE_NUM|y);
160 }
161
162 static void ks108_set_xaddr(struct arcfb_par *par,
163                                 unsigned int chipindex, unsigned char x)
164 {
165         ks108_writeb_ctl(par, chipindex, KS_SET_X|x);
166 }
167
168 static void ks108_clear_lcd(struct arcfb_par *par, unsigned int chipindex)
169 {
170         int i,j;
171
172         for (i = 0; i <= 8; i++) {
173                 ks108_set_yaddr(par, chipindex, i);
174                 ks108_set_xaddr(par, chipindex, 0);
175                 for (j = 0; j < 64; j++) {
176                         ks108_writeb_data(par, chipindex,
177                                 (unsigned char) splashval);
178                 }
179         }
180 }
181
182 /* main arcfb functions */
183
184 static int arcfb_open(struct fb_info *info, int user)
185 {
186         struct arcfb_par *par = info->par;
187
188         atomic_inc(&par->ref_count);
189         return 0;
190 }
191
192 static int arcfb_release(struct fb_info *info, int user)
193 {
194         struct arcfb_par *par = info->par;
195         int count = atomic_read(&par->ref_count);
196
197         if (!count)
198                 return -EINVAL;
199         atomic_dec(&par->ref_count);
200         return 0;
201 }
202
203 static int arcfb_pan_display(struct fb_var_screeninfo *var,
204                                 struct fb_info *info)
205 {
206         int i;
207         struct arcfb_par *par = info->par;
208
209         if ((var->vmode & FB_VMODE_YWRAP) && (var->yoffset < 64)
210                 && (info->var.yres <= 64)) {
211                 for (i = 0; i < num_cols; i++) {
212                         ks108_set_start_line(par, i, var->yoffset);
213                 }
214                 info->var.yoffset = var->yoffset;
215                 return 0;
216         }
217
218         return -EINVAL;
219 }
220
221 static irqreturn_t arcfb_interrupt(int vec, void *dev_instance)
222 {
223         struct fb_info *info = dev_instance;
224         unsigned char ctl2status;
225         struct arcfb_par *par = info->par;
226
227         ctl2status = ks108_readb_ctl2(par);
228
229         if (!(ctl2status & KS_INTACK)) /* not arc generated interrupt */
230                 return IRQ_NONE;
231
232         ks108_writeb_mainctl(par, KS_CLRINT);
233
234         spin_lock(&par->lock);
235         if (waitqueue_active(&arcfb_waitq)) {
236                 wake_up(&arcfb_waitq);
237         }
238         spin_unlock(&par->lock);
239
240         return IRQ_HANDLED;
241 }
242
243 /*
244  * here we handle a specific page on the lcd. the complexity comes from
245  * the fact that the fb is laidout in 8xX vertical columns. we extract
246  * each write of 8 vertical pixels. then we shift out as we move along
247  * X. That's what rightshift does. bitmask selects the desired input bit.
248  */
249 static void arcfb_lcd_update_page(struct arcfb_par *par, unsigned int upper,
250                 unsigned int left, unsigned int right, unsigned int distance)
251 {
252         unsigned char *src;
253         unsigned int xindex, yindex, chipindex, linesize;
254         int i;
255         unsigned char val;
256         unsigned char bitmask, rightshift;
257
258         xindex = left >> 6;
259         yindex = upper >> 6;
260         chipindex = (xindex + (yindex*num_cols));
261
262         ks108_set_yaddr(par, chipindex, upper/8);
263
264         linesize = par->info->var.xres/8;
265         src = par->info->screen_base + (left/8) + (upper * linesize);
266         ks108_set_xaddr(par, chipindex, left);
267
268         bitmask=1;
269         rightshift=0;
270         while (left <= right) {
271                 val = 0;
272                 for (i = 0; i < 8; i++) {
273                         if ( i > rightshift) {
274                                 val |= (*(src + (i*linesize)) & bitmask)
275                                                 << (i - rightshift);
276                         } else {
277                                 val |= (*(src + (i*linesize)) & bitmask)
278                                                  >> (rightshift - i);
279                         }
280                 }
281                 ks108_writeb_data(par, chipindex, val);
282                 left++;
283                 if (bitmask == 0x80) {
284                         bitmask = 1;
285                         src++;
286                         rightshift=0;
287                 } else {
288                         bitmask <<= 1;
289                         rightshift++;
290                 }
291         }
292 }
293
294 /*
295  * here we handle the entire vertical page of the update. we write across
296  * lcd chips. update_page uses the upper/left values to decide which
297  * chip to select for the right. upper is needed for setting the page
298  * desired for the write.
299  */
300 static void arcfb_lcd_update_vert(struct arcfb_par *par, unsigned int top,
301                 unsigned int bottom, unsigned int left, unsigned int right)
302 {
303         unsigned int distance, upper, lower;
304
305         distance = (bottom - top) + 1;
306         upper = top;
307         lower = top + 7;
308
309         while (distance > 0) {
310                 distance -= 8;
311                 arcfb_lcd_update_page(par, upper, left, right, 8);
312                 upper = lower + 1;
313                 lower = upper + 7;
314         }
315 }
316
317 /*
318  * here we handle horizontal blocks for the update. update_vert will
319  * handle spaning multiple pages. we break out each horizontal
320  * block in to individual blocks no taller than 64 pixels.
321  */
322 static void arcfb_lcd_update_horiz(struct arcfb_par *par, unsigned int left,
323                         unsigned int right, unsigned int top, unsigned int h)
324 {
325         unsigned int distance, upper, lower;
326
327         distance = h;
328         upper = floor8(top);
329         lower = min(upper + distance - 1, ceil64(upper));
330
331         while (distance > 0) {
332                 distance -= ((lower - upper) + 1 );
333                 arcfb_lcd_update_vert(par, upper, lower, left, right);
334                 upper = lower + 1;
335                 lower = min(upper + distance - 1, ceil64(upper));
336         }
337 }
338
339 /*
340  * here we start the process of spliting out the fb update into
341  * individual blocks of pixels. we end up spliting into 64x64 blocks
342  * and finally down to 64x8 pages.
343  */
344 static void arcfb_lcd_update(struct arcfb_par *par, unsigned int dx,
345                         unsigned int dy, unsigned int w, unsigned int h)
346 {
347         unsigned int left, right, distance, y;
348
349         /* align the request first */
350         y = floor8(dy);
351         h += dy - y;
352         h = iceil8(h);
353
354         distance = w;
355         left = dx;
356         right = min(left + w - 1, ceil64(left));
357
358         while (distance > 0) {
359                 arcfb_lcd_update_horiz(par, left, right, y, h);
360                 distance -= ((right - left) + 1);
361                 left = right + 1;
362                 right = min(left + distance - 1, ceil64(left));
363         }
364 }
365
366 static void arcfb_fillrect(struct fb_info *info,
367                            const struct fb_fillrect *rect)
368 {
369         struct arcfb_par *par = info->par;
370
371         cfb_fillrect(info, rect);
372
373         /* update the physical lcd */
374         arcfb_lcd_update(par, rect->dx, rect->dy, rect->width, rect->height);
375 }
376
377 static void arcfb_copyarea(struct fb_info *info,
378                            const struct fb_copyarea *area)
379 {
380         struct arcfb_par *par = info->par;
381
382         cfb_copyarea(info, area);
383
384         /* update the physical lcd */
385         arcfb_lcd_update(par, area->dx, area->dy, area->width, area->height);
386 }
387
388 static void arcfb_imageblit(struct fb_info *info, const struct fb_image *image)
389 {
390         struct arcfb_par *par = info->par;
391
392         cfb_imageblit(info, image);
393
394         /* update the physical lcd */
395         arcfb_lcd_update(par, image->dx, image->dy, image->width,
396                                 image->height);
397 }
398
399 static int arcfb_ioctl(struct fb_info *info,
400                           unsigned int cmd, unsigned long arg)
401 {
402         void __user *argp = (void __user *)arg;
403         struct arcfb_par *par = info->par;
404         unsigned long flags;
405
406         switch (cmd) {
407                 case FBIO_WAITEVENT:
408                 {
409                         DEFINE_WAIT(wait);
410                         /* illegal to wait on arc if no irq will occur */
411                         if (!par->irq)
412                                 return -EINVAL;
413
414                         /* wait until the Arc has generated an interrupt
415                          * which will wake us up */
416                         spin_lock_irqsave(&par->lock, flags);
417                         prepare_to_wait(&arcfb_waitq, &wait,
418                                         TASK_INTERRUPTIBLE);
419                         spin_unlock_irqrestore(&par->lock, flags);
420                         schedule();
421                         finish_wait(&arcfb_waitq, &wait);
422                 }
423                 case FBIO_GETCONTROL2:
424                 {
425                         unsigned char ctl2;
426
427                         ctl2 = ks108_readb_ctl2(info->par);
428                         if (copy_to_user(argp, &ctl2, sizeof(ctl2)))
429                                 return -EFAULT;
430                         return 0;
431                 }
432                 default:
433                         return -EINVAL;
434         }
435 }
436
437 /*
438  * this is the access path from userspace. they can seek and write to
439  * the fb. it's inefficient for them to do anything less than 64*8
440  * writes since we update the lcd in each write() anyway.
441  */
442 static ssize_t arcfb_write(struct file *file, const char __user *buf, size_t count,
443                                 loff_t *ppos)
444 {
445         /* modded from epson 1355 */
446
447         struct inode *inode;
448         int fbidx;
449         struct fb_info *info;
450         unsigned long p;
451         int err=-EINVAL;
452         unsigned int fbmemlength,x,y,w,h, bitppos, startpos, endpos, bitcount;
453         struct arcfb_par *par;
454         unsigned int xres;
455
456         p = *ppos;
457         inode = file->f_path.dentry->d_inode;
458         fbidx = iminor(inode);
459         info = registered_fb[fbidx];
460
461         if (!info || !info->screen_base)
462                 return -ENODEV;
463
464         par = info->par;
465         xres = info->var.xres;
466         fbmemlength = (xres * info->var.yres)/8;
467
468         if (p > fbmemlength)
469                 return -ENOSPC;
470
471         err = 0;
472         if ((count + p) > fbmemlength) {
473                 count = fbmemlength - p;
474                 err = -ENOSPC;
475         }
476
477         if (count) {
478                 char *base_addr;
479
480                 base_addr = info->screen_base;
481                 count -= copy_from_user(base_addr + p, buf, count);
482                 *ppos += count;
483                 err = -EFAULT;
484         }
485
486
487         bitppos = p*8;
488         startpos = floorXres(bitppos, xres);
489         endpos = ceilXres((bitppos + (count*8)), xres);
490         bitcount = endpos - startpos;
491
492         x = startpos % xres;
493         y = startpos / xres;
494         w = xres;
495         h = bitcount / xres;
496         arcfb_lcd_update(par, x, y, w, h);
497
498         if (count)
499                 return count;
500         return err;
501 }
502
503 static struct fb_ops arcfb_ops = {
504         .owner          = THIS_MODULE,
505         .fb_open        = arcfb_open,
506         .fb_write       = arcfb_write,
507         .fb_release     = arcfb_release,
508         .fb_pan_display = arcfb_pan_display,
509         .fb_fillrect    = arcfb_fillrect,
510         .fb_copyarea    = arcfb_copyarea,
511         .fb_imageblit   = arcfb_imageblit,
512         .fb_ioctl       = arcfb_ioctl,
513 };
514
515 static int __init arcfb_probe(struct platform_device *dev)
516 {
517         struct fb_info *info;
518         int retval = -ENOMEM;
519         int videomemorysize;
520         unsigned char *videomemory;
521         struct arcfb_par *par;
522         int i;
523
524         videomemorysize = (((64*64)*num_cols)*num_rows)/8;
525
526         /* We need a flat backing store for the Arc's
527            less-flat actual paged framebuffer */
528         if (!(videomemory = vmalloc(videomemorysize)))
529                 return retval;
530
531         memset(videomemory, 0, videomemorysize);
532
533         info = framebuffer_alloc(sizeof(struct arcfb_par), &dev->dev);
534         if (!info)
535                 goto err;
536
537         info->screen_base = (char __iomem *)videomemory;
538         info->fbops = &arcfb_ops;
539
540         info->var = arcfb_var;
541         info->fix = arcfb_fix;
542         par = info->par;
543         par->info = info;
544
545         if (!dio_addr || !cio_addr || !c2io_addr) {
546                 printk(KERN_WARNING "no IO addresses supplied\n");
547                 goto err1;
548         }
549         par->dio_addr = dio_addr;
550         par->cio_addr = cio_addr;
551         par->c2io_addr = c2io_addr;
552         par->cslut[0] = 0x00;
553         par->cslut[1] = 0x06;
554         info->flags = FBINFO_FLAG_DEFAULT;
555         spin_lock_init(&par->lock);
556         retval = register_framebuffer(info);
557         if (retval < 0)
558                 goto err1;
559         platform_set_drvdata(dev, info);
560         if (irq) {
561                 par->irq = irq;
562                 if (request_irq(par->irq, &arcfb_interrupt, IRQF_SHARED,
563                                 "arcfb", info)) {
564                         printk(KERN_INFO
565                                 "arcfb: Failed req IRQ %d\n", par->irq);
566                         goto err1;
567                 }
568         }
569         printk(KERN_INFO
570                "fb%d: Arc frame buffer device, using %dK of video memory\n",
571                info->node, videomemorysize >> 10);
572
573         /* this inits the lcd but doesn't clear dirty pixels */
574         for (i = 0; i < num_cols * num_rows; i++) {
575                 ks108_writeb_ctl(par, i, KS_DPY_OFF);
576                 ks108_set_start_line(par, i, 0);
577                 ks108_set_yaddr(par, i, 0);
578                 ks108_set_xaddr(par, i, 0);
579                 ks108_writeb_ctl(par, i, KS_DPY_ON);
580         }
581
582         /* if we were told to splash the screen, we just clear it */
583         if (!nosplash) {
584                 for (i = 0; i < num_cols * num_rows; i++) {
585                         printk(KERN_INFO "fb%d: splashing lcd %d\n",
586                                 info->node, i);
587                         ks108_set_start_line(par, i, 0);
588                         ks108_clear_lcd(par, i);
589                 }
590         }
591
592         return 0;
593 err1:
594         framebuffer_release(info);
595 err:
596         vfree(videomemory);
597         return retval;
598 }
599
600 static int arcfb_remove(struct platform_device *dev)
601 {
602         struct fb_info *info = platform_get_drvdata(dev);
603
604         if (info) {
605                 unregister_framebuffer(info);
606                 vfree(info->screen_base);
607                 framebuffer_release(info);
608         }
609         return 0;
610 }
611
612 static struct platform_driver arcfb_driver = {
613         .probe  = arcfb_probe,
614         .remove = arcfb_remove,
615         .driver = {
616                 .name   = "arcfb",
617         },
618 };
619
620 static struct platform_device *arcfb_device;
621
622 static int __init arcfb_init(void)
623 {
624         int ret;
625
626         if (!arcfb_enable)
627                 return -ENXIO;
628
629         ret = platform_driver_register(&arcfb_driver);
630         if (!ret) {
631                 arcfb_device = platform_device_alloc("arcfb", 0);
632                 if (arcfb_device) {
633                         ret = platform_device_add(arcfb_device);
634                 } else {
635                         ret = -ENOMEM;
636                 }
637                 if (ret) {
638                         platform_device_put(arcfb_device);
639                         platform_driver_unregister(&arcfb_driver);
640                 }
641         }
642         return ret;
643
644 }
645
646 static void __exit arcfb_exit(void)
647 {
648         platform_device_unregister(arcfb_device);
649         platform_driver_unregister(&arcfb_driver);
650 }
651
652 module_param(num_cols, ulong, 0);
653 MODULE_PARM_DESC(num_cols, "Num horiz panels, eg: 2 = 128 bit wide");
654 module_param(num_rows, ulong, 0);
655 MODULE_PARM_DESC(num_rows, "Num vert panels, eg: 1 = 64 bit high");
656 module_param(nosplash, uint, 0);
657 MODULE_PARM_DESC(nosplash, "Disable doing the splash screen");
658 module_param(arcfb_enable, uint, 0);
659 MODULE_PARM_DESC(arcfb_enable, "Enable communication with Arc board");
660 module_param(dio_addr, ulong, 0);
661 MODULE_PARM_DESC(dio_addr, "IO address for data, eg: 0x480");
662 module_param(cio_addr, ulong, 0);
663 MODULE_PARM_DESC(cio_addr, "IO address for control, eg: 0x400");
664 module_param(c2io_addr, ulong, 0);
665 MODULE_PARM_DESC(c2io_addr, "IO address for secondary control, eg: 0x408");
666 module_param(splashval, ulong, 0);
667 MODULE_PARM_DESC(splashval, "Splash pattern: 0xFF is black, 0x00 is green");
668 module_param(tuhold, ulong, 0);
669 MODULE_PARM_DESC(tuhold, "Time to hold between strobing data to Arc board");
670 module_param(irq, uint, 0);
671 MODULE_PARM_DESC(irq, "IRQ for the Arc board");
672
673 module_init(arcfb_init);
674 module_exit(arcfb_exit);
675
676 MODULE_DESCRIPTION("fbdev driver for Arc monochrome LCD board");
677 MODULE_AUTHOR("Jaya Kumar");
678 MODULE_LICENSE("GPL");
679