const: struct nla_policy
[safe/jmp/linux-2.6] / drivers / hwmon / adt7473.c
index 0cd6c72..434576f 100644 (file)
@@ -32,9 +32,6 @@
 /* Addresses to scan */
 static const unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END };
 
-/* Insmod parameters */
-I2C_CLIENT_INSMOD_1(adt7473);
-
 /* ADT7473 registers */
 #define ADT7473_REG_BASE_ADDR                  0x20
 
@@ -130,7 +127,6 @@ I2C_CLIENT_INSMOD_1(adt7473);
 #define FAN_DATA_VALID(x)      ((x) && (x) != FAN_PERIOD_INVALID)
 
 struct adt7473_data {
-       struct i2c_client       client;
        struct device           *hwmon_dev;
        struct attribute_group  attrs;
        struct mutex            lock;
@@ -165,16 +161,27 @@ struct adt7473_data {
        u8                      max_duty_at_overheat;
 };
 
-static int adt7473_attach_adapter(struct i2c_adapter *adapter);
-static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind);
-static int adt7473_detach_client(struct i2c_client *client);
+static int adt7473_probe(struct i2c_client *client,
+                        const struct i2c_device_id *id);
+static int adt7473_detect(struct i2c_client *client,
+                         struct i2c_board_info *info);
+static int adt7473_remove(struct i2c_client *client);
+
+static const struct i2c_device_id adt7473_id[] = {
+       { "adt7473", 0 },
+       { }
+};
 
 static struct i2c_driver adt7473_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                .name   = "adt7473",
        },
-       .attach_adapter = adt7473_attach_adapter,
-       .detach_client  = adt7473_detach_client,
+       .probe          = adt7473_probe,
+       .remove         = adt7473_remove,
+       .id_table       = adt7473_id,
+       .detect         = adt7473_detect,
+       .address_list   = normal_i2c,
 };
 
 /*
@@ -308,35 +315,24 @@ out:
 }
 
 /*
- * On this chip, voltages are given as a count of steps between a minimum
- * and maximum voltage, not a direct voltage.
+ * Conversions
  */
-static const int volt_convert_table[][2] = {
-       {2997, 3},
-       {4395, 4},
+
+/* IN are scaled acording to built-in resistors */
+static const int adt7473_scaling[] = {  /* .001 Volts */
+       2250, 3300
 };
+#define SCALE(val, from, to)   (((val) * (to) + ((from) / 2)) / (from))
 
 static int decode_volt(int volt_index, u8 raw)
 {
-       int cmax = volt_convert_table[volt_index][0];
-       int cmin = volt_convert_table[volt_index][1];
-       return ((raw * (cmax - cmin)) / 255) + cmin;
+       return SCALE(raw, 192, adt7473_scaling[volt_index]);
 }
 
 static u8 encode_volt(int volt_index, int cooked)
 {
-       int cmax = volt_convert_table[volt_index][0];
-       int cmin = volt_convert_table[volt_index][1];
-       u8 x;
-
-       if (cooked > cmax)
-               cooked = cmax;
-       else if (cooked < cmin)
-               cooked = cmin;
-
-       x = ((cooked - cmin) * 255) / (cmax - cmin);
-
-       return x;
+       int raw = SCALE(cooked, adt7473_scaling[volt_index], 192);
+       return SENSORS_LIMIT(raw, 0, 255);
 }
 
 static ssize_t show_volt_min(struct device *dev,
@@ -357,7 +353,12 @@ static ssize_t set_volt_min(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));
+       long volt;
+
+       if (strict_strtol(buf, 10, &volt))
+               return -EINVAL;
+
+       volt = encode_volt(attr->index, volt);
 
        mutex_lock(&data->lock);
        data->volt_min[attr->index] = volt;
@@ -386,7 +387,12 @@ static ssize_t set_volt_max(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));
+       long volt;
+
+       if (strict_strtol(buf, 10, &volt))
+               return -EINVAL;
+
+       volt = encode_volt(attr->index, volt);
 
        mutex_lock(&data->lock);
        data->volt_max[attr->index] = volt;
