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/init.h>
31#include <linux/interrupt.h>
32#include <linux/input.h>
33#include <linux/platform_device.h>
34#include <linux/i2c/twl.h>
35#include <linux/slab.h>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#define TWL4030_MAX_ROWS 8
53#define TWL4030_MAX_COLS 8
54
55
56
57
58#define TWL4030_ROW_SHIFT 4
59#define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS << TWL4030_ROW_SHIFT)
60
61struct twl4030_keypad {
62 unsigned short keymap[TWL4030_KEYMAP_SIZE];
63 u16 kp_state[TWL4030_MAX_ROWS];
64 unsigned n_rows;
65 unsigned n_cols;
66 unsigned 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 if (col == 0xFF)
171 return 1 << kp->n_cols;
172 else
173 return col & ((1 << kp->n_cols) - 1);
174}
175
176static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
177{
178 u8 new_state[TWL4030_MAX_ROWS];
179 int row;
180 int ret = twl4030_kpread(kp, new_state,
181 KEYP_FULL_CODE_7_0, kp->n_rows);
182 if (ret >= 0)
183 for (row = 0; row < kp->n_rows; row++)
184 state[row] = twl4030_col_xlate(kp, new_state[row]);
185
186 return ret;
187}
188
189static bool twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
190{
191 int i;
192 u16 check = 0;
193
194 for (i = 0; i < kp->n_rows; i++) {
195 u16 col = key_state[i];
196
197 if ((col & check) && hweight16(col) > 1)
198 return true;
199
200 check |= col;
201 }
202
203 return false;
204}
205
206static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
207{
208 struct input_dev *input = kp->input;
209 u16 new_state[TWL4030_MAX_ROWS];
210 int col, row;
211
212 if (release_all)
213 memset(new_state, 0, sizeof(new_state));
214 else {
215
216 int ret = twl4030_read_kp_matrix_state(kp, new_state);
217
218 if (ret < 0)
219 return;
220
221 if (twl4030_is_in_ghost_state(kp, new_state))
222 return;
223 }
224
225
226 for (row = 0; row < kp->n_rows; row++) {
227 int changed = new_state[row] ^ kp->kp_state[row];
228
229 if (!changed)
230 continue;
231
232
233 for (col = 0; col < kp->n_cols + 1; col++) {
234 int code;
235
236 if (!(changed & (1 << col)))
237 continue;
238
239 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
240 (new_state[row] & (1 << col)) ?
241 "press" : "release");
242
243 code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
244 input_event(input, EV_MSC, MSC_SCAN, code);
245 input_report_key(input, kp->keymap[code],
246 new_state[row] & (1 << col));
247 }
248 kp->kp_state[row] = new_state[row];
249 }
250 input_sync(input);
251}
252
253
254
255
256static irqreturn_t do_kp_irq(int irq, void *_kp)
257{
258 struct twl4030_keypad *kp = _kp;
259 u8 reg;
260 int ret;
261
262
263 ret = twl4030_kpread(kp, ®, KEYP_ISR1, 1);
264
265
266
267 if (ret >= 0 && (reg & KEYP_IMR1_KP))
268 twl4030_kp_scan(kp, false);
269 else
270 twl4030_kp_scan(kp, true);
271
272 return IRQ_HANDLED;
273}
274
275static int __devinit twl4030_kp_program(struct twl4030_keypad *kp)
276{
277 u8 reg;
278 int i;
279
280
281 reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
282 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
283 if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
284 return -EIO;
285
286
287
288
289
290
291 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
292 if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
293 return -EIO;
294
295
296 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
297 if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
298 return -EIO;
299
300
301 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
302 if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
303 return -EIO;
304
305
306 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
307 if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
308 return -EIO;
309
310 if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
311 return -EIO;
312
313
314
315
316
317 reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
318 if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
319 return -EIO;
320
321
322 if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
323 return -EIO;
324
325 return 0;
326}
327
328
329
330
331
332static int __devinit twl4030_kp_probe(struct platform_device *pdev)
333{
334 struct twl4030_keypad_data *pdata = pdev->dev.platform_data;
335 const struct matrix_keymap_data *keymap_data;
336 struct twl4030_keypad *kp;
337 struct input_dev *input;
338 u8 reg;
339 int error;
340
341 if (!pdata || !pdata->rows || !pdata->cols || !pdata->keymap_data ||
342 pdata->rows > TWL4030_MAX_ROWS || pdata->cols > TWL4030_MAX_COLS) {
343 dev_err(&pdev->dev, "Invalid platform_data\n");
344 return -EINVAL;
345 }
346
347 keymap_data = pdata->keymap_data;
348
349 kp = kzalloc(sizeof(*kp), GFP_KERNEL);
350 input = input_allocate_device();
351 if (!kp || !input) {
352 error = -ENOMEM;
353 goto err1;
354 }
355
356
357 kp->dbg_dev = &pdev->dev;
358 kp->input = input;
359
360 kp->n_rows = pdata->rows;
361 kp->n_cols = pdata->cols;
362 kp->irq = platform_get_irq(pdev, 0);
363
364
365 __set_bit(EV_KEY, input->evbit);
366
367
368 if (pdata->rep)
369 __set_bit(EV_REP, input->evbit);
370
371 input_set_capability(input, EV_MSC, MSC_SCAN);
372
373 input->name = "TWL4030 Keypad";
374 input->phys = "twl4030_keypad/input0";
375 input->dev.parent = &pdev->dev;
376
377 input->id.bustype = BUS_HOST;
378 input->id.vendor = 0x0001;
379 input->id.product = 0x0001;
380 input->id.version = 0x0003;
381
382 input->keycode = kp->keymap;
383 input->keycodesize = sizeof(kp->keymap[0]);
384 input->keycodemax = ARRAY_SIZE(kp->keymap);
385
386 matrix_keypad_build_keymap(keymap_data, TWL4030_ROW_SHIFT,
387 input->keycode, input->keybit);
388
389 error = input_register_device(input);
390 if (error) {
391 dev_err(kp->dbg_dev,
392 "Unable to register twl4030 keypad device\n");
393 goto err1;
394 }
395
396 error = twl4030_kp_program(kp);
397 if (error)
398 goto err2;
399
400
401
402
403
404
405
406 error = request_threaded_irq(kp->irq, NULL, do_kp_irq,
407 0, pdev->name, kp);
408 if (error) {
409 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",
410 kp->irq);
411 goto err2;
412 }
413
414
415 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
416 if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
417 error = -EIO;
418 goto err3;
419 }
420
421 platform_set_drvdata(pdev, kp);
422 return 0;
423
424err3:
425
426 (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
427 free_irq(kp->irq, NULL);
428err2:
429 input_unregister_device(input);
430 input = NULL;
431err1:
432 input_free_device(input);
433 kfree(kp);
434 return error;
435}
436
437static int __devexit twl4030_kp_remove(struct platform_device *pdev)
438{
439 struct twl4030_keypad *kp = platform_get_drvdata(pdev);
440
441 free_irq(kp->irq, kp);
442 input_unregister_device(kp->input);
443 platform_set_drvdata(pdev, NULL);
444 kfree(kp);
445
446 return 0;
447}
448
449
450
451
452
453
454
455static struct platform_driver twl4030_kp_driver = {
456 .probe = twl4030_kp_probe,
457 .remove = __devexit_p(twl4030_kp_remove),
458 .driver = {
459 .name = "twl4030_keypad",
460 .owner = THIS_MODULE,
461 },
462};
463
464static int __init twl4030_kp_init(void)
465{
466 return platform_driver_register(&twl4030_kp_driver);
467}
468module_init(twl4030_kp_init);
469
470static void __exit twl4030_kp_exit(void)
471{
472 platform_driver_unregister(&twl4030_kp_driver);
473}
474module_exit(twl4030_kp_exit);
475
476MODULE_AUTHOR("Texas Instruments");
477MODULE_DESCRIPTION("TWL4030 Keypad Driver");
478MODULE_LICENSE("GPL");
479MODULE_ALIAS("platform:twl4030_keypad");
480
481