Skip to contentSkip to conversation

gpio: aggregator: Incorporate gpio-delay functionality

PATCH Superseded Andy Shevchenko 4 participants 16 messages Jun 14, 2023 – Jun 15, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover gpio: aggregator: Incorporate gpio-delay functionality

The newly appeared gpio-delay module enables external signal delay lines that may be connected to the GPIOs. But at the same time it copies the GPIO forwarder functionality. Besides that the approach does not scale. If we would have another external component, we would need yet another driver. That's why I think, and seems others support me, better to enable such a functionality inside GPIO aggregator driver.

Patch 1 is a cleanup that may be applied independently on the decision about the rest.

Please, test and comment!

In v2:
- split as a series
- covered CONFIG_OF_GPIO=n case
- removed the gpio-delay
- moved gpio-delay Kconfig help to the comment in the code

Not in v2:
- updated DT description to note about long timeouts in atomic mode
- sysfs hiding for gpio-delay functionality
- used msleep(): the documentation is _against_ this

Andy Shevchenko (4):
gpio: aggregator: Remove CONFIG_OF and of_match_ptr() protections
gpio: aggregator: Support delay for setting up individual GPIOs
gpio: aggregator: Set up a parser of delay line parameters
gpio: delay: Remove duplicative functionality

 drivers/gpio/Kconfig           |   9 --
 drivers/gpio/Makefile          |   1 -
 drivers/gpio/gpio-aggregator.c | 109 ++++++++++++++++++++--
 drivers/gpio/gpio-delay.c      | 164 ---------------------------------
 4 files changed, 103 insertions(+), 180 deletions(-)
 delete mode 100644 drivers/gpio/gpio-delay.c
Signature · 1 line
2.40.0.1.gaa8946217a0b
#1 lore raw

PATCH v2 1/4 gpio: aggregator: Remove CONFIG_OF and of_match_ptr() protections

They stop the driver being used with ACPI PRP0001 and are something I want to avoid being cut and paste into new drivers. Also include mod_devicetable.h as we struct of_device_id is defined in there.

---
 drivers/gpio/gpio-aggregator.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpio/gpio-aggregator.c +3 -4
index 20a686f12df7..1aa7455672d3 100644
@@ -12,6 +12,7 @@
 #include <linux/ctype.h>
 #include <linux/idr.h>
 #include <linux/kernel.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/overflow.h>
@@ -500,23 +501,21 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
 	return 0;
 }
 
-#ifdef CONFIG_OF
 static const struct of_device_id gpio_aggregator_dt_ids[] = {
 	/*
 	 * Add GPIO-operated devices controlled from userspace below,
-	 * or use "driver_override" in sysfs
+	 * or use "driver_override" in sysfs.
 	 */
 	{}
 };
 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
-#endif
 
 static struct platform_driver gpio_aggregator_driver = {
 	.probe = gpio_aggregator_probe,
 	.driver = {
 		.name = DRV_NAME,
 		.groups = gpio_aggregator_groups,
-		.of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
+		.of_match_table = gpio_aggregator_dt_ids,
 	},
 };
 
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] gpio: aggregator: Remove CONFIG_OF and of_match_ptr() protections

Quoted 5 lines · Andy Shevchenko

They stop the driver being used with ACPI PRP0001 and are something I want to avoid being cut and paste into new drivers. Also include mod_devicetable.h as we struct of_device_id is defined in there.

Yours,
Linus Walleij

Reviewed-by on v2 1/4
#3 lore raw

Re: [PATCH v2 1/4] gpio: aggregator: Remove CONFIG_OF and of_match_ptr() protections

In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

They stop the driver being used with ACPI PRP0001 and are something I want to avoid being cut and paste into new drivers. Also include mod_devicetable.h as we struct of_device_id is defined in there.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw

PATCH v2 2/4 gpio: aggregator: Support delay for setting up individual GPIOs

In reply to Andy Shevchenko

In some cases the GPIO may require an additional delay after setting its value. Add support for that into the GPIO forwarder code.

This will be fully enabled for use in the following changes.

---
 drivers/gpio/gpio-aggregator.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/gpio/gpio-aggregator.c +32 -0
index 1aa7455672d3..a74a8d86caf3 100644
@@ -10,6 +10,7 @@
 #include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/ctype.h>
+#include <linux/delay.h>
 #include <linux/idr.h>
 #include <linux/kernel.h>
 #include <linux/mod_devicetable.h>
@@ -240,6 +241,11 @@ static void __exit gpio_aggregator_remove_all(void)
  *  GPIO Forwarder
  */
 
