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> |
| 26 | #include <linux/gpio_keys.h> |
Sachin Kamat | 2780170 | 2013-10-06 00:55:33 -0700 | [diff] [blame] | 27 | #include <linux/of.h> |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 28 | #include <linux/of_platform.h> |
| 29 | #include <linux/of_gpio.h> |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 30 | |
| 31 | #define DRV_NAME "gpio-keys-polled" |
| 32 | |
| 33 | struct gpio_keys_button_data { |
| 34 | int last_state; |
| 35 | int count; |
| 36 | int threshold; |
| 37 | int can_sleep; |
| 38 | }; |
| 39 | |
| 40 | struct gpio_keys_polled_dev { |
| 41 | struct input_polled_dev *poll_dev; |
| 42 | struct device *dev; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 43 | const struct gpio_keys_platform_data *pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 44 | struct gpio_keys_button_data data[0]; |
| 45 | }; |
| 46 | |
| 47 | static void gpio_keys_polled_check_state(struct input_dev *input, |
| 48 | struct gpio_keys_button *button, |
| 49 | struct gpio_keys_button_data *bdata) |
| 50 | { |
| 51 | int state; |
| 52 | |
| 53 | if (bdata->can_sleep) |
| 54 | state = !!gpio_get_value_cansleep(button->gpio); |
| 55 | else |
| 56 | state = !!gpio_get_value(button->gpio); |
| 57 | |
| 58 | if (state != bdata->last_state) { |
| 59 | unsigned int type = button->type ?: EV_KEY; |
| 60 | |
| 61 | input_event(input, type, button->code, |
| 62 | !!(state ^ button->active_low)); |
| 63 | input_sync(input); |
| 64 | bdata->count = 0; |
| 65 | bdata->last_state = state; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void gpio_keys_polled_poll(struct input_polled_dev *dev) |
| 70 | { |
| 71 | struct gpio_keys_polled_dev *bdev = dev->private; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 72 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 73 | struct input_dev *input = dev->input; |
| 74 | int i; |
| 75 | |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 76 | for (i = 0; i < pdata->nbuttons; i++) { |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 77 | struct gpio_keys_button_data *bdata = &bdev->data[i]; |
| 78 | |
| 79 | if (bdata->count < bdata->threshold) |
| 80 | bdata->count++; |
| 81 | else |
| 82 | gpio_keys_polled_check_state(input, &pdata->buttons[i], |
| 83 | bdata); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void gpio_keys_polled_open(struct input_polled_dev *dev) |
| 88 | { |
| 89 | struct gpio_keys_polled_dev *bdev = dev->private; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 90 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 91 | |
| 92 | if (pdata->enable) |
| 93 | pdata->enable(bdev->dev); |
| 94 | } |
| 95 | |
| 96 | static void gpio_keys_polled_close(struct input_polled_dev *dev) |
| 97 | { |
| 98 | struct gpio_keys_polled_dev *bdev = dev->private; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 99 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 100 | |
| 101 | if (pdata->disable) |
| 102 | pdata->disable(bdev->dev); |
| 103 | } |
| 104 | |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 105 | #ifdef CONFIG_OF |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 106 | 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] | 107 | { |
| 108 | struct device_node *node, *pp; |
| 109 | struct gpio_keys_platform_data *pdata; |
| 110 | struct gpio_keys_button *button; |
| 111 | int error; |
| 112 | int nbuttons; |
| 113 | int i; |
| 114 | |
| 115 | node = dev->of_node; |
| 116 | if (!node) |
| 117 | return NULL; |
| 118 | |
| 119 | nbuttons = of_get_child_count(node); |
| 120 | if (nbuttons == 0) |
| 121 | return NULL; |
| 122 | |
| 123 | pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button), |
| 124 | GFP_KERNEL); |
| 125 | if (!pdata) { |
| 126 | error = -ENOMEM; |
| 127 | goto err_out; |
| 128 | } |
| 129 | |
| 130 | pdata->buttons = (struct gpio_keys_button *)(pdata + 1); |
Alexandre Pereira da Silva | 8757145 | 2012-09-12 21:49:26 -0700 | [diff] [blame] | 131 | pdata->nbuttons = nbuttons; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 132 | |
| 133 | pdata->rep = !!of_get_property(node, "autorepeat", NULL); |
| 134 | of_property_read_u32(node, "poll-interval", &pdata->poll_interval); |
| 135 | |
| 136 | i = 0; |
| 137 | for_each_child_of_node(node, pp) { |
Gabor Juhos | d46329a | 2012-12-23 01:54:58 -0800 | [diff] [blame] | 138 | int gpio; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 139 | enum of_gpio_flags flags; |
| 140 | |
| 141 | if (!of_find_property(pp, "gpios", NULL)) { |
| 142 | pdata->nbuttons--; |
| 143 | dev_warn(dev, "Found button without gpios\n"); |
| 144 | continue; |
| 145 | } |
| 146 | |
Gabor Juhos | d46329a | 2012-12-23 01:54:58 -0800 | [diff] [blame] | 147 | gpio = of_get_gpio_flags(pp, 0, &flags); |
| 148 | if (gpio < 0) { |
| 149 | error = gpio; |
| 150 | if (error != -EPROBE_DEFER) |
| 151 | dev_err(dev, |
| 152 | "Failed to get gpio flags, error: %d\n", |
| 153 | error); |
| 154 | goto err_free_pdata; |
| 155 | } |
| 156 | |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 157 | button = &pdata->buttons[i++]; |
| 158 | |
Gabor Juhos | d46329a | 2012-12-23 01:54:58 -0800 | [diff] [blame] | 159 | button->gpio = gpio; |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 160 | button->active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 161 | |
| 162 | if (of_property_read_u32(pp, "linux,code", &button->code)) { |
| 163 | dev_err(dev, "Button without keycode: 0x%x\n", |
| 164 | button->gpio); |
| 165 | error = -EINVAL; |
| 166 | goto err_free_pdata; |
| 167 | } |
| 168 | |
| 169 | button->desc = of_get_property(pp, "label", NULL); |
| 170 | |
| 171 | if (of_property_read_u32(pp, "linux,input-type", &button->type)) |
| 172 | button->type = EV_KEY; |
| 173 | |
| 174 | button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL); |
| 175 | |
| 176 | if (of_property_read_u32(pp, "debounce-interval", |
| 177 | &button->debounce_interval)) |
| 178 | button->debounce_interval = 5; |
| 179 | } |
| 180 | |
| 181 | if (pdata->nbuttons == 0) { |
| 182 | error = -EINVAL; |
| 183 | goto err_free_pdata; |
| 184 | } |
| 185 | |
| 186 | return pdata; |
| 187 | |
| 188 | err_free_pdata: |
| 189 | kfree(pdata); |
| 190 | err_out: |
| 191 | return ERR_PTR(error); |
| 192 | } |
| 193 | |
Jingoo Han | 90c98ef | 2014-05-07 12:58:36 -0700 | [diff] [blame^] | 194 | 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] | 195 | { .compatible = "gpio-keys-polled", }, |
| 196 | { }, |
| 197 | }; |
| 198 | MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); |
| 199 | |
| 200 | #else |
| 201 | |
| 202 | static inline struct gpio_keys_platform_data * |
| 203 | gpio_keys_polled_get_devtree_pdata(struct device *dev) |
| 204 | { |
| 205 | return NULL; |
| 206 | } |
| 207 | #endif |
| 208 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 209 | static int gpio_keys_polled_probe(struct platform_device *pdev) |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 210 | { |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 211 | struct device *dev = &pdev->dev; |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 212 | const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 213 | struct gpio_keys_polled_dev *bdev; |
| 214 | struct input_polled_dev *poll_dev; |
| 215 | struct input_dev *input; |
| 216 | int error; |
| 217 | int i; |
| 218 | |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 219 | if (!pdata) { |
| 220 | pdata = gpio_keys_polled_get_devtree_pdata(dev); |
| 221 | if (IS_ERR(pdata)) |
| 222 | return PTR_ERR(pdata); |
| 223 | if (!pdata) { |
| 224 | dev_err(dev, "missing platform data\n"); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (!pdata->poll_interval) { |
| 230 | dev_err(dev, "missing poll_interval value\n"); |
| 231 | error = -EINVAL; |
| 232 | goto err_free_pdata; |
| 233 | } |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 234 | |
| 235 | bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) + |
| 236 | pdata->nbuttons * sizeof(struct gpio_keys_button_data), |
| 237 | GFP_KERNEL); |
| 238 | if (!bdev) { |
| 239 | dev_err(dev, "no memory for private data\n"); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 240 | error = -ENOMEM; |
| 241 | goto err_free_pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | poll_dev = input_allocate_polled_device(); |
| 245 | if (!poll_dev) { |
| 246 | dev_err(dev, "no memory for polled device\n"); |
| 247 | error = -ENOMEM; |
| 248 | goto err_free_bdev; |
| 249 | } |
| 250 | |
| 251 | poll_dev->private = bdev; |
| 252 | poll_dev->poll = gpio_keys_polled_poll; |
| 253 | poll_dev->poll_interval = pdata->poll_interval; |
| 254 | poll_dev->open = gpio_keys_polled_open; |
| 255 | poll_dev->close = gpio_keys_polled_close; |
| 256 | |
| 257 | input = poll_dev->input; |
| 258 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 259 | input->name = pdev->name; |
| 260 | input->phys = DRV_NAME"/input0"; |
| 261 | input->dev.parent = &pdev->dev; |
| 262 | |
| 263 | input->id.bustype = BUS_HOST; |
| 264 | input->id.vendor = 0x0001; |
| 265 | input->id.product = 0x0001; |
| 266 | input->id.version = 0x0100; |
| 267 | |
Alexander Shiyan | 1a22e16 | 2012-11-29 08:57:17 -0800 | [diff] [blame] | 268 | __set_bit(EV_KEY, input->evbit); |
| 269 | if (pdata->rep) |
| 270 | __set_bit(EV_REP, input->evbit); |
| 271 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 272 | for (i = 0; i < pdata->nbuttons; i++) { |
| 273 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 274 | struct gpio_keys_button_data *bdata = &bdev->data[i]; |
| 275 | unsigned int gpio = button->gpio; |
| 276 | unsigned int type = button->type ?: EV_KEY; |
| 277 | |
| 278 | if (button->wakeup) { |
| 279 | dev_err(dev, DRV_NAME " does not support wakeup\n"); |
| 280 | error = -EINVAL; |
| 281 | goto err_free_gpio; |
| 282 | } |
| 283 | |
Dmitry Torokhov | da5ee07 | 2012-11-29 09:24:08 -0800 | [diff] [blame] | 284 | error = gpio_request_one(gpio, GPIOF_IN, |
| 285 | button->desc ?: DRV_NAME); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 286 | if (error) { |
| 287 | dev_err(dev, "unable to claim gpio %u, err=%d\n", |
| 288 | gpio, error); |
| 289 | goto err_free_gpio; |
| 290 | } |
| 291 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 292 | bdata->can_sleep = gpio_cansleep(gpio); |
| 293 | bdata->last_state = -1; |
| 294 | bdata->threshold = DIV_ROUND_UP(button->debounce_interval, |
| 295 | pdata->poll_interval); |
| 296 | |
| 297 | input_set_capability(input, type, button->code); |
| 298 | } |
| 299 | |
| 300 | bdev->poll_dev = poll_dev; |
| 301 | bdev->dev = dev; |
| 302 | bdev->pdata = pdata; |
| 303 | platform_set_drvdata(pdev, bdev); |
| 304 | |
| 305 | error = input_register_polled_device(poll_dev); |
| 306 | if (error) { |
| 307 | dev_err(dev, "unable to register polled device, err=%d\n", |
| 308 | error); |
| 309 | goto err_free_gpio; |
| 310 | } |
| 311 | |
| 312 | /* report initial state of the buttons */ |
| 313 | for (i = 0; i < pdata->nbuttons; i++) |
| 314 | gpio_keys_polled_check_state(input, &pdata->buttons[i], |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 315 | &bdev->data[i]); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | |
| 319 | err_free_gpio: |
| 320 | while (--i >= 0) |
| 321 | gpio_free(pdata->buttons[i].gpio); |
| 322 | |
| 323 | input_free_polled_device(poll_dev); |
| 324 | |
| 325 | err_free_bdev: |
| 326 | kfree(bdev); |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 327 | |
| 328 | err_free_pdata: |
| 329 | /* If we have no platform_data, we allocated pdata dynamically. */ |
| 330 | if (!dev_get_platdata(&pdev->dev)) |
| 331 | kfree(pdata); |
| 332 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 333 | return error; |
| 334 | } |
| 335 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 336 | static int gpio_keys_polled_remove(struct platform_device *pdev) |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 337 | { |
| 338 | struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev); |
Dmitry Torokhov | 2976f24 | 2012-07-29 22:48:33 -0700 | [diff] [blame] | 339 | const struct gpio_keys_platform_data *pdata = bdev->pdata; |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 340 | int i; |
| 341 | |
| 342 | input_unregister_polled_device(bdev->poll_dev); |
| 343 | |
| 344 | for (i = 0; i < pdata->nbuttons; i++) |
| 345 | gpio_free(pdata->buttons[i].gpio); |
| 346 | |
| 347 | input_free_polled_device(bdev->poll_dev); |
| 348 | |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 349 | /* |
| 350 | * If we had no platform_data, we allocated pdata dynamically and |
| 351 | * must free it here. |
| 352 | */ |
| 353 | if (!dev_get_platdata(&pdev->dev)) |
| 354 | kfree(pdata); |
| 355 | |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 356 | kfree(bdev); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static struct platform_driver gpio_keys_polled_driver = { |
| 362 | .probe = gpio_keys_polled_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 363 | .remove = gpio_keys_polled_remove, |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 364 | .driver = { |
| 365 | .name = DRV_NAME, |
| 366 | .owner = THIS_MODULE, |
Alexandre Pereira da Silva | a2f2524 | 2012-07-31 22:08:45 -0700 | [diff] [blame] | 367 | .of_match_table = of_match_ptr(gpio_keys_polled_of_match), |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 368 | }, |
| 369 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 370 | module_platform_driver(gpio_keys_polled_driver); |
Gabor Juhos | 0e7d0c8 | 2010-12-06 17:14:47 -0800 | [diff] [blame] | 371 | |
| 372 | MODULE_LICENSE("GPL v2"); |
| 373 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 374 | MODULE_DESCRIPTION("Polled GPIO Buttons driver"); |
| 375 | MODULE_ALIAS("platform:" DRV_NAME); |