string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / drivers / hwmon / ds1621.c
index b0199e0..e113634 100644 (file)
 #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 <linux/sysfs.h>
 #include "lm75.h"
 
 /* Addresses to scan */
-static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
+static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
                                        0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
 
 /* Insmod parameters */
-I2C_CLIENT_INSMOD_1(ds1621);
 static int polarity = -1;
 module_param(polarity, int, 0);
 MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low");
@@ -50,9 +52,11 @@ MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low")
 #define DS1621_REG_CONFIG_DONE         0x80
 
 /* The DS1621 registers */
-#define DS1621_REG_TEMP                        0xAA /* word, RO */
-#define DS1621_REG_TEMP_MIN            0xA1 /* word, RW */
-#define DS1621_REG_TEMP_MAX            0xA2 /* word, RW */
+static const u8 DS1621_REG_TEMP[3] = {
+       0xAA,           /* input, word, RO */
+       0xA2,           /* min, word, RW */
+       0xA1,           /* max, word, RW */
+};
 #define DS1621_REG_CONF                        0xAC /* byte, RW */
 #define DS1621_COM_START               0xEE /* no data */
 #define DS1621_COM_STOP                        0x22 /* no data */
@@ -61,275 +65,263 @@ MODULE_PARM_DESC(polarity, "Output's polarity: 0 = active high, 1 = active low")
 #define DS1621_ALARM_TEMP_HIGH         0x40
 #define DS1621_ALARM_TEMP_LOW          0x20
 
-/* Conversions. Rounding and limit checking is only done on the TO_REG
-   variants. Note that you should be a bit careful with which arguments
-   these macros are called: arguments may be evaluated more than once.
-   Fixing this is just not worth it. */
+/* Conversions */
 #define ALARMS_FROM_REG(val) ((val) & \
                               (DS1621_ALARM_TEMP_HIGH | DS1621_ALARM_TEMP_LOW))
 
 /* Each client has this additional data */
 struct ds1621_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, temp_min, temp_max;   /* Register values, word */
+       u16 temp[3];                    /* Register values, word */
        u8 conf;                        /* Register encoding, combined */
 };
 
-static int ds1621_attach_adapter(struct i2c_adapter *adapter);
-static int ds1621_detect(struct i2c_adapter *adapter, int address,
-                        int kind);
-static void ds1621_init_client(struct i2c_client *client);
-static int ds1621_detach_client(struct i2c_client *client);
-static struct ds1621_data *ds1621_update_client(struct device *dev);
-
-/* This is the driver that will be inserted */
-static struct i2c_driver ds1621_driver = {
-       .owner          = THIS_MODULE,
-       .name           = "ds1621",
-       .id             = I2C_DRIVERID_DS1621,
-       .flags          = I2C_DF_NOTIFY,
-       .attach_adapter = ds1621_attach_adapter,
-       .detach_client  = ds1621_detach_client,
-};
-
-/* All registers are word-sized, except for the configuration register.
+/* Temperature registers are word-sized.
    DS1621 uses a high-byte first convention, which is exactly opposite to
-   the usual practice. */
-static int ds1621_read_value(struct i2c_client *client, u8 reg)
+   the SMBus standard. */
+static int ds1621_read_temp(struct i2c_client *client, u8 reg)
 {
-       if (reg == DS1621_REG_CONF)
-               return i2c_smbus_read_byte_data(client, reg);
-       else
-               return swab16(i2c_smbus_read_word_data(client, reg));
+       int ret;
+
+       ret = i2c_smbus_read_word_data(client, reg);
+       if (ret < 0)
+               return ret;
+       return swab16(ret);
 }
 
