Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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