gpiolib: allow simultaneous setting of multiple GPIO outputs
RFC PATCH 1/2 gpiolib: allow simultaneous setting of multiple GPIO outputs
This patch introduces a new function gpiod_set_raw_array to the consumer interface which allows setting multiple outputs with just one function call. It also adds an optional set_multiple function to the driver interface.
Multiple GPIOs are represented by an array of descriptors. The values to be set are stored in an integer array. Example:
struct gpio_desc *desc_array[10]; int value_array[10]; ... acquire descriptors ... ... set values in value_array ... gpiod_set_raw_array(10, desc_array, value_array);
Benefits:
- Uses descriptor interface. GPIOs have to be acquired before use.
- Allows arbitrary groups of GPIOs spanning multiple chips.
- Works without adjustments in GPIO chip drivers.
- Implementing the new set_multiple function in a chip driver results in additional benefits:
* Improved performance for certain use cases. The original motivation for this was the task of configuring an FPGA. In that specific case, where 9 GPIO lines have to be set many times, configuration time goes down from 48 s to 19 s when using the new function.
* Simultaneous glitch-free setting of multiple pins on any kind of parallel bus attached to GPIOs.
Limitations:
- Performance is only improved for normal high-low outputs. Open drain and open source outputs are always set separately from each other. Those kinds of outputs could probably be accelerated in a similar way if we could forgo the error checking when setting GPIO directions.
- There is no gpiod_set_array function that regards the ACTIVE_LOW bits. The _raw_ function should be sufficient for many use cases. So I avoided the code duplication the other functions would require.
--- drivers/gpio/gpiolib.c | 116 ++++++++++++++++++++++++++++++++++++++++++ include/linux/gpio/consumer.h | 18 +++++++ include/linux/gpio/driver.h | 3 ++ 3 files changed, 137 insertions(+)
drivers/gpio/gpiolib.c +116 -0
@@ -2345,6 +2345,77 @@ static void _gpiod_set_raw_value(struct gpio_desc *desc, bool value) chip->set (chip,gpio_chip_hwgpio (desc), value); } +static void _gpio_chip_set_multiple (struct gpio_chip *chip, +u32 mask[ARCH_NR_GPIOS /32 ], +u32 bits[ARCH_NR_GPIOS /32 ]) +{ +if (chip->set_multiple) { + chip->set_multiple (chip, mask, bits); + }else { +int i; +for (i =0 ; i <ARCH_NR_GPIOS ; i++) { +if (i > chip->ngpio -1 ) +break ; +if (mask[i/32 ] ==0 ) { +/* skip ahead */ + i = (i/32 +1 ) *32 -1 ; +continue ; + } +if (mask[i/32 ] & (1 << (i %32 ))) { + chip->set (chip, i, (bits[i/32 ] >> (i %32 )) &1 ); + mask[i/32 ] &= ~(1 << (i %32 )); + } + } + } +} + +static void _gpiod_set_raw_array (unsigned int array_size, +struct gpio_desc **desc_array, +int *value_array) +{ +struct gpio_chip *chip = desc_array[0 ]->chip; +u32 mask[ARCH_NR_GPIOS /32 ]; +u32 bits[ARCH_NR_GPIOS /32 ]; +int count =0 ; +int i; + +memset (mask,0 ,sizeof (mask)); +for (i =0 ; i < array_size; i++) { +struct gpio_desc *desc = desc_array[i]; +int hwgpio =gpio_chip_hwgpio (desc); +int value = value_array[i]; + +/* another chip; push collected bits to outputs */ +if (desc->chip != chip) { +if (count !=0 ) { +_gpio_chip_set_multiple (chip, mask, bits); +memset (mask,0 ,sizeof (mask)); + count =0 ; + } + chip = desc->chip; + } +/* collect all normal outputs belonging to the same chip */ +/* open drain and open source outputs are set individually */ +trace_gpio_value (desc_to_gpio (desc),0 , value); +if (test_bit (FLAG_OPEN_DRAIN , &desc->flags)) { +_gpio_set_open_drain_value (desc, value); + }else if (test_bit (FLAG_OPEN_SOURCE , &desc->flags)) { +_gpio_set_open_source_value (desc, value); + }else { + mask[hwgpio/32 ] |= (1 << (hwgpio %32 )); +if (value) { + bits[hwgpio/32 ] |= (1 << (hwgpio %32 )); + }else { + bits[hwgpio/32 ] &= ~(1 << (hwgpio %32 )); + } + count++; + } + } +if (count !=0 ) { +_gpio_chip_set_multiple (chip, mask, bits); + } +} +/** * gpiod_set_raw_value() - assign a gpio's raw value * @desc: gpio whose value will be assigned
@@ -2390,6 +2461,30 @@ void gpiod_set_value(struct gpio_desc *desc, int value)EXPORT_SYMBOL_GPL (gpiod_set_value);/** +* gpiod_set_raw_array() - assign values to an array of GPIOs +* @desc_array: array of GPIO descriptors whose values will be assigned +* @value_array: array of values to assign +* +* Set the raw values of the GPIOs, i.e. the values of the physical lines +* without regard for their ACTIVE_LOW status. +* +* This function should be called from contexts where we cannot sleep, and will +* complain if the GPIO chip functions potentially sleep. +*/ +void gpiod_set_raw_array (unsigned int array_size, +struct gpio_desc **desc_array,int *value_array) +{ +if (!desc_array) +return ; +if (!desc_array[0 ]) +return ; +/* Should be using gpiod_set_raw_array_cansleep() */ +WARN_ON (desc_array[0 ]->chip->can_sleep); +_gpiod_set_raw_array (array_size, desc_array, value_array); +} +EXPORT_SYMBOL_GPL (gpiod_set_raw_array); + +/** * gpiod_cansleep() - report whether gpio value access may sleep * @desc: gpio to check *
@@ -2559,6 +2654,27 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)EXPORT_SYMBOL_GPL (gpiod_set_value_cansleep);/** +* gpiod_set_raw_array_cansleep() - assign values to an array of GPIOs +* @desc_array: array of GPIO descriptors whose values will be assigned +* @value_array: array of values to assign +* +* Set the raw values of the GPIOs, i.e. the values of the physical lines +* without regard for their ACTIVE_LOW status. +* +* This function is to be called from contexts that can sleep. +*/ +void gpiod_set_raw_array_cansleep (unsigned int array_size, +struct gpio_desc **desc_array, +int *value_array) +{ +might_sleep_if (extra_checks); +if (!desc_array) +return ; +_gpiod_set_raw_array (array_size, desc_array, value_array); +} +EXPORT_SYMBOL_GPL (gpiod_set_raw_array_cansleep); + +/** * gpiod_add_lookup_table() - register GPIO device consumers * @table: table of consumers to register */
include/linux/gpio/consumer.h +18 -0
@@ -42,12 +42,17 @@ int gpiod_get_value(const struct gpio_desc *desc);void gpiod_set_value (struct gpio_desc *desc,int value);int gpiod_get_raw_value (const struct gpio_desc *desc);void gpiod_set_raw_value (struct gpio_desc *desc,int value); +void gpiod_set_raw_array (unsigned int array_size, +struct gpio_desc **desc_array,int *value_array);/* Value get/set from sleeping context */ int gpiod_get_value_cansleep (const struct gpio_desc *desc);void gpiod_set_value_cansleep (struct gpio_desc *desc,int value);int gpiod_get_raw_value_cansleep (const struct gpio_desc *desc);void gpiod_set_raw_value_cansleep (struct gpio_desc *desc,int value); +void gpiod_set_raw_array_cansleep (unsigned int array_size, +struct gpio_desc **desc_array, +int *value_array);int gpiod_set_debounce (struct gpio_desc *desc,unsigned debounce);
@@ -150,6 +155,12 @@ static inline void gpiod_set_raw_value(struct gpio_desc *desc, int value)/* GPIO can never have been requested */ WARN_ON (1 ); } +void gpiod_set_raw_array (unsigned int array_size, +struct gpio_desc **desc_array,int *value_array) +{ +/* GPIO can never have been requested */ +WARN_ON (1 ); +}static inline int gpiod_get_value_cansleep (const struct gpio_desc *desc) {
@@ -174,6 +185,13 @@ static inline void gpiod_set_raw_value_cansleep(struct gpio_desc *desc,/* GPIO can never have been requested */ WARN_ON (1 ); } +void gpiod_set_raw_array_cansleep (unsigned int array_size, +struct gpio_desc **desc_array, +int *value_array) +{ +/* GPIO can never have been requested */ +WARN_ON (1 ); +}static inline int gpiod_set_debounce (struct gpio_desc *desc,unsigned debounce) {
include/linux/gpio/driver.h +3 -0
@@ -32,6 +32,7 @@ struct seq_file;* @get: returns value for signal "offset"; for output signals this * returns either the value actually sensed, or zero * @set: assigns output value for signal "offset" +* @set_multiple: assigns output values for multiple signals defined by "mask" * @set_debounce: optional hook for setting debounce time for specified gpio in * interrupt triggered gpio chips * @to_irq: optional hook supporting non-static gpio_to_irq() mappings;
@@ -84,6 +85,8 @@ struct gpio_chip { unsigned offset);void (*set)(struct gpio_chip *chip,unsigned offset,int value); +void (*set_multiple)(struct gpio_chip *chip, +u32 *mask,u32 *bits);int (*set_debounce)(struct gpio_chip *chip,unsigned offset,unsigned debounce);
--
1.8.5.5
RFC PATCH 2/2 gpio-mpc8xxx: add mpc8xxx_gpio_set_multiple function
This patch adds a set_multiple function to the MPC8xxx GPIO chip driver. It depends on my previous patch "gpiolib: allow simultaneous setting of multiple GPIO outputs" and allows for actual performance improvements when setting multiple outputs simultaneously. In my case the time needed to configure an FPGA goes down from 48 s to 19 s.
--- drivers/gpio/gpio-mpc8xxx.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
drivers/gpio/gpio-mpc8xxx.c +28 -0
@@ -105,6 +105,33 @@ static void mpc8xxx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)spin_unlock_irqrestore (&mpc8xxx_gc->lock, flags); } +static void mpc8xxx_gpio_set_multiple (struct gpio_chip *gc, +u32 *mask,u32 *bits) +{ +struct of_mm_gpio_chip *mm =to_of_mm_gpio_chip (gc); +struct mpc8xxx_gpio_chip *mpc8xxx_gc =to_mpc8xxx_gpio_chip (mm); +unsigned long flags; +int i; + +spin_lock_irqsave (&mpc8xxx_gc->lock, flags); + +for (i =0 ; i < gc->ngpio; i++) { +if (*mask ==0 ) +break ; +if (*mask & (1 << i)) { +if ((*bits >> i) &1 ) + mpc8xxx_gc->data |=mpc8xxx_gpio2mask (i); +else + mpc8xxx_gc->data &= ~mpc8xxx_gpio2mask (i); + *mask &= ~(1 << i); + } + } + +out_be32 (mm->regs +GPIO_DAT , mpc8xxx_gc->data); + +spin_unlock_irqrestore (&mpc8xxx_gc->lock, flags); +} +static int mpc8xxx_gpio_dir_in (struct gpio_chip *gc,unsigned int gpio) {struct of_mm_gpio_chip *mm =to_of_mm_gpio_chip (gc);
@@ -344,6 +371,7 @@ static void __init mpc8xxx_add_controller(struct device_node *np) gc->get =of_device_is_compatible (np,"fsl,mpc8572-gpio" ) ? mpc8572_gpio_get : mpc8xxx_gpio_get; gc->set = mpc8xxx_gpio_set; + gc->set_multiple = mpc8xxx_gpio_set_multiple; gc->to_irq = mpc8xxx_gpio_to_irq; ret =of_mm_gpiochip_add (np, mm_gc);
--
1.8.5.5
Regarding the subject: Should you not flag this as "another iteration" (v2 or something) since there already was a submission for this very feature (2014-01-23)? Reviewers may want to look up the previous version, and see whether you addressed feedback that was provided so far.
Quoted 8 lines · Rojhalat Ibrahim
This patch introduces a new function
gpiod_set_raw_arrayto the consumer interface which allows setting multiple outputs with just one function call. It also adds an optionalset_multiplefunction to the driver interface.Multiple GPIOs are represented by an array of descriptors. The values to be set are stored in an integer array. Example:
Quoted 7 lines
struct gpio_desc *desc_array[10]; int value_array[10]; ... acquire descriptors ... ... set values in value_array ... gpiod_set_raw_array(10, desc_array, value_array);
It's nice that you provide this information, but could this not better be done in a text file which is kept in the Documentation part of the source tree? This would live longer and be more apparent to fellow developers than the commit message, and would cut down on the commit message. After all you do introduce a new and non-trivial feature.
Quoted 4 lines
Benefits:
- Uses descriptor interface. GPIOs have to be acquired before use.
- Allows arbitrary groups of GPIOs spanning multiple chips.
- Works without adjustments in GPIO chip drivers.
Quoted 8 lines
- Implementing the new
set_multiplefunction in a chip driver results in additional benefits:
* Improved performance for certain use cases. The original motivation for this was the task of configuring an FPGA. In that specific case, where 9 GPIO lines have to be set many times, configuration time goes down from 48 s to 19 s when using the new function.
* Simultaneous glitch-free setting of multiple pins on any kind of parallel bus attached to GPIOs.
Note that the glitch-free setting of multiple pins is only available if all the pins reside in the same bank of one specific chip that implements the set_multiple feature. Given how generic the GPIO API is (users need not care which banks the pins are on, whether they are different types of chips, how the chips are connected to the CPU (think expanders), or what their respective drivers' feature sets are), there may be more involved than just implementing the set_multiple function.
Style nit: Please check your commit messages for appropriate line lengths.
Quoted 8 lines
Limitations:
- Performance is only improved for normal high-low outputs. Open drain and open source outputs are always set separately from each other. Those kinds of outputs could probably be accelerated in a similar way if we could forgo the error checking when setting GPIO directions.
- There is nogpiod_set_arrayfunction that regards theACTIVE_LOWbits. The _raw_ function should be sufficient for many use cases. So I avoided the code duplication the other functions would require.
Have you considered the implications of running this new feature on a board which has multiple GPIO chips of different types, where the use case involves arbitrary pins of these chips to "run the bus protocol"?
virtually yours
Gerhard Sittig
Signature · 3 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr. 5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-0 Fax: +49-8142-66989-80 Email: [email protected]
Re: [RFC PATCH 2/2] gpio-mpc8xxx: add mpc8xxx_gpio_set_multiple function
In reply to Rojhalat IbrahimQuoted 3 lines · Rojhalat Ibrahim
This patch adds a
set_multiplefunction to the MPC8xxx GPIO chip driver. It depends on my previous patch "gpiolib: allow simultaneous setting of
^^^^^^^^^^^^^
This information is relevant to review and pick up, but is not appropriate for the commit message (which ends up in the mainline repo where the condition does not apply). I'd suggest to put it into the comment section below the dashes line.
virtually yours
Gerhard Sittig
Signature · 3 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr. 5, D-82194 Groebenzell, Germany Phone: +49-8142-66989-0 Fax: +49-8142-66989-80 Email: [email protected]
Quoted 6 lines · On Thursday 22 May 2014 18:50:12 Gerhard Sittig wrote:
Regarding the subject: Should you not flag this as "another iteration" (v2 or something) since there already was a submission for this very feature (2014-01-23)? Reviewers may want to look up the previous version, and see whether you addressed feedback that was provided so far.
Ok. Will do next time.
Quoted 15 lines · Rojhalat Ibrahim
Quoted 14 lines
This patch introduces a new function
gpiod_set_raw_arrayto the consumer interface which allows setting multiple outputs with just one function call. It also adds an optionalset_multiplefunction to the driver interface.Multiple GPIOs are represented by an array of descriptors. The values to be set are stored in an integer array. Example:
struct gpio_desc *desc_array[10]; int value_array[10]; ... acquire descriptors ... ... set values in value_array ...
Quoted 9 lines
Quoted 1 line
gpiod_set_raw_array(10, desc_array, value_array);It's nice that you provide this information, but could this not better be done in a text file which is kept in the Documentation part of the source tree? This would live longer and be more apparent to fellow developers than the commit message, and would cut down on the commit message. After all you do introduce a new and non-trivial feature.
Ok. I can do that in the next version of the patch.
Quoted 13 lines
Quoted 4 lines
Benefits:
- Uses descriptor interface. GPIOs have to be acquired before use.
- Allows arbitrary groups of GPIOs spanning multiple chips.
- Works without adjustments in GPIO chip drivers.Quoted 8 lines
- Implementing the new
set_multiplefunction in a chip driver results in additional benefits:
* Improved performance for certain use cases. The original motivation for this was the task of configuring an FPGA. In that specific case, where 9 GPIO lines have to be set many times, configuration time goes down from 48 s to 19 s when using the new function.
* Simultaneous glitch-free setting of multiple pins on any kind of parallel bus attached to GPIOs.
Quoted 9 lines
Note that the glitch-free setting of multiple pins is only available if all the pins reside in the same bank of one specific chip that implements the
set_multiplefeature. Given how generic the GPIO API is (users need not care which banks the pins are on, whether they are different types of chips, how the chips are connected to the CPU (think expanders), or what their respective drivers' feature sets are), there may be more involved than just implementing theset_multiplefunction.
Right. That limitation should be explicitely noted. Will do next time. But what does that imply? Do you think we should add a function that lets the user check if a given group of pins all belong to the same chip and / or if the driver for that chip has the set_multiple function implemented? That would give the user a way to find out what he can expect but not without having to think about such details.
Quoted 3 lines
Style nit: Please check your commit messages for appropriate line lengths.
I did. None of the lines exceed 80 characters.
Quoted 9 lines
Quoted 8 lines
Limitations:
- Performance is only improved for normal high-low outputs. Open drain and open source outputs are always set separately from each other. Those kinds of outputs could probably be accelerated in a similar way if we could forgo the error checking when setting GPIO directions.
- There is nogpiod_set_arrayfunction that regards theACTIVE_LOWbits. The _raw_ function should be sufficient for many use cases. So I avoided the code duplication the other functions would require.
Quoted 5 lines
Have you considered the implications of running this new feature on a board which has multiple GPIO chips of different types, where the use case involves arbitrary pins of these chips to "run the bus protocol"?
I have. All I can do is set all the pins on a chip by chip basis and use the functions the chip drivers provide. I don't know how I could guarantee glitch-free operation in such a case.
Thanks
Rojhalat
Re: [RFC PATCH 2/2] gpio-mpc8xxx: add mpc8xxx_gpio_set_multiple function
In reply to Gerhard SittigQuoted 12 lines · On Thursday 22 May 2014 18:54:18 Gerhard Sittig wrote:
Quoted 3 lines · Rojhalat Ibrahim
This patch adds a
set_multiplefunction to the MPC8xxx GPIO chip driver. It depends on my previous patch "gpiolib: allow simultaneous setting of^^^^^^^^^^^^^
This information is relevant to review and pick up, but is not appropriate for the commit message (which ends up in the mainline repo where the condition does not apply). I'd suggest to put it into the comment section below the dashes line.
Ok. Will do next time.
Thanks
Rojhalat