fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / hwmon / lm78.c
index e404001..73bc2ff 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/hwmon.h>
 #include <linux/hwmon-vid.h>
 #include <linux/err.h>
+#include <linux/mutex.h>
 #include <asm/io.h>
 
 /* Addresses to scan */
@@ -131,10 +132,10 @@ static inline int TEMP_FROM_REG(s8 val)
 struct lm78_data {
        struct i2c_client client;
        struct class_device *class_dev;
-       struct semaphore lock;
+       struct mutex lock;
        enum chips type;
 
-       struct semaphore update_lock;
+       struct mutex update_lock;
        char valid;             /* !=0 if following fields are valid */
        unsigned long last_updated;     /* In jiffies */
 
@@ -157,8 +158,8 @@ static int lm78_isa_attach_adapter(struct i2c_adapter *adapter);
 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
 static int lm78_detach_client(struct i2c_client *client);
 
-static int lm78_read_value(struct i2c_client *client, u8 register);
-static int lm78_write_value(struct i2c_client *client, u8 register, u8 value);
+static int lm78_read_value(struct i2c_client *client, u8 reg);
+static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value);
 static struct lm78_data *lm78_update_device(struct device *dev);
 static void lm78_init_client(struct i2c_client *client);
 
@@ -174,6 +175,7 @@ static struct i2c_driver lm78_driver = {
 
 static struct i2c_driver lm78_isa_driver = {
        .driver = {
+               .owner  = THIS_MODULE,
                .name   = "lm78-isa",
        },
        .attach_adapter = lm78_isa_attach_adapter,
@@ -207,10 +209,10 @@ static ssize_t set_in_min(struct device *dev, const char *buf,
        struct lm78_data *data = i2c_get_clientdata(client);
        unsigned long val = simple_strtoul(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->in_min[nr] = IN_TO_REG(val);
        lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
 
@@ -221,10 +223,10 @@ static ssize_t set_in_max(struct device *dev, const char *buf,
        struct lm78_data *data = i2c_get_clientdata(client);
        unsigned long val = simple_strtoul(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->in_max[nr] = IN_TO_REG(val);
        lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
        
@@ -288,10 +290,10 @@ static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr,
        struct lm78_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->temp_over = TEMP_TO_REG(val);
        lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
 
@@ -307,10 +309,10 @@ static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr,
        struct lm78_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->temp_hyst = TEMP_TO_REG(val);
        lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
 
@@ -342,10 +344,10 @@ static ssize_t set_fan_min(struct device *dev, const char *buf,
        struct lm78_data *data = i2c_get_clientdata(client);
        unsigned long val = simple_strtoul(buf, NULL, 10);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
        lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
        return count;
 }
 
@@ -357,7 +359,7 @@ static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
 
 /* Note: we save and restore the fan minimum here, because its value is
    determined in part by the fan divisor.  This follows the principle of
-   least suprise; the user doesn't expect the fan minimum to change just
+   least surprise; the user doesn't expect the fan minimum to change just
    because the divisor changed. */
 static ssize_t set_fan_div(struct device *dev, const char *buf,
        size_t count, int nr)
@@ -368,7 +370,7 @@ static ssize_t set_fan_div(struct device *dev, const char *buf,
        unsigned long min;
        u8 reg;
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
        min = FAN_FROM_REG(data->fan_min[nr],
                           DIV_FROM_REG(data->fan_div[nr]));
 
@@ -380,7 +382,7 @@ static ssize_t set_fan_div(struct device *dev, const char *buf,
        default:
                dev_err(&client->dev, "fan_div value %ld not "
                        "supported. Choose one of 1, 2, 4 or 8!\n", val);
-               up(&data->update_lock);
+               mutex_unlock(&data->update_lock);
                return -EINVAL;
        }
 
@@ -398,7 +400,7 @@ static ssize_t set_fan_div(struct device *dev, const char *buf,
        data->fan_min[nr] =
                FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
        lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 
        return count;
 }
@@ -480,6 +482,50 @@ static int lm78_isa_attach_adapter(struct i2c_adapter *adapter)
        return lm78_detect(adapter, isa_address, -1);
 }
 
+static struct attribute *lm78_attributes[] = {
+       &dev_attr_in0_input.attr,
+       &dev_attr_in0_min.attr,
+       &dev_attr_in0_max.attr,
+       &dev_attr_in1_input.attr,
+       &dev_attr_in1_min.attr,
+       &dev_attr_in1_max.attr,
+       &dev_attr_in2_input.attr,
+       &dev_attr_in2_min.attr,
+       &dev_attr_in2_max.attr,
+       &dev_attr_in3_input.attr,
+       &dev_attr_in3_min.attr,
+       &dev_attr_in3_max.attr,
+       &dev_attr_in4_input.attr,
+       &dev_attr_in4_min.attr,
+       &dev_attr_in4_max.attr,
+       &dev_attr_in5_input.attr,
+       &dev_attr_in5_min.attr,
+       &dev_attr_in5_max.attr,
+       &dev_attr_in6_input.attr,
+       &dev_attr_in6_min.attr,
+       &dev_attr_in6_max.attr,
+       &dev_attr_temp1_input.attr,
+       &dev_attr_temp1_max.attr,
+       &dev_attr_temp1_max_hyst.attr,
+       &dev_attr_fan1_input.attr,
+       &dev_attr_fan1_min.attr,
+       &dev_attr_fan1_div.attr,
+       &dev_attr_fan2_input.attr,
+       &dev_attr_fan2_min.attr,
+       &dev_attr_fan2_div.attr,
+       &dev_attr_fan3_input.attr,
+       &dev_attr_fan3_min.attr,
+       &dev_attr_fan3_div.attr,
+       &dev_attr_alarms.attr,
+       &dev_attr_cpu0_vid.attr,
+
+       NULL
+};
+
+static const struct attribute_group lm78_group = {
+       .attrs = lm78_attributes,
+};
+
 /* This function is called by i2c_probe */
 static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
 {
@@ -548,7 +594,7 @@ static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
 
        new_client = &data->client;
        if (is_isa)
-               init_MUTEX(&data->lock);
+               mutex_init(&data->lock);
        i2c_set_clientdata(new_client, data);
        new_client->addr = address;
        new_client->adapter = adapter;
@@ -598,7 +644,7 @@ static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
        data->type = kind;
 
        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)))
@@ -614,50 +660,19 @@ static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
        }
 
        /* Register sysfs hooks */
+       if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
+               goto ERROR3;
+
        data->class_dev = hwmon_device_register(&new_client->dev);
        if (IS_ERR(data->class_dev)) {
                err = PTR_ERR(data->class_dev);
-               goto ERROR3;
+               goto ERROR4;
        }
 
-       device_create_file(&new_client->dev, &dev_attr_in0_input);
-       device_create_file(&new_client->dev, &dev_attr_in0_min);
-       device_create_file(&new_client->dev, &dev_attr_in0_max);
-       device_create_file(&new_client->dev, &dev_attr_in1_input);
-       device_create_file(&new_client->dev, &dev_attr_in1_min);
-       device_create_file(&new_client->dev, &dev_attr_in1_max);
-       device_create_file(&new_client->dev, &dev_attr_in2_input);
-       device_create_file(&new_client->dev, &dev_attr_in2_min);
-       device_create_file(&new_client->dev, &dev_attr_in2_max);
-       device_create_file(&new_client->dev, &dev_attr_in3_input);
-       device_create_file(&new_client->dev, &dev_attr_in3_min);
-       device_create_file(&new_client->dev, &dev_attr_in3_max);
-       device_create_file(&new_client->dev, &dev_attr_in4_input);
-       device_create_file(&new_client->dev, &dev_attr_in4_min);
-       device_create_file(&new_client->dev, &dev_attr_in4_max);
-       device_create_file(&new_client->dev, &dev_attr_in5_input);
-       device_create_file(&new_client->dev, &dev_attr_in5_min);
-       device_create_file(&new_client->dev, &dev_attr_in5_max);
-       device_create_file(&new_client->dev, &dev_attr_in6_input);
-       device_create_file(&new_client->dev, &dev_attr_in6_min);
-       device_create_file(&new_client->dev, &dev_attr_in6_max);
-       device_create_file(&new_client->dev, &dev_attr_temp1_input);
-       device_create_file(&new_client->dev, &dev_attr_temp1_max);
-       device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
-       device_create_file(&new_client->dev, &dev_attr_fan1_input);
-       device_create_file(&new_client->dev, &dev_attr_fan1_min);
-       device_create_file(&new_client->dev, &dev_attr_fan1_div);
-       device_create_file(&new_client->dev, &dev_attr_fan2_input);
-       device_create_file(&new_client->dev, &dev_attr_fan2_min);
-       device_create_file(&new_client->dev, &dev_attr_fan2_div);
-       device_create_file(&new_client->dev, &dev_attr_fan3_input);
-       device_create_file(&new_client->dev, &dev_attr_fan3_min);
-       device_create_file(&new_client->dev, &dev_attr_fan3_div);
-       device_create_file(&new_client->dev, &dev_attr_alarms);
-       device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
-
        return 0;
 
+ERROR4:
+       sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
 ERROR3:
        i2c_detach_client(new_client);
 ERROR2:
@@ -675,6 +690,7 @@ static int lm78_detach_client(struct i2c_client *client)
        int err;
 
        hwmon_device_unregister(data->class_dev);
+       sysfs_remove_group(&client->dev.kobj, &lm78_group);
 
        if ((err = i2c_detach_client(client)))
                return err;
@@ -697,10 +713,10 @@ static int lm78_read_value(struct i2c_client *client, u8 reg)
        int res;
        if (i2c_is_isa_client(client)) {
                struct lm78_data *data = i2c_get_clientdata(client);
-               down(&data->lock);
+               mutex_lock(&data->lock);
                outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
                res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
-               up(&data->lock);
+               mutex_unlock(&data->lock);
                return res;
        } else
                return i2c_smbus_read_byte_data(client, reg);
@@ -717,10 +733,10 @@ static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
 {
        if (i2c_is_isa_client(client)) {
                struct lm78_data *data = i2c_get_clientdata(client);
-               down(&data->lock);
+               mutex_lock(&data->lock);
                outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
                outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
-               up(&data->lock);
+               mutex_unlock(&data->lock);
                return 0;
        } else
                return i2c_smbus_write_byte_data(client, reg, value);
@@ -742,7 +758,7 @@ static struct lm78_data *lm78_update_device(struct device *dev)
        struct lm78_data *data = i2c_get_clientdata(client);
        int i;
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
 
        if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
            || !data->valid) {
@@ -786,7 +802,7 @@ static struct lm78_data *lm78_update_device(struct device *dev)
                data->fan_div[2] = 1;
        }
 
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 
        return data;
 }
@@ -799,18 +815,18 @@ static int __init sm_lm78_init(void)
        if (res)
                return res;
 
-       res = i2c_isa_add_driver(&lm78_isa_driver);
-       if (res) {
-               i2c_del_driver(&lm78_driver);
-               return res;
-       }
+       /* Don't exit if this one fails, we still want the I2C variants
+          to work! */
+       if (i2c_isa_add_driver(&lm78_isa_driver))
+               isa_address = 0;
 
        return 0;
 }
 
 static void __exit sm_lm78_exit(void)
 {
-       i2c_isa_del_driver(&lm78_isa_driver);
+       if (isa_address)
+               i2c_isa_del_driver(&lm78_isa_driver);
        i2c_del_driver(&lm78_driver);
 }