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