Skip to content

Commit cee544a

Browse files
krzkpH5
authored andcommitted
reset: gpio: Add GPIO-based reset controller
Add a simple driver to control GPIO-based resets using the reset controller API for the cases when the GPIOs are shared and reset should be coordinated. The driver is expected to be used by reset core framework for ad-hoc reset controllers. Cc: Bartosz Golaszewski <[email protected]> Cc: Chris Packham <[email protected]> Cc: Sean Anderson <[email protected]> Reviewed-by: Bartosz Golaszewski <[email protected]> Reviewed-by: Philipp Zabel <[email protected]> Signed-off-by: Krzysztof Kozlowski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Philipp Zabel <[email protected]>
1 parent 0f28982 commit cee544a

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

MAINTAINERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8911,6 +8911,11 @@ F: Documentation/i2c/muxes/i2c-mux-gpio.rst
89118911
F: drivers/i2c/muxes/i2c-mux-gpio.c
89128912
F: include/linux/platform_data/i2c-mux-gpio.h
89138913

8914+
GENERIC GPIO RESET DRIVER
8915+
M: Krzysztof Kozlowski <[email protected]>
8916+
S: Maintained
8917+
F: drivers/reset/reset-gpio.c
8918+
89148919
GENERIC HDLC (WAN) DRIVERS
89158920
M: Krzysztof Halasa <[email protected]>
89168921
S: Maintained

drivers/reset/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ config RESET_BRCMSTB_RESCAL
6666
This enables the RESCAL reset controller for SATA, PCIe0, or PCIe1 on
6767
BCM7216.
6868

69+
config RESET_GPIO
70+
tristate "GPIO reset controller"
71+
help
72+
This enables a generic reset controller for resets attached via
73+
GPIOs. Typically for OF platforms this driver expects "reset-gpios"
74+
property.
75+
76+
If compiled as module, it will be called reset-gpio.
77+
6978
config RESET_HSDK
7079
bool "Synopsys HSDK Reset Driver"
7180
depends on HAS_IOMEM

drivers/reset/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ obj-$(CONFIG_RESET_BCM6345) += reset-bcm6345.o
1111
obj-$(CONFIG_RESET_BERLIN) += reset-berlin.o
1212
obj-$(CONFIG_RESET_BRCMSTB) += reset-brcmstb.o
1313
obj-$(CONFIG_RESET_BRCMSTB_RESCAL) += reset-brcmstb-rescal.o
14+
obj-$(CONFIG_RESET_GPIO) += reset-gpio.o
1415
obj-$(CONFIG_RESET_HSDK) += reset-hsdk.o
1516
obj-$(CONFIG_RESET_IMX7) += reset-imx7.o
1617
obj-$(CONFIG_RESET_INTEL_GW) += reset-intel-gw.o

drivers/reset/reset-gpio.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/gpio/consumer.h>
4+
#include <linux/mod_devicetable.h>
5+
#include <linux/module.h>
6+
#include <linux/of.h>
7+
#include <linux/platform_device.h>
8+
#include <linux/reset-controller.h>
9+
10+
struct reset_gpio_priv {
11+
struct reset_controller_dev rc;
12+
struct gpio_desc *reset;
13+
};
14+
15+
static inline struct reset_gpio_priv
16+
*rc_to_reset_gpio(struct reset_controller_dev *rc)
17+
{
18+
return container_of(rc, struct reset_gpio_priv, rc);
19+
}
20+
21+
static int reset_gpio_assert(struct reset_controller_dev *rc, unsigned long id)
22+
{
23+
struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
24+
25+
gpiod_set_value_cansleep(priv->reset, 1);
26+
27+
return 0;
28+
}
29+
30+
static int reset_gpio_deassert(struct reset_controller_dev *rc,
31+
unsigned long id)
32+
{
33+
struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
34+
35+
gpiod_set_value_cansleep(priv->reset, 0);
36+
37+
return 0;
38+
}
39+
40+
static int reset_gpio_status(struct reset_controller_dev *rc, unsigned long id)
41+
{
42+
struct reset_gpio_priv *priv = rc_to_reset_gpio(rc);
43+
44+
return gpiod_get_value_cansleep(priv->reset);
45+
}
46+
47+
static const struct reset_control_ops reset_gpio_ops = {
48+
.assert = reset_gpio_assert,
49+
.deassert = reset_gpio_deassert,
50+
.status = reset_gpio_status,
51+
};
52+
53+
static int reset_gpio_of_xlate(struct reset_controller_dev *rcdev,
54+
const struct of_phandle_args *reset_spec)
55+
{
56+
return reset_spec->args[0];
57+
}
58+
59+
static void reset_gpio_of_node_put(void *data)
60+
{
61+
of_node_put(data);
62+
}
63+
64+
static int reset_gpio_probe(struct platform_device *pdev)
65+
{
66+
struct device *dev = &pdev->dev;
67+
struct of_phandle_args *platdata = dev_get_platdata(dev);
68+
struct reset_gpio_priv *priv;
69+
int ret;
70+
71+
if (!platdata)
72+
return -EINVAL;
73+
74+
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
75+
if (!priv)
76+
return -ENOMEM;
77+
78+
platform_set_drvdata(pdev, &priv->rc);
79+
80+
priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
81+
if (IS_ERR(priv->reset))
82+
return dev_err_probe(dev, PTR_ERR(priv->reset),
83+
"Could not get reset gpios\n");
84+
85+
priv->rc.ops = &reset_gpio_ops;
86+
priv->rc.owner = THIS_MODULE;
87+
priv->rc.dev = dev;
88+
priv->rc.of_args = platdata;
89+
ret = devm_add_action_or_reset(dev, reset_gpio_of_node_put,
90+
priv->rc.of_node);
91+
if (ret)
92+
return ret;
93+
94+
/* Cells to match GPIO specifier, but it's not really used */
95+
priv->rc.of_reset_n_cells = 2;
96+
priv->rc.of_xlate = reset_gpio_of_xlate;
97+
priv->rc.nr_resets = 1;
98+
99+
return devm_reset_controller_register(dev, &priv->rc);
100+
}
101+
102+
static const struct platform_device_id reset_gpio_ids[] = {
103+
{ .name = "reset-gpio", },
104+
{}
105+
};
106+
MODULE_DEVICE_TABLE(platform, reset_gpio_ids);
107+
108+
static struct platform_driver reset_gpio_driver = {
109+
.probe = reset_gpio_probe,
110+
.id_table = reset_gpio_ids,
111+
.driver = {
112+
.name = "reset-gpio",
113+
},
114+
};
115+
module_platform_driver(reset_gpio_driver);
116+
117+
MODULE_AUTHOR("Krzysztof Kozlowski <[email protected]>");
118+
MODULE_DESCRIPTION("Generic GPIO reset driver");
119+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)