Skip to contentSkip to conversation

gpiolib: add set/get batch v4

RFC In review Jaya Kumar 3 participants 9 messages Jan 17, 2009 – Jan 19, 2009

Subsystems (7)
lore
Share & tools
lore

Jaya Kumar

author
#0 lore raw

RFC 1/2 gpiolib: add set/get batch v4

Hi friends,

This is v4 of batch support for gpiolib. Thanks to David Brownell, Eric Miao, David Hylands, Robin, Ben, Jamie and others for prior feedback. The post for v3 summarized the previous discussion. Since then the changes I've made are:
- split the patches into generic and arch specific
- optimizing the empty mask case
- adjusting the API to add error returns
- documenting this optional API in gpio.txt
- cleanup of the commenting

Please let me know your thoughts and feedback.

Thanks,
jaya

---
 Documentation/gpio.txt     |   60 ++++++++
 drivers/gpio/Kconfig       |    5 +
 drivers/gpio/gpiolib.c     |  323 ++++++++++++++++++++++++++++++++++++++++++++
 include/asm-generic/gpio.h |   19 +++-
 4 files changed, 406 insertions(+), 1 deletions(-)
Documentation/gpio.txt +60 -0
index b1b9887..d7e5fe9 100644
@@ -185,6 +185,66 @@ and not to need spinlocks.  Such optimized calls can make bitbanging
 applications a lot more efficient (in both space and time) than spending
 dozens of instructions on subroutine calls.
 
+[OPTIONAL] Spinlock-Safe GPIO Batch access
+------------------------------------------
+The original GPIO API implements single bit access to GPIO pins. Some
+drivers for devices that treat GPIO as a mechanism for bulk data transfer
+may find the performance of the single bit API to be inadequate. In this
+scenario, the user can consider enabling the batch access API. This API is
+as follows:
+
+	/* BATCH GPIO INPUT */
+int gpio_get_batch(unsigned gpio, u32 bitmask, int maskwidth, u32 *result);
+
+The following examples help explain how this function is to be used.
+ Q: How to get gpio pins 0 through 7? (8 bits)
+ A: gpio_get_batch(gpio=0, bitmask=0xFF, width=8, &result) result=0xnn
+ Q: How to get gpio pins 58 through 73? (16 bits)
+ A: gpio_get_batch(gpio=58, bitmask=0xFFFF, width=16, &result) result=0xnnnn
+ Q: How to get gpio pins 16 through 47? (32 bits)
+ A: gpio_get_batch(offset=16, bitmask=0xFFFFFFFF, width=32, &result)
+ A: result=0xnnnnnnnn
+
+	/* BATCH GPIO OUTPUT */
+int gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int maskwidth);
+
+The following examples help explain how this function is to be used.
+ Q: How to set gpio pins 0 through 7 to all 0? (8 bits)
+ A: gpio_set_batch(gpio=0, values=0x0, bitmask=0xFF, width=8);
+ Q: How to set gpio pins 58 through 73 to all 1? (16 bits)
+ A: gpio_set_batch(gpio=58, values=0xFFFF, bitmask=0xFFFF, width=16);
+ Q: How to set gpio pins 16 through 47 to 0xCAFEC001? (32 bits)
+ A: gpio_set_batch(gpio=16, values=0xCAFEC001, bitmask=0xFFFFFFFF, width=32);
+
+The following example shows the use of the batch API and a comparison with
+the original single bit API:
+
+Original input method which loops through a set of pins:
+-       for (i = 0; i <= (DB15_GPIO_PIN - DB0_GPIO_PIN) ; i++)
+-               res |= (gpio_get_value(DB0_GPIO_PIN + i)) ? (1 << i) : 0;
+
+Batch input method:
++       u16 val;
++       err = gpio_get_batch(DB0_GPIO_PIN, 0xFFFF, 16, &val);
+
+Original output method:
+-       for (i = 0; i <= (DB15_GPIO_PIN - DB0_GPIO_PIN) ; i++)
+-               gpio_set_value(DB0_GPIO_PIN + i, (data >> i) & 0x01);
+Batch output method:
++       int err;
++       err = gpio_set_batch(DB0_GPIO_PIN, data, 0xFFFF, 16);
+
+Using these calls for GPIOs that can't safely be accessed without sleeping
+(see below) is an error.
+
+Platform-specific implementations are encouraged to optimize the two
+calls by checking if the batch get/set requested can be achieved within the
+platform's fast path access to gpio registers. For example, if the starting
+gpio and width of bits to be written is contained within a single register
+then the platform specific implementation may choose to execute it. If the
+request is more complex, then the platform specific implementation can
+choose to call upon the platform independent __gpio_get/set_batch functions
+which are able to cross gpio_chip boundaries.
 
 GPIO access that may sleep
 --------------------------
