2 * Driver for watchdog device controlled through GPIO-line
4 * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
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.
12 #include <linux/err.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/notifier.h>
16 #include <linux/of_gpio.h>
17 #include <linux/platform_device.h>
18 #include <linux/reboot.h>
19 #include <linux/watchdog.h>
21 #define SOFT_TIMEOUT_MIN 1
22 #define SOFT_TIMEOUT_DEF 60
23 #define SOFT_TIMEOUT_MAX 0xffff
30 struct gpio_wdt_priv
{
37 unsigned int hw_margin
;
38 unsigned long last_jiffies
;
39 struct notifier_block notifier
;
40 struct timer_list timer
;
41 struct watchdog_device wdd
;
44 static void gpio_wdt_disable(struct gpio_wdt_priv
*priv
)
46 gpio_set_value_cansleep(priv
->gpio
, !priv
->active_low
);
48 /* Put GPIO back to tristate */
49 if (priv
->hw_algo
== HW_ALGO_TOGGLE
)
50 gpio_direction_input(priv
->gpio
);
53 static void gpio_wdt_start_impl(struct gpio_wdt_priv
*priv
)
55 priv
->state
= priv
->active_low
;
56 gpio_direction_output(priv
->gpio
, priv
->state
);
57 priv
->last_jiffies
= jiffies
;
58 mod_timer(&priv
->timer
, priv
->last_jiffies
+ priv
->hw_margin
);
61 static int gpio_wdt_start(struct watchdog_device
*wdd
)
63 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
65 gpio_wdt_start_impl(priv
);
71 static int gpio_wdt_stop(struct watchdog_device
*wdd
)
73 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
76 if (!priv
->always_running
) {
77 mod_timer(&priv
->timer
, 0);
78 gpio_wdt_disable(priv
);
84 static int gpio_wdt_ping(struct watchdog_device
*wdd
)
86 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
88 priv
->last_jiffies
= jiffies
;
93 static int gpio_wdt_set_timeout(struct watchdog_device
*wdd
, unsigned int t
)
97 return gpio_wdt_ping(wdd
);
100 static void gpio_wdt_hwping(unsigned long data
)
102 struct watchdog_device
*wdd
= (struct watchdog_device
*)data
;
103 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
105 if (priv
->armed
&& time_after(jiffies
, priv
->last_jiffies
+
106 msecs_to_jiffies(wdd
->timeout
* 1000))) {
107 dev_crit(wdd
->dev
, "Timer expired. System will reboot soon!\n");
112 mod_timer(&priv
->timer
, jiffies
+ priv
->hw_margin
);
114 switch (priv
->hw_algo
) {
116 /* Toggle output pin */
117 priv
->state
= !priv
->state
;
118 gpio_set_value_cansleep(priv
->gpio
, priv
->state
);
122 gpio_set_value_cansleep(priv
->gpio
, !priv
->active_low
);
124 gpio_set_value_cansleep(priv
->gpio
, priv
->active_low
);
129 static int gpio_wdt_notify_sys(struct notifier_block
*nb
, unsigned long code
,
132 struct gpio_wdt_priv
*priv
= container_of(nb
, struct gpio_wdt_priv
,
135 mod_timer(&priv
->timer
, 0);
140 gpio_wdt_disable(priv
);
149 static const struct watchdog_info gpio_wdt_ident
= {
150 .options
= WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
|
152 .identity
= "GPIO Watchdog",
155 static const struct watchdog_ops gpio_wdt_ops
= {
156 .owner
= THIS_MODULE
,
157 .start
= gpio_wdt_start
,
158 .stop
= gpio_wdt_stop
,
159 .ping
= gpio_wdt_ping
,
160 .set_timeout
= gpio_wdt_set_timeout
,
163 static int gpio_wdt_probe(struct platform_device
*pdev
)
165 struct gpio_wdt_priv
*priv
;
166 enum of_gpio_flags flags
;
167 unsigned int hw_margin
;
172 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
176 priv
->gpio
= of_get_gpio_flags(pdev
->dev
.of_node
, 0, &flags
);
177 if (!gpio_is_valid(priv
->gpio
))
180 priv
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
182 ret
= of_property_read_string(pdev
->dev
.of_node
, "hw_algo", &algo
);
185 if (!strncmp(algo
, "toggle", 6)) {
186 priv
->hw_algo
= HW_ALGO_TOGGLE
;
188 } else if (!strncmp(algo
, "level", 5)) {
189 priv
->hw_algo
= HW_ALGO_LEVEL
;
190 f
= priv
->active_low
? GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
195 ret
= devm_gpio_request_one(&pdev
->dev
, priv
->gpio
, f
,
196 dev_name(&pdev
->dev
));
200 ret
= of_property_read_u32(pdev
->dev
.of_node
,
201 "hw_margin_ms", &hw_margin
);
204 /* Disallow values lower than 2 and higher than 65535 ms */
205 if (hw_margin
< 2 || hw_margin
> 65535)
208 /* Use safe value (1/2 of real timeout) */
209 priv
->hw_margin
= msecs_to_jiffies(hw_margin
/ 2);
211 priv
->always_running
= of_property_read_bool(pdev
->dev
.of_node
,
214 watchdog_set_drvdata(&priv
->wdd
, priv
);
216 priv
->wdd
.info
= &gpio_wdt_ident
;
217 priv
->wdd
.ops
= &gpio_wdt_ops
;
218 priv
->wdd
.min_timeout
= SOFT_TIMEOUT_MIN
;
219 priv
->wdd
.max_timeout
= SOFT_TIMEOUT_MAX
;
221 if (watchdog_init_timeout(&priv
->wdd
, 0, &pdev
->dev
) < 0)
222 priv
->wdd
.timeout
= SOFT_TIMEOUT_DEF
;
224 setup_timer(&priv
->timer
, gpio_wdt_hwping
, (unsigned long)&priv
->wdd
);
226 ret
= watchdog_register_device(&priv
->wdd
);
230 priv
->notifier
.notifier_call
= gpio_wdt_notify_sys
;
231 ret
= register_reboot_notifier(&priv
->notifier
);
233 goto error_unregister
;
235 if (priv
->always_running
)
236 gpio_wdt_start_impl(priv
);
241 watchdog_unregister_device(&priv
->wdd
);
245 static int gpio_wdt_remove(struct platform_device
*pdev
)
247 struct gpio_wdt_priv
*priv
= platform_get_drvdata(pdev
);
249 del_timer_sync(&priv
->timer
);
250 unregister_reboot_notifier(&priv
->notifier
);
251 watchdog_unregister_device(&priv
->wdd
);
256 static const struct of_device_id gpio_wdt_dt_ids
[] = {
257 { .compatible
= "linux,wdt-gpio", },
260 MODULE_DEVICE_TABLE(of
, gpio_wdt_dt_ids
);
262 static struct platform_driver gpio_wdt_driver
= {
265 .of_match_table
= gpio_wdt_dt_ids
,
267 .probe
= gpio_wdt_probe
,
268 .remove
= gpio_wdt_remove
,
271 #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
272 static int __init
gpio_wdt_init(void)
274 return platform_driver_register(&gpio_wdt_driver
);
276 arch_initcall(gpio_wdt_init
);
278 module_platform_driver(gpio_wdt_driver
);
281 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
282 MODULE_DESCRIPTION("GPIO Watchdog");
283 MODULE_LICENSE("GPL");