ecryptfs: fix use with tmpfs by removing d_drop from ecryptfs_destroy_inode
[linux/fpc-iii.git] / drivers / input / keyboard / matrix_keypad.c
blobd3c8b61a941d46aa677e8f13dff3aba9edcb6f56
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>
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;
36 spinlock_t lock;
37 bool scan_pending;
38 bool stopped;
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,
47 int col, bool on)
49 bool level_on = !pdata->active_low;
51 if (on) {
52 gpio_direction_output(pdata->col_gpios[col], level_on);
53 } else {
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,
60 int col, bool on)
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,
69 bool on)
71 int col;
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,
78 int row)
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;
87 int i;
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;
96 int i;
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];
112 int row, col, code;
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++)
125 new_state[col] |=
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)
136 continue;
138 for (row = 0; row < pdata->num_row_gpios; row++) {
139 if ((bits_changed & (1 << row)) == 0)
140 continue;
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;
165 unsigned long flags;
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))
175 goto out;
177 disable_row_irqs(keypad);
178 keypad->scan_pending = true;
179 schedule_delayed_work(&keypad->work,
180 msecs_to_jiffies(keypad->pdata->debounce_ms));
182 out:
183 spin_unlock_irqrestore(&keypad->lock, flags);
184 return IRQ_HANDLED;
187 static int matrix_keypad_start(struct input_dev *dev)
189 struct matrix_keypad *keypad = input_get_drvdata(dev);
191 keypad->stopped = false;
192 mb();
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);
200 return 0;
203 static void matrix_keypad_stop(struct input_dev *dev)
205 struct matrix_keypad *keypad = input_get_drvdata(dev);
207 keypad->stopped = true;
208 mb();
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);
217 #ifdef CONFIG_PM
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;
223 int i;
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);
238 return 0;
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;
246 int i;
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);
260 return 0;
263 static const SIMPLE_DEV_PM_OPS(matrix_keypad_pm_ops,
264 matrix_keypad_suspend, matrix_keypad_resume);
265 #endif
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");
276 if (err) {
277 dev_err(&pdev->dev,
278 "failed to request GPIO%d for COL%d\n",
279 pdata->col_gpios[i], i);
280 goto err_free_cols;
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");
288 if (err) {
289 dev_err(&pdev->dev,
290 "failed to request GPIO%d for ROW%d\n",
291 pdata->row_gpios[i], i);
292 goto err_free_rows;
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,
301 IRQF_DISABLED |
302 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
303 "matrix-keypad", keypad);
304 if (err) {
305 dev_err(&pdev->dev,
306 "Unable to acquire interrupt for GPIO line %i\n",
307 pdata->row_gpios[i]);
308 goto err_free_irqs;
312 /* initialized as disabled - enabled by input->open */
313 disable_row_irqs(keypad);
314 return 0;
316 err_free_irqs:
317 while (--i >= 0)
318 free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
319 i = pdata->num_row_gpios;
320 err_free_rows:
321 while (--i >= 0)
322 gpio_free(pdata->row_gpios[i]);
323 i = pdata->num_col_gpios;
324 err_free_cols:
325 while (--i >= 0)
326 gpio_free(pdata->col_gpios[i]);
328 return err;
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;
339 int err;
341 pdata = pdev->dev.platform_data;
342 if (!pdata) {
343 dev_err(&pdev->dev, "no platform data defined\n");
344 return -EINVAL;
347 keymap_data = pdata->keymap_data;
348 if (!keymap_data) {
349 dev_err(&pdev->dev, "no keymap data defined\n");
350 return -EINVAL;
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) *
357 sizeof(*keycodes),
358 GFP_KERNEL);
359 input_dev = input_allocate_device();
360 if (!keypad || !keycodes || !input_dev) {
361 err = -ENOMEM;
362 goto err_free_mem;
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);
391 if (err)
392 goto err_free_mem;
394 err = input_register_device(keypad->input_dev);
395 if (err)
396 goto err_free_mem;
398 device_init_wakeup(&pdev->dev, pdata->wakeup);
399 platform_set_drvdata(pdev, keypad);
401 return 0;
403 err_free_mem:
404 input_free_device(input_dev);
405 kfree(keycodes);
406 kfree(keypad);
407 return err;
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;
414 int i;
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);
429 kfree(keypad);
431 return 0;
434 static struct platform_driver matrix_keypad_driver = {
435 .probe = matrix_keypad_probe,
436 .remove = __devexit_p(matrix_keypad_remove),
437 .driver = {
438 .name = "matrix-keypad",
439 .owner = THIS_MODULE,
440 #ifdef CONFIG_PM
441 .pm = &matrix_keypad_pm_ops,
442 #endif
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");