2 * Keyboard driver for Sharp Spitz, Borzoi and Akita (SL-Cxx00 series)
4 * Copyright (c) 2005 Richard Purdie
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.
14 #include <linux/delay.h>
15 #include <linux/platform_device.h>
16 #include <linux/init.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/jiffies.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
23 #include <asm/arch/spitz.h>
24 #include <asm/arch/hardware.h>
25 #include <asm/arch/pxa-regs.h>
26 #include <asm/arch/pxa2xx-gpio.h>
30 #define KB_ROWMASK(r) (1 << (r))
31 #define SCANCODE(r,c) (((r)<<4) + (c) + 1)
32 #define NR_SCANCODES ((KB_ROWS<<4) + 1)
34 #define SCAN_INTERVAL (50) /* ms */
35 #define HINGE_SCAN_INTERVAL (150) /* ms */
37 #define SPITZ_KEY_CALENDER KEY_F1
38 #define SPITZ_KEY_ADDRESS KEY_F2
39 #define SPITZ_KEY_FN KEY_F3
40 #define SPITZ_KEY_CANCEL KEY_F4
41 #define SPITZ_KEY_EXOK KEY_F5
42 #define SPITZ_KEY_EXCANCEL KEY_F6
43 #define SPITZ_KEY_EXJOGDOWN KEY_F7
44 #define SPITZ_KEY_EXJOGUP KEY_F8
45 #define SPITZ_KEY_JAP1 KEY_LEFTALT
46 #define SPITZ_KEY_JAP2 KEY_RIGHTCTRL
47 #define SPITZ_KEY_SYNC KEY_F9
48 #define SPITZ_KEY_MAIL KEY_F10
49 #define SPITZ_KEY_OK KEY_F11
50 #define SPITZ_KEY_MENU KEY_F12
52 static unsigned char spitzkbd_keycode
[NR_SCANCODES
] = {
54 KEY_LEFTCTRL
, KEY_1
, KEY_3
, KEY_5
, KEY_6
, KEY_7
, KEY_9
, KEY_0
, KEY_BACKSPACE
, SPITZ_KEY_EXOK
, SPITZ_KEY_EXCANCEL
, 0, 0, 0, 0, 0, /* 1-16 */
55 0, KEY_2
, KEY_4
, KEY_R
, KEY_Y
, KEY_8
, KEY_I
, KEY_O
, KEY_P
, SPITZ_KEY_EXJOGDOWN
, SPITZ_KEY_EXJOGUP
, 0, 0, 0, 0, 0, /* 17-32 */
56 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 */
57 SPITZ_KEY_ADDRESS
, 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 */
58 SPITZ_KEY_CALENDER
, KEY_A
, KEY_D
, KEY_C
, KEY_B
, KEY_N
, KEY_DOT
, 0, KEY_ENTER
, KEY_LEFTSHIFT
, 0, 0, 0, 0, 0, 0, /* 65-80 */
59 SPITZ_KEY_MAIL
, KEY_Z
, KEY_X
, KEY_MINUS
, KEY_SPACE
, KEY_COMMA
, 0, KEY_UP
, 0, 0, SPITZ_KEY_FN
, 0, 0, 0, 0, 0, /* 81-96 */
60 KEY_SYSRQ
, SPITZ_KEY_JAP1
, SPITZ_KEY_JAP2
, SPITZ_KEY_CANCEL
, SPITZ_KEY_OK
, SPITZ_KEY_MENU
, KEY_LEFT
, KEY_DOWN
, KEY_RIGHT
, 0, 0, 0, 0, 0, 0, 0 /* 97-112 */
63 static int spitz_strobes
[] = {
64 SPITZ_GPIO_KEY_STROBE0
,
65 SPITZ_GPIO_KEY_STROBE1
,
66 SPITZ_GPIO_KEY_STROBE2
,
67 SPITZ_GPIO_KEY_STROBE3
,
68 SPITZ_GPIO_KEY_STROBE4
,
69 SPITZ_GPIO_KEY_STROBE5
,
70 SPITZ_GPIO_KEY_STROBE6
,
71 SPITZ_GPIO_KEY_STROBE7
,
72 SPITZ_GPIO_KEY_STROBE8
,
73 SPITZ_GPIO_KEY_STROBE9
,
74 SPITZ_GPIO_KEY_STROBE10
,
77 static int spitz_senses
[] = {
78 SPITZ_GPIO_KEY_SENSE0
,
79 SPITZ_GPIO_KEY_SENSE1
,
80 SPITZ_GPIO_KEY_SENSE2
,
81 SPITZ_GPIO_KEY_SENSE3
,
82 SPITZ_GPIO_KEY_SENSE4
,
83 SPITZ_GPIO_KEY_SENSE5
,
84 SPITZ_GPIO_KEY_SENSE6
,
88 unsigned char keycode
[ARRAY_SIZE(spitzkbd_keycode
)];
89 struct input_dev
*input
;
93 struct timer_list timer
;
94 struct timer_list htimer
;
96 unsigned int suspended
;
97 unsigned long suspend_jiffies
;
100 #define KB_DISCHARGE_DELAY 10
101 #define KB_ACTIVATE_DELAY 10
103 /* Helper functions for reading the keyboard matrix
104 * Note: We should really be using pxa_gpio_mode to alter GPDR but it
105 * requires a function call per GPIO bit which is excessive
106 * when we need to access 11 bits at once, multiple times.
107 * These functions must be called within local_irq_save()/local_irq_restore()
110 static inline void spitzkbd_discharge_all(void)
113 GPCR0
= SPITZ_GPIO_G0_STROBE_BIT
;
114 GPDR0
&= ~SPITZ_GPIO_G0_STROBE_BIT
;
115 GPCR1
= SPITZ_GPIO_G1_STROBE_BIT
;
116 GPDR1
&= ~SPITZ_GPIO_G1_STROBE_BIT
;
117 GPCR2
= SPITZ_GPIO_G2_STROBE_BIT
;
118 GPDR2
&= ~SPITZ_GPIO_G2_STROBE_BIT
;
119 GPCR3
= SPITZ_GPIO_G3_STROBE_BIT
;
120 GPDR3
&= ~SPITZ_GPIO_G3_STROBE_BIT
;
123 static inline void spitzkbd_activate_all(void)
125 /* STROBE ALL -> High */
126 GPSR0
= SPITZ_GPIO_G0_STROBE_BIT
;
127 GPDR0
|= SPITZ_GPIO_G0_STROBE_BIT
;
128 GPSR1
= SPITZ_GPIO_G1_STROBE_BIT
;
129 GPDR1
|= SPITZ_GPIO_G1_STROBE_BIT
;
130 GPSR2
= SPITZ_GPIO_G2_STROBE_BIT
;
131 GPDR2
|= SPITZ_GPIO_G2_STROBE_BIT
;
132 GPSR3
= SPITZ_GPIO_G3_STROBE_BIT
;
133 GPDR3
|= SPITZ_GPIO_G3_STROBE_BIT
;
135 udelay(KB_DISCHARGE_DELAY
);
137 /* Clear any interrupts we may have triggered when altering the GPIO lines */
138 GEDR0
= SPITZ_GPIO_G0_SENSE_BIT
;
139 GEDR1
= SPITZ_GPIO_G1_SENSE_BIT
;
140 GEDR2
= SPITZ_GPIO_G2_SENSE_BIT
;
141 GEDR3
= SPITZ_GPIO_G3_SENSE_BIT
;
144 static inline void spitzkbd_activate_col(int col
)
146 int gpio
= spitz_strobes
[col
];
147 GPDR0
&= ~SPITZ_GPIO_G0_STROBE_BIT
;
148 GPDR1
&= ~SPITZ_GPIO_G1_STROBE_BIT
;
149 GPDR2
&= ~SPITZ_GPIO_G2_STROBE_BIT
;
150 GPDR3
&= ~SPITZ_GPIO_G3_STROBE_BIT
;
151 GPSR(gpio
) = GPIO_bit(gpio
);
152 GPDR(gpio
) |= GPIO_bit(gpio
);
155 static inline void spitzkbd_reset_col(int col
)
157 int gpio
= spitz_strobes
[col
];
158 GPDR0
&= ~SPITZ_GPIO_G0_STROBE_BIT
;
159 GPDR1
&= ~SPITZ_GPIO_G1_STROBE_BIT
;
160 GPDR2
&= ~SPITZ_GPIO_G2_STROBE_BIT
;
161 GPDR3
&= ~SPITZ_GPIO_G3_STROBE_BIT
;
162 GPCR(gpio
) = GPIO_bit(gpio
);
163 GPDR(gpio
) |= GPIO_bit(gpio
);
166 static inline int spitzkbd_get_row_status(int col
)
168 return ((GPLR0
>> 12) & 0x01) | ((GPLR0
>> 16) & 0x02)
169 | ((GPLR2
>> 25) & 0x04) | ((GPLR1
<< 1) & 0x08)
170 | ((GPLR1
>> 0) & 0x10) | ((GPLR1
>> 1) & 0x60);
174 * The spitz keyboard only generates interrupts when a key is pressed.
175 * When a key is pressed, we enable a timer which then scans the
176 * keyboard to detect when the key is released.
179 /* Scan the hardware keyboard and push any changes up through the input layer */
180 static void spitzkbd_scankeyboard(struct spitzkbd
*spitzkbd_data
)
182 unsigned int row
, col
, rowd
;
184 unsigned int num_pressed
, pwrkey
= ((GPLR(SPITZ_GPIO_ON_KEY
) & GPIO_bit(SPITZ_GPIO_ON_KEY
)) != 0);
186 if (spitzkbd_data
->suspended
)
189 spin_lock_irqsave(&spitzkbd_data
->lock
, flags
);
192 for (col
= 0; col
< KB_COLS
; col
++) {
194 * Discharge the output driver capacitatance
195 * in the keyboard matrix. (Yes it is significant..)
198 spitzkbd_discharge_all();
199 udelay(KB_DISCHARGE_DELAY
);
201 spitzkbd_activate_col(col
);
202 udelay(KB_ACTIVATE_DELAY
);
204 rowd
= spitzkbd_get_row_status(col
);
205 for (row
= 0; row
< KB_ROWS
; row
++) {
206 unsigned int scancode
, pressed
;
208 scancode
= SCANCODE(row
, col
);
209 pressed
= rowd
& KB_ROWMASK(row
);
211 input_report_key(spitzkbd_data
->input
, spitzkbd_data
->keycode
[scancode
], pressed
);
216 spitzkbd_reset_col(col
);
219 spitzkbd_activate_all();
221 input_report_key(spitzkbd_data
->input
, SPITZ_KEY_SYNC
, (GPLR(SPITZ_GPIO_SYNC
) & GPIO_bit(SPITZ_GPIO_SYNC
)) != 0 );
222 input_report_key(spitzkbd_data
->input
, KEY_SUSPEND
, pwrkey
);
224 if (pwrkey
&& time_after(jiffies
, spitzkbd_data
->suspend_jiffies
+ msecs_to_jiffies(1000))) {
225 input_event(spitzkbd_data
->input
, EV_PWR
, KEY_SUSPEND
, 1);
226 spitzkbd_data
->suspend_jiffies
= jiffies
;
229 input_sync(spitzkbd_data
->input
);
231 /* if any keys are pressed, enable the timer */
233 mod_timer(&spitzkbd_data
->timer
, jiffies
+ msecs_to_jiffies(SCAN_INTERVAL
));
235 spin_unlock_irqrestore(&spitzkbd_data
->lock
, flags
);
239 * spitz keyboard interrupt handler.
241 static irqreturn_t
spitzkbd_interrupt(int irq
, void *dev_id
)
243 struct spitzkbd
*spitzkbd_data
= dev_id
;
245 if (!timer_pending(&spitzkbd_data
->timer
)) {
246 /** wait chattering delay **/
248 spitzkbd_scankeyboard(spitzkbd_data
);
255 * spitz timer checking for released keys
257 static void spitzkbd_timer_callback(unsigned long data
)
259 struct spitzkbd
*spitzkbd_data
= (struct spitzkbd
*) data
;
261 spitzkbd_scankeyboard(spitzkbd_data
);
265 * The hinge switches generate an interrupt.
266 * We debounce the switches and pass them to the input system.
269 static irqreturn_t
spitzkbd_hinge_isr(int irq
, void *dev_id
)
271 struct spitzkbd
*spitzkbd_data
= dev_id
;
273 if (!timer_pending(&spitzkbd_data
->htimer
))
274 mod_timer(&spitzkbd_data
->htimer
, jiffies
+ msecs_to_jiffies(HINGE_SCAN_INTERVAL
));
279 #define HINGE_STABLE_COUNT 2
280 static int sharpsl_hinge_state
;
281 static int hinge_count
;
283 static void spitzkbd_hinge_timer(unsigned long data
)
285 struct spitzkbd
*spitzkbd_data
= (struct spitzkbd
*) data
;
289 state
= GPLR(SPITZ_GPIO_SWA
) & (GPIO_bit(SPITZ_GPIO_SWA
)|GPIO_bit(SPITZ_GPIO_SWB
));
290 state
|= (GPLR(SPITZ_GPIO_AK_INT
) & GPIO_bit(SPITZ_GPIO_AK_INT
));
291 if (state
!= sharpsl_hinge_state
) {
293 sharpsl_hinge_state
= state
;
294 } else if (hinge_count
< HINGE_STABLE_COUNT
) {
298 if (hinge_count
>= HINGE_STABLE_COUNT
) {
299 spin_lock_irqsave(&spitzkbd_data
->lock
, flags
);
301 input_report_switch(spitzkbd_data
->input
, SW_LID
, ((GPLR(SPITZ_GPIO_SWA
) & GPIO_bit(SPITZ_GPIO_SWA
)) != 0));
302 input_report_switch(spitzkbd_data
->input
, SW_TABLET_MODE
, ((GPLR(SPITZ_GPIO_SWB
) & GPIO_bit(SPITZ_GPIO_SWB
)) != 0));
303 input_report_switch(spitzkbd_data
->input
, SW_HEADPHONE_INSERT
, ((GPLR(SPITZ_GPIO_AK_INT
) & GPIO_bit(SPITZ_GPIO_AK_INT
)) != 0));
304 input_sync(spitzkbd_data
->input
);
306 spin_unlock_irqrestore(&spitzkbd_data
->lock
, flags
);
308 mod_timer(&spitzkbd_data
->htimer
, jiffies
+ msecs_to_jiffies(HINGE_SCAN_INTERVAL
));
313 static int spitzkbd_suspend(struct platform_device
*dev
, pm_message_t state
)
316 struct spitzkbd
*spitzkbd
= platform_get_drvdata(dev
);
317 spitzkbd
->suspended
= 1;
319 /* Set Strobe lines as inputs - *except* strobe line 0 leave this
320 enabled so we can detect a power button press for resume */
321 for (i
= 1; i
< SPITZ_KEY_STROBE_NUM
; i
++)
322 pxa_gpio_mode(spitz_strobes
[i
] | GPIO_IN
);
327 static int spitzkbd_resume(struct platform_device
*dev
)
330 struct spitzkbd
*spitzkbd
= platform_get_drvdata(dev
);
332 for (i
= 0; i
< SPITZ_KEY_STROBE_NUM
; i
++)
333 pxa_gpio_mode(spitz_strobes
[i
] | GPIO_OUT
| GPIO_DFLT_HIGH
);
335 /* Upon resume, ignore the suspend key for a short while */
336 spitzkbd
->suspend_jiffies
= jiffies
;
337 spitzkbd
->suspended
= 0;
342 #define spitzkbd_suspend NULL
343 #define spitzkbd_resume NULL
346 static int __init
spitzkbd_probe(struct platform_device
*dev
)
348 struct spitzkbd
*spitzkbd
;
349 struct input_dev
*input_dev
;
350 int i
, err
= -ENOMEM
;
352 spitzkbd
= kzalloc(sizeof(struct spitzkbd
), GFP_KERNEL
);
353 input_dev
= input_allocate_device();
354 if (!spitzkbd
|| !input_dev
)
357 platform_set_drvdata(dev
, spitzkbd
);
358 strcpy(spitzkbd
->phys
, "spitzkbd/input0");
360 spin_lock_init(&spitzkbd
->lock
);
362 /* Init Keyboard rescan timer */
363 init_timer(&spitzkbd
->timer
);
364 spitzkbd
->timer
.function
= spitzkbd_timer_callback
;
365 spitzkbd
->timer
.data
= (unsigned long) spitzkbd
;
367 /* Init Hinge Timer */
368 init_timer(&spitzkbd
->htimer
);
369 spitzkbd
->htimer
.function
= spitzkbd_hinge_timer
;
370 spitzkbd
->htimer
.data
= (unsigned long) spitzkbd
;
372 spitzkbd
->suspend_jiffies
= jiffies
;
374 spitzkbd
->input
= input_dev
;
376 input_dev
->name
= "Spitz Keyboard";
377 input_dev
->phys
= spitzkbd
->phys
;
378 input_dev
->dev
.parent
= &dev
->dev
;
380 input_dev
->id
.bustype
= BUS_HOST
;
381 input_dev
->id
.vendor
= 0x0001;
382 input_dev
->id
.product
= 0x0001;
383 input_dev
->id
.version
= 0x0100;
385 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
) |
386 BIT_MASK(EV_PWR
) | BIT_MASK(EV_SW
);
387 input_dev
->keycode
= spitzkbd
->keycode
;
388 input_dev
->keycodesize
= sizeof(unsigned char);
389 input_dev
->keycodemax
= ARRAY_SIZE(spitzkbd_keycode
);
391 memcpy(spitzkbd
->keycode
, spitzkbd_keycode
, sizeof(spitzkbd
->keycode
));
392 for (i
= 0; i
< ARRAY_SIZE(spitzkbd_keycode
); i
++)
393 set_bit(spitzkbd
->keycode
[i
], input_dev
->keybit
);
394 clear_bit(0, input_dev
->keybit
);
395 set_bit(KEY_SUSPEND
, input_dev
->keybit
);
396 set_bit(SW_LID
, input_dev
->swbit
);
397 set_bit(SW_TABLET_MODE
, input_dev
->swbit
);
398 set_bit(SW_HEADPHONE_INSERT
, input_dev
->swbit
);
400 err
= input_register_device(input_dev
);
404 mod_timer(&spitzkbd
->htimer
, jiffies
+ msecs_to_jiffies(HINGE_SCAN_INTERVAL
));
406 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
407 for (i
= 0; i
< SPITZ_KEY_SENSE_NUM
; i
++) {
408 pxa_gpio_mode(spitz_senses
[i
] | GPIO_IN
);
409 if (request_irq(IRQ_GPIO(spitz_senses
[i
]), spitzkbd_interrupt
,
410 IRQF_DISABLED
|IRQF_TRIGGER_RISING
,
411 "Spitzkbd Sense", spitzkbd
))
412 printk(KERN_WARNING
"spitzkbd: Can't get Sense IRQ: %d!\n", i
);
415 /* Set Strobe lines as outputs - set high */
416 for (i
= 0; i
< SPITZ_KEY_STROBE_NUM
; i
++)
417 pxa_gpio_mode(spitz_strobes
[i
] | GPIO_OUT
| GPIO_DFLT_HIGH
);
419 pxa_gpio_mode(SPITZ_GPIO_SYNC
| GPIO_IN
);
420 pxa_gpio_mode(SPITZ_GPIO_ON_KEY
| GPIO_IN
);
421 pxa_gpio_mode(SPITZ_GPIO_SWA
| GPIO_IN
);
422 pxa_gpio_mode(SPITZ_GPIO_SWB
| GPIO_IN
);
424 request_irq(SPITZ_IRQ_GPIO_SYNC
, spitzkbd_interrupt
,
425 IRQF_DISABLED
| IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
426 "Spitzkbd Sync", spitzkbd
);
427 request_irq(SPITZ_IRQ_GPIO_ON_KEY
, spitzkbd_interrupt
,
428 IRQF_DISABLED
| IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
429 "Spitzkbd PwrOn", spitzkbd
);
430 request_irq(SPITZ_IRQ_GPIO_SWA
, spitzkbd_hinge_isr
,
431 IRQF_DISABLED
| IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
432 "Spitzkbd SWA", spitzkbd
);
433 request_irq(SPITZ_IRQ_GPIO_SWB
, spitzkbd_hinge_isr
,
434 IRQF_DISABLED
| IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
435 "Spitzkbd SWB", spitzkbd
);
436 request_irq(SPITZ_IRQ_GPIO_AK_INT
, spitzkbd_hinge_isr
,
437 IRQF_DISABLED
| IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
438 "Spitzkbd HP", spitzkbd
);
442 fail
: input_free_device(input_dev
);
447 static int spitzkbd_remove(struct platform_device
*dev
)
450 struct spitzkbd
*spitzkbd
= platform_get_drvdata(dev
);
452 for (i
= 0; i
< SPITZ_KEY_SENSE_NUM
; i
++)
453 free_irq(IRQ_GPIO(spitz_senses
[i
]), spitzkbd
);
455 free_irq(SPITZ_IRQ_GPIO_SYNC
, spitzkbd
);
456 free_irq(SPITZ_IRQ_GPIO_ON_KEY
, spitzkbd
);
457 free_irq(SPITZ_IRQ_GPIO_SWA
, spitzkbd
);
458 free_irq(SPITZ_IRQ_GPIO_SWB
, spitzkbd
);
459 free_irq(SPITZ_IRQ_GPIO_AK_INT
, spitzkbd
);
461 del_timer_sync(&spitzkbd
->htimer
);
462 del_timer_sync(&spitzkbd
->timer
);
464 input_unregister_device(spitzkbd
->input
);
471 static struct platform_driver spitzkbd_driver
= {
472 .probe
= spitzkbd_probe
,
473 .remove
= spitzkbd_remove
,
474 .suspend
= spitzkbd_suspend
,
475 .resume
= spitzkbd_resume
,
477 .name
= "spitz-keyboard",
478 .owner
= THIS_MODULE
,
482 static int __devinit
spitzkbd_init(void)
484 return platform_driver_register(&spitzkbd_driver
);
487 static void __exit
spitzkbd_exit(void)
489 platform_driver_unregister(&spitzkbd_driver
);
492 module_init(spitzkbd_init
);
493 module_exit(spitzkbd_exit
);
495 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
496 MODULE_DESCRIPTION("Spitz Keyboard Driver");
497 MODULE_LICENSE("GPL v2");
498 MODULE_ALIAS("platform:spitz-keyboard");