blob: 7a6279daa8b909f5f8a5f798876a267ac664f259 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Driver for watchdog device controlled through GPIO-line
3 *
4 * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/err.h>
13#include <linux/delay.h>
14#include <linux/module.h>
15#include <linux/of_gpio.h>
16#include <linux/platform_device.h>
17#include <linux/watchdog.h>
18
19#define SOFT_TIMEOUT_MIN 1
20#define SOFT_TIMEOUT_DEF 60
21
22enum {
23 HW_ALGO_TOGGLE,
24 HW_ALGO_LEVEL,
25};
26
27struct gpio_wdt_priv {
28 int gpio;
29 bool active_low;
30 bool state;
31 bool always_running;
32 unsigned int hw_algo;
33 struct watchdog_device wdd;
34};
35
36static void gpio_wdt_disable(struct gpio_wdt_priv *priv)
37{
38 gpio_set_value_cansleep(priv->gpio, !priv->active_low);
39
40 /* Put GPIO back to tristate */
41 if (priv->hw_algo == HW_ALGO_TOGGLE)
42 gpio_direction_input(priv->gpio);
43}
44
45static int gpio_wdt_ping(struct watchdog_device *wdd)
46{
47 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
48
49 switch (priv->hw_algo) {
50 case HW_ALGO_TOGGLE:
51 /* Toggle output pin */
52 priv->state = !priv->state;
53 gpio_set_value_cansleep(priv->gpio, priv->state);
54 break;
55 case HW_ALGO_LEVEL:
56 /* Pulse */
57 gpio_set_value_cansleep(priv->gpio, !priv->active_low);
58 udelay(1);
59 gpio_set_value_cansleep(priv->gpio, priv->active_low);
60 break;
61 }
62 return 0;
63}
64
65static int gpio_wdt_start(struct watchdog_device *wdd)
66{
67 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
68
69 priv->state = priv->active_low;
70 gpio_direction_output(priv->gpio, priv->state);
71
72 set_bit(WDOG_HW_RUNNING, &wdd->status);
73
74 return gpio_wdt_ping(wdd);
75}
76
77static int gpio_wdt_stop(struct watchdog_device *wdd)
78{
79 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
80
81 if (!priv->always_running) {
82 gpio_wdt_disable(priv);
83 } else {
84 set_bit(WDOG_HW_RUNNING, &wdd->status);
85 }
86
87 return 0;
88}
89
90static const struct watchdog_info gpio_wdt_ident = {
91 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
92 WDIOF_SETTIMEOUT,
93 .identity = "GPIO Watchdog",
94};
95
96static const struct watchdog_ops gpio_wdt_ops = {
97 .owner = THIS_MODULE,
98 .start = gpio_wdt_start,
99 .stop = gpio_wdt_stop,
100 .ping = gpio_wdt_ping,
101};
102
103static int gpio_wdt_probe(struct platform_device *pdev)
104{
105 struct gpio_wdt_priv *priv;
106 enum of_gpio_flags flags;
107 unsigned int hw_margin;
108 unsigned long f = 0;
109 const char *algo;
110 int ret;
111
112 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
113 if (!priv)
114 return -ENOMEM;
115
116 platform_set_drvdata(pdev, priv);
117
118 priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
119 if (!gpio_is_valid(priv->gpio))
120 return priv->gpio;
121
122 priv->active_low = flags & OF_GPIO_ACTIVE_LOW;
123
124 ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo);
125 if (ret)
126 return ret;
127 if (!strcmp(algo, "toggle")) {
128 priv->hw_algo = HW_ALGO_TOGGLE;
129 f = GPIOF_IN;
130 } else if (!strcmp(algo, "level")) {
131 priv->hw_algo = HW_ALGO_LEVEL;
132 f = priv->active_low ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
133 } else {
134 return -EINVAL;
135 }
136
137 ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f,
138 dev_name(&pdev->dev));
139 if (ret)
140 return ret;
141
142 ret = of_property_read_u32(pdev->dev.of_node,
143 "hw_margin_ms", &hw_margin);
144 if (ret)
145 return ret;
146 /* Disallow values lower than 2 and higher than 65535 ms */
147 if (hw_margin < 2 || hw_margin > 65535)
148 return -EINVAL;
149
150 priv->always_running = of_property_read_bool(pdev->dev.of_node,
151 "always-running");
152
153 watchdog_set_drvdata(&priv->wdd, priv);
154
155 priv->wdd.info = &gpio_wdt_ident;
156 priv->wdd.ops = &gpio_wdt_ops;
157 priv->wdd.min_timeout = SOFT_TIMEOUT_MIN;
158 priv->wdd.max_hw_heartbeat_ms = hw_margin;
159 priv->wdd.parent = &pdev->dev;
160
161 if (watchdog_init_timeout(&priv->wdd, 0, &pdev->dev) < 0)
162 priv->wdd.timeout = SOFT_TIMEOUT_DEF;
163
164 watchdog_stop_on_reboot(&priv->wdd);
165
166 if (priv->always_running)
167 gpio_wdt_start(&priv->wdd);
168
169 ret = watchdog_register_device(&priv->wdd);
170
171 return ret;
172}
173
174static int gpio_wdt_remove(struct platform_device *pdev)
175{
176 struct gpio_wdt_priv *priv = platform_get_drvdata(pdev);
177
178 watchdog_unregister_device(&priv->wdd);
179
180 return 0;
181}
182
183static const struct of_device_id gpio_wdt_dt_ids[] = {
184 { .compatible = "linux,wdt-gpio", },
185 { }
186};
187MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
188
189static struct platform_driver gpio_wdt_driver = {
190 .driver = {
191 .name = "gpio-wdt",
192 .of_match_table = gpio_wdt_dt_ids,
193 },
194 .probe = gpio_wdt_probe,
195 .remove = gpio_wdt_remove,
196};
197
198#ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
199static int __init gpio_wdt_init(void)
200{
201 return platform_driver_register(&gpio_wdt_driver);
202}
203arch_initcall(gpio_wdt_init);
204#else
205module_platform_driver(gpio_wdt_driver);
206#endif
207
208MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
209MODULE_DESCRIPTION("GPIO Watchdog");
210MODULE_LICENSE("GPL");