1 // SPDX-License-Identifier: GPL-2.0+
3 * System monitoring driver for DA9055 PMICs.
5 * Copyright(c) 2012 Dialog Semiconductor Ltd.
7 * Author: David Dajun Chen <dchen@diasemi.com>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/platform_device.h>
16 #include <linux/watchdog.h>
17 #include <linux/delay.h>
19 #include <linux/mfd/da9055/core.h>
20 #include <linux/mfd/da9055/reg.h>
22 static bool nowayout = WATCHDOG_NOWAYOUT;
23 module_param(nowayout, bool, 0);
24 MODULE_PARM_DESC(nowayout,
25 "Watchdog cannot be stopped once started (default="
26 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
28 #define DA9055_DEF_TIMEOUT 4
29 #define DA9055_TWDMIN 256
31 struct da9055_wdt_data {
32 struct watchdog_device wdt;
33 struct da9055 *da9055;
38 int user_time; /* In seconds */
39 } da9055_wdt_maps[] = {
46 { 5, 33 }, /* Actual time 32.768s so included both 32s and 33s */
48 { 6, 66 }, /* Actual time 65.536s so include both, 65s and 66s */
52 static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
55 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
56 struct da9055 *da9055 = driver_data->da9055;
59 for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
60 if (da9055_wdt_maps[i].user_time == timeout)
63 if (i == ARRAY_SIZE(da9055_wdt_maps))
66 ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
68 da9055_wdt_maps[i].reg_val <<
69 DA9055_TWDSCALE_SHIFT);
72 "Failed to update timescale bit, %d\n", ret);
76 wdt_dev->timeout = timeout;
81 static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
83 struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
84 struct da9055 *da9055 = driver_data->da9055;
87 * We have a minimum time for watchdog window called TWDMIN. A write
88 * to the watchdog before this elapsed time will cause an error.
90 mdelay(DA9055_TWDMIN);
92 /* Reset the watchdog timer */
93 return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
94 DA9055_WATCHDOG_MASK, 1);
97 static int da9055_wdt_start(struct watchdog_device *wdt_dev)
99 return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
102 static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
104 return da9055_wdt_set_timeout(wdt_dev, 0);
107 static const struct watchdog_info da9055_wdt_info = {
108 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
109 .identity = "DA9055 Watchdog",
112 static const struct watchdog_ops da9055_wdt_ops = {
113 .owner = THIS_MODULE,
114 .start = da9055_wdt_start,
115 .stop = da9055_wdt_stop,
116 .ping = da9055_wdt_ping,
117 .set_timeout = da9055_wdt_set_timeout,
120 static int da9055_wdt_probe(struct platform_device *pdev)
122 struct device *dev = &pdev->dev;
123 struct da9055 *da9055 = dev_get_drvdata(dev->parent);
124 struct da9055_wdt_data *driver_data;
125 struct watchdog_device *da9055_wdt;
128 driver_data = devm_kzalloc(dev, sizeof(*driver_data), GFP_KERNEL);
132 driver_data->da9055 = da9055;
134 da9055_wdt = &driver_data->wdt;
136 da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
137 da9055_wdt->info = &da9055_wdt_info;
138 da9055_wdt->ops = &da9055_wdt_ops;
139 da9055_wdt->parent = dev;
140 watchdog_set_nowayout(da9055_wdt, nowayout);
141 watchdog_set_drvdata(da9055_wdt, driver_data);
143 ret = da9055_wdt_stop(da9055_wdt);
145 dev_err(dev, "Failed to stop watchdog, %d\n", ret);
149 ret = devm_watchdog_register_device(dev, &driver_data->wdt);
151 dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
157 static struct platform_driver da9055_wdt_driver = {
158 .probe = da9055_wdt_probe,
160 .name = "da9055-watchdog",
164 module_platform_driver(da9055_wdt_driver);
166 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
167 MODULE_DESCRIPTION("DA9055 watchdog");
168 MODULE_LICENSE("GPL");
169 MODULE_ALIAS("platform:da9055-watchdog");