diff options
Diffstat (limited to 'drivers/input/keyboard/gpio_keys.c')
-rw-r--r-- | drivers/input/keyboard/gpio_keys.c | 259 |
1 files changed, 169 insertions, 90 deletions
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c index ed1ed469d085..62bfce468f9f 100644 --- a/drivers/input/keyboard/gpio_keys.c +++ b/drivers/input/keyboard/gpio_keys.c | |||
@@ -28,14 +28,18 @@ | |||
28 | #include <linux/gpio.h> | 28 | #include <linux/gpio.h> |
29 | #include <linux/of_platform.h> | 29 | #include <linux/of_platform.h> |
30 | #include <linux/of_gpio.h> | 30 | #include <linux/of_gpio.h> |
31 | #include <linux/spinlock.h> | ||
31 | 32 | ||
32 | struct gpio_button_data { | 33 | struct gpio_button_data { |
33 | struct gpio_keys_button *button; | 34 | const struct gpio_keys_button *button; |
34 | struct input_dev *input; | 35 | struct input_dev *input; |
35 | struct timer_list timer; | 36 | struct timer_list timer; |
36 | struct work_struct work; | 37 | struct work_struct work; |
37 | int timer_debounce; /* in msecs */ | 38 | unsigned int timer_debounce; /* in msecs */ |
39 | unsigned int irq; | ||
40 | spinlock_t lock; | ||
38 | bool disabled; | 41 | bool disabled; |
42 | bool key_pressed; | ||
39 | }; | 43 | }; |
40 | 44 | ||
41 | struct gpio_keys_drvdata { | 45 | struct gpio_keys_drvdata { |
@@ -114,7 +118,7 @@ static void gpio_keys_disable_button(struct gpio_button_data *bdata) | |||
114 | /* | 118 | /* |
115 | * Disable IRQ and possible debouncing timer. | 119 | * Disable IRQ and possible debouncing timer. |
116 | */ | 120 | */ |
117 | disable_irq(gpio_to_irq(bdata->button->gpio)); | 121 | disable_irq(bdata->irq); |
118 | if (bdata->timer_debounce) | 122 | if (bdata->timer_debounce) |
119 | del_timer_sync(&bdata->timer); | 123 | del_timer_sync(&bdata->timer); |
120 | 124 | ||
@@ -135,7 +139,7 @@ static void gpio_keys_disable_button(struct gpio_button_data *bdata) | |||
135 | static void gpio_keys_enable_button(struct gpio_button_data *bdata) | 139 | static void gpio_keys_enable_button(struct gpio_button_data *bdata) |
136 | { | 140 | { |
137 | if (bdata->disabled) { | 141 | if (bdata->disabled) { |
138 | enable_irq(gpio_to_irq(bdata->button->gpio)); | 142 | enable_irq(bdata->irq); |
139 | bdata->disabled = false; | 143 | bdata->disabled = false; |
140 | } | 144 | } |
141 | } | 145 | } |
@@ -195,7 +199,7 @@ static ssize_t gpio_keys_attr_show_helper(struct gpio_keys_drvdata *ddata, | |||
195 | * @type: button type (%EV_KEY, %EV_SW) | 199 | * @type: button type (%EV_KEY, %EV_SW) |
196 | * | 200 | * |
197 | * This function parses stringified bitmap from @buf and disables/enables | 201 | * This function parses stringified bitmap from @buf and disables/enables |
198 | * GPIO buttons accordinly. Returns 0 on success and negative error | 202 | * GPIO buttons accordingly. Returns 0 on success and negative error |
199 | * on failure. | 203 | * on failure. |
200 | */ | 204 | */ |
201 | static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, | 205 | static ssize_t gpio_keys_attr_store_helper(struct gpio_keys_drvdata *ddata, |
@@ -320,9 +324,9 @@ static struct attribute_group gpio_keys_attr_group = { | |||
320 | .attrs = gpio_keys_attrs, | 324 | .attrs = gpio_keys_attrs, |
321 | }; | 325 | }; |
322 | 326 | ||
323 | static void gpio_keys_report_event(struct gpio_button_data *bdata) | 327 | static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata) |
324 | { | 328 | { |
325 | struct gpio_keys_button *button = bdata->button; | 329 | const struct gpio_keys_button *button = bdata->button; |
326 | struct input_dev *input = bdata->input; | 330 | struct input_dev *input = bdata->input; |
327 | unsigned int type = button->type ?: EV_KEY; | 331 | unsigned int type = button->type ?: EV_KEY; |
328 | int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low; | 332 | int state = (gpio_get_value_cansleep(button->gpio) ? 1 : 0) ^ button->active_low; |
@@ -336,27 +340,26 @@ static void gpio_keys_report_event(struct gpio_button_data *bdata) | |||
336 | input_sync(input); | 340 | input_sync(input); |
337 | } | 341 | } |
338 | 342 | ||
339 | static void gpio_keys_work_func(struct work_struct *work) | 343 | static void gpio_keys_gpio_work_func(struct work_struct *work) |
340 | { | 344 | { |
341 | struct gpio_button_data *bdata = | 345 | struct gpio_button_data *bdata = |
342 | container_of(work, struct gpio_button_data, work); | 346 | container_of(work, struct gpio_button_data, work); |
343 | 347 | ||
344 | gpio_keys_report_event(bdata); | 348 | gpio_keys_gpio_report_event(bdata); |
345 | } | 349 | } |
346 | 350 | ||
347 | static void gpio_keys_timer(unsigned long _data) | 351 | static void gpio_keys_gpio_timer(unsigned long _data) |
348 | { | 352 | { |
349 | struct gpio_button_data *data = (struct gpio_button_data *)_data; | 353 | struct gpio_button_data *bdata = (struct gpio_button_data *)_data; |
350 | 354 | ||
351 | schedule_work(&data->work); | 355 | schedule_work(&bdata->work); |
352 | } | 356 | } |
353 | 357 | ||
354 | static irqreturn_t gpio_keys_isr(int irq, void *dev_id) | 358 | static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id) |
355 | { | 359 | { |
356 | struct gpio_button_data *bdata = dev_id; | 360 | struct gpio_button_data *bdata = dev_id; |
357 | struct gpio_keys_button *button = bdata->button; | ||
358 | 361 | ||
359 | BUG_ON(irq != gpio_to_irq(button->gpio)); | 362 | BUG_ON(irq != bdata->irq); |
360 | 363 | ||
361 | if (bdata->timer_debounce) | 364 | if (bdata->timer_debounce) |
362 | mod_timer(&bdata->timer, | 365 | mod_timer(&bdata->timer, |
@@ -367,50 +370,133 @@ static irqreturn_t gpio_keys_isr(int irq, void *dev_id) | |||
367 | return IRQ_HANDLED; | 370 | return IRQ_HANDLED; |
368 | } | 371 | } |
369 | 372 | ||
373 | static void gpio_keys_irq_timer(unsigned long _data) | ||
374 | { | ||
375 | struct gpio_button_data *bdata = (struct gpio_button_data *)_data; | ||
376 | struct input_dev *input = bdata->input; | ||
377 | unsigned long flags; | ||
378 | |||
379 | spin_lock_irqsave(&bdata->lock, flags); | ||
380 | if (bdata->key_pressed) { | ||
381 | input_event(input, EV_KEY, bdata->button->code, 0); | ||
382 | input_sync(input); | ||
383 | bdata->key_pressed = false; | ||
384 | } | ||
385 | spin_unlock_irqrestore(&bdata->lock, flags); | ||
386 | } | ||
387 | |||
388 | static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id) | ||
389 | { | ||
390 | struct gpio_button_data *bdata = dev_id; | ||
391 | const struct gpio_keys_button *button = bdata->button; | ||
392 | struct input_dev *input = bdata->input; | ||
393 | unsigned long flags; | ||
394 | |||
395 | BUG_ON(irq != bdata->irq); | ||
396 | |||
397 | spin_lock_irqsave(&bdata->lock, flags); | ||
398 | |||
399 | if (!bdata->key_pressed) { | ||
400 | input_event(input, EV_KEY, button->code, 1); | ||
401 | input_sync(input); | ||
402 | |||
403 | if (!bdata->timer_debounce) { | ||
404 | input_event(input, EV_KEY, button->code, 0); | ||
405 | input_sync(input); | ||
406 | goto out; | ||
407 | } | ||
408 | |||
409 | bdata->key_pressed = true; | ||
410 | } | ||
411 | |||
412 | if (bdata->timer_debounce) | ||
413 | mod_timer(&bdata->timer, | ||
414 | jiffies + msecs_to_jiffies(bdata->timer_debounce)); | ||
415 | out: | ||
416 | spin_unlock_irqrestore(&bdata->lock, flags); | ||
417 | return IRQ_HANDLED; | ||
418 | } | ||
419 | |||
370 | static int __devinit gpio_keys_setup_key(struct platform_device *pdev, | 420 | static int __devinit gpio_keys_setup_key(struct platform_device *pdev, |
421 | struct input_dev *input, | ||
371 | struct gpio_button_data *bdata, | 422 | struct gpio_button_data *bdata, |
372 | struct gpio_keys_button *button) | 423 | const struct gpio_keys_button *button) |
373 | { | 424 | { |
374 | const char *desc = button->desc ? button->desc : "gpio_keys"; | 425 | const char *desc = button->desc ? button->desc : "gpio_keys"; |
375 | struct device *dev = &pdev->dev; | 426 | struct device *dev = &pdev->dev; |
427 | irq_handler_t isr; | ||
376 | unsigned long irqflags; | 428 | unsigned long irqflags; |
377 | int irq, error; | 429 | int irq, error; |
378 | 430 | ||
379 | setup_timer(&bdata->timer, gpio_keys_timer, (unsigned long)bdata); | 431 | bdata->input = input; |
380 | INIT_WORK(&bdata->work, gpio_keys_work_func); | 432 | bdata->button = button; |
433 | spin_lock_init(&bdata->lock); | ||
381 | 434 | ||
382 | error = gpio_request(button->gpio, desc); | 435 | if (gpio_is_valid(button->gpio)) { |
383 | if (error < 0) { | ||
384 | dev_err(dev, "failed to request GPIO %d, error %d\n", | ||
385 | button->gpio, error); | ||
386 | goto fail2; | ||
387 | } | ||
388 | 436 | ||
389 | error = gpio_direction_input(button->gpio); | 437 | error = gpio_request(button->gpio, desc); |
390 | if (error < 0) { | 438 | if (error < 0) { |
391 | dev_err(dev, "failed to configure" | 439 | dev_err(dev, "Failed to request GPIO %d, error %d\n", |
392 | " direction for GPIO %d, error %d\n", | 440 | button->gpio, error); |
393 | button->gpio, error); | 441 | return error; |
394 | goto fail3; | 442 | } |
395 | } | ||
396 | 443 | ||
397 | if (button->debounce_interval) { | 444 | error = gpio_direction_input(button->gpio); |
398 | error = gpio_set_debounce(button->gpio, | 445 | if (error < 0) { |
399 | button->debounce_interval * 1000); | 446 | dev_err(dev, |
400 | /* use timer if gpiolib doesn't provide debounce */ | 447 | "Failed to configure direction for GPIO %d, error %d\n", |
401 | if (error < 0) | 448 | button->gpio, error); |
402 | bdata->timer_debounce = button->debounce_interval; | 449 | goto fail; |
403 | } | 450 | } |
404 | 451 | ||
405 | irq = gpio_to_irq(button->gpio); | 452 | if (button->debounce_interval) { |
406 | if (irq < 0) { | 453 | error = gpio_set_debounce(button->gpio, |
407 | error = irq; | 454 | button->debounce_interval * 1000); |
408 | dev_err(dev, "Unable to get irq number for GPIO %d, error %d\n", | 455 | /* use timer if gpiolib doesn't provide debounce */ |
409 | button->gpio, error); | 456 | if (error < 0) |
410 | goto fail3; | 457 | bdata->timer_debounce = |
458 | button->debounce_interval; | ||
459 | } | ||
460 | |||
461 | irq = gpio_to_irq(button->gpio); | ||
462 | if (irq < 0) { | ||
463 | error = irq; | ||
464 | dev_err(dev, | ||
465 | "Unable to get irq number for GPIO %d, error %d\n", | ||
466 | button->gpio, error); | ||
467 | goto fail; | ||
468 | } | ||
469 | bdata->irq = irq; | ||
470 | |||
471 | INIT_WORK(&bdata->work, gpio_keys_gpio_work_func); | ||
472 | setup_timer(&bdata->timer, | ||
473 | gpio_keys_gpio_timer, (unsigned long)bdata); | ||
474 | |||
475 | isr = gpio_keys_gpio_isr; | ||
476 | irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING; | ||
477 | |||
478 | } else { | ||
479 | if (!button->irq) { | ||
480 | dev_err(dev, "No IRQ specified\n"); | ||
481 | return -EINVAL; | ||
482 | } | ||
483 | bdata->irq = button->irq; | ||
484 | |||
485 | if (button->type && button->type != EV_KEY) { | ||
486 | dev_err(dev, "Only EV_KEY allowed for IRQ buttons.\n"); | ||
487 | return -EINVAL; | ||
488 | } | ||
489 | |||
490 | bdata->timer_debounce = button->debounce_interval; | ||
491 | setup_timer(&bdata->timer, | ||
492 | gpio_keys_irq_timer, (unsigned long)bdata); | ||
493 | |||
494 | isr = gpio_keys_irq_isr; | ||
495 | irqflags = 0; | ||
411 | } | 496 | } |
412 | 497 | ||
413 | irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING; | 498 | input_set_capability(input, button->type ?: EV_KEY, button->code); |
499 | |||
414 | /* | 500 | /* |
415 | * If platform has specified that the button can be disabled, | 501 | * If platform has specified that the button can be disabled, |
416 | * we don't want it to share the interrupt line. | 502 | * we don't want it to share the interrupt line. |
@@ -418,18 +504,19 @@ static int __devinit gpio_keys_setup_key(struct platform_device *pdev, | |||
418 | if (!button->can_disable) | 504 | if (!button->can_disable) |
419 | irqflags |= IRQF_SHARED; | 505 | irqflags |= IRQF_SHARED; |
420 | 506 | ||
421 | error = request_threaded_irq(irq, NULL, gpio_keys_isr, irqflags, desc, bdata); | 507 | error = request_any_context_irq(bdata->irq, isr, irqflags, desc, bdata); |
422 | if (error < 0) { | 508 | if (error < 0) { |
423 | dev_err(dev, "Unable to claim irq %d; error %d\n", | 509 | dev_err(dev, "Unable to claim irq %d; error %d\n", |
424 | irq, error); | 510 | bdata->irq, error); |
425 | goto fail3; | 511 | goto fail; |
426 | } | 512 | } |
427 | 513 | ||
428 | return 0; | 514 | return 0; |
429 | 515 | ||
430 | fail3: | 516 | fail: |
431 | gpio_free(button->gpio); | 517 | if (gpio_is_valid(button->gpio)) |
432 | fail2: | 518 | gpio_free(button->gpio); |
519 | |||
433 | return error; | 520 | return error; |
434 | } | 521 | } |
435 | 522 | ||
@@ -547,9 +634,19 @@ static int gpio_keys_get_devtree_pdata(struct device *dev, | |||
547 | 634 | ||
548 | #endif | 635 | #endif |
549 | 636 | ||
637 | static void gpio_remove_key(struct gpio_button_data *bdata) | ||
638 | { | ||
639 | free_irq(bdata->irq, bdata); | ||
640 | if (bdata->timer_debounce) | ||
641 | del_timer_sync(&bdata->timer); | ||
642 | cancel_work_sync(&bdata->work); | ||
643 | if (gpio_is_valid(bdata->button->gpio)) | ||
644 | gpio_free(bdata->button->gpio); | ||
645 | } | ||
646 | |||
550 | static int __devinit gpio_keys_probe(struct platform_device *pdev) | 647 | static int __devinit gpio_keys_probe(struct platform_device *pdev) |
551 | { | 648 | { |
552 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; | 649 | const struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
553 | struct gpio_keys_drvdata *ddata; | 650 | struct gpio_keys_drvdata *ddata; |
554 | struct device *dev = &pdev->dev; | 651 | struct device *dev = &pdev->dev; |
555 | struct gpio_keys_platform_data alt_pdata; | 652 | struct gpio_keys_platform_data alt_pdata; |
@@ -599,21 +696,15 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev) | |||
599 | __set_bit(EV_REP, input->evbit); | 696 | __set_bit(EV_REP, input->evbit); |
600 | 697 | ||
601 | for (i = 0; i < pdata->nbuttons; i++) { | 698 | for (i = 0; i < pdata->nbuttons; i++) { |
602 | struct gpio_keys_button *button = &pdata->buttons[i]; | 699 | const struct gpio_keys_button *button = &pdata->buttons[i]; |
603 | struct gpio_button_data *bdata = &ddata->data[i]; | 700 | struct gpio_button_data *bdata = &ddata->data[i]; |
604 | unsigned int type = button->type ?: EV_KEY; | ||
605 | |||
606 | bdata->input = input; | ||
607 | bdata->button = button; | ||
608 | 701 | ||
609 | error = gpio_keys_setup_key(pdev, bdata, button); | 702 | error = gpio_keys_setup_key(pdev, input, bdata, button); |
610 | if (error) | 703 | if (error) |
611 | goto fail2; | 704 | goto fail2; |
612 | 705 | ||
613 | if (button->wakeup) | 706 | if (button->wakeup) |
614 | wakeup = 1; | 707 | wakeup = 1; |
615 | |||
616 | input_set_capability(input, type, button->code); | ||
617 | } | 708 | } |
618 | 709 | ||
619 | error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group); | 710 | error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
@@ -630,9 +721,12 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev) | |||
630 | goto fail3; | 721 | goto fail3; |
631 | } | 722 | } |
632 | 723 | ||
633 | /* get current state of buttons */ | 724 | /* get current state of buttons that are connected to GPIOs */ |
634 | for (i = 0; i < pdata->nbuttons; i++) | 725 | for (i = 0; i < pdata->nbuttons; i++) { |
635 | gpio_keys_report_event(&ddata->data[i]); | 726 | struct gpio_button_data *bdata = &ddata->data[i]; |
727 | if (gpio_is_valid(bdata->button->gpio)) | ||
728 | gpio_keys_gpio_report_event(bdata); | ||
729 | } | ||
636 | input_sync(input); | 730 | input_sync(input); |
637 | 731 | ||
638 | device_init_wakeup(&pdev->dev, wakeup); | 732 | device_init_wakeup(&pdev->dev, wakeup); |
@@ -642,13 +736,8 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev) | |||
642 | fail3: | 736 | fail3: |
643 | sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group); | 737 | sysfs_remove_group(&pdev->dev.kobj, &gpio_keys_attr_group); |
644 | fail2: | 738 | fail2: |
645 | while (--i >= 0) { | 739 | while (--i >= 0) |
646 | free_irq(gpio_to_irq(pdata->buttons[i].gpio), &ddata->data[i]); | 740 | gpio_remove_key(&ddata->data[i]); |
647 | if (ddata->data[i].timer_debounce) | ||
648 | del_timer_sync(&ddata->data[i].timer); | ||
649 | cancel_work_sync(&ddata->data[i].work); | ||
650 | gpio_free(pdata->buttons[i].gpio); | ||
651 | } | ||
652 | 741 | ||
653 | platform_set_drvdata(pdev, NULL); | 742 | platform_set_drvdata(pdev, NULL); |
654 | fail1: | 743 | fail1: |
@@ -671,14 +760,8 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev) | |||
671 | 760 | ||
672 | device_init_wakeup(&pdev->dev, 0); | 761 | device_init_wakeup(&pdev->dev, 0); |
673 | 762 | ||
674 | for (i = 0; i < ddata->n_buttons; i++) { | 763 | for (i = 0; i < ddata->n_buttons; i++) |
675 | int irq = gpio_to_irq(ddata->data[i].button->gpio); | 764 | gpio_remove_key(&ddata->data[i]); |
676 | free_irq(irq, &ddata->data[i]); | ||
677 | if (ddata->data[i].timer_debounce) | ||
678 | del_timer_sync(&ddata->data[i].timer); | ||
679 | cancel_work_sync(&ddata->data[i].work); | ||
680 | gpio_free(ddata->data[i].button->gpio); | ||
681 | } | ||
682 | 765 | ||
683 | input_unregister_device(input); | 766 | input_unregister_device(input); |
684 | 767 | ||
@@ -703,11 +786,9 @@ static int gpio_keys_suspend(struct device *dev) | |||
703 | 786 | ||
704 | if (device_may_wakeup(dev)) { | 787 | if (device_may_wakeup(dev)) { |
705 | for (i = 0; i < ddata->n_buttons; i++) { | 788 | for (i = 0; i < ddata->n_buttons; i++) { |
706 | struct gpio_keys_button *button = ddata->data[i].button; | 789 | struct gpio_button_data *bdata = &ddata->data[i]; |
707 | if (button->wakeup) { | 790 | if (bdata->button->wakeup) |
708 | int irq = gpio_to_irq(button->gpio); | 791 | enable_irq_wake(bdata->irq); |
709 | enable_irq_wake(irq); | ||
710 | } | ||
711 | } | 792 | } |
712 | } | 793 | } |
713 | 794 | ||
@@ -720,14 +801,12 @@ static int gpio_keys_resume(struct device *dev) | |||
720 | int i; | 801 | int i; |
721 | 802 | ||
722 | for (i = 0; i < ddata->n_buttons; i++) { | 803 | for (i = 0; i < ddata->n_buttons; i++) { |
804 | struct gpio_button_data *bdata = &ddata->data[i]; | ||
805 | if (bdata->button->wakeup && device_may_wakeup(dev)) | ||
806 | disable_irq_wake(bdata->irq); | ||
723 | 807 | ||
724 | struct gpio_keys_button *button = ddata->data[i].button; | 808 | if (gpio_is_valid(bdata->button->gpio)) |
725 | if (button->wakeup && device_may_wakeup(dev)) { | 809 | gpio_keys_gpio_report_event(bdata); |
726 | int irq = gpio_to_irq(button->gpio); | ||
727 | disable_irq_wake(irq); | ||
728 | } | ||
729 | |||
730 | gpio_keys_report_event(&ddata->data[i]); | ||
731 | } | 810 | } |
732 | input_sync(ddata->input); | 811 | input_sync(ddata->input); |
733 | 812 | ||