blob: d96ad8f38bd2e647938b4dfbee9317d046284550 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Mark Brown502a0102009-07-27 14:46:12 +01002/*
3 * Watchdog driver for the wm831x PMICs
4 *
5 * Copyright (C) 2009 Wolfson Microelectronics
Mark Brown502a0102009-07-27 14:46:12 +01006 */
7
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
Mark Brown00411ee2011-10-04 16:43:40 +010012#include <linux/slab.h>
Mark Brown502a0102009-07-27 14:46:12 +010013#include <linux/platform_device.h>
14#include <linux/watchdog.h>
15#include <linux/uaccess.h>
Mark Brown502a0102009-07-27 14:46:12 +010016
17#include <linux/mfd/wm831x/core.h>
18#include <linux/mfd/wm831x/pdata.h>
19#include <linux/mfd/wm831x/watchdog.h>
20
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010021static bool nowayout = WATCHDOG_NOWAYOUT;
22module_param(nowayout, bool, 0);
Mark Brown502a0102009-07-27 14:46:12 +010023MODULE_PARM_DESC(nowayout,
24 "Watchdog cannot be stopped once started (default="
25 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
26
Mark Brown00411ee2011-10-04 16:43:40 +010027struct wm831x_wdt_drvdata {
28 struct watchdog_device wdt;
29 struct wm831x *wm831x;
30 struct mutex lock;
Mark Brown00411ee2011-10-04 16:43:40 +010031 int update_state;
32};
Mark Brown502a0102009-07-27 14:46:12 +010033
34/* We can't use the sub-second values here but they're included
35 * for completeness. */
36static struct {
Mark Brown00411ee2011-10-04 16:43:40 +010037 unsigned int time; /* Seconds */
38 u16 val; /* WDOG_TO value */
Mark Brown502a0102009-07-27 14:46:12 +010039} wm831x_wdt_cfgs[] = {
40 { 1, 2 },
41 { 2, 3 },
42 { 4, 4 },
43 { 8, 5 },
44 { 16, 6 },
45 { 32, 7 },
46 { 33, 7 }, /* Actually 32.768s so include both, others round down */
47};
48
Mark Brown00411ee2011-10-04 16:43:40 +010049static int wm831x_wdt_start(struct watchdog_device *wdt_dev)
Mark Brown502a0102009-07-27 14:46:12 +010050{
Mark Brown00411ee2011-10-04 16:43:40 +010051 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
52 struct wm831x *wm831x = driver_data->wm831x;
Mark Brown502a0102009-07-27 14:46:12 +010053 int ret;
54
Mark Brown00411ee2011-10-04 16:43:40 +010055 mutex_lock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010056
57 ret = wm831x_reg_unlock(wm831x);
58 if (ret == 0) {
59 ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
60 WM831X_WDOG_ENA, WM831X_WDOG_ENA);
61 wm831x_reg_lock(wm831x);
62 } else {
63 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
64 ret);
65 }
66
Mark Brown00411ee2011-10-04 16:43:40 +010067 mutex_unlock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010068
69 return ret;
70}
71
Mark Brown00411ee2011-10-04 16:43:40 +010072static int wm831x_wdt_stop(struct watchdog_device *wdt_dev)
Mark Brown502a0102009-07-27 14:46:12 +010073{
Mark Brown00411ee2011-10-04 16:43:40 +010074 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
75 struct wm831x *wm831x = driver_data->wm831x;
Mark Brown502a0102009-07-27 14:46:12 +010076 int ret;
77
Mark Brown00411ee2011-10-04 16:43:40 +010078 mutex_lock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010079
80 ret = wm831x_reg_unlock(wm831x);
81 if (ret == 0) {
82 ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
83 WM831X_WDOG_ENA, 0);
84 wm831x_reg_lock(wm831x);
85 } else {
86 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
87 ret);
88 }
89
Mark Brown00411ee2011-10-04 16:43:40 +010090 mutex_unlock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +010091
92 return ret;
93}
94
Mark Brown00411ee2011-10-04 16:43:40 +010095static int wm831x_wdt_ping(struct watchdog_device *wdt_dev)
Mark Brown502a0102009-07-27 14:46:12 +010096{
Mark Brown00411ee2011-10-04 16:43:40 +010097 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
98 struct wm831x *wm831x = driver_data->wm831x;
Mark Brown502a0102009-07-27 14:46:12 +010099 int ret;
100 u16 reg;
101
Mark Brown00411ee2011-10-04 16:43:40 +0100102 mutex_lock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +0100103
Mark Brown502a0102009-07-27 14:46:12 +0100104 reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
105
106 if (!(reg & WM831X_WDOG_RST_SRC)) {
107 dev_err(wm831x->dev, "Hardware watchdog update unsupported\n");
108 ret = -EINVAL;
109 goto out;
110 }
111
112 reg |= WM831X_WDOG_RESET;
113
114 ret = wm831x_reg_unlock(wm831x);
115 if (ret == 0) {
116 ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
117 wm831x_reg_lock(wm831x);
118 } else {
119 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
120 ret);
121 }
122
123out:
Mark Brown00411ee2011-10-04 16:43:40 +0100124 mutex_unlock(&driver_data->lock);
Mark Brown502a0102009-07-27 14:46:12 +0100125
126 return ret;
127}
128
Mark Brown00411ee2011-10-04 16:43:40 +0100129static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev,
130 unsigned int timeout)
Mark Brown502a0102009-07-27 14:46:12 +0100131{
Mark Brown00411ee2011-10-04 16:43:40 +0100132 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
133 struct wm831x *wm831x = driver_data->wm831x;
134 int ret, i;
Mark Brown502a0102009-07-27 14:46:12 +0100135
Mark Brown00411ee2011-10-04 16:43:40 +0100136 for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
137 if (wm831x_wdt_cfgs[i].time == timeout)
138 break;
139 if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
Mark Brownf9849102011-11-16 11:52:12 +0000140 return -EINVAL;
Mark Brown502a0102009-07-27 14:46:12 +0100141
Mark Brown00411ee2011-10-04 16:43:40 +0100142 ret = wm831x_reg_unlock(wm831x);
143 if (ret == 0) {
144 ret = wm831x_set_bits(wm831x, WM831X_WATCHDOG,
145 WM831X_WDOG_TO_MASK,
146 wm831x_wdt_cfgs[i].val);
147 wm831x_reg_lock(wm831x);
148 } else {
149 dev_err(wm831x->dev, "Failed to unlock security key: %d\n",
150 ret);
Mark Brown502a0102009-07-27 14:46:12 +0100151 }
152
Wim Van Sebroeck0197c1c2012-02-29 20:20:58 +0100153 wdt_dev->timeout = timeout;
154
Mark Brown00411ee2011-10-04 16:43:40 +0100155 return ret;
Mark Brown502a0102009-07-27 14:46:12 +0100156}
157
Mark Brown00411ee2011-10-04 16:43:40 +0100158static const struct watchdog_info wm831x_wdt_info = {
Mark Brown502a0102009-07-27 14:46:12 +0100159 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
160 .identity = "WM831x Watchdog",
161};
162
Mark Brown00411ee2011-10-04 16:43:40 +0100163static const struct watchdog_ops wm831x_wdt_ops = {
Mark Brown502a0102009-07-27 14:46:12 +0100164 .owner = THIS_MODULE,
Mark Brown00411ee2011-10-04 16:43:40 +0100165 .start = wm831x_wdt_start,
166 .stop = wm831x_wdt_stop,
167 .ping = wm831x_wdt_ping,
168 .set_timeout = wm831x_wdt_set_timeout,
Mark Brown502a0102009-07-27 14:46:12 +0100169};
170
Bill Pemberton2d991a12012-11-19 13:21:41 -0500171static int wm831x_wdt_probe(struct platform_device *pdev)
Mark Brown502a0102009-07-27 14:46:12 +0100172{
Guenter Roeck30f57e02019-04-10 09:27:49 -0700173 struct device *dev = &pdev->dev;
174 struct wm831x *wm831x = dev_get_drvdata(dev->parent);
175 struct wm831x_pdata *chip_pdata = dev_get_platdata(dev->parent);
Mark Brown502a0102009-07-27 14:46:12 +0100176 struct wm831x_watchdog_pdata *pdata;
Mark Brown00411ee2011-10-04 16:43:40 +0100177 struct wm831x_wdt_drvdata *driver_data;
178 struct watchdog_device *wm831x_wdt;
179 int reg, ret, i;
Mark Brown502a0102009-07-27 14:46:12 +0100180
181 ret = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
182 if (ret < 0) {
183 dev_err(wm831x->dev, "Failed to read watchdog status: %d\n",
184 ret);
Guenter Roeck30cba9a12017-01-10 15:22:01 -0800185 return ret;
Mark Brown502a0102009-07-27 14:46:12 +0100186 }
187 reg = ret;
188
189 if (reg & WM831X_WDOG_DEBUG)
190 dev_warn(wm831x->dev, "Watchdog is paused\n");
191
Guenter Roeck30f57e02019-04-10 09:27:49 -0700192 driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
Guenter Roeck30cba9a12017-01-10 15:22:01 -0800193 if (!driver_data)
194 return -ENOMEM;
Mark Brown00411ee2011-10-04 16:43:40 +0100195
196 mutex_init(&driver_data->lock);
197 driver_data->wm831x = wm831x;
198
199 wm831x_wdt = &driver_data->wdt;
200
201 wm831x_wdt->info = &wm831x_wdt_info;
202 wm831x_wdt->ops = &wm831x_wdt_ops;
Guenter Roeck30f57e02019-04-10 09:27:49 -0700203 wm831x_wdt->parent = dev;
Wim Van Sebroeckff0b3cd2011-11-29 16:24:16 +0100204 watchdog_set_nowayout(wm831x_wdt, nowayout);
Mark Brown00411ee2011-10-04 16:43:40 +0100205 watchdog_set_drvdata(wm831x_wdt, driver_data);
206
Mark Brown00411ee2011-10-04 16:43:40 +0100207 reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
208 reg &= WM831X_WDOG_TO_MASK;
209 for (i = 0; i < ARRAY_SIZE(wm831x_wdt_cfgs); i++)
210 if (wm831x_wdt_cfgs[i].val == reg)
211 break;
212 if (i == ARRAY_SIZE(wm831x_wdt_cfgs))
213 dev_warn(wm831x->dev,
214 "Unknown watchdog timeout: %x\n", reg);
215 else
216 wm831x_wdt->timeout = wm831x_wdt_cfgs[i].time;
217
Mark Brown502a0102009-07-27 14:46:12 +0100218 /* Apply any configuration */
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900219 if (chip_pdata)
Mark Brown502a0102009-07-27 14:46:12 +0100220 pdata = chip_pdata->watchdog;
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900221 else
Mark Brown502a0102009-07-27 14:46:12 +0100222 pdata = NULL;
Mark Brown502a0102009-07-27 14:46:12 +0100223
224 if (pdata) {
225 reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK |
226 WM831X_WDOG_RST_SRC);
227
228 reg |= pdata->primary << WM831X_WDOG_PRIMACT_SHIFT;
229 reg |= pdata->secondary << WM831X_WDOG_SECACT_SHIFT;
230 reg |= pdata->software << WM831X_WDOG_RST_SRC_SHIFT;
231
Mark Brown502a0102009-07-27 14:46:12 +0100232 ret = wm831x_reg_unlock(wm831x);
233 if (ret == 0) {
234 ret = wm831x_reg_write(wm831x, WM831X_WATCHDOG, reg);
235 wm831x_reg_lock(wm831x);
236 } else {
237 dev_err(wm831x->dev,
238 "Failed to unlock security key: %d\n", ret);
Guenter Roeck30cba9a12017-01-10 15:22:01 -0800239 return ret;
Mark Brown502a0102009-07-27 14:46:12 +0100240 }
241 }
242
Wolfram Sangf848a152019-05-18 23:28:00 +0200243 return devm_watchdog_register_device(dev, &driver_data->wdt);
Mark Brown502a0102009-07-27 14:46:12 +0100244}
245
246static struct platform_driver wm831x_wdt_driver = {
247 .probe = wm831x_wdt_probe,
Mark Brown502a0102009-07-27 14:46:12 +0100248 .driver = {
249 .name = "wm831x-watchdog",
250 },
251};
252
Mark Brown216f3ad2011-11-23 15:22:36 +0000253module_platform_driver(wm831x_wdt_driver);
Mark Brown502a0102009-07-27 14:46:12 +0100254
255MODULE_AUTHOR("Mark Brown");
256MODULE_DESCRIPTION("WM831x Watchdog");
257MODULE_LICENSE("GPL");
258MODULE_ALIAS("platform:wm831x-watchdog");