ACPI: thinkpad-acpi: use bitfields to hold subdriver flags
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / lcd.c
blob1f0962616ee509b2e53f996619191a933b3702dc
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>
28 #include <linux/delay.h>
30 #include "lcd.h"
32 static int lcd_ioctl(struct inode *inode, struct file *file,
33 unsigned int cmd, unsigned long arg);
35 static unsigned int lcd_present = 1;
37 /* used in arch/mips/cobalt/reset.c */
38 int led_state = 0;
40 #if defined(CONFIG_TULIP) && 0
42 #define MAX_INTERFACES 8
43 static linkcheck_func_t linkcheck_callbacks[MAX_INTERFACES];
44 static void *linkcheck_cookies[MAX_INTERFACES];
46 int lcd_register_linkcheck_func(int iface_num, void *func, void *cookie)
48 if (iface_num < 0 ||
49 iface_num >= MAX_INTERFACES ||
50 linkcheck_callbacks[iface_num] != NULL)
51 return -1;
52 linkcheck_callbacks[iface_num] = (linkcheck_func_t) func;
53 linkcheck_cookies[iface_num] = cookie;
54 return 0;
56 #endif
58 static int lcd_ioctl(struct inode *inode, struct file *file,
59 unsigned int cmd, unsigned long arg)
61 struct lcd_display button_display;
62 unsigned long address, a;
64 switch (cmd) {
65 case LCD_On:
66 udelay(150);
67 BusyCheck();
68 LCDWriteInst(0x0F);
69 break;
71 case LCD_Off:
72 udelay(150);
73 BusyCheck();
74 LCDWriteInst(0x08);
75 break;
77 case LCD_Reset:
78 udelay(150);
79 LCDWriteInst(0x3F);
80 udelay(150);
81 LCDWriteInst(0x3F);
82 udelay(150);
83 LCDWriteInst(0x3F);
84 udelay(150);
85 LCDWriteInst(0x3F);
86 udelay(150);
87 LCDWriteInst(0x01);
88 udelay(150);
89 LCDWriteInst(0x06);
90 break;
92 case LCD_Clear:
93 udelay(150);
94 BusyCheck();
95 LCDWriteInst(0x01);
96 break;
98 case LCD_Cursor_Left:
99 udelay(150);
100 BusyCheck();
101 LCDWriteInst(0x10);
102 break;
104 case LCD_Cursor_Right:
105 udelay(150);
106 BusyCheck();
107 LCDWriteInst(0x14);
108 break;
110 case LCD_Cursor_Off:
111 udelay(150);
112 BusyCheck();
113 LCDWriteInst(0x0C);
114 break;
116 case LCD_Cursor_On:
117 udelay(150);
118 BusyCheck();
119 LCDWriteInst(0x0F);
120 break;
122 case LCD_Blink_Off:
123 udelay(150);
124 BusyCheck();
125 LCDWriteInst(0x0E);
126 break;
128 case LCD_Get_Cursor_Pos:{
129 struct lcd_display display;
131 udelay(150);
132 BusyCheck();
133 display.cursor_address = (LCDReadInst);
134 display.cursor_address =
135 (display.cursor_address & 0x07F);
136 if (copy_to_user
137 ((struct lcd_display *) arg, &display,
138 sizeof(struct lcd_display)))
139 return -EFAULT;
141 break;
145 case LCD_Set_Cursor_Pos:{
146 struct lcd_display display;
148 if (copy_from_user
149 (&display, (struct lcd_display *) arg,
150 sizeof(struct lcd_display)))
151 return -EFAULT;
153 a = (display.cursor_address | kLCD_Addr);
155 udelay(150);
156 BusyCheck();
157 LCDWriteInst(a);
159 break;
162 case LCD_Get_Cursor:{
163 struct lcd_display display;
165 udelay(150);
166 BusyCheck();
167 display.character = LCDReadData;
169 if (copy_to_user
170 ((struct lcd_display *) arg, &display,
171 sizeof(struct lcd_display)))
172 return -EFAULT;
173 udelay(150);
174 BusyCheck();
175 LCDWriteInst(0x10);
177 break;
180 case LCD_Set_Cursor:{
181 struct lcd_display display;
183 if (copy_from_user
184 (&display, (struct lcd_display *) arg,
185 sizeof(struct lcd_display)))
186 return -EFAULT;
188 udelay(150);
189 BusyCheck();
190 LCDWriteData(display.character);
191 udelay(150);
192 BusyCheck();
193 LCDWriteInst(0x10);
195 break;
199 case LCD_Disp_Left:
200 udelay(150);
201 BusyCheck();
202 LCDWriteInst(0x18);
203 break;
205 case LCD_Disp_Right:
206 udelay(150);
207 BusyCheck();
208 LCDWriteInst(0x1C);
209 break;
211 case LCD_Home:
212 udelay(150);
213 BusyCheck();
214 LCDWriteInst(0x02);
215 break;
217 case LCD_Write:{
218 struct lcd_display display;
219 unsigned int index;
222 if (copy_from_user
223 (&display, (struct lcd_display *) arg,
224 sizeof(struct lcd_display)))
225 return -EFAULT;
227 udelay(150);
228 BusyCheck();
229 LCDWriteInst(0x80);
230 udelay(150);
231 BusyCheck();
233 for (index = 0; index < (display.size1); index++) {
234 udelay(150);
235 BusyCheck();
236 LCDWriteData(display.line1[index]);
237 BusyCheck();
240 udelay(150);
241 BusyCheck();
242 LCDWriteInst(0xC0);
243 udelay(150);
244 BusyCheck();
245 for (index = 0; index < (display.size2); index++) {
246 udelay(150);
247 BusyCheck();
248 LCDWriteData(display.line2[index]);
251 break;
254 case LCD_Read:{
255 struct lcd_display display;
257 BusyCheck();
258 for (address = kDD_R00; address <= kDD_R01;
259 address++) {
260 a = (address | kLCD_Addr);
262 udelay(150);
263 BusyCheck();
264 LCDWriteInst(a);
265 udelay(150);
266 BusyCheck();
267 display.line1[address] = LCDReadData;
270 display.line1[0x27] = '\0';
272 for (address = kDD_R10; address <= kDD_R11;
273 address++) {
274 a = (address | kLCD_Addr);
276 udelay(150);
277 BusyCheck();
278 LCDWriteInst(a);
280 udelay(150);
281 BusyCheck();
282 display.line2[address - 0x40] =
283 LCDReadData;
286 display.line2[0x27] = '\0';
288 if (copy_to_user
289 ((struct lcd_display *) arg, &display,
290 sizeof(struct lcd_display)))
291 return -EFAULT;
292 break;
295 // set all GPIO leds to led_display.leds
297 case LED_Set:{
298 struct lcd_display led_display;
301 if (copy_from_user
302 (&led_display, (struct lcd_display *) arg,
303 sizeof(struct lcd_display)))
304 return -EFAULT;
306 led_state = led_display.leds;
307 LEDSet(led_state);
309 break;
313 // set only bit led_display.leds
315 case LED_Bit_Set:{
316 unsigned int i;
317 int bit = 1;
318 struct lcd_display led_display;
321 if (copy_from_user
322 (&led_display, (struct lcd_display *) arg,
323 sizeof(struct lcd_display)))
324 return -EFAULT;
326 for (i = 0; i < (int) led_display.leds; i++) {
327 bit = 2 * bit;
330 led_state = led_state | bit;
331 LEDSet(led_state);
332 break;
335 // clear only bit led_display.leds
337 case LED_Bit_Clear:{
338 unsigned int i;
339 int bit = 1;
340 struct lcd_display led_display;
343 if (copy_from_user
344 (&led_display, (struct lcd_display *) arg,
345 sizeof(struct lcd_display)))
346 return -EFAULT;
348 for (i = 0; i < (int) led_display.leds; i++) {
349 bit = 2 * bit;
352 led_state = led_state & ~bit;
353 LEDSet(led_state);
354 break;
358 case BUTTON_Read:{
359 button_display.buttons = GPIRead;
360 if (copy_to_user
361 ((struct lcd_display *) arg, &button_display,
362 sizeof(struct lcd_display)))
363 return -EFAULT;
364 break;
367 case LINK_Check:{
368 button_display.buttons =
369 *((volatile unsigned long *) (0xB0100060));
370 if (copy_to_user
371 ((struct lcd_display *) arg, &button_display,
372 sizeof(struct lcd_display)))
373 return -EFAULT;
374 break;
377 case LINK_Check_2:{
378 int iface_num;
380 /* panel-utils should pass in the desired interface status is wanted for
381 * in "buttons" of the structure. We will set this to non-zero if the
382 * link is in fact up for the requested interface. --DaveM
384 if (copy_from_user
385 (&button_display, (struct lcd_display *) arg,
386 sizeof(button_display)))
387 return -EFAULT;
388 iface_num = button_display.buttons;
389 #if defined(CONFIG_TULIP) && 0
390 if (iface_num >= 0 &&
391 iface_num < MAX_INTERFACES &&
392 linkcheck_callbacks[iface_num] != NULL) {
393 button_display.buttons =
394 linkcheck_callbacks[iface_num]
395 (linkcheck_cookies[iface_num]);
396 } else
397 #endif
398 button_display.buttons = 0;
400 if (__copy_to_user
401 ((struct lcd_display *) arg, &button_display,
402 sizeof(struct lcd_display)))
403 return -EFAULT;
404 break;
407 default:
408 return -EINVAL;
412 return 0;
416 static int lcd_open(struct inode *inode, struct file *file)
418 if (!lcd_present)
419 return -ENXIO;
420 else
421 return 0;
424 /* Only RESET or NEXT counts as button pressed */
426 static inline int button_pressed(void)
428 unsigned long buttons = GPIRead;
430 if ((buttons == BUTTON_Next) || (buttons == BUTTON_Next_B)
431 || (buttons == BUTTON_Reset_B))
432 return buttons;
433 return 0;
436 /* LED daemon sits on this and we wake him up once a key is pressed. */
438 static int lcd_waiters = 0;
440 static ssize_t lcd_read(struct file *file, char *buf,
441 size_t count, loff_t *ofs)
443 long buttons_now;
445 if (lcd_waiters > 0)
446 return -EINVAL;
448 lcd_waiters++;
449 while (((buttons_now = (long) button_pressed()) == 0) &&
450 !(signal_pending(current))) {
451 msleep_interruptible(2000);
453 lcd_waiters--;
455 if (signal_pending(current))
456 return -ERESTARTSYS;
457 return buttons_now;
461 * The various file operations we support.
464 static const struct file_operations lcd_fops = {
465 .read = lcd_read,
466 .ioctl = lcd_ioctl,
467 .open = lcd_open,
470 static struct miscdevice lcd_dev = {
471 MISC_DYNAMIC_MINOR,
472 "lcd",
473 &lcd_fops
476 static int lcd_init(void)
478 int ret;
479 unsigned long data;
481 pr_info("%s\n", LCD_DRIVER);
482 ret = misc_register(&lcd_dev);
483 if (ret) {
484 printk(KERN_WARNING LCD "Unable to register misc device.\n");
485 return ret;
488 /* Check region? Naaah! Just snarf it up. */
489 /* request_region(RTC_PORT(0), RTC_IO_EXTENT, "lcd");*/
491 udelay(150);
492 data = LCDReadData;
493 if ((data & 0x000000FF) == (0x00)) {
494 lcd_present = 0;
495 pr_info(LCD "LCD Not Present\n");
496 } else {
497 lcd_present = 1;
498 WRITE_GAL(kGal_DevBank2PReg, kGal_DevBank2Cfg);
499 WRITE_GAL(kGal_DevBank3PReg, kGal_DevBank3Cfg);
502 return 0;
505 static void __exit lcd_exit(void)
507 misc_deregister(&lcd_dev);
510 module_init(lcd_init);
511 module_exit(lcd_exit);
513 MODULE_AUTHOR("Andrew Bose");
514 MODULE_LICENSE("GPL");