i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / input / misc / gpio_tilt_polled.c
blob277a0574c199c7b06ae90fe16167aaf84d9c2498
1 /*
2 * Driver for tilt switches connected via GPIO lines
3 * not capable of generating interrupts
5 * Copyright (C) 2011 Heiko Stuebner <heiko@sntech.de>
7 * based on: drivers/input/keyboard/gpio_keys_polled.c
9 * Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
10 * Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.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/input/gpio_tilt.h>
28 #define DRV_NAME "gpio-tilt-polled"
30 struct gpio_tilt_polled_dev {
31 struct input_polled_dev *poll_dev;
32 struct device *dev;
33 const struct gpio_tilt_platform_data *pdata;
35 int last_state;
37 int threshold;
38 int count;
41 static void gpio_tilt_polled_poll(struct input_polled_dev *dev)
43 struct gpio_tilt_polled_dev *tdev = dev->private;
44 const struct gpio_tilt_platform_data *pdata = tdev->pdata;
45 struct input_dev *input = dev->input;
46 struct gpio_tilt_state *tilt_state = NULL;
47 int state, i;
49 if (tdev->count < tdev->threshold) {
50 tdev->count++;
51 } else {
52 state = 0;
53 for (i = 0; i < pdata->nr_gpios; i++)
54 state |= (!!gpio_get_value(pdata->gpios[i].gpio) << i);
56 if (state != tdev->last_state) {
57 for (i = 0; i < pdata->nr_states; i++)
58 if (pdata->states[i].gpios == state)
59 tilt_state = &pdata->states[i];
61 if (tilt_state) {
62 for (i = 0; i < pdata->nr_axes; i++)
63 input_report_abs(input,
64 pdata->axes[i].axis,
65 tilt_state->axes[i]);
67 input_sync(input);
70 tdev->count = 0;
71 tdev->last_state = state;
76 static void gpio_tilt_polled_open(struct input_polled_dev *dev)
78 struct gpio_tilt_polled_dev *tdev = dev->private;
79 const struct gpio_tilt_platform_data *pdata = tdev->pdata;
81 if (pdata->enable)
82 pdata->enable(tdev->dev);
84 /* report initial state of the axes */
85 tdev->last_state = -1;
86 tdev->count = tdev->threshold;
87 gpio_tilt_polled_poll(tdev->poll_dev);
90 static void gpio_tilt_polled_close(struct input_polled_dev *dev)
92 struct gpio_tilt_polled_dev *tdev = dev->private;
93 const struct gpio_tilt_platform_data *pdata = tdev->pdata;
95 if (pdata->disable)
96 pdata->disable(tdev->dev);
99 static int __devinit gpio_tilt_polled_probe(struct platform_device *pdev)
101 const struct gpio_tilt_platform_data *pdata = pdev->dev.platform_data;
102 struct device *dev = &pdev->dev;
103 struct gpio_tilt_polled_dev *tdev;
104 struct input_polled_dev *poll_dev;
105 struct input_dev *input;
106 int error, i;
108 if (!pdata || !pdata->poll_interval)
109 return -EINVAL;
111 tdev = kzalloc(sizeof(struct gpio_tilt_polled_dev), GFP_KERNEL);
112 if (!tdev) {
113 dev_err(dev, "no memory for private data\n");
114 return -ENOMEM;
117 error = gpio_request_array(pdata->gpios, pdata->nr_gpios);
118 if (error) {
119 dev_err(dev,
120 "Could not request tilt GPIOs: %d\n", error);
121 goto err_free_tdev;
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_gpios;
131 poll_dev->private = tdev;
132 poll_dev->poll = gpio_tilt_polled_poll;
133 poll_dev->poll_interval = pdata->poll_interval;
134 poll_dev->open = gpio_tilt_polled_open;
135 poll_dev->close = gpio_tilt_polled_close;
137 input = poll_dev->input;
139 input->name = pdev->name;
140 input->phys = DRV_NAME"/input0";
141 input->dev.parent = &pdev->dev;
143 input->id.bustype = BUS_HOST;
144 input->id.vendor = 0x0001;
145 input->id.product = 0x0001;
146 input->id.version = 0x0100;
148 __set_bit(EV_ABS, input->evbit);
149 for (i = 0; i < pdata->nr_axes; i++)
150 input_set_abs_params(input, pdata->axes[i].axis,
151 pdata->axes[i].min, pdata->axes[i].max,
152 pdata->axes[i].fuzz, pdata->axes[i].flat);
154 tdev->threshold = DIV_ROUND_UP(pdata->debounce_interval,
155 pdata->poll_interval);
157 tdev->poll_dev = poll_dev;
158 tdev->dev = dev;
159 tdev->pdata = pdata;
161 error = input_register_polled_device(poll_dev);
162 if (error) {
163 dev_err(dev, "unable to register polled device, err=%d\n",
164 error);
165 goto err_free_polldev;
168 platform_set_drvdata(pdev, tdev);
170 return 0;
172 err_free_polldev:
173 input_free_polled_device(poll_dev);
174 err_free_gpios:
175 gpio_free_array(pdata->gpios, pdata->nr_gpios);
176 err_free_tdev:
177 kfree(tdev);
179 return error;
182 static int __devexit gpio_tilt_polled_remove(struct platform_device *pdev)
184 struct gpio_tilt_polled_dev *tdev = platform_get_drvdata(pdev);
185 const struct gpio_tilt_platform_data *pdata = tdev->pdata;
187 platform_set_drvdata(pdev, NULL);
189 input_unregister_polled_device(tdev->poll_dev);
190 input_free_polled_device(tdev->poll_dev);
192 gpio_free_array(pdata->gpios, pdata->nr_gpios);
194 kfree(tdev);
196 return 0;
199 static struct platform_driver gpio_tilt_polled_driver = {
200 .probe = gpio_tilt_polled_probe,
201 .remove = __devexit_p(gpio_tilt_polled_remove),
202 .driver = {
203 .name = DRV_NAME,
204 .owner = THIS_MODULE,
208 module_platform_driver(gpio_tilt_polled_driver);
210 MODULE_LICENSE("GPL v2");
211 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
212 MODULE_DESCRIPTION("Polled GPIO tilt driver");
213 MODULE_ALIAS("platform:" DRV_NAME);