Skip to contentSkip to conversation

gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

PATCH Applied Andy Shevchenko 3 participants 11 messages Nov 7, 2022 – Nov 9, 2022 GPIO

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpiolib: of: Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode

GPIO library is getting rid of of_node, fwnode should be utilized instead. Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.

---
 drivers/gpio/gpiolib-of.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-of.c +6 -4
index be9c34cca322..000020eb78d8 100644
@@ -1104,9 +1104,11 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
 
 int of_gpiochip_add(struct gpio_chip *chip)
 {
+	struct device_node *np;
 	int ret;
 
-	if (!chip->of_node)
+	np = to_of_node(chip->fwnode);
+	if (!np)
 		return 0;
 
 	if (!chip->of_xlate) {
@@ -1123,18 +1125,18 @@ int of_gpiochip_add(struct gpio_chip *chip)
 	if (ret)
 		return ret;
 
-	of_node_get(chip->of_node);
+	fwnode_handle_get(chip->fwnode);
 
 	ret = of_gpiochip_scan_gpios(chip);
 	if (ret)
-		of_node_put(chip->of_node);
+		fwnode_handle_put(chip->fwnode);
 
 	return ret;
 }
 
 void of_gpiochip_remove(struct gpio_chip *chip)
 {
-	of_node_put(chip->of_node);
+	fwnode_handle_put(chip->fwnode);
 }
 
 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

In preparation to complete fwnode switch, integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask().

---
 drivers/gpio/gpiolib-of.c | 42 -------------------------------
 drivers/gpio/gpiolib-of.h |  5 ----
 drivers/gpio/gpiolib.c    | 52 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 51 insertions(+), 48 deletions(-)
drivers/gpio/gpiolib-of.c +0 -42
index 000020eb78d8..4be3c21aa718 100644
@@ -112,24 +112,6 @@ static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
 	return gpiochip_get_desc(chip, ret);
 }
 
