blob: 34f4a29d497313f84dbfffd6d107deff1f2b79cc [file] [log] [blame]
Eric Miaobab76142009-06-29 00:20:52 -07001/*
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>
25
26struct matrix_keypad {
27 const struct matrix_keypad_platform_data *pdata;
28 struct input_dev *input_dev;
29 unsigned short *keycodes;
Eric Miaod82f1c32009-08-05 01:24:41 -070030 unsigned int row_shift;
Eric Miaobab76142009-06-29 00:20:52 -070031
32 uint32_t last_key_state[MATRIX_MAX_COLS];
33 struct delayed_work work;
34 bool scan_pending;
35 bool stopped;
36 spinlock_t lock;
37};
38
39/*
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.
43 */
44static void __activate_col(const struct matrix_keypad_platform_data *pdata,
45 int col, bool on)
46{
47 bool level_on = !pdata->active_low;
48
49 if (on) {
50 gpio_direction_output(pdata->col_gpios[col], level_on);
51 } else {
52 gpio_set_value_cansleep(pdata->col_gpios[col], !level_on);
53 gpio_direction_input(pdata->col_gpios[col]);
54 }
55}
56
57static void activate_col(const struct matrix_keypad_platform_data *pdata,
58 int col, bool on)
59{
60 __activate_col(pdata, col, on);
61
62 if (on && pdata->col_scan_delay_us)
63 udelay(pdata->col_scan_delay_us);
64}
65
66static void activate_all_cols(const struct matrix_keypad_platform_data *pdata,
67 bool on)
68{
69 int col;
70
71 for (col = 0; col < pdata->num_col_gpios; col++)
72 __activate_col(pdata, col, on);
73}
74
75static bool row_asserted(const struct matrix_keypad_platform_data *pdata,
76 int row)
77{
78 return gpio_get_value_cansleep(pdata->row_gpios[row]) ?
79 !pdata->active_low : pdata->active_low;
80}
81
82static void enable_row_irqs(struct matrix_keypad *keypad)
83{
84 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
85 int i;
86
87 for (i = 0; i < pdata->num_row_gpios; i++)
88 enable_irq(gpio_to_irq(pdata->row_gpios[i]));
89}
90
91static void disable_row_irqs(struct matrix_keypad *keypad)
92{
93 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
94 int i;
95
96 for (i = 0; i < pdata->num_row_gpios; i++)
97 disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
98}
99
100/*
101 * This gets the keys from keyboard and reports it to input subsystem
102 */
103static void matrix_keypad_scan(struct work_struct *work)
104{
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];
110 int row, col, code;
111
112 /* de-activate all columns for scanning */
113 activate_all_cols(pdata, false);
114
115 memset(new_state, 0, sizeof(new_state));
116
117 /* assert each column and read the row status out */
118 for (col = 0; col < pdata->num_col_gpios; col++) {
119
120 activate_col(pdata, col, true);
121
122 for (row = 0; row < pdata->num_row_gpios; row++)
123 new_state[col] |=
124 row_asserted(pdata, row) ? (1 << row) : 0;
125
126 activate_col(pdata, col, false);
127 }
128
129 for (col = 0; col < pdata->num_col_gpios; col++) {
130 uint32_t bits_changed;
131
132 bits_changed = keypad->last_key_state[col] ^ new_state[col];
133 if (bits_changed == 0)
134 continue;
135
136 for (row = 0; row < pdata->num_row_gpios; row++) {
137 if ((bits_changed & (1 << row)) == 0)
138 continue;
139
Eric Miaod82f1c32009-08-05 01:24:41 -0700140 code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
Eric Miaobab76142009-06-29 00:20:52 -0700141 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));
145 }
146 }
147 input_sync(input_dev);
148
149 memcpy(keypad->last_key_state, new_state, sizeof(new_state));
150
151 activate_all_cols(pdata, true);
152
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);
158}
159
160static irqreturn_t matrix_keypad_interrupt(int irq, void *id)
161{
162 struct matrix_keypad *keypad = id;
163 unsigned long flags;
164
165 spin_lock_irqsave(&keypad->lock, flags);
166
167 /*
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.
171 */
172 if (unlikely(keypad->scan_pending || keypad->stopped))
173 goto out;
174
175 disable_row_irqs(keypad);
176 keypad->scan_pending = true;
177 schedule_delayed_work(&keypad->work,
178 msecs_to_jiffies(keypad->pdata->debounce_ms));
179
180out:
181 spin_unlock_irqrestore(&keypad->lock, flags);
182 return IRQ_HANDLED;
183}
184
185static int matrix_keypad_start(struct input_dev *dev)
186{
187 struct matrix_keypad *keypad = input_get_drvdata(dev);
188
189 keypad->stopped = false;
190 mb();
191
192 /*
193 * Schedule an immediate key scan to capture current key state;
194 * columns will be activated and IRQs be enabled after the scan.
195 */
196 schedule_delayed_work(&keypad->work, 0);
197
198 return 0;
199}
200
201static void matrix_keypad_stop(struct input_dev *dev)
202{
203 struct matrix_keypad *keypad = input_get_drvdata(dev);
204
205 keypad->stopped = true;
206 mb();
207 flush_work(&keypad->work.work);
208 /*
209 * matrix_keypad_scan() will leave IRQs enabled;
210 * we should disable them now.
211 */
212 disable_row_irqs(keypad);
213}
214
215#ifdef CONFIG_PM
Dmitry Torokhovf72a28a2009-12-03 22:24:15 -0800216static int matrix_keypad_suspend(struct device *dev)
Eric Miaobab76142009-06-29 00:20:52 -0700217{
Dmitry Torokhovf72a28a2009-12-03 22:24:15 -0800218 struct platform_device *pdev = to_platform_device(dev);
Eric Miaobab76142009-06-29 00:20:52 -0700219 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
220 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
221 int i;
222
223 matrix_keypad_stop(keypad->input_dev);
224
225 if (device_may_wakeup(&pdev->dev))
226 for (i = 0; i < pdata->num_row_gpios; i++)
227 enable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
228
229 return 0;
230}
231
Dmitry Torokhovf72a28a2009-12-03 22:24:15 -0800232static int matrix_keypad_resume(struct device *dev)
Eric Miaobab76142009-06-29 00:20:52 -0700233{
Dmitry Torokhovf72a28a2009-12-03 22:24:15 -0800234 struct platform_device *pdev = to_platform_device(dev);
Eric Miaobab76142009-06-29 00:20:52 -0700235 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
236 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
237 int i;
238
239 if (device_may_wakeup(&pdev->dev))
240 for (i = 0; i < pdata->num_row_gpios; i++)
241 disable_irq_wake(gpio_to_irq(pdata->row_gpios[i]));
242
243 matrix_keypad_start(keypad->input_dev);
244
245 return 0;
246}
Dmitry Torokhovf72a28a2009-12-03 22:24:15 -0800247
248static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
249 matrix_keypad_suspend, matrix_keypad_resume);
Eric Miaobab76142009-06-29 00:20:52 -0700250#endif
251
252static int __devinit init_matrix_gpio(struct platform_device *pdev,
253 struct matrix_keypad *keypad)
254{
255 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
256 int i, err = -EINVAL;
257
258 /* initialized strobe lines as outputs, activated */
259 for (i = 0; i < pdata->num_col_gpios; i++) {
260 err = gpio_request(pdata->col_gpios[i], "matrix_kbd_col");
261 if (err) {
262 dev_err(&pdev->dev,
263 "failed to request GPIO%d for COL%d\n",
264 pdata->col_gpios[i], i);
265 goto err_free_cols;
266 }
267
268 gpio_direction_output(pdata->col_gpios[i], !pdata->active_low);
269 }
270
271 for (i = 0; i < pdata->num_row_gpios; i++) {
272 err = gpio_request(pdata->row_gpios[i], "matrix_kbd_row");
273 if (err) {
274 dev_err(&pdev->dev,
275 "failed to request GPIO%d for ROW%d\n",
276 pdata->row_gpios[i], i);
277 goto err_free_rows;
278 }
279
280 gpio_direction_input(pdata->row_gpios[i]);
281 }
282
283 for (i = 0; i < pdata->num_row_gpios; i++) {
284 err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
285 matrix_keypad_interrupt,
286 IRQF_DISABLED |
287 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
288 "matrix-keypad", keypad);
289 if (err) {
290 dev_err(&pdev->dev,
291 "Unable to acquire interrupt for GPIO line %i\n",
292 pdata->row_gpios[i]);
293 goto err_free_irqs;
294 }
295 }
296
297 /* initialized as disabled - enabled by input->open */
298 disable_row_irqs(keypad);
299 return 0;
300
301err_free_irqs:
302 while (--i >= 0)
303 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
304 i = pdata->num_row_gpios;
305err_free_rows:
306 while (--i >= 0)
307 gpio_free(pdata->row_gpios[i]);
308 i = pdata->num_col_gpios;
309err_free_cols:
310 while (--i >= 0)
311 gpio_free(pdata->col_gpios[i]);
312
313 return err;
314}
315
316static int __devinit matrix_keypad_probe(struct platform_device *pdev)
317{
318 const struct matrix_keypad_platform_data *pdata;
319 const struct matrix_keymap_data *keymap_data;
320 struct matrix_keypad *keypad;
321 struct input_dev *input_dev;
322 unsigned short *keycodes;
Eric Miaod82f1c32009-08-05 01:24:41 -0700323 unsigned int row_shift;
Eric Miaobab76142009-06-29 00:20:52 -0700324 int err;
325
326 pdata = pdev->dev.platform_data;
327 if (!pdata) {
328 dev_err(&pdev->dev, "no platform data defined\n");
329 return -EINVAL;
330 }
331
332 keymap_data = pdata->keymap_data;
333 if (!keymap_data) {
334 dev_err(&pdev->dev, "no keymap data defined\n");
335 return -EINVAL;
336 }
337
Eric Miaod82f1c32009-08-05 01:24:41 -0700338 row_shift = get_count_order(pdata->num_col_gpios);
Eric Miaobab76142009-06-29 00:20:52 -0700339
340 keypad = kzalloc(sizeof(struct matrix_keypad), GFP_KERNEL);
Eric Miaod82f1c32009-08-05 01:24:41 -0700341 keycodes = kzalloc((pdata->num_row_gpios << row_shift) *
342 sizeof(*keycodes),
Eric Miaobab76142009-06-29 00:20:52 -0700343 GFP_KERNEL);
344 input_dev = input_allocate_device();
345 if (!keypad || !keycodes || !input_dev) {
346 err = -ENOMEM;
347 goto err_free_mem;
348 }
349
350 keypad->input_dev = input_dev;
351 keypad->pdata = pdata;
352 keypad->keycodes = keycodes;
Eric Miaod82f1c32009-08-05 01:24:41 -0700353 keypad->row_shift = row_shift;
Eric Miaobab76142009-06-29 00:20:52 -0700354 keypad->stopped = true;
355 INIT_DELAYED_WORK(&keypad->work, matrix_keypad_scan);
356 spin_lock_init(&keypad->lock);
357
358 input_dev->name = pdev->name;
359 input_dev->id.bustype = BUS_HOST;
360 input_dev->dev.parent = &pdev->dev;
361 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
362 input_dev->open = matrix_keypad_start;
363 input_dev->close = matrix_keypad_stop;
364
365 input_dev->keycode = keycodes;
366 input_dev->keycodesize = sizeof(*keycodes);
Dmitry Torokhov77a53fd2009-08-25 19:24:13 -0700367 input_dev->keycodemax = pdata->num_row_gpios << row_shift;
Eric Miaobab76142009-06-29 00:20:52 -0700368
Dmitry Torokhov77a53fd2009-08-25 19:24:13 -0700369 matrix_keypad_build_keymap(keymap_data, row_shift,
370 input_dev->keycode, input_dev->keybit);
Eric Miaobab76142009-06-29 00:20:52 -0700371
372 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
373 input_set_drvdata(input_dev, keypad);
374
375 err = init_matrix_gpio(pdev, keypad);
376 if (err)
377 goto err_free_mem;
378
379 err = input_register_device(keypad->input_dev);
380 if (err)
381 goto err_free_mem;
382
383 device_init_wakeup(&pdev->dev, pdata->wakeup);
384 platform_set_drvdata(pdev, keypad);
385
386 return 0;
387
388err_free_mem:
389 input_free_device(input_dev);
390 kfree(keycodes);
391 kfree(keypad);
392 return err;
393}
394
395static int __devexit matrix_keypad_remove(struct platform_device *pdev)
396{
397 struct matrix_keypad *keypad = platform_get_drvdata(pdev);
398 const struct matrix_keypad_platform_data *pdata = keypad->pdata;
399 int i;
400
401 device_init_wakeup(&pdev->dev, 0);
402
403 for (i = 0; i < pdata->num_row_gpios; i++) {
404 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
405 gpio_free(pdata->row_gpios[i]);
406 }
407
408 for (i = 0; i < pdata->num_col_gpios; i++)
409 gpio_free(pdata->col_gpios[i]);
410
411 input_unregister_device(keypad->input_dev);
412 platform_set_drvdata(pdev, NULL);
413 kfree(keypad->keycodes);
414 kfree(keypad);
415
416 return 0;
417}
418
419static struct platform_driver matrix_keypad_driver = {
420 .probe = matrix_keypad_probe,
421 .remove = __devexit_p(matrix_keypad_remove),
Eric Miaobab76142009-06-29 00:20:52 -0700422 .driver = {
423 .name = "matrix-keypad",
424 .owner = THIS_MODULE,
Dmitry Torokhovf72a28a2009-12-03 22:24:15 -0800425#ifdef CONFIG_PM
426 .pm = &matrix_keypad_pm_ops,
427#endif
Eric Miaobab76142009-06-29 00:20:52 -0700428 },
429};
430
431static int __init matrix_keypad_init(void)
432{
433 return platform_driver_register(&matrix_keypad_driver);
434}
435
436static void __exit matrix_keypad_exit(void)
437{
438 platform_driver_unregister(&matrix_keypad_driver);
439}
440
441module_init(matrix_keypad_init);
442module_exit(matrix_keypad_exit);
443
444MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
445MODULE_DESCRIPTION("GPIO Driven Matrix Keypad Driver");
446MODULE_LICENSE("GPL v2");
447MODULE_ALIAS("platform:matrix-keypad");