1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/input.h>
11 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/input/matrix_keypad.h>
15 #include <linux/mfd/stmpe.h>
17 /* These are at the same addresses in all STMPE variants */
18 #define STMPE_KPC_COL 0x60
19 #define STMPE_KPC_ROW_MSB 0x61
20 #define STMPE_KPC_ROW_LSB 0x62
21 #define STMPE_KPC_CTRL_MSB 0x63
22 #define STMPE_KPC_CTRL_LSB 0x64
23 #define STMPE_KPC_COMBI_KEY_0 0x65
24 #define STMPE_KPC_COMBI_KEY_1 0x66
25 #define STMPE_KPC_COMBI_KEY_2 0x67
26 #define STMPE_KPC_DATA_BYTE0 0x68
27 #define STMPE_KPC_DATA_BYTE1 0x69
28 #define STMPE_KPC_DATA_BYTE2 0x6a
29 #define STMPE_KPC_DATA_BYTE3 0x6b
30 #define STMPE_KPC_DATA_BYTE4 0x6c
32 #define STMPE_KPC_CTRL_LSB_SCAN (0x1 << 0)
33 #define STMPE_KPC_CTRL_LSB_DEBOUNCE (0x7f << 1)
34 #define STMPE_KPC_CTRL_MSB_SCAN_COUNT (0xf << 4)
36 #define STMPE_KPC_ROW_MSB_ROWS 0xff
38 #define STMPE_KPC_DATA_UP (0x1 << 7)
39 #define STMPE_KPC_DATA_ROW (0xf << 3)
40 #define STMPE_KPC_DATA_COL (0x7 << 0)
41 #define STMPE_KPC_DATA_NOKEY_MASK 0x78
43 #define STMPE_KEYPAD_MAX_DEBOUNCE 127
44 #define STMPE_KEYPAD_MAX_SCAN_COUNT 15
46 #define STMPE_KEYPAD_MAX_ROWS 8
47 #define STMPE_KEYPAD_MAX_COLS 8
48 #define STMPE_KEYPAD_ROW_SHIFT 3
49 #define STMPE_KEYPAD_KEYMAP_MAX_SIZE \
50 (STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
53 #define STMPE1601_NUM_DATA 5
54 #define STMPE2401_NUM_DATA 3
55 #define STMPE2403_NUM_DATA 5
57 /* Make sure it covers all cases above */
58 #define MAX_NUM_DATA 5
61 * struct stmpe_keypad_variant - model-specific attributes
62 * @auto_increment: whether the KPC_DATA_BYTE register address
63 * auto-increments on multiple read
64 * @set_pullup: whether the pins need to have their pull-ups set
65 * @num_data: number of data bytes
66 * @num_normal_data: number of normal keys' data bytes
67 * @max_cols: maximum number of columns supported
68 * @max_rows: maximum number of rows supported
69 * @col_gpios: bitmask of gpios which can be used for columns
70 * @row_gpios: bitmask of gpios which can be used for rows
72 struct stmpe_keypad_variant
{
79 unsigned int col_gpios
;
80 unsigned int row_gpios
;
83 static const struct stmpe_keypad_variant stmpe_keypad_variants
[] = {
85 .auto_increment
= true,
86 .num_data
= STMPE1601_NUM_DATA
,
90 .col_gpios
= 0x000ff, /* GPIO 0 - 7 */
91 .row_gpios
= 0x0ff00, /* GPIO 8 - 15 */
94 .auto_increment
= false,
96 .num_data
= STMPE2401_NUM_DATA
,
100 .col_gpios
= 0x0000ff, /* GPIO 0 - 7*/
101 .row_gpios
= 0x1f7f00, /* GPIO 8-14, 16-20 */
104 .auto_increment
= true,
106 .num_data
= STMPE2403_NUM_DATA
,
107 .num_normal_data
= 3,
110 .col_gpios
= 0x0000ff, /* GPIO 0 - 7*/
111 .row_gpios
= 0x1fef00, /* GPIO 8-14, 16-20 */
116 * struct stmpe_keypad - STMPE keypad state container
117 * @stmpe: pointer to parent STMPE device
118 * @input: spawned input device
119 * @variant: STMPE variant
120 * @debounce_ms: debounce interval, in ms. Maximum is
121 * %STMPE_KEYPAD_MAX_DEBOUNCE.
122 * @scan_count: number of key scanning cycles to confirm key data.
123 * Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
124 * @no_autorepeat: disable key autorepeat
125 * @rows: bitmask for the rows
126 * @cols: bitmask for the columns
127 * @keymap: the keymap
129 struct stmpe_keypad
{
131 struct input_dev
*input
;
132 const struct stmpe_keypad_variant
*variant
;
133 unsigned int debounce_ms
;
134 unsigned int scan_count
;
138 unsigned short keymap
[STMPE_KEYPAD_KEYMAP_MAX_SIZE
];
141 static int stmpe_keypad_read_data(struct stmpe_keypad
*keypad
, u8
*data
)
143 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
144 struct stmpe
*stmpe
= keypad
->stmpe
;
148 if (variant
->auto_increment
)
149 return stmpe_block_read(stmpe
, STMPE_KPC_DATA_BYTE0
,
150 variant
->num_data
, data
);
152 for (i
= 0; i
< variant
->num_data
; i
++) {
153 ret
= stmpe_reg_read(stmpe
, STMPE_KPC_DATA_BYTE0
+ i
);
163 static irqreturn_t
stmpe_keypad_irq(int irq
, void *dev
)
165 struct stmpe_keypad
*keypad
= dev
;
166 struct input_dev
*input
= keypad
->input
;
167 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
168 u8 fifo
[MAX_NUM_DATA
];
172 ret
= stmpe_keypad_read_data(keypad
, fifo
);
176 for (i
= 0; i
< variant
->num_normal_data
; i
++) {
178 int row
= (data
& STMPE_KPC_DATA_ROW
) >> 3;
179 int col
= data
& STMPE_KPC_DATA_COL
;
180 int code
= MATRIX_SCAN_CODE(row
, col
, STMPE_KEYPAD_ROW_SHIFT
);
181 bool up
= data
& STMPE_KPC_DATA_UP
;
183 if ((data
& STMPE_KPC_DATA_NOKEY_MASK
)
184 == STMPE_KPC_DATA_NOKEY_MASK
)
187 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
188 input_report_key(input
, keypad
->keymap
[code
], !up
);
195 static int stmpe_keypad_altfunc_init(struct stmpe_keypad
*keypad
)
197 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
198 unsigned int col_gpios
= variant
->col_gpios
;
199 unsigned int row_gpios
= variant
->row_gpios
;
200 struct stmpe
*stmpe
= keypad
->stmpe
;
201 u8 pureg
= stmpe
->regs
[STMPE_IDX_GPPUR_LSB
];
202 unsigned int pins
= 0;
203 unsigned int pu_pins
= 0;
208 * Figure out which pins need to be set to the keypad alternate
211 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
214 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
215 * for the keypad) are used on the board.
218 for (i
= 0; i
< variant
->max_cols
; i
++) {
219 int num
= __ffs(col_gpios
);
221 if (keypad
->cols
& (1 << i
)) {
226 col_gpios
&= ~(1 << num
);
229 for (i
= 0; i
< variant
->max_rows
; i
++) {
230 int num
= __ffs(row_gpios
);
232 if (keypad
->rows
& (1 << i
))
235 row_gpios
&= ~(1 << num
);
238 ret
= stmpe_set_altfunc(stmpe
, pins
, STMPE_BLOCK_KEYPAD
);
243 * On STMPE24xx, set pin bias to pull-up on all keypad input
244 * pins (columns), this incidentally happen to be maximum 8 pins
245 * and placed at GPIO0-7 so only the LSB of the pull up register
246 * ever needs to be written.
248 if (variant
->set_pullup
) {
251 ret
= stmpe_reg_read(stmpe
, pureg
);
255 /* Do not touch unused pins, may be used for GPIO */
256 val
= ret
& ~pu_pins
;
259 ret
= stmpe_reg_write(stmpe
, pureg
, val
);
265 static int stmpe_keypad_chip_init(struct stmpe_keypad
*keypad
)
267 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
268 struct stmpe
*stmpe
= keypad
->stmpe
;
271 if (keypad
->debounce_ms
> STMPE_KEYPAD_MAX_DEBOUNCE
)
274 if (keypad
->scan_count
> STMPE_KEYPAD_MAX_SCAN_COUNT
)
277 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_KEYPAD
);
281 ret
= stmpe_keypad_altfunc_init(keypad
);
285 ret
= stmpe_reg_write(stmpe
, STMPE_KPC_COL
, keypad
->cols
);
289 ret
= stmpe_reg_write(stmpe
, STMPE_KPC_ROW_LSB
, keypad
->rows
);
293 if (variant
->max_rows
> 8) {
294 ret
= stmpe_set_bits(stmpe
, STMPE_KPC_ROW_MSB
,
295 STMPE_KPC_ROW_MSB_ROWS
,
301 ret
= stmpe_set_bits(stmpe
, STMPE_KPC_CTRL_MSB
,
302 STMPE_KPC_CTRL_MSB_SCAN_COUNT
,
303 keypad
->scan_count
<< 4);
307 return stmpe_set_bits(stmpe
, STMPE_KPC_CTRL_LSB
,
308 STMPE_KPC_CTRL_LSB_SCAN
|
309 STMPE_KPC_CTRL_LSB_DEBOUNCE
,
310 STMPE_KPC_CTRL_LSB_SCAN
|
311 (keypad
->debounce_ms
<< 1));
314 static void stmpe_keypad_fill_used_pins(struct stmpe_keypad
*keypad
,
315 u32 used_rows
, u32 used_cols
)
319 for (row
= 0; row
< used_rows
; row
++) {
320 for (col
= 0; col
< used_cols
; col
++) {
321 int code
= MATRIX_SCAN_CODE(row
, col
,
322 STMPE_KEYPAD_ROW_SHIFT
);
323 if (keypad
->keymap
[code
] != KEY_RESERVED
) {
324 keypad
->rows
|= 1 << row
;
325 keypad
->cols
|= 1 << col
;
331 static int stmpe_keypad_probe(struct platform_device
*pdev
)
333 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
334 struct device_node
*np
= pdev
->dev
.of_node
;
335 struct stmpe_keypad
*keypad
;
336 struct input_dev
*input
;
342 irq
= platform_get_irq(pdev
, 0);
346 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(struct stmpe_keypad
),
351 keypad
->stmpe
= stmpe
;
352 keypad
->variant
= &stmpe_keypad_variants
[stmpe
->partnum
];
354 of_property_read_u32(np
, "debounce-interval", &keypad
->debounce_ms
);
355 of_property_read_u32(np
, "st,scan-count", &keypad
->scan_count
);
356 keypad
->no_autorepeat
= of_property_read_bool(np
, "st,no-autorepeat");
358 input
= devm_input_allocate_device(&pdev
->dev
);
362 input
->name
= "STMPE keypad";
363 input
->id
.bustype
= BUS_I2C
;
364 input
->dev
.parent
= &pdev
->dev
;
366 error
= matrix_keypad_parse_properties(&pdev
->dev
, &rows
, &cols
);
370 error
= matrix_keypad_build_keymap(NULL
, NULL
, rows
, cols
,
371 keypad
->keymap
, input
);
375 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
376 if (!keypad
->no_autorepeat
)
377 __set_bit(EV_REP
, input
->evbit
);
379 stmpe_keypad_fill_used_pins(keypad
, rows
, cols
);
381 keypad
->input
= input
;
383 error
= stmpe_keypad_chip_init(keypad
);
387 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
388 NULL
, stmpe_keypad_irq
,
389 IRQF_ONESHOT
, "stmpe-keypad", keypad
);
391 dev_err(&pdev
->dev
, "unable to get irq: %d\n", error
);
395 error
= input_register_device(input
);
398 "unable to register input device: %d\n", error
);
402 platform_set_drvdata(pdev
, keypad
);
407 static void stmpe_keypad_remove(struct platform_device
*pdev
)
409 struct stmpe_keypad
*keypad
= platform_get_drvdata(pdev
);
411 stmpe_disable(keypad
->stmpe
, STMPE_BLOCK_KEYPAD
);
414 static struct platform_driver stmpe_keypad_driver
= {
415 .driver
.name
= "stmpe-keypad",
416 .probe
= stmpe_keypad_probe
,
417 .remove
= stmpe_keypad_remove
,
419 module_platform_driver(stmpe_keypad_driver
);
421 MODULE_LICENSE("GPL v2");
422 MODULE_DESCRIPTION("STMPExxxx keypad driver");
423 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");