blob: d3e9dd6a13cdc8236010765c56e12b648c16fe31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
41MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
42MODULE_DESCRIPTION("LoCoMo keyboard driver");
43MODULE_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
51static 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
77struct 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 */
90static inline void locomokbd_charge_all(unsigned long membase)
91{
92 locomo_writel(0x00FF, membase + LOCOMO_KSC);
93}
94
95static 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
105static 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
115static 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 */
130static 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 if (regs)
140 input_regs(&locomokbd->input, regs);
141
142 locomokbd_charge_all(membase);
143
144 num_pressed = 0;
145 for (col = 0; col < KB_COLS; col++) {
146
147 locomokbd_activate_col(membase, col);
148 udelay(KB_DELAY);
149
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)) {
154 num_pressed += 1;
155 input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 1);
156 } else {
157 input_report_key(&locomokbd->input, locomokbd->keycode[scancode], 0);
158 }
159 }
160 locomokbd_reset_col(membase, col);
161 }
162 locomokbd_activate_all(membase);
163
164 input_sync(&locomokbd->input);
165
166 /* if any keys are pressed, enable the timer */
167 if (num_pressed)
168 mod_timer(&locomokbd->timer, jiffies + SCAN_INTERVAL);
169
170 spin_unlock_irqrestore(&locomokbd->lock, flags);
171}
172
173/*
174 * LoCoMo keyboard interrupt handler.
175 */
176static irqreturn_t locomokbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
177{
178 struct locomokbd *locomokbd = dev_id;
179 /** wait chattering delay **/
180 udelay(100);
181
182 locomokbd_scankeyboard(locomokbd, regs);
183
184 return IRQ_HANDLED;
185}
186
187/*
188 * LoCoMo timer checking for released keys
189 */
190static void locomokbd_timer_callback(unsigned long data)
191{
192 struct locomokbd *locomokbd = (struct locomokbd *) data;
193 locomokbd_scankeyboard(locomokbd, NULL);
194}
195
196static int locomokbd_probe(struct locomo_dev *dev)
197{
198 struct locomokbd *locomokbd;
199 int i, ret;
200
201 locomokbd = kmalloc(sizeof(struct locomokbd), GFP_KERNEL);
202 if (!locomokbd)
203 return -ENOMEM;
204
205 memset(locomokbd, 0, sizeof(struct locomokbd));
206
207 /* try and claim memory region */
208 if (!request_mem_region((unsigned long) dev->mapbase,
209 dev->length,
210 LOCOMO_DRIVER_NAME(dev))) {
211 ret = -EBUSY;
212 printk(KERN_ERR "locomokbd: Can't acquire access to io memory for keyboard\n");
213 goto free;
214 }
215
216 locomokbd->ldev = dev;
217 locomo_set_drvdata(dev, locomokbd);
218
219 locomokbd->base = (unsigned long) dev->mapbase;
220
221 spin_lock_init(&locomokbd->lock);
222
223 init_timer(&locomokbd->timer);
224 locomokbd->timer.function = locomokbd_timer_callback;
225 locomokbd->timer.data = (unsigned long) locomokbd;
226
227 locomokbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
228
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;
234
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);
239
240 strcpy(locomokbd->phys, "locomokbd/input0");
241
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;
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 printk(KERN_INFO "input: LoCoMo keyboard on locomokbd\n");
259
260 return 0;
261
262out:
263 release_mem_region((unsigned long) dev->mapbase, dev->length);
264 locomo_set_drvdata(dev, NULL);
265free:
266 kfree(locomokbd);
267
268 return ret;
269}
270
271static int locomokbd_remove(struct locomo_dev *dev)
272{
273 struct locomokbd *locomokbd = locomo_get_drvdata(dev);
274
275 free_irq(dev->irq[0], locomokbd);
276
277 del_timer_sync(&locomokbd->timer);
278
279 input_unregister_device(&locomokbd->input);
280 locomo_set_drvdata(dev, NULL);
281
282 release_mem_region((unsigned long) dev->mapbase, dev->length);
283
284 kfree(locomokbd);
285
286 return 0;
287}
288
289static struct locomo_driver keyboard_driver = {
290 .drv = {
291 .name = "locomokbd"
292 },
293 .devid = LOCOMO_DEVID_KEYBOARD,
294 .probe = locomokbd_probe,
295 .remove = locomokbd_remove,
296};
297
298static int __init locomokbd_init(void)
299{
300 return locomo_driver_register(&keyboard_driver);
301}
302
303static void __exit locomokbd_exit(void)
304{
305 locomo_driver_unregister(&keyboard_driver);
306}
307
308module_init(locomokbd_init);
309module_exit(locomokbd_exit);