xref: /linux/drivers/watchdog/ziirave_wdt.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Zodiac Inflight Innovations
4  *
5  * Author: Martyn Welch <martyn.welch@collabora.co.uk>
6  *
7  * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>:
8  *
9  * Copyright (C) Nokia Corporation
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/ihex.h>
15 #include <linux/firmware.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/types.h>
21 #include <linux/watchdog.h>
22 
23 #include <asm/unaligned.h>
24 
25 #define ZIIRAVE_TIMEOUT_MIN	3
26 #define ZIIRAVE_TIMEOUT_MAX	255
27 #define ZIIRAVE_TIMEOUT_DEFAULT	30
28 
29 #define ZIIRAVE_PING_VALUE	0x0
30 
31 #define ZIIRAVE_STATE_INITIAL	0x0
32 #define ZIIRAVE_STATE_OFF	0x1
33 #define ZIIRAVE_STATE_ON	0x2
34 
35 #define ZIIRAVE_FW_NAME		"ziirave_wdt.fw"
36 
37 static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
38 				  "host request", NULL, "illegal configuration",
39 				  "illegal instruction", "illegal trap",
40 				  "unknown"};
41 
42 #define ZIIRAVE_WDT_FIRM_VER_MAJOR	0x1
43 #define ZIIRAVE_WDT_BOOT_VER_MAJOR	0x3
44 #define ZIIRAVE_WDT_RESET_REASON	0x5
45 #define ZIIRAVE_WDT_STATE		0x6
46 #define ZIIRAVE_WDT_TIMEOUT		0x7
47 #define ZIIRAVE_WDT_TIME_LEFT		0x8
48 #define ZIIRAVE_WDT_PING		0x9
49 #define ZIIRAVE_WDT_RESET_DURATION	0xa
50 
51 #define ZIIRAVE_FIRM_PKT_TOTAL_SIZE	20
52 #define ZIIRAVE_FIRM_PKT_DATA_SIZE	16
53 #define ZIIRAVE_FIRM_FLASH_MEMORY_START	(2 * 0x1600)
54 #define ZIIRAVE_FIRM_FLASH_MEMORY_END	(2 * 0x2bbf)
55 #define ZIIRAVE_FIRM_PAGE_SIZE		128
56 
57 /* Received and ready for next Download packet. */
58 #define ZIIRAVE_FIRM_DOWNLOAD_ACK	1
59 
60 /* Firmware commands */
61 #define ZIIRAVE_CMD_DOWNLOAD_START		0x10
62 #define ZIIRAVE_CMD_DOWNLOAD_END		0x11
63 #define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR	0x12
64 #define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE		0x13
65 #define ZIIRAVE_CMD_RESET_PROCESSOR		0x0b
66 #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER		0x0c
67 #define ZIIRAVE_CMD_DOWNLOAD_PACKET		0x0e
68 
69 #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC	1
70 #define ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC	1
71 
72 #define ZIIRAVE_FW_VERSION_FMT	"02.%02u.%02u"
73 #define ZIIRAVE_BL_VERSION_FMT	"01.%02u.%02u"
74 
75 struct ziirave_wdt_rev {
76 	unsigned char major;
77 	unsigned char minor;
78 };
79 
80 struct ziirave_wdt_data {
81 	struct mutex sysfs_mutex;
82 	struct watchdog_device wdd;
83 	struct ziirave_wdt_rev bootloader_rev;
84 	struct ziirave_wdt_rev firmware_rev;
85 	int reset_reason;
86 };
87 
88 static int wdt_timeout;
89 module_param(wdt_timeout, int, 0);
90 MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
91 
92 static int reset_duration;
93 module_param(reset_duration, int, 0);
94 MODULE_PARM_DESC(reset_duration,
95 		 "Watchdog reset pulse duration in milliseconds");
96 
97 static bool nowayout = WATCHDOG_NOWAYOUT;
98 module_param(nowayout, bool, 0);
99 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
100 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
101 
102 static int ziirave_wdt_revision(struct i2c_client *client,
103 				struct ziirave_wdt_rev *rev, u8 command)
104 {
105 	int ret;
106 
107 	ret = i2c_smbus_read_byte_data(client, command);
108 	if (ret < 0)
109 		return ret;
110 
111 	rev->major = ret;
112 
113 	ret = i2c_smbus_read_byte_data(client, command + 1);
114 	if (ret < 0)
115 		return ret;
116 
117 	rev->minor = ret;
118 
119 	return 0;
120 }
121 
122 static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
123 {
124 	struct i2c_client *client = to_i2c_client(wdd->parent);
125 
126 	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
127 }
128 
129 static int ziirave_wdt_start(struct watchdog_device *wdd)
130 {
131 	return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
132 }
133 
134 static int ziirave_wdt_stop(struct watchdog_device *wdd)
135 {
136 	return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
137 }
138 
139 static int ziirave_wdt_ping(struct watchdog_device *wdd)
140 {
141 	struct i2c_client *client = to_i2c_client(wdd->parent);
142 
143 	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
144 					 ZIIRAVE_PING_VALUE);
145 }
146 
147 static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
148 				   unsigned int timeout)
149 {
150 	struct i2c_client *client = to_i2c_client(wdd->parent);
151 	int ret;
152 
153 	ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
154 	if (!ret)
155 		wdd->timeout = timeout;
156 
157 	return ret;
158 }
159 
160 static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
161 {
162 	struct i2c_client *client = to_i2c_client(wdd->parent);
163 	int ret;
164 
165 	ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
166 	if (ret < 0)
167 		ret = 0;
168 
169 	return ret;
170 }
171 
172 static int ziirave_firm_read_ack(struct watchdog_device *wdd)
173 {
174 	struct i2c_client *client = to_i2c_client(wdd->parent);
175 	int ret;
176 
177 	ret = i2c_smbus_read_byte(client);
178 	if (ret < 0) {
179 		dev_err(&client->dev, "Failed to read status byte\n");
180 		return ret;
181 	}
182 
183 	return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
184 }
185 
186 static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
187 {
188 	struct i2c_client *client = to_i2c_client(wdd->parent);
189 	const u16 addr16 = (u16)addr / 2;
190 	u8 address[2];
191 
192 	put_unaligned_le16(addr16, address);
193 
194 	return i2c_smbus_write_block_data(client,
195 					  ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
196 					  sizeof(address), address);
197 }
198 
199 static bool ziirave_firm_addr_readonly(u32 addr)
200 {
201 	return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
202 	       addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
203 }
204 
205 /*
206  * ziirave_firm_write_pkt() - Build and write a firmware packet
207  *
208  * A packet to send to the firmware is composed by following bytes:
209  *     Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
210  * Where,
211  *     Length: A data byte containing the length of the data.
212  *     Addr0: Low byte of the address.
213  *     Addr1: High byte of the address.
214  *     Data0 .. Data15: Array of 16 bytes of data.
215  *     Checksum: Checksum byte to verify data integrity.
216  */
217 static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
218 				    u32 addr, const u8 *data, u8 len)
219 {
220 	const u16 addr16 = (u16)addr / 2;
221 	struct i2c_client *client = to_i2c_client(wdd->parent);
222 	u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
223 	int ret;
224 
225 	/* Check max data size */
226 	if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
227 		dev_err(&client->dev, "Firmware packet too long (%d)\n",
228 			len);
229 		return -EMSGSIZE;
230 	}
231 
232 	/*
233 	 * Ignore packets that are targeting program memory outisde of
234 	 * app partition, since they will be ignored by the
235 	 * bootloader. At the same time, we need to make sure we'll
236 	 * allow zero length packet that will be sent as the last step
237 	 * of firmware update
238 	 */
239 	if (len && ziirave_firm_addr_readonly(addr))
240 		return 0;
241 
242 	/* Packet length */
243 	packet[0] = len;
244 	/* Packet address */
245 	put_unaligned_le16(addr16, packet + 1);
246 
247 	memcpy(packet + 3, data, len);
248 	memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
249 
250 	/* Packet checksum */
251 	for (i = 0; i < len + 3; i++)
252 		checksum += packet[i];
253 	packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
254 
255 	ret = i2c_smbus_write_block_data(client, ZIIRAVE_CMD_DOWNLOAD_PACKET,
256 					 sizeof(packet), packet);
257 	if (ret) {
258 		dev_err(&client->dev,
259 			"Failed to send DOWNLOAD_PACKET: %d\n", ret);
260 		return ret;
261 	}
262 
263 	ret = ziirave_firm_read_ack(wdd);
264 	if (ret)
265 		dev_err(&client->dev,
266 		      "Failed to write firmware packet at address 0x%04x: %d\n",
267 		      addr, ret);
268 
269 	return ret;
270 }
271 
272 static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
273 				  u32 addr, const u8 *data, u8 len)
274 {
275 	const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
276 		(addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
277 	int ret;
278 
279 	if (len > max_write_len) {
280 		/*
281 		 * If data crossed page boundary we need to split this
282 		 * write in two
283 		 */
284 		ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
285 		if (ret)
286 			return ret;
287 
288 		addr += max_write_len;
289 		data += max_write_len;
290 		len  -= max_write_len;
291 	}
292 
293 	return __ziirave_firm_write_pkt(wdd, addr, data, len);
294 }
295 
296 static int ziirave_firm_verify(struct watchdog_device *wdd,
297 			       const struct firmware *fw)
298 {
299 	struct i2c_client *client = to_i2c_client(wdd->parent);
300 	const struct ihex_binrec *rec;
301 	int i, ret;
302 	u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
303 
304 	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
305 		const u16 len = be16_to_cpu(rec->len);
306 		const u32 addr = be32_to_cpu(rec->addr);
307 
308 		if (ziirave_firm_addr_readonly(addr))
309 			continue;
310 
311 		ret = ziirave_firm_set_read_addr(wdd, addr);
312 		if (ret) {
313 			dev_err(&client->dev,
314 				"Failed to send SET_READ_ADDR command: %d\n",
315 				ret);
316 			return ret;
317 		}
318 
319 		for (i = 0; i < len; i++) {
320 			ret = i2c_smbus_read_byte_data(client,
321 						ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
322 			if (ret < 0) {
323 				dev_err(&client->dev,
324 					"Failed to READ DATA: %d\n", ret);
325 				return ret;
326 			}
327 			data[i] = ret;
328 		}
329 
330 		if (memcmp(data, rec->data, len)) {
331 			dev_err(&client->dev,
332 				"Firmware mismatch at address 0x%04x\n", addr);
333 			return -EINVAL;
334 		}
335 	}
336 
337 	return 0;
338 }
339 
340 static int ziirave_firm_upload(struct watchdog_device *wdd,
341 			       const struct firmware *fw)
342 {
343 	struct i2c_client *client = to_i2c_client(wdd->parent);
344 	const struct ihex_binrec *rec;
345 	int ret;
346 
347 	ret = i2c_smbus_write_byte_data(client,
348 					ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
349 					ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
350 	if (ret) {
351 		dev_err(&client->dev, "Failed to jump to bootloader\n");
352 		return ret;
353 	}
354 
355 	msleep(500);
356 
357 	ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START);
358 	if (ret) {
359 		dev_err(&client->dev, "Failed to start download\n");
360 		return ret;
361 	}
362 
363 	ret = ziirave_firm_read_ack(wdd);
364 	if (ret) {
365 		dev_err(&client->dev, "No ACK for start download\n");
366 		return ret;
367 	}
368 
369 	msleep(500);
370 
371 	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
372 		ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
373 					     rec->data, be16_to_cpu(rec->len));
374 		if (ret)
375 			return ret;
376 	}
377 
378 	/*
379 	 * Finish firmware download process by sending a zero length
380 	 * payload
381 	 */
382 	ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
383 	if (ret) {
384 		dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
385 		return ret;
386 	}
387 
388 	/* This sleep seems to be required */
389 	msleep(20);
390 
391 	/* Start firmware verification */
392 	ret = ziirave_firm_verify(wdd, fw);
393 	if (ret) {
394 		dev_err(&client->dev,
395 			"Failed to verify firmware: %d\n", ret);
396 		return ret;
397 	}
398 
399 	/* End download operation */
400 	ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END);
401 	if (ret) {
402 		dev_err(&client->dev,
403 			"Failed to end firmware download: %d\n", ret);
404 		return ret;
405 	}
406 
407 	/* Reset the processor */
408 	ret = i2c_smbus_write_byte_data(client,
409 					ZIIRAVE_CMD_RESET_PROCESSOR,
410 					ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC);
411 	if (ret) {
412 		dev_err(&client->dev,
413 			"Failed to reset the watchdog: %d\n", ret);
414 		return ret;
415 	}
416 
417 	msleep(500);
418 
419 	return 0;
420 }
421 
422 static const struct watchdog_info ziirave_wdt_info = {
423 	.options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
424 	.identity = "RAVE Switch Watchdog",
425 };
426 
427 static const struct watchdog_ops ziirave_wdt_ops = {
428 	.owner		= THIS_MODULE,
429 	.start		= ziirave_wdt_start,
430 	.stop		= ziirave_wdt_stop,
431 	.ping		= ziirave_wdt_ping,
432 	.set_timeout	= ziirave_wdt_set_timeout,
433 	.get_timeleft	= ziirave_wdt_get_timeleft,
434 };
435 
436 static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
437 					   struct device_attribute *attr,
438 					   char *buf)
439 {
440 	struct i2c_client *client = to_i2c_client(dev->parent);
441 	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
442 	int ret;
443 
444 	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
445 	if (ret)
446 		return ret;
447 
448 	ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major,
449 		      w_priv->firmware_rev.minor);
450 
451 	mutex_unlock(&w_priv->sysfs_mutex);
452 
453 	return ret;
454 }
455 
456 static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
457 		   NULL);
458 
459 static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
460 					   struct device_attribute *attr,
461 					   char *buf)
462 {
463 	struct i2c_client *client = to_i2c_client(dev->parent);
464 	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
465 	int ret;
466 
467 	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
468 	if (ret)
469 		return ret;
470 
471 	ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major,
472 		      w_priv->bootloader_rev.minor);
473 
474 	mutex_unlock(&w_priv->sysfs_mutex);
475 
476 	return ret;
477 }
478 
479 static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
480 		   NULL);
481 
482 static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
483 					     struct device_attribute *attr,
484 					     char *buf)
485 {
486 	struct i2c_client *client = to_i2c_client(dev->parent);
487 	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
488 	int ret;
489 
490 	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
491 	if (ret)
492 		return ret;
493 
494 	ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
495 
496 	mutex_unlock(&w_priv->sysfs_mutex);
497 
498 	return ret;
499 }
500 
501 static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
502 		   NULL);
503 
504 static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
505 					    struct device_attribute *attr,
506 					    const char *buf, size_t count)
507 {
508 	struct i2c_client *client = to_i2c_client(dev->parent);
509 	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
510 	const struct firmware *fw;
511 	int err;
512 
513 	err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
514 	if (err) {
515 		dev_err(&client->dev, "Failed to request ihex firmware\n");
516 		return err;
517 	}
518 
519 	err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
520 	if (err)
521 		goto release_firmware;
522 
523 	err = ziirave_firm_upload(&w_priv->wdd, fw);
524 	if (err) {
525 		dev_err(&client->dev, "The firmware update failed: %d\n", err);
526 		goto unlock_mutex;
527 	}
528 
529 	/* Update firmware version */
530 	err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
531 				   ZIIRAVE_WDT_FIRM_VER_MAJOR);
532 	if (err) {
533 		dev_err(&client->dev, "Failed to read firmware version: %d\n",
534 			err);
535 		goto unlock_mutex;
536 	}
537 
538 	dev_info(&client->dev,
539 		 "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n",
540 		 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
541 
542 	/* Restore the watchdog timeout */
543 	err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
544 	if (err)
545 		dev_err(&client->dev, "Failed to set timeout: %d\n", err);
546 
547 unlock_mutex:
548 	mutex_unlock(&w_priv->sysfs_mutex);
549 
550 release_firmware:
551 	release_firmware(fw);
552 
553 	return err ? err : count;
554 }
555 
556 static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
557 		   ziirave_wdt_sysfs_store_firm);
558 
559 static struct attribute *ziirave_wdt_attrs[] = {
560 	&dev_attr_firmware_version.attr,
561 	&dev_attr_bootloader_version.attr,
562 	&dev_attr_reset_reason.attr,
563 	&dev_attr_update_firmware.attr,
564 	NULL
565 };
566 ATTRIBUTE_GROUPS(ziirave_wdt);
567 
568 static int ziirave_wdt_init_duration(struct i2c_client *client)
569 {
570 	int ret;
571 
572 	if (!reset_duration) {
573 		/* See if the reset pulse duration is provided in an of_node */
574 		if (!client->dev.of_node)
575 			ret = -ENODEV;
576 		else
577 			ret = of_property_read_u32(client->dev.of_node,
578 						   "reset-duration-ms",
579 						   &reset_duration);
580 		if (ret) {
581 			dev_info(&client->dev,
582 			 "No reset pulse duration specified, using default\n");
583 			return 0;
584 		}
585 	}
586 
587 	if (reset_duration < 1 || reset_duration > 255)
588 		return -EINVAL;
589 
590 	dev_info(&client->dev, "Setting reset duration to %dms",
591 		 reset_duration);
592 
593 	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
594 					 reset_duration);
595 }
596 
597 static int ziirave_wdt_probe(struct i2c_client *client,
598 			     const struct i2c_device_id *id)
599 {
600 	int ret;
601 	struct ziirave_wdt_data *w_priv;
602 	int val;
603 
604 	if (!i2c_check_functionality(client->adapter,
605 				     I2C_FUNC_SMBUS_BYTE |
606 				     I2C_FUNC_SMBUS_BYTE_DATA |
607 				     I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
608 		return -ENODEV;
609 
610 	w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
611 	if (!w_priv)
612 		return -ENOMEM;
613 
614 	mutex_init(&w_priv->sysfs_mutex);
615 
616 	w_priv->wdd.info = &ziirave_wdt_info;
617 	w_priv->wdd.ops = &ziirave_wdt_ops;
618 	w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
619 	w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
620 	w_priv->wdd.parent = &client->dev;
621 	w_priv->wdd.groups = ziirave_wdt_groups;
622 
623 	watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
624 
625 	/*
626 	 * The default value set in the watchdog should be perfectly valid, so
627 	 * pass that in if we haven't provided one via the module parameter or
628 	 * of property.
629 	 */
630 	if (w_priv->wdd.timeout == 0) {
631 		val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
632 		if (val < 0) {
633 			dev_err(&client->dev, "Failed to read timeout\n");
634 			return val;
635 		}
636 
637 		if (val > ZIIRAVE_TIMEOUT_MAX ||
638 		    val < ZIIRAVE_TIMEOUT_MIN)
639 			val = ZIIRAVE_TIMEOUT_DEFAULT;
640 
641 		w_priv->wdd.timeout = val;
642 	}
643 
644 	ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
645 	if (ret) {
646 		dev_err(&client->dev, "Failed to set timeout\n");
647 		return ret;
648 	}
649 
650 	dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
651 
652 	watchdog_set_nowayout(&w_priv->wdd, nowayout);
653 
654 	i2c_set_clientdata(client, w_priv);
655 
656 	/* If in unconfigured state, set to stopped */
657 	val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
658 	if (val < 0) {
659 		dev_err(&client->dev, "Failed to read state\n");
660 		return val;
661 	}
662 
663 	if (val == ZIIRAVE_STATE_INITIAL)
664 		ziirave_wdt_stop(&w_priv->wdd);
665 
666 	ret = ziirave_wdt_init_duration(client);
667 	if (ret) {
668 		dev_err(&client->dev, "Failed to init duration\n");
669 		return ret;
670 	}
671 
672 	ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
673 				   ZIIRAVE_WDT_FIRM_VER_MAJOR);
674 	if (ret) {
675 		dev_err(&client->dev, "Failed to read firmware version\n");
676 		return ret;
677 	}
678 
679 	dev_info(&client->dev,
680 		 "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n",
681 		 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
682 
683 	ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
684 				   ZIIRAVE_WDT_BOOT_VER_MAJOR);
685 	if (ret) {
686 		dev_err(&client->dev, "Failed to read bootloader version\n");
687 		return ret;
688 	}
689 
690 	dev_info(&client->dev,
691 		 "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n",
692 		 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
693 
694 	w_priv->reset_reason = i2c_smbus_read_byte_data(client,
695 						ZIIRAVE_WDT_RESET_REASON);
696 	if (w_priv->reset_reason < 0) {
697 		dev_err(&client->dev, "Failed to read reset reason\n");
698 		return w_priv->reset_reason;
699 	}
700 
701 	if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
702 	    !ziirave_reasons[w_priv->reset_reason]) {
703 		dev_err(&client->dev, "Invalid reset reason\n");
704 		return -ENODEV;
705 	}
706 
707 	ret = watchdog_register_device(&w_priv->wdd);
708 
709 	return ret;
710 }
711 
712 static int ziirave_wdt_remove(struct i2c_client *client)
713 {
714 	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
715 
716 	watchdog_unregister_device(&w_priv->wdd);
717 
718 	return 0;
719 }
720 
721 static const struct i2c_device_id ziirave_wdt_id[] = {
722 	{ "rave-wdt", 0 },
723 	{ }
724 };
725 MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
726 
727 static const struct of_device_id zrv_wdt_of_match[] = {
728 	{ .compatible = "zii,rave-wdt", },
729 	{ },
730 };
731 MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
732 
733 static struct i2c_driver ziirave_wdt_driver = {
734 	.driver = {
735 		.name = "ziirave_wdt",
736 		.of_match_table = zrv_wdt_of_match,
737 	},
738 	.probe = ziirave_wdt_probe,
739 	.remove = ziirave_wdt_remove,
740 	.id_table = ziirave_wdt_id,
741 };
742 
743 module_i2c_driver(ziirave_wdt_driver);
744 
745 MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
746 MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
747 MODULE_LICENSE("GPL");
748