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