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/init.h>
10 #include <linux/slab.h>
11 #include <linux/input.h>
12 #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_SIZE \
50 (STMPE_KEYPAD_MAX_ROWS * STMPE_KEYPAD_MAX_COLS)
53 * struct stmpe_keypad_variant - model-specific attributes
54 * @auto_increment: whether the KPC_DATA_BYTE register address
55 * auto-increments on multiple read
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 {
69 unsigned int col_gpios;
70 unsigned int row_gpios;
73 static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
75 .auto_increment = true,
80 .col_gpios = 0x000ff, /* GPIO 0 - 7 */
81 .row_gpios = 0x0ff00, /* GPIO 8 - 15 */
84 .auto_increment = false,
89 .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
90 .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
93 .auto_increment = true,
98 .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
99 .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
103 struct stmpe_keypad {
105 struct input_dev *input;
106 const struct stmpe_keypad_variant *variant;
107 const struct stmpe_keypad_platform_data *plat;
112 unsigned short keymap[STMPE_KEYPAD_KEYMAP_SIZE];
115 static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
117 const struct stmpe_keypad_variant *variant = keypad->variant;
118 struct stmpe *stmpe = keypad->stmpe;
122 if (variant->auto_increment)
123 return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
124 variant->num_data, data);
126 for (i = 0; i < variant->num_data; i++) {
127 ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
137 static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
139 struct stmpe_keypad *keypad = dev;
140 struct input_dev *input = keypad->input;
141 const struct stmpe_keypad_variant *variant = keypad->variant;
142 u8 fifo[variant->num_data];
146 ret = stmpe_keypad_read_data(keypad, fifo);
150 for (i = 0; i < variant->num_normal_data; i++) {
152 int row = (data & STMPE_KPC_DATA_ROW) >> 3;
153 int col = data & STMPE_KPC_DATA_COL;
154 int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
155 bool up = data & STMPE_KPC_DATA_UP;
157 if ((data & STMPE_KPC_DATA_NOKEY_MASK)
158 == STMPE_KPC_DATA_NOKEY_MASK)
161 input_event(input, EV_MSC, MSC_SCAN, code);
162 input_report_key(input, keypad->keymap[code], !up);
169 static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
171 const struct stmpe_keypad_variant *variant = keypad->variant;
172 unsigned int col_gpios = variant->col_gpios;
173 unsigned int row_gpios = variant->row_gpios;
174 struct stmpe *stmpe = keypad->stmpe;
175 unsigned int pins = 0;
179 * Figure out which pins need to be set to the keypad alternate
182 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
185 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
186 * for the keypad) are used on the board.
189 for (i = 0; i < variant->max_cols; i++) {
190 int num = __ffs(col_gpios);
192 if (keypad->cols & (1 << i))
195 col_gpios &= ~(1 << num);
198 for (i = 0; i < variant->max_rows; i++) {
199 int num = __ffs(row_gpios);
201 if (keypad->rows & (1 << i))
204 row_gpios &= ~(1 << num);
207 return stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
210 static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
212 const struct stmpe_keypad_platform_data *plat = keypad->plat;
213 const struct stmpe_keypad_variant *variant = keypad->variant;
214 struct stmpe *stmpe = keypad->stmpe;
217 if (plat->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
220 if (plat->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
223 ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
227 ret = stmpe_keypad_altfunc_init(keypad);
231 ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
235 ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
239 if (variant->max_rows > 8) {
240 ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
241 STMPE_KPC_ROW_MSB_ROWS,
247 ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
248 STMPE_KPC_CTRL_MSB_SCAN_COUNT,
249 plat->scan_count << 4);
253 return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
254 STMPE_KPC_CTRL_LSB_SCAN |
255 STMPE_KPC_CTRL_LSB_DEBOUNCE,
256 STMPE_KPC_CTRL_LSB_SCAN |
257 (plat->debounce_ms << 1));
260 static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad)
264 for (row = 0; row < STMPE_KEYPAD_MAX_ROWS; row++) {
265 for (col = 0; col < STMPE_KEYPAD_MAX_COLS; col++) {
266 int code = MATRIX_SCAN_CODE(row, col,
267 STMPE_KEYPAD_ROW_SHIFT);
268 if (keypad->keymap[code] != KEY_RESERVED) {
269 keypad->rows |= 1 << row;
270 keypad->cols |= 1 << col;
277 static const struct stmpe_keypad_platform_data *
278 stmpe_keypad_of_probe(struct device *dev)
280 struct device_node *np = dev->of_node;
281 struct stmpe_keypad_platform_data *plat;
284 return ERR_PTR(-ENODEV);
286 plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
288 return ERR_PTR(-ENOMEM);
290 of_property_read_u32(np, "debounce-interval", &plat->debounce_ms);
291 of_property_read_u32(np, "st,scan-count", &plat->scan_count);
293 plat->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");
298 static inline const struct stmpe_keypad_platform_data *
299 stmpe_keypad_of_probe(struct device *dev)
301 return ERR_PTR(-EINVAL);
305 static int stmpe_keypad_probe(struct platform_device *pdev)
307 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
308 const struct stmpe_keypad_platform_data *plat;
309 struct stmpe_keypad *keypad;
310 struct input_dev *input;
314 plat = stmpe->pdata->keypad;
316 plat = stmpe_keypad_of_probe(&pdev->dev);
318 return PTR_ERR(plat);
321 irq = platform_get_irq(pdev, 0);
325 keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
330 input = devm_input_allocate_device(&pdev->dev);
334 input->name = "STMPE keypad";
335 input->id.bustype = BUS_I2C;
336 input->dev.parent = &pdev->dev;
338 error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
339 STMPE_KEYPAD_MAX_ROWS,
340 STMPE_KEYPAD_MAX_COLS,
341 keypad->keymap, input);
345 input_set_capability(input, EV_MSC, MSC_SCAN);
346 if (!plat->no_autorepeat)
347 __set_bit(EV_REP, input->evbit);
349 stmpe_keypad_fill_used_pins(keypad);
351 keypad->stmpe = stmpe;
353 keypad->input = input;
354 keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
356 error = stmpe_keypad_chip_init(keypad);
360 error = devm_request_threaded_irq(&pdev->dev, irq,
361 NULL, stmpe_keypad_irq,
362 IRQF_ONESHOT, "stmpe-keypad", keypad);
364 dev_err(&pdev->dev, "unable to get irq: %d\n", error);
368 error = input_register_device(input);
371 "unable to register input device: %d\n", error);
375 platform_set_drvdata(pdev, keypad);
380 static int stmpe_keypad_remove(struct platform_device *pdev)
382 struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
384 stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
389 static struct platform_driver stmpe_keypad_driver = {
390 .driver.name = "stmpe-keypad",
391 .driver.owner = THIS_MODULE,
392 .probe = stmpe_keypad_probe,
393 .remove = stmpe_keypad_remove,
395 module_platform_driver(stmpe_keypad_driver);
397 MODULE_LICENSE("GPL v2");
398 MODULE_DESCRIPTION("STMPExxxx keypad driver");
399 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");