Skip to contentSkip to conversation

gpiolib: Deduplicate some code in for_each_requested_gpio_in_range()

PATCH Superseded Andy Shevchenko 2 participants 5 messages Feb 4, 2025 – Feb 7, 2025 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: Deduplicate some code in for_each_requested_gpio_in_range()

Refactor for_each_requested_gpio_in_range() to deduplicate some code which is basically repeats the for_each_hwgpio(). In order to achieve this, split the latter to two, for_each_hwgpio_in_range() and for_each_hwgpio().

---
 include/linux/gpio/driver.h | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
include/linux/gpio/driver.h +11 -10
index a307d8ed9c51..24890ecfa0fb 100644
@@ -549,20 +549,25 @@ DEFINE_CLASS(_gpiochip_for_each_data,
 	     const char **label, int *i)
 
 /**
- * for_each_hwgpio - Iterates over all GPIOs for given chip.
+ * for_each_hwgpio_in_range - Iterates over all GPIOs in a given range
  * @_chip: Chip to iterate over.
  * @_i: Loop counter.
+ * @_base: First GPIO in the ranger.
+ * @_size: Amount of GPIOs to check starting from @base.
  * @_label: Place to store the address of the label if the GPIO is requested.
  *          Set to NULL for unused GPIOs.
  */
-#define for_each_hwgpio(_chip, _i, _label) \
+#define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)			\
 	for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \
-	     *_data.i < _chip->ngpio; \
+	     *_data.i < _size;								\
 	     (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \
 		if (IS_ERR(*_data.label = \
-			gpiochip_dup_line_label(_chip, *_data.i))) {} \
+			gpiochip_dup_line_label(_chip, _base + *_data.i))) {}		\
 		else
 
+#define for_each_hwgpio(_chip, _i, _label)						\
+	for_each_hwgpio_in_range(_chip, _i, 0, _chip->ngpio, _label)
+
 /**
  * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
  * @_chip:	the chip to query
@@ -572,12 +577,8 @@ DEFINE_CLASS(_gpiochip_for_each_data,
  * @_label:	label of current GPIO
  */
 #define for_each_requested_gpio_in_range(_chip, _i, _base, _size, _label)		\
-	for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i);			\
-	     *_data.i < _size;								\
-	     (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL)			\
-		if ((*_data.label =							\
-			gpiochip_dup_line_label(_chip, _base + *_data.i)) == NULL) {}	\
-		else if (IS_ERR(*_data.label)) {}					\
+	for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)			\
+		if (*_data.label == NULL) {}						\
 		else
 
 /* Iterates over all requested GPIO of the given @chip */
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#1 lore raw
Quoted 29 lines · Andy Shevchenko

Refactor for_each_requested_gpio_in_range() to deduplicate some code which is basically repeats the for_each_hwgpio(). In order to achieve this, split the latter to two, for_each_hwgpio_in_range() and for_each_hwgpio().

---
 include/linux/gpio/driver.h | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
include/linux/gpio/driver.h +3 -2
index a307d8ed9c51..24890ecfa0fb 100644
@@ -549,20 +549,25 @@ DEFINE_CLASS(_gpiochip_for_each_data,
             const char **label, int *i)
 
 /**
- * for_each_hwgpio - Iterates over all GPIOs for given chip.
+ * for_each_hwgpio_in_range - Iterates over all GPIOs in a given range
  * @_chip: Chip to iterate over.
  * @_i: Loop counter.
+ * @_base: First GPIO in the ranger.
+ * @_size: Amount of GPIOs to check starting from @base.
  * @_label: Place to store the address of the label if the GPIO is requested.
  *          Set to NULL for unused GPIOs.
  */
-#define for_each_hwgpio(_chip, _i, _label) \

include/linux/gpio/driver.h DEFINE_CLASS(_gpiochip_for_each_data, · line 567

Quoted 10 lines
include/linux/gpio/driver.h +3 -2
+#define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)                      \
        for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \
-            *_data.i < _chip->ngpio; \
+            *_data.i < _size;                                                          \
             (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \
                if (IS_ERR(*_data.label = \
-                       gpiochip_dup_line_label(_chip, *_data.i))) {} \
+                       gpiochip_dup_line_label(_chip, _base + *_data.i))) {}           \
                else
 

Can you add a kerneldoc here as well, please?

Bart

Quoted 24 lines
include/linux/gpio/driver.h +5 -7
+#define for_each_hwgpio(_chip, _i, _label)                                             \
+       for_each_hwgpio_in_range(_chip, _i, 0, _chip->ngpio, _label)
+
 /**
  * for_each_requested_gpio_in_range - iterates over requested GPIOs in a given range
  * @_chip:     the chip to query
@@ -572,12 +577,8 @@ DEFINE_CLASS(_gpiochip_for_each_data,
  * @_label:    label of current GPIO
  */
 #define for_each_requested_gpio_in_range(_chip, _i, _base, _size, _label)              \
