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/consumer.h>
27 #include <linux/gpio_keys.h>
28 #include <linux/property.h>
29 
30 #define DRV_NAME	"gpio-keys-polled"
31 
32 struct gpio_keys_button_data {
33 	int last_state;
34 	int count;
35 	int threshold;
36 	int can_sleep;
37 };
38 
39 struct gpio_keys_polled_dev {
40 	struct input_polled_dev *poll_dev;
41 	struct device *dev;
42 	const struct gpio_keys_platform_data *pdata;
43 	struct gpio_keys_button_data data[0];
44 };
45 
46 static void gpio_keys_polled_check_state(struct input_dev *input,
47 					 struct gpio_keys_button *button,
48 					 struct gpio_keys_button_data *bdata)
49 {
50 	int state;
51 
52 	if (bdata->can_sleep)
53 		state = !!gpiod_get_value_cansleep(button->gpiod);
54 	else
55 		state = !!gpiod_get_value(button->gpiod);
56 
57 	if (state != bdata->last_state) {
58 		unsigned int type = button->type ?: EV_KEY;
59 
60 		input_event(input, type, button->code, state);
61 		input_sync(input);
62 		bdata->count = 0;
63 		bdata->last_state = state;
64 	}
65 }
66 
67 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
68 {
69 	struct gpio_keys_polled_dev *bdev = dev->private;
70 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
71 	struct input_dev *input = dev->input;
72 	int i;
73 
74 	for (i = 0; i < pdata->nbuttons; i++) {
75 		struct gpio_keys_button_data *bdata = &bdev->data[i];
76 
77 		if (bdata->count < bdata->threshold)
78 			bdata->count++;
79 		else
80 			gpio_keys_polled_check_state(input, &pdata->buttons[i],
81 						     bdata);
82 	}
83 }
84 
85 static void gpio_keys_polled_open(struct input_polled_dev *dev)
86 {
87 	struct gpio_keys_polled_dev *bdev = dev->private;
88 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
89 
90 	if (pdata->enable)
91 		pdata->enable(bdev->dev);
92 }
93 
94 static void gpio_keys_polled_close(struct input_polled_dev *dev)
95 {
96 	struct gpio_keys_polled_dev *bdev = dev->private;
97 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
98 
99 	if (pdata->disable)
100 		pdata->disable(bdev->dev);
101 }
102 
103 static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct device *dev)
104 {
105 	struct gpio_keys_platform_data *pdata;
106 	struct gpio_keys_button *button;
107 	struct fwnode_handle *child;
108 	int error;
109 	int nbuttons;
110 
111 	nbuttons = device_get_child_node_count(dev);
112 	if (nbuttons == 0)
113 		return NULL;
114 
115 	pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
116 			     GFP_KERNEL);
117 	if (!pdata)
118 		return ERR_PTR(-ENOMEM);
119 
120 	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
121 
122 	pdata->rep = device_property_present(dev, "autorepeat");
123 	device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
124 
125 	device_for_each_child_node(dev, child) {
126 		struct gpio_desc *desc;
127 
128 		desc = devm_get_gpiod_from_child(dev, NULL, child);
129 		if (IS_ERR(desc)) {
130 			error = PTR_ERR(desc);
131 			if (error != -EPROBE_DEFER)
132 				dev_err(dev,
133 					"Failed to get gpio flags, error: %d\n",
134 					error);
135 			fwnode_handle_put(child);
136 			return ERR_PTR(error);
137 		}
138 
139 		button = &pdata->buttons[pdata->nbuttons++];
140 		button->gpiod = desc;
141 
142 		if (fwnode_property_read_u32(child, "linux,code", &button->code)) {
143 			dev_err(dev, "Button without keycode: %d\n",
144 				pdata->nbuttons - 1);
145 			fwnode_handle_put(child);
146 			return ERR_PTR(-EINVAL);
147 		}
148 
149 		fwnode_property_read_string(child, "label", &button->desc);
150 
151 		if (fwnode_property_read_u32(child, "linux,input-type",
152 					     &button->type))
153 			button->type = EV_KEY;
154 
155 		button->wakeup =
156 			fwnode_property_read_bool(child, "wakeup-source") ||
157 			/* legacy name */
158 			fwnode_property_read_bool(child, "gpio-key,wakeup");
159 
160 		if (fwnode_property_read_u32(child, "debounce-interval",
161 					     &button->debounce_interval))
162 			button->debounce_interval = 5;
163 	}
164 
165 	if (pdata->nbuttons == 0)
166 		return ERR_PTR(-EINVAL);
167 
168 	return pdata;
169 }
170 
171 static const struct of_device_id gpio_keys_polled_of_match[] = {
172 	{ .compatible = "gpio-keys-polled", },
173 	{ },
174 };
175 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
176 
177 static int gpio_keys_polled_probe(struct platform_device *pdev)
178 {
179 	struct device *dev = &pdev->dev;
180 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
181 	struct gpio_keys_polled_dev *bdev;
182 	struct input_polled_dev *poll_dev;
183 	struct input_dev *input;
184 	size_t size;
185 	int error;
186 	int i;
187 
188 	if (!pdata) {
189 		pdata = gpio_keys_polled_get_devtree_pdata(dev);
190 		if (IS_ERR(pdata))
191 			return PTR_ERR(pdata);
192 		if (!pdata) {
193 			dev_err(dev, "missing platform data\n");
194 			return -EINVAL;
195 		}
196 	}
197 
198 	if (!pdata->poll_interval) {
199 		dev_err(dev, "missing poll_interval value\n");
200 		return -EINVAL;
201 	}
202 
203 	size = sizeof(struct gpio_keys_polled_dev) +
204 			pdata->nbuttons * sizeof(struct gpio_keys_button_data);
205 	bdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
206 	if (!bdev) {
207 		dev_err(dev, "no memory for private data\n");
208 		return -ENOMEM;
209 	}
210 
211 	poll_dev = devm_input_allocate_polled_device(&pdev->dev);
212 	if (!poll_dev) {
213 		dev_err(dev, "no memory for polled device\n");
214 		return -ENOMEM;
215 	}
216 
217 	poll_dev->private = bdev;
218 	poll_dev->poll = gpio_keys_polled_poll;
219 	poll_dev->poll_interval = pdata->poll_interval;
220 	poll_dev->open = gpio_keys_polled_open;
221 	poll_dev->close = gpio_keys_polled_close;
222 
223 	input = poll_dev->input;
224 
225 	input->name = pdev->name;
226 	input->phys = DRV_NAME"/input0";
227 
228 	input->id.bustype = BUS_HOST;
229 	input->id.vendor = 0x0001;
230 	input->id.product = 0x0001;
231 	input->id.version = 0x0100;
232 
233 	__set_bit(EV_KEY, input->evbit);
234 	if (pdata->rep)
235 		__set_bit(EV_REP, input->evbit);
236 
237 	for (i = 0; i < pdata->nbuttons; i++) {
238 		struct gpio_keys_button *button = &pdata->buttons[i];
239 		struct gpio_keys_button_data *bdata = &bdev->data[i];
240 		unsigned int type = button->type ?: EV_KEY;
241 
242 		if (button->wakeup) {
243 			dev_err(dev, DRV_NAME " does not support wakeup\n");
244 			return -EINVAL;
245 		}
246 
247 		/*
248 		 * Legacy GPIO number so request the GPIO here and
249 		 * convert it to descriptor.
250 		 */
251 		if (!button->gpiod && gpio_is_valid(button->gpio)) {
252 			unsigned flags = GPIOF_IN;
253 
254 			if (button->active_low)
255 				flags |= GPIOF_ACTIVE_LOW;
256 
257 			error = devm_gpio_request_one(&pdev->dev, button->gpio,
258 					flags, button->desc ? : DRV_NAME);
259 			if (error) {
260 				dev_err(dev, "unable to claim gpio %u, err=%d\n",
261 					button->gpio, error);
262 				return error;
263 			}
264 
265 			button->gpiod = gpio_to_desc(button->gpio);
266 		}
267 
268 		if (IS_ERR(button->gpiod))
269 			return PTR_ERR(button->gpiod);
270 
271 		bdata->can_sleep = gpiod_cansleep(button->gpiod);
272 		bdata->last_state = -1;
273 		bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
274 						pdata->poll_interval);
275 
276 		input_set_capability(input, type, button->code);
277 	}
278 
279 	bdev->poll_dev = poll_dev;
280 	bdev->dev = dev;
281 	bdev->pdata = pdata;
282 	platform_set_drvdata(pdev, bdev);
283 
284 	error = input_register_polled_device(poll_dev);
285 	if (error) {
286 		dev_err(dev, "unable to register polled device, err=%d\n",
287 			error);
288 		return error;
289 	}
290 
291 	/* report initial state of the buttons */
292 	for (i = 0; i < pdata->nbuttons; i++)
293 		gpio_keys_polled_check_state(input, &pdata->buttons[i],
294 					     &bdev->data[i]);
295 
296 	return 0;
297 }
298 
299 static struct platform_driver gpio_keys_polled_driver = {
300 	.probe	= gpio_keys_polled_probe,
301 	.driver	= {
302 		.name	= DRV_NAME,
303 		.of_match_table = gpio_keys_polled_of_match,
304 	},
305 };
306 module_platform_driver(gpio_keys_polled_driver);
307 
308 MODULE_LICENSE("GPL v2");
309 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
310 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
311 MODULE_ALIAS("platform:" DRV_NAME);
312