xref: /linux/drivers/watchdog/da9055_wdt.c (revision 2e62c4988bbf2b514aa7672c2ee5252531c7155f)
1*2e62c498SMarcus Folkesson // SPDX-License-Identifier: GPL-2.0+
2312b00e1SAshish Jangam /*
3312b00e1SAshish Jangam  * System monitoring driver for DA9055 PMICs.
4312b00e1SAshish Jangam  *
5312b00e1SAshish Jangam  * Copyright(c) 2012 Dialog Semiconductor Ltd.
6312b00e1SAshish Jangam  *
7312b00e1SAshish Jangam  * Author: David Dajun Chen <dchen@diasemi.com>
8312b00e1SAshish Jangam  *
9312b00e1SAshish Jangam  */
10312b00e1SAshish Jangam 
11312b00e1SAshish Jangam #include <linux/module.h>
12312b00e1SAshish Jangam #include <linux/types.h>
13312b00e1SAshish Jangam #include <linux/kernel.h>
14312b00e1SAshish Jangam #include <linux/slab.h>
15312b00e1SAshish Jangam #include <linux/platform_device.h>
16312b00e1SAshish Jangam #include <linux/watchdog.h>
17312b00e1SAshish Jangam #include <linux/delay.h>
18312b00e1SAshish Jangam 
19312b00e1SAshish Jangam #include <linux/mfd/da9055/core.h>
20312b00e1SAshish Jangam #include <linux/mfd/da9055/reg.h>
21312b00e1SAshish Jangam 
22312b00e1SAshish Jangam static bool nowayout = WATCHDOG_NOWAYOUT;
23312b00e1SAshish Jangam module_param(nowayout, bool, 0);
24312b00e1SAshish Jangam MODULE_PARM_DESC(nowayout,
25312b00e1SAshish Jangam 		 "Watchdog cannot be stopped once started (default="
26312b00e1SAshish Jangam 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
27312b00e1SAshish Jangam 
28312b00e1SAshish Jangam #define DA9055_DEF_TIMEOUT	4
29312b00e1SAshish Jangam #define DA9055_TWDMIN		256
30312b00e1SAshish Jangam 
31312b00e1SAshish Jangam struct da9055_wdt_data {
32312b00e1SAshish Jangam 	struct watchdog_device wdt;
33312b00e1SAshish Jangam 	struct da9055 *da9055;
34312b00e1SAshish Jangam };
35312b00e1SAshish Jangam 
36312b00e1SAshish Jangam static const struct {
37312b00e1SAshish Jangam 	u8 reg_val;
38312b00e1SAshish Jangam 	int user_time;  /* In seconds */
39312b00e1SAshish Jangam } da9055_wdt_maps[] = {
40312b00e1SAshish Jangam 	{ 0, 0 },
41312b00e1SAshish Jangam 	{ 1, 2 },
42312b00e1SAshish Jangam 	{ 2, 4 },
43312b00e1SAshish Jangam 	{ 3, 8 },
44312b00e1SAshish Jangam 	{ 4, 16 },
45312b00e1SAshish Jangam 	{ 5, 32 },
46312b00e1SAshish Jangam 	{ 5, 33 },  /* Actual time  32.768s so included both 32s and 33s */
47312b00e1SAshish Jangam 	{ 6, 65 },
48312b00e1SAshish Jangam 	{ 6, 66 },  /* Actual time 65.536s so include both, 65s and 66s */
49312b00e1SAshish Jangam 	{ 7, 131 },
50312b00e1SAshish Jangam };
51312b00e1SAshish Jangam 
52312b00e1SAshish Jangam static int da9055_wdt_set_timeout(struct watchdog_device *wdt_dev,
53312b00e1SAshish Jangam 				  unsigned int timeout)
54312b00e1SAshish Jangam {
55312b00e1SAshish Jangam 	struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
56312b00e1SAshish Jangam 	struct da9055 *da9055 = driver_data->da9055;
57312b00e1SAshish Jangam 	int ret, i;
58312b00e1SAshish Jangam 
59312b00e1SAshish Jangam 	for (i = 0; i < ARRAY_SIZE(da9055_wdt_maps); i++)
60312b00e1SAshish Jangam 		if (da9055_wdt_maps[i].user_time == timeout)
61312b00e1SAshish Jangam 			break;
62312b00e1SAshish Jangam 
63312b00e1SAshish Jangam 	if (i == ARRAY_SIZE(da9055_wdt_maps))
64312b00e1SAshish Jangam 		ret = -EINVAL;
65312b00e1SAshish Jangam 	else
66312b00e1SAshish Jangam 		ret = da9055_reg_update(da9055, DA9055_REG_CONTROL_B,
67312b00e1SAshish Jangam 					DA9055_TWDSCALE_MASK,
68312b00e1SAshish Jangam 					da9055_wdt_maps[i].reg_val <<
69312b00e1SAshish Jangam 					DA9055_TWDSCALE_SHIFT);
7098e4a293SAxel Lin 	if (ret < 0) {
71312b00e1SAshish Jangam 		dev_err(da9055->dev,
72312b00e1SAshish Jangam 			"Failed to update timescale bit, %d\n", ret);
7398e4a293SAxel Lin 		return ret;
7498e4a293SAxel Lin 	}
75312b00e1SAshish Jangam 
76312b00e1SAshish Jangam 	wdt_dev->timeout = timeout;
77312b00e1SAshish Jangam 
7898e4a293SAxel Lin 	return 0;
79312b00e1SAshish Jangam }
80312b00e1SAshish Jangam 
81312b00e1SAshish Jangam static int da9055_wdt_ping(struct watchdog_device *wdt_dev)
82312b00e1SAshish Jangam {
83312b00e1SAshish Jangam 	struct da9055_wdt_data *driver_data = watchdog_get_drvdata(wdt_dev);
84312b00e1SAshish Jangam 	struct da9055 *da9055 = driver_data->da9055;
85312b00e1SAshish Jangam 
86312b00e1SAshish Jangam 	/*
87312b00e1SAshish Jangam 	 * We have a minimum time for watchdog window called TWDMIN. A write
88312b00e1SAshish Jangam 	 * to the watchdog before this elapsed time will cause an error.
89312b00e1SAshish Jangam 	 */
90312b00e1SAshish Jangam 	mdelay(DA9055_TWDMIN);
91312b00e1SAshish Jangam 
92312b00e1SAshish Jangam 	/* Reset the watchdog timer */
9398e4a293SAxel Lin 	return da9055_reg_update(da9055, DA9055_REG_CONTROL_E,
94312b00e1SAshish Jangam 				 DA9055_WATCHDOG_MASK, 1);
95312b00e1SAshish Jangam }
96312b00e1SAshish Jangam 
97312b00e1SAshish Jangam static int da9055_wdt_start(struct watchdog_device *wdt_dev)
98312b00e1SAshish Jangam {
99312b00e1SAshish Jangam 	return da9055_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
100312b00e1SAshish Jangam }
101312b00e1SAshish Jangam 
102312b00e1SAshish Jangam static int da9055_wdt_stop(struct watchdog_device *wdt_dev)
103312b00e1SAshish Jangam {
104312b00e1SAshish Jangam 	return da9055_wdt_set_timeout(wdt_dev, 0);
105312b00e1SAshish Jangam }
106312b00e1SAshish Jangam 
1076c368932SBhumika Goyal static const struct watchdog_info da9055_wdt_info = {
108312b00e1SAshish Jangam 	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
109312b00e1SAshish Jangam 	.identity	= "DA9055 Watchdog",
110312b00e1SAshish Jangam };
111312b00e1SAshish Jangam 
112312b00e1SAshish Jangam static const struct watchdog_ops da9055_wdt_ops = {
113312b00e1SAshish Jangam 	.owner = THIS_MODULE,
114312b00e1SAshish Jangam 	.start = da9055_wdt_start,
115312b00e1SAshish Jangam 	.stop = da9055_wdt_stop,
116312b00e1SAshish Jangam 	.ping = da9055_wdt_ping,
117312b00e1SAshish Jangam 	.set_timeout = da9055_wdt_set_timeout,
118312b00e1SAshish Jangam };
119312b00e1SAshish Jangam 
120312b00e1SAshish Jangam static int da9055_wdt_probe(struct platform_device *pdev)
121312b00e1SAshish Jangam {
122312b00e1SAshish Jangam 	struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
123312b00e1SAshish Jangam 	struct da9055_wdt_data *driver_data;
124312b00e1SAshish Jangam 	struct watchdog_device *da9055_wdt;
125312b00e1SAshish Jangam 	int ret;
126312b00e1SAshish Jangam 
127312b00e1SAshish Jangam 	driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data),
128312b00e1SAshish Jangam 				   GFP_KERNEL);
1298deea830SJingoo Han 	if (!driver_data)
130312b00e1SAshish Jangam 		return -ENOMEM;
131312b00e1SAshish Jangam 
132312b00e1SAshish Jangam 	driver_data->da9055 = da9055;
133312b00e1SAshish Jangam 
134312b00e1SAshish Jangam 	da9055_wdt = &driver_data->wdt;
135312b00e1SAshish Jangam 
136312b00e1SAshish Jangam 	da9055_wdt->timeout = DA9055_DEF_TIMEOUT;
137312b00e1SAshish Jangam 	da9055_wdt->info = &da9055_wdt_info;
138312b00e1SAshish Jangam 	da9055_wdt->ops = &da9055_wdt_ops;
1396551881cSPratyush Anand 	da9055_wdt->parent = &pdev->dev;
140312b00e1SAshish Jangam 	watchdog_set_nowayout(da9055_wdt, nowayout);
141312b00e1SAshish Jangam 	watchdog_set_drvdata(da9055_wdt, driver_data);
142312b00e1SAshish Jangam 
143312b00e1SAshish Jangam 	ret = da9055_wdt_stop(da9055_wdt);
144312b00e1SAshish Jangam 	if (ret < 0) {
145312b00e1SAshish Jangam 		dev_err(&pdev->dev, "Failed to stop watchdog, %d\n", ret);
14689f944c0SGuenter Roeck 		return ret;
147312b00e1SAshish Jangam 	}
148312b00e1SAshish Jangam 
14989f944c0SGuenter Roeck 	ret = devm_watchdog_register_device(&pdev->dev, &driver_data->wdt);
150312b00e1SAshish Jangam 	if (ret != 0)
151312b00e1SAshish Jangam 		dev_err(da9055->dev, "watchdog_register_device() failed: %d\n",
152312b00e1SAshish Jangam 			ret);
153312b00e1SAshish Jangam 
154312b00e1SAshish Jangam 	return ret;
155312b00e1SAshish Jangam }
156312b00e1SAshish Jangam 
157312b00e1SAshish Jangam static struct platform_driver da9055_wdt_driver = {
158312b00e1SAshish Jangam 	.probe = da9055_wdt_probe,
159312b00e1SAshish Jangam 	.driver = {
160312b00e1SAshish Jangam 		.name	= "da9055-watchdog",
161312b00e1SAshish Jangam 	},
162312b00e1SAshish Jangam };
163312b00e1SAshish Jangam 
164312b00e1SAshish Jangam module_platform_driver(da9055_wdt_driver);
165312b00e1SAshish Jangam 
166312b00e1SAshish Jangam MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
167312b00e1SAshish Jangam MODULE_DESCRIPTION("DA9055 watchdog");
168312b00e1SAshish Jangam MODULE_LICENSE("GPL");
169312b00e1SAshish Jangam MODULE_ALIAS("platform:da9055-watchdog");
170