gpio: Add block GPIO
Subsystems (5)
v4 cover gpio: Add block GPIO
This set of patches adds:
* Block GPIO API to gpiolib
* Sysfs support for GPIO API, to provide userland access
* Devicetree support to instantiate GPIO blocks via DT
* Example implementations in several gpio drivers since they need special accessor functions for block wise GPIO access
--
Changes since v3:
* Added driver support for pca953x, em, pl061, max732x, pcf857x
* Coding style improvements
* Fixed krealloc memory leak in error case
* sysfs: values in hex
* Register blocks in a list
* Narrowing lock scope
* Use S_IWUSR and S_IRUGO instead of direct octal values
* Use for_each_set_bit()
* Change from unsigned to unsigned long for masks and values
Changes since v2:
* Added sysfs support
* Added devicetree support
* Added support for lpc32xx, generic
* Added functions for GPIO block registration
* Added more error checking
* Bit remapping bugfix
Changes since v1:
* API change to 32/64 bit word, bit masks
Thanks to Ryan Mallon, Linus Walleij, Stijn Devriendt, Jean-Christophe Plagniol-Villard and Mark Brown for reviewing!
Roland Stigge (11):
gpio: Add a block GPIO API to gpiolib
gpio: Add sysfs support to block GPIO API
gpio: Add device tree support to block GPIO API
gpio-max730x: Add block GPIO API
gpio-lpc32xx: Add block GPIO API
gpio-generic: Add block GPIO API
gpio-pca953x: Add block GPIO API
gpio-em: Add block GPIO API
gpio-pl061: Add block GPIO API
gpio-max732x: Add block GPIO API
gpio-pcf857x: Add block GPIO API
Documentation/ABI/testing/sysfs-gpio | 6 Documentation/devicetree/bindings/gpio/gpio-block.txt | 36 + Documentation/gpio.txt | 45 + drivers/gpio/Makefile | 1 drivers/gpio/gpio-em.c | 23 drivers/gpio/gpio-generic.c | 56 ++ drivers/gpio/gpio-lpc32xx.c | 82 +++ drivers/gpio/gpio-max730x.c | 61 ++ drivers/gpio/gpio-max732x.c | 59 ++ drivers/gpio/gpio-pca953x.c | 64 ++ drivers/gpio/gpio-pcf857x.c | 24 + drivers/gpio/gpio-pl061.c | 17 drivers/gpio/gpioblock-of.c | 84 +++ drivers/gpio/gpiolib.c | 428 ++++++++++++++++++ include/asm-generic/gpio.h | 26 + include/linux/gpio.h | 87 +++ 16 files changed, 1098 insertions(+), 1 deletion(-)
RFC PATCH v4 1/11 gpio: Add a block GPIO API to gpiolib
The recurring task of providing simultaneous access to GPIO lines (especially for bit banging protocols) needs an appropriate API.
This patch adds a kernel internal "Block GPIO" API that enables simultaneous access to several GPIOs. This is done by abstracting GPIOs to an n-bit word:
Once requested, it provides access to a group of GPIOs which can range over multiple GPIO chips.
---
Documentation/gpio.txt | 45 +++++++++ drivers/gpio/gpiolib.c | 217 +++++++++++++++++++++++++++++++++++++++++++++ include/asm-generic/gpio.h | 15 +++ include/linux/gpio.h | 74 +++++++++++++++ 4 files changed, 351 insertions(+)
Documentation/gpio.txt +45 -0
@@ -439,6 +439,51 @@ slower clock delays the rising edge of S signaling rate accordingly. +Block GPIO +---------- + +The above described interface concentrates on handling single GPIOs. However, +in applications where it is critical to set several GPIOs at once, this +interface doesn't work well, e.g. bit-banging protocols via grouped GPIO lines. +Consider a GPIO controller that is connected via a slow I2C line. When +switching two or more GPIOs one after another, there can be considerable time +between those events. This is solved by an interface called Block GPIO: + +struct gpio_block *gpio_block_create(unsigned int *gpios, size_t size); + +This creates a new block of GPIOs as a list of GPIO numbers with the specified +size which are accessible via the returned struct gpio_block and the accessor +functions described below. Please note that you need to request the GPIOs +separately via gpio_request(). An arbitrary list of globally valid GPIOs can be +specified, even ranging over several gpio_chips. Actual handling of I/O +operations will be done on a best effort base, i.e. simultaneous I/O only where +possible by hardware and implemented in the respective GPIO driver. The number +of GPIOs in one block is limited to 32 on a 32 bit system, and 64 on a 64 bit +system. However, several blocks can be defined at once. + +unsigned gpio_block_get(struct gpio_block *block); +void gpio_block_set(struct gpio_block *block, unsigned value); + +With those accessor functions, setting and getting the GPIO values is possible, +analogous to gpio_get_value() and gpio_set_value(). Each bit in the return +value of gpio_block_get() and in the value argument of gpio_block_set() +corresponds to a bit specified on gpio_block_create(). Block operations in +hardware are only possible where the respective GPIO driver implements it, +falling back to using single GPIO operations where the driver only implements +single GPIO access. + +void gpio_block_free(struct gpio_block *block); + +After the GPIO block isn't used anymore, it should be free'd via +gpio_block_free(). + +int gpio_block_register(struct gpio_block *block); +void gpio_block_unregister(struct gpio_block *block); + +These functions can be used to register a GPIO block. Blocks registered this +way will be available via sysfs. + + What do these conventions omit? =============================== One of the biggest things these conventions omit is pin multiplexing, since
drivers/gpio/gpiolib.c +217 -0
229 lines not on this page · raw message
include/asm-generic/gpio.h +15 -0
@@ -43,6 +43,7 @@ static inline bool gpio_is_valid(int numstruct device ;struct gpio ; +struct gpio_block ;struct seq_file ;struct module ;struct device_node ;
@@ -105,6 +106,8 @@ struct gpio_chip { unsigned offset);int (*get)(struct gpio_chip *chip,unsigned offset); +unsigned long (*get_block)(struct gpio_chip *chip, +unsigned long mask);int (*direction_output)(struct gpio_chip *chip,unsigned offset,int value); int (*set_debounce)(struct gpio_chip *chip,
@@ -112,6 +115,9 @@ struct gpio_chip {void (*set)(struct gpio_chip *chip,unsigned offset,int value); +void (*set_block)(struct gpio_chip *chip, +unsigned long mask, +unsigned long values);int (*to_irq)(struct gpio_chip *chip,unsigned offset);
@@ -171,6 +177,15 @@ extern void gpio_set_value_cansleep(unsiextern int __gpio_get_value (unsigned gpio);extern void __gpio_set_value (unsigned gpio,int value); +extern struct gpio_block *gpio_block_create (unsigned *gpio,size_t size, +const char *name); +extern void gpio_block_free (struct gpio_block *block); +extern unsigned long gpio_block_get (const struct gpio_block *block); +extern void gpio_block_set (struct gpio_block *block,unsigned long values); +extern struct gpio_block *gpio_block_find_by_name (const char *name); +extern int gpio_block_register (struct gpio_block *block); +extern void gpio_block_unregister (struct gpio_block *block); +extern int __gpio_cansleep (unsigned gpio);extern int __gpio_to_irq (unsigned gpio);
include/linux/gpio.h +74 -0
92 lines not on this page · raw message
Re: [PATCH RFC 01/11 v4] gpio: Add a block GPIO API to gpiolib
Quoted 10 lines · Roland Stigge
The recurring task of providing simultaneous access to GPIO lines (especially for bit banging protocols) needs an appropriate API.
This patch adds a kernel internal "Block GPIO" API that enables simultaneous access to several GPIOs. This is done by abstracting GPIOs to an n-bit word:
Once requested, it provides access to a group of GPIOs which can range over multiple GPIO chips.Signed-off-by Roland Stigge <[email protected]>---
<snip>
Documentation/gpio.txt slower clock delays the rising edge of S · line 462
Quoted 9 lines
Documentation/gpio.txt +9
-0+This creates a new block of GPIOs as a list of GPIO numbers with the specified +size which are accessible via the returned struct gpio_block and the accessor +functions described below. Please note that you need to request the GPIOs +separately via gpio_request(). An arbitrary list of globally valid GPIOs can be +specified, even ranging over several gpio_chips. Actual handling of I/O +operations will be done on a best effort base, i.e. simultaneous I/O only where +possible by hardware and implemented in the respective GPIO driver. The number +of GPIOs in one block is limited to 32 on a 32 bit system, and 64 on a 64 bit +system. However, several blocks can be defined at once.
This should probably say that the number of gpios in a block is limited to sizeof(unsigned long) or BITS_PER_LONG. I think sizeof(unsigned long) == 8 on some 32 bit architectures.
~Ryan
RFC PATCH v4 2/11 gpio: Add sysfs support to block GPIO API
In reply to Roland StiggeThis patch adds sysfs support to the block GPIO API.
--- Documentation/ABI/testing/sysfs-gpio | 6 drivers/gpio/gpiolib.c | 211 +++++++++++++++++++++++++++++++++++ include/asm-generic/gpio.h | 11 + include/linux/gpio.h | 13 ++ 4 files changed, 240 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-gpio +5 -1
@@ -24,4 +24,8 @@ Description: /base ... (r/o) same as N /label ... (r/o) descriptive, not necessarily unique /ngpio ... (r/o) number of GPIOs; numbered N to N + (ngpio - 1) - + /blockN ... for each GPIO block #N + /ngpio ... (r/o) number of GPIOs in this group + /exported ... sysfs export state of this group (0, 1) + /value ... current value as 32 or 64 bit integer in decimal + (only available if /exported is 1)
drivers/gpio/gpiolib.c +211 -0
229 lines not on this page · raw message
include/asm-generic/gpio.h +11 -0
@@ -211,6 +211,8 @@ extern int gpio_export_link(struct devic unsigned gpio);extern int gpio_sysfs_set_active_low (unsigned gpio,int value);extern void gpio_unexport (unsigned gpio); +extern int gpio_block_export (struct gpio_block *block); +extern void gpio_block_unexport (struct gpio_block *block);#endif /* CONFIG_GPIO_SYSFS */
@@ -270,6 +272,15 @@ static inline int gpio_sysfs_set_active_static inline void gpio_unexport (unsigned gpio) { } + +static inline int gpio_block_export (struct gpio_block *block) +{ +return -ENOSYS ; +} + +static inline void gpio_block_unexport (struct gpio_block *block) +{ +}#endif /* CONFIG_GPIO_SYSFS */ #endif /* _ASM_GENERIC_GPIO_H */
include/linux/gpio.h +13 -0
@@ -291,6 +291,19 @@ static inline void gpio_unexport(unsigneWARN_ON (1 ); } +static inline int gpio_block_export (struct gpio_block *block) +{ +/* GPIO block can never have been requested or set as {in,out}put */ +WARN_ON (1 ); +return -EINVAL ; +} + +static inline void gpio_block_unexport (struct gpio_block *block) +{ +/* GPIO block can never have been exported */ +WARN_ON (1 ); +} +static inline int gpio_to_irq (unsigned gpio) {/* GPIO can never have been requested or set as input */
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 9 lines · Roland Stigge
drivers/gpio/gpiolib.c +9
-0+int gpio_block_export (struct gpio_block *block) +{ +int status; +struct device *dev; + +/* can't export until sysfs is available ... */ +if (!gpio_class.p) { +pr_debug ("%s: called too early!\n" , __func__); +return -ENOENT ;
drivers/gpio/gpiolib.c static void gpiochip_unexport(struct gpi · line 1166
Quoted 10 lines
drivers/gpio/gpiolib.c +10
-0+ } + +mutex_lock (&sysfs_lock); + dev =device_create (&gpio_class,NULL ,MKDEV (0 ,0 ), block, + block->name); +if (!IS_ERR (dev)) + status =sysfs_create_group (&dev->kobj, &gpio_block_attr_group); +else + status =PTR_ERR (dev); +mutex_unlock (&sysfs_lock);
You just raced with userspace telling it that the device was present, yet the attributes are not there. Don't do that, use the default class attributes for the class and then the driver core will create them automagically without needing to this "by hand" at all.
thanks,
greg k-h
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 17 lines · Greg KH
Quoted 16 lines · Roland Stigge
drivers/gpio/gpiolib.c +16
-0+int gpio_block_export (struct gpio_block *block) +{ +int status; +struct device *dev; + +/* can't export until sysfs is available ... */ +if (!gpio_class.p) { +pr_debug ("%s: called too early!\n" , __func__); +return -ENOENT ; + } + +mutex_lock (&sysfs_lock); + dev =device_create (&gpio_class,NULL ,MKDEV (0 ,0 ), block, + block->name); +if (!IS_ERR (dev)) + status =sysfs_create_group (&dev->kobj, &gpio_block_attr_group);
Quoted 8 lines
drivers/gpio/gpiolib.c static void gpiochip_unexport(struct gpi · line 1166
Quoted 3 lines
drivers/gpio/gpiolib.c +3
-0+else + status =PTR_ERR (dev); +mutex_unlock (&sysfs_lock);You just raced with userspace telling it that the device was present, yet the attributes are not there. Don't do that, use the default class attributes for the class and then the driver core will create them automagically without needing to this "by hand" at all.
I guess you mean class attributes like gpio_class_attrs[] of gpio_class? Aren't class attributes specific to a class only (i.e. only one attribute at the root for all devices)? What I needed above are attributes for the block itself (of which there can be several). So we need device attributes for each block, not class attributes here.
Maybe there's some other kind of locking/atomicity available for this task?
Further, current gpio and gpiochip devices are also doing this way:
creating the device and subsequently their attrs, even though there may be a better way but I'm still wondering how this would be.
Any hint appreciated.
Thanks in advance,
Roland
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 27 lines · Roland Stigge
Quoted 17 lines · Greg KH
Quoted 16 lines · Roland Stigge
drivers/gpio/gpiolib.c +16
-0+int gpio_block_export (struct gpio_block *block) +{ +int status; +struct device *dev; + +/* can't export until sysfs is available ... */ +if (!gpio_class.p) { +pr_debug ("%s: called too early!\n" , __func__); +return -ENOENT ; + } + +mutex_lock (&sysfs_lock); + dev =device_create (&gpio_class,NULL ,MKDEV (0 ,0 ), block, + block->name); +if (!IS_ERR (dev)) + status =sysfs_create_group (&dev->kobj, &gpio_block_attr_group);Quoted 8 lines
drivers/gpio/gpiolib.c static void gpiochip_unexport(struct gpi · line 1166
Quoted 3 lines
drivers/gpio/gpiolib.c +3
-0+else + status =PTR_ERR (dev); +mutex_unlock (&sysfs_lock);You just raced with userspace telling it that the device was present, yet the attributes are not there. Don't do that, use the default class attributes for the class and then the driver core will create them automagically without needing to this "by hand" at all.
Quoted 1 line
I guess you mean class attributes like gpio_class_attrs[] of
gpio_class?
Yes.
Quoted 4 lines
Aren't class attributes specific to a class only (i.e. only one attribute at the root for all devices)? What I needed above are attributes for the block itself (of which there can be several). So we need device attributes for each block, not class attributes here.
Yes, that is what the dev_attrs field in 'struct class' is for.
Quoted 5 lines
Maybe there's some other kind of locking/atomicity available for this task?
Further, current gpio and gpiochip devices are also doing this way:
creating the device and subsequently their attrs, even though there may be a better way but I'm still wondering how this would be.
Then the existing code is broken and should be fixed to use dev_attrs. I guess it's time to audit the tree and find all places that get this wrong...
greg k-h
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 9 lines · Greg KH
Quoted 4 lines · Roland Stigge
Further, current gpio and gpiochip devices are also doing this way:
creating the device and subsequently their attrs, even though there may be a better way but I'm still wondering how this would be.Then the existing code is broken and should be fixed to use
dev_attrs. I guess it's time to audit the tree and find all places that get this wrong...
The thing is, as I've tried to explain but maybe didn't get across, that these devices don't *have* a parent, and are not part of any tree.
They are parentless mock devices, created on-the-fly just to get sysfs entries.
What is needed it to get the device model right in the first place.
Fixing it has been drafted by me and Grant:
https://blueprints.launchpad.net/linux-linaro/+spec/gpiochip-to-dev
This is not all-encompassing though :-/
Yours,
Linus Walleij
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 7 lines · Linus Walleij
Quoted 6 lines · Greg KH
Quoted 4 lines · Roland Stigge
Further, current gpio and gpiochip devices are also doing this way:
creating the device and subsequently their attrs, even though there may be a better way but I'm still wondering how this would be.
Quoted 7 lines
Quoted 3 lines
Then the existing code is broken and should be fixed to use
dev_attrs. I guess it's time to audit the tree and find all places that get this wrong...The thing is, as I've tried to explain but maybe didn't get across, that these devices don't *have* a parent, and are not part of any tree.
You are passing in a parent device to the device_create() call, where did that pointer come from?
Either way, the attribute creation needs to happen before we announce the device to userspace, that's a bug that should be fixed now.
Quoted 2 lines
They are parentless mock devices, created on-the-fly just to get sysfs entries.
That's fine, well, not the "parentless" part, but that should be trivial to fix, just pass in the correct pointer and you should be fine.
Quoted 2 lines
What is needed it to get the device model right in the first place.
I thought it was in the device model already?
Quoted 4 lines
Fixing it has been drafted by me and Grant:
https://blueprints.launchpad.net/linux-linaro/+spec/gpiochip-to-devThis is not all-encompassing though :-/
That's a good list to work from, good luck :)
greg k-h
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 7 lines · Greg KH
Quoted 3 lines · Linus Walleij
The thing is, as I've tried to explain but maybe didn't get across, that these devices don't *have* a parent, and are not part of any tree.
You are passing in a parent device to the
device_create()call, where did that pointer come from?
You mean this:
dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0),
desc, ioname ? ioname : "gpio%u", gpio);
desc->chip->dev is an *optional* pointer to a parent device of the GPIO chip (not the GPIO chip itself). It is usually NULL.
Quoted 5 lines
Quoted 2 lines
They are parentless mock devices, created on-the-fly just to get sysfs entries.
That's fine, well, not the "parentless" part, but that should be trivial to fix, just pass in the correct pointer and you should be fine.
Hm, yeah well, they are orphans mostly.
Quoted 4 lines
Quoted 2 lines
What is needed it to get the device model right in the first place.
I thought it was in the device model already?
GPIO chips are not devices. :-(
Yours,
Linus Walleij
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
Quoted 6 lines · Linus Walleij
Quoted 5 lines · Greg KH
Quoted 3 lines · Linus Walleij
The thing is, as I've tried to explain but maybe didn't get across, that these devices don't *have* a parent, and are not part of any tree.
Quoted 7 lines
Quoted 2 lines
You are passing in a parent device to the
device_create()call, where did that pointer come from?You mean this:
dev = device_create(&gpio_class,
desc->chip->dev,MKDEV(0, 0),desc, ioname ? ioname : "gpio%u", gpio);
Yes.
Quoted 2 lines
desc->chip->devis an *optional* pointer to a parent device of the GPIO chip (not the GPIO chip itself). It is usually NULL.
Ah, I didn't realize that.
Well, then they turn into "virtual" devices, which are still fine, they show up in the device tree properly, so all is ok.
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
They are parentless mock devices, created on-the-fly just to get sysfs entries.
That's fine, well, not the "parentless" part, but that should be trivial to fix, just pass in the correct pointer and you should be fine.
Quoted 8 lines
Hm, yeah well, they are orphans mostly.
Quoted 4 lines
Quoted 2 lines
What is needed it to get the device model right in the first place.
I thought it was in the device model already?
GPIO chips are not devices. :-(
Point to the device that the GPIO pins are located on. Be it the cpu, or platform, or something else. If not, making them "virtual" is fine, that's what it is there for.
greg k-h
Re: [PATCH RFC 02/11 v4] gpio: Add sysfs support to block GPIO API
In reply to Greg KHQuoted 30 lines · Greg KH
Quoted 27 lines · Roland Stigge
Quoted 17 lines · Greg KH
Quoted 16 lines · Roland Stigge
drivers/gpio/gpiolib.c +16
-0+int gpio_block_export (struct gpio_block *block) +{ +int status; +struct device *dev; + +/* can't export until sysfs is available ... */ +if (!gpio_class.p) { +pr_debug ("%s: called too early!\n" , __func__); +return -ENOENT ; + } + +mutex_lock (&sysfs_lock); + dev =device_create (&gpio_class,NULL ,MKDEV (0 ,0 ), block, + block->name); +if (!IS_ERR (dev)) + status =sysfs_create_group (&dev->kobj, &gpio_block_attr_group);Quoted 8 lines
drivers/gpio/gpiolib.c static void gpiochip_unexport(struct gpi · line 1166
Quoted 3 lines
drivers/gpio/gpiolib.c +3
-0+else + status =PTR_ERR (dev); +mutex_unlock (&sysfs_lock);You just raced with userspace telling it that the device was present, yet the attributes are not there. Don't do that, use the default class attributes for the class and then the driver core will create them automagically without needing to this "by hand" at all.
Quoted 1 line
I guess you mean class attributes like gpio_class_attrs[] of
gpio_class?
Quoted 8 lines
Yes.
Quoted 4 lines
Aren't class attributes specific to a class only (i.e. only one attribute at the root for all devices)? What I needed above are attributes for the block itself (of which there can be several). So we need device attributes for each block, not class attributes here.
Yes, that is what the
dev_attrsfield in 'struct class' is for.
That's what I was missing. Will update.
Thanks,
Roland
RFC PATCH v4 3/11 gpio: Add device tree support to block GPIO API
In reply to Roland StiggeThis patch adds device tree support to the block GPIO API.
--- Documentation/devicetree/bindings/gpio/gpio-block.txt | 36 +++++++ drivers/gpio/Makefile | 1 drivers/gpio/gpioblock-of.c | 84 ++++++++++++++++++ 3 files changed, 121 insertions(+)
Documentation/devicetree/bindings/gpio/gpio-block.txt new file +36 -0
@@ -0,0 +1,36 @@ +Block GPIO definition +===================== + +This binding specifies arbitrary blocks of gpios, combining gpios from one or +more GPIO controllers together, to form a word for r/w access. + +Required property: +- compatible: must be "linux,gpio-block" + +Required subnodes: +- the name will be the registered name of the block +- property "gpios" is a list of gpios for the respective block + +Example: + + blockgpio { + compatible = "linux,gpio-block"; + + block0 { + gpios = <&gpio 3 0 0>, + <&gpio 3 1 0>; + }; + block1 { + gpios = <&gpio 4 1 0>, + <&gpio 4 3 0>, + <&gpio 4 2 0>, + <&gpio 4 4 0>, + <&gpio 4 5 0>, + <&gpio 4 6 0>, + <&gpio 4 7 0>, + <&gpio 4 8 0>, + <&gpio 4 9 0>, + <&gpio 4 10 0>, + <&gpio 4 19 0>; + }; + };
drivers/gpio/Makefile +1 -0
@@ -4,6 +4,7 @@ ccflags-$(CONFIG_DEBUG_GPIO) += -DDEBUG obj-$(CONFIG_GPIOLIB) += gpiolib.o devres.o obj-$(CONFIG_OF_GPIO) += gpiolib-of.o +obj-$(CONFIG_OF_GPIO) += gpioblock-of.o # Device drivers. Generally keep list sorted alphabetically obj-$(CONFIG_GPIO_GENERIC) += gpio-generic.o
drivers/gpio/gpioblock-of.c new file +84 -0
84 lines not on this page · raw message
RFC PATCH v4 4/11 gpio-max730x: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the MAX730x driver.
Due to hardware constraints in this chip, simultaneous access to GPIO lines can only be done in groups of 8: GPIOs 0-7, 8-15, 16-23, 24-27. However, setting and clearing will be done at once.
--- drivers/gpio/gpio-max730x.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
drivers/gpio/gpio-max730x.c +61 -0
@@ -146,6 +146,44 @@ static int max7301_get(struct gpio_chipreturn level; } +static unsigned long max7301_get_block (struct gpio_chip *chip, +unsigned long mask) +{ + struct max7301 *ts = container_of(chip, struct max7301, chip); +int i, j; +unsigned long result =0 ; + +for (i =0 ; i <4 ; i++) { +if ((mask >> (i *8 )) &0xFF ) {/* i/o only if necessary */ +u8 in_level = ts->read (ts->dev,0x44 + i *8 ); +u8 in_mask =0 ; +u8 out_level = (ts->out_level >> (i *8 +4 )) &0xFF ; +u8 out_mask =0 ; + +for (j =0 ; j <8 ; j++) { +int offset =4 + i *8 + j; +int config = (ts->port_config[offset >>2 ] >> + ((offset &3 ) <<1 )) & +PIN_CONFIG_MASK ; + +switch (config) { +case PIN_CONFIG_OUT : + out_mask |=BIT (j); +break ; +case PIN_CONFIG_IN_WO_PULLUP : +case PIN_CONFIG_IN_PULLUP : + in_mask |=BIT (j); + } + } + + result |= ((unsigned long )(in_level & in_mask) | + (out_level & out_mask)) << (i *8 ); + } + } + +return result & mask; +} +static void max7301_set (struct gpio_chip *chip,unsigned offset,int value) { struct max7301 *ts = container_of(chip, struct max7301, chip);
@@ -160,6 +198,27 @@ static void max7301_set(struct gpio_chipmutex_unlock (&ts->lock); } +static void max7301_set_block (struct gpio_chip *chip,unsigned long mask, +unsigned long values) +{ + struct max7301 *ts = container_of(chip, struct max7301, chip); +unsigned long changes; +int i; + +mutex_lock (&ts->lock); + + changes = (ts->out_level ^ (values <<4 )) & (mask <<4 ); + ts->out_level ^= changes; + +for (i =0 ; i <4 ; i++) { +if ((changes >> (i *8 +4 )) &0xFF )/* i/o only on change */ + ts->write (ts->dev,0x44 + i *8 , + (ts->out_level >> (i *8 +4 )) &0xFF ); + } + +mutex_unlock (&ts->lock); +} +int __devinit __max730x_probe (struct max7301 *ts) {struct device *dev = ts->dev;
@@ -183,8 +242,10 @@ int __devinit __max730x_probe(struct max ts->chip.direction_input = max7301_direction_input; ts->chip.get = max7301_get; + ts->chip.get_block = max7301_get_block; ts->chip.direction_output = max7301_direction_output; ts->chip.set = max7301_set; + ts->chip.set_block = max7301_set_block; ts->chip.base = pdata->base; ts->chip.ngpio =PIN_NUMBER ;
RFC PATCH v4 5/11 gpio-lpc32xx: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the LPC32xx driver.
--- drivers/gpio/gpio-lpc32xx.c | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
drivers/gpio/gpio-lpc32xx.c +82 -0
@@ -297,6 +297,22 @@ static int lpc32xx_gpio_get_value_p3(strreturn __get_gpio_state_p3 (group, pin); } +static unsigned long lpc32xx_gpio_get_block_p3 (struct gpio_chip *chip, +unsigned long mask) +{ +struct lpc32xx_gpio_chip *group =to_lpc32xx_gpio (chip); +u32 bits =__raw_readl (group->gpio_grp->inp_state); + +/* In P3_INP_STATE, the 6 GPIOs are scattered over the register, +* rearranging to bits 0-5 +*/ + bits >>=10 ; + bits &=0x401F ; + bits |= (bits &0x4000 ) >>9 ; + +return bits & mask &0x3F ; +} +static int lpc32xx_gpi_get_value (struct gpio_chip *chip,unsigned pin) {struct lpc32xx_gpio_chip *group =to_lpc32xx_gpio (chip);
@@ -304,6 +320,15 @@ static int lpc32xx_gpi_get_value(structreturn __get_gpi_state_p3 (group, pin); } +static unsigned long lpc32xx_gpi_get_block (struct gpio_chip *chip, +unsigned long mask) +{ +struct lpc32xx_gpio_chip *group =to_lpc32xx_gpio (chip); +u32 bits =__raw_readl (group->gpio_grp->inp_state); + +return bits & mask; +} +static int lpc32xx_gpio_dir_output_p012 (struct gpio_chip *chip,unsigned pin,int value) {
@@ -351,6 +376,27 @@ static void lpc32xx_gpio_set_value_p3(st__set_gpio_level_p3 (group, pin, value); } +static void lpc32xx_gpio_set_block_p3 (struct gpio_chip *chip, +unsigned long mask, +unsigned long values) +{ +struct lpc32xx_gpio_chip *group =to_lpc32xx_gpio (chip); +u32 set_bits = values & mask; +u32 clr_bits = ~values & mask; + +/* States of GPIO 0-5 start at bit 25 */ + set_bits <<=25 ; + clr_bits <<=25 ; + +/* Note: On LPC32xx, GPOs can only be set at once or cleared at once, +* but not set and cleared@once +*/ +if (set_bits) +__raw_writel (set_bits, group->gpio_grp->outp_set); +if (clr_bits) +__raw_writel (clr_bits, group->gpio_grp->outp_clr); +} +static void lpc32xx_gpo_set_value (struct gpio_chip *chip,unsigned pin,int value) {
@@ -366,6 +412,31 @@ static int lpc32xx_gpo_get_value(structreturn __get_gpo_state_p3 (group, pin); } +static void lpc32xx_gpo_set_block (struct gpio_chip *chip,unsigned long mask, +unsigned long values) +{ +struct lpc32xx_gpio_chip *group =to_lpc32xx_gpio (chip); +u32 set_bits = values & mask; +u32 clr_bits = ~values & mask; + +/* Note: On LPC32xx, GPOs can only be set at once or cleared at once, +* but not set and cleared at once +*/ +if (set_bits) +__raw_writel (set_bits, group->gpio_grp->outp_set); +if (clr_bits) +__raw_writel (clr_bits, group->gpio_grp->outp_clr); +} + +static unsigned long lpc32xx_gpo_get_block (struct gpio_chip *chip, +unsigned long mask) +{ +struct lpc32xx_gpio_chip *group =to_lpc32xx_gpio (chip); +u32 bits =__raw_readl (group->gpio_grp->outp_state); + +return bits & mask; +} +static int lpc32xx_gpio_request (struct gpio_chip *chip,unsigned pin) {if (pin < chip->ngpio)
@@ -440,8 +511,10 @@ static struct lpc32xx_gpio_chip lpc32xx_ .label = "gpio_p0", .direction_input = lpc32xx_gpio_dir_input_p012, .get = lpc32xx_gpio_get_value_p012, + .get_block = lpc32xx_gpi_get_block, .direction_output = lpc32xx_gpio_dir_output_p012, .set = lpc32xx_gpio_set_value_p012, + .set_block = lpc32xx_gpo_set_block, .request = lpc32xx_gpio_request, .to_irq = lpc32xx_gpio_to_irq_p01, .base = LPC32XX_GPIO_P0_GRP,
@@ -456,8 +529,10 @@ static struct lpc32xx_gpio_chip lpc32xx_ .label = "gpio_p1", .direction_input = lpc32xx_gpio_dir_input_p012, .get = lpc32xx_gpio_get_value_p012, + .get_block = lpc32xx_gpi_get_block, .direction_output = lpc32xx_gpio_dir_output_p012, .set = lpc32xx_gpio_set_value_p012, + .set_block = lpc32xx_gpo_set_block, .request = lpc32xx_gpio_request, .to_irq = lpc32xx_gpio_to_irq_p01, .base = LPC32XX_GPIO_P1_GRP,
@@ -472,8 +547,10 @@ static struct lpc32xx_gpio_chip lpc32xx_ .label = "gpio_p2", .direction_input = lpc32xx_gpio_dir_input_p012, .get = lpc32xx_gpio_get_value_p012, + .get_block = lpc32xx_gpi_get_block, .direction_output = lpc32xx_gpio_dir_output_p012, .set = lpc32xx_gpio_set_value_p012, + .set_block = lpc32xx_gpo_set_block, .request = lpc32xx_gpio_request, .base = LPC32XX_GPIO_P2_GRP, .ngpio = LPC32XX_GPIO_P2_MAX,
@@ -487,8 +564,10 @@ static struct lpc32xx_gpio_chip lpc32xx_ .label = "gpio_p3", .direction_input = lpc32xx_gpio_dir_input_p3, .get = lpc32xx_gpio_get_value_p3, + .get_block = lpc32xx_gpio_get_block_p3, .direction_output = lpc32xx_gpio_dir_output_p3, .set = lpc32xx_gpio_set_value_p3, + .set_block = lpc32xx_gpio_set_block_p3, .request = lpc32xx_gpio_request, .to_irq = lpc32xx_gpio_to_irq_gpio_p3, .base = LPC32XX_GPIO_P3_GRP,
@@ -503,6 +582,7 @@ static struct lpc32xx_gpio_chip lpc32xx_ .label = "gpi_p3", .direction_input = lpc32xx_gpio_dir_in_always, .get = lpc32xx_gpi_get_value, + .get_block = lpc32xx_gpi_get_block, .request = lpc32xx_gpio_request, .to_irq = lpc32xx_gpio_to_irq_gpi_p3, .base = LPC32XX_GPI_P3_GRP,
@@ -517,7 +597,9 @@ static struct lpc32xx_gpio_chip lpc32xx_ .label = "gpo_p3", .direction_output = lpc32xx_gpio_dir_out_always, .set = lpc32xx_gpo_set_value, + .set_block = lpc32xx_gpo_set_block, .get = lpc32xx_gpo_get_value, + .get_block = lpc32xx_gpo_get_block, .request = lpc32xx_gpio_request, .base = LPC32XX_GPO_P3_GRP, .ngpio = LPC32XX_GPO_P3_MAX,
RFC PATCH v4 6/11 gpio-generic: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the gpio-generic driver.
--- drivers/gpio/gpio-generic.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
drivers/gpio/gpio-generic.c +56 -0
@@ -122,6 +122,13 @@ static int bgpio_get(struct gpio_chip *greturn bgc->read_reg (bgc->reg_dat) & bgc->pin2mask (bgc, gpio); } +static unsigned long bgpio_get_block (struct gpio_chip *gc,unsigned long mask) +{ +struct bgpio_chip *bgc =to_bgpio_chip (gc); + +return bgc->read_reg (bgc->reg_dat) & mask; +} +static void bgpio_set (struct gpio_chip *gc,unsigned int gpio,int val) {struct bgpio_chip *bgc =to_bgpio_chip (gc);
@@ -170,6 +177,51 @@ static void bgpio_set_set(struct gpio_chspin_unlock_irqrestore (&bgc->lock, flags); } +static void bgpio_set_block (struct gpio_chip *gc,unsigned long mask, +unsigned long values) +{ +struct bgpio_chip *bgc =to_bgpio_chip (gc); +unsigned long flags; + +spin_lock_irqsave (&bgc->lock, flags); + + bgc->data &= ~mask; + bgc->data |= values & mask; + + bgc->write_reg (bgc->reg_dat, bgc->data); + +spin_unlock_irqrestore (&bgc->lock, flags); +} + +static void bgpio_set_with_clear_block (struct gpio_chip *gc,unsigned long mask, +unsigned long values) +{ +struct bgpio_chip *bgc =to_bgpio_chip (gc); +unsigned long set_bits = values & mask; +unsigned long clr_bits = ~values & mask; + +if (set_bits) + bgc->write_reg (bgc->reg_set, set_bits); +if (clr_bits) + bgc->write_reg (bgc->reg_set, clr_bits); +} + +static void bgpio_set_set_block (struct gpio_chip *gc,unsigned long mask, +unsigned long values) +{ +struct bgpio_chip *bgc =to_bgpio_chip (gc); +unsigned long flags; + +spin_lock_irqsave (&bgc->lock, flags); + + bgc->data &= ~mask; + bgc->data |= values & mask; + + bgc->write_reg (bgc->reg_set, bgc->data); + +spin_unlock_irqrestore (&bgc->lock, flags); +} +static int bgpio_simple_dir_in (struct gpio_chip *gc,unsigned int gpio) {return 0 ;
@@ -317,14 +369,18 @@ static int bgpio_setup_io(struct bgpio_c bgc->reg_set = set; bgc->reg_clr = clr; bgc->gc.set = bgpio_set_with_clear; + bgc->gc.set_block = bgpio_set_with_clear_block; }else if (set && !clr) { bgc->reg_set = set; bgc->gc.set = bgpio_set_set; + bgc->gc.set_block = bgpio_set_set_block; }else { bgc->gc.set = bgpio_set; + bgc->gc.set_block = bgpio_set_block; } bgc->gc.get = bgpio_get; + bgc->gc.get_block = bgpio_get_block;return 0 ; }
RFC PATCH v4 7/11 gpio-pca953x: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the gpio-pca953x driver.
--- drivers/gpio/gpio-pca953x.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
drivers/gpio/gpio-pca953x.c +64 -0
@@ -300,6 +300,68 @@ exit:mutex_unlock (&chip->i2c_lock); } +static unsigned long pca953x_gpio_get_block (struct gpio_chip *gc, +unsigned long mask) +{ +struct pca953x_chip *chip; +u32 reg_val; +int ret, offset =0 ; + + chip = container_of(gc, struct pca953x_chip, gpio_chip); + +mutex_lock (&chip->i2c_lock); +switch (chip->chip_type) { +case PCA953X_TYPE : + offset =PCA953X_INPUT ; +break ; +case PCA957X_TYPE : + offset =PCA957X_IN ; +break ; + } + ret =pca953x_read_reg (chip, offset, ®_val); +mutex_unlock (&chip->i2c_lock); +if (ret <0 ) { +/* NOTE: diagnostic already emitted; that's all we should +* do unless gpio_*_value_cansleep() calls become different +* from their nonsleeping siblings (and report faults). +*/ +return 0 ; + } + +return reg_val & mask; +} + +static void pca953x_gpio_set_block (struct gpio_chip *gc,unsigned long mask, +unsigned long values) +{ +struct pca953x_chip *chip; +u32 reg_val; +int ret, offset =0 ; + + chip = container_of(gc, struct pca953x_chip, gpio_chip); + +mutex_lock (&chip->i2c_lock); + + reg_val = chip->reg_output & ~mask; + reg_val |= values & mask; + +switch (chip->chip_type) { +case PCA953X_TYPE : + offset =PCA953X_OUTPUT ; +break ; +case PCA957X_TYPE : + offset =PCA957X_OUT ; +break ; + } + ret =pca953x_write_reg (chip, offset, reg_val); +if (ret) + goto exit; + + chip->reg_output = reg_val; +exit: +mutex_unlock (&chip->i2c_lock); +} +static void pca953x_setup_gpio (struct pca953x_chip *chip,int gpios) {struct gpio_chip *gc;
@@ -310,6 +372,8 @@ static void pca953x_setup_gpio(struct pc gc->direction_output = pca953x_gpio_direction_output; gc->get = pca953x_gpio_get_value; gc->set = pca953x_gpio_set_value; + gc->get_block = pca953x_gpio_get_block; + gc->set_block = pca953x_gpio_set_block; gc->can_sleep =1 ; gc->base = chip->gpio_start;
RFC PATCH v4 8/11 gpio-em: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the gpio-em driver.
--- drivers/gpio/gpio-em.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
drivers/gpio/gpio-em.c +23 -0
@@ -203,6 +203,27 @@ static void em_gio_set(struct gpio_chip__em_gio_set (chip,GIO_OH , offset -16 , value); } +static unsigned long em_gio_get_block (struct gpio_chip *chip, +unsigned long mask) +{ +return (int )(em_gio_read (gpio_to_priv (chip),GIO_I ) & mask); +} + +static void em_gio_set_block (struct gpio_chip *chip,unsigned long mask, +unsigned long values) +{ +unsigned long mask_ol = mask &0xFFFF ; +unsigned long mask_oh = mask >>16 ; + +unsigned long values_ol = values & mask_ol; +unsigned long values_oh = (values >>16 ) & mask_oh; + +em_gio_write (gpio_to_priv (chip),GIO_OL , + mask_ol <<16 | values_ol); +em_gio_write (gpio_to_priv (chip),GIO_OH , + mask_oh <<16 | values_oh); +} +static int em_gio_direction_output (struct gpio_chip *chip,unsigned offset,int value) {
@@ -317,8 +338,10 @@ static int __devinit em_gio_probe(struct gpio_chip = &p->gpio_chip; gpio_chip->direction_input = em_gio_direction_input; gpio_chip->get = em_gio_get; + gpio_chip->get_block = em_gio_get_block; gpio_chip->direction_output = em_gio_direction_output; gpio_chip->set = em_gio_set; + gpio_chip->set_block = em_gio_set_block; gpio_chip->to_irq = em_gio_to_irq; gpio_chip->label = name; gpio_chip->owner =THIS_MODULE ;
RFC PATCH v4 9/11 gpio-pl061: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the gpio-pl061 driver.
--- drivers/gpio/gpio-pl061.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/gpio/gpio-pl061.c +17 -0
@@ -123,6 +123,21 @@ static void pl061_set_value(struct gpio_writeb (!!value << offset, chip->base + (1 << (offset +2 ))); } +static unsigned long pl061_get_block (struct gpio_chip *gc,unsigned long mask) +{ + struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); + +return !!readb (chip->base + (mask <<2 )); +} + +static void pl061_set_block (struct gpio_chip *gc,unsigned long mask, +unsigned long values) +{ + struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc); + +writeb (values & mask, chip->base + (mask <<2 )); +} +static int pl061_to_irq (struct gpio_chip *gc,unsigned offset) { struct pl061_gpio *chip = container_of(gc, struct pl061_gpio, gc);
@@ -256,6 +271,8 @@ static int pl061_probe(struct amba_devic chip->gc.direction_output = pl061_direction_output; chip->gc.get = pl061_get_value; chip->gc.set = pl061_set_value; + chip->gc.get_block = pl061_get_block; + chip->gc.set_block = pl061_set_block; chip->gc.to_irq = pl061_to_irq; chip->gc.ngpio =PL061_GPIO_NR ; chip->gc.label =dev_name (&dev->dev);
RFC PATCH v4 10/11 gpio-max732x: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the gpio-max732x driver.
--- drivers/gpio/gpio-max732x.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
drivers/gpio/gpio-max732x.c +59 -0
@@ -219,6 +219,63 @@ out:mutex_unlock (&chip->lock); } +static unsigned long max732x_gpio_get_block (struct gpio_chip *gc, +unsigned long mask) +{ +struct max732x_chip *chip; +uint8_t lo =0 ; +uint8_t hi =0 ; +int ret; + + chip = container_of(gc, struct max732x_chip, gpio_chip); + +if (mask &0xFF ) { + ret =max732x_readb (chip,is_group_a (chip,0 ), &lo); +if (ret <0 ) +return 0 ; + } +if (mask &0xFF00 ) { + ret =max732x_readb (chip,is_group_a (chip,8 ), &hi); +if (ret <0 ) +return 0 ; + } + +return (((unsigned long )hi <<8 ) | lo) & mask; +} + +static void max732x_gpio_set_block (struct gpio_chip *gc,unsigned long mask, +unsigned long values) +{ +struct max732x_chip *chip; +uint8_t reg_out; +uint8_t lo_mask = mask &0xFF ; +uint8_t hi_mask = (mask >>8 ) &0xFF ; +int ret; + + chip = container_of(gc, struct max732x_chip, gpio_chip); + +mutex_lock (&chip->lock); + +if (lo_mask) { + reg_out = (chip->reg_out[0 ] & ~lo_mask) | (values & lo_mask); + ret =max732x_writeb (chip,is_group_a (chip,0 ), reg_out); +if (ret <0 ) + goto out; + chip->reg_out[0 ] = reg_out; + } +if (hi_mask) { + reg_out = (chip->reg_out[1 ] & ~hi_mask) | + ((values >>8 ) & hi_mask); + ret =max732x_writeb (chip,is_group_a (chip,8 ), reg_out); +if (ret <0 ) + goto out; + chip->reg_out[1 ] = reg_out; + } + +out: +mutex_unlock (&chip->lock); +} +static int max732x_gpio_direction_input (struct gpio_chip *gc,unsigned off) {struct max732x_chip *chip;
@@ -562,8 +619,10 @@ static int __devinit max732x_setup_gpio(if (chip->dir_output) { gc->direction_output = max732x_gpio_direction_output; gc->set = max732x_gpio_set_value; + gc->set_block = max732x_gpio_set_block; } gc->get = max732x_gpio_get_value; + gc->get_block = max732x_gpio_get_block; gc->can_sleep =1 ; gc->base = gpio_start;
RFC PATCH v4 11/11 gpio-pcf857x: Add block GPIO API
In reply to Roland StiggeThis patch adds block GPIO API support to the gpio-pcf857x driver.
--- drivers/gpio/gpio-pcf857x.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/gpio/gpio-pcf857x.c +24 -0
@@ -158,6 +158,28 @@ static void pcf857x_set(struct gpio_chippcf857x_output (chip, offset, value); } +static unsigned long pcf857x_get_block (struct gpio_chip *chip, +unsigned long mask) +{ + struct pcf857x *gpio = container_of(chip, struct pcf857x, chip); +int value; + + value = gpio->read (gpio->client); +return (value <0 ) ?0 : (value & mask); +} + +static void pcf857x_set_block (struct gpio_chip *chip,unsigned long mask, +unsigned long values) +{ + struct pcf857x *gpio = container_of(chip, struct pcf857x, chip); + +mutex_lock (&gpio->lock); + gpio->out &= ~mask; + gpio->out |= values & mask; + gpio->write (gpio->client, gpio->out); +mutex_unlock (&gpio->lock); +} +/*-------------------------------------------------------------------------*/ static int pcf857x_to_irq (struct gpio_chip *chip,unsigned offset)
@@ -280,6 +302,8 @@ static int pcf857x_probe(struct i2c_clie gpio->chip.owner =THIS_MODULE ; gpio->chip.get = pcf857x_get; gpio->chip.set = pcf857x_set; + gpio->chip.get_block = pcf857x_get_block; + gpio->chip.set_block = pcf857x_set_block; gpio->chip.direction_input = pcf857x_input; gpio->chip.direction_output = pcf857x_output; gpio->chip.ngpio = id->driver_data;