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