2 * Driver for keys on GPIO lines capable of generating interrupts.
4 * Copyright 2005 Phil Blundell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/sched.h>
19 #include <linux/sysctl.h>
20 #include <linux/proc_fs.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/input.h>
24 #include <linux/gpio_keys.h>
25 #include <linux/workqueue.h>
26 #include <linux/gpio.h>
28 struct gpio_button_data {
29 struct gpio_keys_button *button;
30 struct input_dev *input;
31 struct timer_list timer;
32 struct work_struct work;
36 struct gpio_keys_drvdata {
37 struct input_dev *input;
38 struct mutex disable_lock;
39 unsigned int n_buttons;
40 struct gpio_button_data data[0];
44 * SYSFS interface for enabling/disabling keys and switches:
46 * There are 4 attributes under /sys/devices/platform/gpio-keys/
47 * keys [ro] - bitmap of keys (EV_KEY) which can be
49 * switches [ro] - bitmap of switches (EV_SW) which can be
51 * disabled_keys [rw] - bitmap of keys currently disabled
52 * disabled_switches [rw] - bitmap of switches currently disabled
54 * Userland can change these values and hence disable event generation
55 * for each key (or switch). Disabling a key means its interrupt line
58 * For example, if we have following switches set up as gpio-keys:
60 * SW_CAMERA_LENS_COVER = 9
61 * SW_KEYPAD_SLIDE = 10
62 * SW_FRONT_PROXIMITY = 11
63 * This is read from switches:
65 * Next we want to disable proximity (11) and dock (5), we write:
67 * to file disabled_switches. Now proximity and dock IRQs are disabled.
68 * This can be verified by reading the file disabled_switches:
70 * If we now want to enable proximity (11) switch we write:
72 * to disabled_switches.
74 * We can disable only those keys which don't allow sharing the irq.
78 * get_n_events_by_type() - returns maximum number of events per @type
79 * @type: type of button (%EV_KEY, %EV_SW)
81 * Return value of this function can be used to allocate bitmap
82 * large enough to hold all bits for given type.
84 static inline int get_n_events_by_type(int type)
86 BUG_ON(type != EV_SW && type != EV_KEY);
88 return (type == EV_KEY) ? KEY_CNT : SW_CNT;
92 * gpio_keys_disable_button() - disables given GPIO button
93 * @bdata: button data for button to be disabled
95 * Disables button pointed by @bdata. This is done by masking
96 * IRQ line. After this function is called, button won't generate
97 * input events anymore. Note that one can only disable buttons
98 * that don't share IRQs.
100 * Make sure that @bdata->disable_lock is locked when entering
101 * this function to avoid races when concurrent threads are
102 * disabling buttons at the same time.
104 static void gpio_keys_disable_button(struct gpio_button_data *bdata)
106 if (!bdata->disabled) {
108 * Disable IRQ and possible debouncing timer.
110 disable_irq(gpio_to_irq(bdata->button->gpio));
111 if (bdata->button->debounce_interval)
112 del_timer_sync(&bdata->timer);
114 bdata->disabled = true;
119 * gpio_keys_enable_button() - enables given GPIO button
120 * @bdata: button data for button to be disabled
122 * Enables given button pointed by @bdata.
124 * Make sure that @bdata->disable_lock is locked when entering
125 * this function to avoid races with concurrent threads trying
126 * to enable the same button at the same time.
128 static void gpio_keys_enable_button(struct gpio_button_data *bdata)
130 if (bdata->disabled) {
131 enable_irq(gpio_to_irq(bdata->button->gpio));
132 bdata->disabled = false;
137 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
138 * @ddata: pointer to drvdata
139 * @buf: buffer where stringified bitmap is written
140 * @type: button type (%EV_KEY, %EV_SW)
141 * @only_disabled: does caller want only those buttons that are
142 * currently disabled or all buttons that can be
145 * This function writes buttons that can be disabled to @buf. If
146 * @only_disabled is true, then @buf contains only those buttons
147 * that are currently disabled. Returns 0 on success or negative
150 static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata,
151 char *buf, unsigned int type,
154 int n_events = get_n_events_by_type(type);
159 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
163 for (i = 0; i < ddata->n_buttons; i++) {
164 struct gpio_button_data *bdata = &ddata->data[i];
166 if (bdata->button->type != type)
169 if (only_disabled && !bdata->disabled)
172 __set_bit(bdata->button->code, bits);
175 ret = bitmap_scnlistprintf(buf, PAGE_SIZE - 2, bits, n_events);
185 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
186 * @ddata: pointer to drvdata
187 * @buf: buffer from userspace that contains stringified bitmap
188 * @type: button type (%EV_KEY, %EV_SW)
190 * This function parses stringified bitmap from @buf and disables/enables
191 * GPIO buttons accordinly. Returns 0 on success and negative error
194 static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata,
195 const char *buf, unsigned int type)
197 int n_events = get_n_events_by_type(type);
202 bits = kcalloc(BITS_TO_LONGS(n_events), sizeof(*bits), GFP_KERNEL);
206 error = bitmap_parselist(buf, bits, n_events);
211 for (i = 0; i < ddata->n_buttons; i++) {
212 struct gpio_button_data *bdata = &ddata->data[i];
214 if (bdata->button->type != type)
217 if (test_bit(bdata->button->code, bits) &&
218 !bdata->button->can_disable) {
224 mutex_lock(&ddata->disable_lock);
226 for (i = 0; i < ddata->n_buttons; i++) {
227 struct gpio_button_data *bdata = &ddata->data[i];
229 if (bdata->button->type != type)
232 if (test_bit(bdata->button->code, bits))
233 gpio_keys_disable_button(bdata);
235 gpio_keys_enable_button(bdata);
238 mutex_unlock(&ddata->disable_lock);
245 #define ATTR_SHOW_FN(name, type, only_disabled) \
246 static ssize_t gpio_keys_show_##name(struct device *dev, \
247 struct device_attribute *attr, \
250 struct platform_device *pdev = to_platform_device(dev); \
251 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
253 return gpio_keys_attr_show_helper(ddata, buf, \
254 type, only_disabled); \
257 ATTR_SHOW_FN(keys, EV_KEY, false);
258 ATTR_SHOW_FN(switches, EV_SW, false);
259 ATTR_SHOW_FN(disabled_keys, EV_KEY, true);
260 ATTR_SHOW_FN(disabled_switches, EV_SW, true);
265 * /sys/devices/platform/gpio-keys/keys [ro]
266 * /sys/devices/platform/gpio-keys/switches [ro]
268 static DEVICE_ATTR(keys, S_IRUGO, gpio_keys_show_keys, NULL);
269 static DEVICE_ATTR(switches, S_IRUGO, gpio_keys_show_switches, NULL);
271 #define ATTR_STORE_FN(name, type) \
272 static ssize_t gpio_keys_store_##name(struct device *dev, \
273 struct device_attribute *attr, \
277 struct platform_device *pdev = to_platform_device(dev); \
278 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
281 error = gpio_keys_attr_store_helper(ddata, buf, type); \
288 ATTR_STORE_FN(disabled_keys, EV_KEY);
289 ATTR_STORE_FN(disabled_switches, EV_SW);
294 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
295 * /sys/devices/platform/gpio-keys/disables_switches [rw]
297 static DEVICE_ATTR(disabled_keys, S_IWUSR | S_IRUGO,
298 gpio_keys_show_disabled_keys,
299 gpio_keys_store_disabled_keys);
300 static DEVICE_ATTR(disabled_switches, S_IWUSR | S_IRUGO,
301 gpio_keys_show_disabled_switches,
302 gpio_keys_store_disabled_switches);
304 static struct attribute *gpio_keys_attrs[] = {
306 &dev_attr_switches.attr,
307 &dev_attr_disabled_keys.attr,
308 &dev_attr_disabled_switches.attr,
312 static struct attribute_group gpio_keys_attr_group = {
313 .attrs = gpio_keys_attrs,
316 static void gpio_keys_report_event(struct gpio_button_data *bdata)
318 struct gpio_keys_button *button = bdata->button;
319 struct input_dev *input = bdata->input;
320 unsigned int type = button->type ?: EV_KEY;
321 int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
323 input_event(input, type, button->code, !!state);
327 static void gpio_keys_work_func(struct work_struct *work)
329 struct gpio_button_data *bdata =
330 container_of(work, struct gpio_button_data, work);
332 gpio_keys_report_event(bdata);
335 static void gpio_keys_timer(unsigned long _data)
337 struct gpio_button_data *data = (struct gpio_button_data *)_data;
339 schedule_work(&data->work);
342 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
344 struct gpio_button_data *bdata = dev_id;
345 struct gpio_keys_button *button = bdata->button;
347 BUG_ON(irq != gpio_to_irq(button->gpio));
349 if (button->debounce_interval)
350 mod_timer(&bdata->timer,
351 jiffies + msecs_to_jiffies(button->debounce_interval));
353 schedule_work(&bdata->work);
358 static int __devinit gpio_keys_setup_key(struct platform_device *pdev,
359 struct gpio_button_data *bdata,
360 struct gpio_keys_button *button)
362 char *desc = button->desc ? button->desc : "gpio_keys";
363 struct device *dev = &pdev->dev;
364 unsigned long irqflags;
367 setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata);
368 INIT_WORK(&bdata->work, gpio_keys_work_func);
370 error = gpio_request(button->gpio, desc);
372 dev_err(dev, "failed to request GPIO %d, error %d\n",
373 button->gpio, error);
377 error = gpio_direction_input(button->gpio);
379 dev_err(dev, "failed to configure"
380 " direction for GPIO %d, error %d\n",
381 button->gpio, error);
385 irq = gpio_to_irq(button->gpio);
388 dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n",
389 button->gpio, error);
393 irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
395 * If platform has specified that the button can be disabled,
396 * we don't want it to share the interrupt line.
398 if (!button->can_disable)
399 irqflags |= IRQF_SHARED;
401 error = request_irq(irq, gpio_keys_isr, irqflags, desc, bdata);
403 dev_err(dev, "Unable to claim irq %d; error %d\n",
411 gpio_free(button->gpio);
416 static int __devinit gpio_keys_probe(struct platform_device *pdev)
418 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
419 struct gpio_keys_drvdata *ddata;
420 struct device *dev = &pdev->dev;
421 struct input_dev *input;
425 ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
426 pdata->nbuttons * sizeof(struct gpio_button_data),
428 input = input_allocate_device();
429 if (!ddata || !input) {
430 dev_err(dev, "failed to allocate state\n");
435 ddata->input = input;
436 ddata->n_buttons = pdata->nbuttons;
437 mutex_init(&ddata->disable_lock);
439 platform_set_drvdata(pdev, ddata);
441 input->name = pdev->name;
442 input->phys = "gpio-keys/input0";
443 input->dev.parent = &pdev->dev;
445 input->id.bustype = BUS_HOST;
446 input->id.vendor = 0x0001;
447 input->id.product = 0x0001;
448 input->id.version = 0x0100;
450 /* Enable auto repeat feature of Linux input subsystem */
452 __set_bit(EV_REP, input->evbit);
454 for (i = 0; i < pdata->nbuttons; i++) {
455 struct gpio_keys_button *button = &pdata->buttons[i];
456 struct gpio_button_data *bdata = &ddata->data[i];
457 unsigned int type = button->type ?: EV_KEY;
459 bdata->input = input;
460 bdata->button = button;
462 error = gpio_keys_setup_key(pdev, bdata, button);
469 input_set_capability(input, type, button->code);
472 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group);
474 dev_err(dev, "Unable to export keys/switches, error: %d\n",
479 error = input_register_device(input);
481 dev_err(dev, "Unable to register input device, error: %d\n",
486 /* get current state of buttons */
487 for (i = 0; i < pdata->nbuttons; i++)
488 gpio_keys_report_event(&ddata->data[i]);
491 device_init_wakeup(&pdev->dev, wakeup);
496 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
499 free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]);
500 if (pdata->buttons[i].debounce_interval)
501 del_timer_sync(&ddata->data[i].timer);
502 cancel_work_sync(&ddata->data[i].work);
503 gpio_free(pdata->buttons[i].gpio);
506 platform_set_drvdata(pdev, NULL);
508 input_free_device(input);
514 static int __devexit gpio_keys_remove(struct platform_device *pdev)
516 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
517 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
518 struct input_dev *input = ddata->input;
521 sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group);
523 device_init_wakeup(&pdev->dev, 0);
525 for (i = 0; i < pdata->nbuttons; i++) {
526 int irq = gpio_to_irq(pdata->buttons[i].gpio);
527 free_irq(irq, &ddata->data[i]);
528 if (pdata->buttons[i].debounce_interval)
529 del_timer_sync(&ddata->data[i].timer);
530 cancel_work_sync(&ddata->data[i].work);
531 gpio_free(pdata->buttons[i].gpio);
534 input_unregister_device(input);
541 static int gpio_keys_suspend(struct device *dev)
543 struct platform_device *pdev = to_platform_device(dev);
544 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
547 if (device_may_wakeup(&pdev->dev)) {
548 for (i = 0; i < pdata->nbuttons; i++) {
549 struct gpio_keys_button *button = &pdata->buttons[i];
550 if (button->wakeup) {
551 int irq = gpio_to_irq(button->gpio);
552 enable_irq_wake(irq);
560 static int gpio_keys_resume(struct device *dev)
562 struct platform_device *pdev = to_platform_device(dev);
563 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev);
564 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
567 for (i = 0; i < pdata->nbuttons; i++) {
569 struct gpio_keys_button *button = &pdata->buttons[i];
570 if (button->wakeup && device_may_wakeup(&pdev->dev)) {
571 int irq = gpio_to_irq(button->gpio);
572 disable_irq_wake(irq);
575 gpio_keys_report_event(&ddata->data[i]);
577 input_sync(ddata->input);
582 static const struct dev_pm_ops gpio_keys_pm_ops = {
583 .suspend = gpio_keys_suspend,
584 .resume = gpio_keys_resume,
588 static struct platform_driver gpio_keys_device_driver = {
589 .probe = gpio_keys_probe,
590 .remove = __devexit_p(gpio_keys_remove),
593 .owner = THIS_MODULE,
595 .pm = &gpio_keys_pm_ops,
600 static int __init gpio_keys_init(void)
602 return platform_driver_register(&gpio_keys_device_driver);
605 static void __exit gpio_keys_exit(void)
607 platform_driver_unregister(&gpio_keys_device_driver);
610 module_init(gpio_keys_init);
611 module_exit(gpio_keys_exit);
613 MODULE_LICENSE("GPL");
614 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
615 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
616 MODULE_ALIAS("platform:gpio-keys");