xref: /linux/drivers/input/keyboard/pmic8xxx-keypad.c (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
3  */
4 
5 #include <linux/module.h>
6 #include <linux/platform_device.h>
7 #include <linux/kernel.h>
8 #include <linux/interrupt.h>
9 #include <linux/slab.h>
10 #include <linux/input.h>
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/regmap.h>
15 #include <linux/of.h>
16 #include <linux/input/matrix_keypad.h>
17 
18 #define PM8XXX_MAX_ROWS		18
19 #define PM8XXX_MAX_COLS		8
20 #define PM8XXX_ROW_SHIFT	3
21 #define PM8XXX_MATRIX_MAX_SIZE	(PM8XXX_MAX_ROWS * PM8XXX_MAX_COLS)
22 
23 #define PM8XXX_MIN_ROWS		5
24 #define PM8XXX_MIN_COLS		5
25 
26 #define MAX_SCAN_DELAY		128
27 #define MIN_SCAN_DELAY		1
28 
29 /* in nanoseconds */
30 #define MAX_ROW_HOLD_DELAY	122000
31 #define MIN_ROW_HOLD_DELAY	30500
32 
33 #define MAX_DEBOUNCE_TIME	20
34 #define MIN_DEBOUNCE_TIME	5
35 
36 #define KEYP_CTRL			0x148
37 
38 #define KEYP_CTRL_EVNTS			BIT(0)
39 #define KEYP_CTRL_EVNTS_MASK		0x3
40 
41 #define KEYP_CTRL_SCAN_COLS_SHIFT	5
42 #define KEYP_CTRL_SCAN_COLS_MIN		5
43 #define KEYP_CTRL_SCAN_COLS_BITS	0x3
44 
45 #define KEYP_CTRL_SCAN_ROWS_SHIFT	2
46 #define KEYP_CTRL_SCAN_ROWS_MIN		5
47 #define KEYP_CTRL_SCAN_ROWS_BITS	0x7
48 
49 #define KEYP_CTRL_KEYP_EN		BIT(7)
50 
51 #define KEYP_SCAN			0x149
52 
53 #define KEYP_SCAN_READ_STATE		BIT(0)
54 #define KEYP_SCAN_DBOUNCE_SHIFT		1
55 #define KEYP_SCAN_PAUSE_SHIFT		3
56 #define KEYP_SCAN_ROW_HOLD_SHIFT	6
57 
58 #define KEYP_TEST			0x14A
59 
60 #define KEYP_TEST_CLEAR_RECENT_SCAN	BIT(6)
61 #define KEYP_TEST_CLEAR_OLD_SCAN	BIT(5)
62 #define KEYP_TEST_READ_RESET		BIT(4)
63 #define KEYP_TEST_DTEST_EN		BIT(3)
64 #define KEYP_TEST_ABORT_READ		BIT(0)
65 
66 #define KEYP_TEST_DBG_SELECT_SHIFT	1
67 
68 /* bits of these registers represent
69  * '0' for key press
70  * '1' for key release
71  */
72 #define KEYP_RECENT_DATA		0x14B
73 #define KEYP_OLD_DATA			0x14C
74 
75 #define KEYP_CLOCK_FREQ			32768
76 
77 /**
78  * struct pmic8xxx_kp - internal keypad data structure
79  * @num_cols: number of columns of keypad
80  * @num_rows: number of row of keypad
81  * @input: input device pointer for keypad
82  * @regmap: regmap handle
83  * @key_sense_irq: key press/release irq number
84  * @key_stuck_irq: key stuck notification irq number
85  * @keycodes: array to hold the key codes
86  * @dev: parent device pointer
87  * @keystate: present key press/release state
88  * @stuckstate: present state when key stuck irq
89  * @ctrl_reg: control register value
90  */
91 struct pmic8xxx_kp {
92 	unsigned int num_rows;
93 	unsigned int num_cols;
94 	struct input_dev *input;
95 	struct regmap *regmap;
96 	int key_sense_irq;
97 	int key_stuck_irq;
98 
99 	unsigned short keycodes[PM8XXX_MATRIX_MAX_SIZE];
100 
101 	struct device *dev;
102 	u16 keystate[PM8XXX_MAX_ROWS];
103 	u16 stuckstate[PM8XXX_MAX_ROWS];
104 
105 	u8 ctrl_reg;
106 };
107 
108 static u8 pmic8xxx_col_state(struct pmic8xxx_kp *kp, u8 col)
109 {
110 	/* all keys pressed on that particular row? */
111 	if (col == 0x00)
112 		return 1 << kp->num_cols;
113 	else
114 		return col & ((1 << kp->num_cols) - 1);
115 }
116 
117 /*
118  * Synchronous read protocol for RevB0 onwards:
119  *
120  * 1. Write '1' to ReadState bit in KEYP_SCAN register
121  * 2. Wait 2*32KHz clocks, so that HW can successfully enter read mode
122  *    synchronously
123  * 3. Read rows in old array first if events are more than one
124  * 4. Read rows in recent array
125  * 5. Wait 4*32KHz clocks
126  * 6. Write '0' to ReadState bit of KEYP_SCAN register so that hw can
127  *    synchronously exit read mode.
128  */
129 static int pmic8xxx_chk_sync_read(struct pmic8xxx_kp *kp)
130 {
131 	int rc;
132 	unsigned int scan_val;
133 
134 	rc = regmap_read(kp->regmap, KEYP_SCAN, &scan_val);
135 	if (rc < 0) {
136 		dev_err(kp->dev, "Error reading KEYP_SCAN reg, rc=%d\n", rc);
137 		return rc;
138 	}
139 
140 	scan_val |= 0x1;
141 
142 	rc = regmap_write(kp->regmap, KEYP_SCAN, scan_val);
143 	if (rc < 0) {
144 		dev_err(kp->dev, "Error writing KEYP_SCAN reg, rc=%d\n", rc);
145 		return rc;
146 	}
147 
148 	/* 2 * 32KHz clocks */
149 	udelay((2 * DIV_ROUND_UP(USEC_PER_SEC, KEYP_CLOCK_FREQ)) + 1);
150 
151 	return rc;
152 }
153 
154 static int pmic8xxx_kp_read_data(struct pmic8xxx_kp *kp, u16 *state,
155 					u16 data_reg, int read_rows)
156 {
157 	int rc, row;
158 	unsigned int val;
159 
160 	for (row = 0; row < read_rows; row++) {
161 		rc = regmap_read(kp->regmap, data_reg, &val);
162 		if (rc)
163 			return rc;
164 		dev_dbg(kp->dev, "%d = %d\n", row, val);
165 		state[row] = pmic8xxx_col_state(kp, val);
166 	}
167 
168 	return 0;
169 }
170 
171 static int pmic8xxx_kp_read_matrix(struct pmic8xxx_kp *kp, u16 *new_state,
172 					 u16 *old_state)
173 {
174 	int rc, read_rows;
175 	unsigned int scan_val;
176 
177 	if (kp->num_rows < PM8XXX_MIN_ROWS)
178 		read_rows = PM8XXX_MIN_ROWS;
179 	else
180 		read_rows = kp->num_rows;
181 
182 	pmic8xxx_chk_sync_read(kp);
183 
184 	if (old_state) {
185 		rc = pmic8xxx_kp_read_data(kp, old_state, KEYP_OLD_DATA,
186 						read_rows);
187 		if (rc < 0) {
188 			dev_err(kp->dev,
189 				"Error reading KEYP_OLD_DATA, rc=%d\n", rc);
190 			return rc;
191 		}
192 	}
193 
194 	rc = pmic8xxx_kp_read_data(kp, new_state, KEYP_RECENT_DATA,
195 					 read_rows);
196 	if (rc < 0) {
197 		dev_err(kp->dev,
198 			"Error reading KEYP_RECENT_DATA, rc=%d\n", rc);
199 		return rc;
200 	}
201 
202 	/* 4 * 32KHz clocks */
203 	udelay((4 * DIV_ROUND_UP(USEC_PER_SEC, KEYP_CLOCK_FREQ)) + 1);
204 
205 	rc = regmap_read(kp->regmap, KEYP_SCAN, &scan_val);
206 	if (rc < 0) {
207 		dev_err(kp->dev, "Error reading KEYP_SCAN reg, rc=%d\n", rc);
208 		return rc;
209 	}
210 
211 	scan_val &= 0xFE;
212 	rc = regmap_write(kp->regmap, KEYP_SCAN, scan_val);
213 	if (rc < 0)
214 		dev_err(kp->dev, "Error writing KEYP_SCAN reg, rc=%d\n", rc);
215 
216 	return rc;
217 }
218 
219 static void __pmic8xxx_kp_scan_matrix(struct pmic8xxx_kp *kp, u16 *new_state,
220 					 u16 *old_state)
221 {
222 	int row, col, code;
223 
224 	for (row = 0; row < kp->num_rows; row++) {
225 		int bits_changed = new_state[row] ^ old_state[row];
226 
227 		if (!bits_changed)
228 			continue;
229 
230 		for (col = 0; col < kp->num_cols; col++) {
231 			if (!(bits_changed & (1 << col)))
232 				continue;
233 
234 			dev_dbg(kp->dev, "key [%d:%d] %s\n", row, col,
235 					!(new_state[row] & (1 << col)) ?
236 					"pressed" : "released");
237 
238 			code = MATRIX_SCAN_CODE(row, col, PM8XXX_ROW_SHIFT);
239 
240 			input_event(kp->input, EV_MSC, MSC_SCAN, code);
241 			input_report_key(kp->input,
242 					kp->keycodes[code],
243 					!(new_state[row] & (1 << col)));
244 
245 			input_sync(kp->input);
246 		}
247 	}
248 }
249 
250 static bool pmic8xxx_detect_ghost_keys(struct pmic8xxx_kp *kp, u16 *new_state)
251 {
252 	int row, found_first = -1;
253 	u16 check, row_state;
254 
255 	check = 0;
256 	for (row = 0; row < kp->num_rows; row++) {
257 		row_state = (~new_state[row]) &
258 				 ((1 << kp->num_cols) - 1);
259 
260 		if (hweight16(row_state) > 1) {
261 			if (found_first == -1)
262 				found_first = row;
263 			if (check & row_state) {
264 				dev_dbg(kp->dev, "detected ghost key on row[%d]"
265 					 " and row[%d]\n", found_first, row);
266 				return true;
267 			}
268 		}
269 		check |= row_state;
270 	}
271 	return false;
272 }
273 
274 static int pmic8xxx_kp_scan_matrix(struct pmic8xxx_kp *kp, unsigned int events)
275 {
276 	u16 new_state[PM8XXX_MAX_ROWS];
277 	u16 old_state[PM8XXX_MAX_ROWS];
278 	int rc;
279 
280 	switch (events) {
281 	case 0x1:
282 		rc = pmic8xxx_kp_read_matrix(kp, new_state, NULL);
283 		if (rc < 0)
284 			return rc;
285 
286 		/* detecting ghost key is not an error */
287 		if (pmic8xxx_detect_ghost_keys(kp, new_state))
288 			return 0;
289 		__pmic8xxx_kp_scan_matrix(kp, new_state, kp->keystate);
290 		memcpy(kp->keystate, new_state, sizeof(new_state));
291 	break;
292 	case 0x3: /* two events - eventcounter is gray-coded */
293 		rc = pmic8xxx_kp_read_matrix(kp, new_state, old_state);
294 		if (rc < 0)
295 			return rc;
296 
297 		__pmic8xxx_kp_scan_matrix(kp, old_state, kp->keystate);
298 		__pmic8xxx_kp_scan_matrix(kp, new_state, old_state);
299 		memcpy(kp->keystate, new_state, sizeof(new_state));
300 	break;
301 	case 0x2:
302 		dev_dbg(kp->dev, "Some key events were lost\n");
303 		rc = pmic8xxx_kp_read_matrix(kp, new_state, old_state);
304 		if (rc < 0)
305 			return rc;
306 		__pmic8xxx_kp_scan_matrix(kp, old_state, kp->keystate);
307 		__pmic8xxx_kp_scan_matrix(kp, new_state, old_state);
308 		memcpy(kp->keystate, new_state, sizeof(new_state));
309 	break;
310 	default:
311 		rc = -EINVAL;
312 	}
313 	return rc;
314 }
315 
316 /*
317  * NOTE: We are reading recent and old data registers blindly
318  * whenever key-stuck interrupt happens, because events counter doesn't
319  * get updated when this interrupt happens due to key stuck doesn't get
320  * considered as key state change.
321  *
322  * We are not using old data register contents after they are being read
323  * because it might report the key which was pressed before the key being stuck
324  * as stuck key because it's pressed status is stored in the old data
325  * register.
326  */
327 static irqreturn_t pmic8xxx_kp_stuck_irq(int irq, void *data)
328 {
329 	u16 new_state[PM8XXX_MAX_ROWS];
330 	u16 old_state[PM8XXX_MAX_ROWS];
331 	int rc;
332 	struct pmic8xxx_kp *kp = data;
333 
334 	rc = pmic8xxx_kp_read_matrix(kp, new_state, old_state);
335 	if (rc < 0) {
336 		dev_err(kp->dev, "failed to read keypad matrix\n");
337 		return IRQ_HANDLED;
338 	}
339 
340 	__pmic8xxx_kp_scan_matrix(kp, new_state, kp->stuckstate);
341 
342 	return IRQ_HANDLED;
343 }
344 
345 static irqreturn_t pmic8xxx_kp_irq(int irq, void *data)
346 {
347 	struct pmic8xxx_kp *kp = data;
348 	unsigned int ctrl_val, events;
349 	int rc;
350 
351 	rc = regmap_read(kp->regmap, KEYP_CTRL, &ctrl_val);
352 	if (rc < 0) {
353 		dev_err(kp->dev, "failed to read keyp_ctrl register\n");
354 		return IRQ_HANDLED;
355 	}
356 
357 	events = ctrl_val & KEYP_CTRL_EVNTS_MASK;
358 
359 	rc = pmic8xxx_kp_scan_matrix(kp, events);
360 	if (rc < 0)
361 		dev_err(kp->dev, "failed to scan matrix\n");
362 
363 	return IRQ_HANDLED;
364 }
365 
366 static int pmic8xxx_kpd_init(struct pmic8xxx_kp *kp,
367 			     struct platform_device *pdev)
368 {
369 	const struct device_node *of_node = pdev->dev.of_node;
370 	unsigned int scan_delay_ms;
371 	unsigned int row_hold_ns;
372 	unsigned int debounce_ms;
373 	int bits, rc, cycles;
374 	u8 scan_val = 0, ctrl_val = 0;
375 	static const u8 row_bits[] = {
376 		0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7,
377 	};
378 
379 	/* Find column bits */
380 	if (kp->num_cols < KEYP_CTRL_SCAN_COLS_MIN)
381 		bits = 0;
382 	else
383 		bits = kp->num_cols - KEYP_CTRL_SCAN_COLS_MIN;
384 	ctrl_val = (bits & KEYP_CTRL_SCAN_COLS_BITS) <<
385 		KEYP_CTRL_SCAN_COLS_SHIFT;
386 
387 	/* Find row bits */
388 	if (kp->num_rows < KEYP_CTRL_SCAN_ROWS_MIN)
389 		bits = 0;
390 	else
391 		bits = row_bits[kp->num_rows - KEYP_CTRL_SCAN_ROWS_MIN];
392 
393 	ctrl_val |= (bits << KEYP_CTRL_SCAN_ROWS_SHIFT);
394 
395 	rc = regmap_write(kp->regmap, KEYP_CTRL, ctrl_val);
396 	if (rc < 0) {
397 		dev_err(kp->dev, "Error writing KEYP_CTRL reg, rc=%d\n", rc);
398 		return rc;
399 	}
400 
401 	if (of_property_read_u32(of_node, "scan-delay", &scan_delay_ms))
402 		scan_delay_ms = MIN_SCAN_DELAY;
403 
404 	if (scan_delay_ms > MAX_SCAN_DELAY || scan_delay_ms < MIN_SCAN_DELAY ||
405 	    !is_power_of_2(scan_delay_ms)) {
406 		dev_err(&pdev->dev, "invalid keypad scan time supplied\n");
407 		return -EINVAL;
408 	}
409 
410 	if (of_property_read_u32(of_node, "row-hold", &row_hold_ns))
411 		row_hold_ns = MIN_ROW_HOLD_DELAY;
412 
413 	if (row_hold_ns > MAX_ROW_HOLD_DELAY ||
414 	    row_hold_ns < MIN_ROW_HOLD_DELAY ||
415 	    ((row_hold_ns % MIN_ROW_HOLD_DELAY) != 0)) {
416 		dev_err(&pdev->dev, "invalid keypad row hold time supplied\n");
417 		return -EINVAL;
418 	}
419 
420 	if (of_property_read_u32(of_node, "debounce", &debounce_ms))
421 		debounce_ms = MIN_DEBOUNCE_TIME;
422 
423 	if (((debounce_ms % 5) != 0) ||
424 	    debounce_ms > MAX_DEBOUNCE_TIME ||
425 	    debounce_ms < MIN_DEBOUNCE_TIME) {
426 		dev_err(&pdev->dev, "invalid debounce time supplied\n");
427 		return -EINVAL;
428 	}
429 
430 	bits = (debounce_ms / 5) - 1;
431 
432 	scan_val |= (bits << KEYP_SCAN_DBOUNCE_SHIFT);
433 
434 	bits = fls(scan_delay_ms) - 1;
435 	scan_val |= (bits << KEYP_SCAN_PAUSE_SHIFT);
436 
437 	/* Row hold time is a multiple of 32KHz cycles. */
438 	cycles = (row_hold_ns * KEYP_CLOCK_FREQ) / NSEC_PER_SEC;
439 
440 	scan_val |= (cycles << KEYP_SCAN_ROW_HOLD_SHIFT);
441 
442 	rc = regmap_write(kp->regmap, KEYP_SCAN, scan_val);
443 	if (rc)
444 		dev_err(kp->dev, "Error writing KEYP_SCAN reg, rc=%d\n", rc);
445 
446 	return rc;
447 
448 }
449 
450 static int pmic8xxx_kp_enable(struct pmic8xxx_kp *kp)
451 {
452 	int rc;
453 
454 	kp->ctrl_reg |= KEYP_CTRL_KEYP_EN;
455 
456 	rc = regmap_write(kp->regmap, KEYP_CTRL, kp->ctrl_reg);
457 	if (rc < 0)
458 		dev_err(kp->dev, "Error writing KEYP_CTRL reg, rc=%d\n", rc);
459 
460 	return rc;
461 }
462 
463 static int pmic8xxx_kp_disable(struct pmic8xxx_kp *kp)
464 {
465 	int rc;
466 
467 	kp->ctrl_reg &= ~KEYP_CTRL_KEYP_EN;
468 
469 	rc = regmap_write(kp->regmap, KEYP_CTRL, kp->ctrl_reg);
470 	if (rc < 0)
471 		return rc;
472 
473 	return rc;
474 }
475 
476 static int pmic8xxx_kp_open(struct input_dev *dev)
477 {
478 	struct pmic8xxx_kp *kp = input_get_drvdata(dev);
479 
480 	return pmic8xxx_kp_enable(kp);
481 }
482 
483 static void pmic8xxx_kp_close(struct input_dev *dev)
484 {
485 	struct pmic8xxx_kp *kp = input_get_drvdata(dev);
486 
487 	pmic8xxx_kp_disable(kp);
488 }
489 
490 /*
491  * keypad controller should be initialized in the following sequence
492  * only, otherwise it might get into FSM stuck state.
493  *
494  * - Initialize keypad control parameters, like no. of rows, columns,
495  *   timing values etc.,
496  * - configure rows and column gpios pull up/down.
497  * - set irq edge type.
498  * - enable the keypad controller.
499  */
500 static int pmic8xxx_kp_probe(struct platform_device *pdev)
501 {
502 	struct device_node *np = pdev->dev.of_node;
503 	unsigned int rows, cols;
504 	bool repeat;
505 	bool wakeup;
506 	struct pmic8xxx_kp *kp;
507 	int rc;
508 	unsigned int ctrl_val;
509 
510 	rc = matrix_keypad_parse_properties(&pdev->dev, &rows, &cols);
511 	if (rc)
512 		return rc;
513 
514 	if (cols > PM8XXX_MAX_COLS || rows > PM8XXX_MAX_ROWS ||
515 	    cols < PM8XXX_MIN_COLS) {
516 		dev_err(&pdev->dev, "invalid platform data\n");
517 		return -EINVAL;
518 	}
519 
520 	repeat = !of_property_read_bool(np, "linux,input-no-autorepeat");
521 
522 	wakeup = of_property_read_bool(np, "wakeup-source") ||
523 		 /* legacy name */
524 		 of_property_read_bool(np, "linux,keypad-wakeup");
525 
526 	kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
527 	if (!kp)
528 		return -ENOMEM;
529 
530 	kp->regmap = dev_get_regmap(pdev->dev.parent, NULL);
531 	if (!kp->regmap)
532 		return -ENODEV;
533 
534 	platform_set_drvdata(pdev, kp);
535 
536 	kp->num_rows	= rows;
537 	kp->num_cols	= cols;
538 	kp->dev		= &pdev->dev;
539 
540 	kp->input = devm_input_allocate_device(&pdev->dev);
541 	if (!kp->input) {
542 		dev_err(&pdev->dev, "unable to allocate input device\n");
543 		return -ENOMEM;
544 	}
545 
546 	kp->key_sense_irq = platform_get_irq(pdev, 0);
547 	if (kp->key_sense_irq < 0)
548 		return kp->key_sense_irq;
549 
550 	kp->key_stuck_irq = platform_get_irq(pdev, 1);
551 	if (kp->key_stuck_irq < 0)
552 		return kp->key_stuck_irq;
553 
554 	kp->input->name = "PMIC8XXX keypad";
555 	kp->input->phys = "pmic8xxx_keypad/input0";
556 
557 	kp->input->id.bustype	= BUS_I2C;
558 	kp->input->id.version	= 0x0001;
559 	kp->input->id.product	= 0x0001;
560 	kp->input->id.vendor	= 0x0001;
561 
562 	kp->input->open		= pmic8xxx_kp_open;
563 	kp->input->close	= pmic8xxx_kp_close;
564 
565 	rc = matrix_keypad_build_keymap(NULL, NULL,
566 					PM8XXX_MAX_ROWS, PM8XXX_MAX_COLS,
567 					kp->keycodes, kp->input);
568 	if (rc) {
569 		dev_err(&pdev->dev, "failed to build keymap\n");
570 		return rc;
571 	}
572 
573 	if (repeat)
574 		__set_bit(EV_REP, kp->input->evbit);
575 	input_set_capability(kp->input, EV_MSC, MSC_SCAN);
576 
577 	input_set_drvdata(kp->input, kp);
578 
579 	/* initialize keypad state */
580 	memset(kp->keystate, 0xff, sizeof(kp->keystate));
581 	memset(kp->stuckstate, 0xff, sizeof(kp->stuckstate));
582 
583 	rc = pmic8xxx_kpd_init(kp, pdev);
584 	if (rc < 0) {
585 		dev_err(&pdev->dev, "unable to initialize keypad controller\n");
586 		return rc;
587 	}
588 
589 	rc = devm_request_any_context_irq(&pdev->dev, kp->key_sense_irq,
590 			pmic8xxx_kp_irq, IRQF_TRIGGER_RISING, "pmic-keypad",
591 			kp);
592 	if (rc < 0) {
593 		dev_err(&pdev->dev, "failed to request keypad sense irq\n");
594 		return rc;
595 	}
596 
597 	rc = devm_request_any_context_irq(&pdev->dev, kp->key_stuck_irq,
598 			pmic8xxx_kp_stuck_irq, IRQF_TRIGGER_RISING,
599 			"pmic-keypad-stuck", kp);
600 	if (rc < 0) {
601 		dev_err(&pdev->dev, "failed to request keypad stuck irq\n");
602 		return rc;
603 	}
604 
605 	rc = regmap_read(kp->regmap, KEYP_CTRL, &ctrl_val);
606 	if (rc < 0) {
607 		dev_err(&pdev->dev, "failed to read KEYP_CTRL register\n");
608 		return rc;
609 	}
610 
611 	kp->ctrl_reg = ctrl_val;
612 
613 	rc = input_register_device(kp->input);
614 	if (rc < 0) {
615 		dev_err(&pdev->dev, "unable to register keypad input device\n");
616 		return rc;
617 	}
618 
619 	device_init_wakeup(&pdev->dev, wakeup);
620 
621 	return 0;
622 }
623 
624 #ifdef CONFIG_PM_SLEEP
625 static int pmic8xxx_kp_suspend(struct device *dev)
626 {
627 	struct platform_device *pdev = to_platform_device(dev);
628 	struct pmic8xxx_kp *kp = platform_get_drvdata(pdev);
629 	struct input_dev *input_dev = kp->input;
630 
631 	if (device_may_wakeup(dev)) {
632 		enable_irq_wake(kp->key_sense_irq);
633 	} else {
634 		mutex_lock(&input_dev->mutex);
635 
636 		if (input_device_enabled(input_dev))
637 			pmic8xxx_kp_disable(kp);
638 
639 		mutex_unlock(&input_dev->mutex);
640 	}
641 
642 	return 0;
643 }
644 
645 static int pmic8xxx_kp_resume(struct device *dev)
646 {
647 	struct platform_device *pdev = to_platform_device(dev);
648 	struct pmic8xxx_kp *kp = platform_get_drvdata(pdev);
649 	struct input_dev *input_dev = kp->input;
650 
651 	if (device_may_wakeup(dev)) {
652 		disable_irq_wake(kp->key_sense_irq);
653 	} else {
654 		mutex_lock(&input_dev->mutex);
655 
656 		if (input_device_enabled(input_dev))
657 			pmic8xxx_kp_enable(kp);
658 
659 		mutex_unlock(&input_dev->mutex);
660 	}
661 
662 	return 0;
663 }
664 #endif
665 
666 static SIMPLE_DEV_PM_OPS(pm8xxx_kp_pm_ops,
667 			 pmic8xxx_kp_suspend, pmic8xxx_kp_resume);
668 
669 static const struct of_device_id pm8xxx_match_table[] = {
670 	{ .compatible = "qcom,pm8058-keypad" },
671 	{ .compatible = "qcom,pm8921-keypad" },
672 	{ }
673 };
674 MODULE_DEVICE_TABLE(of, pm8xxx_match_table);
675 
676 static struct platform_driver pmic8xxx_kp_driver = {
677 	.probe		= pmic8xxx_kp_probe,
678 	.driver		= {
679 		.name = "pm8xxx-keypad",
680 		.pm = &pm8xxx_kp_pm_ops,
681 		.of_match_table = pm8xxx_match_table,
682 	},
683 };
684 module_platform_driver(pmic8xxx_kp_driver);
685 
686 MODULE_LICENSE("GPL v2");
687 MODULE_DESCRIPTION("PMIC8XXX keypad driver");
688 MODULE_ALIAS("platform:pmic8xxx_keypad");
689 MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");
690