Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[safe/jmp/linux-2.6] / drivers / hwmon / w83793.c
index 6b5c99c..697202e 100644 (file)
@@ -3,6 +3,10 @@
     Copyright (C) 2006 Winbond Electronics Corp.
                   Yuan Mu
                   Rudolf Marek <r.marek@assembler.cz>
+    Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
+                 Watchdog driver part
+                 (Based partially on fschmd driver,
+                  Copyright 2007-2008 by Hans de Goede)
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #include <linux/slab.h>
 #include <linux/i2c.h>
 #include <linux/hwmon.h>
+#include <linux/smp_lock.h>
 #include <linux/hwmon-vid.h>
 #include <linux/hwmon-sysfs.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
+#include <linux/fs.h>
+#include <linux/watchdog.h>
+#include <linux/miscdevice.h>
+#include <linux/uaccess.h>
+#include <linux/kref.h>
+#include <linux/notifier.h>
+#include <linux/reboot.h>
+
+/* Default values */
+#define WATCHDOG_TIMEOUT 2     /* 2 minute default timeout */
 
 /* Addresses to scan */
-static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
+static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
+                                               I2C_CLIENT_END };
 
 /* Insmod parameters */
-I2C_CLIENT_INSMOD_1(w83793);
-I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
+
+static unsigned short force_subclients[4];
+module_param_array(force_subclients, short, NULL, 0);
+MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
                       "{bus, clientaddr, subclientaddr1, subclientaddr2}");
 
 static int reset;
 module_param(reset, bool, 0);
 MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
 
+static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+       "Watchdog timeout in minutes. 2<= timeout <=255 (default="
+                               __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
+
+static int nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, int, 0);
+MODULE_PARM_DESC(nowayout,
+       "Watchdog cannot be stopped once started (default="
+                               __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
 /*
    Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
    as ID, Bank Select registers
@@ -69,6 +99,11 @@ MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
 #define W83793_REG_VID_LATCHB          0x08
 #define W83793_REG_VID_CTRL            0x59
 
+#define W83793_REG_WDT_LOCK            0x01
+#define W83793_REG_WDT_ENABLE          0x02
+#define W83793_REG_WDT_STATUS          0x03
+#define W83793_REG_WDT_TIMEOUT         0x04
+
 static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
 
 #define TEMP_READ      0
@@ -178,7 +213,6 @@ static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
 }
 
 struct w83793_data {
-       struct i2c_client client;
        struct i2c_client *lm75[2];
        struct device *hwmon_dev;
        struct mutex update_lock;
@@ -221,31 +255,70 @@ struct w83793_data {
        u8 tolerance[3];        /* Temp tolerance(Smart Fan I/II) */
        u8 sf2_pwm[6][7];       /* Smart FanII: Fan duty cycle */
        u8 sf2_temp[6][7];      /* Smart FanII: Temp level point */
+
+       /* watchdog */
+       struct i2c_client *client;
+       struct mutex watchdog_lock;
+       struct list_head list; /* member of the watchdog_data_list */
+       struct kref kref;
+       struct miscdevice watchdog_miscdev;
+       unsigned long watchdog_is_open;
+       char watchdog_expect_close;
+       char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
+       unsigned int watchdog_caused_reboot;
+       int watchdog_timeout; /* watchdog timeout in minutes */
 };
 
+/* Somewhat ugly :( global data pointer list with all devices, so that
+   we can find our device data as when using misc_register. There is no
+   other method to get to one's device data from the open file-op and
+   for usage in the reboot notifier callback. */
+static LIST_HEAD(watchdog_data_list);
+
+/* Note this lock not only protect list access, but also data.kref access */
+static DEFINE_MUTEX(watchdog_data_mutex);
+
+/* Release our data struct when we're detached from the i2c client *and* all
+   references to our watchdog device are released */
+static void w83793_release_resources(struct kref *ref)
+{
+       struct w83793_data *data = container_of(ref, struct w83793_data, kref);
+       kfree(data);
+}
+
 static u8 w83793_read_value(struct i2c_client *client, u16 reg);
 static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
