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/clk.h>
15 #include <linux/errno.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>
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
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)
68 KBD_REVISION_OMAP4
= 0,
73 struct input_dev
*input
;
77 struct mutex lock
; /* for key scan */
83 unsigned int row_shift
;
86 unsigned short *keymap
;
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
)
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
)
112 keypad_data
->base
+ keypad_data
->irqreg_offset
+ offset
);
115 static int omap4_keypad_report_keys(struct omap4_keypad
*keypad_data
,
118 struct input_dev
*input_dev
= keypad_data
->input
;
119 unsigned int col
, row
, code
;
120 DECLARE_BITMAP(mask
, 64);
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
);
138 input_sync(input_dev
);
143 static void omap4_keypad_scan_keys(struct omap4_keypad
*keypad_data
, u64 keys
)
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
;
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 error
= clk_prepare_enable(keypad_data
->fck
);
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
);
234 pm_runtime_mark_last_busy(dev
);
235 pm_runtime_put_autosuspend(dev
);
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
;
258 error
= pm_runtime_resume_and_get(dev
);
260 dev_err(dev
, "%s: pm_runtime_resume_and_get() failed: %d\n",
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
;
278 err
= matrix_keypad_parse_properties(dev
, &keypad_data
->rows
,
283 keypad_data
->no_autorepeat
= of_property_read_bool(np
, "linux,input-no-autorepeat");
288 static int omap4_keypad_check_revision(struct device
*dev
,
289 struct omap4_keypad
*keypad_data
)
293 rev
= __raw_readl(keypad_data
->base
+ OMAP4_KBD_REVISION
);
297 case KBD_REVISION_OMAP4
:
298 keypad_data
->reg_offset
= 0x00;
299 keypad_data
->irqreg_offset
= 0x00;
301 case KBD_REVISION_OMAP5
:
302 keypad_data
->reg_offset
= 0x10;
303 keypad_data
->irqreg_offset
= 0x0c;
306 dev_err(dev
, "Keypad reports unsupported revision %d", rev
);
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
);
324 active
= kbd_readl(keypad_data
, OMAP4_KBD_STATEMACHINE
);
326 pm_runtime_mark_last_busy(dev
);
330 omap4_keypad_scan_keys(keypad_data
, 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
;
354 irq
= platform_get_irq(pdev
, 0);
358 keypad_data
= devm_kzalloc(dev
, sizeof(*keypad_data
), GFP_KERNEL
);
360 dev_err(dev
, "keypad_data memory allocation failed\n");
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
);
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
);
387 dev_err(dev
, "unable to register cleanup action\n");
392 * Enable clocks for the keypad module so that we can read
395 error
= pm_runtime_resume_and_get(dev
);
397 dev_err(dev
, "pm_runtime_resume_and_get() failed\n");
401 error
= omap4_keypad_check_revision(dev
, keypad_data
);
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
);
412 /* input device allocation */
413 keypad_data
->input
= input_dev
= devm_input_allocate_device(dev
);
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
,
436 sizeof(keypad_data
->keymap
[0]),
438 if (!keypad_data
->keymap
) {
439 dev_err(dev
, "Not enough memory for keymap\n");
443 error
= matrix_keypad_build_keymap(NULL
, NULL
,
444 keypad_data
->rows
, keypad_data
->cols
,
445 keypad_data
->keymap
, input_dev
);
447 dev_err(dev
, "failed to build keymap\n");
451 error
= devm_request_threaded_irq(dev
, keypad_data
->irq
,
452 omap4_keypad_irq_handler
,
453 omap4_keypad_irq_thread_fn
,
455 "omap4-keypad", keypad_data
);
457 dev_err(dev
, "failed to register interrupt\n");
461 error
= input_register_device(keypad_data
->input
);
463 dev_err(dev
, "failed to register input device\n");
467 device_init_wakeup(dev
, true);
468 error
= dev_pm_set_wake_irq(dev
, keypad_data
->irq
);
470 dev_warn(dev
, "failed to set up wakeup irq: %d\n", error
);
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
,
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");