Skip to contentSkip to conversation

gpio: move ppc4xx driver to drivers/gpio and modernize

PATCH Applied Rosen Penev 3 participants 12 messages Aug 3 – Aug 11

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

v2 cover gpio: move ppc4xx driver to drivers/gpio and modernize

This series moves the ppc4xx GPIO driver from arch/powerpc to drivers/gpio, converts it to be a proper platform driver using generic MMIO helpers, drops architecture-specific accessors, and prepares it for module build and COMPILE_TEST.

v2: use guard(), reorganize a bit for clarity.

Rosen Penev (8):
gpio: move ppc4xx gpio driver from arch/powerpc to drivers/gpio
gpio: ppc44x: update all 4xx to 44x
gpio: ppc44x: Use module platform driver helper for GPIO
gpio: ppc44x: Use platform resource helper for GPIO MMIO
gpio: ppc44x: Convert GPIO to generic MMIO
gpio: ppc44x: drop PPC-specific IO helpers
gpio: ppc44x: fix undefined behavior in GPIO_MASK2 macro
gpio: ppc44x: use dev_name() for chip label

 arch/powerpc/configs/44x/warp_defconfig |   2 +-
 arch/powerpc/configs/ppc44x_defconfig   |   2 +-
 arch/powerpc/platforms/44x/Kconfig      |   7 -
 arch/powerpc/platforms/44x/Makefile     |   2 +-
 arch/powerpc/platforms/44x/gpio.c       | 211 ------------------------
 drivers/gpio/Kconfig                    |   7 +
 drivers/gpio/Makefile                   |   1 +
 drivers/gpio/gpio-ppc44x.c              | 208 +++++++++++++++++++++++
 8 files changed, 219 insertions(+), 221 deletions(-)
 delete mode 100644 arch/powerpc/platforms/44x/gpio.c
 create mode 100644 drivers/gpio/gpio-ppc44x.c
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/8 gpio: move ppc4xx gpio driver from arch/powerpc to drivers/gpio

Move the ppc4xx gpio driver out of arch/powerpc/platforms/44x/ into drivers/gpio/gpio-ppc44x.c. The driver has no architecture-specific dependencies and follows the same pattern as other PowerPC GPIO drivers already in drivers/gpio/ (e.g. gpio-mpc8xxx, gpio-mpc5200).

- Renamed Kconfig symbol from PPC4xx_GPIO to GPIO_PPC44X
- Updated ppc44x_defconfig and warp_defconfig to use the new symbol
- Marked the new option as tristate (was bool) since the driver supports module build via module_platform_driver()

Assisted-by opencode:big-pickle
---
 arch/powerpc/configs/44x/warp_defconfig                    | 2 +-
 arch/powerpc/configs/ppc44x_defconfig                      | 2 +-
 arch/powerpc/platforms/44x/Kconfig                         | 7 -------
 arch/powerpc/platforms/44x/Makefile                        | 2 +-
 drivers/gpio/Kconfig                                       | 7 +++++++
 drivers/gpio/Makefile                                      | 1 +
 .../platforms/44x/gpio.c => drivers/gpio/gpio-ppc44x.c     | 0
 7 files changed, 11 insertions(+), 10 deletions(-)
 rename arch/powerpc/platforms/44x/gpio.c => drivers/gpio/gpio-ppc44x.c (100%)
arch/powerpc/configs/44x/warp_defconfig +1 -1
index 5757625469c4..d6014b9c5708 100644
@@ -12,7 +12,7 @@ CONFIG_MODULE_UNLOAD=y
 # CONFIG_BLK_DEV_BSG is not set
 # CONFIG_EBONY is not set
 CONFIG_WARP=y
-CONFIG_PPC4xx_GPIO=y
+CONFIG_GPIO_PPC44X=y
 CONFIG_HZ_1000=y
 CONFIG_CMDLINE="ip=on"
 # CONFIG_PCI is not set
arch/powerpc/configs/ppc44x_defconfig +1 -1
index 0dc537f6aff3..d351551e266f 100644
@@ -22,7 +22,7 @@ CONFIG_GLACIER=y
 CONFIG_REDWOOD=y
 CONFIG_EIGER=y
 CONFIG_YOSEMITE=y
