Thomas Gleixner | 0376148 | 2019-05-28 09:57:24 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) ST-Ericsson SA 2011-2013 |
| 4 | * |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 5 | * Author: Mathieu Poirier <mathieu.poirier@linaro.org> for ST-Ericsson |
| 6 | * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/moduleparam.h> |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <linux/watchdog.h> |
| 17 | #include <linux/platform_device.h> |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 18 | |
| 19 | #include <linux/mfd/dbx500-prcmu.h> |
| 20 | |
| 21 | #define WATCHDOG_TIMEOUT 600 /* 10 minutes */ |
| 22 | |
| 23 | #define WATCHDOG_MIN 0 |
| 24 | #define WATCHDOG_MAX28 268435 /* 28 bit resolution in ms == 268435.455 s */ |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 25 | |
| 26 | static unsigned int timeout = WATCHDOG_TIMEOUT; |
| 27 | module_param(timeout, uint, 0); |
| 28 | MODULE_PARM_DESC(timeout, |
| 29 | "Watchdog timeout in seconds. default=" |
| 30 | __MODULE_STRING(WATCHDOG_TIMEOUT) "."); |
| 31 | |
| 32 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 33 | module_param(nowayout, bool, 0); |
| 34 | MODULE_PARM_DESC(nowayout, |
| 35 | "Watchdog cannot be stopped once started (default=" |
| 36 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 37 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 38 | static int db8500_wdt_start(struct watchdog_device *wdd) |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 39 | { |
| 40 | return prcmu_enable_a9wdog(PRCMU_WDOG_ALL); |
| 41 | } |
| 42 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 43 | static int db8500_wdt_stop(struct watchdog_device *wdd) |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 44 | { |
| 45 | return prcmu_disable_a9wdog(PRCMU_WDOG_ALL); |
| 46 | } |
| 47 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 48 | static int db8500_wdt_keepalive(struct watchdog_device *wdd) |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 49 | { |
| 50 | return prcmu_kick_a9wdog(PRCMU_WDOG_ALL); |
| 51 | } |
| 52 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 53 | static int db8500_wdt_set_timeout(struct watchdog_device *wdd, |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 54 | unsigned int timeout) |
| 55 | { |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 56 | db8500_wdt_stop(wdd); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 57 | prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000); |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 58 | db8500_wdt_start(wdd); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 63 | static const struct watchdog_info db8500_wdt_info = { |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 64 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 65 | .identity = "DB8500 WDT", |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 66 | .firmware_version = 1, |
| 67 | }; |
| 68 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 69 | static const struct watchdog_ops db8500_wdt_ops = { |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 70 | .owner = THIS_MODULE, |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 71 | .start = db8500_wdt_start, |
| 72 | .stop = db8500_wdt_stop, |
| 73 | .ping = db8500_wdt_keepalive, |
| 74 | .set_timeout = db8500_wdt_set_timeout, |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 77 | static struct watchdog_device db8500_wdt = { |
| 78 | .info = &db8500_wdt_info, |
| 79 | .ops = &db8500_wdt_ops, |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 80 | .min_timeout = WATCHDOG_MIN, |
Linus Walleij | 74128d8 | 2021-09-23 01:09:45 +0200 | [diff] [blame] | 81 | .max_timeout = WATCHDOG_MAX28, |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 84 | static int db8500_wdt_probe(struct platform_device *pdev) |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 85 | { |
Guenter Roeck | 89e5b7a | 2019-04-10 09:27:55 -0700 | [diff] [blame] | 86 | struct device *dev = &pdev->dev; |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 87 | int ret; |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 88 | |
Linus Walleij | 74128d8 | 2021-09-23 01:09:45 +0200 | [diff] [blame] | 89 | timeout = 600; /* Default to 10 minutes */ |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 90 | db8500_wdt.parent = dev; |
| 91 | watchdog_set_nowayout(&db8500_wdt, nowayout); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 92 | |
| 93 | /* disable auto off on sleep */ |
| 94 | prcmu_config_a9wdog(PRCMU_WDOG_CPU1, false); |
| 95 | |
| 96 | /* set HW initial value */ |
| 97 | prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000); |
| 98 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 99 | ret = devm_watchdog_register_device(dev, &db8500_wdt); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 100 | if (ret) |
| 101 | return ret; |
| 102 | |
Guenter Roeck | 89e5b7a | 2019-04-10 09:27:55 -0700 | [diff] [blame] | 103 | dev_info(dev, "initialized\n"); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | #ifdef CONFIG_PM |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 109 | static int db8500_wdt_suspend(struct platform_device *pdev, |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 110 | pm_message_t state) |
| 111 | { |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 112 | if (watchdog_active(&db8500_wdt)) { |
| 113 | db8500_wdt_stop(&db8500_wdt); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 114 | prcmu_config_a9wdog(PRCMU_WDOG_CPU1, true); |
| 115 | |
| 116 | prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000); |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 117 | db8500_wdt_start(&db8500_wdt); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 122 | static int db8500_wdt_resume(struct platform_device *pdev) |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 123 | { |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 124 | if (watchdog_active(&db8500_wdt)) { |
| 125 | db8500_wdt_stop(&db8500_wdt); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 126 | prcmu_config_a9wdog(PRCMU_WDOG_CPU1, false); |
| 127 | |
| 128 | prcmu_load_a9wdog(PRCMU_WDOG_ALL, timeout * 1000); |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 129 | db8500_wdt_start(&db8500_wdt); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | #else |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 134 | #define db8500_wdt_suspend NULL |
| 135 | #define db8500_wdt_resume NULL |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 136 | #endif |
| 137 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 138 | static struct platform_driver db8500_wdt_driver = { |
| 139 | .probe = db8500_wdt_probe, |
| 140 | .suspend = db8500_wdt_suspend, |
| 141 | .resume = db8500_wdt_resume, |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 142 | .driver = { |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 143 | .name = "db8500_wdt", |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 144 | }, |
| 145 | }; |
| 146 | |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 147 | module_platform_driver(db8500_wdt_driver); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 148 | |
| 149 | MODULE_AUTHOR("Jonas Aaberg <jonas.aberg@stericsson.com>"); |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 150 | MODULE_DESCRIPTION("DB8500 Watchdog Driver"); |
Fabio Baltieri | f0e5bd4 | 2013-01-29 09:57:19 +0100 | [diff] [blame] | 151 | MODULE_LICENSE("GPL"); |
Linus Walleij | c738888 | 2021-09-23 01:09:47 +0200 | [diff] [blame] | 152 | MODULE_ALIAS("platform:db8500_wdt"); |