1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/interrupt.h>
31#include <linux/input.h>
32#include <linux/platform_device.h>
33#include <linux/i2c/twl.h>
34#include <linux/slab.h>
35#include <linux/of.h>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#define TWL4030_MAX_ROWS 8
52#define TWL4030_MAX_COLS 8
53
54
55
56
57#define TWL4030_ROW_SHIFT 4
58#define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)
59
60struct twl4030_keypad {
61 unsigned short keymap[TWL4030_KEYMAP_SIZE];
62 u16 kp_state[TWL4030_MAX_ROWS];
63 bool autorepeat;
64 unsigned int n_rows;
65 unsigned int n_cols;
66 unsigned int irq;
67
68 struct device *dbg_dev;
69 struct input_dev *input;
70};
71
72
73
74
75#define PTV_PRESCALER 4
76
77
78#define KEYP_CTRL 0x00
79#define KEYP_DEB 0x01
80#define KEYP_LONG_KEY 0x02
81#define KEYP_LK_PTV 0x03
82#define KEYP_TIMEOUT_L 0x04
83#define KEYP_TIMEOUT_H 0x05
84#define KEYP_KBC 0x06
85#define KEYP_KBR 0x07
86#define KEYP_SMS 0x08
87#define KEYP_FULL_CODE_7_0 0x09
88#define KEYP_FULL_CODE_15_8 0x0a
89#define KEYP_FULL_CODE_23_16 0x0b
90#define KEYP_FULL_CODE_31_24 0x0c
91#define KEYP_FULL_CODE_39_32 0x0d
92#define KEYP_FULL_CODE_47_40 0x0e
93#define KEYP_FULL_CODE_55_48 0x0f
94#define KEYP_FULL_CODE_63_56 0x10
95#define KEYP_ISR1 0x11
96#define KEYP_IMR1 0x12
97#define KEYP_ISR2 0x13
98#define KEYP_IMR2 0x14
99#define KEYP_SIR 0x15
100#define KEYP_EDR 0x16
101#define KEYP_SIH_CTRL 0x17
102
103
104#define KEYP_CTRL_SOFT_NRST BIT(0)
105#define KEYP_CTRL_SOFTMODEN BIT(1)
106#define KEYP_CTRL_LK_EN BIT(2)
107#define KEYP_CTRL_TOE_EN BIT(3)
108#define KEYP_CTRL_TOLE_EN BIT(4)
109#define KEYP_CTRL_RP_EN BIT(5)
110#define KEYP_CTRL_KBD_ON BIT(6)
111
112
113#define KEYP_PERIOD_US(t, prescale) ((t) / (31 << ((prescale) + 1)) - 1)
114
115
116#define KEYP_LK_PTV_PTV_SHIFT 5
117
118
119#define KEYP_IMR1_MIS BIT(3)
120#define KEYP_IMR1_TO BIT(2)
121#define KEYP_IMR1_LK BIT(1)
122#define KEYP_IMR1_KP BIT(0)
123
124
125#define KEYP_EDR_KP_FALLING 0x01
126#define KEYP_EDR_KP_RISING 0x02
127#define KEYP_EDR_KP_BOTH 0x03
128#define KEYP_EDR_LK_FALLING 0x04
129#define KEYP_EDR_LK_RISING 0x08
130#define KEYP_EDR_TO_FALLING 0x10
131#define KEYP_EDR_TO_RISING 0x20
132#define KEYP_EDR_MIS_FALLING 0x40
133#define KEYP_EDR_MIS_RISING 0x80
134
135
136
137
138static int twl4030_kpread(struct twl4030_keypad *kp,
139 u8 *data, u32 reg, u8 num_bytes)
140{
141 int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
142
143 if (ret < 0)
144 dev_warn(kp->dbg_dev,
145 "Couldn't read TWL4030: %X - ret %d[%x]\n",
146 reg, ret, ret);
147
148 return ret;
149}
150
151static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
152{
153 int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
154
155 if (ret < 0)
156 dev_warn(kp->dbg_dev,
157 "Could not write TWL4030: %X - ret %d[%x]\n",
158 reg, ret, ret);
159
160 return ret;
161}
162
163static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
164{
165
166
167
168
169
170
171 if (col == 0xFF)
172 return 1 << kp->n_cols;
173 else
174 return col & ((1 << kp->n_cols) - 1);
175}
176
177static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
178{
179 u8 new_state[TWL4030_MAX_ROWS];
180 int row;
181 int ret = twl4030_kpread(kp, new_state,
182 KEYP_FULL_CODE_7_0, kp->n_rows);
183 if (ret >= 0)
184 for (row = 0; row < kp->n_rows; row++)
185 state[row] = twl4030_col_xlate(kp, new_state[row]);
186
187 return ret;
188}
189
190static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
191{
192 int i;
193 u16 check = 0;
194
195 for (i = 0; i < kp->n_rows; i++) {
196 u16 col = key_state[i];
197
198 if ((col & check) && hweight16(col) > 1)
199 return true;
200
201 check |= col;
202 }
203
204 return false;
205}
206
207static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
208{
209 struct input_dev *input = kp->input;
210 u16 new_state[TWL4030_MAX_ROWS];
211 int col, row;
212
213 if (release_all) {
214 memset(new_state, 0, sizeof(new_state));
215 } else {
216
217 int ret = twl4030_read_kp_matrix_state(kp, new_state);
218
219 if (ret < 0)
220 return;
221
222 if (twl4030_is_in_ghost_state(kp, new_state))
223 return;
224 }
225
226
227 for (row = 0; row < kp->n_rows; row++) {
228 int changed = new_state[row] ^ kp->kp_state[row];
229
230 if (!changed)
231 continue;
232
233
234 for (col = 0; col < kp->n_cols + 1; col++) {
235 int code;
236
237 if (!(changed & (1 << col)))
238 continue;
239
240 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
241 (new_state[row] & (1 << col)) ?
242 "press" : "release");
243
244 code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
245 input_event(input, EV_MSC, MSC_SCAN, code);
246 input_report_key(input, kp->keymap[code],
247 new_state[row] & (1 << col));
248 }
249 kp->kp_state[row] = new_state[row];
250 }
251 input_sync(input);
252}
253
254
255
256
257static irqreturn_t do_kp_irq(int irq, void *_kp)
258{
259 struct twl4030_keypad *kp = _kp;
260 u8 reg;
261 int ret;
262
263
264 ret = twl4030_kpread(kp, ®, KEYP_ISR1, 1);
265
266
267
268
269
270 if (ret >= 0 && (reg & KEYP_IMR1_KP))
271 twl4030_kp_scan(kp, false);
272 else
273 twl4030_kp_scan(kp, true);
274
275 return IRQ_HANDLED;
276}
277
278static int twl4030_kp_program(struct twl4030_keypad *kp)
279{
280 u8 reg;
281 int i;
282
283
284 reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
285 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
286 if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
287 return -EIO;
288
289
290
291
292
293
294
295 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
296 if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
297 return -EIO;
298
299
300 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
301 if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
302 return -EIO;
303
304
305 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
306 if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
307 return -EIO;
308
309
310 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
311 if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
312 return -EIO;
313
314 if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
315 return -EIO;
316
317
318
319
320
321 reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
322 if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
323 return -EIO;
324
325
326 if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
327 return -EIO;
328
329 return 0;
330}
331
332
333
334
335
336static int twl4030_kp_probe(struct platform_device *pdev)
337{
338 struct twl4030_keypad_data *pdata = dev_get_platdata(&pdev->dev);
339 const struct matrix_keymap_data *keymap_data = NULL;
340 struct twl4030_keypad *kp;
341 struct input_dev *input;
342 u8 reg;
343 int error;
344
345 kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL);
346 if (!kp)
347 return -ENOMEM;
348
349 input = devm_input_allocate_device(&pdev->dev);
350 if (!input)
351 return -ENOMEM;
352
353
354 kp->dbg_dev = &pdev->dev;
355 kp->input = input;
356
357
358 input->name = "TWL4030 Keypad";
359 input->phys = "twl4030_keypad/input0";
360
361 input->id.bustype = BUS_HOST;
362 input->id.vendor = 0x0001;
363 input->id.product = 0x0001;
364 input->id.version = 0x0003;
365
366 if (pdata) {
367 if (!pdata->rows || !pdata->cols || !pdata->keymap_data) {
368 dev_err(&pdev->dev, "Missing platform_data\n");
369 return -EINVAL;
370 }
371
372 kp->n_rows = pdata->rows;
373 kp->n_cols = pdata->cols;
374 kp->autorepeat = pdata->rep;
375 keymap_data = pdata->keymap_data;
376 } else {
377 error = matrix_keypad_parse_of_params(&pdev->dev, &kp->n_rows,
378 &kp->n_cols);
379 if (error)
380 return error;
381
382 kp->autorepeat = true;
383 }
384
385 if (kp->n_rows > TWL4030_MAX_ROWS || kp->n_cols > TWL4030_MAX_COLS) {
386 dev_err(&pdev->dev,
387 "Invalid rows/cols amount specified in platform/devicetree data\n");
388 return -EINVAL;
389 }
390
391 kp->irq = platform_get_irq(pdev, 0);
392 if (!kp->irq) {
393 dev_err(&pdev->dev, "no keyboard irq assigned\n");
394 return -EINVAL;
395 }
396
397 error = matrix_keypad_build_keymap(keymap_data, NULL,
398 TWL4030_MAX_ROWS,
399 1 << TWL4030_ROW_SHIFT,
400 kp->keymap, input);
401 if (error) {
402 dev_err(kp->dbg_dev, "Failed to build keymap\n");
403 return error;
404 }
405
406 input_set_capability(input, EV_MSC, MSC_SCAN);
407
408 if (kp->autorepeat)
409 __set_bit(EV_REP, input->evbit);
410
411 error = input_register_device(input);
412 if (error) {
413 dev_err(kp->dbg_dev,
414 "Unable to register twl4030 keypad device\n");
415 return error;
416 }
417
418 error = twl4030_kp_program(kp);
419 if (error)
420 return error;
421
422
423
424
425
426
427
428 error = devm_request_threaded_irq(&pdev->dev, kp->irq, NULL, do_kp_irq,
429 0, pdev->name, kp);
430 if (error) {
431 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d: %d\n",
432 kp->irq, error);
433 return error;
434 }
435
436
437 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
438 if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
439
440 (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
441 return -EIO;
442 }
443
444 platform_set_drvdata(pdev, kp);
445 return 0;
446}
447
448#ifdef CONFIG_OF
449static const struct of_device_id twl4030_keypad_dt_match_table[] = {
450 { .compatible = "ti,twl4030-keypad" },
451 {},
452};
453MODULE_DEVICE_TABLE(of, twl4030_keypad_dt_match_table);
454#endif
455
456
457
458
459
460
461
462static struct platform_driver twl4030_kp_driver = {
463 .probe = twl4030_kp_probe,
464 .driver = {
465 .name = "twl4030_keypad",
466 .of_match_table = of_match_ptr(twl4030_keypad_dt_match_table),
467 },
468};
469module_platform_driver(twl4030_kp_driver);
470
471MODULE_AUTHOR("Texas Instruments");
472MODULE_DESCRIPTION("TWL4030 Keypad Driver");
473MODULE_LICENSE("GPL");
474MODULE_ALIAS("platform:twl4030_keypad");
475