2 * Driver for keys on GPIO lines capable of generating interrupts.
4 * Copyright 2005 Phil Blundell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/version.h>
14 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/input.h>
25 #include <linux/irq.h>
28 #include <asm/arch/hardware.h>
30 #include <asm/hardware/gpio_keys.h>
32 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
35 struct platform_device *pdev = dev_id;
36 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
37 struct input_dev *input = platform_get_drvdata(pdev);
39 for (i = 0; i < pdata->nbuttons; i++) {
40 int gpio = pdata->buttons[i].gpio;
41 if (irq == gpio_to_irq(gpio)) {
42 int state = (gpio_get_value(gpio) ? 1 : 0) ^ (pdata->buttons[i].active_low);
44 input_report_key(input, pdata->buttons[i].keycode, state);
52 static int __devinit gpio_keys_probe(struct platform_device *pdev)
54 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
55 struct input_dev *input;
58 input = input_allocate_device();
62 platform_set_drvdata(pdev, input);
64 input->evbit[0] = BIT(EV_KEY);
66 input->name = pdev->name;
67 input->phys = "gpio-keys/input0";
68 input->dev.parent = &pdev->dev;
70 input->id.bustype = BUS_HOST;
71 input->id.vendor = 0x0001;
72 input->id.product = 0x0001;
73 input->id.version = 0x0100;
75 for (i = 0; i < pdata->nbuttons; i++) {
76 int code = pdata->buttons[i].keycode;
77 int irq = gpio_to_irq(pdata->buttons[i].gpio);
79 set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
80 error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
81 pdata->buttons[i].desc ? pdata->buttons[i].desc : "gpio_keys",
84 printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n",
88 set_bit(code, input->keybit);
91 error = input_register_device(input);
93 printk(KERN_ERR "Unable to register gpio-keys input device\n");
100 for (i = i - 1; i >= 0; i--)
101 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
103 input_free_device(input);
108 static int __devexit gpio_keys_remove(struct platform_device *pdev)
110 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
111 struct input_dev *input = platform_get_drvdata(pdev);
114 for (i = 0; i < pdata->nbuttons; i++) {
115 int irq = gpio_to_irq(pdata->buttons[i].gpio);
119 input_unregister_device(input);
124 struct platform_driver gpio_keys_device_driver = {
125 .probe = gpio_keys_probe,
126 .remove = __devexit_p(gpio_keys_remove),
132 static int __init gpio_keys_init(void)
134 return platform_driver_register(&gpio_keys_device_driver);
137 static void __exit gpio_keys_exit(void)
139 platform_driver_unregister(&gpio_keys_device_driver);
142 module_init(gpio_keys_init);
143 module_exit(gpio_keys_exit);
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
147 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");