devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Stein <alexander.stein@ew.tq-group.com>
To: Linus Walleij <linus.walleij@linaro.org>,
	Bartosz Golaszewski <brgl@bgdev.pl>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>
Cc: Alexander Stein <alexander.stein@ew.tq-group.com>,
	linux-gpio@vger.kernel.org, devicetree@vger.kernel.org,
	Marek Vasut <marex@denx.de>,
	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Subject: [PATCH v1 2/3] gpio: Add gpio delay driver
Date: Thu,  6 Apr 2023 11:33:43 +0200	[thread overview]
Message-ID: <20230406093344.917259-3-alexander.stein@ew.tq-group.com> (raw)
In-Reply-To: <20230406093344.917259-1-alexander.stein@ew.tq-group.com>

This driver implements a GPIO enable/disable delay. It supports a list
of GPIO outputs, which ramp-up/ramp-down delay can be specified at
consumer location.
The main purpose is to address external, passive delays upon line
voltage changes.

Signed-off-by: Alexander Stein <alexander.stein@ew.tq-group.com>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
---
Changes since RFC v2:
* Removed 'input' con_id
* Added Linus' A-b

 drivers/gpio/Kconfig      |   8 ++
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-delay.c | 164 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 173 insertions(+)
 create mode 100644 drivers/gpio/gpio-delay.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 5521f060d58e..87c79d7a4aea 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -1733,6 +1733,14 @@ config GPIO_AGGREGATOR
 	      industrial control context, to be operated from userspace using
 	      the GPIO chardev interface.
 
