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/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>
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 = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
124 			     GFP_KERNEL);
125 	if (!pdata)
126 		return ERR_PTR(-ENOMEM);
127 
128 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
129 	pdata->nbuttons = nbuttons;
130 
131 	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
132 	of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
133 
134 	i = 0;
135 	for_each_child_of_node(node, pp) {
136 		int gpio;
137 		enum of_gpio_flags flags;
138 
139 		if (!of_find_property(pp, "gpios", NULL)) {
140 			pdata->nbuttons--;
141 			dev_warn(dev, "Found button without gpios\n");
142 			continue;
143 		}
144 
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);
153 		}
154 
155 		button = &pdata->buttons[i++];
156 
157 		button->gpio = gpio;
158 		button->active_low = flags & OF_GPIO_ACTIVE_LOW;
159 
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);
164 		}
165 
166 		button->desc = of_get_property(pp, "label", NULL);
167 
168 		if (of_property_read_u32(pp, "linux,input-type", &button->type))
169 			button->type = EV_KEY;
170 
171 		button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
172 
173 		if (of_property_read_u32(pp, "debounce-interval",
174 					 &button->debounce_interval))
175 			button->debounce_interval = 5;
176 	}
177 
178 	if (pdata->nbuttons == 0)
179 		return ERR_PTR(-EINVAL);
180 
181 	return pdata;
182 }
183 
184 static const struct of_device_id gpio_keys_polled_of_match[] = {
185 	{ .compatible = "gpio-keys-polled", },
186 	{ },
187 };
188 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
189 
190 #else
191 
192 static inline struct gpio_keys_platform_data *
193 gpio_keys_polled_get_devtree_pdata(struct device *dev)
194 {
195 	return NULL;
196 }
197 #endif
198 
199 static int gpio_keys_polled_probe(struct platform_device *pdev)
200 {
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;
209 
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;
217 		}
218 	}
219 
220 	if (!pdata->poll_interval) {
221 		dev_err(dev, "missing poll_interval value\n");
222 		return -EINVAL;
223 	}
224 
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;
231 	}
232 
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;
237 	}
238 
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;
244 
245 	input = poll_dev->input;
246 
247 	input->name = pdev->name;
248 	input->phys = DRV_NAME"/input0";
249 
250 	input->id.bustype = BUS_HOST;
251 	input->id.vendor = 0x0001;
252 	input->id.product = 0x0001;
253 	input->id.version = 0x0100;
254 
255 	__set_bit(EV_KEY, input->evbit);
256 	if (pdata->rep)
257 		__set_bit(EV_REP, input->evbit);
258 
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;
264 
265 		if (button->wakeup) {
266 			dev_err(dev, DRV_NAME " does not support wakeup\n");
267 			return -EINVAL;
268 		}
269 
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;
276 		}
277 
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);
282 
283 		input_set_capability(input, type, button->code);
284 	}
285 
286 	bdev->poll_dev = poll_dev;
287 	bdev->dev = dev;
288 	bdev->pdata = pdata;
289 	platform_set_drvdata(pdev, bdev);
290 
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;
296 	}
297 
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]);
302 
303 	return 0;
304 }
305 
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),
312 	},
313 };
314 module_platform_driver(gpio_keys_polled_driver);
315 
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);
320