Btrfs: don't consider the missing device when allocating new chunks
[linux/fpc-iii.git] / drivers / input / keyboard / gpio_keys_polled.c
blob432d36395f3503d7ffcaeae88fb6a02c85c48fd3
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/slab.h>
21 #include <linux/input.h>
22 #include <linux/input-polldev.h>
23 #include <linux/ioport.h>
24 #include <linux/platform_device.h>
25 #include <linux/gpio.h>
26 #include <linux/gpio_keys.h>
27 #include <linux/of.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_gpio.h>
31 #define DRV_NAME "gpio-keys-polled"
33 struct gpio_keys_button_data {
34 int last_state;
35 int count;
36 int threshold;
37 int can_sleep;
40 struct gpio_keys_polled_dev {
41 struct input_polled_dev *poll_dev;
42 struct device *dev;
43 const struct gpio_keys_platform_data *pdata;
44 struct gpio_keys_button_data data[0];
47 static void gpio_keys_polled_check_state(struct input_dev *input,
48 struct gpio_keys_button *button,
49 struct gpio_keys_button_data *bdata)
51 int state;
53 if (bdata->can_sleep)
54 state = !!gpio_get_value_cansleep(button->gpio);
55 else
56 state = !!gpio_get_value(button->gpio);
58 if (state != bdata->last_state) {
59 unsigned int type = button->type ?: EV_KEY;
61 input_event(input, type, button->code,
62 !!(state ^ button->active_low));
63 input_sync(input);
64 bdata->count = 0;
65 bdata->last_state = state;
69 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
71 struct gpio_keys_polled_dev *bdev = dev->private;
72 const struct gpio_keys_platform_data *pdata = bdev->pdata;
73 struct input_dev *input = dev->input;
74 int i;
76 for (i = 0; i < pdata->nbuttons; i++) {
77 struct gpio_keys_button_data *bdata = &bdev->data[i];
79 if (bdata->count < bdata->threshold)
80 bdata->count++;
81 else
82 gpio_keys_polled_check_state(input, &pdata->buttons[i],
83 bdata);
87 static void gpio_keys_polled_open(struct input_polled_dev *dev)
89 struct gpio_keys_polled_dev *bdev = dev->private;
90 const struct gpio_keys_platform_data *pdata = bdev->pdata;
92 if (pdata->enable)
93 pdata->enable(bdev->dev);
96 static void gpio_keys_polled_close(struct input_polled_dev *dev)
98 struct gpio_keys_polled_dev *bdev = dev->private;
99 const struct gpio_keys_platform_data *pdata = bdev->pdata;
101 if (pdata->disable)
102 pdata->disable(bdev->dev);
105 #ifdef CONFIG_OF
106 static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct device *dev)
108 struct device_node *node, *pp;
109 struct gpio_keys_platform_data *pdata;
110 struct gpio_keys_button *button;
111 int error;
112 int nbuttons;
113 int i;
115 node = dev->of_node;
116 if (!node)
117 return NULL;
119 nbuttons = of_get_child_count(node);
120 if (nbuttons == 0)
121 return NULL;
123 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
124 GFP_KERNEL);
125 if (!pdata)
126 return ERR_PTR(-ENOMEM);
128 pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
129 pdata->nbuttons = nbuttons;
131 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
132 of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
134 i = 0;
135 for_each_child_of_node(node, pp) {
136 int gpio;
137 enum of_gpio_flags flags;
139 if (!of_find_property(pp, "gpios", NULL)) {
140 pdata->nbuttons--;
141 dev_warn(dev, "Found button without gpios\n");
142 continue;
145 gpio = of_get_gpio_flags(pp, 0, &flags);
146 if (gpio < 0) {
147 error = gpio;
148 if (error != -EPROBE_DEFER)
149 dev_err(dev,
150 "Failed to get gpio flags, error: %d\n",
151 error);
152 return ERR_PTR(error);
155 button = &pdata->buttons[i++];
157 button->gpio = gpio;
158 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
160 if (of_property_read_u32(pp, "linux,code", &button->code)) {
161 dev_err(dev, "Button without keycode: 0x%x\n",
162 button->gpio);
163 return ERR_PTR(-EINVAL);
166 button->desc = of_get_property(pp, "label", NULL);
168 if (of_property_read_u32(pp, "linux,input-type", &button->type))
169 button->type = EV_KEY;
171 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
173 if (of_property_read_u32(pp, "debounce-interval",
174 &button->debounce_interval))
175 button->debounce_interval = 5;
178 if (pdata->nbuttons == 0)
179 return ERR_PTR(-EINVAL);
181 return pdata;
184 static const struct of_device_id gpio_keys_polled_of_match[] = {
185 { .compatible = "gpio-keys-polled", },
186 { },
188 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
190 #else
192 static inline struct gpio_keys_platform_data *
193 gpio_keys_polled_get_devtree_pdata(struct device *dev)
195 return NULL;
197 #endif
199 static int gpio_keys_polled_probe(struct platform_device *pdev)
201 struct device *dev = &pdev->dev;
202 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
203 struct gpio_keys_polled_dev *bdev;
204 struct input_polled_dev *poll_dev;
205 struct input_dev *input;
206 size_t size;
207 int error;
208 int i;
210 if (!pdata) {
211 pdata = gpio_keys_polled_get_devtree_pdata(dev);
212 if (IS_ERR(pdata))
213 return PTR_ERR(pdata);
214 if (!pdata) {
215 dev_err(dev, "missing platform data\n");
216 return -EINVAL;
220 if (!pdata->poll_interval) {
221 dev_err(dev, "missing poll_interval value\n");
222 return -EINVAL;
225 size = sizeof(struct gpio_keys_polled_dev) +
226 pdata->nbuttons * sizeof(struct gpio_keys_button_data);
227 bdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
228 if (!bdev) {
229 dev_err(dev, "no memory for private data\n");
230 return -ENOMEM;
233 poll_dev = devm_input_allocate_polled_device(&pdev->dev);
234 if (!poll_dev) {
235 dev_err(dev, "no memory for polled device\n");
236 return -ENOMEM;
239 poll_dev->private = bdev;
240 poll_dev->poll = gpio_keys_polled_poll;
241 poll_dev->poll_interval = pdata->poll_interval;
242 poll_dev->open = gpio_keys_polled_open;
243 poll_dev->close = gpio_keys_polled_close;
245 input = poll_dev->input;
247 input->name = pdev->name;
248 input->phys = DRV_NAME"/input0";
250 input->id.bustype = BUS_HOST;
251 input->id.vendor = 0x0001;
252 input->id.product = 0x0001;
253 input->id.version = 0x0100;
255 __set_bit(EV_KEY, input->evbit);
256 if (pdata->rep)
257 __set_bit(EV_REP, input->evbit);
259 for (i = 0; i < pdata->nbuttons; i++) {
260 struct gpio_keys_button *button = &pdata->buttons[i];
261 struct gpio_keys_button_data *bdata = &bdev->data[i];
262 unsigned int gpio = button->gpio;
263 unsigned int type = button->type ?: EV_KEY;
265 if (button->wakeup) {
266 dev_err(dev, DRV_NAME " does not support wakeup\n");
267 return -EINVAL;
270 error = devm_gpio_request_one(&pdev->dev, gpio, GPIOF_IN,
271 button->desc ? : DRV_NAME);
272 if (error) {
273 dev_err(dev, "unable to claim gpio %u, err=%d\n",
274 gpio, error);
275 return error;
278 bdata->can_sleep = gpio_cansleep(gpio);
279 bdata->last_state = -1;
280 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
281 pdata->poll_interval);
283 input_set_capability(input, type, button->code);
286 bdev->poll_dev = poll_dev;
287 bdev->dev = dev;
288 bdev->pdata = pdata;
289 platform_set_drvdata(pdev, bdev);
291 error = input_register_polled_device(poll_dev);
292 if (error) {
293 dev_err(dev, "unable to register polled device, err=%d\n",
294 error);
295 return error;
298 /* report initial state of the buttons */
299 for (i = 0; i < pdata->nbuttons; i++)
300 gpio_keys_polled_check_state(input, &pdata->buttons[i],
301 &bdev->data[i]);
303 return 0;
306 static struct platform_driver gpio_keys_polled_driver = {
307 .probe = gpio_keys_polled_probe,
308 .driver = {
309 .name = DRV_NAME,
310 .owner = THIS_MODULE,
311 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
314 module_platform_driver(gpio_keys_polled_driver);
316 MODULE_LICENSE("GPL v2");
317 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
318 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
319 MODULE_ALIAS("platform:" DRV_NAME);