1 // SPDX-License-Identifier: GPL-2.0-only
3 * Maxim MAX77620 Watchdog Driver
5 * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
6 * Copyright (C) 2022 Luca Ceresoli
8 * Author: Laxman Dewangan <ldewangan@nvidia.com>
9 * Author: Luca Ceresoli <luca.ceresoli@bootlin.com>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/mfd/max77620.h>
18 #include <linux/mfd/max77714.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/watchdog.h>
24 static bool nowayout
= WATCHDOG_NOWAYOUT
;
27 * struct max77620_variant - Data specific to a chip variant
28 * @wdt_info: watchdog descriptor
29 * @reg_onoff_cnfg2: ONOFF_CNFG2 register offset
30 * @reg_cnfg_glbl2: CNFG_GLBL2 register offset
31 * @reg_cnfg_glbl3: CNFG_GLBL3 register offset
32 * @wdtc_mask: WDTC bit mask in CNFG_GLBL3 (=bits to update to ping the watchdog)
33 * @bit_wd_rst_wk: WD_RST_WK bit offset within ONOFF_CNFG2
34 * @cnfg_glbl2_cfg_bits: configuration bits to enable in CNFG_GLBL2 register
36 struct max77620_variant
{
42 u8 cnfg_glbl2_cfg_bits
;
48 const struct max77620_variant
*drv_data
;
49 struct watchdog_device wdt_dev
;
52 static const struct max77620_variant max77620_wdt_data
= {
53 .reg_onoff_cnfg2
= MAX77620_REG_ONOFFCNFG2
,
54 .reg_cnfg_glbl2
= MAX77620_REG_CNFGGLBL2
,
55 .reg_cnfg_glbl3
= MAX77620_REG_CNFGGLBL3
,
56 .wdtc_mask
= MAX77620_WDTC_MASK
,
57 .bit_wd_rst_wk
= MAX77620_ONOFFCNFG2_WD_RST_WK
,
58 /* Set WDT clear in OFF and sleep mode */
59 .cnfg_glbl2_cfg_bits
= MAX77620_WDTSLPC
| MAX77620_WDTOFFC
,
62 static const struct max77620_variant max77714_wdt_data
= {
63 .reg_onoff_cnfg2
= MAX77714_CNFG2_ONOFF
,
64 .reg_cnfg_glbl2
= MAX77714_CNFG_GLBL2
,
65 .reg_cnfg_glbl3
= MAX77714_CNFG_GLBL3
,
66 .wdtc_mask
= MAX77714_WDTC
,
67 .bit_wd_rst_wk
= MAX77714_WD_RST_WK
,
68 /* Set WDT clear in sleep mode (there is no WDTOFFC on MAX77714) */
69 .cnfg_glbl2_cfg_bits
= MAX77714_WDTSLPC
,
72 static int max77620_wdt_start(struct watchdog_device
*wdt_dev
)
74 struct max77620_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
76 return regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl2
,
77 MAX77620_WDTEN
, MAX77620_WDTEN
);
80 static int max77620_wdt_stop(struct watchdog_device
*wdt_dev
)
82 struct max77620_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
84 return regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl2
,
88 static int max77620_wdt_ping(struct watchdog_device
*wdt_dev
)
90 struct max77620_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
92 return regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl3
,
93 wdt
->drv_data
->wdtc_mask
, 0x1);
96 static int max77620_wdt_set_timeout(struct watchdog_device
*wdt_dev
,
99 struct max77620_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
100 unsigned int wdt_timeout
;
106 regval
= MAX77620_TWD_2s
;
111 regval
= MAX77620_TWD_16s
;
116 regval
= MAX77620_TWD_64s
;
121 regval
= MAX77620_TWD_128s
;
127 * "If the value of TWD needs to be changed, clear the system
128 * watchdog timer first [...], then change the value of TWD."
129 * (MAX77714 datasheet but applies to MAX77620 too)
131 ret
= regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl3
,
132 wdt
->drv_data
->wdtc_mask
, 0x1);
136 ret
= regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl2
,
137 MAX77620_TWD_MASK
, regval
);
141 wdt_dev
->timeout
= wdt_timeout
;
146 static const struct watchdog_info max77620_wdt_info
= {
147 .identity
= "max77620-watchdog",
148 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
| WDIOF_MAGICCLOSE
,
151 static const struct watchdog_ops max77620_wdt_ops
= {
152 .start
= max77620_wdt_start
,
153 .stop
= max77620_wdt_stop
,
154 .ping
= max77620_wdt_ping
,
155 .set_timeout
= max77620_wdt_set_timeout
,
158 static int max77620_wdt_probe(struct platform_device
*pdev
)
160 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
161 struct device
*dev
= &pdev
->dev
;
162 struct max77620_wdt
*wdt
;
163 struct watchdog_device
*wdt_dev
;
167 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
172 wdt
->drv_data
= (const struct max77620_variant
*) id
->driver_data
;
174 wdt
->rmap
= dev_get_regmap(dev
->parent
, NULL
);
176 dev_err(wdt
->dev
, "Failed to get parent regmap\n");
180 wdt_dev
= &wdt
->wdt_dev
;
181 wdt_dev
->info
= &max77620_wdt_info
;
182 wdt_dev
->ops
= &max77620_wdt_ops
;
183 wdt_dev
->min_timeout
= 2;
184 wdt_dev
->max_timeout
= 128;
185 wdt_dev
->max_hw_heartbeat_ms
= 128 * 1000;
187 platform_set_drvdata(pdev
, wdt
);
189 /* Enable WD_RST_WK - WDT expire results in a restart */
190 ret
= regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_onoff_cnfg2
,
191 wdt
->drv_data
->bit_wd_rst_wk
,
192 wdt
->drv_data
->bit_wd_rst_wk
);
194 dev_err(wdt
->dev
, "Failed to set WD_RST_WK: %d\n", ret
);
198 /* Set the "auto WDT clear" bits available on the chip */
199 ret
= regmap_update_bits(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl2
,
200 wdt
->drv_data
->cnfg_glbl2_cfg_bits
,
201 wdt
->drv_data
->cnfg_glbl2_cfg_bits
);
203 dev_err(wdt
->dev
, "Failed to set WDT OFF mode: %d\n", ret
);
207 /* Check if WDT running and if yes then set flags properly */
208 ret
= regmap_read(wdt
->rmap
, wdt
->drv_data
->reg_cnfg_glbl2
, ®val
);
210 dev_err(wdt
->dev
, "Failed to read WDT CFG register: %d\n", ret
);
214 switch (regval
& MAX77620_TWD_MASK
) {
215 case MAX77620_TWD_2s
:
216 wdt_dev
->timeout
= 2;
218 case MAX77620_TWD_16s
:
219 wdt_dev
->timeout
= 16;
221 case MAX77620_TWD_64s
:
222 wdt_dev
->timeout
= 64;
225 wdt_dev
->timeout
= 128;
229 if (regval
& MAX77620_WDTEN
)
230 set_bit(WDOG_HW_RUNNING
, &wdt_dev
->status
);
232 watchdog_set_nowayout(wdt_dev
, nowayout
);
233 watchdog_set_drvdata(wdt_dev
, wdt
);
235 watchdog_stop_on_unregister(wdt_dev
);
236 return devm_watchdog_register_device(dev
, wdt_dev
);
239 static const struct platform_device_id max77620_wdt_devtype
[] = {
240 { "max77620-watchdog", (kernel_ulong_t
)&max77620_wdt_data
},
241 { "max77714-watchdog", (kernel_ulong_t
)&max77714_wdt_data
},
244 MODULE_DEVICE_TABLE(platform
, max77620_wdt_devtype
);
246 static struct platform_driver max77620_wdt_driver
= {
248 .name
= "max77620-watchdog",
250 .probe
= max77620_wdt_probe
,
251 .id_table
= max77620_wdt_devtype
,
254 module_platform_driver(max77620_wdt_driver
);
256 MODULE_DESCRIPTION("Max77620 watchdog timer driver");
258 module_param(nowayout
, bool, 0);
259 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started "
260 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
262 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
263 MODULE_AUTHOR("Luca Ceresoli <luca.ceresoli@bootlin.com>");
264 MODULE_LICENSE("GPL v2");