xref: /openbmc/linux/drivers/watchdog/gpio_wdt.c (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
225134eafSAlexander Shiyan /*
325134eafSAlexander Shiyan  * Driver for watchdog device controlled through GPIO-line
425134eafSAlexander Shiyan  *
525134eafSAlexander Shiyan  * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
625134eafSAlexander Shiyan  */
725134eafSAlexander Shiyan 
825134eafSAlexander Shiyan #include <linux/err.h>
925134eafSAlexander Shiyan #include <linux/delay.h>
1025134eafSAlexander Shiyan #include <linux/module.h>
11a2363f9dSLinus Walleij #include <linux/gpio/consumer.h>
12a2363f9dSLinus Walleij #include <linux/of.h>
1325134eafSAlexander Shiyan #include <linux/platform_device.h>
1425134eafSAlexander Shiyan #include <linux/watchdog.h>
1525134eafSAlexander Shiyan 
16*1a4aaf9fSMans Rullgard static bool nowayout = WATCHDOG_NOWAYOUT;
17*1a4aaf9fSMans Rullgard module_param(nowayout, bool, 0);
18*1a4aaf9fSMans Rullgard MODULE_PARM_DESC(nowayout,
19*1a4aaf9fSMans Rullgard 		"Watchdog cannot be stopped once started (default="
20*1a4aaf9fSMans Rullgard 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
21*1a4aaf9fSMans Rullgard 
2225134eafSAlexander Shiyan #define SOFT_TIMEOUT_MIN	1
2325134eafSAlexander Shiyan #define SOFT_TIMEOUT_DEF	60
2425134eafSAlexander Shiyan 
2525134eafSAlexander Shiyan enum {
2625134eafSAlexander Shiyan 	HW_ALGO_TOGGLE,
2725134eafSAlexander Shiyan 	HW_ALGO_LEVEL,
2825134eafSAlexander Shiyan };
2925134eafSAlexander Shiyan 
3025134eafSAlexander Shiyan struct gpio_wdt_priv {
31a2363f9dSLinus Walleij 	struct gpio_desc	*gpiod;
3225134eafSAlexander Shiyan 	bool			state;
33ba804a95SMike Looijmans 	bool			always_running;
3425134eafSAlexander Shiyan 	unsigned int		hw_algo;
3525134eafSAlexander Shiyan 	struct watchdog_device	wdd;
3625134eafSAlexander Shiyan };
3725134eafSAlexander Shiyan 
gpio_wdt_disable(struct gpio_wdt_priv * priv)3825134eafSAlexander Shiyan static void gpio_wdt_disable(struct gpio_wdt_priv *priv)
3925134eafSAlexander Shiyan {
40a2363f9dSLinus Walleij 	/* Eternal ping */
41a2363f9dSLinus Walleij 	gpiod_set_value_cansleep(priv->gpiod, 1);
4225134eafSAlexander Shiyan 
4325134eafSAlexander Shiyan 	/* Put GPIO back to tristate */
4425134eafSAlexander Shiyan 	if (priv->hw_algo == HW_ALGO_TOGGLE)
45a2363f9dSLinus Walleij 		gpiod_direction_input(priv->gpiod);
4625134eafSAlexander Shiyan }
4725134eafSAlexander Shiyan 
gpio_wdt_ping(struct watchdog_device * wdd)4803bca158SGuenter Roeck static int gpio_wdt_ping(struct watchdog_device *wdd)
494f2d0b2dSUwe Kleine-König {
504f2d0b2dSUwe Kleine-König 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
514f2d0b2dSUwe Kleine-König 
524f2d0b2dSUwe Kleine-König 	switch (priv->hw_algo) {
534f2d0b2dSUwe Kleine-König 	case HW_ALGO_TOGGLE:
544f2d0b2dSUwe Kleine-König 		/* Toggle output pin */
554f2d0b2dSUwe Kleine-König 		priv->state = !priv->state;
56a2363f9dSLinus Walleij 		gpiod_set_value_cansleep(priv->gpiod, priv->state);
574f2d0b2dSUwe Kleine-König 		break;
584f2d0b2dSUwe Kleine-König 	case HW_ALGO_LEVEL:
594f2d0b2dSUwe Kleine-König 		/* Pulse */
60a2363f9dSLinus Walleij 		gpiod_set_value_cansleep(priv->gpiod, 1);
614f2d0b2dSUwe Kleine-König 		udelay(1);
62a2363f9dSLinus Walleij 		gpiod_set_value_cansleep(priv->gpiod, 0);
634f2d0b2dSUwe Kleine-König 		break;
644f2d0b2dSUwe Kleine-König 	}
6503bca158SGuenter Roeck 	return 0;
66ba804a95SMike Looijmans }
67ba804a95SMike Looijmans 
gpio_wdt_start(struct watchdog_device * wdd)68ba804a95SMike Looijmans static int gpio_wdt_start(struct watchdog_device *wdd)
69ba804a95SMike Looijmans {
70ba804a95SMike Looijmans 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
71ba804a95SMike Looijmans 
72a2363f9dSLinus Walleij 	priv->state = 0;
73a2363f9dSLinus Walleij 	gpiod_direction_output(priv->gpiod, priv->state);
7425134eafSAlexander Shiyan 
7503bca158SGuenter Roeck 	set_bit(WDOG_HW_RUNNING, &wdd->status);
7603bca158SGuenter Roeck 
7703bca158SGuenter Roeck 	return gpio_wdt_ping(wdd);
7825134eafSAlexander Shiyan }
7925134eafSAlexander Shiyan 
gpio_wdt_stop(struct watchdog_device * wdd)8025134eafSAlexander Shiyan static int gpio_wdt_stop(struct watchdog_device *wdd)
8125134eafSAlexander Shiyan {
8225134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
8325134eafSAlexander Shiyan 
84ba804a95SMike Looijmans 	if (!priv->always_running) {
8525134eafSAlexander Shiyan 		gpio_wdt_disable(priv);
86bc137dfdSRasmus Villemoes 	} else {
87bc137dfdSRasmus Villemoes 		set_bit(WDOG_HW_RUNNING, &wdd->status);
88ba804a95SMike Looijmans 	}
8925134eafSAlexander Shiyan 
9025134eafSAlexander Shiyan 	return 0;
9125134eafSAlexander Shiyan }
9225134eafSAlexander Shiyan 
9325134eafSAlexander Shiyan static const struct watchdog_info gpio_wdt_ident = {
9425134eafSAlexander Shiyan 	.options	= WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
9525134eafSAlexander Shiyan 			  WDIOF_SETTIMEOUT,
9625134eafSAlexander Shiyan 	.identity	= "GPIO Watchdog",
9725134eafSAlexander Shiyan };
9825134eafSAlexander Shiyan 
9925134eafSAlexander Shiyan static const struct watchdog_ops gpio_wdt_ops = {
10025134eafSAlexander Shiyan 	.owner		= THIS_MODULE,
10125134eafSAlexander Shiyan 	.start		= gpio_wdt_start,
10225134eafSAlexander Shiyan 	.stop		= gpio_wdt_stop,
10325134eafSAlexander Shiyan 	.ping		= gpio_wdt_ping,
10425134eafSAlexander Shiyan };
10525134eafSAlexander Shiyan 
gpio_wdt_probe(struct platform_device * pdev)10625134eafSAlexander Shiyan static int gpio_wdt_probe(struct platform_device *pdev)
10725134eafSAlexander Shiyan {
108d0d0677eSLinus Walleij 	struct device *dev = &pdev->dev;
109d0d0677eSLinus Walleij 	struct device_node *np = dev->of_node;
11025134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv;
111a2363f9dSLinus Walleij 	enum gpiod_flags gflags;
11225134eafSAlexander Shiyan 	unsigned int hw_margin;
11325134eafSAlexander Shiyan 	const char *algo;
11425134eafSAlexander Shiyan 	int ret;
11525134eafSAlexander Shiyan 
116d0d0677eSLinus Walleij 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
11725134eafSAlexander Shiyan 	if (!priv)
11825134eafSAlexander Shiyan 		return -ENOMEM;
11925134eafSAlexander Shiyan 
1201ac06563SWei Yongjun 	platform_set_drvdata(pdev, priv);
1211ac06563SWei Yongjun 
122d0d0677eSLinus Walleij 	ret = of_property_read_string(np, "hw_algo", &algo);
12325134eafSAlexander Shiyan 	if (ret)
12425134eafSAlexander Shiyan 		return ret;
1250a0a542fSUwe Kleine-König 	if (!strcmp(algo, "toggle")) {
12625134eafSAlexander Shiyan 		priv->hw_algo = HW_ALGO_TOGGLE;
127a2363f9dSLinus Walleij 		gflags = GPIOD_IN;
1280a0a542fSUwe Kleine-König 	} else if (!strcmp(algo, "level")) {
12925134eafSAlexander Shiyan 		priv->hw_algo = HW_ALGO_LEVEL;
130a2363f9dSLinus Walleij 		gflags = GPIOD_OUT_LOW;
13125134eafSAlexander Shiyan 	} else {
13225134eafSAlexander Shiyan 		return -EINVAL;
13325134eafSAlexander Shiyan 	}
13425134eafSAlexander Shiyan 
135a2363f9dSLinus Walleij 	priv->gpiod = devm_gpiod_get(dev, NULL, gflags);
136a2363f9dSLinus Walleij 	if (IS_ERR(priv->gpiod))
137a2363f9dSLinus Walleij 		return PTR_ERR(priv->gpiod);
13825134eafSAlexander Shiyan 
139d0d0677eSLinus Walleij 	ret = of_property_read_u32(np,
14025134eafSAlexander Shiyan 				   "hw_margin_ms", &hw_margin);
14125134eafSAlexander Shiyan 	if (ret)
14225134eafSAlexander Shiyan 		return ret;
14325134eafSAlexander Shiyan 	/* Disallow values lower than 2 and higher than 65535 ms */
14425134eafSAlexander Shiyan 	if (hw_margin < 2 || hw_margin > 65535)
14525134eafSAlexander Shiyan 		return -EINVAL;
14625134eafSAlexander Shiyan 
147d0d0677eSLinus Walleij 	priv->always_running = of_property_read_bool(np,
148ba804a95SMike Looijmans 						     "always-running");
149ba804a95SMike Looijmans 
15025134eafSAlexander Shiyan 	watchdog_set_drvdata(&priv->wdd, priv);
15125134eafSAlexander Shiyan 
15225134eafSAlexander Shiyan 	priv->wdd.info		= &gpio_wdt_ident;
15325134eafSAlexander Shiyan 	priv->wdd.ops		= &gpio_wdt_ops;
15425134eafSAlexander Shiyan 	priv->wdd.min_timeout	= SOFT_TIMEOUT_MIN;
15503bca158SGuenter Roeck 	priv->wdd.max_hw_heartbeat_ms = hw_margin;
156d0d0677eSLinus Walleij 	priv->wdd.parent	= dev;
15725134eafSAlexander Shiyan 	priv->wdd.timeout	= SOFT_TIMEOUT_DEF;
15825134eafSAlexander Shiyan 
1593564fbc5SGuenter Roeck 	watchdog_init_timeout(&priv->wdd, 0, dev);
160*1a4aaf9fSMans Rullgard 	watchdog_set_nowayout(&priv->wdd, nowayout);
16165adfa22SMarcus Folkesson 
16228e805b4SDamien Riegel 	watchdog_stop_on_reboot(&priv->wdd);
16328e805b4SDamien Riegel 
164ba804a95SMike Looijmans 	if (priv->always_running)
16503bca158SGuenter Roeck 		gpio_wdt_start(&priv->wdd);
166ba804a95SMike Looijmans 
1673564fbc5SGuenter Roeck 	return devm_watchdog_register_device(dev, &priv->wdd);
16825134eafSAlexander Shiyan }
16925134eafSAlexander Shiyan 
17025134eafSAlexander Shiyan static const struct of_device_id gpio_wdt_dt_ids[] = {
17125134eafSAlexander Shiyan 	{ .compatible = "linux,wdt-gpio", },
17225134eafSAlexander Shiyan 	{ }
17325134eafSAlexander Shiyan };
17425134eafSAlexander Shiyan MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
17525134eafSAlexander Shiyan 
17625134eafSAlexander Shiyan static struct platform_driver gpio_wdt_driver = {
17725134eafSAlexander Shiyan 	.driver	= {
17825134eafSAlexander Shiyan 		.name		= "gpio-wdt",
17925134eafSAlexander Shiyan 		.of_match_table	= gpio_wdt_dt_ids,
18025134eafSAlexander Shiyan 	},
18125134eafSAlexander Shiyan 	.probe	= gpio_wdt_probe,
18225134eafSAlexander Shiyan };
1835e53c8edSJean-Baptiste Theou 
1845e53c8edSJean-Baptiste Theou #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
gpio_wdt_init(void)1855e53c8edSJean-Baptiste Theou static int __init gpio_wdt_init(void)
1865e53c8edSJean-Baptiste Theou {
1875e53c8edSJean-Baptiste Theou 	return platform_driver_register(&gpio_wdt_driver);
1885e53c8edSJean-Baptiste Theou }
1895e53c8edSJean-Baptiste Theou arch_initcall(gpio_wdt_init);
1905e53c8edSJean-Baptiste Theou #else
19125134eafSAlexander Shiyan module_platform_driver(gpio_wdt_driver);
1925e53c8edSJean-Baptiste Theou #endif
19325134eafSAlexander Shiyan 
19425134eafSAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
19525134eafSAlexander Shiyan MODULE_DESCRIPTION("GPIO Watchdog");
19625134eafSAlexander Shiyan MODULE_LICENSE("GPL");
197