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_hwping(unsigned long data
)
55 struct watchdog_device
*wdd
= (struct watchdog_device
*)data
;
56 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
58 if (priv
->armed
&& time_after(jiffies
, priv
->last_jiffies
+
59 msecs_to_jiffies(wdd
->timeout
* 1000))) {
60 dev_crit(wdd
->dev
, "Timer expired. System will reboot soon!\n");
65 mod_timer(&priv
->timer
, jiffies
+ priv
->hw_margin
);
67 switch (priv
->hw_algo
) {
69 /* Toggle output pin */
70 priv
->state
= !priv
->state
;
71 gpio_set_value_cansleep(priv
->gpio
, priv
->state
);
75 gpio_set_value_cansleep(priv
->gpio
, !priv
->active_low
);
77 gpio_set_value_cansleep(priv
->gpio
, priv
->active_low
);
82 static void gpio_wdt_start_impl(struct gpio_wdt_priv
*priv
)
84 priv
->state
= priv
->active_low
;
85 gpio_direction_output(priv
->gpio
, priv
->state
);
86 priv
->last_jiffies
= jiffies
;
87 gpio_wdt_hwping((unsigned long)&priv
->wdd
);
90 static int gpio_wdt_start(struct watchdog_device
*wdd
)
92 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
94 gpio_wdt_start_impl(priv
);
100 static int gpio_wdt_stop(struct watchdog_device
*wdd
)
102 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
105 if (!priv
->always_running
) {
106 mod_timer(&priv
->timer
, 0);
107 gpio_wdt_disable(priv
);
113 static int gpio_wdt_ping(struct watchdog_device
*wdd
)
115 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
117 priv
->last_jiffies
= jiffies
;
122 static int gpio_wdt_set_timeout(struct watchdog_device
*wdd
, unsigned int t
)
126 return gpio_wdt_ping(wdd
);
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 (!strcmp(algo
, "toggle")) {
186 priv
->hw_algo
= HW_ALGO_TOGGLE
;
188 } else if (!strcmp(algo
, "level")) {
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
;
220 priv
->wdd
.parent
= &pdev
->dev
;
222 if (watchdog_init_timeout(&priv
->wdd
, 0, &pdev
->dev
) < 0)
223 priv
->wdd
.timeout
= SOFT_TIMEOUT_DEF
;
225 setup_timer(&priv
->timer
, gpio_wdt_hwping
, (unsigned long)&priv
->wdd
);
227 ret
= watchdog_register_device(&priv
->wdd
);
231 priv
->notifier
.notifier_call
= gpio_wdt_notify_sys
;
232 ret
= register_reboot_notifier(&priv
->notifier
);
234 goto error_unregister
;
236 if (priv
->always_running
)
237 gpio_wdt_start_impl(priv
);
242 watchdog_unregister_device(&priv
->wdd
);
246 static int gpio_wdt_remove(struct platform_device
*pdev
)
248 struct gpio_wdt_priv
*priv
= platform_get_drvdata(pdev
);
250 del_timer_sync(&priv
->timer
);
251 unregister_reboot_notifier(&priv
->notifier
);
252 watchdog_unregister_device(&priv
->wdd
);
257 static const struct of_device_id gpio_wdt_dt_ids
[] = {
258 { .compatible
= "linux,wdt-gpio", },
261 MODULE_DEVICE_TABLE(of
, gpio_wdt_dt_ids
);
263 static struct platform_driver gpio_wdt_driver
= {
266 .of_match_table
= gpio_wdt_dt_ids
,
268 .probe
= gpio_wdt_probe
,
269 .remove
= gpio_wdt_remove
,
272 #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
273 static int __init
gpio_wdt_init(void)
275 return platform_driver_register(&gpio_wdt_driver
);
277 arch_initcall(gpio_wdt_init
);
279 module_platform_driver(gpio_wdt_driver
);
282 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
283 MODULE_DESCRIPTION("GPIO Watchdog");
284 MODULE_LICENSE("GPL");