Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groec...
[linux-2.6.git] / drivers / hwmon / lm83.c
index 0223b4d..8290476 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-2009  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
 #include <linux/slab.h>
 #include <linux/jiffies.h>
 #include <linux/i2c.h>
-#include <linux/i2c-sensor.h>
 #include <linux/hwmon-sysfs.h>
 #include <linux/hwmon.h>
 #include <linux/err.h>
+#include <linux/mutex.h>
+#include <linux/sysfs.h>
 
 /*
  * Addresses to scan
  * addresses.
  */
 
-static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
-                                       0x29, 0x2a, 0x2b,
-                                       0x4c, 0x4d, 0x4e,
-                                       I2C_CLIENT_END };
-
-/*
- * Insmod parameters
- */
+static const unsigned short normal_i2c[] = {
+       0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
 
-SENSORS_INSMOD_1(lm83);
+enum chips { lm83, lm82 };
 
 /*
  * The LM83 registers
@@ -115,22 +114,34 @@ static const u8 LM83_REG_W_HIGH[] = {
  * Functions declaration
  */
 
-static int lm83_attach_adapter(struct i2c_adapter *adapter);
-static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
-static int lm83_detach_client(struct i2c_client *client);
+static int lm83_detect(struct i2c_client *new_client,
+                      struct i2c_board_info *info);
+static int lm83_probe(struct i2c_client *client,
+                     const struct i2c_device_id *id);
+static int lm83_remove(struct i2c_client *client);
 static struct lm83_data *lm83_update_device(struct device *dev);
 
 /*
  * Driver data (common to all clients)
  */
  
+static const struct i2c_device_id lm83_id[] = {
+       { "lm83", lm83 },
+       { "lm82", lm82 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, lm83_id);
+
 static struct i2c_driver lm83_driver = {
-       .owner          = THIS_MODULE,
-       .name           = "lm83",
-       .id             = I2C_DRIVERID_LM83,
-       .flags          = I2C_DF_NOTIFY,
-       .attach_adapter = lm83_attach_adapter,
-       .detach_client  = lm83_detach_client,
+       .class          = I2C_CLASS_HWMON,
+       .driver = {
+               .name   = "lm83",
+       },
+       .probe          = lm83_probe,
+       .remove         = lm83_remove,
+       .id_table       = lm83_id,
+       .detect         = lm83_detect,
+       .address_list   = normal_i2c,
 };
 
 /*
@@ -138,9 +149,8 @@ static struct i2c_driver lm83_driver = {
  */
 
 struct lm83_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 */
 
@@ -172,11 +182,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;
 }
 
@@ -187,6 +197,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);
@@ -204,170 +224,170 @@ 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_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_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_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);
 
-/*
- * Real code
- */
+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_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 int lm83_attach_adapter(struct i2c_adapter *adapter)
-{
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_detect(adapter, &addr_data, lm83_detect);
-}
+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_fault.dev_attr.attr,
+       &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp2_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,
+};
 
 /*
- * The following function does more than just detection. If detection
- * succeeds, it also registers the new chip.
+ * Real code
  */
-static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
+
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int lm83_detect(struct i2c_client *new_client,
+                      struct i2c_board_info *info)
 {
-       struct i2c_client *new_client;
-       struct lm83_data *data;
-       int err = 0;
-       const char *name = "";
+       struct i2c_adapter *adapter = new_client->adapter;
+       const char *name;
+       u8 man_id, chip_id;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
-               goto exit;
-
-       if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
+               return -ENODEV;
+
+       /* Detection */
+       if ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1) & 0xA8) ||
+           (i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2) & 0x48) ||
+           (i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG) & 0x41)) {
+               dev_dbg(&adapter->dev, "LM83 detection failed at 0x%02x\n",
+                       new_client->addr);
+               return -ENODEV;
        }
-       memset(data, 0, sizeof(struct lm83_data));
 
