]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/input/keyboard/twl4030_keypad.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-artful-kernel.git] / drivers / input / keyboard / twl4030_keypad.c
CommitLineData
9d834068
DB
1/*
2 * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
3 *
4 * Copyright (C) 2007 Texas Instruments, Inc.
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * Code re-written for 2430SDP by:
8 * Syed Mohammed Khasim <x0khasim@ti.com>
9 *
10 * Initial Code:
11 * Manjunatha G K <manjugk@ti.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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>
b07682b6 34#include <linux/i2c/twl.h>
5a0e3ad6 35#include <linux/slab.h>
9d834068
DB
36
37
38/*
39 * The TWL4030 family chips include a keypad controller that supports
40 * up to an 8x8 switch matrix. The controller can issue system wakeup
41 * events, since it uses only the always-on 32KiHz oscillator, and has
42 * an internal state machine that decodes pressed keys, including
43 * multi-key combinations.
44 *
45 * This driver lets boards define what keycodes they wish to report for
46 * which scancodes, as part of the "struct twl4030_keypad_data" used in
47 * the probe() routine.
48 *
49 * See the TPS65950 documentation; that's the general availability
50 * version of the TWL5030 second generation part.
51 */
52#define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */
53#define TWL4030_MAX_COLS 8
54#define TWL4030_ROW_SHIFT 3
55#define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS * TWL4030_MAX_COLS)
56
57struct twl4030_keypad {
58 unsigned short keymap[TWL4030_KEYMAP_SIZE];
59 u16 kp_state[TWL4030_MAX_ROWS];
60 unsigned n_rows;
61 unsigned n_cols;
62 unsigned irq;
63
64 struct device *dbg_dev;
65 struct input_dev *input;
66};
67
68/*----------------------------------------------------------------------*/
69
70/* arbitrary prescaler value 0..7 */
71#define PTV_PRESCALER 4
72
73/* Register Offsets */
74#define KEYP_CTRL 0x00
75#define KEYP_DEB 0x01
76#define KEYP_LONG_KEY 0x02
77#define KEYP_LK_PTV 0x03
78#define KEYP_TIMEOUT_L 0x04
79#define KEYP_TIMEOUT_H 0x05
80#define KEYP_KBC 0x06
81#define KEYP_KBR 0x07
82#define KEYP_SMS 0x08
83#define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */
84#define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */
85#define KEYP_FULL_CODE_23_16 0x0b
86#define KEYP_FULL_CODE_31_24 0x0c
87#define KEYP_FULL_CODE_39_32 0x0d
88#define KEYP_FULL_CODE_47_40 0x0e
89#define KEYP_FULL_CODE_55_48 0x0f
90#define KEYP_FULL_CODE_63_56 0x10
91#define KEYP_ISR1 0x11
92#define KEYP_IMR1 0x12
93#define KEYP_ISR2 0x13
94#define KEYP_IMR2 0x14
95#define KEYP_SIR 0x15
96#define KEYP_EDR 0x16 /* edge triggers */
97#define KEYP_SIH_CTRL 0x17
98
99/* KEYP_CTRL_REG Fields */
100#define KEYP_CTRL_SOFT_NRST BIT(0)
101#define KEYP_CTRL_SOFTMODEN BIT(1)
102#define KEYP_CTRL_LK_EN BIT(2)
103#define KEYP_CTRL_TOE_EN BIT(3)
104#define KEYP_CTRL_TOLE_EN BIT(4)
105#define KEYP_CTRL_RP_EN BIT(5)
106#define KEYP_CTRL_KBD_ON BIT(6)
107
108/* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
109#define KEYP_PERIOD_US(t, prescale) ((t) / (31 << (prescale + 1)) - 1)
110
111/* KEYP_LK_PTV_REG Fields */
112#define KEYP_LK_PTV_PTV_SHIFT 5
113
114/* KEYP_{IMR,ISR,SIR} Fields */
115#define KEYP_IMR1_MIS BIT(3)
116#define KEYP_IMR1_TO BIT(2)
117#define KEYP_IMR1_LK BIT(1)
118#define KEYP_IMR1_KP BIT(0)
119
120/* KEYP_EDR Fields */
121#define KEYP_EDR_KP_FALLING 0x01
122#define KEYP_EDR_KP_RISING 0x02
123#define KEYP_EDR_KP_BOTH 0x03
124#define KEYP_EDR_LK_FALLING 0x04
125#define KEYP_EDR_LK_RISING 0x08
126#define KEYP_EDR_TO_FALLING 0x10
127#define KEYP_EDR_TO_RISING 0x20
128#define KEYP_EDR_MIS_FALLING 0x40
129#define KEYP_EDR_MIS_RISING 0x80
130
131
132/*----------------------------------------------------------------------*/
133
134static int twl4030_kpread(struct twl4030_keypad *kp,
135 u8 *data, u32 reg, u8 num_bytes)
136{
fc7b92fc 137 int ret = twl_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
9d834068
DB
138
139 if (ret < 0)
140 dev_warn(kp->dbg_dev,
141 "Couldn't read TWL4030: %X - ret %d[%x]\n",
142 reg, ret, ret);
143
144 return ret;
145}
146
147static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
148{
fc7b92fc 149 int ret = twl_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
9d834068
DB
150
151 if (ret < 0)
152 dev_warn(kp->dbg_dev,
153 "Could not write TWL4030: %X - ret %d[%x]\n",
154 reg, ret, ret);
155
156 return ret;
157}
158
159static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
160{
161 /* If all bits in a row are active for all coloumns then
162 * we have that row line connected to gnd. Mark this
163 * key on as if it was on matrix position n_cols (ie
164 * one higher than the size of the matrix).
165 */
166 if (col == 0xFF)
167 return 1 << kp->n_cols;
168 else
169 return col & ((1 << kp->n_cols) - 1);
170}
171
172static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
173{
174 u8 new_state[TWL4030_MAX_ROWS];
175 int row;
176 int ret = twl4030_kpread(kp, new_state,
177 KEYP_FULL_CODE_7_0, kp->n_rows);
178 if (ret >= 0)
179 for (row = 0; row < kp->n_rows; row++)
180 state[row] = twl4030_col_xlate(kp, new_state[row]);
181
182 return ret;
183}
184
185static int twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
186{
187 int i;
188 u16 check = 0;
189
190 for (i = 0; i < kp->n_rows; i++) {
191 u16 col = key_state[i];
192
193 if ((col & check) && hweight16(col) > 1)
194 return 1;
195
196 check |= col;
197 }
198
199 return 0;
200}
201
202static void twl4030_kp_scan(struct twl4030_keypad *kp, bool release_all)
203{
204 struct input_dev *input = kp->input;
205 u16 new_state[TWL4030_MAX_ROWS];
206 int col, row;
207
208 if (release_all)
209 memset(new_state, 0, sizeof(new_state));
210 else {
211 /* check for any changes */
212 int ret = twl4030_read_kp_matrix_state(kp, new_state);
213
214 if (ret < 0) /* panic ... */
215 return;
216
217 if (twl4030_is_in_ghost_state(kp, new_state))
218 return;
219 }
220
221 /* check for changes and print those */
222 for (row = 0; row < kp->n_rows; row++) {
223 int changed = new_state[row] ^ kp->kp_state[row];
224
225 if (!changed)
226 continue;
227
228 for (col = 0; col < kp->n_cols; col++) {
229 int code;
230
231 if (!(changed & (1 << col)))
232 continue;
233
234 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
235 (new_state[row] & (1 << col)) ?
236 "press" : "release");
237
238 code = MATRIX_SCAN_CODE(row, col, TWL4030_ROW_SHIFT);
239 input_event(input, EV_MSC, MSC_SCAN, code);
240 input_report_key(input, kp->keymap[code],
241 new_state[row] & (1 << col));
242 }
243 kp->kp_state[row] = new_state[row];
244 }
245 input_sync(input);
246}
247
248/*
249 * Keypad interrupt handler
250 */
251static irqreturn_t do_kp_irq(int irq, void *_kp)
252{
253 struct twl4030_keypad *kp = _kp;
254 u8 reg;
255 int ret;
256
9d834068
DB
257 /* Read & Clear TWL4030 pending interrupt */
258 ret = twl4030_kpread(kp, &reg, KEYP_ISR1, 1);
259
260 /* Release all keys if I2C has gone bad or
261 * the KEYP has gone to idle state */
262 if (ret >= 0 && (reg & KEYP_IMR1_KP))
263 twl4030_kp_scan(kp, false);
264 else
265 twl4030_kp_scan(kp, true);
266
267 return IRQ_HANDLED;
268}
269
270static int __devinit twl4030_kp_program(struct twl4030_keypad *kp)
271{
272 u8 reg;
273 int i;
274
275 /* Enable controller, with hardware decoding but not autorepeat */
276 reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
277 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
278 if (twl4030_kpwrite_u8(kp, reg, KEYP_CTRL) < 0)
279 return -EIO;
280
281 /* NOTE: we could use sih_setup() here to package keypad
282 * event sources as four different IRQs ... but we don't.
283 */
284
285 /* Enable TO rising and KP rising and falling edge detection */
286 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
287 if (twl4030_kpwrite_u8(kp, reg, KEYP_EDR) < 0)
288 return -EIO;
289
290 /* Set PTV prescaler Field */
291 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
292 if (twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV) < 0)
293 return -EIO;
294
295 /* Set key debounce time to 20 ms */
296 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
297 if (twl4030_kpwrite_u8(kp, i, KEYP_DEB) < 0)
298 return -EIO;
299
300 /* Set timeout period to 100 ms */
301 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
302 if (twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L) < 0)
303 return -EIO;
304
305 if (twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H) < 0)
306 return -EIO;
307
308 /*
309 * Enable Clear-on-Read; disable remembering events that fire
310 * after the IRQ but before our handler acks (reads) them,
311 */
312 reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
313 if (twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL) < 0)
314 return -EIO;
315
316 /* initialize key state; irqs update it from here on */
317 if (twl4030_read_kp_matrix_state(kp, kp->kp_state) < 0)
318 return -EIO;
319
320 return 0;
321}
322
323/*
324 * Registers keypad device with input subsystem
325 * and configures TWL4030 keypad registers
326 */
327static int __devinit twl4030_kp_probe(struct platform_device *pdev)
328{
329 struct twl4030_keypad_data *pdata = pdev->dev.platform_data;
330 const struct matrix_keymap_data *keymap_data = pdata->keymap_data;
331 struct twl4030_keypad *kp;
332 struct input_dev *input;
333 u8 reg;
334 int error;
335
336 if (!pdata || !pdata->rows || !pdata->cols ||
337 pdata->rows > TWL4030_MAX_ROWS || pdata->cols > TWL4030_MAX_COLS) {
338 dev_err(&pdev->dev, "Invalid platform_data\n");
339 return -EINVAL;
340 }
341
342 kp = kzalloc(sizeof(*kp), GFP_KERNEL);
343 input = input_allocate_device();
344 if (!kp || !input) {
345 error = -ENOMEM;
346 goto err1;
347 }
348
349 /* Get the debug Device */
350 kp->dbg_dev = &pdev->dev;
351 kp->input = input;
352
353 kp->n_rows = pdata->rows;
354 kp->n_cols = pdata->cols;
355 kp->irq = platform_get_irq(pdev, 0);
356
357 /* setup input device */
358 __set_bit(EV_KEY, input->evbit);
359
360 /* Enable auto repeat feature of Linux input subsystem */
361 if (pdata->rep)
362 __set_bit(EV_REP, input->evbit);
363
364 input_set_capability(input, EV_MSC, MSC_SCAN);
365
366 input->name = "TWL4030 Keypad";
367 input->phys = "twl4030_keypad/input0";
368 input->dev.parent = &pdev->dev;
369
370 input->id.bustype = BUS_HOST;
371 input->id.vendor = 0x0001;
372 input->id.product = 0x0001;
373 input->id.version = 0x0003;
374
375 input->keycode = kp->keymap;
376 input->keycodesize = sizeof(kp->keymap[0]);
377 input->keycodemax = ARRAY_SIZE(kp->keymap);
378
379 matrix_keypad_build_keymap(keymap_data, TWL4030_ROW_SHIFT,
380 input->keycode, input->keybit);
381
382 error = input_register_device(input);
383 if (error) {
384 dev_err(kp->dbg_dev,
385 "Unable to register twl4030 keypad device\n");
386 goto err1;
387 }
388
389 error = twl4030_kp_program(kp);
390 if (error)
391 goto err2;
392
393 /*
394 * This ISR will always execute in kernel thread context because of
395 * the need to access the TWL4030 over the I2C bus.
396 *
397 * NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ...
398 */
3f58061d
FB
399 error = request_threaded_irq(kp->irq, NULL, do_kp_irq,
400 0, pdev->name, kp);
9d834068
DB
401 if (error) {
402 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",
403 kp->irq);
404 goto err3;
405 }
406
407 /* Enable KP and TO interrupts now. */
408 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
409 if (twl4030_kpwrite_u8(kp, reg, KEYP_IMR1)) {
410 error = -EIO;
411 goto err4;
412 }
413
414 platform_set_drvdata(pdev, kp);
415 return 0;
416
417err4:
418 /* mask all events - we don't care about the result */
419 (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
420err3:
421 free_irq(kp->irq, NULL);
422err2:
423 input_unregister_device(input);
424 input = NULL;
425err1:
426 input_free_device(input);
427 kfree(kp);
428 return error;
429}
430
431static int __devexit twl4030_kp_remove(struct platform_device *pdev)
432{
433 struct twl4030_keypad *kp = platform_get_drvdata(pdev);
434
435 free_irq(kp->irq, kp);
436 input_unregister_device(kp->input);
437 platform_set_drvdata(pdev, NULL);
438 kfree(kp);
439
440 return 0;
441}
442
443/*
444 * NOTE: twl4030 are multi-function devices connected via I2C.
445 * So this device is a child of an I2C parent, thus it needs to
446 * support unplug/replug (which most platform devices don't).
447 */
448
449static struct platform_driver twl4030_kp_driver = {
450 .probe = twl4030_kp_probe,
451 .remove = __devexit_p(twl4030_kp_remove),
452 .driver = {
453 .name = "twl4030_keypad",
454 .owner = THIS_MODULE,
455 },
456};
457
458static int __init twl4030_kp_init(void)
459{
460 return platform_driver_register(&twl4030_kp_driver);
461}
462module_init(twl4030_kp_init);
463
464static void __exit twl4030_kp_exit(void)
465{
466 platform_driver_unregister(&twl4030_kp_driver);
467}
468module_exit(twl4030_kp_exit);
469
470MODULE_AUTHOR("Texas Instruments");
471MODULE_DESCRIPTION("TWL4030 Keypad Driver");
472MODULE_LICENSE("GPL");
473MODULE_ALIAS("platform:twl4030_keypad");
474