Fix calculation in move_freepages_block for counting pages
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / char / lcd.c
blob4fe9206f84deaffcdb5ea044f918c3f710f5f25e
1 /*
2 * LCD, LED and Button interface for Cobalt
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
8 * Copyright (C) 1996, 1997 by Andrew Bose
10 * Linux kernel version history:
11 * March 2001: Ported from 2.0.34 by Liam Davies
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/miscdevice.h>
17 #include <linux/slab.h>
18 #include <linux/ioport.h>
19 #include <linux/fcntl.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/netdevice.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
25 #include <asm/io.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
29 #include "lcd.h"
31 static int lcd_ioctl(struct inode *inode, struct file *file,
32 unsigned int cmd, unsigned long arg);
34 static unsigned int lcd_present = 1;
36 /* used in arch/mips/cobalt/reset.c */
37 int led_state = 0;
39 #if defined(CONFIG_TULIP) && 0
41 #define MAX_INTERFACES 8
42 static linkcheck_func_t linkcheck_callbacks[MAX_INTERFACES];
43 static void *linkcheck_cookies[MAX_INTERFACES];
45 int lcd_register_linkcheck_func(int iface_num, void *func, void *cookie)
47 if (iface_num < 0 ||
48 iface_num >= MAX_INTERFACES ||
49 linkcheck_callbacks[iface_num] != NULL)
50 return -1;
51 linkcheck_callbacks[iface_num] = (linkcheck_func_t) func;
52 linkcheck_cookies[iface_num] = cookie;
53 return 0;
55 #endif
57 static int lcd_ioctl(struct inode *inode, struct file *file,
58 unsigned int cmd, unsigned long arg)
60 struct lcd_display button_display;
61 unsigned long address, a;
63 switch (cmd) {
64 case LCD_On:
65 udelay(150);
66 BusyCheck();
67 LCDWriteInst(0x0F);
68 break;
70 case LCD_Off:
71 udelay(150);
72 BusyCheck();
73 LCDWriteInst(0x08);
74 break;
76 case LCD_Reset:
77 udelay(150);
78 LCDWriteInst(0x3F);
79 udelay(150);
80 LCDWriteInst(0x3F);
81 udelay(150);
82 LCDWriteInst(0x3F);
83 udelay(150);
84 LCDWriteInst(0x3F);
85 udelay(150);
86 LCDWriteInst(0x01);
87 udelay(150);
88 LCDWriteInst(0x06);
89 break;
91 case LCD_Clear:
92 udelay(150);
93 BusyCheck();
94 LCDWriteInst(0x01);
95 break;
97 case LCD_Cursor_Left:
98 udelay(150);
99 BusyCheck();
100 LCDWriteInst(0x10);
101 break;
103 case LCD_Cursor_Right:
104 udelay(150);
105 BusyCheck();
106 LCDWriteInst(0x14);
107 break;
109 case LCD_Cursor_Off:
110 udelay(150);
111 BusyCheck();
112 LCDWriteInst(0x0C);
113 break;
115 case LCD_Cursor_On:
116 udelay(150);
117 BusyCheck();
118 LCDWriteInst(0x0F);
119 break;
121 case LCD_Blink_Off:
122 udelay(150);
123 BusyCheck();
124 LCDWriteInst(0x0E);
125 break;
127 case LCD_Get_Cursor_Pos:{
128 struct lcd_display display;
130 udelay(150);
131 BusyCheck();
132 display.cursor_address = (LCDReadInst);
133 display.cursor_address =
134 (display.cursor_address & 0x07F);
135 if (copy_to_user
136 ((struct lcd_display *) arg, &display,
137 sizeof(struct lcd_display)))
138 return -EFAULT;
140 break;
144 case LCD_Set_Cursor_Pos:{
145 struct lcd_display display;
147 if (copy_from_user
148 (&display, (struct lcd_display *) arg,
149 sizeof(struct lcd_display)))
150 return -EFAULT;
152 a = (display.cursor_address | kLCD_Addr);
154 udelay(150);
155 BusyCheck();
156 LCDWriteInst(a);
158 break;
161 case LCD_Get_Cursor:{
162 struct lcd_display display;
164 udelay(150);
165 BusyCheck();
166 display.character = LCDReadData;
168 if (copy_to_user
169 ((struct lcd_display *) arg, &display,
170 sizeof(struct lcd_display)))
171 return -EFAULT;
172 udelay(150);
173 BusyCheck();
174 LCDWriteInst(0x10);
176 break;
179 case LCD_Set_Cursor:{
180 struct lcd_display display;
182 if (copy_from_user
183 (&display, (struct lcd_display *) arg,
184 sizeof(struct lcd_display)))
185 return -EFAULT;
187 udelay(150);
188 BusyCheck();
189 LCDWriteData(display.character);
190 udelay(150);
191 BusyCheck();
192 LCDWriteInst(0x10);
194 break;
198 case LCD_Disp_Left:
199 udelay(150);
200 BusyCheck();
201 LCDWriteInst(0x18);
202 break;
204 case LCD_Disp_Right:
205 udelay(150);
206 BusyCheck();
207 LCDWriteInst(0x1C);
208 break;
210 case LCD_Home:
211 udelay(150);
212 BusyCheck();
213 LCDWriteInst(0x02);
214 break;
216 case LCD_Write:{
217 struct lcd_display display;
218 unsigned int index;
221 if (copy_from_user
222 (&display, (struct lcd_display *) arg,
223 sizeof(struct lcd_display)))
224 return -EFAULT;
226 udelay(150);
227 BusyCheck();
228 LCDWriteInst(0x80);
229 udelay(150);
230 BusyCheck();
232 for (index = 0; index < (display.size1); index++) {
233 udelay(150);
234 BusyCheck();
235 LCDWriteData(display.line1[index]);
236 BusyCheck();
239 udelay(150);
240 BusyCheck();
241 LCDWriteInst(0xC0);
242 udelay(150);
243 BusyCheck();
244 for (index = 0; index < (display.size2); index++) {
245 udelay(150);
246 BusyCheck();
247 LCDWriteData(display.line2[index]);
250 break;
253 case LCD_Read:{
254 struct lcd_display display;
256 BusyCheck();
257 for (address = kDD_R00; address <= kDD_R01;
258 address++) {
259 a = (address | kLCD_Addr);
261 udelay(150);
262 BusyCheck();
263 LCDWriteInst(a);
264 udelay(150);
265 BusyCheck();
266 display.line1[address] = LCDReadData;
269 display.line1[0x27] = '\0';
271 for (address = kDD_R10; address <= kDD_R11;
272 address++) {
273 a = (address | kLCD_Addr);
275 udelay(150);
276 BusyCheck();
277 LCDWriteInst(a);
279 udelay(150);
280 BusyCheck();
281 display.line2[address - 0x40] =
282 LCDReadData;
285 display.line2[0x27] = '\0';
287 if (copy_to_user
288 ((struct lcd_display *) arg, &display,
289 sizeof(struct lcd_display)))
290 return -EFAULT;
291 break;
294 // set all GPIO leds to led_display.leds
296 case LED_Set:{
297 struct lcd_display led_display;
300 if (copy_from_user
301 (&led_display, (struct lcd_display *) arg,
302 sizeof(struct lcd_display)))
303 return -EFAULT;
305 led_state = led_display.leds;
306 LEDSet(led_state);
308 break;
312 // set only bit led_display.leds
314 case LED_Bit_Set:{
315 unsigned int i;
316 int bit = 1;
317 struct lcd_display led_display;
320 if (copy_from_user
321 (&led_display, (struct lcd_display *) arg,
322 sizeof(struct lcd_display)))
323 return -EFAULT;
325 for (i = 0; i < (int) led_display.leds; i++) {
326 bit = 2 * bit;
329 led_state = led_state | bit;
330 LEDSet(led_state);
331 break;
334 // clear only bit led_display.leds
336 case LED_Bit_Clear:{
337 unsigned int i;
338 int bit = 1;
339 struct lcd_display led_display;
342 if (copy_from_user
343 (&led_display, (struct lcd_display *) arg,
344 sizeof(struct lcd_display)))
345 return -EFAULT;
347 for (i = 0; i < (int) led_display.leds; i++) {
348 bit = 2 * bit;
351 led_state = led_state & ~bit;
352 LEDSet(led_state);
353 break;
357 case BUTTON_Read:{
358 button_display.buttons = GPIRead;
359 if (copy_to_user
360 ((struct lcd_display *) arg, &button_display,
361 sizeof(struct lcd_display)))
362 return -EFAULT;
363 break;
366 case LINK_Check:{
367 button_display.buttons =
368 *((volatile unsigned long *) (0xB0100060));
369 if (copy_to_user
370 ((struct lcd_display *) arg, &button_display,
371 sizeof(struct lcd_display)))
372 return -EFAULT;
373 break;
376 case LINK_Check_2:{
377 int iface_num;
379 /* panel-utils should pass in the desired interface status is wanted for
380 * in "buttons" of the structure. We will set this to non-zero if the
381 * link is in fact up for the requested interface. --DaveM
383 if (copy_from_user
384 (&button_display, (struct lcd_display *) arg,
385 sizeof(button_display)))
386 return -EFAULT;
387 iface_num = button_display.buttons;
388 #if defined(CONFIG_TULIP) && 0
389 if (iface_num >= 0 &&
390 iface_num < MAX_INTERFACES &&
391 linkcheck_callbacks[iface_num] != NULL) {
392 button_display.buttons =
393 linkcheck_callbacks[iface_num]
394 (linkcheck_cookies[iface_num]);
395 } else
396 #endif
397 button_display.buttons = 0;
399 if (__copy_to_user
400 ((struct lcd_display *) arg, &button_display,
401 sizeof(struct lcd_display)))
402 return -EFAULT;
403 break;
406 default:
407 return -EINVAL;
411 return 0;
415 static int lcd_open(struct inode *inode, struct file *file)
417 if (!lcd_present)
418 return -ENXIO;
419 else
420 return 0;
423 /* Only RESET or NEXT counts as button pressed */
425 static inline int button_pressed(void)
427 unsigned long buttons = GPIRead;
429 if ((buttons == BUTTON_Next) || (buttons == BUTTON_Next_B)
430 || (buttons == BUTTON_Reset_B))
431 return buttons;
432 return 0;
435 /* LED daemon sits on this and we wake him up once a key is pressed. */
437 static int lcd_waiters = 0;
439 static ssize_t lcd_read(struct file *file, char *buf,
440 size_t count, loff_t *ofs)
442 long buttons_now;
444 if (lcd_waiters > 0)
445 return -EINVAL;
447 lcd_waiters++;
448 while (((buttons_now = (long) button_pressed()) == 0) &&
449 !(signal_pending(current))) {
450 msleep_interruptible(2000);
452 lcd_waiters--;
454 if (signal_pending(current))
455 return -ERESTARTSYS;
456 return buttons_now;
460 * The various file operations we support.
463 static const struct file_operations lcd_fops = {
464 .read = lcd_read,
465 .ioctl = lcd_ioctl,
466 .open = lcd_open,
469 static struct miscdevice lcd_dev = {
470 MISC_DYNAMIC_MINOR,
471 "lcd",
472 &lcd_fops
475 static int lcd_init(void)
477 int ret;
478 unsigned long data;
480 pr_info("%s\n", LCD_DRIVER);
481 ret = misc_register(&lcd_dev);
482 if (ret) {
483 printk(KERN_WARNING LCD "Unable to register misc device.\n");
484 return ret;
487 /* Check region? Naaah! Just snarf it up. */
488 /* request_region(RTC_PORT(0), RTC_IO_EXTENT, "lcd");*/
490 udelay(150);
491 data = LCDReadData;
492 if ((data & 0x000000FF) == (0x00)) {
493 lcd_present = 0;
494 pr_info(LCD "LCD Not Present\n");
495 } else {
496 lcd_present = 1;
497 WRITE_GAL(kGal_DevBank2PReg, kGal_DevBank2Cfg);
498 WRITE_GAL(kGal_DevBank3PReg, kGal_DevBank3Cfg);
501 return 0;
504 static void __exit lcd_exit(void)
506 misc_deregister(&lcd_dev);
509 module_init(lcd_init);
510 module_exit(lcd_exit);
512 MODULE_AUTHOR("Andrew Bose");
513 MODULE_LICENSE("GPL");