-       /* The common I2C client data is placed right after the
-        * LM83-specific data. */
-       new_client = &data->client;
-       i2c_set_clientdata(new_client, data);
-       new_client->addr = address;
-       new_client->adapter = adapter;
-       new_client->driver = &lm83_driver;
-       new_client->flags = 0;
-
-       /* Now we do the detection and identification. A negative kind
-        * means that the driver was loaded with no force parameter
-        * (default), so we must both detect and identify the chip
-        * (actually there is only one possible kind of chip for now, LM83).
-        * A zero kind means that the driver was loaded with the force
-        * parameter, the detection step shall be skipped. A positive kind
-        * means that the driver was loaded with the force parameter and a
-        * given kind of chip is requested, so both the detection and the
-        * identification steps are skipped. */
-
-       /* Default to an LM83 if forced */
-       if (kind == 0)
-               kind = lm83;
-
-       if (kind < 0) { /* detection */
-               if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
-                   & 0xA8) != 0x00) ||
-                   ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
-                   & 0x48) != 0x00) ||
-                   ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
-                   & 0x41) != 0x00)) {
-                       dev_dbg(&adapter->dev,
-                           "LM83 detection failed at 0x%02x.\n", address);
-                       goto exit_free;
-               }
-       }
+       /* Identification */
+       man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
+       if (man_id != 0x01)     /* National Semiconductor */
+               return -ENODEV;
 
-       if (kind <= 0) { /* identification */
-               u8 man_id, chip_id;
+       chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
+       switch (chip_id) {
+       case 0x03:
+               name = "lm83";
+               break;
+       case 0x01:
+               name = "lm82";
+               break;
+       default:
+               /* identification failed */
+               dev_info(&adapter->dev,
+                        "Unsupported chip (man_id=0x%02X, chip_id=0x%02X)\n",
+                        man_id, chip_id);
+               return -ENODEV;
+       }
 
-               man_id = i2c_smbus_read_byte_data(new_client,
-                   LM83_REG_R_MAN_ID);
-               chip_id = i2c_smbus_read_byte_data(new_client,
-                   LM83_REG_R_CHIP_ID);
+       strlcpy(info->type, name, I2C_NAME_SIZE);
 
-               if (man_id == 0x01) { /* National Semiconductor */
-                       if (chip_id == 0x03) {
-                               kind = lm83;
-                       }
-               }
+       return 0;
+}
 
-               if (kind <= 0) { /* identification failed */
-                       dev_info(&adapter->dev,
-                           "Unsupported chip (man_id=0x%02X, "
-                           "chip_id=0x%02X).\n", man_id, chip_id);
-                       goto exit_free;
-               }
-       }
+static int lm83_probe(struct i2c_client *new_client,
+                     const struct i2c_device_id *id)
+{
+       struct lm83_data *data;
+       int err;
 
-       if (kind == lm83) {
-               name = "lm83";
+       data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
        }
 
-       /* We can fill in the remaining client fields */
-       strlcpy(new_client->name, name, I2C_NAME_SIZE);
+       i2c_set_clientdata(new_client, data);
        data->valid = 0;
-       init_MUTEX(&data->update_lock);
-
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(new_client)))
-               goto exit_free;
+       mutex_init(&data->update_lock);
 
        /*
-        * 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 */
-       data->class_dev = hwmon_device_register(&new_client->dev);
-       if (IS_ERR(data->class_dev)) {
-               err = PTR_ERR(data->class_dev);
-               goto exit_detach;
+       if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
+               goto exit_free;
+
+       if (id->driver_data == lm83) {
+               if ((err = sysfs_create_group(&new_client->dev.kobj,
+                                             &lm83_group_opt)))
+                       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);
+       data->hwmon_dev = hwmon_device_register(&new_client->dev);
+       if (IS_ERR(data->hwmon_dev)) {
+               err = PTR_ERR(data->hwmon_dev);
+               goto exit_remove_files;
+       }
 
        return 0;
 
-exit_detach:
-       i2c_detach_client(new_client);
+exit_remove_files:
+       sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
+       sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
 exit_free:
        kfree(data);
 exit:
        return err;
 }
 
-static int lm83_detach_client(struct i2c_client *client)
+static int lm83_remove(struct i2c_client *client)
 {
        struct lm83_data *data = i2c_get_clientdata(client);
-       int err;
-
-       hwmon_device_unregister(data->class_dev);
 
-       if ((err = i2c_detach_client(client))) {
-               dev_err(&client->dev,
-                   "Client deregistration failed, client not detached.\n");
-               return err;
-       }
+       hwmon_device_unregister(data->hwmon_dev);
+       sysfs_remove_group(&client->dev.kobj, &lm83_group);
+       sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
 
        kfree(data);
        return 0;
@@ -378,7 +398,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;
@@ -398,7 +418,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;
 }