2 * Copyright (C) ST-Ericsson SA 2010
4 * License Terms: GNU General Public License, version 2
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>
12 #include <linux/platform_device.h>
13 #include <linux/input/matrix_keypad.h>
14 #include <linux/mfd/stmpe.h>
16 /* These are at the same addresses in all STMPE variants */
17 #define STMPE_KPC_COL 0x60
18 #define STMPE_KPC_ROW_MSB 0x61
19 #define STMPE_KPC_ROW_LSB 0x62
20 #define STMPE_KPC_CTRL_MSB 0x63
21 #define STMPE_KPC_CTRL_LSB 0x64
22 #define STMPE_KPC_COMBI_KEY_0 0x65
23 #define STMPE_KPC_COMBI_KEY_1 0x66
24 #define STMPE_KPC_COMBI_KEY_2 0x67
25 #define STMPE_KPC_DATA_BYTE0 0x68
26 #define STMPE_KPC_DATA_BYTE1 0x69
27 #define STMPE_KPC_DATA_BYTE2 0x6a
28 #define STMPE_KPC_DATA_BYTE3 0x6b
29 #define STMPE_KPC_DATA_BYTE4 0x6c
31 #define STMPE_KPC_CTRL_LSB_SCAN (0x1 << 0)
32 #define STMPE_KPC_CTRL_LSB_DEBOUNCE (0x7f << 1)
33 #define STMPE_KPC_CTRL_MSB_SCAN_COUNT (0xf << 4)
35 #define STMPE_KPC_ROW_MSB_ROWS 0xff
37 #define STMPE_KPC_DATA_UP (0x1 << 7)
38 #define STMPE_KPC_DATA_ROW (0xf << 3)
39 #define STMPE_KPC_DATA_COL (0x7 << 0)
40 #define STMPE_KPC_DATA_NOKEY_MASK 0x78
42 #define STMPE_KEYPAD_MAX_DEBOUNCE 127
43 #define STMPE_KEYPAD_MAX_SCAN_COUNT 15
45 #define STMPE_KEYPAD_MAX_ROWS 8
46 #define STMPE_KEYPAD_MAX_COLS 8
47 #define STMPE_KEYPAD_ROW_SHIFT 3
48 #define STMPE_KEYPAD_KEYMAP_MAX_SIZE \
49 (STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
52 * struct stmpe_keypad_variant - model-specific attributes
53 * @auto_increment: whether the KPC_DATA_BYTE register address
54 * auto-increments on multiple read
55 * @set_pullup: whether the pins need to have their pull-ups set
56 * @num_data: number of data bytes
57 * @num_normal_data: number of normal keys' data bytes
58 * @max_cols: maximum number of columns supported
59 * @max_rows: maximum number of rows supported
60 * @col_gpios: bitmask of gpios which can be used for columns
61 * @row_gpios: bitmask of gpios which can be used for rows
63 struct stmpe_keypad_variant
{
70 unsigned int col_gpios
;
71 unsigned int row_gpios
;
74 static const struct stmpe_keypad_variant stmpe_keypad_variants
[] = {
76 .auto_increment
= true,
81 .col_gpios
= 0x000ff, /* GPIO 0 - 7 */
82 .row_gpios
= 0x0ff00, /* GPIO 8 - 15 */
85 .auto_increment
= false,
91 .col_gpios
= 0x0000ff, /* GPIO 0 - 7*/
92 .row_gpios
= 0x1f7f00, /* GPIO 8-14, 16-20 */
95 .auto_increment
= true,
101 .col_gpios
= 0x0000ff, /* GPIO 0 - 7*/
102 .row_gpios
= 0x1fef00, /* GPIO 8-14, 16-20 */
107 * struct stmpe_keypad - STMPE keypad state container
108 * @stmpe: pointer to parent STMPE device
109 * @input: spawned input device
110 * @variant: STMPE variant
111 * @debounce_ms: debounce interval, in ms. Maximum is
112 * %STMPE_KEYPAD_MAX_DEBOUNCE.
113 * @scan_count: number of key scanning cycles to confirm key data.
114 * Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
115 * @no_autorepeat: disable key autorepeat
116 * @rows: bitmask for the rows
117 * @cols: bitmask for the columns
118 * @keymap: the keymap
120 struct stmpe_keypad
{
122 struct input_dev
*input
;
123 const struct stmpe_keypad_variant
*variant
;
124 unsigned int debounce_ms
;
125 unsigned int scan_count
;
129 unsigned short keymap
[STMPE_KEYPAD_KEYMAP_MAX_SIZE
];
132 static int stmpe_keypad_read_data(struct stmpe_keypad
*keypad
, u8
*data
)
134 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
135 struct stmpe
*stmpe
= keypad
->stmpe
;
139 if (variant
->auto_increment
)
140 return stmpe_block_read(stmpe
, STMPE_KPC_DATA_BYTE0
,
141 variant
->num_data
, data
);
143 for (i
= 0; i
< variant
->num_data
; i
++) {
144 ret
= stmpe_reg_read(stmpe
, STMPE_KPC_DATA_BYTE0
+ i
);
154 static irqreturn_t
stmpe_keypad_irq(int irq
, void *dev
)
156 struct stmpe_keypad
*keypad
= dev
;
157 struct input_dev
*input
= keypad
->input
;
158 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
159 u8 fifo
[variant
->num_data
];
163 ret
= stmpe_keypad_read_data(keypad
, fifo
);
167 for (i
= 0; i
< variant
->num_normal_data
; i
++) {
169 int row
= (data
& STMPE_KPC_DATA_ROW
) >> 3;
170 int col
= data
& STMPE_KPC_DATA_COL
;
171 int code
= MATRIX_SCAN_CODE(row
, col
, STMPE_KEYPAD_ROW_SHIFT
);
172 bool up
= data
& STMPE_KPC_DATA_UP
;
174 if ((data
& STMPE_KPC_DATA_NOKEY_MASK
)
175 == STMPE_KPC_DATA_NOKEY_MASK
)
178 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
179 input_report_key(input
, keypad
->keymap
[code
], !up
);
186 static int stmpe_keypad_altfunc_init(struct stmpe_keypad
*keypad
)
188 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
189 unsigned int col_gpios
= variant
->col_gpios
;
190 unsigned int row_gpios
= variant
->row_gpios
;
191 struct stmpe
*stmpe
= keypad
->stmpe
;
192 u8 pureg
= stmpe
->regs
[STMPE_IDX_GPPUR_LSB
];
193 unsigned int pins
= 0;
194 unsigned int pu_pins
= 0;
199 * Figure out which pins need to be set to the keypad alternate
202 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
205 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
206 * for the keypad) are used on the board.
209 for (i
= 0; i
< variant
->max_cols
; i
++) {
210 int num
= __ffs(col_gpios
);
212 if (keypad
->cols
& (1 << i
)) {
217 col_gpios
&= ~(1 << num
);
220 for (i
= 0; i
< variant
->max_rows
; i
++) {
221 int num
= __ffs(row_gpios
);
223 if (keypad
->rows
& (1 << i
))
226 row_gpios
&= ~(1 << num
);
229 ret
= stmpe_set_altfunc(stmpe
, pins
, STMPE_BLOCK_KEYPAD
);
234 * On STMPE24xx, set pin bias to pull-up on all keypad input
235 * pins (columns), this incidentally happen to be maximum 8 pins
236 * and placed at GPIO0-7 so only the LSB of the pull up register
237 * ever needs to be written.
239 if (variant
->set_pullup
) {
242 ret
= stmpe_reg_read(stmpe
, pureg
);
246 /* Do not touch unused pins, may be used for GPIO */
247 val
= ret
& ~pu_pins
;
250 ret
= stmpe_reg_write(stmpe
, pureg
, val
);
256 static int stmpe_keypad_chip_init(struct stmpe_keypad
*keypad
)
258 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
259 struct stmpe
*stmpe
= keypad
->stmpe
;
262 if (keypad
->debounce_ms
> STMPE_KEYPAD_MAX_DEBOUNCE
)
265 if (keypad
->scan_count
> STMPE_KEYPAD_MAX_SCAN_COUNT
)
268 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_KEYPAD
);
272 ret
= stmpe_keypad_altfunc_init(keypad
);
276 ret
= stmpe_reg_write(stmpe
, STMPE_KPC_COL
, keypad
->cols
);
280 ret
= stmpe_reg_write(stmpe
, STMPE_KPC_ROW_LSB
, keypad
->rows
);
284 if (variant
->max_rows
> 8) {
285 ret
= stmpe_set_bits(stmpe
, STMPE_KPC_ROW_MSB
,
286 STMPE_KPC_ROW_MSB_ROWS
,
292 ret
= stmpe_set_bits(stmpe
, STMPE_KPC_CTRL_MSB
,
293 STMPE_KPC_CTRL_MSB_SCAN_COUNT
,
294 keypad
->scan_count
<< 4);
298 return stmpe_set_bits(stmpe
, STMPE_KPC_CTRL_LSB
,
299 STMPE_KPC_CTRL_LSB_SCAN
|
300 STMPE_KPC_CTRL_LSB_DEBOUNCE
,
301 STMPE_KPC_CTRL_LSB_SCAN
|
302 (keypad
->debounce_ms
<< 1));
305 static void stmpe_keypad_fill_used_pins(struct stmpe_keypad
*keypad
,
306 u32 used_rows
, u32 used_cols
)
310 for (row
= 0; row
< used_rows
; row
++) {
311 for (col
= 0; col
< used_cols
; col
++) {
312 int code
= MATRIX_SCAN_CODE(row
, col
,
313 STMPE_KEYPAD_ROW_SHIFT
);
314 if (keypad
->keymap
[code
] != KEY_RESERVED
) {
315 keypad
->rows
|= 1 << row
;
316 keypad
->cols
|= 1 << col
;
322 static int stmpe_keypad_probe(struct platform_device
*pdev
)
324 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
325 struct device_node
*np
= pdev
->dev
.of_node
;
326 struct stmpe_keypad
*keypad
;
327 struct input_dev
*input
;
333 irq
= platform_get_irq(pdev
, 0);
337 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(struct stmpe_keypad
),
342 keypad
->stmpe
= stmpe
;
343 keypad
->variant
= &stmpe_keypad_variants
[stmpe
->partnum
];
345 of_property_read_u32(np
, "debounce-interval", &keypad
->debounce_ms
);
346 of_property_read_u32(np
, "st,scan-count", &keypad
->scan_count
);
347 keypad
->no_autorepeat
= of_property_read_bool(np
, "st,no-autorepeat");
349 input
= devm_input_allocate_device(&pdev
->dev
);
353 input
->name
= "STMPE keypad";
354 input
->id
.bustype
= BUS_I2C
;
355 input
->dev
.parent
= &pdev
->dev
;
357 error
= matrix_keypad_parse_of_params(&pdev
->dev
, &rows
, &cols
);
361 error
= matrix_keypad_build_keymap(NULL
, NULL
, rows
, cols
,
362 keypad
->keymap
, input
);
366 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
367 if (!keypad
->no_autorepeat
)
368 __set_bit(EV_REP
, input
->evbit
);
370 stmpe_keypad_fill_used_pins(keypad
, rows
, cols
);
372 keypad
->input
= input
;
374 error
= stmpe_keypad_chip_init(keypad
);
378 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
379 NULL
, stmpe_keypad_irq
,
380 IRQF_ONESHOT
, "stmpe-keypad", keypad
);
382 dev_err(&pdev
->dev
, "unable to get irq: %d\n", error
);
386 error
= input_register_device(input
);
389 "unable to register input device: %d\n", error
);
393 platform_set_drvdata(pdev
, keypad
);
398 static int stmpe_keypad_remove(struct platform_device
*pdev
)
400 struct stmpe_keypad
*keypad
= platform_get_drvdata(pdev
);
402 stmpe_disable(keypad
->stmpe
, STMPE_BLOCK_KEYPAD
);
407 static struct platform_driver stmpe_keypad_driver
= {
408 .driver
.name
= "stmpe-keypad",
409 .driver
.owner
= THIS_MODULE
,
410 .probe
= stmpe_keypad_probe
,
411 .remove
= stmpe_keypad_remove
,
413 module_platform_driver(stmpe_keypad_driver
);
415 MODULE_LICENSE("GPL v2");
416 MODULE_DESCRIPTION("STMPExxxx keypad driver");
417 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");