-CONFIG_PPC4xx_GPIO=y
+CONFIG_GPIO_PPC44X=y
 CONFIG_MATH_EMULATION=y
 CONFIG_NET=y
 CONFIG_PACKET=y
arch/powerpc/platforms/44x/Kconfig +0 -7
index fc79f8466933..150813cea945 100644
@@ -227,13 +227,6 @@ config PPC44x_SIMPLE
 	help
 	  This option enables the simple PowerPC 44x platform support.
 
-config PPC4xx_GPIO
-	bool "PPC4xx GPIO support"
-	depends on 44x
-	select GPIOLIB
-	help
-	  Enable gpiolib support for ppc440 based boards
-
 # 44x specific CPU modules, selected based on the board above.
 config 440EP
 	bool
arch/powerpc/platforms/44x/Makefile +1 -1
index ca7b1bb442d9..4598d8b89bf4 100644
@@ -15,4 +15,4 @@ obj-$(CONFIG_FSP2)	+= fsp2.o
 obj-$(CONFIG_PCI)		+= pci.o
 obj-$(CONFIG_PPC4xx_HSTA_MSI)	+= hsta_msi.o
 obj-$(CONFIG_PPC4xx_CPM)	+= cpm.o
-obj-$(CONFIG_PPC4xx_GPIO)	+= gpio.o
+
drivers/gpio/Kconfig +7 -0
index af84285ed1bd..15fd5874f4ea 100644
@@ -593,6 +593,13 @@ config GPIO_POLARFIRE_SOC
 	help
 	  Say yes here to support the GPIO controllers on Microchip FPGAs.
 
+config GPIO_PPC44X
+	tristate "PPC44x GPIO support"
+	depends on 44x
+	select GPIO_GENERIC
+	help
+	  Enable gpiolib support for ppc440 based boards.
+
 config GPIO_PXA
 	bool "PXA GPIO support"
 	depends on ARCH_PXA || ARCH_MMP || COMPILE_TEST
drivers/gpio/Makefile +1 -0
index fa14581e3995..dc9e6d643b5b 100644
@@ -147,6 +147,7 @@ obj-$(CONFIG_GPIO_PCIE_IDIO_24)		+= gpio-pcie-idio-24.o
 obj-$(CONFIG_GPIO_PCI_IDIO_16)		+= gpio-pci-idio-16.o
 obj-$(CONFIG_GPIO_PISOSR)		+= gpio-pisosr.o
 obj-$(CONFIG_GPIO_PL061)		+= gpio-pl061.o
+obj-$(CONFIG_GPIO_PPC44X)		+= gpio-ppc44x.o
 obj-$(CONFIG_GPIO_PMIC_EIC_SPRD)	+= gpio-pmic-eic-sprd.o
 obj-$(CONFIG_GPIO_POLARFIRE_SOC)	+= gpio-mpfs.o
 obj-$(CONFIG_GPIO_PXA)			+= gpio-pxa.o
drivers/gpio/gpio-ppc44x.c renamed from arch/powerpc/platforms/44x/gpio.c +0 -0
similarity index 100%
rename from arch/powerpc/platforms/44x/gpio.c
rename to drivers/gpio/gpio-ppc44x.c
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/8 gpio: ppc44x: update all 4xx to 44x

In reply to Rosen Penev

The kernel lost support for 4xx platforms and now only supports 44x. Since this driver is being moved to drivers/gpio/ , take the opportunity to modernize the name.

---
 drivers/gpio/gpio-ppc44x.c | 68 +++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 34 deletions(-)
drivers/gpio/gpio-ppc44x.c +34 -34
index 4413a94cf7a6..9b874ff8b124 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * PPC4xx gpio driver
+ * PPC44x gpio driver
  *
  * Copyright (c) 2008 Harris Corporation
  * Copyright (c) 2008 Sascha Hauer <[email protected]>, Pengutronix
@@ -23,7 +23,7 @@
 #define GPIO_MASK2(gpio)	(0xc0000000 >> ((gpio) * 2))
 
 /* Physical GPIO register layout */
