hwmon: Add WM835x PMIC hardware monitoring driver
[safe/jmp/linux-2.6] / drivers / hwmon / lm87.c
index d0d2464..2e4a3ce 100644 (file)
@@ -5,7 +5,7 @@
  *                          Philip Edelbrock <phil@netroedge.com>
  *                          Stephen Rousset <stephen.rousset@rocketlogix.com>
  *                          Dan Eaton <dan.eaton@rocketlogix.com>
- * Copyright (C) 2004       Jean Delvare <khali@linux-fr.org>
+ * Copyright (C) 2004-2008  Jean Delvare <khali@linux-fr.org>
  *
  * Original port to Linux 2.6 by Jeff Oliver.
  *
  *   http://www.national.com/pf/LM/LM87.html
  *
  * Some functions share pins, so not all functions are available at the same
- * time. Which are depends on the hardware setup. This driver assumes that
- * the BIOS configured the chip correctly. In that respect, it  differs from
- * the original driver (from lm_sensors for Linux 2.4), which would force the
- * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
- * chipset wiring.
+ * time. Which are depends on the hardware setup. This driver normally
+ * assumes that firmware configured the chip correctly. Where this is not
+ * the case, platform code must set the I2C client's platform_data to point
+ * to a u8 value to be written to the channel register.
  * For reference, here is the list of exclusive functions:
  *  - in0+in5 (default) or temp3
  *  - fan1 (default) or in6
  * instead. The LM87 is the only hardware monitoring chipset I know of
  * which uses amplitude modulation. Be careful when using this feature.
  *
+ * This driver also supports the ADM1024, a sensor chip made by Analog
+ * Devices. That chip is fully compatible with the LM87. Complete
+ * datasheet can be obtained from Analog's website at:
+ *   http://www.analog.com/en/prod/0,2877,ADM1024,00.html
+ *
  * 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
  * the Free Software Foundation; either version 2 of the License, or
 #include <linux/slab.h>
 #include <linux/jiffies.h>
 #include <linux/i2c.h>
-#include <linux/i2c-vid.h>
 #include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/hwmon-vid.h>
 #include <linux/err.h>
+#include <linux/mutex.h>
 
 /*
  * Addresses to scan
  * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
  */
 
-static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
+static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
 
 /*
  * Insmod parameters
  */
 
-I2C_CLIENT_INSMOD_1(lm87);
+I2C_CLIENT_INSMOD_2(lm87, adm1024);
 
 /*
  * The LM87 registers
@@ -128,7 +134,7 @@ static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
                                 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
 
 #define FAN_FROM_REG(reg,div)  ((reg) == 255 || (reg) == 0 ? 0 : \
-                                1350000 + (reg)*(div) / 2) / ((reg)*(div))
+                                (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
 #define FAN_TO_REG(val,div)    ((val)*(div) * 255 <= 1350000 ? 255 : \
                                 (1350000 + (val)*(div) / 2) / ((val)*(div)))
 
@@ -144,29 +150,41 @@ static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
 #define CHAN_NO_FAN(nr)                (1 << (nr))
 #define CHAN_TEMP3             (1 << 2)
 #define CHAN_VCC_5V            (1 << 3)
-#define CHAN_NO_VID            (1 << 8)
+#define CHAN_NO_VID            (1 << 7)
 
 /*
  * Functions declaration
  */
 
-static int lm87_attach_adapter(struct i2c_adapter *adapter);
-static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
+static int lm87_probe(struct i2c_client *client,
+                     const struct i2c_device_id *id);
+static int lm87_detect(struct i2c_client *new_client, int kind,
+                      struct i2c_board_info *info);
 static void lm87_init_client(struct i2c_client *client);
-static int lm87_detach_client(struct i2c_client *client);
+static int lm87_remove(struct i2c_client *client);
 static struct lm87_data *lm87_update_device(struct device *dev);
 
 /*
  * Driver data (common to all clients)
  */
 
