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/slab.h>
43 #include <linux/vmalloc.h>
44 #include <linux/delay.h>
45 #include <linux/interrupt.h>
47 #include <linux/init.h>
48 #include <linux/arcfb.h>
49 #include <linux/platform_device.h>
51 #include <asm/uaccess.h>
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))
59 /* ks108 chipset specific defines and code */
61 #define KS_SET_DPY_START_LINE 0xC0
62 #define KS_SET_PAGE_NUM 0xB8
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
74 unsigned long dio_addr
;
75 unsigned long cio_addr
;
76 unsigned long c2io_addr
;
78 unsigned char cslut
[9];
84 static struct fb_fix_screeninfo arcfb_fix __initdata
= {
86 .type
= FB_TYPE_PACKED_PIXELS
,
87 .visual
= FB_VISUAL_MONO01
,
91 .accel
= FB_ACCEL_NONE
,
94 static struct fb_var_screeninfo arcfb_var __initdata
= {
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
;
114 static DECLARE_WAIT_QUEUE_HEAD(arcfb_waitq
);
116 static void ks108_writeb_ctl(struct arcfb_par
*par
,
117 unsigned int chipindex
, unsigned char value
)
119 unsigned char chipselval
= par
->cslut
[chipindex
];
121 outb(chipselval
|KS_CEHI
|KS_SEL_CMD
, par
->cio_addr
);
122 outb(value
, par
->dio_addr
);
124 outb(chipselval
|KS_CELO
|KS_SEL_CMD
, par
->cio_addr
);
127 static void ks108_writeb_mainctl(struct arcfb_par
*par
, unsigned char value
)
130 outb(value
, par
->cio_addr
);
134 static unsigned char ks108_readb_ctl2(struct arcfb_par
*par
)
136 return inb(par
->c2io_addr
);
139 static void ks108_writeb_data(struct arcfb_par
*par
,
140 unsigned int chipindex
, unsigned char value
)
142 unsigned char chipselval
= par
->cslut
[chipindex
];
144 outb(chipselval
|KS_CEHI
|KS_SEL_DATA
, par
->cio_addr
);
145 outb(value
, par
->dio_addr
);
147 outb(chipselval
|KS_CELO
|KS_SEL_DATA
, par
->cio_addr
);
150 static void ks108_set_start_line(struct arcfb_par
*par
,
151 unsigned int chipindex
, unsigned char y
)
153 ks108_writeb_ctl(par
, chipindex
, KS_SET_DPY_START_LINE
|y
);
156 static void ks108_set_yaddr(struct arcfb_par
*par
,
157 unsigned int chipindex
, unsigned char y
)
159 ks108_writeb_ctl(par
, chipindex
, KS_SET_PAGE_NUM
|y
);
162 static void ks108_set_xaddr(struct arcfb_par
*par
,
163 unsigned int chipindex
, unsigned char x
)
165 ks108_writeb_ctl(par
, chipindex
, KS_SET_X
|x
);
168 static void ks108_clear_lcd(struct arcfb_par
*par
, unsigned int chipindex
)
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
);
182 /* main arcfb functions */
184 static int arcfb_open(struct fb_info
*info
, int user
)
186 struct arcfb_par
*par
= info
->par
;
188 atomic_inc(&par
->ref_count
);
192 static int arcfb_release(struct fb_info
*info
, int user
)
194 struct arcfb_par
*par
= info
->par
;
195 int count
= atomic_read(&par
->ref_count
);
199 atomic_dec(&par
->ref_count
);
203 static int arcfb_pan_display(struct fb_var_screeninfo
*var
,
204 struct fb_info
*info
)
207 struct arcfb_par
*par
= info
->par
;
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
);
214 info
->var
.yoffset
= var
->yoffset
;
221 static irqreturn_t
arcfb_interrupt(int vec
, void *dev_instance
,
222 struct pt_regs
*regs
)
224 struct fb_info
*info
= dev_instance
;
225 unsigned char ctl2status
;
226 struct arcfb_par
*par
= info
->par
;
228 ctl2status
= ks108_readb_ctl2(par
);
230 if (!(ctl2status
& KS_INTACK
)) /* not arc generated interrupt */
233 ks108_writeb_mainctl(par
, KS_CLRINT
);
235 spin_lock(&par
->lock
);
236 if (waitqueue_active(&arcfb_waitq
)) {
237 wake_up(&arcfb_waitq
);
239 spin_unlock(&par
->lock
);
245 * here we handle a specific page on the lcd. the complexity comes from
246 * the fact that the fb is laidout in 8xX vertical columns. we extract
247 * each write of 8 vertical pixels. then we shift out as we move along
248 * X. That's what rightshift does. bitmask selects the desired input bit.
250 static void arcfb_lcd_update_page(struct arcfb_par
*par
, unsigned int upper
,
251 unsigned int left
, unsigned int right
, unsigned int distance
)
254 unsigned int xindex
, yindex
, chipindex
, linesize
;
257 unsigned char bitmask
, rightshift
;
261 chipindex
= (xindex
+ (yindex
*num_cols
));
263 ks108_set_yaddr(par
, chipindex
, upper
/8);
265 linesize
= par
->info
->var
.xres
/8;
266 src
= par
->info
->screen_base
+ (left
/8) + (upper
* linesize
);
267 ks108_set_xaddr(par
, chipindex
, left
);
271 while (left
<= right
) {
273 for (i
= 0; i
< 8; i
++) {
274 if ( i
> rightshift
) {
275 val
|= (*(src
+ (i
*linesize
)) & bitmask
)
278 val
|= (*(src
+ (i
*linesize
)) & bitmask
)
282 ks108_writeb_data(par
, chipindex
, val
);
284 if (bitmask
== 0x80) {
296 * here we handle the entire vertical page of the update. we write across
297 * lcd chips. update_page uses the upper/left values to decide which
298 * chip to select for the right. upper is needed for setting the page
299 * desired for the write.
301 static void arcfb_lcd_update_vert(struct arcfb_par
*par
, unsigned int top
,
302 unsigned int bottom
, unsigned int left
, unsigned int right
)
304 unsigned int distance
, upper
, lower
;
306 distance
= (bottom
- top
) + 1;
310 while (distance
> 0) {
312 arcfb_lcd_update_page(par
, upper
, left
, right
, 8);
319 * here we handle horizontal blocks for the update. update_vert will
320 * handle spaning multiple pages. we break out each horizontal
321 * block in to individual blocks no taller than 64 pixels.
323 static void arcfb_lcd_update_horiz(struct arcfb_par
*par
, unsigned int left
,
324 unsigned int right
, unsigned int top
, unsigned int h
)
326 unsigned int distance
, upper
, lower
;
330 lower
= min(upper
+ distance
- 1, ceil64(upper
));
332 while (distance
> 0) {
333 distance
-= ((lower
- upper
) + 1 );
334 arcfb_lcd_update_vert(par
, upper
, lower
, left
, right
);
336 lower
= min(upper
+ distance
- 1, ceil64(upper
));
341 * here we start the process of spliting out the fb update into
342 * individual blocks of pixels. we end up spliting into 64x64 blocks
343 * and finally down to 64x8 pages.
345 static void arcfb_lcd_update(struct arcfb_par
*par
, unsigned int dx
,
346 unsigned int dy
, unsigned int w
, unsigned int h
)
348 unsigned int left
, right
, distance
, y
;
350 /* align the request first */
357 right
= min(left
+ w
- 1, ceil64(left
));
359 while (distance
> 0) {
360 arcfb_lcd_update_horiz(par
, left
, right
, y
, h
);
361 distance
-= ((right
- left
) + 1);
363 right
= min(left
+ distance
- 1, ceil64(left
));
367 static void arcfb_fillrect(struct fb_info
*info
,
368 const struct fb_fillrect
*rect
)
370 struct arcfb_par
*par
= info
->par
;
372 cfb_fillrect(info
, rect
);
374 /* update the physical lcd */
375 arcfb_lcd_update(par
, rect
->dx
, rect
->dy
, rect
->width
, rect
->height
);
378 static void arcfb_copyarea(struct fb_info
*info
,
379 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 static 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 fb_info
*info
,
401 unsigned int cmd
, unsigned long arg
)
403 void __user
*argp
= (void __user
*)arg
;
404 struct arcfb_par
*par
= info
->par
;
411 /* illegal to wait on arc if no irq will occur */
415 /* wait until the Arc has generated an interrupt
416 * which will wake us up */
417 spin_lock_irqsave(&par
->lock
, flags
);
418 prepare_to_wait(&arcfb_waitq
, &wait
,
420 spin_unlock_irqrestore(&par
->lock
, flags
);
422 finish_wait(&arcfb_waitq
, &wait
);
424 case FBIO_GETCONTROL2
:
428 ctl2
= ks108_readb_ctl2(info
->par
);
429 if (copy_to_user(argp
, &ctl2
, sizeof(ctl2
)))
439 * this is the access path from userspace. they can seek and write to
440 * the fb. it's inefficient for them to do anything less than 64*8
441 * writes since we update the lcd in each write() anyway.
443 static ssize_t
arcfb_write(struct file
*file
, const char __user
*buf
, size_t count
,
446 /* modded from epson 1355 */
450 struct fb_info
*info
;
453 unsigned int fbmemlength
,x
,y
,w
,h
, bitppos
, startpos
, endpos
, bitcount
;
454 struct arcfb_par
*par
;
458 inode
= file
->f_dentry
->d_inode
;
459 fbidx
= iminor(inode
);
460 info
= registered_fb
[fbidx
];
462 if (!info
|| !info
->screen_base
)
466 xres
= info
->var
.xres
;
467 fbmemlength
= (xres
* info
->var
.yres
)/8;
473 if ((count
+ p
) > fbmemlength
) {
474 count
= fbmemlength
- p
;
481 base_addr
= info
->screen_base
;
482 count
-= copy_from_user(base_addr
+ p
, buf
, count
);
489 startpos
= floorXres(bitppos
, xres
);
490 endpos
= ceilXres((bitppos
+ (count
*8)), xres
);
491 bitcount
= endpos
- startpos
;
497 arcfb_lcd_update(par
, x
, y
, w
, h
);
504 static struct fb_ops arcfb_ops
= {
505 .owner
= THIS_MODULE
,
506 .fb_open
= arcfb_open
,
507 .fb_write
= arcfb_write
,
508 .fb_release
= arcfb_release
,
509 .fb_pan_display
= arcfb_pan_display
,
510 .fb_fillrect
= arcfb_fillrect
,
511 .fb_copyarea
= arcfb_copyarea
,
512 .fb_imageblit
= arcfb_imageblit
,
513 .fb_ioctl
= arcfb_ioctl
,
516 static int __init
arcfb_probe(struct platform_device
*dev
)
518 struct fb_info
*info
;
519 int retval
= -ENOMEM
;
521 unsigned char *videomemory
;
522 struct arcfb_par
*par
;
525 videomemorysize
= (((64*64)*num_cols
)*num_rows
)/8;
527 /* We need a flat backing store for the Arc's
528 less-flat actual paged framebuffer */
529 if (!(videomemory
= vmalloc(videomemorysize
)))
532 memset(videomemory
, 0, videomemorysize
);
534 info
= framebuffer_alloc(sizeof(struct arcfb_par
), &dev
->dev
);
538 info
->screen_base
= (char __iomem
*)videomemory
;
539 info
->fbops
= &arcfb_ops
;
541 info
->var
= arcfb_var
;
542 info
->fix
= arcfb_fix
;
546 if (!dio_addr
|| !cio_addr
|| !c2io_addr
) {
547 printk(KERN_WARNING
"no IO addresses supplied\n");
550 par
->dio_addr
= dio_addr
;
551 par
->cio_addr
= cio_addr
;
552 par
->c2io_addr
= c2io_addr
;
553 par
->cslut
[0] = 0x00;
554 par
->cslut
[1] = 0x06;
555 info
->flags
= FBINFO_FLAG_DEFAULT
;
556 spin_lock_init(&par
->lock
);
557 retval
= register_framebuffer(info
);
560 platform_set_drvdata(dev
, info
);
563 if (request_irq(par
->irq
, &arcfb_interrupt
, IRQF_SHARED
,
566 "arcfb: Failed req IRQ %d\n", par
->irq
);
571 "fb%d: Arc frame buffer device, using %dK of video memory\n",
572 info
->node
, videomemorysize
>> 10);
574 /* this inits the lcd but doesn't clear dirty pixels */
575 for (i
= 0; i
< num_cols
* num_rows
; i
++) {
576 ks108_writeb_ctl(par
, i
, KS_DPY_OFF
);
577 ks108_set_start_line(par
, i
, 0);
578 ks108_set_yaddr(par
, i
, 0);
579 ks108_set_xaddr(par
, i
, 0);
580 ks108_writeb_ctl(par
, i
, KS_DPY_ON
);
583 /* if we were told to splash the screen, we just clear it */
585 for (i
= 0; i
< num_cols
* num_rows
; i
++) {
586 printk(KERN_INFO
"fb%d: splashing lcd %d\n",
588 ks108_set_start_line(par
, i
, 0);
589 ks108_clear_lcd(par
, i
);
595 framebuffer_release(info
);
601 static int arcfb_remove(struct platform_device
*dev
)
603 struct fb_info
*info
= platform_get_drvdata(dev
);
606 unregister_framebuffer(info
);
607 vfree(info
->screen_base
);
608 framebuffer_release(info
);
613 static struct platform_driver arcfb_driver
= {
614 .probe
= arcfb_probe
,
615 .remove
= arcfb_remove
,
621 static struct platform_device
*arcfb_device
;
623 static int __init
arcfb_init(void)
630 ret
= platform_driver_register(&arcfb_driver
);
632 arcfb_device
= platform_device_alloc("arcfb", 0);
634 ret
= platform_device_add(arcfb_device
);
639 platform_device_put(arcfb_device
);
640 platform_driver_unregister(&arcfb_driver
);
647 static void __exit
arcfb_exit(void)
649 platform_device_unregister(arcfb_device
);
650 platform_driver_unregister(&arcfb_driver
);
653 module_param(num_cols
, ulong
, 0);
654 MODULE_PARM_DESC(num_cols
, "Num horiz panels, eg: 2 = 128 bit wide");
655 module_param(num_rows
, ulong
, 0);
656 MODULE_PARM_DESC(num_rows
, "Num vert panels, eg: 1 = 64 bit high");
657 module_param(nosplash
, uint
, 0);
658 MODULE_PARM_DESC(nosplash
, "Disable doing the splash screen");
659 module_param(arcfb_enable
, uint
, 0);
660 MODULE_PARM_DESC(arcfb_enable
, "Enable communication with Arc board");
661 module_param(dio_addr
, ulong
, 0);
662 MODULE_PARM_DESC(dio_addr
, "IO address for data, eg: 0x480");
663 module_param(cio_addr
, ulong
, 0);
664 MODULE_PARM_DESC(cio_addr
, "IO address for control, eg: 0x400");
665 module_param(c2io_addr
, ulong
, 0);
666 MODULE_PARM_DESC(c2io_addr
, "IO address for secondary control, eg: 0x408");
667 module_param(splashval
, ulong
, 0);
668 MODULE_PARM_DESC(splashval
, "Splash pattern: 0xFF is black, 0x00 is green");
669 module_param(tuhold
, ulong
, 0);
670 MODULE_PARM_DESC(tuhold
, "Time to hold between strobing data to Arc board");
671 module_param(irq
, uint
, 0);
672 MODULE_PARM_DESC(irq
, "IRQ for the Arc board");
674 module_init(arcfb_init
);
675 module_exit(arcfb_exit
);
677 MODULE_DESCRIPTION("fbdev driver for Arc monochrome LCD board");
678 MODULE_AUTHOR("Jaya Kumar");
679 MODULE_LICENSE("GPL");