-struct ppc4xx_gpio {
+struct ppc44x_gpio {
 	__be32 or;
 	__be32 tcr;
 	__be32 osrl;
@@ -44,7 +44,7 @@ struct ppc4xx_gpio {
 	__be32 isr3h;
 };
 
-struct ppc4xx_gpio_chip {
+struct ppc44x_gpio_chip {
 	struct gpio_chip gc;
 	void __iomem *regs;
 	spinlock_t lock;
@@ -56,19 +56,19 @@ struct ppc4xx_gpio_chip {
  * There are a maximum of 32 gpios in each gpio controller.
  */
 
-static int ppc4xx_gpio_get(struct gpio_chip *gc, unsigned int gpio)
+static int ppc44x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
 {
-	struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
-	struct ppc4xx_gpio __iomem *regs = chip->regs;
+	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
+	struct ppc44x_gpio __iomem *regs = chip->regs;
 
 	return !!(in_be32(&regs->ir) & GPIO_MASK(gpio));
 }
 
 static inline void
-__ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
+__ppc44x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 {
-	struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
-	struct ppc4xx_gpio __iomem *regs = chip->regs;
+	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
+	struct ppc44x_gpio __iomem *regs = chip->regs;
 
 	if (val)
 		setbits32(&regs->or, GPIO_MASK(gpio));
@@ -76,14 +76,14 @@ __ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 		clrbits32(&regs->or, GPIO_MASK(gpio));
 }
 
-static int ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
+static int ppc44x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 {
-	struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
+	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
 	unsigned long flags;
 
 	spin_lock_irqsave(&chip->lock, flags);
 
-	__ppc4xx_gpio_set(gc, gpio, val);
+	__ppc44x_gpio_set(gc, gpio, val);
 
 	spin_unlock_irqrestore(&chip->lock, flags);
 
@@ -92,10 +92,10 @@ static int ppc4xx_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 	return 0;
 }
 
-static int ppc4xx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
+static int ppc44x_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 {
-	struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
-	struct ppc4xx_gpio __iomem *regs = chip->regs;
+	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
+	struct ppc44x_gpio __iomem *regs = chip->regs;
 	unsigned long flags;
 
 	spin_lock_irqsave(&chip->lock, flags);
@@ -121,16 +121,16 @@ static int ppc4xx_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 }
 
 static int
-ppc4xx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
+ppc44x_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 {
-	struct ppc4xx_gpio_chip *chip = gpiochip_get_data(gc);
-	struct ppc4xx_gpio __iomem *regs = chip->regs;
+	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
+	struct ppc44x_gpio __iomem *regs = chip->regs;
 	unsigned long flags;
 
 	spin_lock_irqsave(&chip->lock, flags);
 
 	/* First set initial value */
-	__ppc4xx_gpio_set(gc, gpio, val);
+	__ppc44x_gpio_set(gc, gpio, val);
 
 	/* Disable open-drain function */
 	clrbits32(&regs->odr, GPIO_MASK(gpio));
@@ -154,11 +154,11 @@ ppc4xx_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 	return 0;
 }
 
-static int ppc4xx_gpio_probe(struct platform_device *ofdev)
+static int ppc44x_gpio_probe(struct platform_device *ofdev)
 {
 	struct device *dev = &ofdev->dev;
 	struct device_node *np = dev->of_node;
-	struct ppc4xx_gpio_chip *chip;
+	struct ppc44x_gpio_chip *chip;
 	struct gpio_chip *gc;
 
 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
@@ -172,10 +172,10 @@ static int ppc4xx_gpio_probe(struct platform_device *ofdev)
 	gc->parent = dev;
 	gc->base = -1;
 	gc->ngpio = 32;
-	gc->direction_input = ppc4xx_gpio_dir_in;
-	gc->direction_output = ppc4xx_gpio_dir_out;
-	gc->get = ppc4xx_gpio_get;
-	gc->set = ppc4xx_gpio_set;
+	gc->direction_input = ppc44x_gpio_dir_in;
+	gc->direction_output = ppc44x_gpio_dir_out;
+	gc->get = ppc44x_gpio_get;
+	gc->set = ppc44x_gpio_set;
 
 	gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np);
 	if (!gc->label)
@@ -188,24 +188,24 @@ static int ppc4xx_gpio_probe(struct platform_device *ofdev)
 	return devm_gpiochip_add_data(dev, gc, chip);
 }
 
-static const struct of_device_id ppc4xx_gpio_match[] = {
+static const struct of_device_id ppc44x_gpio_match[] = {
 	{
 		.compatible = "ibm,ppc4xx-gpio",
 	},
 	{},
 };
-MODULE_DEVICE_TABLE(of, ppc4xx_gpio_match);
+MODULE_DEVICE_TABLE(of, ppc44x_gpio_match);
 
-static struct platform_driver ppc4xx_gpio_driver = {
-	.probe		= ppc4xx_gpio_probe,
+static struct platform_driver ppc44x_gpio_driver = {
+	.probe		= ppc44x_gpio_probe,
 	.driver		= {
-		.name	= "ppc4xx-gpio",
-		.of_match_table	= ppc4xx_gpio_match,
+		.name	= "ppc44x-gpio",
+		.of_match_table	= ppc44x_gpio_match,
 	},
 };
 
-static int __init ppc4xx_gpio_init(void)
+static int __init ppc44x_gpio_init(void)
 {
-	return platform_driver_register(&ppc4xx_gpio_driver);
+	return platform_driver_register(&ppc44x_gpio_driver);
 }
-arch_initcall(ppc4xx_gpio_init);
+arch_initcall(ppc44x_gpio_init);
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 3/8 gpio: ppc44x: Use module platform driver helper for GPIO

In reply to Rosen Penev

Replace the open-coded arch initcall registration with module_platform_driver(). The initcall level changes from arch_initcall to device_initcall, which is safe since the driver no longer needs architecture-specific ordering.

Added MODULE info as a result, otherwise these warnings appear

ERROR: modpost: missing MODULE_LICENSE() in drivers/gpio/gpio-ppc4xx.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/gpio/gpio-ppc4xx.o

Assisted-by Codex:GPT-5.5
---
 drivers/gpio/gpio-ppc44x.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/gpio/gpio-ppc44x.c +4 -5
index 9b874ff8b124..99fb11cd7966 100644
@@ -204,8 +204,7 @@ static struct platform_driver ppc44x_gpio_driver = {
 	},
 };
 
-static int __init ppc44x_gpio_init(void)
-{
-	return platform_driver_register(&ppc44x_gpio_driver);
-}
-arch_initcall(ppc44x_gpio_init);
+MODULE_DESCRIPTION("PPC44x gpio driver");
+MODULE_LICENSE("GPL");
+
+module_platform_driver(ppc44x_gpio_driver);
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 4/8 gpio: ppc44x: Use platform resource helper for GPIO MMIO

In reply to Rosen Penev

Map the PPC44x GPIO register block through the platform device resource instead of reparsing the firmware node directly.

The GPIO node now probes as a platform device, so use the platform helper to keep resource handling aligned with the converted driver model and to report mapping failures with the platform device context.

Move ioremap up in order to avoid doing extra work in case of -EPROBE_DEFER.

Assisted-by Codex:GPT-5.5
---
 drivers/gpio/gpio-ppc44x.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpio/gpio-ppc44x.c +7 -4
index 99fb11cd7966..5db5217c0225 100644
@@ -158,13 +158,20 @@ static int ppc44x_gpio_probe(struct platform_device *ofdev)
 {
 	struct device *dev = &ofdev->dev;
 	struct device_node *np = dev->of_node;
+	struct ppc44x_gpio __iomem *regs;
 	struct ppc44x_gpio_chip *chip;
 	struct gpio_chip *gc;
 
+	regs = devm_platform_ioremap_resource(ofdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
+
 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
 	if (!chip)
 		return -ENOMEM;
 
+	chip->regs = regs;
+
 	spin_lock_init(&chip->lock);
 
 	gc = &chip->gc;
@@ -181,10 +188,6 @@ static int ppc44x_gpio_probe(struct platform_device *ofdev)
 	if (!gc->label)
 		return -ENOMEM;
 
-	chip->regs = devm_of_iomap(dev, np, 0, NULL);
-	if (IS_ERR(chip->regs))
-		return PTR_ERR(chip->regs);
-
 	return devm_gpiochip_add_data(dev, gc, chip);
 }
 
Signature · 1 line
2.55.0
#5 lore raw

PATCH v2 5/8 gpio: ppc44x: Convert GPIO to generic MMIO

In reply to Rosen Penev

Use gpio_generic_chip_init() to set up the PPC44x GPIO chip instead of open-coding the basic get, set, locking and state handling.

Keep the PPC44x-specific direction callbacks because they still need to program ODR and the OSR/TSR registers around the generic data and direction registers.

Assisted-by Codex:GPT-5.5
---
 drivers/gpio/gpio-ppc44x.c | 75 +++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 46 deletions(-)
drivers/gpio/gpio-ppc44x.c +29 -46
index 5db5217c0225..d9048452162a 100644
@@ -11,10 +11,9 @@
 
 #include <linux/kernel.h>
 #include <linux/init.h>
-#include <linux/spinlock.h>
 #include <linux/io.h>
 #include <linux/of.h>
-#include <linux/gpio/driver.h>
+#include <linux/gpio/generic.h>
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <linux/platform_device.h>
@@ -45,9 +44,8 @@ struct ppc44x_gpio {
 };
 
 struct ppc44x_gpio_chip {
-	struct gpio_chip gc;
+	struct gpio_generic_chip chip;
 	void __iomem *regs;
-	spinlock_t lock;
 };
 
 /*
@@ -56,55 +54,34 @@ struct ppc44x_gpio_chip {
  * There are a maximum of 32 gpios in each gpio controller.
  */
 
-static int ppc44x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
-{
-	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
-	struct ppc44x_gpio __iomem *regs = chip->regs;
-
-	return !!(in_be32(&regs->ir) & GPIO_MASK(gpio));
-}
-
 static inline void
 __ppc44x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 {
 	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
-	struct ppc44x_gpio __iomem *regs = chip->regs;
+	struct gpio_generic_chip *gen_gc = &chip->chip;
 
 	if (val)
-		setbits32(&regs->or, GPIO_MASK(gpio));
+		gen_gc->sdata |= GPIO_MASK(gpio);
 	else
-		clrbits32(&regs->or, GPIO_MASK(gpio));
-}
-
-static int ppc44x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
-{
-	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
-	unsigned long flags;
+		gen_gc->sdata &= ~GPIO_MASK(gpio);
 
-	spin_lock_irqsave(&chip->lock, flags);
-
-	__ppc44x_gpio_set(gc, gpio, val);
-
-	spin_unlock_irqrestore(&chip->lock, flags);
-
-	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
-
-	return 0;
+	gpio_generic_write_reg(gen_gc, gen_gc->reg_set, gen_gc->sdata);
 }
 
 static int ppc44x_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 {
 	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
+	struct gpio_generic_chip *gen_gc = &chip->chip;
 	struct ppc44x_gpio __iomem *regs = chip->regs;
-	unsigned long flags;
 
-	spin_lock_irqsave(&chip->lock, flags);
+	guard(gpio_generic_lock_irqsave)(gen_gc);
 
 	/* Disable open-drain function */
 	clrbits32(&regs->odr, GPIO_MASK(gpio));
 
 	/* Float the pin */
 	clrbits32(&regs->tcr, GPIO_MASK(gpio));
+	gen_gc->sdir &= ~GPIO_MASK(gpio);
 
 	/* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
 	if (gpio < 16) {
@@ -115,8 +92,6 @@ static int ppc44x_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 		clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
 	}
 
-	spin_unlock_irqrestore(&chip->lock, flags);
-
 	return 0;
 }
 
@@ -124,10 +99,10 @@ static int
 ppc44x_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 {
 	struct ppc44x_gpio_chip *chip = gpiochip_get_data(gc);
+	struct gpio_generic_chip *gen_gc = &chip->chip;
 	struct ppc44x_gpio __iomem *regs = chip->regs;
-	unsigned long flags;
 
-	spin_lock_irqsave(&chip->lock, flags);
+	guard(gpio_generic_lock_irqsave)(gen_gc);
 
 	/* First set initial value */
 	__ppc44x_gpio_set(gc, gpio, val);
@@ -137,6 +112,7 @@ ppc44x_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 
 	/* Drive the pin */
 	setbits32(&regs->tcr, GPIO_MASK(gpio));
+	gen_gc->sdir |= GPIO_MASK(gpio);
 
 	/* Bits 0-15 use TSRL, bits 16-31 use TSRH */
 	if (gpio < 16) {
@@ -147,8 +123,6 @@ ppc44x_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 		clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
 	}
 
-	spin_unlock_irqrestore(&chip->lock, flags);
-
 	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
 
 	return 0;
@@ -160,7 +134,9 @@ static int ppc44x_gpio_probe(struct platform_device *ofdev)
 	struct device_node *np = dev->of_node;
 	struct ppc44x_gpio __iomem *regs;
 	struct ppc44x_gpio_chip *chip;
+	struct gpio_generic_chip_config config;
 	struct gpio_chip *gc;
+	int ret;
 
 	regs = devm_platform_ioremap_resource(ofdev, 0);
 	if (IS_ERR(regs))
@@ -172,17 +148,24 @@ static int ppc44x_gpio_probe(struct platform_device *ofdev)
 
 	chip->regs = regs;
 
-	spin_lock_init(&chip->lock);
-
-	gc = &chip->gc;
-
+	config = (struct gpio_generic_chip_config) {
+		.dev = dev,
+		.sz = 4,
+		.dat = &regs->ir,
+		.set = &regs->or,
+		.dirout = &regs->tcr,
+		.flags = GPIO_GENERIC_BIG_ENDIAN |
+			 GPIO_GENERIC_BIG_ENDIAN_BYTE_ORDER,
+	};
+
+	ret = gpio_generic_chip_init(&chip->chip, &config);
+	if (ret)
+		return ret;
+
+	gc = &chip->chip.gc;
 	gc->parent = dev;
-	gc->base = -1;
-	gc->ngpio = 32;
 	gc->direction_input = ppc44x_gpio_dir_in;
 	gc->direction_output = ppc44x_gpio_dir_out;
-	gc->get = ppc44x_gpio_get;
-	gc->set = ppc44x_gpio_set;
 
 	gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np);
 	if (!gc->label)
Signature · 1 line
2.55.0
#6 lore raw

PATCH v2 6/8 gpio: ppc44x: drop PPC-specific IO helpers

In reply to Rosen Penev

Replace PPC-specific clrbits32()/setbits32() with local helpers using ioread32be()/iowrite32be() which are equivalent on PPC since commit 894fa235eb4c ("powerpc: inline iomap accessors").

Add COMPILE_TEST as a result to increase compile coverage.

Assisted-by opencode:big-pickle
---
 drivers/gpio/Kconfig       |  2 +-
 drivers/gpio/gpio-ppc44x.c | 40 ++++++++++++++++++++++++++------------
 2 files changed, 29 insertions(+), 13 deletions(-)
drivers/gpio/Kconfig +1 -1
index 15fd5874f4ea..0e021af4e81e 100644
@@ -595,7 +595,7 @@ config GPIO_POLARFIRE_SOC
 
 config GPIO_PPC44X
 	tristate "PPC44x GPIO support"
-	depends on 44x
+	depends on 44x || COMPILE_TEST
 	select GPIO_GENERIC
 	help
 	  Enable gpiolib support for ppc440 based boards.
drivers/gpio/gpio-ppc44x.c +28 -12
index d9048452162a..fd543fbb959a 100644
@@ -48,6 +48,22 @@ struct ppc44x_gpio_chip {
 	void __iomem *regs;
 };
 
+static inline void ppc44x_clrbits32(void __iomem *addr, u32 mask)
+{
+	u32 val = ioread32be(addr);
+
+	val &= ~mask;
+	iowrite32be(val, addr);
+}
+
+static inline void ppc44x_setbits32(void __iomem *addr, u32 mask)
+{
+	u32 val = ioread32be(addr);
+
+	val |= mask;
+	iowrite32be(val, addr);
+}
+
 /*
  * GPIO LIB API implementation for GPIOs
  *
@@ -77,19 +93,19 @@ static int ppc44x_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
 	guard(gpio_generic_lock_irqsave)(gen_gc);
 
 	/* Disable open-drain function */
-	clrbits32(&regs->odr, GPIO_MASK(gpio));
+	ppc44x_clrbits32(&regs->odr, GPIO_MASK(gpio));
 
 	/* Float the pin */
-	clrbits32(&regs->tcr, GPIO_MASK(gpio));
+	ppc44x_clrbits32(&regs->tcr, GPIO_MASK(gpio));
 	gen_gc->sdir &= ~GPIO_MASK(gpio);
 
 	/* Bits 0-15 use TSRL/OSRL, bits 16-31 use TSRH/OSRH */
 	if (gpio < 16) {
-		clrbits32(&regs->osrl, GPIO_MASK2(gpio));
-		clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->osrl, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
 	} else {
-		clrbits32(&regs->osrh, GPIO_MASK2(gpio));
-		clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->osrh, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
 	}
 
 	return 0;
@@ -108,19 +124,19 @@ ppc44x_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 	__ppc44x_gpio_set(gc, gpio, val);
 
 	/* Disable open-drain function */
-	clrbits32(&regs->odr, GPIO_MASK(gpio));
+	ppc44x_clrbits32(&regs->odr, GPIO_MASK(gpio));
 
 	/* Drive the pin */
-	setbits32(&regs->tcr, GPIO_MASK(gpio));
+	ppc44x_setbits32(&regs->tcr, GPIO_MASK(gpio));
 	gen_gc->sdir |= GPIO_MASK(gpio);
 
 	/* Bits 0-15 use TSRL, bits 16-31 use TSRH */
 	if (gpio < 16) {
-		clrbits32(&regs->osrl, GPIO_MASK2(gpio));
-		clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->osrl, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->tsrl, GPIO_MASK2(gpio));
 	} else {
-		clrbits32(&regs->osrh, GPIO_MASK2(gpio));
-		clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->osrh, GPIO_MASK2(gpio));
+		ppc44x_clrbits32(&regs->tsrh, GPIO_MASK2(gpio));
 	}
 
 	pr_debug("%s: gpio: %d val: %d\n", __func__, gpio, val);
Signature · 1 line
2.55.0
#7 lore raw

PATCH v2 7/8 gpio: ppc44x: fix undefined behavior in GPIO_MASK2 macro

In reply to Rosen Penev

Shifting a 32-bit unsigned integer by 32 or more places is undefined behavior in C. GPIO_MASK2 computes its shift amount as (gpio) * 2, and for pins 16-31 in the OSRH/TSRH bank this yields shifts of 32-62. While this happens to work on PowerPC because slw masks the shift count to the low 5 bits, compilers performing value-range propagation may assume the else branch is unreachable and optimize it away, or may evaluate the shift as zero on other architectures via COMPILE_TEST.

Mask gpio to the 16-pin bank index so the shift stays within [0, 30]. The registers are banked (OSRL/TSRL for gpio 0-15, OSRH/TSRH for gpio 16-31) with an identical 2-bit-per-pin layout from MSB to LSB, so masking to the within-bank index preserves the intended behavior.

Assisted-by opencode:big-pickle
---
 drivers/gpio/gpio-ppc44x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-ppc44x.c +1 -1
index fd543fbb959a..9fdc84e922f4 100644
@@ -19,7 +19,7 @@
 #include <linux/platform_device.h>
 
 #define GPIO_MASK(gpio)		(0x80000000 >> (gpio))
-#define GPIO_MASK2(gpio)	(0xc0000000 >> ((gpio) * 2))
+#define GPIO_MASK2(gpio)	(0xc0000000 >> (((gpio) % 16) * 2))
 
 /* Physical GPIO register layout */
 struct ppc44x_gpio {
Signature · 1 line
2.55.0
#8 lore raw

PATCH v2 8/8 gpio: ppc44x: use dev_name() for chip label

In reply to Rosen Penev

Replace devm_kasprintf() with dev_name() for the chip label. dev_name() returns a stable pointer to the device name, so the separate allocation and -ENOMEM check can be dropped.

Using dev_name() seems to be common for GPIO labels.

---
 drivers/gpio/gpio-ppc44x.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/gpio/gpio-ppc44x.c +1 -5
index 9fdc84e922f4..189667e0e1ba 100644
@@ -147,7 +147,6 @@ ppc44x_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 static int ppc44x_gpio_probe(struct platform_device *ofdev)
 {
 	struct device *dev = &ofdev->dev;
-	struct device_node *np = dev->of_node;
 	struct ppc44x_gpio __iomem *regs;
 	struct ppc44x_gpio_chip *chip;
 	struct gpio_generic_chip_config config;
@@ -179,14 +178,11 @@ static int ppc44x_gpio_probe(struct platform_device *ofdev)
 		return ret;
 
 	gc = &chip->chip.gc;
+	gc->label = dev_name(dev);
 	gc->parent = dev;
 	gc->direction_input = ppc44x_gpio_dir_in;
 	gc->direction_output = ppc44x_gpio_dir_out;
 
-	gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np);
-	if (!gc->label)
-		return -ENOMEM;
-
 	return devm_gpiochip_add_data(dev, gc, chip);
 }
 
Signature · 1 line
2.55.0
maintainer Reviewed-by on the v2 series
#9 lore raw
In reply to Rosen Penev
Quoted 4 lines · Rosen Penev

This series moves the ppc4xx GPIO driver from arch/powerpc to drivers/gpio, converts it to be a proper platform driver using generic MMIO helpers, drops architecture-specific accessors, and prepares it for module build and COMPILE_TEST.

Excellent patches!

Yours,
Linus Walleij

#10 lore raw
Quoted 8 lines · Linus Walleij
Quoted 4 lines · Rosen Penev

This series moves the ppc4xx GPIO driver from arch/powerpc to drivers/gpio, converts it to be a proper platform driver using generic MMIO helpers, drops architecture-specific accessors, and prepares it for module build and COMPILE_TEST.

Excellent patches!

I will take this via ppc tree.

Maddy

Quoted 4 lines

Yours,
Linus Walleij

maintainer Applied
#11 lore raw
In reply to Rosen Penev
Quoted 8 lines · Rosen Penev

This series moves the ppc4xx GPIO driver from arch/powerpc to drivers/gpio, converts it to be a proper platform driver using generic MMIO helpers, drops architecture-specific accessors, and prepares it for module build and COMPILE_TEST.

v2: use guard(), reorganize a bit for clarity.

[...]

Applied to powerpc/next.

[1/8] gpio: move ppc4xx gpio driver from arch/powerpc to drivers/gpio

      https://git.kernel.org/powerpc/c/66da1ae19672a295507b0c66cd74231e5bf482a9
[2/8] gpio: ppc44x: update all 4xx to 44x
      https://git.kernel.org/powerpc/c/f9476e237ab351b840f99aadc865c0fc913b65f9
[3/8] gpio: ppc44x: Use module platform driver helper for GPIO
      https://git.kernel.org/powerpc/c/f17b0ef8593d2c9f38197c396146b63e918e46fc
[4/8] gpio: ppc44x: Use platform resource helper for GPIO MMIO
      https://git.kernel.org/powerpc/c/8ab2d98f635f77132c9fca4e3983c53729ef52cf
[5/8] gpio: ppc44x: Convert GPIO to generic MMIO
      https://git.kernel.org/powerpc/c/714d445a1c8eeefc3167a0e1a6dc766cbc162e40
[6/8] gpio: ppc44x: drop PPC-specific IO helpers
      https://git.kernel.org/powerpc/c/595f5f25a96adb4ffec89e973756161c30753276
[7/8] gpio: ppc44x: fix undefined behavior in GPIO_MASK2 macro
      https://git.kernel.org/powerpc/c/9c4d539d112fb32355a403513023f08bd17360bc
[8/8] gpio: ppc44x: use dev_name() for chip label
      https://git.kernel.org/powerpc/c/98a39710fc8d9c70fcf9bacbb0cc802b0c1a0bbe

cheers