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
{
35 unsigned int hw_margin
;
36 unsigned long last_jiffies
;
37 struct notifier_block notifier
;
38 struct timer_list timer
;
39 struct watchdog_device wdd
;
42 static void gpio_wdt_disable(struct gpio_wdt_priv
*priv
)
44 gpio_set_value_cansleep(priv
->gpio
, !priv
->active_low
);
46 /* Put GPIO back to tristate */
47 if (priv
->hw_algo
== HW_ALGO_TOGGLE
)
48 gpio_direction_input(priv
->gpio
);
51 static int gpio_wdt_start(struct watchdog_device
*wdd
)
53 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
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
);
63 static int gpio_wdt_stop(struct watchdog_device
*wdd
)
65 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
67 mod_timer(&priv
->timer
, 0);
68 gpio_wdt_disable(priv
);
73 static int gpio_wdt_ping(struct watchdog_device
*wdd
)
75 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
77 priv
->last_jiffies
= jiffies
;
82 static int gpio_wdt_set_timeout(struct watchdog_device
*wdd
, unsigned int t
)
86 return gpio_wdt_ping(wdd
);
89 static void gpio_wdt_hwping(unsigned long data
)
91 struct watchdog_device
*wdd
= (struct watchdog_device
*)data
;
92 struct gpio_wdt_priv
*priv
= watchdog_get_drvdata(wdd
);
94 if (time_after(jiffies
, priv
->last_jiffies
+
95 msecs_to_jiffies(wdd
->timeout
* 1000))) {
96 dev_crit(wdd
->dev
, "Timer expired. System will reboot soon!\n");
101 mod_timer(&priv
->timer
, jiffies
+ priv
->hw_margin
);
103 switch (priv
->hw_algo
) {
105 /* Toggle output pin */
106 priv
->state
= !priv
->state
;
107 gpio_set_value_cansleep(priv
->gpio
, priv
->state
);
111 gpio_set_value_cansleep(priv
->gpio
, !priv
->active_low
);
113 gpio_set_value_cansleep(priv
->gpio
, priv
->active_low
);
118 static int gpio_wdt_notify_sys(struct notifier_block
*nb
, unsigned long code
,
121 struct gpio_wdt_priv
*priv
= container_of(nb
, struct gpio_wdt_priv
,
124 mod_timer(&priv
->timer
, 0);
129 gpio_wdt_disable(priv
);
138 static const struct watchdog_info gpio_wdt_ident
= {
139 .options
= WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
|
141 .identity
= "GPIO Watchdog",
144 static const struct watchdog_ops gpio_wdt_ops
= {
145 .owner
= THIS_MODULE
,
146 .start
= gpio_wdt_start
,
147 .stop
= gpio_wdt_stop
,
148 .ping
= gpio_wdt_ping
,
149 .set_timeout
= gpio_wdt_set_timeout
,
152 static int gpio_wdt_probe(struct platform_device
*pdev
)
154 struct gpio_wdt_priv
*priv
;
155 enum of_gpio_flags flags
;
156 unsigned int hw_margin
;
161 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
165 priv
->gpio
= of_get_gpio_flags(pdev
->dev
.of_node
, 0, &flags
);
166 if (!gpio_is_valid(priv
->gpio
))
169 priv
->active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
171 ret
= of_property_read_string(pdev
->dev
.of_node
, "hw_algo", &algo
);
174 if (!strncmp(algo
, "toggle", 6)) {
175 priv
->hw_algo
= HW_ALGO_TOGGLE
;
177 } else if (!strncmp(algo
, "level", 5)) {
178 priv
->hw_algo
= HW_ALGO_LEVEL
;
179 f
= priv
->active_low
? GPIOF_OUT_INIT_HIGH
: GPIOF_OUT_INIT_LOW
;
184 ret
= devm_gpio_request_one(&pdev
->dev
, priv
->gpio
, f
,
185 dev_name(&pdev
->dev
));
189 ret
= of_property_read_u32(pdev
->dev
.of_node
,
190 "hw_margin_ms", &hw_margin
);
193 /* Disallow values lower than 2 and higher than 65535 ms */
194 if (hw_margin
< 2 || hw_margin
> 65535)
197 /* Use safe value (1/2 of real timeout) */
198 priv
->hw_margin
= msecs_to_jiffies(hw_margin
/ 2);
200 watchdog_set_drvdata(&priv
->wdd
, priv
);
202 priv
->wdd
.info
= &gpio_wdt_ident
;
203 priv
->wdd
.ops
= &gpio_wdt_ops
;
204 priv
->wdd
.min_timeout
= SOFT_TIMEOUT_MIN
;
205 priv
->wdd
.max_timeout
= SOFT_TIMEOUT_MAX
;
207 if (watchdog_init_timeout(&priv
->wdd
, 0, &pdev
->dev
) < 0)
208 priv
->wdd
.timeout
= SOFT_TIMEOUT_DEF
;
210 setup_timer(&priv
->timer
, gpio_wdt_hwping
, (unsigned long)&priv
->wdd
);
212 ret
= watchdog_register_device(&priv
->wdd
);
216 priv
->notifier
.notifier_call
= gpio_wdt_notify_sys
;
217 ret
= register_reboot_notifier(&priv
->notifier
);
219 watchdog_unregister_device(&priv
->wdd
);
224 static int gpio_wdt_remove(struct platform_device
*pdev
)
226 struct gpio_wdt_priv
*priv
= platform_get_drvdata(pdev
);
228 del_timer_sync(&priv
->timer
);
229 unregister_reboot_notifier(&priv
->notifier
);
230 watchdog_unregister_device(&priv
->wdd
);
235 static const struct of_device_id gpio_wdt_dt_ids
[] = {
236 { .compatible
= "linux,wdt-gpio", },
239 MODULE_DEVICE_TABLE(of
, gpio_wdt_dt_ids
);
241 static struct platform_driver gpio_wdt_driver
= {
244 .owner
= THIS_MODULE
,
245 .of_match_table
= gpio_wdt_dt_ids
,
247 .probe
= gpio_wdt_probe
,
248 .remove
= gpio_wdt_remove
,
250 module_platform_driver(gpio_wdt_driver
);
252 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
253 MODULE_DESCRIPTION("GPIO Watchdog");
254 MODULE_LICENSE("GPL");