Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 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> |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 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> |
Aaron Lu | 633a21d | 2014-10-21 23:30:25 +0200 | [diff] [blame] | 26 | #include <linux/gpio/consumer.h> |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 27 | #include <linux/gpio_keys.h> |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 28 | #include <linux/property.h> |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 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; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 42 | const struct gpio_keys_platform_data *pdata; |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 43 | unsigned long rel_axis_seen[BITS_TO_LONGS(REL_CNT)]; |
| 44 | unsigned long abs_axis_seen[BITS_TO_LONGS(ABS_CNT)]; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 45 | struct gpio_keys_button_data data[0]; |
| 46 | }; |
| 47 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 48 | static void gpio_keys_button_event(struct input_polled_dev *dev, |
| 49 | struct gpio_keys_button *button, |
| 50 | 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 | |
| 72 | static void gpio_keys_polled_check_state(struct input_polled_dev *dev, |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 73 | struct gpio_keys_button *button, |
| 74 | struct gpio_keys_button_data *bdata) |
| 75 | { |
| 76 | int state; |
| 77 | |
| 78 | if (bdata->can_sleep) |
Aaron Lu | 633a21d | 2014-10-21 23:30:25 +0200 | [diff] [blame] | 79 | state = !!gpiod_get_value_cansleep(button->gpiod); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 80 | else |
Aaron Lu | 633a21d | 2014-10-21 23:30:25 +0200 | [diff] [blame] | 81 | state = !!gpiod_get_value(button->gpiod); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 82 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 83 | gpio_keys_button_event(dev, button, state); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 84 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 85 | if (state != bdata->last_state) { |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 86 | bdata->count = 0; |
| 87 | bdata->last_state = state; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void gpio_keys_polled_poll(struct input_polled_dev *dev) |
| 92 | { |
| 93 | struct gpio_keys_polled_dev *bdev = dev->private; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 94 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 95 | struct input_dev *input = dev->input; |
| 96 | int i; |
| 97 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 98 | memset(bdev->rel_axis_seen, 0, sizeof(bdev->rel_axis_seen)); |
| 99 | memset(bdev->abs_axis_seen, 0, sizeof(bdev->abs_axis_seen)); |
| 100 | |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 101 | for (i = 0; i < pdata->nbuttons; i++) { |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 102 | struct gpio_keys_button_data *bdata = &bdev->data[i]; |
| 103 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 104 | if (bdata->count < bdata->threshold) { |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 105 | bdata->count++; |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 106 | gpio_keys_button_event(dev, &pdata->buttons[i], |
| 107 | bdata->last_state); |
| 108 | } else { |
| 109 | gpio_keys_polled_check_state(dev, &pdata->buttons[i], |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 110 | bdata); |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 111 | } |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 112 | } |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 113 | |
| 114 | for_each_set_bit(i, input->relbit, REL_CNT) { |
| 115 | if (!test_bit(i, bdev->rel_axis_seen)) |
| 116 | input_event(input, EV_REL, i, 0); |
| 117 | } |
| 118 | |
| 119 | for_each_set_bit(i, input->absbit, ABS_CNT) { |
| 120 | if (!test_bit(i, bdev->abs_axis_seen)) |
| 121 | input_event(input, EV_ABS, i, 0); |
| 122 | } |
| 123 | |
| 124 | input_sync(input); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void gpio_keys_polled_open(struct input_polled_dev *dev) |
| 128 | { |
| 129 | struct gpio_keys_polled_dev *bdev = dev->private; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 130 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 131 | |
| 132 | if (pdata->enable) |
| 133 | pdata->enable(bdev->dev); |
| 134 | } |
| 135 | |
| 136 | static void gpio_keys_polled_close(struct input_polled_dev *dev) |
| 137 | { |
| 138 | struct gpio_keys_polled_dev *bdev = dev->private; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 139 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 140 | |
| 141 | if (pdata->disable) |
| 142 | pdata->disable(bdev->dev); |
| 143 | } |
| 144 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 145 | static struct gpio_keys_platform_data *gpio_keys_polled_get_devtree_pdata(struct device *dev) |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 146 | { |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 147 | struct gpio_keys_platform_data *pdata; |
| 148 | struct gpio_keys_button *button; |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 149 | struct fwnode_handle *child; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 150 | int error; |
| 151 | int nbuttons; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 152 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 153 | nbuttons = device_get_child_node_count(dev); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 154 | if (nbuttons == 0) |
| 155 | return NULL; |
| 156 | |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 157 | pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * sizeof(*button), |
| 158 | GFP_KERNEL); |
| 159 | if (!pdata) |
| 160 | return ERR_PTR(-ENOMEM); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 161 | |
| 162 | pdata->buttons = (struct gpio_keys_button *)(pdata + 1); |
| 163 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 164 | pdata->rep = device_property_present(dev, "autorepeat"); |
| 165 | device_property_read_u32(dev, "poll-interval", &pdata->poll_interval); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 166 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 167 | device_for_each_child_node(dev, child) { |
| 168 | struct gpio_desc *desc; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 169 | |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 170 | desc = devm_get_gpiod_from_child(dev, NULL, child); |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 171 | if (IS_ERR(desc)) { |
| 172 | error = PTR_ERR(desc); |
Gabor Juhos | d46329a | 2012-12-23 01:54:58 -0800 | [diff] [blame] | 173 | if (error != -EPROBE_DEFER) |
| 174 | dev_err(dev, |
| 175 | "Failed to get gpio flags, error: %d\n", |
| 176 | error); |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 177 | fwnode_handle_put(child); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 178 | return ERR_PTR(error); |
Gabor Juhos | d46329a | 2012-12-23 01:54:58 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 181 | button = &pdata->buttons[pdata->nbuttons++]; |
| 182 | button->gpiod = desc; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 183 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 184 | if (fwnode_property_read_u32(child, "linux,code", &button->code)) { |
| 185 | dev_err(dev, "Button without keycode: %d\n", |
| 186 | pdata->nbuttons - 1); |
| 187 | fwnode_handle_put(child); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 188 | return ERR_PTR(-EINVAL); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 191 | fwnode_property_read_string(child, "label", &button->desc); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 192 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 193 | if (fwnode_property_read_u32(child, "linux,input-type", |
| 194 | &button->type)) |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 195 | button->type = EV_KEY; |
| 196 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 197 | if (fwnode_property_read_u32(child, "linux,input-value", |
| 198 | (u32 *)&button->value)) |
| 199 | button->value = 1; |
| 200 | |
Dmitry Torokhov | 99b4ffb | 2015-07-16 12:10:05 -0700 | [diff] [blame] | 201 | button->wakeup = |
| 202 | fwnode_property_read_bool(child, "wakeup-source") || |
| 203 | /* legacy name */ |
| 204 | fwnode_property_read_bool(child, "gpio-key,wakeup"); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 205 | |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 206 | if (fwnode_property_read_u32(child, "debounce-interval", |
| 207 | &button->debounce_interval)) |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 208 | button->debounce_interval = 5; |
| 209 | } |
| 210 | |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 211 | if (pdata->nbuttons == 0) |
| 212 | return ERR_PTR(-EINVAL); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 213 | |
| 214 | return pdata; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 217 | static void gpio_keys_polled_set_abs_params(struct input_dev *input, |
| 218 | const struct gpio_keys_platform_data *pdata, unsigned int code) |
| 219 | { |
| 220 | int i, min = 0, max = 0; |
| 221 | |
| 222 | for (i = 0; i < pdata->nbuttons; i++) { |
| 223 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 224 | |
| 225 | if (button->type != EV_ABS || button->code != code) |
| 226 | continue; |
| 227 | |
| 228 | if (button->value < min) |
| 229 | min = button->value; |
| 230 | if (button->value > max) |
| 231 | max = button->value; |
| 232 | } |
| 233 | input_set_abs_params(input, code, min, max, 0, 0); |
| 234 | } |
| 235 | |
Jingoo Han | 90c98ef | 2014-05-07 12:58:36 -0700 | [diff] [blame] | 236 | static const struct of_device_id gpio_keys_polled_of_match[] = { |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 237 | { .compatible = "gpio-keys-polled", }, |
| 238 | { }, |
| 239 | }; |
| 240 | MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); |
| 241 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 242 | static int gpio_keys_polled_probe(struct platform_device *pdev) |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 243 | { |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 244 | struct device *dev = &pdev->dev; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 245 | const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 246 | struct gpio_keys_polled_dev *bdev; |
| 247 | struct input_polled_dev *poll_dev; |
| 248 | struct input_dev *input; |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 249 | size_t size; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 250 | int error; |
| 251 | int i; |
| 252 | |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 253 | if (!pdata) { |
| 254 | pdata = gpio_keys_polled_get_devtree_pdata(dev); |
| 255 | if (IS_ERR(pdata)) |
| 256 | return PTR_ERR(pdata); |
| 257 | if (!pdata) { |
| 258 | dev_err(dev, "missing platform data\n"); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (!pdata->poll_interval) { |
| 264 | dev_err(dev, "missing poll_interval value\n"); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 265 | return -EINVAL; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 266 | } |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 267 | |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 268 | size = sizeof(struct gpio_keys_polled_dev) + |
| 269 | pdata->nbuttons * sizeof(struct gpio_keys_button_data); |
| 270 | bdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 271 | if (!bdev) { |
| 272 | dev_err(dev, "no memory for private data\n"); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 273 | return -ENOMEM; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 276 | poll_dev = devm_input_allocate_polled_device(&pdev->dev); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 277 | if (!poll_dev) { |
| 278 | dev_err(dev, "no memory for polled device\n"); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 279 | return -ENOMEM; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | poll_dev->private = bdev; |
| 283 | poll_dev->poll = gpio_keys_polled_poll; |
| 284 | poll_dev->poll_interval = pdata->poll_interval; |
| 285 | poll_dev->open = gpio_keys_polled_open; |
| 286 | poll_dev->close = gpio_keys_polled_close; |
| 287 | |
| 288 | input = poll_dev->input; |
| 289 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 290 | input->name = pdev->name; |
| 291 | input->phys = DRV_NAME"/input0"; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 292 | |
| 293 | input->id.bustype = BUS_HOST; |
| 294 | input->id.vendor = 0x0001; |
| 295 | input->id.product = 0x0001; |
| 296 | input->id.version = 0x0100; |
| 297 | |
Alexander Shiyan | 1a22e16 | 2012-11-29 08:57:17 -0800 | [diff] [blame] | 298 | __set_bit(EV_KEY, input->evbit); |
| 299 | if (pdata->rep) |
| 300 | __set_bit(EV_REP, input->evbit); |
| 301 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 302 | for (i = 0; i < pdata->nbuttons; i++) { |
| 303 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 304 | struct gpio_keys_button_data *bdata = &bdev->data[i]; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 305 | unsigned int type = button->type ?: EV_KEY; |
| 306 | |
| 307 | if (button->wakeup) { |
| 308 | dev_err(dev, DRV_NAME " does not support wakeup\n"); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 309 | return -EINVAL; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Aaron Lu | 633a21d | 2014-10-21 23:30:25 +0200 | [diff] [blame] | 312 | /* |
| 313 | * Legacy GPIO number so request the GPIO here and |
| 314 | * convert it to descriptor. |
| 315 | */ |
| 316 | if (!button->gpiod && gpio_is_valid(button->gpio)) { |
Vincent Pelletier | 1ae5ddb | 2015-08-20 12:00:19 -0700 | [diff] [blame] | 317 | unsigned flags = GPIOF_IN; |
Aaron Lu | 633a21d | 2014-10-21 23:30:25 +0200 | [diff] [blame] | 318 | |
| 319 | if (button->active_low) |
| 320 | flags |= GPIOF_ACTIVE_LOW; |
| 321 | |
| 322 | error = devm_gpio_request_one(&pdev->dev, button->gpio, |
| 323 | flags, button->desc ? : DRV_NAME); |
| 324 | if (error) { |
| 325 | dev_err(dev, "unable to claim gpio %u, err=%d\n", |
| 326 | button->gpio, error); |
| 327 | return error; |
| 328 | } |
| 329 | |
| 330 | button->gpiod = gpio_to_desc(button->gpio); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Aaron Lu | 633a21d | 2014-10-21 23:30:25 +0200 | [diff] [blame] | 333 | if (IS_ERR(button->gpiod)) |
| 334 | return PTR_ERR(button->gpiod); |
| 335 | |
| 336 | bdata->can_sleep = gpiod_cansleep(button->gpiod); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 337 | bdata->last_state = -1; |
| 338 | bdata->threshold = DIV_ROUND_UP(button->debounce_interval, |
| 339 | pdata->poll_interval); |
| 340 | |
| 341 | input_set_capability(input, type, button->code); |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 342 | if (type == EV_ABS) |
| 343 | gpio_keys_polled_set_abs_params(input, pdata, |
| 344 | button->code); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | bdev->poll_dev = poll_dev; |
| 348 | bdev->dev = dev; |
| 349 | bdev->pdata = pdata; |
| 350 | platform_set_drvdata(pdev, bdev); |
| 351 | |
| 352 | error = input_register_polled_device(poll_dev); |
| 353 | if (error) { |
| 354 | dev_err(dev, "unable to register polled device, err=%d\n", |
| 355 | error); |
Alexander Shiyan | 6825263 | 2014-04-28 10:48:49 -0700 | [diff] [blame] | 356 | return error; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* report initial state of the buttons */ |
| 360 | for (i = 0; i < pdata->nbuttons; i++) |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 361 | gpio_keys_polled_check_state(poll_dev, &pdata->buttons[i], |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 362 | &bdev->data[i]); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 363 | |
Hans de Goede | 6f29d3b | 2015-10-14 16:44:35 -0700 | [diff] [blame] | 364 | input_sync(input); |
| 365 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 366 | return 0; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static struct platform_driver gpio_keys_polled_driver = { |
| 370 | .probe = gpio_keys_polled_probe, |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 371 | .driver = { |
| 372 | .name = DRV_NAME, |
Aaron Lu | b26d4e2 | 2014-10-21 13:34:00 +0200 | [diff] [blame] | 373 | .of_match_table = gpio_keys_polled_of_match, |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 374 | }, |
| 375 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 376 | module_platform_driver(gpio_keys_polled_driver); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 377 | |
| 378 | MODULE_LICENSE("GPL v2"); |
| 379 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 380 | MODULE_DESCRIPTION("Polled GPIO Buttons driver"); |
| 381 | MODULE_ALIAS("platform:" DRV_NAME); |