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