rename target/linux/generic-2.6 to generic
[openwrt.git] / target / linux / generic / files / drivers / input / misc / gpio_buttons.c
1 /*
2 * Driver for buttons on GPIO lines not capable of generating interrupts
3 *
4 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
6 *
7 * This file was based on: /drivers/input/misc/cobalt_btns.c
8 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
9 *
10 * also was based on: /drivers/input/keyboard/gpio_keys.c
11 * Copyright 2005 Phil Blundell
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23
24 #include <linux/input.h>
25 #include <linux/input-polldev.h>
26 #include <linux/ioport.h>
27 #include <linux/platform_device.h>
28
29 #include <linux/gpio_buttons.h>
30
31 #include <asm/gpio.h>
32
33 #define DRV_NAME "gpio-buttons"
34 #define DRV_VERSION "0.1.2"
35 #define PFX DRV_NAME ": "
36
37 struct gpio_button_data {
38 int last_state;
39 int count;
40 };
41
42 struct gpio_buttons_dev {
43 struct input_polled_dev *poll_dev;
44 struct gpio_buttons_platform_data *pdata;
45 struct gpio_button_data *data;
46 };
47
48 static void gpio_buttons_poll(struct input_polled_dev *dev)
49 {
50 struct gpio_buttons_dev *bdev = dev->private;
51 struct gpio_buttons_platform_data *pdata = bdev->pdata;
52 struct input_dev *input = dev->input;
53 int i;
54
55 for (i = 0; i < bdev->pdata->nbuttons; i++) {
56 struct gpio_button *button = &pdata->buttons[i];
57 unsigned int type = button->type ?: EV_KEY;
58 int state;
59
60 if (bdev->data[i].count < button->threshold) {
61 bdev->data[i].count++;
62 continue;
63 }
64
65 state = gpio_get_value(button->gpio) ? 1 : 0;
66 if (state != bdev->data[i].last_state) {
67 input_event(input, type, button->code,
68 !!(state ^ button->active_low));
69 input_sync(input);
70 bdev->data[i].count = 0;
71 bdev->data[i].last_state = state;
72 }
73 }
74 }
75
76 static int __devinit gpio_buttons_probe(struct platform_device *pdev)
77 {
78 struct gpio_buttons_platform_data *pdata = pdev->dev.platform_data;
79 struct gpio_buttons_dev *bdev;
80 struct input_polled_dev *poll_dev;
81 struct input_dev *input;
82 int error, i;
83
84 if (!pdata)
85 return -ENXIO;
86
87 bdev = kzalloc(sizeof(struct gpio_buttons_dev) +
88 sizeof(struct gpio_button_data) * pdata->nbuttons,
89 GFP_KERNEL);
90 if (!bdev) {
91 printk(KERN_ERR DRV_NAME "no memory for device\n");
92 return -ENOMEM;
93 }
94
95 bdev->data = (struct gpio_button_data *) &bdev[1];
96
97 poll_dev = input_allocate_polled_device();
98 if (!poll_dev) {
99 printk(KERN_ERR DRV_NAME "no memory for polled device\n");
100 error = -ENOMEM;
101 goto err_free_bdev;
102 }
103
104 poll_dev->private = bdev;
105 poll_dev->poll = gpio_buttons_poll;
106 poll_dev->poll_interval = pdata->poll_interval;
107
108 input = poll_dev->input;
109
110 input->evbit[0] = BIT(EV_KEY);
111 input->name = pdev->name;
112 input->phys = "gpio-buttons/input0";
113 input->dev.parent = &pdev->dev;
114
115 input->id.bustype = BUS_HOST;
116 input->id.vendor = 0x0001;
117 input->id.product = 0x0001;
118 input->id.version = 0x0100;
119
120 for (i = 0; i < pdata->nbuttons; i++) {
121 struct gpio_button *button = &pdata->buttons[i];
122 unsigned int gpio = button->gpio;
123 unsigned int type = button->type ?: EV_KEY;
124
125 error = gpio_request(gpio, button->desc ?
126 button->desc : DRV_NAME);
127 if (error) {
128 printk(KERN_ERR PFX "unable to claim gpio %u, "
129 "error %d\n", gpio, error);
130 goto err_free_gpio;
131 }
132
133 error = gpio_direction_input(gpio);
134 if (error) {
135 printk(KERN_ERR PFX "unable to set direction on "
136 "gpio %u, error %d\n", gpio, error);
137 goto err_free_gpio;
138 }
139
140 input_set_capability(input, type, button->code);
141 bdev->data[i].last_state = gpio_get_value(button->gpio) ? 1 : 0;
142 }
143
144 bdev->poll_dev = poll_dev;
145 bdev->pdata = pdata;
146 platform_set_drvdata(pdev, bdev);
147
148 error = input_register_polled_device(poll_dev);
149 if (error) {
150 printk(KERN_ERR PFX "unable to register polled device, "
151 "error %d\n", error);
152 goto err_free_gpio;
153 }
154
155 return 0;
156
157 err_free_gpio:
158 for (i = i - 1; i >= 0; i--)
159 gpio_free(pdata->buttons[i].gpio);
160
161 input_free_polled_device(poll_dev);
162
163 err_free_bdev:
164 kfree(bdev);
165
166 platform_set_drvdata(pdev, NULL);
167 return error;
168 }
169
170 static int __devexit gpio_buttons_remove(struct platform_device *pdev)
171 {
172 struct gpio_buttons_dev *bdev = platform_get_drvdata(pdev);
173 struct gpio_buttons_platform_data *pdata = bdev->pdata;
174 int i;
175
176 input_unregister_polled_device(bdev->poll_dev);
177
178 for (i = 0; i < pdata->nbuttons; i++)
179 gpio_free(pdata->buttons[i].gpio);
180
181 input_free_polled_device(bdev->poll_dev);
182
183 kfree(bdev);
184 platform_set_drvdata(pdev, NULL);
185
186 return 0;
187 }
188
189 static struct platform_driver gpio_buttons_driver = {
190 .probe = gpio_buttons_probe,
191 .remove = __devexit_p(gpio_buttons_remove),
192 .driver = {
193 .name = DRV_NAME,
194 .owner = THIS_MODULE,
195 },
196 };
197
198 static int __init gpio_buttons_init(void)
199 {
200 printk(KERN_INFO DRV_NAME " driver version " DRV_VERSION "\n");
201 return platform_driver_register(&gpio_buttons_driver);
202 }
203
204 static void __exit gpio_buttons_exit(void)
205 {
206 platform_driver_unregister(&gpio_buttons_driver);
207 }
208
209 module_init(gpio_buttons_init);
210 module_exit(gpio_buttons_exit);
211
212 MODULE_LICENSE("GPL");
213 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
214 MODULE_VERSION(DRV_VERSION);
215 MODULE_DESCRIPTION("Polled buttons driver for CPU GPIOs");
216
This page took 0.052816 seconds and 5 git commands to generate.