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_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 * @num_data: number of data bytes
56 * @num_normal_data: number of normal keys' data bytes
57 * @max_cols: maximum number of columns supported
58 * @max_rows: maximum number of rows supported
59 * @col_gpios: bitmask of gpios which can be used for columns
60 * @row_gpios: bitmask of gpios which can be used for rows
62 struct stmpe_keypad_variant
{
68 unsigned int col_gpios
;
69 unsigned int row_gpios
;
72 static const struct stmpe_keypad_variant stmpe_keypad_variants
[] = {
74 .auto_increment
= true,
79 .col_gpios
= 0x000ff, /* GPIO 0 - 7 */
80 .row_gpios
= 0x0ff00, /* GPIO 8 - 15 */
83 .auto_increment
= false,
88 .col_gpios
= 0x0000ff, /* GPIO 0 - 7*/
89 .row_gpios
= 0x1fef00, /* GPIO 8-14, 16-20 */
92 .auto_increment
= true,
97 .col_gpios
= 0x0000ff, /* GPIO 0 - 7*/
98 .row_gpios
= 0x1fef00, /* GPIO 8-14, 16-20 */
102 struct stmpe_keypad
{
104 struct input_dev
*input
;
105 const struct stmpe_keypad_variant
*variant
;
106 const struct stmpe_keypad_platform_data
*plat
;
111 unsigned short keymap
[STMPE_KEYPAD_KEYMAP_SIZE
];
114 static int stmpe_keypad_read_data(struct stmpe_keypad
*keypad
, u8
*data
)
116 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
117 struct stmpe
*stmpe
= keypad
->stmpe
;
121 if (variant
->auto_increment
)
122 return stmpe_block_read(stmpe
, STMPE_KPC_DATA_BYTE0
,
123 variant
->num_data
, data
);
125 for (i
= 0; i
< variant
->num_data
; i
++) {
126 ret
= stmpe_reg_read(stmpe
, STMPE_KPC_DATA_BYTE0
+ i
);
136 static irqreturn_t
stmpe_keypad_irq(int irq
, void *dev
)
138 struct stmpe_keypad
*keypad
= dev
;
139 struct input_dev
*input
= keypad
->input
;
140 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
141 u8 fifo
[variant
->num_data
];
145 ret
= stmpe_keypad_read_data(keypad
, fifo
);
149 for (i
= 0; i
< variant
->num_normal_data
; i
++) {
151 int row
= (data
& STMPE_KPC_DATA_ROW
) >> 3;
152 int col
= data
& STMPE_KPC_DATA_COL
;
153 int code
= MATRIX_SCAN_CODE(row
, col
, STMPE_KEYPAD_ROW_SHIFT
);
154 bool up
= data
& STMPE_KPC_DATA_UP
;
156 if ((data
& STMPE_KPC_DATA_NOKEY_MASK
)
157 == STMPE_KPC_DATA_NOKEY_MASK
)
160 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
161 input_report_key(input
, keypad
->keymap
[code
], !up
);
168 static int stmpe_keypad_altfunc_init(struct stmpe_keypad
*keypad
)
170 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
171 unsigned int col_gpios
= variant
->col_gpios
;
172 unsigned int row_gpios
= variant
->row_gpios
;
173 struct stmpe
*stmpe
= keypad
->stmpe
;
174 unsigned int pins
= 0;
178 * Figure out which pins need to be set to the keypad alternate
181 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
184 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
185 * for the keypad) are used on the board.
188 for (i
= 0; i
< variant
->max_cols
; i
++) {
189 int num
= __ffs(col_gpios
);
191 if (keypad
->cols
& (1 << i
))
194 col_gpios
&= ~(1 << num
);
197 for (i
= 0; i
< variant
->max_rows
; i
++) {
198 int num
= __ffs(row_gpios
);
200 if (keypad
->rows
& (1 << i
))
203 row_gpios
&= ~(1 << num
);
206 return stmpe_set_altfunc(stmpe
, pins
, STMPE_BLOCK_KEYPAD
);
209 static int stmpe_keypad_chip_init(struct stmpe_keypad
*keypad
)
211 const struct stmpe_keypad_platform_data
*plat
= keypad
->plat
;
212 const struct stmpe_keypad_variant
*variant
= keypad
->variant
;
213 struct stmpe
*stmpe
= keypad
->stmpe
;
216 if (plat
->debounce_ms
> STMPE_KEYPAD_MAX_DEBOUNCE
)
219 if (plat
->scan_count
> STMPE_KEYPAD_MAX_SCAN_COUNT
)
222 ret
= stmpe_enable(stmpe
, STMPE_BLOCK_KEYPAD
);
226 ret
= stmpe_keypad_altfunc_init(keypad
);
230 ret
= stmpe_reg_write(stmpe
, STMPE_KPC_COL
, keypad
->cols
);
234 ret
= stmpe_reg_write(stmpe
, STMPE_KPC_ROW_LSB
, keypad
->rows
);
238 if (variant
->max_rows
> 8) {
239 ret
= stmpe_set_bits(stmpe
, STMPE_KPC_ROW_MSB
,
240 STMPE_KPC_ROW_MSB_ROWS
,
246 ret
= stmpe_set_bits(stmpe
, STMPE_KPC_CTRL_MSB
,
247 STMPE_KPC_CTRL_MSB_SCAN_COUNT
,
248 plat
->scan_count
<< 4);
252 return stmpe_set_bits(stmpe
, STMPE_KPC_CTRL_LSB
,
253 STMPE_KPC_CTRL_LSB_SCAN
|
254 STMPE_KPC_CTRL_LSB_DEBOUNCE
,
255 STMPE_KPC_CTRL_LSB_SCAN
|
256 (plat
->debounce_ms
<< 1));
259 static void stmpe_keypad_fill_used_pins(struct stmpe_keypad
*keypad
)
263 for (row
= 0; row
< STMPE_KEYPAD_MAX_ROWS
; row
++) {
264 for (col
= 0; col
< STMPE_KEYPAD_MAX_COLS
; col
++) {
265 int code
= MATRIX_SCAN_CODE(row
, col
,
266 STMPE_KEYPAD_ROW_SHIFT
);
267 if (keypad
->keymap
[code
] != KEY_RESERVED
) {
268 keypad
->rows
|= 1 << row
;
269 keypad
->cols
|= 1 << col
;
276 static const struct stmpe_keypad_platform_data
*
277 stmpe_keypad_of_probe(struct device
*dev
)
279 struct device_node
*np
= dev
->of_node
;
280 struct stmpe_keypad_platform_data
*plat
;
283 return ERR_PTR(-ENODEV
);
285 plat
= devm_kzalloc(dev
, sizeof(*plat
), GFP_KERNEL
);
287 return ERR_PTR(-ENOMEM
);
289 of_property_read_u32(np
, "debounce-interval", &plat
->debounce_ms
);
290 of_property_read_u32(np
, "st,scan-count", &plat
->scan_count
);
292 plat
->no_autorepeat
= of_property_read_bool(np
, "st,no-autorepeat");
297 static inline const struct stmpe_keypad_platform_data
*
298 stmpe_keypad_of_probe(struct device
*dev
)
300 return ERR_PTR(-EINVAL
);
304 static int stmpe_keypad_probe(struct platform_device
*pdev
)
306 struct stmpe
*stmpe
= dev_get_drvdata(pdev
->dev
.parent
);
307 const struct stmpe_keypad_platform_data
*plat
;
308 struct stmpe_keypad
*keypad
;
309 struct input_dev
*input
;
313 plat
= stmpe
->pdata
->keypad
;
315 plat
= stmpe_keypad_of_probe(&pdev
->dev
);
317 return PTR_ERR(plat
);
320 irq
= platform_get_irq(pdev
, 0);
324 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(struct stmpe_keypad
),
329 input
= devm_input_allocate_device(&pdev
->dev
);
333 input
->name
= "STMPE keypad";
334 input
->id
.bustype
= BUS_I2C
;
335 input
->dev
.parent
= &pdev
->dev
;
337 error
= matrix_keypad_build_keymap(plat
->keymap_data
, NULL
,
338 STMPE_KEYPAD_MAX_ROWS
,
339 STMPE_KEYPAD_MAX_COLS
,
340 keypad
->keymap
, input
);
344 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
345 if (!plat
->no_autorepeat
)
346 __set_bit(EV_REP
, input
->evbit
);
348 stmpe_keypad_fill_used_pins(keypad
);
350 keypad
->stmpe
= stmpe
;
352 keypad
->input
= input
;
353 keypad
->variant
= &stmpe_keypad_variants
[stmpe
->partnum
];
355 error
= stmpe_keypad_chip_init(keypad
);
359 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
360 NULL
, stmpe_keypad_irq
,
361 IRQF_ONESHOT
, "stmpe-keypad", keypad
);
363 dev_err(&pdev
->dev
, "unable to get irq: %d\n", error
);
367 error
= input_register_device(input
);
370 "unable to register input device: %d\n", error
);
374 platform_set_drvdata(pdev
, keypad
);
379 static int stmpe_keypad_remove(struct platform_device
*pdev
)
381 struct stmpe_keypad
*keypad
= platform_get_drvdata(pdev
);
383 stmpe_disable(keypad
->stmpe
, STMPE_BLOCK_KEYPAD
);
388 static struct platform_driver stmpe_keypad_driver
= {
389 .driver
.name
= "stmpe-keypad",
390 .driver
.owner
= THIS_MODULE
,
391 .probe
= stmpe_keypad_probe
,
392 .remove
= stmpe_keypad_remove
,
394 module_platform_driver(stmpe_keypad_driver
);
396 MODULE_LICENSE("GPL v2");
397 MODULE_DESCRIPTION("STMPExxxx keypad driver");
398 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");