@@ -63,6 +63,7 @@ struct gpio_bank {
63
63
struct gpio_chip chip ;
64
64
struct clk * dbck ;
65
65
u32 mod_usage ;
66
+ u32 irq_usage ;
66
67
u32 dbck_enable_mask ;
67
68
bool dbck_enabled ;
68
69
struct device * dev ;
@@ -86,6 +87,9 @@ struct gpio_bank {
86
87
#define GPIO_BIT (bank , gpio ) (1 << GPIO_INDEX(bank, gpio))
87
88
#define GPIO_MOD_CTRL_BIT BIT(0)
88
89
90
+ #define BANK_USED (bank ) (bank->mod_usage || bank->irq_usage)
91
+ #define LINE_USED (line , offset ) (line & (1 << offset))
92
+
89
93
static int irq_to_gpio (struct gpio_bank * bank , unsigned int gpio_irq )
90
94
{
91
95
return bank -> chip .base + gpio_irq ;
@@ -420,15 +424,69 @@ static int _set_gpio_triggering(struct gpio_bank *bank, int gpio,
420
424
return 0 ;
421
425
}
422
426
427
+ static void _enable_gpio_module (struct gpio_bank * bank , unsigned offset )
428
+ {
429
+ if (bank -> regs -> pinctrl ) {
430
+ void __iomem * reg = bank -> base + bank -> regs -> pinctrl ;
431
+
432
+ /* Claim the pin for MPU */
433
+ __raw_writel (__raw_readl (reg ) | (1 << offset ), reg );
434
+ }
435
+
436
+ if (bank -> regs -> ctrl && !BANK_USED (bank )) {
437
+ void __iomem * reg = bank -> base + bank -> regs -> ctrl ;
438
+ u32 ctrl ;
439
+
440
+ ctrl = __raw_readl (reg );
441
+ /* Module is enabled, clocks are not gated */
442
+ ctrl &= ~GPIO_MOD_CTRL_BIT ;
443
+ __raw_writel (ctrl , reg );
444
+ bank -> context .ctrl = ctrl ;
445
+ }
446
+ }
447
+
448
+ static void _disable_gpio_module (struct gpio_bank * bank , unsigned offset )
449
+ {
450
+ void __iomem * base = bank -> base ;
451
+
452
+ if (bank -> regs -> wkup_en &&
453
+ !LINE_USED (bank -> mod_usage , offset ) &&
454
+ !LINE_USED (bank -> irq_usage , offset )) {
455
+ /* Disable wake-up during idle for dynamic tick */
456
+ _gpio_rmw (base , bank -> regs -> wkup_en , 1 << offset , 0 );
457
+ bank -> context .wake_en =
458
+ __raw_readl (bank -> base + bank -> regs -> wkup_en );
459
+ }
460
+
461
+ if (bank -> regs -> ctrl && !BANK_USED (bank )) {
462
+ void __iomem * reg = bank -> base + bank -> regs -> ctrl ;
463
+ u32 ctrl ;
464
+
465
+ ctrl = __raw_readl (reg );
466
+ /* Module is disabled, clocks are gated */
467
+ ctrl |= GPIO_MOD_CTRL_BIT ;
468
+ __raw_writel (ctrl , reg );
469
+ bank -> context .ctrl = ctrl ;
470
+ }
471
+ }
472
+
473
+ static int gpio_is_input (struct gpio_bank * bank , int mask )
474
+ {
475
+ void __iomem * reg = bank -> base + bank -> regs -> direction ;
476
+
477
+ return __raw_readl (reg ) & mask ;
478
+ }
479
+
423
480
static int gpio_irq_type (struct irq_data * d , unsigned type )
424
481
{
425
482
struct gpio_bank * bank = irq_data_get_irq_chip_data (d );
426
483
unsigned gpio = 0 ;
427
484
int retval ;
428
485
unsigned long flags ;
486
+ unsigned offset ;
429
487
430
- if (WARN_ON (! bank -> mod_usage ))
431
- return - EINVAL ;
488
+ if (! BANK_USED ( bank ))
489
+ pm_runtime_get_sync ( bank -> dev ) ;
432
490
433
491
#ifdef CONFIG_ARCH_OMAP1
434
492
if (d -> irq > IH_MPUIO_BASE )
@@ -446,7 +504,17 @@ static int gpio_irq_type(struct irq_data *d, unsigned type)
446
504
return - EINVAL ;
447
505
448
506
spin_lock_irqsave (& bank -> lock , flags );
449
- retval = _set_gpio_triggering (bank , GPIO_INDEX (bank , gpio ), type );
507
+ offset = GPIO_INDEX (bank , gpio );
508
+ retval = _set_gpio_triggering (bank , offset , type );
509
+ if (!LINE_USED (bank -> mod_usage , offset )) {
510
+ _enable_gpio_module (bank , offset );
511
+ _set_gpio_direction (bank , offset , 1 );
512
+ } else if (!gpio_is_input (bank , 1 << offset )) {
513
+ spin_unlock_irqrestore (& bank -> lock , flags );
514
+ return - EINVAL ;
515
+ }
516
+
517
+ bank -> irq_usage |= 1 << GPIO_INDEX (bank , gpio );
450
518
spin_unlock_irqrestore (& bank -> lock , flags );
451
519
452
520
if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH ))
@@ -603,35 +671,19 @@ static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
603
671
* If this is the first gpio_request for the bank,
604
672
* enable the bank module.
605
673
*/
606
- if (!bank -> mod_usage )
674
+ if (!BANK_USED ( bank ) )
607
675
pm_runtime_get_sync (bank -> dev );
608
676
609
677
spin_lock_irqsave (& bank -> lock , flags );
610
678
/* Set trigger to none. You need to enable the desired trigger with
611
- * request_irq() or set_irq_type().
679
+ * request_irq() or set_irq_type(). Only do this if the IRQ line has
680
+ * not already been requested.
612
681
*/
613
- _set_gpio_triggering (bank , offset , IRQ_TYPE_NONE );
614
-
615
- if (bank -> regs -> pinctrl ) {
616
- void __iomem * reg = bank -> base + bank -> regs -> pinctrl ;
617
-
618
- /* Claim the pin for MPU */
619
- __raw_writel (__raw_readl (reg ) | (1 << offset ), reg );
620
- }
621
-
622
- if (bank -> regs -> ctrl && !bank -> mod_usage ) {
623
- void __iomem * reg = bank -> base + bank -> regs -> ctrl ;
624
- u32 ctrl ;
625
-
626
- ctrl = __raw_readl (reg );
627
- /* Module is enabled, clocks are not gated */
628
- ctrl &= ~GPIO_MOD_CTRL_BIT ;
629
- __raw_writel (ctrl , reg );
630
- bank -> context .ctrl = ctrl ;
682
+ if (!LINE_USED (bank -> irq_usage , offset )) {
683
+ _set_gpio_triggering (bank , offset , IRQ_TYPE_NONE );
684
+ _enable_gpio_module (bank , offset );
631
685
}
632
-
633
686
bank -> mod_usage |= 1 << offset ;
634
-
635
687
spin_unlock_irqrestore (& bank -> lock , flags );
636
688
637
689
return 0 ;
@@ -640,39 +692,19 @@ static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
640
692
static void omap_gpio_free (struct gpio_chip * chip , unsigned offset )
641
693
{
642
694
struct gpio_bank * bank = container_of (chip , struct gpio_bank , chip );
643
- void __iomem * base = bank -> base ;
644
695
unsigned long flags ;
645
696
646
697
spin_lock_irqsave (& bank -> lock , flags );
647
-
648
- if (bank -> regs -> wkup_en ) {
649
- /* Disable wake-up during idle for dynamic tick */
650
- _gpio_rmw (base , bank -> regs -> wkup_en , 1 << offset , 0 );
651
- bank -> context .wake_en =
652
- __raw_readl (bank -> base + bank -> regs -> wkup_en );
653
- }
654
-
655
698
bank -> mod_usage &= ~(1 << offset );
656
-
657
- if (bank -> regs -> ctrl && !bank -> mod_usage ) {
658
- void __iomem * reg = bank -> base + bank -> regs -> ctrl ;
659
- u32 ctrl ;
660
-
661
- ctrl = __raw_readl (reg );
662
- /* Module is disabled, clocks are gated */
663
- ctrl |= GPIO_MOD_CTRL_BIT ;
664
- __raw_writel (ctrl , reg );
665
- bank -> context .ctrl = ctrl ;
666
- }
667
-
699
+ _disable_gpio_module (bank , offset );
668
700
_reset_gpio (bank , bank -> chip .base + offset );
669
701
spin_unlock_irqrestore (& bank -> lock , flags );
670
702
671
703
/*
672
704
* If this is the last gpio to be freed in the bank,
673
705
* disable the bank module.
674
706
*/
675
- if (!bank -> mod_usage )
707
+ if (!BANK_USED ( bank ) )
676
708
pm_runtime_put (bank -> dev );
677
709
}
678
710
@@ -762,10 +794,20 @@ static void gpio_irq_shutdown(struct irq_data *d)
762
794
struct gpio_bank * bank = irq_data_get_irq_chip_data (d );
763
795
unsigned int gpio = irq_to_gpio (bank , d -> hwirq );
764
796
unsigned long flags ;
797
+ unsigned offset = GPIO_INDEX (bank , gpio );
765
798
766
799
spin_lock_irqsave (& bank -> lock , flags );
800
+ bank -> irq_usage &= ~(1 << offset );
801
+ _disable_gpio_module (bank , offset );
767
802
_reset_gpio (bank , gpio );
768
803
spin_unlock_irqrestore (& bank -> lock , flags );
804
+
805
+ /*
806
+ * If this is the last IRQ to be freed in the bank,
807
+ * disable the bank module.
808
+ */
809
+ if (!BANK_USED (bank ))
810
+ pm_runtime_put (bank -> dev );
769
811
}
770
812
771
813
static void gpio_ack_irq (struct irq_data * d )
@@ -897,13 +939,6 @@ static int gpio_input(struct gpio_chip *chip, unsigned offset)
897
939
return 0 ;
898
940
}
899
941
900
- static int gpio_is_input (struct gpio_bank * bank , int mask )
901
- {
902
- void __iomem * reg = bank -> base + bank -> regs -> direction ;
903
-
904
- return __raw_readl (reg ) & mask ;
905
- }
906
-
907
942
static int gpio_get (struct gpio_chip * chip , unsigned offset )
908
943
{
909
944
struct gpio_bank * bank ;
@@ -922,13 +957,22 @@ static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
922
957
{
923
958
struct gpio_bank * bank ;
924
959
unsigned long flags ;
960
+ int retval = 0 ;
925
961
926
962
bank = container_of (chip , struct gpio_bank , chip );
927
963
spin_lock_irqsave (& bank -> lock , flags );
964
+
965
+ if (LINE_USED (bank -> irq_usage , offset )) {
966
+ retval = - EINVAL ;
967
+ goto exit ;
968
+ }
969
+
928
970
bank -> set_dataout (bank , offset , value );
929
971
_set_gpio_direction (bank , offset , 0 );
972
+
973
+ exit :
930
974
spin_unlock_irqrestore (& bank -> lock , flags );
931
- return 0 ;
975
+ return retval ;
932
976
}
933
977
934
978
static int gpio_debounce (struct gpio_chip * chip , unsigned offset ,
@@ -1400,7 +1444,7 @@ void omap2_gpio_prepare_for_idle(int pwr_mode)
1400
1444
struct gpio_bank * bank ;
1401
1445
1402
1446
list_for_each_entry (bank , & omap_gpio_list , node ) {
1403
- if (!bank -> mod_usage || !bank -> loses_context )
1447
+ if (!BANK_USED ( bank ) || !bank -> loses_context )
1404
1448
continue ;
1405
1449
1406
1450
bank -> power_mode = pwr_mode ;
@@ -1414,7 +1458,7 @@ void omap2_gpio_resume_after_idle(void)
1414
1458
struct gpio_bank * bank ;
1415
1459
1416
1460
list_for_each_entry (bank , & omap_gpio_list , node ) {
1417
- if (!bank -> mod_usage || !bank -> loses_context )
1461
+ if (!BANK_USED ( bank ) || !bank -> loses_context )
1418
1462
continue ;
1419
1463
1420
1464
pm_runtime_get_sync (bank -> dev );
0 commit comments