Skip to content

Commit cf5dec8

Browse files
tq-steinaBartosz Golaszewski
authored andcommitted
gpio: Add gpio delay driver
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> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
1 parent c7239a3 commit cf5dec8

3 files changed

Lines changed: 173 additions & 0 deletions

File tree

drivers/gpio/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,14 @@ config GPIO_AGGREGATOR
17331733
industrial control context, to be operated from userspace using
17341734
the GPIO chardev interface.
17351735

1736+
config GPIO_DELAY
1737+
tristate "GPIO delay"
1738+
help
1739+
Say yes here to enable the GPIO delay, which provides a way to
1740+
configure platform specific delays for GPIO ramp-up or ramp-down
1741+
delays. This can serve the following purposes:
1742+
- Open-drain output using an RC filter
1743+
17361744
config GPIO_LATCH
17371745
tristate "GPIO latch driver"
17381746
help

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ obj-$(CONFIG_GPIO_DA9052) += gpio-da9052.o
5252
obj-$(CONFIG_GPIO_DA9055) += gpio-da9055.o
5353
obj-$(CONFIG_GPIO_DAVINCI) += gpio-davinci.o
5454
obj-$(CONFIG_GPIO_DLN2) += gpio-dln2.o
55+
obj-$(CONFIG_GPIO_DELAY) += gpio-delay.o
5556
obj-$(CONFIG_GPIO_DWAPB) += gpio-dwapb.o
5657
obj-$(CONFIG_GPIO_EIC_SPRD) += gpio-eic-sprd.o
5758
obj-$(CONFIG_GPIO_ELKHARTLAKE) += gpio-elkhartlake.o