drivers/gpio/Kconfig +5 -0
index 3d25654..474070b 100644
@@ -37,6 +37,11 @@ menuconfig GPIOLIB
 
 if GPIOLIB
 
+config GPIOLIB_BATCH
+	bool "Batch GPIO support"
+	help
+	  Say Y here to add the capability to batch set/get GPIOs.
+
 config DEBUG_GPIO
 	bool "Debug GPIO calls"
 	depends on DEBUG_KERNEL
drivers/gpio/gpiolib.c +323 -0

335 lines not on this page · raw message

include/asm-generic/gpio.h +18 -1
index 81797ec..29478d2 100644
@@ -44,6 +44,10 @@ struct module;
  *	returns either the value actually sensed, or zero
  * @direction_output: configures signal "offset" as output, or returns error
  * @set: assigns output value for signal "offset"
+ * @set_batch: batch assigns output values for signals starting at
+ *	"offset" with mask in "bitmask" all within this gpio_chip
+ * @get_batch: batch fetches values for consecutive signals starting at
+ *	"offset" with mask in "bitmask" all within this gpio_chip
  * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
  *	implementation may not sleep
  * @dbg_show: optional routine to show contents in debugfs; default code
@@ -84,7 +88,14 @@ struct gpio_chip {
 						unsigned offset, int value);
 	void			(*set)(struct gpio_chip *chip,
 						unsigned offset, int value);
-
+#ifdef CONFIG_GPIOLIB_BATCH
+	int			(*set_batch)(struct gpio_chip *chip,
+						unsigned offset, u32 values,
+						u32 bitmask, int width);
+	int			(*get_batch)(struct gpio_chip *chip,
+						unsigned offset, u32 bitmask,
+						int width, u32 *result);
+#endif
 	int			(*to_irq)(struct gpio_chip *chip,
 						unsigned offset);
 
@@ -124,6 +135,12 @@ extern void gpio_set_value_cansleep(unsigned gpio, int value);
  */
 extern int __gpio_get_value(unsigned gpio);
 extern void __gpio_set_value(unsigned gpio, int value);
+#ifdef CONFIG_GPIOLIB_BATCH
+extern int __gpio_set_batch(unsigned gpio, u32 values, u32 bitmask,
+				int bitwidth);
+extern int __gpio_get_batch(unsigned gpio, u32 bitmask, int bitwidth,
+				u32 *result);
+#endif
 
 extern int __gpio_cansleep(unsigned gpio);
 
Signature · 1 line
1.5.2.3
author
#1 lore raw

RFC 2/2 mach-pxa: add and use batch set/get gpio

This patch adds support for pxa specific batch set/get of gpio. This is exported to gpiolib via the gpio_chip interface and then used within am300epd.c

---
 arch/arm/mach-pxa/Kconfig             |    1 +
 arch/arm/mach-pxa/am300epd.c          |   23 ++++++------
 arch/arm/mach-pxa/gpio.c              |   63 +++++++++++++++++++++++++++++++++
 arch/arm/mach-pxa/include/mach/gpio.h |   48 +++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 11 deletions(-)
