2 * Copyright (c) 2005 John Lenz
4 * Based on from xtkbd.c
8 * LoCoMo keyboard driver for Linux/ARM
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.
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.
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
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>
38 #include <asm/hardware/locomo.h>
41 MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
42 MODULE_DESCRIPTION("LoCoMo keyboard driver");
43 MODULE_LICENSE("GPL");
45 #define LOCOMOKBD_NUMKEYS 128
47 #define KEY_ACTIVITY KEY_F16
48 #define KEY_CONTACT KEY_F18
49 #define KEY_CENTER KEY_F15
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 */
69 #define KB_ROWMASK(r) (1 << (r))
70 #define SCANCODE(c,r) ( ((c)<<4) + (r) + 1 )
71 #define NR_SCANCODES 128
74 #define SCAN_INTERVAL (HZ/10)
75 #define LOCOMOKBD_PRESSED 1
78 unsigned char keycode[LOCOMOKBD_NUMKEYS];
79 struct input_dev input;
82 struct locomo_dev *ldev;
86 struct timer_list timer;
89 /* helper functions for reading the keyboard matrix */
90 static inline void locomokbd_charge_all(unsigned long membase)
92 locomo_writel(0x00FF, membase + LOCOMO_KSC);
95 static inline void locomokbd_activate_all(unsigned long membase)
99 locomo_writel(0, membase + LOCOMO_KSC);
100 r = locomo_readl(membase + LOCOMO_KIC);
102 locomo_writel(r, membase + LOCOMO_KIC);
105 static inline void locomokbd_activate_col(unsigned long membase, int col)
108 unsigned short nbset;
110 nset = 0xFF & ~(1 << col);
111 nbset = (nset << 8) + nset;
112 locomo_writel(nbset, membase + LOCOMO_KSC);
115 static inline void locomokbd_reset_col(unsigned long membase, int col)
117 unsigned short nbset;
119 nbset = ((0xFF & ~(1 << col)) << 8) + 0xFF;
120 locomo_writel(nbset, membase + LOCOMO_KSC);
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.
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)
132 unsigned int row, col, rowd, scancode;
134 unsigned int num_pressed;
135 unsigned long membase = locomokbd->base;
137 spin_lock_irqsave(&locomokbd->lock, flags);
140 input_regs(&locomokbd->input, regs);
142 locomokbd_charge_all(membase);
145 for (col = 0; col < KB_COLS; col++) {
147 locomokbd_activate_col(membase, col);
150 rowd = ~locomo_readl(membase + LOCOMO_KIB);
151 for (row = 0; row < KB_ROWS; row++) {
152 scancode = SCANCODE(col, row);
153 if (rowd & KB_ROWMASK(row)) {
155 input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 1);
157 input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 0);
160 locomokbd_reset_col(membase, col);
162 locomokbd_activate_all(membase);
164 input_sync(&locomokbd->input);
166 /* if any keys are pressed, enable the timer */
168 mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
170 spin_unlock_irqrestore(&locomokbd->lock, flags);
174 * LoCoMo keyboard interrupt handler.
176 static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
178 struct locomokbd *locomokbd = dev_id;
179 /** wait chattering delay **/
182 locomokbd_scankeyboard(locomokbd, regs);
188 * LoCoMo timer checking for released keys
190 static void locomokbd_timer_callback(unsigned long data)
192 struct locomokbd *locomokbd = (struct locomokbd *) data;
193 locomokbd_scankeyboard(locomokbd, NULL);
196 static int locomokbd_probe(struct locomo_dev *dev)
198 struct locomokbd *locomokbd;
201 locomokbd = kmalloc(sizeof(struct locomokbd), GFP_KERNEL);
205 memset(locomokbd, 0, sizeof(struct locomokbd));
207 /* try and claim memory region */
208 if (!request_mem_region((unsigned long) dev->mapbase,
210 LOCOMO_DRIVER_NAME(dev))) {
212 printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
216 locomokbd->ldev = dev;
217 locomo_set_drvdata(dev, locomokbd);
219 locomokbd->base = (unsigned long) dev->mapbase;
221 spin_lock_init(&locomokbd->lock);
223 init_timer(&locomokbd->timer);
224 locomokbd->timer.function = locomokbd_timer_callback;
225 locomokbd->timer.data = (unsigned long) locomokbd;
227 locomokbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
229 init_input_dev(&locomokbd->input);
230 locomokbd->input.keycode = locomokbd->keycode;
231 locomokbd->input.keycodesize = sizeof(unsigned char);
232 locomokbd->input.keycodemax = ARRAY_SIZE(locomokbd_keycode);
233 locomokbd->input.private = locomokbd;
235 memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
236 for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
237 set_bit(locomokbd->keycode[i], locomokbd->input.keybit);
238 clear_bit(0, locomokbd->input.keybit);
240 strcpy(locomokbd->phys, "locomokbd/input0");
242 locomokbd->input.name = "LoCoMo keyboard";
243 locomokbd->input.phys = locomokbd->phys;
244 locomokbd->input.id.bustype = BUS_XTKBD;
245 locomokbd->input.id.vendor = 0x0001;
246 locomokbd->input.id.product = 0x0001;
247 locomokbd->input.id.version = 0x0100;
249 /* attempt to get the interrupt */
250 ret = request_irq(dev->irq[0], locomokbd_interrupt, 0, "locomokbd", locomokbd);
252 printk(KERN_ERR "locomokbd: Can't get irq for keyboard\n");
256 input_register_device(&locomokbd->input);
258 printk(KERN_INFO "input: LoCoMo keyboard on locomokbd\n");
263 release_mem_region((unsigned long) dev->mapbase, dev->length);
264 locomo_set_drvdata(dev, NULL);
271 static int locomokbd_remove(struct locomo_dev *dev)
273 struct locomokbd *locomokbd = locomo_get_drvdata(dev);
275 free_irq(dev->irq[0], locomokbd);
277 del_timer_sync(&locomokbd->timer);
279 input_unregister_device(&locomokbd->input);
280 locomo_set_drvdata(dev, NULL);
282 release_mem_region((unsigned long) dev->mapbase, dev->length);
289 static struct locomo_driver keyboard_driver = {
293 .devid = LOCOMO_DEVID_KEYBOARD,
294 .probe = locomokbd_probe,
295 .remove = locomokbd_remove,
298 static int __init locomokbd_init(void)
300 return locomo_driver_register(&keyboard_driver);
303 static void __exit locomokbd_exit(void)
305 locomo_driver_unregister(&keyboard_driver);
308 module_init(locomokbd_init);
309 module_exit(locomokbd_exit);