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