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