1 /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/input.h>
19 #include <linux/bitops.h>
20 #include <linux/delay.h>
21 #include <linux/mutex.h>
22 #include <linux/regmap.h>
24 #include <linux/input/matrix_keypad.h>
26 #define PM8XXX_MAX_ROWS 18
27 #define PM8XXX_MAX_COLS 8
28 #define PM8XXX_ROW_SHIFT 3
29 #define PM8XXX_MATRIX_MAX_SIZE (PM8XXX_MAX_ROWS * PM8XXX_MAX_COLS)
31 #define PM8XXX_MIN_ROWS 5
32 #define PM8XXX_MIN_COLS 5
34 #define MAX_SCAN_DELAY 128
35 #define MIN_SCAN_DELAY 1
38 #define MAX_ROW_HOLD_DELAY 122000
39 #define MIN_ROW_HOLD_DELAY 30500
41 #define MAX_DEBOUNCE_TIME 20
42 #define MIN_DEBOUNCE_TIME 5
44 #define KEYP_CTRL 0x148
46 #define KEYP_CTRL_EVNTS BIT(0)
47 #define KEYP_CTRL_EVNTS_MASK 0x3
49 #define KEYP_CTRL_SCAN_COLS_SHIFT 5
50 #define KEYP_CTRL_SCAN_COLS_MIN 5
51 #define KEYP_CTRL_SCAN_COLS_BITS 0x3
53 #define KEYP_CTRL_SCAN_ROWS_SHIFT 2
54 #define KEYP_CTRL_SCAN_ROWS_MIN 5
55 #define KEYP_CTRL_SCAN_ROWS_BITS 0x7
57 #define KEYP_CTRL_KEYP_EN BIT(7)
59 #define KEYP_SCAN 0x149
61 #define KEYP_SCAN_READ_STATE BIT(0)
62 #define KEYP_SCAN_DBOUNCE_SHIFT 1
63 #define KEYP_SCAN_PAUSE_SHIFT 3
64 #define KEYP_SCAN_ROW_HOLD_SHIFT 6
66 #define KEYP_TEST 0x14A
68 #define KEYP_TEST_CLEAR_RECENT_SCAN BIT(6)
69 #define KEYP_TEST_CLEAR_OLD_SCAN BIT(5)
70 #define KEYP_TEST_READ_RESET BIT(4)
71 #define KEYP_TEST_DTEST_EN BIT(3)
72 #define KEYP_TEST_ABORT_READ BIT(0)
74 #define KEYP_TEST_DBG_SELECT_SHIFT 1
76 /* bits of these registers represent
80 #define KEYP_RECENT_DATA 0x14B
81 #define KEYP_OLD_DATA 0x14C
83 #define KEYP_CLOCK_FREQ 32768
86 * struct pmic8xxx_kp - internal keypad data structure
87 * @num_cols - number of columns of keypad
88 * @num_rows - number of row of keypad
89 * @input - input device pointer for keypad
90 * @regmap - regmap handle
91 * @key_sense_irq - key press/release irq number
92 * @key_stuck_irq - key stuck notification irq number
93 * @keycodes - array to hold the key codes
94 * @dev - parent device pointer
95 * @keystate - present key press/release state
96 * @stuckstate - present state when key stuck irq
97 * @ctrl_reg - control register value
100 unsigned int num_rows
;
101 unsigned int num_cols
;
102 struct input_dev
*input
;
103 struct regmap
*regmap
;
107 unsigned short keycodes
[PM8XXX_MATRIX_MAX_SIZE
];
110 u16 keystate
[PM8XXX_MAX_ROWS
];
111 u16 stuckstate
[PM8XXX_MAX_ROWS
];
116 static u8
pmic8xxx_col_state(struct pmic8xxx_kp
*kp
, u8 col
)
118 /* all keys pressed on that particular row? */
120 return 1 << kp
->num_cols
;
122 return col
& ((1 << kp
->num_cols
) - 1);
126 * Synchronous read protocol for RevB0 onwards:
128 * 1. Write '1' to ReadState bit in KEYP_SCAN register
129 * 2. Wait 2*32KHz clocks, so that HW can successfully enter read mode
131 * 3. Read rows in old array first if events are more than one
132 * 4. Read rows in recent array
133 * 5. Wait 4*32KHz clocks
134 * 6. Write '0' to ReadState bit of KEYP_SCAN register so that hw can
135 * synchronously exit read mode.
137 static int pmic8xxx_chk_sync_read(struct pmic8xxx_kp
*kp
)
140 unsigned int scan_val
;
142 rc
= regmap_read(kp
->regmap
, KEYP_SCAN
, &scan_val
);
144 dev_err(kp
->dev
, "Error reading KEYP_SCAN reg, rc=%d\n", rc
);
150 rc
= regmap_write(kp
->regmap
, KEYP_SCAN
, scan_val
);
152 dev_err(kp
->dev
, "Error writing KEYP_SCAN reg, rc=%d\n", rc
);
156 /* 2 * 32KHz clocks */
157 udelay((2 * DIV_ROUND_UP(USEC_PER_SEC
, KEYP_CLOCK_FREQ
)) + 1);
162 static int pmic8xxx_kp_read_data(struct pmic8xxx_kp
*kp
, u16
*state
,
163 u16 data_reg
, int read_rows
)
168 for (row
= 0; row
< read_rows
; row
++) {
169 rc
= regmap_read(kp
->regmap
, data_reg
, &val
);
172 dev_dbg(kp
->dev
, "%d = %d\n", row
, val
);
173 state
[row
] = pmic8xxx_col_state(kp
, val
);
179 static int pmic8xxx_kp_read_matrix(struct pmic8xxx_kp
*kp
, u16
*new_state
,
183 unsigned int scan_val
;
185 if (kp
->num_rows
< PM8XXX_MIN_ROWS
)
186 read_rows
= PM8XXX_MIN_ROWS
;
188 read_rows
= kp
->num_rows
;
190 pmic8xxx_chk_sync_read(kp
);
193 rc
= pmic8xxx_kp_read_data(kp
, old_state
, KEYP_OLD_DATA
,
197 "Error reading KEYP_OLD_DATA, rc=%d\n", rc
);
202 rc
= pmic8xxx_kp_read_data(kp
, new_state
, KEYP_RECENT_DATA
,
206 "Error reading KEYP_RECENT_DATA, rc=%d\n", rc
);
210 /* 4 * 32KHz clocks */
211 udelay((4 * DIV_ROUND_UP(USEC_PER_SEC
, KEYP_CLOCK_FREQ
)) + 1);
213 rc
= regmap_read(kp
->regmap
, KEYP_SCAN
, &scan_val
);
215 dev_err(kp
->dev
, "Error reading KEYP_SCAN reg, rc=%d\n", rc
);
220 rc
= regmap_write(kp
->regmap
, KEYP_SCAN
, scan_val
);
222 dev_err(kp
->dev
, "Error writing KEYP_SCAN reg, rc=%d\n", rc
);
227 static void __pmic8xxx_kp_scan_matrix(struct pmic8xxx_kp
*kp
, u16
*new_state
,
232 for (row
= 0; row
< kp
->num_rows
; row
++) {
233 int bits_changed
= new_state
[row
] ^ old_state
[row
];
238 for (col
= 0; col
< kp
->num_cols
; col
++) {
239 if (!(bits_changed
& (1 << col
)))
242 dev_dbg(kp
->dev
, "key [%d:%d] %s\n", row
, col
,
243 !(new_state
[row
] & (1 << col
)) ?
244 "pressed" : "released");
246 code
= MATRIX_SCAN_CODE(row
, col
, PM8XXX_ROW_SHIFT
);
248 input_event(kp
->input
, EV_MSC
, MSC_SCAN
, code
);
249 input_report_key(kp
->input
,
251 !(new_state
[row
] & (1 << col
)));
253 input_sync(kp
->input
);
258 static bool pmic8xxx_detect_ghost_keys(struct pmic8xxx_kp
*kp
, u16
*new_state
)
260 int row
, found_first
= -1;
261 u16 check
, row_state
;
264 for (row
= 0; row
< kp
->num_rows
; row
++) {
265 row_state
= (~new_state
[row
]) &
266 ((1 << kp
->num_cols
) - 1);
268 if (hweight16(row_state
) > 1) {
269 if (found_first
== -1)
271 if (check
& row_state
) {
272 dev_dbg(kp
->dev
, "detected ghost key on row[%d]"
273 " and row[%d]\n", found_first
, row
);
282 static int pmic8xxx_kp_scan_matrix(struct pmic8xxx_kp
*kp
, unsigned int events
)
284 u16 new_state
[PM8XXX_MAX_ROWS
];
285 u16 old_state
[PM8XXX_MAX_ROWS
];
290 rc
= pmic8xxx_kp_read_matrix(kp
, new_state
, NULL
);
294 /* detecting ghost key is not an error */
295 if (pmic8xxx_detect_ghost_keys(kp
, new_state
))
297 __pmic8xxx_kp_scan_matrix(kp
, new_state
, kp
->keystate
);
298 memcpy(kp
->keystate
, new_state
, sizeof(new_state
));
300 case 0x3: /* two events - eventcounter is gray-coded */
301 rc
= pmic8xxx_kp_read_matrix(kp
, new_state
, old_state
);
305 __pmic8xxx_kp_scan_matrix(kp
, old_state
, kp
->keystate
);
306 __pmic8xxx_kp_scan_matrix(kp
, new_state
, old_state
);
307 memcpy(kp
->keystate
, new_state
, sizeof(new_state
));
310 dev_dbg(kp
->dev
, "Some key events were lost\n");
311 rc
= pmic8xxx_kp_read_matrix(kp
, new_state
, old_state
);
314 __pmic8xxx_kp_scan_matrix(kp
, old_state
, kp
->keystate
);
315 __pmic8xxx_kp_scan_matrix(kp
, new_state
, old_state
);
316 memcpy(kp
->keystate
, new_state
, sizeof(new_state
));
325 * NOTE: We are reading recent and old data registers blindly
326 * whenever key-stuck interrupt happens, because events counter doesn't
327 * get updated when this interrupt happens due to key stuck doesn't get
328 * considered as key state change.
330 * We are not using old data register contents after they are being read
331 * because it might report the key which was pressed before the key being stuck
332 * as stuck key because it's pressed status is stored in the old data
335 static irqreturn_t
pmic8xxx_kp_stuck_irq(int irq
, void *data
)
337 u16 new_state
[PM8XXX_MAX_ROWS
];
338 u16 old_state
[PM8XXX_MAX_ROWS
];
340 struct pmic8xxx_kp
*kp
= data
;
342 rc
= pmic8xxx_kp_read_matrix(kp
, new_state
, old_state
);
344 dev_err(kp
->dev
, "failed to read keypad matrix\n");
348 __pmic8xxx_kp_scan_matrix(kp
, new_state
, kp
->stuckstate
);
353 static irqreturn_t
pmic8xxx_kp_irq(int irq
, void *data
)
355 struct pmic8xxx_kp
*kp
= data
;
356 unsigned int ctrl_val
, events
;
359 rc
= regmap_read(kp
->regmap
, KEYP_CTRL
, &ctrl_val
);
361 dev_err(kp
->dev
, "failed to read keyp_ctrl register\n");
365 events
= ctrl_val
& KEYP_CTRL_EVNTS_MASK
;
367 rc
= pmic8xxx_kp_scan_matrix(kp
, events
);
369 dev_err(kp
->dev
, "failed to scan matrix\n");
374 static int pmic8xxx_kpd_init(struct pmic8xxx_kp
*kp
,
375 struct platform_device
*pdev
)
377 const struct device_node
*of_node
= pdev
->dev
.of_node
;
378 unsigned int scan_delay_ms
;
379 unsigned int row_hold_ns
;
380 unsigned int debounce_ms
;
381 int bits
, rc
, cycles
;
382 u8 scan_val
= 0, ctrl_val
= 0;
383 static const u8 row_bits
[] = {
384 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7,
387 /* Find column bits */
388 if (kp
->num_cols
< KEYP_CTRL_SCAN_COLS_MIN
)
391 bits
= kp
->num_cols
- KEYP_CTRL_SCAN_COLS_MIN
;
392 ctrl_val
= (bits
& KEYP_CTRL_SCAN_COLS_BITS
) <<
393 KEYP_CTRL_SCAN_COLS_SHIFT
;
396 if (kp
->num_rows
< KEYP_CTRL_SCAN_ROWS_MIN
)
399 bits
= row_bits
[kp
->num_rows
- KEYP_CTRL_SCAN_ROWS_MIN
];
401 ctrl_val
|= (bits
<< KEYP_CTRL_SCAN_ROWS_SHIFT
);
403 rc
= regmap_write(kp
->regmap
, KEYP_CTRL
, ctrl_val
);
405 dev_err(kp
->dev
, "Error writing KEYP_CTRL reg, rc=%d\n", rc
);
409 if (of_property_read_u32(of_node
, "scan-delay", &scan_delay_ms
))
410 scan_delay_ms
= MIN_SCAN_DELAY
;
412 if (scan_delay_ms
> MAX_SCAN_DELAY
|| scan_delay_ms
< MIN_SCAN_DELAY
||
413 !is_power_of_2(scan_delay_ms
)) {
414 dev_err(&pdev
->dev
, "invalid keypad scan time supplied\n");
418 if (of_property_read_u32(of_node
, "row-hold", &row_hold_ns
))
419 row_hold_ns
= MIN_ROW_HOLD_DELAY
;
421 if (row_hold_ns
> MAX_ROW_HOLD_DELAY
||
422 row_hold_ns
< MIN_ROW_HOLD_DELAY
||
423 ((row_hold_ns
% MIN_ROW_HOLD_DELAY
) != 0)) {
424 dev_err(&pdev
->dev
, "invalid keypad row hold time supplied\n");
428 if (of_property_read_u32(of_node
, "debounce", &debounce_ms
))
429 debounce_ms
= MIN_DEBOUNCE_TIME
;
431 if (((debounce_ms
% 5) != 0) ||
432 debounce_ms
> MAX_DEBOUNCE_TIME
||
433 debounce_ms
< MIN_DEBOUNCE_TIME
) {
434 dev_err(&pdev
->dev
, "invalid debounce time supplied\n");
438 bits
= (debounce_ms
/ 5) - 1;
440 scan_val
|= (bits
<< KEYP_SCAN_DBOUNCE_SHIFT
);
442 bits
= fls(scan_delay_ms
) - 1;
443 scan_val
|= (bits
<< KEYP_SCAN_PAUSE_SHIFT
);
445 /* Row hold time is a multiple of 32KHz cycles. */
446 cycles
= (row_hold_ns
* KEYP_CLOCK_FREQ
) / NSEC_PER_SEC
;
448 scan_val
|= (cycles
<< KEYP_SCAN_ROW_HOLD_SHIFT
);
450 rc
= regmap_write(kp
->regmap
, KEYP_SCAN
, scan_val
);
452 dev_err(kp
->dev
, "Error writing KEYP_SCAN reg, rc=%d\n", rc
);
458 static int pmic8xxx_kp_enable(struct pmic8xxx_kp
*kp
)
462 kp
->ctrl_reg
|= KEYP_CTRL_KEYP_EN
;
464 rc
= regmap_write(kp
->regmap
, KEYP_CTRL
, kp
->ctrl_reg
);
466 dev_err(kp
->dev
, "Error writing KEYP_CTRL reg, rc=%d\n", rc
);
471 static int pmic8xxx_kp_disable(struct pmic8xxx_kp
*kp
)
475 kp
->ctrl_reg
&= ~KEYP_CTRL_KEYP_EN
;
477 rc
= regmap_write(kp
->regmap
, KEYP_CTRL
, kp
->ctrl_reg
);
484 static int pmic8xxx_kp_open(struct input_dev
*dev
)
486 struct pmic8xxx_kp
*kp
= input_get_drvdata(dev
);
488 return pmic8xxx_kp_enable(kp
);
491 static void pmic8xxx_kp_close(struct input_dev
*dev
)
493 struct pmic8xxx_kp
*kp
= input_get_drvdata(dev
);
495 pmic8xxx_kp_disable(kp
);
499 * keypad controller should be initialized in the following sequence
500 * only, otherwise it might get into FSM stuck state.
502 * - Initialize keypad control parameters, like no. of rows, columns,
503 * timing values etc.,
504 * - configure rows and column gpios pull up/down.
505 * - set irq edge type.
506 * - enable the keypad controller.
508 static int pmic8xxx_kp_probe(struct platform_device
*pdev
)
510 unsigned int rows
, cols
;
513 struct pmic8xxx_kp
*kp
;
515 unsigned int ctrl_val
;
517 rc
= matrix_keypad_parse_of_params(&pdev
->dev
, &rows
, &cols
);
521 if (cols
> PM8XXX_MAX_COLS
|| rows
> PM8XXX_MAX_ROWS
||
522 cols
< PM8XXX_MIN_COLS
) {
523 dev_err(&pdev
->dev
, "invalid platform data\n");
527 repeat
= !of_property_read_bool(pdev
->dev
.of_node
,
528 "linux,input-no-autorepeat");
529 wakeup
= of_property_read_bool(pdev
->dev
.of_node
,
530 "linux,keypad-wakeup");
532 kp
= devm_kzalloc(&pdev
->dev
, sizeof(*kp
), GFP_KERNEL
);
536 kp
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
540 platform_set_drvdata(pdev
, kp
);
544 kp
->dev
= &pdev
->dev
;
546 kp
->input
= devm_input_allocate_device(&pdev
->dev
);
548 dev_err(&pdev
->dev
, "unable to allocate input device\n");
552 kp
->key_sense_irq
= platform_get_irq(pdev
, 0);
553 if (kp
->key_sense_irq
< 0) {
554 dev_err(&pdev
->dev
, "unable to get keypad sense irq\n");
555 return kp
->key_sense_irq
;
558 kp
->key_stuck_irq
= platform_get_irq(pdev
, 1);
559 if (kp
->key_stuck_irq
< 0) {
560 dev_err(&pdev
->dev
, "unable to get keypad stuck irq\n");
561 return kp
->key_stuck_irq
;
564 kp
->input
->name
= "PMIC8XXX keypad";
565 kp
->input
->phys
= "pmic8xxx_keypad/input0";
567 kp
->input
->id
.bustype
= BUS_I2C
;
568 kp
->input
->id
.version
= 0x0001;
569 kp
->input
->id
.product
= 0x0001;
570 kp
->input
->id
.vendor
= 0x0001;
572 kp
->input
->open
= pmic8xxx_kp_open
;
573 kp
->input
->close
= pmic8xxx_kp_close
;
575 rc
= matrix_keypad_build_keymap(NULL
, NULL
,
576 PM8XXX_MAX_ROWS
, PM8XXX_MAX_COLS
,
577 kp
->keycodes
, kp
->input
);
579 dev_err(&pdev
->dev
, "failed to build keymap\n");
584 __set_bit(EV_REP
, kp
->input
->evbit
);
585 input_set_capability(kp
->input
, EV_MSC
, MSC_SCAN
);
587 input_set_drvdata(kp
->input
, kp
);
589 /* initialize keypad state */
590 memset(kp
->keystate
, 0xff, sizeof(kp
->keystate
));
591 memset(kp
->stuckstate
, 0xff, sizeof(kp
->stuckstate
));
593 rc
= pmic8xxx_kpd_init(kp
, pdev
);
595 dev_err(&pdev
->dev
, "unable to initialize keypad controller\n");
599 rc
= devm_request_any_context_irq(&pdev
->dev
, kp
->key_sense_irq
,
600 pmic8xxx_kp_irq
, IRQF_TRIGGER_RISING
, "pmic-keypad",
603 dev_err(&pdev
->dev
, "failed to request keypad sense irq\n");
607 rc
= devm_request_any_context_irq(&pdev
->dev
, kp
->key_stuck_irq
,
608 pmic8xxx_kp_stuck_irq
, IRQF_TRIGGER_RISING
,
609 "pmic-keypad-stuck", kp
);
611 dev_err(&pdev
->dev
, "failed to request keypad stuck irq\n");
615 rc
= regmap_read(kp
->regmap
, KEYP_CTRL
, &ctrl_val
);
617 dev_err(&pdev
->dev
, "failed to read KEYP_CTRL register\n");
621 kp
->ctrl_reg
= ctrl_val
;
623 rc
= input_register_device(kp
->input
);
625 dev_err(&pdev
->dev
, "unable to register keypad input device\n");
629 device_init_wakeup(&pdev
->dev
, wakeup
);
634 #ifdef CONFIG_PM_SLEEP
635 static int pmic8xxx_kp_suspend(struct device
*dev
)
637 struct platform_device
*pdev
= to_platform_device(dev
);
638 struct pmic8xxx_kp
*kp
= platform_get_drvdata(pdev
);
639 struct input_dev
*input_dev
= kp
->input
;
641 if (device_may_wakeup(dev
)) {
642 enable_irq_wake(kp
->key_sense_irq
);
644 mutex_lock(&input_dev
->mutex
);
646 if (input_dev
->users
)
647 pmic8xxx_kp_disable(kp
);
649 mutex_unlock(&input_dev
->mutex
);
655 static int pmic8xxx_kp_resume(struct device
*dev
)
657 struct platform_device
*pdev
= to_platform_device(dev
);
658 struct pmic8xxx_kp
*kp
= platform_get_drvdata(pdev
);
659 struct input_dev
*input_dev
= kp
->input
;
661 if (device_may_wakeup(dev
)) {
662 disable_irq_wake(kp
->key_sense_irq
);
664 mutex_lock(&input_dev
->mutex
);
666 if (input_dev
->users
)
667 pmic8xxx_kp_enable(kp
);
669 mutex_unlock(&input_dev
->mutex
);
676 static SIMPLE_DEV_PM_OPS(pm8xxx_kp_pm_ops
,
677 pmic8xxx_kp_suspend
, pmic8xxx_kp_resume
);
679 static const struct of_device_id pm8xxx_match_table
[] = {
680 { .compatible
= "qcom,pm8058-keypad" },
681 { .compatible
= "qcom,pm8921-keypad" },
684 MODULE_DEVICE_TABLE(of
, pm8xxx_match_table
);
686 static struct platform_driver pmic8xxx_kp_driver
= {
687 .probe
= pmic8xxx_kp_probe
,
689 .name
= "pm8xxx-keypad",
690 .pm
= &pm8xxx_kp_pm_ops
,
691 .of_match_table
= pm8xxx_match_table
,
694 module_platform_driver(pmic8xxx_kp_driver
);
696 MODULE_LICENSE("GPL v2");
697 MODULE_DESCRIPTION("PMIC8XXX keypad driver");
698 MODULE_VERSION("1.0");
699 MODULE_ALIAS("platform:pmic8xxx_keypad");
700 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");