9
9
*/
10
10
11
11
#include <linux/acpi.h>
12
- #include <linux/bits.h>
13
- #include <linux/bitops.h>
12
+ #include <linux/bitmap.h>
14
13
#include <linux/gpio/driver.h>
15
14
#include <linux/gpio/consumer.h>
16
15
#include <linux/i2c.h>
@@ -116,6 +115,7 @@ MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
116
115
117
116
#define MAX_BANK 5
118
117
#define BANK_SZ 8
118
+ #define MAX_LINE (MAX_BANK * BANK_SZ)
119
119
120
120
#define NBANK (chip ) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
121
121
@@ -147,10 +147,10 @@ struct pca953x_chip {
147
147
148
148
#ifdef CONFIG_GPIO_PCA953X_IRQ
149
149
struct mutex irq_lock ;
150
- u8 irq_mask [ MAX_BANK ] ;
151
- u8 irq_stat [ MAX_BANK ] ;
152
- u8 irq_trig_raise [ MAX_BANK ] ;
153
- u8 irq_trig_fall [ MAX_BANK ] ;
150
+ DECLARE_BITMAP ( irq_mask , MAX_LINE ) ;
151
+ DECLARE_BITMAP ( irq_stat , MAX_LINE ) ;
152
+ DECLARE_BITMAP ( irq_trig_raise , MAX_LINE ) ;
153
+ DECLARE_BITMAP ( irq_trig_fall , MAX_LINE ) ;
154
154
struct irq_chip irq_chip ;
155
155
#endif
156
156
atomic_t wakeup_path ;
@@ -334,12 +334,16 @@ static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off,
334
334
return regaddr ;
335
335
}
336
336
337
- static int pca953x_write_regs (struct pca953x_chip * chip , int reg , u8 * val )
337
+ static int pca953x_write_regs (struct pca953x_chip * chip , int reg , unsigned long * val )
338
338
{
339
339
u8 regaddr = pca953x_recalc_addr (chip , reg , 0 , true, true);
340
- int ret ;
340
+ u8 value [MAX_BANK ];
341
+ int i , ret ;
342
+
343
+ for (i = 0 ; i < NBANK (chip ); i ++ )
344
+ value [i ] = bitmap_get_value8 (val , i * BANK_SZ );
341
345
342
- ret = regmap_bulk_write (chip -> regmap , regaddr , val , NBANK (chip ));
346
+ ret = regmap_bulk_write (chip -> regmap , regaddr , value , NBANK (chip ));
343
347
if (ret < 0 ) {
344
348
dev_err (& chip -> client -> dev , "failed writing register\n" );
345
349
return ret ;
@@ -348,17 +352,21 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
348
352
return 0 ;
349
353
}
350
354
351
- static int pca953x_read_regs (struct pca953x_chip * chip , int reg , u8 * val )
355
+ static int pca953x_read_regs (struct pca953x_chip * chip , int reg , unsigned long * val )
352
356
{
353
357
u8 regaddr = pca953x_recalc_addr (chip , reg , 0 , false, true);
354
- int ret ;
358
+ u8 value [MAX_BANK ];
359
+ int i , ret ;
355
360
356
- ret = regmap_bulk_read (chip -> regmap , regaddr , val , NBANK (chip ));
361
+ ret = regmap_bulk_read (chip -> regmap , regaddr , value , NBANK (chip ));
357
362
if (ret < 0 ) {
358
363
dev_err (& chip -> client -> dev , "failed reading register\n" );
359
364
return ret ;
360
365
}
361
366
367
+ for (i = 0 ; i < NBANK (chip ); i ++ )
368
+ bitmap_set_value8 (val , value [i ], i * BANK_SZ );
369
+
362
370
return 0 ;
363
371
}
364
372
@@ -460,22 +468,15 @@ static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
460
468
unsigned long * mask , unsigned long * bits )
461
469
{
462
470
struct pca953x_chip * chip = gpiochip_get_data (gc );
463
- unsigned long offset ;
464
- unsigned long bank_mask ;
465
- int bank ;
466
- u8 reg_val [MAX_BANK ];
471
+ DECLARE_BITMAP (reg_val , MAX_LINE );
467
472
int ret ;
468
473
469
474
mutex_lock (& chip -> i2c_lock );
470
475
ret = pca953x_read_regs (chip , chip -> regs -> output , reg_val );
471
476
if (ret )
472
477
goto exit ;
473
478
474
- for_each_set_clump8 (offset , bank_mask , mask , gc -> ngpio ) {
475
- bank = offset / 8 ;
476
- reg_val [bank ] &= ~bank_mask ;
477
- reg_val [bank ] |= bitmap_get_value8 (bits , offset ) & bank_mask ;
478
- }
479
+ bitmap_replace (reg_val , reg_val , bits , mask , gc -> ngpio );
479
480
480
481
pca953x_write_regs (chip , chip -> regs -> output , reg_val );
481
482
exit :
@@ -602,36 +603,28 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
602
603
{
603
604
struct gpio_chip * gc = irq_data_get_irq_chip_data (d );
604
605
struct pca953x_chip * chip = gpiochip_get_data (gc );
605
- u8 new_irqs ;
606
- int level , i ;
607
- u8 invert_irq_mask [MAX_BANK ];
608
- u8 reg_direction [MAX_BANK ];
606
+ DECLARE_BITMAP (irq_mask , MAX_LINE );
607
+ DECLARE_BITMAP (reg_direction , MAX_LINE );
608
+ int level ;
609
609
610
610
pca953x_read_regs (chip , chip -> regs -> direction , reg_direction );
611
611
612
612
if (chip -> driver_data & PCA_PCAL ) {
613
613
/* Enable latch on interrupt-enabled inputs */
614
614
pca953x_write_regs (chip , PCAL953X_IN_LATCH , chip -> irq_mask );
615
615
616
- for (i = 0 ; i < NBANK (chip ); i ++ )
617
- invert_irq_mask [i ] = ~chip -> irq_mask [i ];
616
+ bitmap_complement (irq_mask , chip -> irq_mask , gc -> ngpio );
618
617
619
618
/* Unmask enabled interrupts */
620
- pca953x_write_regs (chip , PCAL953X_INT_MASK , invert_irq_mask );
619
+ pca953x_write_regs (chip , PCAL953X_INT_MASK , irq_mask );
621
620
}
622
621
622
+ bitmap_or (irq_mask , chip -> irq_trig_fall , chip -> irq_trig_raise , gc -> ngpio );
623
+ bitmap_and (irq_mask , irq_mask , reg_direction , gc -> ngpio );
624
+
623
625
/* Look for any newly setup interrupt */
624
- for (i = 0 ; i < NBANK (chip ); i ++ ) {
625
- new_irqs = chip -> irq_trig_fall [i ] | chip -> irq_trig_raise [i ];
626
- new_irqs &= reg_direction [i ];
627
-
628
- while (new_irqs ) {
629
- level = __ffs (new_irqs );
630
- pca953x_gpio_direction_input (& chip -> gpio_chip ,
631
- level + (BANK_SZ * i ));
632
- new_irqs &= ~(1 << level );
633
- }
634
- }
626
+ for_each_set_bit (level , irq_mask , gc -> ngpio )
627
+ pca953x_gpio_direction_input (& chip -> gpio_chip , level );
635
628
636
629
mutex_unlock (& chip -> irq_lock );
637
630
}
@@ -672,15 +665,15 @@ static void pca953x_irq_shutdown(struct irq_data *d)
672
665
chip -> irq_trig_fall [d -> hwirq / BANK_SZ ] &= ~mask ;
673
666
}
674
667
675
- static bool pca953x_irq_pending (struct pca953x_chip * chip , u8 * pending )
668
+ static bool pca953x_irq_pending (struct pca953x_chip * chip , unsigned long * pending )
676
669
{
677
- u8 cur_stat [ MAX_BANK ] ;
678
- u8 old_stat [ MAX_BANK ] ;
679
- bool pending_seen = false ;
680
- bool trigger_seen = false ;
681
- u8 trigger [ MAX_BANK ] ;
682
- u8 reg_direction [ MAX_BANK ] ;
683
- int ret , i ;
670
+ struct gpio_chip * gc = & chip -> gpio_chip ;
671
+ DECLARE_BITMAP ( reg_direction , MAX_LINE ) ;
672
+ DECLARE_BITMAP ( old_stat , MAX_LINE ) ;
673
+ DECLARE_BITMAP ( cur_stat , MAX_LINE ) ;
674
+ DECLARE_BITMAP ( new_stat , MAX_LINE ) ;
675
+ DECLARE_BITMAP ( trigger , MAX_LINE ) ;
676
+ int ret ;
684
677
685
678
if (chip -> driver_data & PCA_PCAL ) {
686
679
/* Read the current interrupt status from the device */
@@ -693,16 +686,12 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
693
686
if (ret )
694
687
return false;
695
688
696
- for (i = 0 ; i < NBANK (chip ); i ++ ) {
697
- /* Apply filter for rising/falling edge selection */
698
- pending [i ] = (~cur_stat [i ] & chip -> irq_trig_fall [i ]) |
699
- (cur_stat [i ] & chip -> irq_trig_raise [i ]);
700
- pending [i ] &= trigger [i ];
701
- if (pending [i ])
702
- pending_seen = true;
703
- }
689
+ /* Apply filter for rising/falling edge selection */
690
+ bitmap_replace (new_stat , chip -> irq_trig_fall , chip -> irq_trig_raise , cur_stat , gc -> ngpio );
691
+
692
+ bitmap_and (pending , new_stat , trigger , gc -> ngpio );
704
693
705
- return pending_seen ;
694
+ return ! bitmap_empty ( pending , gc -> ngpio ) ;
706
695
}
707
696
708
697
ret = pca953x_read_regs (chip , chip -> regs -> input , cur_stat );
@@ -711,51 +700,38 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
711
700
712
701
/* Remove output pins from the equation */
713
702
pca953x_read_regs (chip , chip -> regs -> direction , reg_direction );
714
- for (i = 0 ; i < NBANK (chip ); i ++ )
715
- cur_stat [i ] &= reg_direction [i ];
716
703
717
- memcpy (old_stat , chip -> irq_stat , NBANK ( chip ) );
704
+ bitmap_copy (old_stat , chip -> irq_stat , gc -> ngpio );
718
705
719
- for (i = 0 ; i < NBANK (chip ); i ++ ) {
720
- trigger [i ] = (cur_stat [i ] ^ old_stat [i ]) & chip -> irq_mask [i ];
721
- if (trigger [i ])
722
- trigger_seen = true;
723
- }
706
+ bitmap_and (new_stat , cur_stat , reg_direction , gc -> ngpio );
707
+ bitmap_xor (cur_stat , new_stat , old_stat , gc -> ngpio );
708
+ bitmap_and (trigger , cur_stat , chip -> irq_mask , gc -> ngpio );
724
709
725
- if (! trigger_seen )
710
+ if (bitmap_empty ( trigger , gc -> ngpio ) )
726
711
return false;
727
712
728
- memcpy (chip -> irq_stat , cur_stat , NBANK ( chip ) );
713
+ bitmap_copy (chip -> irq_stat , new_stat , gc -> ngpio );
729
714
730
- for (i = 0 ; i < NBANK (chip ); i ++ ) {
731
- pending [i ] = (old_stat [i ] & chip -> irq_trig_fall [i ]) |
732
- (cur_stat [i ] & chip -> irq_trig_raise [i ]);
733
- pending [i ] &= trigger [i ];
734
- if (pending [i ])
735
- pending_seen = true;
736
- }
715
+ bitmap_and (cur_stat , chip -> irq_trig_fall , old_stat , gc -> ngpio );
716
+ bitmap_and (old_stat , chip -> irq_trig_raise , new_stat , gc -> ngpio );
717
+ bitmap_or (new_stat , old_stat , cur_stat , gc -> ngpio );
718
+ bitmap_and (pending , new_stat , trigger , gc -> ngpio );
737
719
738
- return pending_seen ;
720
+ return ! bitmap_empty ( pending , gc -> ngpio ) ;
739
721
}
740
722
741
723
static irqreturn_t pca953x_irq_handler (int irq , void * devid )
742
724
{
743
725
struct pca953x_chip * chip = devid ;
744
- u8 pending [ MAX_BANK ] ;
745
- u8 level ;
746
- int i ;
726
+ struct gpio_chip * gc = & chip -> gpio_chip ;
727
+ DECLARE_BITMAP ( pending , MAX_LINE ) ;
728
+ int level ;
747
729
748
730
if (!pca953x_irq_pending (chip , pending ))
749
731
return IRQ_NONE ;
750
732
751
- for (i = 0 ; i < NBANK (chip ); i ++ ) {
752
- while (pending [i ]) {
753
- level = __ffs (pending [i ]);
754
- handle_nested_irq (irq_find_mapping (chip -> gpio_chip .irq .domain ,
755
- level + (BANK_SZ * i )));
756
- pending [i ] &= ~(1 << level );
757
- }
758
- }
733
+ for_each_set_bit (level , pending , gc -> ngpio )
734
+ handle_nested_irq (irq_find_mapping (gc -> irq .domain , level ));
759
735
760
736
return IRQ_HANDLED ;
761
737
}
@@ -765,8 +741,9 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,
765
741
{
766
742
struct i2c_client * client = chip -> client ;
767
743
struct irq_chip * irq_chip = & chip -> irq_chip ;
768
- u8 reg_direction [MAX_BANK ];
769
- int ret , i ;
744
+ DECLARE_BITMAP (reg_direction , MAX_LINE );
745
+ DECLARE_BITMAP (irq_stat , MAX_LINE );
746
+ int ret ;
770
747
771
748
if (!client -> irq )
772
749
return 0 ;
@@ -777,7 +754,7 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,
777
754
if (!(chip -> driver_data & PCA_INT ))
778
755
return 0 ;
779
756
780
- ret = pca953x_read_regs (chip , chip -> regs -> input , chip -> irq_stat );
757
+ ret = pca953x_read_regs (chip , chip -> regs -> input , irq_stat );
781
758
if (ret )
782
759
return ret ;
783
760
@@ -787,8 +764,7 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,
787
764
* this purpose.
788
765
*/
789
766
pca953x_read_regs (chip , chip -> regs -> direction , reg_direction );
790
- for (i = 0 ; i < NBANK (chip ); i ++ )
791
- chip -> irq_stat [i ] &= reg_direction [i ];
767
+ bitmap_and (chip -> irq_stat , irq_stat , reg_direction , chip -> gpio_chip .ngpio );
792
768
mutex_init (& chip -> irq_lock );
793
769
794
770
ret = devm_request_threaded_irq (& client -> dev , client -> irq ,
@@ -840,8 +816,8 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,
840
816
841
817
static int device_pca95xx_init (struct pca953x_chip * chip , u32 invert )
842
818
{
819
+ DECLARE_BITMAP (val , MAX_LINE );
843
820
int ret ;
844
- u8 val [MAX_BANK ];
845
821
846
822
ret = regcache_sync_region (chip -> regmap , chip -> regs -> output ,
847
823
chip -> regs -> output + NBANK (chip ));
@@ -855,9 +831,9 @@ static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
855
831
856
832
/* set platform specific polarity inversion */
857
833
if (invert )
858
- memset (val , 0xFF , NBANK ( chip ) );
834
+ bitmap_fill (val , MAX_LINE );
859
835
else
860
- memset (val , 0 , NBANK ( chip ) );
836
+ bitmap_zero (val , MAX_LINE );
861
837
862
838
ret = pca953x_write_regs (chip , chip -> regs -> invert , val );
863
839
out :
@@ -866,8 +842,8 @@ static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)
866
842
867
843
static int device_pca957x_init (struct pca953x_chip * chip , u32 invert )
868
844
{
845
+ DECLARE_BITMAP (val , MAX_LINE );
869
846
int ret ;
870
- u8 val [MAX_BANK ];
871
847
872
848
ret = device_pca95xx_init (chip , invert );
873
849
if (ret )
0 commit comments