1 // SPDX-License-Identifier: GPL-2.0+
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 *
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/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/mfd/da9063/registers.h>
20 #include <linux/mfd/da9063/core.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23
24 /*
25 * Watchdog selector to timeout in seconds.
26 * 0: WDT disabled;
27 * others: timeout = 2048 ms * 2^(TWDSCALE-1).
28 */
29 static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
30
31 #define DA9063_TWDSCALE_DISABLE 0
32 #define DA9063_TWDSCALE_MIN 1
33 #define DA9063_TWDSCALE_MAX (ARRAY_SIZE(wdt_timeout) - 1)
34 #define DA9063_WDT_MIN_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MIN]
35 #define DA9063_WDT_MAX_TIMEOUT wdt_timeout[DA9063_TWDSCALE_MAX]
36 #define DA9063_WDG_TIMEOUT wdt_timeout[3]
37 #define DA9063_RESET_PROTECTION_MS 256
38
da9063_wdt_timeout_to_sel(unsigned int secs)39 static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
40 {
41 unsigned int i;
42
43 for (i = DA9063_TWDSCALE_MIN; i <= DA9063_TWDSCALE_MAX; i++) {
44 if (wdt_timeout[i] >= secs)
45 return i;
46 }
47
48 return DA9063_TWDSCALE_MAX;
49 }
50
51 /*
52 * Read the currently active timeout.
53 * Zero means the watchdog is disabled.
54 */
da9063_wdt_read_timeout(struct da9063 * da9063)55 static unsigned int da9063_wdt_read_timeout(struct da9063 *da9063)
56 {
57 unsigned int val;
58
59 regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);
60
61 return wdt_timeout[val & DA9063_TWDSCALE_MASK];
62 }
63
da9063_wdt_disable_timer(struct da9063 * da9063)64 static int da9063_wdt_disable_timer(struct da9063 *da9063)
65 {
66 return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
67 DA9063_TWDSCALE_MASK,
68 DA9063_TWDSCALE_DISABLE);
69 }
70
71 static int
da9063_wdt_update_timeout(struct da9063 * da9063,unsigned int timeout)72 da9063_wdt_update_timeout(struct da9063 *da9063, unsigned int timeout)
73 {
74 unsigned int regval;
75 int ret;
76
77 /*
78 * The watchdog triggers a reboot if a timeout value is already
79 * programmed because the timeout value combines two functions
80 * in one: indicating the counter limit and starting the watchdog.
81 * The watchdog must be disabled to be able to change the timeout
82 * value if the watchdog is already running. Then we can set the
83 * new timeout value which enables the watchdog again.
84 */
85 ret = da9063_wdt_disable_timer(da9063);
86 if (ret)
87 return ret;
88
89 usleep_range(150, 300);
90 regval = da9063_wdt_timeout_to_sel(timeout);
91
92 return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
93 DA9063_TWDSCALE_MASK, regval);
94 }
95
da9063_wdt_start(struct watchdog_device * wdd)96 static int da9063_wdt_start(struct watchdog_device *wdd)
97 {
98 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
99 int ret;
100
101 ret = da9063_wdt_update_timeout(da9063, wdd->timeout);
102 if (ret)
103 dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
104 ret);
105
106 return ret;
107 }
108
da9063_wdt_stop(struct watchdog_device * wdd)109 static int da9063_wdt_stop(struct watchdog_device *wdd)
110 {
111 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
112 int ret;
113
114 ret = da9063_wdt_disable_timer(da9063);
115 if (ret)
116 dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n",
117 ret);
118
119 return ret;
120 }
121
da9063_wdt_ping(struct watchdog_device * wdd)122 static int da9063_wdt_ping(struct watchdog_device *wdd)
123 {
124 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
125 int ret;
126
127 /*
128 * Prevent pings from occurring late in system poweroff/reboot sequence
129 * and possibly locking out restart handler from accessing i2c bus.
130 */
131 if (system_state > SYSTEM_RUNNING)
132 return 0;
133
134 ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
135 DA9063_WATCHDOG);
136 if (ret)
137 dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n",
138 ret);
139
140 return ret;
141 }
142
da9063_wdt_set_timeout(struct watchdog_device * wdd,unsigned int timeout)143 static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
144 unsigned int timeout)
145 {
146 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
147 int ret = 0;
148
149 /*
150 * There are two cases when a set_timeout() will be called:
151 * 1. The watchdog is off and someone wants to set the timeout for the
152 * further use.
153 * 2. The watchdog is already running and a new timeout value should be
154 * set.
155 *
156 * The watchdog can't store a timeout value not equal zero without
157 * enabling the watchdog, so the timeout must be buffered by the driver.
158 */
159 if (watchdog_active(wdd))
160 ret = da9063_wdt_update_timeout(da9063, timeout);
161
162 if (ret)
163 dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
164 ret);
165 else
166 wdd->timeout = wdt_timeout[da9063_wdt_timeout_to_sel(timeout)];
167
168 return ret;
169 }
170
da9063_wdt_restart(struct watchdog_device * wdd,unsigned long action,void * data)171 static int da9063_wdt_restart(struct watchdog_device *wdd, unsigned long action,
172 void *data)
173 {
174 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
175 struct i2c_client *client = to_i2c_client(da9063->dev);
176 union i2c_smbus_data msg;
177 int ret;
178
179 /*
180 * Don't use regmap because it is not atomic safe. Additionally, use
181 * unlocked flavor of i2c_smbus_xfer to avoid scenario where i2c bus
182 * might previously be locked by some process unable to release the
183 * lock due to interrupts already being disabled at this late stage.
184 */
185 msg.byte = DA9063_SHUTDOWN;
186 ret = __i2c_smbus_xfer(client->adapter, client->addr, client->flags,
187 I2C_SMBUS_WRITE, DA9063_REG_CONTROL_F,
188 I2C_SMBUS_BYTE_DATA, &msg);
189
190 if (ret < 0)
191 dev_alert(da9063->dev, "Failed to shutdown (err = %d)\n",
192 ret);
193
194 /* wait for reset to assert... */
195 mdelay(500);
196
197 return ret;
198 }
199
200 static const struct watchdog_info da9063_watchdog_info = {
201 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
202 .identity = "DA9063 Watchdog",
203 };
204
205 static const struct watchdog_ops da9063_watchdog_ops = {
206 .owner = THIS_MODULE,
207 .start = da9063_wdt_start,
208 .stop = da9063_wdt_stop,
209 .ping = da9063_wdt_ping,
210 .set_timeout = da9063_wdt_set_timeout,
211 .restart = da9063_wdt_restart,
212 };
213
da9063_wdt_probe(struct platform_device * pdev)214 static int da9063_wdt_probe(struct platform_device *pdev)
215 {
216 struct device *dev = &pdev->dev;
217 struct da9063 *da9063;
218 struct watchdog_device *wdd;
219 unsigned int timeout;
220
221 if (!dev->parent)
222 return -EINVAL;
223
224 da9063 = dev_get_drvdata(dev->parent);
225 if (!da9063)
226 return -EINVAL;
227
228 wdd = devm_kzalloc(dev, sizeof(*wdd), GFP_KERNEL);
229 if (!wdd)
230 return -ENOMEM;
231
232 da9063->use_sw_pm = device_property_present(dev, "dlg,use-sw-pm");
233
234 wdd->info = &da9063_watchdog_info;
235 wdd->ops = &da9063_watchdog_ops;
236 wdd->min_timeout = DA9063_WDT_MIN_TIMEOUT;
237 wdd->max_timeout = DA9063_WDT_MAX_TIMEOUT;
238 wdd->min_hw_heartbeat_ms = DA9063_RESET_PROTECTION_MS;
239 wdd->parent = dev;
240 wdd->status = WATCHDOG_NOWAYOUT_INIT_STATUS;
241
242 watchdog_set_restart_priority(wdd, 128);
243 watchdog_set_drvdata(wdd, da9063);
244 dev_set_drvdata(dev, wdd);
245
246 wdd->timeout = DA9063_WDG_TIMEOUT;
247
248 /* Use pre-configured timeout if watchdog is already running. */
249 timeout = da9063_wdt_read_timeout(da9063);
250 if (timeout)
251 wdd->timeout = timeout;
252
253 /* Set timeout, maybe override it with DT value, scale it */
254 watchdog_init_timeout(wdd, 0, dev);
255 da9063_wdt_set_timeout(wdd, wdd->timeout);
256
257 /* Update timeout if the watchdog is already running. */
258 if (timeout) {
259 da9063_wdt_update_timeout(da9063, wdd->timeout);
260 set_bit(WDOG_HW_RUNNING, &wdd->status);
261 }
262
263 return devm_watchdog_register_device(dev, wdd);
264 }
265
da9063_wdt_suspend(struct device * dev)266 static int da9063_wdt_suspend(struct device *dev)
267 {
268 struct watchdog_device *wdd = dev_get_drvdata(dev);
269 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
270
271 if (!da9063->use_sw_pm)
272 return 0;
273
274 if (watchdog_active(wdd))
275 return da9063_wdt_stop(wdd);
276
277 return 0;
278 }
279
da9063_wdt_resume(struct device * dev)280 static int da9063_wdt_resume(struct device *dev)
281 {
282 struct watchdog_device *wdd = dev_get_drvdata(dev);
283 struct da9063 *da9063 = watchdog_get_drvdata(wdd);
284
285 if (!da9063->use_sw_pm)
286 return 0;
287
288 if (watchdog_active(wdd))
289 return da9063_wdt_start(wdd);
290
291 return 0;
292 }
293
294 static DEFINE_SIMPLE_DEV_PM_OPS(da9063_wdt_pm_ops, da9063_wdt_suspend,
295 da9063_wdt_resume);
296
297 static struct platform_driver da9063_wdt_driver = {
298 .probe = da9063_wdt_probe,
299 .driver = {
300 .name = DA9063_DRVNAME_WATCHDOG,
301 .pm = pm_sleep_ptr(&da9063_wdt_pm_ops),
302 },
303 };
304 module_platform_driver(da9063_wdt_driver);
305
306 MODULE_AUTHOR("Mariusz Wojtasik <mariusz.wojtasik@diasemi.com>");
307 MODULE_DESCRIPTION("Watchdog driver for Dialog DA9063");
308 MODULE_LICENSE("GPL");
309 MODULE_ALIAS("platform:" DA9063_DRVNAME_WATCHDOG);
310