aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/keyboard
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/keyboard')
-rw-r--r--drivers/input/keyboard/Kconfig29
-rw-r--r--drivers/input/keyboard/Makefile3
-rw-r--r--drivers/input/keyboard/atkbd.c91
-rw-r--r--drivers/input/keyboard/lkkbd.c1
-rw-r--r--drivers/input/keyboard/pxa27x_keyboard.c274
-rw-r--r--drivers/input/keyboard/pxa27x_keypad.c572
-rw-r--r--drivers/input/keyboard/tosakbd.c415
7 files changed, 1086 insertions, 299 deletions
diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index 086d58c0ccbe..8ea709be3306 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -154,6 +154,27 @@ config KEYBOARD_SPITZ
154 To compile this driver as a module, choose M here: the 154 To compile this driver as a module, choose M here: the
155 module will be called spitzkbd. 155 module will be called spitzkbd.
156 156
157config KEYBOARD_TOSA
158 tristate "Tosa keyboard"
159 depends on MACH_TOSA
160 default y
161 help
162 Say Y here to enable the keyboard on the Sharp Zaurus SL-6000x (Tosa)
163
164 To compile this driver as a module, choose M here: the
165 module will be called tosakbd.
166
167config KEYBOARD_TOSA_USE_EXT_KEYCODES
168 bool "Tosa keyboard: use extended keycodes"
169 depends on KEYBOARD_TOSA
170 default n
171 help
172 Say Y here to enable the tosa keyboard driver to generate extended
173 (>= 127) keycodes. Be aware, that they can't be correctly interpreted
174 by either console keyboard driver or by Kdrive keybd driver.
175
176 Say Y only if you know, what you are doing!
177
157config KEYBOARD_AMIGA 178config KEYBOARD_AMIGA
158 tristate "Amiga keyboard" 179 tristate "Amiga keyboard"
159 depends on AMIGA 180 depends on AMIGA
@@ -239,13 +260,13 @@ config KEYBOARD_OMAP
239 module will be called omap-keypad. 260 module will be called omap-keypad.
240 261
241config KEYBOARD_PXA27x 262config KEYBOARD_PXA27x
242 tristate "PXA27x keyboard support" 263 tristate "PXA27x/PXA3xx keypad support"
243 depends on PXA27x 264 depends on PXA27x || PXA3xx
244 help 265 help
245 Enable support for PXA27x matrix keyboard controller 266 Enable support for PXA27x/PXA3xx keypad controller
246 267
247 To compile this driver as a module, choose M here: the 268 To compile this driver as a module, choose M here: the
248 module will be called pxa27x_keyboard. 269 module will be called pxa27x_keypad.
249 270
250config KEYBOARD_AAED2000 271config KEYBOARD_AAED2000
251 tristate "AAED-2000 keyboard" 272 tristate "AAED-2000 keyboard"
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index e97455fdcc83..e741f4031012 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -15,10 +15,11 @@ obj-$(CONFIG_KEYBOARD_NEWTON) += newtonkbd.o
15obj-$(CONFIG_KEYBOARD_STOWAWAY) += stowaway.o 15obj-$(CONFIG_KEYBOARD_STOWAWAY) += stowaway.o
16obj-$(CONFIG_KEYBOARD_CORGI) += corgikbd.o 16obj-$(CONFIG_KEYBOARD_CORGI) += corgikbd.o
17obj-$(CONFIG_KEYBOARD_SPITZ) += spitzkbd.o 17obj-$(CONFIG_KEYBOARD_SPITZ) += spitzkbd.o
18obj-$(CONFIG_KEYBOARD_TOSA) += tosakbd.o
18obj-$(CONFIG_KEYBOARD_HIL) += hil_kbd.o 19obj-$(CONFIG_KEYBOARD_HIL) += hil_kbd.o
19obj-$(CONFIG_KEYBOARD_HIL_OLD) += hilkbd.o 20obj-$(CONFIG_KEYBOARD_HIL_OLD) += hilkbd.o
20obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o 21obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
21obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keyboard.o 22obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keypad.o
22obj-$(CONFIG_KEYBOARD_AAED2000) += aaed2000_kbd.o 23obj-$(CONFIG_KEYBOARD_AAED2000) += aaed2000_kbd.o
23obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o 24obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o
24obj-$(CONFIG_KEYBOARD_HP6XX) += jornada680_kbd.o 25obj-$(CONFIG_KEYBOARD_HP6XX) += jornada680_kbd.o
diff --git a/drivers/input/keyboard/atkbd.c b/drivers/input/keyboard/atkbd.c
index b39c5b31e620..4a95adc4cc78 100644
--- a/drivers/input/keyboard/atkbd.c
+++ b/drivers/input/keyboard/atkbd.c
@@ -19,7 +19,6 @@
19 19
20#include <linux/delay.h> 20#include <linux/delay.h>
21#include <linux/module.h> 21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/slab.h> 22#include <linux/slab.h>
24#include <linux/interrupt.h> 23#include <linux/interrupt.h>
25#include <linux/init.h> 24#include <linux/init.h>
@@ -28,6 +27,7 @@
28#include <linux/workqueue.h> 27#include <linux/workqueue.h>
29#include <linux/libps2.h> 28#include <linux/libps2.h>
30#include <linux/mutex.h> 29#include <linux/mutex.h>
30#include <linux/dmi.h>
31 31
32#define DRIVER_DESC "AT and PS/2 keyboard driver" 32#define DRIVER_DESC "AT and PS/2 keyboard driver"
33 33
@@ -201,6 +201,7 @@ struct atkbd {
201 201
202 unsigned short id; 202 unsigned short id;
203 unsigned char keycode[512]; 203 unsigned char keycode[512];
204 DECLARE_BITMAP(force_release_mask, 512);
204 unsigned char set; 205 unsigned char set;
205 unsigned char translated; 206 unsigned char translated;
206 unsigned char extra; 207 unsigned char extra;
@@ -225,6 +226,11 @@ struct atkbd {
225 unsigned long event_mask; 226 unsigned long event_mask;
226}; 227};
227 228
229/*
230 * System-specific ketymap fixup routine
231 */
232static void (*atkbd_platform_fixup)(struct atkbd *);
233
228static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf, 234static ssize_t atkbd_attr_show_helper(struct device *dev, char *buf,
229 ssize_t (*handler)(struct atkbd *, char *)); 235 ssize_t (*handler)(struct atkbd *, char *));
230static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count, 236static ssize_t atkbd_attr_set_helper(struct device *dev, const char *buf, size_t count,
@@ -349,7 +355,7 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
349 struct atkbd *atkbd = serio_get_drvdata(serio); 355 struct atkbd *atkbd = serio_get_drvdata(serio);
350 struct input_dev *dev = atkbd->dev; 356 struct input_dev *dev = atkbd->dev;
351 unsigned int code = data; 357 unsigned int code = data;
352 int scroll = 0, hscroll = 0, click = -1, add_release_event = 0; 358 int scroll = 0, hscroll = 0, click = -1;
353 int value; 359 int value;
354 unsigned char keycode; 360 unsigned char keycode;
355 361
@@ -414,14 +420,6 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
414 "Some program might be trying access hardware directly.\n", 420 "Some program might be trying access hardware directly.\n",
415 data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys); 421 data == ATKBD_RET_ACK ? "ACK" : "NAK", serio->phys);
416 goto out; 422 goto out;
417 case ATKBD_RET_HANGEUL:
418 case ATKBD_RET_HANJA:
419 /*
420 * These keys do not report release and thus need to be
421 * flagged properly
422 */
423 add_release_event = 1;
424 break;
425 case ATKBD_RET_ERR: 423 case ATKBD_RET_ERR:
426 atkbd->err_count++; 424 atkbd->err_count++;
427#ifdef ATKBD_DEBUG 425#ifdef ATKBD_DEBUG
@@ -491,7 +489,7 @@ static irqreturn_t atkbd_interrupt(struct serio *serio, unsigned char data,
491 input_event(dev, EV_KEY, keycode, value); 489 input_event(dev, EV_KEY, keycode, value);
492 input_sync(dev); 490 input_sync(dev);
493 491
494 if (value && add_release_event) { 492 if (value && test_bit(code, atkbd->force_release_mask)) {
495 input_report_key(dev, keycode, 0); 493 input_report_key(dev, keycode, 0);
496 input_sync(dev); 494 input_sync(dev);
497 } 495 }
@@ -824,7 +822,6 @@ static void atkbd_disconnect(struct serio *serio)
824 atkbd_disable(atkbd); 822 atkbd_disable(atkbd);
825 823
826 /* make sure we don't have a command in flight */ 824 /* make sure we don't have a command in flight */
827 synchronize_sched(); /* Allow atkbd_interrupt()s to complete. */
828 flush_scheduled_work(); 825 flush_scheduled_work();
829 826
830 sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group); 827 sysfs_remove_group(&serio->dev.kobj, &atkbd_attribute_group);
@@ -834,6 +831,22 @@ static void atkbd_disconnect(struct serio *serio)
834 kfree(atkbd); 831 kfree(atkbd);
835} 832}
836 833
834/*
835 * Most special keys (Fn+F?) on Dell Latitudes do not generate release
836 * events so we have to do it ourselves.
837 */
838static void atkbd_latitude_keymap_fixup(struct atkbd *atkbd)
839{
840 const unsigned int forced_release_keys[] = {
841 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8f, 0x93,
842 };
843 int i;
844
845 if (atkbd->set == 2)
846 for (i = 0; i < ARRAY_SIZE(forced_release_keys); i++)
847 __set_bit(forced_release_keys[i],
848 atkbd->force_release_mask);
849}
837 850
838/* 851/*
839 * atkbd_set_keycode_table() initializes keyboard's keycode table 852 * atkbd_set_keycode_table() initializes keyboard's keycode table
@@ -842,17 +855,20 @@ static void atkbd_disconnect(struct serio *serio)
842 855
843static void atkbd_set_keycode_table(struct atkbd *atkbd) 856static void atkbd_set_keycode_table(struct atkbd *atkbd)
844{ 857{
858 unsigned int scancode;
845 int i, j; 859 int i, j;
846 860
847 memset(atkbd->keycode, 0, sizeof(atkbd->keycode)); 861 memset(atkbd->keycode, 0, sizeof(atkbd->keycode));
862 bitmap_zero(atkbd->force_release_mask, 512);
848 863
849 if (atkbd->translated) { 864 if (atkbd->translated) {
850 for (i = 0; i < 128; i++) { 865 for (i = 0; i < 128; i++) {
851 atkbd->keycode[i] = atkbd_set2_keycode[atkbd_unxlate_table[i]]; 866 scancode = atkbd_unxlate_table[i];
852 atkbd->keycode[i | 0x80] = atkbd_set2_keycode[atkbd_unxlate_table[i] | 0x80]; 867 atkbd->keycode[i] = atkbd_set2_keycode[scancode];
868 atkbd->keycode[i | 0x80] = atkbd_set2_keycode[scancode | 0x80];
853 if (atkbd->scroll) 869 if (atkbd->scroll)
854 for (j = 0; j < ARRAY_SIZE(atkbd_scroll_keys); j++) 870 for (j = 0; j < ARRAY_SIZE(atkbd_scroll_keys); j++)
855 if ((atkbd_unxlate_table[i] | 0x80) == atkbd_scroll_keys[j].set2) 871 if ((scancode | 0x80) == atkbd_scroll_keys[j].set2)
856 atkbd->keycode[i | 0x80] = atkbd_scroll_keys[j].keycode; 872 atkbd->keycode[i | 0x80] = atkbd_scroll_keys[j].keycode;
857 } 873 }
858 } else if (atkbd->set == 3) { 874 } else if (atkbd->set == 3) {
@@ -861,12 +877,29 @@ static void atkbd_set_keycode_table(struct atkbd *atkbd)
861 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode)); 877 memcpy(atkbd->keycode, atkbd_set2_keycode, sizeof(atkbd->keycode));
862 878
863 if (atkbd->scroll) 879 if (atkbd->scroll)
864 for (i = 0; i < ARRAY_SIZE(atkbd_scroll_keys); i++) 880 for (i = 0; i < ARRAY_SIZE(atkbd_scroll_keys); i++) {
865 atkbd->keycode[atkbd_scroll_keys[i].set2] = atkbd_scroll_keys[i].keycode; 881 scancode = atkbd_scroll_keys[i].set2;
882 atkbd->keycode[scancode] = atkbd_scroll_keys[i].keycode;
883 }
866 } 884 }
867 885
868 atkbd->keycode[atkbd_compat_scancode(atkbd, ATKBD_RET_HANGEUL)] = KEY_HANGUEL; 886/*
869 atkbd->keycode[atkbd_compat_scancode(atkbd, ATKBD_RET_HANJA)] = KEY_HANJA; 887 * HANGEUL and HANJA keys do not send release events so we need to
888 * generate such events ourselves
889 */
890 scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANGEUL);
891 atkbd->keycode[scancode] = KEY_HANGEUL;
892 __set_bit(scancode, atkbd->force_release_mask);
893
894 scancode = atkbd_compat_scancode(atkbd, ATKBD_RET_HANJA);
895 atkbd->keycode[scancode] = KEY_HANJA;
896 __set_bit(scancode, atkbd->force_release_mask);
897
898/*
899 * Perform additional fixups
900 */
901 if (atkbd_platform_fixup)
902 atkbd_platform_fixup(atkbd);
870} 903}
871 904
872/* 905/*
@@ -1401,9 +1434,29 @@ static ssize_t atkbd_show_err_count(struct atkbd *atkbd, char *buf)
1401 return sprintf(buf, "%lu\n", atkbd->err_count); 1434 return sprintf(buf, "%lu\n", atkbd->err_count);
1402} 1435}
1403 1436
1437static int __init atkbd_setup_fixup(const struct dmi_system_id *id)
1438{
1439 atkbd_platform_fixup = id->driver_data;
1440 return 0;
1441}
1442
1443static struct dmi_system_id atkbd_dmi_quirk_table[] __initdata = {
1444 {
1445 .ident = "Dell Latitude series",
1446 .matches = {
1447 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1448 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1449 },
1450 .callback = atkbd_setup_fixup,
1451 .driver_data = atkbd_latitude_keymap_fixup,
1452 },
1453 { }
1454};
1404 1455
1405static int __init atkbd_init(void) 1456static int __init atkbd_init(void)
1406{ 1457{
1458 dmi_check_system(atkbd_dmi_quirk_table);
1459
1407 return serio_register_driver(&atkbd_drv); 1460 return serio_register_driver(&atkbd_drv);
1408} 1461}
1409 1462
diff --git a/drivers/input/keyboard/lkkbd.c b/drivers/input/keyboard/lkkbd.c
index 1b08f4e79dd2..32e2c2605d95 100644
--- a/drivers/input/keyboard/lkkbd.c
+++ b/drivers/input/keyboard/lkkbd.c
@@ -64,7 +64,6 @@
64#include <linux/delay.h> 64#include <linux/delay.h>
65#include <linux/slab.h> 65#include <linux/slab.h>
66#include <linux/module.h> 66#include <linux/module.h>
67#include <linux/moduleparam.h>
68#include <linux/interrupt.h> 67#include <linux/interrupt.h>
69#include <linux/init.h> 68#include <linux/init.h>
70#include <linux/input.h> 69#include <linux/input.h>
diff --git a/drivers/input/keyboard/pxa27x_keyboard.c b/drivers/input/keyboard/pxa27x_keyboard.c
deleted file mode 100644
index bdd64ee4c5c8..000000000000
--- a/drivers/input/keyboard/pxa27x_keyboard.c
+++ /dev/null
@@ -1,274 +0,0 @@
1/*
2 * linux/drivers/input/keyboard/pxa27x_keyboard.c
3 *
4 * Driver for the pxa27x matrix keyboard controller.
5 *
6 * Created: Feb 22, 2007
7 * Author: Rodolfo Giometti <giometti@linux.it>
8 *
9 * Based on a previous implementations by Kevin O'Connor
10 * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
11 * on some suggestions by Nicolas Pitre <nico@cam.org>.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/input.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
26#include <linux/clk.h>
27#include <linux/err.h>
28
29#include <asm/mach-types.h>
30#include <asm/mach/arch.h>
31#include <asm/mach/map.h>
32
33#include <asm/arch/hardware.h>
34#include <asm/arch/pxa-regs.h>
35#include <asm/arch/irqs.h>
36#include <asm/arch/pxa27x_keyboard.h>
37
38#define DRIVER_NAME "pxa27x-keyboard"
39
40#define KPASMKP(col) (col/2 == 0 ? KPASMKP0 : \
41 col/2 == 1 ? KPASMKP1 : \
42 col/2 == 2 ? KPASMKP2 : KPASMKP3)
43#define KPASMKPx_MKC(row, col) (1 << (row + 16 * (col % 2)))
44
45static struct clk *pxakbd_clk;
46
47static irqreturn_t pxakbd_irq_handler(int irq, void *dev_id)
48{
49 struct platform_device *pdev = dev_id;
50 struct pxa27x_keyboard_platform_data *pdata = pdev->dev.platform_data;
51 struct input_dev *input_dev = platform_get_drvdata(pdev);
52 unsigned long kpc = KPC;
53 int p, row, col, rel;
54
55 if (kpc & KPC_DI) {
56 unsigned long kpdk = KPDK;
57
58 if (!(kpdk & KPDK_DKP)) {
59 /* better luck next time */
60 } else if (kpc & KPC_REE0) {
61 unsigned long kprec = KPREC;
62 KPREC = 0x7f;
63
64 if (kprec & KPREC_OF0)
65 rel = (kprec & 0xff) + 0x7f;
66 else if (kprec & KPREC_UF0)
67 rel = (kprec & 0xff) - 0x7f - 0xff;
68 else
69 rel = (kprec & 0xff) - 0x7f;
70
71 if (rel) {
72 input_report_rel(input_dev, REL_WHEEL, rel);
73 input_sync(input_dev);
74 }
75 }
76 }
77
78 if (kpc & KPC_MI) {
79 /* report the status of every button */
80 for (row = 0; row < pdata->nr_rows; row++) {
81 for (col = 0; col < pdata->nr_cols; col++) {
82 p = KPASMKP(col) & KPASMKPx_MKC(row, col) ?
83 1 : 0;
84 pr_debug("keycode %x - pressed %x\n",
85 pdata->keycodes[row][col], p);
86 input_report_key(input_dev,
87 pdata->keycodes[row][col], p);
88 }
89 }
90 input_sync(input_dev);
91 }
92
93 return IRQ_HANDLED;
94}
95
96static int pxakbd_open(struct input_dev *dev)
97{
98 /* Set keypad control register */
99 KPC |= (KPC_ASACT |
100 KPC_MS_ALL |
101 (2 << 6) | KPC_REE0 | KPC_DK_DEB_SEL |
102 KPC_ME | KPC_MIE | KPC_DE | KPC_DIE);
103
104 KPC &= ~KPC_AS; /* disable automatic scan */
105 KPC &= ~KPC_IMKP; /* do not ignore multiple keypresses */
106
107 /* Set rotary count to mid-point value */
108 KPREC = 0x7F;
109
110 /* Enable unit clock */
111 clk_enable(pxakbd_clk);
112
113 return 0;
114}
115
116static void pxakbd_close(struct input_dev *dev)
117{
118 /* Disable clock unit */
119 clk_disable(pxakbd_clk);
120}
121
122#ifdef CONFIG_PM
123static int pxakbd_suspend(struct platform_device *pdev, pm_message_t state)
124{
125 struct pxa27x_keyboard_platform_data *pdata = pdev->dev.platform_data;
126
127 /* Save controller status */
128 pdata->reg_kpc = KPC;
129 pdata->reg_kprec = KPREC;
130
131 return 0;
132}
133
134static int pxakbd_resume(struct platform_device *pdev)
135{
136 struct pxa27x_keyboard_platform_data *pdata = pdev->dev.platform_data;
137 struct input_dev *input_dev = platform_get_drvdata(pdev);
138
139 mutex_lock(&input_dev->mutex);
140
141 if (input_dev->users) {
142 /* Restore controller status */
143 KPC = pdata->reg_kpc;
144 KPREC = pdata->reg_kprec;
145
146 /* Enable unit clock */
147 clk_disable(pxakbd_clk);
148 clk_enable(pxakbd_clk);
149 }
150
151 mutex_unlock(&input_dev->mutex);
152
153 return 0;
154}
155#else
156#define pxakbd_suspend NULL
157#define pxakbd_resume NULL
158#endif
159
160static int __devinit pxakbd_probe(struct platform_device *pdev)
161{
162 struct pxa27x_keyboard_platform_data *pdata = pdev->dev.platform_data;
163 struct input_dev *input_dev;
164 int i, row, col, error;
165
166 pxakbd_clk = clk_get(&pdev->dev, "KBDCLK");
167 if (IS_ERR(pxakbd_clk)) {
168 error = PTR_ERR(pxakbd_clk);
169 goto err_clk;
170 }
171
172 /* Create and register the input driver. */
173 input_dev = input_allocate_device();
174 if (!input_dev) {
175 printk(KERN_ERR "Cannot request keypad device\n");
176 error = -ENOMEM;
177 goto err_alloc;
178 }
179
180 input_dev->name = DRIVER_NAME;
181 input_dev->id.bustype = BUS_HOST;
182 input_dev->open = pxakbd_open;
183 input_dev->close = pxakbd_close;
184 input_dev->dev.parent = &pdev->dev;
185
186 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
187 BIT_MASK(EV_REL);
188 input_dev->relbit[BIT_WORD(REL_WHEEL)] = BIT_MASK(REL_WHEEL);
189 for (row = 0; row < pdata->nr_rows; row++) {
190 for (col = 0; col < pdata->nr_cols; col++) {
191 int code = pdata->keycodes[row][col];
192 if (code > 0)
193 set_bit(code, input_dev->keybit);
194 }
195 }
196
197 error = request_irq(IRQ_KEYPAD, pxakbd_irq_handler, IRQF_DISABLED,
198 DRIVER_NAME, pdev);
199 if (error) {
200 printk(KERN_ERR "Cannot request keypad IRQ\n");
201 goto err_free_dev;
202 }
203
204 platform_set_drvdata(pdev, input_dev);
205
206 /* Register the input device */
207 error = input_register_device(input_dev);
208 if (error)
209 goto err_free_irq;
210
211 /* Setup GPIOs. */
212 for (i = 0; i < pdata->nr_rows + pdata->nr_cols; i++)
213 pxa_gpio_mode(pdata->gpio_modes[i]);
214
215 /*
216 * Store rows/cols info into keyboard registers.
217 */
218
219 KPC |= (pdata->nr_rows - 1) << 26;
220 KPC |= (pdata->nr_cols - 1) << 23;
221
222 for (col = 0; col < pdata->nr_cols; col++)
223 KPC |= KPC_MS0 << col;
224
225 return 0;
226
227 err_free_irq:
228 platform_set_drvdata(pdev, NULL);
229 free_irq(IRQ_KEYPAD, pdev);
230 err_free_dev:
231 input_free_device(input_dev);
232 err_alloc:
233 clk_put(pxakbd_clk);
234 err_clk:
235 return error;
236}
237
238static int __devexit pxakbd_remove(struct platform_device *pdev)
239{
240 struct input_dev *input_dev = platform_get_drvdata(pdev);
241
242 input_unregister_device(input_dev);
243 free_irq(IRQ_KEYPAD, pdev);
244 clk_put(pxakbd_clk);
245 platform_set_drvdata(pdev, NULL);
246
247 return 0;
248}
249
250static struct platform_driver pxakbd_driver = {
251 .probe = pxakbd_probe,
252 .remove = __devexit_p(pxakbd_remove),
253 .suspend = pxakbd_suspend,
254 .resume = pxakbd_resume,
255 .driver = {
256 .name = DRIVER_NAME,
257 },
258};
259
260static int __init pxakbd_init(void)
261{
262 return platform_driver_register(&pxakbd_driver);
263}
264
265static void __exit pxakbd_exit(void)
266{
267 platform_driver_unregister(&pxakbd_driver);
268}
269
270module_init(pxakbd_init);
271module_exit(pxakbd_exit);
272
273MODULE_DESCRIPTION("PXA27x Matrix Keyboard Driver");
274MODULE_LICENSE("GPL");
diff --git a/drivers/input/keyboard/pxa27x_keypad.c b/drivers/input/keyboard/pxa27x_keypad.c
new file mode 100644
index 000000000000..6224c2fb3b65
--- /dev/null
+++ b/drivers/input/keyboard/pxa27x_keypad.c
@@ -0,0 +1,572 @@
1/*
2 * linux/drivers/input/keyboard/pxa27x_keypad.c
3 *
4 * Driver for the pxa27x matrix keyboard controller.
5 *
6 * Created: Feb 22, 2007
7 * Author: Rodolfo Giometti <giometti@linux.it>
8 *
9 * Based on a previous implementations by Kevin O'Connor
10 * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
11 * on some suggestions by Nicolas Pitre <nico@cam.org>.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/input.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
26#include <linux/clk.h>
27#include <linux/err.h>
28
29#include <asm/mach-types.h>
30#include <asm/mach/arch.h>
31#include <asm/mach/map.h>
32
33#include <asm/arch/hardware.h>
34#include <asm/arch/pxa27x_keypad.h>
35
36/*
37 * Keypad Controller registers
38 */
39#define KPC 0x0000 /* Keypad Control register */
40#define KPDK 0x0008 /* Keypad Direct Key register */
41#define KPREC 0x0010 /* Keypad Rotary Encoder register */
42#define KPMK 0x0018 /* Keypad Matrix Key register */
43#define KPAS 0x0020 /* Keypad Automatic Scan register */
44
45/* Keypad Automatic Scan Multiple Key Presser register 0-3 */
46#define KPASMKP0 0x0028
47#define KPASMKP1 0x0030
48#define KPASMKP2 0x0038
49#define KPASMKP3 0x0040
50#define KPKDI 0x0048
51
52/* bit definitions */
53#define KPC_MKRN(n) ((((n) & 0x7) - 1) << 26) /* matrix key row number */
54#define KPC_MKCN(n) ((((n) & 0x7) - 1) << 23) /* matrix key column number */
55#define KPC_DKN(n) ((((n) & 0x7) - 1) << 6) /* direct key number */
56
57#define KPC_AS (0x1 << 30) /* Automatic Scan bit */
58#define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */
59#define KPC_MI (0x1 << 22) /* Matrix interrupt bit */
60#define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */
61
62#define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */
63#define KPC_MS_ALL (0xff << 13)
64
65#define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */
66#define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */
67#define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */
68#define KPC_DI (0x1 << 5) /* Direct key interrupt bit */
69#define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */
70#define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */
71#define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */
72#define KPC_DE (0x1 << 1) /* Direct Keypad Enable */
73#define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */
74
75#define KPDK_DKP (0x1 << 31)
76#define KPDK_DK(n) ((n) & 0xff)
77
78#define KPREC_OF1 (0x1 << 31)
79#define kPREC_UF1 (0x1 << 30)
80#define KPREC_OF0 (0x1 << 15)
81#define KPREC_UF0 (0x1 << 14)
82
83#define KPREC_RECOUNT0(n) ((n) & 0xff)
84#define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff)
85
86#define KPMK_MKP (0x1 << 31)
87#define KPAS_SO (0x1 << 31)
88#define KPASMKPx_SO (0x1 << 31)
89
90#define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
91#define KPAS_RP(n) (((n) >> 4) & 0xf)
92#define KPAS_CP(n) ((n) & 0xf)
93
94#define KPASMKP_MKC_MASK (0xff)
95
96#define keypad_readl(off) __raw_readl(keypad->mmio_base + (off))
97#define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off))
98
99#define MAX_MATRIX_KEY_NUM (8 * 8)
100
101struct pxa27x_keypad {
102 struct pxa27x_keypad_platform_data *pdata;
103
104 struct clk *clk;
105 struct input_dev *input_dev;
106 void __iomem *mmio_base;
107
108 /* matrix key code map */
109 unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
110
111 /* state row bits of each column scan */
112 uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
113 uint32_t direct_key_state;
114
115 unsigned int direct_key_mask;
116
117 int rotary_rel_code[2];
118 int rotary_up_key[2];
119 int rotary_down_key[2];
120};
121
122static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
123{
124 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
125 struct input_dev *input_dev = keypad->input_dev;
126 unsigned int *key;
127 int i;
128
129 key = &pdata->matrix_key_map[0];
130 for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
131 int row = ((*key) >> 28) & 0xf;
132 int col = ((*key) >> 24) & 0xf;
133 int code = (*key) & 0xffffff;
134
135 keypad->matrix_keycodes[(row << 3) + col] = code;
136 set_bit(code, input_dev->keybit);
137 }
138
139 keypad->rotary_up_key[0] = pdata->rotary0_up_key;
140 keypad->rotary_up_key[1] = pdata->rotary1_up_key;
141 keypad->rotary_down_key[0] = pdata->rotary0_down_key;
142 keypad->rotary_down_key[1] = pdata->rotary1_down_key;
143 keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
144 keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
145
146 if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
147 set_bit(pdata->rotary0_up_key, input_dev->keybit);
148 set_bit(pdata->rotary0_down_key, input_dev->keybit);
149 } else
150 set_bit(pdata->rotary0_rel_code, input_dev->relbit);
151
152 if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
153 set_bit(pdata->rotary1_up_key, input_dev->keybit);
154 set_bit(pdata->rotary1_down_key, input_dev->keybit);
155 } else
156 set_bit(pdata->rotary1_rel_code, input_dev->relbit);
157}
158
159static inline unsigned int lookup_matrix_keycode(
160 struct pxa27x_keypad *keypad, int row, int col)
161{
162 return keypad->matrix_keycodes[(row << 3) + col];
163}
164
165static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
166{
167 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
168 int row, col, num_keys_pressed = 0;
169 uint32_t new_state[MAX_MATRIX_KEY_COLS];
170 uint32_t kpas = keypad_readl(KPAS);
171
172 num_keys_pressed = KPAS_MUKP(kpas);
173
174 memset(new_state, 0, sizeof(new_state));
175
176 if (num_keys_pressed == 0)
177 goto scan;
178
179 if (num_keys_pressed == 1) {
180 col = KPAS_CP(kpas);
181 row = KPAS_RP(kpas);
182
183 /* if invalid row/col, treat as no key pressed */
184 if (col >= pdata->matrix_key_cols ||
185 row >= pdata->matrix_key_rows)
186 goto scan;
187
188 new_state[col] = (1 << row);
189 goto scan;
190 }
191
192 if (num_keys_pressed > 1) {
193 uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
194 uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
195 uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
196 uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
197
198 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
199 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
200 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
201 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
202 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
203 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
204 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
205 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
206 }
207scan:
208 for (col = 0; col < pdata->matrix_key_cols; col++) {
209 uint32_t bits_changed;
210
211 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
212 if (bits_changed == 0)
213 continue;
214
215 for (row = 0; row < pdata->matrix_key_rows; row++) {
216 if ((bits_changed & (1 << row)) == 0)
217 continue;
218
219 input_report_key(keypad->input_dev,
220 lookup_matrix_keycode(keypad, row, col),
221 new_state[col] & (1 << row));
222 }
223 }
224 input_sync(keypad->input_dev);
225 memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
226}
227
228#define DEFAULT_KPREC (0x007f007f)
229
230static inline int rotary_delta(uint32_t kprec)
231{
232 if (kprec & KPREC_OF0)
233 return (kprec & 0xff) + 0x7f;
234 else if (kprec & KPREC_UF0)
235 return (kprec & 0xff) - 0x7f - 0xff;
236 else
237 return (kprec & 0xff) - 0x7f;
238}
239
240static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
241{
242 struct input_dev *dev = keypad->input_dev;
243
244 if (delta == 0)
245 return;
246
247 if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
248 int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
249 keypad->rotary_down_key[r];
250
251 /* simulate a press-n-release */
252 input_report_key(dev, keycode, 1);
253 input_sync(dev);
254 input_report_key(dev, keycode, 0);
255 input_sync(dev);
256 } else {
257 input_report_rel(dev, keypad->rotary_rel_code[r], delta);
258 input_sync(dev);
259 }
260}
261
262static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
263{
264 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
265 uint32_t kprec;
266
267 /* read and reset to default count value */
268 kprec = keypad_readl(KPREC);
269 keypad_writel(KPREC, DEFAULT_KPREC);
270
271 if (pdata->enable_rotary0)
272 report_rotary_event(keypad, 0, rotary_delta(kprec));
273
274 if (pdata->enable_rotary1)
275 report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
276}
277
278static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
279{
280 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
281 unsigned int new_state;
282 uint32_t kpdk, bits_changed;
283 int i;
284
285 kpdk = keypad_readl(KPDK);
286
287 if (pdata->enable_rotary0 || pdata->enable_rotary1)
288 pxa27x_keypad_scan_rotary(keypad);
289
290 if (pdata->direct_key_map == NULL)
291 return;
292
293 new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
294 bits_changed = keypad->direct_key_state ^ new_state;
295
296 if (bits_changed == 0)
297 return;
298
299 for (i = 0; i < pdata->direct_key_num; i++) {
300 if (bits_changed & (1 << i))
301 input_report_key(keypad->input_dev,
302 pdata->direct_key_map[i],
303 (new_state & (1 << i)));
304 }
305 input_sync(keypad->input_dev);
306 keypad->direct_key_state = new_state;
307}
308
309static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
310{
311 struct pxa27x_keypad *keypad = dev_id;
312 unsigned long kpc = keypad_readl(KPC);
313
314 if (kpc & KPC_DI)
315 pxa27x_keypad_scan_direct(keypad);
316
317 if (kpc & KPC_MI)
318 pxa27x_keypad_scan_matrix(keypad);
319
320 return IRQ_HANDLED;
321}
322
323static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
324{
325 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
326 unsigned int mask = 0, direct_key_num = 0;
327 unsigned long kpc = 0;
328
329 /* enable matrix keys with automatic scan */
330 if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
331 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
332 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
333 KPC_MKCN(pdata->matrix_key_cols);
334 }
335
336 /* enable rotary key, debounce interval same as direct keys */
337 if (pdata->enable_rotary0) {
338 mask |= 0x03;
339 direct_key_num = 2;
340 kpc |= KPC_REE0;
341 }
342
343 if (pdata->enable_rotary1) {
344 mask |= 0x0c;
345 direct_key_num = 4;
346 kpc |= KPC_REE1;
347 }
348
349 if (pdata->direct_key_num > direct_key_num)
350 direct_key_num = pdata->direct_key_num;
351
352 keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
353
354 /* enable direct key */
355 if (direct_key_num)
356 kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
357
358 keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
359 keypad_writel(KPREC, DEFAULT_KPREC);
360 keypad_writel(KPKDI, pdata->debounce_interval);
361}
362
363static int pxa27x_keypad_open(struct input_dev *dev)
364{
365 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
366
367 /* Enable unit clock */
368 clk_enable(keypad->clk);
369 pxa27x_keypad_config(keypad);
370
371 return 0;
372}
373
374static void pxa27x_keypad_close(struct input_dev *dev)
375{
376 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
377
378 /* Disable clock unit */
379 clk_disable(keypad->clk);
380}
381
382#ifdef CONFIG_PM
383static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
384{
385 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
386
387 clk_disable(keypad->clk);
388 return 0;
389}
390
391static int pxa27x_keypad_resume(struct platform_device *pdev)
392{
393 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
394 struct input_dev *input_dev = keypad->input_dev;
395
396 mutex_lock(&input_dev->mutex);
397
398 if (input_dev->users) {
399 /* Enable unit clock */
400 clk_enable(keypad->clk);
401 pxa27x_keypad_config(keypad);
402 }
403
404 mutex_unlock(&input_dev->mutex);
405
406 return 0;
407}
408#else
409#define pxa27x_keypad_suspend NULL
410#define pxa27x_keypad_resume NULL
411#endif
412
413#define res_size(res) ((res)->end - (res)->start + 1)
414
415static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
416{
417 struct pxa27x_keypad *keypad;
418 struct input_dev *input_dev;
419 struct resource *res;
420 int irq, error;
421
422 keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
423 if (keypad == NULL) {
424 dev_err(&pdev->dev, "failed to allocate driver data\n");
425 return -ENOMEM;
426 }
427
428 keypad->pdata = pdev->dev.platform_data;
429 if (keypad->pdata == NULL) {
430 dev_err(&pdev->dev, "no platform data defined\n");
431 error = -EINVAL;
432 goto failed_free;
433 }
434
435 irq = platform_get_irq(pdev, 0);
436 if (irq < 0) {
437 dev_err(&pdev->dev, "failed to get keypad irq\n");
438 error = -ENXIO;
439 goto failed_free;
440 }
441
442 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
443 if (res == NULL) {
444 dev_err(&pdev->dev, "failed to get I/O memory\n");
445 error = -ENXIO;
446 goto failed_free;
447 }
448
449 res = request_mem_region(res->start, res_size(res), pdev->name);
450 if (res == NULL) {
451 dev_err(&pdev->dev, "failed to request I/O memory\n");
452 error = -EBUSY;
453 goto failed_free;
454 }
455
456 keypad->mmio_base = ioremap(res->start, res_size(res));
457 if (keypad->mmio_base == NULL) {
458 dev_err(&pdev->dev, "failed to remap I/O memory\n");
459 error = -ENXIO;
460 goto failed_free_mem;
461 }
462
463 keypad->clk = clk_get(&pdev->dev, "KBDCLK");
464 if (IS_ERR(keypad->clk)) {
465 dev_err(&pdev->dev, "failed to get keypad clock\n");
466 error = PTR_ERR(keypad->clk);
467 goto failed_free_io;
468 }
469
470 /* Create and register the input driver. */
471 input_dev = input_allocate_device();
472 if (!input_dev) {
473 dev_err(&pdev->dev, "failed to allocate input device\n");
474 error = -ENOMEM;
475 goto failed_put_clk;
476 }
477
478 input_dev->name = pdev->name;
479 input_dev->id.bustype = BUS_HOST;
480 input_dev->open = pxa27x_keypad_open;
481 input_dev->close = pxa27x_keypad_close;
482 input_dev->dev.parent = &pdev->dev;
483
484 keypad->input_dev = input_dev;
485 input_set_drvdata(input_dev, keypad);
486
487 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
488 BIT_MASK(EV_REL);
489
490 pxa27x_keypad_build_keycode(keypad);
491 platform_set_drvdata(pdev, keypad);
492
493 error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED,
494 pdev->name, keypad);
495 if (error) {
496 dev_err(&pdev->dev, "failed to request IRQ\n");
497 goto failed_free_dev;
498 }
499
500 /* Register the input device */
501 error = input_register_device(input_dev);
502 if (error) {
503 dev_err(&pdev->dev, "failed to register input device\n");
504 goto failed_free_irq;
505 }
506
507 return 0;
508
509failed_free_irq:
510 free_irq(irq, pdev);
511 platform_set_drvdata(pdev, NULL);
512failed_free_dev:
513 input_free_device(input_dev);
514failed_put_clk:
515 clk_put(keypad->clk);
516failed_free_io:
517 iounmap(keypad->mmio_base);
518failed_free_mem:
519 release_mem_region(res->start, res_size(res));
520failed_free:
521 kfree(keypad);
522 return error;
523}
524
525static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
526{
527 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
528 struct resource *res;
529
530 free_irq(platform_get_irq(pdev, 0), pdev);
531
532 clk_disable(keypad->clk);
533 clk_put(keypad->clk);
534
535 input_unregister_device(keypad->input_dev);
536 input_free_device(keypad->input_dev);
537
538 iounmap(keypad->mmio_base);
539
540 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
541 release_mem_region(res->start, res_size(res));
542
543 platform_set_drvdata(pdev, NULL);
544 kfree(keypad);
545 return 0;
546}
547
548static struct platform_driver pxa27x_keypad_driver = {
549 .probe = pxa27x_keypad_probe,
550 .remove = __devexit_p(pxa27x_keypad_remove),
551 .suspend = pxa27x_keypad_suspend,
552 .resume = pxa27x_keypad_resume,
553 .driver = {
554 .name = "pxa27x-keypad",
555 },
556};
557
558static int __init pxa27x_keypad_init(void)
559{
560 return platform_driver_register(&pxa27x_keypad_driver);
561}
562
563static void __exit pxa27x_keypad_exit(void)
564{
565 platform_driver_unregister(&pxa27x_keypad_driver);
566}
567
568module_init(pxa27x_keypad_init);
569module_exit(pxa27x_keypad_exit);
570
571MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
572MODULE_LICENSE("GPL");
diff --git a/drivers/input/keyboard/tosakbd.c b/drivers/input/keyboard/tosakbd.c
new file mode 100644
index 000000000000..3884d1e3f070
--- /dev/null
+++ b/drivers/input/keyboard/tosakbd.c
@@ -0,0 +1,415 @@
1/*
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;
55
56 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
136 for (col = 0; col < TOSA_KEY_STROBE_NUM; col++) {
137 /*
138 * Discharge the output driver capacitatance
139 * in the keyboard matrix. (Yes it is significant..)
140 */
141 tosakbd_discharge_all();
142 udelay(KB_DISCHARGE_DELAY);
143
144 tosakbd_activate_col(col);
145 udelay(KB_ACTIVATE_DELAY);
146
147 rowd = GET_ROWS_STATUS(col);
148
149 for (row = 0; row < TOSA_KEY_SENSE_NUM; row++) {
150 unsigned int scancode, pressed;
151 scancode = SCANCODE(row, col);
152 pressed = rowd & KB_ROWMASK(row);
153
154 if (pressed && !tosakbd->keycode[scancode])
155 dev_warn(&dev->dev,
156 "unhandled scancode: 0x%02x\n",
157 scancode);
158
159 input_report_key(tosakbd->input,
160 tosakbd->keycode[scancode],
161 pressed);
162 if (pressed)
163 num_pressed++;
164 }
165
166 tosakbd_reset_col(col);
167 }
168
169 tosakbd_activate_all();
170
171 input_sync(tosakbd->input);
172
173 /* if any keys are pressed, enable the timer */
174 if (num_pressed)
175 mod_timer(&tosakbd->timer, jiffies + SCAN_INTERVAL);
176
177 spin_unlock_irqrestore(&tosakbd->lock, flags);
178}
179
180/*
181 * tosa keyboard interrupt handler.
182 */
183static irqreturn_t tosakbd_interrupt(int irq, void *__dev)
184{
185 struct platform_device *dev = __dev;
186 struct tosakbd *tosakbd = platform_get_drvdata(dev);
187
188 if (!timer_pending(&tosakbd->timer)) {
189 /** wait chattering delay **/
190 udelay(20);
191 tosakbd_scankeyboard(dev);
192 }
193
194 return IRQ_HANDLED;
195}
196
197/*
198 * tosa timer checking for released keys
199 */
200static void tosakbd_timer_callback(unsigned long __dev)
201{
202 struct platform_device *dev = (struct platform_device *)__dev;
203 tosakbd_scankeyboard(dev);
204}
205
206#ifdef CONFIG_PM
207static int tosakbd_suspend(struct platform_device *dev, pm_message_t state)
208{
209 struct tosakbd *tosakbd = platform_get_drvdata(dev);
210
211 del_timer_sync(&tosakbd->timer);
212
213 return 0;
214}
215
216static int tosakbd_resume(struct platform_device *dev)
217{
218 tosakbd_scankeyboard(dev);
219
220 return 0;
221}
222#else
223#define tosakbd_suspend NULL
224#define tosakbd_resume NULL
225#endif
226
227static int __devinit tosakbd_probe(struct platform_device *pdev) {
228
229 int i;
230 struct tosakbd *tosakbd;
231 struct input_dev *input_dev;
232 int error;
233
234 tosakbd = kzalloc(sizeof(struct tosakbd), GFP_KERNEL);
235 if (!tosakbd)
236 return -ENOMEM;
237
238 input_dev = input_allocate_device();
239 if (!input_dev) {
240 kfree(tosakbd);
241 return -ENOMEM;
242 }
243
244 platform_set_drvdata(pdev, tosakbd);
245
246 spin_lock_init(&tosakbd->lock);
247
248 /* Init Keyboard rescan timer */
249 init_timer(&tosakbd->timer);
250 tosakbd->timer.function = tosakbd_timer_callback;
251 tosakbd->timer.data = (unsigned long) pdev;
252
253 tosakbd->input = input_dev;
254
255 input_set_drvdata(input_dev, tosakbd);
256 input_dev->name = "Tosa Keyboard";
257 input_dev->phys = "tosakbd/input0";
258 input_dev->dev.parent = &pdev->dev;
259
260 input_dev->id.bustype = BUS_HOST;
261 input_dev->id.vendor = 0x0001;
262 input_dev->id.product = 0x0001;
263 input_dev->id.version = 0x0100;
264
265 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
266 input_dev->keycode = tosakbd->keycode;
267 input_dev->keycodesize = sizeof(unsigned int);
268 input_dev->keycodemax = ARRAY_SIZE(tosakbd_keycode);
269
270 memcpy(tosakbd->keycode, tosakbd_keycode, sizeof(tosakbd_keycode));
271
272 for (i = 0; i < ARRAY_SIZE(tosakbd_keycode); i++)
273 __set_bit(tosakbd->keycode[i], input_dev->keybit);
274 clear_bit(0, input_dev->keybit);
275
276 /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
277 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
278 int gpio = TOSA_GPIO_KEY_SENSE(i);
279 int irq;
280 error = gpio_request(gpio, "tosakbd");
281 if (error < 0) {
282 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
283 " error %d\n", gpio, error);
284 goto fail;
285 }
286
287 error = gpio_direction_input(TOSA_GPIO_KEY_SENSE(i));
288 if (error < 0) {
289 printk(KERN_ERR "tosakbd: failed to configure input"
290 " direction for GPIO %d, error %d\n",
291 gpio, error);
292 gpio_free(gpio);
293 goto fail;
294 }
295
296 irq = gpio_to_irq(gpio);
297 if (irq < 0) {
298 error = irq;
299 printk(KERN_ERR "gpio-keys: Unable to get irq number"
300 " for GPIO %d, error %d\n",
301 gpio, error);
302 gpio_free(gpio);
303 goto fail;
304 }
305
306 error = request_irq(irq, tosakbd_interrupt,
307 IRQF_DISABLED | IRQF_TRIGGER_RISING,
308 "tosakbd", pdev);
309
310 if (error) {
311 printk("tosakbd: Can't get IRQ: %d: error %d!\n",
312 irq, error);
313 gpio_free(gpio);
314 goto fail;
315 }
316 }
317
318 /* Set Strobe lines as outputs - set high */
319 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++) {
320 int gpio = TOSA_GPIO_KEY_STROBE(i);
321 error = gpio_request(gpio, "tosakbd");
322 if (error < 0) {
323 printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
324 " error %d\n", gpio, error);
325 goto fail2;
326 }
327
328 error = gpio_direction_output(gpio, 1);
329 if (error < 0) {
330 printk(KERN_ERR "tosakbd: failed to configure input"
331 " direction for GPIO %d, error %d\n",
332 gpio, error);
333 gpio_free(gpio);
334 goto fail;
335 }
336
337 }
338
339 error = input_register_device(input_dev);
340 if (error) {
341 printk(KERN_ERR "tosakbd: Unable to register input device, "
342 "error: %d\n", error);
343 goto fail;
344 }
345
346 printk(KERN_INFO "input: Tosa Keyboard Registered\n");
347
348 return 0;
349
350fail2:
351 while (--i >= 0)
352 gpio_free(TOSA_GPIO_KEY_STROBE(i));
353
354 i = TOSA_KEY_SENSE_NUM;
355fail:
356 while (--i >= 0) {
357 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), pdev);
358 gpio_free(TOSA_GPIO_KEY_SENSE(i));
359 }
360
361 platform_set_drvdata(pdev, NULL);
362 input_free_device(input_dev);
363 kfree(tosakbd);
364
365 return error;
366}
367
368static int __devexit tosakbd_remove(struct platform_device *dev) {
369
370 int i;
371 struct tosakbd *tosakbd = platform_get_drvdata(dev);
372
373 for (i = 0; i < TOSA_KEY_STROBE_NUM; i++)
374 gpio_free(TOSA_GPIO_KEY_STROBE(i));
375
376 for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
377 free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), dev);
378 gpio_free(TOSA_GPIO_KEY_SENSE(i));
379 }
380
381 del_timer_sync(&tosakbd->timer);
382
383 input_unregister_device(tosakbd->input);
384
385 kfree(tosakbd);
386
387 return 0;
388}
389
390static struct platform_driver tosakbd_driver = {
391 .probe = tosakbd_probe,
392 .remove = __devexit_p(tosakbd_remove),
393 .suspend = tosakbd_suspend,
394 .resume = tosakbd_resume,
395 .driver = {
396 .name = "tosa-keyboard",
397 },
398};
399
400static int __devinit tosakbd_init(void)
401{
402 return platform_driver_register(&tosakbd_driver);
403}
404
405static void __exit tosakbd_exit(void)
406{
407 platform_driver_unregister(&tosakbd_driver);
408}
409
410module_init(tosakbd_init);
411module_exit(tosakbd_exit);
412
413MODULE_AUTHOR("Dirk Opfer <Dirk@Opfer-Online.de>");
414MODULE_DESCRIPTION("Tosa Keyboard Driver");
415MODULE_LICENSE("GPL v2");