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
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
25 * - testing with interrupt hw
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>
41 #include <linux/vmalloc.h>
42 #include <linux/delay.h>
43 #include <linux/interrupt.h>
46 #include <linux/init.h>
47 #include <linux/arcfb.h>
48 #include <linux/platform_device.h>
50 #include <linux/uaccess.h>
52 #define floor8(a) (a&(~0x07))
53 #define floorXres(a,xres) (a&(~(xres - 1)))
54 #define iceil8(a) (((int)((a+7)/8))*8)
55 #define ceil64(a) (a|0x3F)
56 #define ceilXres(a,xres) (a|(xres - 1))
58 /* ks108 chipset specific defines and code */
60 #define KS_SET_DPY_START_LINE 0xC0
61 #define KS_SET_PAGE_NUM 0xB8
65 #define KS_SEL_CMD 0x08
66 #define KS_SEL_DATA 0x00
67 #define KS_DPY_ON 0x3F
68 #define KS_DPY_OFF 0x3E
69 #define KS_INTACK 0x40
70 #define KS_CLRINT 0x02
73 unsigned long dio_addr
;
74 unsigned long cio_addr
;
75 unsigned long c2io_addr
;
77 unsigned char cslut
[9];
83 static const struct fb_fix_screeninfo arcfb_fix
= {
85 .type
= FB_TYPE_PACKED_PIXELS
,
86 .visual
= FB_VISUAL_MONO01
,
90 .accel
= FB_ACCEL_NONE
,
93 static const struct fb_var_screeninfo arcfb_var
= {
102 static unsigned long num_cols
;
103 static unsigned long num_rows
;
104 static unsigned long dio_addr
;
105 static unsigned long cio_addr
;
106 static unsigned long c2io_addr
;
107 static unsigned long splashval
;
108 static unsigned long tuhold
;
109 static unsigned int nosplash
;
110 static unsigned int arcfb_enable
;
111 static unsigned int irq
;
113 static DECLARE_WAIT_QUEUE_HEAD(arcfb_waitq
);
115 static void ks108_writeb_ctl(struct arcfb_par
*par
,
116 unsigned int chipindex
, unsigned char value
)
118 unsigned char chipselval
= par
->cslut
[chipindex
];
120 outb(chipselval
|KS_CEHI
|KS_SEL_CMD
, par
->cio_addr
);
121 outb(value
, par
->dio_addr
);
123 outb(chipselval
|KS_CELO
|KS_SEL_CMD
, par
->cio_addr
);
126 static void ks108_writeb_mainctl(struct arcfb_par
*par
, unsigned char value
)
129 outb(value
, par
->cio_addr
);
133 static unsigned char ks108_readb_ctl2(struct arcfb_par
*par
)
135 return inb(par
->c2io_addr
);
138 static void ks108_writeb_data(struct arcfb_par
*par
,
139 unsigned int chipindex
, unsigned char value
)
141 unsigned char chipselval
= par
->cslut
[chipindex
];
143 outb(chipselval
|KS_CEHI
|KS_SEL_DATA
, par
->cio_addr
);
144 outb(value
, par
->dio_addr
);
146 outb(chipselval
|KS_CELO
|KS_SEL_DATA
, par
->cio_addr
);
149 static void ks108_set_start_line(struct arcfb_par
*par
,
150 unsigned int chipindex
, unsigned char y
)
152 ks108_writeb_ctl(par
, chipindex
, KS_SET_DPY_START_LINE
|y
);
155 static void ks108_set_yaddr(struct arcfb_par
*par
,
156 unsigned int chipindex
, unsigned char y
)
158 ks108_writeb_ctl(par
, chipindex
, KS_SET_PAGE_NUM
|y
);
161 static void ks108_set_xaddr(struct arcfb_par
*par
,
162 unsigned int chipindex
, unsigned char x
)
164 ks108_writeb_ctl(par
, chipindex
, KS_SET_X
|x
);
167 static void ks108_clear_lcd(struct arcfb_par
*par
, unsigned int chipindex
)
171 for (i
= 0; i
<= 8; i
++) {
172 ks108_set_yaddr(par
, chipindex
, i
);
173 ks108_set_xaddr(par
, chipindex
, 0);
174 for (j
= 0; j
< 64; j
++) {
175 ks108_writeb_data(par
, chipindex
,
176 (unsigned char) splashval
);
181 /* main arcfb functions */
183 static int arcfb_open(struct fb_info
*info
, int user
)
185 struct arcfb_par
*par
= info
->par
;
187 atomic_inc(&par
->ref_count
);
191 static int arcfb_release(struct fb_info
*info
, int user
)
193 struct arcfb_par
*par
= info
->par
;
194 int count
= atomic_read(&par
->ref_count
);
198 atomic_dec(&par
->ref_count
);
202 static int arcfb_pan_display(struct fb_var_screeninfo
*var
,
203 struct fb_info
*info
)
206 struct arcfb_par
*par
= info
->par
;
208 if ((var
->vmode
& FB_VMODE_YWRAP
) && (var
->yoffset
< 64)
209 && (info
->var
.yres
<= 64)) {
210 for (i
= 0; i
< num_cols
; i
++) {
211 ks108_set_start_line(par
, i
, var
->yoffset
);
213 info
->var
.yoffset
= var
->yoffset
;
220 static irqreturn_t
arcfb_interrupt(int vec
, void *dev_instance
)
222 struct fb_info
*info
= dev_instance
;
223 unsigned char ctl2status
;
224 struct arcfb_par
*par
= info
->par
;
226 ctl2status
= ks108_readb_ctl2(par
);
228 if (!(ctl2status
& KS_INTACK
)) /* not arc generated interrupt */
231 ks108_writeb_mainctl(par
, KS_CLRINT
);
233 spin_lock(&par
->lock
);
234 if (waitqueue_active(&arcfb_waitq
)) {
235 wake_up(&arcfb_waitq
);
237 spin_unlock(&par
->lock
);
243 * here we handle a specific page on the lcd. the complexity comes from
244 * the fact that the fb is laidout in 8xX vertical columns. we extract
245 * each write of 8 vertical pixels. then we shift out as we move along
246 * X. That's what rightshift does. bitmask selects the desired input bit.
248 static void arcfb_lcd_update_page(struct arcfb_par
*par
, unsigned int upper
,
249 unsigned int left
, unsigned int right
, unsigned int distance
)
252 unsigned int xindex
, yindex
, chipindex
, linesize
;
255 unsigned char bitmask
, rightshift
;
259 chipindex
= (xindex
+ (yindex
*num_cols
));
261 ks108_set_yaddr(par
, chipindex
, upper
/8);
263 linesize
= par
->info
->var
.xres
/8;
264 src
= (unsigned char *)par
->info
->screen_buffer
+ (left
/8) +
266 ks108_set_xaddr(par
, chipindex
, left
);
270 while (left
<= right
) {
272 for (i
= 0; i
< 8; i
++) {
273 if ( i
> rightshift
) {
274 val
|= (*(src
+ (i
*linesize
)) & bitmask
)
277 val
|= (*(src
+ (i
*linesize
)) & bitmask
)
281 ks108_writeb_data(par
, chipindex
, val
);
283 if (bitmask
== 0x80) {
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.
300 static void arcfb_lcd_update_vert(struct arcfb_par
*par
, unsigned int top
,
301 unsigned int bottom
, unsigned int left
, unsigned int right
)
303 unsigned int distance
, upper
, lower
;
305 distance
= (bottom
- top
) + 1;
309 while (distance
> 0) {
311 arcfb_lcd_update_page(par
, upper
, left
, right
, 8);
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.
322 static void arcfb_lcd_update_horiz(struct arcfb_par
*par
, unsigned int left
,
323 unsigned int right
, unsigned int top
, unsigned int h
)
325 unsigned int distance
, upper
, lower
;
329 lower
= min(upper
+ distance
- 1, ceil64(upper
));
331 while (distance
> 0) {
332 distance
-= ((lower
- upper
) + 1 );
333 arcfb_lcd_update_vert(par
, upper
, lower
, left
, right
);
335 lower
= min(upper
+ distance
- 1, ceil64(upper
));
340 * here we start the process of splitting out the fb update into
341 * individual blocks of pixels. we end up splitting into 64x64 blocks
342 * and finally down to 64x8 pages.
344 static void arcfb_lcd_update(struct arcfb_par
*par
, unsigned int dx
,
345 unsigned int dy
, unsigned int w
, unsigned int h
)
347 unsigned int left
, right
, distance
, y
;
349 /* align the request first */
356 right
= min(left
+ w
- 1, ceil64(left
));
358 while (distance
> 0) {
359 arcfb_lcd_update_horiz(par
, left
, right
, y
, h
);
360 distance
-= ((right
- left
) + 1);
362 right
= min(left
+ distance
- 1, ceil64(left
));
366 static int arcfb_ioctl(struct fb_info
*info
,
367 unsigned int cmd
, unsigned long arg
)
369 void __user
*argp
= (void __user
*)arg
;
370 struct arcfb_par
*par
= info
->par
;
377 /* illegal to wait on arc if no irq will occur */
381 /* wait until the Arc has generated an interrupt
382 * which will wake us up */
383 spin_lock_irqsave(&par
->lock
, flags
);
384 prepare_to_wait(&arcfb_waitq
, &wait
,
386 spin_unlock_irqrestore(&par
->lock
, flags
);
388 finish_wait(&arcfb_waitq
, &wait
);
392 case FBIO_GETCONTROL2
:
396 ctl2
= ks108_readb_ctl2(info
->par
);
397 if (copy_to_user(argp
, &ctl2
, sizeof(ctl2
)))
406 static void arcfb_damage_range(struct fb_info
*info
, off_t off
, size_t len
)
408 struct arcfb_par
*par
= info
->par
;
409 unsigned int xres
= info
->var
.xres
;
410 unsigned int bitppos
, startpos
, endpos
, bitcount
;
411 unsigned int x
, y
, width
, height
;
414 startpos
= floorXres(bitppos
, xres
);
415 endpos
= ceilXres((bitppos
+ (len
* 8)), xres
);
416 bitcount
= endpos
- startpos
;
421 height
= bitcount
/ xres
;
423 arcfb_lcd_update(par
, x
, y
, width
, height
);
426 static void arcfb_damage_area(struct fb_info
*info
, u32 x
, u32 y
,
427 u32 width
, u32 height
)
429 struct arcfb_par
*par
= info
->par
;
431 /* update the physical lcd */
432 arcfb_lcd_update(par
, x
, y
, width
, height
);
435 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(arcfb
,
439 static const struct fb_ops arcfb_ops
= {
440 .owner
= THIS_MODULE
,
441 .fb_open
= arcfb_open
,
442 __FB_DEFAULT_DEFERRED_OPS_RDWR(arcfb
),
443 .fb_release
= arcfb_release
,
444 .fb_pan_display
= arcfb_pan_display
,
445 __FB_DEFAULT_DEFERRED_OPS_DRAW(arcfb
),
446 .fb_ioctl
= arcfb_ioctl
,
447 // .fb_mmap reqires deferred I/O
450 static int arcfb_probe(struct platform_device
*dev
)
452 struct fb_info
*info
;
453 int retval
= -ENOMEM
;
455 unsigned char *videomemory
;
456 struct arcfb_par
*par
;
459 videomemorysize
= (((64*64)*num_cols
)*num_rows
)/8;
461 /* We need a flat backing store for the Arc's
462 less-flat actual paged framebuffer */
463 videomemory
= vzalloc(videomemorysize
);
467 info
= framebuffer_alloc(sizeof(struct arcfb_par
), &dev
->dev
);
471 info
->flags
|= FBINFO_VIRTFB
;
472 info
->screen_buffer
= videomemory
;
473 info
->fbops
= &arcfb_ops
;
475 info
->var
= arcfb_var
;
476 info
->fix
= arcfb_fix
;
480 if (!dio_addr
|| !cio_addr
|| !c2io_addr
) {
481 printk(KERN_WARNING
"no IO addresses supplied\n");
484 par
->dio_addr
= dio_addr
;
485 par
->cio_addr
= cio_addr
;
486 par
->c2io_addr
= c2io_addr
;
487 par
->cslut
[0] = 0x00;
488 par
->cslut
[1] = 0x06;
489 spin_lock_init(&par
->lock
);
492 if (request_irq(par
->irq
, &arcfb_interrupt
, IRQF_SHARED
,
495 "arcfb: Failed req IRQ %d\n", par
->irq
);
500 retval
= register_framebuffer(info
);
502 goto err_register_fb
;
503 platform_set_drvdata(dev
, info
);
504 fb_info(info
, "Arc frame buffer device, using %dK of video memory\n",
505 videomemorysize
>> 10);
507 /* this inits the lcd but doesn't clear dirty pixels */
508 for (i
= 0; i
< num_cols
* num_rows
; i
++) {
509 ks108_writeb_ctl(par
, i
, KS_DPY_OFF
);
510 ks108_set_start_line(par
, i
, 0);
511 ks108_set_yaddr(par
, i
, 0);
512 ks108_set_xaddr(par
, i
, 0);
513 ks108_writeb_ctl(par
, i
, KS_DPY_ON
);
516 /* if we were told to splash the screen, we just clear it */
518 for (i
= 0; i
< num_cols
* num_rows
; i
++) {
519 fb_info(info
, "splashing lcd %d\n", i
);
520 ks108_set_start_line(par
, i
, 0);
521 ks108_clear_lcd(par
, i
);
528 free_irq(par
->irq
, info
);
530 framebuffer_release(info
);
536 static void arcfb_remove(struct platform_device
*dev
)
538 struct fb_info
*info
= platform_get_drvdata(dev
);
541 unregister_framebuffer(info
);
543 free_irq(((struct arcfb_par
*)(info
->par
))->irq
, info
);
544 vfree(info
->screen_buffer
);
545 framebuffer_release(info
);
549 static struct platform_driver arcfb_driver
= {
550 .probe
= arcfb_probe
,
551 .remove
= arcfb_remove
,
557 static struct platform_device
*arcfb_device
;
559 static int __init
arcfb_init(void)
566 ret
= platform_driver_register(&arcfb_driver
);
568 arcfb_device
= platform_device_alloc("arcfb", 0);
570 ret
= platform_device_add(arcfb_device
);
575 platform_device_put(arcfb_device
);
576 platform_driver_unregister(&arcfb_driver
);
583 static void __exit
arcfb_exit(void)
585 platform_device_unregister(arcfb_device
);
586 platform_driver_unregister(&arcfb_driver
);
589 module_param(num_cols
, ulong
, 0);
590 MODULE_PARM_DESC(num_cols
, "Num horiz panels, eg: 2 = 128 bit wide");
591 module_param(num_rows
, ulong
, 0);
592 MODULE_PARM_DESC(num_rows
, "Num vert panels, eg: 1 = 64 bit high");
593 module_param(nosplash
, uint
, 0);
594 MODULE_PARM_DESC(nosplash
, "Disable doing the splash screen");
595 module_param(arcfb_enable
, uint
, 0);
596 MODULE_PARM_DESC(arcfb_enable
, "Enable communication with Arc board");
597 module_param_hw(dio_addr
, ulong
, ioport
, 0);
598 MODULE_PARM_DESC(dio_addr
, "IO address for data, eg: 0x480");
599 module_param_hw(cio_addr
, ulong
, ioport
, 0);
600 MODULE_PARM_DESC(cio_addr
, "IO address for control, eg: 0x400");
601 module_param_hw(c2io_addr
, ulong
, ioport
, 0);
602 MODULE_PARM_DESC(c2io_addr
, "IO address for secondary control, eg: 0x408");
603 module_param(splashval
, ulong
, 0);
604 MODULE_PARM_DESC(splashval
, "Splash pattern: 0xFF is black, 0x00 is green");
605 module_param(tuhold
, ulong
, 0);
606 MODULE_PARM_DESC(tuhold
, "Time to hold between strobing data to Arc board");
607 module_param_hw(irq
, uint
, irq
, 0);
608 MODULE_PARM_DESC(irq
, "IRQ for the Arc board");
610 module_init(arcfb_init
);
611 module_exit(arcfb_exit
);
613 MODULE_DESCRIPTION("fbdev driver for Arc monochrome LCD board");
614 MODULE_AUTHOR("Jaya Kumar");
615 MODULE_LICENSE("GPL");