PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / watchdog / gpio_wdt.c
blob220a9e07cfd576459b3a58467e17480552a2949d
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 unsigned int hw_algo;
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);
60 return 0;
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);
70 return 0;
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;
79 return 0;
82 static int gpio_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
84 wdd->timeout = 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");
97 return;
100 /* Restart timer */
101 mod_timer(&priv->timer, jiffies + priv->hw_margin);
103 switch (priv->hw_algo) {
104 case HW_ALGO_TOGGLE:
105 /* Toggle output pin */
106 priv->state = !priv->state;
107 gpio_set_value_cansleep(priv->gpio, priv->state);
108 break;
109 case HW_ALGO_LEVEL:
110 /* Pulse */
111 gpio_set_value_cansleep(priv->gpio, !priv->active_low);
112 udelay(1);
113 gpio_set_value_cansleep(priv->gpio, priv->active_low);
114 break;
118 static int gpio_wdt_notify_sys(struct notifier_block *nb, unsigned long code,
119 void *unused)
121 struct gpio_wdt_priv *priv = container_of(nb, struct gpio_wdt_priv,
122 notifier);
124 mod_timer(&priv->timer, 0);
126 switch (code) {
127 case SYS_HALT:
128 case SYS_POWER_OFF:
129 gpio_wdt_disable(priv);
130 break;
131 default:
132 break;
135 return NOTIFY_DONE;
138 static const struct watchdog_info gpio_wdt_ident = {
139 .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
140 WDIOF_SETTIMEOUT,
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;
157 unsigned long f = 0;
158 const char *algo;
159 int ret;
161 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
162 if (!priv)
163 return -ENOMEM;
165 priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
166 if (!gpio_is_valid(priv->gpio))
167 return priv->gpio;
169 priv->active_low = flags & OF_GPIO_ACTIVE_LOW;
171 ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo);
172 if (ret)
173 return ret;
174 if (!strncmp(algo, "toggle", 6)) {
175 priv->hw_algo = HW_ALGO_TOGGLE;
176 f = GPIOF_IN;
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;
180 } else {
181 return -EINVAL;
184 ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f,
185 dev_name(&pdev->dev));
186 if (ret)
187 return ret;
189 ret = of_property_read_u32(pdev->dev.of_node,
190 "hw_margin_ms", &hw_margin);
191 if (ret)
192 return ret;
193 /* Disallow values lower than 2 and higher than 65535 ms */
194 if (hw_margin < 2 || hw_margin > 65535)
195 return -EINVAL;
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);
213 if (ret)
214 return ret;
216 priv->notifier.notifier_call = gpio_wdt_notify_sys;
217 ret = register_reboot_notifier(&priv->notifier);
218 if (ret)
219 watchdog_unregister_device(&priv->wdd);
221 return ret;
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);
232 return 0;
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 = {
242 .driver = {
243 .name = "gpio-wdt",
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");