-/* All registers are word-sized, except for the configuration register.
-   DS1621 uses a high-byte first convention, which is exactly opposite to
-   the usual practice. */
-static int ds1621_write_value(struct i2c_client *client, u8 reg, u16 value)
+static int ds1621_write_temp(struct i2c_client *client, u8 reg, u16 value)
 {
-       if (reg == DS1621_REG_CONF)
-               return i2c_smbus_write_byte_data(client, reg, value);
-       else
-               return i2c_smbus_write_word_data(client, reg, swab16(value));
+       return i2c_smbus_write_word_data(client, reg, swab16(value));
 }
 
 static void ds1621_init_client(struct i2c_client *client)
 {
-       int reg = ds1621_read_value(client, DS1621_REG_CONF);
+       u8 conf, new_conf;
+
+       new_conf = conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
        /* switch to continuous conversion mode */
-       reg &= ~ DS1621_REG_CONFIG_1SHOT;
+       new_conf &= ~DS1621_REG_CONFIG_1SHOT;
 
        /* setup output polarity */
        if (polarity == 0)
-               reg &= ~DS1621_REG_CONFIG_POLARITY;
+               new_conf &= ~DS1621_REG_CONFIG_POLARITY;
        else if (polarity == 1)
-               reg |= DS1621_REG_CONFIG_POLARITY;
+               new_conf |= DS1621_REG_CONFIG_POLARITY;
        
-       ds1621_write_value(client, DS1621_REG_CONF, reg);
+       if (conf != new_conf)
+               i2c_smbus_write_byte_data(client, DS1621_REG_CONF, new_conf);
        
        /* start conversion */
        i2c_smbus_write_byte(client, DS1621_COM_START);
 }
 
-#define show(value)                                                    \
-static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf)              \
-{                                                                      \
-       struct ds1621_data *data = ds1621_update_client(dev);           \
-       return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->value));   \
+static struct ds1621_data *ds1621_update_client(struct device *dev)
+{
+       struct i2c_client *client = to_i2c_client(dev);
+       struct ds1621_data *data = i2c_get_clientdata(client);
+       u8 new_conf;
+
+       mutex_lock(&data->update_lock);
+
+       if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
+           || !data->valid) {
+               int i;
+
+               dev_dbg(&client->dev, "Starting ds1621 update\n");
+
+               data->conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
+
+               for (i = 0; i < ARRAY_SIZE(data->temp); i++)
+                       data->temp[i] = ds1621_read_temp(client,
+                                                        DS1621_REG_TEMP[i]);
+
+               /* reset alarms if necessary */
+               new_conf = data->conf;
+               if (data->temp[0] > data->temp[1])      /* input > min */
+                       new_conf &= ~DS1621_ALARM_TEMP_LOW;
+               if (data->temp[0] < data->temp[2])      /* input < max */
+                       new_conf &= ~DS1621_ALARM_TEMP_HIGH;
+               if (data->conf != new_conf)
+                       i2c_smbus_write_byte_data(client, DS1621_REG_CONF,
+                                                 new_conf);
+
+               data->last_updated = jiffies;
+               data->valid = 1;
+       }
+
+       mutex_unlock(&data->update_lock);
+
+       return data;
 }
 
-show(temp);
-show(temp_min);
-show(temp_max);
-
-#define set_temp(suffix, value, reg)                                   \
-static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf,   \
-                                size_t count)                          \
-{                                                                      \
-       struct i2c_client *client = to_i2c_client(dev);                 \
-       struct ds1621_data *data = ds1621_update_client(dev);           \
-       u16 val = LM75_TEMP_TO_REG(simple_strtoul(buf, NULL, 10));      \
-                                                                       \
-       down(&data->update_lock);                                       \
-       data->value = val;                                              \
-       ds1621_write_value(client, reg, data->value);                   \
-       up(&data->update_lock);                                         \
-       return count;                                                   \
+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 ds1621_data *data = ds1621_update_client(dev);
+       return sprintf(buf, "%d\n",
+                      LM75_TEMP_FROM_REG(data->temp[attr->index]));
 }
 
-set_temp(min, temp_min, DS1621_REG_TEMP_MIN);
-set_temp(max, temp_max, DS1621_REG_TEMP_MAX);
+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 ds1621_data *data = i2c_get_clientdata(client);
+       u16 val = LM75_TEMP_TO_REG(simple_strtol(buf, NULL, 10));
+
+       mutex_lock(&data->update_lock);
+       data->temp[attr->index] = val;
+       ds1621_write_temp(client, DS1621_REG_TEMP[attr->index],
+                         data->temp[attr->index]);
+       mutex_unlock(&data->update_lock);
+       return count;
+}
 
-static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
+static ssize_t show_alarms(struct device *dev, struct device_attribute *da,
+                          char *buf)
 {
        struct ds1621_data *data = ds1621_update_client(dev);
        return sprintf(buf, "%d\n", ALARMS_FROM_REG(data->conf));
 }
 