+static const struct i2c_device_id lm87_id[] = {
+       { "lm87", lm87 },
+       { "adm1024", adm1024 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, lm87_id);
+
 static struct i2c_driver lm87_driver = {
-       .owner          = THIS_MODULE,
-       .name           = "lm87",
-       .id             = I2C_DRIVERID_LM87,
-       .flags          = I2C_DF_NOTIFY,
-       .attach_adapter = lm87_attach_adapter,
-       .detach_client  = lm87_detach_client,
+       .class          = I2C_CLASS_HWMON,
+       .driver = {
+               .name   = "lm87",
+       },
+       .probe          = lm87_probe,
+       .remove         = lm87_remove,
+       .id_table       = lm87_id,
+       .detect         = lm87_detect,
+       .address_data   = &addr_data,
 };
 
 /*
@@ -174,13 +192,13 @@ static struct i2c_driver lm87_driver = {
  */
 
 struct lm87_data {
-       struct i2c_client client;
-       struct class_device *class_dev;
-       struct semaphore update_lock;
+       struct device *hwmon_dev;
+       struct mutex update_lock;
        char valid; /* zero until following fields are valid */
        unsigned long last_updated; /* In jiffies */
 
        u8 channel;             /* register value */
+       u8 config;              /* original register value */
 
        u8 in[8];               /* register value */
        u8 in_max[8];           /* register value */
@@ -253,11 +271,11 @@ static void set_in_min(struct device *dev, const char *buf, int nr)
        struct lm87_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
        lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
                         LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 }
 
 static void set_in_max(struct device *dev, const char *buf, int nr)
@@ -266,11 +284,11 @@ static void set_in_max(struct device *dev, const char *buf, int nr)
        struct lm87_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
        lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
                         LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 }
 
 #define set_in(offset) \
@@ -327,10 +345,10 @@ static void set_temp_low(struct device *dev, const char *buf, int nr)
        struct lm87_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->temp_low[nr] = TEMP_TO_REG(val);
        lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 }
 
 static void set_temp_high(struct device *dev, const char *buf, int nr)
@@ -339,10 +357,10 @@ static void set_temp_high(struct device *dev, const char *buf, int nr)
        struct lm87_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->temp_high[nr] = TEMP_TO_REG(val);
        lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 }
 
 #define set_temp(offset) \
@@ -411,16 +429,16 @@ static void set_fan_min(struct device *dev, const char *buf, int nr)
        struct lm87_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->fan_min[nr] = FAN_TO_REG(val,
                            FAN_DIV_FROM_REG(data->fan_div[nr]));
        lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 }
 
 /* Note: we save and restore the fan minimum here, because its value is
    determined in part by the fan clock divider.  This follows the principle
-   of least suprise; the user doesn't expect the fan minimum to change just
+   of least surprise; the user doesn't expect the fan minimum to change just
    because the divider changed. */
 static ssize_t set_fan_div(struct device *dev, const char *buf,
                size_t count, int nr)
