2 * Keyboard driver for the AAED-2000 dev board
4 * Copyright (c) 2006 Nicolas Bellido Y Ortega
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>
22 #include <linux/workqueue.h>
24 #include <asm/arch/hardware.h>
25 #include <asm/arch/aaed2000.h>
29 #define KB_ROWMASK(r) (1 << (r))
30 #define SCANCODE(r,c) (((c) * KB_ROWS) + (r))
31 #define NR_SCANCODES (KB_COLS * KB_ROWS)
33 #define SCAN_INTERVAL (50) /* ms */
34 #define KB_ACTIVATE_DELAY (20) /* us */
36 static unsigned char aaedkbd_keycode
[NR_SCANCODES
] = {
37 KEY_9
, KEY_0
, KEY_MINUS
, KEY_EQUAL
, KEY_BACKSPACE
, 0, KEY_SPACE
, KEY_KP6
, 0, KEY_KPDOT
, 0, 0,
38 KEY_K
, KEY_M
, KEY_O
, KEY_DOT
, KEY_SLASH
, 0, KEY_F
, 0, 0, 0, KEY_LEFTSHIFT
, 0,
39 KEY_I
, KEY_P
, KEY_LEFTBRACE
, KEY_RIGHTBRACE
, KEY_BACKSLASH
, 0, 0, 0, 0, 0, KEY_RIGHTSHIFT
, 0,
40 KEY_8
, KEY_L
, KEY_SEMICOLON
, KEY_APOSTROPHE
, KEY_ENTER
, 0, 0, 0, 0, 0, 0, 0,
41 KEY_J
, KEY_H
, KEY_B
, KEY_KP8
, KEY_KP4
, 0, KEY_C
, KEY_D
, KEY_S
, KEY_A
, 0, KEY_CAPSLOCK
,
42 KEY_Y
, KEY_U
, KEY_N
, KEY_T
, 0, 0, KEY_R
, KEY_E
, KEY_W
, KEY_Q
, 0, KEY_TAB
,
43 KEY_7
, KEY_6
, KEY_G
, 0, KEY_5
, 0, KEY_4
, KEY_3
, KEY_2
, KEY_1
, 0, KEY_GRAVE
,
44 0, 0, KEY_COMMA
, 0, KEY_KP2
, 0, KEY_V
, KEY_LEFTALT
, KEY_X
, KEY_Z
, 0, KEY_LEFTCTRL
48 unsigned char keycode
[ARRAY_SIZE(aaedkbd_keycode
)];
49 struct input_dev
*input
;
50 struct work_struct workq
;
51 int kbdscan_state
[KB_COLS
];
52 int kbdscan_count
[KB_COLS
];
55 #define KBDSCAN_STABLE_COUNT 2
57 static void aaedkbd_report_col(struct aaedkbd
*aaedkbd
,
58 unsigned int col
, unsigned int rowd
)
60 unsigned int scancode
, pressed
;
63 for (row
= 0; row
< KB_ROWS
; row
++) {
64 scancode
= SCANCODE(row
, col
);
65 pressed
= rowd
& KB_ROWMASK(row
);
67 input_report_key(aaedkbd
->input
, aaedkbd
->keycode
[scancode
], pressed
);
71 /* Scan the hardware keyboard and push any changes up through the input layer */
72 static void aaedkbd_work(void *data
)
74 struct aaedkbd
*aaedkbd
= data
;
75 unsigned int col
, rowd
;
79 AAEC_GPIO_KSCAN
= col
+ 8;
80 udelay(KB_ACTIVATE_DELAY
);
81 rowd
= AAED_EXT_GPIO
& AAED_EGPIO_KBD_SCAN
;
83 if (rowd
!= aaedkbd
->kbdscan_state
[col
]) {
84 aaedkbd
->kbdscan_count
[col
] = 0;
85 aaedkbd
->kbdscan_state
[col
] = rowd
;
86 } else if (++aaedkbd
->kbdscan_count
[col
] >= KBDSCAN_STABLE_COUNT
) {
87 aaedkbd_report_col(aaedkbd
, col
, rowd
);
90 } while (col
< KB_COLS
);
92 AAEC_GPIO_KSCAN
= 0x07;
93 input_sync(aaedkbd
->input
);
95 schedule_delayed_work(&aaedkbd
->workq
, msecs_to_jiffies(SCAN_INTERVAL
));
98 static int aaedkbd_open(struct input_dev
*indev
)
100 struct aaedkbd
*aaedkbd
= indev
->private;
102 schedule_delayed_work(&aaedkbd
->workq
, msecs_to_jiffies(SCAN_INTERVAL
));
107 static void aaedkbd_close(struct input_dev
*indev
)
109 struct aaedkbd
*aaedkbd
= indev
->private;
111 cancel_delayed_work(&aaedkbd
->workq
);
112 flush_scheduled_work();
115 static int __devinit
aaedkbd_probe(struct platform_device
*pdev
)
117 struct aaedkbd
*aaedkbd
;
118 struct input_dev
*input_dev
;
122 aaedkbd
= kzalloc(sizeof(struct aaedkbd
), GFP_KERNEL
);
123 input_dev
= input_allocate_device();
124 if (!aaedkbd
|| !input_dev
) {
129 platform_set_drvdata(pdev
, aaedkbd
);
131 aaedkbd
->input
= input_dev
;
133 /* Init keyboard rescan workqueue */
134 INIT_WORK(&aaedkbd
->workq
, aaedkbd_work
, aaedkbd
);
136 memcpy(aaedkbd
->keycode
, aaedkbd_keycode
, sizeof(aaedkbd
->keycode
));
138 input_dev
->name
= "AAED-2000 Keyboard";
139 input_dev
->phys
= "aaedkbd/input0";
140 input_dev
->id
.bustype
= BUS_HOST
;
141 input_dev
->id
.vendor
= 0x0001;
142 input_dev
->id
.product
= 0x0001;
143 input_dev
->id
.version
= 0x0100;
144 input_dev
->cdev
.dev
= &pdev
->dev
;
145 input_dev
->private = aaedkbd
;
147 input_dev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_REP
);
148 input_dev
->keycode
= aaedkbd
->keycode
;
149 input_dev
->keycodesize
= sizeof(unsigned char);
150 input_dev
->keycodemax
= ARRAY_SIZE(aaedkbd_keycode
);
152 for (i
= 0; i
< ARRAY_SIZE(aaedkbd_keycode
); i
++)
153 set_bit(aaedkbd
->keycode
[i
], input_dev
->keybit
);
154 clear_bit(0, input_dev
->keybit
);
156 input_dev
->open
= aaedkbd_open
;
157 input_dev
->close
= aaedkbd_close
;
159 error
= input_register_device(aaedkbd
->input
);
165 fail
: kfree(aaedkbd
);
166 input_free_device(input_dev
);
170 static int __devexit
aaedkbd_remove(struct platform_device
*pdev
)
172 struct aaedkbd
*aaedkbd
= platform_get_drvdata(pdev
);
174 input_unregister_device(aaedkbd
->input
);
180 static struct platform_driver aaedkbd_driver
= {
181 .probe
= aaedkbd_probe
,
182 .remove
= __devexit_p(aaedkbd_remove
),
184 .name
= "aaed2000-keyboard",
188 static int __init
aaedkbd_init(void)
190 return platform_driver_register(&aaedkbd_driver
);
193 static void __exit
aaedkbd_exit(void)
195 platform_driver_unregister(&aaedkbd_driver
);
198 module_init(aaedkbd_init
);
199 module_exit(aaedkbd_exit
);
201 MODULE_AUTHOR("Nicolas Bellido Y Ortega");
202 MODULE_DESCRIPTION("AAED-2000 Keyboard Driver");
203 MODULE_LICENSE("GPLv2");