xref: /linux/drivers/input/keyboard/locomokbd.c (revision 6b995751c2e851d2bc9c277b5884d0adb519e31d)
1 /*
2  *  Copyright (c) 2005 John Lenz
3  *
4  * Based on from xtkbd.c
5  */
6 
7 /*
8  * LoCoMo keyboard driver for Linux/ARM
9  */
10 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  */
27 
28 #include <linux/config.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/input.h>
33 #include <linux/delay.h>
34 #include <linux/device.h>
35 #include <linux/interrupt.h>
36 #include <linux/ioport.h>
37 
38 #include <asm/hardware/locomo.h>
39 #include <asm/irq.h>
40 
41 MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
42 MODULE_DESCRIPTION("LoCoMo keyboard driver");
43 MODULE_LICENSE("GPL");
44 
45 #define LOCOMOKBD_NUMKEYS	128
46 
47 #define KEY_ACTIVITY		KEY_F16
48 #define KEY_CONTACT		KEY_F18
49 #define KEY_CENTER		KEY_F15
50 
51 static unsigned char locomokbd_keycode[LOCOMOKBD_NUMKEYS] = {
52 	0, KEY_ESC, KEY_ACTIVITY, 0, 0, 0, 0, 0, 0, 0,				/* 0 - 9 */
53 	0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_HOME, KEY_CONTACT,			/* 10 - 19 */
54 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0,						/* 20 - 29 */
55 	0, 0, 0, KEY_CENTER, 0, KEY_MAIL, 0, 0, 0, 0,				/* 30 - 39 */
56 	0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RIGHT,					/* 40 - 49 */
57 	KEY_UP, KEY_LEFT, 0, 0, KEY_P, 0, KEY_O, KEY_I, KEY_Y, KEY_T,		/* 50 - 59 */
58 	KEY_E, KEY_W, 0, 0, 0, 0, KEY_DOWN, KEY_ENTER, 0, 0,			/* 60 - 69 */
59 	KEY_BACKSPACE, 0, KEY_L, KEY_U, KEY_H, KEY_R, KEY_D, KEY_Q, 0, 0,	/* 70 - 79 */
60 	0, 0, 0, 0, 0, 0, KEY_ENTER, KEY_RIGHTSHIFT, KEY_K, KEY_J,		/* 80 - 89 */
61 	KEY_G, KEY_F, KEY_X, KEY_S, 0, 0, 0, 0, 0, 0,				/* 90 - 99 */
62 	0, 0, KEY_DOT, 0, KEY_COMMA, KEY_N, KEY_B, KEY_C, KEY_Z, KEY_A,		/* 100 - 109 */
63 	KEY_LEFTSHIFT, KEY_TAB, KEY_LEFTCTRL, 0, 0, 0, 0, 0, 0, 0,		/* 110 - 119 */
64 	KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0		/* 120 - 128 */
65 };
66 
67 #define KB_ROWS			16
68 #define KB_COLS			8
69 #define KB_ROWMASK(r)		(1 << (r))
70 #define SCANCODE(c,r)		( ((c)<<4) + (r) + 1 )
71 #define	NR_SCANCODES		128
72 
73 #define KB_DELAY		8
74 #define SCAN_INTERVAL		(HZ/10)
75 #define LOCOMOKBD_PRESSED	1
76 
77 struct locomokbd {
78 	unsigned char keycode[LOCOMOKBD_NUMKEYS];
79 	struct input_dev *input;
80 	char phys[32];
81 
82 	struct locomo_dev *ldev;
83 	unsigned long base;
84 	spinlock_t lock;
85 
86 	struct timer_list timer;
87 };
88 
89 /* helper functions for reading the keyboard matrix */
90 static inline void locomokbd_charge_all(unsigned long membase)
91 {
92 	locomo_writel(0x00FF, membase + LOCOMO_KSC);
93 }
94 
95 static inline void locomokbd_activate_all(unsigned long membase)
96 {
97 	unsigned long r;
98 
99 	locomo_writel(0, membase + LOCOMO_KSC);
100 	r = locomo_readl(membase + LOCOMO_KIC);
101 	r &= 0xFEFF;
102 	locomo_writel(r, membase + LOCOMO_KIC);
103 }
104 
105 static inline void locomokbd_activate_col(unsigned long membase, int col)
106 {
107 	unsigned short nset;
108 	unsigned short nbset;
109 
110 	nset = 0xFF & ~(1 << col);
111 	nbset = (nset << 8) + nset;
112 	locomo_writel(nbset, membase + LOCOMO_KSC);
113 }
114 
115 static inline void locomokbd_reset_col(unsigned long membase, int col)
116 {
117 	unsigned short nbset;
118 
119 	nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
120 	locomo_writel(nbset, membase + LOCOMO_KSC);
121 }
122 
123 /*
124  * The LoCoMo keyboard only generates interrupts when a key is pressed.
125  * So when a key is pressed, we enable a timer.  This timer scans the
126  * keyboard, and this is how we detect when the key is released.
127  */
128 
129 /* Scan the hardware keyboard and push any changes up through the input layer */
130 static void locomokbd_scankeyboard(struct locomokbd *locomokbd, struct pt_regs *regs)
131 {
132 	unsigned int row, col, rowd, scancode;
133 	unsigned long flags;
134 	unsigned int num_pressed;
135 	unsigned long membase = locomokbd->base;
136 
137 	spin_lock_irqsave(&locomokbd->lock, flags);
138 
139 	input_regs(locomokbd->input, regs);
140 
141 	locomokbd_charge_all(membase);
142 
143 	num_pressed = 0;
144 	for (col = 0; col < KB_COLS; col++) {
145 
146 		locomokbd_activate_col(membase, col);
147 		udelay(KB_DELAY);
148 
149 		rowd = ~locomo_readl(membase + LOCOMO_KIB);
150 		for (row = 0; row < KB_ROWS; row++) {
151 			scancode = SCANCODE(col, row);
152 			if (rowd & KB_ROWMASK(row)) {
153 				num_pressed += 1;
154 				input_report_key(locomokbd->input, locomokbd->keycode[scancode], 1);
155 			} else {
156 				input_report_key(locomokbd->input, locomokbd->keycode[scancode], 0);
157 			}
158 		}
159 		locomokbd_reset_col(membase, col);
160 	}
161 	locomokbd_activate_all(membase);
162 
163 	input_sync(locomokbd->input);
164 
165 	/* if any keys are pressed, enable the timer */
166 	if (num_pressed)
167 		mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
168 
169 	spin_unlock_irqrestore(&locomokbd->lock, flags);
170 }
171 
172 /*
173  * LoCoMo keyboard interrupt handler.
174  */
175 static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
176 {
177 	struct locomokbd *locomokbd = dev_id;
178 	/** wait chattering delay **/
179 	udelay(100);
180 
181 	locomokbd_scankeyboard(locomokbd, regs);
182 
183 	return IRQ_HANDLED;
184 }
185 
186 /*
187  * LoCoMo timer checking for released keys
188  */
189 static void locomokbd_timer_callback(unsigned long data)
190 {
191 	struct locomokbd *locomokbd = (struct locomokbd *) data;
192 	locomokbd_scankeyboard(locomokbd, NULL);
193 }
194 
195 static int locomokbd_probe(struct locomo_dev *dev)
196 {
197 	struct locomokbd *locomokbd;
198 	struct input_dev *input_dev;
199 	int i, ret;
200 
201 	locomokbd = kzalloc(sizeof(struct locomokbd), GFP_KERNEL);
202 	input_dev = input_allocate_device();
203 	if (!locomokbd || !input_dev) {
204 		ret = -ENOMEM;
205 		goto free;
206 	}
207 
208 	/* try and claim memory region */
209 	if (!request_mem_region((unsigned long) dev->mapbase,
210 				dev->length,
211 				LOCOMO_DRIVER_NAME(dev))) {
212 		ret = -EBUSY;
213 		printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
214 		goto free;
215 	}
216 
217 	locomokbd->ldev = dev;
218 	locomo_set_drvdata(dev, locomokbd);
219 
220 	locomokbd->base = (unsigned long) dev->mapbase;
221 
222 	spin_lock_init(&locomokbd->lock);
223 
224 	init_timer(&locomokbd->timer);
225 	locomokbd->timer.function = locomokbd_timer_callback;
226 	locomokbd->timer.data = (unsigned long) locomokbd;
227 
228 	locomokbd->input = input_dev;
229 	strcpy(locomokbd->phys, "locomokbd/input0");
230 
231 	input_dev->name = "LoCoMo keyboard";
232 	input_dev->phys = locomokbd->phys;
233 	input_dev->id.bustype = BUS_HOST;
234 	input_dev->id.vendor = 0x0001;
235 	input_dev->id.product = 0x0001;
236 	input_dev->id.version = 0x0100;
237 	input_dev->private = locomokbd;
238 
239 	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
240 	input_dev->keycode = locomokbd->keycode;
241 	input_dev->keycodesize = sizeof(unsigned char);
242 	input_dev->keycodemax = ARRAY_SIZE(locomokbd_keycode);
243 
244 	memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
245 	for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
246 		set_bit(locomokbd->keycode[i], input_dev->keybit);
247 	clear_bit(0, input_dev->keybit);
248 
249 	/* attempt to get the interrupt */
250 	ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
251 	if (ret) {
252 		printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
253 		goto out;
254 	}
255 
256 	input_register_device(locomokbd->input);
257 
258 	return 0;
259 
260 out:
261 	release_mem_region((unsigned long) dev->mapbase, dev->length);
262 	locomo_set_drvdata(dev, NULL);
263 free:
264 	input_free_device(input_dev);
265 	kfree(locomokbd);
266 
267 	return ret;
268 }
269 
270 static int locomokbd_remove(struct locomo_dev *dev)
271 {
272 	struct locomokbd *locomokbd = locomo_get_drvdata(dev);
273 
274 	free_irq(dev->irq[0], locomokbd);
275 
276 	del_timer_sync(&locomokbd->timer);
277 
278 	input_unregister_device(locomokbd->input);
279 	locomo_set_drvdata(dev, NULL);
280 
281 	release_mem_region((unsigned long) dev->mapbase, dev->length);
282 
283 	kfree(locomokbd);
284 
285 	return 0;
286 }
287 
288 static struct locomo_driver keyboard_driver = {
289 	.drv = {
290 		.name = "locomokbd"
291 	},
292 	.devid	= LOCOMO_DEVID_KEYBOARD,
293 	.probe	= locomokbd_probe,
294 	.remove	= locomokbd_remove,
295 };
296 
297 static int __init locomokbd_init(void)
298 {
299 	return locomo_driver_register(&keyboard_driver);
300 }
301 
302 static void __exit locomokbd_exit(void)
303 {
304 	locomo_driver_unregister(&keyboard_driver);
305 }
306 
307 module_init(locomokbd_init);
308 module_exit(locomokbd_exit);
309