x86: cache_info: Kill the atomic allocation in amd_init_l3_cache()
[linux-2.6/linux-mips.git] / drivers / input / keyboard / gpio_keys_polled.c
blob4c17aff206576d72468071c4465e1a08e4f36fa4
1 /*
2 * Driver for buttons on GPIO lines not capable of generating interrupts
4 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
7 * This file was based on: /drivers/input/misc/cobalt_btns.c
8 * Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
10 * also was based on: /drivers/input/keyboard/gpio_keys.c
11 * Copyright 2005 Phil Blundell
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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/input-polldev.h>
24 #include <linux/ioport.h>
25 #include <linux/platform_device.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio_keys.h>
29 #define DRV_NAME "gpio-keys-polled"
31 struct gpio_keys_button_data {
32 int last_state;
33 int count;
34 int threshold;
35 int can_sleep;
38 struct gpio_keys_polled_dev {
39 struct input_polled_dev *poll_dev;
40 struct device *dev;
41 struct gpio_keys_platform_data *pdata;
42 struct gpio_keys_button_data data[0];
45 static void gpio_keys_polled_check_state(struct input_dev *input,
46 struct gpio_keys_button *button,
47 struct gpio_keys_button_data *bdata)
49 int state;
51 if (bdata->can_sleep)
52 state = !!gpio_get_value_cansleep(button->gpio);
53 else
54 state = !!gpio_get_value(button->gpio);
56 if (state != bdata->last_state) {
57 unsigned int type = button->type ?: EV_KEY;
59 input_event(input, type, button->code,
60 !!(state ^ button->active_low));
61 input_sync(input);
62 bdata->count = 0;
63 bdata->last_state = state;
67 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
69 struct gpio_keys_polled_dev *bdev = dev->private;
70 struct gpio_keys_platform_data *pdata = bdev->pdata;
71 struct input_dev *input = dev->input;
72 int i;
74 for (i = 0; i < bdev->pdata->nbuttons; i++) {
75 struct gpio_keys_button_data *bdata = &bdev->data[i];
77 if (bdata->count < bdata->threshold)
78 bdata->count++;
79 else
80 gpio_keys_polled_check_state(input, &pdata->buttons[i],
81 bdata);
85 static void gpio_keys_polled_open(struct input_polled_dev *dev)
87 struct gpio_keys_polled_dev *bdev = dev->private;
88 struct gpio_keys_platform_data *pdata = bdev->pdata;
90 if (pdata->enable)
91 pdata->enable(bdev->dev);
94 static void gpio_keys_polled_close(struct input_polled_dev *dev)
96 struct gpio_keys_polled_dev *bdev = dev->private;
97 struct gpio_keys_platform_data *pdata = bdev->pdata;
99 if (pdata->disable)
100 pdata->disable(bdev->dev);
103 static int __devinit gpio_keys_polled_probe(struct platform_device *pdev)
105 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
106 struct device *dev = &pdev->dev;
107 struct gpio_keys_polled_dev *bdev;
108 struct input_polled_dev *poll_dev;
109 struct input_dev *input;
110 int error;
111 int i;
113 if (!pdata || !pdata->poll_interval)
114 return -EINVAL;
116 bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) +
117 pdata->nbuttons * sizeof(struct gpio_keys_button_data),
118 GFP_KERNEL);
119 if (!bdev) {
120 dev_err(dev, "no memory for private data\n");
121 return -ENOMEM;
124 poll_dev = input_allocate_polled_device();
125 if (!poll_dev) {
126 dev_err(dev, "no memory for polled device\n");
127 error = -ENOMEM;
128 goto err_free_bdev;
131 poll_dev->private = bdev;
132 poll_dev->poll = gpio_keys_polled_poll;
133 poll_dev->poll_interval = pdata->poll_interval;
134 poll_dev->open = gpio_keys_polled_open;
135 poll_dev->close = gpio_keys_polled_close;
137 input = poll_dev->input;
139 input->evbit[0] = BIT(EV_KEY);
140 input->name = pdev->name;
141 input->phys = DRV_NAME"/input0";
142 input->dev.parent = &pdev->dev;
144 input->id.bustype = BUS_HOST;
145 input->id.vendor = 0x0001;
146 input->id.product = 0x0001;
147 input->id.version = 0x0100;
149 for (i = 0; i < pdata->nbuttons; i++) {
150 struct gpio_keys_button *button = &pdata->buttons[i];
151 struct gpio_keys_button_data *bdata = &bdev->data[i];
152 unsigned int gpio = button->gpio;
153 unsigned int type = button->type ?: EV_KEY;
155 if (button->wakeup) {
156 dev_err(dev, DRV_NAME " does not support wakeup\n");
157 error = -EINVAL;
158 goto err_free_gpio;
161 error = gpio_request(gpio,
162 button->desc ? button->desc : DRV_NAME);
163 if (error) {
164 dev_err(dev, "unable to claim gpio %u, err=%d\n",
165 gpio, error);
166 goto err_free_gpio;
169 error = gpio_direction_input(gpio);
170 if (error) {
171 dev_err(dev,
172 "unable to set direction on gpio %u, err=%d\n",
173 gpio, error);
174 goto err_free_gpio;
177 bdata->can_sleep = gpio_cansleep(gpio);
178 bdata->last_state = -1;
179 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
180 pdata->poll_interval);
182 input_set_capability(input, type, button->code);
185 bdev->poll_dev = poll_dev;
186 bdev->dev = dev;
187 bdev->pdata = pdata;
188 platform_set_drvdata(pdev, bdev);
190 error = input_register_polled_device(poll_dev);
191 if (error) {
192 dev_err(dev, "unable to register polled device, err=%d\n",
193 error);
194 goto err_free_gpio;
197 /* report initial state of the buttons */
198 for (i = 0; i < pdata->nbuttons; i++)
199 gpio_keys_polled_check_state(input, &pdata->buttons[i],
200 &bdev->data[i]);
202 return 0;
204 err_free_gpio:
205 while (--i >= 0)
206 gpio_free(pdata->buttons[i].gpio);
208 input_free_polled_device(poll_dev);
210 err_free_bdev:
211 kfree(bdev);
213 platform_set_drvdata(pdev, NULL);
214 return error;
217 static int __devexit gpio_keys_polled_remove(struct platform_device *pdev)
219 struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
220 struct gpio_keys_platform_data *pdata = bdev->pdata;
221 int i;
223 input_unregister_polled_device(bdev->poll_dev);
225 for (i = 0; i < pdata->nbuttons; i++)
226 gpio_free(pdata->buttons[i].gpio);
228 input_free_polled_device(bdev->poll_dev);
230 kfree(bdev);
231 platform_set_drvdata(pdev, NULL);
233 return 0;
236 static struct platform_driver gpio_keys_polled_driver = {
237 .probe = gpio_keys_polled_probe,
238 .remove = __devexit_p(gpio_keys_polled_remove),
239 .driver = {
240 .name = DRV_NAME,
241 .owner = THIS_MODULE,
245 static int __init gpio_keys_polled_init(void)
247 return platform_driver_register(&gpio_keys_polled_driver);
250 static void __exit gpio_keys_polled_exit(void)
252 platform_driver_unregister(&gpio_keys_polled_driver);
255 module_init(gpio_keys_polled_init);
256 module_exit(gpio_keys_polled_exit);
258 MODULE_LICENSE("GPL v2");
259 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
260 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
261 MODULE_ALIAS("platform:" DRV_NAME);