fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / hwmon / lm83.c
index 26dfa9e..feb87b4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
  *          monitoring
- * Copyright (C) 2003-2005  Jean Delvare <khali@linux-fr.org>
+ * Copyright (C) 2003-2006  Jean Delvare <khali@linux-fr.org>
  *
  * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
  * a sensor chip made by National Semiconductor. It reports up to four
  * Since the datasheet omits to give the chip stepping code, I give it
  * here: 0x03 (at register 0xff).
  *
+ * Also supports the LM82 temp sensor, which is basically a stripped down
+ * model of the LM83.  Datasheet is here:
+ * http://www.national.com/pf/LM/LM82.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
@@ -35,6 +39,8 @@
 #include <linux/hwmon-sysfs.h>
 #include <linux/hwmon.h>
 #include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
 
 /*
  * Addresses to scan
@@ -51,7 +57,7 @@ static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
  * Insmod parameters
  */
 
-I2C_CLIENT_INSMOD_1(lm83);
+I2C_CLIENT_INSMOD_2(lm83, lm82);
 
 /*
  * The LM83 registers
@@ -139,7 +145,7 @@ static struct i2c_driver lm83_driver = {
 struct lm83_data {
        struct i2c_client client;
        struct class_device *class_dev;
-       struct semaphore update_lock;
+       struct mutex update_lock;
        char valid; /* zero until following fields are valid */
        unsigned long last_updated; /* in jiffies */
 
@@ -171,11 +177,11 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
        long val = simple_strtol(buf, NULL, 10);
        int nr = attr->index;
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->temp[nr] = TEMP_TO_REG(val);
        i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
                                  data->temp[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
 
@@ -186,6 +192,16 @@ static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
        return sprintf(buf, "%d\n", data->alarms);
 }
 
+static ssize_t show_alarm(struct device *dev, struct device_attribute
+                         *devattr, char *buf)
+{
+       struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+       struct lm83_data *data = lm83_update_device(dev);
+       int bitnr = attr->index;
+
+       return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
+}
+
 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
@@ -203,8 +219,64 @@ static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
 static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
        set_temp, 8);
 static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
+
+/* Individual alarm files */
+static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
+static SENSOR_DEVICE_ATTR(temp3_input_fault, S_IRUGO, show_alarm, NULL, 2);
+static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
+static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
+static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
+static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
+static SENSOR_DEVICE_ATTR(temp4_input_fault, S_IRUGO, show_alarm, NULL, 10);
+static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
+static SENSOR_DEVICE_ATTR(temp2_input_fault, S_IRUGO, show_alarm, NULL, 13);
+static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
+/* Raw alarm file for compatibility */
 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
 
+static struct attribute *lm83_attributes[] = {
+       &sensor_dev_attr_temp1_input.dev_attr.attr,
+       &sensor_dev_attr_temp3_input.dev_attr.attr,
+       &sensor_dev_attr_temp1_max.dev_attr.attr,
+       &sensor_dev_attr_temp3_max.dev_attr.attr,
+       &sensor_dev_attr_temp1_crit.dev_attr.attr,
+       &sensor_dev_attr_temp3_crit.dev_attr.attr,
+
+       &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp3_input_fault.dev_attr.attr,
+       &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
+       &dev_attr_alarms.attr,
+       NULL
+};
+
+static const struct attribute_group lm83_group = {
+       .attrs = lm83_attributes,
+};
+
+static struct attribute *lm83_attributes_opt[] = {
+       &sensor_dev_attr_temp2_input.dev_attr.attr,
+       &sensor_dev_attr_temp4_input.dev_attr.attr,
+       &sensor_dev_attr_temp2_max.dev_attr.attr,
+       &sensor_dev_attr_temp4_max.dev_attr.attr,
+       &sensor_dev_attr_temp2_crit.dev_attr.attr,
+       &sensor_dev_attr_temp4_crit.dev_attr.attr,
+
+       &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp4_input_fault.dev_attr.attr,
+       &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp2_input_fault.dev_attr.attr,
+       &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
+       NULL
+};
+
+static const struct attribute_group lm83_group_opt = {
+       .attrs = lm83_attributes_opt,
+};
+
 /*
  * Real code
  */
@@ -282,6 +354,9 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
                if (man_id == 0x01) { /* National Semiconductor */
                        if (chip_id == 0x03) {
                                kind = lm83;
+                       } else
+                       if (chip_id == 0x01) {
+                               kind = lm82;
                        }
                }
 
@@ -295,57 +370,47 @@ static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
 
        if (kind == lm83) {
                name = "lm83";
+       } else
+       if (kind == lm82) {
+               name = "lm82";
        }
 
        /* We can fill in the remaining client fields */
        strlcpy(new_client->name, name, I2C_NAME_SIZE);
        data->valid = 0;
-       init_MUTEX(&data->update_lock);
+       mutex_init(&data->update_lock);
 
        /* Tell the I2C layer a new client has arrived */
        if ((err = i2c_attach_client(new_client)))
                goto exit_free;
 
        /*
-        * Initialize the LM83 chip
-        * (Nothing to do for this one.)
+        * Register sysfs hooks
+        * The LM82 can only monitor one external diode which is
+        * at the same register as the LM83 temp3 entry - so we
+        * declare 1 and 3 common, and then 2 and 4 only for the LM83.
         */
 
-       /* Register sysfs hooks */
+       if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
+               goto exit_detach;
+
+       if (kind == lm83) {
+               if ((err = sysfs_create_group(&new_client->dev.kobj,
+                                             &lm83_group_opt)))
+                       goto exit_remove_files;
+       }
+
        data->class_dev = hwmon_device_register(&new_client->dev);
        if (IS_ERR(data->class_dev)) {
                err = PTR_ERR(data->class_dev);
-               goto exit_detach;
+               goto exit_remove_files;
        }
 
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp1_input.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp2_input.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp3_input.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp4_input.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp1_max.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp2_max.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp3_max.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp4_max.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp1_crit.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp2_crit.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp3_crit.dev_attr);
-       device_create_file(&new_client->dev,
-                          &sensor_dev_attr_temp4_crit.dev_attr);
-       device_create_file(&new_client->dev, &dev_attr_alarms);
-
        return 0;
 
+exit_remove_files:
+       sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
+       sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
 exit_detach:
        i2c_detach_client(new_client);
 exit_free:
@@ -360,6 +425,8 @@ static int lm83_detach_client(struct i2c_client *client)
        int err;
 
        hwmon_device_unregister(data->class_dev);
+       sysfs_remove_group(&client->dev.kobj, &lm83_group);
+       sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
 
        if ((err = i2c_detach_client(client)))
                return err;
@@ -373,7 +440,7 @@ static struct lm83_data *lm83_update_device(struct device *dev)
        struct i2c_client *client = to_i2c_client(dev);
        struct lm83_data *data = i2c_get_clientdata(client);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
 
        if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
                int nr;
@@ -393,7 +460,7 @@ static struct lm83_data *lm83_update_device(struct device *dev)
                data->valid = 1;
        }
 
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 
        return data;
 }