+config GPIO_DELAY
+	tristate "GPIO delay"
+	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
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 20036af3acb1..abb6ead17d83 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -52,6 +52,7 @@ 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
diff --git a/drivers/gpio/gpio-delay.c b/drivers/gpio/gpio-delay.c
new file mode 100644
index 000000000000..b489b561b225
--- /dev/null
+++ b/drivers/gpio/gpio-delay.c
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright 2022 TQ-Systems GmbH
+ * Author: Alexander Stein <linux@ew.tq-group.com>
+ */
+
+#include <linux/err.h>
+#include <linux/gpio/consumer.h>
+#include <linux/gpio/driver.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/platform_device.h>
+#include <linux/delay.h>
+
+#include "gpiolib.h"
+
+struct gpio_delay_timing {
+	unsigned long ramp_up_delay_us;
+	unsigned long ramp_down_delay_us;
+};
+
+struct gpio_delay_priv {
+	struct gpio_chip gc;
+	struct gpio_descs *input_gpio;
+	struct gpio_delay_timing *delay_timings;
+};
+
+static int gpio_delay_get_direction(struct gpio_chip *gc, unsigned int offset)
+{
+	return GPIO_LINE_DIRECTION_OUT;
+}
+
+static void gpio_delay_set(struct gpio_chip *gc, unsigned int offset, int val)
+{
+	struct gpio_delay_priv *priv = gpiochip_get_data(gc);
+	struct gpio_desc *gpio_desc = priv->input_gpio->desc[offset];
+	const struct gpio_delay_timing *delay_timings;
+	bool ramp_up;
+
+	gpiod_set_value(gpio_desc, val);
+
+	delay_timings = &priv->delay_timings[offset];
+	ramp_up = (!gpiod_is_active_low(gpio_desc) && val) ||
+		  (gpiod_is_active_low(gpio_desc) && !val);
+
+	if (ramp_up && delay_timings->ramp_up_delay_us)
+		udelay(delay_timings->ramp_up_delay_us);
+	if (!ramp_up && delay_timings->ramp_down_delay_us)
+		udelay(delay_timings->ramp_down_delay_us);
+}
+
+static void gpio_delay_set_can_sleep(struct gpio_chip *gc, unsigned int offset, int val)
+{
+	struct gpio_delay_priv *priv = gpiochip_get_data(gc);
+	struct gpio_desc *gpio_desc = priv->input_gpio->desc[offset];
+	const struct gpio_delay_timing *delay_timings;
+	bool ramp_up;
+
+	gpiod_set_value_cansleep(gpio_desc, val);
+
+	delay_timings = &priv->delay_timings[offset];
+	ramp_up = (!gpiod_is_active_low(gpio_desc) && val) ||
+		  (gpiod_is_active_low(gpio_desc) && !val);
+
+	if (ramp_up && delay_timings->ramp_up_delay_us)
+		fsleep(delay_timings->ramp_up_delay_us);
+	if (!ramp_up && delay_timings->ramp_down_delay_us)
+		fsleep(delay_timings->ramp_down_delay_us);
+}
+
+static int gpio_delay_of_xlate(struct gpio_chip *gc,
+			       const struct of_phandle_args *gpiospec,
+			       u32 *flags)
+{
+	struct gpio_delay_priv *priv = gpiochip_get_data(gc);
+	struct gpio_delay_timing *timings;
+	u32 line;
+
+	if (gpiospec->args_count != gc->of_gpio_n_cells)
+		return -EINVAL;
+
+	line = gpiospec->args[0];
+	if (line >= gc->ngpio)
+		return -EINVAL;
+
+	timings = &priv->delay_timings[line];
+	timings->ramp_up_delay_us = gpiospec->args[1];
+	timings->ramp_down_delay_us = gpiospec->args[2];
+
+	return line;
+}
+
+static bool gpio_delay_can_sleep(const struct gpio_delay_priv *priv)
+{
+	int i;
+
+	for (i = 0; i < priv->input_gpio->ndescs; i++)
+		if (gpiod_cansleep(priv->input_gpio->desc[i]))
+			return true;
+
+	return false;
+}
+
+static int gpio_delay_probe(struct platform_device *pdev)
+{
+	struct gpio_delay_priv *priv;
+
+	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->input_gpio = devm_gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
+	if (IS_ERR(priv->input_gpio))
+		return dev_err_probe(&pdev->dev, PTR_ERR(priv->input_gpio),
+				     "Failed to get input-gpios");
+
+	priv->delay_timings = devm_kcalloc(&pdev->dev,
+					   priv->input_gpio->ndescs,
+					   sizeof(*priv->delay_timings),
+					   GFP_KERNEL);
+	if (!priv->delay_timings)
+		return -ENOMEM;
+
+	if (gpio_delay_can_sleep(priv)) {
+		priv->gc.can_sleep = true;
+		priv->gc.set = gpio_delay_set_can_sleep;
+	} else {
+		priv->gc.can_sleep = false;
+		priv->gc.set = gpio_delay_set;
+	}
+
+	priv->gc.get_direction = gpio_delay_get_direction;
+	priv->gc.of_xlate = gpio_delay_of_xlate;
+	priv->gc.of_gpio_n_cells = 3;
+	priv->gc.ngpio = priv->input_gpio->ndescs;
+	priv->gc.owner = THIS_MODULE;
+	priv->gc.base = -1;
+	priv->gc.parent = &pdev->dev;
+
+	platform_set_drvdata(pdev, priv);
+
+	return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
+}
+
+static const struct of_device_id gpio_delay_ids[] = {
+	{
+		.compatible = "gpio-delay",
+	},
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, gpio_delay_ids);
+
+static struct platform_driver gpio_delay_driver = {
+	.driver	= {
+		.name		= "gpio-delay",
+		.of_match_table	= gpio_delay_ids,
+	},
+	.probe	= gpio_delay_probe,
+};
+module_platform_driver(gpio_delay_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Alexander Stein <alexander.stein@ew.tq-group.com>");
+MODULE_DESCRIPTION("GPIO delay driver");
-- 
2.34.1


  parent reply	other threads:[~2023-04-06  9:34 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06  9:33 [PATCH v1 0/3] gpio: Add gpio-delay support Alexander Stein
2023-04-06  9:33 ` [PATCH v1 1/3] dt-bindings: gpio: Add gpio-delay binding document Alexander Stein
2023-05-31 16:37   ` Krzysztof Kozlowski
2023-06-02 16:29   ` Bartosz Golaszewski
2023-04-06  9:33 ` Alexander Stein [this message]
2023-06-02 16:31   ` [PATCH v1 2/3] gpio: Add gpio delay driver Bartosz Golaszewski
2023-04-06  9:33 ` [PATCH v1 3/3] [DNI] arm64: dts: mba8mx: Use gpio-delay for LVDS bridge Alexander Stein
2023-04-07 18:57 ` [PATCH v1 0/3] gpio: Add gpio-delay support andy.shevchenko
2023-04-11  7:19   ` Alexander Stein
2023-04-11  9:34     ` Andy Shevchenko
2023-04-14  6:37       ` Alexander Stein
2023-04-15 15:06         ` Andy Shevchenko
2023-04-16  7:42           ` Krzysztof Kozlowski
2023-04-16  9:36             ` Andy Shevchenko
2023-04-16 11:04               ` Krzysztof Kozlowski
2023-04-16 11:14                 ` Andy Shevchenko
2023-04-16 11:21                   ` Krzysztof Kozlowski
2023-04-16 11:33                     ` Andy Shevchenko
2023-04-16 11:42                       ` Krzysztof Kozlowski
2023-04-16 18:46                         ` Andy Shevchenko
2023-05-31  6:53                           ` Alexander Stein
2023-05-31 12:02                             ` Andy Shevchenko
2023-05-31 13:44                             ` Linus Walleij
2023-05-31 14:37                               ` Andy Shevchenko
2023-05-31 16:37                               ` Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230406093344.917259-3-alexander.stein@ew.tq-group.com \
    --to=alexander.stein@ew.tq-group.com \
    --cc=brgl@bgdev.pl \
    --cc=devicetree@vger.kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=marex@denx.de \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).