drivers/gpio/gpio-delay.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright 2022 TQ-Systems GmbH
4+
* Author: Alexander Stein <linux@ew.tq-group.com>
5+
*/
6+
7+
#include <linux/err.h>
8+
#include <linux/gpio/consumer.h>
9+
#include <linux/gpio/driver.h>
10+
#include <linux/module.h>
11+
#include <linux/mod_devicetable.h>
12+
#include <linux/platform_device.h>
13+
#include <linux/delay.h>
14+
15+
#include "gpiolib.h"
16+
17+
struct gpio_delay_timing {
18+
unsigned long ramp_up_delay_us;
19+
unsigned long ramp_down_delay_us;
20+
};
21+
22+
struct gpio_delay_priv {
23+
struct gpio_chip gc;
24+
struct gpio_descs *input_gpio;
25+
struct gpio_delay_timing *delay_timings;
26+
};
27+
28+
static int gpio_delay_get_direction(struct gpio_chip *gc, unsigned int offset)
29+
{
30+
return GPIO_LINE_DIRECTION_OUT;
31+
}
32+
33+
static void gpio_delay_set(struct gpio_chip *gc, unsigned int offset, int val)
34+
{
35+
struct gpio_delay_priv *priv = gpiochip_get_data(gc);
36+
struct gpio_desc *gpio_desc = priv->input_gpio->desc[offset];
37+
const struct gpio_delay_timing *delay_timings;
38+
bool ramp_up;
39+
40+
gpiod_set_value(gpio_desc, val);
41+
42+
delay_timings = &priv->delay_timings[offset];
43+
ramp_up = (!gpiod_is_active_low(gpio_desc) && val) ||
44+
(gpiod_is_active_low(gpio_desc) && !val);
45+
46+
if (ramp_up && delay_timings->ramp_up_delay_us)
47+
udelay(delay_timings->ramp_up_delay_us);
48+
if (!ramp_up && delay_timings->ramp_down_delay_us)
49+
udelay(delay_timings->ramp_down_delay_us);
50+
}
51+
52+
static void gpio_delay_set_can_sleep(struct gpio_chip *gc, unsigned int offset, int val)
53+
{
54+
struct gpio_delay_priv *priv = gpiochip_get_data(gc);
55+
struct gpio_desc *gpio_desc = priv->input_gpio->desc[offset];
56+
const struct gpio_delay_timing *delay_timings;
57+
bool ramp_up;
58+
59+
gpiod_set_value_cansleep(gpio_desc, val);
60+
61+
delay_timings = &priv->delay_timings[offset];
62+
ramp_up = (!gpiod_is_active_low(gpio_desc) && val) ||
63+
(gpiod_is_active_low(gpio_desc) && !val);
64+
65+
if (ramp_up && delay_timings->ramp_up_delay_us)
66+
fsleep(delay_timings->ramp_up_delay_us);
67+
if (!ramp_up && delay_timings->ramp_down_delay_us)
68+
fsleep(delay_timings->ramp_down_delay_us);
69+
}
70+
71+
static int gpio_delay_of_xlate(struct gpio_chip *gc,
72+
const struct of_phandle_args *gpiospec,
73+
u32 *flags)
74+
{
75+
struct gpio_delay_priv *priv = gpiochip_get_data(gc);
76+
struct gpio_delay_timing *timings;
77+
u32 line;
78+
79+
if (gpiospec->args_count != gc->of_gpio_n_cells)
80+
return -EINVAL;
81+
82+
line = gpiospec->args[0];
83+
if (line >= gc->ngpio)
84+
return -EINVAL;
85+
86+
timings = &priv->delay_timings[line];
87+
timings->ramp_up_delay_us = gpiospec->args[1];
88+
timings->ramp_down_delay_us = gpiospec->args[2];
89+
90+
return line;
91+
}
92+
93+
static bool gpio_delay_can_sleep(const struct gpio_delay_priv *priv)
94+
{
95+
int i;
96+
97+
for (i = 0; i < priv->input_gpio->ndescs; i++)
98+
if (gpiod_cansleep(priv->input_gpio->desc[i]))
99+
return true;
100+
101+
return false;
102+
}
103+
104+
static int gpio_delay_probe(struct platform_device *pdev)
105+
{
106+
struct gpio_delay_priv *priv;
107+
108+
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
109+
if (!priv)
110+
return -ENOMEM;
111+
112+
priv->input_gpio = devm_gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
113+
if (IS_ERR(priv->input_gpio))
114+
return dev_err_probe(&pdev->dev, PTR_ERR(priv->input_gpio),
115+
"Failed to get input-gpios");
116+
117+
priv->delay_timings = devm_kcalloc(&pdev->dev,
118+
priv->input_gpio->ndescs,
119+
sizeof(*priv->delay_timings),
120+
GFP_KERNEL);
121+
if (!priv->delay_timings)
122+
return -ENOMEM;
123+
124+
if (gpio_delay_can_sleep(priv)) {
125+
priv->gc.can_sleep = true;
126+
priv->gc.set = gpio_delay_set_can_sleep;
127+
} else {
128+
priv->gc.can_sleep = false;
129+
priv->gc.set = gpio_delay_set;
130+
}
131+
132+
priv->gc.get_direction = gpio_delay_get_direction;
133+
priv->gc.of_xlate = gpio_delay_of_xlate;
134+
priv->gc.of_gpio_n_cells = 3;
135+
priv->gc.ngpio = priv->input_gpio->ndescs;
136+
priv->gc.owner = THIS_MODULE;
137+
priv->gc.base = -1;
138+
priv->gc.parent = &pdev->dev;
139+
140+
platform_set_drvdata(pdev, priv);
141+
142+
return devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
143+
}
144+
145+
static const struct of_device_id gpio_delay_ids[] = {
146+
{
147+
.compatible = "gpio-delay",
148+
},
149+
{ /* sentinel */ }
150+
};
151+
MODULE_DEVICE_TABLE(of, gpio_delay_ids);
152+
153+
static struct platform_driver gpio_delay_driver = {
154+
.driver = {
155+
.name = "gpio-delay",
156+
.of_match_table = gpio_delay_ids,
157+
},
158+
.probe = gpio_delay_probe,
159+
};
160+
module_platform_driver(gpio_delay_driver);
161+
162+
MODULE_LICENSE("GPL");
163+
MODULE_AUTHOR("Alexander Stein <alexander.stein@ew.tq-group.com>");
164+
MODULE_DESCRIPTION("GPIO delay driver");

0 commit comments

Comments
 (0)