diff mbox series

[v3,4/4] gpio: xilinx: Utilize for_each_set_clump macro

Message ID 80745504d15c87aa1da0d4be3c16d1279f48615b.1588112716.git.syednwaris@gmail.com
State New
Headers show
Series Introduce the for_each_set_clump macro | expand

Commit Message

Syed Nayyar Waris April 28, 2020, 11:09 p.m. UTC
This patch reimplements the xgpio_set_multiple function in
drivers/gpio/gpio-xilinx.c to use the new for_each_set_clump macro.
Instead of looping for each bit in xgpio_set_multiple
function, now we can check each channel at a time and save cycles.

Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Cc: Michal Simek <michal.simek@xilinx.com>
Signed-off-by: Syed Nayyar Waris <syednwaris@gmail.com>
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
---
Changes in v3:
 - No change.

Changes in v2:
 - No change.

 drivers/gpio/gpio-xilinx.c | 64 ++++++++++++++++++++------------------
 1 file changed, 34 insertions(+), 30 deletions(-)

Comments

andriy.shevchenko@linux.intel.com April 29, 2020, 10:21 a.m. UTC | #1
On Wed, Apr 29, 2020 at 04:39:47AM +0530, Syed Nayyar Waris wrote:
> This patch reimplements the xgpio_set_multiple function in
> drivers/gpio/gpio-xilinx.c to use the new for_each_set_clump macro.
> Instead of looping for each bit in xgpio_set_multiple
> function, now we can check each channel at a time and save cycles.

> +	const unsigned long state_size = BITS_PER_TYPE(*state);

This '*state' is unneeded complication, use BITS_PER_U32.

> +#define TOTAL_BITS BITS_PER_TYPE(chip->gpio_state)

This macro makes code uglier, besides the fact of absence of #undef.
And also see above.

> +	DECLARE_BITMAP(old, TOTAL_BITS);
> +	DECLARE_BITMAP(new, TOTAL_BITS);
> +	DECLARE_BITMAP(changed, TOTAL_BITS);
Syed Nayyar Waris April 30, 2020, 4:15 p.m. UTC | #2
On Wed, Apr 29, 2020 at 01:21:14PM +0300, Andy Shevchenko wrote:
> On Wed, Apr 29, 2020 at 04:39:47AM +0530, Syed Nayyar Waris wrote:
> > This patch reimplements the xgpio_set_multiple function in
> > drivers/gpio/gpio-xilinx.c to use the new for_each_set_clump macro.
> > Instead of looping for each bit in xgpio_set_multiple
> > function, now we can check each channel at a time and save cycles.
> 
> > +	const unsigned long state_size = BITS_PER_TYPE(*state);
> 
> This '*state' is unneeded complication, use BITS_PER_U32.
> 
> > +#define TOTAL_BITS BITS_PER_TYPE(chip->gpio_state)
> 
> This macro makes code uglier, besides the fact of absence of #undef.
> And also see above.

Thank you for your review comments. Just want to clarify, you want
 a new macro to be created - 'BITS_PER_U32' ?  
Also, don't you think that with BITS_PER_TYPE(), in case later the type
of 'state' changes, it will be reflected in this code without any code
change?

Let me know if I have misunderstood something.

> 
> > +	DECLARE_BITMAP(old, TOTAL_BITS);
> > +	DECLARE_BITMAP(new, TOTAL_BITS);
> > +	DECLARE_BITMAP(changed, TOTAL_BITS);
> 
> -- 
> With Best Regards,
> Andy Shevchenko
> 
>
andriy.shevchenko@linux.intel.com April 30, 2020, 4:38 p.m. UTC | #3
On Thu, Apr 30, 2020 at 09:45:14PM +0530, Syed Nayyar Waris wrote:
> On Wed, Apr 29, 2020 at 01:21:14PM +0300, Andy Shevchenko wrote:
> > On Wed, Apr 29, 2020 at 04:39:47AM +0530, Syed Nayyar Waris wrote:

...

> > > +	const unsigned long state_size = BITS_PER_TYPE(*state);
> > 
> > This '*state' is unneeded complication, use BITS_PER_U32.
> > 
> > > +#define TOTAL_BITS BITS_PER_TYPE(chip->gpio_state)
> > 
> > This macro makes code uglier, besides the fact of absence of #undef.
> > And also see above.
> 
> Thank you for your review comments. Just want to clarify, you want
>  a new macro to be created - 'BITS_PER_U32' ?

It's already there (read bits.h).

> Also, don't you think that with BITS_PER_TYPE(), in case later the type
> of 'state' changes, it will be reflected in this code without any code
> change?

If it changes the bits per type will be least issues there. The rationale
behind is to have code readable. In proposed change it is not.

> Let me know if I have misunderstood something.
> 
> > 
> > > +	DECLARE_BITMAP(old, TOTAL_BITS);
> > > +	DECLARE_BITMAP(new, TOTAL_BITS);
> > > +	DECLARE_BITMAP(changed, TOTAL_BITS);
William Breathitt Gray April 30, 2020, 11:32 p.m. UTC | #4
On Thu, Apr 30, 2020 at 07:38:55PM +0300, Andy Shevchenko wrote:
> On Thu, Apr 30, 2020 at 09:45:14PM +0530, Syed Nayyar Waris wrote:
> > On Wed, Apr 29, 2020 at 01:21:14PM +0300, Andy Shevchenko wrote:
> > > On Wed, Apr 29, 2020 at 04:39:47AM +0530, Syed Nayyar Waris wrote:
> 
> ...
> 
> > > > +	const unsigned long state_size = BITS_PER_TYPE(*state);
> > > 
> > > This '*state' is unneeded complication, use BITS_PER_U32.
> > > 
> > > > +#define TOTAL_BITS BITS_PER_TYPE(chip->gpio_state)
> > > 
> > > This macro makes code uglier, besides the fact of absence of #undef.
> > > And also see above.
> > 
> > Thank you for your review comments. Just want to clarify, you want
> >  a new macro to be created - 'BITS_PER_U32' ?
> 
> It's already there (read bits.h).

I'm having trouble finding the BITS_PER_U32 macro; are you thinking of
BITS_PER_LONG? I don't think there are any cases where u32 is not 32
bits wide, so perhaps it'll be better to just hardcode 32 directly in
the code here to make it easier to read.

William Breathitt Gray

> 
> > Also, don't you think that with BITS_PER_TYPE(), in case later the type
> > of 'state' changes, it will be reflected in this code without any code
> > change?
> 
> If it changes the bits per type will be least issues there. The rationale
> behind is to have code readable. In proposed change it is not.
> 
> > Let me know if I have misunderstood something.
> > 
> > > 
> > > > +	DECLARE_BITMAP(old, TOTAL_BITS);
> > > > +	DECLARE_BITMAP(new, TOTAL_BITS);
> > > > +	DECLARE_BITMAP(changed, TOTAL_BITS);
> 
> -- 
> With Best Regards,
> Andy Shevchenko
> 
>
Andy Shevchenko May 1, 2020, 1:32 p.m. UTC | #5
On Fri, May 1, 2020 at 2:38 AM William Breathitt Gray
<vilhelm.gray@gmail.com> wrote:
> On Thu, Apr 30, 2020 at 07:38:55PM +0300, Andy Shevchenko wrote:
> > On Thu, Apr 30, 2020 at 09:45:14PM +0530, Syed Nayyar Waris wrote:
> > > On Wed, Apr 29, 2020 at 01:21:14PM +0300, Andy Shevchenko wrote:
> > > > On Wed, Apr 29, 2020 at 04:39:47AM +0530, Syed Nayyar Waris wrote:
> >
> > ...
> >
> > > > > +       const unsigned long state_size = BITS_PER_TYPE(*state);
> > > >
> > > > This '*state' is unneeded complication, use BITS_PER_U32.
> > > >
> > > > > +#define TOTAL_BITS BITS_PER_TYPE(chip->gpio_state)
> > > >
> > > > This macro makes code uglier, besides the fact of absence of #undef.
> > > > And also see above.
> > >
> > > Thank you for your review comments. Just want to clarify, you want
> > >  a new macro to be created - 'BITS_PER_U32' ?
> >
> > It's already there (read bits.h).
>
> I'm having trouble finding the BITS_PER_U32 macro; are you thinking of
> BITS_PER_LONG?

