]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/input/keyboard/corgikbd.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / drivers / input / keyboard / corgikbd.c
1 /*
2 * Keyboard driver for Sharp Corgi models (SL-C7xx)
3 *
4 * Copyright (c) 2004-2005 Richard Purdie
5 *
6 * Based on xtkbd.c/locomkbd.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <asm/irq.h>
22
23 #include <asm/arch/corgi.h>
24 #include <asm/arch/hardware.h>
25 #include <asm/arch/pxa-regs.h>
26 #include <asm/hardware/scoop.h>
27
28 #define KB_ROWS 8
29 #define KB_COLS 12
30 #define KB_ROWMASK(r) (1 << (r))
31 #define SCANCODE(r,c) ( ((r)<<4) + (c) + 1 )
32 /* zero code, 124 scancodes + 3 hinge combinations */
33 #define NR_SCANCODES ( SCANCODE(KB_ROWS-1,KB_COLS-1) +1 +1 +3 )
34 #define SCAN_INTERVAL (HZ/10)
35 #define CORGIKBD_PRESSED 1
36
37 #define HINGE_SCAN_INTERVAL (HZ/4)
38
39 #define CORGI_KEY_CALENDER KEY_F1
40 #define CORGI_KEY_ADDRESS KEY_F2
41 #define CORGI_KEY_FN KEY_F3
42 #define CORGI_KEY_OFF KEY_SUSPEND
43 #define CORGI_KEY_EXOK KEY_F5
44 #define CORGI_KEY_EXCANCEL KEY_F6
45 #define CORGI_KEY_EXJOGDOWN KEY_F7
46 #define CORGI_KEY_EXJOGUP KEY_F8
47 #define CORGI_KEY_JAP1 KEY_LEFTCTRL
48 #define CORGI_KEY_JAP2 KEY_LEFTALT
49 #define CORGI_KEY_OK KEY_F11
50 #define CORGI_KEY_MENU KEY_F12
51 #define CORGI_HINGE_0 KEY_KP0
52 #define CORGI_HINGE_1 KEY_KP1
53 #define CORGI_HINGE_2 KEY_KP2
54
55 static unsigned char corgikbd_keycode[NR_SCANCODES] = {
56 0, /* 0 */
57 0, KEY_1, KEY_3, KEY_5, KEY_6, KEY_7, KEY_9, KEY_0, KEY_BACKSPACE, 0, 0, 0, 0, 0, 0, 0, /* 1-16 */
58 0, KEY_2, KEY_4, KEY_R, KEY_Y, KEY_8, KEY_I, KEY_O, KEY_P, 0, 0, 0, 0, 0, 0, 0, /* 17-32 */
59 KEY_TAB, KEY_Q, KEY_E, KEY_T, KEY_G, KEY_U, KEY_J, KEY_K, 0, 0, 0, 0, 0, 0, 0, 0, /* 33-48 */
60 CORGI_KEY_CALENDER, KEY_W, KEY_S, KEY_F, KEY_V, KEY_H, KEY_M, KEY_L, 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0, /* 49-64 */
61 CORGI_KEY_ADDRESS, KEY_A, KEY_D, KEY_C, KEY_B, KEY_N, KEY_DOT, 0, KEY_ENTER, 0, KEY_LEFTSHIFT, 0, 0, 0, 0, 0, /* 65-80 */
62 KEY_MAIL, KEY_Z, KEY_X, KEY_MINUS, KEY_SPACE, KEY_COMMA, 0, KEY_UP, 0, 0, 0, CORGI_KEY_FN, 0, 0, 0, 0, /* 81-96 */
63 KEY_SYSRQ, CORGI_KEY_JAP1, CORGI_KEY_JAP2, KEY_CANCEL, CORGI_KEY_OK, CORGI_KEY_MENU, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0, 0, 0, 0, 0, /* 97-112 */
64 CORGI_KEY_OFF, CORGI_KEY_EXOK, CORGI_KEY_EXCANCEL, CORGI_KEY_EXJOGDOWN, CORGI_KEY_EXJOGUP, 0, 0, 0, 0, 0, 0, 0, /* 113-124 */
65 CORGI_HINGE_0, CORGI_HINGE_1, CORGI_HINGE_2 /* 125-127 */
66 };
67
68
69 struct corgikbd {
70 unsigned char keycode[ARRAY_SIZE(corgikbd_keycode)];
71 struct input_dev input;
72 char phys[32];
73
74 unsigned char state[ARRAY_SIZE(corgikbd_keycode)];
75 spinlock_t lock;
76
77 struct timer_list timer;
78 struct timer_list htimer;
79 };
80
81 static void handle_scancode(unsigned int pressed,unsigned int scancode, struct corgikbd *corgikbd_data)
82 {
83 if (pressed && !(corgikbd_data->state[scancode] & CORGIKBD_PRESSED)) {
84 corgikbd_data->state[scancode] |= CORGIKBD_PRESSED;
85 input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 1);
86 if (corgikbd_data->keycode[scancode] == CORGI_KEY_OFF)
87 input_event(&corgikbd_data->input, EV_PWR, CORGI_KEY_OFF, 1);
88 } else if (!pressed && corgikbd_data->state[scancode] & CORGIKBD_PRESSED) {
89 corgikbd_data->state[scancode] &= ~CORGIKBD_PRESSED;
90 input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 0);
91 }
92 }
93
94 #define KB_DISCHARGE_DELAY 10
95 #define KB_ACTIVATE_DELAY 10
96
97 /* Helper functions for reading the keyboard matrix
98 * Note: We should really be using pxa_gpio_mode to alter GPDR but it
99 * requires a function call per GPIO bit which is excessive
100 * when we need to access 12 bits at once multiple times.
101 * These functions must be called within local_irq_save()/local_irq_restore()
102 * or similar.
103 */
104 static inline void corgikbd_discharge_all(void)
105 {
106 // STROBE All HiZ
107 GPCR2 = CORGI_GPIO_ALL_STROBE_BIT;
108 GPDR2 &= ~CORGI_GPIO_ALL_STROBE_BIT;
109 }
110
111 static inline void corgikbd_activate_all(void)
112 {
113 // STROBE ALL -> High
114 GPSR2 = CORGI_GPIO_ALL_STROBE_BIT;
115 GPDR2 |= CORGI_GPIO_ALL_STROBE_BIT;
116
117 udelay(KB_DISCHARGE_DELAY);
118
119 // Clear any interrupts we may have triggered when altering the GPIO lines
120 GEDR1 = CORGI_GPIO_HIGH_SENSE_BIT;
121 GEDR2 = CORGI_GPIO_LOW_SENSE_BIT;
122 }
123
124 static inline void corgikbd_activate_col(int col)
125 {
126 // STROBE col -> High, not col -> HiZ
127 GPSR2 = CORGI_GPIO_STROBE_BIT(col);
128 GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
129 }
130
131 static inline void corgikbd_reset_col(int col)
132 {
133 // STROBE col -> Low
134 GPCR2 = CORGI_GPIO_STROBE_BIT(col);
135 // STROBE col -> out, not col -> HiZ
136 GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
137 }
138
139 #define GET_ROWS_STATUS(c) (((GPLR1 & CORGI_GPIO_HIGH_SENSE_BIT) >> CORGI_GPIO_HIGH_SENSE_RSHIFT) | ((GPLR2 & CORGI_GPIO_LOW_SENSE_BIT) << CORGI_GPIO_LOW_SENSE_LSHIFT))
140
141 /*
142 * The corgi keyboard only generates interrupts when a key is pressed.
143 * When a key is pressed, we enable a timer which then scans the
144 * keyboard to detect when the key is released.
145 */
146
147 /* Scan the hardware keyboard and push any changes up through the input layer */
148 static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data, struct pt_regs *regs)
149 {
150 unsigned int row, col, rowd, scancode;
151 unsigned long flags;
152 unsigned int num_pressed;
153
154 spin_lock_irqsave(&corgikbd_data->lock, flags);
155
156 if (regs)
157 input_regs(&corgikbd_data->input, regs);
158
159 num_pressed = 0;
160 for (col = 0; col < KB_COLS; col++) {
161 /*
162 * Discharge the output driver capacitatance
163 * in the keyboard matrix. (Yes it is significant..)
164 */
165
166 corgikbd_discharge_all();
167 udelay(KB_DISCHARGE_DELAY);
168
169 corgikbd_activate_col(col);
170 udelay(KB_ACTIVATE_DELAY);
171
172 rowd = GET_ROWS_STATUS(col);
173 for (row = 0; row < KB_ROWS; row++) {
174 scancode = SCANCODE(row, col);
175 handle_scancode((rowd & KB_ROWMASK(row)), scancode, corgikbd_data);
176 if (rowd & KB_ROWMASK(row))
177 num_pressed++;
178 }
179 corgikbd_reset_col(col);
180 }
181
182 corgikbd_activate_all();
183
184 input_sync(&corgikbd_data->input);
185
186 /* if any keys are pressed, enable the timer */
187 if (num_pressed)
188 mod_timer(&corgikbd_data->timer, jiffies + SCAN_INTERVAL);
189
190 spin_unlock_irqrestore(&corgikbd_data->lock, flags);
191 }
192
193 /*
194 * corgi keyboard interrupt handler.
195 */
196 static irqreturn_t corgikbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
197 {
198 struct corgikbd *corgikbd_data = dev_id;
199
200 if (!timer_pending(&corgikbd_data->timer)) {
201 /** wait chattering delay **/
202 udelay(20);
203 corgikbd_scankeyboard(corgikbd_data, regs);
204 }
205
206 return IRQ_HANDLED;
207 }
208
209 /*
210 * corgi timer checking for released keys
211 */
212 static void corgikbd_timer_callback(unsigned long data)
213 {
214 struct corgikbd *corgikbd_data = (struct corgikbd *) data;
215 corgikbd_scankeyboard(corgikbd_data, NULL);
216 }
217
218 /*
219 * The hinge switches generate no interrupt so they need to be
220 * monitored by a timer.
221 *
222 * When we detect changes, we debounce it and then pass the three
223 * positions the system can take as keypresses to the input system.
224 */
225
226 #define HINGE_STABLE_COUNT 2
227 static int sharpsl_hinge_state;
228 static int hinge_count;
229
230 static void corgikbd_hinge_timer(unsigned long data)
231 {
232 struct corgikbd *corgikbd_data = (struct corgikbd *) data;
233 unsigned long gprr;
234 unsigned long flags;
235
236 gprr = read_scoop_reg(SCOOP_GPRR) & (CORGI_SCP_SWA | CORGI_SCP_SWB);
237 if (gprr != sharpsl_hinge_state) {
238 hinge_count = 0;
239 sharpsl_hinge_state = gprr;
240 } else if (hinge_count < HINGE_STABLE_COUNT) {
241 hinge_count++;
242 if (hinge_count >= HINGE_STABLE_COUNT) {
243 spin_lock_irqsave(&corgikbd_data->lock, flags);
244
245 handle_scancode((sharpsl_hinge_state == 0x00), 125, corgikbd_data); /* Keyboard with Landscape Screen */
246 handle_scancode((sharpsl_hinge_state == 0x08), 126, corgikbd_data); /* No Keyboard with Portrait Screen */
247 handle_scancode((sharpsl_hinge_state == 0x0c), 127, corgikbd_data); /* Keyboard and Screen Closed */
248 input_sync(&corgikbd_data->input);
249
250 spin_unlock_irqrestore(&corgikbd_data->lock, flags);
251 }
252 }
253 mod_timer(&corgikbd_data->htimer, jiffies + HINGE_SCAN_INTERVAL);
254 }
255
256 static int __init corgikbd_probe(struct device *dev)
257 {
258 int i;
259 struct corgikbd *corgikbd;
260
261 corgikbd = kcalloc(1, sizeof(struct corgikbd), GFP_KERNEL);
262 if (!corgikbd)
263 return -ENOMEM;
264
265 dev_set_drvdata(dev,corgikbd);
266 strcpy(corgikbd->phys, "corgikbd/input0");
267
268 spin_lock_init(corgikbd->lock);
269
270 /* Init Keyboard rescan timer */
271 init_timer(&corgikbd->timer);
272 corgikbd->timer.function = corgikbd_timer_callback;
273 corgikbd->timer.data = (unsigned long) corgikbd;
274
275 /* Init Hinge Timer */
276 init_timer(&corgikbd->htimer);
277 corgikbd->htimer.function = corgikbd_hinge_timer;
278 corgikbd->htimer.data = (unsigned long) corgikbd;
279
280 init_input_dev(&corgikbd->input);
281 corgikbd->input.private = corgikbd;
282 corgikbd->input.name = "Corgi Keyboard";
283 corgikbd->input.dev = dev;
284 corgikbd->input.phys = corgikbd->phys;
285 corgikbd->input.id.bustype = BUS_HOST;
286 corgikbd->input.id.vendor = 0x0001;
287 corgikbd->input.id.product = 0x0001;
288 corgikbd->input.id.version = 0x0100;
289 corgikbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_PWR);
290 corgikbd->input.keycode = corgikbd->keycode;
291 corgikbd->input.keycodesize = sizeof(unsigned char);
292 corgikbd->input.keycodemax = ARRAY_SIZE(corgikbd_keycode);
293
294 memcpy(corgikbd->keycode, corgikbd_keycode, sizeof(corgikbd->keycode));
295 for (i = 0; i < ARRAY_SIZE(corgikbd_keycode); i++)
296 set_bit(corgikbd->keycode[i], corgikbd->input.keybit);
297 clear_bit(0, corgikbd->input.keybit);
298
299 input_register_device(&corgikbd->input);
300 mod_timer(&corgikbd->htimer, jiffies + HINGE_SCAN_INTERVAL);
301
302 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
303 for (i = 0; i < CORGI_KEY_SENSE_NUM; i++) {
304 pxa_gpio_mode(CORGI_GPIO_KEY_SENSE(i) | GPIO_IN);
305 if (request_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd_interrupt,
306 SA_INTERRUPT, "corgikbd", corgikbd))
307 printk(KERN_WARNING "corgikbd: Can't get IRQ: %d!\n", i);
308 else
309 set_irq_type(CORGI_IRQ_GPIO_KEY_SENSE(i),IRQT_RISING);
310 }
311
312 /* Set Strobe lines as outputs - set high */
313 for (i = 0; i < CORGI_KEY_STROBE_NUM; i++)
314 pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i) | GPIO_OUT | GPIO_DFLT_HIGH);
315
316 printk(KERN_INFO "input: Corgi Keyboard Registered\n");
317
318 return 0;
319 }
320
321 static int corgikbd_remove(struct device *dev)
322 {
323 int i;
324 struct corgikbd *corgikbd = dev_get_drvdata(dev);
325
326 for (i = 0; i < CORGI_KEY_SENSE_NUM; i++)
327 free_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd);
328
329 del_timer_sync(&corgikbd->htimer);
330 del_timer_sync(&corgikbd->timer);
331
332 input_unregister_device(&corgikbd->input);
333
334 kfree(corgikbd);
335
336 return 0;
337 }
338
339 static struct device_driver corgikbd_driver = {
340 .name = "corgi-keyboard",
341 .bus = &platform_bus_type,
342 .probe = corgikbd_probe,
343 .remove = corgikbd_remove,
344 };
345
346 static int __devinit corgikbd_init(void)
347 {
348 return driver_register(&corgikbd_driver);
349 }
350
351 static void __exit corgikbd_exit(void)
352 {
353 driver_unregister(&corgikbd_driver);
354 }
355
356 module_init(corgikbd_init);
357 module_exit(corgikbd_exit);
358
359 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
360 MODULE_DESCRIPTION("Corgi Keyboard Driver");
361 MODULE_LICENSE("GPLv2");