Input: stmpe - enforce device tree only mode
[deliverable/linux.git] / drivers / input / keyboard / stmpe-keypad.c
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
3 *
4 * License Terms: GNU General Public License, version 2
5 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
6 */
7
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>
15
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
30
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)
34
35 #define STMPE_KPC_ROW_MSB_ROWS 0xff
36
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
41
42 #define STMPE_KEYPAD_MAX_DEBOUNCE 127
43 #define STMPE_KEYPAD_MAX_SCAN_COUNT 15
44
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)
50
51 /**
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
61 */
62 struct stmpe_keypad_variant {
63 bool auto_increment;
64 int num_data;
65 int num_normal_data;
66 int max_cols;
67 int max_rows;
68 unsigned int col_gpios;
69 unsigned int row_gpios;
70 };
71
72 static const struct stmpe_keypad_variant stmpe_keypad_variants[] = {
73 [STMPE1601] = {
74 .auto_increment = true,
75 .num_data = 5,
76 .num_normal_data = 3,
77 .max_cols = 8,
78 .max_rows = 8,
79 .col_gpios = 0x000ff, /* GPIO 0 - 7 */
80 .row_gpios = 0x0ff00, /* GPIO 8 - 15 */
81 },
82 [STMPE2401] = {
83 .auto_increment = false,
84 .num_data = 3,
85 .num_normal_data = 2,
86 .max_cols = 8,
87 .max_rows = 12,
88 .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
89 .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
90 },
91 [STMPE2403] = {
92 .auto_increment = true,
93 .num_data = 5,
94 .num_normal_data = 3,
95 .max_cols = 8,
96 .max_rows = 12,
97 .col_gpios = 0x0000ff, /* GPIO 0 - 7*/
98 .row_gpios = 0x1fef00, /* GPIO 8-14, 16-20 */
99 },
100 };
101
102 /**
103 * struct stmpe_keypad - STMPE keypad state container
104 * @stmpe: pointer to parent STMPE device
105 * @input: spawned input device
106 * @variant: STMPE variant
107 * @debounce_ms: debounce interval, in ms. Maximum is
108 * %STMPE_KEYPAD_MAX_DEBOUNCE.
109 * @scan_count: number of key scanning cycles to confirm key data.
110 * Maximum is %STMPE_KEYPAD_MAX_SCAN_COUNT.
111 * @no_autorepeat: disable key autorepeat
112 * @rows: bitmask for the rows
113 * @cols: bitmask for the columns
114 * @keymap: the keymap
115 */
116 struct stmpe_keypad {
117 struct stmpe *stmpe;
118 struct input_dev *input;
119 const struct stmpe_keypad_variant *variant;
120 unsigned int debounce_ms;
121 unsigned int scan_count;
122 bool no_autorepeat;
123 unsigned int rows;
124 unsigned int cols;
125 unsigned short keymap[STMPE_KEYPAD_KEYMAP_MAX_SIZE];
126 };
127
128 static int stmpe_keypad_read_data(struct stmpe_keypad *keypad, u8 *data)
129 {
130 const struct stmpe_keypad_variant *variant = keypad->variant;
131 struct stmpe *stmpe = keypad->stmpe;
132 int ret;
133 int i;
134
135 if (variant->auto_increment)
136 return stmpe_block_read(stmpe, STMPE_KPC_DATA_BYTE0,
137 variant->num_data, data);
138
139 for (i = 0; i < variant->num_data; i++) {
140 ret = stmpe_reg_read(stmpe, STMPE_KPC_DATA_BYTE0 + i);
141 if (ret < 0)
142 return ret;
143
144 data[i] = ret;
145 }
146
147 return 0;
148 }
149
150 static irqreturn_t stmpe_keypad_irq(int irq, void *dev)
151 {
152 struct stmpe_keypad *keypad = dev;
153 struct input_dev *input = keypad->input;
154 const struct stmpe_keypad_variant *variant = keypad->variant;
155 u8 fifo[variant->num_data];
156 int ret;
157 int i;
158
159 ret = stmpe_keypad_read_data(keypad, fifo);
160 if (ret < 0)
161 return IRQ_NONE;
162
163 for (i = 0; i < variant->num_normal_data; i++) {
164 u8 data = fifo[i];
165 int row = (data & STMPE_KPC_DATA_ROW) >> 3;
166 int col = data & STMPE_KPC_DATA_COL;
167 int code = MATRIX_SCAN_CODE(row, col, STMPE_KEYPAD_ROW_SHIFT);
168 bool up = data & STMPE_KPC_DATA_UP;
169
170 if ((data & STMPE_KPC_DATA_NOKEY_MASK)
171 == STMPE_KPC_DATA_NOKEY_MASK)
172 continue;
173
174 input_event(input, EV_MSC, MSC_SCAN, code);
175 input_report_key(input, keypad->keymap[code], !up);
176 input_sync(input);
177 }
178
179 return IRQ_HANDLED;
180 }
181
182 static int stmpe_keypad_altfunc_init(struct stmpe_keypad *keypad)
183 {
184 const struct stmpe_keypad_variant *variant = keypad->variant;
185 unsigned int col_gpios = variant->col_gpios;
186 unsigned int row_gpios = variant->row_gpios;
187 struct stmpe *stmpe = keypad->stmpe;
188 unsigned int pins = 0;
189 int i;
190
191 /*
192 * Figure out which pins need to be set to the keypad alternate
193 * function.
194 *
195 * {cols,rows}_gpios are bitmasks of which pins on the chip can be used
196 * for the keypad.
197 *
198 * keypad->{cols,rows} are a bitmask of which pins (of the ones useable
199 * for the keypad) are used on the board.
200 */
201
202 for (i = 0; i < variant->max_cols; i++) {
203 int num = __ffs(col_gpios);
204
205 if (keypad->cols & (1 << i))
206 pins |= 1 << num;
207
208 col_gpios &= ~(1 << num);
209 }
210
211 for (i = 0; i < variant->max_rows; i++) {
212 int num = __ffs(row_gpios);
213
214 if (keypad->rows & (1 << i))
215 pins |= 1 << num;
216
217 row_gpios &= ~(1 << num);
218 }
219
220 return stmpe_set_altfunc(stmpe, pins, STMPE_BLOCK_KEYPAD);
221 }
222
223 static int stmpe_keypad_chip_init(struct stmpe_keypad *keypad)
224 {
225 const struct stmpe_keypad_variant *variant = keypad->variant;
226 struct stmpe *stmpe = keypad->stmpe;
227 int ret;
228
229 if (keypad->debounce_ms > STMPE_KEYPAD_MAX_DEBOUNCE)
230 return -EINVAL;
231
232 if (keypad->scan_count > STMPE_KEYPAD_MAX_SCAN_COUNT)
233 return -EINVAL;
234
235 ret = stmpe_enable(stmpe, STMPE_BLOCK_KEYPAD);
236 if (ret < 0)
237 return ret;
238
239 ret = stmpe_keypad_altfunc_init(keypad);
240 if (ret < 0)
241 return ret;
242
243 ret = stmpe_reg_write(stmpe, STMPE_KPC_COL, keypad->cols);
244 if (ret < 0)
245 return ret;
246
247 ret = stmpe_reg_write(stmpe, STMPE_KPC_ROW_LSB, keypad->rows);
248 if (ret < 0)
249 return ret;
250
251 if (variant->max_rows > 8) {
252 ret = stmpe_set_bits(stmpe, STMPE_KPC_ROW_MSB,
253 STMPE_KPC_ROW_MSB_ROWS,
254 keypad->rows >> 8);
255 if (ret < 0)
256 return ret;
257 }
258
259 ret = stmpe_set_bits(stmpe, STMPE_KPC_CTRL_MSB,
260 STMPE_KPC_CTRL_MSB_SCAN_COUNT,
261 keypad->scan_count << 4);
262 if (ret < 0)
263 return ret;
264
265 return stmpe_set_bits(stmpe, STMPE_KPC_CTRL_LSB,
266 STMPE_KPC_CTRL_LSB_SCAN |
267 STMPE_KPC_CTRL_LSB_DEBOUNCE,
268 STMPE_KPC_CTRL_LSB_SCAN |
269 (keypad->debounce_ms << 1));
270 }
271
272 static void stmpe_keypad_fill_used_pins(struct stmpe_keypad *keypad,
273 u32 used_rows, u32 used_cols)
274 {
275 int row, col;
276
277 for (row = 0; row < used_rows; row++) {
278 for (col = 0; col < used_cols; col++) {
279 int code = MATRIX_SCAN_CODE(row, col,
280 STMPE_KEYPAD_ROW_SHIFT);
281 if (keypad->keymap[code] != KEY_RESERVED) {
282 keypad->rows |= 1 << row;
283 keypad->cols |= 1 << col;
284 }
285 }
286 }
287 }
288
289 static int stmpe_keypad_probe(struct platform_device *pdev)
290 {
291 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
292 struct device_node *np = pdev->dev.of_node;
293 struct stmpe_keypad *keypad;
294 struct input_dev *input;
295 u32 rows;
296 u32 cols;
297 int error;
298 int irq;
299
300 irq = platform_get_irq(pdev, 0);
301 if (irq < 0)
302 return irq;
303
304 keypad = devm_kzalloc(&pdev->dev, sizeof(struct stmpe_keypad),
305 GFP_KERNEL);
306 if (!keypad)
307 return -ENOMEM;
308
309 keypad->stmpe = stmpe;
310 keypad->variant = &stmpe_keypad_variants[stmpe->partnum];
311
312 of_property_read_u32(np, "debounce-interval", &keypad->debounce_ms);
313 of_property_read_u32(np, "st,scan-count", &keypad->scan_count);
314 keypad->no_autorepeat = of_property_read_bool(np, "st,no-autorepeat");
315
316 input = devm_input_allocate_device(&pdev->dev);
317 if (!input)
318 return -ENOMEM;
319
320 input->name = "STMPE keypad";
321 input->id.bustype = BUS_I2C;
322 input->dev.parent = &pdev->dev;
323
324 error = matrix_keypad_parse_of_params(&pdev->dev, &rows, &cols);
325 if (error)
326 return error;
327
328 error = matrix_keypad_build_keymap(NULL, NULL, rows, cols,
329 keypad->keymap, input);
330 if (error)
331 return error;
332
333 input_set_capability(input, EV_MSC, MSC_SCAN);
334 if (!keypad->no_autorepeat)
335 __set_bit(EV_REP, input->evbit);
336
337 stmpe_keypad_fill_used_pins(keypad, rows, cols);
338
339 keypad->input = input;
340
341 error = stmpe_keypad_chip_init(keypad);
342 if (error < 0)
343 return error;
344
345 error = devm_request_threaded_irq(&pdev->dev, irq,
346 NULL, stmpe_keypad_irq,
347 IRQF_ONESHOT, "stmpe-keypad", keypad);
348 if (error) {
349 dev_err(&pdev->dev, "unable to get irq: %d\n", error);
350 return error;
351 }
352
353 error = input_register_device(input);
354 if (error) {
355 dev_err(&pdev->dev,
356 "unable to register input device: %d\n", error);
357 return error;
358 }
359
360 platform_set_drvdata(pdev, keypad);
361
362 return 0;
363 }
364
365 static int stmpe_keypad_remove(struct platform_device *pdev)
366 {
367 struct stmpe_keypad *keypad = platform_get_drvdata(pdev);
368
369 stmpe_disable(keypad->stmpe, STMPE_BLOCK_KEYPAD);
370
371 return 0;
372 }
373
374 static struct platform_driver stmpe_keypad_driver = {
375 .driver.name = "stmpe-keypad",
376 .driver.owner = THIS_MODULE,
377 .probe = stmpe_keypad_probe,
378 .remove = stmpe_keypad_remove,
379 };
380 module_platform_driver(stmpe_keypad_driver);
381
382 MODULE_LICENSE("GPL v2");
383 MODULE_DESCRIPTION("STMPExxxx keypad driver");
384 MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");
This page took 0.062005 seconds and 5 git commands to generate.