drm/panel: simple: add Multi-Inno Technology MI0700A2T-30
[drm/drm-misc.git] / drivers / input / keyboard / omap4-keypad.c
blobbffe89c0717adf9ebe5b33892efa4dc30b158f83
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OMAP4 Keypad Driver
5 * Copyright (C) 2010 Texas Instruments
7 * Author: Abraham Arce <x0066660@ti.com>
8 * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
9 */
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/errno.h>
16 #include <linux/io.h>
17 #include <linux/of.h>
18 #include <linux/input.h>
19 #include <linux/input/matrix_keypad.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/pm_wakeirq.h>
24 /* OMAP4 registers */
25 #define OMAP4_KBD_REVISION 0x00
26 #define OMAP4_KBD_SYSCONFIG 0x10
27 #define OMAP4_KBD_SYSSTATUS 0x14
28 #define OMAP4_KBD_IRQSTATUS 0x18
29 #define OMAP4_KBD_IRQENABLE 0x1C
30 #define OMAP4_KBD_WAKEUPENABLE 0x20
31 #define OMAP4_KBD_PENDING 0x24
32 #define OMAP4_KBD_CTRL 0x28
33 #define OMAP4_KBD_DEBOUNCINGTIME 0x2C
34 #define OMAP4_KBD_LONGKEYTIME 0x30
35 #define OMAP4_KBD_TIMEOUT 0x34
36 #define OMAP4_KBD_STATEMACHINE 0x38
37 #define OMAP4_KBD_ROWINPUTS 0x3C
38 #define OMAP4_KBD_COLUMNOUTPUTS 0x40
39 #define OMAP4_KBD_FULLCODE31_0 0x44
40 #define OMAP4_KBD_FULLCODE63_32 0x48
42 /* OMAP4 bit definitions */
43 #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0)
44 #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1)
45 #define OMAP4_DEF_WUP_EVENT_ENA BIT(0)
46 #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1)
47 #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1)
48 #define OMAP4_DEF_CTRL_PTV_SHIFT 2
50 /* OMAP4 values */
51 #define OMAP4_VAL_IRQDISABLE 0x0
54 * Errata i689: If a key is released for a time shorter than debounce time,
55 * the keyboard will idle and never detect the key release. The workaround
56 * is to use at least a 12ms debounce time. See omap5432 TRM chapter
57 * "26.4.6.2 Keyboard Controller Timer" for more information.
59 #define OMAP4_KEYPAD_PTV_DIV_128 0x6
60 #define OMAP4_KEYPAD_DEBOUNCINGTIME_MS(dbms, ptv) \
61 ((((dbms) * 1000) / ((1 << ((ptv) + 1)) * (1000000 / 32768))) - 1)
62 #define OMAP4_VAL_DEBOUNCINGTIME_16MS \
63 OMAP4_KEYPAD_DEBOUNCINGTIME_MS(16, OMAP4_KEYPAD_PTV_DIV_128)
64 #define OMAP4_KEYPAD_AUTOIDLE_MS 50 /* Approximate measured time */
65 #define OMAP4_KEYPAD_IDLE_CHECK_MS (OMAP4_KEYPAD_AUTOIDLE_MS / 2)
67 enum {
68 KBD_REVISION_OMAP4 = 0,
69 KBD_REVISION_OMAP5,
72 struct omap4_keypad {
73 struct input_dev *input;
75 void __iomem *base;
76 unsigned int irq;
77 struct mutex lock; /* for key scan */
79 unsigned int rows;
80 unsigned int cols;
81 u32 reg_offset;
82 u32 irqreg_offset;
83 unsigned int row_shift;
84 bool no_autorepeat;
85 u64 keys;
86 unsigned short *keymap;
87 struct clk *fck;
90 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
92 return __raw_readl(keypad_data->base +
93 keypad_data->reg_offset + offset);
96 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
98 __raw_writel(value,
99 keypad_data->base + keypad_data->reg_offset + offset);
102 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
104 return __raw_readl(keypad_data->base +
105 keypad_data->irqreg_offset + offset);
108 static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
109 u32 offset, u32 value)
111 __raw_writel(value,
112 keypad_data->base + keypad_data->irqreg_offset + offset);
115 static int omap4_keypad_report_keys(struct omap4_keypad *keypad_data,
116 u64 keys, bool down)
118 struct input_dev *input_dev = keypad_data->input;
119 unsigned int col, row, code;
120 DECLARE_BITMAP(mask, 64);
121 unsigned long bit;
122 int events = 0;
124 bitmap_from_u64(mask, keys);
126 for_each_set_bit(bit, mask, keypad_data->rows * BITS_PER_BYTE) {
127 row = bit / BITS_PER_BYTE;
128 col = bit % BITS_PER_BYTE;
129 code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift);
131 input_event(input_dev, EV_MSC, MSC_SCAN, code);
132 input_report_key(input_dev, keypad_data->keymap[code], down);
134 events++;
137 if (events)
138 input_sync(input_dev);
140 return events;
143 static void omap4_keypad_scan_keys(struct omap4_keypad *keypad_data, u64 keys)
145 u64 changed;
147 guard(mutex)(&keypad_data->lock);
149 changed = keys ^ keypad_data->keys;
152 * Report key up events separately and first. This matters in case we
153 * lost key-up interrupt and just now catching up.
155 omap4_keypad_report_keys(keypad_data, changed & ~keys, false);
157 /* Report key down events */
158 omap4_keypad_report_keys(keypad_data, changed & keys, true);
160 keypad_data->keys = keys;
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;
171 return IRQ_NONE;
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;
178 u32 low, high;
179 int error;
180 u64 keys;
182 error = pm_runtime_resume_and_get(dev);
183 if (error)
184 return IRQ_NONE;
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);
199 return IRQ_HANDLED;
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;
206 int error;
208 error = pm_runtime_resume_and_get(dev);
209 if (error)
210 return error;
212 error = clk_prepare_enable(keypad_data->fck);
213 if (error)
214 goto out;
216 disable_irq(keypad_data->irq);
218 kbd_writel(keypad_data, OMAP4_KBD_CTRL,
219 OMAP4_DEF_CTRL_NOSOFTMODE |
220 (OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT));
221 kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
222 OMAP4_VAL_DEBOUNCINGTIME_16MS);
223 /* clear pending interrupts */
224 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
225 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
226 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
227 OMAP4_DEF_IRQENABLE_EVENTEN);
228 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
229 OMAP4_DEF_WUP_EVENT_ENA);
231 enable_irq(keypad_data->irq);
233 out:
234 pm_runtime_mark_last_busy(dev);
235 pm_runtime_put_autosuspend(dev);
237 return error;
240 static void omap4_keypad_stop(struct omap4_keypad *keypad_data)
242 /* Disable interrupts and wake-up events */
243 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
244 OMAP4_VAL_IRQDISABLE);
245 kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0);
247 /* clear pending interrupts */
248 kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
249 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
252 static void omap4_keypad_close(struct input_dev *input)
254 struct omap4_keypad *keypad_data = input_get_drvdata(input);
255 struct device *dev = input->dev.parent;
256 int error;
258 error = pm_runtime_resume_and_get(dev);
259 if (error)
260 dev_err(dev, "%s: pm_runtime_resume_and_get() failed: %d\n",
261 __func__, error);
263 disable_irq(keypad_data->irq);
264 omap4_keypad_stop(keypad_data);
265 enable_irq(keypad_data->irq);
266 clk_disable_unprepare(keypad_data->fck);
268 pm_runtime_mark_last_busy(dev);
269 pm_runtime_put_autosuspend(dev);
272 static int omap4_keypad_parse_dt(struct device *dev,
273 struct omap4_keypad *keypad_data)
275 struct device_node *np = dev->of_node;
276 int err;
278 err = matrix_keypad_parse_properties(dev, &keypad_data->rows,
279 &keypad_data->cols);
280 if (err)
281 return err;
283 keypad_data->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat");
285 return 0;
288 static int omap4_keypad_check_revision(struct device *dev,
289 struct omap4_keypad *keypad_data)
291 unsigned int rev;
293 rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
294 rev &= 0x03 << 30;
295 rev >>= 30;
296 switch (rev) {
297 case KBD_REVISION_OMAP4:
298 keypad_data->reg_offset = 0x00;
299 keypad_data->irqreg_offset = 0x00;
300 break;
301 case KBD_REVISION_OMAP5:
302 keypad_data->reg_offset = 0x10;
303 keypad_data->irqreg_offset = 0x0c;
304 break;
305 default:
306 dev_err(dev, "Keypad reports unsupported revision %d", rev);
307 return -EINVAL;
310 return 0;
314 * Errata ID i689 "1.32 Keyboard Key Up Event Can Be Missed".
315 * Interrupt may not happen for key-up events. We must clear stuck
316 * key-up events after the keyboard hardware has auto-idled.
318 static int omap4_keypad_runtime_suspend(struct device *dev)
320 struct platform_device *pdev = to_platform_device(dev);
321 struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
322 u32 active;
324 active = kbd_readl(keypad_data, OMAP4_KBD_STATEMACHINE);
325 if (active) {
326 pm_runtime_mark_last_busy(dev);
327 return -EBUSY;
330 omap4_keypad_scan_keys(keypad_data, 0);
332 return 0;
335 static const struct dev_pm_ops omap4_keypad_pm_ops = {
336 RUNTIME_PM_OPS(omap4_keypad_runtime_suspend, NULL, NULL)
339 static void omap4_disable_pm(void *d)
341 pm_runtime_dont_use_autosuspend(d);
342 pm_runtime_disable(d);
345 static int omap4_keypad_probe(struct platform_device *pdev)
347 struct device *dev = &pdev->dev;
348 struct omap4_keypad *keypad_data;
349 struct input_dev *input_dev;
350 unsigned int max_keys;
351 int irq;
352 int error;
354 irq = platform_get_irq(pdev, 0);
355 if (irq < 0)
356 return irq;
358 keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
359 if (!keypad_data) {
360 dev_err(dev, "keypad_data memory allocation failed\n");
361 return -ENOMEM;
364 keypad_data->irq = irq;
365 keypad_data->fck = devm_clk_get(&pdev->dev, "fck");
366 if (IS_ERR(keypad_data->fck))
367 return dev_err_probe(&pdev->dev, PTR_ERR(keypad_data->fck),
368 "unable to get fck");
370 mutex_init(&keypad_data->lock);
371 platform_set_drvdata(pdev, keypad_data);
373 error = omap4_keypad_parse_dt(dev, keypad_data);
374 if (error)
375 return error;
377 keypad_data->base = devm_platform_ioremap_resource(pdev, 0);
378 if (IS_ERR(keypad_data->base))
379 return PTR_ERR(keypad_data->base);
381 pm_runtime_use_autosuspend(dev);
382 pm_runtime_set_autosuspend_delay(dev, OMAP4_KEYPAD_IDLE_CHECK_MS);
383 pm_runtime_enable(dev);
385 error = devm_add_action_or_reset(dev, omap4_disable_pm, dev);
386 if (error) {
387 dev_err(dev, "unable to register cleanup action\n");
388 return error;
392 * Enable clocks for the keypad module so that we can read
393 * revision register.
395 error = pm_runtime_resume_and_get(dev);
396 if (error) {
397 dev_err(dev, "pm_runtime_resume_and_get() failed\n");
398 return error;
401 error = omap4_keypad_check_revision(dev, keypad_data);
402 if (!error) {
403 /* Ensure device does not raise interrupts */
404 omap4_keypad_stop(keypad_data);
407 pm_runtime_mark_last_busy(dev);
408 pm_runtime_put_autosuspend(dev);
409 if (error)
410 return error;
412 /* input device allocation */
413 keypad_data->input = input_dev = devm_input_allocate_device(dev);
414 if (!input_dev)
415 return -ENOMEM;
417 input_dev->name = pdev->name;
418 input_dev->id.bustype = BUS_HOST;
419 input_dev->id.vendor = 0x0001;
420 input_dev->id.product = 0x0001;
421 input_dev->id.version = 0x0001;
423 input_dev->open = omap4_keypad_open;
424 input_dev->close = omap4_keypad_close;
426 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
427 if (!keypad_data->no_autorepeat)
428 __set_bit(EV_REP, input_dev->evbit);
430 input_set_drvdata(input_dev, keypad_data);
432 keypad_data->row_shift = get_count_order(keypad_data->cols);
433 max_keys = keypad_data->rows << keypad_data->row_shift;
434 keypad_data->keymap = devm_kcalloc(dev,
435 max_keys,
436 sizeof(keypad_data->keymap[0]),
437 GFP_KERNEL);
438 if (!keypad_data->keymap) {
439 dev_err(dev, "Not enough memory for keymap\n");
440 return -ENOMEM;
443 error = matrix_keypad_build_keymap(NULL, NULL,
444 keypad_data->rows, keypad_data->cols,
445 keypad_data->keymap, input_dev);
446 if (error) {
447 dev_err(dev, "failed to build keymap\n");
448 return error;
451 error = devm_request_threaded_irq(dev, keypad_data->irq,
452 omap4_keypad_irq_handler,
453 omap4_keypad_irq_thread_fn,
454 IRQF_ONESHOT,
455 "omap4-keypad", keypad_data);
456 if (error) {
457 dev_err(dev, "failed to register interrupt\n");
458 return error;
461 error = input_register_device(keypad_data->input);
462 if (error) {
463 dev_err(dev, "failed to register input device\n");
464 return error;
467 device_init_wakeup(dev, true);
468 error = dev_pm_set_wake_irq(dev, keypad_data->irq);
469 if (error)
470 dev_warn(dev, "failed to set up wakeup irq: %d\n", error);
472 return 0;
475 static void omap4_keypad_remove(struct platform_device *pdev)
477 dev_pm_clear_wake_irq(&pdev->dev);
480 static const struct of_device_id omap_keypad_dt_match[] = {
481 { .compatible = "ti,omap4-keypad" },
484 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
486 static struct platform_driver omap4_keypad_driver = {
487 .probe = omap4_keypad_probe,
488 .remove = omap4_keypad_remove,
489 .driver = {
490 .name = "omap4-keypad",
491 .of_match_table = omap_keypad_dt_match,
492 .pm = pm_ptr(&omap4_keypad_pm_ops),
495 module_platform_driver(omap4_keypad_driver);
497 MODULE_AUTHOR("Texas Instruments");
498 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
499 MODULE_LICENSE("GPL");
500 MODULE_ALIAS("platform:omap4-keypad");