]>
Commit | Line | Data |
---|---|---|
a17f7955 AA |
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> | |
a17f7955 AA |
25 | #include <linux/interrupt.h> |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/io.h> | |
13987435 | 29 | #include <linux/of.h> |
a17f7955 | 30 | #include <linux/input.h> |
61721c88 | 31 | #include <linux/input/matrix_keypad.h> |
a17f7955 | 32 | #include <linux/slab.h> |
5ad567ff | 33 | #include <linux/pm_runtime.h> |
a17f7955 | 34 | |
a17f7955 AA |
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 */ | |
875ad696 IS |
54 | #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0) |
55 | #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1) | |
56 | #define OMAP4_DEF_WUP_EVENT_ENA BIT(0) | |
57 | #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1) | |
58 | #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1) | |
59 | #define OMAP4_DEF_CTRL_PTV_SHIFT 2 | |
a17f7955 AA |
60 | |
61 | /* OMAP4 values */ | |
875ad696 IS |
62 | #define OMAP4_VAL_IRQDISABLE 0x0 |
63 | #define OMAP4_VAL_DEBOUNCINGTIME 0x7 | |
64 | #define OMAP4_VAL_PVT 0x7 | |
a17f7955 | 65 | |
f77621cc PS |
66 | enum { |
67 | KBD_REVISION_OMAP4 = 0, | |
68 | KBD_REVISION_OMAP5, | |
69 | }; | |
70 | ||
a17f7955 AA |
71 | struct omap4_keypad { |
72 | struct input_dev *input; | |
73 | ||
74 | void __iomem *base; | |
78608a0d | 75 | bool irq_wake_enabled; |
f77621cc | 76 | unsigned int irq; |
a17f7955 AA |
77 | |
78 | unsigned int rows; | |
79 | unsigned int cols; | |
f77621cc PS |
80 | u32 reg_offset; |
81 | u32 irqreg_offset; | |
a17f7955 | 82 | unsigned int row_shift; |
13987435 | 83 | bool no_autorepeat; |
a17f7955 | 84 | unsigned char key_state[8]; |
13987435 | 85 | unsigned short *keymap; |
a17f7955 AA |
86 | }; |
87 | ||
f77621cc PS |
88 | static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset) |
89 | { | |
90 | return __raw_readl(keypad_data->base + | |
91 | keypad_data->reg_offset + offset); | |
92 | } | |
93 | ||
94 | static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value) | |
95 | { | |
96 | __raw_writel(value, | |
97 | keypad_data->base + keypad_data->reg_offset + offset); | |
98 | } | |
99 | ||
100 | static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset) | |
101 | { | |
102 | return __raw_readl(keypad_data->base + | |
103 | keypad_data->irqreg_offset + offset); | |
104 | } | |
105 | ||
106 | static void kbd_write_irqreg(struct omap4_keypad *keypad_data, | |
107 | u32 offset, u32 value) | |
108 | { | |
109 | __raw_writel(value, | |
110 | keypad_data->base + keypad_data->irqreg_offset + offset); | |
111 | } | |
112 | ||
113 | ||
c683da3e IS |
114 | /* Interrupt handlers */ |
115 | static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id) | |
116 | { | |
117 | struct omap4_keypad *keypad_data = dev_id; | |
118 | ||
119 | if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)) { | |
120 | /* Disable interrupts */ | |
121 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, | |
122 | OMAP4_VAL_IRQDISABLE); | |
123 | return IRQ_WAKE_THREAD; | |
124 | } | |
125 | ||
126 | return IRQ_NONE; | |
127 | } | |
128 | ||
129 | static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id) | |
a17f7955 AA |
130 | { |
131 | struct omap4_keypad *keypad_data = dev_id; | |
132 | struct input_dev *input_dev = keypad_data->input; | |
133 | unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)]; | |
134 | unsigned int col, row, code, changed; | |
135 | u32 *new_state = (u32 *) key_state; | |
136 | ||
f77621cc PS |
137 | *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0); |
138 | *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32); | |
a17f7955 AA |
139 | |
140 | for (row = 0; row < keypad_data->rows; row++) { | |
141 | changed = key_state[row] ^ keypad_data->key_state[row]; | |
142 | if (!changed) | |
143 | continue; | |
144 | ||
145 | for (col = 0; col < keypad_data->cols; col++) { | |
146 | if (changed & (1 << col)) { | |
147 | code = MATRIX_SCAN_CODE(row, col, | |
148 | keypad_data->row_shift); | |
149 | input_event(input_dev, EV_MSC, MSC_SCAN, code); | |
150 | input_report_key(input_dev, | |
151 | keypad_data->keymap[code], | |
152 | key_state[row] & (1 << col)); | |
153 | } | |
154 | } | |
155 | } | |
156 | ||
157 | input_sync(input_dev); | |
158 | ||
159 | memcpy(keypad_data->key_state, key_state, | |
160 | sizeof(keypad_data->key_state)); | |
161 | ||
162 | /* clear pending interrupts */ | |
f77621cc PS |
163 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, |
164 | kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); | |
a17f7955 AA |
165 | |
166 | /* enable interrupts */ | |
f77621cc PS |
167 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
168 | OMAP4_DEF_IRQENABLE_EVENTEN | | |
169 | OMAP4_DEF_IRQENABLE_LONGKEY); | |
a17f7955 AA |
170 | |
171 | return IRQ_HANDLED; | |
172 | } | |
173 | ||
5ad567ff AA |
174 | static int omap4_keypad_open(struct input_dev *input) |
175 | { | |
176 | struct omap4_keypad *keypad_data = input_get_drvdata(input); | |
177 | ||
178 | pm_runtime_get_sync(input->dev.parent); | |
179 | ||
180 | disable_irq(keypad_data->irq); | |
181 | ||
f77621cc | 182 | kbd_writel(keypad_data, OMAP4_KBD_CTRL, |
875ad696 IS |
183 | OMAP4_DEF_CTRL_NOSOFTMODE | |
184 | (OMAP4_VAL_PVT << OMAP4_DEF_CTRL_PTV_SHIFT)); | |
f77621cc PS |
185 | kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME, |
186 | OMAP4_VAL_DEBOUNCINGTIME); | |
afbac60b | 187 | /* clear pending interrupts */ |
f77621cc | 188 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, |
afbac60b | 189 | kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); |
f77621cc PS |
190 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
191 | OMAP4_DEF_IRQENABLE_EVENTEN | | |
192 | OMAP4_DEF_IRQENABLE_LONGKEY); | |
193 | kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, | |
194 | OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA); | |
5ad567ff AA |
195 | |
196 | enable_irq(keypad_data->irq); | |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
201 | static void omap4_keypad_close(struct input_dev *input) | |
202 | { | |
203 | struct omap4_keypad *keypad_data = input_get_drvdata(input); | |
204 | ||
205 | disable_irq(keypad_data->irq); | |
206 | ||
207 | /* Disable interrupts */ | |
f77621cc PS |
208 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, |
209 | OMAP4_VAL_IRQDISABLE); | |
5ad567ff AA |
210 | |
211 | /* clear pending interrupts */ | |
f77621cc PS |
212 | kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, |
213 | kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); | |
5ad567ff AA |
214 | |
215 | enable_irq(keypad_data->irq); | |
216 | ||
217 | pm_runtime_put_sync(input->dev.parent); | |
218 | } | |
219 | ||
5298cc4c BP |
220 | static int omap4_keypad_parse_dt(struct device *dev, |
221 | struct omap4_keypad *keypad_data) | |
13987435 SP |
222 | { |
223 | struct device_node *np = dev->of_node; | |
43840415 | 224 | int err; |
13987435 | 225 | |
aef01aad DT |
226 | err = matrix_keypad_parse_properties(dev, &keypad_data->rows, |
227 | &keypad_data->cols); | |
43840415 SG |
228 | if (err) |
229 | return err; | |
13987435 SP |
230 | |
231 | if (of_get_property(np, "linux,input-no-autorepeat", NULL)) | |
232 | keypad_data->no_autorepeat = true; | |
233 | ||
234 | return 0; | |
235 | } | |
13987435 | 236 | |
5298cc4c | 237 | static int omap4_keypad_probe(struct platform_device *pdev) |
a17f7955 | 238 | { |
a17f7955 AA |
239 | struct omap4_keypad *keypad_data; |
240 | struct input_dev *input_dev; | |
f3a1ba60 | 241 | struct resource *res; |
13987435 | 242 | unsigned int max_keys; |
f77621cc | 243 | int rev; |
f3a1ba60 | 244 | int irq; |
a17f7955 AA |
245 | int error; |
246 | ||
f3a1ba60 AA |
247 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
248 | if (!res) { | |
a17f7955 AA |
249 | dev_err(&pdev->dev, "no base address specified\n"); |
250 | return -EINVAL; | |
251 | } | |
252 | ||
f3a1ba60 AA |
253 | irq = platform_get_irq(pdev, 0); |
254 | if (!irq) { | |
a17f7955 AA |
255 | dev_err(&pdev->dev, "no keyboard irq assigned\n"); |
256 | return -EINVAL; | |
257 | } | |
258 | ||
13987435 | 259 | keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL); |
a17f7955 AA |
260 | if (!keypad_data) { |
261 | dev_err(&pdev->dev, "keypad_data memory allocation failed\n"); | |
262 | return -ENOMEM; | |
263 | } | |
264 | ||
13987435 SP |
265 | keypad_data->irq = irq; |
266 | ||
61721c88 JE |
267 | error = omap4_keypad_parse_dt(&pdev->dev, keypad_data); |
268 | if (error) | |
b83b1459 | 269 | goto err_free_keypad; |
f3a1ba60 | 270 | |
13987435 | 271 | res = request_mem_region(res->start, resource_size(res), pdev->name); |
f3a1ba60 AA |
272 | if (!res) { |
273 | dev_err(&pdev->dev, "can't request mem region\n"); | |
274 | error = -EBUSY; | |
275 | goto err_free_keypad; | |
276 | } | |
a17f7955 | 277 | |
f3a1ba60 AA |
278 | keypad_data->base = ioremap(res->start, resource_size(res)); |
279 | if (!keypad_data->base) { | |
280 | dev_err(&pdev->dev, "can't ioremap mem resource\n"); | |
281 | error = -ENOMEM; | |
282 | goto err_release_mem; | |
283 | } | |
284 | ||
a17f7955 | 285 | |
f77621cc | 286 | /* |
13987435 SP |
287 | * Enable clocks for the keypad module so that we can read |
288 | * revision register. | |
289 | */ | |
f77621cc PS |
290 | pm_runtime_enable(&pdev->dev); |
291 | error = pm_runtime_get_sync(&pdev->dev); | |
292 | if (error) { | |
293 | dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n"); | |
294 | goto err_unmap; | |
295 | } | |
296 | rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION); | |
297 | rev &= 0x03 << 30; | |
298 | rev >>= 30; | |
299 | switch (rev) { | |
300 | case KBD_REVISION_OMAP4: | |
301 | keypad_data->reg_offset = 0x00; | |
302 | keypad_data->irqreg_offset = 0x00; | |
303 | break; | |
304 | case KBD_REVISION_OMAP5: | |
305 | keypad_data->reg_offset = 0x10; | |
306 | keypad_data->irqreg_offset = 0x0c; | |
307 | break; | |
308 | default: | |
309 | dev_err(&pdev->dev, | |
310 | "Keypad reports unsupported revision %d", rev); | |
311 | error = -EINVAL; | |
312 | goto err_pm_put_sync; | |
313 | } | |
314 | ||
a17f7955 AA |
315 | /* input device allocation */ |
316 | keypad_data->input = input_dev = input_allocate_device(); | |
317 | if (!input_dev) { | |
318 | error = -ENOMEM; | |
f77621cc | 319 | goto err_pm_put_sync; |
a17f7955 AA |
320 | } |
321 | ||
322 | input_dev->name = pdev->name; | |
323 | input_dev->dev.parent = &pdev->dev; | |
324 | input_dev->id.bustype = BUS_HOST; | |
325 | input_dev->id.vendor = 0x0001; | |
326 | input_dev->id.product = 0x0001; | |
327 | input_dev->id.version = 0x0001; | |
328 | ||
5ad567ff AA |
329 | input_dev->open = omap4_keypad_open; |
330 | input_dev->close = omap4_keypad_close; | |
331 | ||
13987435 SP |
332 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
333 | if (!keypad_data->no_autorepeat) | |
334 | __set_bit(EV_REP, input_dev->evbit); | |
335 | ||
336 | input_set_drvdata(input_dev, keypad_data); | |
337 | ||
338 | keypad_data->row_shift = get_count_order(keypad_data->cols); | |
339 | max_keys = keypad_data->rows << keypad_data->row_shift; | |
340 | keypad_data->keymap = kzalloc(max_keys * sizeof(keypad_data->keymap[0]), | |
341 | GFP_KERNEL); | |
342 | if (!keypad_data->keymap) { | |
343 | dev_err(&pdev->dev, "Not enough memory for keymap\n"); | |
344 | error = -ENOMEM; | |
345 | goto err_free_input; | |
346 | } | |
347 | ||
61721c88 | 348 | error = matrix_keypad_build_keymap(NULL, NULL, |
13987435 | 349 | keypad_data->rows, keypad_data->cols, |
1932811f DT |
350 | keypad_data->keymap, input_dev); |
351 | if (error) { | |
352 | dev_err(&pdev->dev, "failed to build keymap\n"); | |
13987435 | 353 | goto err_free_keymap; |
1932811f | 354 | } |
a17f7955 | 355 | |
c683da3e | 356 | error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler, |
50f3c163 | 357 | omap4_keypad_irq_thread_fn, 0, |
c683da3e | 358 | "omap4-keypad", keypad_data); |
a17f7955 AA |
359 | if (error) { |
360 | dev_err(&pdev->dev, "failed to register interrupt\n"); | |
9bb9dc13 | 361 | goto err_free_keymap; |
a17f7955 AA |
362 | } |
363 | ||
78608a0d | 364 | device_init_wakeup(&pdev->dev, true); |
f77621cc | 365 | pm_runtime_put_sync(&pdev->dev); |
5ad567ff | 366 | |
a17f7955 AA |
367 | error = input_register_device(keypad_data->input); |
368 | if (error < 0) { | |
369 | dev_err(&pdev->dev, "failed to register input device\n"); | |
5ad567ff | 370 | goto err_pm_disable; |
a17f7955 AA |
371 | } |
372 | ||
a17f7955 AA |
373 | platform_set_drvdata(pdev, keypad_data); |
374 | return 0; | |
375 | ||
5ad567ff AA |
376 | err_pm_disable: |
377 | pm_runtime_disable(&pdev->dev); | |
a17f7955 | 378 | free_irq(keypad_data->irq, keypad_data); |
13987435 SP |
379 | err_free_keymap: |
380 | kfree(keypad_data->keymap); | |
a17f7955 AA |
381 | err_free_input: |
382 | input_free_device(input_dev); | |
f77621cc PS |
383 | err_pm_put_sync: |
384 | pm_runtime_put_sync(&pdev->dev); | |
f3a1ba60 AA |
385 | err_unmap: |
386 | iounmap(keypad_data->base); | |
387 | err_release_mem: | |
13987435 | 388 | release_mem_region(res->start, resource_size(res)); |
a17f7955 AA |
389 | err_free_keypad: |
390 | kfree(keypad_data); | |
391 | return error; | |
392 | } | |
393 | ||
e2619cf7 | 394 | static int omap4_keypad_remove(struct platform_device *pdev) |
a17f7955 AA |
395 | { |
396 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); | |
f3a1ba60 | 397 | struct resource *res; |
a17f7955 AA |
398 | |
399 | free_irq(keypad_data->irq, keypad_data); | |
5ad567ff AA |
400 | |
401 | pm_runtime_disable(&pdev->dev); | |
402 | ||
a17f7955 | 403 | input_unregister_device(keypad_data->input); |
f3a1ba60 AA |
404 | |
405 | iounmap(keypad_data->base); | |
406 | ||
407 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
408 | release_mem_region(res->start, resource_size(res)); | |
409 | ||
13987435 | 410 | kfree(keypad_data->keymap); |
a17f7955 | 411 | kfree(keypad_data); |
13987435 | 412 | |
a17f7955 AA |
413 | return 0; |
414 | } | |
415 | ||
13987435 SP |
416 | static const struct of_device_id omap_keypad_dt_match[] = { |
417 | { .compatible = "ti,omap4-keypad" }, | |
418 | {}, | |
419 | }; | |
420 | MODULE_DEVICE_TABLE(of, omap_keypad_dt_match); | |
13987435 | 421 | |
78608a0d IS |
422 | #ifdef CONFIG_PM_SLEEP |
423 | static int omap4_keypad_suspend(struct device *dev) | |
424 | { | |
425 | struct platform_device *pdev = to_platform_device(dev); | |
426 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); | |
427 | int error; | |
428 | ||
429 | if (device_may_wakeup(&pdev->dev)) { | |
430 | error = enable_irq_wake(keypad_data->irq); | |
431 | if (!error) | |
432 | keypad_data->irq_wake_enabled = true; | |
433 | } | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | static int omap4_keypad_resume(struct device *dev) | |
439 | { | |
440 | struct platform_device *pdev = to_platform_device(dev); | |
441 | struct omap4_keypad *keypad_data = platform_get_drvdata(pdev); | |
442 | ||
443 | if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) { | |
444 | disable_irq_wake(keypad_data->irq); | |
445 | keypad_data->irq_wake_enabled = false; | |
446 | } | |
447 | ||
448 | return 0; | |
449 | } | |
450 | #endif | |
451 | ||
452 | static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops, | |
453 | omap4_keypad_suspend, omap4_keypad_resume); | |
454 | ||
a17f7955 AA |
455 | static struct platform_driver omap4_keypad_driver = { |
456 | .probe = omap4_keypad_probe, | |
1cb0aa88 | 457 | .remove = omap4_keypad_remove, |
a17f7955 AA |
458 | .driver = { |
459 | .name = "omap4-keypad", | |
78608a0d | 460 | .pm = &omap4_keypad_pm_ops, |
61721c88 | 461 | .of_match_table = omap_keypad_dt_match, |
a17f7955 AA |
462 | }, |
463 | }; | |
5146c84f | 464 | module_platform_driver(omap4_keypad_driver); |
a17f7955 AA |
465 | |
466 | MODULE_AUTHOR("Texas Instruments"); | |
467 | MODULE_DESCRIPTION("OMAP4 Keypad Driver"); | |
468 | MODULE_LICENSE("GPL"); | |
469 | MODULE_ALIAS("platform:omap4-keypad"); |