xref: /openbmc/linux/drivers/watchdog/da9062_wdt.c (revision 5d331b7f)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Watchdog device driver for DA9062 and DA9061 PMICs
4  * Copyright (C) 2015  Dialog Semiconductor Ltd.
5  *
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/watchdog.h>
11 #include <linux/platform_device.h>
12 #include <linux/uaccess.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/mfd/da9062/registers.h>
17 #include <linux/mfd/da9062/core.h>
18 #include <linux/regmap.h>
19 #include <linux/of.h>
20 
21 static const unsigned int wdt_timeout[] = { 0, 2, 4, 8, 16, 32, 65, 131 };
22 #define DA9062_TWDSCALE_DISABLE		0
23 #define DA9062_TWDSCALE_MIN		1
24 #define DA9062_TWDSCALE_MAX		(ARRAY_SIZE(wdt_timeout) - 1)
25 #define DA9062_WDT_MIN_TIMEOUT		wdt_timeout[DA9062_TWDSCALE_MIN]
26 #define DA9062_WDT_MAX_TIMEOUT		wdt_timeout[DA9062_TWDSCALE_MAX]
27 #define DA9062_WDG_DEFAULT_TIMEOUT	wdt_timeout[DA9062_TWDSCALE_MAX-1]
28 #define DA9062_RESET_PROTECTION_MS	300
29 
30 struct da9062_watchdog {
31 	struct da9062 *hw;
32 	struct watchdog_device wdtdev;
33 };
34 
35 static unsigned int da9062_wdt_timeout_to_sel(unsigned int secs)
36 {
37 	unsigned int i;
38 
39 	for (i = DA9062_TWDSCALE_MIN; i <= DA9062_TWDSCALE_MAX; i++) {
40 		if (wdt_timeout[i] >= secs)
41 			return i;
42 	}
43 
44 	return DA9062_TWDSCALE_MAX;
45 }
46 
47 static int da9062_reset_watchdog_timer(struct da9062_watchdog *wdt)
48 {
49 	int ret;
50 
51 	ret = regmap_update_bits(wdt->hw->regmap,
52 			   DA9062AA_CONTROL_F,
53 			   DA9062AA_WATCHDOG_MASK,
54 			   DA9062AA_WATCHDOG_MASK);
55 
56 	return ret;
57 }
58 
59 static int da9062_wdt_update_timeout_register(struct da9062_watchdog *wdt,
60 					      unsigned int regval)
61 {
62 	struct da9062 *chip = wdt->hw;
63 	int ret;
64 
65 	ret = da9062_reset_watchdog_timer(wdt);
66 	if (ret)
67 		return ret;
68 
69 	regmap_update_bits(chip->regmap,
70 				  DA9062AA_CONTROL_D,
71 				  DA9062AA_TWDSCALE_MASK,
72 				  DA9062_TWDSCALE_DISABLE);
73 
74 	usleep_range(150, 300);
75 
76 	return regmap_update_bits(chip->regmap,
77 				  DA9062AA_CONTROL_D,
78 				  DA9062AA_TWDSCALE_MASK,
79 				  regval);
80 }
81 
82 static int da9062_wdt_start(struct watchdog_device *wdd)
83 {
84 	struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
85 	unsigned int selector;
86 	int ret;
87 
88 	selector = da9062_wdt_timeout_to_sel(wdt->wdtdev.timeout);
89 	ret = da9062_wdt_update_timeout_register(wdt, selector);
90 	if (ret)
91 		dev_err(wdt->hw->dev, "Watchdog failed to start (err = %d)\n",
92 			ret);
93 
94 	return ret;
95 }
96 
97 static int da9062_wdt_stop(struct watchdog_device *wdd)
98 {
99 	struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
100 	int ret;
101 
102 	ret = da9062_reset_watchdog_timer(wdt);
103 	if (ret) {
104 		dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
105 			ret);
106 		return ret;
107 	}
108 
109 	ret = regmap_update_bits(wdt->hw->regmap,
110 				 DA9062AA_CONTROL_D,
111 				 DA9062AA_TWDSCALE_MASK,
112 				 DA9062_TWDSCALE_DISABLE);
113 	if (ret)
114 		dev_err(wdt->hw->dev, "Watchdog failed to stop (err = %d)\n",
115 			ret);
116 
117 	return ret;
118 }
119 
120 static int da9062_wdt_ping(struct watchdog_device *wdd)
121 {
122 	struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
123 	int ret;
124 
125 	ret = da9062_reset_watchdog_timer(wdt);
126 	if (ret)
127 		dev_err(wdt->hw->dev, "Failed to ping the watchdog (err = %d)\n",
128 			ret);
129 
130 	return ret;
131 }
132 
133 static int da9062_wdt_set_timeout(struct watchdog_device *wdd,
134 				  unsigned int timeout)
135 {
136 	struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
137 	unsigned int selector;
138 	int ret;
139 
140 	selector = da9062_wdt_timeout_to_sel(timeout);
141 	ret = da9062_wdt_update_timeout_register(wdt, selector);
142 	if (ret)
143 		dev_err(wdt->hw->dev, "Failed to set watchdog timeout (err = %d)\n",
144 			ret);
145 	else
146 		wdd->timeout = wdt_timeout[selector];
147 
148 	return ret;
149 }
150 
151 static int da9062_wdt_restart(struct watchdog_device *wdd, unsigned long action,
152 			      void *data)
153 {
154 	struct da9062_watchdog *wdt = watchdog_get_drvdata(wdd);
155 	int ret;
156 
157 	ret = regmap_write(wdt->hw->regmap,
158 			   DA9062AA_CONTROL_F,
159 			   DA9062AA_SHUTDOWN_MASK);
160 	if (ret)
161 		dev_alert(wdt->hw->dev, "Failed to shutdown (err = %d)\n",
162 			  ret);
163 
164 	/* wait for reset to assert... */
165 	mdelay(500);
166 
167 	return ret;
168 }
169 
170 static const struct watchdog_info da9062_watchdog_info = {
171 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
172 	.identity = "DA9062 WDT",
173 };
174 
175 static const struct watchdog_ops da9062_watchdog_ops = {
176 	.owner = THIS_MODULE,
177 	.start = da9062_wdt_start,
178 	.stop = da9062_wdt_stop,
179 	.ping = da9062_wdt_ping,
180 	.set_timeout = da9062_wdt_set_timeout,
181 	.restart = da9062_wdt_restart,
182 };
183 
184 static const struct of_device_id da9062_compatible_id_table[] = {
185 	{ .compatible = "dlg,da9062-watchdog", },
186 	{ },
187 };
188 
189 MODULE_DEVICE_TABLE(of, da9062_compatible_id_table);
190 
191 static int da9062_wdt_probe(struct platform_device *pdev)
192 {
193 	int ret;
194 	struct da9062 *chip;
195 	struct da9062_watchdog *wdt;
196 
197 	chip = dev_get_drvdata(pdev->dev.parent);
198 	if (!chip)
199 		return -EINVAL;
200 
201 	wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
202 	if (!wdt)
203 		return -ENOMEM;
204 
205 	wdt->hw = chip;
206 
207 	wdt->wdtdev.info = &da9062_watchdog_info;
208 	wdt->wdtdev.ops = &da9062_watchdog_ops;
209 	wdt->wdtdev.min_timeout = DA9062_WDT_MIN_TIMEOUT;
210 	wdt->wdtdev.max_timeout = DA9062_WDT_MAX_TIMEOUT;
211 	wdt->wdtdev.min_hw_heartbeat_ms = DA9062_RESET_PROTECTION_MS;
212 	wdt->wdtdev.timeout = DA9062_WDG_DEFAULT_TIMEOUT;
213 	wdt->wdtdev.status = WATCHDOG_NOWAYOUT_INIT_STATUS;
214 	wdt->wdtdev.parent = &pdev->dev;
215 
216 	watchdog_set_restart_priority(&wdt->wdtdev, 128);
217 
218 	watchdog_set_drvdata(&wdt->wdtdev, wdt);
219 
220 	ret = devm_watchdog_register_device(&pdev->dev, &wdt->wdtdev);
221 	if (ret < 0) {
222 		dev_err(wdt->hw->dev,
223 			"watchdog registration failed (%d)\n", ret);
224 		return ret;
225 	}
226 
227 	return da9062_wdt_ping(&wdt->wdtdev);
228 }
229 
230 static struct platform_driver da9062_wdt_driver = {
231 	.probe = da9062_wdt_probe,
232 	.driver = {
233 		.name = "da9062-watchdog",
234 		.of_match_table = da9062_compatible_id_table,
235 	},
236 };
237 module_platform_driver(da9062_wdt_driver);
238 
239 MODULE_AUTHOR("S Twiss <stwiss.opensource@diasemi.com>");
240 MODULE_DESCRIPTION("WDT device driver for Dialog DA9062 and DA9061");
241 MODULE_LICENSE("GPL");
242 MODULE_ALIAS("platform:da9062-watchdog");
243