@@ -431,7 +449,7 @@ static ssize_t set_fan_div(struct device *dev, const char *buf,
        unsigned long min;
        u8 reg;
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        min = FAN_FROM_REG(data->fan_min[nr],
                           FAN_DIV_FROM_REG(data->fan_div[nr]));
 
@@ -441,7 +459,7 @@ static ssize_t set_fan_div(struct device *dev, const char *buf,
        case 4: data->fan_div[nr] = 2; break;
        case 8: data->fan_div[nr] = 3; break;
        default:
-               up(&data->update_lock);
+               mutex_unlock(&data->update_lock);
                return -EINVAL;
        }
 
@@ -459,7 +477,7 @@ static ssize_t set_fan_div(struct device *dev, const char *buf,
        data->fan_min[nr] = FAN_TO_REG(min, val);
        lm87_write_value(client, LM87_REG_FAN_MIN(nr),
                         data->fan_min[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 
        return count;
 }
@@ -499,13 +517,12 @@ static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
 
 static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
 {
-       struct lm87_data *data = lm87_update_device(dev);
+       struct lm87_data *data = dev_get_drvdata(dev);
        return sprintf(buf, "%d\n", data->vrm);
 }
 static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
 {
-       struct i2c_client *client = to_i2c_client(dev);
-       struct lm87_data *data = i2c_get_clientdata(client);
+       struct lm87_data *data = dev_get_drvdata(dev);
        data->vrm = simple_strtoul(buf, NULL, 10);
        return count;
 }
@@ -522,52 +539,137 @@ static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const
        struct lm87_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->aout = AOUT_TO_REG(val);
        lm87_write_value(client, LM87_REG_AOUT, data->aout);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
 
-/*
- * Real code
- */
-
-static int lm87_attach_adapter(struct i2c_adapter *adapter)
+static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
+                         char *buf)
 {
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, lm87_detect);
+       struct lm87_data *data = lm87_update_device(dev);
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
 }
+static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
+static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
+static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
+static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
+static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
+static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
+static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
+static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
+static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
+static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
+static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
+static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
+static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
 
 /*
- * The following function does more than just detection. If detection
- * succeeds, it also registers the new chip.
+ * Real code
  */
-static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
-{
-       struct i2c_client *new_client;
-       struct lm87_data *data;
-       int err = 0;
 
-       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
-               goto exit;
+static struct attribute *lm87_attributes[] = {
+       &dev_attr_in1_input.attr,
+       &dev_attr_in1_min.attr,
+       &dev_attr_in1_max.attr,
+       &sensor_dev_attr_in1_alarm.dev_attr.attr,
+       &dev_attr_in2_input.attr,
+       &dev_attr_in2_min.attr,
+       &dev_attr_in2_max.attr,
+       &sensor_dev_attr_in2_alarm.dev_attr.attr,
+       &dev_attr_in3_input.attr,
+       &dev_attr_in3_min.attr,
+       &dev_attr_in3_max.attr,
+       &sensor_dev_attr_in3_alarm.dev_attr.attr,
+       &dev_attr_in4_input.attr,
+       &dev_attr_in4_min.attr,
+       &dev_attr_in4_max.attr,
+       &sensor_dev_attr_in4_alarm.dev_attr.attr,
+
+       &dev_attr_temp1_input.attr,
+       &dev_attr_temp1_max.attr,
+       &dev_attr_temp1_min.attr,
+       &dev_attr_temp1_crit.attr,
+       &sensor_dev_attr_temp1_alarm.dev_attr.attr,
+       &dev_attr_temp2_input.attr,
+       &dev_attr_temp2_max.attr,
+       &dev_attr_temp2_min.attr,
+       &dev_attr_temp2_crit.attr,
+       &sensor_dev_attr_temp2_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp2_fault.dev_attr.attr,
+
+       &dev_attr_alarms.attr,
+       &dev_attr_aout_output.attr,
+
+       NULL
+};
 
-       if (!(data = kmalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
-       }
-       memset(data, 0, sizeof(struct lm87_data));
+static const struct attribute_group lm87_group = {
+       .attrs = lm87_attributes,
+};
 
-       /* The common I2C client data is placed right before the
-          LM87-specific data. */
-       new_client = &data->client;
-       i2c_set_clientdata(new_client, data);
-       new_client->addr = address;
-       new_client->adapter = adapter;
-       new_client->driver = &lm87_driver;
-       new_client->flags = 0;
+static struct attribute *lm87_attributes_opt[] = {
+       &dev_attr_in6_input.attr,
+       &dev_attr_in6_min.attr,
+       &dev_attr_in6_max.attr,
+       &sensor_dev_attr_in6_alarm.dev_attr.attr,
+
+       &dev_attr_fan1_input.attr,
+       &dev_attr_fan1_min.attr,
+       &dev_attr_fan1_div.attr,
+       &sensor_dev_attr_fan1_alarm.dev_attr.attr,
+
+       &dev_attr_in7_input.attr,
+       &dev_attr_in7_min.attr,
+       &dev_attr_in7_max.attr,
+       &sensor_dev_attr_in7_alarm.dev_attr.attr,
+
+       &dev_attr_fan2_input.attr,
+       &dev_attr_fan2_min.attr,
+       &dev_attr_fan2_div.attr,
+       &sensor_dev_attr_fan2_alarm.dev_attr.attr,
+
+       &dev_attr_temp3_input.attr,
+       &dev_attr_temp3_max.attr,
+       &dev_attr_temp3_min.attr,
+       &dev_attr_temp3_crit.attr,
+       &sensor_dev_attr_temp3_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp3_fault.dev_attr.attr,
+
+       &dev_attr_in0_input.attr,
+       &dev_attr_in0_min.attr,
+       &dev_attr_in0_max.attr,
+       &sensor_dev_attr_in0_alarm.dev_attr.attr,
+       &dev_attr_in5_input.attr,
+       &dev_attr_in5_min.attr,
+       &dev_attr_in5_max.attr,
+       &sensor_dev_attr_in5_alarm.dev_attr.attr,
+
+       &dev_attr_cpu0_vid.attr,
+       &dev_attr_vrm.attr,
+
+       NULL
+};
+
+static const struct attribute_group lm87_group_opt = {
+       .attrs = lm87_attributes_opt,
+};
+
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int lm87_detect(struct i2c_client *new_client, int kind,
+                      struct i2c_board_info *info)
+{
+       struct i2c_adapter *adapter = new_client->adapter;
+       static const char *names[] = { "lm87", "adm1024" };
+
+       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
+               return -ENODEV;
 
        /* Default to an LM87 if forced */
        if (kind == 0)
@@ -575,26 +677,45 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
 
        /* Now, we do the remaining detection. */
        if (kind < 0) {
+               u8 cid = lm87_read_value(new_client, LM87_REG_COMPANY_ID);
                u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
 
-               if (rev < 0x01 || rev > 0x08
-                || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
-                || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
+               if (cid == 0x02                 /* National Semiconductor */
+                && (rev >= 0x01 && rev <= 0x08))
+                       kind = lm87;
+               else if (cid == 0x41            /* Analog Devices */
+                     && (rev & 0xf0) == 0x10)
+                       kind = adm1024;
+
+               if (kind < 0
+                || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)) {
                        dev_dbg(&adapter->dev,
                                "LM87 detection failed at 0x%02x.\n",
-                               address);
-                       goto exit_free;
+                               new_client->addr);
+                       return -ENODEV;
                }
        }
 
-       /* We can fill in the remaining client fields */
-       strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
-       data->valid = 0;
-       init_MUTEX(&data->update_lock);
+       strlcpy(info->type, names[kind - 1], I2C_NAME_SIZE);
 
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(new_client)))
-               goto exit_free;
+       return 0;
+}
+
+static int lm87_probe(struct i2c_client *new_client,
+                     const struct i2c_device_id *id)
+{
+       struct lm87_data *data;
+       int err;
+
+       data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
+       }
+
+       i2c_set_clientdata(new_client, data);
+       data->valid = 0;
+       mutex_init(&data->update_lock);
 
        /* Initialize the LM87 chip */
        lm87_init_client(new_client);
