Bagas Sanjaya | a0d261cc | 2023-05-17 14:21:39 +0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog driver for TS-4800 based boards |
| 4 | * |
| 5 | * Copyright (c) 2015 - Savoir-faire Linux |
| 6 | * |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/mfd/syscon.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/regmap.h> |
| 15 | #include <linux/watchdog.h> |
| 16 | |
| 17 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 18 | module_param(nowayout, bool, 0); |
| 19 | MODULE_PARM_DESC(nowayout, |
| 20 | "Watchdog cannot be stopped once started (default=" |
| 21 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
| 22 | |
| 23 | /* possible feed values */ |
| 24 | #define TS4800_WDT_FEED_2S 0x1 |
| 25 | #define TS4800_WDT_FEED_10S 0x2 |
| 26 | #define TS4800_WDT_DISABLE 0x3 |
| 27 | |
| 28 | struct ts4800_wdt { |
| 29 | struct watchdog_device wdd; |
| 30 | struct regmap *regmap; |
| 31 | u32 feed_offset; |
| 32 | u32 feed_val; |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * TS-4800 supports the following timeout values: |
| 37 | * |
| 38 | * value desc |
| 39 | * --------------------- |
| 40 | * 0 feed for 338ms |
| 41 | * 1 feed for 2.706s |
| 42 | * 2 feed for 10.824s |
| 43 | * 3 disable watchdog |
| 44 | * |
| 45 | * Keep the regmap/timeout map ordered by timeout |
| 46 | */ |
| 47 | static const struct { |
| 48 | const int timeout; |
| 49 | const int regval; |
| 50 | } ts4800_wdt_map[] = { |
| 51 | { 2, TS4800_WDT_FEED_2S }, |
| 52 | { 10, TS4800_WDT_FEED_10S }, |
| 53 | }; |
| 54 | |
| 55 | #define MAX_TIMEOUT_INDEX (ARRAY_SIZE(ts4800_wdt_map) - 1) |
| 56 | |
| 57 | static void ts4800_write_feed(struct ts4800_wdt *wdt, u32 val) |
| 58 | { |
| 59 | regmap_write(wdt->regmap, wdt->feed_offset, val); |
| 60 | } |
| 61 | |
| 62 | static int ts4800_wdt_start(struct watchdog_device *wdd) |
| 63 | { |
| 64 | struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd); |
| 65 | |
| 66 | ts4800_write_feed(wdt, wdt->feed_val); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int ts4800_wdt_stop(struct watchdog_device *wdd) |
| 71 | { |
| 72 | struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd); |
| 73 | |
| 74 | ts4800_write_feed(wdt, TS4800_WDT_DISABLE); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int ts4800_wdt_set_timeout(struct watchdog_device *wdd, |
| 79 | unsigned int timeout) |
| 80 | { |
| 81 | struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd); |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < MAX_TIMEOUT_INDEX; i++) { |
| 85 | if (ts4800_wdt_map[i].timeout >= timeout) |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | wdd->timeout = ts4800_wdt_map[i].timeout; |
| 90 | wdt->feed_val = ts4800_wdt_map[i].regval; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static const struct watchdog_ops ts4800_wdt_ops = { |
| 96 | .owner = THIS_MODULE, |
| 97 | .start = ts4800_wdt_start, |
| 98 | .stop = ts4800_wdt_stop, |
| 99 | .set_timeout = ts4800_wdt_set_timeout, |
| 100 | }; |
| 101 | |
| 102 | static const struct watchdog_info ts4800_wdt_info = { |
| 103 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, |
| 104 | .identity = "TS-4800 Watchdog", |
| 105 | }; |
| 106 | |
| 107 | static int ts4800_wdt_probe(struct platform_device *pdev) |
| 108 | { |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 109 | struct device *dev = &pdev->dev; |
| 110 | struct device_node *np = dev->of_node; |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 111 | struct device_node *syscon_np; |
| 112 | struct watchdog_device *wdd; |
| 113 | struct ts4800_wdt *wdt; |
| 114 | u32 reg; |
| 115 | int ret; |
| 116 | |
| 117 | syscon_np = of_parse_phandle(np, "syscon", 0); |
| 118 | if (!syscon_np) { |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 119 | dev_err(dev, "no syscon property\n"); |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 120 | return -ENODEV; |
| 121 | } |
| 122 | |
| 123 | ret = of_property_read_u32_index(np, "syscon", 1, ®); |
| 124 | if (ret < 0) { |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 125 | dev_err(dev, "no offset in syscon\n"); |
Miaoqian Lin | 5d24df3 | 2022-05-11 15:42:03 +0400 | [diff] [blame] | 126 | of_node_put(syscon_np); |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /* allocate memory for watchdog struct */ |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 131 | wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
Miaoqian Lin | 5d24df3 | 2022-05-11 15:42:03 +0400 | [diff] [blame] | 132 | if (!wdt) { |
| 133 | of_node_put(syscon_np); |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 134 | return -ENOMEM; |
Miaoqian Lin | 5d24df3 | 2022-05-11 15:42:03 +0400 | [diff] [blame] | 135 | } |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 136 | |
| 137 | /* set regmap and offset to know where to write */ |
| 138 | wdt->feed_offset = reg; |
| 139 | wdt->regmap = syscon_node_to_regmap(syscon_np); |
Alexey Khoroshilov | cd6ba41 | 2018-10-13 23:51:03 +0300 | [diff] [blame] | 140 | of_node_put(syscon_np); |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 141 | if (IS_ERR(wdt->regmap)) { |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 142 | dev_err(dev, "cannot get parent's regmap\n"); |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 143 | return PTR_ERR(wdt->regmap); |
| 144 | } |
| 145 | |
| 146 | /* Initialize struct watchdog_device */ |
| 147 | wdd = &wdt->wdd; |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 148 | wdd->parent = dev; |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 149 | wdd->info = &ts4800_wdt_info; |
| 150 | wdd->ops = &ts4800_wdt_ops; |
| 151 | wdd->min_timeout = ts4800_wdt_map[0].timeout; |
| 152 | wdd->max_timeout = ts4800_wdt_map[MAX_TIMEOUT_INDEX].timeout; |
| 153 | |
| 154 | watchdog_set_drvdata(wdd, wdt); |
| 155 | watchdog_set_nowayout(wdd, nowayout); |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 156 | watchdog_init_timeout(wdd, 0, dev); |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * As this watchdog supports only a few values, ts4800_wdt_set_timeout |
| 160 | * must be called to initialize timeout and feed_val with valid values. |
| 161 | * Default to maximum timeout if none, or an invalid one, is provided in |
| 162 | * device tree. |
| 163 | */ |
| 164 | if (!wdd->timeout) |
| 165 | wdd->timeout = wdd->max_timeout; |
| 166 | ts4800_wdt_set_timeout(wdd, wdd->timeout); |
| 167 | |
| 168 | /* |
| 169 | * The feed register is write-only, so it is not possible to determine |
| 170 | * watchdog's state. Disable it to be in a known state. |
| 171 | */ |
| 172 | ts4800_wdt_stop(wdd); |
| 173 | |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 174 | ret = devm_watchdog_register_device(dev, wdd); |
Wolfram Sang | c8c844f | 2019-05-18 23:27:59 +0200 | [diff] [blame] | 175 | if (ret) |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 176 | return ret; |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 177 | |
| 178 | platform_set_drvdata(pdev, wdt); |
| 179 | |
Guenter Roeck | 44affc0 | 2019-04-10 09:27:44 -0700 | [diff] [blame] | 180 | dev_info(dev, "initialized (timeout = %d sec, nowayout = %d)\n", |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 181 | wdd->timeout, nowayout); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 186 | static const struct of_device_id ts4800_wdt_of_match[] = { |
| 187 | { .compatible = "technologic,ts4800-wdt", }, |
| 188 | { }, |
| 189 | }; |
| 190 | MODULE_DEVICE_TABLE(of, ts4800_wdt_of_match); |
| 191 | |
| 192 | static struct platform_driver ts4800_wdt_driver = { |
| 193 | .probe = ts4800_wdt_probe, |
Damien Riegel | bf90063 | 2015-12-08 11:37:28 -0500 | [diff] [blame] | 194 | .driver = { |
| 195 | .name = "ts4800_wdt", |
| 196 | .of_match_table = ts4800_wdt_of_match, |
| 197 | }, |
| 198 | }; |
| 199 | |
| 200 | module_platform_driver(ts4800_wdt_driver); |
| 201 | |
| 202 | MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>"); |
| 203 | MODULE_LICENSE("GPL v2"); |
| 204 | MODULE_ALIAS("platform:ts4800_wdt"); |