-static int w83793_attach_adapter(struct i2c_adapter *adapter);
-static int w83793_detect(struct i2c_adapter *adapter, int address, int kind);
-static int w83793_detach_client(struct i2c_client *client);
+static int w83793_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id);
+static int w83793_detect(struct i2c_client *client,
+                        struct i2c_board_info *info);
+static int w83793_remove(struct i2c_client *client);
 static void w83793_init_client(struct i2c_client *client);
 static void w83793_update_nonvolatile(struct device *dev);
 static struct w83793_data *w83793_update_device(struct device *dev);
 
+static const struct i2c_device_id w83793_id[] = {
+       { "w83793", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, w83793_id);
+
 static struct i2c_driver w83793_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                   .name = "w83793",
        },
-       .attach_adapter = w83793_attach_adapter,
-       .detach_client = w83793_detach_client,
+       .probe          = w83793_probe,
+       .remove         = w83793_remove,
+       .id_table       = w83793_id,
+       .detect         = w83793_detect,
+       .address_list   = normal_i2c,
 };
 
 static ssize_t
 show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
 {
-       struct i2c_client *client = to_i2c_client(dev);
-       struct w83793_data *data = i2c_get_clientdata(client);
-
+       struct w83793_data *data = dev_get_drvdata(dev);
        return sprintf(buf, "%d\n", data->vrm);
 }
 
@@ -264,9 +337,7 @@ static ssize_t
 store_vrm(struct device *dev, struct device_attribute *attr,
          const char *buf, size_t count)
 {
-       struct i2c_client *client = to_i2c_client(dev);
-       struct w83793_data *data = i2c_get_clientdata(client);
-
+       struct w83793_data *data = dev_get_drvdata(dev);
        data->vrm = simple_strtoul(buf, NULL, 10);
        return count;
 }
@@ -1027,10 +1098,9 @@ static struct sensor_device_attribute_2 w83793_vid[] = {
        SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
        SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
 };
+static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
 
 static struct sensor_device_attribute_2 sda_single_files[] = {
-       SENSOR_ATTR_2(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm,
-                     NOT_USED, NOT_USED),
        SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
                      store_chassis_clear, ALARM_STATUS, 30),
        SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
@@ -1054,91 +1124,393 @@ static void w83793_init_client(struct i2c_client *client)
        /* Start monitoring */
        w83793_write_value(client, W83793_REG_CONFIG,
                           w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
+}
+
+/*
+ * Watchdog routines
+ */
+
+static int watchdog_set_timeout(struct w83793_data *data, int timeout)
+{
+       int ret, mtimeout;
 
+       mtimeout = DIV_ROUND_UP(timeout, 60);
+
+       if (mtimeout > 255)
+               return -EINVAL;
+
+       mutex_lock(&data->watchdog_lock);
+       if (!data->client) {
+               ret = -ENODEV;
+               goto leave;
+       }
+
+       data->watchdog_timeout = mtimeout;
+
+       /* Set Timeout value (in Minutes) */
+       w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
+                          data->watchdog_timeout);
+
+       ret = mtimeout * 60;
+
+leave:
+       mutex_unlock(&data->watchdog_lock);
+       return ret;
 }
 
-static int w83793_attach_adapter(struct i2c_adapter *adapter)
+static int watchdog_get_timeout(struct w83793_data *data)
 {
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, w83793_detect);
+       int timeout;
+
+       mutex_lock(&data->watchdog_lock);
+       timeout = data->watchdog_timeout * 60;
+       mutex_unlock(&data->watchdog_lock);
+
+       return timeout;
 }
 
