gpio: pca953x: Convert to bitmap (extended) API
Subsystems (3)
v2 cover gpio: pca953x: Convert to bitmap (extended) API
While converting gpio-pca953x driver to bitmap API, I noticed that we have no function to replace bits.
So, that's how patch 7 appears.
First 6 patches are preparatory of the test suite (including some warning fixes, etc).
Patches 8-9 are preparatory for the GPIO driver to be easier converted to bitmap API, conversion to which happens in patch 10.
Patch 11 simple indentation fixes.
Since the series depends to the commit e51819d749db ("bitops: introduce the for_each_set_clump8 macro") in Andrew's set and taking into consideration a lot of bitmap related patches here, it would make sense to route these thru Andrew as well.
In v2:
- address wrong logic in pca953x_gpio_set_multiple() (William)
- 10 more patches as described above
Andy Shevchenko (11):
lib/test_bitmap: Force argument of bitmap_parselist_user() to proper
address space
lib/test_bitmap: Undefine macros after use lib/test_bitmap: Name EXP_BYTES properly
lib/test_bitmap: Rename exp to exp1 to avoid ambiguous name lib/test_bitmap: Move exp1 and exp2 upper for others to use lib/test_bitmap: Fix comment about this file
bitmap: Introduce bitmap_replace() helper
gpio: pca953x: Remove redundant variable and check in IRQ handler
gpio: pca953x: Use input from regs structure in pca953x_irq_pending()
gpio: pca953x: Convert to use bitmap API
gpio: pca953x: Tight up indentation
drivers/gpio/gpio-pca953x.c | 196 ++++++++++++++++-------------------- include/linux/bitmap.h | 16 +++ lib/bitmap.c | 12 +++ lib/test_bitmap.c | 137 ++++++++++++++++--------- 4 files changed, 203 insertions(+), 158 deletions(-)
Signature · 1 line
2.23.0
PATCH v2 1/11 lib/test_bitmap: Force argument of bitmap_parselist_user() to proper address space
Sparse complains:
lib/test_bitmap.c:345:58: warning: incorrect type in argument 1 (different address spaces)lib/test_bitmap.c:345:58: expected char const [noderef] <asn:1> *ubuflib/test_bitmap.c:345:58: got char const *const in
Force argument of bitmap_parselist_user() to proper address space.
--- lib/test_bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_bitmap.c +1 -1
@@ -330,7 +330,7 @@ static void __init __test_bitmap_parselist(int is_user)set_fs (KERNEL_DS ); time =ktime_get (); - err =bitmap_parselist_user (ptest.in, len, + err =bitmap_parselist_user ((__force const char __user *)ptest.in, len, bmap, ptest.nbits); time =ktime_get () - time;set_fs (orig_fs);
Signature · 1 line
2.23.0
PATCH v2 2/11 lib/test_bitmap: Undefine macros after use
In reply to Andy ShevchenkoThere is no need to keep step and ptest macros defined in entire file.
--- lib/test_bitmap.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/test_bitmap.c +4 -0
@@ -311,6 +311,8 @@ static const struct test_bitmap_parselist parselist_tests[] __initconst = { {-EINVAL, "a-31:10/1", NULL, 8, 0}, {-EINVAL, "0-31:a/1", NULL, 8, 0}, {-EINVAL, "0-\n", NULL, 8, 0}, + +#undef step };static void __init __test_bitmap_parselist (int is_user)
@@ -357,6 +359,8 @@ static void __init __test_bitmap_parselist(int is_user)if (ptest.flags &PARSE_TIME )pr_err ("parselist%s: %d: input is '%s' OK, Time: %llu\n" , mode, i, ptest.in, time); + +#undef ptest } }
Signature · 1 line
2.23.0
PATCH v2 3/11 lib/test_bitmap: Name EXP_BYTES properly
In reply to Andy ShevchenkoEXP_BYTES has been wrongly named. It's a size of the exp array in bits.
While here, go ahead and rename to EXP1_IN_BITS to avoid double renaming when exp will be renamed to exp1 in the next patch
--- lib/test_bitmap.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
lib/test_bitmap.c +5 -5
@@ -374,17 +374,17 @@ static void __init test_bitmap_parselist_user(void)__test_bitmap_parselist (1 ); } -#define EXP_BYTES (sizeof(exp) * 8) +#define EXP1_IN_BITS (sizeof(exp) * 8)static void __init test_bitmap_arr32 (void ) {unsigned int nbits, next_bit; -u32 arr[sizeof (exp) /4 ]; -DECLARE_BITMAP (bmap2,EXP_BYTES ); +u32 arr[EXP1_IN_BITS /32 ]; +DECLARE_BITMAP (bmap2,EXP1_IN_BITS );memset (arr,0xa5 ,sizeof (arr)); -for (nbits =0 ; nbits <EXP_BYTES ; ++nbits) { +for (nbits =0 ; nbits <EXP1_IN_BITS ; ++nbits) {bitmap_to_arr32 (arr, exp, nbits);bitmap_from_arr32 (bmap2, arr, nbits);expect_eq_bitmap (bmap2, exp, nbits);
@@ -396,7 +396,7 @@ static void __init test_bitmap_arr32(void)" tail is not safely cleared: %d\n" , nbits, next_bit); -if (nbits <EXP_BYTES -32 ) +if (nbits <EXP1_IN_BITS -32 )expect_eq_uint (arr[DIV_ROUND_UP (nbits,32 )],0xa5a5a5a5 ); }
Signature · 1 line
2.23.0
PATCH v2 4/11 lib/test_bitmap: Rename exp to exp1 to avoid ambiguous name
In reply to Andy ShevchenkoOne function is using exp as local variable. Avoid ambiguous naming by rename global one to exp1.
--- lib/test_bitmap.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-)
lib/test_bitmap.c +22 -22
@@ -247,7 +247,7 @@ struct test_bitmap_parselist{const int flags; }; -static const unsigned long exp[]__initconst = { +static const unsigned long exp1[]__initconst = {BITMAP_FROM_U64 (1 ),BITMAP_FROM_U64 (2 ),BITMAP_FROM_U64 (0x0000ffff ),
@@ -271,29 +271,29 @@ static const unsigned long exp2[] __initconst = {static const struct test_bitmap_parselist parselist_tests[]__initconst = { #define step (sizeof(u64) / sizeof(unsigned long)) - {0 ,"0" , &exp[0 ],8 ,0 }, - {0 ,"1" , &exp[1 * step],8 ,0 }, - {0 ,"0-15" , &exp[2 * step],32 ,0 }, - {0 ,"16-31" , &exp[3 * step],32 ,0 }, - {0 ,"0-31:1/2" , &exp[4 * step],32 ,0 }, - {0 ,"1-31:1/2" , &exp[5 * step],32 ,0 }, - {0 ,"0-31:1/4" , &exp[6 * step],32 ,0 }, - {0 ,"1-31:1/4" , &exp[7 * step],32 ,0 }, - {0 ,"0-31:4/4" , &exp[8 * step],32 ,0 }, - {0 ,"1-31:4/4" , &exp[9 * step],32 ,0 }, - {0 ,"0-31:1/4,32-63:2/4" , &exp[10 * step],64 ,0 }, - {0 ,"0-31:3/4,32-63:4/4" , &exp[11 * step],64 ,0 }, - {0 ," ,, 0-31:3/4 ,, 32-63:4/4 ,, " , &exp[11 * step],64 ,0 }, + {0 ,"0" , &exp1[0 ],8 ,0 }, + {0 ,"1" , &exp1[1 * step],8 ,0 }, + {0 ,"0-15" , &exp1[2 * step],32 ,0 }, + {0 ,"16-31" , &exp1[3 * step],32 ,0 }, + {0 ,"0-31:1/2" , &exp1[4 * step],32 ,0 }, + {0 ,"1-31:1/2" , &exp1[5 * step],32 ,0 }, + {0 ,"0-31:1/4" , &exp1[6 * step],32 ,0 }, + {0 ,"1-31:1/4" , &exp1[7 * step],32 ,0 }, + {0 ,"0-31:4/4" , &exp1[8 * step],32 ,0 }, + {0 ,"1-31:4/4" , &exp1[9 * step],32 ,0 }, + {0 ,"0-31:1/4,32-63:2/4" , &exp1[10 * step],64 ,0 }, + {0 ,"0-31:3/4,32-63:4/4" , &exp1[11 * step],64 ,0 }, + {0 ," ,, 0-31:3/4 ,, 32-63:4/4 ,, " , &exp1[11 * step],64 ,0 }, {0 ,"0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4" , exp2,128 ,0 }, {0 ,"0-2047:128/256" ,NULL ,2048 ,PARSE_TIME }, - {0 ,"" , &exp[12 * step],8 ,0 }, - {0 ,"\n" , &exp[12 * step],8 ,0 }, - {0 ,",, ,, , , ," , &exp[12 * step],8 ,0 }, - {0 ," , ,, , , " , &exp[12 * step],8 ,0 }, - {0 ," , ,, , , \n" , &exp[12 * step],8 ,0 }, + {0 ,"" , &exp1[12 * step],8 ,0 }, + {0 ,"\n" , &exp1[12 * step],8 ,0 }, + {0 ,",, ,, , , ," , &exp1[12 * step],8 ,0 }, + {0 ," , ,, , , " , &exp1[12 * step],8 ,0 }, + {0 ," , ,, , , \n" , &exp1[12 * step],8 ,0 }, {-EINVAL ,"-1" ,NULL ,8 ,0 }, {-EINVAL ,"-0" ,NULL ,8 ,0 },
@@ -374,7 +374,7 @@ static void __init test_bitmap_parselist_user(void)__test_bitmap_parselist (1 ); } -#define EXP1_IN_BITS (sizeof(exp) * 8) +#define EXP1_IN_BITS (sizeof(exp1) * 8)static void __init test_bitmap_arr32 (void ) {
@@ -385,9 +385,9 @@ static void __init test_bitmap_arr32(void)memset (arr,0xa5 ,sizeof (arr));for (nbits =0 ; nbits <EXP1_IN_BITS ; ++nbits) { -bitmap_to_arr32 (arr, exp, nbits); +bitmap_to_arr32 (arr, exp1, nbits);bitmap_from_arr32 (bmap2, arr, nbits); -expect_eq_bitmap (bmap2, exp, nbits); +expect_eq_bitmap (bmap2, exp1, nbits); next_bit =find_next_bit (bmap2,round_up (nbits,BITS_PER_LONG ), nbits);
Signature · 1 line
2.23.0
PATCH v2 5/11 lib/test_bitmap: Move exp1 and exp2 upper for others to use
In reply to Andy ShevchenkoSome test cases may re-use predefined exp1 and exp2 bitmaps. Move them upper in the file.
--- lib/test_bitmap.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-)
lib/test_bitmap.c +20 -21
@@ -21,6 +21,26 @@ static unsigned failed_tests __initdata;static char pbl_buffer[PAGE_SIZE ]__initdata ; +static const unsigned long exp1[]__initconst = { +BITMAP_FROM_U64 (1 ), +BITMAP_FROM_U64 (2 ), +BITMAP_FROM_U64 (0x0000ffff ), +BITMAP_FROM_U64 (0xffff0000 ), +BITMAP_FROM_U64 (0x55555555 ), +BITMAP_FROM_U64 (0xaaaaaaaa ), +BITMAP_FROM_U64 (0x11111111 ), +BITMAP_FROM_U64 (0x22222222 ), +BITMAP_FROM_U64 (0xffffffff ), +BITMAP_FROM_U64 (0xfffffffe ), +BITMAP_FROM_U64 (0x3333333311111111ULL ), +BITMAP_FROM_U64 (0xffffffff77777777ULL ), +BITMAP_FROM_U64 (0 ), +}; + +static const unsigned long exp2[]__initconst = { +BITMAP_FROM_U64 (0x3333333311111111ULL ), +BITMAP_FROM_U64 (0xffffffff77777777ULL ), +}; static bool __init __check_eq_uint(const char *srcfile, unsigned int line,
@@ -247,27 +267,6 @@ struct test_bitmap_parselist{const int flags; }; -static const unsigned long exp1[]__initconst = { -BITMAP_FROM_U64 (1 ), -BITMAP_FROM_U64 (2 ), -BITMAP_FROM_U64 (0x0000ffff ), -BITMAP_FROM_U64 (0xffff0000 ), -BITMAP_FROM_U64 (0x55555555 ), -BITMAP_FROM_U64 (0xaaaaaaaa ), -BITMAP_FROM_U64 (0x11111111 ), -BITMAP_FROM_U64 (0x22222222 ), -BITMAP_FROM_U64 (0xffffffff ), -BITMAP_FROM_U64 (0xfffffffe ), -BITMAP_FROM_U64 (0x3333333311111111ULL ), -BITMAP_FROM_U64 (0xffffffff77777777ULL ), -BITMAP_FROM_U64 (0 ), -}; - -static const unsigned long exp2[]__initconst = { -BITMAP_FROM_U64 (0x3333333311111111ULL ), -BITMAP_FROM_U64 (0xffffffff77777777ULL ) -}; -static const struct test_bitmap_parselist parselist_tests[]__initconst = { #define step (sizeof(u64) / sizeof(unsigned long))
Signature · 1 line
2.23.0
PATCH v2 6/11 lib/test_bitmap: Fix comment about this file
In reply to Andy ShevchenkoThis test case file is about bitmap API, and not printf() facility.
--- lib/test_bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_bitmap.c +1 -1
@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* -* Test cases for printf facility. +* Test cases for bitmap API. */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt
Signature · 1 line
2.23.0
PATCH v2 7/11 bitmap: Introduce bitmap_replace() helper
In reply to Andy ShevchenkoIn some drivers we want to have a single operation over bitmap which is an equivalent to:
*dst = (*old & ~(*mask)) | (*new & *mask)
Introduce bitmap_replace() helper for this.
--- include/linux/bitmap.h | 16 ++++++++++++++++ lib/bitmap.c | 12 ++++++++++++ lib/test_bitmap.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+)
include/linux/bitmap.h +16 -0
@@ -53,6 +53,7 @@* bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n +* bitmap_replace(dst, old, new, mask, nbits) *dst = (*old & ~(*mask)) | (*new & *mask) * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src) * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit) * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
@@ -140,6 +141,9 @@ extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits);extern int __bitmap_andnot (unsigned long *dst,const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int nbits); +extern void __bitmap_replace (unsigned long *dst, +const unsigned long *old,const unsigned long *new, +const unsigned long *mask,unsigned int nbits);extern int __bitmap_intersects (const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int nbits); extern int __bitmap_subset(const unsigned long *bitmap1,
@@ -434,6 +438,18 @@ static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *sr__bitmap_shift_left (dst, src, shift, nbits); } +static inline void bitmap_replace (unsigned long *dst, +const unsigned long *old, +const unsigned long *new, +const unsigned long *mask, +unsigned int nbits) +{ +if (small_const_nbits (nbits)) + *dst = (*old & ~(*mask)) | (*new & *mask); +else +__bitmap_replace (dst, old, new, mask, nbits); +} +static inline int bitmap_parse (const char *buf,unsigned int buflen,unsigned long *maskp,int nmaskbits) {
lib/bitmap.c +12 -0
@@ -222,6 +222,18 @@ int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, }EXPORT_SYMBOL (__bitmap_andnot); +void __bitmap_replace (unsigned long *dst, +const unsigned long *old,const unsigned long *new, +const unsigned long *mask,unsigned int nbits) +{ +unsigned int k; +unsigned int nr =BITS_TO_LONGS (nbits); + +for (k =0 ; k < nr; k++) + dst[k] = (old[k] & ~mask[k]) | (new[k] & mask[k]); +} +EXPORT_SYMBOL (__bitmap_replace); +int __bitmap_intersects (const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int bits) {
lib/test_bitmap.c +38 -0
@@ -42,6 +42,19 @@ static const unsigned long exp2[] __initconst = { BITMAP_FROM_U64(0xffffffff77777777ULL), }; +/* Fibonacci sequence */ +static const unsigned long exp2_to_exp3_mask[]__initconst = { +BITMAP_FROM_U64 (0x008000020020212eULL ), +}; +/* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */ +static const unsigned long exp3_0_1[]__initconst = { +BITMAP_FROM_U64 (0x33b3333311313137ULL ), +}; +/* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */ +static const unsigned long exp3_1_0[]__initconst = { +BITMAP_FROM_U64 (0xff7fffff77575751ULL ), +}; +static bool __init __check_eq_uint (const char *srcfile,unsigned int line,const unsigned int exp_uint,unsigned int x)
@@ -257,6 +270,30 @@ static void __init test_copy(void)expect_eq_pbl ("0-108,128-1023" , bmap2,1024 ); } +#define EXP2_IN_BITS (sizeof(exp2) * 8) + +static void __init test_replace (void ) +{ +unsigned int nbits =64 ; +DECLARE_BITMAP (bmap,1024 ); + +bitmap_zero (bmap,1024 ); +bitmap_replace (bmap, &exp2[0 ], &exp2[1 ], exp2_to_exp3_mask, nbits); +expect_eq_bitmap (bmap, exp3_0_1, nbits); + +bitmap_zero (bmap,1024 ); +bitmap_replace (bmap, &exp2[1 ], &exp2[0 ], exp2_to_exp3_mask, nbits); +expect_eq_bitmap (bmap, exp3_1_0, nbits); + +bitmap_fill (bmap,1024 ); +bitmap_replace (bmap, &exp2[0 ], &exp2[1 ], exp2_to_exp3_mask, nbits); +expect_eq_bitmap (bmap, exp3_0_1, nbits); + +bitmap_fill (bmap,1024 ); +bitmap_replace (bmap, &exp2[1 ], &exp2[0 ], exp2_to_exp3_mask, nbits); +expect_eq_bitmap (bmap, exp3_1_0, nbits); +} +#define PARSE_TIME 0x1struct test_bitmap_parselist {
@@ -476,6 +513,7 @@ static void __init selftest(void)test_zero_clear ();test_fill_set ();test_copy (); +test_replace ();test_bitmap_arr32 ();test_bitmap_parselist ();test_bitmap_parselist_user ();
Signature · 1 line
2.23.0
Re: [PATCH v2 07/11] bitmap: Introduce bitmap_replace() helper
Hi,
Quoted 10 lines · Andy Shevchenko
In some drivers we want to have a single operation over bitmap which is an equivalent to:
*dst = (*old & ~(*mask)) | (*new & *mask)Introduce
bitmap_replace()helper for this.Cc Yury Norov <[email protected]>Cc Rasmus Villemoes <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
This patch results in the following boot log messages on various architectures. I have seen it on arm and mipsel, but others may be affected as well.
test_bitmap: [lib/test_bitmap.c:282] bitmaps contents differ: expected "0-1,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,56-57,60-61", got "0-2,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,55-57,60-61"test_bitmap: [lib/test_bitmap.c:286] bitmaps contents differ: expected "0,4,8-9,12,16-17,20,24-25,28-29,32-34,36-38,40-42,44-46,48-50,52-54,56-58,60-62", got "0,4,6,8-10,12,14,16-18,20,22,24-26,28-30,32-54,56-63"test_bitmap: [lib/test_bitmap.c:290] bitmaps contents differ: expected "0-1,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,56-57,60-61", got "0-2,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,55-57,60-61"test_bitmap: [lib/test_bitmap.c:294] bitmaps contents differ: expected "0,4,8-9,12,16-17,20,24-25,28-29,32-34,36-38,40-42,44-46,48-50,52-54,56-58,60-62", got "0,4,6,8-10,12,14,16-18,20,22,24-26,28-30,32-54,56-63"
Guenter
Re: [PATCH v2 07/11] bitmap: Introduce bitmap_replace() helper
Quoted 8 lines · Guenter Roeck
Hi,
Quoted 5 lines · Andy Shevchenko
In some drivers we want to have a single operation over bitmap which is an equivalent to:
*dst = (*old & ~(*mask)) | (*new & *mask)
Quoted 8 lines
Quoted 5 lines
Introduce
bitmap_replace()helper for this.Cc Yury Norov <[email protected]>Cc Rasmus Villemoes <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>This patch results in the following boot log messages on various architectures. I have seen it on arm and mipsel, but others may be affected as well.
Thanks for report. I can reproduce on x86_32, and not on x86_64. I'll look at it.
Quoted 7 lines
test_bitmap: [lib/test_bitmap.c:282] bitmaps contents differ: expected "0-1,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,56-57,60-61", got "0-2,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,55-57,60-61"test_bitmap: [lib/test_bitmap.c:286] bitmaps contents differ: expected "0,4,8-9,12,16-17,20,24-25,28-29,32-34,36-38,40-42,44-46,48-50,52-54,56-58,60-62", got "0,4,6,8-10,12,14,16-18,20,22,24-26,28-30,32-54,56-63"test_bitmap: [lib/test_bitmap.c:290] bitmaps contents differ: expected "0-1,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,56-57,60-61", got "0-2,4-5,8,12-13,16,20-21,24,28,32-33,36-37,40-41,44-45,48-49,52-53,55-57,60-61"test_bitmap: [lib/test_bitmap.c:294] bitmaps contents differ: expected "0,4,8-9,12,16-17,20,24-25,28-29,32-34,36-38,40-42,44-46,48-50,52-54,56-58,60-62", got "0,4,6,8-10,12,14,16-18,20,22,24-26,28-30,32-54,56-63"Guenter
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 8/11 gpio: pca953x: Remove redundant variable and check in IRQ handler
In reply to Andy ShevchenkoWe always will have at least one iteration of the loop due to pending being guaranteed to be non-zero. That is, we may remove extra variable and check in the IRQ handler.
--- drivers/gpio/gpio-pca953x.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpio/gpio-pca953x.c +1 -3
@@ -740,7 +740,6 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)struct pca953x_chip *chip = devid;u8 pending[MAX_BANK ];u8 level; -unsigned nhandled =0 ;int i;if (!pca953x_irq_pending (chip, pending))
@@ -752,11 +751,10 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)handle_nested_irq (irq_find_mapping (chip->gpio_chip.irq.domain, level + (BANK_SZ * i))); pending[i] &= ~(1 << level); - nhandled++; } } -return (nhandled >0 ) ?IRQ_HANDLED :IRQ_NONE ; +return IRQ_HANDLED ; } static int pca953x_irq_setup(struct pca953x_chip *chip,
Signature · 1 line
2.23.0
PATCH v2 9/11 gpio: pca953x: Use input from regs structure in pca953x_irq_pending()
In reply to Andy ShevchenkoWhile PCA_PCAL is defined for PCA953X type only, we still may use an offset of the input from regs structure for sake of consistency.
--- drivers/gpio/gpio-pca953x.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-pca953x.c +1 -1
@@ -686,7 +686,7 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)return false;/* Check latched inputs and clear interrupt status */ - ret =pca953x_read_regs (chip,PCA953X_INPUT , cur_stat); + ret =pca953x_read_regs (chip, chip->regs->input, cur_stat);if (ret)return false;
Signature · 1 line
2.23.0
PATCH v2 10/11 gpio: pca953x: Convert to use bitmap API
In reply to Andy ShevchenkoInstead of customized approach convert the driver to use bitmap API.
Depends-on: 6e9c6674d1bf ("gpio: pca953x: utilize the for_each_set_clump8 macro")
--- drivers/gpio/gpio-pca953x.c | 168 ++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 95 deletions(-)
drivers/gpio/gpio-pca953x.c +73 -95
@@ -9,8 +9,7 @@*/ #include <linux/acpi.h> -#include <linux/bits.h> -#include <linux/bitops.h> +#include <linux/bitmap.h> #include <linux/gpio/driver.h> #include <linux/gpio/consumer.h> #include <linux/i2c.h>
@@ -116,6 +115,7 @@ MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);#define MAX_BANK 5#define BANK_SZ 8 +#define MAX_LINE (MAX_BANK * BANK_SZ)#define NBANK (chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
@@ -147,10 +147,10 @@ struct pca953x_chip {#ifdef CONFIG_GPIO_PCA953X_IRQ struct mutex irq_lock; -u8 irq_mask[MAX_BANK ]; -u8 irq_stat[MAX_BANK ]; -u8 irq_trig_raise[MAX_BANK ]; -u8 irq_trig_fall[MAX_BANK ]; +DECLARE_BITMAP (irq_mask,MAX_LINE ); +DECLARE_BITMAP (irq_stat,MAX_LINE ); +DECLARE_BITMAP (irq_trig_raise,MAX_LINE ); +DECLARE_BITMAP (irq_trig_fall,MAX_LINE );struct irq_chip irq_chip;#endif atomic_t wakeup_path;
@@ -334,12 +334,16 @@ static u8 pca953x_recalc_addr(struct pca953x_chip *chip, int reg, int off,return regaddr; } -static int pca953x_write_regs (struct pca953x_chip *chip,int reg,u8 *val) +static int pca953x_write_regs (struct pca953x_chip *chip,int reg,unsigned long *val) {u8 regaddr =pca953x_recalc_addr (chip, reg,0 , true, true); -int ret; +u8 value[MAX_BANK ]; +int i, ret; + +for (i =0 ; i <NBANK (chip); i++) + value[i] =bitmap_get_value8 (val, i *BANK_SZ ); - ret =regmap_bulk_write (chip->regmap, regaddr, val,NBANK (chip)); + ret =regmap_bulk_write (chip->regmap, regaddr, value,NBANK (chip));if (ret <0 ) {dev_err (&chip->client->dev,"failed writing register\n" );return ret;
@@ -348,17 +352,21 @@ static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)return 0 ; } -static int pca953x_read_regs (struct pca953x_chip *chip,int reg,u8 *val) +static int pca953x_read_regs (struct pca953x_chip *chip,int reg,unsigned long *val) {u8 regaddr =pca953x_recalc_addr (chip, reg,0 , false, true); -int ret; +u8 value[MAX_BANK ]; +int i, ret; - ret =regmap_bulk_read (chip->regmap, regaddr, val,NBANK (chip)); + ret =regmap_bulk_read (chip->regmap, regaddr, value,NBANK (chip));if (ret <0 ) {dev_err (&chip->client->dev,"failed reading register\n" );return ret; } +for (i =0 ; i <NBANK (chip); i++) +bitmap_set_value8 (val, value[i], i *BANK_SZ ); +return 0 ; }
@@ -457,10 +465,8 @@ static void pca953x_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, unsigned long *bits) { struct pca953x_chip *chip = gpiochip_get_data(gc); -unsigned long offset; -unsigned long bank_mask; -int bank; -u8 reg_val[MAX_BANK ]; +DECLARE_BITMAP (reg_val,MAX_LINE ); +DECLARE_BITMAP (new_val,MAX_LINE );int ret;mutex_lock (&chip->i2c_lock);
@@ -468,13 +474,9 @@ static void pca953x_gpio_set_multiple(struct gpio_chip *gc,if (ret) goto exit; -for_each_set_clump8 (offset, bank_mask, mask, gc->ngpio) { - bank = offset /8 ; - reg_val[bank] &= ~bank_mask; - reg_val[bank] |=bitmap_get_value8 (bits, offset) & bank_mask; - } +bitmap_replace (new_val, reg_val, bits, mask, gc->ngpio); -pca953x_write_regs (chip, chip->regs->output, reg_val); +pca953x_write_regs (chip, chip->regs->output, new_val); exit:mutex_unlock (&chip->i2c_lock); }
@@ -599,10 +601,10 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d) {struct gpio_chip *gc =irq_data_get_irq_chip_data (d);struct pca953x_chip *chip =gpiochip_get_data (gc); -u8 new_irqs; -int level, i; -u8 invert_irq_mask[MAX_BANK ]; -u8 reg_direction[MAX_BANK ]; +DECLARE_BITMAP (irq_mask,MAX_LINE ); +DECLARE_BITMAP (reg_direction,MAX_LINE ); +DECLARE_BITMAP (new_irqs,MAX_LINE ); +int level;pca953x_read_regs (chip, chip->regs->direction, reg_direction);
@@ -610,25 +612,18 @@ static void pca953x_irq_bus_sync_unlock(struct irq_data *d)/* Enable latch on interrupt-enabled inputs */ pca953x_write_regs (chip,PCAL953X_IN_LATCH , chip->irq_mask); -for (i =0 ; i <NBANK (chip); i++) - invert_irq_mask[i] = ~chip->irq_mask[i]; +bitmap_complement (irq_mask, chip->irq_mask, gc->ngpio);/* Unmask enabled interrupts */ -pca953x_write_regs (chip,PCAL953X_INT_MASK , invert_irq_mask); +pca953x_write_regs (chip,PCAL953X_INT_MASK , irq_mask); } +bitmap_or (new_irqs, chip->irq_trig_fall, chip->irq_trig_raise, gc->ngpio); +bitmap_and (irq_mask, new_irqs, reg_direction, gc->ngpio); +/* Look for any newly setup interrupt */ -for (i =0 ; i <NBANK (chip); i++) { - new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i]; - new_irqs &= reg_direction[i]; - -while (new_irqs) { - level =__ffs (new_irqs); -pca953x_gpio_direction_input (&chip->gpio_chip, - level + (BANK_SZ * i)); - new_irqs &= ~(1 << level); - } - } +for_each_set_bit (level, irq_mask, gc->ngpio) +pca953x_gpio_direction_input (&chip->gpio_chip, level);mutex_unlock (&chip->irq_lock); }
@@ -669,15 +664,15 @@ static void pca953x_irq_shutdown(struct irq_data *d) chip->irq_trig_fall[d->hwirq /BANK_SZ ] &= ~mask; } -static bool pca953x_irq_pending (struct pca953x_chip *chip,u8 *pending) +static bool pca953x_irq_pending (struct pca953x_chip *chip,unsigned long *pending) { -u8 cur_stat[MAX_BANK ]; -u8 old_stat[MAX_BANK ]; -bool pending_seen = false; -bool trigger_seen = false; -u8 trigger[MAX_BANK ]; -u8 reg_direction[MAX_BANK ]; -int ret, i; +struct gpio_chip *gc = &chip->gpio_chip; +DECLARE_BITMAP (reg_direction,MAX_LINE ); +DECLARE_BITMAP (old_stat,MAX_LINE ); +DECLARE_BITMAP (cur_stat,MAX_LINE ); +DECLARE_BITMAP (new_stat,MAX_LINE ); +DECLARE_BITMAP (trigger,MAX_LINE ); +int ret;if (chip->driver_data &PCA_PCAL ) {/* Read the current interrupt status from the device */
@@ -690,16 +685,12 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)if (ret)return false; -for (i =0 ; i <NBANK (chip); i++) { -/* Apply filter for rising/falling edge selection */ - pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) | - (cur_stat[i] & chip->irq_trig_raise[i]); - pending[i] &= trigger[i]; -if (pending[i]) - pending_seen = true; - } +/* Apply filter for rising/falling edge selection */ +bitmap_replace (new_stat, chip->irq_trig_fall, chip->irq_trig_raise, cur_stat, gc->ngpio); + +bitmap_and (pending, new_stat, trigger, gc->ngpio); -return pending_seen; +return !bitmap_empty (pending, gc->ngpio); } ret =pca953x_read_regs (chip, chip->regs->input, cur_stat);
@@ -708,51 +699,38 @@ static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)/* Remove output pins from the equation */ pca953x_read_regs (chip, chip->regs->direction, reg_direction); -for (i =0 ; i <NBANK (chip); i++) - cur_stat[i] &= reg_direction[i]; -memcpy (old_stat, chip->irq_stat,NBANK (chip)); +bitmap_copy (old_stat, chip->irq_stat, gc->ngpio); -for (i =0 ; i <NBANK (chip); i++) { - trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i]; -if (trigger[i]) - trigger_seen = true; - } +bitmap_and (new_stat, cur_stat, reg_direction, gc->ngpio); +bitmap_xor (cur_stat, new_stat, old_stat, gc->ngpio); +bitmap_and (trigger, cur_stat, chip->irq_mask, gc->ngpio); -if (!trigger_seen) +if (bitmap_empty (trigger, gc->ngpio))return false; -memcpy (chip->irq_stat, cur_stat,NBANK (chip)); +bitmap_copy (chip->irq_stat, new_stat, gc->ngpio); -for (i =0 ; i <NBANK (chip); i++) { - pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) | - (cur_stat[i] & chip->irq_trig_raise[i]); - pending[i] &= trigger[i]; -if (pending[i]) - pending_seen = true; - } +bitmap_and (cur_stat, chip->irq_trig_fall, old_stat, gc->ngpio); +bitmap_and (old_stat, chip->irq_trig_raise, new_stat, gc->ngpio); +bitmap_or (new_stat, old_stat, cur_stat, gc->ngpio); +bitmap_and (pending, new_stat, trigger, gc->ngpio); -return pending_seen; +return !bitmap_empty (pending, gc->ngpio); }static irqreturn_t pca953x_irq_handler (int irq,void *devid) {struct pca953x_chip *chip = devid; -u8 pending[MAX_BANK ]; -u8 level; -int i; +struct gpio_chip *gc = &chip->gpio_chip; +DECLARE_BITMAP (pending,MAX_LINE ); +int level;if (!pca953x_irq_pending (chip, pending))return IRQ_NONE ; -for (i =0 ; i <NBANK (chip); i++) { -while (pending[i]) { - level =__ffs (pending[i]); -handle_nested_irq (irq_find_mapping (chip->gpio_chip.irq.domain, - level + (BANK_SZ * i))); - pending[i] &= ~(1 << level); - } - } +for_each_set_bit (level, pending, gc->ngpio) +handle_nested_irq (irq_find_mapping (gc->irq.domain, level));return IRQ_HANDLED ; }
@@ -762,8 +740,9 @@ static int pca953x_irq_setup(struct pca953x_chip *chip, {struct i2c_client *client = chip->client;struct irq_chip *irq_chip = &chip->irq_chip; -u8 reg_direction[MAX_BANK ]; -int ret, i; +DECLARE_BITMAP (reg_direction,MAX_LINE ); +DECLARE_BITMAP (irq_stat,MAX_LINE ); +int ret;if (!client->irq)return 0 ;
@@ -774,7 +753,7 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,if (!(chip->driver_data &PCA_INT ))return 0 ; - ret =pca953x_read_regs (chip, chip->regs->input, chip->irq_stat); + ret =pca953x_read_regs (chip, chip->regs->input, irq_stat);if (ret)return ret;
@@ -784,8 +763,7 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,* this purpose. */ pca953x_read_regs (chip, chip->regs->direction, reg_direction); -for (i =0 ; i <NBANK (chip); i++) - chip->irq_stat[i] &= reg_direction[i]; +bitmap_and (chip->irq_stat, irq_stat, reg_direction, chip->gpio_chip.ngpio);mutex_init (&chip->irq_lock); ret = devm_request_threaded_irq(&client->dev, client->irq,
@@ -837,8 +815,8 @@ static int pca953x_irq_setup(struct pca953x_chip *chip,static int device_pca95xx_init (struct pca953x_chip *chip,u32 invert) { +DECLARE_BITMAP (val,MAX_LINE );int ret; -u8 val[MAX_BANK ]; ret =regcache_sync_region (chip->regmap, chip->regs->output, chip->regs->output +NBANK (chip));
@@ -852,9 +830,9 @@ static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)/* set platform specific polarity inversion */ if (invert) -memset (val,0xFF ,NBANK (chip)); +bitmap_fill (val,MAX_LINE );else -memset (val,0 ,NBANK (chip)); +bitmap_zero (val,MAX_LINE ); ret =pca953x_write_regs (chip, chip->regs->invert, val); out:
@@ -863,8 +841,8 @@ static int device_pca95xx_init(struct pca953x_chip *chip, u32 invert)static int device_pca957x_init (struct pca953x_chip *chip,u32 invert) { +DECLARE_BITMAP (val,MAX_LINE );int ret; -u8 val[MAX_BANK ]; ret =device_pca95xx_init (chip, invert);if (ret)
Signature · 1 line
2.23.0
Re: [PATCH v2 10/11] gpio: pca953x: Convert to use bitmap API
Hi Andy,
Quoted 22 lines · Andy Shevchenko
Instead of customized approach convert the driver to use bitmap API.
Depends-on:
6e9c6674d1bf("gpio: pca953x: utilize thefor_each_set_clump8macro")Cc William Breathitt Gray <[email protected]>Cc Geert Uytterhoeven <[email protected]>Cc Thomas Petazzoni <[email protected]>Cc Marek Vasut <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>drivers/gpio/gpio-pca953x.c +1
-2@@ -9,8 +9,7 @@*/ #include <linux/acpi.h> -#include <linux/bits.h> -#include <linux/bitops.h> +#include <linux/bitmap.h> #include <linux/gpio/driver.h> #include <linux/gpio/consumer.h> #include <linux/i2c.h>
drivers/gpio/gpio-pca953x.c MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids); · line 118
Quoted 4 lines
drivers/gpio/gpio-pca953x.c +1
-0@@ -116,6 +115,7 @@ MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);#define MAX_BANK 5#define BANK_SZ 8 +#define MAX_LINE (MAX_BANK * BANK_SZ)
Given (almost) everything is now bitmap (i.e. long [])-based, you might as well increase MAX_BANK to a multiple of 4 or 8, e.g. 8.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH v2 10/11] gpio: pca953x: Convert to use bitmap API
Quoted 10 lines · Geert Uytterhoeven
Quoted 5 lines · Andy Shevchenko
Instead of customized approach convert the driver to use bitmap API.
#define
MAX_BANK5
#defineBANK_SZ8
+#defineMAX_LINE(MAX_BANK*BANK_SZ)Given (almost) everything is now bitmap (i.e. long [])-based, you might as well increase
MAX_BANKto a multiple of 4 or 8, e.g. 8.
We can do it any time when we will really need it.
(Yes, I understand that we have no penalty for the change anyway)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 10/11] gpio: pca953x: Convert to use bitmap API
Hi Andy,
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Geert Uytterhoeven
Quoted 1 line · Andy Shevchenko
Instead of customized approach convert the driver to use bitmap API.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
#define
MAX_BANK5
#defineBANK_SZ8
+#defineMAX_LINE(MAX_BANK*BANK_SZ)Given (almost) everything is now bitmap (i.e. long [])-based, you might as well increase
MAX_BANKto a multiple of 4 or 8, e.g. 8.We can do it any time when we will really need it.
True. Especially as there's no real need to do it now. (sorry, my mind mixed this up with gpio-74x164...)
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
PATCH v2 11/11 gpio: pca953x: Tight up indentation
In reply to Andy ShevchenkoThere is no need to split some of the lines. However, improve the style of multi-line comment. On top of this there is no need to have double space.
Correct above indentation issues without altering the functionality.
--- drivers/gpio/gpio-pca953x.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/gpio/gpio-pca953x.c +11 -11
@@ -421,7 +421,9 @@ static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off) ret =regmap_read (chip->regmap, inreg, ®_val);mutex_unlock (&chip->i2c_lock);if (ret <0 ) { -/* NOTE: diagnostic already emitted; that's all we should +/* +* NOTE: +* diagnostic already emitted; that's all we should * do unless gpio_*_value_cansleep() calls become different * from their nonsleeping siblings (and report faults). */
@@ -735,8 +737,7 @@ static irqreturn_t pca953x_irq_handler(int irq, void *devid)return IRQ_HANDLED ; } -static int pca953x_irq_setup (struct pca953x_chip *chip, -int irq_base) +static int pca953x_irq_setup (struct pca953x_chip *chip,int irq_base) {struct i2c_client *client = chip->client;struct irq_chip *irq_chip = &chip->irq_chip;
@@ -786,9 +787,9 @@ static int pca953x_irq_setup(struct pca953x_chip *chip, irq_chip->irq_set_type = pca953x_irq_set_type; irq_chip->irq_shutdown = pca953x_irq_shutdown; - ret =gpiochip_irqchip_add_nested (&chip->gpio_chip, irq_chip, - irq_base, handle_simple_irq, -IRQ_TYPE_NONE ); + ret =gpiochip_irqchip_add_nested (&chip->gpio_chip, irq_chip, + irq_base, handle_simple_irq, +IRQ_TYPE_NONE );if (ret) {dev_err (&client->dev,"could not connect irqchip to gpiochip\n" );
@@ -862,7 +863,7 @@ static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)static const struct of_device_id pca953x_dt_ids[];static int pca953x_probe (struct i2c_client *client, -const struct i2c_device_id *i2c_id) +const struct i2c_device_id *i2c_id) {struct pca953x_platform_data *pdata;struct pca953x_chip *chip;
@@ -871,8 +872,7 @@ static int pca953x_probe(struct i2c_client *client,u32 invert =0 ;struct regulator *reg; - chip =devm_kzalloc (&client->dev, -sizeof (struct pca953x_chip ),GFP_KERNEL ); + chip =devm_kzalloc (&client->dev,sizeof (*chip),GFP_KERNEL );if (chip ==NULL )return -ENOMEM ;
@@ -986,7 +986,7 @@ static int pca953x_probe(struct i2c_client *client,if (pdata && pdata->setup) { ret = pdata->setup (client, chip->gpio_chip.base, - chip->gpio_chip.ngpio, pdata->context); + chip->gpio_chip.ngpio, pdata->context);if (ret <0 )dev_warn (&client->dev,"setup failed, %d\n" , ret); }
@@ -1006,7 +1006,7 @@ static int pca953x_remove(struct i2c_client *client)if (pdata && pdata->teardown) { ret = pdata->teardown (client, chip->gpio_chip.base, - chip->gpio_chip.ngpio, pdata->context); + chip->gpio_chip.ngpio, pdata->context);if (ret <0 )dev_err (&client->dev,"teardown failed, %d\n" , ret); }else {
Signature · 1 line
2.23.0
Quoted 4 lines · Andy Shevchenko
While converting gpio-pca953x driver to bitmap API, I noticed that we have no function to replace bits.
So, that's how patch 7 appears.
Nice, I see that Andrew picked up the patches so no action required from my side I guess? Else poke me.
Yours,
Linus Walleij