+static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
+                         char *buf)
+{
+       struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
+       struct ds1621_data *data = ds1621_update_client(dev);
+       return sprintf(buf, "%d\n", !!(data->conf & attr->index));
+}
+
 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
-static DEVICE_ATTR(temp1_input, S_IRUGO , show_temp, NULL);
-static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO , show_temp_min, set_temp_min);
-static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp, set_temp, 1);
+static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp, set_temp, 2);
+static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
+               DS1621_ALARM_TEMP_LOW);
+static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
+               DS1621_ALARM_TEMP_HIGH);
+
+static struct attribute *ds1621_attributes[] = {
+       &sensor_dev_attr_temp1_input.dev_attr.attr,
+       &sensor_dev_attr_temp1_min.dev_attr.attr,
+       &sensor_dev_attr_temp1_max.dev_attr.attr,
+       &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
+       &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
+       &dev_attr_alarms.attr,
+       NULL
+};
 
+static const struct attribute_group ds1621_group = {
+       .attrs = ds1621_attributes,
+};
 
-static int ds1621_attach_adapter(struct i2c_adapter *adapter)
-{
-       return i2c_probe(adapter, &addr_data, ds1621_detect);
-}
 
-/* This function is called by i2c_probe */
-int ds1621_detect(struct i2c_adapter *adapter, int address,
-                  int kind)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int ds1621_detect(struct i2c_client *client,
+                        struct i2c_board_info *info)
 {
+       struct i2c_adapter *adapter = client->adapter;
        int conf, temp;
-       struct i2c_client *new_client;
-       struct ds1621_data *data;
-       int err = 0;
+       int i;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA 
                                     | I2C_FUNC_SMBUS_WORD_DATA 
                                     | I2C_FUNC_SMBUS_WRITE_BYTE))
-               goto exit;
+               return -ENODEV;
 