-static int w83793_detach_client(struct i2c_client *client)
+static int watchdog_trigger(struct w83793_data *data)
 {
-       struct w83793_data *data = i2c_get_clientdata(client);
-       struct device *dev = &client->dev;
-       int err, i;
+       int ret = 0;
 
-       /* main client */
-       if (data) {
-               hwmon_device_unregister(data->hwmon_dev);
+       mutex_lock(&data->watchdog_lock);
+       if (!data->client) {
+               ret = -ENODEV;
+               goto leave;
+       }
 
-               for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
-                       device_remove_file(dev,
-                                          &w83793_sensor_attr_2[i].dev_attr);
+       /* Set Timeout value (in Minutes) */
+       w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
+                          data->watchdog_timeout);
 
-               for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
-                       device_remove_file(dev, &sda_single_files[i].dev_attr);
+leave:
+       mutex_unlock(&data->watchdog_lock);
+       return ret;
+}
+
+static int watchdog_enable(struct w83793_data *data)
+{
+       int ret = 0;
+
+       mutex_lock(&data->watchdog_lock);
+       if (!data->client) {
+               ret = -ENODEV;
+               goto leave;
+       }
+
+       /* Set initial timeout */
+       w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
+                          data->watchdog_timeout);
 
-               for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
-                       device_remove_file(dev, &w83793_vid[i].dev_attr);
+       /* Enable Soft Watchdog */
+       w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
 
-               for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
-                       device_remove_file(dev, &w83793_left_fan[i].dev_attr);
+leave:
+       mutex_unlock(&data->watchdog_lock);
+       return ret;
+}
 
-               for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
-                       device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
+static int watchdog_disable(struct w83793_data *data)
+{
+       int ret = 0;
 
-               for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
-                       device_remove_file(dev, &w83793_temp[i].dev_attr);
+       mutex_lock(&data->watchdog_lock);
+       if (!data->client) {
+               ret = -ENODEV;
+               goto leave;
        }
 
-       if ((err = i2c_detach_client(client)))
-               return err;
+       /* Disable Soft Watchdog */
+       w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
 
-       /* main client */
-       if (data)
-               kfree(data);
-       /* subclient */
-       else
-               kfree(client);
+leave:
+       mutex_unlock(&data->watchdog_lock);
+       return ret;
+}
+
+static int watchdog_open(struct inode *inode, struct file *filp)
+{
+       struct w83793_data *pos, *data = NULL;
+       int watchdog_is_open;
+
+       /* We get called from drivers/char/misc.c with misc_mtx hold, and we
+          call misc_register() from  w83793_probe() with watchdog_data_mutex
+          hold, as misc_register() takes the misc_mtx lock, this is a possible
+          deadlock, so we use mutex_trylock here. */
+       if (!mutex_trylock(&watchdog_data_mutex))
+               return -ERESTARTSYS;
+       list_for_each_entry(pos, &watchdog_data_list, list) {
+               if (pos->watchdog_miscdev.minor == iminor(inode)) {
+                       data = pos;
+                       break;
+               }
+       }
+
+       /* Check, if device is already open */
+       watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
+
+       /* Increase data reference counter (if not already done).
+          Note we can never not have found data, so we don't check for this */
+       if (!watchdog_is_open)
+               kref_get(&data->kref);
+
+       mutex_unlock(&watchdog_data_mutex);
+
+       /* Check, if device is already open and possibly issue error */
+       if (watchdog_is_open)
+               return -EBUSY;
+
+       /* Enable Soft Watchdog */
+       watchdog_enable(data);
+
+       /* Store pointer to data into filp's private data */
+       filp->private_data = data;
+
+       return nonseekable_open(inode, filp);
+}
+
+static int watchdog_close(struct inode *inode, struct file *filp)
+{
+       struct w83793_data *data = filp->private_data;
+
+       if (data->watchdog_expect_close) {
+               watchdog_disable(data);
+               data->watchdog_expect_close = 0;
+       } else {
+               watchdog_trigger(data);
+               dev_crit(&data->client->dev,
+                       "unexpected close, not stopping watchdog!\n");
+       }
+
+       clear_bit(0, &data->watchdog_is_open);
+
+       /* Decrease data reference counter */
+       mutex_lock(&watchdog_data_mutex);
+       kref_put(&data->kref, w83793_release_resources);
+       mutex_unlock(&watchdog_data_mutex);
 
        return 0;
 }
 
