aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/keyboard/omap4-keypad.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/keyboard/omap4-keypad.c')
-rw-r--r--drivers/input/keyboard/omap4-keypad.c287
1 files changed, 287 insertions, 0 deletions
diff --git a/drivers/input/keyboard/omap4-keypad.c b/drivers/input/keyboard/omap4-keypad.c
new file mode 100644
index 000000000000..975f8bbcc69b
--- /dev/null
+++ b/drivers/input/keyboard/omap4-keypad.c
@@ -0,0 +1,287 @@
1/*
2 * OMAP4 Keypad Driver
3 *
4 * Copyright (C) 2010 Texas Instruments
5 *
6 * Author: Abraham Arce <x0066660@ti.com>
7 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/platform_device.h>
28#include <linux/errno.h>
29#include <linux/io.h>
30#include <linux/input.h>
31#include <linux/slab.h>
32
33#include <plat/omap4-keypad.h>
34
35/* OMAP4 registers */
36#define OMAP4_KBD_REVISION 0x00
37#define OMAP4_KBD_SYSCONFIG 0x10
38#define OMAP4_KBD_SYSSTATUS 0x14
39#define OMAP4_KBD_IRQSTATUS 0x18
40#define OMAP4_KBD_IRQENABLE 0x1C
41#define OMAP4_KBD_WAKEUPENABLE 0x20
42#define OMAP4_KBD_PENDING 0x24
43#define OMAP4_KBD_CTRL 0x28
44#define OMAP4_KBD_DEBOUNCINGTIME 0x2C
45#define OMAP4_KBD_LONGKEYTIME 0x30
46#define OMAP4_KBD_TIMEOUT 0x34
47#define OMAP4_KBD_STATEMACHINE 0x38
48#define OMAP4_KBD_ROWINPUTS 0x3C
49#define OMAP4_KBD_COLUMNOUTPUTS 0x40
50#define OMAP4_KBD_FULLCODE31_0 0x44
51#define OMAP4_KBD_FULLCODE63_32 0x48
52
53/* OMAP4 bit definitions */
54#define OMAP4_DEF_SYSCONFIG_SOFTRST (1 << 1)
55#define OMAP4_DEF_SYSCONFIG_ENAWKUP (1 << 2)
56#define OMAP4_DEF_IRQENABLE_EVENTEN (1 << 0)
57#define OMAP4_DEF_IRQENABLE_LONGKEY (1 << 1)
58#define OMAP4_DEF_IRQENABLE_TIMEOUTEN (1 << 2)
59#define OMAP4_DEF_CTRL_NOSOFTMODE (1 << 1)
60#define OMAP4_DEF_CTRLPTVVALUE (1 << 2)
61#define OMAP4_DEF_CTRLPTV (1 << 1)
62#define OMAP4_DEF_IRQDISABLE 0x00
63
64/* OMAP4 values */
65#define OMAP4_VAL_DEBOUNCINGTIME 0x07
66#define OMAP4_VAL_FUNCTIONALCFG 0x1E
67
68#define OMAP4_MASK_IRQSTATUSDISABLE 0xFFFF
69
70struct omap4_keypad {
71 struct input_dev *input;
72
73 void __iomem *base;
74 int irq;
75
76 unsigned int rows;
77 unsigned int cols;
78 unsigned int row_shift;
79 unsigned char key_state[8];
80 unsigned short keymap[];
81};
82
83static void __devinit omap4_keypad_config(struct omap4_keypad *keypad_data)
84{
85 __raw_writel(OMAP4_DEF_SYSCONFIG_SOFTRST | OMAP4_DEF_SYSCONFIG_ENAWKUP,
86 keypad_data->base + OMAP4_KBD_SYSCONFIG);
87 __raw_writel(OMAP4_VAL_FUNCTIONALCFG,
88 keypad_data->base + OMAP4_KBD_CTRL);
89 __raw_writel(OMAP4_VAL_DEBOUNCINGTIME,
90 keypad_data->base + OMAP4_KBD_DEBOUNCINGTIME);
91 __raw_writel(OMAP4_DEF_IRQDISABLE,
92 keypad_data->base + OMAP4_KBD_IRQSTATUS);
93 __raw_writel(OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY,
94 keypad_data->base + OMAP4_KBD_IRQENABLE);
95}
96
97/* Interrupt handler */
98static irqreturn_t omap4_keypad_interrupt(int irq, void *dev_id)
99{
100 struct omap4_keypad *keypad_data = dev_id;
101 struct input_dev *input_dev = keypad_data->input;
102 unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
103 unsigned int col, row, code, changed;
104 u32 *new_state = (u32 *) key_state;
105
106 /* Disable interrupts */
107 __raw_writel(OMAP4_DEF_IRQDISABLE,
108 keypad_data->base + OMAP4_KBD_IRQENABLE);
109
110 *new_state = __raw_readl(keypad_data->base + OMAP4_KBD_FULLCODE31_0);
111 *(new_state + 1) = __raw_readl(keypad_data->base + OMAP4_KBD_FULLCODE63_32);
112
113 for (row = 0; row < keypad_data->rows; row++) {
114 changed = key_state[row] ^ keypad_data->key_state[row];
115 if (!changed)
116 continue;
117
118 for (col = 0; col < keypad_data->cols; col++) {
119 if (changed & (1 << col)) {
120 code = MATRIX_SCAN_CODE(row, col,
121 keypad_data->row_shift);
122 input_event(input_dev, EV_MSC, MSC_SCAN, code);
123 input_report_key(input_dev,
124 keypad_data->keymap[code],
125 key_state[row] & (1 << col));
126 }
127 }
128 }
129
130 input_sync(input_dev);
131
132 memcpy(keypad_data->key_state, key_state,
133 sizeof(keypad_data->key_state));
134
135 /* clear pending interrupts */
136 __raw_writel(__raw_readl(keypad_data->base + OMAP4_KBD_IRQSTATUS),
137 keypad_data->base + OMAP4_KBD_IRQSTATUS);
138
139 /* enable interrupts */
140 __raw_writel(OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY,
141 keypad_data->base + OMAP4_KBD_IRQENABLE);
142
143 return IRQ_HANDLED;
144}
145
146static int __devinit omap4_keypad_probe(struct platform_device *pdev)
147{
148 const struct omap4_keypad_platform_data *pdata;
149 struct omap4_keypad *keypad_data;
150 struct input_dev *input_dev;
151 unsigned int row_shift, max_keys;
152 int error;
153
154 /* platform data */
155 pdata = pdev->dev.platform_data;
156 if (!pdata) {
157 dev_err(&pdev->dev, "no platform data defined\n");
158 return -EINVAL;
159 }
160
161 if (!pdata->base) {
162 dev_err(&pdev->dev, "no base address specified\n");
163 return -EINVAL;
164 }
165
166 if (!pdata->irq) {
167 dev_err(&pdev->dev, "no keyboard irq assigned\n");
168 return -EINVAL;
169 }
170
171 if (!pdata->keymap_data) {
172 dev_err(&pdev->dev, "no keymap data defined\n");
173 return -EINVAL;
174 }
175
176 row_shift = get_count_order(pdata->cols);
177 max_keys = pdata->rows << row_shift;
178
179 keypad_data = kzalloc(sizeof(struct omap4_keypad) +
180 max_keys * sizeof(keypad_data->keymap[0]),
181 GFP_KERNEL);
182 if (!keypad_data) {
183 dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
184 return -ENOMEM;
185 }
186
187 keypad_data->base = pdata->base;
188 keypad_data->irq = pdata->irq;
189
190 keypad_data->row_shift = row_shift;
191 keypad_data->rows = pdata->rows;
192 keypad_data->cols = pdata->cols;
193
194 /* input device allocation */
195 keypad_data->input = input_dev = input_allocate_device();
196 if (!input_dev) {
197 error = -ENOMEM;
198 goto err_free_keypad;
199 }
200
201 input_dev->name = pdev->name;
202 input_dev->dev.parent = &pdev->dev;
203 input_dev->id.bustype = BUS_HOST;
204 input_dev->id.vendor = 0x0001;
205 input_dev->id.product = 0x0001;
206 input_dev->id.version = 0x0001;
207
208 input_dev->keycode = keypad_data->keymap;
209 input_dev->keycodesize = sizeof(keypad_data->keymap[0]);
210 input_dev->keycodemax = max_keys;
211
212 __set_bit(EV_KEY, input_dev->evbit);
213 __set_bit(EV_REP, input_dev->evbit);
214
215 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
216
217 input_set_drvdata(input_dev, keypad_data);
218
219 matrix_keypad_build_keymap(pdata->keymap_data, row_shift,
220 input_dev->keycode, input_dev->keybit);
221
222 omap4_keypad_config(keypad_data);
223
224 error = request_irq(keypad_data->irq, omap4_keypad_interrupt,
225 IRQF_TRIGGER_FALLING,
226 "omap4-keypad", keypad_data);
227 if (error) {
228 dev_err(&pdev->dev, "failed to register interrupt\n");
229 goto err_free_input;
230 }
231
232 error = input_register_device(keypad_data->input);
233 if (error < 0) {
234 dev_err(&pdev->dev, "failed to register input device\n");
235 goto err_free_irq;
236 }
237
238
239 platform_set_drvdata(pdev, keypad_data);
240 return 0;
241
242err_free_irq:
243 free_irq(keypad_data->irq, keypad_data);
244err_free_input:
245 input_free_device(input_dev);
246err_free_keypad:
247 kfree(keypad_data);
248 return error;
249}
250
251static int __devexit omap4_keypad_remove(struct platform_device *pdev)
252{
253 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
254
255 free_irq(keypad_data->irq, keypad_data);
256 input_unregister_device(keypad_data->input);
257 kfree(keypad_data);
258 platform_set_drvdata(pdev, NULL);
259
260 return 0;
261}
262
263static struct platform_driver omap4_keypad_driver = {
264 .probe = omap4_keypad_probe,
265 .remove = __devexit_p(omap4_keypad_remove),
266 .driver = {
267 .name = "omap4-keypad",
268 .owner = THIS_MODULE,
269 },
270};
271
272static int __init omap4_keypad_init(void)
273{
274 return platform_driver_register(&omap4_keypad_driver);
275}
276module_init(omap4_keypad_init);
277
278static void __exit omap4_keypad_exit(void)
279{
280 platform_driver_unregister(&omap4_keypad_driver);
281}
282module_exit(omap4_keypad_exit);
283
284MODULE_AUTHOR("Texas Instruments");
285MODULE_DESCRIPTION("OMAP4 Keypad Driver");
286MODULE_LICENSE("GPL");
287MODULE_ALIAS("platform:omap4-keypad");