@@ -609,80 +730,109 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
        data->in_scale[7] = 1875;
 
        /* Register sysfs hooks */
-       data->class_dev = hwmon_device_register(&new_client->dev);
-       if (IS_ERR(data->class_dev)) {
-               err = PTR_ERR(data->class_dev);
-               goto exit_detach;
-       }
-
-       device_create_file(&new_client->dev, &dev_attr_in1_input);
-       device_create_file(&new_client->dev, &dev_attr_in1_min);
-       device_create_file(&new_client->dev, &dev_attr_in1_max);
-       device_create_file(&new_client->dev, &dev_attr_in2_input);
-       device_create_file(&new_client->dev, &dev_attr_in2_min);
-       device_create_file(&new_client->dev, &dev_attr_in2_max);
-       device_create_file(&new_client->dev, &dev_attr_in3_input);
-       device_create_file(&new_client->dev, &dev_attr_in3_min);
-       device_create_file(&new_client->dev, &dev_attr_in3_max);
-       device_create_file(&new_client->dev, &dev_attr_in4_input);
-       device_create_file(&new_client->dev, &dev_attr_in4_min);
-       device_create_file(&new_client->dev, &dev_attr_in4_max);
+       if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
+               goto exit_free;
 
        if (data->channel & CHAN_NO_FAN(0)) {
-               device_create_file(&new_client->dev, &dev_attr_in6_input);
-               device_create_file(&new_client->dev, &dev_attr_in6_min);
-               device_create_file(&new_client->dev, &dev_attr_in6_max);
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_in6_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in6_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in6_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in6_alarm.dev_attr)))
+                       goto exit_remove;
        } else {
-               device_create_file(&new_client->dev, &dev_attr_fan1_input);
-               device_create_file(&new_client->dev, &dev_attr_fan1_min);
-               device_create_file(&new_client->dev, &dev_attr_fan1_div);
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_fan1_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_fan1_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_fan1_div))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_fan1_alarm.dev_attr)))
+                       goto exit_remove;
        }
+
        if (data->channel & CHAN_NO_FAN(1)) {
-               device_create_file(&new_client->dev, &dev_attr_in7_input);
-               device_create_file(&new_client->dev, &dev_attr_in7_min);
-               device_create_file(&new_client->dev, &dev_attr_in7_max);
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_in7_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in7_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in7_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in7_alarm.dev_attr)))
+                       goto exit_remove;
        } else {
-               device_create_file(&new_client->dev, &dev_attr_fan2_input);
-               device_create_file(&new_client->dev, &dev_attr_fan2_min);
-               device_create_file(&new_client->dev, &dev_attr_fan2_div);
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_fan2_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_fan2_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_fan2_div))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_fan2_alarm.dev_attr)))
+                       goto exit_remove;
        }
 
