2 * Maxim MAX77620 Watchdog Driver
4 * Copyright (C) 2016 NVIDIA CORPORATION. All rights reserved.
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/mfd/max77620.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;
29 struct watchdog_device wdt_dev;
32 static int max77620_wdt_start(struct watchdog_device *wdt_dev)
34 struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
36 return regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
37 MAX77620_WDTEN, MAX77620_WDTEN);
40 static int max77620_wdt_stop(struct watchdog_device *wdt_dev)
42 struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
44 return regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
48 static int max77620_wdt_ping(struct watchdog_device *wdt_dev)
50 struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
52 return regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL3,
53 MAX77620_WDTC_MASK, 0x1);
56 static int max77620_wdt_set_timeout(struct watchdog_device *wdt_dev,
59 struct max77620_wdt *wdt = watchdog_get_drvdata(wdt_dev);
60 unsigned int wdt_timeout;
66 regval = MAX77620_TWD_2s;
71 regval = MAX77620_TWD_16s;
76 regval = MAX77620_TWD_64s;
81 regval = MAX77620_TWD_128s;
86 ret = regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL3,
87 MAX77620_WDTC_MASK, 0x1);
91 ret = regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
92 MAX77620_TWD_MASK, regval);
96 wdt_dev->timeout = wdt_timeout;
101 static const struct watchdog_info max77620_wdt_info = {
102 .identity = "max77620-watchdog",
103 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
106 static const struct watchdog_ops max77620_wdt_ops = {
107 .start = max77620_wdt_start,
108 .stop = max77620_wdt_stop,
109 .ping = max77620_wdt_ping,
110 .set_timeout = max77620_wdt_set_timeout,
113 static int max77620_wdt_probe(struct platform_device *pdev)
115 struct device *dev = &pdev->dev;
116 struct max77620_wdt *wdt;
117 struct watchdog_device *wdt_dev;
121 wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
126 wdt->rmap = dev_get_regmap(dev->parent, NULL);
128 dev_err(wdt->dev, "Failed to get parent regmap\n");
132 wdt_dev = &wdt->wdt_dev;
133 wdt_dev->info = &max77620_wdt_info;
134 wdt_dev->ops = &max77620_wdt_ops;
135 wdt_dev->min_timeout = 2;
136 wdt_dev->max_timeout = 128;
137 wdt_dev->max_hw_heartbeat_ms = 128 * 1000;
139 platform_set_drvdata(pdev, wdt);
141 /* Enable WD_RST_WK - WDT expire results in a restart */
142 ret = regmap_update_bits(wdt->rmap, MAX77620_REG_ONOFFCNFG2,
143 MAX77620_ONOFFCNFG2_WD_RST_WK,
144 MAX77620_ONOFFCNFG2_WD_RST_WK);
146 dev_err(wdt->dev, "Failed to set WD_RST_WK: %d\n", ret);
150 /* Set WDT clear in OFF and sleep mode */
151 ret = regmap_update_bits(wdt->rmap, MAX77620_REG_CNFGGLBL2,
152 MAX77620_WDTOFFC | MAX77620_WDTSLPC,
153 MAX77620_WDTOFFC | MAX77620_WDTSLPC);
155 dev_err(wdt->dev, "Failed to set WDT OFF mode: %d\n", ret);
159 /* Check if WDT running and if yes then set flags properly */
160 ret = regmap_read(wdt->rmap, MAX77620_REG_CNFGGLBL2, ®val);
162 dev_err(wdt->dev, "Failed to read WDT CFG register: %d\n", ret);
166 switch (regval & MAX77620_TWD_MASK) {
167 case MAX77620_TWD_2s:
168 wdt_dev->timeout = 2;
170 case MAX77620_TWD_16s:
171 wdt_dev->timeout = 16;
173 case MAX77620_TWD_64s:
174 wdt_dev->timeout = 64;
177 wdt_dev->timeout = 128;
181 if (regval & MAX77620_WDTEN)
182 set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
184 watchdog_set_nowayout(wdt_dev, nowayout);
185 watchdog_set_drvdata(wdt_dev, wdt);
187 watchdog_stop_on_unregister(wdt_dev);
188 ret = devm_watchdog_register_device(dev, wdt_dev);
190 dev_err(dev, "watchdog registration failed: %d\n", ret);
197 static const struct platform_device_id max77620_wdt_devtype[] = {
198 { .name = "max77620-watchdog", },
201 MODULE_DEVICE_TABLE(platform, max77620_wdt_devtype);
203 static struct platform_driver max77620_wdt_driver = {
205 .name = "max77620-watchdog",
207 .probe = max77620_wdt_probe,
208 .id_table = max77620_wdt_devtype,
211 module_platform_driver(max77620_wdt_driver);
213 MODULE_DESCRIPTION("Max77620 watchdog timer driver");
215 module_param(nowayout, bool, 0);
216 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
217 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
219 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
220 MODULE_LICENSE("GPL v2");