Marcus Folkesson | 2e62c49 | 2018-03-16 16:14:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 2 | /* |
Steve Twiss | 72106c1 | 2016-11-14 08:22:45 +0000 | [diff] [blame] | 3 | * Watchdog device driver for DA9062 and DA9061 PMICs |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 4 | * Copyright (C) 2015 Dialog Semiconductor Ltd. |
| 5 | * |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/watchdog.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/uaccess.h> |
| 13 | #include <linux/slab.h> |
Marco Felsch | 057b52b | 2020-01-15 17:23:07 +0100 | [diff] [blame] | 14 | #include <linux/i2c.h> |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include <linux/jiffies.h> |
| 17 | #include <linux/mfd/da9062/registers.h> |
| 18 | #include <linux/mfd/da9062/core.h> |
Marco Felsch | 8541673 | 2020-02-07 08:15:18 +0100 | [diff] [blame] | 19 | #include <linux/property.h> |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 20 | #include <linux/regmap.h> |
| 21 | #include <linux/of.h> |
| 22 | |
| 23 | static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 }; |
| 24 | #define DA9062_TWDSCALE_DISABLE 0 |
| 25 | #define DA9062_TWDSCALE_MIN 1 |
| 26 | #define DA9062_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1) |
| 27 | #define DA9062_WDT_MIN_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MIN] |
| 28 | #define DA9062_WDT_MAX_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX] |
| 29 | #define DA9062_WDG_DEFAULT_TIMEOUT wdt_timeout[DA9062_TWDSCALE_MAX-1] |
| 30 | #define DA9062_RESET_PROTECTION_MS 300 |
| 31 | |
| 32 | struct da9062_watchdog { |
| 33 | struct da9062 *hw; |
| 34 | struct watchdog_device wdtdev; |
Marco Felsch | 8541673 | 2020-02-07 08:15:18 +0100 | [diff] [blame] | 35 | bool use_sw_pm; |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
Stefan Riedmueller | e8799ce | 2020-04-03 15:07:26 +0200 | [diff] [blame] | 38 | static unsigned int da9062_wdt_read_timeout(struct da9062_watchdog *wdt) |
| 39 | { |
| 40 | unsigned int val; |
| 41 | |
| 42 | regmap_read(wdt->hw->regmap, DA9062AA_CONTROL_D, &val); |
| 43 | |
| 44 | return wdt_timeout[val & DA9062AA_TWDSCALE_MASK]; |
| 45 | } |
| 46 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 47 | static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs) |
| 48 | { |
| 49 | unsigned int i; |
| 50 | |
| 51 | for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) { |
| 52 | if (wdt_timeout[i] >= secs) |
| 53 | return i; |
| 54 | } |
| 55 | |
| 56 | return DA9062_TWDSCALE_MAX; |
| 57 | } |
| 58 | |
| 59 | static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt) |
| 60 | { |
Guenter Roeck | 6e8a7c7 | 2019-04-08 12:38:35 -0700 | [diff] [blame] | 61 | return regmap_update_bits(wdt->hw->regmap, DA9062AA_CONTROL_F, |
| 62 | DA9062AA_WATCHDOG_MASK, |
| 63 | DA9062AA_WATCHDOG_MASK); |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt, |
| 67 | unsigned int regval) |
| 68 | { |
| 69 | struct da9062 *chip = wdt->hw; |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 70 | |
Michael Grzeschik | f31b2a9 | 2017-10-17 17:30:25 +0200 | [diff] [blame] | 71 | regmap_update_bits(chip->regmap, |
| 72 | DA9062AA_CONTROL_D, |
| 73 | DA9062AA_TWDSCALE_MASK, |
| 74 | DA9062_TWDSCALE_DISABLE); |
| 75 | |
| 76 | usleep_range(150, 300); |
| 77 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 78 | return regmap_update_bits(chip->regmap, |
| 79 | DA9062AA_CONTROL_D, |
| 80 | DA9062AA_TWDSCALE_MASK, |
| 81 | regval); |
| 82 | } |
| 83 | |
| 84 | static int da9062_wdt_start(struct watchdog_device *wdd) |
| 85 | { |
| 86 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
| 87 | unsigned int selector; |
| 88 | int ret; |
| 89 | |
| 90 | selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout); |
| 91 | ret = da9062_wdt_update_timeout_register(wdt, selector); |
| 92 | if (ret) |
| 93 | dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n", |
| 94 | ret); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static int da9062_wdt_stop(struct watchdog_device *wdd) |
| 100 | { |
| 101 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
| 102 | int ret; |
| 103 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 104 | ret = regmap_update_bits(wdt->hw->regmap, |
| 105 | DA9062AA_CONTROL_D, |
| 106 | DA9062AA_TWDSCALE_MASK, |
| 107 | DA9062_TWDSCALE_DISABLE); |
| 108 | if (ret) |
| 109 | dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n", |
| 110 | ret); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static int da9062_wdt_ping(struct watchdog_device *wdd) |
| 116 | { |
| 117 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
| 118 | int ret; |
| 119 | |
Primoz Fiser | 2f61b3a | 2021-07-08 10:21:28 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Prevent pings from occurring late in system poweroff/reboot sequence |
| 122 | * and possibly locking out restart handler from accessing i2c bus. |
| 123 | */ |
| 124 | if (system_state > SYSTEM_RUNNING) |
| 125 | return 0; |
| 126 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 127 | ret = da9062_reset_watchdog_timer(wdt); |
| 128 | if (ret) |
| 129 | dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n", |
| 130 | ret); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | static int da9062_wdt_set_timeout(struct watchdog_device *wdd, |
| 136 | unsigned int timeout) |
| 137 | { |
| 138 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
| 139 | unsigned int selector; |
| 140 | int ret; |
| 141 | |
| 142 | selector = da9062_wdt_timeout_to_sel(timeout); |
| 143 | ret = da9062_wdt_update_timeout_register(wdt, selector); |
| 144 | if (ret) |
| 145 | dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n", |
| 146 | ret); |
| 147 | else |
| 148 | wdd->timeout = wdt_timeout[selector]; |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
Michael Grzeschik | 540f635 | 2017-10-17 17:30:22 +0200 | [diff] [blame] | 153 | static int da9062_wdt_restart(struct watchdog_device *wdd, unsigned long action, |
| 154 | void *data) |
| 155 | { |
| 156 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
Marco Felsch | 057b52b | 2020-01-15 17:23:07 +0100 | [diff] [blame] | 157 | struct i2c_client *client = to_i2c_client(wdt->hw->dev); |
Michael Grzeschik | 540f635 | 2017-10-17 17:30:22 +0200 | [diff] [blame] | 158 | int ret; |
| 159 | |
Marco Felsch | 057b52b | 2020-01-15 17:23:07 +0100 | [diff] [blame] | 160 | /* Don't use regmap because it is not atomic safe */ |
| 161 | ret = i2c_smbus_write_byte_data(client, DA9062AA_CONTROL_F, |
| 162 | DA9062AA_SHUTDOWN_MASK); |
| 163 | if (ret < 0) |
Michael Grzeschik | 540f635 | 2017-10-17 17:30:22 +0200 | [diff] [blame] | 164 | dev_alert(wdt->hw->dev, "Failed to shutdown (err = %d)\n", |
| 165 | ret); |
| 166 | |
| 167 | /* wait for reset to assert... */ |
| 168 | mdelay(500); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 173 | static const struct watchdog_info da9062_watchdog_info = { |
| 174 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 175 | .identity = "DA9062 WDT", |
| 176 | }; |
| 177 | |
| 178 | static const struct watchdog_ops da9062_watchdog_ops = { |
| 179 | .owner = THIS_MODULE, |
| 180 | .start = da9062_wdt_start, |
| 181 | .stop = da9062_wdt_stop, |
| 182 | .ping = da9062_wdt_ping, |
| 183 | .set_timeout = da9062_wdt_set_timeout, |
Michael Grzeschik | 540f635 | 2017-10-17 17:30:22 +0200 | [diff] [blame] | 184 | .restart = da9062_wdt_restart, |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
Steve Twiss | 72106c1 | 2016-11-14 08:22:45 +0000 | [diff] [blame] | 187 | static const struct of_device_id da9062_compatible_id_table[] = { |
| 188 | { .compatible = "dlg,da9062-watchdog", }, |
| 189 | { }, |
| 190 | }; |
| 191 | |
| 192 | MODULE_DEVICE_TABLE(of, da9062_compatible_id_table); |
| 193 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 194 | static int da9062_wdt_probe(struct platform_device *pdev) |
| 195 | { |
Guenter Roeck | 6e8a7c7 | 2019-04-08 12:38:35 -0700 | [diff] [blame] | 196 | struct device *dev = &pdev->dev; |
Stefan Riedmueller | e8799ce | 2020-04-03 15:07:26 +0200 | [diff] [blame] | 197 | unsigned int timeout; |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 198 | struct da9062 *chip; |
| 199 | struct da9062_watchdog *wdt; |
| 200 | |
Guenter Roeck | 6e8a7c7 | 2019-04-08 12:38:35 -0700 | [diff] [blame] | 201 | chip = dev_get_drvdata(dev->parent); |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 202 | if (!chip) |
| 203 | return -EINVAL; |
| 204 | |
Guenter Roeck | 6e8a7c7 | 2019-04-08 12:38:35 -0700 | [diff] [blame] | 205 | wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 206 | if (!wdt) |
| 207 | return -ENOMEM; |
| 208 | |
Marco Felsch | 8541673 | 2020-02-07 08:15:18 +0100 | [diff] [blame] | 209 | wdt->use_sw_pm = device_property_present(dev, "dlg,use-sw-pm"); |
| 210 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 211 | wdt->hw = chip; |
| 212 | |
| 213 | wdt->wdtdev.info = &da9062_watchdog_info; |
| 214 | wdt->wdtdev.ops = &da9062_watchdog_ops; |
| 215 | wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT; |
| 216 | wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT; |
Michael Grzeschik | fb48426 | 2017-10-17 17:30:26 +0200 | [diff] [blame] | 217 | wdt->wdtdev.min_hw_heartbeat_ms = DA9062_RESET_PROTECTION_MS; |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 218 | wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT; |
| 219 | wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS; |
Guenter Roeck | 6e8a7c7 | 2019-04-08 12:38:35 -0700 | [diff] [blame] | 220 | wdt->wdtdev.parent = dev; |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 221 | |
Michael Grzeschik | 540f635 | 2017-10-17 17:30:22 +0200 | [diff] [blame] | 222 | watchdog_set_restart_priority(&wdt->wdtdev, 128); |
| 223 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 224 | watchdog_set_drvdata(&wdt->wdtdev, wdt); |
Marco Felsch | f6c98b0 | 2019-11-28 18:19:31 +0100 | [diff] [blame] | 225 | dev_set_drvdata(dev, &wdt->wdtdev); |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 226 | |
Stefan Riedmueller | e8799ce | 2020-04-03 15:07:26 +0200 | [diff] [blame] | 227 | timeout = da9062_wdt_read_timeout(wdt); |
| 228 | if (timeout) |
| 229 | wdt->wdtdev.timeout = timeout; |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 230 | |
Stefan Riedmueller | e8799ce | 2020-04-03 15:07:26 +0200 | [diff] [blame] | 231 | /* Set timeout from DT value if available */ |
| 232 | watchdog_init_timeout(&wdt->wdtdev, 0, dev); |
| 233 | |
| 234 | if (timeout) { |
| 235 | da9062_wdt_set_timeout(&wdt->wdtdev, wdt->wdtdev.timeout); |
| 236 | set_bit(WDOG_HW_RUNNING, &wdt->wdtdev.status); |
| 237 | } |
| 238 | |
| 239 | return devm_watchdog_register_device(dev, &wdt->wdtdev); |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Marco Felsch | f6c98b0 | 2019-11-28 18:19:31 +0100 | [diff] [blame] | 242 | static int __maybe_unused da9062_wdt_suspend(struct device *dev) |
| 243 | { |
| 244 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
Marco Felsch | 8541673 | 2020-02-07 08:15:18 +0100 | [diff] [blame] | 245 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
| 246 | |
| 247 | if (!wdt->use_sw_pm) |
| 248 | return 0; |
Marco Felsch | f6c98b0 | 2019-11-28 18:19:31 +0100 | [diff] [blame] | 249 | |
| 250 | if (watchdog_active(wdd)) |
| 251 | return da9062_wdt_stop(wdd); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int __maybe_unused da9062_wdt_resume(struct device *dev) |
| 257 | { |
| 258 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
Marco Felsch | 8541673 | 2020-02-07 08:15:18 +0100 | [diff] [blame] | 259 | struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd); |
| 260 | |
| 261 | if (!wdt->use_sw_pm) |
| 262 | return 0; |
Marco Felsch | f6c98b0 | 2019-11-28 18:19:31 +0100 | [diff] [blame] | 263 | |
| 264 | if (watchdog_active(wdd)) |
| 265 | return da9062_wdt_start(wdd); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static SIMPLE_DEV_PM_OPS(da9062_wdt_pm_ops, |
| 271 | da9062_wdt_suspend, da9062_wdt_resume); |
| 272 | |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 273 | static struct platform_driver da9062_wdt_driver = { |
| 274 | .probe = da9062_wdt_probe, |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 275 | .driver = { |
| 276 | .name = "da9062-watchdog", |
Marco Felsch | f6c98b0 | 2019-11-28 18:19:31 +0100 | [diff] [blame] | 277 | .pm = &da9062_wdt_pm_ops, |
Steve Twiss | 72106c1 | 2016-11-14 08:22:45 +0000 | [diff] [blame] | 278 | .of_match_table = da9062_compatible_id_table, |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 279 | }, |
| 280 | }; |
| 281 | module_platform_driver(da9062_wdt_driver); |
| 282 | |
| 283 | MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>"); |
Steve Twiss | 72106c1 | 2016-11-14 08:22:45 +0000 | [diff] [blame] | 284 | MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061"); |
S Twiss | 7a7cb00 | 2015-05-28 14:33:31 +0100 | [diff] [blame] | 285 | MODULE_LICENSE("GPL"); |
| 286 | MODULE_ALIAS("platform:da9062-watchdog"); |