1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2010 Texas Instruments
7 * Author: Abraham Arce <x0066660@ti.com>
8 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/errno.h>
17 #include <linux/input.h>
18 #include <linux/input/matrix_keypad.h>
19 #include <linux/slab.h>
20 #include <linux/pm_runtime.h>
23 #define OMAP4_KBD_REVISION 0x00
24 #define OMAP4_KBD_SYSCONFIG 0x10
25 #define OMAP4_KBD_SYSSTATUS 0x14
26 #define OMAP4_KBD_IRQSTATUS 0x18
27 #define OMAP4_KBD_IRQENABLE 0x1C
28 #define OMAP4_KBD_WAKEUPENABLE 0x20
29 #define OMAP4_KBD_PENDING 0x24
30 #define OMAP4_KBD_CTRL 0x28
31 #define OMAP4_KBD_DEBOUNCINGTIME 0x2C
32 #define OMAP4_KBD_LONGKEYTIME 0x30
33 #define OMAP4_KBD_TIMEOUT 0x34
34 #define OMAP4_KBD_STATEMACHINE 0x38
35 #define OMAP4_KBD_ROWINPUTS 0x3C
36 #define OMAP4_KBD_COLUMNOUTPUTS 0x40
37 #define OMAP4_KBD_FULLCODE31_0 0x44
38 #define OMAP4_KBD_FULLCODE63_32 0x48
40 /* OMAP4 bit definitions */
41 #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0)
42 #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1)
43 #define OMAP4_DEF_WUP_EVENT_ENA BIT(0)
44 #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1)
45 #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1)
46 #define OMAP4_DEF_CTRL_PTV_SHIFT 2
49 #define OMAP4_VAL_IRQDISABLE 0x0
52 * Errata i689: If a key is released for a time shorter than debounce time,
53 * the keyboard will idle and never detect the key release. The workaround
54 * is to use at least a 12ms debounce time. See omap5432 TRM chapter
55 * "26.4.6.2 Keyboard Controller Timer" for more information.
57 #define OMAP4_KEYPAD_PTV_DIV_128 0x6
58 #define OMAP4_KEYPAD_DEBOUNCINGTIME_MS(dbms, ptv) \
59 ((((dbms) * 1000) / ((1 << ((ptv) + 1)) * (1000000 / 32768))) - 1)
60 #define OMAP4_VAL_DEBOUNCINGTIME_16MS \
61 OMAP4_KEYPAD_DEBOUNCINGTIME_MS(16, OMAP4_KEYPAD_PTV_DIV_128)
64 KBD_REVISION_OMAP4 = 0,
69 struct input_dev *input;
72 bool irq_wake_enabled;
79 unsigned int row_shift;
81 unsigned char key_state[8];
82 unsigned short *keymap;
85 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
87 return __raw_readl(keypad_data->base +
88 keypad_data->reg_offset + offset);
91 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
94 keypad_data->base + keypad_data->reg_offset + offset);
97 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
99 return __raw_readl(keypad_data->base +
100 keypad_data->irqreg_offset + offset);
103 static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
104 u32 offset, u32 value)
107 keypad_data->base + keypad_data->irqreg_offset + offset);
111 /* Interrupt handlers */
112 static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
114 struct omap4_keypad *keypad_data = dev_id;
116 if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS))
117 return IRQ_WAKE_THREAD;
122 static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
124 struct omap4_keypad *keypad_data = dev_id;
125 struct input_dev *input_dev = keypad_data->input;
126 unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
127 unsigned int col, row, code, changed;
128 u32 *new_state = (u32 *) key_state;
130 *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
131 *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
133 for (row = 0; row < keypad_data->rows; row++) {
134 changed = key_state[row] ^ keypad_data->key_state[row];
138 for (col = 0; col < keypad_data->cols; col++) {
139 if (changed & (1 << col)) {
140 code = MATRIX_SCAN_CODE(row, col,
141 keypad_data->row_shift);
142 input_event(input_dev, EV_MSC, MSC_SCAN, code);
143 input_report_key(input_dev,
144 keypad_data->keymap[code],
145 key_state[row] & (1 << col));
150 input_sync(input_dev);
152 memcpy(keypad_data->key_state, key_state,
153 sizeof(keypad_data->key_state));
155 /* clear pending interrupts */
156 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
157 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
162 static int omap4_keypad_open(struct input_dev *input)
164 struct omap4_keypad *keypad_data = input_get_drvdata(input);
166 pm_runtime_get_sync(input->dev.parent);
168 disable_irq(keypad_data->irq);
170 kbd_writel(keypad_data, OMAP4_KBD_CTRL,
171 OMAP4_DEF_CTRL_NOSOFTMODE |
172 (OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT));
173 kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
174 OMAP4_VAL_DEBOUNCINGTIME_16MS);
175 /* clear pending interrupts */
176 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
177 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
178 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
179 OMAP4_DEF_IRQENABLE_EVENTEN |
180 OMAP4_DEF_IRQENABLE_LONGKEY);
181 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
182 OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
184 enable_irq(keypad_data->irq);
189 static void omap4_keypad_close(struct input_dev *input)
191 struct omap4_keypad *keypad_data = input_get_drvdata(input);
193 disable_irq(keypad_data->irq);
195 /* Disable interrupts and wake-up events */
196 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
197 OMAP4_VAL_IRQDISABLE);
198 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0);
200 /* clear pending interrupts */
201 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
202 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
204 enable_irq(keypad_data->irq);
206 pm_runtime_put_sync(input->dev.parent);
209 static int omap4_keypad_parse_dt(struct device *dev,
210 struct omap4_keypad *keypad_data)
212 struct device_node *np = dev->of_node;
215 err = matrix_keypad_parse_properties(dev, &keypad_data->rows,
220 if (of_get_property(np, "linux,input-no-autorepeat", NULL))
221 keypad_data->no_autorepeat = true;
226 static int omap4_keypad_probe(struct platform_device *pdev)
228 struct omap4_keypad *keypad_data;
229 struct input_dev *input_dev;
230 struct resource *res;
231 unsigned int max_keys;
236 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 dev_err(&pdev->dev, "no base address specified\n");
242 irq = platform_get_irq(pdev, 0);
244 dev_err(&pdev->dev, "no keyboard irq assigned\n");
248 keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
250 dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
254 keypad_data->irq = irq;
256 error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
258 goto err_free_keypad;
260 res = request_mem_region(res->start, resource_size(res), pdev->name);
262 dev_err(&pdev->dev, "can't request mem region\n");
264 goto err_free_keypad;
267 keypad_data->base = ioremap(res->start, resource_size(res));
268 if (!keypad_data->base) {
269 dev_err(&pdev->dev, "can't ioremap mem resource\n");
271 goto err_release_mem;
276 * Enable clocks for the keypad module so that we can read
279 pm_runtime_enable(&pdev->dev);
280 error = pm_runtime_get_sync(&pdev->dev);
282 dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
285 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
289 case KBD_REVISION_OMAP4:
290 keypad_data->reg_offset = 0x00;
291 keypad_data->irqreg_offset = 0x00;
293 case KBD_REVISION_OMAP5:
294 keypad_data->reg_offset = 0x10;
295 keypad_data->irqreg_offset = 0x0c;
299 "Keypad reports unsupported revision %d", rev);
301 goto err_pm_put_sync;
304 /* input device allocation */
305 keypad_data->input = input_dev = input_allocate_device();
308 goto err_pm_put_sync;
311 input_dev->name = pdev->name;
312 input_dev->dev.parent = &pdev->dev;
313 input_dev->id.bustype = BUS_HOST;
314 input_dev->id.vendor = 0x0001;
315 input_dev->id.product = 0x0001;
316 input_dev->id.version = 0x0001;
318 input_dev->open = omap4_keypad_open;
319 input_dev->close = omap4_keypad_close;
321 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
322 if (!keypad_data->no_autorepeat)
323 __set_bit(EV_REP, input_dev->evbit);
325 input_set_drvdata(input_dev, keypad_data);
327 keypad_data->row_shift = get_count_order(keypad_data->cols);
328 max_keys = keypad_data->rows << keypad_data->row_shift;
329 keypad_data->keymap = kcalloc(max_keys,
330 sizeof(keypad_data->keymap[0]),
332 if (!keypad_data->keymap) {
333 dev_err(&pdev->dev, "Not enough memory for keymap\n");
338 error = matrix_keypad_build_keymap(NULL, NULL,
339 keypad_data->rows, keypad_data->cols,
340 keypad_data->keymap, input_dev);
342 dev_err(&pdev->dev, "failed to build keymap\n");
343 goto err_free_keymap;
346 error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler,
347 omap4_keypad_irq_thread_fn, IRQF_ONESHOT,
348 "omap4-keypad", keypad_data);
350 dev_err(&pdev->dev, "failed to register interrupt\n");
351 goto err_free_keymap;
354 device_init_wakeup(&pdev->dev, true);
355 pm_runtime_put_sync(&pdev->dev);
357 error = input_register_device(keypad_data->input);
359 dev_err(&pdev->dev, "failed to register input device\n");
363 platform_set_drvdata(pdev, keypad_data);
367 pm_runtime_disable(&pdev->dev);
368 free_irq(keypad_data->irq, keypad_data);
370 kfree(keypad_data->keymap);
372 input_free_device(input_dev);
374 pm_runtime_put_sync(&pdev->dev);
376 iounmap(keypad_data->base);
378 release_mem_region(res->start, resource_size(res));
384 static int omap4_keypad_remove(struct platform_device *pdev)
386 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
387 struct resource *res;
389 free_irq(keypad_data->irq, keypad_data);
391 pm_runtime_disable(&pdev->dev);
393 input_unregister_device(keypad_data->input);
395 iounmap(keypad_data->base);
397 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
398 release_mem_region(res->start, resource_size(res));
400 kfree(keypad_data->keymap);
406 static const struct of_device_id omap_keypad_dt_match[] = {
407 { .compatible = "ti,omap4-keypad" },
410 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
412 #ifdef CONFIG_PM_SLEEP
413 static int omap4_keypad_suspend(struct device *dev)
415 struct platform_device *pdev = to_platform_device(dev);
416 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
419 if (device_may_wakeup(&pdev->dev)) {
420 error = enable_irq_wake(keypad_data->irq);
422 keypad_data->irq_wake_enabled = true;
428 static int omap4_keypad_resume(struct device *dev)
430 struct platform_device *pdev = to_platform_device(dev);
431 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
433 if (device_may_wakeup(&pdev->dev) && keypad_data->irq_wake_enabled) {
434 disable_irq_wake(keypad_data->irq);
435 keypad_data->irq_wake_enabled = false;
442 static SIMPLE_DEV_PM_OPS(omap4_keypad_pm_ops,
443 omap4_keypad_suspend, omap4_keypad_resume);
445 static struct platform_driver omap4_keypad_driver = {
446 .probe = omap4_keypad_probe,
447 .remove = omap4_keypad_remove,
449 .name = "omap4-keypad",
450 .pm = &omap4_keypad_pm_ops,
451 .of_match_table = omap_keypad_dt_match,
454 module_platform_driver(omap4_keypad_driver);
456 MODULE_AUTHOR("Texas Instruments");
457 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
458 MODULE_LICENSE("GPL");
459 MODULE_ALIAS("platform:omap4-keypad");