xref: /linux/drivers/input/keyboard/samsung-keypad.c (revision 0074281bb6316108e0cff094bd4db78ab3eee236)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Samsung keypad driver
4  *
5  * Copyright (C) 2010 Samsung Electronics Co.Ltd
6  * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7  * Author: Donghwa Lee <dh09.lee@samsung.com>
8  */
9 
10 #include <linux/bits.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/input.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/sched.h>
24 #include <linux/input/samsung-keypad.h>
25 
26 #define SAMSUNG_KEYIFCON			0x00
27 #define SAMSUNG_KEYIFSTSCLR			0x04
28 #define SAMSUNG_KEYIFCOL			0x08
29 #define SAMSUNG_KEYIFROW			0x0c
30 #define SAMSUNG_KEYIFFC				0x10
31 
32 /* SAMSUNG_KEYIFCON */
33 #define SAMSUNG_KEYIFCON_INT_F_EN		BIT(0)
34 #define SAMSUNG_KEYIFCON_INT_R_EN		BIT(1)
35 #define SAMSUNG_KEYIFCON_DF_EN			BIT(2)
36 #define SAMSUNG_KEYIFCON_FC_EN			BIT(3)
37 #define SAMSUNG_KEYIFCON_WAKEUPEN		BIT(4)
38 
39 /* SAMSUNG_KEYIFSTSCLR */
40 #define SAMSUNG_KEYIFSTSCLR_P_INT_MASK		(0xff << 0)
41 #define SAMSUNG_KEYIFSTSCLR_R_INT_MASK		(0xff << 8)
42 #define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET	8
43 #define S5PV210_KEYIFSTSCLR_P_INT_MASK		(0x3fff << 0)
44 #define S5PV210_KEYIFSTSCLR_R_INT_MASK		(0x3fff << 16)
45 #define S5PV210_KEYIFSTSCLR_R_INT_OFFSET	16
46 
47 /* SAMSUNG_KEYIFCOL */
48 #define SAMSUNG_KEYIFCOL_MASK			0xff
49 
50 /* SAMSUNG_KEYIFROW */
51 #define SAMSUNG_KEYIFROW_MASK			(0xff << 0)
52 #define S5PV210_KEYIFROW_MASK			(0x3fff << 0)
53 
54 /* SAMSUNG_KEYIFFC */
55 #define SAMSUNG_KEYIFFC_MASK			(0x3ff << 0)
56 
57 struct samsung_chip_info {
58 	unsigned int column_shift;
59 };
60 
61 struct samsung_keypad {
62 	const struct samsung_chip_info *chip;
63 	struct input_dev *input_dev;
64 	struct platform_device *pdev;
65 	struct clk *clk;
66 	void __iomem *base;
67 	wait_queue_head_t wait;
68 	bool stopped;
69 	bool wake_enabled;
70 	int irq;
71 	unsigned int row_shift;
72 	unsigned int rows;
73 	unsigned int cols;
74 	unsigned int row_state[SAMSUNG_MAX_COLS];
75 	unsigned short keycodes[];
76 };
77 
samsung_keypad_scan(struct samsung_keypad * keypad,unsigned int * row_state)78 static void samsung_keypad_scan(struct samsung_keypad *keypad,
79 				unsigned int *row_state)
80 {
81 	unsigned int col;
82 	unsigned int val;
83 
84 	for (col = 0; col < keypad->cols; col++) {
85 		val = SAMSUNG_KEYIFCOL_MASK & ~BIT(col);
86 		val <<= keypad->chip->column_shift;
87 
88 		writel(val, keypad->base + SAMSUNG_KEYIFCOL);
89 		mdelay(1);
90 
91 		val = readl(keypad->base + SAMSUNG_KEYIFROW);
92 		row_state[col] = ~val & GENMASK(keypad->rows - 1, 0);
93 	}
94 
95 	/* KEYIFCOL reg clear */
96 	writel(0, keypad->base + SAMSUNG_KEYIFCOL);
97 }
98 
samsung_keypad_report(struct samsung_keypad * keypad,unsigned int * row_state)99 static bool samsung_keypad_report(struct samsung_keypad *keypad,
100 				  unsigned int *row_state)
101 {
102 	struct input_dev *input_dev = keypad->input_dev;
103 	unsigned int changed;
104 	unsigned int pressed;
105 	unsigned int key_down = 0;
106 	unsigned int val;
107 	unsigned int col, row;
108 
109 	for (col = 0; col < keypad->cols; col++) {
110 		changed = row_state[col] ^ keypad->row_state[col];
111 		key_down |= row_state[col];
112 		if (!changed)
113 			continue;
114 
115 		for (row = 0; row < keypad->rows; row++) {
116 			if (!(changed & BIT(row)))
117 				continue;
118 
119 			pressed = row_state[col] & BIT(row);
120 
121 			dev_dbg(&keypad->input_dev->dev,
122 				"key %s, row: %d, col: %d\n",
123 				pressed ? "pressed" : "released", row, col);
124 
125 			val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
126 
127 			input_event(input_dev, EV_MSC, MSC_SCAN, val);
128 			input_report_key(input_dev,
129 					keypad->keycodes[val], pressed);
130 		}
131 		input_sync(keypad->input_dev);
132 	}
133 
134 	memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
135 
136 	return key_down;
137 }
138 
samsung_keypad_irq(int irq,void * dev_id)139 static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
140 {
141 	struct samsung_keypad *keypad = dev_id;
142 	unsigned int row_state[SAMSUNG_MAX_COLS];
143 	bool key_down;
144 
145 	pm_runtime_get_sync(&keypad->pdev->dev);
146 
147 	do {
148 		readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
149 		/* Clear interrupt. */
150 		writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
151 
152 		samsung_keypad_scan(keypad, row_state);
153 
154 		key_down = samsung_keypad_report(keypad, row_state);
155 		if (key_down)
156 			wait_event_timeout(keypad->wait, keypad->stopped,
157 					   msecs_to_jiffies(50));
158 
159 	} while (key_down && !keypad->stopped);
160 
161 	pm_runtime_put(&keypad->pdev->dev);
162 
163 	return IRQ_HANDLED;
164 }
165 
samsung_keypad_start(struct samsung_keypad * keypad)166 static void samsung_keypad_start(struct samsung_keypad *keypad)
167 {
168 	unsigned int val;
169 
170 	pm_runtime_get_sync(&keypad->pdev->dev);
171 
172 	/* Tell IRQ thread that it may poll the device. */
173 	keypad->stopped = false;
174 
175 	clk_enable(keypad->clk);
176 
177 	/* Enable interrupt bits. */
178 	val = readl(keypad->base + SAMSUNG_KEYIFCON);
179 	val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN;
180 	writel(val, keypad->base + SAMSUNG_KEYIFCON);
181 
182 	/* KEYIFCOL reg clear. */
183 	writel(0, keypad->base + SAMSUNG_KEYIFCOL);
184 
185 	pm_runtime_put(&keypad->pdev->dev);
186 }
187 
samsung_keypad_stop(struct samsung_keypad * keypad)188 static void samsung_keypad_stop(struct samsung_keypad *keypad)
189 {
190 	unsigned int val;
191 
192 	pm_runtime_get_sync(&keypad->pdev->dev);
193 
194 	/* Signal IRQ thread to stop polling and disable the handler. */
195 	keypad->stopped = true;
196 	wake_up(&keypad->wait);
197 	disable_irq(keypad->irq);
198 
199 	/* Clear interrupt. */
200 	writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
201 
202 	/* Disable interrupt bits. */
203 	val = readl(keypad->base + SAMSUNG_KEYIFCON);
204 	val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
205 	writel(val, keypad->base + SAMSUNG_KEYIFCON);
206 
207 	clk_disable(keypad->clk);
208 
209 	/*
210 	 * Now that chip should not generate interrupts we can safely
211 	 * re-enable the handler.
212 	 */
213 	enable_irq(keypad->irq);
214 
215 	pm_runtime_put(&keypad->pdev->dev);
216 }
217 
samsung_keypad_open(struct input_dev * input_dev)218 static int samsung_keypad_open(struct input_dev *input_dev)
219 {
220 	struct samsung_keypad *keypad = input_get_drvdata(input_dev);
221 
222 	samsung_keypad_start(keypad);
223 
224 	return 0;
225 }
226 
samsung_keypad_close(struct input_dev * input_dev)227 static void samsung_keypad_close(struct input_dev *input_dev)
228 {
229 	struct samsung_keypad *keypad = input_get_drvdata(input_dev);
230 
231 	samsung_keypad_stop(keypad);
232 }
233 
234 #ifdef CONFIG_OF
235 static struct samsung_keypad_platdata *
samsung_keypad_parse_dt(struct device * dev)236 samsung_keypad_parse_dt(struct device *dev)
237 {
238 	struct samsung_keypad_platdata *pdata;
239 	struct matrix_keymap_data *keymap_data;
240 	uint32_t *keymap, num_rows = 0, num_cols = 0;
241 	struct device_node *np = dev->of_node, *key_np;
242 	unsigned int key_count;
243 
244 	if (!np) {
245 		dev_err(dev, "missing device tree data\n");
246 		return ERR_PTR(-EINVAL);
247 	}
248 
249 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
250 	if (!pdata) {
251 		dev_err(dev, "could not allocate memory for platform data\n");
252 		return ERR_PTR(-ENOMEM);
253 	}
254 
255 	of_property_read_u32(np, "samsung,keypad-num-rows", &num_rows);
256 	of_property_read_u32(np, "samsung,keypad-num-columns", &num_cols);
257 	if (!num_rows || !num_cols) {
258 		dev_err(dev, "number of keypad rows/columns not specified\n");
259 		return ERR_PTR(-EINVAL);
260 	}
261 	pdata->rows = num_rows;
262 	pdata->cols = num_cols;
263 
264 	keymap_data = devm_kzalloc(dev, sizeof(*keymap_data), GFP_KERNEL);
265 	if (!keymap_data) {
266 		dev_err(dev, "could not allocate memory for keymap data\n");
267 		return ERR_PTR(-ENOMEM);
268 	}
269 	pdata->keymap_data = keymap_data;
270 
271 	key_count = of_get_child_count(np);
272 	keymap_data->keymap_size = key_count;
273 	keymap = devm_kcalloc(dev, key_count, sizeof(uint32_t), GFP_KERNEL);
274 	if (!keymap) {
275 		dev_err(dev, "could not allocate memory for keymap\n");
276 		return ERR_PTR(-ENOMEM);
277 	}
278 	keymap_data->keymap = keymap;
279 
280 	for_each_child_of_node(np, key_np) {
281 		u32 row, col, key_code;
282 		of_property_read_u32(key_np, "keypad,row", &row);
283 		of_property_read_u32(key_np, "keypad,column", &col);
284 		of_property_read_u32(key_np, "linux,code", &key_code);
285 		*keymap++ = KEY(row, col, key_code);
286 	}
287 
288 	pdata->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat");
289 
290 	pdata->wakeup = of_property_read_bool(np, "wakeup-source") ||
291 			/* legacy name */
292 			of_property_read_bool(np, "linux,input-wakeup");
293 
294 
295 	return pdata;
296 }
297 #else
298 static struct samsung_keypad_platdata *
samsung_keypad_parse_dt(struct device * dev)299 samsung_keypad_parse_dt(struct device *dev)
300 {
301 	dev_err(dev, "no platform data defined\n");
302 
303 	return ERR_PTR(-EINVAL);
304 }
305 #endif
306 
samsung_keypad_probe(struct platform_device * pdev)307 static int samsung_keypad_probe(struct platform_device *pdev)
308 {
309 	const struct samsung_keypad_platdata *pdata;
310 	const struct matrix_keymap_data *keymap_data;
311 	const struct platform_device_id *id;
312 	struct samsung_keypad *keypad;
313 	struct resource *res;
314 	struct input_dev *input_dev;
315 	unsigned int row_shift;
316 	int error;
317 
318 	pdata = dev_get_platdata(&pdev->dev);
319 	if (!pdata) {
320 		pdata = samsung_keypad_parse_dt(&pdev->dev);
321 		if (IS_ERR(pdata))
322 			return PTR_ERR(pdata);
323 	}
324 
325 	keymap_data = pdata->keymap_data;
326 	if (!keymap_data) {
327 		dev_err(&pdev->dev, "no keymap data defined\n");
328 		return -EINVAL;
329 	}
330 
331 	if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
332 		return -EINVAL;
333 
334 	if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
335 		return -EINVAL;
336 
337 	/* initialize the gpio */
338 	if (pdata->cfg_gpio)
339 		pdata->cfg_gpio(pdata->rows, pdata->cols);
340 
341 	row_shift = get_count_order(pdata->cols);
342 
343 	keypad = devm_kzalloc(&pdev->dev,
344 			      struct_size(keypad, keycodes,
345 					  pdata->rows << row_shift),
346 			      GFP_KERNEL);
347 	if (!keypad)
348 		return -ENOMEM;
349 
350 	input_dev = devm_input_allocate_device(&pdev->dev);
351 	if (!input_dev)
352 		return -ENOMEM;
353 
354 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
355 	if (!res)
356 		return -ENODEV;
357 
358 	keypad->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
359 	if (!keypad->base)
360 		return -EBUSY;
361 
362 	keypad->clk = devm_clk_get_prepared(&pdev->dev, "keypad");
363 	if (IS_ERR(keypad->clk)) {
364 		dev_err(&pdev->dev, "failed to get keypad clk\n");
365 		return PTR_ERR(keypad->clk);
366 	}
367 
368 	keypad->input_dev = input_dev;
369 	keypad->pdev = pdev;
370 	keypad->row_shift = row_shift;
371 	keypad->rows = pdata->rows;
372 	keypad->cols = pdata->cols;
373 	keypad->stopped = true;
374 	init_waitqueue_head(&keypad->wait);
375 
376 	keypad->chip = device_get_match_data(&pdev->dev);
377 	if (!keypad->chip) {
378 		id = platform_get_device_id(pdev);
379 		if (id)
380 			keypad->chip = (const void *)id->driver_data;
381 	}
382 
383 	if (!keypad->chip) {
384 		dev_err(&pdev->dev, "Unable to determine chip type");
385 		return -EINVAL;
386 	}
387 
388 	input_dev->name = pdev->name;
389 	input_dev->id.bustype = BUS_HOST;
390 
391 	input_dev->open = samsung_keypad_open;
392 	input_dev->close = samsung_keypad_close;
393 
394 	error = matrix_keypad_build_keymap(keymap_data, NULL,
395 					   pdata->rows, pdata->cols,
396 					   keypad->keycodes, input_dev);
397 	if (error) {
398 		dev_err(&pdev->dev, "failed to build keymap\n");
399 		return error;
400 	}
401 
402 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
403 	if (!pdata->no_autorepeat)
404 		__set_bit(EV_REP, input_dev->evbit);
405 
406 	input_set_drvdata(input_dev, keypad);
407 
408 	keypad->irq = platform_get_irq(pdev, 0);
409 	if (keypad->irq < 0) {
410 		error = keypad->irq;
411 		return error;
412 	}
413 
414 	error = devm_request_threaded_irq(&pdev->dev, keypad->irq, NULL,
415 					  samsung_keypad_irq, IRQF_ONESHOT,
416 					  dev_name(&pdev->dev), keypad);
417 	if (error) {
418 		dev_err(&pdev->dev, "failed to register keypad interrupt\n");
419 		return error;
420 	}
421 
422 	device_init_wakeup(&pdev->dev, pdata->wakeup);
423 	platform_set_drvdata(pdev, keypad);
424 
425 	error = devm_pm_runtime_enable(&pdev->dev);
426 	if (error)
427 		return error;
428 
429 	error = input_register_device(keypad->input_dev);
430 	if (error)
431 		return error;
432 
433 	if (pdev->dev.of_node) {
434 		devm_kfree(&pdev->dev, (void *)pdata->keymap_data->keymap);
435 		devm_kfree(&pdev->dev, (void *)pdata->keymap_data);
436 		devm_kfree(&pdev->dev, (void *)pdata);
437 	}
438 	return 0;
439 }
440 
samsung_keypad_runtime_suspend(struct device * dev)441 static int samsung_keypad_runtime_suspend(struct device *dev)
442 {
443 	struct platform_device *pdev = to_platform_device(dev);
444 	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
445 	unsigned int val;
446 	int error;
447 
448 	if (keypad->stopped)
449 		return 0;
450 
451 	/* This may fail on some SoCs due to lack of controller support */
452 	error = enable_irq_wake(keypad->irq);
453 	if (!error)
454 		keypad->wake_enabled = true;
455 
456 	val = readl(keypad->base + SAMSUNG_KEYIFCON);
457 	val |= SAMSUNG_KEYIFCON_WAKEUPEN;
458 	writel(val, keypad->base + SAMSUNG_KEYIFCON);
459 
460 	clk_disable(keypad->clk);
461 
462 	return 0;
463 }
464 
samsung_keypad_runtime_resume(struct device * dev)465 static int samsung_keypad_runtime_resume(struct device *dev)
466 {
467 	struct platform_device *pdev = to_platform_device(dev);
468 	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
469 	unsigned int val;
470 
471 	if (keypad->stopped)
472 		return 0;
473 
474 	clk_enable(keypad->clk);
475 
476 	val = readl(keypad->base + SAMSUNG_KEYIFCON);
477 	val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
478 	writel(val, keypad->base + SAMSUNG_KEYIFCON);
479 
480 	if (keypad->wake_enabled)
481 		disable_irq_wake(keypad->irq);
482 
483 	return 0;
484 }
485 
samsung_keypad_toggle_wakeup(struct samsung_keypad * keypad,bool enable)486 static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
487 					 bool enable)
488 {
489 	unsigned int val;
490 
491 	clk_enable(keypad->clk);
492 
493 	val = readl(keypad->base + SAMSUNG_KEYIFCON);
494 	if (enable) {
495 		val |= SAMSUNG_KEYIFCON_WAKEUPEN;
496 		if (device_may_wakeup(&keypad->pdev->dev))
497 			enable_irq_wake(keypad->irq);
498 	} else {
499 		val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
500 		if (device_may_wakeup(&keypad->pdev->dev))
501 			disable_irq_wake(keypad->irq);
502 	}
503 	writel(val, keypad->base + SAMSUNG_KEYIFCON);
504 
505 	clk_disable(keypad->clk);
506 }
507 
samsung_keypad_suspend(struct device * dev)508 static int samsung_keypad_suspend(struct device *dev)
509 {
510 	struct platform_device *pdev = to_platform_device(dev);
511 	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
512 	struct input_dev *input_dev = keypad->input_dev;
513 
514 	guard(mutex)(&input_dev->mutex);
515 
516 	if (input_device_enabled(input_dev))
517 		samsung_keypad_stop(keypad);
518 
519 	samsung_keypad_toggle_wakeup(keypad, true);
520 
521 	return 0;
522 }
523 
samsung_keypad_resume(struct device * dev)524 static int samsung_keypad_resume(struct device *dev)
525 {
526 	struct platform_device *pdev = to_platform_device(dev);
527 	struct samsung_keypad *keypad = platform_get_drvdata(pdev);
528 	struct input_dev *input_dev = keypad->input_dev;
529 
530 	guard(mutex)(&input_dev->mutex);
531 
532 	samsung_keypad_toggle_wakeup(keypad, false);
533 
534 	if (input_device_enabled(input_dev))
535 		samsung_keypad_start(keypad);
536 
537 	return 0;
538 }
539 
540 static const struct dev_pm_ops samsung_keypad_pm_ops = {
541 	SYSTEM_SLEEP_PM_OPS(samsung_keypad_suspend, samsung_keypad_resume)
542 	RUNTIME_PM_OPS(samsung_keypad_runtime_suspend,
543 		       samsung_keypad_runtime_resume, NULL)
544 };
545 
546 static const struct samsung_chip_info samsung_s3c6410_chip_info = {
547 	.column_shift = 0,
548 };
549 
550 static const struct samsung_chip_info samsung_s5pv210_chip_info = {
551 	.column_shift = 8,
552 };
553 
554 #ifdef CONFIG_OF
555 static const struct of_device_id samsung_keypad_dt_match[] = {
556 	{
557 		.compatible = "samsung,s3c6410-keypad",
558 		.data = &samsung_s3c6410_chip_info,
559 	}, {
560 		.compatible = "samsung,s5pv210-keypad",
561 		.data = &samsung_s5pv210_chip_info,
562 	},
563 	{ }
564 };
565 MODULE_DEVICE_TABLE(of, samsung_keypad_dt_match);
566 #endif
567 
568 static const struct platform_device_id samsung_keypad_driver_ids[] = {
569 	{
570 		.name		= "samsung-keypad",
571 		.driver_data	= (kernel_ulong_t)&samsung_s3c6410_chip_info,
572 	}, {
573 		.name		= "s5pv210-keypad",
574 		.driver_data	= (kernel_ulong_t)&samsung_s5pv210_chip_info,
575 	},
576 	{ }
577 };
578 MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
579 
580 static struct platform_driver samsung_keypad_driver = {
581 	.probe		= samsung_keypad_probe,
582 	.driver		= {
583 		.name	= "samsung-keypad",
584 		.of_match_table = of_match_ptr(samsung_keypad_dt_match),
585 		.pm	= pm_ptr(&samsung_keypad_pm_ops),
586 	},
587 	.id_table	= samsung_keypad_driver_ids,
588 };
589 module_platform_driver(samsung_keypad_driver);
590 
591 MODULE_DESCRIPTION("Samsung keypad driver");
592 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
593 MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
594 MODULE_LICENSE("GPL");
595