hwmon: (it87) Add support for beep on alarm
[safe/jmp/linux-2.6] / drivers / hwmon / lm80.c
index 612807c..18a0e6c 100644 (file)
 #include <linux/mutex.h>
 
 /* Addresses to scan */
-static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
-                                       0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
-
-/* Insmod parameters */
-I2C_CLIENT_INSMOD_1(lm80);
+static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
+                                               0x2e, 0x2f, I2C_CLIENT_END };
 
 /* Many LM80 constants specified below */
 
@@ -108,7 +105,6 @@ static inline long TEMP_FROM_REG(u16 temp)
  */
 
 struct lm80_data {
-       struct i2c_client client;
        struct device *hwmon_dev;
        struct mutex update_lock;
        char valid;             /* !=0 if following fields are valid */
@@ -132,10 +128,11 @@ struct lm80_data {
  * Functions declaration
  */
 
-static int lm80_attach_adapter(struct i2c_adapter *adapter);
-static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
+static int lm80_probe(struct i2c_client *client,
+                     const struct i2c_device_id *id);
+static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
 static void lm80_init_client(struct i2c_client *client);
-static int lm80_detach_client(struct i2c_client *client);
+static int lm80_remove(struct i2c_client *client);
 static struct lm80_data *lm80_update_device(struct device *dev);
 static int lm80_read_value(struct i2c_client *client, u8 reg);
 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
@@ -144,12 +141,22 @@ static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
  * Driver data (common to all clients)
  */
 
+static const struct i2c_device_id lm80_id[] = {
+       { "lm80", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, lm80_id);
+
 static struct i2c_driver lm80_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                .name   = "lm80",
        },
-       .attach_adapter = lm80_attach_adapter,
-       .detach_client  = lm80_detach_client,
+       .probe          = lm80_probe,
+       .remove         = lm80_remove,
+       .id_table       = lm80_id,
+       .detect         = lm80_detect,
+       .address_list   = normal_i2c,
 };
 
 /*
@@ -304,6 +311,14 @@ static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
        return sprintf(buf, "%u\n", data->alarms);
 }
 
+static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
+                         char *buf)
+{
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       struct lm80_data *data = lm80_update_device(dev);
+       return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
+}
+
 static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
                show_in_min, set_in_min, 0);
 static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
@@ -359,18 +374,22 @@ static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
     set_temp_os_hyst);
 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
+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, 4);
+static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
+static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
+static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
+static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
+static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
+static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
 
 /*
  * Real code
  */
 
-static int lm80_attach_adapter(struct i2c_adapter *adapter)
-{
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, lm80_detect);
-}
-
 static struct attribute *lm80_attributes[] = {
        &sensor_dev_attr_in0_min.dev_attr.attr,
        &sensor_dev_attr_in1_min.dev_attr.attr,
@@ -405,7 +424,17 @@ static struct attribute *lm80_attributes[] = {
        &dev_attr_temp1_crit.attr,
        &dev_attr_temp1_crit_hyst.attr,
        &dev_attr_alarms.attr,
-
+       &sensor_dev_attr_in0_alarm.dev_attr.attr,
+       &sensor_dev_attr_in1_alarm.dev_attr.attr,
+       &sensor_dev_attr_in2_alarm.dev_attr.attr,
+       &sensor_dev_attr_in3_alarm.dev_attr.attr,
+       &sensor_dev_attr_in4_alarm.dev_attr.attr,
+       &sensor_dev_attr_in5_alarm.dev_attr.attr,
+       &sensor_dev_attr_in6_alarm.dev_attr.attr,
+       &sensor_dev_attr_fan1_alarm.dev_attr.attr,
+       &sensor_dev_attr_fan2_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
        NULL
 };
 
@@ -413,53 +442,45 @@ static const struct attribute_group lm80_group = {
        .attrs = lm80_attributes,
 };
 
-static int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
 {
+       struct i2c_adapter *adapter = client->adapter;
        int i, cur;
-       struct i2c_client *client;
-       struct lm80_data *data;
-       int err = 0;
-       const char *name;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
-               goto exit;
-
-       /* 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 lm80_{read,write}_value. */
-       if (!(data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
-       }
-
-       client = &data->client;
-       i2c_set_clientdata(client, data);
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &lm80_driver;
+               return -ENODEV;
 
        /* Now, we do the remaining detection. It is lousy. */
        if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
-               goto error_free;
+               return -ENODEV;
        for (i = 0x2a; i <= 0x3d; i++) {
                cur = i2c_smbus_read_byte_data(client, i);
                if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
                 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
                 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
-                   goto error_free;
+                   return -ENODEV;
        }
 
-       /* Determine the chip type - only one kind supported! */
-       kind = lm80;
-       name = "lm80";
+       strlcpy(info->type, "lm80", I2C_NAME_SIZE);
 
-       /* Fill in the remaining client fields */
-       strlcpy(client->name, name, I2C_NAME_SIZE);
-       mutex_init(&data->update_lock);
+       return 0;
+}
 
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(client)))
-               goto error_free;
+static int lm80_probe(struct i2c_client *client,
+                     const struct i2c_device_id *id)
+{
+       struct lm80_data *data;
+       int err;
+
+       data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
+       }
+
+       i2c_set_clientdata(client, data);
+       mutex_init(&data->update_lock);
 
        /* Initialize the LM80 chip */
        lm80_init_client(client);
@@ -470,7 +491,7 @@ static int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
 
        /* Register sysfs hooks */
        if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group)))
-               goto error_detach;
+               goto error_free;
 
        data->hwmon_dev = hwmon_device_register(&client->dev);
        if (IS_ERR(data->hwmon_dev)) {
@@ -482,23 +503,18 @@ static int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
 
 error_remove:
        sysfs_remove_group(&client->dev.kobj, &lm80_group);
-error_detach:
-       i2c_detach_client(client);
 error_free:
        kfree(data);
 exit:
        return err;
 }
 
-static int lm80_detach_client(struct i2c_client *client)
+static int lm80_remove(struct i2c_client *client)
 {
        struct lm80_data *data = i2c_get_clientdata(client);
-       int err;
 
        hwmon_device_unregister(data->hwmon_dev);
        sysfs_remove_group(&client->dev.kobj, &lm80_group);
-       if ((err = i2c_detach_client(client)))
-               return err;
 
        kfree(data);
        return 0;