-       device_create_file(&new_client->dev, &dev_attr_temp1_input);
-       device_create_file(&new_client->dev, &dev_attr_temp1_max);
-       device_create_file(&new_client->dev, &dev_attr_temp1_min);
-       device_create_file(&new_client->dev, &dev_attr_temp1_crit);
-       device_create_file(&new_client->dev, &dev_attr_temp2_input);
-       device_create_file(&new_client->dev, &dev_attr_temp2_max);
-       device_create_file(&new_client->dev, &dev_attr_temp2_min);
-       device_create_file(&new_client->dev, &dev_attr_temp2_crit);
-
        if (data->channel & CHAN_TEMP3) {
-               device_create_file(&new_client->dev, &dev_attr_temp3_input);
-               device_create_file(&new_client->dev, &dev_attr_temp3_max);
-               device_create_file(&new_client->dev, &dev_attr_temp3_min);
-               device_create_file(&new_client->dev, &dev_attr_temp3_crit);
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_temp3_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_temp3_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_temp3_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_temp3_crit))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_temp3_alarm.dev_attr))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_temp3_fault.dev_attr)))
+                       goto exit_remove;
        } else {
-               device_create_file(&new_client->dev, &dev_attr_in0_input);
-               device_create_file(&new_client->dev, &dev_attr_in0_min);
-               device_create_file(&new_client->dev, &dev_attr_in0_max);
-               device_create_file(&new_client->dev, &dev_attr_in5_input);
-               device_create_file(&new_client->dev, &dev_attr_in5_min);
-               device_create_file(&new_client->dev, &dev_attr_in5_max);
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_in0_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in0_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in0_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in0_alarm.dev_attr))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in5_input))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in5_min))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_in5_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in5_alarm.dev_attr)))
+                       goto exit_remove;
        }
 
        if (!(data->channel & CHAN_NO_VID)) {
-               device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
-               device_create_file(&new_client->dev, &dev_attr_vrm);
+               data->vrm = vid_which_vrm();
+               if ((err = device_create_file(&new_client->dev,
+                                       &dev_attr_cpu0_vid))
+                || (err = device_create_file(&new_client->dev,
+                                       &dev_attr_vrm)))
+                       goto exit_remove;
        }
 
-       device_create_file(&new_client->dev, &dev_attr_alarms);
-       device_create_file(&new_client->dev, &dev_attr_aout_output);
+       data->hwmon_dev = hwmon_device_register(&new_client->dev);
+       if (IS_ERR(data->hwmon_dev)) {
+               err = PTR_ERR(data->hwmon_dev);
+               goto exit_remove;
+       }
 
        return 0;
 
-exit_detach:
-       i2c_detach_client(new_client);
+exit_remove:
+       sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
+       sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
 exit_free:
+       lm87_write_value(new_client, LM87_REG_CONFIG, data->config);
        kfree(data);
 exit:
        return err;
@@ -691,13 +841,17 @@ exit:
 static void lm87_init_client(struct i2c_client *client)
 {
        struct lm87_data *data = i2c_get_clientdata(client);
-       u8 config;
 
-       data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
-       data->vrm = i2c_which_vrm();
+       if (client->dev.platform_data) {
+               data->channel = *(u8 *)client->dev.platform_data;
+               lm87_write_value(client,
+                                LM87_REG_CHANNEL_MODE, data->channel);
+       } else {
+               data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
+       }
+       data->config = lm87_read_value(client, LM87_REG_CONFIG) & 0x6F;
 
-       config = lm87_read_value(client, LM87_REG_CONFIG);
-       if (!(config & 0x01)) {
+       if (!(data->config & 0x01)) {
                int i;
 
                /* Limits are left uninitialized after power-up */
@@ -719,23 +873,22 @@ static void lm87_init_client(struct i2c_client *client)
                        lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
                }
        }
-       if ((config & 0x81) != 0x01) {
-               /* Start monitoring */
+
+       /* Make sure Start is set and INT#_Clear is clear */
+       if ((data->config & 0x09) != 0x01)
                lm87_write_value(client, LM87_REG_CONFIG,
-                                (config & 0xF7) | 0x01);
-       }
+                                (data->config & 0x77) | 0x01);
 }
 
-static int lm87_detach_client(struct i2c_client *client)
+static int lm87_remove(struct i2c_client *client)
 {
        struct lm87_data *data = i2c_get_clientdata(client);
-       int err;
-
-       hwmon_device_unregister(data->class_dev);
 
-       if ((err = i2c_detach_client(client)))
-               return err;
+       hwmon_device_unregister(data->hwmon_dev);
+       sysfs_remove_group(&client->dev.kobj, &lm87_group);
+       sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
 
+       lm87_write_value(client, LM87_REG_CONFIG, data->config);
        kfree(data);
        return 0;
 }
@@ -745,7 +898,7 @@ static struct lm87_data *lm87_update_device(struct device *dev)
        struct i2c_client *client = to_i2c_client(dev);
        struct lm87_data *data = i2c_get_clientdata(client);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
 
        if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
                int i, j;
@@ -814,7 +967,7 @@ static struct lm87_data *lm87_update_device(struct device *dev)
                data->valid = 1;
        }
 
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 
        return data;
 }