]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/keyboard/matrix_keypad.c
Merge branch 'master' of git://dev.medozas.de/linux
[net-next-2.6.git] / drivers / input / keyboard / matrix_keypad.c
CommitLineData
bab7614d
EM
1/*
2 * GPIO driven matrix keyboard driver
3 *
4 * Copyright (c) 2008 Marek Vasut <marek.vasut@gmail.com>
5 *
6 * Based on corgikbd.c
7 *
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.
11 *
12 */
13
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>
5a0e3ad6 25#include <linux/slab.h>
bab7614d
EM
26
27struct matrix_keypad {
28 const struct matrix_keypad_platform_data *pdata;
29 struct input_dev *input_dev;
30 unsigned short *keycodes;
d82f1c35 31 unsigned int row_shift;
bab7614d 32
dd219234
DT
33 DECLARE_BITMAP(disabled_gpios, MATRIX_MAX_ROWS);
34
bab7614d
EM
35 uint32_t last_key_state[MATRIX_MAX_COLS];
36 struct delayed_work work;
dd219234 37 spinlock_t lock;
bab7614d
EM
38 bool scan_pending;
39 bool stopped;
bab7614d
EM
40};
41
42/*
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.
46 */
47static void __activate_col(const struct matrix_keypad_platform_data *pdata,
48 int col, bool on)
49{
50 bool level_on = !pdata->active_low;
51
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]);
57 }
58}
59
60static void activate_col(const struct matrix_keypad_platform_data *pdata,
61 int col, bool on)
62{
63 __activate_col(pdata, col, on);
64
65 if (on && pdata->col_scan_delay_us)
66 udelay(pdata->col_scan_delay_us);
67}
68
69static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
70 bool on)
71{
72 int col;
73
74 for (col = 0; col < pdata->num_col_gpios; col++)
75 __activate_col(pdata, col, on);
76}
77
78static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
79 int row)
80{
81 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
82 !pdata->active_low : pdata->active_low;
83}
84
85static void enable_row_irqs(struct matrix_keypad *keypad)
86{
87 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
88 int i;
89
90 for (i = 0; i < pdata->num_row_gpios; i++)
91 enable_irq(gpio_to_irq(pdata->row_gpios[i]));
92}
93
94static void disable_row_irqs(struct matrix_keypad *keypad)
95{
96 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
97 int i;
98
99 for (i = 0; i < pdata->num_row_gpios; i++)
100 disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
101}
102
103/*
104 * This gets the keys from keyboard and reports it to input subsystem
105 */
106static void matrix_keypad_scan(struct work_struct *work)
107{
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;
114
115 /* de-activate all columns for scanning */
116 activate_all_cols(pdata, false);
117
118 memset(new_state, 0, sizeof(new_state));
119
120 /* assert each column and read the row status out */
121 for (col = 0; col < pdata->num_col_gpios; col++) {
122
123 activate_col(pdata, col, true);
124
125 for (row = 0; row < pdata->num_row_gpios; row++)
126 new_state[col] |=
127 row_asserted(pdata, row) ? (1 << row) : 0;
128
129 activate_col(pdata, col, false);
130 }
131
132 for (col = 0; col < pdata->num_col_gpios; col++) {
133 uint32_t bits_changed;
134
135 bits_changed = keypad->last_key_state[col] ^ new_state[col];
136 if (bits_changed == 0)
137 continue;
138
139 for (row = 0; row < pdata->num_row_gpios; row++) {
140 if ((bits_changed & (1 << row)) == 0)
141 continue;
142
d82f1c35 143 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
bab7614d
EM
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));
148 }
149 }
150 input_sync(input_dev);
151
152 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
153
154 activate_all_cols(pdata, true);
155
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);
161}
162
163static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
164{
165 struct matrix_keypad *keypad = id;
166 unsigned long flags;
167
168 spin_lock_irqsave(&keypad->lock, flags);
169
170 /*
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.
174 */
175 if (unlikely(keypad->scan_pending || keypad->stopped))
176 goto out;
177
178 disable_row_irqs(keypad);
179 keypad->scan_pending = true;
180 schedule_delayed_work(&keypad->work,
181 msecs_to_jiffies(keypad->pdata->debounce_ms));
182
183out:
184 spin_unlock_irqrestore(&keypad->lock, flags);
185 return IRQ_HANDLED;
186}
187
188static int matrix_keypad_start(struct input_dev *dev)
189{
190 struct matrix_keypad *keypad = input_get_drvdata(dev);
191
192 keypad->stopped = false;
193 mb();
194
195 /*
196 * Schedule an immediate key scan to capture current key state;
197 * columns will be activated and IRQs be enabled after the scan.
198 */
199 schedule_delayed_work(&keypad->work, 0);
200
201 return 0;
202}
203
204static void matrix_keypad_stop(struct input_dev *dev)
205{
206 struct matrix_keypad *keypad = input_get_drvdata(dev);
207
208 keypad->stopped = true;
209 mb();
210 flush_work(&keypad->work.work);
211 /*
212 * matrix_keypad_scan() will leave IRQs enabled;
213 * we should disable them now.
214 */
215 disable_row_irqs(keypad);
216}
217
218#ifdef CONFIG_PM
f72a28ab 219static int matrix_keypad_suspend(struct device *dev)
bab7614d 220{
f72a28ab 221 struct platform_device *pdev = to_platform_device(dev);
bab7614d
EM
222 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
223 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
224 int i;
225
226 matrix_keypad_stop(keypad->input_dev);
227
dd219234
DT
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];
232
233 if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
234 __set_bit(i, keypad->disabled_gpios);
235 }
236 }
237 }
bab7614d
EM
238
239 return 0;
240}
241
f72a28ab 242static int matrix_keypad_resume(struct device *dev)
bab7614d 243{
f72a28ab 244 struct platform_device *pdev = to_platform_device(dev);
bab7614d
EM
245 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
246 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
247 int i;
248
dd219234
DT
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];
253
254 disable_irq_wake(gpio_to_irq(gpio));
255 }
256 }
257 }
bab7614d
EM
258
259 matrix_keypad_start(keypad->input_dev);
260
261 return 0;
262}
f72a28ab
DT
263
264static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
265 matrix_keypad_suspend, matrix_keypad_resume);
bab7614d
EM
266#endif
267
268static int __devinit init_matrix_gpio(struct platform_device *pdev,
269 struct matrix_keypad *keypad)
270{
271 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
272 int i, err = -EINVAL;
273
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;
282 }
283
284 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
285 }
286
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;
294 }
295
296 gpio_direction_input(pdata->row_gpios[i]);
297 }
298
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;
310 }
311 }
312
313 /* initialized as disabled - enabled by input->open */
314 disable_row_irqs(keypad);
315 return 0;
316
317err_free_irqs:
318 while (--i >= 0)
319 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
320 i = pdata->num_row_gpios;
321err_free_rows:
322 while (--i >= 0)
323 gpio_free(pdata->row_gpios[i]);
324 i = pdata->num_col_gpios;
325err_free_cols:
326 while (--i >= 0)
327 gpio_free(pdata->col_gpios[i]);
328
329 return err;
330}
331
332static int __devinit matrix_keypad_probe(struct platform_device *pdev)
333{
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;
d82f1c35 339 unsigned int row_shift;
bab7614d
EM
340 int err;
341
342 pdata = pdev->dev.platform_data;
343 if (!pdata) {
344 dev_err(&pdev->dev, "no platform data defined\n");
345 return -EINVAL;
346 }
347
348 keymap_data = pdata->keymap_data;
349 if (!keymap_data) {
350 dev_err(&pdev->dev, "no keymap data defined\n");
351 return -EINVAL;
352 }
353
d82f1c35 354 row_shift = get_count_order(pdata->num_col_gpios);
bab7614d
EM
355
356 keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
d82f1c35
EM
357 keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
358 sizeof(*keycodes),
bab7614d
EM
359 GFP_KERNEL);
360 input_dev = input_allocate_device();
361 if (!keypad || !keycodes || !input_dev) {
362 err = -ENOMEM;
363 goto err_free_mem;
364 }
365
366 keypad->input_dev = input_dev;
367 keypad->pdata = pdata;
368 keypad->keycodes = keycodes;
d82f1c35 369 keypad->row_shift = row_shift;
bab7614d
EM
370 keypad->stopped = true;
371 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
372 spin_lock_init(&keypad->lock);
373
374 input_dev->name = pdev->name;
375 input_dev->id.bustype = BUS_HOST;
376 input_dev->dev.parent = &pdev->dev;
9d32c305
HS
377 input_dev->evbit[0] = BIT_MASK(EV_KEY);
378 if (!pdata->no_autorepeat)
379 input_dev->evbit[0] |= BIT_MASK(EV_REP);
bab7614d
EM
380 input_dev->open = matrix_keypad_start;
381 input_dev->close = matrix_keypad_stop;
382
383 input_dev->keycode = keycodes;
384 input_dev->keycodesize = sizeof(*keycodes);
77a53fd2 385 input_dev->keycodemax = pdata->num_row_gpios << row_shift;
bab7614d 386
77a53fd2
DT
387 matrix_keypad_build_keymap(keymap_data, row_shift,
388 input_dev->keycode, input_dev->keybit);
bab7614d
EM
389
390 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
391 input_set_drvdata(input_dev, keypad);
392
393 err = init_matrix_gpio(pdev, keypad);
394 if (err)
395 goto err_free_mem;
396
397 err = input_register_device(keypad->input_dev);
398 if (err)
399 goto err_free_mem;
400
401 device_init_wakeup(&pdev->dev, pdata->wakeup);
402 platform_set_drvdata(pdev, keypad);
403
404 return 0;
405
406err_free_mem:
407 input_free_device(input_dev);
408 kfree(keycodes);
409 kfree(keypad);
410 return err;
411}
412
413static int __devexit matrix_keypad_remove(struct platform_device *pdev)
414{
415 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
416 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
417 int i;
418
419 device_init_wakeup(&pdev->dev, 0);
420
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]);
424 }
425
426 for (i = 0; i < pdata->num_col_gpios; i++)
427 gpio_free(pdata->col_gpios[i]);
428
429 input_unregister_device(keypad->input_dev);
430 platform_set_drvdata(pdev, NULL);
431 kfree(keypad->keycodes);
432 kfree(keypad);
433
434 return 0;
435}
436
437static struct platform_driver matrix_keypad_driver = {
438 .probe = matrix_keypad_probe,
439 .remove = __devexit_p(matrix_keypad_remove),
bab7614d
EM
440 .driver = {
441 .name = "matrix-keypad",
442 .owner = THIS_MODULE,
f72a28ab
DT
443#ifdef CONFIG_PM
444 .pm = &matrix_keypad_pm_ops,
445#endif
bab7614d
EM
446 },
447};
448
449static int __init matrix_keypad_init(void)
450{
451 return platform_driver_register(&matrix_keypad_driver);
452}
453
454static void __exit matrix_keypad_exit(void)
455{
456 platform_driver_unregister(&matrix_keypad_driver);
457}
458
459module_init(matrix_keypad_init);
460module_exit(matrix_keypad_exit);
461
462MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
463MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
464MODULE_LICENSE("GPL v2");
465MODULE_ALIAS("platform:matrix-keypad");