blob: a247006757ded4e49865286628feade022309245 [file] [log] [blame]
Dmitry Baryshkov93e90122008-01-21 01:04:20 -05001/*
2 * Keyboard driver for Sharp Tosa models (SL-6000x)
3 *
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2007 Dmitry Baryshkov
6 *
7 * Based on xtkbd.c/locomkbd.c/corgikbd.c
8 *
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.
12 *
13 */
14
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
22#include <asm/arch/gpio.h>
23#include <asm/arch/tosa.h>
24
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
28
29#define SCAN_INTERVAL (HZ/10)
30
31#define KB_DISCHARGE_DELAY 10
32#define KB_ACTIVATE_DELAY 10
33
34static unsigned int tosakbd_keycode[NR_SCANCODES] = {
350,
360, KEY_W, 0, 0, 0, KEY_K, KEY_BACKSPACE, KEY_P,
370, 0, 0, 0, 0, 0, 0, 0,
38KEY_Q, KEY_E, KEY_T, KEY_Y, 0, KEY_O, KEY_I, KEY_COMMA,
390, 0, 0, 0, 0, 0, 0, 0,
40KEY_A, KEY_D, KEY_G, KEY_U, 0, KEY_L, KEY_ENTER, KEY_DOT,
410, 0, 0, 0, 0, 0, 0, 0,
42KEY_Z, KEY_C, KEY_V, KEY_J, TOSA_KEY_ADDRESSBOOK, TOSA_KEY_CANCEL, TOSA_KEY_CENTER, TOSA_KEY_OK,
43KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 0, 0,
44KEY_S, KEY_R, KEY_B, KEY_N, TOSA_KEY_CALENDAR, TOSA_KEY_HOMEPAGE, KEY_LEFTCTRL, TOSA_KEY_LIGHT,
450, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0,
46KEY_TAB, KEY_SLASH, KEY_H, KEY_M, TOSA_KEY_MENU, 0, KEY_UP, 0,
470, 0, TOSA_KEY_FN, 0, 0, 0, 0, 0,
48KEY_X, KEY_F, KEY_SPACE, KEY_APOSTROPHE, TOSA_KEY_MAIL, KEY_LEFT, KEY_DOWN, KEY_RIGHT,
490, 0, 0,
50};
51
52struct tosakbd {
53 unsigned int keycode[ARRAY_SIZE(tosakbd_keycode)];
54 struct input_dev *input;
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -040055 int suspended;
Dmitry Baryshkov93e90122008-01-21 01:04:20 -050056 spinlock_t lock; /* protect kbd scanning */
57 struct timer_list timer;
58};
59
60
61/* Helper functions for reading the keyboard matrix
62 * Note: We should really be using pxa_gpio_mode to alter GPDR but it
63 * requires a function call per GPIO bit which is excessive
64 * 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.
67 */
68#define GET_ROWS_STATUS(c) ((GPLR2 & TOSA_GPIO_ALL_SENSE_BIT) >> TOSA_GPIO_ALL_SENSE_RSHIFT)
69
70static inline void tosakbd_discharge_all(void)
71{
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;
77}
78
79static inline void tosakbd_activate_all(void)
80{
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;
86
87 udelay(KB_DISCHARGE_DELAY);
88
89 /* STATE CLEAR */
90 GEDR2 |= TOSA_GPIO_ALL_SENSE_BIT;
91}
92
93static inline void tosakbd_activate_col(int col)
94{
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);
103 }
104}
105
106static inline void tosakbd_reset_col(int col)
107{
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);
118 }
119}
120/*
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.
124 */
125
126/* Scan the hardware keyboard and push any changes up through the input layer */
127static void tosakbd_scankeyboard(struct platform_device *dev)
128{
129 struct tosakbd *tosakbd = platform_get_drvdata(dev);
130 unsigned int row, col, rowd;
131 unsigned long flags;
132 unsigned int num_pressed = 0;
133
134 spin_lock_irqsave(&tosakbd->lock, flags);
135
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -0400136 if (tosakbd->suspended)
137 goto out;
138
Dmitry Baryshkov93e90122008-01-21 01:04:20 -0500139 for (col = 0; col < TOSA_KEY_STROBE_NUM; col++) {
140 /*
141 * Discharge the output driver capacitatance
142 * in the keyboard matrix. (Yes it is significant..)
143 */
144 tosakbd_discharge_all();
145 udelay(KB_DISCHARGE_DELAY);
146
147 tosakbd_activate_col(col);
148 udelay(KB_ACTIVATE_DELAY);
149
150 rowd = GET_ROWS_STATUS(col);
151
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);
156
157 if (pressed && !tosakbd->keycode[scancode])
158 dev_warn(&dev->dev,
159 "unhandled scancode: 0x%02x\n",
160 scancode);
161
162 input_report_key(tosakbd->input,
163 tosakbd->keycode[scancode],
164 pressed);
165 if (pressed)
166 num_pressed++;
167 }
168
169 tosakbd_reset_col(col);
170 }
171
172 tosakbd_activate_all();
173
174 input_sync(tosakbd->input);
175
176 /* if any keys are pressed, enable the timer */
177 if (num_pressed)
178 mod_timer(&tosakbd->timer, jiffies + SCAN_INTERVAL);
179
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -0400180 out:
Dmitry Baryshkov93e90122008-01-21 01:04:20 -0500181 spin_unlock_irqrestore(&tosakbd->lock, flags);
182}
183
184/*
185 * tosa keyboard interrupt handler.
186 */
187static irqreturn_t tosakbd_interrupt(int irq, void *__dev)
188{
189 struct platform_device *dev = __dev;
190 struct tosakbd *tosakbd = platform_get_drvdata(dev);
191
192 if (!timer_pending(&tosakbd->timer)) {
193 /** wait chattering delay **/
194 udelay(20);
195 tosakbd_scankeyboard(dev);
196 }
197
198 return IRQ_HANDLED;
199}
200
201/*
202 * tosa timer checking for released keys
203 */
204static void tosakbd_timer_callback(unsigned long __dev)
205{
206 struct platform_device *dev = (struct platform_device *)__dev;
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -0400207
Dmitry Baryshkov93e90122008-01-21 01:04:20 -0500208 tosakbd_scankeyboard(dev);
209}
210
211#ifdef CONFIG_PM
212static int tosakbd_suspend(struct platform_device *dev, pm_message_t state)
213{
214 struct tosakbd *tosakbd = platform_get_drvdata(dev);
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -0400215 unsigned long flags;
216
217 spin_lock_irqsave(&tosakbd->lock, flags);
218 PGSR1 = (PGSR1 & ~TOSA_GPIO_LOW_STROBE_BIT);
219 PGSR2 = (PGSR2 & ~TOSA_GPIO_HIGH_STROBE_BIT);
220 tosakbd->suspended = 1;
221 spin_unlock_irqrestore(&tosakbd->lock, flags);
Dmitry Baryshkov93e90122008-01-21 01:04:20 -0500222
223 del_timer_sync(&tosakbd->timer);
224
225 return 0;
226}
227
228static int tosakbd_resume(struct platform_device *dev)
229{
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -0400230 struct tosakbd *tosakbd = platform_get_drvdata(dev);
231
232 tosakbd->suspended = 0;
Dmitry Baryshkov93e90122008-01-21 01:04:20 -0500233 tosakbd_scankeyboard(dev);
234
235 return 0;
236}
237#else
238#define tosakbd_suspend NULL
239#define tosakbd_resume NULL
240#endif
241
242static int __devinit tosakbd_probe(struct platform_device *pdev) {
243
244 int i;
245 struct tosakbd *tosakbd;
246 struct input_dev *input_dev;
247 int error;
248
249 tosakbd = kzalloc(sizeof(struct tosakbd), GFP_KERNEL);
250 if (!tosakbd)
251 return -ENOMEM;
252
253 input_dev = input_allocate_device();
254 if (!input_dev) {
255 kfree(tosakbd);
256 return -ENOMEM;
257 }
258
259 platform_set_drvdata(pdev, tosakbd);
260
261 spin_lock_init(&tosakbd->lock);
262
263 /* Init Keyboard rescan timer */
264 init_timer(&tosakbd->timer);
265 tosakbd->timer.function = tosakbd_timer_callback;
266 tosakbd->timer.data = (unsigned long) pdev;
267
268 tosakbd->input = input_dev;
269
270 input_set_drvdata(input_dev, tosakbd);
271 input_dev->name = "Tosa Keyboard";
272 input_dev->phys = "tosakbd/input0";
273 input_dev->dev.parent = &pdev->dev;
274
275 input_dev->id.bustype = BUS_HOST;
276 input_dev->id.vendor = 0x0001;
277 input_dev->id.product = 0x0001;
278 input_dev->id.version = 0x0100;
279
280 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
281 input_dev->keycode = tosakbd->keycode;
282 input_dev->keycodesize = sizeof(unsigned int);
283 input_dev->keycodemax = ARRAY_SIZE(tosakbd_keycode);
284
285 memcpy(tosakbd->keycode, tosakbd_keycode, sizeof(tosakbd_keycode));
286
287 for (i = 0; i < ARRAY_SIZE(tosakbd_keycode); i++)
288 __set_bit(tosakbd->keycode[i], input_dev->keybit);
289 clear_bit(0, input_dev->keybit);
290
291 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
292 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
293 int gpio = TOSA_GPIO_KEY_SENSE(i);
294 int irq;
295 error = gpio_request(gpio, "tosakbd");
296 if (error < 0) {
297 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
298 " error %d\n", gpio, error);
299 goto fail;
300 }
301
302 error = gpio_direction_input(TOSA_GPIO_KEY_SENSE(i));
303 if (error < 0) {
304 printk(KERN_ERR "tosakbd: failed to configure input"
305 " direction for GPIO %d, error %d\n",
306 gpio, error);
307 gpio_free(gpio);
308 goto fail;
309 }
310
311 irq = gpio_to_irq(gpio);
312 if (irq < 0) {
313 error = irq;
314 printk(KERN_ERR "gpio-keys: Unable to get irq number"
315 " for GPIO %d, error %d\n",
316 gpio, error);
317 gpio_free(gpio);
318 goto fail;
319 }
320
321 error = request_irq(irq, tosakbd_interrupt,
322 IRQF_DISABLED | IRQF_TRIGGER_RISING,
323 "tosakbd", pdev);
324
325 if (error) {
326 printk("tosakbd: Can't get IRQ: %d: error %d!\n",
327 irq, error);
328 gpio_free(gpio);
329 goto fail;
330 }
331 }
332
333 /* Set Strobe lines as outputs - set high */
334 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++) {
335 int gpio = TOSA_GPIO_KEY_STROBE(i);
336 error = gpio_request(gpio, "tosakbd");
337 if (error < 0) {
338 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
339 " error %d\n", gpio, error);
340 goto fail2;
341 }
342
343 error = gpio_direction_output(gpio, 1);
344 if (error < 0) {
345 printk(KERN_ERR "tosakbd: failed to configure input"
346 " direction for GPIO %d, error %d\n",
347 gpio, error);
348 gpio_free(gpio);
349 goto fail;
350 }
351
352 }
353
354 error = input_register_device(input_dev);
355 if (error) {
356 printk(KERN_ERR "tosakbd: Unable to register input device, "
357 "error: %d\n", error);
358 goto fail;
359 }
360
361 printk(KERN_INFO "input: Tosa Keyboard Registered\n");
362
363 return 0;
364
365fail2:
366 while (--i >= 0)
367 gpio_free(TOSA_GPIO_KEY_STROBE(i));
368
369 i = TOSA_KEY_SENSE_NUM;
370fail:
371 while (--i >= 0) {
372 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), pdev);
373 gpio_free(TOSA_GPIO_KEY_SENSE(i));
374 }
375
376 platform_set_drvdata(pdev, NULL);
377 input_free_device(input_dev);
378 kfree(tosakbd);
379
380 return error;
381}
382
Dmitry Baryshkov5550fba2008-04-02 11:22:51 -0400383static int __devexit tosakbd_remove(struct platform_device *dev)
384{
Dmitry Baryshkov93e90122008-01-21 01:04:20 -0500385 int i;
386 struct tosakbd *tosakbd = platform_get_drvdata(dev);
387
388 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++)
389 gpio_free(TOSA_GPIO_KEY_STROBE(i));
390
391 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
392 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), dev);
393 gpio_free(TOSA_GPIO_KEY_SENSE(i));
394 }
395
396 del_timer_sync(&tosakbd->timer);
397
398 input_unregister_device(tosakbd->input);
399
400 kfree(tosakbd);
401
402 return 0;
403}
404
405static struct platform_driver tosakbd_driver = {
406 .probe = tosakbd_probe,
407 .remove = __devexit_p(tosakbd_remove),
408 .suspend = tosakbd_suspend,
409 .resume = tosakbd_resume,
410 .driver = {
411 .name = "tosa-keyboard",
412 },
413};
414
415static int __devinit tosakbd_init(void)
416{
417 return platform_driver_register(&tosakbd_driver);
418}
419
420static void __exit tosakbd_exit(void)
421{
422 platform_driver_unregister(&tosakbd_driver);
423}
424
425module_init(tosakbd_init);
426module_exit(tosakbd_exit);
427
428MODULE_AUTHOR("Dirk Opfer <Dirk@Opfer-Online.de>");
429MODULE_DESCRIPTION("Tosa Keyboard Driver");
430MODULE_LICENSE("GPL v2");