switch lookup_mnt()
[linux-2.6.git] / drivers / hwmon / gl520sm.c
index 40bfdc7..19616f2 100644 (file)
@@ -39,7 +39,7 @@ module_param(extra_sensor_type, ushort, 0);
 MODULE_PARM_DESC(extra_sensor_type, "Type of extra sensor (0=autodetect, 1=temperature, 2=voltage)");
 
 /* Addresses to scan */
-static unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
+static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END };
 
 /* Insmod parameters */
 I2C_CLIENT_INSMOD_1(gl520sm);
@@ -79,26 +79,37 @@ static const u8 GL520_REG_TEMP_MAX_HYST[]   = { 0x06, 0x18 };
  * Function declarations
  */
 
-static int gl520_attach_adapter(struct i2c_adapter *adapter);
-static int gl520_detect(struct i2c_adapter *adapter, int address, int kind);
+static int gl520_probe(struct i2c_client *client,
+                      const struct i2c_device_id *id);
+static int gl520_detect(struct i2c_client *client, int kind,
+                       struct i2c_board_info *info);
 static void gl520_init_client(struct i2c_client *client);
-static int gl520_detach_client(struct i2c_client *client);
+static int gl520_remove(struct i2c_client *client);
 static int gl520_read_value(struct i2c_client *client, u8 reg);
 static int gl520_write_value(struct i2c_client *client, u8 reg, u16 value);
 static struct gl520_data *gl520_update_device(struct device *dev);
 
 /* Driver data */
+static const struct i2c_device_id gl520_id[] = {
+       { "gl520sm", gl520sm },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, gl520_id);
+
 static struct i2c_driver gl520_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                .name   = "gl520sm",
        },
