xref: /openbmc/linux/drivers/watchdog/gpio_wdt.c (revision a2363f9d2f34bbfd9899b3a3b3cc7022e0f85724)
125134eafSAlexander Shiyan /*
225134eafSAlexander Shiyan  * Driver for watchdog device controlled through GPIO-line
325134eafSAlexander Shiyan  *
425134eafSAlexander Shiyan  * Author: 2013, Alexander Shiyan <shc_work@mail.ru>
525134eafSAlexander Shiyan  *
625134eafSAlexander Shiyan  * This program is free software; you can redistribute it and/or modify
725134eafSAlexander Shiyan  * it under the terms of the GNU General Public License as published by
825134eafSAlexander Shiyan  * the Free Software Foundation; either version 2 of the License, or
925134eafSAlexander Shiyan  * (at your option) any later version.
1025134eafSAlexander Shiyan  */
1125134eafSAlexander Shiyan 
1225134eafSAlexander Shiyan #include <linux/err.h>
1325134eafSAlexander Shiyan #include <linux/delay.h>
1425134eafSAlexander Shiyan #include <linux/module.h>
15*a2363f9dSLinus Walleij #include <linux/gpio/consumer.h>
16*a2363f9dSLinus Walleij #include <linux/of.h>
1725134eafSAlexander Shiyan #include <linux/platform_device.h>
1825134eafSAlexander Shiyan #include <linux/watchdog.h>
1925134eafSAlexander Shiyan 
2025134eafSAlexander Shiyan #define SOFT_TIMEOUT_MIN	1
2125134eafSAlexander Shiyan #define SOFT_TIMEOUT_DEF	60
2225134eafSAlexander Shiyan 
2325134eafSAlexander Shiyan enum {
2425134eafSAlexander Shiyan 	HW_ALGO_TOGGLE,
2525134eafSAlexander Shiyan 	HW_ALGO_LEVEL,
2625134eafSAlexander Shiyan };
2725134eafSAlexander Shiyan 
2825134eafSAlexander Shiyan struct gpio_wdt_priv {
29*a2363f9dSLinus Walleij 	struct gpio_desc	*gpiod;
3025134eafSAlexander Shiyan 	bool			state;
31ba804a95SMike Looijmans 	bool			always_running;
3225134eafSAlexander Shiyan 	unsigned int		hw_algo;
3325134eafSAlexander Shiyan 	struct watchdog_device	wdd;
3425134eafSAlexander Shiyan };
3525134eafSAlexander Shiyan 
3625134eafSAlexander Shiyan static void gpio_wdt_disable(struct gpio_wdt_priv *priv)
3725134eafSAlexander Shiyan {
38*a2363f9dSLinus Walleij 	/* Eternal ping */
39*a2363f9dSLinus Walleij 	gpiod_set_value_cansleep(priv->gpiod, 1);
4025134eafSAlexander Shiyan 
4125134eafSAlexander Shiyan 	/* Put GPIO back to tristate */
4225134eafSAlexander Shiyan 	if (priv->hw_algo == HW_ALGO_TOGGLE)
43*a2363f9dSLinus Walleij 		gpiod_direction_input(priv->gpiod);
4425134eafSAlexander Shiyan }
4525134eafSAlexander Shiyan 
4603bca158SGuenter Roeck static int gpio_wdt_ping(struct watchdog_device *wdd)
474f2d0b2dSUwe Kleine-König {
484f2d0b2dSUwe Kleine-König 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
494f2d0b2dSUwe Kleine-König 
504f2d0b2dSUwe Kleine-König 	switch (priv->hw_algo) {
514f2d0b2dSUwe Kleine-König 	case HW_ALGO_TOGGLE:
524f2d0b2dSUwe Kleine-König 		/* Toggle output pin */
534f2d0b2dSUwe Kleine-König 		priv->state = !priv->state;
54*a2363f9dSLinus Walleij 		gpiod_set_value_cansleep(priv->gpiod, priv->state);
554f2d0b2dSUwe Kleine-König 		break;
564f2d0b2dSUwe Kleine-König 	case HW_ALGO_LEVEL:
574f2d0b2dSUwe Kleine-König 		/* Pulse */
58*a2363f9dSLinus Walleij 		gpiod_set_value_cansleep(priv->gpiod, 1);
594f2d0b2dSUwe Kleine-König 		udelay(1);
60*a2363f9dSLinus Walleij 		gpiod_set_value_cansleep(priv->gpiod, 0);
614f2d0b2dSUwe Kleine-König 		break;
624f2d0b2dSUwe Kleine-König 	}
6303bca158SGuenter Roeck 	return 0;
64ba804a95SMike Looijmans }
65ba804a95SMike Looijmans 
66ba804a95SMike Looijmans static int gpio_wdt_start(struct watchdog_device *wdd)
67ba804a95SMike Looijmans {
68ba804a95SMike Looijmans 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
69ba804a95SMike Looijmans 
70*a2363f9dSLinus Walleij 	priv->state = 0;
71*a2363f9dSLinus Walleij 	gpiod_direction_output(priv->gpiod, priv->state);
7225134eafSAlexander Shiyan 
7303bca158SGuenter Roeck 	set_bit(WDOG_HW_RUNNING, &wdd->status);
7403bca158SGuenter Roeck 
7503bca158SGuenter Roeck 	return gpio_wdt_ping(wdd);
7625134eafSAlexander Shiyan }
7725134eafSAlexander Shiyan 
7825134eafSAlexander Shiyan static int gpio_wdt_stop(struct watchdog_device *wdd)
7925134eafSAlexander Shiyan {
8025134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
8125134eafSAlexander Shiyan 
82ba804a95SMike Looijmans 	if (!priv->always_running) {
8325134eafSAlexander Shiyan 		gpio_wdt_disable(priv);
8403bca158SGuenter Roeck 		clear_bit(WDOG_HW_RUNNING, &wdd->status);
85ba804a95SMike Looijmans 	}
8625134eafSAlexander Shiyan 
8725134eafSAlexander Shiyan 	return 0;
8825134eafSAlexander Shiyan }
8925134eafSAlexander Shiyan 
9025134eafSAlexander Shiyan static const struct watchdog_info gpio_wdt_ident = {
9125134eafSAlexander Shiyan 	.options	= WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
9225134eafSAlexander Shiyan 			  WDIOF_SETTIMEOUT,
9325134eafSAlexander Shiyan 	.identity	= "GPIO Watchdog",
9425134eafSAlexander Shiyan };
9525134eafSAlexander Shiyan 
9625134eafSAlexander Shiyan static const struct watchdog_ops gpio_wdt_ops = {
9725134eafSAlexander Shiyan 	.owner		= THIS_MODULE,
9825134eafSAlexander Shiyan 	.start		= gpio_wdt_start,
9925134eafSAlexander Shiyan 	.stop		= gpio_wdt_stop,
10025134eafSAlexander Shiyan 	.ping		= gpio_wdt_ping,
10125134eafSAlexander Shiyan };
10225134eafSAlexander Shiyan 
10325134eafSAlexander Shiyan static int gpio_wdt_probe(struct platform_device *pdev)
10425134eafSAlexander Shiyan {
105d0d0677eSLinus Walleij 	struct device *dev = &pdev->dev;
106d0d0677eSLinus Walleij 	struct device_node *np = dev->of_node;
10725134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv;
108*a2363f9dSLinus Walleij 	enum gpiod_flags gflags;
10925134eafSAlexander Shiyan 	unsigned int hw_margin;
11025134eafSAlexander Shiyan 	const char *algo;
11125134eafSAlexander Shiyan 	int ret;
11225134eafSAlexander Shiyan 
113d0d0677eSLinus Walleij 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
11425134eafSAlexander Shiyan 	if (!priv)
11525134eafSAlexander Shiyan 		return -ENOMEM;
11625134eafSAlexander Shiyan 
1171ac06563SWei Yongjun 	platform_set_drvdata(pdev, priv);
1181ac06563SWei Yongjun 
119d0d0677eSLinus Walleij 	ret = of_property_read_string(np, "hw_algo", &algo);
12025134eafSAlexander Shiyan 	if (ret)
12125134eafSAlexander Shiyan 		return ret;
1220a0a542fSUwe Kleine-König 	if (!strcmp(algo, "toggle")) {
12325134eafSAlexander Shiyan 		priv->hw_algo = HW_ALGO_TOGGLE;
124*a2363f9dSLinus Walleij 		gflags = GPIOD_IN;
1250a0a542fSUwe Kleine-König 	} else if (!strcmp(algo, "level")) {
12625134eafSAlexander Shiyan 		priv->hw_algo = HW_ALGO_LEVEL;
127*a2363f9dSLinus Walleij 		gflags = GPIOD_OUT_LOW;
12825134eafSAlexander Shiyan 	} else {
12925134eafSAlexander Shiyan 		return -EINVAL;
13025134eafSAlexander Shiyan 	}
13125134eafSAlexander Shiyan 
132*a2363f9dSLinus Walleij 	priv->gpiod = devm_gpiod_get(dev, NULL, gflags);
133*a2363f9dSLinus Walleij 	if (IS_ERR(priv->gpiod))
134*a2363f9dSLinus Walleij 		return PTR_ERR(priv->gpiod);
13525134eafSAlexander Shiyan 
136d0d0677eSLinus Walleij 	ret = of_property_read_u32(np,
13725134eafSAlexander Shiyan 				   "hw_margin_ms", &hw_margin);
13825134eafSAlexander Shiyan 	if (ret)
13925134eafSAlexander Shiyan 		return ret;
14025134eafSAlexander Shiyan 	/* Disallow values lower than 2 and higher than 65535 ms */
14125134eafSAlexander Shiyan 	if (hw_margin < 2 || hw_margin > 65535)
14225134eafSAlexander Shiyan 		return -EINVAL;
14325134eafSAlexander Shiyan 
144d0d0677eSLinus Walleij 	priv->always_running = of_property_read_bool(np,
145ba804a95SMike Looijmans 						     "always-running");
146ba804a95SMike Looijmans 
14725134eafSAlexander Shiyan 	watchdog_set_drvdata(&priv->wdd, priv);
14825134eafSAlexander Shiyan 
14925134eafSAlexander Shiyan 	priv->wdd.info		= &gpio_wdt_ident;
15025134eafSAlexander Shiyan 	priv->wdd.ops		= &gpio_wdt_ops;
15125134eafSAlexander Shiyan 	priv->wdd.min_timeout	= SOFT_TIMEOUT_MIN;
15203bca158SGuenter Roeck 	priv->wdd.max_hw_heartbeat_ms = hw_margin;
153d0d0677eSLinus Walleij 	priv->wdd.parent	= dev;
15425134eafSAlexander Shiyan 
155d0d0677eSLinus Walleij 	if (watchdog_init_timeout(&priv->wdd, 0, dev) < 0)
15625134eafSAlexander Shiyan 		priv->wdd.timeout = SOFT_TIMEOUT_DEF;
15725134eafSAlexander Shiyan 
15828e805b4SDamien Riegel 	watchdog_stop_on_reboot(&priv->wdd);
15928e805b4SDamien Riegel 
160ba804a95SMike Looijmans 	if (priv->always_running)
16103bca158SGuenter Roeck 		gpio_wdt_start(&priv->wdd);
162ba804a95SMike Looijmans 
16303bca158SGuenter Roeck 	ret = watchdog_register_device(&priv->wdd);
16403bca158SGuenter Roeck 
16503bca158SGuenter Roeck 	return ret;
16625134eafSAlexander Shiyan }
16725134eafSAlexander Shiyan 
16825134eafSAlexander Shiyan static int gpio_wdt_remove(struct platform_device *pdev)
16925134eafSAlexander Shiyan {
17025134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv = platform_get_drvdata(pdev);
17125134eafSAlexander Shiyan 
17225134eafSAlexander Shiyan 	watchdog_unregister_device(&priv->wdd);
17325134eafSAlexander Shiyan 
17425134eafSAlexander Shiyan 	return 0;
17525134eafSAlexander Shiyan }
17625134eafSAlexander Shiyan 
17725134eafSAlexander Shiyan static const struct of_device_id gpio_wdt_dt_ids[] = {
17825134eafSAlexander Shiyan 	{ .compatible = "linux,wdt-gpio", },
17925134eafSAlexander Shiyan 	{ }
18025134eafSAlexander Shiyan };
18125134eafSAlexander Shiyan MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
18225134eafSAlexander Shiyan 
18325134eafSAlexander Shiyan static struct platform_driver gpio_wdt_driver = {
18425134eafSAlexander Shiyan 	.driver	= {
18525134eafSAlexander Shiyan 		.name		= "gpio-wdt",
18625134eafSAlexander Shiyan 		.of_match_table	= gpio_wdt_dt_ids,
18725134eafSAlexander Shiyan 	},
18825134eafSAlexander Shiyan 	.probe	= gpio_wdt_probe,
18925134eafSAlexander Shiyan 	.remove	= gpio_wdt_remove,
19025134eafSAlexander Shiyan };
1915e53c8edSJean-Baptiste Theou 
1925e53c8edSJean-Baptiste Theou #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
1935e53c8edSJean-Baptiste Theou static int __init gpio_wdt_init(void)
1945e53c8edSJean-Baptiste Theou {
1955e53c8edSJean-Baptiste Theou 	return platform_driver_register(&gpio_wdt_driver);
1965e53c8edSJean-Baptiste Theou }
1975e53c8edSJean-Baptiste Theou arch_initcall(gpio_wdt_init);
1985e53c8edSJean-Baptiste Theou #else
19925134eafSAlexander Shiyan module_platform_driver(gpio_wdt_driver);
2005e53c8edSJean-Baptiste Theou #endif
20125134eafSAlexander Shiyan 
20225134eafSAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
20325134eafSAlexander Shiyan MODULE_DESCRIPTION("GPIO Watchdog");
20425134eafSAlexander Shiyan MODULE_LICENSE("GPL");
205