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 DECLARE_BITMAP(disabled_gpios
, MATRIX_MAX_ROWS
);
34 uint32_t last_key_state
[MATRIX_MAX_COLS
];
35 struct delayed_work work
;
42 * NOTE: normally the GPIO has to be put into HiZ when de-activated to cause
43 * minmal side effect when scanning other columns, here it is configured to
44 * be input, and it should work on most platforms.
46 static void __activate_col(const struct matrix_keypad_platform_data
*pdata
,
49 bool level_on
= !pdata
->active_low
;
52 gpio_direction_output(pdata
->col_gpios
[col
], level_on
);
54 gpio_set_value_cansleep(pdata
->col_gpios
[col
], !level_on
);
55 gpio_direction_input(pdata
->col_gpios
[col
]);
59 static void activate_col(const struct matrix_keypad_platform_data
*pdata
,
62 __activate_col(pdata
, col
, on
);
64 if (on
&& pdata
->col_scan_delay_us
)
65 udelay(pdata
->col_scan_delay_us
);
68 static void activate_all_cols(const struct matrix_keypad_platform_data
*pdata
,
73 for (col
= 0; col
< pdata
->num_col_gpios
; col
++)
74 __activate_col(pdata
, col
, on
);
77 static bool row_asserted(const struct matrix_keypad_platform_data
*pdata
,
80 return gpio_get_value_cansleep(pdata
->row_gpios
[row
]) ?
81 !pdata
->active_low
: pdata
->active_low
;
84 static void enable_row_irqs(struct matrix_keypad
*keypad
)
86 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
89 for (i
= 0; i
< pdata
->num_row_gpios
; i
++)
90 enable_irq(gpio_to_irq(pdata
->row_gpios
[i
]));
93 static void disable_row_irqs(struct matrix_keypad
*keypad
)
95 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
98 for (i
= 0; i
< pdata
->num_row_gpios
; i
++)
99 disable_irq_nosync(gpio_to_irq(pdata
->row_gpios
[i
]));
103 * This gets the keys from keyboard and reports it to input subsystem
105 static void matrix_keypad_scan(struct work_struct
*work
)
107 struct matrix_keypad
*keypad
=
108 container_of(work
, struct matrix_keypad
, work
.work
);
109 struct input_dev
*input_dev
= keypad
->input_dev
;
110 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
111 uint32_t new_state
[MATRIX_MAX_COLS
];
114 /* de-activate all columns for scanning */
115 activate_all_cols(pdata
, false);
117 memset(new_state
, 0, sizeof(new_state
));
119 /* assert each column and read the row status out */
120 for (col
= 0; col
< pdata
->num_col_gpios
; col
++) {
122 activate_col(pdata
, col
, true);
124 for (row
= 0; row
< pdata
->num_row_gpios
; row
++)
126 row_asserted(pdata
, row
) ? (1 << row
) : 0;
128 activate_col(pdata
, col
, false);
131 for (col
= 0; col
< pdata
->num_col_gpios
; col
++) {
132 uint32_t bits_changed
;
134 bits_changed
= keypad
->last_key_state
[col
] ^ new_state
[col
];
135 if (bits_changed
== 0)
138 for (row
= 0; row
< pdata
->num_row_gpios
; row
++) {
139 if ((bits_changed
& (1 << row
)) == 0)
142 code
= MATRIX_SCAN_CODE(row
, col
, keypad
->row_shift
);
143 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
144 input_report_key(input_dev
,
145 keypad
->keycodes
[code
],
146 new_state
[col
] & (1 << row
));
149 input_sync(input_dev
);
151 memcpy(keypad
->last_key_state
, new_state
, sizeof(new_state
));
153 activate_all_cols(pdata
, true);
155 /* Enable IRQs again */
156 spin_lock_irq(&keypad
->lock
);
157 keypad
->scan_pending
= false;
158 enable_row_irqs(keypad
);
159 spin_unlock_irq(&keypad
->lock
);
162 static irqreturn_t
matrix_keypad_interrupt(int irq
, void *id
)
164 struct matrix_keypad
*keypad
= id
;
167 spin_lock_irqsave(&keypad
->lock
, flags
);
170 * See if another IRQ beaten us to it and scheduled the
171 * scan already. In that case we should not try to
172 * disable IRQs again.
174 if (unlikely(keypad
->scan_pending
|| keypad
->stopped
))
177 disable_row_irqs(keypad
);
178 keypad
->scan_pending
= true;
179 schedule_delayed_work(&keypad
->work
,
180 msecs_to_jiffies(keypad
->pdata
->debounce_ms
));
183 spin_unlock_irqrestore(&keypad
->lock
, flags
);
187 static int matrix_keypad_start(struct input_dev
*dev
)
189 struct matrix_keypad
*keypad
= input_get_drvdata(dev
);
191 keypad
->stopped
= false;
195 * Schedule an immediate key scan to capture current key state;
196 * columns will be activated and IRQs be enabled after the scan.
198 schedule_delayed_work(&keypad
->work
, 0);
203 static void matrix_keypad_stop(struct input_dev
*dev
)
205 struct matrix_keypad
*keypad
= input_get_drvdata(dev
);
207 keypad
->stopped
= true;
209 flush_work(&keypad
->work
.work
);
211 * matrix_keypad_scan() will leave IRQs enabled;
212 * we should disable them now.
214 disable_row_irqs(keypad
);
218 static int matrix_keypad_suspend(struct device
*dev
)
220 struct platform_device
*pdev
= to_platform_device(dev
);
221 struct matrix_keypad
*keypad
= platform_get_drvdata(pdev
);
222 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
225 matrix_keypad_stop(keypad
->input_dev
);
227 if (device_may_wakeup(&pdev
->dev
)) {
228 for (i
= 0; i
< pdata
->num_row_gpios
; i
++) {
229 if (!test_bit(i
, keypad
->disabled_gpios
)) {
230 unsigned int gpio
= pdata
->row_gpios
[i
];
232 if (enable_irq_wake(gpio_to_irq(gpio
)) == 0)
233 __set_bit(i
, keypad
->disabled_gpios
);
241 static int matrix_keypad_resume(struct device
*dev
)
243 struct platform_device
*pdev
= to_platform_device(dev
);
244 struct matrix_keypad
*keypad
= platform_get_drvdata(pdev
);
245 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
248 if (device_may_wakeup(&pdev
->dev
)) {
249 for (i
= 0; i
< pdata
->num_row_gpios
; i
++) {
250 if (test_and_clear_bit(i
, keypad
->disabled_gpios
)) {
251 unsigned int gpio
= pdata
->row_gpios
[i
];
253 disable_irq_wake(gpio_to_irq(gpio
));
258 matrix_keypad_start(keypad
->input_dev
);
263 static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops
,
264 matrix_keypad_suspend
, matrix_keypad_resume
);
267 static int __devinit
init_matrix_gpio(struct platform_device
*pdev
,
268 struct matrix_keypad
*keypad
)
270 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
271 int i
, err
= -EINVAL
;
273 /* initialized strobe lines as outputs, activated */
274 for (i
= 0; i
< pdata
->num_col_gpios
; i
++) {
275 err
= gpio_request(pdata
->col_gpios
[i
], "matrix_kbd_col");
278 "failed to request GPIO%d for COL%d\n",
279 pdata
->col_gpios
[i
], i
);
283 gpio_direction_output(pdata
->col_gpios
[i
], !pdata
->active_low
);
286 for (i
= 0; i
< pdata
->num_row_gpios
; i
++) {
287 err
= gpio_request(pdata
->row_gpios
[i
], "matrix_kbd_row");
290 "failed to request GPIO%d for ROW%d\n",
291 pdata
->row_gpios
[i
], i
);
295 gpio_direction_input(pdata
->row_gpios
[i
]);
298 for (i
= 0; i
< pdata
->num_row_gpios
; i
++) {
299 err
= request_irq(gpio_to_irq(pdata
->row_gpios
[i
]),
300 matrix_keypad_interrupt
,
302 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
303 "matrix-keypad", keypad
);
306 "Unable to acquire interrupt for GPIO line %i\n",
307 pdata
->row_gpios
[i
]);
312 /* initialized as disabled - enabled by input->open */
313 disable_row_irqs(keypad
);
318 free_irq(gpio_to_irq(pdata
->row_gpios
[i
]), keypad
);
319 i
= pdata
->num_row_gpios
;
322 gpio_free(pdata
->row_gpios
[i
]);
323 i
= pdata
->num_col_gpios
;
326 gpio_free(pdata
->col_gpios
[i
]);
331 static int __devinit
matrix_keypad_probe(struct platform_device
*pdev
)
333 const struct matrix_keypad_platform_data
*pdata
;
334 const struct matrix_keymap_data
*keymap_data
;
335 struct matrix_keypad
*keypad
;
336 struct input_dev
*input_dev
;
337 unsigned short *keycodes
;
338 unsigned int row_shift
;
341 pdata
= pdev
->dev
.platform_data
;
343 dev_err(&pdev
->dev
, "no platform data defined\n");
347 keymap_data
= pdata
->keymap_data
;
349 dev_err(&pdev
->dev
, "no keymap data defined\n");
353 row_shift
= get_count_order(pdata
->num_col_gpios
);
355 keypad
= kzalloc(sizeof(struct matrix_keypad
), GFP_KERNEL
);
356 keycodes
= kzalloc((pdata
->num_row_gpios
<< row_shift
) *
359 input_dev
= input_allocate_device();
360 if (!keypad
|| !keycodes
|| !input_dev
) {
365 keypad
->input_dev
= input_dev
;
366 keypad
->pdata
= pdata
;
367 keypad
->keycodes
= keycodes
;
368 keypad
->row_shift
= row_shift
;
369 keypad
->stopped
= true;
370 INIT_DELAYED_WORK(&keypad
->work
, matrix_keypad_scan
);
371 spin_lock_init(&keypad
->lock
);
373 input_dev
->name
= pdev
->name
;
374 input_dev
->id
.bustype
= BUS_HOST
;
375 input_dev
->dev
.parent
= &pdev
->dev
;
376 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
);
377 input_dev
->open
= matrix_keypad_start
;
378 input_dev
->close
= matrix_keypad_stop
;
380 input_dev
->keycode
= keycodes
;
381 input_dev
->keycodesize
= sizeof(*keycodes
);
382 input_dev
->keycodemax
= pdata
->num_row_gpios
<< row_shift
;
384 matrix_keypad_build_keymap(keymap_data
, row_shift
,
385 input_dev
->keycode
, input_dev
->keybit
);
387 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
388 input_set_drvdata(input_dev
, keypad
);
390 err
= init_matrix_gpio(pdev
, keypad
);
394 err
= input_register_device(keypad
->input_dev
);
398 device_init_wakeup(&pdev
->dev
, pdata
->wakeup
);
399 platform_set_drvdata(pdev
, keypad
);
404 input_free_device(input_dev
);
410 static int __devexit
matrix_keypad_remove(struct platform_device
*pdev
)
412 struct matrix_keypad
*keypad
= platform_get_drvdata(pdev
);
413 const struct matrix_keypad_platform_data
*pdata
= keypad
->pdata
;
416 device_init_wakeup(&pdev
->dev
, 0);
418 for (i
= 0; i
< pdata
->num_row_gpios
; i
++) {
419 free_irq(gpio_to_irq(pdata
->row_gpios
[i
]), keypad
);
420 gpio_free(pdata
->row_gpios
[i
]);
423 for (i
= 0; i
< pdata
->num_col_gpios
; i
++)
424 gpio_free(pdata
->col_gpios
[i
]);
426 input_unregister_device(keypad
->input_dev
);
427 platform_set_drvdata(pdev
, NULL
);
428 kfree(keypad
->keycodes
);
434 static struct platform_driver matrix_keypad_driver
= {
435 .probe
= matrix_keypad_probe
,
436 .remove
= __devexit_p(matrix_keypad_remove
),
438 .name
= "matrix-keypad",
439 .owner
= THIS_MODULE
,
441 .pm
= &matrix_keypad_pm_ops
,
446 static int __init
matrix_keypad_init(void)
448 return platform_driver_register(&matrix_keypad_driver
);
451 static void __exit
matrix_keypad_exit(void)
453 platform_driver_unregister(&matrix_keypad_driver
);
456 module_init(matrix_keypad_init
);
457 module_exit(matrix_keypad_exit
);
459 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
460 MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
461 MODULE_LICENSE("GPL v2");
462 MODULE_ALIAS("platform:matrix-keypad");