-       .attach_adapter = gl520_attach_adapter,
-       .detach_client  = gl520_detach_client,
+       .probe          = gl520_probe,
+       .remove         = gl520_remove,
+       .id_table       = gl520_id,
+       .detect         = gl520_detect,
+       .address_data   = &addr_data,
 };
 
 /* Client data */
 struct gl520_data {
-       struct i2c_client client;
        struct device *hwmon_dev;
        struct mutex update_lock;
        char valid;             /* zero until the following fields are valid */
@@ -532,33 +543,108 @@ static DEVICE_ATTR(beep_enable, S_IRUGO | S_IWUSR,
 static DEVICE_ATTR(beep_mask, S_IRUGO | S_IWUSR,
                get_beep_mask, set_beep_mask);
 
+static ssize_t get_alarm(struct device *dev, struct device_attribute *attr,
+                        char *buf)
+{
+       int bit_nr = to_sensor_dev_attr(attr)->index;
+       struct gl520_data *data = gl520_update_device(dev);
+
+       return sprintf(buf, "%d\n", (data->alarms >> bit_nr) & 1);
+}
+
+static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, get_alarm, NULL, 0);
+static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, get_alarm, NULL, 1);
+static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, get_alarm, NULL, 2);
+static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, get_alarm, NULL, 3);
+static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, get_alarm, NULL, 4);
+static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, get_alarm, NULL, 5);
+static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, get_alarm, NULL, 6);
+static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, get_alarm, NULL, 7);
+static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, get_alarm, NULL, 7);
+
+static ssize_t get_beep(struct device *dev, struct device_attribute *attr,
+                       char *buf)
+{
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       struct gl520_data *data = gl520_update_device(dev);
+
+       return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
+}
+
+static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
+                       const char *buf, size_t count)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+       struct gl520_data *data = i2c_get_clientdata(client);
+       int bitnr = to_sensor_dev_attr(attr)->index;
+       unsigned long bit;
+
+       bit = simple_strtoul(buf, NULL, 10);
+       if (bit & ~1)
+               return -EINVAL;
+
+       mutex_lock(&data->update_lock);
+       data->beep_mask = gl520_read_value(client, GL520_REG_BEEP_MASK);
+       if (bit)
+               data->beep_mask |= (1 << bitnr);
+       else
+               data->beep_mask &= ~(1 << bitnr);
+       gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
+       mutex_unlock(&data->update_lock);
+       return count;
+}
+
+static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 0);
+static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 1);
+static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 2);
+static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 3);
+static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 4);
+static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 5);
+static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 6);
+static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
+static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO | S_IWUSR, get_beep, set_beep, 7);
+
 static struct attribute *gl520_attributes[] = {
        &dev_attr_cpu0_vid.attr,
 
        &sensor_dev_attr_in0_input.dev_attr.attr,
        &sensor_dev_attr_in0_min.dev_attr.attr,
        &sensor_dev_attr_in0_max.dev_attr.attr,
+       &sensor_dev_attr_in0_alarm.dev_attr.attr,
+       &sensor_dev_attr_in0_beep.dev_attr.attr,
        &sensor_dev_attr_in1_input.dev_attr.attr,
        &sensor_dev_attr_in1_min.dev_attr.attr,
        &sensor_dev_attr_in1_max.dev_attr.attr,
+       &sensor_dev_attr_in1_alarm.dev_attr.attr,
+       &sensor_dev_attr_in1_beep.dev_attr.attr,
        &sensor_dev_attr_in2_input.dev_attr.attr,
        &sensor_dev_attr_in2_min.dev_attr.attr,
        &sensor_dev_attr_in2_max.dev_attr.attr,
+       &sensor_dev_attr_in2_alarm.dev_attr.attr,
+       &sensor_dev_attr_in2_beep.dev_attr.attr,
        &sensor_dev_attr_in3_input.dev_attr.attr,
        &sensor_dev_attr_in3_min.dev_attr.attr,
        &sensor_dev_attr_in3_max.dev_attr.attr,
+       &sensor_dev_attr_in3_alarm.dev_attr.attr,
+       &sensor_dev_attr_in3_beep.dev_attr.attr,
 
        &sensor_dev_attr_fan1_input.dev_attr.attr,
        &sensor_dev_attr_fan1_min.dev_attr.attr,
        &sensor_dev_attr_fan1_div.dev_attr.attr,
+       &sensor_dev_attr_fan1_alarm.dev_attr.attr,
+       &sensor_dev_attr_fan1_beep.dev_attr.attr,
        &dev_attr_fan1_off.attr,
        &sensor_dev_attr_fan2_input.dev_attr.attr,
        &sensor_dev_attr_fan2_min.dev_attr.attr,
        &sensor_dev_attr_fan2_div.dev_attr.attr,
+       &sensor_dev_attr_fan2_alarm.dev_attr.attr,
+       &sensor_dev_attr_fan2_beep.dev_attr.attr,
 
        &sensor_dev_attr_temp1_input.dev_attr.attr,
        &sensor_dev_attr_temp1_max.dev_attr.attr,
        &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
+       &sensor_dev_attr_temp1_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp1_beep.dev_attr.attr,
 
        &dev_attr_alarms.attr,
        &dev_attr_beep_enable.attr,
@@ -574,10 +660,14 @@ static struct attribute *gl520_attributes_opt[] = {
        &sensor_dev_attr_in4_input.dev_attr.attr,
        &sensor_dev_attr_in4_min.dev_attr.attr,
        &sensor_dev_attr_in4_max.dev_attr.attr,
+       &sensor_dev_attr_in4_alarm.dev_attr.attr,
+       &sensor_dev_attr_in4_beep.dev_attr.attr,
 
        &sensor_dev_attr_temp2_input.dev_attr.attr,
        &sensor_dev_attr_temp2_max.dev_attr.attr,
        &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
+       &sensor_dev_attr_temp2_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp2_beep.dev_attr.attr,
        NULL
 };
 
@@ -590,37 +680,15 @@ static const struct attribute_group gl520_group_opt = {
  * Real code
  */
 
-static int gl520_attach_adapter(struct i2c_adapter *adapter)
-{
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, gl520_detect);
-}
-
-static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int gl520_detect(struct i2c_client *client, int kind,
+                       struct i2c_board_info *info)
 {
-       struct i2c_client *client;
-       struct gl520_data *data;
-       int err = 0;
+       struct i2c_adapter *adapter = client->adapter;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
                                     I2C_FUNC_SMBUS_WORD_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 gl520_{read,write}_value. */
-
-       if (!(data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
-       }
-
-       client = &data->client;
-       i2c_set_clientdata(client, data);
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &gl520_driver;
+               return -ENODEV;
 
        /* Determine the chip type. */
        if (kind < 0) {
@@ -628,24 +696,36 @@ static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
                    ((gl520_read_value(client, GL520_REG_REVISION) & 0x7f) != 0x00) ||
                    ((gl520_read_value(client, GL520_REG_CONF) & 0x80) != 0x00)) {
                        dev_dbg(&client->dev, "Unknown chip type, skipping\n");
-                       goto exit_free;
+                       return -ENODEV;
                }
        }
 
-       /* Fill in the remaining client fields */
-       strlcpy(client->name, "gl520sm", I2C_NAME_SIZE);
-       mutex_init(&data->update_lock);
+       strlcpy(info->type, "gl520sm", I2C_NAME_SIZE);
 
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(client)))
-               goto exit_free;
+       return 0;
+}
+
+static int gl520_probe(struct i2c_client *client,
+                      const struct i2c_device_id *id)
+{
+       struct gl520_data *data;
+       int err;
+
+       data = kzalloc(sizeof(struct gl520_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
+       }
+
+       i2c_set_clientdata(client, data);
+       mutex_init(&data->update_lock);
 
        /* Initialize the GL520SM chip */
        gl520_init_client(client);
 
        /* Register sysfs hooks */
        if ((err = sysfs_create_group(&client->dev.kobj, &gl520_group)))
-               goto exit_detach;
+               goto exit_free;
 
        if (data->two_temps) {
                if ((err = device_create_file(&client->dev,
@@ -653,7 +733,11 @@ static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&client->dev,
                                &sensor_dev_attr_temp2_max.dev_attr))
                 || (err = device_create_file(&client->dev,
-                               &sensor_dev_attr_temp2_max_hyst.dev_attr)))
+                               &sensor_dev_attr_temp2_max_hyst.dev_attr))
+                || (err = device_create_file(&client->dev,
+                               &sensor_dev_attr_temp2_alarm.dev_attr))
+                || (err = device_create_file(&client->dev,
+                               &sensor_dev_attr_temp2_beep.dev_attr)))
                        goto exit_remove_files;
        } else {
                if ((err = device_create_file(&client->dev,
@@ -661,7 +745,11 @@ static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
                 || (err = device_create_file(&client->dev,
                                &sensor_dev_attr_in4_min.dev_attr))
                 || (err = device_create_file(&client->dev,
-                               &sensor_dev_attr_in4_max.dev_attr)))
+                               &sensor_dev_attr_in4_max.dev_attr))
+                || (err = device_create_file(&client->dev,
+                               &sensor_dev_attr_in4_alarm.dev_attr))
+                || (err = device_create_file(&client->dev,
+                               &sensor_dev_attr_in4_beep.dev_attr)))
                        goto exit_remove_files;
        }
 
@@ -677,8 +765,6 @@ static int gl520_detect(struct i2c_adapter *adapter, int address, int kind)
 exit_remove_files:
        sysfs_remove_group(&client->dev.kobj, &gl520_group);
        sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
-exit_detach:
-       i2c_detach_client(client);
 exit_free:
        kfree(data);
 exit:
@@ -724,18 +810,14 @@ static void gl520_init_client(struct i2c_client *client)
        gl520_write_value(client, GL520_REG_BEEP_MASK, data->beep_mask);
 }
 
-static int gl520_detach_client(struct i2c_client *client)
+static int gl520_remove(struct i2c_client *client)
 {
        struct gl520_data *data = i2c_get_clientdata(client);
-       int err;
 
        hwmon_device_unregister(data->hwmon_dev);
        sysfs_remove_group(&client->dev.kobj, &gl520_group);
        sysfs_remove_group(&client->dev.kobj, &gl520_group_opt);
 
-       if ((err = i2c_detach_client(client)))
-               return err;
-
        kfree(data);
        return 0;
 }