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
);
139 input_regs(locomokbd
->input
, regs
);
141 locomokbd_charge_all(membase
);
144 for (col
= 0; col
< KB_COLS
; col
++) {
146 locomokbd_activate_col(membase
, col
);
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
)) {
154 input_report_key(locomokbd
->input
, locomokbd
->keycode
[scancode
], 1);
156 input_report_key(locomokbd
->input
, locomokbd
->keycode
[scancode
], 0);
159 locomokbd_reset_col(membase
, col
);
161 locomokbd_activate_all(membase
);
163 input_sync(locomokbd
->input
);
165 /* if any keys are pressed, enable the timer */
167 mod_timer(&locomokbd
->timer
, jiffies
+ SCAN_INTERVAL
);
169 spin_unlock_irqrestore(&locomokbd
->lock
, flags
);
173 * LoCoMo keyboard interrupt handler.
175 static irqreturn_t
locomokbd_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
177 struct locomokbd
*locomokbd
= dev_id
;
178 /** wait chattering delay **/
181 locomokbd_scankeyboard(locomokbd
, regs
);
187 * LoCoMo timer checking for released keys
189 static void locomokbd_timer_callback(unsigned long data
)
191 struct locomokbd
*locomokbd
= (struct locomokbd
*) data
;
192 locomokbd_scankeyboard(locomokbd
, NULL
);
195 static int locomokbd_probe(struct locomo_dev
*dev
)
197 struct locomokbd
*locomokbd
;
198 struct input_dev
*input_dev
;
201 locomokbd
= kzalloc(sizeof(struct locomokbd
), GFP_KERNEL
);
202 input_dev
= input_allocate_device();
203 if (!locomokbd
|| !input_dev
) {
208 /* try and claim memory region */
209 if (!request_mem_region((unsigned long) dev
->mapbase
,
211 LOCOMO_DRIVER_NAME(dev
))) {
213 printk(KERN_ERR
"locomokbd: Can't acquire access to io memory for keyboard\n");
217 locomokbd
->ldev
= dev
;
218 locomo_set_drvdata(dev
, locomokbd
);
220 locomokbd
->base
= (unsigned long) dev
->mapbase
;
222 spin_lock_init(&locomokbd
->lock
);
224 init_timer(&locomokbd
->timer
);
225 locomokbd
->timer
.function
= locomokbd_timer_callback
;
226 locomokbd
->timer
.data
= (unsigned long) locomokbd
;
228 locomokbd
->input
= input_dev
;
229 strcpy(locomokbd
->phys
, "locomokbd/input0");
231 input_dev
->name
= "LoCoMo keyboard";
232 input_dev
->phys
= locomokbd
->phys
;
233 input_dev
->id
.bustype
= BUS_XTKBD
;
234 input_dev
->id
.vendor
= 0x0001;
235 input_dev
->id
.product
= 0x0001;
236 input_dev
->id
.version
= 0x0100;
237 input_dev
->private = locomokbd
;
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
);
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
);
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
);
261 release_mem_region((unsigned long) dev
->mapbase
, dev
->length
);
262 locomo_set_drvdata(dev
, NULL
);
264 input_free_device(input_dev
);
270 static int locomokbd_remove(struct locomo_dev
*dev
)
272 struct locomokbd
*locomokbd
= locomo_get_drvdata(dev
);
274 free_irq(dev
->irq
[0], locomokbd
);
276 del_timer_sync(&locomokbd
->timer
);
278 input_unregister_device(locomokbd
->input
);
279 locomo_set_drvdata(dev
, NULL
);
281 release_mem_region((unsigned long) dev
->mapbase
, dev
->length
);
288 static struct locomo_driver keyboard_driver
= {
292 .devid
= LOCOMO_DEVID_KEYBOARD
,
293 .probe
= locomokbd_probe
,
294 .remove
= locomokbd_remove
,
297 static int __init
locomokbd_init(void)
299 return locomo_driver_register(&keyboard_driver
);
302 static void __exit
locomokbd_exit(void)
304 locomo_driver_unregister(&keyboard_driver
);
307 module_init(locomokbd_init
);
308 module_exit(locomokbd_exit
);