-static int
-w83793_create_subclient(struct i2c_adapter *adapter,
-                       struct i2c_client *client, int addr,
-                       struct i2c_client **sub_cli)
+static ssize_t watchdog_write(struct file *filp, const char __user *buf,
+       size_t count, loff_t *offset)
+{
+       ssize_t ret;
+       struct w83793_data *data = filp->private_data;
+
+       if (count) {
+               if (!nowayout) {
+                       size_t i;
+
+                       /* Clear it in case it was set with a previous write */
+                       data->watchdog_expect_close = 0;
+
+                       for (i = 0; i != count; i++) {
+                               char c;
+                               if (get_user(c, buf + i))
+                                       return -EFAULT;
+                               if (c == 'V')
+                                       data->watchdog_expect_close = 1;
+                       }
+               }
+               ret = watchdog_trigger(data);
+               if (ret < 0)
+                       return ret;
+       }
+       return count;
+}
+
+static long watchdog_ioctl(struct file *filp, unsigned int cmd,
+                          unsigned long arg)
 {
-       int err = 0;
-       struct i2c_client *sub_client;
+       static struct watchdog_info ident = {
+               .options = WDIOF_KEEPALIVEPING |
+                          WDIOF_SETTIMEOUT |
+                          WDIOF_CARDRESET,
+               .identity = "w83793 watchdog"
+       };
+
+       int val, ret = 0;
+       struct w83793_data *data = filp->private_data;
+
+       lock_kernel();
+       switch (cmd) {
+       case WDIOC_GETSUPPORT:
+               if (!nowayout)
+                       ident.options |= WDIOF_MAGICCLOSE;
+               if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
+                       ret = -EFAULT;
+               break;
+
+       case WDIOC_GETSTATUS:
+               val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
+               ret = put_user(val, (int __user *)arg);
+               break;
+
+       case WDIOC_GETBOOTSTATUS:
+               ret = put_user(0, (int __user *)arg);
+               break;
+
+       case WDIOC_KEEPALIVE:
+               ret = watchdog_trigger(data);
+               break;
+
+       case WDIOC_GETTIMEOUT:
+               val = watchdog_get_timeout(data);
+               ret = put_user(val, (int __user *)arg);
+               break;
+
+       case WDIOC_SETTIMEOUT:
+               if (get_user(val, (int __user *)arg)) {
+                       ret = -EFAULT;
+                       break;
+               }
+               ret = watchdog_set_timeout(data, val);
+               if (ret > 0)
+                       ret = put_user(ret, (int __user *)arg);
+               break;
+
+       case WDIOC_SETOPTIONS:
+               if (get_user(val, (int __user *)arg)) {
+                       ret = -EFAULT;
+                       break;
+               }
 
-       (*sub_cli) = sub_client =
-           kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
-       if (!(sub_client)) {
-               return -ENOMEM;
+               if (val & WDIOS_DISABLECARD)
+                       ret = watchdog_disable(data);
+               else if (val & WDIOS_ENABLECARD)
+                       ret = watchdog_enable(data);
+               else
+                       ret = -EINVAL;
+
+               break;
+       default:
+               ret = -ENOTTY;
        }
-       sub_client->addr = 0x48 + addr;
-       i2c_set_clientdata(sub_client, NULL);
-       sub_client->adapter = adapter;
-       sub_client->driver = &w83793_driver;
-       strlcpy(sub_client->name, "w83793 subclient", I2C_NAME_SIZE);
-       if ((err = i2c_attach_client(sub_client))) {
-               dev_err(&client->dev, "subclient registration "
-                       "at address 0x%x failed\n", sub_client->addr);
-               kfree(sub_client);
+       unlock_kernel();
+       return ret;
+}
+
+static const struct file_operations watchdog_fops = {
+       .owner = THIS_MODULE,
+       .llseek = no_llseek,
+       .open = watchdog_open,
+       .release = watchdog_close,
+       .write = watchdog_write,
+       .unlocked_ioctl = watchdog_ioctl,
+};
+
+/*
+ *     Notifier for system down
+ */
+
+static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
+                              void *unused)
+{
+       struct w83793_data *data = NULL;
+
+       if (code == SYS_DOWN || code == SYS_HALT) {
+
+               /* Disable each registered watchdog */
+               mutex_lock(&watchdog_data_mutex);
+               list_for_each_entry(data, &watchdog_data_list, list) {
+                       if (data->watchdog_miscdev.minor)
+                               watchdog_disable(data);
+               }
+               mutex_unlock(&watchdog_data_mutex);
        }
-       return err;
+
+       return NOTIFY_DONE;
+}
+
+/*
+ *     The WDT needs to learn about soft shutdowns in order to
+ *     turn the timebomb registers off.
+ */
+
+static struct notifier_block watchdog_notifier = {
+       .notifier_call = watchdog_notify_sys,
+};
+
+/*
+ * Init / remove routines
+ */
+
+static int w83793_remove(struct i2c_client *client)
+{
+       struct w83793_data *data = i2c_get_clientdata(client);
+       struct device *dev = &client->dev;
+       int i, tmp;
+
+       /* Unregister the watchdog (if registered) */
+       if (data->watchdog_miscdev.minor) {
+               misc_deregister(&data->watchdog_miscdev);
+
+               if (data->watchdog_is_open) {
+                       dev_warn(&client->dev,
+                               "i2c client detached with watchdog open! "
+                               "Stopping watchdog.\n");
+                       watchdog_disable(data);
+               }
+
+               mutex_lock(&watchdog_data_mutex);
+               list_del(&data->list);
+               mutex_unlock(&watchdog_data_mutex);
+
+               /* Tell the watchdog code the client is gone */
+               mutex_lock(&data->watchdog_lock);
+               data->client = NULL;
+               mutex_unlock(&data->watchdog_lock);
+       }
+
+       /* Reset Configuration Register to Disable Watch Dog Registers */
+       tmp = w83793_read_value(client, W83793_REG_CONFIG);
+       w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
+
+       unregister_reboot_notifier(&watchdog_notifier);
+
+       hwmon_device_unregister(data->hwmon_dev);
+
+       for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
+               device_remove_file(dev,
+                                  &w83793_sensor_attr_2[i].dev_attr);
+
+       for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
+               device_remove_file(dev, &sda_single_files[i].dev_attr);
+
+       for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
+               device_remove_file(dev, &w83793_vid[i].dev_attr);
+       device_remove_file(dev, &dev_attr_vrm);
+
+       for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
+               device_remove_file(dev, &w83793_left_fan[i].dev_attr);
+
+       for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
+               device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
+
+       for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
+               device_remove_file(dev, &w83793_temp[i].dev_attr);
+
+       if (data->lm75[0] != NULL)
+               i2c_unregister_device(data->lm75[0]);
+       if (data->lm75[1] != NULL)
+               i2c_unregister_device(data->lm75[1]);
+
+       /* Decrease data reference counter */
+       mutex_lock(&watchdog_data_mutex);
+       kref_put(&data->kref, w83793_release_resources);
+       mutex_unlock(&watchdog_data_mutex);
+
+       return 0;
 }
 
 static int
-w83793_detect_subclients(struct i2c_adapter *adapter, int address,
-                        int kind, struct i2c_client *client)
+w83793_detect_subclients(struct i2c_client *client)
 {
        int i, id, err;
+       int address = client->addr;
        u8 tmp;
+       struct i2c_adapter *adapter = client->adapter;
        struct w83793_data *data = i2c_get_clientdata(client);
 
        id = i2c_adapter_id(adapter);
@@ -1161,11 +1533,7 @@ w83793_detect_subclients(struct i2c_adapter *adapter, int address,
 
        tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
        if (!(tmp & 0x08)) {
-               err =
-                   w83793_create_subclient(adapter, client, tmp & 0x7,
-                                           &data->lm75[0]);
-               if (err < 0)
-                       goto ERROR_SC_0;
+               data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
        }
        if (!(tmp & 0x80)) {
                if ((data->lm75[0] != NULL)
@@ -1176,10 +1544,8 @@ w83793_detect_subclients(struct i2c_adapter *adapter, int address,
                        err = -ENODEV;
                        goto ERROR_SC_1;
                }
-               err = w83793_create_subclient(adapter, client,
-                                             (tmp >> 4) & 0x7, &data->lm75[1]);
-               if (err < 0)
-                       goto ERROR_SC_1;
+               data->lm75[1] = i2c_new_dummy(adapter,
+                                             0x48 + ((tmp >> 4) & 0x7));
        }
 
        return 0;
@@ -1187,105 +1553,88 @@ w83793_detect_subclients(struct i2c_adapter *adapter, int address,
        /* Undo inits in case of errors */
 
 ERROR_SC_1:
-       if (data->lm75[0] != NULL) {
-               i2c_detach_client(data->lm75[0]);
-               kfree(data->lm75[0]);
-       }
+       if (data->lm75[0] != NULL)
+               i2c_unregister_device(data->lm75[0]);
 ERROR_SC_0:
        return err;
 }
 
-static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int w83793_detect(struct i2c_client *client,
+                        struct i2c_board_info *info)
 {
-       int i;
-       u8 tmp, val;
-       struct i2c_client *client;
-       struct device *dev;
-       struct w83793_data *data;
-       int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
-       int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
-       int files_temp = ARRAY_SIZE(w83793_temp) / 6;
-       int err = 0;
+       u8 tmp, bank, chip_id;
+       struct i2c_adapter *adapter = client->adapter;
+       unsigned short address = client->addr;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
-               goto exit;
+               return -ENODEV;
        }
 
-       /* OK. For now, we presume we have a valid client. We now create the
-          client structure, even though we cannot fill it completely yet.
-          But it allows us to access w83793_{read,write}_value. */
+       bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
 
-       if (!(data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
+       tmp = bank & 0x80 ? 0x5c : 0xa3;
+       /* Check Winbond vendor ID */
+       if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
+               pr_debug("w83793: Detection failed at check vendor id\n");
+               return -ENODEV;
        }
 
-       client = &data->client;
-       dev = &client->dev;
-       i2c_set_clientdata(client, data);
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &w83793_driver;
-
-       data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
+       /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
+          should match */
+       if ((bank & 0x07) == 0
+        && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
+           (address << 1)) {
+               pr_debug("w83793: Detection failed at check i2c addr\n");
+               return -ENODEV;
+       }
 
-       /* Now, we do the remaining detection. */
-       if (kind < 0) {
-               tmp = data->bank & 0x80 ? 0x5c : 0xa3;
-               /* Check Winbond vendor ID */
-               if (tmp != i2c_smbus_read_byte_data(client,
-                                                       W83793_REG_VENDORID)) {
-                       pr_debug("w83793: Detection failed at check "
-                                "vendor id\n");
-                       err = -ENODEV;
-                       goto free_mem;
-               }
+       /* Determine the chip type now */
+       chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
+       if (chip_id != 0x7b)
+               return -ENODEV;
 
-               /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
-                  should match */
-               if ((data->bank & 0x07) == 0
-                && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
-                   (address << 1)) {
-                       pr_debug("w83793: Detection failed at check "
-                                "i2c addr\n");
-                       err = -ENODEV;
-                       goto free_mem;
-               }
+       strlcpy(info->type, "w83793", I2C_NAME_SIZE);
 
-       }
+       return 0;
+}
 
-       /* We have either had a force parameter, or we have already detected the
-          Winbond. Determine the chip type now */
+static int w83793_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
+{
+       struct device *dev = &client->dev;
+       const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
+       struct w83793_data *data;
+       int i, tmp, val, err;
+       int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
+       int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
+       int files_temp = ARRAY_SIZE(w83793_temp) / 6;
 
-       if (kind <= 0) {
-               if (0x7b == w83793_read_value(client, W83793_REG_CHIPID)) {
-                       kind = w83793;
-               } else {
-                       if (kind == 0)
-                               dev_warn(&adapter->dev, "w83793: Ignoring "
-                                        "'force' parameter for unknown chip "
-                                        "at address 0x%02x\n", address);
-                       err = -ENODEV;
-                       goto free_mem;
-               }
+       data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
        }
 
-       /* Fill in the remaining client fields and put into the global list */
-       strlcpy(client->name, "w83793", I2C_NAME_SIZE);
-
+       i2c_set_clientdata(client, data);
+       data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
        mutex_init(&data->update_lock);
+       mutex_init(&data->watchdog_lock);
+       INIT_LIST_HEAD(&data->list);
+       kref_init(&data->kref);
 
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(client)))
-               goto free_mem;
+       /* Store client pointer in our data struct for watchdog usage
+          (where the client is found through a data ptr instead of the
+          otherway around) */
+       data->client = client;
 
-       if ((err = w83793_detect_subclients(adapter, address, kind, client)))
-               goto detach_client;
+       err = w83793_detect_subclients(client);
+       if (err)
+               goto free_mem;
 
        /* Initialize the chip */
        w83793_init_client(client);
 
-       data->vrm = vid_which_vrm();
        /*
           Only fan 1-5 has their own input pins,
           Pwm 1-3 has their own pins
@@ -1296,7 +1645,9 @@ static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
        val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
 
        /* check the function of pins 49-56 */
-       if (!(tmp & 0x80)) {
+       if (tmp & 0x80) {
+               data->has_vid |= 0x2;   /* has VIDB */
+       } else {
                data->has_pwm |= 0x18;  /* pwm 4,5 */
                if (val & 0x01) {       /* fan 6 */
                        data->has_fan |= 0x20;
@@ -1312,13 +1663,15 @@ static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
                }
        }
 
+       /* check the function of pins 37-40 */
+       if (!(tmp & 0x29))
+               data->has_vid |= 0x1;   /* has VIDA */
        if (0x08 == (tmp & 0x0c)) {
                if (val & 0x08) /* fan 9 */
                        data->has_fan |= 0x100;
                if (val & 0x10) /* fan 10 */
                        data->has_fan |= 0x200;
        }
-
        if (0x20 == (tmp & 0x30)) {
                if (val & 0x20) /* fan 11 */
                        data->has_fan |= 0x400;
@@ -1362,13 +1715,6 @@ static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
        if (tmp & 0x02)
                data->has_temp |= 0x20;
 
-       /* Detect the VID usage and ignore unused input */
-       tmp = w83793_read_value(client, W83793_REG_MFC);
-       if (!(tmp & 0x29))
-               data->has_vid |= 0x1;   /* has VIDA */
-       if (tmp & 0x80)
-               data->has_vid |= 0x2;   /* has VIDB */
-
        /* Register sysfs hooks */
        for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
                err = device_create_file(dev,
@@ -1384,6 +1730,12 @@ static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
                if (err)
                        goto exit_remove;
        }
+       if (data->has_vid) {
+               data->vrm = vid_which_vrm();
+               err = device_create_file(dev, &dev_attr_vrm);
+               if (err)
+                       goto exit_remove;
+       }
 
        for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
                err = device_create_file(dev, &sda_single_files[i].dev_attr);
@@ -1437,8 +1789,77 @@ static int w83793_detect(struct i2c_adapter *adapter, int address, int kind)
                goto exit_remove;
        }
 
+       /* Watchdog initialization */
+
+       /* Register boot notifier */
+       err = register_reboot_notifier(&watchdog_notifier);
+       if (err != 0) {
+               dev_err(&client->dev,
+                       "cannot register reboot notifier (err=%d)\n", err);
+               goto exit_devunreg;
+       }
+
+       /* Enable Watchdog registers.
+          Set Configuration Register to Enable Watch Dog Registers
+          (Bit 2) = XXXX, X1XX. */
+       tmp = w83793_read_value(client, W83793_REG_CONFIG);
+       w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
+
+       /* Set the default watchdog timeout */
+       data->watchdog_timeout = timeout;
+
+       /* Check, if last reboot was caused by watchdog */
+       data->watchdog_caused_reboot =
+         w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
+
+       /* Disable Soft Watchdog during initialiation */
+       watchdog_disable(data);
+
+       /* We take the data_mutex lock early so that watchdog_open() cannot
+          run when misc_register() has completed, but we've not yet added
+          our data to the watchdog_data_list (and set the default timeout) */
+       mutex_lock(&watchdog_data_mutex);
+       for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
+               /* Register our watchdog part */
+               snprintf(data->watchdog_name, sizeof(data->watchdog_name),
+                       "watchdog%c", (i == 0) ? '\0' : ('0' + i));
+               data->watchdog_miscdev.name = data->watchdog_name;
+               data->watchdog_miscdev.fops = &watchdog_fops;
+               data->watchdog_miscdev.minor = watchdog_minors[i];
+
+               err = misc_register(&data->watchdog_miscdev);
+               if (err == -EBUSY)
+                       continue;
+               if (err) {
+                       data->watchdog_miscdev.minor = 0;
+                       dev_err(&client->dev,
+                               "Registering watchdog chardev: %d\n", err);
+                       break;
+               }
+
+               list_add(&data->list, &watchdog_data_list);
+
+               dev_info(&client->dev,
+                       "Registered watchdog chardev major 10, minor: %d\n",
+                       watchdog_minors[i]);
+               break;
+       }
+       if (i == ARRAY_SIZE(watchdog_minors)) {
+               data->watchdog_miscdev.minor = 0;
+               dev_warn(&client->dev, "Couldn't register watchdog chardev "
+                       "(due to no free minor)\n");
+       }
+
+       mutex_unlock(&watchdog_data_mutex);
+
        return 0;
 
+       /* Unregister hwmon device */
+
+exit_devunreg:
+
+       hwmon_device_unregister(data->hwmon_dev);
+
        /* Unregister sysfs hooks */
 
 exit_remove:
@@ -1460,16 +1881,10 @@ exit_remove:
        for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
                device_remove_file(dev, &w83793_temp[i].dev_attr);
 
-       if (data->lm75[0] != NULL) {
-               i2c_detach_client(data->lm75[0]);
-               kfree(data->lm75[0]);
-       }
-       if (data->lm75[1] != NULL) {
-               i2c_detach_client(data->lm75[1]);
-               kfree(data->lm75[1]);
-       }
-detach_client:
-       i2c_detach_client(client);
+       if (data->lm75[0] != NULL)
+               i2c_unregister_device(data->lm75[0]);
+       if (data->lm75[1] != NULL)
+               i2c_unregister_device(data->lm75[1]);
 free_mem:
        kfree(data);
 exit:
@@ -1691,7 +2106,7 @@ static void __exit sensors_w83793_exit(void)
        i2c_del_driver(&w83793_driver);
 }
 
-MODULE_AUTHOR("Yuan Mu");
+MODULE_AUTHOR("Yuan Mu, Sven Anders");
 MODULE_DESCRIPTION("w83793 driver");
 MODULE_LICENSE("GPL");