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>
21 #include <linux/slab.h>
23 #include <mach/gpio.h>
24 #include <mach/tosa.h>
26 #define KB_ROWMASK(r) (1 << (r))
27 #define SCANCODE(r, c) (((r)<<4) + (c) + 1)
28 #define NR_SCANCODES SCANCODE(TOSA_KEY_SENSE_NUM - 1, TOSA_KEY_STROBE_NUM - 1) + 1
30 #define SCAN_INTERVAL (HZ/10)
32 #define KB_DISCHARGE_DELAY 10
33 #define KB_ACTIVATE_DELAY 10
35 static unsigned short tosakbd_keycode[NR_SCANCODES] = {
37 0, KEY_W, 0, 0, 0, KEY_K, KEY_BACKSPACE, KEY_P,
38 0, 0, 0, 0, 0, 0, 0, 0,
39 KEY_Q, KEY_E, KEY_T, KEY_Y, 0, KEY_O, KEY_I, KEY_COMMA,
40 0, 0, 0, 0, 0, 0, 0, 0,
41 KEY_A, KEY_D, KEY_G, KEY_U, 0, KEY_L, KEY_ENTER, KEY_DOT,
42 0, 0, 0, 0, 0, 0, 0, 0,
43 KEY_Z, KEY_C, KEY_V, KEY_J, TOSA_KEY_ADDRESSBOOK, TOSA_KEY_CANCEL, TOSA_KEY_CENTER, TOSA_KEY_OK,
44 KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 0, 0,
45 KEY_S, KEY_R, KEY_B, KEY_N, TOSA_KEY_CALENDAR, TOSA_KEY_HOMEPAGE, KEY_LEFTCTRL, TOSA_KEY_LIGHT,
46 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0,
47 KEY_TAB, KEY_SLASH, KEY_H, KEY_M, TOSA_KEY_MENU, 0, KEY_UP, 0,
48 0, 0, TOSA_KEY_FN, 0, 0, 0, 0, 0,
49 KEY_X, KEY_F, KEY_SPACE, KEY_APOSTROPHE, TOSA_KEY_MAIL, KEY_LEFT, KEY_DOWN, KEY_RIGHT,
54 unsigned short keycode[ARRAY_SIZE(tosakbd_keycode)];
55 struct input_dev *input;
57 spinlock_t lock; /* protect kbd scanning */
58 struct timer_list timer;
62 /* Helper functions for reading the keyboard matrix
63 * Note: We should really be using the generic gpio functions to alter
64 * GPDR but it requires a function call per GPIO bit which is
65 * excessive when we need to access 12 bits at once, multiple times.
66 * These functions must be called within local_irq_save()/local_irq_restore()
69 #define GET_ROWS_STATUS(c) ((GPLR2 & TOSA_GPIO_ALL_SENSE_BIT) >> TOSA_GPIO_ALL_SENSE_RSHIFT)
71 static inline void tosakbd_discharge_all(void)
74 GPCR1 = TOSA_GPIO_HIGH_STROBE_BIT;
75 GPDR1 &= ~TOSA_GPIO_HIGH_STROBE_BIT;
76 GPCR2 = TOSA_GPIO_LOW_STROBE_BIT;
77 GPDR2 &= ~TOSA_GPIO_LOW_STROBE_BIT;
80 static inline void tosakbd_activate_all(void)
82 /* STROBE ALL -> High */
83 GPSR1 = TOSA_GPIO_HIGH_STROBE_BIT;
84 GPDR1 |= TOSA_GPIO_HIGH_STROBE_BIT;
85 GPSR2 = TOSA_GPIO_LOW_STROBE_BIT;
86 GPDR2 |= TOSA_GPIO_LOW_STROBE_BIT;
88 udelay(KB_DISCHARGE_DELAY);
91 GEDR2 |= TOSA_GPIO_ALL_SENSE_BIT;
94 static inline void tosakbd_activate_col(int col)
97 /* STROBE col -> High, not col -> HiZ */
98 GPSR1 = TOSA_GPIO_STROBE_BIT(col);
99 GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
101 /* STROBE col -> High, not col -> HiZ */
102 GPSR2 = TOSA_GPIO_STROBE_BIT(col);
103 GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
107 static inline void tosakbd_reset_col(int col)
110 /* STROBE col -> Low */
111 GPCR1 = TOSA_GPIO_STROBE_BIT(col);
112 /* STROBE col -> out, not col -> HiZ */
113 GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
115 /* STROBE col -> Low */
116 GPCR2 = TOSA_GPIO_STROBE_BIT(col);
117 /* STROBE col -> out, not col -> HiZ */
118 GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
122 * The tosa keyboard only generates interrupts when a key is pressed.
123 * So when a key is pressed, we enable a timer. This timer scans the
124 * keyboard, and this is how we detect when the key is released.
127 /* Scan the hardware keyboard and push any changes up through the input layer */
128 static void tosakbd_scankeyboard(struct platform_device *dev)
130 struct tosakbd *tosakbd = platform_get_drvdata(dev);
131 unsigned int row, col, rowd;
133 unsigned int num_pressed = 0;
135 spin_lock_irqsave(&tosakbd->lock, flags);
137 if (tosakbd->suspended)
140 for (col = 0; col < TOSA_KEY_STROBE_NUM; col++) {
142 * Discharge the output driver capacitatance
143 * in the keyboard matrix. (Yes it is significant..)
145 tosakbd_discharge_all();
146 udelay(KB_DISCHARGE_DELAY);
148 tosakbd_activate_col(col);
149 udelay(KB_ACTIVATE_DELAY);
151 rowd = GET_ROWS_STATUS(col);
153 for (row = 0; row < TOSA_KEY_SENSE_NUM; row++) {
154 unsigned int scancode, pressed;
155 scancode = SCANCODE(row, col);
156 pressed = rowd & KB_ROWMASK(row);
158 if (pressed && !tosakbd->keycode[scancode])
160 "unhandled scancode: 0x%02x\n",
163 input_report_key(tosakbd->input,
164 tosakbd->keycode[scancode],
170 tosakbd_reset_col(col);
173 tosakbd_activate_all();
175 input_sync(tosakbd->input);
177 /* if any keys are pressed, enable the timer */
179 mod_timer(&tosakbd->timer, jiffies + SCAN_INTERVAL);
182 spin_unlock_irqrestore(&tosakbd->lock, flags);
186 * tosa keyboard interrupt handler.
188 static irqreturn_t tosakbd_interrupt(int irq, void *__dev)
190 struct platform_device *dev = __dev;
191 struct tosakbd *tosakbd = platform_get_drvdata(dev);
193 if (!timer_pending(&tosakbd->timer)) {
194 /** wait chattering delay **/
196 tosakbd_scankeyboard(dev);
203 * tosa timer checking for released keys
205 static void tosakbd_timer_callback(unsigned long __dev)
207 struct platform_device *dev = (struct platform_device *)__dev;
209 tosakbd_scankeyboard(dev);
213 static int tosakbd_suspend(struct platform_device *dev, pm_message_t state)
215 struct tosakbd *tosakbd = platform_get_drvdata(dev);
218 spin_lock_irqsave(&tosakbd->lock, flags);
219 tosakbd->suspended = true;
220 spin_unlock_irqrestore(&tosakbd->lock, flags);
222 del_timer_sync(&tosakbd->timer);
227 static int tosakbd_resume(struct platform_device *dev)
229 struct tosakbd *tosakbd = platform_get_drvdata(dev);
231 tosakbd->suspended = false;
232 tosakbd_scankeyboard(dev);
237 #define tosakbd_suspend NULL
238 #define tosakbd_resume NULL
241 static int __devinit tosakbd_probe(struct platform_device *pdev) {
244 struct tosakbd *tosakbd;
245 struct input_dev *input_dev;
248 tosakbd = kzalloc(sizeof(struct tosakbd), GFP_KERNEL);
252 input_dev = input_allocate_device();
258 platform_set_drvdata(pdev, tosakbd);
260 spin_lock_init(&tosakbd->lock);
262 /* Init Keyboard rescan timer */
263 init_timer(&tosakbd->timer);
264 tosakbd->timer.function = tosakbd_timer_callback;
265 tosakbd->timer.data = (unsigned long) pdev;
267 tosakbd->input = input_dev;
269 input_set_drvdata(input_dev, tosakbd);
270 input_dev->name = "Tosa Keyboard";
271 input_dev->phys = "tosakbd/input0";
272 input_dev->dev.parent = &pdev->dev;
274 input_dev->id.bustype = BUS_HOST;
275 input_dev->id.vendor = 0x0001;
276 input_dev->id.product = 0x0001;
277 input_dev->id.version = 0x0100;
279 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
280 input_dev->keycode = tosakbd->keycode;
281 input_dev->keycodesize = sizeof(tosakbd->keycode[0]);
282 input_dev->keycodemax = ARRAY_SIZE(tosakbd_keycode);
284 memcpy(tosakbd->keycode, tosakbd_keycode, sizeof(tosakbd_keycode));
286 for (i = 0; i < ARRAY_SIZE(tosakbd_keycode); i++)
287 __set_bit(tosakbd->keycode[i], input_dev->keybit);
288 __clear_bit(KEY_RESERVED, input_dev->keybit);
290 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
291 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
292 int gpio = TOSA_GPIO_KEY_SENSE(i);
294 error = gpio_request(gpio, "tosakbd");
296 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
297 " error %d\n", gpio, error);
301 error = gpio_direction_input(TOSA_GPIO_KEY_SENSE(i));
303 printk(KERN_ERR "tosakbd: failed to configure input"
304 " direction for GPIO %d, error %d\n",
310 irq = gpio_to_irq(gpio);
313 printk(KERN_ERR "gpio-keys: Unable to get irq number"
314 " for GPIO %d, error %d\n",
320 error = request_irq(irq, tosakbd_interrupt,
321 IRQF_DISABLED | IRQF_TRIGGER_RISING,
325 printk("tosakbd: Can't get IRQ: %d: error %d!\n",
332 /* Set Strobe lines as outputs - set high */
333 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++) {
334 int gpio = TOSA_GPIO_KEY_STROBE(i);
335 error = gpio_request(gpio, "tosakbd");
337 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
338 " error %d\n", gpio, error);
342 error = gpio_direction_output(gpio, 1);
344 printk(KERN_ERR "tosakbd: failed to configure input"
345 " direction for GPIO %d, error %d\n",
353 error = input_register_device(input_dev);
355 printk(KERN_ERR "tosakbd: Unable to register input device, "
356 "error: %d\n", error);
360 printk(KERN_INFO "input: Tosa Keyboard Registered\n");
366 gpio_free(TOSA_GPIO_KEY_STROBE(i));
368 i = TOSA_KEY_SENSE_NUM;
371 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), pdev);
372 gpio_free(TOSA_GPIO_KEY_SENSE(i));
375 platform_set_drvdata(pdev, NULL);
376 input_free_device(input_dev);
382 static int __devexit tosakbd_remove(struct platform_device *dev)
385 struct tosakbd *tosakbd = platform_get_drvdata(dev);
387 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++)
388 gpio_free(TOSA_GPIO_KEY_STROBE(i));
390 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
391 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), dev);
392 gpio_free(TOSA_GPIO_KEY_SENSE(i));
395 del_timer_sync(&tosakbd->timer);
397 input_unregister_device(tosakbd->input);
404 static struct platform_driver tosakbd_driver = {
405 .probe = tosakbd_probe,
406 .remove = __devexit_p(tosakbd_remove),
407 .suspend = tosakbd_suspend,
408 .resume = tosakbd_resume,
410 .name = "tosa-keyboard",
411 .owner = THIS_MODULE,
415 static int __devinit tosakbd_init(void)
417 return platform_driver_register(&tosakbd_driver);
420 static void __exit tosakbd_exit(void)
422 platform_driver_unregister(&tosakbd_driver);
425 module_init(tosakbd_init);
426 module_exit(tosakbd_exit);
428 MODULE_AUTHOR("Dirk Opfer <Dirk@Opfer-Online.de>");
429 MODULE_DESCRIPTION("Tosa Keyboard Driver");
430 MODULE_LICENSE("GPL v2");
431 MODULE_ALIAS("platform:tosa-keyboard");