1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Jayeeta Banerjee <jayeeta.banerjee@stericsson.com>
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
8 * TC35893 MFD Keypad Controller driver
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/input.h>
14 #include <linux/platform_device.h>
15 #include <linux/input/matrix_keypad.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/mfd/tc3589x.h>
19 #include <linux/device.h>
21 /* Maximum supported keypad matrix row/columns size */
22 #define TC3589x_MAX_KPROW 8
23 #define TC3589x_MAX_KPCOL 12
25 /* keypad related Constants */
26 #define TC3589x_MAX_DEBOUNCE_SETTLE 0xFF
27 #define DEDICATED_KEY_VAL 0xFF
29 /* Pull up/down masks */
30 #define TC3589x_NO_PULL_MASK 0x0
31 #define TC3589x_PULL_DOWN_MASK 0x1
32 #define TC3589x_PULL_UP_MASK 0x2
33 #define TC3589x_PULLUP_ALL_MASK 0xAA
34 #define TC3589x_IO_PULL_VAL(index, mask) ((mask)<<((index)%4)*2)
36 /* Bit masks for IOCFG register */
37 #define IOCFG_BALLCFG 0x01
40 #define KP_EVCODE_COL_MASK 0x0F
41 #define KP_EVCODE_ROW_MASK 0x70
42 #define KP_RELEASE_EVT_MASK 0x80
44 #define KP_ROW_SHIFT 4
46 #define KP_NO_VALID_KEY_MASK 0x7F
48 /* bit masks for RESTCTRL register */
49 #define TC3589x_KBDRST 0x2
50 #define TC3589x_IRQRST 0x10
51 #define TC3589x_RESET_ALL 0x1B
53 /* KBDMFS register bit mask */
54 #define TC3589x_KBDMFS_EN 0x1
56 /* CLKEN register bitmask */
57 #define KPD_CLK_EN 0x1
59 /* RSTINTCLR register bit mask */
62 /* bit masks for keyboard interrupts*/
63 #define TC3589x_EVT_LOSS_INT 0x8
64 #define TC3589x_EVT_INT 0x4
65 #define TC3589x_KBD_LOSS_INT 0x2
66 #define TC3589x_KBD_INT 0x1
68 /* bit masks for keyboard interrupt clear*/
69 #define TC3589x_EVT_INT_CLR 0x2
70 #define TC3589x_KBD_INT_CLR 0x1
73 * struct tc3589x_keypad_platform_data - platform specific keypad data
74 * @keymap_data: matrix scan code table for keycodes
75 * @krow: mask for available rows, value is 0xFF
76 * @kcol: mask for available columns, value is 0xFF
77 * @debounce_period: platform specific debounce time
78 * @settle_time: platform specific settle down time
79 * @irqtype: type of interrupt, falling or rising edge
80 * @enable_wakeup: specifies if keypad event can wake up system from sleep
81 * @no_autorepeat: flag for auto repetition
83 struct tc3589x_keypad_platform_data
{
84 const struct matrix_keymap_data
*keymap_data
;
89 unsigned long irqtype
;
95 * struct tc_keypad - data structure used by keypad driver
96 * @tc3589x: pointer to tc35893
97 * @input: pointer to input device object
98 * @board: keypad platform device
99 * @krow: number of rows
100 * @kcol: number of columns
101 * @keymap: matrix scan code table for keycodes
102 * @keypad_stopped: holds keypad status
105 struct tc3589x
*tc3589x
;
106 struct input_dev
*input
;
107 const struct tc3589x_keypad_platform_data
*board
;
110 unsigned short *keymap
;
114 static int tc3589x_keypad_init_key_hardware(struct tc_keypad
*keypad
)
117 struct tc3589x
*tc3589x
= keypad
->tc3589x
;
118 const struct tc3589x_keypad_platform_data
*board
= keypad
->board
;
120 /* validate platform configuration */
121 if (board
->kcol
> TC3589x_MAX_KPCOL
|| board
->krow
> TC3589x_MAX_KPROW
)
124 /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
125 ret
= tc3589x_reg_write(tc3589x
, TC3589x_KBDSIZE
,
126 (board
->krow
<< KP_ROW_SHIFT
) | board
->kcol
);
130 /* configure dedicated key config, no dedicated key selected */
131 ret
= tc3589x_reg_write(tc3589x
, TC3589x_KBCFG_LSB
, DEDICATED_KEY_VAL
);
135 ret
= tc3589x_reg_write(tc3589x
, TC3589x_KBCFG_MSB
, DEDICATED_KEY_VAL
);
139 /* Configure settle time */
140 ret
= tc3589x_reg_write(tc3589x
, TC3589x_KBDSETTLE_REG
,
145 /* Configure debounce time */
146 ret
= tc3589x_reg_write(tc3589x
, TC3589x_KBDBOUNCE
,
147 board
->debounce_period
);
151 /* Start of initialise keypad GPIOs */
152 ret
= tc3589x_set_bits(tc3589x
, TC3589x_IOCFG
, 0x0, IOCFG_IG
);
156 /* Configure pull-up resistors for all row GPIOs */
157 ret
= tc3589x_reg_write(tc3589x
, TC3589x_IOPULLCFG0_LSB
,
158 TC3589x_PULLUP_ALL_MASK
);
162 ret
= tc3589x_reg_write(tc3589x
, TC3589x_IOPULLCFG0_MSB
,
163 TC3589x_PULLUP_ALL_MASK
);
167 /* Configure pull-up resistors for all column GPIOs */
168 ret
= tc3589x_reg_write(tc3589x
, TC3589x_IOPULLCFG1_LSB
,
169 TC3589x_PULLUP_ALL_MASK
);
173 ret
= tc3589x_reg_write(tc3589x
, TC3589x_IOPULLCFG1_MSB
,
174 TC3589x_PULLUP_ALL_MASK
);
178 ret
= tc3589x_reg_write(tc3589x
, TC3589x_IOPULLCFG2_LSB
,
179 TC3589x_PULLUP_ALL_MASK
);
184 #define TC35893_DATA_REGS 4
185 #define TC35893_KEYCODE_FIFO_EMPTY 0x7f
186 #define TC35893_KEYCODE_FIFO_CLEAR 0xff
187 #define TC35893_KEYPAD_ROW_SHIFT 0x3
189 static irqreturn_t
tc3589x_keypad_irq(int irq
, void *dev
)
191 struct tc_keypad
*keypad
= dev
;
192 struct tc3589x
*tc3589x
= keypad
->tc3589x
;
193 u8 i
, row_index
, col_index
, kbd_code
, up
;
196 for (i
= 0; i
< TC35893_DATA_REGS
* 2; i
++) {
197 kbd_code
= tc3589x_reg_read(tc3589x
, TC3589x_EVTCODE_FIFO
);
199 /* loop till fifo is empty and no more keys are pressed */
200 if (kbd_code
== TC35893_KEYCODE_FIFO_EMPTY
||
201 kbd_code
== TC35893_KEYCODE_FIFO_CLEAR
)
204 /* valid key is found */
205 col_index
= kbd_code
& KP_EVCODE_COL_MASK
;
206 row_index
= (kbd_code
& KP_EVCODE_ROW_MASK
) >> KP_ROW_SHIFT
;
207 code
= MATRIX_SCAN_CODE(row_index
, col_index
,
208 TC35893_KEYPAD_ROW_SHIFT
);
209 up
= kbd_code
& KP_RELEASE_EVT_MASK
;
211 input_event(keypad
->input
, EV_MSC
, MSC_SCAN
, code
);
212 input_report_key(keypad
->input
, keypad
->keymap
[code
], !up
);
213 input_sync(keypad
->input
);
217 tc3589x_set_bits(tc3589x
, TC3589x_KBDIC
,
218 0x0, TC3589x_EVT_INT_CLR
| TC3589x_KBD_INT_CLR
);
220 tc3589x_set_bits(tc3589x
, TC3589x_KBDMSK
,
221 0x0, TC3589x_EVT_LOSS_INT
| TC3589x_EVT_INT
);
226 static int tc3589x_keypad_enable(struct tc_keypad
*keypad
)
228 struct tc3589x
*tc3589x
= keypad
->tc3589x
;
231 /* pull the keypad module out of reset */
232 ret
= tc3589x_set_bits(tc3589x
, TC3589x_RSTCTRL
, TC3589x_KBDRST
, 0x0);
236 /* configure KBDMFS */
237 ret
= tc3589x_set_bits(tc3589x
, TC3589x_KBDMFS
, 0x0, TC3589x_KBDMFS_EN
);
241 /* enable the keypad clock */
242 ret
= tc3589x_set_bits(tc3589x
, TC3589x_CLKEN
, 0x0, KPD_CLK_EN
);
246 /* clear pending IRQs */
247 ret
= tc3589x_set_bits(tc3589x
, TC3589x_RSTINTCLR
, 0x0, 0x1);
251 /* enable the IRQs */
252 ret
= tc3589x_set_bits(tc3589x
, TC3589x_KBDMSK
, 0x0,
253 TC3589x_EVT_LOSS_INT
| TC3589x_EVT_INT
);
257 keypad
->keypad_stopped
= false;
262 static int tc3589x_keypad_disable(struct tc_keypad
*keypad
)
264 struct tc3589x
*tc3589x
= keypad
->tc3589x
;
268 ret
= tc3589x_set_bits(tc3589x
, TC3589x_KBDIC
,
269 0x0, TC3589x_EVT_INT_CLR
| TC3589x_KBD_INT_CLR
);
273 /* disable all interrupts */
274 ret
= tc3589x_set_bits(tc3589x
, TC3589x_KBDMSK
,
275 ~(TC3589x_EVT_LOSS_INT
| TC3589x_EVT_INT
), 0x0);
279 /* disable the keypad module */
280 ret
= tc3589x_set_bits(tc3589x
, TC3589x_CLKEN
, 0x1, 0x0);
284 /* put the keypad module into reset */
285 ret
= tc3589x_set_bits(tc3589x
, TC3589x_RSTCTRL
, TC3589x_KBDRST
, 0x1);
287 keypad
->keypad_stopped
= true;
292 static int tc3589x_keypad_open(struct input_dev
*input
)
295 struct tc_keypad
*keypad
= input_get_drvdata(input
);
297 /* enable the keypad module */
298 error
= tc3589x_keypad_enable(keypad
);
300 dev_err(&input
->dev
, "failed to enable keypad module\n");
304 error
= tc3589x_keypad_init_key_hardware(keypad
);
306 dev_err(&input
->dev
, "failed to configure keypad module\n");
313 static void tc3589x_keypad_close(struct input_dev
*input
)
315 struct tc_keypad
*keypad
= input_get_drvdata(input
);
317 /* disable the keypad module */
318 tc3589x_keypad_disable(keypad
);
321 static const struct tc3589x_keypad_platform_data
*
322 tc3589x_keypad_of_probe(struct device
*dev
)
324 struct device_node
*np
= dev
->of_node
;
325 struct tc3589x_keypad_platform_data
*plat
;
330 return ERR_PTR(-ENODEV
);
332 plat
= devm_kzalloc(dev
, sizeof(*plat
), GFP_KERNEL
);
334 return ERR_PTR(-ENOMEM
);
336 of_property_read_u32(np
, "keypad,num-columns", &cols
);
337 of_property_read_u32(np
, "keypad,num-rows", &rows
);
338 plat
->kcol
= (u8
) cols
;
339 plat
->krow
= (u8
) rows
;
340 if (!plat
->krow
|| !plat
->kcol
||
341 plat
->krow
> TC_KPD_ROWS
|| plat
->kcol
> TC_KPD_COLUMNS
) {
343 "keypad columns/rows not properly specified (%ux%u)\n",
344 plat
->kcol
, plat
->krow
);
345 return ERR_PTR(-EINVAL
);
348 if (!of_property_present(np
, "linux,keymap")) {
349 dev_err(dev
, "property linux,keymap not found\n");
350 return ERR_PTR(-ENOENT
);
353 plat
->no_autorepeat
= of_property_read_bool(np
, "linux,no-autorepeat");
355 plat
->enable_wakeup
= of_property_read_bool(np
, "wakeup-source") ||
357 of_property_read_bool(np
, "linux,wakeup");
359 /* The custom delay format is ms/16 */
360 of_property_read_u32(np
, "debounce-delay-ms", &debounce_ms
);
362 plat
->debounce_period
= debounce_ms
* 16;
364 plat
->debounce_period
= TC_KPD_DEBOUNCE_PERIOD
;
366 plat
->settle_time
= TC_KPD_SETTLE_TIME
;
367 /* FIXME: should be property of the IRQ resource? */
368 plat
->irqtype
= IRQF_TRIGGER_FALLING
;
373 static int tc3589x_keypad_probe(struct platform_device
*pdev
)
375 struct tc3589x
*tc3589x
= dev_get_drvdata(pdev
->dev
.parent
);
376 struct tc_keypad
*keypad
;
377 struct input_dev
*input
;
378 const struct tc3589x_keypad_platform_data
*plat
;
381 plat
= tc3589x_keypad_of_probe(&pdev
->dev
);
383 dev_err(&pdev
->dev
, "invalid keypad platform data\n");
384 return PTR_ERR(plat
);
387 irq
= platform_get_irq(pdev
, 0);
391 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(struct tc_keypad
),
396 input
= devm_input_allocate_device(&pdev
->dev
);
398 dev_err(&pdev
->dev
, "failed to allocate input device\n");
402 keypad
->board
= plat
;
403 keypad
->input
= input
;
404 keypad
->tc3589x
= tc3589x
;
406 input
->id
.bustype
= BUS_I2C
;
407 input
->name
= pdev
->name
;
408 input
->dev
.parent
= &pdev
->dev
;
410 input
->open
= tc3589x_keypad_open
;
411 input
->close
= tc3589x_keypad_close
;
413 error
= matrix_keypad_build_keymap(plat
->keymap_data
, NULL
,
414 TC3589x_MAX_KPROW
, TC3589x_MAX_KPCOL
,
417 dev_err(&pdev
->dev
, "Failed to build keymap\n");
421 keypad
->keymap
= input
->keycode
;
423 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
424 if (!plat
->no_autorepeat
)
425 __set_bit(EV_REP
, input
->evbit
);
427 input_set_drvdata(input
, keypad
);
429 tc3589x_keypad_disable(keypad
);
431 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
432 NULL
, tc3589x_keypad_irq
,
433 plat
->irqtype
| IRQF_ONESHOT
,
434 "tc3589x-keypad", keypad
);
437 "Could not allocate irq %d,error %d\n",
442 error
= input_register_device(input
);
444 dev_err(&pdev
->dev
, "Could not register input device\n");
448 /* let platform decide if keypad is a wakeup source or not */
449 device_init_wakeup(&pdev
->dev
, plat
->enable_wakeup
);
450 device_set_wakeup_capable(&pdev
->dev
, plat
->enable_wakeup
);
452 platform_set_drvdata(pdev
, keypad
);
457 static int tc3589x_keypad_suspend(struct device
*dev
)
459 struct platform_device
*pdev
= to_platform_device(dev
);
460 struct tc_keypad
*keypad
= platform_get_drvdata(pdev
);
461 int irq
= platform_get_irq(pdev
, 0);
463 /* keypad is already off; we do nothing */
464 if (keypad
->keypad_stopped
)
467 /* if device is not a wakeup source, disable it for powersave */
468 if (!device_may_wakeup(&pdev
->dev
))
469 tc3589x_keypad_disable(keypad
);
471 enable_irq_wake(irq
);
476 static int tc3589x_keypad_resume(struct device
*dev
)
478 struct platform_device
*pdev
= to_platform_device(dev
);
479 struct tc_keypad
*keypad
= platform_get_drvdata(pdev
);
480 int irq
= platform_get_irq(pdev
, 0);
482 if (!keypad
->keypad_stopped
)
485 /* enable the device to resume normal operations */
486 if (!device_may_wakeup(&pdev
->dev
))
487 tc3589x_keypad_enable(keypad
);
489 disable_irq_wake(irq
);
494 static DEFINE_SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops
,
495 tc3589x_keypad_suspend
, tc3589x_keypad_resume
);
497 static struct platform_driver tc3589x_keypad_driver
= {
499 .name
= "tc3589x-keypad",
500 .pm
= pm_sleep_ptr(&tc3589x_keypad_dev_pm_ops
),
502 .probe
= tc3589x_keypad_probe
,
504 module_platform_driver(tc3589x_keypad_driver
);
506 MODULE_LICENSE("GPL v2");
507 MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
508 MODULE_DESCRIPTION("TC35893 Keypad Driver");
509 MODULE_ALIAS("platform:tc3589x-keypad");