-/**
- * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
- * to set the .valid_mask
- * @gc: the target gpio_chip
- *
- * Return: true if the valid mask needs to be set
- */
-bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
-{
-	int size;
-	const struct device_node *np = gc->of_node;
-
-	size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
-	if (size > 0 && size % 2 == 0)
-		return true;
-	return false;
-}
-
 /*
  * Overrides stated polarity of a gpio line and warns when there is a
  * discrepancy.
@@ -989,28 +971,6 @@ void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
 }
 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
 
-static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
-{
-	int len, i;
-	u32 start, count;
-	struct device_node *np = chip->of_node;
-
-	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
-	if (len < 0 || len % 2 != 0)
-		return;
-
-	for (i = 0; i < len; i += 2) {
-		of_property_read_u32_index(np, "gpio-reserved-ranges",
-					   i, &start);
-		of_property_read_u32_index(np, "gpio-reserved-ranges",
-					   i + 1, &count);
-		if (start >= chip->ngpio || start + count > chip->ngpio)
-			continue;
-
-		bitmap_clear(chip->valid_mask, start, count);
-	}
-};
-
 #ifdef CONFIG_PINCTRL
 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
 {
@@ -1119,8 +1079,6 @@ int of_gpiochip_add(struct gpio_chip *chip)
 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
 		return -EINVAL;
 
-	of_gpiochip_init_valid_mask(chip);
-
 	ret = of_gpiochip_add_pin_range(chip);
 	if (ret)
 		return ret;
drivers/gpio/gpiolib-of.h +0 -5
index 1b5df39a952e..2a2f7d17fa7e 100644
@@ -23,7 +23,6 @@ struct gpio_desc *of_find_gpio(struct device *dev,
 int of_gpiochip_add(struct gpio_chip *gc);
 void of_gpiochip_remove(struct gpio_chip *gc);
 int of_gpio_get_count(struct device *dev, const char *con_id);
-bool of_gpio_need_valid_mask(const struct gpio_chip *gc);
 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev);
 #else
 static inline struct gpio_desc *of_find_gpio(struct device *dev,
@@ -39,10 +38,6 @@ static inline int of_gpio_get_count(struct device *dev, const char *con_id)
 {
 	return 0;
 }
-static inline bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
-{
-	return false;
-}
 static inline void of_gpio_dev_init(struct gpio_chip *gc,
 				    struct gpio_device *gdev)
 {
drivers/gpio/gpiolib.c +51 -1

68 lines not on this page · raw message

Signature · 1 line
2.35.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH v1 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

Hi Andy,

drivers/gpio/gpiolib.c static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc) · line 452

Quoted 5 lines · Andy Shevchenko
drivers/gpio/gpiolib.c +5 -0
+static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
+{
+	int size;
+
+	size = fwnode_property_count_u32(gc->fwnode, "gpio-reserved-ranges");

I wonder if a comment why we need even size would not be helpful.

Quoted 25 lines
drivers/gpio/gpiolib.c +15 -1
+	if (size > 0 && size % 2 == 0)
+		return size;
+
+	return 0;
+}
+
 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
 {
-	if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
+	if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
 		return 0;
 
 	gc->valid_mask = gpiochip_allocate_mask(gc);
@@ -457,8 +468,47 @@ static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
 	return 0;
 }
 
+static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc, unsigned int sz)
+{
+	u32 *ranges;
+	int ret;
+
+	ranges = kmalloc_array(sz, sizeof(*ranges), GFP_KERNEL);
+	if (!ranges)
+		return -ENOMEM;

drivers/gpio/gpiolib.c static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) · line 488

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+
+	ret = fwnode_property_read_u32_array(gc->fwnode, "gpio-reserved-ranges", ranges, sz);
+	if (ret) {
+		kfree(ranges);
+		return ret;
+	}
+
+	while (sz) {
+		u32 count = ranges[--sz];
+		u32 start = ranges[--sz];

I know we checked sz validity, but I wonder if re-checking it in this function would not insulate us from errors creeping in after some other code refactoring.

In any case,

Thanks.

Signature · 1 line
Dmitry
#3 lore raw

Re: [PATCH v1 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

Quoted 8 lines · Dmitry Torokhov

drivers/gpio/gpiolib.c static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc) · line 452

Quoted 5 lines · Andy Shevchenko
drivers/gpio/gpiolib.c +5 -0
+static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
+{
+	int size;
+
+	size = fwnode_property_count_u32(gc->fwnode, "gpio-reserved-ranges");

I wonder if a comment why we need even size would not be helpful.

Was it in the original code?
Anyway, if Bart thinks so as well, I may add it in v2.

Quoted 31 lines
Quoted 31 lines
drivers/gpio/gpiolib.c +21 -1
+	if (size > 0 && size % 2 == 0)
+		return size;
+
+	return 0;
+}
+
 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
 {
-	if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
+	if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
 		return 0;
 
 	gc->valid_mask = gpiochip_allocate_mask(gc);
@@ -457,8 +468,47 @@ static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
 	return 0;
 }
 
+static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc, unsigned int sz)
+{
+	u32 *ranges;
+	int ret;
+
+	ranges = kmalloc_array(sz, sizeof(*ranges), GFP_KERNEL);
+	if (!ranges)
+		return -ENOMEM;
+
+	ret = fwnode_property_read_u32_array(gc->fwnode, "gpio-reserved-ranges", ranges, sz);
+	if (ret) {
+		kfree(ranges);
+		return ret;
+	}
Quoted 8 lines

drivers/gpio/gpiolib.c static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) · line 488

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+
+	while (sz) {
+		u32 count = ranges[--sz];
+		u32 start = ranges[--sz];

I know we checked sz validity, but I wonder if re-checking it in this function would not insulate us from errors creeping in after some other code refactoring.

I'm not sure I understand what you meant. The fwnode_property_read_u32_array() will fail if the given sz is too big for the real data, so while (sz) would never even go on the invalid data.

Quoted 3 lines

In any case,

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v1 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

Quoted 53 lines · Andy Shevchenko
Quoted 8 lines · Dmitry Torokhov

drivers/gpio/gpiolib.c static unsigned long *gpiochip_allocate_mask(struct gpio_chip *gc) · line 452

Quoted 5 lines · Andy Shevchenko
drivers/gpio/gpiolib.c +5 -0
+static unsigned int gpiochip_count_reserved_ranges(struct gpio_chip *gc)
+{
+	int size;
+
+	size = fwnode_property_count_u32(gc->fwnode, "gpio-reserved-ranges");

I wonder if a comment why we need even size would not be helpful.

Was it in the original code?
Anyway, if Bart thinks so as well, I may add it in v2.

Quoted 31 lines
Quoted 31 lines
drivers/gpio/gpiolib.c +21 -1
+	if (size > 0 && size % 2 == 0)
+		return size;
+
+	return 0;
+}
+
 static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
 {
-	if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
+	if (!(gpiochip_count_reserved_ranges(gc) || gc->init_valid_mask))
 		return 0;
 
 	gc->valid_mask = gpiochip_allocate_mask(gc);
@@ -457,8 +468,47 @@ static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
 	return 0;
 }
 
+static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc, unsigned int sz)
+{
+	u32 *ranges;
+	int ret;
+
+	ranges = kmalloc_array(sz, sizeof(*ranges), GFP_KERNEL);
+	if (!ranges)
+		return -ENOMEM;
+
+	ret = fwnode_property_read_u32_array(gc->fwnode, "gpio-reserved-ranges", ranges, sz);
+	if (ret) {
+		kfree(ranges);
+		return ret;
+	}
Quoted 8 lines

drivers/gpio/gpiolib.c static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) · line 488

Quoted 4 lines
drivers/gpio/gpiolib.c +4 -0
+
+	while (sz) {
+		u32 count = ranges[--sz];
+		u32 start = ranges[--sz];

I know we checked sz validity, but I wonder if re-checking it in this function would not insulate us from errors creeping in after some other code refactoring.

Quoted 3 lines

I'm not sure I understand what you meant. The fwnode_property_read_u32_array() will fail if the given sz is too big for the real data, so while (sz) would never even go on the invalid data.

I am more worried about sz being odd and the loop ending up trying to dereference ranges[-1].

Thanks.

Signature · 1 line
Dmitry
#5 lore raw

Re: [PATCH v1 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

Quoted 3 lines · Dmitry Torokhov
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Dmitry Torokhov

On Mon, Nov 07, 2022 at 06:10:27PM +0200, Andy Shevchenko wrote:

...

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 8 lines
drivers/gpio/gpiolib.c +8 -0
+static int gpiochip_apply_reserved_ranges(struct gpio_chip *gc, unsigned int sz)
+{
+	u32 *ranges;
+	int ret;
+
+	ranges = kmalloc_array(sz, sizeof(*ranges), GFP_KERNEL);
+	if (!ranges)
+		return -ENOMEM;

drivers/gpio/gpiolib.c static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) · line 488

Quoted 10 lines
drivers/gpio/gpiolib.c +10 -0
+
+	ret = fwnode_property_read_u32_array(gc->fwnode, "gpio-reserved-ranges", ranges, sz);
+	if (ret) {
+		kfree(ranges);
+		return ret;
+	}
+
+	while (sz) {
+		u32 count = ranges[--sz];
+		u32 start = ranges[--sz];

I know we checked sz validity, but I wonder if re-checking it in this function would not insulate us from errors creeping in after some other code refactoring.

Quoted 6 lines
Quoted 3 lines

I'm not sure I understand what you meant. The fwnode_property_read_u32_array() will fail if the given sz is too big for the real data, so while (sz) would never even go on the invalid data.

I am more worried about sz being odd and the loop ending up trying to dereference ranges[-1].

I see. What if we take amount of ranges as the parameter and convert to size by multiplying by 2?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v1 2/2] gpiolib: of: Integrate of_gpiochip_init_valid_mask() into gpiochip_init_valid_mask()

Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Dmitry Torokhov

On Mon, Nov 07, 2022 at 11:09:19PM +0200, Andy Shevchenko wrote:

...

Quoted 5 lines
Quoted 2 lines

I am more worried about sz being odd and the loop ending up trying to dereference ranges[-1].

I see. What if we take amount of ranges as the parameter and convert to size by multiplying by 2?

Okay, I found a way how to avoid additional churn and add validation. I will incorporate that in v2 with your tags.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v1 1/2
#7 lore raw
In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko

GPIO library is getting rid of of_node, fwnode should be utilized instead. Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.

Quoted 46 lines
---
 drivers/gpio/gpiolib-of.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-of.c +6 -4
index be9c34cca322..000020eb78d8 100644
@@ -1104,9 +1104,11 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
 
 int of_gpiochip_add(struct gpio_chip *chip)
 {
+	struct device_node *np;
 	int ret;
 
-	if (!chip->of_node)
+	np = to_of_node(chip->fwnode);
+	if (!np)
 		return 0;
 
 	if (!chip->of_xlate) {
@@ -1123,18 +1125,18 @@ int of_gpiochip_add(struct gpio_chip *chip)
 	if (ret)
 		return ret;
 
-	of_node_get(chip->of_node);
+	fwnode_handle_get(chip->fwnode);
 
 	ret = of_gpiochip_scan_gpios(chip);
 	if (ret)
-		of_node_put(chip->of_node);
+		fwnode_handle_put(chip->fwnode);
 
 	return ret;
 }
 
 void of_gpiochip_remove(struct gpio_chip *chip)
 {
-	of_node_put(chip->of_node);
+	fwnode_handle_put(chip->fwnode);
 }
 
 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
Signature · 1 line
2.35.1
Signature · 1 line
Dmitry
#8 lore raw
In reply to Andy Shevchenko
Quoted 49 lines · Andy Shevchenko

GPIO library is getting rid of of_node, fwnode should be utilized instead. Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.

---
 drivers/gpio/gpiolib-of.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-of.c +6 -5
index be9c34cca322..000020eb78d8 100644
@@ -1104,9 +1104,11 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
 
 int of_gpiochip_add(struct gpio_chip *chip)
 {
+       struct device_node *np;
        int ret;
 
-       if (!chip->of_node)
+       np = to_of_node(chip->fwnode);
+       if (!np)
                return 0;
 
        if (!chip->of_xlate) {
@@ -1123,18 +1125,18 @@ int of_gpiochip_add(struct gpio_chip *chip)
        if (ret)
                return ret;
 
-       of_node_get(chip->of_node);
+       fwnode_handle_get(chip->fwnode);
 
        ret = of_gpiochip_scan_gpios(chip);
        if (ret)
-               of_node_put(chip->of_node);
+               fwnode_handle_put(chip->fwnode);
 
        return ret;
 }
 
 void of_gpiochip_remove(struct gpio_chip *chip)
 {
-       of_node_put(chip->of_node);
+       fwnode_handle_put(chip->fwnode);
 }
 
 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
--
Quoted 2 lines

2.35.1

Applied, thanks!

Bart

#9 lore raw
Quoted 50 lines · Bartosz Golaszewski
Quoted 47 lines · Andy Shevchenko

GPIO library is getting rid of of_node, fwnode should be utilized instead. Prepare of_gpiochip_add() / of_gpiochip_remove() for fwnode.

---
 drivers/gpio/gpiolib-of.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-of.c +6 -4
index be9c34cca322..000020eb78d8 100644
@@ -1104,9 +1104,11 @@ static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
 
 int of_gpiochip_add(struct gpio_chip *chip)
 {
+       struct device_node *np;
        int ret;
 
-       if (!chip->of_node)
+       np = to_of_node(chip->fwnode);
+       if (!np)
                return 0;
 
        if (!chip->of_xlate) {
@@ -1123,18 +1125,18 @@ int of_gpiochip_add(struct gpio_chip *chip)
        if (ret)
                return ret;
 
-       of_node_get(chip->of_node);
+       fwnode_handle_get(chip->fwnode);
 
        ret = of_gpiochip_scan_gpios(chip);
        if (ret)
-               of_node_put(chip->of_node);
+               fwnode_handle_put(chip->fwnode);
 
        return ret;
 }
 
 void of_gpiochip_remove(struct gpio_chip *chip)
 {
-       of_node_put(chip->of_node);
+       fwnode_handle_put(chip->fwnode);
 }
 
Quoted 8 lines
Quoted 4 lines
drivers/gpio/gpiolib-of.c +0 -1
 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
--

2.35.1

Applied, thanks!

Bart

I actually applied v2 and both the patches from this series.

Bart

#10 lore raw
Quoted 3 lines · Bartosz Golaszewski
Quoted 2 lines · Bartosz Golaszewski

On Mon, Nov 7, 2022 at 5:10 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 3 lines
Quoted 2 lines

Applied, thanks!

I actually applied v2 and both the patches from this series.

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko