Skip to contentSkip to conversation

gpio: Fix ngpio in gpio-xilinx driver

PATCH Superseded Gernot Vormayr 2 participants 5 messages Sep 18, 2014 – Sep 24, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: Fix ngpio in gpio-xilinx driver

If one adds 'gpio-controller;' to the chip in the devicetree, then initialization fails with 'gpiochip_find_base: cannot find free range', because ngpio is 0. This patch fixes the bug.

Tested on ml507 board.

---
 drivers/gpio/gpio-xilinx.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpio/gpio-xilinx.c +7 -4
index 1248186..4606ad2 100644
@@ -197,6 +197,7 @@ static int xgpio_of_probe(struct device_node *np)
 	struct xgpio_instance *chip;
 	int status = 0;
 	const u32 *tree_info;
+	u32 ngpio;
 
 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
 	if (!chip)
@@ -215,8 +216,9 @@ static int xgpio_of_probe(struct device_node *np)
 	chip->mmchip.gc.ngpio = 32;
 
 	/* Check device node and parent device node for device width */
-	of_property_read_u32(np, "xlnx,gpio-width",
-			      (u32 *)&chip->mmchip.gc.ngpio);
+	status = of_property_read_u32(np, "xlnx,gpio-width", &ngpio);
+	if (status == 0)
+		chip->mmchip.gc.ngpio = (u16)ngpio;
 
 	spin_lock_init(&chip->gpio_lock);
 
@@ -262,8 +264,9 @@ static int xgpio_of_probe(struct device_node *np)
 		chip->mmchip.gc.ngpio = 32;
 
 		/* Check device node and parent device node for device width */
-		of_property_read_u32(np, "xlnx,gpio2-width",
-				     (u32 *)&chip->mmchip.gc.ngpio);
+		status = of_property_read_u32(np, "xlnx,gpio2-width", &ngpio);
+		if (status == 0)
+			chip->mmchip.gc.ngpio = (u16)ngpio;
 
 		spin_lock_init(&chip->gpio_lock);
 
Signature · 1 line
2.1.0
#1 lore raw
Quoted 24 lines · Gernot Vormayr

If one adds 'gpio-controller;' to the chip in the devicetree, then initialization fails with 'gpiochip_find_base: cannot find free range', because ngpio is 0. This patch fixes the bug.

Tested on ml507 board.

---
 drivers/gpio/gpio-xilinx.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpio/gpio-xilinx.c +1 -0
index 1248186..4606ad2 100644
@@ -197,6 +197,7 @@ static int xgpio_of_probe(struct device_node *np)
        struct xgpio_instance *chip;
        int status = 0;
        const u32 *tree_info;
+       u32 ngpio;
 
        chip = kzalloc(sizeof(*chip), GFP_KERNEL);
        if (!chip)

drivers/gpio/gpio-xilinx.c static int xgpio_of_probe(struct device_node *np) · line 216

Quoted 1 line
drivers/gpio/gpio-xilinx.c +0 -0
@@ -215,8 +216,9 @@ static int xgpio_of_probe(struct device_node *np)
        chip->mmchip.gc.ngpio = 32;

Delete that line ^

drivers/gpio/gpio-xilinx.c static int xgpio_of_probe(struct device_node *np) · line 221

Quoted 7 lines
drivers/gpio/gpio-xilinx.c +3 -2
        /* Check device node and parent device node for device width */
-       of_property_read_u32(np, "xlnx,gpio-width",
-                             (u32 *)&chip->mmchip.gc.ngpio);
+       status = of_property_read_u32(np, "xlnx,gpio-width", &ngpio);
+       if (status == 0)
+               chip->mmchip.gc.ngpio = (u16)ngpio;

Rewrite like this:

if (of_property_read_u32(np, "xlnx,gpio-width", &ngpio))

    ngpio = 32;

chip->mmchip.gc.ngpio = (u16)ngpio;

drivers/gpio/gpio-xilinx.c static int xgpio_of_probe(struct device_node *np) · line 269

Quoted 12 lines
drivers/gpio/gpio-xilinx.c +3 -2
        spin_lock_init(&chip->gpio_lock);
 
@@ -262,8 +264,9 @@ static int xgpio_of_probe(struct device_node *np)
                chip->mmchip.gc.ngpio = 32;
 
                /* Check device node and parent device node for device width */
-               of_property_read_u32(np, "xlnx,gpio2-width",
-                                    (u32 *)&chip->mmchip.gc.ngpio);
+               status = of_property_read_u32(np, "xlnx,gpio2-width", &ngpio);
+               if (status == 0)
+                       chip->mmchip.gc.ngpio = (u16)ngpio;

Same comment.

The fact that the same code appears in two places tells us something is not quite right. Can you look into consolidating this (in a separate patch)?

Yours,
Linus Walleij

#2 lore raw
Quoted 4 lines · Linus Walleij

The fact that the same code appears in two places tells us something is not quite right. Can you look into consolidating this (in a separate patch)?

Well, I already thought about that, but since the second time is for the second part of a dual gpio, which has different device tree property names, the only way of consolidating that I can think of right now would be to create a function that takes the property names. If you think that's ok, I can try to come up with something.

Cheers,
Gernot Vormayr

#3 lore raw

PATCH v2 1/1 gpio: Fix ngpio in gpio-xilinx driver

In reply to Gernot Vormayr

If one adds gpio-controller; to the chip in the devicetree, then initialization fails with 'gpiochip_find_base: cannot find free range', because ngpio is 0. This patch fixes the bug.

This version includes the suggestions from Linus Walleij.

Tested on ml507 board.

---
 drivers/gpio/gpio-xilinx.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpio/gpio-xilinx.c +11 -12
index 1248186..c11bc11 100644
@@ -197,6 +197,7 @@ static int xgpio_of_probe(struct device_node *np)
 	struct xgpio_instance *chip;
 	int status = 0;
 	const u32 *tree_info;
+	u32 ngpio;
 
 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
 	if (!chip)
@@ -211,12 +212,11 @@ static int xgpio_of_probe(struct device_node *np)
 	/* Update GPIO direction shadow register with default value */
 	of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir);
 
-	/* By default assume full GPIO controller */
-	chip->mmchip.gc.ngpio = 32;
-
-	/* Check device node and parent device node for device width */
-	of_property_read_u32(np, "xlnx,gpio-width",
-			      (u32 *)&chip->mmchip.gc.ngpio);
+	/* Check device node and parent device node for device width
+	   and assume default width of 32 */
+	if (of_property_read_u32(np, "xlnx,gpio-width", &ngpio))
+		ngpio = 32;
+	chip->mmchip.gc.ngpio = (u16)ngpio;
 
 	spin_lock_init(&chip->gpio_lock);
 
@@ -258,12 +258,11 @@ static int xgpio_of_probe(struct device_node *np)
 		/* Update GPIO direction shadow register with default value */
 		of_property_read_u32(np, "xlnx,tri-default-2", &chip->gpio_dir);
 
-		/* By default assume full GPIO controller */
-		chip->mmchip.gc.ngpio = 32;
-
-		/* Check device node and parent device node for device width */
-		of_property_read_u32(np, "xlnx,gpio2-width",
-				     (u32 *)&chip->mmchip.gc.ngpio);
+		/* Check device node and parent device node for device width
+		   and assume default width of 32 */
+		if (of_property_read_u32(np, "xlnx,gpio2-width", &ngpio))
+			ngpio = 32;
+		chip->mmchip.gc.ngpio = (u16)ngpio;
 
 		spin_lock_init(&chip->gpio_lock);
 
Signature · 1 line
2.1.0
#4 lore raw
Quoted 9 lines · Gernot Vormayr

If one adds gpio-controller; to the chip in the devicetree, then initialization fails with 'gpiochip_find_base: cannot find free range', because ngpio is 0. This patch fixes the bug.

This version includes the suggestions from Linus Walleij.

Tested on ml507 board.

Patch applied.

Yours,
Linus Walleij