1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 ROHM Semiconductors
3 // ROHM BD70528MWV watchdog driver
6 #include <linux/kernel.h>
7 #include <linux/mfd/rohm-bd70528.h>
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
12 #include <linux/watchdog.h>
15 * Max time we can set is 1 hour, 59 minutes and 59 seconds
16 * and Minimum time is 1 second
18 #define WDT_MAX_MS ((2 * 60 * 60 - 1) * 1000)
19 #define WDT_MIN_MS 1000
20 #define DEFAULT_TIMEOUT 60
22 #define WD_CTRL_MAGIC1 0x55
23 #define WD_CTRL_MAGIC2 0xAA
27 struct regmap
*regmap
;
28 struct rohm_regmap_dev
*mfd
;
29 struct watchdog_device wdt
;
33 * bd70528_wdt_set - arm or disarm watchdog timer
35 * @data: device data for the PMIC instance we want to operate on
36 * @enable: new state of WDT. zero to disable, non zero to enable
37 * @old_state: previous state of WDT will be filled here
39 * Arm or disarm WDT on BD70528 PMIC. Expected to be called only by
40 * BD70528 RTC and BD70528 WDT drivers. The rtc_timer_lock must be taken
41 * by calling bd70528_wdt_lock before calling bd70528_wdt_set.
43 int bd70528_wdt_set(struct rohm_regmap_dev
*data
, int enable
, int *old_state
)
47 struct bd70528_data
*bd70528
= container_of(data
, struct bd70528_data
,
49 u8 wd_ctrl_arr
[3] = { WD_CTRL_MAGIC1
, WD_CTRL_MAGIC2
, 0 };
50 u8
*wd_ctrl
= &wd_ctrl_arr
[2];
52 ret
= regmap_read(bd70528
->chip
.regmap
, BD70528_REG_WDT_CTRL
, &tmp
);
59 if (*wd_ctrl
& BD70528_MASK_WDT_EN
)
60 *old_state
|= BD70528_WDT_STATE_BIT
;
62 *old_state
&= ~BD70528_WDT_STATE_BIT
;
63 if ((!enable
) == (!(*old_state
& BD70528_WDT_STATE_BIT
)))
68 if (*wd_ctrl
& BD70528_MASK_WDT_EN
)
70 *wd_ctrl
|= BD70528_MASK_WDT_EN
;
72 if (*wd_ctrl
& BD70528_MASK_WDT_EN
)
73 *wd_ctrl
&= ~BD70528_MASK_WDT_EN
;
78 for (i
= 0; i
< 3; i
++) {
79 ret
= regmap_write(bd70528
->chip
.regmap
, BD70528_REG_WDT_CTRL
,
85 ret
= regmap_read(bd70528
->chip
.regmap
, BD70528_REG_WDT_CTRL
, &tmp
);
86 if ((tmp
& BD70528_MASK_WDT_EN
) != (*wd_ctrl
& BD70528_MASK_WDT_EN
)) {
87 dev_err(bd70528
->chip
.dev
,
88 "Watchdog ctrl mismatch (hw) 0x%x (set) 0x%x\n",
95 EXPORT_SYMBOL(bd70528_wdt_set
);
98 * bd70528_wdt_lock - take WDT lock
100 * @data: device data for the PMIC instance we want to operate on
102 * Lock WDT for arming/disarming in order to avoid race condition caused
103 * by WDT state changes initiated by WDT and RTC drivers.
105 void bd70528_wdt_lock(struct rohm_regmap_dev
*data
)
107 struct bd70528_data
*bd70528
= container_of(data
, struct bd70528_data
,
110 mutex_lock(&bd70528
->rtc_timer_lock
);
112 EXPORT_SYMBOL(bd70528_wdt_lock
);
115 * bd70528_wdt_unlock - unlock WDT lock
117 * @data: device data for the PMIC instance we want to operate on
119 * Unlock WDT lock which has previously been taken by call to
122 void bd70528_wdt_unlock(struct rohm_regmap_dev
*data
)
124 struct bd70528_data
*bd70528
= container_of(data
, struct bd70528_data
,
127 mutex_unlock(&bd70528
->rtc_timer_lock
);
129 EXPORT_SYMBOL(bd70528_wdt_unlock
);
131 static int bd70528_wdt_set_locked(struct wdtbd70528
*w
, int enable
)
133 return bd70528_wdt_set(w
->mfd
, enable
, NULL
);
136 static int bd70528_wdt_change(struct wdtbd70528
*w
, int enable
)
140 bd70528_wdt_lock(w
->mfd
);
141 ret
= bd70528_wdt_set_locked(w
, enable
);
142 bd70528_wdt_unlock(w
->mfd
);
147 static int bd70528_wdt_start(struct watchdog_device
*wdt
)
149 struct wdtbd70528
*w
= watchdog_get_drvdata(wdt
);
151 dev_dbg(w
->dev
, "WDT ping...\n");
152 return bd70528_wdt_change(w
, 1);
155 static int bd70528_wdt_stop(struct watchdog_device
*wdt
)
157 struct wdtbd70528
*w
= watchdog_get_drvdata(wdt
);
159 dev_dbg(w
->dev
, "WDT stopping...\n");
160 return bd70528_wdt_change(w
, 0);
163 static int bd70528_wdt_set_timeout(struct watchdog_device
*wdt
,
164 unsigned int timeout
)
167 unsigned int minutes
;
168 unsigned int seconds
;
170 struct wdtbd70528
*w
= watchdog_get_drvdata(wdt
);
173 hours
= timeout
/ (60 * 60);
174 /* Maximum timeout is 1h 59m 59s => hours is 1 or 0 */
176 seconds
-= (60 * 60);
177 minutes
= seconds
/ 60;
178 seconds
= seconds
% 60;
180 bd70528_wdt_lock(w
->mfd
);
182 ret
= bd70528_wdt_set_locked(w
, 0);
186 ret
= regmap_update_bits(w
->regmap
, BD70528_REG_WDT_HOUR
,
187 BD70528_MASK_WDT_HOUR
, hours
);
189 dev_err(w
->dev
, "Failed to set WDT hours\n");
192 ret
= regmap_update_bits(w
->regmap
, BD70528_REG_WDT_MINUTE
,
193 BD70528_MASK_WDT_MINUTE
, bin2bcd(minutes
));
195 dev_err(w
->dev
, "Failed to set WDT minutes\n");
198 ret
= regmap_update_bits(w
->regmap
, BD70528_REG_WDT_SEC
,
199 BD70528_MASK_WDT_SEC
, bin2bcd(seconds
));
201 dev_err(w
->dev
, "Failed to set WDT seconds\n");
203 dev_dbg(w
->dev
, "WDT tmo set to %u\n", timeout
);
206 ret
= bd70528_wdt_set_locked(w
, 1);
208 bd70528_wdt_unlock(w
->mfd
);
213 static const struct watchdog_info bd70528_wdt_info
= {
214 .identity
= "bd70528-wdt",
215 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
218 static const struct watchdog_ops bd70528_wdt_ops
= {
219 .start
= bd70528_wdt_start
,
220 .stop
= bd70528_wdt_stop
,
221 .set_timeout
= bd70528_wdt_set_timeout
,
224 static int bd70528_wdt_probe(struct platform_device
*pdev
)
226 struct rohm_regmap_dev
*bd70528
;
227 struct wdtbd70528
*w
;
231 bd70528
= dev_get_drvdata(pdev
->dev
.parent
);
233 dev_err(&pdev
->dev
, "No MFD driver data\n");
236 w
= devm_kzalloc(&pdev
->dev
, sizeof(*w
), GFP_KERNEL
);
240 w
->regmap
= bd70528
->regmap
;
244 w
->wdt
.info
= &bd70528_wdt_info
;
245 w
->wdt
.ops
= &bd70528_wdt_ops
;
246 w
->wdt
.min_hw_heartbeat_ms
= WDT_MIN_MS
;
247 w
->wdt
.max_hw_heartbeat_ms
= WDT_MAX_MS
;
248 w
->wdt
.parent
= pdev
->dev
.parent
;
249 w
->wdt
.timeout
= DEFAULT_TIMEOUT
;
250 watchdog_set_drvdata(&w
->wdt
, w
);
251 watchdog_init_timeout(&w
->wdt
, 0, pdev
->dev
.parent
);
253 ret
= bd70528_wdt_set_timeout(&w
->wdt
, w
->wdt
.timeout
);
255 dev_err(&pdev
->dev
, "Failed to set the watchdog timeout\n");
259 bd70528_wdt_lock(w
->mfd
);
260 ret
= regmap_read(w
->regmap
, BD70528_REG_WDT_CTRL
, ®
);
261 bd70528_wdt_unlock(w
->mfd
);
264 dev_err(&pdev
->dev
, "Failed to get the watchdog state\n");
267 if (reg
& BD70528_MASK_WDT_EN
) {
268 dev_dbg(&pdev
->dev
, "watchdog was running during probe\n");
269 set_bit(WDOG_HW_RUNNING
, &w
->wdt
.status
);
272 ret
= devm_watchdog_register_device(&pdev
->dev
, &w
->wdt
);
274 dev_err(&pdev
->dev
, "watchdog registration failed: %d\n", ret
);
279 static struct platform_driver bd70528_wdt
= {
281 .name
= "bd70528-wdt"
283 .probe
= bd70528_wdt_probe
,
286 module_platform_driver(bd70528_wdt
);
288 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
289 MODULE_DESCRIPTION("BD70528 watchdog driver");
290 MODULE_LICENSE("GPL");
291 MODULE_ALIAS("platform:bd70528-wdt");