Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Input: gpio_keys - switch to using generic device properties

Make use of the device property API in this driver so that both OF based
systems and ACPI based systems can use this driver.

Suggested-by: Geert Uytterhoeven <geert+renesas@glider.be>
Suggested-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Tested-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+69 -72
+69 -72
drivers/input/keyboard/gpio_keys.c
··· 28 28 #include <linux/gpio.h> 29 29 #include <linux/gpio/consumer.h> 30 30 #include <linux/of.h> 31 - #include <linux/of_platform.h> 32 - #include <linux/of_gpio.h> 33 31 #include <linux/of_irq.h> 34 32 #include <linux/spinlock.h> 35 33 ··· 466 468 static int gpio_keys_setup_key(struct platform_device *pdev, 467 469 struct input_dev *input, 468 470 struct gpio_button_data *bdata, 469 - const struct gpio_keys_button *button) 471 + const struct gpio_keys_button *button, 472 + struct fwnode_handle *child) 470 473 { 471 474 const char *desc = button->desc ? button->desc : "gpio_keys"; 472 475 struct device *dev = &pdev->dev; ··· 480 481 bdata->button = button; 481 482 spin_lock_init(&bdata->lock); 482 483 483 - /* 484 - * Legacy GPIO number, so request the GPIO here and 485 - * convert it to descriptor. 486 - */ 487 - if (gpio_is_valid(button->gpio)) { 484 + if (child) { 485 + bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child); 486 + if (IS_ERR(bdata->gpiod)) { 487 + error = PTR_ERR(bdata->gpiod); 488 + if (error == -ENOENT) { 489 + /* 490 + * GPIO is optional, we may be dealing with 491 + * purely interrupt-driven setup. 492 + */ 493 + bdata->gpiod = NULL; 494 + } else { 495 + if (error != -EPROBE_DEFER) 496 + dev_err(dev, "failed to get gpio: %d\n", 497 + error); 498 + return error; 499 + } 500 + } 501 + } else if (gpio_is_valid(button->gpio)) { 502 + /* 503 + * Legacy GPIO number, so request the GPIO here and 504 + * convert it to descriptor. 505 + */ 488 506 unsigned flags = GPIOF_IN; 489 507 490 508 if (button->active_low) ··· 518 502 bdata->gpiod = gpio_to_desc(button->gpio); 519 503 if (!bdata->gpiod) 520 504 return -EINVAL; 505 + } 521 506 507 + if (bdata->gpiod) { 522 508 if (button->debounce_interval) { 523 509 error = gpiod_set_debounce(bdata->gpiod, 524 510 button->debounce_interval * 1000); ··· 551 533 552 534 } else { 553 535 if (!button->irq) { 554 - dev_err(dev, "No IRQ specified\n"); 536 + dev_err(dev, "Found button without gpio or irq\n"); 555 537 return -EINVAL; 556 538 } 539 + 557 540 bdata->irq = button->irq; 558 541 559 542 if (button->type && button->type != EV_KEY) { ··· 646 627 * Handlers for alternative sources of platform_data 647 628 */ 648 629 649 - #ifdef CONFIG_OF 650 630 /* 651 - * Translate OpenFirmware node properties into platform_data 631 + * Translate properties into platform_data 652 632 */ 653 633 static struct gpio_keys_platform_data * 654 634 gpio_keys_get_devtree_pdata(struct device *dev) 655 635 { 656 - struct device_node *node, *pp; 657 636 struct gpio_keys_platform_data *pdata; 658 637 struct gpio_keys_button *button; 659 - int error; 638 + struct fwnode_handle *child; 660 639 int nbuttons; 661 640 662 - node = dev->of_node; 663 - if (!node) 664 - return ERR_PTR(-ENODEV); 665 - 666 - nbuttons = of_get_available_child_count(node); 641 + nbuttons = device_get_child_node_count(dev); 667 642 if (nbuttons == 0) 668 643 return ERR_PTR(-ENODEV); 669 644 ··· 672 659 pdata->buttons = button; 673 660 pdata->nbuttons = nbuttons; 674 661 675 - pdata->rep = !!of_get_property(node, "autorepeat", NULL); 662 + pdata->rep = device_property_read_bool(dev, "autorepeat"); 676 663 677 - of_property_read_string(node, "label", &pdata->name); 664 + device_property_read_string(dev, "label", &pdata->name); 678 665 679 - for_each_available_child_of_node(node, pp) { 680 - enum of_gpio_flags flags; 666 + device_for_each_child_node(dev, child) { 667 + if (is_of_node(child)) 668 + button->irq = 669 + irq_of_parse_and_map(to_of_node(child), 0); 681 670 682 - button->gpio = of_get_gpio_flags(pp, 0, &flags); 683 - if (button->gpio < 0) { 684 - error = button->gpio; 685 - if (error != -ENOENT) { 686 - if (error != -EPROBE_DEFER) 687 - dev_err(dev, 688 - "Failed to get gpio flags, error: %d\n", 689 - error); 690 - of_node_put(pp); 691 - return ERR_PTR(error); 692 - } 693 - } else { 694 - button->active_low = flags & OF_GPIO_ACTIVE_LOW; 695 - } 696 - 697 - button->irq = irq_of_parse_and_map(pp, 0); 698 - 699 - if (!gpio_is_valid(button->gpio) && !button->irq) { 700 - dev_err(dev, "Found button without gpios or irqs\n"); 701 - of_node_put(pp); 671 + if (fwnode_property_read_u32(child, "linux,code", 672 + &button->code)) { 673 + dev_err(dev, "Button without keycode\n"); 674 + fwnode_handle_put(child); 702 675 return ERR_PTR(-EINVAL); 703 676 } 704 677 705 - if (of_property_read_u32(pp, "linux,code", &button->code)) { 706 - dev_err(dev, "Button without keycode: 0x%x\n", 707 - button->gpio); 708 - of_node_put(pp); 709 - return ERR_PTR(-EINVAL); 710 - } 678 + fwnode_property_read_string(child, "label", &button->desc); 711 679 712 - button->desc = of_get_property(pp, "label", NULL); 713 - 714 - if (of_property_read_u32(pp, "linux,input-type", &button->type)) 680 + if (fwnode_property_read_u32(child, "linux,input-type", 681 + &button->type)) 715 682 button->type = EV_KEY; 716 683 717 - button->wakeup = of_property_read_bool(pp, "wakeup-source") || 718 - /* legacy name */ 719 - of_property_read_bool(pp, "gpio-key,wakeup"); 684 + button->wakeup = 685 + fwnode_property_read_bool(child, "wakeup-source") || 686 + /* legacy name */ 687 + fwnode_property_read_bool(child, "gpio-key,wakeup"); 720 688 721 - button->can_disable = !!of_get_property(pp, "linux,can-disable", NULL); 689 + button->can_disable = 690 + fwnode_property_read_bool(child, "linux,can-disable"); 722 691 723 - if (of_property_read_u32(pp, "debounce-interval", 692 + if (fwnode_property_read_u32(child, "debounce-interval", 724 693 &button->debounce_interval)) 725 694 button->debounce_interval = 5; 726 695 727 696 button++; 728 697 } 729 - 730 - if (pdata->nbuttons == 0) 731 - return ERR_PTR(-EINVAL); 732 698 733 699 return pdata; 734 700 } ··· 718 726 }; 719 727 MODULE_DEVICE_TABLE(of, gpio_keys_of_match); 720 728 721 - #else 722 - 723 - static inline struct gpio_keys_platform_data * 724 - gpio_keys_get_devtree_pdata(struct device *dev) 725 - { 726 - return ERR_PTR(-ENODEV); 727 - } 728 - 729 - #endif 730 - 731 729 static int gpio_keys_probe(struct platform_device *pdev) 732 730 { 733 731 struct device *dev = &pdev->dev; 734 732 const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev); 733 + struct fwnode_handle *child = NULL; 735 734 struct gpio_keys_drvdata *ddata; 736 735 struct input_dev *input; 737 736 size_t size; ··· 775 792 const struct gpio_keys_button *button = &pdata->buttons[i]; 776 793 struct gpio_button_data *bdata = &ddata->data[i]; 777 794 778 - error = gpio_keys_setup_key(pdev, input, bdata, button); 779 - if (error) 795 + if (!dev_get_platdata(dev)) { 796 + child = device_get_next_child_node(&pdev->dev, child); 797 + if (!child) { 798 + dev_err(&pdev->dev, 799 + "missing child device node for entry %d\n", 800 + i); 801 + return -EINVAL; 802 + } 803 + } 804 + 805 + error = gpio_keys_setup_key(pdev, input, bdata, button, child); 806 + if (error) { 807 + fwnode_handle_put(child); 780 808 return error; 809 + } 781 810 782 811 if (button->wakeup) 783 812 wakeup = 1; 784 813 } 814 + 815 + fwnode_handle_put(child); 785 816 786 817 error = sysfs_create_group(&pdev->dev.kobj, &gpio_keys_attr_group); 787 818 if (error) { ··· 886 889 .driver = { 887 890 .name = "gpio-keys", 888 891 .pm = &gpio_keys_pm_ops, 889 - .of_match_table = of_match_ptr(gpio_keys_of_match), 892 + .of_match_table = gpio_keys_of_match, 890 893 } 891 894 }; 892 895