@@ -419,7 +425,8 @@ static int decode_temp(u8 twos_complement, u8 raw)
 
 static u8 encode_temp(u8 twos_complement, int cooked)
 {
-       return twos_complement ? cooked & 0xFF : cooked + 64;
+       u8 ret = twos_complement ? cooked & 0xFF : cooked + 64;
+       return SENSORS_LIMIT(ret, 0, 255);
 }
 
 static ssize_t show_temp_min(struct device *dev,
@@ -441,7 +448,12 @@ static ssize_t set_temp_min(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10) / 1000;
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = DIV_ROUND_CLOSEST(temp, 1000);
        temp = encode_temp(data->temp_twos_complement, temp);
 
        mutex_lock(&data->lock);
@@ -472,7 +484,12 @@ static ssize_t set_temp_max(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10) / 1000;
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = DIV_ROUND_CLOSEST(temp, 1000);
        temp = encode_temp(data->temp_twos_complement, temp);
 
        mutex_lock(&data->lock);
@@ -515,11 +532,13 @@ static ssize_t set_fan_min(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
 
-       if (!temp)
+       if (strict_strtol(buf, 10, &temp) || !temp)
                return -EINVAL;
+
        temp = FAN_RPM_TO_PERIOD(temp);
+       temp = SENSORS_LIMIT(temp, 1, 65534);
 
        mutex_lock(&data->lock);
        data->fan_min[attr->index] = temp;
@@ -558,7 +577,10 @@ static ssize_t set_max_duty_at_crit(struct device *dev,
        u8 reg;
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
 
        mutex_lock(&data->lock);
        data->max_duty_at_overheat = !!temp;
@@ -587,7 +609,12 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = SENSORS_LIMIT(temp, 0, 255);
 
        mutex_lock(&data->lock);
        data->pwm[attr->index] = temp;
@@ -614,7 +641,12 @@ static ssize_t set_pwm_max(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = SENSORS_LIMIT(temp, 0, 255);
 
        mutex_lock(&data->lock);
        data->pwm_max[attr->index] = temp;
@@ -642,7 +674,12 @@ static ssize_t set_pwm_min(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = SENSORS_LIMIT(temp, 0, 255);
 
        mutex_lock(&data->lock);
        data->pwm_min[attr->index] = temp;
@@ -672,7 +709,12 @@ static ssize_t set_temp_tmax(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10) / 1000;
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = DIV_ROUND_CLOSEST(temp, 1000);
        temp = encode_temp(data->temp_twos_complement, temp);
 
        mutex_lock(&data->lock);
@@ -703,7 +745,12 @@ static ssize_t set_temp_tmin(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10) / 1000;
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
+
+       temp = DIV_ROUND_CLOSEST(temp, 1000);
        temp = encode_temp(data->temp_twos_complement, temp);
 
        mutex_lock(&data->lock);
@@ -741,7 +788,10 @@ static ssize_t set_pwm_enable(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
 
        switch (temp) {
        case 0:
@@ -805,7 +855,10 @@ static ssize_t set_pwm_auto_temp(struct device *dev,
        struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
        struct i2c_client *client = to_i2c_client(dev);
        struct adt7473_data *data = i2c_get_clientdata(client);
-       int temp = simple_strtol(buf, NULL, 10);
+       long temp;
+
+       if (strict_strtol(buf, 10, &temp))
+               return -EINVAL;
 
        switch (temp) {
        case 1:
@@ -1028,21 +1081,38 @@ static struct attribute *adt7473_attr[] =
        NULL
 };
 
-static int adt7473_attach_adapter(struct i2c_adapter *adapter)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int adt7473_detect(struct i2c_client *client,
+                         struct i2c_board_info *info)
 {
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, adt7473_detect);
+       struct i2c_adapter *adapter = client->adapter;
+       int vendor, device, revision;
+
+       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
+               return -ENODEV;
+
+       vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
+       if (vendor != ADT7473_VENDOR)
+               return -ENODEV;
+
+       device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
+       if (device != ADT7473_DEVICE)
+               return -ENODEV;
+
+       revision = i2c_smbus_read_byte_data(client, ADT7473_REG_REVISION);
+       if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69)
+               return -ENODEV;
+
+       strlcpy(info->type, "adt7473", I2C_NAME_SIZE);
+
+       return 0;
 }
 
-static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind)
+static int adt7473_probe(struct i2c_client *client,
+                        const struct i2c_device_id *id)
 {
-       struct i2c_client *client;
        struct adt7473_data *data;
-       int err = 0;
-
-       if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
-               goto exit;
+       int err;
 
        data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
        if (!data) {
@@ -1050,45 +1120,9 @@ static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind)
                goto exit;
        }
 
-       client = &data->client;
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &adt7473_driver;
-
        i2c_set_clientdata(client, data);
-
        mutex_init(&data->lock);
 
-       if (kind <= 0) {
-               int vendor, device, revision;
-
-               vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
-               if (vendor != ADT7473_VENDOR) {
-                       err = -ENODEV;
-                       goto exit_free;
-               }
-
-               device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
-               if (device != ADT7473_DEVICE) {
-                       err = -ENODEV;
-                       goto exit_free;
-               }
-
-               revision = i2c_smbus_read_byte_data(client,
-                                                   ADT7473_REG_REVISION);
-               if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69) {
-                       err = -ENODEV;
-                       goto exit_free;
-               }
-       } else
-               dev_dbg(&adapter->dev, "detection forced\n");
-
-       strlcpy(client->name, "adt7473", I2C_NAME_SIZE);
-
-       err = i2c_attach_client(client);
-       if (err)
-               goto exit_free;
-
        dev_info(&client->dev, "%s chip found\n", client->name);
 
        /* Initialize the ADT7473 chip */
@@ -1098,7 +1132,7 @@ static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind)
        data->attrs.attrs = adt7473_attr;
        err = sysfs_create_group(&client->dev.kobj, &data->attrs);
        if (err)
-               goto exit_detach;
+               goto exit_free;
 
        data->hwmon_dev = hwmon_device_register(&client->dev);
        if (IS_ERR(data->hwmon_dev)) {
@@ -1110,27 +1144,26 @@ static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind)
 
 exit_remove:
        sysfs_remove_group(&client->dev.kobj, &data->attrs);
-exit_detach:
-       i2c_detach_client(client);
 exit_free:
        kfree(data);
 exit:
        return err;
 }
 
-static int adt7473_detach_client(struct i2c_client *client)
+static int adt7473_remove(struct i2c_client *client)
 {
        struct adt7473_data *data = i2c_get_clientdata(client);
 
        hwmon_device_unregister(data->hwmon_dev);
        sysfs_remove_group(&client->dev.kobj, &data->attrs);
-       i2c_detach_client(client);
        kfree(data);
        return 0;
 }
 
 static int __init adt7473_init(void)
 {
+       pr_notice("The adt7473 driver is deprecated, please use the adt7475 "
+                 "driver instead\n");
        return i2c_add_driver(&adt7473_driver);
 }