Linux 4.2.1
[linux/fpc-iii.git] / drivers / watchdog / gpio_wdt.c
blob1687cc2d71223cc799fbb60bb1d3f0914d7c3ada
1 /*
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
25 enum {
26 HW_ALGO_TOGGLE,
27 HW_ALGO_LEVEL,
30 struct gpio_wdt_priv {
31 int gpio;
32 bool active_low;
33 bool state;
34 bool always_running;
35 bool armed;
36 unsigned int hw_algo;
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);
66 priv->armed = true;
68 return 0;
71 static int gpio_wdt_stop(struct watchdog_device *wdd)
73 struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
75 priv->armed = false;
76 if (!priv->always_running) {
77 mod_timer(&priv->timer, 0);
78 gpio_wdt_disable(priv);
81 return 0;
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;
90 return 0;
93 static int gpio_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
95 wdd->timeout = 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");
108 return;
111 /* Restart timer */
112 mod_timer(&priv->timer, jiffies + priv->hw_margin);
114 switch (priv->hw_algo) {
115 case HW_ALGO_TOGGLE:
116 /* Toggle output pin */
117 priv->state = !priv->state;
118 gpio_set_value_cansleep(priv->gpio, priv->state);
119 break;
120 case HW_ALGO_LEVEL:
121 /* Pulse */
122 gpio_set_value_cansleep(priv->gpio, !priv->active_low);
123 udelay(1);
124 gpio_set_value_cansleep(priv->gpio, priv->active_low);
125 break;
129 static int gpio_wdt_notify_sys(struct notifier_block *nb, unsigned long code,
130 void *unused)
132 struct gpio_wdt_priv *priv = container_of(nb, struct gpio_wdt_priv,
133 notifier);
135 mod_timer(&priv->timer, 0);
137 switch (code) {
138 case SYS_HALT:
139 case SYS_POWER_OFF:
140 gpio_wdt_disable(priv);
141 break;
142 default:
143 break;
146 return NOTIFY_DONE;
149 static const struct watchdog_info gpio_wdt_ident = {
150 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
151 WDIOF_SETTIMEOUT,
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;
168 unsigned long f = 0;
169 const char *algo;
170 int ret;
172 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
173 if (!priv)
174 return -ENOMEM;
176 priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
177 if (!gpio_is_valid(priv->gpio))
178 return priv->gpio;
180 priv->active_low = flags & OF_GPIO_ACTIVE_LOW;
182 ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo);
183 if (ret)
184 return ret;
185 if (!strncmp(algo, "toggle", 6)) {
186 priv->hw_algo = HW_ALGO_TOGGLE;
187 f = GPIOF_IN;
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;
191 } else {
192 return -EINVAL;
195 ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f,
196 dev_name(&pdev->dev));
197 if (ret)
198 return ret;
200 ret = of_property_read_u32(pdev->dev.of_node,
201 "hw_margin_ms", &hw_margin);
202 if (ret)
203 return ret;
204 /* Disallow values lower than 2 and higher than 65535 ms */
205 if (hw_margin < 2 || hw_margin > 65535)
206 return -EINVAL;
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,
212 "always-running");
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);
227 if (ret)
228 return ret;
230 priv->notifier.notifier_call = gpio_wdt_notify_sys;
231 ret = register_reboot_notifier(&priv->notifier);
232 if (ret)
233 goto error_unregister;
235 if (priv->always_running)
236 gpio_wdt_start_impl(priv);
238 return 0;
240 error_unregister:
241 watchdog_unregister_device(&priv->wdd);
242 return ret;
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);
253 return 0;
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 = {
263 .driver = {
264 .name = "gpio-wdt",
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);
277 #else
278 module_platform_driver(gpio_wdt_driver);
279 #endif
281 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
282 MODULE_DESCRIPTION("GPIO Watchdog");
283 MODULE_LICENSE("GPL");