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/slab.h>
20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/input.h>
25 #include <linux/gpio_keys.h>
26 #include <linux/workqueue.h>
27 #include <linux/gpio.h>
29 struct gpio_button_data
{
30 struct gpio_keys_button
*button
;
31 struct input_dev
*input
;
32 struct timer_list timer
;
33 struct work_struct work
;
34 int timer_debounce
; /* in msecs */
38 struct gpio_keys_drvdata
{
39 struct input_dev
*input
;
40 struct mutex disable_lock
;
41 unsigned int n_buttons
;
42 int (*enable
)(struct device
*dev
);
43 void (*disable
)(struct device
*dev
);
44 struct gpio_button_data data
[0];
48 * SYSFS interface for enabling/disabling keys and switches:
50 * There are 4 attributes under /sys/devices/platform/gpio-keys/
51 * keys [ro] - bitmap of keys (EV_KEY) which can be
53 * switches [ro] - bitmap of switches (EV_SW) which can be
55 * disabled_keys [rw] - bitmap of keys currently disabled
56 * disabled_switches [rw] - bitmap of switches currently disabled
58 * Userland can change these values and hence disable event generation
59 * for each key (or switch). Disabling a key means its interrupt line
62 * For example, if we have following switches set up as gpio-keys:
64 * SW_CAMERA_LENS_COVER = 9
65 * SW_KEYPAD_SLIDE = 10
66 * SW_FRONT_PROXIMITY = 11
67 * This is read from switches:
69 * Next we want to disable proximity (11) and dock (5), we write:
71 * to file disabled_switches. Now proximity and dock IRQs are disabled.
72 * This can be verified by reading the file disabled_switches:
74 * If we now want to enable proximity (11) switch we write:
76 * to disabled_switches.
78 * We can disable only those keys which don't allow sharing the irq.
82 * get_n_events_by_type() - returns maximum number of events per @type
83 * @type: type of button (%EV_KEY, %EV_SW)
85 * Return value of this function can be used to allocate bitmap
86 * large enough to hold all bits for given type.
88 static inline int get_n_events_by_type(int type
)
90 BUG_ON(type
!= EV_SW
&& type
!= EV_KEY
);
92 return (type
== EV_KEY
) ? KEY_CNT
: SW_CNT
;
96 * gpio_keys_disable_button() - disables given GPIO button
97 * @bdata: button data for button to be disabled
99 * Disables button pointed by @bdata. This is done by masking
100 * IRQ line. After this function is called, button won't generate
101 * input events anymore. Note that one can only disable buttons
102 * that don't share IRQs.
104 * Make sure that @bdata->disable_lock is locked when entering
105 * this function to avoid races when concurrent threads are
106 * disabling buttons at the same time.
108 static void gpio_keys_disable_button(struct gpio_button_data
*bdata
)
110 if (!bdata
->disabled
) {
112 * Disable IRQ and possible debouncing timer.
114 disable_irq(gpio_to_irq(bdata
->button
->gpio
));
115 if (bdata
->timer_debounce
)
116 del_timer_sync(&bdata
->timer
);
118 bdata
->disabled
= true;
123 * gpio_keys_enable_button() - enables given GPIO button
124 * @bdata: button data for button to be disabled
126 * Enables given button pointed by @bdata.
128 * Make sure that @bdata->disable_lock is locked when entering
129 * this function to avoid races with concurrent threads trying
130 * to enable the same button at the same time.
132 static void gpio_keys_enable_button(struct gpio_button_data
*bdata
)
134 if (bdata
->disabled
) {
135 enable_irq(gpio_to_irq(bdata
->button
->gpio
));
136 bdata
->disabled
= false;
141 * gpio_keys_attr_show_helper() - fill in stringified bitmap of buttons
142 * @ddata: pointer to drvdata
143 * @buf: buffer where stringified bitmap is written
144 * @type: button type (%EV_KEY, %EV_SW)
145 * @only_disabled: does caller want only those buttons that are
146 * currently disabled or all buttons that can be
149 * This function writes buttons that can be disabled to @buf. If
150 * @only_disabled is true, then @buf contains only those buttons
151 * that are currently disabled. Returns 0 on success or negative
154 static ssize_t
gpio_keys_attr_show_helper(struct gpio_keys_drvdata
*ddata
,
155 char *buf
, unsigned int type
,
158 int n_events
= get_n_events_by_type(type
);
163 bits
= kcalloc(BITS_TO_LONGS(n_events
), sizeof(*bits
), GFP_KERNEL
);
167 for (i
= 0; i
< ddata
->n_buttons
; i
++) {
168 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
170 if (bdata
->button
->type
!= type
)
173 if (only_disabled
&& !bdata
->disabled
)
176 __set_bit(bdata
->button
->code
, bits
);
179 ret
= bitmap_scnlistprintf(buf
, PAGE_SIZE
- 2, bits
, n_events
);
189 * gpio_keys_attr_store_helper() - enable/disable buttons based on given bitmap
190 * @ddata: pointer to drvdata
191 * @buf: buffer from userspace that contains stringified bitmap
192 * @type: button type (%EV_KEY, %EV_SW)
194 * This function parses stringified bitmap from @buf and disables/enables
195 * GPIO buttons accordinly. Returns 0 on success and negative error
198 static ssize_t
gpio_keys_attr_store_helper(struct gpio_keys_drvdata
*ddata
,
199 const char *buf
, unsigned int type
)
201 int n_events
= get_n_events_by_type(type
);
206 bits
= kcalloc(BITS_TO_LONGS(n_events
), sizeof(*bits
), GFP_KERNEL
);
210 error
= bitmap_parselist(buf
, bits
, n_events
);
215 for (i
= 0; i
< ddata
->n_buttons
; i
++) {
216 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
218 if (bdata
->button
->type
!= type
)
221 if (test_bit(bdata
->button
->code
, bits
) &&
222 !bdata
->button
->can_disable
) {
228 mutex_lock(&ddata
->disable_lock
);
230 for (i
= 0; i
< ddata
->n_buttons
; i
++) {
231 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
233 if (bdata
->button
->type
!= type
)
236 if (test_bit(bdata
->button
->code
, bits
))
237 gpio_keys_disable_button(bdata
);
239 gpio_keys_enable_button(bdata
);
242 mutex_unlock(&ddata
->disable_lock
);
249 #define ATTR_SHOW_FN(name, type, only_disabled) \
250 static ssize_t gpio_keys_show_##name(struct device *dev, \
251 struct device_attribute *attr, \
254 struct platform_device *pdev = to_platform_device(dev); \
255 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
257 return gpio_keys_attr_show_helper(ddata, buf, \
258 type, only_disabled); \
261 ATTR_SHOW_FN(keys
, EV_KEY
, false);
262 ATTR_SHOW_FN(switches
, EV_SW
, false);
263 ATTR_SHOW_FN(disabled_keys
, EV_KEY
, true);
264 ATTR_SHOW_FN(disabled_switches
, EV_SW
, true);
269 * /sys/devices/platform/gpio-keys/keys [ro]
270 * /sys/devices/platform/gpio-keys/switches [ro]
272 static DEVICE_ATTR(keys
, S_IRUGO
, gpio_keys_show_keys
, NULL
);
273 static DEVICE_ATTR(switches
, S_IRUGO
, gpio_keys_show_switches
, NULL
);
275 #define ATTR_STORE_FN(name, type) \
276 static ssize_t gpio_keys_store_##name(struct device *dev, \
277 struct device_attribute *attr, \
281 struct platform_device *pdev = to_platform_device(dev); \
282 struct gpio_keys_drvdata *ddata = platform_get_drvdata(pdev); \
285 error = gpio_keys_attr_store_helper(ddata, buf, type); \
292 ATTR_STORE_FN(disabled_keys
, EV_KEY
);
293 ATTR_STORE_FN(disabled_switches
, EV_SW
);
298 * /sys/devices/platform/gpio-keys/disabled_keys [rw]
299 * /sys/devices/platform/gpio-keys/disables_switches [rw]
301 static DEVICE_ATTR(disabled_keys
, S_IWUSR
| S_IRUGO
,
302 gpio_keys_show_disabled_keys
,
303 gpio_keys_store_disabled_keys
);
304 static DEVICE_ATTR(disabled_switches
, S_IWUSR
| S_IRUGO
,
305 gpio_keys_show_disabled_switches
,
306 gpio_keys_store_disabled_switches
);
308 static struct attribute
*gpio_keys_attrs
[] = {
310 &dev_attr_switches
.attr
,
311 &dev_attr_disabled_keys
.attr
,
312 &dev_attr_disabled_switches
.attr
,
316 static struct attribute_group gpio_keys_attr_group
= {
317 .attrs
= gpio_keys_attrs
,
320 static void gpio_keys_report_event(struct gpio_button_data
*bdata
)
322 struct gpio_keys_button
*button
= bdata
->button
;
323 struct input_dev
*input
= bdata
->input
;
324 unsigned int type
= button
->type
?: EV_KEY
;
325 int state
= (gpio_get_value_cansleep(button
->gpio
) ? 1 : 0) ^ button
->active_low
;
327 if (type
== EV_ABS
) {
329 input_event(input
, type
, button
->code
, button
->value
);
331 input_event(input
, type
, button
->code
, !!state
);
336 static void gpio_keys_work_func(struct work_struct
*work
)
338 struct gpio_button_data
*bdata
=
339 container_of(work
, struct gpio_button_data
, work
);
341 gpio_keys_report_event(bdata
);
344 static void gpio_keys_timer(unsigned long _data
)
346 struct gpio_button_data
*data
= (struct gpio_button_data
*)_data
;
348 schedule_work(&data
->work
);
351 static irqreturn_t
gpio_keys_isr(int irq
, void *dev_id
)
353 struct gpio_button_data
*bdata
= dev_id
;
354 struct gpio_keys_button
*button
= bdata
->button
;
356 BUG_ON(irq
!= gpio_to_irq(button
->gpio
));
358 if (bdata
->timer_debounce
)
359 mod_timer(&bdata
->timer
,
360 jiffies
+ msecs_to_jiffies(bdata
->timer_debounce
));
362 schedule_work(&bdata
->work
);
367 static int __devinit
gpio_keys_setup_key(struct platform_device
*pdev
,
368 struct gpio_button_data
*bdata
,
369 struct gpio_keys_button
*button
)
371 const char *desc
= button
->desc
? button
->desc
: "gpio_keys";
372 struct device
*dev
= &pdev
->dev
;
373 unsigned long irqflags
;
376 setup_timer(&bdata
->timer
, gpio_keys_timer
, (unsigned long)bdata
);
377 INIT_WORK(&bdata
->work
, gpio_keys_work_func
);
379 error
= gpio_request(button
->gpio
, desc
);
381 dev_err(dev
, "failed to request GPIO %d, error %d\n",
382 button
->gpio
, error
);
386 error
= gpio_direction_input(button
->gpio
);
388 dev_err(dev
, "failed to configure"
389 " direction for GPIO %d, error %d\n",
390 button
->gpio
, error
);
394 if (button
->debounce_interval
) {
395 error
= gpio_set_debounce(button
->gpio
,
396 button
->debounce_interval
* 1000);
397 /* use timer if gpiolib doesn't provide debounce */
399 bdata
->timer_debounce
= button
->debounce_interval
;
402 irq
= gpio_to_irq(button
->gpio
);
405 dev_err(dev
, "Unable to get irq number for GPIO %d, error %d\n",
406 button
->gpio
, error
);
410 irqflags
= IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
;
412 * If platform has specified that the button can be disabled,
413 * we don't want it to share the interrupt line.
415 if (!button
->can_disable
)
416 irqflags
|= IRQF_SHARED
;
418 error
= request_any_context_irq(irq
, gpio_keys_isr
, irqflags
, desc
, bdata
);
420 dev_err(dev
, "Unable to claim irq %d; error %d\n",
428 gpio_free(button
->gpio
);
433 static int gpio_keys_open(struct input_dev
*input
)
435 struct gpio_keys_drvdata
*ddata
= input_get_drvdata(input
);
437 return ddata
->enable
? ddata
->enable(input
->dev
.parent
) : 0;
440 static void gpio_keys_close(struct input_dev
*input
)
442 struct gpio_keys_drvdata
*ddata
= input_get_drvdata(input
);
445 ddata
->disable(input
->dev
.parent
);
448 static int __devinit
gpio_keys_probe(struct platform_device
*pdev
)
450 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
451 struct gpio_keys_drvdata
*ddata
;
452 struct device
*dev
= &pdev
->dev
;
453 struct input_dev
*input
;
457 ddata
= kzalloc(sizeof(struct gpio_keys_drvdata
) +
458 pdata
->nbuttons
* sizeof(struct gpio_button_data
),
460 input
= input_allocate_device();
461 if (!ddata
|| !input
) {
462 dev_err(dev
, "failed to allocate state\n");
467 ddata
->input
= input
;
468 ddata
->n_buttons
= pdata
->nbuttons
;
469 ddata
->enable
= pdata
->enable
;
470 ddata
->disable
= pdata
->disable
;
471 mutex_init(&ddata
->disable_lock
);
473 platform_set_drvdata(pdev
, ddata
);
474 input_set_drvdata(input
, ddata
);
476 input
->name
= pdata
->name
? : pdev
->name
;
477 input
->phys
= "gpio-keys/input0";
478 input
->dev
.parent
= &pdev
->dev
;
479 input
->open
= gpio_keys_open
;
480 input
->close
= gpio_keys_close
;
482 input
->id
.bustype
= BUS_HOST
;
483 input
->id
.vendor
= 0x0001;
484 input
->id
.product
= 0x0001;
485 input
->id
.version
= 0x0100;
487 /* Enable auto repeat feature of Linux input subsystem */
489 __set_bit(EV_REP
, input
->evbit
);
491 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
492 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
493 struct gpio_button_data
*bdata
= &ddata
->data
[i
];
494 unsigned int type
= button
->type
?: EV_KEY
;
496 bdata
->input
= input
;
497 bdata
->button
= button
;
499 error
= gpio_keys_setup_key(pdev
, bdata
, button
);
506 input_set_capability(input
, type
, button
->code
);
509 error
= sysfs_create_group(&pdev
->dev
.kobj
, &gpio_keys_attr_group
);
511 dev_err(dev
, "Unable to export keys/switches, error: %d\n",
516 error
= input_register_device(input
);
518 dev_err(dev
, "Unable to register input device, error: %d\n",
523 /* get current state of buttons */
524 for (i
= 0; i
< pdata
->nbuttons
; i
++)
525 gpio_keys_report_event(&ddata
->data
[i
]);
528 device_init_wakeup(&pdev
->dev
, wakeup
);
533 sysfs_remove_group(&pdev
->dev
.kobj
, &gpio_keys_attr_group
);
536 free_irq(gpio_to_irq(pdata
->buttons
[i
].gpio
), &ddata
->data
[i
]);
537 if (ddata
->data
[i
].timer_debounce
)
538 del_timer_sync(&ddata
->data
[i
].timer
);
539 cancel_work_sync(&ddata
->data
[i
].work
);
540 gpio_free(pdata
->buttons
[i
].gpio
);
543 platform_set_drvdata(pdev
, NULL
);
545 input_free_device(input
);
551 static int __devexit
gpio_keys_remove(struct platform_device
*pdev
)
553 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
554 struct gpio_keys_drvdata
*ddata
= platform_get_drvdata(pdev
);
555 struct input_dev
*input
= ddata
->input
;
558 sysfs_remove_group(&pdev
->dev
.kobj
, &gpio_keys_attr_group
);
560 device_init_wakeup(&pdev
->dev
, 0);
562 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
563 int irq
= gpio_to_irq(pdata
->buttons
[i
].gpio
);
564 free_irq(irq
, &ddata
->data
[i
]);
565 if (ddata
->data
[i
].timer_debounce
)
566 del_timer_sync(&ddata
->data
[i
].timer
);
567 cancel_work_sync(&ddata
->data
[i
].work
);
568 gpio_free(pdata
->buttons
[i
].gpio
);
571 input_unregister_device(input
);
578 static int gpio_keys_suspend(struct device
*dev
)
580 struct platform_device
*pdev
= to_platform_device(dev
);
581 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
584 if (device_may_wakeup(&pdev
->dev
)) {
585 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
586 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
587 if (button
->wakeup
) {
588 int irq
= gpio_to_irq(button
->gpio
);
589 enable_irq_wake(irq
);
597 static int gpio_keys_resume(struct device
*dev
)
599 struct platform_device
*pdev
= to_platform_device(dev
);
600 struct gpio_keys_drvdata
*ddata
= platform_get_drvdata(pdev
);
601 struct gpio_keys_platform_data
*pdata
= pdev
->dev
.platform_data
;
604 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
606 struct gpio_keys_button
*button
= &pdata
->buttons
[i
];
607 if (button
->wakeup
&& device_may_wakeup(&pdev
->dev
)) {
608 int irq
= gpio_to_irq(button
->gpio
);
609 disable_irq_wake(irq
);
612 gpio_keys_report_event(&ddata
->data
[i
]);
614 input_sync(ddata
->input
);
619 static const struct dev_pm_ops gpio_keys_pm_ops
= {
620 .suspend
= gpio_keys_suspend
,
621 .resume
= gpio_keys_resume
,
625 static struct platform_driver gpio_keys_device_driver
= {
626 .probe
= gpio_keys_probe
,
627 .remove
= __devexit_p(gpio_keys_remove
),
630 .owner
= THIS_MODULE
,
632 .pm
= &gpio_keys_pm_ops
,
637 static int __init
gpio_keys_init(void)
639 return platform_driver_register(&gpio_keys_device_driver
);
642 static void __exit
gpio_keys_exit(void)
644 platform_driver_unregister(&gpio_keys_device_driver
);
647 module_init(gpio_keys_init
);
648 module_exit(gpio_keys_exit
);
650 MODULE_LICENSE("GPL");
651 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
652 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");
653 MODULE_ALIAS("platform:gpio-keys");