blob: 281fa9e6fc1fee2a0b5caf9bffd2632fe31dc0d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * /dev/lcd driver for Apple Network Servers.
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/kernel.h>
8#include <linux/miscdevice.h>
9#include <linux/fcntl.h>
Luis Henriques120d2002015-07-17 14:20:31 +010010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/delay.h>
12#include <linux/fs.h>
13
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080014#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/sections.h>
16#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/io.h>
18
Kumar Gala33d71d22007-08-20 08:50:28 -050019#include "ans-lcd.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define ANSLCD_ADDR 0xf301c000
22#define ANSLCD_CTRL_IX 0x00
23#define ANSLCD_DATA_IX 0x10
24
25static unsigned long anslcd_short_delay = 80;
26static unsigned long anslcd_long_delay = 3280;
27static volatile unsigned char __iomem *anslcd_ptr;
Thomas Gleixner95fdac72009-10-10 13:38:57 +020028static DEFINE_MUTEX(anslcd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#undef DEBUG
31
Jon Loeligeraacaf9b2005-09-17 10:36:54 -050032static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070033anslcd_write_byte_ctrl ( unsigned char c )
34{
35#ifdef DEBUG
36 printk(KERN_DEBUG "LCD: CTRL byte: %02x\n",c);
37#endif
38 out_8(anslcd_ptr + ANSLCD_CTRL_IX, c);
39 switch(c) {
40 case 1:
41 case 2:
42 case 3:
43 udelay(anslcd_long_delay); break;
44 default: udelay(anslcd_short_delay);
45 }
46}
47
Jon Loeligeraacaf9b2005-09-17 10:36:54 -050048static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070049anslcd_write_byte_data ( unsigned char c )
50{
51 out_8(anslcd_ptr + ANSLCD_DATA_IX, c);
52 udelay(anslcd_short_delay);
53}
54
Jon Loeligeraacaf9b2005-09-17 10:36:54 -050055static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -070056anslcd_write( struct file * file, const char __user * buf,
57 size_t count, loff_t *ppos )
58{
59 const char __user *p = buf;
60 int i;
61
62#ifdef DEBUG
63 printk(KERN_DEBUG "LCD: write\n");
64#endif
65
66 if (!access_ok(VERIFY_READ, buf, count))
67 return -EFAULT;
Thomas Gleixner95fdac72009-10-10 13:38:57 +020068
69 mutex_lock(&anslcd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 for ( i = *ppos; count > 0; ++i, ++p, --count )
71 {
72 char c;
73 __get_user(c, p);
74 anslcd_write_byte_data( c );
75 }
Thomas Gleixner95fdac72009-10-10 13:38:57 +020076 mutex_unlock(&anslcd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *ppos = i;
78 return p - buf;
79}
80
Thomas Gleixner95fdac72009-10-10 13:38:57 +020081static long
82anslcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 char ch, __user *temp;
Thomas Gleixner95fdac72009-10-10 13:38:57 +020085 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#ifdef DEBUG
88 printk(KERN_DEBUG "LCD: ioctl(%d,%d)\n",cmd,arg);
89#endif
90
Thomas Gleixner95fdac72009-10-10 13:38:57 +020091 mutex_lock(&anslcd_mutex);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 switch ( cmd )
94 {
95 case ANSLCD_CLEAR:
96 anslcd_write_byte_ctrl ( 0x38 );
97 anslcd_write_byte_ctrl ( 0x0f );
98 anslcd_write_byte_ctrl ( 0x06 );
99 anslcd_write_byte_ctrl ( 0x01 );
100 anslcd_write_byte_ctrl ( 0x02 );
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case ANSLCD_SENDCTRL:
103 temp = (char __user *) arg;
104 __get_user(ch, temp);
105 for (; ch; temp++) { /* FIXME: This is ugly, but should work, as a \0 byte is not a valid command code */
106 anslcd_write_byte_ctrl ( ch );
107 __get_user(ch, temp);
108 }
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 case ANSLCD_SETSHORTDELAY:
111 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200112 ret =-EACCES;
113 else
114 anslcd_short_delay=arg;
115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 case ANSLCD_SETLONGDELAY:
117 if (!capable(CAP_SYS_ADMIN))
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200118 ret = -EACCES;
119 else
120 anslcd_long_delay=arg;
121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 default:
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200123 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200125
126 mutex_unlock(&anslcd_mutex);
127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500130static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131anslcd_open( struct inode * inode, struct file * file )
132{
133 return 0;
134}
135
Arjan van de Venfa027c22007-02-12 00:55:33 -0800136const struct file_operations anslcd_fops = {
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200137 .write = anslcd_write,
138 .unlocked_ioctl = anslcd_ioctl,
139 .open = anslcd_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200140 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143static struct miscdevice anslcd_dev = {
144 ANSLCD_MINOR,
145 "anslcd",
146 &anslcd_fops
147};
148
149const char anslcd_logo[] = "********************" /* Line #1 */
150 "* LINUX! *" /* Line #3 */
151 "* Welcome to *" /* Line #2 */
152 "********************"; /* Line #4 */
153
154static int __init
155anslcd_init(void)
156{
157 int a;
158 int retval;
159 struct device_node* node;
160
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000161 node = of_find_node_by_name(NULL, "lcd");
162 if (!node || !node->parent || strcmp(node->parent->name, "gc")) {
163 of_node_put(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENODEV;
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000165 }
166 of_node_put(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 anslcd_ptr = ioremap(ANSLCD_ADDR, 0x20);
169
170 retval = misc_register(&anslcd_dev);
171 if(retval < 0){
172 printk(KERN_INFO "LCD: misc_register failed\n");
173 iounmap(anslcd_ptr);
174 return retval;
175 }
176
177#ifdef DEBUG
178 printk(KERN_DEBUG "LCD: init\n");
179#endif
180
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200181 mutex_lock(&anslcd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 anslcd_write_byte_ctrl ( 0x38 );
183 anslcd_write_byte_ctrl ( 0x0c );
184 anslcd_write_byte_ctrl ( 0x06 );
185 anslcd_write_byte_ctrl ( 0x01 );
186 anslcd_write_byte_ctrl ( 0x02 );
187 for(a=0;a<80;a++) {
188 anslcd_write_byte_data(anslcd_logo[a]);
189 }
Thomas Gleixner95fdac72009-10-10 13:38:57 +0200190 mutex_unlock(&anslcd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192}
193
194static void __exit
195anslcd_exit(void)
196{
197 misc_deregister(&anslcd_dev);
198 iounmap(anslcd_ptr);
199}
200
201module_init(anslcd_init);
202module_exit(anslcd_exit);