2 * Keyboard driver for Sharp Corgi models (SL-C7xx)
4 * Copyright (c) 2004-2005 Richard Purdie
6 * Based on xtkbd.c/locomkbd.c
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/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>
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>
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
37 #define HINGE_SCAN_INTERVAL (HZ/4)
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_CANCEL KEY_F4
43 #define CORGI_KEY_OFF KEY_SUSPEND
44 #define CORGI_KEY_EXOK KEY_F5
45 #define CORGI_KEY_EXCANCEL KEY_F6
46 #define CORGI_KEY_EXJOGDOWN KEY_F7
47 #define CORGI_KEY_EXJOGUP KEY_F8
48 #define CORGI_KEY_JAP1 KEY_LEFTCTRL
49 #define CORGI_KEY_JAP2 KEY_LEFTALT
50 #define CORGI_KEY_MAIL KEY_F10
51 #define CORGI_KEY_OK KEY_F11
52 #define CORGI_KEY_MENU KEY_F12
53 #define CORGI_HINGE_0 KEY_KP0
54 #define CORGI_HINGE_1 KEY_KP1
55 #define CORGI_HINGE_2 KEY_KP2
57 static unsigned char corgikbd_keycode
[NR_SCANCODES
] = {
59 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 */
60 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 */
61 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 */
62 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 */
63 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 */
64 CORGI_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 */
65 KEY_SYSRQ
, CORGI_KEY_JAP1
, CORGI_KEY_JAP2
, CORGI_KEY_CANCEL
, CORGI_KEY_OK
, CORGI_KEY_MENU
, KEY_LEFT
, KEY_DOWN
, KEY_RIGHT
, 0, 0, 0, 0, 0, 0, 0, /* 97-112 */
66 CORGI_KEY_OFF
, CORGI_KEY_EXOK
, CORGI_KEY_EXCANCEL
, CORGI_KEY_EXJOGDOWN
, CORGI_KEY_EXJOGUP
, 0, 0, 0, 0, 0, 0, 0, /* 113-124 */
67 CORGI_HINGE_0
, CORGI_HINGE_1
, CORGI_HINGE_2
/* 125-127 */
72 unsigned char keycode
[ARRAY_SIZE(corgikbd_keycode
)];
73 struct input_dev input
;
76 unsigned char state
[ARRAY_SIZE(corgikbd_keycode
)];
79 struct timer_list timer
;
80 struct timer_list htimer
;
83 static void handle_scancode(unsigned int pressed
,unsigned int scancode
, struct corgikbd
*corgikbd_data
)
85 if (pressed
&& !(corgikbd_data
->state
[scancode
] & CORGIKBD_PRESSED
)) {
86 corgikbd_data
->state
[scancode
] |= CORGIKBD_PRESSED
;
87 input_report_key(&corgikbd_data
->input
, corgikbd_data
->keycode
[scancode
], 1);
88 if (corgikbd_data
->keycode
[scancode
] == CORGI_KEY_OFF
)
89 input_event(&corgikbd_data
->input
, EV_PWR
, CORGI_KEY_OFF
, 1);
90 } else if (!pressed
&& corgikbd_data
->state
[scancode
] & CORGIKBD_PRESSED
) {
91 corgikbd_data
->state
[scancode
] &= ~CORGIKBD_PRESSED
;
92 input_report_key(&corgikbd_data
->input
, corgikbd_data
->keycode
[scancode
], 0);
96 #define KB_DISCHARGE_DELAY 10
97 #define KB_ACTIVATE_DELAY 10
99 /* Helper functions for reading the keyboard matrix
100 * Note: We should really be using pxa_gpio_mode to alter GPDR but it
101 * requires a function call per GPIO bit which is excessive
102 * when we need to access 12 bits at once multiple times.
103 * These functions must be called within local_irq_save()/local_irq_restore()
106 static inline void corgikbd_discharge_all(void)
109 GPCR2
= CORGI_GPIO_ALL_STROBE_BIT
;
110 GPDR2
&= ~CORGI_GPIO_ALL_STROBE_BIT
;
113 static inline void corgikbd_activate_all(void)
115 // STROBE ALL -> High
116 GPSR2
= CORGI_GPIO_ALL_STROBE_BIT
;
117 GPDR2
|= CORGI_GPIO_ALL_STROBE_BIT
;
119 udelay(KB_DISCHARGE_DELAY
);
121 // Clear any interrupts we may have triggered when altering the GPIO lines
122 GEDR1
= CORGI_GPIO_HIGH_SENSE_BIT
;
123 GEDR2
= CORGI_GPIO_LOW_SENSE_BIT
;
126 static inline void corgikbd_activate_col(int col
)
128 // STROBE col -> High, not col -> HiZ
129 GPSR2
= CORGI_GPIO_STROBE_BIT(col
);
130 GPDR2
= (GPDR2
& ~CORGI_GPIO_ALL_STROBE_BIT
) | CORGI_GPIO_STROBE_BIT(col
);
133 static inline void corgikbd_reset_col(int col
)
136 GPCR2
= CORGI_GPIO_STROBE_BIT(col
);
137 // STROBE col -> out, not col -> HiZ
138 GPDR2
= (GPDR2
& ~CORGI_GPIO_ALL_STROBE_BIT
) | CORGI_GPIO_STROBE_BIT(col
);
141 #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))
144 * The corgi keyboard only generates interrupts when a key is pressed.
145 * When a key is pressed, we enable a timer which then scans the
146 * keyboard to detect when the key is released.
149 /* Scan the hardware keyboard and push any changes up through the input layer */
150 static void corgikbd_scankeyboard(struct corgikbd
*corgikbd_data
, struct pt_regs
*regs
)
152 unsigned int row
, col
, rowd
, scancode
;
154 unsigned int num_pressed
;
156 spin_lock_irqsave(&corgikbd_data
->lock
, flags
);
159 input_regs(&corgikbd_data
->input
, regs
);
162 for (col
= 0; col
< KB_COLS
; col
++) {
164 * Discharge the output driver capacitatance
165 * in the keyboard matrix. (Yes it is significant..)
168 corgikbd_discharge_all();
169 udelay(KB_DISCHARGE_DELAY
);
171 corgikbd_activate_col(col
);
172 udelay(KB_ACTIVATE_DELAY
);
174 rowd
= GET_ROWS_STATUS(col
);
175 for (row
= 0; row
< KB_ROWS
; row
++) {
176 scancode
= SCANCODE(row
, col
);
177 handle_scancode((rowd
& KB_ROWMASK(row
)), scancode
, corgikbd_data
);
178 if (rowd
& KB_ROWMASK(row
))
181 corgikbd_reset_col(col
);
184 corgikbd_activate_all();
186 input_sync(&corgikbd_data
->input
);
188 /* if any keys are pressed, enable the timer */
190 mod_timer(&corgikbd_data
->timer
, jiffies
+ SCAN_INTERVAL
);
192 spin_unlock_irqrestore(&corgikbd_data
->lock
, flags
);
196 * corgi keyboard interrupt handler.
198 static irqreturn_t
corgikbd_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
200 struct corgikbd
*corgikbd_data
= dev_id
;
202 if (!timer_pending(&corgikbd_data
->timer
)) {
203 /** wait chattering delay **/
205 corgikbd_scankeyboard(corgikbd_data
, regs
);
212 * corgi timer checking for released keys
214 static void corgikbd_timer_callback(unsigned long data
)
216 struct corgikbd
*corgikbd_data
= (struct corgikbd
*) data
;
217 corgikbd_scankeyboard(corgikbd_data
, NULL
);
221 * The hinge switches generate no interrupt so they need to be
222 * monitored by a timer.
224 * When we detect changes, we debounce it and then pass the three
225 * positions the system can take as keypresses to the input system.
228 #define HINGE_STABLE_COUNT 2
229 static int sharpsl_hinge_state
;
230 static int hinge_count
;
232 static void corgikbd_hinge_timer(unsigned long data
)
234 struct corgikbd
*corgikbd_data
= (struct corgikbd
*) data
;
238 gprr
= read_scoop_reg(SCOOP_GPRR
) & (CORGI_SCP_SWA
| CORGI_SCP_SWB
);
239 if (gprr
!= sharpsl_hinge_state
) {
241 sharpsl_hinge_state
= gprr
;
242 } else if (hinge_count
< HINGE_STABLE_COUNT
) {
244 if (hinge_count
>= HINGE_STABLE_COUNT
) {
245 spin_lock_irqsave(&corgikbd_data
->lock
, flags
);
247 handle_scancode((sharpsl_hinge_state
== 0x00), 125, corgikbd_data
); /* Keyboard with Landscape Screen */
248 handle_scancode((sharpsl_hinge_state
== 0x08), 126, corgikbd_data
); /* No Keyboard with Portrait Screen */
249 handle_scancode((sharpsl_hinge_state
== 0x0c), 127, corgikbd_data
); /* Keyboard and Screen Closed */
250 input_sync(&corgikbd_data
->input
);
252 spin_unlock_irqrestore(&corgikbd_data
->lock
, flags
);
255 mod_timer(&corgikbd_data
->htimer
, jiffies
+ HINGE_SCAN_INTERVAL
);
258 static int __init
corgikbd_probe(struct device
*dev
)
261 struct corgikbd
*corgikbd
;
263 corgikbd
= kcalloc(1, sizeof(struct corgikbd
), GFP_KERNEL
);
267 dev_set_drvdata(dev
,corgikbd
);
268 strcpy(corgikbd
->phys
, "corgikbd/input0");
270 spin_lock_init(corgikbd
->lock
);
272 /* Init Keyboard rescan timer */
273 init_timer(&corgikbd
->timer
);
274 corgikbd
->timer
.function
= corgikbd_timer_callback
;
275 corgikbd
->timer
.data
= (unsigned long) corgikbd
;
277 /* Init Hinge Timer */
278 init_timer(&corgikbd
->htimer
);
279 corgikbd
->htimer
.function
= corgikbd_hinge_timer
;
280 corgikbd
->htimer
.data
= (unsigned long) corgikbd
;
282 init_input_dev(&corgikbd
->input
);
283 corgikbd
->input
.private = corgikbd
;
284 corgikbd
->input
.name
= "Corgi Keyboard";
285 corgikbd
->input
.dev
= dev
;
286 corgikbd
->input
.phys
= corgikbd
->phys
;
287 corgikbd
->input
.id
.bustype
= BUS_HOST
;
288 corgikbd
->input
.id
.vendor
= 0x0001;
289 corgikbd
->input
.id
.product
= 0x0001;
290 corgikbd
->input
.id
.version
= 0x0100;
291 corgikbd
->input
.evbit
[0] = BIT(EV_KEY
) | BIT(EV_REP
) | BIT(EV_PWR
);
292 corgikbd
->input
.keycode
= corgikbd
->keycode
;
293 corgikbd
->input
.keycodesize
= sizeof(unsigned char);
294 corgikbd
->input
.keycodemax
= ARRAY_SIZE(corgikbd_keycode
);
296 memcpy(corgikbd
->keycode
, corgikbd_keycode
, sizeof(corgikbd
->keycode
));
297 for (i
= 0; i
< ARRAY_SIZE(corgikbd_keycode
); i
++)
298 set_bit(corgikbd
->keycode
[i
], corgikbd
->input
.keybit
);
299 clear_bit(0, corgikbd
->input
.keybit
);
301 input_register_device(&corgikbd
->input
);
302 mod_timer(&corgikbd
->htimer
, jiffies
+ HINGE_SCAN_INTERVAL
);
304 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
305 for (i
= 0; i
< CORGI_KEY_SENSE_NUM
; i
++) {
306 pxa_gpio_mode(CORGI_GPIO_KEY_SENSE(i
) | GPIO_IN
);
307 if (request_irq(CORGI_IRQ_GPIO_KEY_SENSE(i
), corgikbd_interrupt
,
308 SA_INTERRUPT
, "corgikbd", corgikbd
))
309 printk(KERN_WARNING
"corgikbd: Can't get IRQ: %d!\n", i
);
311 set_irq_type(CORGI_IRQ_GPIO_KEY_SENSE(i
),IRQT_RISING
);
314 /* Set Strobe lines as outputs - set high */
315 for (i
= 0; i
< CORGI_KEY_STROBE_NUM
; i
++)
316 pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i
) | GPIO_OUT
| GPIO_DFLT_HIGH
);
318 printk(KERN_INFO
"input: Corgi Keyboard Registered\n");
323 static int corgikbd_remove(struct device
*dev
)
326 struct corgikbd
*corgikbd
= dev_get_drvdata(dev
);
328 for (i
= 0; i
< CORGI_KEY_SENSE_NUM
; i
++)
329 free_irq(CORGI_IRQ_GPIO_KEY_SENSE(i
), corgikbd
);
331 del_timer_sync(&corgikbd
->htimer
);
332 del_timer_sync(&corgikbd
->timer
);
334 input_unregister_device(&corgikbd
->input
);
341 static struct device_driver corgikbd_driver
= {
342 .name
= "corgi-keyboard",
343 .bus
= &platform_bus_type
,
344 .probe
= corgikbd_probe
,
345 .remove
= corgikbd_remove
,
348 static int __devinit
corgikbd_init(void)
350 return driver_register(&corgikbd_driver
);
353 static void __exit
corgikbd_exit(void)
355 driver_unregister(&corgikbd_driver
);
358 module_init(corgikbd_init
);
359 module_exit(corgikbd_exit
);
361 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
362 MODULE_DESCRIPTION("Corgi Keyboard Driver");
363 MODULE_LICENSE("GPLv2");