-       for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i);                       \
-            *_data.i < _size;                                                          \
-            (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL)                  \
-               if ((*_data.label =                                                     \
-                       gpiochip_dup_line_label(_chip, _base + *_data.i)) == NULL) {}   \
-               else if (IS_ERR(*_data.label)) {}                                       \
+       for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)                       \
+               if (*_data.label == NULL) {}                                            \
                else
 
 /* Iterates over all requested GPIO of the given @chip */
--

2.43.0.rc1.1336.g36b5255a03ac

#2 lore raw
Quoted 2 lines · Bartosz Golaszewski

On Tue, Feb 4, 2025 at 6:57 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 14 lines
Quoted 14 lines

/**
- * for_each_hwgpio - Iterates over all GPIOs for given chip.
+ * for_each_hwgpio_in_range - Iterates over all GPIOs in a given range
* @_chip: Chip to iterate over.
* @_i: Loop counter.
+ * @_base: First GPIO in the ranger.
+ * @_size: Amount of GPIOs to check starting from @base.
* @_label: Place to store the address of the label if the GPIO is requested.
* Set to NULL for unused GPIOs.
*/

include/linux/gpio/driver.h +1 -2
-#define for_each_hwgpio(_chip, _i, _label) \
+#define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)                      \
        for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \
-            *_data.i < _chip->ngpio; \
Quoted 8 lines

include/linux/gpio/driver.h DEFINE_CLASS(_gpiochip_for_each_data, · line 566

Quoted 6 lines
include/linux/gpio/driver.h +2 -1
+            *_data.i < _size;                                                          \
             (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \
                if (IS_ERR(*_data.label = \
-                       gpiochip_dup_line_label(_chip, *_data.i))) {} \
+                       gpiochip_dup_line_label(_chip, _base + *_data.i))) {}           \
                else

Can you add a kerneldoc here as well, please?

Sure, but it will duplicate the above.

Quoted 2 lines
Quoted 2 lines
include/linux/gpio/driver.h +2 -0
+#define for_each_hwgpio(_chip, _i, _label)                                             \
+       for_each_hwgpio_in_range(_chip, _i, 0, _chip->ngpio, _label)
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 30 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Tue, Feb 4, 2025 at 6:57 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 14 lines
Quoted 14 lines

/**
- * for_each_hwgpio - Iterates over all GPIOs for given chip.
+ * for_each_hwgpio_in_range - Iterates over all GPIOs in a given range
* @_chip: Chip to iterate over.
* @_i: Loop counter.
+ * @_base: First GPIO in the ranger.
+ * @_size: Amount of GPIOs to check starting from @base.
* @_label: Place to store the address of the label if the GPIO is requested.
* Set to NULL for unused GPIOs.
*/

include/linux/gpio/driver.h +1 -2
-#define for_each_hwgpio(_chip, _i, _label) \
+#define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)                      \
        for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \
-            *_data.i < _chip->ngpio; \
Quoted 8 lines

include/linux/gpio/driver.h DEFINE_CLASS(_gpiochip_for_each_data, · line 566

Quoted 6 lines
include/linux/gpio/driver.h +2 -1
+            *_data.i < _size;                                                          \
             (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \
                if (IS_ERR(*_data.label = \
-                       gpiochip_dup_line_label(_chip, *_data.i))) {} \
+                       gpiochip_dup_line_label(_chip, _base + *_data.i))) {}           \
                else

Can you add a kerneldoc here as well, please?

Quoted 2 lines

Sure, but it will duplicate the above.

Will it though? It's a separate macro with different semantics.

Bart

#4 lore raw
Quoted 5 lines · Bartosz Golaszewski
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Tue, Feb 4, 2025 at 6:57 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 10 lines

/**
- * for_each_hwgpio - Iterates over all GPIOs for given chip.
+ * for_each_hwgpio_in_range - Iterates over all GPIOs in a given range
* @_chip: Chip to iterate over.
* @_i: Loop counter.
+ * @_base: First GPIO in the ranger.
+ * @_size: Amount of GPIOs to check starting from @base.
* @_label: Place to store the address of the label if the GPIO is requested.
* Set to NULL for unused GPIOs.
*/

include/linux/gpio/driver.h DEFINE_CLASS(_gpiochip_for_each_data, · line 566

Quoted 10 lines
include/linux/gpio/driver.h +3 -3
-#define for_each_hwgpio(_chip, _i, _label) \
+#define for_each_hwgpio_in_range(_chip, _i, _base, _size, _label)                      \
        for (CLASS(_gpiochip_for_each_data, _data)(&_label, &_i); \
-            *_data.i < _chip->ngpio; \
+            *_data.i < _size;                                                          \
             (*_data.i)++, kfree(*(_data.label)), *_data.label = NULL) \
                if (IS_ERR(*_data.label = \
-                       gpiochip_dup_line_label(_chip, *_data.i))) {} \
+                       gpiochip_dup_line_label(_chip, _base + *_data.i))) {}           \
                else

Can you add a kerneldoc here as well, please?

Quoted 3 lines
Quoted 1 line

Sure, but it will duplicate the above.

Will it though? It's a separate macro with different semantics.

With 80%+. Whatever, I'll add it.

Signature · 2 lines
With Best Regards,
Andy Shevchenko