arch/arm/mach-pxa/Kconfig +1 -0
index f844425..1b3e631 100644
@@ -41,6 +41,7 @@ config GUMSTIX_AM200EPD
 	bool "Enable AM200EPD board support"
 
 config GUMSTIX_AM300EPD
+	select GPIOLIB_BATCH
 	bool "Enable AM300EPD board support"
 
 endchoice
arch/arm/mach-pxa/am300epd.c +12 -11
index 4bd10a1..c065368 100644
@@ -187,24 +187,25 @@ static void am300_cleanup(struct broadsheetfb_par *par)
 
 static u16 am300_get_hdb(struct broadsheetfb_par *par)
 {
-	u16 res = 0;
-	int i;
-
-	for (i = 0; i <= (DB15_GPIO_PIN - DB0_GPIO_PIN) ; i++)
-		res |= (gpio_get_value(DB0_GPIO_PIN + i)) ? (1 << i) : 0;
+	int err;
+	u32 val;
 
-	return res;
+	err = gpio_get_batch(DB0_GPIO_PIN, 0xFFFF, 16, &val);
+	if (err) {
+		dev_err(&am300_device->dev, "get failed: %d\n", err);
+		return 0;
+	}
+	return (u16) val;
 }
 
 static void am300_set_hdb(struct broadsheetfb_par *par, u16 data)
 {
-	int i;
-
-	for (i = 0; i <= (DB15_GPIO_PIN - DB0_GPIO_PIN) ; i++)
-		gpio_set_value(DB0_GPIO_PIN + i, (data >> i) & 0x01);
+	int err;
+	err = gpio_set_batch(DB0_GPIO_PIN, data, 0xFFFF, 16);
+	if (err)
+		dev_err(&am300_device->dev, "set failed: %d\n", err);
 }
 
-
 static void am300_set_ctl(struct broadsheetfb_par *par, unsigned char bit,
 				u8 state)
 {
arch/arm/mach-pxa/gpio.c +63 -0

75 lines not on this page · raw message

arch/arm/mach-pxa/include/mach/gpio.h +48 -0

54 lines not on this page · raw message

Signature · 1 line
1.5.2.3
#2 lore raw
In reply to Jaya Kumar
Quoted 5 lines · Jaya Kumar

Hi friends,

Documentation/gpio.txt +2 -0
+
+	/* BATCH GPIO OUTPUT */

Documentation/gpio.txt and not to need spinlocks. Such optimized calls can make bitbanging · line 218

Quoted 10 lines
Documentation/gpio.txt +10 -0
+int gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int maskwidth);
+
+The following examples help explain how this function is to be used.
+ Q: How to set gpio pins 0 through 7 to all 0? (8 bits)
+ A: gpio_set_batch(gpio=0, values=0x0, bitmask=0xFF, width=8);
+ Q: How to set gpio pins 58 through 73 to all 1? (16 bits)
+ A: gpio_set_batch(gpio=58, values=0xFFFF, bitmask=0xFFFF, width=16);
+ Q: How to set gpio pins 16 through 47 to 0xCAFEC001? (32 bits)
+ A: gpio_set_batch(gpio=16, values=0xCAFEC001, bitmask=0xFFFFFFFF, width=32);
+

Can the gpio_set_batch function be used to set non-consecutive gpios? For example:

  gpio_set_batch(0, 0x0, 0x88, 8);

To clear gpios 3 and 7? It looks like the pxa implementation will support this, but can it be guaranteed for other architectures? If so, can we put an example in the documentation. If not, can we make it clear that you shouldn't do this in the documentation. Also , in the latter case is it necessary to pass the bitmask, since it will just be ((1 << bitwidth) - 1)?

~Ryan

