2 * GPIO driven matrix keyboard driver
4 * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
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>
26 struct matrix_keypad {
27 const struct matrix_keypad_platform_data *pdata;
28 struct input_dev *input_dev;
29 unsigned short *keycodes;
30 unsigned int row_shift;
32 uint32_t last_key_state[MATRIX_MAX_COLS];
33 struct delayed_work work;
40 * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
41 * minmal side effect when scanning other columns, here it is configured to
42 * be input, and it should work on most platforms.
44 static void __activate_col(const struct matrix_keypad_platform_data *pdata,
47 bool level_on = !pdata->active_low;
50 gpio_direction_output(pdata->col_gpios[col], level_on);
52 gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
53 gpio_direction_input(pdata->col_gpios[col]);
57 static void activate_col(const struct matrix_keypad_platform_data *pdata,
60 __activate_col(pdata, col, on);
62 if (on && pdata->col_scan_delay_us)
63 udelay(pdata->col_scan_delay_us);
66 static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
71 for (col = 0; col < pdata->num_col_gpios; col++)
72 __activate_col(pdata, col, on);
75 static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
78 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
79 !pdata->active_low : pdata->active_low;
82 static void enable_row_irqs(struct matrix_keypad *keypad)
84 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
87 for (i = 0; i < pdata->num_row_gpios; i++)
88 enable_irq(gpio_to_irq(pdata->row_gpios[i]));
91 static void disable_row_irqs(struct matrix_keypad *keypad)
93 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
96 for (i = 0; i < pdata->num_row_gpios; i++)
97 disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
101 * This gets the keys from keyboard and reports it to input subsystem
103 static void matrix_keypad_scan(struct work_struct *work)
105 struct matrix_keypad *keypad =
106 container_of(work, struct matrix_keypad, work.work);
107 struct input_dev *input_dev = keypad->input_dev;
108 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
109 uint32_t new_state[MATRIX_MAX_COLS];
112 /* de-activate all columns for scanning */
113 activate_all_cols(pdata, false);
115 memset(new_state, 0, sizeof(new_state));
117 /* assert each column and read the row status out */
118 for (col = 0; col < pdata->num_col_gpios; col++) {
120 activate_col(pdata, col, true);
122 for (row = 0; row < pdata->num_row_gpios; row++)
124 row_asserted(pdata, row) ? (1 << row) : 0;
126 activate_col(pdata, col, false);
129 for (col = 0; col < pdata->num_col_gpios; col++) {
130 uint32_t bits_changed;
132 bits_changed = keypad->last_key_state[col] ^ new_state[col];
133 if (bits_changed == 0)
136 for (row = 0; row < pdata->num_row_gpios; row++) {
137 if ((bits_changed & (1 << row)) == 0)
140 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
141 input_event(input_dev, EV_MSC, MSC_SCAN, code);
142 input_report_key(input_dev,
143 keypad->keycodes[code],
144 new_state[col] & (1 << row));
147 input_sync(input_dev);
149 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
151 activate_all_cols(pdata, true);
153 /* Enable IRQs again */
154 spin_lock_irq(&keypad->lock);
155 keypad->scan_pending = false;
156 enable_row_irqs(keypad);
157 spin_unlock_irq(&keypad->lock);
160 static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
162 struct matrix_keypad *keypad = id;
165 spin_lock_irqsave(&keypad->lock, flags);
168 * See if another IRQ beaten us to it and scheduled the
169 * scan already. In that case we should not try to
170 * disable IRQs again.
172 if (unlikely(keypad->scan_pending || keypad->stopped))
175 disable_row_irqs(keypad);
176 keypad->scan_pending = true;
177 schedule_delayed_work(&keypad->work,
178 msecs_to_jiffies(keypad->pdata->debounce_ms));
181 spin_unlock_irqrestore(&keypad->lock, flags);
185 static int matrix_keypad_start(struct input_dev *dev)
187 struct matrix_keypad *keypad = input_get_drvdata(dev);
189 keypad->stopped = false;
193 * Schedule an immediate key scan to capture current key state;
194 * columns will be activated and IRQs be enabled after the scan.
196 schedule_delayed_work(&keypad->work, 0);
201 static void matrix_keypad_stop(struct input_dev *dev)
203 struct matrix_keypad *keypad = input_get_drvdata(dev);
205 keypad->stopped = true;
207 flush_work(&keypad->work.work);
209 * matrix_keypad_scan() will leave IRQs enabled;
210 * we should disable them now.
212 disable_row_irqs(keypad);
216 static int matrix_keypad_suspend(struct platform_device *pdev, pm_message_t state)
218 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
219 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
222 matrix_keypad_stop(keypad->input_dev);
224 if (device_may_wakeup(&pdev->dev))
225 for (i = 0; i < pdata->num_row_gpios; i++)
226 enable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
231 static int matrix_keypad_resume(struct platform_device *pdev)
233 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
234 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
237 if (device_may_wakeup(&pdev->dev))
238 for (i = 0; i < pdata->num_row_gpios; i++)
239 disable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
241 matrix_keypad_start(keypad->input_dev);
246 #define matrix_keypad_suspend NULL
247 #define matrix_keypad_resume NULL
250 static int __devinit init_matrix_gpio(struct platform_device *pdev,
251 struct matrix_keypad *keypad)
253 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
254 int i, err = -EINVAL;
256 /* initialized strobe lines as outputs, activated */
257 for (i = 0; i < pdata->num_col_gpios; i++) {
258 err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
261 "failed to request GPIO%d for COL%d\n",
262 pdata->col_gpios[i], i);
266 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
269 for (i = 0; i < pdata->num_row_gpios; i++) {
270 err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
273 "failed to request GPIO%d for ROW%d\n",
274 pdata->row_gpios[i], i);
278 gpio_direction_input(pdata->row_gpios[i]);
281 for (i = 0; i < pdata->num_row_gpios; i++) {
282 err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
283 matrix_keypad_interrupt,
285 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
286 "matrix-keypad", keypad);
289 "Unable to acquire interrupt for GPIO line %i\n",
290 pdata->row_gpios[i]);
295 /* initialized as disabled - enabled by input->open */
296 disable_row_irqs(keypad);
301 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
302 i = pdata->num_row_gpios;
305 gpio_free(pdata->row_gpios[i]);
306 i = pdata->num_col_gpios;
309 gpio_free(pdata->col_gpios[i]);
314 static int __devinit matrix_keypad_probe(struct platform_device *pdev)
316 const struct matrix_keypad_platform_data *pdata;
317 const struct matrix_keymap_data *keymap_data;
318 struct matrix_keypad *keypad;
319 struct input_dev *input_dev;
320 unsigned short *keycodes;
321 unsigned int row_shift;
325 pdata = pdev->dev.platform_data;
327 dev_err(&pdev->dev, "no platform data defined\n");
331 keymap_data = pdata->keymap_data;
333 dev_err(&pdev->dev, "no keymap data defined\n");
337 row_shift = get_count_order(pdata->num_col_gpios);
339 keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
340 keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
343 input_dev = input_allocate_device();
344 if (!keypad || !keycodes || !input_dev) {
349 keypad->input_dev = input_dev;
350 keypad->pdata = pdata;
351 keypad->keycodes = keycodes;
352 keypad->row_shift = row_shift;
353 keypad->stopped = true;
354 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
355 spin_lock_init(&keypad->lock);
357 input_dev->name = pdev->name;
358 input_dev->id.bustype = BUS_HOST;
359 input_dev->dev.parent = &pdev->dev;
360 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
361 input_dev->open = matrix_keypad_start;
362 input_dev->close = matrix_keypad_stop;
364 input_dev->keycode = keycodes;
365 input_dev->keycodesize = sizeof(*keycodes);
366 input_dev->keycodemax = pdata->num_row_gpios << keypad->row_shift;
368 for (i = 0; i < keymap_data->keymap_size; i++) {
369 unsigned int key = keymap_data->keymap[i];
370 unsigned int row = KEY_ROW(key);
371 unsigned int col = KEY_COL(key);
372 unsigned short code = KEY_VAL(key);
374 keycodes[MATRIX_SCAN_CODE(row, col, row_shift)] = code;
375 __set_bit(code, input_dev->keybit);
377 __clear_bit(KEY_RESERVED, input_dev->keybit);
379 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
380 input_set_drvdata(input_dev, keypad);
382 err = init_matrix_gpio(pdev, keypad);
386 err = input_register_device(keypad->input_dev);
390 device_init_wakeup(&pdev->dev, pdata->wakeup);
391 platform_set_drvdata(pdev, keypad);
396 input_free_device(input_dev);
402 static int __devexit matrix_keypad_remove(struct platform_device *pdev)
404 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
405 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
408 device_init_wakeup(&pdev->dev, 0);
410 for (i = 0; i < pdata->num_row_gpios; i++) {
411 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
412 gpio_free(pdata->row_gpios[i]);
415 for (i = 0; i < pdata->num_col_gpios; i++)
416 gpio_free(pdata->col_gpios[i]);
418 input_unregister_device(keypad->input_dev);
419 platform_set_drvdata(pdev, NULL);
420 kfree(keypad->keycodes);
426 static struct platform_driver matrix_keypad_driver = {
427 .probe = matrix_keypad_probe,
428 .remove = __devexit_p(matrix_keypad_remove),
429 .suspend = matrix_keypad_suspend,
430 .resume = matrix_keypad_resume,
432 .name = "matrix-keypad",
433 .owner = THIS_MODULE,
437 static int __init matrix_keypad_init(void)
439 return platform_driver_register(&matrix_keypad_driver);
442 static void __exit matrix_keypad_exit(void)
444 platform_driver_unregister(&matrix_keypad_driver);
447 module_init(matrix_keypad_init);
448 module_exit(matrix_keypad_exit);
450 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
451 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
452 MODULE_LICENSE("GPL v2");
453 MODULE_ALIAS("platform:matrix-keypad");