blob: b3069bc00f03ae120c8e79aa2f59fab181e966d6 [file] [log] [blame]
Phil Blundell78a56aa2007-01-18 00:44:09 -05001/*
2 * Driver for keys on GPIO lines capable of generating interrupts.
3 *
4 * Copyright 2005 Phil Blundell
5 *
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.
9 */
10
11#include <linux/module.h>
12#include <linux/version.h>
13
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/sched.h>
19#include <linux/pm.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>
David Brownell49015be2007-03-05 00:30:22 -080026#include <linux/gpio_keys.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050027
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050028#include <asm/gpio.h>
Phil Blundell78a56aa2007-01-18 00:44:09 -050029
30static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
31{
32 int i;
33 struct platform_device *pdev = dev_id;
34 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
35 struct input_dev *input = platform_get_drvdata(pdev);
36
37 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040038 struct gpio_keys_button *button = &pdata->buttons[i];
39 int gpio = button->gpio;
Phil Blundell78a56aa2007-01-18 00:44:09 -050040
Roman Moravcik84767d02007-05-01 00:39:13 -040041 if (irq == gpio_to_irq(gpio)) {
42 unsigned int type = button->type ?: EV_KEY;
43 int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
44
45 input_event(input, type, button->code, !!state);
Phil Blundell78a56aa2007-01-18 00:44:09 -050046 input_sync(input);
47 }
48 }
49
50 return IRQ_HANDLED;
51}
52
53static int __devinit gpio_keys_probe(struct platform_device *pdev)
54{
55 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
56 struct input_dev *input;
57 int i, error;
58
59 input = input_allocate_device();
60 if (!input)
61 return -ENOMEM;
62
63 platform_set_drvdata(pdev, input);
64
65 input->evbit[0] = BIT(EV_KEY);
66
67 input->name = pdev->name;
68 input->phys = "gpio-keys/input0";
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -040069 input->dev.parent = &pdev->dev;
Phil Blundell78a56aa2007-01-18 00:44:09 -050070
71 input->id.bustype = BUS_HOST;
72 input->id.vendor = 0x0001;
73 input->id.product = 0x0001;
74 input->id.version = 0x0100;
75
76 for (i = 0; i < pdata->nbuttons; i++) {
Roman Moravcik84767d02007-05-01 00:39:13 -040077 struct gpio_keys_button *button = &pdata->buttons[i];
78 int irq = gpio_to_irq(button->gpio);
79 unsigned int type = button->type ?: EV_KEY;
Phil Blundell78a56aa2007-01-18 00:44:09 -050080
Anti Sullin006df302007-09-26 00:01:03 -040081 if (irq < 0) {
82 error = irq;
83 printk(KERN_ERR
84 "gpio-keys: "
85 "Unable to get irq number for GPIO %d,"
86 "error %d\n",
87 button->gpio, error);
88 goto fail;
89 }
90
91 error = request_irq(irq, gpio_keys_isr,
92 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
93 IRQF_TRIGGER_FALLING,
94 button->desc ? button->desc : "gpio_keys",
95 pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -050096 if (error) {
Anti Sullin006df302007-09-26 00:01:03 -040097 printk(KERN_ERR
98 "gpio-keys: Unable to claim irq %d; error %d\n",
Philipp Zabel0d98f6b2007-02-18 01:40:46 -050099 irq, error);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500100 goto fail;
101 }
Roman Moravcik84767d02007-05-01 00:39:13 -0400102
103 input_set_capability(input, type, button->code);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500104 }
105
106 error = input_register_device(input);
107 if (error) {
Anti Sullin006df302007-09-26 00:01:03 -0400108 printk(KERN_ERR
109 "gpio-keys: Unable to register input device, "
110 "error: %d\n", error);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500111 goto fail;
112 }
113
114 return 0;
115
116 fail:
Anti Sullin006df302007-09-26 00:01:03 -0400117 while (--i >= 0)
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500118 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500119
Anti Sullin006df302007-09-26 00:01:03 -0400120 platform_set_drvdata(pdev, NULL);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500121 input_free_device(input);
122
123 return error;
124}
125
126static int __devexit gpio_keys_remove(struct platform_device *pdev)
127{
128 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
129 struct input_dev *input = platform_get_drvdata(pdev);
130 int i;
131
132 for (i = 0; i < pdata->nbuttons; i++) {
Philipp Zabel0d98f6b2007-02-18 01:40:46 -0500133 int irq = gpio_to_irq(pdata->buttons[i].gpio);
Phil Blundell78a56aa2007-01-18 00:44:09 -0500134 free_irq(irq, pdev);
135 }
136
137 input_unregister_device(input);
138
139 return 0;
140}
141
142struct platform_driver gpio_keys_device_driver = {
143 .probe = gpio_keys_probe,
144 .remove = __devexit_p(gpio_keys_remove),
145 .driver = {
146 .name = "gpio-keys",
147 }
148};
149
150static int __init gpio_keys_init(void)
151{
152 return platform_driver_register(&gpio_keys_device_driver);
153}
154
155static void __exit gpio_keys_exit(void)
156{
157 platform_driver_unregister(&gpio_keys_device_driver);
158}
159
160module_init(gpio_keys_init);
161module_exit(gpio_keys_exit);
162
163MODULE_LICENSE("GPL");
164MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
165MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");