Oh, my bad. I messed above with BITS_TO_U32() which is not what we want here.

> I don't think there are any cases where u32 is not 32
> bits wide, so perhaps it'll be better to just hardcode 32 directly in
> the code here to make it easier to read.

Yes, would work!
diff mbox series

Patch

diff --git a/drivers/gpio/gpio-xilinx.c b/drivers/gpio/gpio-xilinx.c
index 67f9f82e0db0..428207f9ab91 100644
--- a/drivers/gpio/gpio-xilinx.c
+++ b/drivers/gpio/gpio-xilinx.c
@@ -136,39 +136,43 @@  static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
 			       unsigned long *bits)
 {
-	unsigned long flags;
+	unsigned long flags[2];
 	struct xgpio_instance *chip = gpiochip_get_data(gc);
-	int index = xgpio_index(chip, 0);
-	int offset, i;
-
-	spin_lock_irqsave(&chip->gpio_lock[index], flags);
-
-	/* Write to GPIO signals */
-	for (i = 0; i < gc->ngpio; i++) {
-		if (*mask == 0)
-			break;
-		/* Once finished with an index write it out to the register */
-		if (index !=  xgpio_index(chip, i)) {
-			xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
-				       index * XGPIO_CHANNEL_OFFSET,
-				       chip->gpio_state[index]);
-			spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
-			index =  xgpio_index(chip, i);
-			spin_lock_irqsave(&chip->gpio_lock[index], flags);
-		}
-		if (__test_and_clear_bit(i, mask)) {
-			offset =  xgpio_offset(chip, i);
-			if (test_bit(i, bits))
-				chip->gpio_state[index] |= BIT(offset);
-			else
-				chip->gpio_state[index] &= ~BIT(offset);
-		}
+	u32 *const state = chip->gpio_state;
+	unsigned int *const width = chip->gpio_width;
+	const unsigned long state_size = BITS_PER_TYPE(*state);
+	unsigned long offset, clump;
+	size_t index;
+
+#define TOTAL_BITS BITS_PER_TYPE(chip->gpio_state)
+	DECLARE_BITMAP(old, TOTAL_BITS);
+	DECLARE_BITMAP(new, TOTAL_BITS);
+	DECLARE_BITMAP(changed, TOTAL_BITS);
+
+	spin_lock_irqsave(&chip->gpio_lock[0], flags[0]);
+	spin_lock_irqsave(&chip->gpio_lock[1], flags[1]);
+
+	bitmap_set_value(old, state[0], 0, width[0]);
+	bitmap_set_value(old, state[1], width[0], width[1]);
+	bitmap_replace(new, old, bits, mask, gc->ngpio);
+
+	bitmap_set_value(old, state[0], 0, state_size);
+	bitmap_set_value(old, state[1], state_size, state_size);
+	state[0] = bitmap_get_value(new, 0, width[0]);
+	state[1] = bitmap_get_value(new, width[0], width[1]);
+	bitmap_set_value(new, state[0], 0, state_size);
+	bitmap_set_value(new, state[1], state_size, state_size);
+	bitmap_xor(changed, old, new, TOTAL_BITS);
+
+	for_each_set_clump(offset, clump, changed, TOTAL_BITS, state_size) {
+		index = offset / state_size;
+		xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
+				index * XGPIO_CHANNEL_OFFSET,
+				state[index]);
 	}
 
-	xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
-		       index * XGPIO_CHANNEL_OFFSET, chip->gpio_state[index]);
-
-	spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
+	spin_unlock_irqrestore(&chip->gpio_lock[1], flags[1]);
+	spin_unlock_irqrestore(&chip->gpio_lock[0], flags[0]);
 }
 
 /**