Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Driver for watchdog device controlled through GPIO-line |
| 3 | * |
| 4 | * Author: 2013, Alexander Shiyan <shc_work@mail.ru> |
| 5 | * |
| 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. |
| 10 | */ |
| 11 | |
| 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> |
| 20 | |
| 21 | #define SOFT_TIMEOUT_MIN 1 |
| 22 | #define SOFT_TIMEOUT_DEF 60 |
| 23 | #define SOFT_TIMEOUT_MAX 0xffff |
| 24 | |
| 25 | enum { |
| 26 | HW_ALGO_TOGGLE, |
| 27 | HW_ALGO_LEVEL, |
| 28 | }; |
| 29 | |
| 30 | struct gpio_wdt_priv { |
| 31 | int gpio; |
| 32 | bool active_low; |
| 33 | bool state; |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 34 | bool always_running; |
| 35 | bool armed; |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 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; |
| 42 | }; |
| 43 | |
| 44 | static void gpio_wdt_disable(struct gpio_wdt_priv *priv) |
| 45 | { |
| 46 | gpio_set_value_cansleep(priv->gpio, !priv->active_low); |
| 47 | |
| 48 | /* Put GPIO back to tristate */ |
| 49 | if (priv->hw_algo == HW_ALGO_TOGGLE) |
| 50 | gpio_direction_input(priv->gpio); |
| 51 | } |
| 52 | |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 53 | static void gpio_wdt_start_impl(struct gpio_wdt_priv *priv) |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 54 | { |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 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); |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int gpio_wdt_start(struct watchdog_device *wdd) |
| 62 | { |
| 63 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); |
| 64 | |
| 65 | gpio_wdt_start_impl(priv); |
| 66 | priv->armed = true; |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int gpio_wdt_stop(struct watchdog_device *wdd) |
| 72 | { |
| 73 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); |
| 74 | |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 75 | priv->armed = false; |
| 76 | if (!priv->always_running) { |
| 77 | mod_timer(&priv->timer, 0); |
| 78 | gpio_wdt_disable(priv); |
| 79 | } |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int gpio_wdt_ping(struct watchdog_device *wdd) |
| 85 | { |
| 86 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); |
| 87 | |
| 88 | priv->last_jiffies = jiffies; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int gpio_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t) |
| 94 | { |
| 95 | wdd->timeout = t; |
| 96 | |
| 97 | return gpio_wdt_ping(wdd); |
| 98 | } |
| 99 | |
| 100 | static void gpio_wdt_hwping(unsigned long data) |
| 101 | { |
| 102 | struct watchdog_device *wdd = (struct watchdog_device *)data; |
| 103 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); |
| 104 | |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 105 | if (priv->armed && time_after(jiffies, priv->last_jiffies + |
| 106 | msecs_to_jiffies(wdd->timeout * 1000))) { |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 107 | dev_crit(wdd->dev, "Timer expired. System will reboot soon!\n"); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | /* Restart timer */ |
| 112 | mod_timer(&priv->timer, jiffies + priv->hw_margin); |
| 113 | |
| 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; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static int gpio_wdt_notify_sys(struct notifier_block *nb, unsigned long code, |
| 130 | void *unused) |
| 131 | { |
| 132 | struct gpio_wdt_priv *priv = container_of(nb, struct gpio_wdt_priv, |
| 133 | notifier); |
| 134 | |
| 135 | mod_timer(&priv->timer, 0); |
| 136 | |
| 137 | switch (code) { |
| 138 | case SYS_HALT: |
| 139 | case SYS_POWER_OFF: |
| 140 | gpio_wdt_disable(priv); |
| 141 | break; |
| 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | return NOTIFY_DONE; |
| 147 | } |
| 148 | |
| 149 | static const struct watchdog_info gpio_wdt_ident = { |
| 150 | .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | |
| 151 | WDIOF_SETTIMEOUT, |
| 152 | .identity = "GPIO Watchdog", |
| 153 | }; |
| 154 | |
| 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, |
| 161 | }; |
| 162 | |
| 163 | static int gpio_wdt_probe(struct platform_device *pdev) |
| 164 | { |
| 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; |
| 171 | |
| 172 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 173 | if (!priv) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags); |
| 177 | if (!gpio_is_valid(priv->gpio)) |
| 178 | return priv->gpio; |
| 179 | |
| 180 | priv->active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 181 | |
| 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; |
| 193 | } |
| 194 | |
| 195 | ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f, |
| 196 | dev_name(&pdev->dev)); |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | |
| 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; |
| 207 | |
| 208 | /* Use safe value (1/2 of real timeout) */ |
| 209 | priv->hw_margin = msecs_to_jiffies(hw_margin / 2); |
| 210 | |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 211 | priv->always_running = of_property_read_bool(pdev->dev.of_node, |
| 212 | "always-running"); |
| 213 | |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 214 | watchdog_set_drvdata(&priv->wdd, priv); |
| 215 | |
| 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 | |
| 221 | if (watchdog_init_timeout(&priv->wdd, 0, &pdev->dev) < 0) |
| 222 | priv->wdd.timeout = SOFT_TIMEOUT_DEF; |
| 223 | |
| 224 | setup_timer(&priv->timer, gpio_wdt_hwping, (unsigned long)&priv->wdd); |
| 225 | |
| 226 | ret = watchdog_register_device(&priv->wdd); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | |
| 230 | priv->notifier.notifier_call = gpio_wdt_notify_sys; |
| 231 | ret = register_reboot_notifier(&priv->notifier); |
| 232 | if (ret) |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 233 | goto error_unregister; |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 234 | |
Mike Looijmans | ba804a9 | 2015-01-14 07:28:29 +0100 | [diff] [blame] | 235 | if (priv->always_running) |
| 236 | gpio_wdt_start_impl(priv); |
| 237 | |
| 238 | return 0; |
| 239 | |
| 240 | error_unregister: |
| 241 | watchdog_unregister_device(&priv->wdd); |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int gpio_wdt_remove(struct platform_device *pdev) |
| 246 | { |
| 247 | struct gpio_wdt_priv *priv = platform_get_drvdata(pdev); |
| 248 | |
| 249 | del_timer_sync(&priv->timer); |
| 250 | unregister_reboot_notifier(&priv->notifier); |
| 251 | watchdog_unregister_device(&priv->wdd); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static const struct of_device_id gpio_wdt_dt_ids[] = { |
| 257 | { .compatible = "linux,wdt-gpio", }, |
| 258 | { } |
| 259 | }; |
| 260 | MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids); |
| 261 | |
| 262 | static struct platform_driver gpio_wdt_driver = { |
| 263 | .driver = { |
| 264 | .name = "gpio-wdt", |
Alexander Shiyan | 25134ea | 2013-11-30 11:54:32 +0400 | [diff] [blame] | 265 | .of_match_table = gpio_wdt_dt_ids, |
| 266 | }, |
| 267 | .probe = gpio_wdt_probe, |
| 268 | .remove = gpio_wdt_remove, |
| 269 | }; |
| 270 | module_platform_driver(gpio_wdt_driver); |
| 271 | |
| 272 | MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); |
| 273 | MODULE_DESCRIPTION("GPIO Watchdog"); |
| 274 | MODULE_LICENSE("GPL"); |