2 * Copyright (C) 2015 Zodiac Inflight Innovations
4 * Author: Martyn Welch <martyn.welch@collabora.co.uk>
6 * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>:
8 * Copyright (C) Nokia Corporation
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/i2c.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
26 #include <linux/types.h>
27 #include <linux/version.h>
28 #include <linux/watchdog.h>
30 #define ZIIRAVE_TIMEOUT_MIN 3
31 #define ZIIRAVE_TIMEOUT_MAX 255
33 #define ZIIRAVE_PING_VALUE 0x0
35 #define ZIIRAVE_STATE_INITIAL 0x0
36 #define ZIIRAVE_STATE_OFF 0x1
37 #define ZIIRAVE_STATE_ON 0x2
39 static char *ziirave_reasons
[] = {"power cycle", "hw watchdog", NULL
, NULL
,
40 "host request", NULL
, "illegal configuration",
41 "illegal instruction", "illegal trap",
44 #define ZIIRAVE_WDT_FIRM_VER_MAJOR 0x1
45 #define ZIIRAVE_WDT_BOOT_VER_MAJOR 0x3
46 #define ZIIRAVE_WDT_RESET_REASON 0x5
47 #define ZIIRAVE_WDT_STATE 0x6
48 #define ZIIRAVE_WDT_TIMEOUT 0x7
49 #define ZIIRAVE_WDT_TIME_LEFT 0x8
50 #define ZIIRAVE_WDT_PING 0x9
51 #define ZIIRAVE_WDT_RESET_DURATION 0xa
53 struct ziirave_wdt_rev
{
58 struct ziirave_wdt_data
{
59 struct watchdog_device wdd
;
60 struct ziirave_wdt_rev bootloader_rev
;
61 struct ziirave_wdt_rev firmware_rev
;
65 static int wdt_timeout
;
66 module_param(wdt_timeout
, int, 0);
67 MODULE_PARM_DESC(wdt_timeout
, "Watchdog timeout in seconds");
69 static int reset_duration
;
70 module_param(reset_duration
, int, 0);
71 MODULE_PARM_DESC(reset_duration
,
72 "Watchdog reset pulse duration in milliseconds");
74 static bool nowayout
= WATCHDOG_NOWAYOUT
;
75 module_param(nowayout
, bool, 0);
76 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started default="
77 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
79 static int ziirave_wdt_revision(struct i2c_client
*client
,
80 struct ziirave_wdt_rev
*rev
, u8 command
)
84 ret
= i2c_smbus_read_byte_data(client
, command
);
90 ret
= i2c_smbus_read_byte_data(client
, command
+ 1);
99 static int ziirave_wdt_set_state(struct watchdog_device
*wdd
, int state
)
101 struct i2c_client
*client
= to_i2c_client(wdd
->parent
);
103 return i2c_smbus_write_byte_data(client
, ZIIRAVE_WDT_STATE
, state
);
106 static int ziirave_wdt_start(struct watchdog_device
*wdd
)
108 return ziirave_wdt_set_state(wdd
, ZIIRAVE_STATE_ON
);
111 static int ziirave_wdt_stop(struct watchdog_device
*wdd
)
113 return ziirave_wdt_set_state(wdd
, ZIIRAVE_STATE_OFF
);
116 static int ziirave_wdt_ping(struct watchdog_device
*wdd
)
118 struct i2c_client
*client
= to_i2c_client(wdd
->parent
);
120 return i2c_smbus_write_byte_data(client
, ZIIRAVE_WDT_PING
,
124 static int ziirave_wdt_set_timeout(struct watchdog_device
*wdd
,
125 unsigned int timeout
)
127 struct i2c_client
*client
= to_i2c_client(wdd
->parent
);
130 ret
= i2c_smbus_write_byte_data(client
, ZIIRAVE_WDT_TIMEOUT
, timeout
);
132 wdd
->timeout
= timeout
;
137 static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device
*wdd
)
139 struct i2c_client
*client
= to_i2c_client(wdd
->parent
);
142 ret
= i2c_smbus_read_byte_data(client
, ZIIRAVE_WDT_TIME_LEFT
);
149 static const struct watchdog_info ziirave_wdt_info
= {
150 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
151 .identity
= "Zodiac RAVE Watchdog",
154 static const struct watchdog_ops ziirave_wdt_ops
= {
155 .owner
= THIS_MODULE
,
156 .start
= ziirave_wdt_start
,
157 .stop
= ziirave_wdt_stop
,
158 .ping
= ziirave_wdt_ping
,
159 .set_timeout
= ziirave_wdt_set_timeout
,
160 .get_timeleft
= ziirave_wdt_get_timeleft
,
163 static ssize_t
ziirave_wdt_sysfs_show_firm(struct device
*dev
,
164 struct device_attribute
*attr
,
167 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
168 struct ziirave_wdt_data
*w_priv
= i2c_get_clientdata(client
);
170 return sprintf(buf
, "02.%02u.%02u", w_priv
->firmware_rev
.major
,
171 w_priv
->firmware_rev
.minor
);
174 static DEVICE_ATTR(firmware_version
, S_IRUGO
, ziirave_wdt_sysfs_show_firm
,
177 static ssize_t
ziirave_wdt_sysfs_show_boot(struct device
*dev
,
178 struct device_attribute
*attr
,
181 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
182 struct ziirave_wdt_data
*w_priv
= i2c_get_clientdata(client
);
184 return sprintf(buf
, "01.%02u.%02u", w_priv
->bootloader_rev
.major
,
185 w_priv
->bootloader_rev
.minor
);
188 static DEVICE_ATTR(bootloader_version
, S_IRUGO
, ziirave_wdt_sysfs_show_boot
,
191 static ssize_t
ziirave_wdt_sysfs_show_reason(struct device
*dev
,
192 struct device_attribute
*attr
,
195 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
196 struct ziirave_wdt_data
*w_priv
= i2c_get_clientdata(client
);
198 return sprintf(buf
, "%s", ziirave_reasons
[w_priv
->reset_reason
]);
201 static DEVICE_ATTR(reset_reason
, S_IRUGO
, ziirave_wdt_sysfs_show_reason
,
204 static struct attribute
*ziirave_wdt_attrs
[] = {
205 &dev_attr_firmware_version
.attr
,
206 &dev_attr_bootloader_version
.attr
,
207 &dev_attr_reset_reason
.attr
,
210 ATTRIBUTE_GROUPS(ziirave_wdt
);
212 static int ziirave_wdt_init_duration(struct i2c_client
*client
)
216 if (!reset_duration
) {
217 /* See if the reset pulse duration is provided in an of_node */
218 if (!client
->dev
.of_node
)
221 ret
= of_property_read_u32(client
->dev
.of_node
,
225 dev_info(&client
->dev
,
226 "Unable to set reset pulse duration, using default\n");
231 if (reset_duration
< 1 || reset_duration
> 255)
234 dev_info(&client
->dev
, "Setting reset duration to %dms",
237 return i2c_smbus_write_byte_data(client
, ZIIRAVE_WDT_RESET_DURATION
,
241 static int ziirave_wdt_probe(struct i2c_client
*client
,
242 const struct i2c_device_id
*id
)
245 struct ziirave_wdt_data
*w_priv
;
248 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
251 w_priv
= devm_kzalloc(&client
->dev
, sizeof(*w_priv
), GFP_KERNEL
);
255 w_priv
->wdd
.info
= &ziirave_wdt_info
;
256 w_priv
->wdd
.ops
= &ziirave_wdt_ops
;
257 w_priv
->wdd
.min_timeout
= ZIIRAVE_TIMEOUT_MIN
;
258 w_priv
->wdd
.max_timeout
= ZIIRAVE_TIMEOUT_MAX
;
259 w_priv
->wdd
.parent
= &client
->dev
;
260 w_priv
->wdd
.groups
= ziirave_wdt_groups
;
262 ret
= watchdog_init_timeout(&w_priv
->wdd
, wdt_timeout
, &client
->dev
);
264 dev_info(&client
->dev
,
265 "Unable to select timeout value, using default\n");
269 * The default value set in the watchdog should be perfectly valid, so
270 * pass that in if we haven't provided one via the module parameter or
273 if (w_priv
->wdd
.timeout
== 0) {
274 val
= i2c_smbus_read_byte_data(client
, ZIIRAVE_WDT_TIMEOUT
);
278 if (val
< ZIIRAVE_TIMEOUT_MIN
)
281 w_priv
->wdd
.timeout
= val
;
283 ret
= ziirave_wdt_set_timeout(&w_priv
->wdd
,
284 w_priv
->wdd
.timeout
);
288 dev_info(&client
->dev
, "Timeout set to %ds.",
289 w_priv
->wdd
.timeout
);
292 watchdog_set_nowayout(&w_priv
->wdd
, nowayout
);
294 i2c_set_clientdata(client
, w_priv
);
296 /* If in unconfigured state, set to stopped */
297 val
= i2c_smbus_read_byte_data(client
, ZIIRAVE_WDT_STATE
);
301 if (val
== ZIIRAVE_STATE_INITIAL
)
302 ziirave_wdt_stop(&w_priv
->wdd
);
304 ret
= ziirave_wdt_init_duration(client
);
308 ret
= ziirave_wdt_revision(client
, &w_priv
->firmware_rev
,
309 ZIIRAVE_WDT_FIRM_VER_MAJOR
);
313 ret
= ziirave_wdt_revision(client
, &w_priv
->bootloader_rev
,
314 ZIIRAVE_WDT_BOOT_VER_MAJOR
);
318 w_priv
->reset_reason
= i2c_smbus_read_byte_data(client
,
319 ZIIRAVE_WDT_RESET_REASON
);
320 if (w_priv
->reset_reason
< 0)
321 return w_priv
->reset_reason
;
323 if (w_priv
->reset_reason
>= ARRAY_SIZE(ziirave_reasons
) ||
324 !ziirave_reasons
[w_priv
->reset_reason
])
327 ret
= watchdog_register_device(&w_priv
->wdd
);
332 static int ziirave_wdt_remove(struct i2c_client
*client
)
334 struct ziirave_wdt_data
*w_priv
= i2c_get_clientdata(client
);
336 watchdog_unregister_device(&w_priv
->wdd
);
341 static struct i2c_device_id ziirave_wdt_id
[] = {
345 MODULE_DEVICE_TABLE(i2c
, ziirave_wdt_id
);
347 static const struct of_device_id zrv_wdt_of_match
[] = {
348 { .compatible
= "zii,rave-wdt", },
351 MODULE_DEVICE_TABLE(of
, zrv_wdt_of_match
);
353 static struct i2c_driver ziirave_wdt_driver
= {
355 .name
= "ziirave_wdt",
356 .of_match_table
= zrv_wdt_of_match
,
358 .probe
= ziirave_wdt_probe
,
359 .remove
= ziirave_wdt_remove
,
360 .id_table
= ziirave_wdt_id
,
363 module_i2c_driver(ziirave_wdt_driver
);
365 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
366 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
367 MODULE_LICENSE("GPL");