Signature · 13 lines
Bluewater Systems Ltd - ARM Technology Solution Centre

       Ryan Mallon                              Unit 5, Amuri Park
       Phone: +64 3 3779127                     404 Barbadoes St
       Fax:   +64 3 3779135                     PO Box 13 889
       Email: [email protected]             Christchurch, 8013
       Web:   http://www.bluewatersys.com       New Zealand
       Freecall Australia  1800 148 751         USA 1800 261 2934

-------------------------------------------------------------------
List admin: http://lists.arm.linux.org.uk/mailman/listinfo/linux-arm-kernel
FAQ:        http://www.arm.linux.org.uk/mailinglists/faq.php
Etiquette:  http://www.arm.linux.org.uk/mailinglists/etiquette.php
author
#3 lore raw
Quoted 20 lines · Ryan Mallon
Quoted 5 lines · Jaya Kumar

Hi friends,

Documentation/gpio.txt +2 -0
+
+     /* BATCH GPIO OUTPUT */

Documentation/gpio.txt and not to need spinlocks. Such optimized calls can make bitbanging · line 218

Quoted 10 lines
Documentation/gpio.txt +10 -0
+int gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int maskwidth);
+
+The following examples help explain how this function is to be used.
+ Q: How to set gpio pins 0 through 7 to all 0? (8 bits)
+ A: gpio_set_batch(gpio=0, values=0x0, bitmask=0xFF, width=8);
+ Q: How to set gpio pins 58 through 73 to all 1? (16 bits)
+ A: gpio_set_batch(gpio=58, values=0xFFFF, bitmask=0xFFFF, width=16);
+ Q: How to set gpio pins 16 through 47 to 0xCAFEC001? (32 bits)
+ A: gpio_set_batch(gpio=16, values=0xCAFEC001, bitmask=0xFFFFFFFF, width=32);
+

Can the gpio_set_batch function be used to set non-consecutive gpios? For example:

Quoted 3 lines

gpio_set_batch(0, 0x0, 0x88, 8);

To clear gpios 3 and 7? It looks like the pxa implementation will

Hi Ryan,

For the first part, yes, it can do non-consecutive gpios by using the mask. Pins 3 and 7 are handled using a 5-bit mask. You'd do gpio_set_batch(3 <- starting pin is gpio 3, 0x0 <- clear, 0x1F <- mask, 5 <- bit width of mask);

Quoted 1 line

support this, but can it be guaranteed for other architectures? If so,

That's a tough question. My basic answer would be yes because I've provided the generic set_batch handler that just uses single bit sets to achieve it. See __generic_gpio_set_batch() in patch). But if your question is deeper, ie: can it be optimized for other architectures? ; then I think I have to handwave a bit. I believe it can, as most of the CPUs I've seen expose gpio via a set register and clear register with variation between a context register to select which bank/module of gpios is being accessed versus just having a pool of set and clear registers (like pxa). If a CPU didn't have this and just had a blanket gpio register then the implementation would have to first read the previous register value (or cache it) in order to support doing the mask.

Quoted 1 line

can we put an example in the documentation. If not, can we make it clear

Good point. I'll put it in the docs.

Quoted 3 lines

that you shouldn't do this in the documentation. Also , in the latter case is it necessary to pass the bitmask, since it will just be ((1 << bitwidth) - 1)?

Yes, it is as you have non-consecutive bits. In the above case the mask width is 5 bits, but 3 bits are masked off, so we need the mask so that the caller can tell us which bits are masked off.

Thanks,
jaya

author
#4 lore raw
Quoted 23 lines · Jaya Kumar
Quoted 22 lines · Ryan Mallon
Quoted 5 lines · Jaya Kumar

Hi friends,

Documentation/gpio.txt +2 -0
+
+     /* BATCH GPIO OUTPUT */

Documentation/gpio.txt and not to need spinlocks. Such optimized calls can make bitbanging · line 218

