2 * linux/drivers/video/arcfb.c -- FB driver for Arc monochrome LCD board
4 * Copyright (C) 2005, Jaya Kumar <jayalk@intworks.biz>
5 * http://www.intworks.biz/arclcd
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
11 * Layout is based on skeletonfb.c by James Simmons and Geert Uytterhoeven.
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
26 * - testing with interrupt hw
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
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/string.h>
42 #include <linux/tty.h>
43 #include <linux/slab.h>
44 #include <linux/vmalloc.h>
45 #include <linux/delay.h>
46 #include <linux/interrupt.h>
48 #include <linux/init.h>
49 #include <linux/arcfb.h>
50 #include <linux/platform_device.h>
52 #include <asm/uaccess.h>
54 #define floor8(a) (a&(~0x07))
55 #define floorXres(a,xres) (a&(~(xres - 1)))
56 #define iceil8(a) (((int)((a+7)/8))*8)
57 #define ceil64(a) (a|0x3F)
58 #define ceilXres(a,xres) (a|(xres - 1))
60 /* ks108 chipset specific defines and code */
62 #define KS_SET_DPY_START_LINE 0xC0
63 #define KS_SET_PAGE_NUM 0xB8
67 #define KS_SEL_CMD 0x08
68 #define KS_SEL_DATA 0x00
69 #define KS_DPY_ON 0x3F
70 #define KS_DPY_OFF 0x3E
71 #define KS_INTACK 0x40
72 #define KS_CLRINT 0x02
75 unsigned long dio_addr;
76 unsigned long cio_addr;
77 unsigned long c2io_addr;
79 unsigned char cslut[9];
85 static struct fb_fix_screeninfo arcfb_fix __initdata = {
87 .type = FB_TYPE_PACKED_PIXELS,
88 .visual = FB_VISUAL_MONO01,
92 .accel = FB_ACCEL_NONE,
95 static struct fb_var_screeninfo arcfb_var __initdata = {
104 static unsigned long num_cols;
105 static unsigned long num_rows;
106 static unsigned long dio_addr;
107 static unsigned long cio_addr;
108 static unsigned long c2io_addr;
109 static unsigned long splashval;
110 static unsigned long tuhold;
111 static unsigned int nosplash;
112 static unsigned int arcfb_enable;
113 static unsigned int irq;
115 static DECLARE_WAIT_QUEUE_HEAD(arcfb_waitq);
117 static void ks108_writeb_ctl(struct arcfb_par *par,
118 unsigned int chipindex, unsigned char value)
120 unsigned char chipselval = par->cslut[chipindex];
122 outb(chipselval|KS_CEHI|KS_SEL_CMD, par->cio_addr);
123 outb(value, par->dio_addr);
125 outb(chipselval|KS_CELO|KS_SEL_CMD, par->cio_addr);
128 static void ks108_writeb_mainctl(struct arcfb_par *par, unsigned char value)
131 outb(value, par->cio_addr);
135 static unsigned char ks108_readb_ctl2(struct arcfb_par *par)
137 return inb(par->c2io_addr);
140 static void ks108_writeb_data(struct arcfb_par *par,
141 unsigned int chipindex, unsigned char value)
143 unsigned char chipselval = par->cslut[chipindex];
145 outb(chipselval|KS_CEHI|KS_SEL_DATA, par->cio_addr);
146 outb(value, par->dio_addr);
148 outb(chipselval|KS_CELO|KS_SEL_DATA, par->cio_addr);
151 static void ks108_set_start_line(struct arcfb_par *par,
152 unsigned int chipindex, unsigned char y)
154 ks108_writeb_ctl(par, chipindex, KS_SET_DPY_START_LINE|y);
157 static void ks108_set_yaddr(struct arcfb_par *par,
158 unsigned int chipindex, unsigned char y)
160 ks108_writeb_ctl(par, chipindex, KS_SET_PAGE_NUM|y);
163 static void ks108_set_xaddr(struct arcfb_par *par,
164 unsigned int chipindex, unsigned char x)
166 ks108_writeb_ctl(par, chipindex, KS_SET_X|x);
169 static void ks108_clear_lcd(struct arcfb_par *par, unsigned int chipindex)
173 for (i = 0; i <= 8; i++) {
174 ks108_set_yaddr(par, chipindex, i);
175 ks108_set_xaddr(par, chipindex, 0);
176 for (j = 0; j < 64; j++) {
177 ks108_writeb_data(par, chipindex,
178 (unsigned char) splashval);
183 /* main arcfb functions */
185 static int arcfb_open(struct fb_info *info, int user)
187 struct arcfb_par *par = info->par;
189 atomic_inc(&par->ref_count);
193 static int arcfb_release(struct fb_info *info, int user)
195 struct arcfb_par *par = info->par;
196 int count = atomic_read(&par->ref_count);
200 atomic_dec(&par->ref_count);
204 static int arcfb_pan_display(struct fb_var_screeninfo *var,
205 struct fb_info *info)
208 struct arcfb_par *par = info->par;
210 if ((var->vmode & FB_VMODE_YWRAP) && (var->yoffset < 64)
211 && (info->var.yres <= 64)) {
212 for (i = 0; i < num_cols; i++) {
213 ks108_set_start_line(par, i, var->yoffset);
215 info->var.yoffset = var->yoffset;
222 static irqreturn_t arcfb_interrupt(int vec, void *dev_instance,
223 struct pt_regs *regs)
225 struct fb_info *info = dev_instance;
226 unsigned char ctl2status;
227 struct arcfb_par *par = info->par;
229 ctl2status = ks108_readb_ctl2(par);
231 if (!(ctl2status & KS_INTACK)) /* not arc generated interrupt */
234 ks108_writeb_mainctl(par, KS_CLRINT);
236 spin_lock(&par->lock);
237 if (waitqueue_active(&arcfb_waitq)) {
238 wake_up(&arcfb_waitq);
240 spin_unlock(&par->lock);
246 * here we handle a specific page on the lcd. the complexity comes from
247 * the fact that the fb is laidout in 8xX vertical columns. we extract
248 * each write of 8 vertical pixels. then we shift out as we move along
249 * X. That's what rightshift does. bitmask selects the desired input bit.
251 static void arcfb_lcd_update_page(struct arcfb_par *par, unsigned int upper,
252 unsigned int left, unsigned int right, unsigned int distance)
255 unsigned int xindex, yindex, chipindex, linesize;
258 unsigned char bitmask, rightshift;
262 chipindex = (xindex + (yindex*num_cols));
264 ks108_set_yaddr(par, chipindex, upper/8);
266 linesize = par->info->var.xres/8;
267 src = par->info->screen_base + (left/8) + (upper * linesize);
268 ks108_set_xaddr(par, chipindex, left);
272 while (left <= right) {
274 for (i = 0; i < 8; i++) {
275 if ( i > rightshift) {
276 val |= (*(src + (i*linesize)) & bitmask)
279 val |= (*(src + (i*linesize)) & bitmask)
283 ks108_writeb_data(par, chipindex, val);
286 if (bitmask == 0x80) {
298 * here we handle the entire vertical page of the update. we write across
299 * lcd chips. update_page uses the upper/left values to decide which
300 * chip to select for the right. upper is needed for setting the page
301 * desired for the write.
303 static void arcfb_lcd_update_vert(struct arcfb_par *par, unsigned int top,
304 unsigned int bottom, unsigned int left, unsigned int right)
306 unsigned int distance, upper, lower;
308 distance = (bottom - top) + 1;
312 while (distance > 0) {
314 arcfb_lcd_update_page(par, upper, left, right, 8);
321 * here we handle horizontal blocks for the update. update_vert will
322 * handle spaning multiple pages. we break out each horizontal
323 * block in to individual blocks no taller than 64 pixels.
325 static void arcfb_lcd_update_horiz(struct arcfb_par *par, unsigned int left,
326 unsigned int right, unsigned int top, unsigned int h)
328 unsigned int distance, upper, lower;
332 lower = min(upper + distance - 1, ceil64(upper));
334 while (distance > 0) {
335 distance -= ((lower - upper) + 1 );
336 arcfb_lcd_update_vert(par, upper, lower, left, right);
338 lower = min(upper + distance - 1, ceil64(upper));
343 * here we start the process of spliting out the fb update into
344 * individual blocks of pixels. we end up spliting into 64x64 blocks
345 * and finally down to 64x8 pages.
347 static void arcfb_lcd_update(struct arcfb_par *par, unsigned int dx,
348 unsigned int dy, unsigned int w, unsigned int h)
350 unsigned int left, right, distance, y;
352 /* align the request first */
359 right = min(left + w - 1, ceil64(left));
361 while (distance > 0) {
362 arcfb_lcd_update_horiz(par, left, right, y, h);
363 distance -= ((right - left) + 1);
365 right = min(left + distance - 1, ceil64(left));
369 void arcfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
371 struct arcfb_par *par = info->par;
373 cfb_fillrect(info, rect);
375 /* update the physical lcd */
376 arcfb_lcd_update(par, rect->dx, rect->dy, rect->width, rect->height);
379 void arcfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
381 struct arcfb_par *par = info->par;
383 cfb_copyarea(info, area);
385 /* update the physical lcd */
386 arcfb_lcd_update(par, area->dx, area->dy, area->width, area->height);
389 void arcfb_imageblit(struct fb_info *info, const struct fb_image *image)
391 struct arcfb_par *par = info->par;
393 cfb_imageblit(info, image);
395 /* update the physical lcd */
396 arcfb_lcd_update(par, image->dx, image->dy, image->width,
400 static int arcfb_ioctl(struct inode *inode, struct file *file,
401 unsigned int cmd, unsigned long arg,
402 struct fb_info *info)
404 void __user *argp = (void __user *)arg;
405 struct arcfb_par *par = info->par;
412 /* illegal to wait on arc if no irq will occur */
416 /* wait until the Arc has generated an interrupt
417 * which will wake us up */
418 spin_lock_irqsave(&par->lock, flags);
419 prepare_to_wait(&arcfb_waitq, &wait,
421 spin_unlock_irqrestore(&par->lock, flags);
423 finish_wait(&arcfb_waitq, &wait);
425 case FBIO_GETCONTROL2:
429 ctl2 = ks108_readb_ctl2(info->par);
430 if (copy_to_user(argp, &ctl2, sizeof(ctl2)))
440 * this is the access path from userspace. they can seek and write to
441 * the fb. it's inefficient for them to do anything less than 64*8
442 * writes since we update the lcd in each write() anyway.
444 static ssize_t arcfb_write(struct file *file, const char *buf, size_t count,
447 /* modded from epson 1355 */
451 struct fb_info *info;
454 unsigned int fbmemlength,x,y,w,h, bitppos, startpos, endpos, bitcount;
455 struct arcfb_par *par;
459 inode = file->f_dentry->d_inode;
460 fbidx = iminor(inode);
461 info = registered_fb[fbidx];
464 if (!info || !info->screen_base)
467 xres = info->var.xres;
468 fbmemlength = (xres * info->var.yres)/8;
474 if ((count + p) > fbmemlength) {
475 count = fbmemlength - p;
482 base_addr = info->screen_base;
483 count -= copy_from_user(base_addr + p, buf, count);
490 startpos = floorXres(bitppos, xres);
491 endpos = ceilXres((bitppos + (count*8)), xres);
492 bitcount = endpos - startpos;
498 arcfb_lcd_update(par, x, y, w, h);
505 static void arcfb_platform_release(struct device *device)
509 static struct fb_ops arcfb_ops = {
510 .owner = THIS_MODULE,
511 .fb_open = arcfb_open,
512 .fb_write = arcfb_write,
513 .fb_release = arcfb_release,
514 .fb_pan_display = arcfb_pan_display,
515 .fb_fillrect = arcfb_fillrect,
516 .fb_copyarea = arcfb_copyarea,
517 .fb_imageblit = arcfb_imageblit,
518 .fb_cursor = soft_cursor,
519 .fb_ioctl = arcfb_ioctl,
522 static int __init arcfb_probe(struct device *device)
524 struct platform_device *dev = to_platform_device(device);
525 struct fb_info *info;
526 int retval = -ENOMEM;
528 unsigned char *videomemory;
529 struct arcfb_par *par;
532 videomemorysize = (((64*64)*num_cols)*num_rows)/8;
534 /* We need a flat backing store for the Arc's
535 less-flat actual paged framebuffer */
536 if (!(videomemory = vmalloc(videomemorysize)))
539 memset(videomemory, 0, videomemorysize);
541 info = framebuffer_alloc(sizeof(struct arcfb_par), &dev->dev);
545 info->screen_base = (char __iomem *)videomemory;
546 info->fbops = &arcfb_ops;
548 info->var = arcfb_var;
549 info->fix = arcfb_fix;
553 if (!dio_addr || !cio_addr || !c2io_addr) {
554 printk(KERN_WARNING "no IO addresses supplied\n");
557 par->dio_addr = dio_addr;
558 par->cio_addr = cio_addr;
559 par->c2io_addr = c2io_addr;
560 par->cslut[0] = 0x00;
561 par->cslut[1] = 0x06;
562 info->flags = FBINFO_FLAG_DEFAULT;
563 spin_lock_init(&par->lock);
564 retval = register_framebuffer(info);
567 dev_set_drvdata(&dev->dev, info);
570 if (request_irq(par->irq, &arcfb_interrupt, SA_SHIRQ,
573 "arcfb: Failed req IRQ %d\n", par->irq);
578 "fb%d: Arc frame buffer device, using %dK of video memory\n",
579 info->node, videomemorysize >> 10);
581 /* this inits the lcd but doesn't clear dirty pixels */
582 for (i = 0; i < num_cols * num_rows; i++) {
583 ks108_writeb_ctl(par, i, KS_DPY_OFF);
584 ks108_set_start_line(par, i, 0);
585 ks108_set_yaddr(par, i, 0);
586 ks108_set_xaddr(par, i, 0);
587 ks108_writeb_ctl(par, i, KS_DPY_ON);
590 /* if we were told to splash the screen, we just clear it */
592 for (i = 0; i < num_cols * num_rows; i++) {
593 printk(KERN_INFO "fb%d: splashing lcd %d\n",
595 ks108_set_start_line(par, i, 0);
596 ks108_clear_lcd(par, i);
602 framebuffer_release(info);
608 static int arcfb_remove(struct device *device)
610 struct fb_info *info = dev_get_drvdata(device);
613 unregister_framebuffer(info);
614 vfree(info->screen_base);
615 framebuffer_release(info);
620 static struct device_driver arcfb_driver = {
622 .bus = &platform_bus_type,
623 .probe = arcfb_probe,
624 .remove = arcfb_remove,
627 static struct platform_device arcfb_device = {
631 .release = arcfb_platform_release,
635 static int __init arcfb_init(void)
642 ret = driver_register(&arcfb_driver);
644 ret = platform_device_register(&arcfb_device);
646 driver_unregister(&arcfb_driver);
652 static void __exit arcfb_exit(void)
654 platform_device_unregister(&arcfb_device);
655 driver_unregister(&arcfb_driver);
658 module_param(num_cols, ulong, 0);
659 MODULE_PARM_DESC(num_cols, "Num horiz panels, eg: 2 = 128 bit wide");
660 module_param(num_rows, ulong, 0);
661 MODULE_PARM_DESC(num_rows, "Num vert panels, eg: 1 = 64 bit high");
662 module_param(nosplash, uint, 0);
663 MODULE_PARM_DESC(nosplash, "Disable doing the splash screen");
664 module_param(arcfb_enable, uint, 0);
665 MODULE_PARM_DESC(arcfb_enable, "Enable communication with Arc board");
666 module_param(dio_addr, ulong, 0);
667 MODULE_PARM_DESC(dio_addr, "IO address for data, eg: 0x480");
668 module_param(cio_addr, ulong, 0);
669 MODULE_PARM_DESC(cio_addr, "IO address for control, eg: 0x400");
670 module_param(c2io_addr, ulong, 0);
671 MODULE_PARM_DESC(c2io_addr, "IO address for secondary control, eg: 0x408");
672 module_param(splashval, ulong, 0);
673 MODULE_PARM_DESC(splashval, "Splash pattern: 0xFF is black, 0x00 is green");
674 module_param(tuhold, ulong, 0);
675 MODULE_PARM_DESC(tuhold, "Time to hold between strobing data to Arc board");
676 module_param(irq, uint, 0);
677 MODULE_PARM_DESC(irq, "IRQ for the Arc board");
679 module_init(arcfb_init);
680 module_exit(arcfb_exit);
682 MODULE_DESCRIPTION("fbdev driver for Arc monochrome LCD board");
683 MODULE_AUTHOR("Jaya Kumar");
684 MODULE_LICENSE("GPL");