1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Driver for buttons on GPIO lines not capable of generating interrupts
4  *
5  *  Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
6  *  Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
7  *
8  *  This file was based on: /drivers/input/misc/cobalt_btns.c
9  *	Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
10  *
11  *  also was based on: /drivers/input/keyboard/gpio_keys.c
12  *	Copyright 2005 Phil Blundell
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/input.h>
19 #include <linux/input-polldev.h>
20 #include <linux/ioport.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/gpio_keys.h>
25 #include <linux/property.h>
26 
27 #define DRV_NAME	"gpio-keys-polled"
28 
29 struct gpio_keys_button_data {
30 	struct gpio_desc *gpiod;
31 	int last_state;
32 	int count;
33 	int threshold;
34 };
35 
36 struct gpio_keys_polled_dev {
37 	struct input_polled_dev *poll_dev;
38 	struct device *dev;
39 	const struct gpio_keys_platform_data *pdata;
40 	unsigned long rel_axis_seen[BITS_TO_LONGS(REL_CNT)];
41 	unsigned long abs_axis_seen[BITS_TO_LONGS(ABS_CNT)];
42 	struct gpio_keys_button_data data[0];
43 };
44 
45 static void gpio_keys_button_event(struct input_polled_dev *dev,
46 				   const struct gpio_keys_button *button,
47 				   int state)
48 {
49 	struct gpio_keys_polled_dev *bdev = dev->private;
50 	struct input_dev *input = dev->input;
51 	unsigned int type = button->type ?: EV_KEY;
52 
53 	if (type == EV_REL) {
54 		if (state) {
55 			input_event(input, type, button->code, button->value);
56 			__set_bit(button->code, bdev->rel_axis_seen);
57 		}
58 	} else if (type == EV_ABS) {
59 		if (state) {
60 			input_event(input, type, button->code, button->value);
61 			__set_bit(button->code, bdev->abs_axis_seen);
62 		}
63 	} else {
64 		input_event(input, type, button->code, state);
65 		input_sync(input);
66 	}
67 }
68 
69 static void gpio_keys_polled_check_state(struct input_polled_dev *dev,
70 					 const struct gpio_keys_button *button,
71 					 struct gpio_keys_button_data *bdata)
72 {
73 	int state;
74 
75 	state = gpiod_get_value_cansleep(bdata->gpiod);
76 	if (state < 0) {
77 		dev_err(dev->input->dev.parent,
78 			"failed to get gpio state: %d\n", state);
79 	} else {
80 		gpio_keys_button_event(dev, button, state);
81 
82 		if (state != bdata->last_state) {
83 			bdata->count = 0;
84 			bdata->last_state = state;
85 		}
86 	}
87 }
88 
89 static void gpio_keys_polled_poll(struct input_polled_dev *dev)
90 {
91 	struct gpio_keys_polled_dev *bdev = dev->private;
92 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
93 	struct input_dev *input = dev->input;
94 	int i;
95 
96 	memset(bdev->rel_axis_seen, 0, sizeof(bdev->rel_axis_seen));
97 	memset(bdev->abs_axis_seen, 0, sizeof(bdev->abs_axis_seen));
98 
99 	for (i = 0; i < pdata->nbuttons; i++) {
100 		struct gpio_keys_button_data *bdata = &bdev->data[i];
101 
102 		if (bdata->count < bdata->threshold) {
103 			bdata->count++;
104 			gpio_keys_button_event(dev, &pdata->buttons[i],
105 					       bdata->last_state);
106 		} else {
107 			gpio_keys_polled_check_state(dev, &pdata->buttons[i],
108 						     bdata);
109 		}
110 	}
111 
112 	for_each_set_bit(i, input->relbit, REL_CNT) {
113 		if (!test_bit(i, bdev->rel_axis_seen))
114 			input_event(input, EV_REL, i, 0);
115 	}
116 
117 	for_each_set_bit(i, input->absbit, ABS_CNT) {
118 		if (!test_bit(i, bdev->abs_axis_seen))
119 			input_event(input, EV_ABS, i, 0);
120 	}
121 
122 	input_sync(input);
123 }
124 
125 static void gpio_keys_polled_open(struct input_polled_dev *dev)
126 {
127 	struct gpio_keys_polled_dev *bdev = dev->private;
128 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
129 
130 	if (pdata->enable)
131 		pdata->enable(bdev->dev);
132 }
133 
134 static void gpio_keys_polled_close(struct input_polled_dev *dev)
135 {
136 	struct gpio_keys_polled_dev *bdev = dev->private;
137 	const struct gpio_keys_platform_data *pdata = bdev->pdata;
138 
139 	if (pdata->disable)
140 		pdata->disable(bdev->dev);
141 }
142 
143 static struct gpio_keys_platform_data *
144 gpio_keys_polled_get_devtree_pdata(struct device *dev)
145 {
146 	struct gpio_keys_platform_data *pdata;
147 	struct gpio_keys_button *button;
148 	struct fwnode_handle *child;
149 	int nbuttons;
150 
151 	nbuttons = device_get_child_node_count(dev);
152 	if (nbuttons == 0)
153 		return ERR_PTR(-EINVAL);
154 
155 	pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
156 			     GFP_KERNEL);
157 	if (!pdata)
158 		return ERR_PTR(-ENOMEM);
159 
160 	button = (struct gpio_keys_button *)(pdata + 1);
161 
162 	pdata->buttons = button;
163 	pdata->nbuttons = nbuttons;
164 
165 	pdata->rep = device_property_present(dev, "autorepeat");
166 	device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
167 
168 	device_property_read_string(dev, "label", &pdata->name);
169 
170 	device_for_each_child_node(dev, child) {
171 		if (fwnode_property_read_u32(child, "linux,code",
172 					     &button->code)) {
173 			dev_err(dev, "button without keycode\n");
174 			fwnode_handle_put(child);
175 			return ERR_PTR(-EINVAL);
176 		}
177 
178 		fwnode_property_read_string(child, "label", &button->desc);
179 
180 		if (fwnode_property_read_u32(child, "linux,input-type",
181 					     &button->type))
182 			button->type = EV_KEY;
183 
184 		if (fwnode_property_read_u32(child, "linux,input-value",
185 					     (u32 *)&button->value))
186 			button->value = 1;
187 
188 		button->wakeup =
189 			fwnode_property_read_bool(child, "wakeup-source") ||
190 			/* legacy name */
191 			fwnode_property_read_bool(child, "gpio-key,wakeup");
192 
193 		if (fwnode_property_read_u32(child, "debounce-interval",
194 					     &button->debounce_interval))
195 			button->debounce_interval = 5;
196 
197 		button++;
198 	}
199 
200 	return pdata;
201 }
202 
203 static void gpio_keys_polled_set_abs_params(struct input_dev *input,
204 	const struct gpio_keys_platform_data *pdata, unsigned int code)
205 {
206 	int i, min = 0, max = 0;
207 
208 	for (i = 0; i < pdata->nbuttons; i++) {
209 		const struct gpio_keys_button *button = &pdata->buttons[i];
210 
211 		if (button->type != EV_ABS || button->code != code)
212 			continue;
213 
214 		if (button->value < min)
215 			min = button->value;
216 		if (button->value > max)
217 			max = button->value;
218 	}
219 
220 	input_set_abs_params(input, code, min, max, 0, 0);
221 }
222 
223 static const struct of_device_id gpio_keys_polled_of_match[] = {
224 	{ .compatible = "gpio-keys-polled", },
225 	{ },
226 };
227 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
228 
229 static int gpio_keys_polled_probe(struct platform_device *pdev)
230 {
231 	struct device *dev = &pdev->dev;
232 	struct fwnode_handle *child = NULL;
233 	const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
234 	struct gpio_keys_polled_dev *bdev;
235 	struct input_polled_dev *poll_dev;
236 	struct input_dev *input;
237 	int error;
238 	int i;
239 
240 	if (!pdata) {
241 		pdata = gpio_keys_polled_get_devtree_pdata(dev);
242 		if (IS_ERR(pdata))
243 			return PTR_ERR(pdata);
244 	}
245 
246 	if (!pdata->poll_interval) {
247 		dev_err(dev, "missing poll_interval value\n");
248 		return -EINVAL;
249 	}
250 
251 	bdev = devm_kzalloc(dev, struct_size(bdev, data, pdata->nbuttons),
252 			    GFP_KERNEL);
253 	if (!bdev) {
254 		dev_err(dev, "no memory for private data\n");
255 		return -ENOMEM;
256 	}
257 
258 	poll_dev = devm_input_allocate_polled_device(dev);
259 	if (!poll_dev) {
260 		dev_err(dev, "no memory for polled device\n");
261 		return -ENOMEM;
262 	}
263 
264 	poll_dev->private = bdev;
265 	poll_dev->poll = gpio_keys_polled_poll;
266 	poll_dev->poll_interval = pdata->poll_interval;
267 	poll_dev->open = gpio_keys_polled_open;
268 	poll_dev->close = gpio_keys_polled_close;
269 
270 	input = poll_dev->input;
271 
272 	input->name = pdata->name ?: pdev->name;
273 	input->phys = DRV_NAME"/input0";
274 
275 	input->id.bustype = BUS_HOST;
276 	input->id.vendor = 0x0001;
277 	input->id.product = 0x0001;
278 	input->id.version = 0x0100;
279 
280 	__set_bit(EV_KEY, input->evbit);
281 	if (pdata->rep)
282 		__set_bit(EV_REP, input->evbit);
283 
284 	for (i = 0; i < pdata->nbuttons; i++) {
285 		const struct gpio_keys_button *button = &pdata->buttons[i];
286 		struct gpio_keys_button_data *bdata = &bdev->data[i];
287 		unsigned int type = button->type ?: EV_KEY;
288 
289 		if (button->wakeup) {
290 			dev_err(dev, DRV_NAME " does not support wakeup\n");
291 			fwnode_handle_put(child);
292 			return -EINVAL;
293 		}
294 
295 		if (!dev_get_platdata(dev)) {
296 			/* No legacy static platform data */
297 			child = device_get_next_child_node(dev, child);
298 			if (!child) {
299 				dev_err(dev, "missing child device node\n");
300 				return -EINVAL;
301 			}
302 
303 			bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev,
304 								NULL, child,
305 								GPIOD_IN,
306 								button->desc);
307 			if (IS_ERR(bdata->gpiod)) {
308 				error = PTR_ERR(bdata->gpiod);
309 				if (error != -EPROBE_DEFER)
310 					dev_err(dev,
311 						"failed to get gpio: %d\n",
312 						error);
313 				fwnode_handle_put(child);
314 				return error;
315 			}
316 		} else if (gpio_is_valid(button->gpio)) {
317 			/*
318 			 * Legacy GPIO number so request the GPIO here and
319 			 * convert it to descriptor.
320 			 */
321 			unsigned flags = GPIOF_IN;
322 
323 			if (button->active_low)
324 				flags |= GPIOF_ACTIVE_LOW;
325 
326 			error = devm_gpio_request_one(dev, button->gpio,
327 					flags, button->desc ? : DRV_NAME);
328 			if (error) {
329 				dev_err(dev,
330 					"unable to claim gpio %u, err=%d\n",
331 					button->gpio, error);
332 				return error;
333 			}
334 
335 			bdata->gpiod = gpio_to_desc(button->gpio);
336 			if (!bdata->gpiod) {
337 				dev_err(dev,
338 					"unable to convert gpio %u to descriptor\n",
339 					button->gpio);
340 				return -EINVAL;
341 			}
342 		}
343 
344 		bdata->last_state = -1;
345 		bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
346 						pdata->poll_interval);
347 
348 		input_set_capability(input, type, button->code);
349 		if (type == EV_ABS)
350 			gpio_keys_polled_set_abs_params(input, pdata,
351 							button->code);
352 	}
353 
354 	fwnode_handle_put(child);
355 
356 	bdev->poll_dev = poll_dev;
357 	bdev->dev = dev;
358 	bdev->pdata = pdata;
359 
360 	error = input_register_polled_device(poll_dev);
361 	if (error) {
362 		dev_err(dev, "unable to register polled device, err=%d\n",
363 			error);
364 		return error;
365 	}
366 
367 	/* report initial state of the buttons */
368 	for (i = 0; i < pdata->nbuttons; i++)
369 		gpio_keys_polled_check_state(poll_dev, &pdata->buttons[i],
370 					     &bdev->data[i]);
371 
372 	input_sync(input);
373 
374 	return 0;
375 }
376 
377 static struct platform_driver gpio_keys_polled_driver = {
378 	.probe	= gpio_keys_polled_probe,
379 	.driver	= {
380 		.name	= DRV_NAME,
381 		.of_match_table = gpio_keys_polled_of_match,
382 	},
383 };
384 module_platform_driver(gpio_keys_polled_driver);
385 
386 MODULE_LICENSE("GPL v2");
387 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
388 MODULE_DESCRIPTION("Polled GPIO Buttons driver");
389 MODULE_ALIAS("platform:" DRV_NAME);
390