Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
25134eaf AS |
2 | /* |
3 | * Driver for watchdog device controlled through GPIO-line | |
4 | * | |
5 | * Author: 2013, Alexander Shiyan <shc_work@mail.ru> | |
25134eaf AS |
6 | */ |
7 | ||
25134eaf | 8 | #include <linux/delay.h> |
7e5bca6e | 9 | #include <linux/err.h> |
a2363f9d | 10 | #include <linux/gpio/consumer.h> |
7e5bca6e AS |
11 | #include <linux/mod_devicetable.h> |
12 | #include <linux/module.h> | |
25134eaf | 13 | #include <linux/platform_device.h> |
7e5bca6e | 14 | #include <linux/property.h> |
25134eaf AS |
15 | #include <linux/watchdog.h> |
16 | ||
1a4aaf9f MR |
17 | static bool nowayout = WATCHDOG_NOWAYOUT; |
18 | module_param(nowayout, bool, 0); | |
19 | MODULE_PARM_DESC(nowayout, | |
20 | "Watchdog cannot be stopped once started (default=" | |
21 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
22 | ||
25134eaf AS |
23 | #define SOFT_TIMEOUT_MIN 1 |
24 | #define SOFT_TIMEOUT_DEF 60 | |
25134eaf AS |
25 | |
26 | enum { | |
27 | HW_ALGO_TOGGLE, | |
28 | HW_ALGO_LEVEL, | |
29 | }; | |
30 | ||
31 | struct gpio_wdt_priv { | |
a2363f9d | 32 | struct gpio_desc *gpiod; |
25134eaf | 33 | bool state; |
ba804a95 | 34 | bool always_running; |
25134eaf | 35 | unsigned int hw_algo; |
25134eaf AS |
36 | struct watchdog_device wdd; |
37 | }; | |
38 | ||
39 | static void gpio_wdt_disable(struct gpio_wdt_priv *priv) | |
40 | { | |
a2363f9d LW |
41 | /* Eternal ping */ |
42 | gpiod_set_value_cansleep(priv->gpiod, 1); | |
25134eaf AS |
43 | |
44 | /* Put GPIO back to tristate */ | |
45 | if (priv->hw_algo == HW_ALGO_TOGGLE) | |
a2363f9d | 46 | gpiod_direction_input(priv->gpiod); |
25134eaf AS |
47 | } |
48 | ||
03bca158 | 49 | static int gpio_wdt_ping(struct watchdog_device *wdd) |
4f2d0b2d | 50 | { |
4f2d0b2d UKK |
51 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); |
52 | ||
4f2d0b2d UKK |
53 | switch (priv->hw_algo) { |
54 | case HW_ALGO_TOGGLE: | |
55 | /* Toggle output pin */ | |
56 | priv->state = !priv->state; | |
a2363f9d | 57 | gpiod_set_value_cansleep(priv->gpiod, priv->state); |
4f2d0b2d UKK |
58 | break; |
59 | case HW_ALGO_LEVEL: | |
60 | /* Pulse */ | |
a2363f9d | 61 | gpiod_set_value_cansleep(priv->gpiod, 1); |
4f2d0b2d | 62 | udelay(1); |
a2363f9d | 63 | gpiod_set_value_cansleep(priv->gpiod, 0); |
4f2d0b2d UKK |
64 | break; |
65 | } | |
03bca158 | 66 | return 0; |
ba804a95 ML |
67 | } |
68 | ||
69 | static int gpio_wdt_start(struct watchdog_device *wdd) | |
70 | { | |
71 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); | |
72 | ||
a2363f9d LW |
73 | priv->state = 0; |
74 | gpiod_direction_output(priv->gpiod, priv->state); | |
25134eaf | 75 | |
03bca158 GR |
76 | set_bit(WDOG_HW_RUNNING, &wdd->status); |
77 | ||
78 | return gpio_wdt_ping(wdd); | |
25134eaf AS |
79 | } |
80 | ||
81 | static int gpio_wdt_stop(struct watchdog_device *wdd) | |
82 | { | |
83 | struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd); | |
84 | ||
ba804a95 | 85 | if (!priv->always_running) { |
ba804a95 | 86 | gpio_wdt_disable(priv); |
bc137dfd RV |
87 | } else { |
88 | set_bit(WDOG_HW_RUNNING, &wdd->status); | |
ba804a95 | 89 | } |
25134eaf AS |
90 | |
91 | return 0; | |
92 | } | |
93 | ||
25134eaf AS |
94 | static const struct watchdog_info gpio_wdt_ident = { |
95 | .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING | | |
96 | WDIOF_SETTIMEOUT, | |
97 | .identity = "GPIO Watchdog", | |
98 | }; | |
99 | ||
100 | static const struct watchdog_ops gpio_wdt_ops = { | |
101 | .owner = THIS_MODULE, | |
102 | .start = gpio_wdt_start, | |
103 | .stop = gpio_wdt_stop, | |
104 | .ping = gpio_wdt_ping, | |
25134eaf AS |
105 | }; |
106 | ||
107 | static int gpio_wdt_probe(struct platform_device *pdev) | |
108 | { | |
d0d0677e | 109 | struct device *dev = &pdev->dev; |
25134eaf | 110 | struct gpio_wdt_priv *priv; |
a2363f9d | 111 | enum gpiod_flags gflags; |
25134eaf | 112 | unsigned int hw_margin; |
25134eaf AS |
113 | const char *algo; |
114 | int ret; | |
115 | ||
d0d0677e | 116 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
25134eaf AS |
117 | if (!priv) |
118 | return -ENOMEM; | |
119 | ||
1ac06563 WY |
120 | platform_set_drvdata(pdev, priv); |
121 | ||
7e5bca6e | 122 | ret = device_property_read_string(dev, "hw_algo", &algo); |
25134eaf AS |
123 | if (ret) |
124 | return ret; | |
0a0a542f | 125 | if (!strcmp(algo, "toggle")) { |
25134eaf | 126 | priv->hw_algo = HW_ALGO_TOGGLE; |
a2363f9d | 127 | gflags = GPIOD_IN; |
0a0a542f | 128 | } else if (!strcmp(algo, "level")) { |
25134eaf | 129 | priv->hw_algo = HW_ALGO_LEVEL; |
a2363f9d | 130 | gflags = GPIOD_OUT_LOW; |
25134eaf AS |
131 | } else { |
132 | return -EINVAL; | |
133 | } | |
134 | ||
a2363f9d LW |
135 | priv->gpiod = devm_gpiod_get(dev, NULL, gflags); |
136 | if (IS_ERR(priv->gpiod)) | |
137 | return PTR_ERR(priv->gpiod); | |
25134eaf | 138 | |
7e5bca6e | 139 | ret = device_property_read_u32(dev, "hw_margin_ms", &hw_margin); |
25134eaf AS |
140 | if (ret) |
141 | return ret; | |
142 | /* Disallow values lower than 2 and higher than 65535 ms */ | |
143 | if (hw_margin < 2 || hw_margin > 65535) | |
144 | return -EINVAL; | |
145 | ||
7e5bca6e | 146 | priv->always_running = device_property_read_bool(dev, "always-running"); |
ba804a95 | 147 | |
25134eaf AS |
148 | watchdog_set_drvdata(&priv->wdd, priv); |
149 | ||
150 | priv->wdd.info = &gpio_wdt_ident; | |
151 | priv->wdd.ops = &gpio_wdt_ops; | |
152 | priv->wdd.min_timeout = SOFT_TIMEOUT_MIN; | |
03bca158 | 153 | priv->wdd.max_hw_heartbeat_ms = hw_margin; |
d0d0677e | 154 | priv->wdd.parent = dev; |
65adfa22 | 155 | priv->wdd.timeout = SOFT_TIMEOUT_DEF; |
25134eaf | 156 | |
3564fbc5 | 157 | watchdog_init_timeout(&priv->wdd, 0, dev); |
1a4aaf9f | 158 | watchdog_set_nowayout(&priv->wdd, nowayout); |
25134eaf | 159 | |
28e805b4 DR |
160 | watchdog_stop_on_reboot(&priv->wdd); |
161 | ||
ba804a95 | 162 | if (priv->always_running) |
03bca158 | 163 | gpio_wdt_start(&priv->wdd); |
ba804a95 | 164 | |
3564fbc5 | 165 | return devm_watchdog_register_device(dev, &priv->wdd); |
25134eaf AS |
166 | } |
167 | ||
168 | static const struct of_device_id gpio_wdt_dt_ids[] = { | |
169 | { .compatible = "linux,wdt-gpio", }, | |
170 | { } | |
171 | }; | |
172 | MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids); | |
173 | ||
174 | static struct platform_driver gpio_wdt_driver = { | |
175 | .driver = { | |
176 | .name = "gpio-wdt", | |
25134eaf AS |
177 | .of_match_table = gpio_wdt_dt_ids, |
178 | }, | |
179 | .probe = gpio_wdt_probe, | |
25134eaf | 180 | }; |
5e53c8ed JBT |
181 | |
182 | #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL | |
183 | static int __init gpio_wdt_init(void) | |
184 | { | |
185 | return platform_driver_register(&gpio_wdt_driver); | |
186 | } | |
187 | arch_initcall(gpio_wdt_init); | |
188 | #else | |
25134eaf | 189 | module_platform_driver(gpio_wdt_driver); |
5e53c8ed | 190 | #endif |
25134eaf AS |
191 | |
192 | MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); | |
193 | MODULE_DESCRIPTION("GPIO Watchdog"); | |
194 | MODULE_LICENSE("GPL"); |