xref: /linux/drivers/input/keyboard/omap4-keypad.c (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
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 
64 enum {
65 	KBD_REVISION_OMAP4 = 0,
66 	KBD_REVISION_OMAP5,
67 };
68 
69 struct omap4_keypad {
70 	struct input_dev *input;
71 
72 	void __iomem *base;
73 	unsigned int irq;
74 
75 	unsigned int rows;
76 	unsigned int cols;
77 	u32 reg_offset;
78 	u32 irqreg_offset;
79 	unsigned int row_shift;
80 	bool no_autorepeat;
81 	unsigned char key_state[8];
82 	unsigned short *keymap;
83 };
84 
85 static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset)
86 {
87 	return __raw_readl(keypad_data->base +
88 				keypad_data->reg_offset + offset);
89 }
90 
91 static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value)
92 {
93 	__raw_writel(value,
94 		     keypad_data->base + keypad_data->reg_offset + offset);
95 }
96 
97 static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset)
98 {
99 	return __raw_readl(keypad_data->base +
100 				keypad_data->irqreg_offset + offset);
101 }
102 
103 static void kbd_write_irqreg(struct omap4_keypad *keypad_data,
104 			     u32 offset, u32 value)
105 {
106 	__raw_writel(value,
107 		     keypad_data->base + keypad_data->irqreg_offset + offset);
108 }
109 
110 
111 /* Interrupt handlers */
112 static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id)
113 {
114 	struct omap4_keypad *keypad_data = dev_id;
115 
116 	if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS))
117 		return IRQ_WAKE_THREAD;
118 
119 	return IRQ_NONE;
120 }
121 
122 static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id)
123 {
124 	struct omap4_keypad *keypad_data = dev_id;
125 	struct input_dev *input_dev = keypad_data->input;
126 	unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)];
127 	unsigned int col, row, code, changed;
128 	u32 *new_state = (u32 *) key_state;
129 
130 	*new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0);
131 	*(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32);
132 
133 	for (row = 0; row < keypad_data->rows; row++) {
134 		changed = key_state[row] ^ keypad_data->key_state[row];
135 		if (!changed)
136 			continue;
137 
138 		for (col = 0; col < keypad_data->cols; col++) {
139 			if (changed & (1 << col)) {
140 				code = MATRIX_SCAN_CODE(row, col,
141 						keypad_data->row_shift);
142 				input_event(input_dev, EV_MSC, MSC_SCAN, code);
143 				input_report_key(input_dev,
144 						 keypad_data->keymap[code],
145 						 key_state[row] & (1 << col));
146 			}
147 		}
148 	}
149 
150 	input_sync(input_dev);
151 
152 	memcpy(keypad_data->key_state, key_state,
153 		sizeof(keypad_data->key_state));
154 
155 	/* clear pending interrupts */
156 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
157 			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
158 
159 	return IRQ_HANDLED;
160 }
161 
162 static int omap4_keypad_open(struct input_dev *input)
163 {
164 	struct omap4_keypad *keypad_data = input_get_drvdata(input);
165 
166 	pm_runtime_get_sync(input->dev.parent);
167 
168 	disable_irq(keypad_data->irq);
169 
170 	kbd_writel(keypad_data, OMAP4_KBD_CTRL,
171 			OMAP4_DEF_CTRL_NOSOFTMODE |
172 			(OMAP4_KEYPAD_PTV_DIV_128 << OMAP4_DEF_CTRL_PTV_SHIFT));
173 	kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME,
174 			OMAP4_VAL_DEBOUNCINGTIME_16MS);
175 	/* clear pending interrupts */
176 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
177 			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
178 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
179 			OMAP4_DEF_IRQENABLE_EVENTEN |
180 				OMAP4_DEF_IRQENABLE_LONGKEY);
181 	kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE,
182 			OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA);
183 
184 	enable_irq(keypad_data->irq);
185 
186 	return 0;
187 }
188 
189 static void omap4_keypad_stop(struct omap4_keypad *keypad_data)
190 {
191 	/* Disable interrupts and wake-up events */
192 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE,
193 			 OMAP4_VAL_IRQDISABLE);
194 	kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, 0);
195 
196 	/* clear pending interrupts */
197 	kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS,
198 			 kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS));
199 }
200 
201 static void omap4_keypad_close(struct input_dev *input)
202 {
203 	struct omap4_keypad *keypad_data;
204 
205 	keypad_data = input_get_drvdata(input);
206 	disable_irq(keypad_data->irq);
207 	omap4_keypad_stop(keypad_data);
208 	enable_irq(keypad_data->irq);
209 
210 	pm_runtime_put_sync(input->dev.parent);
211 }
212 
213 static int omap4_keypad_parse_dt(struct device *dev,
214 				 struct omap4_keypad *keypad_data)
215 {
216 	struct device_node *np = dev->of_node;
217 	int err;
218 
219 	err = matrix_keypad_parse_properties(dev, &keypad_data->rows,
220 					     &keypad_data->cols);
221 	if (err)
222 		return err;
223 
224 	if (of_get_property(np, "linux,input-no-autorepeat", NULL))
225 		keypad_data->no_autorepeat = true;
226 
227 	return 0;
228 }
229 
230 static int omap4_keypad_check_revision(struct device *dev,
231 				       struct omap4_keypad *keypad_data)
232 {
233 	unsigned int rev;
234 
235 	rev = __raw_readl(keypad_data->base + OMAP4_KBD_REVISION);
236 	rev &= 0x03 << 30;
237 	rev >>= 30;
238 	switch (rev) {
239 	case KBD_REVISION_OMAP4:
240 		keypad_data->reg_offset = 0x00;
241 		keypad_data->irqreg_offset = 0x00;
242 		break;
243 	case KBD_REVISION_OMAP5:
244 		keypad_data->reg_offset = 0x10;
245 		keypad_data->irqreg_offset = 0x0c;
246 		break;
247 	default:
248 		dev_err(dev, "Keypad reports unsupported revision %d", rev);
249 		return -EINVAL;
250 	}
251 
252 	return 0;
253 }
254 
255 static int omap4_keypad_probe(struct platform_device *pdev)
256 {
257 	struct omap4_keypad *keypad_data;
258 	struct input_dev *input_dev;
259 	struct resource *res;
260 	unsigned int max_keys;
261 	int irq;
262 	int error;
263 
264 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 	if (!res) {
266 		dev_err(&pdev->dev, "no base address specified\n");
267 		return -EINVAL;
268 	}
269 
270 	irq = platform_get_irq(pdev, 0);
271 	if (irq < 0)
272 		return irq;
273 
274 	keypad_data = kzalloc(sizeof(struct omap4_keypad), GFP_KERNEL);
275 	if (!keypad_data) {
276 		dev_err(&pdev->dev, "keypad_data memory allocation failed\n");
277 		return -ENOMEM;
278 	}
279 
280 	keypad_data->irq = irq;
281 
282 	error = omap4_keypad_parse_dt(&pdev->dev, keypad_data);
283 	if (error)
284 		goto err_free_keypad;
285 
286 	res = request_mem_region(res->start, resource_size(res), pdev->name);
287 	if (!res) {
288 		dev_err(&pdev->dev, "can't request mem region\n");
289 		error = -EBUSY;
290 		goto err_free_keypad;
291 	}
292 
293 	keypad_data->base = ioremap(res->start, resource_size(res));
294 	if (!keypad_data->base) {
295 		dev_err(&pdev->dev, "can't ioremap mem resource\n");
296 		error = -ENOMEM;
297 		goto err_release_mem;
298 	}
299 
300 	pm_runtime_enable(&pdev->dev);
301 
302 	/*
303 	 * Enable clocks for the keypad module so that we can read
304 	 * revision register.
305 	 */
306 	error = pm_runtime_get_sync(&pdev->dev);
307 	if (error) {
308 		dev_err(&pdev->dev, "pm_runtime_get_sync() failed\n");
309 		pm_runtime_put_noidle(&pdev->dev);
310 	} else {
311 		error = omap4_keypad_check_revision(&pdev->dev,
312 						    keypad_data);
313 		if (!error) {
314 			/* Ensure device does not raise interrupts */
315 			omap4_keypad_stop(keypad_data);
316 		}
317 		pm_runtime_put_sync(&pdev->dev);
318 	}
319 	if (error)
320 		goto err_pm_disable;
321 
322 	/* input device allocation */
323 	keypad_data->input = input_dev = input_allocate_device();
324 	if (!input_dev) {
325 		error = -ENOMEM;
326 		goto err_pm_disable;
327 	}
328 
329 	input_dev->name = pdev->name;
330 	input_dev->dev.parent = &pdev->dev;
331 	input_dev->id.bustype = BUS_HOST;
332 	input_dev->id.vendor = 0x0001;
333 	input_dev->id.product = 0x0001;
334 	input_dev->id.version = 0x0001;
335 
336 	input_dev->open = omap4_keypad_open;
337 	input_dev->close = omap4_keypad_close;
338 
339 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
340 	if (!keypad_data->no_autorepeat)
341 		__set_bit(EV_REP, input_dev->evbit);
342 
343 	input_set_drvdata(input_dev, keypad_data);
344 
345 	keypad_data->row_shift = get_count_order(keypad_data->cols);
346 	max_keys = keypad_data->rows << keypad_data->row_shift;
347 	keypad_data->keymap = kcalloc(max_keys,
348 				      sizeof(keypad_data->keymap[0]),
349 				      GFP_KERNEL);
350 	if (!keypad_data->keymap) {
351 		dev_err(&pdev->dev, "Not enough memory for keymap\n");
352 		error = -ENOMEM;
353 		goto err_free_input;
354 	}
355 
356 	error = matrix_keypad_build_keymap(NULL, NULL,
357 					   keypad_data->rows, keypad_data->cols,
358 					   keypad_data->keymap, input_dev);
359 	if (error) {
360 		dev_err(&pdev->dev, "failed to build keymap\n");
361 		goto err_free_keymap;
362 	}
363 
364 	error = request_threaded_irq(keypad_data->irq, omap4_keypad_irq_handler,
365 				     omap4_keypad_irq_thread_fn, IRQF_ONESHOT,
366 				     "omap4-keypad", keypad_data);
367 	if (error) {
368 		dev_err(&pdev->dev, "failed to register interrupt\n");
369 		goto err_free_keymap;
370 	}
371 
372 	error = input_register_device(keypad_data->input);
373 	if (error < 0) {
374 		dev_err(&pdev->dev, "failed to register input device\n");
375 		goto err_free_irq;
376 	}
377 
378 	device_init_wakeup(&pdev->dev, true);
379 	error = dev_pm_set_wake_irq(&pdev->dev, keypad_data->irq);
380 	if (error)
381 		dev_warn(&pdev->dev,
382 			 "failed to set up wakeup irq: %d\n", error);
383 
384 	platform_set_drvdata(pdev, keypad_data);
385 
386 	return 0;
387 
388 err_free_irq:
389 	free_irq(keypad_data->irq, keypad_data);
390 err_free_keymap:
391 	kfree(keypad_data->keymap);
392 err_free_input:
393 	input_free_device(input_dev);
394 err_pm_disable:
395 	pm_runtime_disable(&pdev->dev);
396 	iounmap(keypad_data->base);
397 err_release_mem:
398 	release_mem_region(res->start, resource_size(res));
399 err_free_keypad:
400 	kfree(keypad_data);
401 	return error;
402 }
403 
404 static int omap4_keypad_remove(struct platform_device *pdev)
405 {
406 	struct omap4_keypad *keypad_data = platform_get_drvdata(pdev);
407 	struct resource *res;
408 
409 	dev_pm_clear_wake_irq(&pdev->dev);
410 
411 	free_irq(keypad_data->irq, keypad_data);
412 
413 	pm_runtime_disable(&pdev->dev);
414 
415 	input_unregister_device(keypad_data->input);
416 
417 	iounmap(keypad_data->base);
418 
419 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
420 	release_mem_region(res->start, resource_size(res));
421 
422 	kfree(keypad_data->keymap);
423 	kfree(keypad_data);
424 
425 	return 0;
426 }
427 
428 static const struct of_device_id omap_keypad_dt_match[] = {
429 	{ .compatible = "ti,omap4-keypad" },
430 	{},
431 };
432 MODULE_DEVICE_TABLE(of, omap_keypad_dt_match);
433 
434 static struct platform_driver omap4_keypad_driver = {
435 	.probe		= omap4_keypad_probe,
436 	.remove		= omap4_keypad_remove,
437 	.driver		= {
438 		.name	= "omap4-keypad",
439 		.of_match_table = omap_keypad_dt_match,
440 	},
441 };
442 module_platform_driver(omap4_keypad_driver);
443 
444 MODULE_AUTHOR("Texas Instruments");
445 MODULE_DESCRIPTION("OMAP4 Keypad Driver");
446 MODULE_LICENSE("GPL");
447 MODULE_ALIAS("platform:omap4-keypad");
448