-       /* 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 ds1621_{read,write}_value. */
-       if (!(data = kmalloc(sizeof(struct ds1621_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto exit;
+       /* Now, we do the remaining detection. It is lousy. */
+       /* The NVB bit should be low if no EEPROM write has been  requested
+          during the latest 10ms, which is highly improbable in our case. */
+       conf = i2c_smbus_read_byte_data(client, DS1621_REG_CONF);
+       if (conf < 0 || conf & DS1621_REG_CONFIG_NVB)
+               return -ENODEV;
+       /* The 7 lowest bits of a temperature should always be 0. */
+       for (i = 0; i < ARRAY_SIZE(DS1621_REG_TEMP); i++) {
+               temp = i2c_smbus_read_word_data(client, DS1621_REG_TEMP[i]);
+               if (temp < 0 || (temp & 0x7f00))
+                       return -ENODEV;
        }
-       memset(data, 0, sizeof(struct ds1621_data));
-       
-       new_client = &data->client;
-       i2c_set_clientdata(new_client, data);
-       new_client->addr = address;
-       new_client->adapter = adapter;
-       new_client->driver = &ds1621_driver;
-       new_client->flags = 0;
 
+       strlcpy(info->type, "ds1621", I2C_NAME_SIZE);
 
-       /* Now, we do the remaining detection. It is lousy. */
-       if (kind < 0) {
-               /* The NVB bit should be low if no EEPROM write has been 
-                  requested during the latest 10ms, which is highly 
-                  improbable in our case. */
-               conf = ds1621_read_value(new_client, DS1621_REG_CONF);
-               if (conf & DS1621_REG_CONFIG_NVB)
-                       goto exit_free;
-               /* The 7 lowest bits of a temperature should always be 0. */
-               temp = ds1621_read_value(new_client, DS1621_REG_TEMP);
-               if (temp & 0x007f)
-                       goto exit_free;
-               temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MIN);
-               if (temp & 0x007f)
-                       goto exit_free;
-               temp = ds1621_read_value(new_client, DS1621_REG_TEMP_MAX);
-               if (temp & 0x007f)
-                       goto exit_free;
-       }
+       return 0;
+}
 
-       /* Determine the chip type - only one kind supported! */
-       if (kind <= 0)
-               kind = ds1621;
+static int ds1621_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
+{
+       struct ds1621_data *data;
+       int err;
 
-       /* Fill in remaining client fields and put it into the global list */
-       strlcpy(new_client->name, "ds1621", I2C_NAME_SIZE);
-       data->valid = 0;
-       init_MUTEX(&data->update_lock);
+       data = kzalloc(sizeof(struct ds1621_data), GFP_KERNEL);
+       if (!data) {
+               err = -ENOMEM;
+               goto exit;
+       }
 
-       /* Tell the I2C layer a new client has arrived */
-       if ((err = i2c_attach_client(new_client)))
-               goto exit_free;
+       i2c_set_clientdata(client, data);
+       mutex_init(&data->update_lock);
 
        /* Initialize the DS1621 chip */
-       ds1621_init_client(new_client);
+       ds1621_init_client(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);
-               goto exit_detach;
+       if ((err = sysfs_create_group(&client->dev.kobj, &ds1621_group)))
+               goto exit_free;
+
+       data->hwmon_dev = hwmon_device_register(&client->dev);
+       if (IS_ERR(data->hwmon_dev)) {
+               err = PTR_ERR(data->hwmon_dev);
+               goto exit_remove_files;
        }
 
-       device_create_file(&new_client->dev, &dev_attr_alarms);
-       device_create_file(&new_client->dev, &dev_attr_temp1_input);
-       device_create_file(&new_client->dev, &dev_attr_temp1_min);
-       device_create_file(&new_client->dev, &dev_attr_temp1_max);
-       
        return 0;
 
-/* OK, this is not exactly good programming practice, usually. But it is
-   very code-efficient in this case. */
-      exit_detach:
-       i2c_detach_client(new_client);
+      exit_remove_files:
+       sysfs_remove_group(&client->dev.kobj, &ds1621_group);
       exit_free:
        kfree(data);
       exit:
        return err;
 }
 
-static int ds1621_detach_client(struct i2c_client *client)
+static int ds1621_remove(struct i2c_client *client)
 {
        struct ds1621_data *data = i2c_get_clientdata(client);
-       int err;
-
-       hwmon_device_unregister(data->class_dev);
 
-       if ((err = i2c_detach_client(client)))
-               return err;
+       hwmon_device_unregister(data->hwmon_dev);
+       sysfs_remove_group(&client->dev.kobj, &ds1621_group);
 
        kfree(data);
 
        return 0;
 }
 
+static const struct i2c_device_id ds1621_id[] = {
+       { "ds1621", 0 },
+       { "ds1625", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, ds1621_id);
 
-static struct ds1621_data *ds1621_update_client(struct device *dev)
-{
-       struct i2c_client *client = to_i2c_client(dev);
-       struct ds1621_data *data = i2c_get_clientdata(client);
-       u8 new_conf;
-
-       down(&data->update_lock);
-
-       if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
-           || !data->valid) {
-
-               dev_dbg(&client->dev, "Starting ds1621 update\n");
-
-               data->conf = ds1621_read_value(client, DS1621_REG_CONF);
-
-               data->temp = ds1621_read_value(client, DS1621_REG_TEMP);
-               
-               data->temp_min = ds1621_read_value(client,
-                                                   DS1621_REG_TEMP_MIN);
-               data->temp_max = ds1621_read_value(client,
-                                                   DS1621_REG_TEMP_MAX);
-
-               /* reset alarms if necessary */
-               new_conf = data->conf;
-               if (data->temp < data->temp_min)
-                       new_conf &= ~DS1621_ALARM_TEMP_LOW;
-               if (data->temp > data->temp_max)
-                       new_conf &= ~DS1621_ALARM_TEMP_HIGH;
-               if (data->conf != new_conf)
-                       ds1621_write_value(client, DS1621_REG_CONF,
-                                          new_conf);
-
-               data->last_updated = jiffies;
-               data->valid = 1;
-       }
-
-       up(&data->update_lock);
-
-       return data;
-}
+/* This is the driver that will be inserted */
+static struct i2c_driver ds1621_driver = {
+       .class          = I2C_CLASS_HWMON,
+       .driver = {
+               .name   = "ds1621",
+       },
+       .probe          = ds1621_probe,
+       .remove         = ds1621_remove,
+       .id_table       = ds1621_id,
+       .detect         = ds1621_detect,
+       .address_list   = normal_i2c,
+};
 
 static int __init ds1621_init(void)
 {