1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// SPDX-License-Identifier: GPL-2.0
/*
* Nuvoton NCT6694 WDT driver based on USB interface.
*
* Copyright (C) 2025 Nuvoton Technology Corp.
*/
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/mfd/nct6694.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/watchdog.h>
#define DEVICE_NAME "nct6694-wdt"
#define NCT6694_DEFAULT_TIMEOUT 10
#define NCT6694_DEFAULT_PRETIMEOUT 0
#define NCT6694_WDT_MAX_DEVS 2
/*
* USB command module type for NCT6694 WDT controller.
* This defines the module type used for communication with the NCT6694
* WDT controller over the USB interface.
*/
#define NCT6694_WDT_MOD 0x07
/* Command 00h - WDT Setup */
#define NCT6694_WDT_SETUP 0x00
#define NCT6694_WDT_SETUP_SEL(idx) (idx ? 0x01 : 0x00)
/* Command 01h - WDT Command */
#define NCT6694_WDT_COMMAND 0x01
#define NCT6694_WDT_COMMAND_SEL(idx) (idx ? 0x01 : 0x00)
static unsigned int timeout[NCT6694_WDT_MAX_DEVS] = {
[0 ... (NCT6694_WDT_MAX_DEVS - 1)] = NCT6694_DEFAULT_TIMEOUT
};
module_param_array(timeout, int, NULL, 0644);
MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
static unsigned int pretimeout[NCT6694_WDT_MAX_DEVS] = {
[0 ... (NCT6694_WDT_MAX_DEVS - 1)] = NCT6694_DEFAULT_PRETIMEOUT
};
module_param_array(pretimeout, int, NULL, 0644);
MODULE_PARM_DESC(pretimeout, "Watchdog pre-timeout in seconds");
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
enum {
NCT6694_ACTION_NONE = 0,
NCT6694_ACTION_SIRQ,
NCT6694_ACTION_GPO,
};
struct __packed nct6694_wdt_setup {
__le32 pretimeout;
__le32 timeout;
u8 owner;
u8 scratch;
u8 control;
u8 status;
__le32 countdown;
};
struct __packed nct6694_wdt_cmd {
__le32 wdt_cmd;
__le32 reserved;
};
union __packed nct6694_wdt_msg {
struct nct6694_wdt_setup setup;
struct nct6694_wdt_cmd cmd;
};
struct nct6694_wdt_data {
struct watchdog_device wdev;
struct device *dev;
struct nct6694 *nct6694;
union nct6694_wdt_msg *msg;
unsigned char wdev_idx;
};
static int nct6694_wdt_setting(struct watchdog_device *wdev,
u32 timeout_val, u8 timeout_act,
u32 pretimeout_val, u8 pretimeout_act)
{
struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev);
struct nct6694_wdt_setup *setup = &data->msg->setup;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_WDT_MOD,
.cmd = NCT6694_WDT_SETUP,
.sel = NCT6694_WDT_SETUP_SEL(data->wdev_idx),
.len = cpu_to_le16(sizeof(*setup))
};
unsigned int timeout_fmt, pretimeout_fmt;
if (pretimeout_val == 0)
pretimeout_act = NCT6694_ACTION_NONE;
timeout_fmt = (timeout_val * 1000) | (timeout_act << 24);
pretimeout_fmt = (pretimeout_val * 1000) | (pretimeout_act << 24);
memset(setup, 0, sizeof(*setup));
setup->timeout = cpu_to_le32(timeout_fmt);
setup->pretimeout = cpu_to_le32(pretimeout_fmt);
return nct6694_write_msg(data->nct6694, &cmd_hd, setup);
}
static int nct6694_wdt_start(struct watchdog_device *wdev)
{
struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev);
int ret;
ret = nct6694_wdt_setting(wdev, wdev->timeout, NCT6694_ACTION_GPO,
wdev->pretimeout, NCT6694_ACTION_GPO);
if (ret)
return ret;
dev_dbg(data->dev, "Setting WDT(%d): timeout = %d, pretimeout = %d\n",
data->wdev_idx, wdev->timeout, wdev->pretimeout);
return ret;
}
static int nct6694_wdt_stop(struct watchdog_device *wdev)
{
struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev);
struct nct6694_wdt_cmd *cmd = &data->msg->cmd;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_WDT_MOD,
.cmd = NCT6694_WDT_COMMAND,
.sel = NCT6694_WDT_COMMAND_SEL(data->wdev_idx),
.len = cpu_to_le16(sizeof(*cmd))
};
memcpy(&cmd->wdt_cmd, "WDTC", 4);
cmd->reserved = 0;
return nct6694_write_msg(data->nct6694, &cmd_hd, cmd);
}
static int nct6694_wdt_ping(struct watchdog_device *wdev)
{
struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev);
struct nct6694_wdt_cmd *cmd = &data->msg->cmd;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_WDT_MOD,
.cmd = NCT6694_WDT_COMMAND,
.sel = NCT6694_WDT_COMMAND_SEL(data->wdev_idx),
.len = cpu_to_le16(sizeof(*cmd))
};
memcpy(&cmd->wdt_cmd, "WDTS", 4);
cmd->reserved = 0;
return nct6694_write_msg(data->nct6694, &cmd_hd, cmd);
}
static int nct6694_wdt_set_timeout(struct watchdog_device *wdev,
unsigned int new_timeout)
{
int ret;
ret = nct6694_wdt_setting(wdev, new_timeout, NCT6694_ACTION_GPO,
wdev->pretimeout, NCT6694_ACTION_GPO);
if (ret)
return ret;
wdev->timeout = new_timeout;
return 0;
}
static int nct6694_wdt_set_pretimeout(struct watchdog_device *wdev,
unsigned int new_pretimeout)
{
int ret;
ret = nct6694_wdt_setting(wdev, wdev->timeout, NCT6694_ACTION_GPO,
new_pretimeout, NCT6694_ACTION_GPO);
if (ret)
return ret;
wdev->pretimeout = new_pretimeout;
return 0;
}
static unsigned int nct6694_wdt_get_time(struct watchdog_device *wdev)
{
struct nct6694_wdt_data *data = watchdog_get_drvdata(wdev);
struct nct6694_wdt_setup *setup = &data->msg->setup;
const struct nct6694_cmd_header cmd_hd = {
.mod = NCT6694_WDT_MOD,
.cmd = NCT6694_WDT_SETUP,
.sel = NCT6694_WDT_SETUP_SEL(data->wdev_idx),
.len = cpu_to_le16(sizeof(*setup))
};
unsigned int timeleft_ms;
int ret;
ret = nct6694_read_msg(data->nct6694, &cmd_hd, setup);
if (ret)
return 0;
timeleft_ms = le32_to_cpu(setup->countdown);
return timeleft_ms / 1000;
}
static const struct watchdog_info nct6694_wdt_info = {
.options = WDIOF_SETTIMEOUT |
WDIOF_KEEPALIVEPING |
WDIOF_MAGICCLOSE |
WDIOF_PRETIMEOUT,
.identity = DEVICE_NAME,
};
static const struct watchdog_ops nct6694_wdt_ops = {
.owner = THIS_MODULE,
.start = nct6694_wdt_start,
.stop = nct6694_wdt_stop,
.set_timeout = nct6694_wdt_set_timeout,
.set_pretimeout = nct6694_wdt_set_pretimeout,
.get_timeleft = nct6694_wdt_get_time,
.ping = nct6694_wdt_ping,
};
static void nct6694_wdt_ida_free(void *d)
{
struct nct6694_wdt_data *data = d;
struct nct6694 *nct6694 = data->nct6694;
ida_free(&nct6694->wdt_ida, data->wdev_idx);
}
static int nct6694_wdt_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct nct6694 *nct6694 = dev_get_drvdata(dev->parent);
struct nct6694_wdt_data *data;
struct watchdog_device *wdev;
int ret;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
data->msg = devm_kzalloc(dev, sizeof(union nct6694_wdt_msg),
GFP_KERNEL);
if (!data->msg)
return -ENOMEM;
data->dev = dev;
data->nct6694 = nct6694;
ret = ida_alloc(&nct6694->wdt_ida, GFP_KERNEL);
if (ret < 0)
return ret;
data->wdev_idx = ret;
ret = devm_add_action_or_reset(dev, nct6694_wdt_ida_free, data);
if (ret)
return ret;
wdev = &data->wdev;
wdev->info = &nct6694_wdt_info;
wdev->ops = &nct6694_wdt_ops;
wdev->timeout = timeout[data->wdev_idx];
wdev->pretimeout = pretimeout[data->wdev_idx];
if (timeout[data->wdev_idx] < pretimeout[data->wdev_idx]) {
dev_warn(data->dev, "pretimeout < timeout. Setting to zero\n");
wdev->pretimeout = 0;
}
wdev->min_timeout = 1;
wdev->max_timeout = 255;
platform_set_drvdata(pdev, data);
watchdog_set_drvdata(&data->wdev, data);
watchdog_set_nowayout(&data->wdev, nowayout);
watchdog_stop_on_reboot(&data->wdev);
return devm_watchdog_register_device(dev, &data->wdev);
}
static struct platform_driver nct6694_wdt_driver = {
.driver = {
.name = DEVICE_NAME,
},
.probe = nct6694_wdt_probe,
};
module_platform_driver(nct6694_wdt_driver);
MODULE_DESCRIPTION("USB-WDT driver for NCT6694");
MODULE_AUTHOR("Ming Yu <tmyu0@nuvoton.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:nct6694-wdt");
|