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