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