+struct gpiochip_fwd_timing {
+	u32 ramp_up_us;
+	u32 ramp_down_us;
+};
+
 struct gpiochip_fwd {
 	struct gpio_chip chip;
 	struct gpio_desc **descs;
@@ -247,6 +253,7 @@ struct gpiochip_fwd {
 		struct mutex mlock;	/* protects tmp[] if can_sleep */
 		spinlock_t slock;	/* protects tmp[] if !can_sleep */
 	};
+	struct gpiochip_fwd_timing *delay_timings;
 	unsigned long tmp[];		/* values and descs for multiple ops */
 };
 
@@ -331,6 +338,28 @@ static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
 	return error;
 }
 
+static void gpio_fwd_delay(struct gpio_chip *chip, unsigned int offset, int value)
+{
+	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
+	const struct gpiochip_fwd_timing *delay_timings;
+	struct gpio_desc *desc = fwd->descs[offset];
+	bool is_active_low = gpiod_is_active_low(desc);
+	u32 delay_us;
+
+	delay_timings = &fwd->delay_timings[offset];
+	if ((!is_active_low && value) || (is_active_low && !value))
+		delay_us = delay_timings->ramp_up_us;
+	else
+		delay_us = delay_timings->ramp_down_us;
+	if (!delay_us)
+		return;
+
+	if (chip->can_sleep)
+		fsleep(delay_us);
+	else
+		udelay(delay_us);
+}
+
 static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
 {
 	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
@@ -339,6 +368,9 @@ static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
 		gpiod_set_value_cansleep(fwd->descs[offset], value);
 	else
 		gpiod_set_value(fwd->descs[offset], value);
+
+	if (fwd->delay_timings)
+		gpio_fwd_delay(chip, offset, value);
 }
 
 static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v2 2/4
#5 lore raw

Re: [PATCH v2 2/4] gpio: aggregator: Support delay for setting up individual GPIOs

Quoted 6 lines · Andy Shevchenko

In some cases the GPIO may require an additional delay after setting its value. Add support for that into the GPIO forwarder code.

This will be fully enabled for use in the following changes.

Yours,
Linus Walleij

Reviewed-by on v2 2/4
#6 lore raw

Re: [PATCH v2 2/4] gpio: aggregator: Support delay for setting up individual GPIOs

In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

In some cases the GPIO may require an additional delay after setting its value. Add support for that into the GPIO forwarder code.

This will be fully enabled for use in the following changes.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#7 lore raw

PATCH v2 3/4 gpio: aggregator: Set up a parser of delay line parameters

In reply to Andy Shevchenko

The aggregator mode can also handle properties of the platform, that do not belong to the GPIO controller itself. One of such a property is a signal delay line. Set up a parser to support it.

---
 drivers/gpio/gpio-aggregator.c | 70 +++++++++++++++++++++++++++++++++-
 1 file changed, 68 insertions(+), 2 deletions(-)
drivers/gpio/gpio-aggregator.c +68 -2
index a74a8d86caf3..ed11aa56bc51 100644
@@ -18,6 +18,7 @@
 #include <linux/mutex.h>
 #include <linux/overflow.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/string.h>
@@ -423,6 +424,51 @@ static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
 	return gpiod_to_irq(fwd->descs[offset]);
 }
 
+#ifdef CONFIG_OF_GPIO
+static int gpiochip_fwd_delay_of_xlate(struct gpio_chip *chip,
+				       const struct of_phandle_args *gpiospec,
+				       u32 *flags)
+{
+	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
+	struct gpiochip_fwd_timing *timings;
+	u32 line;
+
+	if (gpiospec->args_count != chip->of_gpio_n_cells)
+		return -EINVAL;
+
+	line = gpiospec->args[0];
+	if (line >= chip->ngpio)
+		return -EINVAL;
+
+	timings = &fwd->delay_timings[line];
+	timings->ramp_up_us = gpiospec->args[1];
+	timings->ramp_down_us = gpiospec->args[2];
+
+	return line;
+}
+
+static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *chip,
+					 struct gpiochip_fwd *fwd)
+{
+	fwd->delay_timings = devm_kcalloc(dev, chip->ngpio,
+					  sizeof(*fwd->delay_timings),
+					  GFP_KERNEL);
+	if (!fwd->delay_timings)
+		return -ENOMEM;
+
+	chip->of_xlate = gpiochip_fwd_delay_of_xlate;
+	chip->of_gpio_n_cells = 3;
+
+	return 0;
+}
+#else
+static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *chip,
+					 struct gpiochip_fwd *fwd)
+{
+	return 0;
+}
+#endif	/* !CONFIG_OF_GPIO */
+
 /**
  * gpiochip_fwd_create() - Create a new GPIO forwarder
  * @dev: Parent device pointer
@@ -430,6 +476,7 @@ static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
  * @descs: Array containing the GPIO descriptors to forward to.
  *         This array must contain @ngpios entries, and must not be deallocated
  *         before the forwarder has been destroyed again.
+ * @delay_line: True if the pins have an external delay line.
  *
  * This function creates a new gpiochip, which forwards all GPIO operations to
  * the passed GPIO descriptors.
@@ -439,7 +486,8 @@ static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
  */
 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
 						unsigned int ngpios,
