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