xref: /openbmc/linux/drivers/watchdog/ts4800_wdt.c (revision e3d786a3)
1 /*
2  * Watchdog driver for TS-4800 based boards
3  *
4  * Copyright (c) 2015 - Savoir-faire Linux
5  *
6  * This file is licensed under the terms of the GNU General Public
7  * License version 2. This program is licensed "as is" without any
8  * warranty of any kind, whether express or implied.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/watchdog.h>
18 
19 static bool nowayout = WATCHDOG_NOWAYOUT;
20 module_param(nowayout, bool, 0);
21 MODULE_PARM_DESC(nowayout,
22 	"Watchdog cannot be stopped once started (default="
23 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
24 
25 /* possible feed values */
26 #define TS4800_WDT_FEED_2S       0x1
27 #define TS4800_WDT_FEED_10S      0x2
28 #define TS4800_WDT_DISABLE       0x3
29 
30 struct ts4800_wdt {
31 	struct watchdog_device  wdd;
32 	struct regmap           *regmap;
33 	u32                     feed_offset;
34 	u32                     feed_val;
35 };
36 
37 /*
38  * TS-4800 supports the following timeout values:
39  *
40  *   value desc
41  *   ---------------------
42  *     0    feed for 338ms
43  *     1    feed for 2.706s
44  *     2    feed for 10.824s
45  *     3    disable watchdog
46  *
47  * Keep the regmap/timeout map ordered by timeout
48  */
49 static const struct {
50 	const int timeout;
51 	const int regval;
52 } ts4800_wdt_map[] = {
53 	{ 2,  TS4800_WDT_FEED_2S },
54 	{ 10, TS4800_WDT_FEED_10S },
55 };
56 
57 #define MAX_TIMEOUT_INDEX       (ARRAY_SIZE(ts4800_wdt_map) - 1)
58 
59 static void ts4800_write_feed(struct ts4800_wdt *wdt, u32 val)
60 {
61 	regmap_write(wdt->regmap, wdt->feed_offset, val);
62 }
63 
64 static int ts4800_wdt_start(struct watchdog_device *wdd)
65 {
66 	struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd);
67 
68 	ts4800_write_feed(wdt, wdt->feed_val);
69 	return 0;
70 }
71 
72 static int ts4800_wdt_stop(struct watchdog_device *wdd)
73 {
74 	struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd);
75 
76 	ts4800_write_feed(wdt, TS4800_WDT_DISABLE);
77 	return 0;
78 }
79 
80 static int ts4800_wdt_set_timeout(struct watchdog_device *wdd,
81 				  unsigned int timeout)
82 {
83 	struct ts4800_wdt *wdt = watchdog_get_drvdata(wdd);
84 	int i;
85 
86 	for (i = 0; i < MAX_TIMEOUT_INDEX; i++) {
87 		if (ts4800_wdt_map[i].timeout >= timeout)
88 			break;
89 	}
90 
91 	wdd->timeout = ts4800_wdt_map[i].timeout;
92 	wdt->feed_val = ts4800_wdt_map[i].regval;
93 
94 	return 0;
95 }
96 
97 static const struct watchdog_ops ts4800_wdt_ops = {
98 	.owner = THIS_MODULE,
99 	.start = ts4800_wdt_start,
100 	.stop = ts4800_wdt_stop,
101 	.set_timeout = ts4800_wdt_set_timeout,
102 };
103 
104 static const struct watchdog_info ts4800_wdt_info = {
105 	.options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
106 	.identity = "TS-4800 Watchdog",
107 };
108 
109 static int ts4800_wdt_probe(struct platform_device *pdev)
110 {
111 	struct device_node *np = pdev->dev.of_node;
112 	struct device_node *syscon_np;
113 	struct watchdog_device *wdd;
114 	struct ts4800_wdt *wdt;
115 	u32 reg;
116 	int ret;
117 
118 	syscon_np = of_parse_phandle(np, "syscon", 0);
119 	if (!syscon_np) {
120 		dev_err(&pdev->dev, "no syscon property\n");
121 		return -ENODEV;
122 	}
123 
124 	ret = of_property_read_u32_index(np, "syscon", 1, &reg);
125 	if (ret < 0) {
126 		dev_err(&pdev->dev, "no offset in syscon\n");
127 		return ret;
128 	}
129 
130 	/* allocate memory for watchdog struct */
131 	wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
132 	if (!wdt)
133 		return -ENOMEM;
134 
135 	/* set regmap and offset to know where to write */
136 	wdt->feed_offset = reg;
137 	wdt->regmap = syscon_node_to_regmap(syscon_np);
138 	of_node_put(syscon_np);
139 	if (IS_ERR(wdt->regmap)) {
140 		dev_err(&pdev->dev, "cannot get parent's regmap\n");
141 		return PTR_ERR(wdt->regmap);
142 	}
143 
144 	/* Initialize struct watchdog_device */
145 	wdd = &wdt->wdd;
146 	wdd->parent = &pdev->dev;
147 	wdd->info = &ts4800_wdt_info;
148 	wdd->ops = &ts4800_wdt_ops;
149 	wdd->min_timeout = ts4800_wdt_map[0].timeout;
150 	wdd->max_timeout = ts4800_wdt_map[MAX_TIMEOUT_INDEX].timeout;
151 
152 	watchdog_set_drvdata(wdd, wdt);
153 	watchdog_set_nowayout(wdd, nowayout);
154 	watchdog_init_timeout(wdd, 0, &pdev->dev);
155 
156 	/*
157 	 * As this watchdog supports only a few values, ts4800_wdt_set_timeout
158 	 * must be called to initialize timeout and feed_val with valid values.
159 	 * Default to maximum timeout if none, or an invalid one, is provided in
160 	 * device tree.
161 	 */
162 	if (!wdd->timeout)
163 		wdd->timeout = wdd->max_timeout;
164 	ts4800_wdt_set_timeout(wdd, wdd->timeout);
165 
166 	/*
167 	 * The feed register is write-only, so it is not possible to determine
168 	 * watchdog's state. Disable it to be in a known state.
169 	 */
170 	ts4800_wdt_stop(wdd);
171 
172 	ret = watchdog_register_device(wdd);
173 	if (ret) {
174 		dev_err(&pdev->dev,
175 			"failed to register watchdog device\n");
176 		return ret;
177 	}
178 
179 	platform_set_drvdata(pdev, wdt);
180 
181 	dev_info(&pdev->dev,
182 		 "initialized (timeout = %d sec, nowayout = %d)\n",
183 		 wdd->timeout, nowayout);
184 
185 	return 0;
186 }
187 
188 static int ts4800_wdt_remove(struct platform_device *pdev)
189 {
190 	struct ts4800_wdt *wdt = platform_get_drvdata(pdev);
191 
192 	watchdog_unregister_device(&wdt->wdd);
193 
194 	return 0;
195 }
196 
197 static const struct of_device_id ts4800_wdt_of_match[] = {
198 	{ .compatible = "technologic,ts4800-wdt", },
199 	{ },
200 };
201 MODULE_DEVICE_TABLE(of, ts4800_wdt_of_match);
202 
203 static struct platform_driver ts4800_wdt_driver = {
204 	.probe		= ts4800_wdt_probe,
205 	.remove		= ts4800_wdt_remove,
206 	.driver		= {
207 		.name	= "ts4800_wdt",
208 		.of_match_table = ts4800_wdt_of_match,
209 	},
210 };
211 
212 module_platform_driver(ts4800_wdt_driver);
213 
214 MODULE_AUTHOR("Damien Riegel <damien.riegel@savoirfairelinux.com>");
215 MODULE_LICENSE("GPL v2");
216 MODULE_ALIAS("platform:ts4800_wdt");
217