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