OMAP3: PM: Prevented DVFS state switches when enabling off-mode
[linux-ginger.git] / drivers / input / keyboard / tosakbd.c
blob42cb3faf7336256ad8de736d7afcb2c4dc1e7756
1 /*
2 * Keyboard driver for Sharp Tosa models (SL-6000x)
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2007 Dmitry Baryshkov
7 * Based on xtkbd.c/locomkbd.c/corgikbd.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/input.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
22 #include <mach/gpio.h>
23 #include <mach/tosa.h>
25 #define KB_ROWMASK(r) (1 << (r))
26 #define SCANCODE(r, c) (((r)<<4) + (c) + 1)
27 #define NR_SCANCODES SCANCODE(TOSA_KEY_SENSE_NUM - 1, TOSA_KEY_STROBE_NUM - 1) + 1
29 #define SCAN_INTERVAL (HZ/10)
31 #define KB_DISCHARGE_DELAY 10
32 #define KB_ACTIVATE_DELAY 10
34 static unsigned short tosakbd_keycode[NR_SCANCODES] = {
36 0, KEY_W, 0, 0, 0, KEY_K, KEY_BACKSPACE, KEY_P,
37 0, 0, 0, 0, 0, 0, 0, 0,
38 KEY_Q, KEY_E, KEY_T, KEY_Y, 0, KEY_O, KEY_I, KEY_COMMA,
39 0, 0, 0, 0, 0, 0, 0, 0,
40 KEY_A, KEY_D, KEY_G, KEY_U, 0, KEY_L, KEY_ENTER, KEY_DOT,
41 0, 0, 0, 0, 0, 0, 0, 0,
42 KEY_Z, KEY_C, KEY_V, KEY_J, TOSA_KEY_ADDRESSBOOK, TOSA_KEY_CANCEL, TOSA_KEY_CENTER, TOSA_KEY_OK,
43 KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 0, 0,
44 KEY_S, KEY_R, KEY_B, KEY_N, TOSA_KEY_CALENDAR, TOSA_KEY_HOMEPAGE, KEY_LEFTCTRL, TOSA_KEY_LIGHT,
45 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0,
46 KEY_TAB, KEY_SLASH, KEY_H, KEY_M, TOSA_KEY_MENU, 0, KEY_UP, 0,
47 0, 0, TOSA_KEY_FN, 0, 0, 0, 0, 0,
48 KEY_X, KEY_F, KEY_SPACE, KEY_APOSTROPHE, TOSA_KEY_MAIL, KEY_LEFT, KEY_DOWN, KEY_RIGHT,
49 0, 0, 0,
52 struct tosakbd {
53 unsigned short keycode[ARRAY_SIZE(tosakbd_keycode)];
54 struct input_dev *input;
55 bool suspended;
56 spinlock_t lock; /* protect kbd scanning */
57 struct timer_list timer;
61 /* Helper functions for reading the keyboard matrix
62 * Note: We should really be using the generic gpio functions to alter
63 * GPDR but it requires a function call per GPIO bit which is
64 * excessive when we need to access 12 bits at once, multiple times.
65 * These functions must be called within local_irq_save()/local_irq_restore()
66 * or similar.
68 #define GET_ROWS_STATUS(c) ((GPLR2 & TOSA_GPIO_ALL_SENSE_BIT) >> TOSA_GPIO_ALL_SENSE_RSHIFT)
70 static inline void tosakbd_discharge_all(void)
72 /* STROBE All HiZ */
73 GPCR1 = TOSA_GPIO_HIGH_STROBE_BIT;
74 GPDR1 &= ~TOSA_GPIO_HIGH_STROBE_BIT;
75 GPCR2 = TOSA_GPIO_LOW_STROBE_BIT;
76 GPDR2 &= ~TOSA_GPIO_LOW_STROBE_BIT;
79 static inline void tosakbd_activate_all(void)
81 /* STROBE ALL -> High */
82 GPSR1 = TOSA_GPIO_HIGH_STROBE_BIT;
83 GPDR1 |= TOSA_GPIO_HIGH_STROBE_BIT;
84 GPSR2 = TOSA_GPIO_LOW_STROBE_BIT;
85 GPDR2 |= TOSA_GPIO_LOW_STROBE_BIT;
87 udelay(KB_DISCHARGE_DELAY);
89 /* STATE CLEAR */
90 GEDR2 |= TOSA_GPIO_ALL_SENSE_BIT;
93 static inline void tosakbd_activate_col(int col)
95 if (col <= 5) {
96 /* STROBE col -> High, not col -> HiZ */
97 GPSR1 = TOSA_GPIO_STROBE_BIT(col);
98 GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
99 } else {
100 /* STROBE col -> High, not col -> HiZ */
101 GPSR2 = TOSA_GPIO_STROBE_BIT(col);
102 GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
106 static inline void tosakbd_reset_col(int col)
108 if (col <= 5) {
109 /* STROBE col -> Low */
110 GPCR1 = TOSA_GPIO_STROBE_BIT(col);
111 /* STROBE col -> out, not col -> HiZ */
112 GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
113 } else {
114 /* STROBE col -> Low */
115 GPCR2 = TOSA_GPIO_STROBE_BIT(col);
116 /* STROBE col -> out, not col -> HiZ */
117 GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
121 * The tosa keyboard only generates interrupts when a key is pressed.
122 * So when a key is pressed, we enable a timer. This timer scans the
123 * keyboard, and this is how we detect when the key is released.
126 /* Scan the hardware keyboard and push any changes up through the input layer */
127 static void tosakbd_scankeyboard(struct platform_device *dev)
129 struct tosakbd *tosakbd = platform_get_drvdata(dev);
130 unsigned int row, col, rowd;
131 unsigned long flags;
132 unsigned int num_pressed = 0;
134 spin_lock_irqsave(&tosakbd->lock, flags);
136 if (tosakbd->suspended)
137 goto out;
139 for (col = 0; col < TOSA_KEY_STROBE_NUM; col++) {
141 * Discharge the output driver capacitatance
142 * in the keyboard matrix. (Yes it is significant..)
144 tosakbd_discharge_all();
145 udelay(KB_DISCHARGE_DELAY);
147 tosakbd_activate_col(col);
148 udelay(KB_ACTIVATE_DELAY);
150 rowd = GET_ROWS_STATUS(col);
152 for (row = 0; row < TOSA_KEY_SENSE_NUM; row++) {
153 unsigned int scancode, pressed;
154 scancode = SCANCODE(row, col);
155 pressed = rowd & KB_ROWMASK(row);
157 if (pressed && !tosakbd->keycode[scancode])
158 dev_warn(&dev->dev,
159 "unhandled scancode: 0x%02x\n",
160 scancode);
162 input_report_key(tosakbd->input,
163 tosakbd->keycode[scancode],
164 pressed);
165 if (pressed)
166 num_pressed++;
169 tosakbd_reset_col(col);
172 tosakbd_activate_all();
174 input_sync(tosakbd->input);
176 /* if any keys are pressed, enable the timer */
177 if (num_pressed)
178 mod_timer(&tosakbd->timer, jiffies + SCAN_INTERVAL);
180 out:
181 spin_unlock_irqrestore(&tosakbd->lock, flags);
185 * tosa keyboard interrupt handler.
187 static irqreturn_t tosakbd_interrupt(int irq, void *__dev)
189 struct platform_device *dev = __dev;
190 struct tosakbd *tosakbd = platform_get_drvdata(dev);
192 if (!timer_pending(&tosakbd->timer)) {
193 /** wait chattering delay **/
194 udelay(20);
195 tosakbd_scankeyboard(dev);
198 return IRQ_HANDLED;
202 * tosa timer checking for released keys
204 static void tosakbd_timer_callback(unsigned long __dev)
206 struct platform_device *dev = (struct platform_device *)__dev;
208 tosakbd_scankeyboard(dev);
211 #ifdef CONFIG_PM
212 static int tosakbd_suspend(struct platform_device *dev, pm_message_t state)
214 struct tosakbd *tosakbd = platform_get_drvdata(dev);
215 unsigned long flags;
217 spin_lock_irqsave(&tosakbd->lock, flags);
218 tosakbd->suspended = true;
219 spin_unlock_irqrestore(&tosakbd->lock, flags);
221 del_timer_sync(&tosakbd->timer);
223 return 0;
226 static int tosakbd_resume(struct platform_device *dev)
228 struct tosakbd *tosakbd = platform_get_drvdata(dev);
230 tosakbd->suspended = false;
231 tosakbd_scankeyboard(dev);
233 return 0;
235 #else
236 #define tosakbd_suspend NULL
237 #define tosakbd_resume NULL
238 #endif
240 static int __devinit tosakbd_probe(struct platform_device *pdev) {
242 int i;
243 struct tosakbd *tosakbd;
244 struct input_dev *input_dev;
245 int error;
247 tosakbd = kzalloc(sizeof(struct tosakbd), GFP_KERNEL);
248 if (!tosakbd)
249 return -ENOMEM;
251 input_dev = input_allocate_device();
252 if (!input_dev) {
253 kfree(tosakbd);
254 return -ENOMEM;
257 platform_set_drvdata(pdev, tosakbd);
259 spin_lock_init(&tosakbd->lock);
261 /* Init Keyboard rescan timer */
262 init_timer(&tosakbd->timer);
263 tosakbd->timer.function = tosakbd_timer_callback;
264 tosakbd->timer.data = (unsigned long) pdev;
266 tosakbd->input = input_dev;
268 input_set_drvdata(input_dev, tosakbd);
269 input_dev->name = "Tosa Keyboard";
270 input_dev->phys = "tosakbd/input0";
271 input_dev->dev.parent = &pdev->dev;
273 input_dev->id.bustype = BUS_HOST;
274 input_dev->id.vendor = 0x0001;
275 input_dev->id.product = 0x0001;
276 input_dev->id.version = 0x0100;
278 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
279 input_dev->keycode = tosakbd->keycode;
280 input_dev->keycodesize = sizeof(tosakbd->keycode[0]);
281 input_dev->keycodemax = ARRAY_SIZE(tosakbd_keycode);
283 memcpy(tosakbd->keycode, tosakbd_keycode, sizeof(tosakbd_keycode));
285 for (i = 0; i < ARRAY_SIZE(tosakbd_keycode); i++)
286 __set_bit(tosakbd->keycode[i], input_dev->keybit);
287 __clear_bit(KEY_RESERVED, input_dev->keybit);
289 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
290 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
291 int gpio = TOSA_GPIO_KEY_SENSE(i);
292 int irq;
293 error = gpio_request(gpio, "tosakbd");
294 if (error < 0) {
295 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
296 " error %d\n", gpio, error);
297 goto fail;
300 error = gpio_direction_input(TOSA_GPIO_KEY_SENSE(i));
301 if (error < 0) {
302 printk(KERN_ERR "tosakbd: failed to configure input"
303 " direction for GPIO %d, error %d\n",
304 gpio, error);
305 gpio_free(gpio);
306 goto fail;
309 irq = gpio_to_irq(gpio);
310 if (irq < 0) {
311 error = irq;
312 printk(KERN_ERR "gpio-keys: Unable to get irq number"
313 " for GPIO %d, error %d\n",
314 gpio, error);
315 gpio_free(gpio);
316 goto fail;
319 error = request_irq(irq, tosakbd_interrupt,
320 IRQF_DISABLED | IRQF_TRIGGER_RISING,
321 "tosakbd", pdev);
323 if (error) {
324 printk("tosakbd: Can't get IRQ: %d: error %d!\n",
325 irq, error);
326 gpio_free(gpio);
327 goto fail;
331 /* Set Strobe lines as outputs - set high */
332 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++) {
333 int gpio = TOSA_GPIO_KEY_STROBE(i);
334 error = gpio_request(gpio, "tosakbd");
335 if (error < 0) {
336 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
337 " error %d\n", gpio, error);
338 goto fail2;
341 error = gpio_direction_output(gpio, 1);
342 if (error < 0) {
343 printk(KERN_ERR "tosakbd: failed to configure input"
344 " direction for GPIO %d, error %d\n",
345 gpio, error);
346 gpio_free(gpio);
347 goto fail2;
352 error = input_register_device(input_dev);
353 if (error) {
354 printk(KERN_ERR "tosakbd: Unable to register input device, "
355 "error: %d\n", error);
356 goto fail2;
359 printk(KERN_INFO "input: Tosa Keyboard Registered\n");
361 return 0;
363 fail2:
364 while (--i >= 0)
365 gpio_free(TOSA_GPIO_KEY_STROBE(i));
367 i = TOSA_KEY_SENSE_NUM;
368 fail:
369 while (--i >= 0) {
370 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), pdev);
371 gpio_free(TOSA_GPIO_KEY_SENSE(i));
374 platform_set_drvdata(pdev, NULL);
375 input_free_device(input_dev);
376 kfree(tosakbd);
378 return error;
381 static int __devexit tosakbd_remove(struct platform_device *dev)
383 int i;
384 struct tosakbd *tosakbd = platform_get_drvdata(dev);
386 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++)
387 gpio_free(TOSA_GPIO_KEY_STROBE(i));
389 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
390 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), dev);
391 gpio_free(TOSA_GPIO_KEY_SENSE(i));
394 del_timer_sync(&tosakbd->timer);
396 input_unregister_device(tosakbd->input);
398 kfree(tosakbd);
400 return 0;
403 static struct platform_driver tosakbd_driver = {
404 .probe = tosakbd_probe,
405 .remove = __devexit_p(tosakbd_remove),
406 .suspend = tosakbd_suspend,
407 .resume = tosakbd_resume,
408 .driver = {
409 .name = "tosa-keyboard",
410 .owner = THIS_MODULE,
414 static int __devinit tosakbd_init(void)
416 return platform_driver_register(&tosakbd_driver);
419 static void __exit tosakbd_exit(void)
421 platform_driver_unregister(&tosakbd_driver);
424 module_init(tosakbd_init);
425 module_exit(tosakbd_exit);
427 MODULE_AUTHOR("Dirk Opfer <Dirk@Opfer-Online.de>");
428 MODULE_DESCRIPTION("Tosa Keyboard Driver");
429 MODULE_LICENSE("GPL v2");
430 MODULE_ALIAS("platform:tosa-keyboard");