Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux/fpc-iii.git] / drivers / watchdog / wm831x_wdt.c
blobd96ad8f38bd2e647938b4dfbee9317d046284550
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Watchdog driver for the wm831x PMICs
5 * Copyright (C) 2009 Wolfson Microelectronics
6 */
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/watchdog.h>
15 #include <linux/uaccess.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/pdata.h>
19 #include <linux/mfd/wm831x/watchdog.h>
21 static bool nowayout = WATCHDOG_NOWAYOUT;
22 module_param(nowayout, bool, 0);
23 MODULE_PARM_DESC(nowayout,
24 "Watchdog cannot be stopped once started (default="
25 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27 struct wm831x_wdt_drvdata {
28 struct watchdog_device wdt;
29 struct wm831x *wm831x;
30 struct mutex lock;
31 int update_state;
34 /* We can't use the sub-second values here but they're included
35 * for completeness. */
36 static struct {
37 unsigned int time; /* Seconds */
38 u16 val; /* WDOG_TO value */
39 } 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 */
49 static int wm831x_wdt_start(struct watchdog_device *wdt_dev)
51 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
52 struct wm831x *wm831x = driver_data->wm831x;
53 int ret;
55 mutex_lock(&driver_data->lock);
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);
67 mutex_unlock(&driver_data->lock);
69 return ret;
72 static int wm831x_wdt_stop(struct watchdog_device *wdt_dev)
74 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
75 struct wm831x *wm831x = driver_data->wm831x;
76 int ret;
78 mutex_lock(&driver_data->lock);
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);
90 mutex_unlock(&driver_data->lock);
92 return ret;
95 static int wm831x_wdt_ping(struct watchdog_device *wdt_dev)
97 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
98 struct wm831x *wm831x = driver_data->wm831x;
99 int ret;
100 u16 reg;
102 mutex_lock(&driver_data->lock);
104 reg = wm831x_reg_read(wm831x, WM831X_WATCHDOG);
106 if (!(reg & WM831X_WDOG_RST_SRC)) {
107 dev_err(wm831x->dev, "Hardware watchdog update unsupported\n");
108 ret = -EINVAL;
109 goto out;
112 reg |= WM831X_WDOG_RESET;
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);
123 out:
124 mutex_unlock(&driver_data->lock);
126 return ret;
129 static int wm831x_wdt_set_timeout(struct watchdog_device *wdt_dev,
130 unsigned int timeout)
132 struct wm831x_wdt_drvdata *driver_data = watchdog_get_drvdata(wdt_dev);
133 struct wm831x *wm831x = driver_data->wm831x;
134 int ret, i;
136 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))
140 return -EINVAL;
142 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);
153 wdt_dev->timeout = timeout;
155 return ret;
158 static const struct watchdog_info wm831x_wdt_info = {
159 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
160 .identity = "WM831x Watchdog",
163 static const struct watchdog_ops wm831x_wdt_ops = {
164 .owner = THIS_MODULE,
165 .start = wm831x_wdt_start,
166 .stop = wm831x_wdt_stop,
167 .ping = wm831x_wdt_ping,
168 .set_timeout = wm831x_wdt_set_timeout,
171 static int wm831x_wdt_probe(struct platform_device *pdev)
173 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);
176 struct wm831x_watchdog_pdata *pdata;
177 struct wm831x_wdt_drvdata *driver_data;
178 struct watchdog_device *wm831x_wdt;
179 int reg, ret, i;
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);
185 return ret;
187 reg = ret;
189 if (reg & WM831X_WDOG_DEBUG)
190 dev_warn(wm831x->dev, "Watchdog is paused\n");
192 driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
193 if (!driver_data)
194 return -ENOMEM;
196 mutex_init(&driver_data->lock);
197 driver_data->wm831x = wm831x;
199 wm831x_wdt = &driver_data->wdt;
201 wm831x_wdt->info = &wm831x_wdt_info;
202 wm831x_wdt->ops = &wm831x_wdt_ops;
203 wm831x_wdt->parent = dev;
204 watchdog_set_nowayout(wm831x_wdt, nowayout);
205 watchdog_set_drvdata(wm831x_wdt, driver_data);
207 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;
218 /* Apply any configuration */
219 if (chip_pdata)
220 pdata = chip_pdata->watchdog;
221 else
222 pdata = NULL;
224 if (pdata) {
225 reg &= ~(WM831X_WDOG_SECACT_MASK | WM831X_WDOG_PRIMACT_MASK |
226 WM831X_WDOG_RST_SRC);
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;
232 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);
239 return ret;
243 return devm_watchdog_register_device(dev, &driver_data->wdt);
246 static struct platform_driver wm831x_wdt_driver = {
247 .probe = wm831x_wdt_probe,
248 .driver = {
249 .name = "wm831x-watchdog",
253 module_platform_driver(wm831x_wdt_driver);
255 MODULE_AUTHOR("Mark Brown");
256 MODULE_DESCRIPTION("WM831x Watchdog");
257 MODULE_LICENSE("GPL");
258 MODULE_ALIAS("platform:wm831x-watchdog");