Quoted 10 lines
Documentation/gpio.txt +10 -0
+int gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int maskwidth);
+
+The following examples help explain how this function is to be used.
+ Q: How to set gpio pins 0 through 7 to all 0? (8 bits)
+ A: gpio_set_batch(gpio=0, values=0x0, bitmask=0xFF, width=8);
+ Q: How to set gpio pins 58 through 73 to all 1? (16 bits)
+ A: gpio_set_batch(gpio=58, values=0xFFFF, bitmask=0xFFFF, width=16);
+ Q: How to set gpio pins 16 through 47 to 0xCAFEC001? (32 bits)
+ A: gpio_set_batch(gpio=16, values=0xCAFEC001, bitmask=0xFFFFFFFF, width=32);
+

Can the gpio_set_batch function be used to set non-consecutive gpios? For example:

gpio_set_batch(0, 0x0, 0x88, 8);

Quoted 8 lines
Quoted 1 line

To clear gpios 3 and 7? It looks like the pxa implementation will

Hi Ryan,

For the first part, yes, it can do non-consecutive gpios by using the mask. Pins 3 and 7 are handled using a 5-bit mask. You'd do gpio_set_batch(3 <- starting pin is gpio 3, 0x0 <- clear, 0x1F <- mask, 5 <- bit width of mask);

Correction: I meant to write 0x11 for the mask above (instead of 0x1F) since you only want to clear the starting pin 3 and the ending pin 7 in the 5 bits. If we used 0x1F here, then we would clear all 5 bits rather than just 3 and 7.

Thanks,
jaya

#5 lore raw
In reply to Jaya Kumar
Quoted 32 lines · Jaya Kumar
Quoted 18 lines · Ryan Mallon
Quoted 4 lines · Jaya Kumar

Hi friends,

Documentation/gpio.txt +2 -0
+
+     /* BATCH GPIO OUTPUT */

Documentation/gpio.txt and not to need spinlocks. Such optimized calls can make bitbanging · line 218

Quoted 10 lines
Documentation/gpio.txt +10 -0
+int gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int maskwidth);
+
+The following examples help explain how this function is to be used.
+ Q: How to set gpio pins 0 through 7 to all 0? (8 bits)
+ A: gpio_set_batch(gpio=0, values=0x0, bitmask=0xFF, width=8);
+ Q: How to set gpio pins 58 through 73 to all 1? (16 bits)
+ A: gpio_set_batch(gpio=58, values=0xFFFF, bitmask=0xFFFF, width=16);
+ Q: How to set gpio pins 16 through 47 to 0xCAFEC001? (32 bits)
+ A: gpio_set_batch(gpio=16, values=0xCAFEC001, bitmask=0xFFFFFFFF, width=32);
+

Can the gpio_set_batch function be used to set non-consecutive gpios? For example:

Quoted 3 lines

gpio_set_batch(0, 0x0, 0x88, 8);

To clear gpios 3 and 7? It looks like the pxa implementation will

Hi Ryan,

For the first part, yes, it can do non-consecutive gpios by using the mask. Pins 3 and 7 are handled using a 5-bit mask. You'd do gpio_set_batch(3 <- starting pin is gpio 3, 0x0 <- clear, 0x1F <- mask, 5 <- bit width of mask);

Quoted 1 line

support this, but can it be guaranteed for other architectures? If so,

Quoted 12 lines

That's a tough question. My basic answer would be yes because I've provided the generic set_batch handler that just uses single bit sets to achieve it. See __generic_gpio_set_batch() in patch). But if your question is deeper, ie: can it be optimized for other architectures? ; then I think I have to handwave a bit. I believe it can, as most of the CPUs I've seen expose gpio via a set register and clear register with variation between a context register to select which bank/module of gpios is being accessed versus just having a pool of set and clear registers (like pxa). If a CPU didn't have this and just had a blanket gpio register then the implementation would have to first read the previous register value (or cache it) in order to support doing the mask.

