hwmon: Add support for Winbond W83L786NG/NR
[sfrench/cifs-2.6.git] / drivers / hwmon / lm75.c
index 74ca2c8c61c3ae4996a46bcf523dba87b26f2561..37a8cc032ffa9a0b535ac26b9d07d37edb78bd3e 100644 (file)
@@ -24,7 +24,9 @@
 #include <linux/jiffies.h>
 #include <linux/i2c.h>
 #include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 #include <linux/err.h>
+#include <linux/mutex.h>
 #include "lm75.h"
 
 
@@ -38,21 +40,24 @@ I2C_CLIENT_INSMOD_1(lm75);
 /* Many LM75 constants specified below */
 
 /* The LM75 registers */
-#define LM75_REG_TEMP          0x00
 #define LM75_REG_CONF          0x01
-#define LM75_REG_TEMP_HYST     0x02
-#define LM75_REG_TEMP_OS       0x03
+static const u8 LM75_REG_TEMP[3] = {
+       0x00,           /* input */
+       0x03,           /* max */
+       0x02,           /* hyst */
+};
 
 /* Each client has this additional data */
 struct lm75_data {
        struct i2c_client       client;
-       struct class_device *class_dev;
-       struct semaphore        update_lock;
+       struct device *hwmon_dev;
+       struct mutex            update_lock;
        char                    valid;          /* !=0 if following fields are valid */
        unsigned long           last_updated;   /* In jiffies */
-       u16                     temp_input;     /* Register values */
-       u16                     temp_max;
-       u16                     temp_hyst;
+       u16                     temp[3];        /* Register values,
+                                                  0 = input
+                                                  1 = max
+                                                  2 = hyst */
 };
 
 static int lm75_attach_adapter(struct i2c_adapter *adapter);
@@ -74,35 +79,36 @@ static struct i2c_driver lm75_driver = {
        .detach_client  = lm75_detach_client,
 };
 
-#define show(value)    \
-static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)              \
-{                                                                      \
-       struct lm75_data *data = lm75_update_device(dev);               \
-       return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value));   \
+static ssize_t show_temp(struct device *dev, struct device_attribute *da,
+                        char *buf)
+{
+       struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
+       struct lm75_data *data = lm75_update_device(dev);
+       return sprintf(buf, "%d\n",
+                      LM75_TEMP_FROM_REG(data->temp[attr->index]));
 }
-show(temp_max);
-show(temp_hyst);
-show(temp_input);
-
-#define set(value, reg)        \
-static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)   \
-{                                                              \
-       struct i2c_client *client = to_i2c_client(dev);         \
-       struct lm75_data *data = i2c_get_clientdata(client);    \
-       int temp = simple_strtoul(buf, NULL, 10);               \
-                                                               \
-       down(&data->update_lock);                               \
-       data->value = LM75_TEMP_TO_REG(temp);                   \
-       lm75_write_value(client, reg, data->value);             \
-       up(&data->update_lock);                                 \
-       return count;                                           \
+
+static ssize_t set_temp(struct device *dev, struct device_attribute *da,
+                       const char *buf, size_t count)
+{
+       struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
+       struct i2c_client *client = to_i2c_client(dev);
+       struct lm75_data *data = i2c_get_clientdata(client);
+       int nr = attr->index;
+       long temp = simple_strtol(buf, NULL, 10);
+
+       mutex_lock(&data->update_lock);
+       data->temp[nr] = LM75_TEMP_TO_REG(temp);
+       lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
+       mutex_unlock(&data->update_lock);
+       return count;
 }
-set(temp_max, LM75_REG_TEMP_OS);
-set(temp_hyst, LM75_REG_TEMP_HYST);
 
-static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
-static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
-static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
+static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
+                       show_temp, set_temp, 1);
+static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
+                       show_temp, set_temp, 2);
+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
 
 static int lm75_attach_adapter(struct i2c_adapter *adapter)
 {
@@ -111,6 +117,18 @@ static int lm75_attach_adapter(struct i2c_adapter *adapter)
        return i2c_probe(adapter, &addr_data, lm75_detect);
 }
 
+static struct attribute *lm75_attributes[] = {
+       &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,
+
+       NULL
+};
+
+static const struct attribute_group lm75_group = {
+       .attrs = lm75_attributes,
+};
+
 /* This function is called by i2c_probe */
 static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
 {
@@ -188,7 +206,7 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
        /* Fill in the remaining client fields and put it into the global list */
        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)))
@@ -198,18 +216,19 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
        lm75_init_client(new_client);
        
        /* Register sysfs hooks */
-       data->class_dev = hwmon_device_register(&new_client->dev);
-       if (IS_ERR(data->class_dev)) {
-               err = PTR_ERR(data->class_dev);
+       if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
                goto exit_detach;
-       }
 
-       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_temp1_input);
+       data->hwmon_dev = hwmon_device_register(&new_client->dev);
+       if (IS_ERR(data->hwmon_dev)) {
+               err = PTR_ERR(data->hwmon_dev);
+               goto exit_remove;
+       }
 
        return 0;
 
+exit_remove:
+       sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
 exit_detach:
        i2c_detach_client(new_client);
 exit_free:
@@ -221,7 +240,8 @@ exit:
 static int lm75_detach_client(struct i2c_client *client)
 {
        struct lm75_data *data = i2c_get_clientdata(client);
-       hwmon_device_unregister(data->class_dev);
+       hwmon_device_unregister(data->hwmon_dev);
+       sysfs_remove_group(&client->dev.kobj, &lm75_group);
        i2c_detach_client(client);
        kfree(data);
        return 0;
@@ -264,20 +284,21 @@ static struct lm75_data *lm75_update_device(struct device *dev)
        struct i2c_client *client = to_i2c_client(dev);
        struct lm75_data *data = i2c_get_clientdata(client);
 
-       down(&data->update_lock);
+       mutex_lock(&data->update_lock);
 
        if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
            || !data->valid) {
+               int i;
                dev_dbg(&client->dev, "Starting lm75 update\n");
 
-               data->temp_input = lm75_read_value(client, LM75_REG_TEMP);
-               data->temp_max = lm75_read_value(client, LM75_REG_TEMP_OS);
-               data->temp_hyst = lm75_read_value(client, LM75_REG_TEMP_HYST);
+               for (i = 0; i < ARRAY_SIZE(data->temp); i++)
+                       data->temp[i] = lm75_read_value(client,
+                                                       LM75_REG_TEMP[i]);
                data->last_updated = jiffies;
                data->valid = 1;
        }
 
-       up(&data->update_lock);
+       mutex_unlock(&data->update_lock);
 
        return data;
 }