]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/watchdog/gpio_wdt.c
Merge tag '9p-for-5.11-rc1' of git://github.com/martinetd/linux
[mirror_ubuntu-hirsute-kernel.git] / drivers / watchdog / gpio_wdt.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
25134eaf
AS
2/*
3 * Driver for watchdog device controlled through GPIO-line
4 *
5 * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
25134eaf
AS
6 */
7
8#include <linux/err.h>
9#include <linux/delay.h>
10#include <linux/module.h>
a2363f9d
LW
11#include <linux/gpio/consumer.h>
12#include <linux/of.h>
25134eaf 13#include <linux/platform_device.h>
25134eaf
AS
14#include <linux/watchdog.h>
15
1a4aaf9f
MR
16static bool nowayout = WATCHDOG_NOWAYOUT;
17module_param(nowayout, bool, 0);
18MODULE_PARM_DESC(nowayout,
19 "Watchdog cannot be stopped once started (default="
20 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
21
25134eaf
AS
22#define SOFT_TIMEOUT_MIN 1
23#define SOFT_TIMEOUT_DEF 60
25134eaf
AS
24
25enum {
26 HW_ALGO_TOGGLE,
27 HW_ALGO_LEVEL,
28};
29
30struct gpio_wdt_priv {
a2363f9d 31 struct gpio_desc *gpiod;
25134eaf 32 bool state;
ba804a95 33 bool always_running;
25134eaf 34 unsigned int hw_algo;
25134eaf
AS
35 struct watchdog_device wdd;
36};
37
38static void gpio_wdt_disable(struct gpio_wdt_priv *priv)
39{
a2363f9d
LW
40 /* Eternal ping */
41 gpiod_set_value_cansleep(priv->gpiod, 1);
25134eaf
AS
42
43 /* Put GPIO back to tristate */
44 if (priv->hw_algo == HW_ALGO_TOGGLE)
a2363f9d 45 gpiod_direction_input(priv->gpiod);
25134eaf
AS
46}
47
03bca158 48static int gpio_wdt_ping(struct watchdog_device *wdd)
4f2d0b2d 49{
4f2d0b2d
UKK
50 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
51
4f2d0b2d
UKK
52 switch (priv->hw_algo) {
53 case HW_ALGO_TOGGLE:
54 /* Toggle output pin */
55 priv->state = !priv->state;
a2363f9d 56 gpiod_set_value_cansleep(priv->gpiod, priv->state);
4f2d0b2d
UKK
57 break;
58 case HW_ALGO_LEVEL:
59 /* Pulse */
a2363f9d 60 gpiod_set_value_cansleep(priv->gpiod, 1);
4f2d0b2d 61 udelay(1);
a2363f9d 62 gpiod_set_value_cansleep(priv->gpiod, 0);
4f2d0b2d
UKK
63 break;
64 }
03bca158 65 return 0;
ba804a95
ML
66}
67
68static int gpio_wdt_start(struct watchdog_device *wdd)
69{
70 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
71
a2363f9d
LW
72 priv->state = 0;
73 gpiod_direction_output(priv->gpiod, priv->state);
25134eaf 74
03bca158
GR
75 set_bit(WDOG_HW_RUNNING, &wdd->status);
76
77 return gpio_wdt_ping(wdd);
25134eaf
AS
78}
79
80static int gpio_wdt_stop(struct watchdog_device *wdd)
81{
82 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
83
ba804a95 84 if (!priv->always_running) {
ba804a95 85 gpio_wdt_disable(priv);
bc137dfd
RV
86 } else {
87 set_bit(WDOG_HW_RUNNING, &wdd->status);
ba804a95 88 }
25134eaf
AS
89
90 return 0;
91}
92
25134eaf
AS
93static const struct watchdog_info gpio_wdt_ident = {
94 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
95 WDIOF_SETTIMEOUT,
96 .identity = "GPIO Watchdog",
97};
98
99static const struct watchdog_ops gpio_wdt_ops = {
100 .owner = THIS_MODULE,
101 .start = gpio_wdt_start,
102 .stop = gpio_wdt_stop,
103 .ping = gpio_wdt_ping,
25134eaf
AS
104};
105
106static int gpio_wdt_probe(struct platform_device *pdev)
107{
d0d0677e
LW
108 struct device *dev = &pdev->dev;
109 struct device_node *np = dev->of_node;
25134eaf 110 struct gpio_wdt_priv *priv;
a2363f9d 111 enum gpiod_flags gflags;
25134eaf 112 unsigned int hw_margin;
25134eaf
AS
113 const char *algo;
114 int ret;
115
d0d0677e 116 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
25134eaf
AS
117 if (!priv)
118 return -ENOMEM;
119
1ac06563
WY
120 platform_set_drvdata(pdev, priv);
121
d0d0677e 122 ret = of_property_read_string(np, "hw_algo", &algo);
25134eaf
AS
123 if (ret)
124 return ret;
0a0a542f 125 if (!strcmp(algo, "toggle")) {
25134eaf 126 priv->hw_algo = HW_ALGO_TOGGLE;
a2363f9d 127 gflags = GPIOD_IN;
0a0a542f 128 } else if (!strcmp(algo, "level")) {
25134eaf 129 priv->hw_algo = HW_ALGO_LEVEL;
a2363f9d 130 gflags = GPIOD_OUT_LOW;
25134eaf
AS
131 } else {
132 return -EINVAL;
133 }
134
a2363f9d
LW
135 priv->gpiod = devm_gpiod_get(dev, NULL, gflags);
136 if (IS_ERR(priv->gpiod))
137 return PTR_ERR(priv->gpiod);
25134eaf 138
d0d0677e 139 ret = of_property_read_u32(np,
25134eaf
AS
140 "hw_margin_ms", &hw_margin);
141 if (ret)
142 return ret;
143 /* Disallow values lower than 2 and higher than 65535 ms */
144 if (hw_margin < 2 || hw_margin > 65535)
145 return -EINVAL;
146
d0d0677e 147 priv->always_running = of_property_read_bool(np,
ba804a95
ML
148 "always-running");
149
25134eaf
AS
150 watchdog_set_drvdata(&priv->wdd, priv);
151
152 priv->wdd.info = &gpio_wdt_ident;
153 priv->wdd.ops = &gpio_wdt_ops;
154 priv->wdd.min_timeout = SOFT_TIMEOUT_MIN;
03bca158 155 priv->wdd.max_hw_heartbeat_ms = hw_margin;
d0d0677e 156 priv->wdd.parent = dev;
65adfa22 157 priv->wdd.timeout = SOFT_TIMEOUT_DEF;
25134eaf 158
3564fbc5 159 watchdog_init_timeout(&priv->wdd, 0, dev);
1a4aaf9f 160 watchdog_set_nowayout(&priv->wdd, nowayout);
25134eaf 161
28e805b4
DR
162 watchdog_stop_on_reboot(&priv->wdd);
163
ba804a95 164 if (priv->always_running)
03bca158 165 gpio_wdt_start(&priv->wdd);
ba804a95 166
3564fbc5 167 return devm_watchdog_register_device(dev, &priv->wdd);
25134eaf
AS
168}
169
170static const struct of_device_id gpio_wdt_dt_ids[] = {
171 { .compatible = "linux,wdt-gpio", },
172 { }
173};
174MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
175
176static struct platform_driver gpio_wdt_driver = {
177 .driver = {
178 .name = "gpio-wdt",
25134eaf
AS
179 .of_match_table = gpio_wdt_dt_ids,
180 },
181 .probe = gpio_wdt_probe,
25134eaf 182};
5e53c8ed
JBT
183
184#ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
185static int __init gpio_wdt_init(void)
186{
187 return platform_driver_register(&gpio_wdt_driver);
188}
189arch_initcall(gpio_wdt_init);
190#else
25134eaf 191module_platform_driver(gpio_wdt_driver);
5e53c8ed 192#endif
25134eaf
AS
193
194MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
195MODULE_DESCRIPTION("GPIO Watchdog");
196MODULE_LICENSE("GPL");