xref: /openbmc/linux/drivers/watchdog/gpio_wdt.c (revision 03bca15833f2865b11835b7f5bfa594d1aaacecc)
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>
1525134eafSAlexander Shiyan #include <linux/of_gpio.h>
1625134eafSAlexander Shiyan #include <linux/platform_device.h>
1725134eafSAlexander Shiyan #include <linux/watchdog.h>
1825134eafSAlexander Shiyan 
1925134eafSAlexander Shiyan #define SOFT_TIMEOUT_MIN	1
2025134eafSAlexander Shiyan #define SOFT_TIMEOUT_DEF	60
2125134eafSAlexander Shiyan 
2225134eafSAlexander Shiyan enum {
2325134eafSAlexander Shiyan 	HW_ALGO_TOGGLE,
2425134eafSAlexander Shiyan 	HW_ALGO_LEVEL,
2525134eafSAlexander Shiyan };
2625134eafSAlexander Shiyan 
2725134eafSAlexander Shiyan struct gpio_wdt_priv {
2825134eafSAlexander Shiyan 	int			gpio;
2925134eafSAlexander Shiyan 	bool			active_low;
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 {
3825134eafSAlexander Shiyan 	gpio_set_value_cansleep(priv->gpio, !priv->active_low);
3925134eafSAlexander Shiyan 
4025134eafSAlexander Shiyan 	/* Put GPIO back to tristate */
4125134eafSAlexander Shiyan 	if (priv->hw_algo == HW_ALGO_TOGGLE)
4225134eafSAlexander Shiyan 		gpio_direction_input(priv->gpio);
4325134eafSAlexander Shiyan }
4425134eafSAlexander Shiyan 
45*03bca158SGuenter Roeck static int gpio_wdt_ping(struct watchdog_device *wdd)
464f2d0b2dSUwe Kleine-König {
474f2d0b2dSUwe Kleine-König 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
484f2d0b2dSUwe Kleine-König 
494f2d0b2dSUwe Kleine-König 	switch (priv->hw_algo) {
504f2d0b2dSUwe Kleine-König 	case HW_ALGO_TOGGLE:
514f2d0b2dSUwe Kleine-König 		/* Toggle output pin */
524f2d0b2dSUwe Kleine-König 		priv->state = !priv->state;
534f2d0b2dSUwe Kleine-König 		gpio_set_value_cansleep(priv->gpio, priv->state);
544f2d0b2dSUwe Kleine-König 		break;
554f2d0b2dSUwe Kleine-König 	case HW_ALGO_LEVEL:
564f2d0b2dSUwe Kleine-König 		/* Pulse */
574f2d0b2dSUwe Kleine-König 		gpio_set_value_cansleep(priv->gpio, !priv->active_low);
584f2d0b2dSUwe Kleine-König 		udelay(1);
594f2d0b2dSUwe Kleine-König 		gpio_set_value_cansleep(priv->gpio, priv->active_low);
604f2d0b2dSUwe Kleine-König 		break;
614f2d0b2dSUwe Kleine-König 	}
62*03bca158SGuenter Roeck 	return 0;
63ba804a95SMike Looijmans }
64ba804a95SMike Looijmans 
65ba804a95SMike Looijmans static int gpio_wdt_start(struct watchdog_device *wdd)
66ba804a95SMike Looijmans {
67ba804a95SMike Looijmans 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
68ba804a95SMike Looijmans 
69*03bca158SGuenter Roeck 	priv->state = priv->active_low;
70*03bca158SGuenter Roeck 	gpio_direction_output(priv->gpio, priv->state);
7125134eafSAlexander Shiyan 
72*03bca158SGuenter Roeck 	set_bit(WDOG_HW_RUNNING, &wdd->status);
73*03bca158SGuenter Roeck 
74*03bca158SGuenter Roeck 	return gpio_wdt_ping(wdd);
7525134eafSAlexander Shiyan }
7625134eafSAlexander Shiyan 
7725134eafSAlexander Shiyan static int gpio_wdt_stop(struct watchdog_device *wdd)
7825134eafSAlexander Shiyan {
7925134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv = watchdog_get_drvdata(wdd);
8025134eafSAlexander Shiyan 
81ba804a95SMike Looijmans 	if (!priv->always_running) {
8225134eafSAlexander Shiyan 		gpio_wdt_disable(priv);
83*03bca158SGuenter Roeck 		clear_bit(WDOG_HW_RUNNING, &wdd->status);
84ba804a95SMike Looijmans 	}
8525134eafSAlexander Shiyan 
8625134eafSAlexander Shiyan 	return 0;
8725134eafSAlexander Shiyan }
8825134eafSAlexander Shiyan 
8925134eafSAlexander Shiyan static const struct watchdog_info gpio_wdt_ident = {
9025134eafSAlexander Shiyan 	.options	= WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING |
9125134eafSAlexander Shiyan 			  WDIOF_SETTIMEOUT,
9225134eafSAlexander Shiyan 	.identity	= "GPIO Watchdog",
9325134eafSAlexander Shiyan };
9425134eafSAlexander Shiyan 
9525134eafSAlexander Shiyan static const struct watchdog_ops gpio_wdt_ops = {
9625134eafSAlexander Shiyan 	.owner		= THIS_MODULE,
9725134eafSAlexander Shiyan 	.start		= gpio_wdt_start,
9825134eafSAlexander Shiyan 	.stop		= gpio_wdt_stop,
9925134eafSAlexander Shiyan 	.ping		= gpio_wdt_ping,
10025134eafSAlexander Shiyan };
10125134eafSAlexander Shiyan 
10225134eafSAlexander Shiyan static int gpio_wdt_probe(struct platform_device *pdev)
10325134eafSAlexander Shiyan {
10425134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv;
10525134eafSAlexander Shiyan 	enum of_gpio_flags flags;
10625134eafSAlexander Shiyan 	unsigned int hw_margin;
10725134eafSAlexander Shiyan 	unsigned long f = 0;
10825134eafSAlexander Shiyan 	const char *algo;
10925134eafSAlexander Shiyan 	int ret;
11025134eafSAlexander Shiyan 
11125134eafSAlexander Shiyan 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
11225134eafSAlexander Shiyan 	if (!priv)
11325134eafSAlexander Shiyan 		return -ENOMEM;
11425134eafSAlexander Shiyan 
1151ac06563SWei Yongjun 	platform_set_drvdata(pdev, priv);
1161ac06563SWei Yongjun 
11725134eafSAlexander Shiyan 	priv->gpio = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
11825134eafSAlexander Shiyan 	if (!gpio_is_valid(priv->gpio))
11925134eafSAlexander Shiyan 		return priv->gpio;
12025134eafSAlexander Shiyan 
12125134eafSAlexander Shiyan 	priv->active_low = flags & OF_GPIO_ACTIVE_LOW;
12225134eafSAlexander Shiyan 
12325134eafSAlexander Shiyan 	ret = of_property_read_string(pdev->dev.of_node, "hw_algo", &algo);
12425134eafSAlexander Shiyan 	if (ret)
12525134eafSAlexander Shiyan 		return ret;
1260a0a542fSUwe Kleine-König 	if (!strcmp(algo, "toggle")) {
12725134eafSAlexander Shiyan 		priv->hw_algo = HW_ALGO_TOGGLE;
12825134eafSAlexander Shiyan 		f = GPIOF_IN;
1290a0a542fSUwe Kleine-König 	} else if (!strcmp(algo, "level")) {
13025134eafSAlexander Shiyan 		priv->hw_algo = HW_ALGO_LEVEL;
13125134eafSAlexander Shiyan 		f = priv->active_low ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
13225134eafSAlexander Shiyan 	} else {
13325134eafSAlexander Shiyan 		return -EINVAL;
13425134eafSAlexander Shiyan 	}
13525134eafSAlexander Shiyan 
13625134eafSAlexander Shiyan 	ret = devm_gpio_request_one(&pdev->dev, priv->gpio, f,
13725134eafSAlexander Shiyan 				    dev_name(&pdev->dev));
13825134eafSAlexander Shiyan 	if (ret)
13925134eafSAlexander Shiyan 		return ret;
14025134eafSAlexander Shiyan 
14125134eafSAlexander Shiyan 	ret = of_property_read_u32(pdev->dev.of_node,
14225134eafSAlexander Shiyan 				   "hw_margin_ms", &hw_margin);
14325134eafSAlexander Shiyan 	if (ret)
14425134eafSAlexander Shiyan 		return ret;
14525134eafSAlexander Shiyan 	/* Disallow values lower than 2 and higher than 65535 ms */
14625134eafSAlexander Shiyan 	if (hw_margin < 2 || hw_margin > 65535)
14725134eafSAlexander Shiyan 		return -EINVAL;
14825134eafSAlexander Shiyan 
149ba804a95SMike Looijmans 	priv->always_running = of_property_read_bool(pdev->dev.of_node,
150ba804a95SMike Looijmans 						     "always-running");
151ba804a95SMike Looijmans 
15225134eafSAlexander Shiyan 	watchdog_set_drvdata(&priv->wdd, priv);
15325134eafSAlexander Shiyan 
15425134eafSAlexander Shiyan 	priv->wdd.info		= &gpio_wdt_ident;
15525134eafSAlexander Shiyan 	priv->wdd.ops		= &gpio_wdt_ops;
15625134eafSAlexander Shiyan 	priv->wdd.min_timeout	= SOFT_TIMEOUT_MIN;
157*03bca158SGuenter Roeck 	priv->wdd.max_hw_heartbeat_ms = hw_margin;
1586551881cSPratyush Anand 	priv->wdd.parent	= &pdev->dev;
15925134eafSAlexander Shiyan 
16025134eafSAlexander Shiyan 	if (watchdog_init_timeout(&priv->wdd, 0, &pdev->dev) < 0)
16125134eafSAlexander Shiyan 		priv->wdd.timeout = SOFT_TIMEOUT_DEF;
16225134eafSAlexander Shiyan 
16328e805b4SDamien Riegel 	watchdog_stop_on_reboot(&priv->wdd);
16428e805b4SDamien Riegel 
165ba804a95SMike Looijmans 	if (priv->always_running)
166*03bca158SGuenter Roeck 		gpio_wdt_start(&priv->wdd);
167ba804a95SMike Looijmans 
168*03bca158SGuenter Roeck 	ret = watchdog_register_device(&priv->wdd);
169*03bca158SGuenter Roeck 
170*03bca158SGuenter Roeck 	return ret;
17125134eafSAlexander Shiyan }
17225134eafSAlexander Shiyan 
17325134eafSAlexander Shiyan static int gpio_wdt_remove(struct platform_device *pdev)
17425134eafSAlexander Shiyan {
17525134eafSAlexander Shiyan 	struct gpio_wdt_priv *priv = platform_get_drvdata(pdev);
17625134eafSAlexander Shiyan 
17725134eafSAlexander Shiyan 	watchdog_unregister_device(&priv->wdd);
17825134eafSAlexander Shiyan 
17925134eafSAlexander Shiyan 	return 0;
18025134eafSAlexander Shiyan }
18125134eafSAlexander Shiyan 
18225134eafSAlexander Shiyan static const struct of_device_id gpio_wdt_dt_ids[] = {
18325134eafSAlexander Shiyan 	{ .compatible = "linux,wdt-gpio", },
18425134eafSAlexander Shiyan 	{ }
18525134eafSAlexander Shiyan };
18625134eafSAlexander Shiyan MODULE_DEVICE_TABLE(of, gpio_wdt_dt_ids);
18725134eafSAlexander Shiyan 
18825134eafSAlexander Shiyan static struct platform_driver gpio_wdt_driver = {
18925134eafSAlexander Shiyan 	.driver	= {
19025134eafSAlexander Shiyan 		.name		= "gpio-wdt",
19125134eafSAlexander Shiyan 		.of_match_table	= gpio_wdt_dt_ids,
19225134eafSAlexander Shiyan 	},
19325134eafSAlexander Shiyan 	.probe	= gpio_wdt_probe,
19425134eafSAlexander Shiyan 	.remove	= gpio_wdt_remove,
19525134eafSAlexander Shiyan };
1965e53c8edSJean-Baptiste Theou 
1975e53c8edSJean-Baptiste Theou #ifdef CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
1985e53c8edSJean-Baptiste Theou static int __init gpio_wdt_init(void)
1995e53c8edSJean-Baptiste Theou {
2005e53c8edSJean-Baptiste Theou 	return platform_driver_register(&gpio_wdt_driver);
2015e53c8edSJean-Baptiste Theou }
2025e53c8edSJean-Baptiste Theou arch_initcall(gpio_wdt_init);
2035e53c8edSJean-Baptiste Theou #else
20425134eafSAlexander Shiyan module_platform_driver(gpio_wdt_driver);
2055e53c8edSJean-Baptiste Theou #endif
20625134eafSAlexander Shiyan 
20725134eafSAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
20825134eafSAlexander Shiyan MODULE_DESCRIPTION("GPIO Watchdog");
20925134eafSAlexander Shiyan MODULE_LICENSE("GPL");
210