1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * OMAP4 Keypad Driver
4  *
5  * Copyright (C) 2010 Texas Instruments
6  *
7  * Author: Abraham Arce <x0066660@ti.com>
8  * Initial Code: Syed Rafiuddin <rafiuddin.syed@ti.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/errno.h>
15 #include <linux/io.h>
16 #include <linux/of.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>
22 
23 /* OMAP4 registers */
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
40 
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
48 
49 /* OMAP4 values */
50 #define OMAP4_VAL_IRQDISABLE		0x0
51 
52 /*
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.
57  */
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)
65 
66 enum {
67 	KBD_REVISION_OMAP4 = 0,
68 	KBD_REVISION_OMAP5,
69 };
70 
71 struct omap4_keypad {
72 	struct input_dev *input;
73 
74 	void __iomem *base;
75 	unsigned int irq;
76 	struct mutex lock;		/* for key scan */
77 
78 	unsigned int rows;
79 	unsigned int cols;
80 	u32 reg_offset;
81 	u32 irqreg_offset;
82 	unsigned int row_shift;
83 	bool no_autorepeat;
84 	u64 keys;
85 	unsigned short *keymap;
86 };
87 
88 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
89 {
90 	return __raw_readl(keypad_data->base +
91 				keypad_data->reg_offset + offset);
92 }
93 
94 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
95 {
96 	__raw_writel(value,
97 		     keypad_data->base + keypad_data->reg_offset + offset);
98 }
99 
100 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
101 {
102 	return __raw_readl(keypad_data->base +
103 				keypad_data->irqreg_offset + offset);
104 }
105 
106 static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
107 			     u32 offset, u32 value)
108 {
109 	__raw_writel(value,
110 		     keypad_data->base + keypad_data->irqreg_offset + offset);
111 }
112 
113 static int omap4_keypad_report_keys(struct omap4_keypad *keypad_data,
114 				    u64 keys, bool down)
115 {
116 	struct input_dev *input_dev = keypad_data->input;
117 	unsigned int col, row, code;
118 	DECLARE_BITMAP(mask, 64);
119 	unsigned long bit;
120 	int events = 0;
121 
122 	bitmap_from_u64(mask, keys);
123 
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);
128 
129 		input_event(input_dev, EV_MSC, MSC_SCAN, code);
130 		input_report_key(input_dev, keypad_data->keymap[code], down);
131 
132 		events++;
133 	}
134 
135 	if (events)
136 		input_sync(input_dev);
137 
138 	return events;
139 }
140 
141 static void omap4_keypad_scan_keys(struct omap4_keypad *keypad_data, u64 keys)
142 {
143 	u64 changed;
144 
145 	mutex_lock(&keypad_data->lock);
146 
147 	changed = keys ^ keypad_data->keys;
148 
149 	/*
150 	 * Report key up events separately and first. This matters in case we
151 	 * lost key-up interrupt and just now catching up.
152 	 */
153 	omap4_keypad_report_keys(keypad_data, changed & ~keys, false);
154 
155 	/* Report key down events */
156 	omap4_keypad_report_keys(keypad_data, changed & keys, true);
157 
158 	keypad_data->keys = keys;
159 
160 	mutex_unlock(&keypad_data->lock);
161 }
162 
163 /* Interrupt handlers */
164 static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
165 {
166 	struct omap4_keypad *keypad_data = dev_id;
167 
168 	if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS))
169 		return IRQ_WAKE_THREAD;
170 
171 	return IRQ_NONE;
172 }
173 
174 static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
175 {
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;
181 
182 	error = pm_runtime_resume_and_get(dev);
183 	if (error)
184 		return IRQ_NONE;
185 
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;
189 
190 	omap4_keypad_scan_keys(keypad_data, keys);
191 
192 	/* clear pending interrupts */
193 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
194 			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
195 
196 	pm_runtime_mark_last_busy(dev);
197 	pm_runtime_put_autosuspend(dev);
198 
199 	return IRQ_HANDLED;
200 }
201 
202 static int omap4_keypad_open(struct input_dev *input)
203 {
204 	struct omap4_keypad *keypad_data = input_get_drvdata(input);
205 	struct device *dev = input->dev.parent;
206 	int error;
207 
208 	error = pm_runtime_resume_and_get(dev);
209 	if (error)
210 		return error;
211 
212 	disable_irq(keypad_data->irq);
213 
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);
226 
227 	enable_irq(keypad_data->irq);
228 
229 	pm_runtime_mark_last_busy(dev);
230 	pm_runtime_put_autosuspend(dev);
231 
232 	return 0;
233 }
234 
235 static void omap4_keypad_stop(struct omap4_keypad *keypad_data)
236 {
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);
241 
242 	/* clear pending interrupts */
243 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
244 			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
245 }
246 
247 static void omap4_keypad_close(struct input_dev *input)
248 {
249 	struct omap4_keypad *keypad_data = input_get_drvdata(input);
250 	struct device *dev = input->dev.parent;
251 	int error;
252 
253 	error = pm_runtime_resume_and_get(dev);
254 	if (error)
255 		dev_err(dev, "%s: pm_runtime_resume_and_get() failed: %d\n",
256 			__func__, error);
257 
258 	disable_irq(keypad_data->irq);
259 	omap4_keypad_stop(keypad_data);
260 	enable_irq(keypad_data->irq);
261 
262 	pm_runtime_mark_last_busy(dev);
263 	pm_runtime_put_autosuspend(dev);
264 }
265 
266 static int omap4_keypad_parse_dt(struct device *dev,
267 				 struct omap4_keypad *keypad_data)
268 {
269 	struct device_node *np = dev->of_node;
270 	int err;
271 
272 	err = matrix_keypad_parse_properties(dev, &keypad_data->rows,
273 					     &keypad_data->cols);
274 	if (err)
275 		return err;
276 
277 	keypad_data->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat");
278 
279 	return 0;
280 }
281 
282 static int omap4_keypad_check_revision(struct device *dev,
283 				       struct omap4_keypad *keypad_data)
284 {
285 	unsigned int rev;
286 
287 	rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
288 	rev &= 0x03 << 30;
289 	rev >>= 30;
290 	switch (rev) {
291 	case KBD_REVISION_OMAP4:
292 		keypad_data->reg_offset = 0x00;
293 		keypad_data->irqreg_offset = 0x00;
294 		break;
295 	case KBD_REVISION_OMAP5:
296 		keypad_data->reg_offset = 0x10;
297 		keypad_data->irqreg_offset = 0x0c;
298 		break;
299 	default:
300 		dev_err(dev, "Keypad reports unsupported revision %d", rev);
301 		return -EINVAL;
302 	}
303 
304 	return 0;
305 }
306 
307 /*
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.
311  */
312 static int omap4_keypad_runtime_suspend(struct device *dev)
313 {
314 	struct platform_device *pdev = to_platform_device(dev);
315 	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
316 	u32 active;
317 
318 	active = kbd_readl(keypad_data, OMAP4_KBD_STATEMACHINE);
319 	if (active) {
320 		pm_runtime_mark_last_busy(dev);
321 		return -EBUSY;
322 	}
323 
324 	omap4_keypad_scan_keys(keypad_data, 0);
325 
326 	return 0;
327 }
328 
329 static const struct dev_pm_ops omap4_keypad_pm_ops = {
330 	RUNTIME_PM_OPS(omap4_keypad_runtime_suspend, NULL, NULL)
331 };
332 
333 static void omap4_disable_pm(void *d)
334 {
335 	pm_runtime_dont_use_autosuspend(d);
336 	pm_runtime_disable(d);
337 }
338 
339 static int omap4_keypad_probe(struct platform_device *pdev)
340 {
341 	struct device *dev = &pdev->dev;
342 	struct omap4_keypad *keypad_data;
343 	struct input_dev *input_dev;
344 	struct resource *res;
345 	unsigned int max_keys;
346 	int irq;
347 	int error;
348 
349 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
350 	if (!res) {
351 		dev_err(&pdev->dev, "no base address specified\n");
352 		return -EINVAL;
353 	}
354 
355 	irq = platform_get_irq(pdev, 0);
356 	if (irq < 0)
357 		return irq;
358 
359 	keypad_data = devm_kzalloc(dev, sizeof(*keypad_data), GFP_KERNEL);
360 	if (!keypad_data) {
361 		dev_err(dev, "keypad_data memory allocation failed\n");
362 		return -ENOMEM;
363 	}
364 
365 	keypad_data->irq = irq;
366 	mutex_init(&keypad_data->lock);
367 	platform_set_drvdata(pdev, keypad_data);
368 
369 	error = omap4_keypad_parse_dt(dev, keypad_data);
370 	if (error)
371 		return error;
372 
373 	keypad_data->base = devm_ioremap_resource(dev, res);
374 	if (IS_ERR(keypad_data->base))
375 		return PTR_ERR(keypad_data->base);
376 
377 	pm_runtime_use_autosuspend(dev);
378 	pm_runtime_set_autosuspend_delay(dev, OMAP4_KEYPAD_IDLE_CHECK_MS);
379 	pm_runtime_enable(dev);
380 
381 	error = devm_add_action_or_reset(dev, omap4_disable_pm, dev);
382 	if (error) {
383 		dev_err(dev, "unable to register cleanup action\n");
384 		return error;
385 	}
386 
387 	/*
388 	 * Enable clocks for the keypad module so that we can read
389 	 * revision register.
390 	 */
391 	error = pm_runtime_resume_and_get(dev);
392 	if (error) {
393 		dev_err(dev, "pm_runtime_resume_and_get() failed\n");
394 		return error;
395 	}
396 
397 	error = omap4_keypad_check_revision(dev, keypad_data);
398 	if (!error) {
399 		/* Ensure device does not raise interrupts */
400 		omap4_keypad_stop(keypad_data);
401 	}
402 
403 	pm_runtime_mark_last_busy(dev);
404 	pm_runtime_put_autosuspend(dev);
405 	if (error)
406 		return error;
407 
408 	/* input device allocation */
409 	keypad_data->input = input_dev = devm_input_allocate_device(dev);
410 	if (!input_dev)
411 		return -ENOMEM;
412 
413 	input_dev->name = pdev->name;
414 	input_dev->id.bustype = BUS_HOST;
415 	input_dev->id.vendor = 0x0001;
416 	input_dev->id.product = 0x0001;
417 	input_dev->id.version = 0x0001;
418 
419 	input_dev->open = omap4_keypad_open;
420 	input_dev->close = omap4_keypad_close;
421 
422 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
423 	if (!keypad_data->no_autorepeat)
424 		__set_bit(EV_REP, input_dev->evbit);
425 
426 	input_set_drvdata(input_dev, keypad_data);
427 
428 	keypad_data->row_shift = get_count_order(keypad_data->cols);
429 	max_keys = keypad_data->rows << keypad_data->row_shift;
430 	keypad_data->keymap = devm_kcalloc(dev,
431 					   max_keys,
432 					   sizeof(keypad_data->keymap[0]),
433 					   GFP_KERNEL);
434 	if (!keypad_data->keymap) {
435 		dev_err(dev, "Not enough memory for keymap\n");
436 		return -ENOMEM;
437 	}
438 
439 	error = matrix_keypad_build_keymap(NULL, NULL,
440 					   keypad_data->rows, keypad_data->cols,
441 					   keypad_data->keymap, input_dev);
442 	if (error) {
443 		dev_err(dev, "failed to build keymap\n");
444 		return error;
445 	}
446 
447 	error = devm_request_threaded_irq(dev, keypad_data->irq,
448 					  omap4_keypad_irq_handler,
449 					  omap4_keypad_irq_thread_fn,
450 					  IRQF_ONESHOT,
451 					  "omap4-keypad", keypad_data);
452 	if (error) {
453 		dev_err(dev, "failed to register interrupt\n");
454 		return error;
455 	}
456 
457 	error = input_register_device(keypad_data->input);
458 	if (error) {
459 		dev_err(dev, "failed to register input device\n");
460 		return error;
461 	}
462 
463 	device_init_wakeup(dev, true);
464 	error = dev_pm_set_wake_irq(dev, keypad_data->irq);
465 	if (error)
466 		dev_warn(dev, "failed to set up wakeup irq: %d\n", error);
467 
468 	return 0;
469 }
470 
471 static int omap4_keypad_remove(struct platform_device *pdev)
472 {
473 	dev_pm_clear_wake_irq(&pdev->dev);
474 
475 	return 0;
476 }
477 
478 static const struct of_device_id omap_keypad_dt_match[] = {
479 	{ .compatible = "ti,omap4-keypad" },
480 	{},
481 };
482 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
483 
484 static struct platform_driver omap4_keypad_driver = {
485 	.probe		= omap4_keypad_probe,
486 	.remove		= omap4_keypad_remove,
487 	.driver		= {
488 		.name	= "omap4-keypad",
489 		.of_match_table = omap_keypad_dt_match,
490 		.pm = pm_ptr(&omap4_keypad_pm_ops),
491 	},
492 };
493 module_platform_driver(omap4_keypad_driver);
494 
495 MODULE_AUTHOR("Texas Instruments");
496 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
497 MODULE_LICENSE("GPL");
498 MODULE_ALIAS("platform:omap4-keypad");
499