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>
21 #include <linux/pm_wakeirq.h>
24 #define OMAP4_KBD_REVISION 0x00
25 #define OMAP4_KBD_SYSCONFIG 0x10
26 #define OMAP4_KBD_SYSSTATUS 0x14
27 #define OMAP4_KBD_IRQSTATUS 0x18
28 #define OMAP4_KBD_IRQENABLE 0x1C
29 #define OMAP4_KBD_WAKEUPENABLE 0x20
30 #define OMAP4_KBD_PENDING 0x24
31 #define OMAP4_KBD_CTRL 0x28
32 #define OMAP4_KBD_DEBOUNCINGTIME 0x2C
33 #define OMAP4_KBD_LONGKEYTIME 0x30
34 #define OMAP4_KBD_TIMEOUT 0x34
35 #define OMAP4_KBD_STATEMACHINE 0x38
36 #define OMAP4_KBD_ROWINPUTS 0x3C
37 #define OMAP4_KBD_COLUMNOUTPUTS 0x40
38 #define OMAP4_KBD_FULLCODE31_0 0x44
39 #define OMAP4_KBD_FULLCODE63_32 0x48
41 /* OMAP4 bit definitions */
42 #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0)
43 #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1)
44 #define OMAP4_DEF_WUP_EVENT_ENA BIT(0)
45 #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1)
46 #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1)
47 #define OMAP4_DEF_CTRL_PTV_SHIFT 2
50 #define OMAP4_VAL_IRQDISABLE 0x0
53 * Errata i689: If a key is released for a time shorter than debounce time,
54 * the keyboard will idle and never detect the key release. The workaround
55 * is to use at least a 12ms debounce time. See omap5432 TRM chapter
56 * "26.4.6.2 Keyboard Controller Timer" for more information.
58 #define OMAP4_KEYPAD_PTV_DIV_128 0x6
59 #define OMAP4_KEYPAD_DEBOUNCINGTIME_MS(dbms, ptv) \
60 ((((dbms) * 1000) / ((1 << ((ptv) + 1)) * (1000000 / 32768))) - 1)
61 #define OMAP4_VAL_DEBOUNCINGTIME_16MS \
62 OMAP4_KEYPAD_DEBOUNCINGTIME_MS(16, OMAP4_KEYPAD_PTV_DIV_128)
63 #define OMAP4_KEYPAD_AUTOIDLE_MS 50 /* Approximate measured time */
64 #define OMAP4_KEYPAD_IDLE_CHECK_MS (OMAP4_KEYPAD_AUTOIDLE_MS / 2)
67 KBD_REVISION_OMAP4 = 0,
72 struct input_dev *input;
76 struct mutex lock; /* for key scan */
82 unsigned int row_shift;
85 unsigned short *keymap;
88 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
90 return __raw_readl(keypad_data->base +
91 keypad_data->reg_offset + offset);
94 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
97 keypad_data->base + keypad_data->reg_offset + offset);
100 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
102 return __raw_readl(keypad_data->base +
103 keypad_data->irqreg_offset + offset);
106 static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
107 u32 offset, u32 value)
110 keypad_data->base + keypad_data->irqreg_offset + offset);
113 static int omap4_keypad_report_keys(struct omap4_keypad *keypad_data,
116 struct input_dev *input_dev = keypad_data->input;
117 unsigned int col, row, code;
118 DECLARE_BITMAP(mask, 64);
122 bitmap_from_u64(mask, keys);
124 for_each_set_bit(bit, mask, keypad_data->rows * BITS_PER_BYTE) {
125 row = bit / BITS_PER_BYTE;
126 col = bit % BITS_PER_BYTE;
127 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
129 input_event(input_dev, EV_MSC, MSC_SCAN, code);
130 input_report_key(input_dev, keypad_data->keymap[code], down);
136 input_sync(input_dev);
141 static void omap4_keypad_scan_keys(struct omap4_keypad *keypad_data, u64 keys)
145 mutex_lock(&keypad_data->lock);
147 changed = keys ^ keypad_data->keys;
150 * Report key up events separately and first. This matters in case we
151 * lost key-up interrupt and just now catching up.
153 omap4_keypad_report_keys(keypad_data, changed & ~keys, false);
155 /* Report key down events */
156 omap4_keypad_report_keys(keypad_data, changed & keys, true);
158 keypad_data->keys = keys;
160 mutex_unlock(&keypad_data->lock);
163 /* Interrupt handlers */
164 static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
166 struct omap4_keypad *keypad_data = dev_id;
168 if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS))
169 return IRQ_WAKE_THREAD;
174 static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
176 struct omap4_keypad *keypad_data = dev_id;
177 struct device *dev = keypad_data->input->dev.parent;
182 error = pm_runtime_resume_and_get(dev);
186 low = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
187 high = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
188 keys = low | (u64)high << 32;
190 omap4_keypad_scan_keys(keypad_data, keys);
192 /* clear pending interrupts */
193 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
194 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
196 pm_runtime_mark_last_busy(dev);
197 pm_runtime_put_autosuspend(dev);
202 static int omap4_keypad_open(struct input_dev *input)
204 struct omap4_keypad *keypad_data = input_get_drvdata(input);
205 struct device *dev = input->dev.parent;
208 error = pm_runtime_resume_and_get(dev);
212 disable_irq(keypad_data->irq);
214 kbd_writel(keypad_data, OMAP4_KBD_CTRL,
215 OMAP4_DEF_CTRL_NOSOFTMODE |
216 (OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT));
217 kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
218 OMAP4_VAL_DEBOUNCINGTIME_16MS);
219 /* clear pending interrupts */
220 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
221 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
222 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
223 OMAP4_DEF_IRQENABLE_EVENTEN);
224 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
225 OMAP4_DEF_WUP_EVENT_ENA);
227 enable_irq(keypad_data->irq);
229 pm_runtime_mark_last_busy(dev);
230 pm_runtime_put_autosuspend(dev);
235 static void omap4_keypad_stop(struct omap4_keypad *keypad_data)
237 /* Disable interrupts and wake-up events */
238 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
239 OMAP4_VAL_IRQDISABLE);
240 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0);
242 /* clear pending interrupts */
243 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
244 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
247 static void omap4_keypad_close(struct input_dev *input)
249 struct omap4_keypad *keypad_data = input_get_drvdata(input);
250 struct device *dev = input->dev.parent;
253 error = pm_runtime_resume_and_get(dev);
255 dev_err(dev, "%s: pm_runtime_resume_and_get() failed: %d\n",
258 disable_irq(keypad_data->irq);
259 omap4_keypad_stop(keypad_data);
260 enable_irq(keypad_data->irq);
262 pm_runtime_mark_last_busy(dev);
263 pm_runtime_put_autosuspend(dev);
266 static int omap4_keypad_parse_dt(struct device *dev,
267 struct omap4_keypad *keypad_data)
269 struct device_node *np = dev->of_node;
272 err = matrix_keypad_parse_properties(dev, &keypad_data->rows,
277 keypad_data->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat");
282 static int omap4_keypad_check_revision(struct device *dev,
283 struct omap4_keypad *keypad_data)
287 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
291 case KBD_REVISION_OMAP4:
292 keypad_data->reg_offset = 0x00;
293 keypad_data->irqreg_offset = 0x00;
295 case KBD_REVISION_OMAP5:
296 keypad_data->reg_offset = 0x10;
297 keypad_data->irqreg_offset = 0x0c;
300 dev_err(dev, "Keypad reports unsupported revision %d", rev);
308 * Errata ID i689 "1.32 Keyboard Key Up Event Can Be Missed".
309 * Interrupt may not happen for key-up events. We must clear stuck
310 * key-up events after the keyboard hardware has auto-idled.
312 static int omap4_keypad_runtime_suspend(struct device *dev)
314 struct platform_device *pdev = to_platform_device(dev);
315 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
318 active = kbd_readl(keypad_data, OMAP4_KBD_STATEMACHINE);
320 pm_runtime_mark_last_busy(dev);
324 omap4_keypad_scan_keys(keypad_data, 0);
329 static const struct dev_pm_ops omap4_keypad_pm_ops = {
330 RUNTIME_PM_OPS(omap4_keypad_runtime_suspend, NULL, NULL)
333 static void omap4_disable_pm(void *d)
335 pm_runtime_dont_use_autosuspend(d);
336 pm_runtime_disable(d);
339 static int omap4_keypad_probe(struct platform_device *pdev)
341 struct device *dev = &pdev->dev;
342 struct omap4_keypad *keypad_data;
343 struct input_dev *input_dev;
344 unsigned int max_keys;
348 irq = platform_get_irq(pdev, 0);
352 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
354 dev_err(dev, "keypad_data memory allocation failed\n");
358 keypad_data->irq = irq;
359 mutex_init(&keypad_data->lock);
360 platform_set_drvdata(pdev, keypad_data);
362 error = omap4_keypad_parse_dt(dev, keypad_data);
366 keypad_data->base = devm_platform_ioremap_resource(pdev, 0);
367 if (IS_ERR(keypad_data->base))
368 return PTR_ERR(keypad_data->base);
370 pm_runtime_use_autosuspend(dev);
371 pm_runtime_set_autosuspend_delay(dev, OMAP4_KEYPAD_IDLE_CHECK_MS);
372 pm_runtime_enable(dev);
374 error = devm_add_action_or_reset(dev, omap4_disable_pm, dev);
376 dev_err(dev, "unable to register cleanup action\n");
381 * Enable clocks for the keypad module so that we can read
384 error = pm_runtime_resume_and_get(dev);
386 dev_err(dev, "pm_runtime_resume_and_get() failed\n");
390 error = omap4_keypad_check_revision(dev, keypad_data);
392 /* Ensure device does not raise interrupts */
393 omap4_keypad_stop(keypad_data);
396 pm_runtime_mark_last_busy(dev);
397 pm_runtime_put_autosuspend(dev);
401 /* input device allocation */
402 keypad_data->input = input_dev = devm_input_allocate_device(dev);
406 input_dev->name = pdev->name;
407 input_dev->id.bustype = BUS_HOST;
408 input_dev->id.vendor = 0x0001;
409 input_dev->id.product = 0x0001;
410 input_dev->id.version = 0x0001;
412 input_dev->open = omap4_keypad_open;
413 input_dev->close = omap4_keypad_close;
415 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
416 if (!keypad_data->no_autorepeat)
417 __set_bit(EV_REP, input_dev->evbit);
419 input_set_drvdata(input_dev, keypad_data);
421 keypad_data->row_shift = get_count_order(keypad_data->cols);
422 max_keys = keypad_data->rows << keypad_data->row_shift;
423 keypad_data->keymap = devm_kcalloc(dev,
425 sizeof(keypad_data->keymap[0]),
427 if (!keypad_data->keymap) {
428 dev_err(dev, "Not enough memory for keymap\n");
432 error = matrix_keypad_build_keymap(NULL, NULL,
433 keypad_data->rows, keypad_data->cols,
434 keypad_data->keymap, input_dev);
436 dev_err(dev, "failed to build keymap\n");
440 error = devm_request_threaded_irq(dev, keypad_data->irq,
441 omap4_keypad_irq_handler,
442 omap4_keypad_irq_thread_fn,
444 "omap4-keypad", keypad_data);
446 dev_err(dev, "failed to register interrupt\n");
450 error = input_register_device(keypad_data->input);
452 dev_err(dev, "failed to register input device\n");
456 device_init_wakeup(dev, true);
457 error = dev_pm_set_wake_irq(dev, keypad_data->irq);
459 dev_warn(dev, "failed to set up wakeup irq: %d\n", error);
464 static void omap4_keypad_remove(struct platform_device *pdev)
466 dev_pm_clear_wake_irq(&pdev->dev);
469 static const struct of_device_id omap_keypad_dt_match[] = {
470 { .compatible = "ti,omap4-keypad" },
473 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
475 static struct platform_driver omap4_keypad_driver = {
476 .probe = omap4_keypad_probe,
477 .remove_new = omap4_keypad_remove,
479 .name = "omap4-keypad",
480 .of_match_table = omap_keypad_dt_match,
481 .pm = pm_ptr(&omap4_keypad_pm_ops),
484 module_platform_driver(omap4_keypad_driver);
486 MODULE_AUTHOR("Texas Instruments");
487 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
488 MODULE_LICENSE("GPL");
489 MODULE_ALIAS("platform:omap4-keypad");