xref: /linux/drivers/input/keyboard/gpio_keys_polled.c (revision b889fcf63cb62e7fdb7816565e28f44dbe4a76a5)
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 #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>
28 #include <linux/of_platform.h>
29 #include <linux/of_gpio.h>
30 
31 #define DRV_NAME	"gpio-keys-polled"
32 
33 struct gpio_keys_button_data {
34 	int last_state;
35 	int count;
36 	int threshold;
37 	int can_sleep;
38 };
39 
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];
45 };
46 
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)
50 {
51 	int state;
52 
53 	if (bdata->can_sleep)
54 		state = !!gpio_get_value_cansleep(button->gpio);
55 	else
56 		state = !!gpio_get_value(button->gpio);
57 
58 	if (state != bdata->last_state) {
59 		unsigned int type = button->type ?: EV_KEY;
60 
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;
66 	}
67 }
68 
69 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
70 {
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;
75 
76 	for (i = 0; i < pdata->nbuttons; i++) {
77 		struct gpio_keys_button_data *bdata = &bdev->data[i];
78 
79 		if (bdata->count < bdata->threshold)
80 			bdata->count++;
81 		else
82 			gpio_keys_polled_check_state(input, &pdata->buttons[i],
83 						     bdata);
84 	}
85 }
86 
87 static void gpio_keys_polled_open(struct input_polled_dev *dev)
88 {
89 	struct gpio_keys_polled_dev *bdev = dev->private;
90 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
91 
92 	if (pdata->enable)
93 		pdata->enable(bdev->dev);
94 }
95 
96 static void gpio_keys_polled_close(struct input_polled_dev *dev)
97 {
98 	struct gpio_keys_polled_dev *bdev = dev->private;
99 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
100 
101 	if (pdata->disable)
102 		pdata->disable(bdev->dev);
103 }
104 
105 #ifdef CONFIG_OF
106 static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct device *dev)
107 {
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;
114 
115 	node = dev->of_node;
116 	if (!node)
117 		return NULL;
118 
119 	nbuttons = of_get_child_count(node);
120 	if (nbuttons == 0)
121 		return NULL;
122 
123 	pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
124 			GFP_KERNEL);
125 	if (!pdata) {
126 		error = -ENOMEM;
127 		goto err_out;
128 	}
129 
130 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
131 	pdata->nbuttons = nbuttons;
132 
133 	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
134 	of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
135 
136 	i = 0;
137 	for_each_child_of_node(node, pp) {
138 		enum of_gpio_flags flags;
139 
140 		if (!of_find_property(pp, "gpios", NULL)) {
141 			pdata->nbuttons--;
142 			dev_warn(dev, "Found button without gpios\n");
143 			continue;
144 		}
145 
146 		button = &pdata->buttons[i++];
147 
148 		button->gpio = of_get_gpio_flags(pp, 0, &flags);
149 		button->active_low = flags & OF_GPIO_ACTIVE_LOW;
150 
151 		if (of_property_read_u32(pp, "linux,code", &button->code)) {
152 			dev_err(dev, "Button without keycode: 0x%x\n",
153 				button->gpio);
154 			error = -EINVAL;
155 			goto err_free_pdata;
156 		}
157 
158 		button->desc = of_get_property(pp, "label", NULL);
159 
160 		if (of_property_read_u32(pp, "linux,input-type", &button->type))
161 			button->type = EV_KEY;
162 
163 		button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
164 
165 		if (of_property_read_u32(pp, "debounce-interval",
166 					 &button->debounce_interval))
167 			button->debounce_interval = 5;
168 	}
169 
170 	if (pdata->nbuttons == 0) {
171 		error = -EINVAL;
172 		goto err_free_pdata;
173 	}
174 
175 	return pdata;
176 
177 err_free_pdata:
178 	kfree(pdata);
179 err_out:
180 	return ERR_PTR(error);
181 }
182 
183 static struct of_device_id gpio_keys_polled_of_match[] = {
184 	{ .compatible = "gpio-keys-polled", },
185 	{ },
186 };
187 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
188 
189 #else
190 
191 static inline struct gpio_keys_platform_data *
192 gpio_keys_polled_get_devtree_pdata(struct device *dev)
193 {
194 	return NULL;
195 }
196 #endif
197 
198 static int gpio_keys_polled_probe(struct platform_device *pdev)
199 {
200 	struct device *dev = &pdev->dev;
201 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
202 	struct gpio_keys_polled_dev *bdev;
203 	struct input_polled_dev *poll_dev;
204 	struct input_dev *input;
205 	int error;
206 	int i;
207 
208 	if (!pdata) {
209 		pdata = gpio_keys_polled_get_devtree_pdata(dev);
210 		if (IS_ERR(pdata))
211 			return PTR_ERR(pdata);
212 		if (!pdata) {
213 			dev_err(dev, "missing platform data\n");
214 			return -EINVAL;
215 		}
216 	}
217 
218 	if (!pdata->poll_interval) {
219 		dev_err(dev, "missing poll_interval value\n");
220 		error = -EINVAL;
221 		goto err_free_pdata;
222 	}
223 
224 	bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) +
225 		       pdata->nbuttons * sizeof(struct gpio_keys_button_data),
226 		       GFP_KERNEL);
227 	if (!bdev) {
228 		dev_err(dev, "no memory for private data\n");
229 		error = -ENOMEM;
230 		goto err_free_pdata;
231 	}
232 
233 	poll_dev = input_allocate_polled_device();
234 	if (!poll_dev) {
235 		dev_err(dev, "no memory for polled device\n");
236 		error = -ENOMEM;
237 		goto err_free_bdev;
238 	}
239 
240 	poll_dev->private = bdev;
241 	poll_dev->poll = gpio_keys_polled_poll;
242 	poll_dev->poll_interval = pdata->poll_interval;
243 	poll_dev->open = gpio_keys_polled_open;
244 	poll_dev->close = gpio_keys_polled_close;
245 
246 	input = poll_dev->input;
247 
248 	input->name = pdev->name;
249 	input->phys = DRV_NAME"/input0";
250 	input->dev.parent = &pdev->dev;
251 
252 	input->id.bustype = BUS_HOST;
253 	input->id.vendor = 0x0001;
254 	input->id.product = 0x0001;
255 	input->id.version = 0x0100;
256 
257 	__set_bit(EV_KEY, input->evbit);
258 	if (pdata->rep)
259 		__set_bit(EV_REP, input->evbit);
260 
261 	for (i = 0; i < pdata->nbuttons; i++) {
262 		struct gpio_keys_button *button = &pdata->buttons[i];
263 		struct gpio_keys_button_data *bdata = &bdev->data[i];
264 		unsigned int gpio = button->gpio;
265 		unsigned int type = button->type ?: EV_KEY;
266 
267 		if (button->wakeup) {
268 			dev_err(dev, DRV_NAME " does not support wakeup\n");
269 			error = -EINVAL;
270 			goto err_free_gpio;
271 		}
272 
273 		error = gpio_request_one(gpio, GPIOF_IN,
274 					 button->desc ?: DRV_NAME);
275 		if (error) {
276 			dev_err(dev, "unable to claim gpio %u, err=%d\n",
277 				gpio, error);
278 			goto err_free_gpio;
279 		}
280 
281 		bdata->can_sleep = gpio_cansleep(gpio);
282 		bdata->last_state = -1;
283 		bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
284 						pdata->poll_interval);
285 
286 		input_set_capability(input, type, button->code);
287 	}
288 
289 	bdev->poll_dev = poll_dev;
290 	bdev->dev = dev;
291 	bdev->pdata = pdata;
292 	platform_set_drvdata(pdev, bdev);
293 
294 	error = input_register_polled_device(poll_dev);
295 	if (error) {
296 		dev_err(dev, "unable to register polled device, err=%d\n",
297 			error);
298 		goto err_free_gpio;
299 	}
300 
301 	/* report initial state of the buttons */
302 	for (i = 0; i < pdata->nbuttons; i++)
303 		gpio_keys_polled_check_state(input, &pdata->buttons[i],
304 					     &bdev->data[i]);
305 
306 	return 0;
307 
308 err_free_gpio:
309 	while (--i >= 0)
310 		gpio_free(pdata->buttons[i].gpio);
311 
312 	input_free_polled_device(poll_dev);
313 
314 err_free_bdev:
315 	kfree(bdev);
316 	platform_set_drvdata(pdev, NULL);
317 
318 err_free_pdata:
319 	/* If we have no platform_data, we allocated pdata dynamically.  */
320 	if (!dev_get_platdata(&pdev->dev))
321 		kfree(pdata);
322 
323 	return error;
324 }
325 
326 static int gpio_keys_polled_remove(struct platform_device *pdev)
327 {
328 	struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
329 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
330 	int i;
331 
332 	input_unregister_polled_device(bdev->poll_dev);
333 
334 	for (i = 0; i < pdata->nbuttons; i++)
335 		gpio_free(pdata->buttons[i].gpio);
336 
337 	input_free_polled_device(bdev->poll_dev);
338 
339 	/*
340 	 * If we had no platform_data, we allocated pdata dynamically and
341 	 * must free it here.
342 	 */
343 	if (!dev_get_platdata(&pdev->dev))
344 		kfree(pdata);
345 
346 	kfree(bdev);
347 	platform_set_drvdata(pdev, NULL);
348 
349 	return 0;
350 }
351 
352 static struct platform_driver gpio_keys_polled_driver = {
353 	.probe	= gpio_keys_polled_probe,
354 	.remove	= gpio_keys_polled_remove,
355 	.driver	= {
356 		.name	= DRV_NAME,
357 		.owner	= THIS_MODULE,
358 		.of_match_table = of_match_ptr(gpio_keys_polled_of_match),
359 	},
360 };
361 module_platform_driver(gpio_keys_polled_driver);
362 
363 MODULE_LICENSE("GPL v2");
364 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
365 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
366 MODULE_ALIAS("platform:" DRV_NAME);
367