The EP93xx has a single register for reading, set and clearing the states of gpios, so it would have to be read, masked and written. I'm not sure if that would be quicker than doing a for-loop over the 8-bits in the register (the EP93xx has 8 register banks of 8 gpios, each with their own reigster set). I guess that even if it cannot be optimised for a given architecture, then the standard for-loop method can be used. As long as it is documented that non-consecutive setting should work so that arch implementers can get it correct.

Quoted 3 lines
Quoted 1 line

can we put an example in the documentation. If not, can we make it clear

Good point. I'll put it in the docs.

Cool, thanks.

~Ryan

Signature · 8 lines
Bluewater Systems Ltd - ARM Technology Solution Centre

       Ryan Mallon                              Unit 5, Amuri Park
       Phone: +64 3 3779127                     404 Barbadoes St
       Fax:   +64 3 3779135                     PO Box 13 889
       Email: [email protected]             Christchurch, 8013
       Web:   http://www.bluewatersys.com       New Zealand
       Freecall Australia  1800 148 751         USA 1800 261 2934
#6 lore raw
In reply to Jaya Kumar

Hi Jaya,

Quoted 6 lines · Jaya Kumar

Hi friends,

This is v4 of batch support for gpiolib. Thanks to David Brownell, Eric Miao, David Hylands, Robin, Ben, Jamie and others for prior feedback. The post for v3 summarized the previous discussion. Since then the changes I've made are:
- split the patches into generic and arch specific

IMHO this should be three patches: "gpiolib", "pxa" and "am300epd". Well, ...

Quoted 1 line

+[OPTIONAL] Spinlock-Safe GPIO Batch access

Is it really spinlock safe in general? Or only if gpio_cansleep(gpio) if false for each gpio to get or set?

drivers/gpio/gpiolib.c static inline void gpiochip_unexport(struct gpio_chip *chip) · line 679

Quoted 2 lines
drivers/gpio/gpiolib.c +2 -0
+static int __generic_gpio_set_batch(struct gpio_chip *chip, unsigned offset,
+					u32 values, u32 bitmask, int width)

IMHO a better name is __gpio_set_batch_generic (or __gpiolib_set_batch_generic?), YMMV.

Quoted 1 line

+int __gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int bitwidth)

Sometimes your width parameter is called bitwidth, sometimes width. I'd like to have that consistant.

While talking about this parameter. I don't really like it, because you can calculate it from bitmask. In an earlier mail you write:

bitwidth (needed to iterate and map to chip ngpios) could be calculated from bitmask, but that involves iteratively counting bits from the mask, so we would have to do 800*600 bit counts. Unless, we do ugly things like cache the previous bitwidth/mask and compare against the current caller arguments. Given that the bitwidth would typically be a fixed value, I believe it is more intuitive for the caller to provide it, ...

I think it's easier than that. bitwidth is just fls(bitmask) which should be efficient enough not to bother the programmer. If bitmask is constant it's even the compiler that does the work here.

BTW, I wonder why the argument to fls has type int and not unsigned.

Best regards,
Uwe

Signature · 4 lines
Pengutronix e.K.                              | Uwe Kleine-König            |
Industrial Linux Solutions                    | http://www.pengutronix.de/  |
Peiner Strasse 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686              | Fax:   +49-5121-206917-5555 |
author
#7 lore raw
Quoted 11 lines · Uwe Kleine-König

Hi Jaya,

Quoted 6 lines · Jaya Kumar

Hi friends,

This is v4 of batch support for gpiolib. Thanks to David Brownell, Eric Miao, David Hylands, Robin, Ben, Jamie and others for prior feedback. The post for v3 summarized the previous discussion. Since then the changes I've made are:
- split the patches into generic and arch specific

IMHO this should be three patches: "gpiolib", "pxa" and "am300epd". Well, ...

Hi Uwe,

Ok, will do.