-						struct gpio_desc *descs[])
+						struct gpio_desc *descs[],
+						bool delay_line)
 {
 	const char *label = dev_name(dev);
 	struct gpiochip_fwd *fwd;
@@ -492,6 +540,12 @@ static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
 	else
 		spin_lock_init(&fwd->slock);
 
+	if (delay_line) {
+		error = gpiochip_fwd_setup_delay_line(dev, chip, fwd);
+		if (error)
+			return ERR_PTR(error);
+	}
+
 	error = devm_gpiochip_add_data(dev, chip, fwd);
 	if (error)
 		return ERR_PTR(error);
@@ -509,6 +563,7 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct gpio_desc **descs;
 	struct gpiochip_fwd *fwd;
+	bool delay_line;
 	int i, n;
 
 	n = gpiod_count(dev, NULL);
@@ -525,7 +580,9 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
 			return PTR_ERR(descs[i]);
 	}
 
-	fwd = gpiochip_fwd_create(dev, n, descs);
+	delay_line = fwnode_device_is_compatible(dev_fwnode(dev), "gpio-delay");
+
+	fwd = gpiochip_fwd_create(dev, n, descs, delay_line);
 	if (IS_ERR(fwd))
 		return PTR_ERR(fwd);
 
@@ -534,6 +591,15 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
 }
 
 static const struct of_device_id gpio_aggregator_dt_ids[] = {
+	/*
+	 * The GPIO delay provides a way to configure platform specific delays
+	 * for GPIO ramp-up or ramp-down delays. This can serve the following
+	 * purposes:
+	 * - Open-drain output using an RC filter
+	 */
+	{
+		.compatible = "gpio-delay",
+	},
 	/*
 	 * Add GPIO-operated devices controlled from userspace below,
 	 * or use "driver_override" in sysfs.
Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v2 3/4
#8 lore raw

Re: [PATCH v2 3/4] gpio: aggregator: Set up a parser of delay line parameters

Quoted 5 lines · Andy Shevchenko

The aggregator mode can also handle properties of the platform, that do not belong to the GPIO controller itself. One of such a property is a signal delay line. Set up a parser to support it.

I guess this is one of those instances where we actually need some OF-specific code for parsing the special case, and other HW descriptions will need other special code. It's fine for non-trivial stuff I think.

Yours,
Linus Walleij

#9 lore raw

Re: [PATCH v2 3/4] gpio: aggregator: Set up a parser of delay line parameters

Quoted 7 lines · Linus Walleij
Quoted 4 lines · Andy Shevchenko

The aggregator mode can also handle properties of the platform, that do not belong to the GPIO controller itself. One of such a property is a signal delay line. Set up a parser to support it.

Quoted 5 lines
Quoted 1 line

I guess this is one of those instances where we actually need some OF-specific code for parsing the special case, and other HW descriptions will need other special code. It's fine for non-trivial stuff I think.

Actually if you look into IIO, the xlate there is fwnode specific nowadays and theoretically the specifics of OF can be parsed in ACPI as well. I would like to get rid of OF_GPIO completely, but let's see...

Quoted 1 line

Thank you!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 3/4
#10 lore raw

Re: [PATCH v2 3/4] gpio: aggregator: Set up a parser of delay line parameters

In reply to Andy Shevchenko

Hi Andy,

Quoted 5 lines · Andy Shevchenko

The aggregator mode can also handle properties of the platform, that do not belong to the GPIO controller itself. One of such a property is a signal delay line. Set up a parser to support it.

Thanks for your patch!

One suggestion for improvement below...

drivers/gpio/gpio-aggregator.c static int gpio_aggregator_probe(struct platform_device *pdev) · line 583

Quoted 8 lines
drivers/gpio/gpio-aggregator.c +1 -1
@@ -525,7 +580,9 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
                        return PTR_ERR(descs[i]);
        }
 
-       fwd = gpiochip_fwd_create(dev, n, descs);
+       delay_line = fwnode_device_is_compatible(dev_fwnode(dev), "gpio-delay");

Please do not use explicit checks for compatible values in .probe() methods. Instead, use device_get_match_data() to get the feature flag(s). This will also make it easier to scale to other external components later.

Quoted 7 lines
drivers/gpio/gpio-aggregator.c +2 -0
+
+       fwd = gpiochip_fwd_create(dev, n, descs, delay_line);
        if (IS_ERR(fwd))
                return PTR_ERR(fwd);
 
@@ -534,6 +591,15 @@ static int gpio_aggregator_probe(struct platform_device *pdev)
 }

drivers/gpio/gpio-aggregator.c static int gpio_aggregator_probe(struct platform_device *pdev) · line 601

Quoted 10 lines
drivers/gpio/gpio-aggregator.c +8 -0
 
 static const struct of_device_id gpio_aggregator_dt_ids[] = {
+       /*
+        * The GPIO delay provides a way to configure platform specific delays
+        * for GPIO ramp-up or ramp-down delays. This can serve the following
+        * purposes:
+        * - Open-drain output using an RC filter
+        */
+       {
+               .compatible = "gpio-delay",

.data = (void *)FWD_FEATURE_DELAY,

drivers/gpio/gpio-aggregator.c static int gpio_aggregator_probe(struct platform_device *pdev) · line 605

Quoted 4 lines
drivers/gpio/gpio-aggregator.c +1 -0
+       },
        /*
         * Add GPIO-operated devices controlled from userspace below,
         * or use "driver_override" in sysfs.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#11 lore raw

Re: [PATCH v2 3/4] gpio: aggregator: Set up a parser of delay line parameters

Quoted 11 lines · Geert Uytterhoeven
Quoted 5 lines · Andy Shevchenko

The aggregator mode can also handle properties of the platform, that do not belong to the GPIO controller itself. One of such a property is a signal delay line. Set up a parser to support it.

Thanks for your patch!

Thank you!

Quoted 1 line

One suggestion for improvement below...

Sure, for v3!

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

PATCH v2 4/4 gpio: delay: Remove duplicative functionality

In reply to Andy Shevchenko

Now that GPIO aggregator supports a delay line, drop the duplicative functionality, i.e. the entire gpio-delay driver.

---
 drivers/gpio/Kconfig      |   9 ---
 drivers/gpio/Makefile     |   1 -
 drivers/gpio/gpio-delay.c | 164 --------------------------------------
 3 files changed, 174 deletions(-)
 delete mode 100644 drivers/gpio/gpio-delay.c
drivers/gpio/Kconfig +0 -9
index 209738ef1446..abaae68c88a4 100644
@@ -1748,15 +1748,6 @@ config GPIO_AGGREGATOR
 	      industrial control context, to be operated from userspace using
 	      the GPIO chardev interface.
 
-config GPIO_DELAY
-	tristate "GPIO delay"
-	depends on OF_GPIO
-	help
-	  Say yes here to enable the GPIO delay, which provides a way to
-	  configure platform specific delays for GPIO ramp-up or ramp-down
-	  delays. This can serve the following purposes:
-	    - Open-drain output using an RC filter
-
 config GPIO_LATCH
 	tristate "GPIO latch driver"
 	help
drivers/gpio/Makefile +0 -1
index 947c9cf9aba8..7843b16f5d59 100644
@@ -52,7 +52,6 @@ obj-$(CONFIG_GPIO_DA9052)		+= gpio-da9052.o
 obj-$(CONFIG_GPIO_DA9055)		+= gpio-da9055.o
 obj-$(CONFIG_GPIO_DAVINCI)		+= gpio-davinci.o
 obj-$(CONFIG_GPIO_DLN2)			+= gpio-dln2.o
-obj-$(CONFIG_GPIO_DELAY)		+= gpio-delay.o
 obj-$(CONFIG_GPIO_DWAPB)		+= gpio-dwapb.o
 obj-$(CONFIG_GPIO_EIC_SPRD)		+= gpio-eic-sprd.o
 obj-$(CONFIG_GPIO_ELKHARTLAKE)		+= gpio-elkhartlake.o
drivers/gpio/gpio-delay.c deleted +0 -164

164 lines not on this page · raw message

Signature · 1 line
2.40.0.1.gaa8946217a0b
Reviewed-by on v2 4/4
#14 lore raw

Re: [PATCH v2 4/4] gpio: delay: Remove duplicative functionality

In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko

Now that GPIO aggregator supports a delay line, drop the duplicative functionality, i.e. the entire gpio-delay driver.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#15 lore raw
In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko

The newly appeared gpio-delay module enables external signal delay lines that may be connected to the GPIOs. But at the same time it copies the GPIO forwarder functionality. Besides that the approach does not scale. If we would have another external component, we would need yet another driver. That's why I think, and seems others support me, better to enable such a functionality inside GPIO aggregator driver.

Quoted 4 lines

Patch 1 is a cleanup that may be applied independently on the decision about the rest.

Please, test and comment!

Alexander, I have slightly changed the code, can you test this and give your formal Tested-by tag?

Signature · 2 lines
With Best Regards,
Andy Shevchenko