blob: 83906f80ba21c31aa0cc5eb443926b972cdefd37 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
40MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
41MODULE_DESCRIPTION("LoCoMo keyboard driver");
42MODULE_LICENSE("GPL");
43
Dmitry Torokhovd083e902005-05-29 02:28:42 -050044#define LOCOMOKBD_NUMKEYS 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define KEY_ACTIVITY KEY_F16
47#define KEY_CONTACT KEY_F18
48#define KEY_CENTER KEY_F15
49
50static 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 */
Dmitry Torokhovd083e902005-05-29 02:28:42 -050063 KEY_M, KEY_SPACE, KEY_V, KEY_APOSTROPHE, KEY_SLASH, 0, 0, 0 /* 120 - 128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
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
76struct locomokbd {
77 unsigned char keycode[LOCOMOKBD_NUMKEYS];
Dmitry Torokhov438c9da2005-11-02 22:49:53 -050078 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 char phys[32];
80
81 struct locomo_dev *ldev;
82 unsigned long base;
83 spinlock_t lock;
Dmitry Torokhovd083e902005-05-29 02:28:42 -050084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct timer_list timer;
86};
87
88/* helper functions for reading the keyboard matrix */
89static inline void locomokbd_charge_all(unsigned long membase)
90{
91 locomo_writel(0x00FF, membase + LOCOMO_KSC);
92}
93
94static inline void locomokbd_activate_all(unsigned long membase)
95{
96 unsigned long r;
Dmitry Torokhovd083e902005-05-29 02:28:42 -050097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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
104static 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
114static 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 */
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500129static void locomokbd_scankeyboard(struct locomokbd *locomokbd, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
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
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500138 input_regs(locomokbd->input, regs);
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 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);
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 rowd = ~locomo_readl(membase + LOCOMO_KIB);
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500149 for (row = 0; row < KB_ROWS; row++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 scancode = SCANCODE(col, row);
151 if (rowd & KB_ROWMASK(row)) {
152 num_pressed += 1;
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500153 input_report_key(locomokbd->input, locomokbd->keycode[scancode], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else {
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500155 input_report_key(locomokbd->input, locomokbd->keycode[scancode], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158 locomokbd_reset_col(membase, col);
159 }
160 locomokbd_activate_all(membase);
161
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500162 input_sync(locomokbd->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
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
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * LoCoMo keyboard interrupt handler.
173 */
174static 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 */
188static void locomokbd_timer_callback(unsigned long data)
189{
190 struct locomokbd *locomokbd = (struct locomokbd *) data;
191 locomokbd_scankeyboard(locomokbd, NULL);
192}
193
194static int locomokbd_probe(struct locomo_dev *dev)
195{
196 struct locomokbd *locomokbd;
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500197 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int i, ret;
199
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500200 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* try and claim memory region */
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500208 if (!request_mem_region((unsigned long) dev->mapbase,
209 dev->length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 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
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500227 locomokbd->input = input_dev;
228 strcpy(locomokbd->phys, "locomokbd/input0");
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500229
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500230 input_dev->name = "LoCoMo keyboard";
231 input_dev->phys = locomokbd->phys;
Pavel Machekd7a767d2005-11-02 22:52:33 -0500232 input_dev->id.bustype = BUS_HOST;
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500233 input_dev->id.vendor = 0x0001;
234 input_dev->id.product = 0x0001;
235 input_dev->id.version = 0x0100;
236 input_dev->private = locomokbd;
237
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 memcpy(locomokbd->keycode, locomokbd_keycode, sizeof(locomokbd->keycode));
244 for (i = 0; i < LOCOMOKBD_NUMKEYS; i++)
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500245 set_bit(locomokbd->keycode[i], input_dev->keybit);
246 clear_bit(0, input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
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
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500255 input_register_device(locomokbd->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return 0;
258
259out:
260 release_mem_region((unsigned long) dev->mapbase, dev->length);
261 locomo_set_drvdata(dev, NULL);
262free:
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500263 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 kfree(locomokbd);
265
266 return ret;
267}
268
269static int locomokbd_remove(struct locomo_dev *dev)
270{
271 struct locomokbd *locomokbd = locomo_get_drvdata(dev);
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 free_irq(dev->irq[0], locomokbd);
274
275 del_timer_sync(&locomokbd->timer);
Dmitry Torokhovd083e902005-05-29 02:28:42 -0500276
Dmitry Torokhov438c9da2005-11-02 22:49:53 -0500277 input_unregister_device(locomokbd->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 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
287static 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
296static int __init locomokbd_init(void)
297{
298 return locomo_driver_register(&keyboard_driver);
299}
300
301static void __exit locomokbd_exit(void)
302{
303 locomo_driver_unregister(&keyboard_driver);
304}
305
306module_init(locomokbd_init);
307module_exit(locomokbd_exit);