• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
kbd_readl(struct omap4_keypad * keypad_data,u32 offset)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 
kbd_writel(struct omap4_keypad * keypad_data,u32 offset,u32 value)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 
kbd_read_irqreg(struct omap4_keypad * keypad_data,u32 offset)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 
kbd_write_irqreg(struct omap4_keypad * keypad_data,u32 offset,u32 value)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 
omap4_keypad_report_keys(struct omap4_keypad * keypad_data,u64 keys,bool down)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 
omap4_keypad_scan_keys(struct omap4_keypad * keypad_data,u64 keys)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 */
omap4_keypad_irq_handler(int irq,void * dev_id)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 
omap4_keypad_irq_thread_fn(int irq,void * dev_id)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 
omap4_keypad_open(struct input_dev * input)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 
omap4_keypad_stop(struct omap4_keypad * keypad_data)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 
omap4_keypad_close(struct input_dev * input)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 
omap4_keypad_parse_dt(struct device * dev,struct omap4_keypad * keypad_data)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 	if (of_get_property(np, "linux,input-no-autorepeat", NULL))
278 		keypad_data->no_autorepeat = true;
279 
280 	return 0;
281 }
282 
omap4_keypad_check_revision(struct device * dev,struct omap4_keypad * keypad_data)283 static int omap4_keypad_check_revision(struct device *dev,
284 				       struct omap4_keypad *keypad_data)
285 {
286 	unsigned int rev;
287 
288 	rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
289 	rev &= 0x03 << 30;
290 	rev >>= 30;
291 	switch (rev) {
292 	case KBD_REVISION_OMAP4:
293 		keypad_data->reg_offset = 0x00;
294 		keypad_data->irqreg_offset = 0x00;
295 		break;
296 	case KBD_REVISION_OMAP5:
297 		keypad_data->reg_offset = 0x10;
298 		keypad_data->irqreg_offset = 0x0c;
299 		break;
300 	default:
301 		dev_err(dev, "Keypad reports unsupported revision %d", rev);
302 		return -EINVAL;
303 	}
304 
305 	return 0;
306 }
307 
308 /*
309  * Errata ID i689 "1.32 Keyboard Key Up Event Can Be Missed".
310  * Interrupt may not happen for key-up events. We must clear stuck
311  * key-up events after the keyboard hardware has auto-idled.
312  */
omap4_keypad_runtime_suspend(struct device * dev)313 static int __maybe_unused omap4_keypad_runtime_suspend(struct device *dev)
314 {
315 	struct platform_device *pdev = to_platform_device(dev);
316 	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
317 	u32 active;
318 
319 	active = kbd_readl(keypad_data, OMAP4_KBD_STATEMACHINE);
320 	if (active) {
321 		pm_runtime_mark_last_busy(dev);
322 		return -EBUSY;
323 	}
324 
325 	omap4_keypad_scan_keys(keypad_data, 0);
326 
327 	return 0;
328 }
329 
330 static const struct dev_pm_ops omap4_keypad_pm_ops = {
331 	SET_RUNTIME_PM_OPS(omap4_keypad_runtime_suspend, NULL, NULL)
332 };
333 
omap4_disable_pm(void * d)334 static void omap4_disable_pm(void *d)
335 {
336 	pm_runtime_dont_use_autosuspend(d);
337 	pm_runtime_disable(d);
338 }
339 
omap4_keypad_probe(struct platform_device * pdev)340 static int omap4_keypad_probe(struct platform_device *pdev)
341 {
342 	struct device *dev = &pdev->dev;
343 	struct omap4_keypad *keypad_data;
344 	struct input_dev *input_dev;
345 	struct resource *res;
346 	unsigned int max_keys;
347 	int irq;
348 	int error;
349 
350 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
351 	if (!res) {
352 		dev_err(&pdev->dev, "no base address specified\n");
353 		return -EINVAL;
354 	}
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 	mutex_init(&keypad_data->lock);
368 	platform_set_drvdata(pdev, keypad_data);
369 
370 	error = omap4_keypad_parse_dt(dev, keypad_data);
371 	if (error)
372 		return error;
373 
374 	keypad_data->base = devm_ioremap_resource(dev, res);
375 	if (IS_ERR(keypad_data->base))
376 		return PTR_ERR(keypad_data->base);
377 
378 	pm_runtime_use_autosuspend(dev);
379 	pm_runtime_set_autosuspend_delay(dev, OMAP4_KEYPAD_IDLE_CHECK_MS);
380 	pm_runtime_enable(dev);
381 
382 	error = devm_add_action_or_reset(dev, omap4_disable_pm, dev);
383 	if (error) {
384 		dev_err(dev, "unable to register cleanup action\n");
385 		return error;
386 	}
387 
388 	/*
389 	 * Enable clocks for the keypad module so that we can read
390 	 * revision register.
391 	 */
392 	error = pm_runtime_resume_and_get(dev);
393 	if (error) {
394 		dev_err(dev, "pm_runtime_resume_and_get() failed\n");
395 		return error;
396 	}
397 
398 	error = omap4_keypad_check_revision(dev, keypad_data);
399 	if (!error) {
400 		/* Ensure device does not raise interrupts */
401 		omap4_keypad_stop(keypad_data);
402 	}
403 
404 	pm_runtime_mark_last_busy(dev);
405 	pm_runtime_put_autosuspend(dev);
406 	if (error)
407 		return error;
408 
409 	/* input device allocation */
410 	keypad_data->input = input_dev = devm_input_allocate_device(dev);
411 	if (!input_dev)
412 		return -ENOMEM;
413 
414 	input_dev->name = pdev->name;
415 	input_dev->id.bustype = BUS_HOST;
416 	input_dev->id.vendor = 0x0001;
417 	input_dev->id.product = 0x0001;
418 	input_dev->id.version = 0x0001;
419 
420 	input_dev->open = omap4_keypad_open;
421 	input_dev->close = omap4_keypad_close;
422 
423 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
424 	if (!keypad_data->no_autorepeat)
425 		__set_bit(EV_REP, input_dev->evbit);
426 
427 	input_set_drvdata(input_dev, keypad_data);
428 
429 	keypad_data->row_shift = get_count_order(keypad_data->cols);
430 	max_keys = keypad_data->rows << keypad_data->row_shift;
431 	keypad_data->keymap = devm_kcalloc(dev,
432 					   max_keys,
433 					   sizeof(keypad_data->keymap[0]),
434 					   GFP_KERNEL);
435 	if (!keypad_data->keymap) {
436 		dev_err(dev, "Not enough memory for keymap\n");
437 		return -ENOMEM;
438 	}
439 
440 	error = matrix_keypad_build_keymap(NULL, NULL,
441 					   keypad_data->rows, keypad_data->cols,
442 					   keypad_data->keymap, input_dev);
443 	if (error) {
444 		dev_err(dev, "failed to build keymap\n");
445 		return error;
446 	}
447 
448 	error = devm_request_threaded_irq(dev, keypad_data->irq,
449 					  omap4_keypad_irq_handler,
450 					  omap4_keypad_irq_thread_fn,
451 					  IRQF_ONESHOT,
452 					  "omap4-keypad", keypad_data);
453 	if (error) {
454 		dev_err(dev, "failed to register interrupt\n");
455 		return error;
456 	}
457 
458 	error = input_register_device(keypad_data->input);
459 	if (error) {
460 		dev_err(dev, "failed to register input device\n");
461 		return error;
462 	}
463 
464 	device_init_wakeup(dev, true);
465 	error = dev_pm_set_wake_irq(dev, keypad_data->irq);
466 	if (error)
467 		dev_warn(dev, "failed to set up wakeup irq: %d\n", error);
468 
469 	return 0;
470 }
471 
omap4_keypad_remove(struct platform_device * pdev)472 static int omap4_keypad_remove(struct platform_device *pdev)
473 {
474 	dev_pm_clear_wake_irq(&pdev->dev);
475 
476 	return 0;
477 }
478 
479 static const struct of_device_id omap_keypad_dt_match[] = {
480 	{ .compatible = "ti,omap4-keypad" },
481 	{},
482 };
483 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
484 
485 static struct platform_driver omap4_keypad_driver = {
486 	.probe		= omap4_keypad_probe,
487 	.remove		= omap4_keypad_remove,
488 	.driver		= {
489 		.name	= "omap4-keypad",
490 		.of_match_table = omap_keypad_dt_match,
491 		.pm = &omap4_keypad_pm_ops,
492 	},
493 };
494 module_platform_driver(omap4_keypad_driver);
495 
496 MODULE_AUTHOR("Texas Instruments");
497 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
498 MODULE_LICENSE("GPL");
499 MODULE_ALIAS("platform:omap4-keypad");
500