Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[sfrench/cifs-2.6.git] / drivers / hwmon / lm87.c
index 988ae1c4aada69ebb2f9315258ac1b4ee878f65b..8ee07c5c97a1df5b504dd8abaff0cd7b54e78709 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,2007  Jean Delvare <khali@linux-fr.org>
  *
  * Original port to Linux 2.6 by Jeff Oliver.
  *
  * 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
@@ -58,6 +63,7 @@
 #include <linux/jiffies.h>
 #include <linux/i2c.h>
 #include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 #include <linux/hwmon-vid.h>
 #include <linux/err.h>
 #include <linux/mutex.h>
@@ -73,7 +79,7 @@ static 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
@@ -129,7 +135,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)))
 
@@ -145,7 +151,7 @@ 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
@@ -165,7 +171,6 @@ static struct i2c_driver lm87_driver = {
        .driver = {
                .name   = "lm87",
        },
-       .id             = I2C_DRIVERID_LM87,
        .attach_adapter = lm87_attach_adapter,
        .detach_client  = lm87_detach_client,
 };
@@ -176,7 +181,7 @@ static struct i2c_driver lm87_driver = {
 
 struct lm87_data {
        struct i2c_client client;
-       struct class_device *class_dev;
+       struct device *hwmon_dev;
        struct mutex update_lock;
        char valid; /* zero until following fields are valid */
        unsigned long last_updated; /* In jiffies */
@@ -500,13 +505,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;
 }
@@ -531,6 +535,29 @@ static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const
 }
 static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
 
+static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
+                         char *buf)
+{
+       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);
+
 /*
  * Real code
  */
@@ -546,24 +573,31 @@ 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,
@@ -579,30 +613,38 @@ 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,
@@ -623,6 +665,7 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
        struct i2c_client *new_client;
        struct lm87_data *data;
        int err = 0;
+       static const char *names[] = { "lm87", "adm1024" };
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
                goto exit;
@@ -647,11 +690,18 @@ 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);
@@ -660,7 +710,7 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
        }
 
        /* We can fill in the remaining client fields */
-       strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
+       strlcpy(new_client->name, names[kind - 1], I2C_NAME_SIZE);
        data->valid = 0;
        mutex_init(&data->update_lock);
 
@@ -690,7 +740,9 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&new_client->dev,
                                        &dev_attr_in6_min))
                 || (err = device_create_file(&new_client->dev,
-                                       &dev_attr_in6_max)))
+                                       &dev_attr_in6_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in6_alarm.dev_attr)))
                        goto exit_remove;
        } else {
                if ((err = device_create_file(&new_client->dev,
@@ -698,7 +750,9 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&new_client->dev,
                                        &dev_attr_fan1_min))
                 || (err = device_create_file(&new_client->dev,
-                                       &dev_attr_fan1_div)))
+                                       &dev_attr_fan1_div))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_fan1_alarm.dev_attr)))
                        goto exit_remove;
        }
 
@@ -708,7 +762,9 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&new_client->dev,
                                        &dev_attr_in7_min))
                 || (err = device_create_file(&new_client->dev,
-                                       &dev_attr_in7_max)))
+                                       &dev_attr_in7_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in7_alarm.dev_attr)))
                        goto exit_remove;
        } else {
                if ((err = device_create_file(&new_client->dev,
@@ -716,7 +772,9 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&new_client->dev,
                                        &dev_attr_fan2_min))
                 || (err = device_create_file(&new_client->dev,
-                                       &dev_attr_fan2_div)))
+                                       &dev_attr_fan2_div))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_fan2_alarm.dev_attr)))
                        goto exit_remove;
        }
 
@@ -728,7 +786,11 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&new_client->dev,
                                        &dev_attr_temp3_min))
                 || (err = device_create_file(&new_client->dev,
-                                       &dev_attr_temp3_crit)))
+                                       &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 {
                if ((err = device_create_file(&new_client->dev,
@@ -737,12 +799,16 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                                        &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)))
+                                       &dev_attr_in5_max))
+                || (err = device_create_file(&new_client->dev,
+                                       &sensor_dev_attr_in5_alarm.dev_attr)))
                        goto exit_remove;
        }
 
@@ -755,9 +821,9 @@ static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
                        goto exit_remove;
        }
 
-       data->class_dev = hwmon_device_register(&new_client->dev);
-       if (IS_ERR(data->class_dev)) {
-               err = PTR_ERR(data->class_dev);
+       data->hwmon_dev = hwmon_device_register(&new_client->dev);
+       if (IS_ERR(data->hwmon_dev)) {
+               err = PTR_ERR(data->hwmon_dev);
                goto exit_remove;
        }
 
@@ -816,7 +882,7 @@ static int lm87_detach_client(struct i2c_client *client)
        struct lm87_data *data = i2c_get_clientdata(client);
        int err;
 
-       hwmon_device_unregister(data->class_dev);
+       hwmon_device_unregister(data->hwmon_dev);
        sysfs_remove_group(&client->dev.kobj, &lm87_group);
        sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);