blob: edc7262103b9ac982f1dee130723849a5d6a275f [file] [log] [blame]
Gabor Juhos0e7d0c82010-12-06 17:14:47 -08001/*
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>
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080020#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>
Aaron Lu633a21d2014-10-21 23:30:25 +020026#include <linux/gpio/consumer.h>
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080027#include <linux/gpio_keys.h>
Aaron Lub26d4e22014-10-21 13:34:00 +020028#include <linux/property.h>
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080029
30#define DRV_NAME "gpio-keys-polled"
31
32struct gpio_keys_button_data {
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -080033 struct gpio_desc *gpiod;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080034 int last_state;
35 int count;
36 int threshold;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080037};
38
39struct gpio_keys_polled_dev {
40 struct input_polled_dev *poll_dev;
41 struct device *dev;
Dmitry Torokhov2976f242012-07-29 22:48:33 -070042 const struct gpio_keys_platform_data *pdata;
Hans de Goede6f29d3b2015-10-14 16:44:35 -070043 unsigned long rel_axis_seen[BITS_TO_LONGS(REL_CNT)];
44 unsigned long abs_axis_seen[BITS_TO_LONGS(ABS_CNT)];
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080045 struct gpio_keys_button_data data[0];
46};
47
Hans de Goede6f29d3b2015-10-14 16:44:35 -070048static void gpio_keys_button_event(struct input_polled_dev *dev,
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -080049 const struct gpio_keys_button *button,
Hans de Goede6f29d3b2015-10-14 16:44:35 -070050 int state)
51{
52 struct gpio_keys_polled_dev *bdev = dev->private;
53 struct input_dev *input = dev->input;
54 unsigned int type = button->type ?: EV_KEY;
55
56 if (type == EV_REL) {
57 if (state) {
58 input_event(input, type, button->code, button->value);
59 __set_bit(button->code, bdev->rel_axis_seen);
60 }
61 } else if (type == EV_ABS) {
62 if (state) {
63 input_event(input, type, button->code, button->value);
64 __set_bit(button->code, bdev->abs_axis_seen);
65 }
66 } else {
67 input_event(input, type, button->code, state);
68 input_sync(input);
69 }
70}
71
72static void gpio_keys_polled_check_state(struct input_polled_dev *dev,
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -080073 const struct gpio_keys_button *button,
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080074 struct gpio_keys_button_data *bdata)
75{
76 int state;
77
Dmitry Torokhovea6aabf2016-10-19 16:36:19 -070078 state = gpiod_get_value_cansleep(bdata->gpiod);
79 if (state < 0) {
80 dev_err(dev->input->dev.parent,
81 "failed to get gpio state: %d\n", state);
82 } else {
83 gpio_keys_button_event(dev, button, state);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080084
Dmitry Torokhovea6aabf2016-10-19 16:36:19 -070085 if (state != bdata->last_state) {
86 bdata->count = 0;
87 bdata->last_state = state;
88 }
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080089 }
90}
91
92static void gpio_keys_polled_poll(struct input_polled_dev *dev)
93{
94 struct gpio_keys_polled_dev *bdev = dev->private;
Dmitry Torokhov2976f242012-07-29 22:48:33 -070095 const struct gpio_keys_platform_data *pdata = bdev->pdata;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -080096 struct input_dev *input = dev->input;
97 int i;
98
Hans de Goede6f29d3b2015-10-14 16:44:35 -070099 memset(bdev->rel_axis_seen, 0, sizeof(bdev->rel_axis_seen));
100 memset(bdev->abs_axis_seen, 0, sizeof(bdev->abs_axis_seen));
101
Dmitry Torokhov2976f242012-07-29 22:48:33 -0700102 for (i = 0; i < pdata->nbuttons; i++) {
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800103 struct gpio_keys_button_data *bdata = &bdev->data[i];
104
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700105 if (bdata->count < bdata->threshold) {
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800106 bdata->count++;
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700107 gpio_keys_button_event(dev, &pdata->buttons[i],
108 bdata->last_state);
109 } else {
110 gpio_keys_polled_check_state(dev, &pdata->buttons[i],
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800111 bdata);
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700112 }
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800113 }
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700114
115 for_each_set_bit(i, input->relbit, REL_CNT) {
116 if (!test_bit(i, bdev->rel_axis_seen))
117 input_event(input, EV_REL, i, 0);
118 }
119
120 for_each_set_bit(i, input->absbit, ABS_CNT) {
121 if (!test_bit(i, bdev->abs_axis_seen))
122 input_event(input, EV_ABS, i, 0);
123 }
124
125 input_sync(input);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800126}
127
128static void gpio_keys_polled_open(struct input_polled_dev *dev)
129{
130 struct gpio_keys_polled_dev *bdev = dev->private;
Dmitry Torokhov2976f242012-07-29 22:48:33 -0700131 const struct gpio_keys_platform_data *pdata = bdev->pdata;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800132
133 if (pdata->enable)
134 pdata->enable(bdev->dev);
135}
136
137static void gpio_keys_polled_close(struct input_polled_dev *dev)
138{
139 struct gpio_keys_polled_dev *bdev = dev->private;
Dmitry Torokhov2976f242012-07-29 22:48:33 -0700140 const struct gpio_keys_platform_data *pdata = bdev->pdata;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800141
142 if (pdata->disable)
143 pdata->disable(bdev->dev);
144}
145
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800146static struct gpio_keys_platform_data *
147gpio_keys_polled_get_devtree_pdata(struct device *dev)
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700148{
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700149 struct gpio_keys_platform_data *pdata;
150 struct gpio_keys_button *button;
Aaron Lub26d4e22014-10-21 13:34:00 +0200151 struct fwnode_handle *child;
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700152 int nbuttons;
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700153
Aaron Lub26d4e22014-10-21 13:34:00 +0200154 nbuttons = device_get_child_node_count(dev);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700155 if (nbuttons == 0)
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800156 return ERR_PTR(-EINVAL);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700157
Alexander Shiyan68252632014-04-28 10:48:49 -0700158 pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button),
159 GFP_KERNEL);
160 if (!pdata)
161 return ERR_PTR(-ENOMEM);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700162
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800163 button = (struct gpio_keys_button *)(pdata + 1);
164
165 pdata->buttons = button;
166 pdata->nbuttons = nbuttons;
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700167
Aaron Lub26d4e22014-10-21 13:34:00 +0200168 pdata->rep = device_property_present(dev, "autorepeat");
169 device_property_read_u32(dev, "poll-interval", &pdata->poll_interval);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700170
Aaron Lub26d4e22014-10-21 13:34:00 +0200171 device_for_each_child_node(dev, child) {
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800172 if (fwnode_property_read_u32(child, "linux,code",
173 &button->code)) {
174 dev_err(dev, "button without keycode\n");
Aaron Lub26d4e22014-10-21 13:34:00 +0200175 fwnode_handle_put(child);
Alexander Shiyan68252632014-04-28 10:48:49 -0700176 return ERR_PTR(-EINVAL);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700177 }
178
Aaron Lub26d4e22014-10-21 13:34:00 +0200179 fwnode_property_read_string(child, "label", &button->desc);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700180
Aaron Lub26d4e22014-10-21 13:34:00 +0200181 if (fwnode_property_read_u32(child, "linux,input-type",
182 &button->type))
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700183 button->type = EV_KEY;
184
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700185 if (fwnode_property_read_u32(child, "linux,input-value",
186 (u32 *)&button->value))
187 button->value = 1;
188
Dmitry Torokhov99b4ffb2015-07-16 12:10:05 -0700189 button->wakeup =
190 fwnode_property_read_bool(child, "wakeup-source") ||
191 /* legacy name */
192 fwnode_property_read_bool(child, "gpio-key,wakeup");
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700193
Aaron Lub26d4e22014-10-21 13:34:00 +0200194 if (fwnode_property_read_u32(child, "debounce-interval",
195 &button->debounce_interval))
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700196 button->debounce_interval = 5;
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700197
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800198 button++;
199 }
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700200
201 return pdata;
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700202}
203
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700204static void gpio_keys_polled_set_abs_params(struct input_dev *input,
205 const struct gpio_keys_platform_data *pdata, unsigned int code)
206{
207 int i, min = 0, max = 0;
208
209 for (i = 0; i < pdata->nbuttons; i++) {
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800210 const struct gpio_keys_button *button = &pdata->buttons[i];
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700211
212 if (button->type != EV_ABS || button->code != code)
213 continue;
214
215 if (button->value < min)
216 min = button->value;
217 if (button->value > max)
218 max = button->value;
219 }
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800220
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700221 input_set_abs_params(input, code, min, max, 0, 0);
222}
223
Jingoo Han90c98ef2014-05-07 12:58:36 -0700224static const struct of_device_id gpio_keys_polled_of_match[] = {
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700225 { .compatible = "gpio-keys-polled", },
226 { },
227};
228MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
229
Bill Pemberton5298cc42012-11-23 21:38:25 -0800230static int gpio_keys_polled_probe(struct platform_device *pdev)
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800231{
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800232 struct device *dev = &pdev->dev;
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800233 struct fwnode_handle *child = NULL;
Dmitry Torokhov2976f242012-07-29 22:48:33 -0700234 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800235 struct gpio_keys_polled_dev *bdev;
236 struct input_polled_dev *poll_dev;
237 struct input_dev *input;
Alexander Shiyan68252632014-04-28 10:48:49 -0700238 size_t size;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800239 int error;
240 int i;
241
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700242 if (!pdata) {
243 pdata = gpio_keys_polled_get_devtree_pdata(dev);
244 if (IS_ERR(pdata))
245 return PTR_ERR(pdata);
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700246 }
247
248 if (!pdata->poll_interval) {
249 dev_err(dev, "missing poll_interval value\n");
Alexander Shiyan68252632014-04-28 10:48:49 -0700250 return -EINVAL;
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700251 }
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800252
Alexander Shiyan68252632014-04-28 10:48:49 -0700253 size = sizeof(struct gpio_keys_polled_dev) +
254 pdata->nbuttons * sizeof(struct gpio_keys_button_data);
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800255 bdev = devm_kzalloc(dev, size, GFP_KERNEL);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800256 if (!bdev) {
257 dev_err(dev, "no memory for private data\n");
Alexander Shiyan68252632014-04-28 10:48:49 -0700258 return -ENOMEM;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800259 }
260
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800261 poll_dev = devm_input_allocate_polled_device(dev);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800262 if (!poll_dev) {
263 dev_err(dev, "no memory for polled device\n");
Alexander Shiyan68252632014-04-28 10:48:49 -0700264 return -ENOMEM;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800265 }
266
267 poll_dev->private = bdev;
268 poll_dev->poll = gpio_keys_polled_poll;
269 poll_dev->poll_interval = pdata->poll_interval;
270 poll_dev->open = gpio_keys_polled_open;
271 poll_dev->close = gpio_keys_polled_close;
272
273 input = poll_dev->input;
274
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800275 input->name = pdev->name;
276 input->phys = DRV_NAME"/input0";
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800277
278 input->id.bustype = BUS_HOST;
279 input->id.vendor = 0x0001;
280 input->id.product = 0x0001;
281 input->id.version = 0x0100;
282
Alexander Shiyan1a22e162012-11-29 08:57:17 -0800283 __set_bit(EV_KEY, input->evbit);
284 if (pdata->rep)
285 __set_bit(EV_REP, input->evbit);
286
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800287 for (i = 0; i < pdata->nbuttons; i++) {
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800288 const struct gpio_keys_button *button = &pdata->buttons[i];
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800289 struct gpio_keys_button_data *bdata = &bdev->data[i];
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800290 unsigned int type = button->type ?: EV_KEY;
291
292 if (button->wakeup) {
293 dev_err(dev, DRV_NAME " does not support wakeup\n");
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800294 fwnode_handle_put(child);
Alexander Shiyan68252632014-04-28 10:48:49 -0700295 return -EINVAL;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800296 }
297
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800298 if (!dev_get_platdata(dev)) {
299 /* No legacy static platform data */
300 child = device_get_next_child_node(dev, child);
301 if (!child) {
302 dev_err(dev, "missing child device node\n");
303 return -EINVAL;
304 }
305
Boris Brezillon4b094792017-02-02 14:53:10 +0100306 bdata->gpiod = devm_fwnode_get_gpiod_from_child(dev,
307 NULL, child,
308 GPIOD_IN,
309 button->desc);
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800310 if (IS_ERR(bdata->gpiod)) {
311 error = PTR_ERR(bdata->gpiod);
312 if (error != -EPROBE_DEFER)
313 dev_err(dev,
314 "failed to get gpio: %d\n",
315 error);
316 fwnode_handle_put(child);
317 return error;
318 }
319 } else if (gpio_is_valid(button->gpio)) {
320 /*
321 * Legacy GPIO number so request the GPIO here and
322 * convert it to descriptor.
323 */
Vincent Pelletier1ae5ddb2015-08-20 12:00:19 -0700324 unsigned flags = GPIOF_IN;
Aaron Lu633a21d2014-10-21 23:30:25 +0200325
326 if (button->active_low)
327 flags |= GPIOF_ACTIVE_LOW;
328
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800329 error = devm_gpio_request_one(dev, button->gpio,
Aaron Lu633a21d2014-10-21 23:30:25 +0200330 flags, button->desc ? : DRV_NAME);
331 if (error) {
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800332 dev_err(dev,
333 "unable to claim gpio %u, err=%d\n",
Aaron Lu633a21d2014-10-21 23:30:25 +0200334 button->gpio, error);
335 return error;
336 }
337
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800338 bdata->gpiod = gpio_to_desc(button->gpio);
339 if (!bdata->gpiod) {
340 dev_err(dev,
341 "unable to convert gpio %u to descriptor\n",
342 button->gpio);
343 return -EINVAL;
344 }
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800345 }
346
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800347 bdata->last_state = -1;
348 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
349 pdata->poll_interval);
350
351 input_set_capability(input, type, button->code);
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700352 if (type == EV_ABS)
353 gpio_keys_polled_set_abs_params(input, pdata,
354 button->code);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800355 }
356
Dmitry Torokhov0f78ba92016-02-23 15:32:14 -0800357 fwnode_handle_put(child);
358
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800359 bdev->poll_dev = poll_dev;
360 bdev->dev = dev;
361 bdev->pdata = pdata;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800362
363 error = input_register_polled_device(poll_dev);
364 if (error) {
365 dev_err(dev, "unable to register polled device, err=%d\n",
366 error);
Alexander Shiyan68252632014-04-28 10:48:49 -0700367 return error;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800368 }
369
370 /* report initial state of the buttons */
371 for (i = 0; i < pdata->nbuttons; i++)
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700372 gpio_keys_polled_check_state(poll_dev, &pdata->buttons[i],
Alexandre Pereira da Silvaa2f25242012-07-31 22:08:45 -0700373 &bdev->data[i]);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800374
Hans de Goede6f29d3b2015-10-14 16:44:35 -0700375 input_sync(input);
376
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800377 return 0;
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800378}
379
380static struct platform_driver gpio_keys_polled_driver = {
381 .probe = gpio_keys_polled_probe,
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800382 .driver = {
383 .name = DRV_NAME,
Aaron Lub26d4e22014-10-21 13:34:00 +0200384 .of_match_table = gpio_keys_polled_of_match,
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800385 },
386};
JJ Ding5146c842011-11-29 11:08:39 -0800387module_platform_driver(gpio_keys_polled_driver);
Gabor Juhos0e7d0c82010-12-06 17:14:47 -0800388
389MODULE_LICENSE("GPL v2");
390MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
391MODULE_DESCRIPTION("Polled GPIO Buttons driver");
392MODULE_ALIAS("platform:" DRV_NAME);