Merge tag 'staging-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6.git] / drivers / input / keyboard / tc3589x-keypad.c
blob208de7cbb7fae2460dac5576c69f5847165ac3c9
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
4 * Author: Jayeeta Banerjee <jayeeta.banerjee@stericsson.com>
5 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
7 * License Terms: GNU General Public License, version 2
9 * TC35893 MFD Keypad Controller driver
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/input.h>
16 #include <linux/platform_device.h>
17 #include <linux/input/matrix_keypad.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/mfd/tc3589x.h>
22 /* Maximum supported keypad matrix row/columns size */
23 #define TC3589x_MAX_KPROW 8
24 #define TC3589x_MAX_KPCOL 12
26 /* keypad related Constants */
27 #define TC3589x_MAX_DEBOUNCE_SETTLE 0xFF
28 #define DEDICATED_KEY_VAL 0xFF
30 /* Pull up/down masks */
31 #define TC3589x_NO_PULL_MASK 0x0
32 #define TC3589x_PULL_DOWN_MASK 0x1
33 #define TC3589x_PULL_UP_MASK 0x2
34 #define TC3589x_PULLUP_ALL_MASK 0xAA
35 #define TC3589x_IO_PULL_VAL(index, mask) ((mask)<<((index)%4)*2))
37 /* Bit masks for IOCFG register */
38 #define IOCFG_BALLCFG 0x01
39 #define IOCFG_IG 0x08
41 #define KP_EVCODE_COL_MASK 0x0F
42 #define KP_EVCODE_ROW_MASK 0x70
43 #define KP_RELEASE_EVT_MASK 0x80
45 #define KP_ROW_SHIFT 4
47 #define KP_NO_VALID_KEY_MASK 0x7F
49 /* bit masks for RESTCTRL register */
50 #define TC3589x_KBDRST 0x2
51 #define TC3589x_IRQRST 0x10
52 #define TC3589x_RESET_ALL 0x1B
54 /* KBDMFS register bit mask */
55 #define TC3589x_KBDMFS_EN 0x1
57 /* CLKEN register bitmask */
58 #define KPD_CLK_EN 0x1
60 /* RSTINTCLR register bit mask */
61 #define IRQ_CLEAR 0x1
63 /* bit masks for keyboard interrupts*/
64 #define TC3589x_EVT_LOSS_INT 0x8
65 #define TC3589x_EVT_INT 0x4
66 #define TC3589x_KBD_LOSS_INT 0x2
67 #define TC3589x_KBD_INT 0x1
69 /* bit masks for keyboard interrupt clear*/
70 #define TC3589x_EVT_INT_CLR 0x2
71 #define TC3589x_KBD_INT_CLR 0x1
73 /**
74 * struct tc_keypad - data structure used by keypad driver
75 * @tc3589x: pointer to tc35893
76 * @input: pointer to input device object
77 * @board: keypad platform device
78 * @krow: number of rows
79 * @kcol: number of columns
80 * @keymap: matrix scan code table for keycodes
81 * @keypad_stopped: holds keypad status
83 struct tc_keypad {
84 struct tc3589x *tc3589x;
85 struct input_dev *input;
86 const struct tc3589x_keypad_platform_data *board;
87 unsigned int krow;
88 unsigned int kcol;
89 unsigned short *keymap;
90 bool keypad_stopped;
93 static int tc3589x_keypad_init_key_hardware(struct tc_keypad *keypad)
95 int ret;
96 struct tc3589x *tc3589x = keypad->tc3589x;
97 const struct tc3589x_keypad_platform_data *board = keypad->board;
99 /* validate platform configuration */
100 if (board->kcol > TC3589x_MAX_KPCOL || board->krow > TC3589x_MAX_KPROW)
101 return -EINVAL;
103 /* configure KBDSIZE 4 LSbits for cols and 4 MSbits for rows */
104 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSIZE,
105 (board->krow << KP_ROW_SHIFT) | board->kcol);
106 if (ret < 0)
107 return ret;
109 /* configure dedicated key config, no dedicated key selected */
110 ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_LSB, DEDICATED_KEY_VAL);
111 if (ret < 0)
112 return ret;
114 ret = tc3589x_reg_write(tc3589x, TC3589x_KBCFG_MSB, DEDICATED_KEY_VAL);
115 if (ret < 0)
116 return ret;
118 /* Configure settle time */
119 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDSETTLE_REG,
120 board->settle_time);
121 if (ret < 0)
122 return ret;
124 /* Configure debounce time */
125 ret = tc3589x_reg_write(tc3589x, TC3589x_KBDBOUNCE,
126 board->debounce_period);
127 if (ret < 0)
128 return ret;
130 /* Start of initialise keypad GPIOs */
131 ret = tc3589x_set_bits(tc3589x, TC3589x_IOCFG, 0x0, IOCFG_IG);
132 if (ret < 0)
133 return ret;
135 /* Configure pull-up resistors for all row GPIOs */
136 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_LSB,
137 TC3589x_PULLUP_ALL_MASK);
138 if (ret < 0)
139 return ret;
141 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG0_MSB,
142 TC3589x_PULLUP_ALL_MASK);
143 if (ret < 0)
144 return ret;
146 /* Configure pull-up resistors for all column GPIOs */
147 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_LSB,
148 TC3589x_PULLUP_ALL_MASK);
149 if (ret < 0)
150 return ret;
152 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG1_MSB,
153 TC3589x_PULLUP_ALL_MASK);
154 if (ret < 0)
155 return ret;
157 ret = tc3589x_reg_write(tc3589x, TC3589x_IOPULLCFG2_LSB,
158 TC3589x_PULLUP_ALL_MASK);
160 return ret;
163 #define TC35893_DATA_REGS 4
164 #define TC35893_KEYCODE_FIFO_EMPTY 0x7f
165 #define TC35893_KEYCODE_FIFO_CLEAR 0xff
166 #define TC35893_KEYPAD_ROW_SHIFT 0x3
168 static irqreturn_t tc3589x_keypad_irq(int irq, void *dev)
170 struct tc_keypad *keypad = dev;
171 struct tc3589x *tc3589x = keypad->tc3589x;
172 u8 i, row_index, col_index, kbd_code, up;
173 u8 code;
175 for (i = 0; i < TC35893_DATA_REGS * 2; i++) {
176 kbd_code = tc3589x_reg_read(tc3589x, TC3589x_EVTCODE_FIFO);
178 /* loop till fifo is empty and no more keys are pressed */
179 if (kbd_code == TC35893_KEYCODE_FIFO_EMPTY ||
180 kbd_code == TC35893_KEYCODE_FIFO_CLEAR)
181 continue;
183 /* valid key is found */
184 col_index = kbd_code & KP_EVCODE_COL_MASK;
185 row_index = (kbd_code & KP_EVCODE_ROW_MASK) >> KP_ROW_SHIFT;
186 code = MATRIX_SCAN_CODE(row_index, col_index,
187 TC35893_KEYPAD_ROW_SHIFT);
188 up = kbd_code & KP_RELEASE_EVT_MASK;
190 input_event(keypad->input, EV_MSC, MSC_SCAN, code);
191 input_report_key(keypad->input, keypad->keymap[code], !up);
192 input_sync(keypad->input);
195 /* clear IRQ */
196 tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
197 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
198 /* enable IRQ */
199 tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
200 0x0, TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
202 return IRQ_HANDLED;
205 static int tc3589x_keypad_enable(struct tc_keypad *keypad)
207 struct tc3589x *tc3589x = keypad->tc3589x;
208 int ret;
210 /* pull the keypad module out of reset */
211 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x0);
212 if (ret < 0)
213 return ret;
215 /* configure KBDMFS */
216 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMFS, 0x0, TC3589x_KBDMFS_EN);
217 if (ret < 0)
218 return ret;
220 /* enable the keypad clock */
221 ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x0, KPD_CLK_EN);
222 if (ret < 0)
223 return ret;
225 /* clear pending IRQs */
226 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTINTCLR, 0x0, 0x1);
227 if (ret < 0)
228 return ret;
230 /* enable the IRQs */
231 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK, 0x0,
232 TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT);
233 if (ret < 0)
234 return ret;
236 keypad->keypad_stopped = false;
238 return ret;
241 static int tc3589x_keypad_disable(struct tc_keypad *keypad)
243 struct tc3589x *tc3589x = keypad->tc3589x;
244 int ret;
246 /* clear IRQ */
247 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDIC,
248 0x0, TC3589x_EVT_INT_CLR | TC3589x_KBD_INT_CLR);
249 if (ret < 0)
250 return ret;
252 /* disable all interrupts */
253 ret = tc3589x_set_bits(tc3589x, TC3589x_KBDMSK,
254 ~(TC3589x_EVT_LOSS_INT | TC3589x_EVT_INT), 0x0);
255 if (ret < 0)
256 return ret;
258 /* disable the keypad module */
259 ret = tc3589x_set_bits(tc3589x, TC3589x_CLKEN, 0x1, 0x0);
260 if (ret < 0)
261 return ret;
263 /* put the keypad module into reset */
264 ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL, TC3589x_KBDRST, 0x1);
266 keypad->keypad_stopped = true;
268 return ret;
271 static int tc3589x_keypad_open(struct input_dev *input)
273 int error;
274 struct tc_keypad *keypad = input_get_drvdata(input);
276 /* enable the keypad module */
277 error = tc3589x_keypad_enable(keypad);
278 if (error < 0) {
279 dev_err(&input->dev, "failed to enable keypad module\n");
280 return error;
283 error = tc3589x_keypad_init_key_hardware(keypad);
284 if (error < 0) {
285 dev_err(&input->dev, "failed to configure keypad module\n");
286 return error;
289 return 0;
292 static void tc3589x_keypad_close(struct input_dev *input)
294 struct tc_keypad *keypad = input_get_drvdata(input);
296 /* disable the keypad module */
297 tc3589x_keypad_disable(keypad);
300 static int tc3589x_keypad_probe(struct platform_device *pdev)
302 struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
303 struct tc_keypad *keypad;
304 struct input_dev *input;
305 const struct tc3589x_keypad_platform_data *plat;
306 int error, irq;
308 plat = tc3589x->pdata->keypad;
309 if (!plat) {
310 dev_err(&pdev->dev, "invalid keypad platform data\n");
311 return -EINVAL;
314 irq = platform_get_irq(pdev, 0);
315 if (irq < 0)
316 return irq;
318 keypad = kzalloc(sizeof(struct tc_keypad), GFP_KERNEL);
319 input = input_allocate_device();
320 if (!keypad || !input) {
321 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
322 error = -ENOMEM;
323 goto err_free_mem;
326 keypad->board = plat;
327 keypad->input = input;
328 keypad->tc3589x = tc3589x;
330 input->id.bustype = BUS_I2C;
331 input->name = pdev->name;
332 input->dev.parent = &pdev->dev;
334 input->open = tc3589x_keypad_open;
335 input->close = tc3589x_keypad_close;
337 error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
338 TC3589x_MAX_KPROW, TC3589x_MAX_KPCOL,
339 NULL, input);
340 if (error) {
341 dev_err(&pdev->dev, "Failed to build keymap\n");
342 goto err_free_mem;
345 keypad->keymap = input->keycode;
347 input_set_capability(input, EV_MSC, MSC_SCAN);
348 if (!plat->no_autorepeat)
349 __set_bit(EV_REP, input->evbit);
351 input_set_drvdata(input, keypad);
353 error = request_threaded_irq(irq, NULL,
354 tc3589x_keypad_irq, plat->irqtype,
355 "tc3589x-keypad", keypad);
356 if (error < 0) {
357 dev_err(&pdev->dev,
358 "Could not allocate irq %d,error %d\n",
359 irq, error);
360 goto err_free_mem;
363 error = input_register_device(input);
364 if (error) {
365 dev_err(&pdev->dev, "Could not register input device\n");
366 goto err_free_irq;
369 /* let platform decide if keypad is a wakeup source or not */
370 device_init_wakeup(&pdev->dev, plat->enable_wakeup);
371 device_set_wakeup_capable(&pdev->dev, plat->enable_wakeup);
373 platform_set_drvdata(pdev, keypad);
375 return 0;
377 err_free_irq:
378 free_irq(irq, keypad);
379 err_free_mem:
380 input_free_device(input);
381 kfree(keypad);
382 return error;
385 static int tc3589x_keypad_remove(struct platform_device *pdev)
387 struct tc_keypad *keypad = platform_get_drvdata(pdev);
388 int irq = platform_get_irq(pdev, 0);
390 if (!keypad->keypad_stopped)
391 tc3589x_keypad_disable(keypad);
393 free_irq(irq, keypad);
395 input_unregister_device(keypad->input);
397 kfree(keypad);
399 return 0;
402 #ifdef CONFIG_PM_SLEEP
403 static int tc3589x_keypad_suspend(struct device *dev)
405 struct platform_device *pdev = to_platform_device(dev);
406 struct tc_keypad *keypad = platform_get_drvdata(pdev);
407 int irq = platform_get_irq(pdev, 0);
409 /* keypad is already off; we do nothing */
410 if (keypad->keypad_stopped)
411 return 0;
413 /* if device is not a wakeup source, disable it for powersave */
414 if (!device_may_wakeup(&pdev->dev))
415 tc3589x_keypad_disable(keypad);
416 else
417 enable_irq_wake(irq);
419 return 0;
422 static int tc3589x_keypad_resume(struct device *dev)
424 struct platform_device *pdev = to_platform_device(dev);
425 struct tc_keypad *keypad = platform_get_drvdata(pdev);
426 int irq = platform_get_irq(pdev, 0);
428 if (!keypad->keypad_stopped)
429 return 0;
431 /* enable the device to resume normal operations */
432 if (!device_may_wakeup(&pdev->dev))
433 tc3589x_keypad_enable(keypad);
434 else
435 disable_irq_wake(irq);
437 return 0;
439 #endif
441 static SIMPLE_DEV_PM_OPS(tc3589x_keypad_dev_pm_ops,
442 tc3589x_keypad_suspend, tc3589x_keypad_resume);
444 static struct platform_driver tc3589x_keypad_driver = {
445 .driver = {
446 .name = "tc3589x-keypad",
447 .owner = THIS_MODULE,
448 .pm = &tc3589x_keypad_dev_pm_ops,
450 .probe = tc3589x_keypad_probe,
451 .remove = tc3589x_keypad_remove,
453 module_platform_driver(tc3589x_keypad_driver);
455 MODULE_LICENSE("GPL v2");
456 MODULE_AUTHOR("Jayeeta Banerjee/Sundar Iyer");
457 MODULE_DESCRIPTION("TC35893 Keypad Driver");
458 MODULE_ALIAS("platform:tc3589x-keypad");