Quoted 4 lines
Quoted 2 lines

+[OPTIONAL] Spinlock-Safe GPIO Batch access

Is it really spinlock safe in general? Or only if gpio_cansleep(gpio) if false for each gpio to get or set?

You are correct to raise this issue. It is only spinlock safe if chip->cansleep is false. Initially, I wasn't sure what to do. The

original gpio set/get_value() just does;
        WARN_ON(extra_checks && chip->can_sleep);

and it is documented as:

"
Spinlock-Safe GPIO access
-------------------------
<snip>
return zero. Also, using these calls for GPIOs that can't safely be accessed without sleeping (see below) is an error.
"

I will change this in the batch code to return an error if can_sleep is detected on any involved gpio_chip.

Quoted 5 lines

drivers/gpio/gpiolib.c static inline void gpiochip_unexport(struct gpio_chip *chip) · line 679

Quoted 3 lines
drivers/gpio/gpiolib.c +2 -0
+static int __generic_gpio_set_batch(struct gpio_chip *chip, unsigned offset,
+                                     u32 values, u32 bitmask, int width)

IMHO a better name is __gpio_set_batch_generic (or __gpiolib_set_batch_generic?), YMMV.

Agreed. Will change.

Quoted 4 lines
Quoted 2 lines

+int __gpio_set_batch(unsigned gpio, u32 values, u32 bitmask, int bitwidth)

Sometimes your width parameter is called bitwidth, sometimes width. I'd like to have that consistant.

Ok, you're right. I'll fix this.

Quoted 3 lines

While talking about this parameter. I don't really like it, because you can calculate it from bitmask. In an earlier mail you write:

Agreed.

Quoted 9 lines

bitwidth (needed to iterate and map to chip ngpios) could be calculated from bitmask, but that involves iteratively counting bits from the mask, so we would have to do 800*600 bit counts. Unless, we do ugly things like cache the previous bitwidth/mask and compare against the current caller arguments. Given that the bitwidth would typically be a fixed value, I believe it is more intuitive for the caller to provide it, ...

Quoted 4 lines

I think it's easier than that. bitwidth is just fls(bitmask) which should be efficient enough not to bother the programmer. If bitmask is constant it's even the compiler that does the work here.

That is a good point. I agree that width is ugly in the main API. It is just fls(mask) and I now realize that this is an inline so you're right it would get taken care of by the compiler. fls is checked with __constant_fls first. Beauty! Thanks Uwe! I'll make these changes.

Thanks,
jaya

#8 lore raw

Hi Jaya,

Quoted 18 lines
Quoted 3 lines
Quoted 1 line

+[OPTIONAL] Spinlock-Safe GPIO Batch access

Is it really spinlock safe in general? Or only if gpio_cansleep(gpio) if false for each gpio to get or set?

You are correct to raise this issue. It is only spinlock safe if chip->cansleep is false. Initially, I wasn't sure what to do. The

original gpio set/get_value() just does;
        WARN_ON(extra_checks && chip->can_sleep);

and it is documented as:

"
Spinlock-Safe GPIO access
-------------------------
<snip>
return zero. Also, using these calls for GPIOs that can't safely be accessed without sleeping (see below) is an error.
"

Quoted 2 lines

I will change this in the batch code to return an error if can_sleep is detected on any involved gpio_chip.

Wait, I got it wrong. I thought gpio_set_value might sleep if chip->cansleep is true, but there are extra API functions for cansleep-chips. So I'd do it the same way as for the non-batch functions and just WARN_ON(extra_checks && chip->cansleep) for each involved chip.

Later it might make sense to add the _cansleep variants.

Best regards
Uwe

Signature · 4 lines
Pengutronix e.K.                              | Uwe Kleine-König            |
Industrial Linux Solutions                    | http://www.pengutronix.de/  |
Peiner Strasse 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686              | Fax:   +49-5121-206917-5555 |