Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / keyboard / matrix_keypad.c
blobb443e088fd3c0c09e3f103011de69b24070e720f
1 /*
2 * GPIO driven matrix keyboard driver
4 * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
6 * Based on corgikbd.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <linux/init.h>
18 #include <linux/input.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/jiffies.h>
22 #include <linux/module.h>
23 #include <linux/gpio.h>
24 #include <linux/input/matrix_keypad.h>
25 #include <linux/slab.h>
27 struct matrix_keypad {
28 const struct matrix_keypad_platform_data *pdata;
29 struct input_dev *input_dev;
30 unsigned short *keycodes;
31 unsigned int row_shift;
33 DECLARE_BITMAP(disabled_gpios, MATRIX_MAX_ROWS);
35 uint32_t last_key_state[MATRIX_MAX_COLS];
36 struct delayed_work work;
37 spinlock_t lock;
38 bool scan_pending;
39 bool stopped;
43 * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
44 * minmal side effect when scanning other columns, here it is configured to
45 * be input, and it should work on most platforms.
47 static void __activate_col(const struct matrix_keypad_platform_data *pdata,
48 int col, bool on)
50 bool level_on = !pdata->active_low;
52 if (on) {
53 gpio_direction_output(pdata->col_gpios[col], level_on);
54 } else {
55 gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
56 gpio_direction_input(pdata->col_gpios[col]);
60 static void activate_col(const struct matrix_keypad_platform_data *pdata,
61 int col, bool on)
63 __activate_col(pdata, col, on);
65 if (on && pdata->col_scan_delay_us)
66 udelay(pdata->col_scan_delay_us);
69 static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
70 bool on)
72 int col;
74 for (col = 0; col < pdata->num_col_gpios; col++)
75 __activate_col(pdata, col, on);
78 static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
79 int row)
81 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
82 !pdata->active_low : pdata->active_low;
85 static void enable_row_irqs(struct matrix_keypad *keypad)
87 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
88 int i;
90 for (i = 0; i < pdata->num_row_gpios; i++)
91 enable_irq(gpio_to_irq(pdata->row_gpios[i]));
94 static void disable_row_irqs(struct matrix_keypad *keypad)
96 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
97 int i;
99 for (i = 0; i < pdata->num_row_gpios; i++)
100 disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
104 * This gets the keys from keyboard and reports it to input subsystem
106 static void matrix_keypad_scan(struct work_struct *work)
108 struct matrix_keypad *keypad =
109 container_of(work, struct matrix_keypad, work.work);
110 struct input_dev *input_dev = keypad->input_dev;
111 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
112 uint32_t new_state[MATRIX_MAX_COLS];
113 int row, col, code;
115 /* de-activate all columns for scanning */
116 activate_all_cols(pdata, false);
118 memset(new_state, 0, sizeof(new_state));
120 /* assert each column and read the row status out */
121 for (col = 0; col < pdata->num_col_gpios; col++) {
123 activate_col(pdata, col, true);
125 for (row = 0; row < pdata->num_row_gpios; row++)
126 new_state[col] |=
127 row_asserted(pdata, row) ? (1 << row) : 0;
129 activate_col(pdata, col, false);
132 for (col = 0; col < pdata->num_col_gpios; col++) {
133 uint32_t bits_changed;
135 bits_changed = keypad->last_key_state[col] ^ new_state[col];
136 if (bits_changed == 0)
137 continue;
139 for (row = 0; row < pdata->num_row_gpios; row++) {
140 if ((bits_changed & (1 << row)) == 0)
141 continue;
143 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
144 input_event(input_dev, EV_MSC, MSC_SCAN, code);
145 input_report_key(input_dev,
146 keypad->keycodes[code],
147 new_state[col] & (1 << row));
150 input_sync(input_dev);
152 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
154 activate_all_cols(pdata, true);
156 /* Enable IRQs again */
157 spin_lock_irq(&keypad->lock);
158 keypad->scan_pending = false;
159 enable_row_irqs(keypad);
160 spin_unlock_irq(&keypad->lock);
163 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
165 struct matrix_keypad *keypad = id;
166 unsigned long flags;
168 spin_lock_irqsave(&keypad->lock, flags);
171 * See if another IRQ beaten us to it and scheduled the
172 * scan already. In that case we should not try to
173 * disable IRQs again.
175 if (unlikely(keypad->scan_pending || keypad->stopped))
176 goto out;
178 disable_row_irqs(keypad);
179 keypad->scan_pending = true;
180 schedule_delayed_work(&keypad->work,
181 msecs_to_jiffies(keypad->pdata->debounce_ms));
183 out:
184 spin_unlock_irqrestore(&keypad->lock, flags);
185 return IRQ_HANDLED;
188 static int matrix_keypad_start(struct input_dev *dev)
190 struct matrix_keypad *keypad = input_get_drvdata(dev);
192 keypad->stopped = false;
193 mb();
196 * Schedule an immediate key scan to capture current key state;
197 * columns will be activated and IRQs be enabled after the scan.
199 schedule_delayed_work(&keypad->work, 0);
201 return 0;
204 static void matrix_keypad_stop(struct input_dev *dev)
206 struct matrix_keypad *keypad = input_get_drvdata(dev);
208 keypad->stopped = true;
209 mb();
210 flush_work(&keypad->work.work);
212 * matrix_keypad_scan() will leave IRQs enabled;
213 * we should disable them now.
215 disable_row_irqs(keypad);
218 #ifdef CONFIG_PM
219 static int matrix_keypad_suspend(struct device *dev)
221 struct platform_device *pdev = to_platform_device(dev);
222 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
223 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
224 int i;
226 matrix_keypad_stop(keypad->input_dev);
228 if (device_may_wakeup(&pdev->dev)) {
229 for (i = 0; i < pdata->num_row_gpios; i++) {
230 if (!test_bit(i, keypad->disabled_gpios)) {
231 unsigned int gpio = pdata->row_gpios[i];
233 if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
234 __set_bit(i, keypad->disabled_gpios);
239 return 0;
242 static int matrix_keypad_resume(struct device *dev)
244 struct platform_device *pdev = to_platform_device(dev);
245 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
246 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
247 int i;
249 if (device_may_wakeup(&pdev->dev)) {
250 for (i = 0; i < pdata->num_row_gpios; i++) {
251 if (test_and_clear_bit(i, keypad->disabled_gpios)) {
252 unsigned int gpio = pdata->row_gpios[i];
254 disable_irq_wake(gpio_to_irq(gpio));
259 matrix_keypad_start(keypad->input_dev);
261 return 0;
264 static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
265 matrix_keypad_suspend, matrix_keypad_resume);
266 #endif
268 static int __devinit init_matrix_gpio(struct platform_device *pdev,
269 struct matrix_keypad *keypad)
271 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
272 int i, err = -EINVAL;
274 /* initialized strobe lines as outputs, activated */
275 for (i = 0; i < pdata->num_col_gpios; i++) {
276 err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
277 if (err) {
278 dev_err(&pdev->dev,
279 "failed to request GPIO%d for COL%d\n",
280 pdata->col_gpios[i], i);
281 goto err_free_cols;
284 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
287 for (i = 0; i < pdata->num_row_gpios; i++) {
288 err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
289 if (err) {
290 dev_err(&pdev->dev,
291 "failed to request GPIO%d for ROW%d\n",
292 pdata->row_gpios[i], i);
293 goto err_free_rows;
296 gpio_direction_input(pdata->row_gpios[i]);
299 for (i = 0; i < pdata->num_row_gpios; i++) {
300 err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
301 matrix_keypad_interrupt,
302 IRQF_DISABLED |
303 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
304 "matrix-keypad", keypad);
305 if (err) {
306 dev_err(&pdev->dev,
307 "Unable to acquire interrupt for GPIO line %i\n",
308 pdata->row_gpios[i]);
309 goto err_free_irqs;
313 /* initialized as disabled - enabled by input->open */
314 disable_row_irqs(keypad);
315 return 0;
317 err_free_irqs:
318 while (--i >= 0)
319 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
320 i = pdata->num_row_gpios;
321 err_free_rows:
322 while (--i >= 0)
323 gpio_free(pdata->row_gpios[i]);
324 i = pdata->num_col_gpios;
325 err_free_cols:
326 while (--i >= 0)
327 gpio_free(pdata->col_gpios[i]);
329 return err;
332 static int __devinit matrix_keypad_probe(struct platform_device *pdev)
334 const struct matrix_keypad_platform_data *pdata;
335 const struct matrix_keymap_data *keymap_data;
336 struct matrix_keypad *keypad;
337 struct input_dev *input_dev;
338 unsigned short *keycodes;
339 unsigned int row_shift;
340 int err;
342 pdata = pdev->dev.platform_data;
343 if (!pdata) {
344 dev_err(&pdev->dev, "no platform data defined\n");
345 return -EINVAL;
348 keymap_data = pdata->keymap_data;
349 if (!keymap_data) {
350 dev_err(&pdev->dev, "no keymap data defined\n");
351 return -EINVAL;
354 row_shift = get_count_order(pdata->num_col_gpios);
356 keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
357 keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
358 sizeof(*keycodes),
359 GFP_KERNEL);
360 input_dev = input_allocate_device();
361 if (!keypad || !keycodes || !input_dev) {
362 err = -ENOMEM;
363 goto err_free_mem;
366 keypad->input_dev = input_dev;
367 keypad->pdata = pdata;
368 keypad->keycodes = keycodes;
369 keypad->row_shift = row_shift;
370 keypad->stopped = true;
371 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
372 spin_lock_init(&keypad->lock);
374 input_dev->name = pdev->name;
375 input_dev->id.bustype = BUS_HOST;
376 input_dev->dev.parent = &pdev->dev;
377 input_dev->evbit[0] = BIT_MASK(EV_KEY);
378 if (!pdata->no_autorepeat)
379 input_dev->evbit[0] |= BIT_MASK(EV_REP);
380 input_dev->open = matrix_keypad_start;
381 input_dev->close = matrix_keypad_stop;
383 input_dev->keycode = keycodes;
384 input_dev->keycodesize = sizeof(*keycodes);
385 input_dev->keycodemax = pdata->num_row_gpios << row_shift;
387 matrix_keypad_build_keymap(keymap_data, row_shift,
388 input_dev->keycode, input_dev->keybit);
390 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
391 input_set_drvdata(input_dev, keypad);
393 err = init_matrix_gpio(pdev, keypad);
394 if (err)
395 goto err_free_mem;
397 err = input_register_device(keypad->input_dev);
398 if (err)
399 goto err_free_mem;
401 device_init_wakeup(&pdev->dev, pdata->wakeup);
402 platform_set_drvdata(pdev, keypad);
404 return 0;
406 err_free_mem:
407 input_free_device(input_dev);
408 kfree(keycodes);
409 kfree(keypad);
410 return err;
413 static int __devexit matrix_keypad_remove(struct platform_device *pdev)
415 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
416 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
417 int i;
419 device_init_wakeup(&pdev->dev, 0);
421 for (i = 0; i < pdata->num_row_gpios; i++) {
422 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
423 gpio_free(pdata->row_gpios[i]);
426 for (i = 0; i < pdata->num_col_gpios; i++)
427 gpio_free(pdata->col_gpios[i]);
429 input_unregister_device(keypad->input_dev);
430 platform_set_drvdata(pdev, NULL);
431 kfree(keypad->keycodes);
432 kfree(keypad);
434 return 0;
437 static struct platform_driver matrix_keypad_driver = {
438 .probe = matrix_keypad_probe,
439 .remove = __devexit_p(matrix_keypad_remove),
440 .driver = {
441 .name = "matrix-keypad",
442 .owner = THIS_MODULE,
443 #ifdef CONFIG_PM
444 .pm = &matrix_keypad_pm_ops,
445 #endif
449 static int __init matrix_keypad_init(void)
451 return platform_driver_register(&matrix_keypad_driver);
454 static void __exit matrix_keypad_exit(void)
456 platform_driver_unregister(&matrix_keypad_driver);
459 module_init(matrix_keypad_init);
460 module_exit(matrix_keypad_exit);
462 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
463 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
464 MODULE_LICENSE("GPL v2");
465 MODULE_ALIAS("platform:matrix-keypad");