Marcus Folkesson | 2e62c49 | 2018-03-16 16:14:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog driver for DA9063 PMICs. |
| 4 | * |
| 5 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 6 | * |
| 7 | * Author: Mariusz Wojtasik <mariusz.wojtasik@diasemi.com> |
| 8 | * |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/watchdog.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/mfd/da9063/registers.h> |
| 19 | #include <linux/mfd/da9063/core.h> |
| 20 | #include <linux/regmap.h> |
| 21 | |
| 22 | /* |
| 23 | * Watchdog selector to timeout in seconds. |
| 24 | * 0: WDT disabled; |
| 25 | * others: timeout = 2048 ms * 2^(TWDSCALE-1). |
| 26 | */ |
| 27 | static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 }; |
| 28 | #define DA9063_TWDSCALE_DISABLE 0 |
| 29 | #define DA9063_TWDSCALE_MIN 1 |
| 30 | #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1) |
| 31 | #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN] |
| 32 | #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX] |
| 33 | #define DA9063_WDG_TIMEOUT wdt_timeout[3] |
Stefan Christ | a74cab4 | 2016-07-06 10:40:11 +0200 | [diff] [blame] | 34 | #define DA9063_RESET_PROTECTION_MS 256 |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 35 | |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 36 | static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs) |
| 37 | { |
| 38 | unsigned int i; |
| 39 | |
| 40 | for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) { |
| 41 | if (wdt_timeout[i] >= secs) |
| 42 | return i; |
| 43 | } |
| 44 | |
| 45 | return DA9063_TWDSCALE_MAX; |
| 46 | } |
| 47 | |
| 48 | static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval) |
| 49 | { |
| 50 | return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D, |
| 51 | DA9063_TWDSCALE_MASK, regval); |
| 52 | } |
| 53 | |
| 54 | static int da9063_wdt_start(struct watchdog_device *wdd) |
| 55 | { |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 56 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 57 | unsigned int selector; |
| 58 | int ret; |
| 59 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 60 | selector = da9063_wdt_timeout_to_sel(wdd->timeout); |
| 61 | ret = _da9063_wdt_set_timeout(da9063, selector); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 62 | if (ret) |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 63 | dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n", |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 64 | ret); |
| 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | static int da9063_wdt_stop(struct watchdog_device *wdd) |
| 70 | { |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 71 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 72 | int ret; |
| 73 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 74 | ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D, |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 75 | DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE); |
| 76 | if (ret) |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 77 | dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n", |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 78 | ret); |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static int da9063_wdt_ping(struct watchdog_device *wdd) |
| 84 | { |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 85 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 86 | int ret; |
| 87 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 88 | ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F, |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 89 | DA9063_WATCHDOG); |
| 90 | if (ret) |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 91 | dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n", |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 92 | ret); |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | static int da9063_wdt_set_timeout(struct watchdog_device *wdd, |
| 98 | unsigned int timeout) |
| 99 | { |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 100 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 101 | unsigned int selector; |
| 102 | int ret; |
| 103 | |
| 104 | selector = da9063_wdt_timeout_to_sel(timeout); |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 105 | ret = _da9063_wdt_set_timeout(da9063, selector); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 106 | if (ret) |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 107 | dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n", |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 108 | ret); |
| 109 | else |
| 110 | wdd->timeout = wdt_timeout[selector]; |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Guenter Roeck | 4d8b229 | 2016-02-26 17:32:49 -0800 | [diff] [blame] | 115 | static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action, |
| 116 | void *data) |
Geert Uytterhoeven | 396f163 | 2015-01-29 15:26:05 +0100 | [diff] [blame] | 117 | { |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 118 | struct da9063 *da9063 = watchdog_get_drvdata(wdd); |
Geert Uytterhoeven | 396f163 | 2015-01-29 15:26:05 +0100 | [diff] [blame] | 119 | int ret; |
| 120 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 121 | ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F, |
Geert Uytterhoeven | 396f163 | 2015-01-29 15:26:05 +0100 | [diff] [blame] | 122 | DA9063_SHUTDOWN); |
| 123 | if (ret) |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 124 | dev_alert(da9063->dev, "Failed to shutdown (err = %d)\n", |
Geert Uytterhoeven | 396f163 | 2015-01-29 15:26:05 +0100 | [diff] [blame] | 125 | ret); |
| 126 | |
Damien Riegel | f79781c | 2015-11-16 12:28:01 -0500 | [diff] [blame] | 127 | return ret; |
Geert Uytterhoeven | 396f163 | 2015-01-29 15:26:05 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 130 | static const struct watchdog_info da9063_watchdog_info = { |
| 131 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, |
| 132 | .identity = "DA9063 Watchdog", |
| 133 | }; |
| 134 | |
| 135 | static const struct watchdog_ops da9063_watchdog_ops = { |
| 136 | .owner = THIS_MODULE, |
| 137 | .start = da9063_wdt_start, |
| 138 | .stop = da9063_wdt_stop, |
| 139 | .ping = da9063_wdt_ping, |
| 140 | .set_timeout = da9063_wdt_set_timeout, |
Damien Riegel | f79781c | 2015-11-16 12:28:01 -0500 | [diff] [blame] | 141 | .restart = da9063_wdt_restart, |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | static int da9063_wdt_probe(struct platform_device *pdev) |
| 145 | { |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 146 | struct da9063 *da9063; |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 147 | struct watchdog_device *wdd; |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 148 | |
| 149 | if (!pdev->dev.parent) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | da9063 = dev_get_drvdata(pdev->dev.parent); |
| 153 | if (!da9063) |
| 154 | return -EINVAL; |
| 155 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 156 | wdd = devm_kzalloc(&pdev->dev, sizeof(*wdd), GFP_KERNEL); |
| 157 | if (!wdd) |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 158 | return -ENOMEM; |
| 159 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 160 | wdd->info = &da9063_watchdog_info; |
| 161 | wdd->ops = &da9063_watchdog_ops; |
| 162 | wdd->min_timeout = DA9063_WDT_MIN_TIMEOUT; |
| 163 | wdd->max_timeout = DA9063_WDT_MAX_TIMEOUT; |
| 164 | wdd->min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS; |
| 165 | wdd->timeout = DA9063_WDG_TIMEOUT; |
| 166 | wdd->parent = &pdev->dev; |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 167 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 168 | wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS; |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 169 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 170 | watchdog_set_restart_priority(wdd, 128); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 171 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 172 | watchdog_set_drvdata(wdd, da9063); |
Damien Riegel | f79781c | 2015-11-16 12:28:01 -0500 | [diff] [blame] | 173 | |
fzuuzf@googlemail.com | a1f2a82 | 2017-07-25 13:25:58 +0200 | [diff] [blame] | 174 | return devm_watchdog_register_device(&pdev->dev, wdd); |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static struct platform_driver da9063_wdt_driver = { |
| 178 | .probe = da9063_wdt_probe, |
Krystian Garbaciak | 5e9c16e | 2014-09-28 19:05:45 +0200 | [diff] [blame] | 179 | .driver = { |
| 180 | .name = DA9063_DRVNAME_WATCHDOG, |
| 181 | }, |
| 182 | }; |
| 183 | module_platform_driver(da9063_wdt_driver); |
| 184 | |
| 185 | MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>"); |
| 186 | MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063"); |
| 187 | MODULE_LICENSE("GPL"); |
| 188 | MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG); |