ar9170: implement frequency calibration for one-stage/openfw
[safe/jmp/linux-2.6] / drivers / hwmon / lm85.c
index 3a84dc8..b251d86 100644 (file)
@@ -39,7 +39,8 @@
 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
 
 /* Insmod parameters */
-I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
+I2C_CLIENT_INSMOD_7(lm85b, lm85c, adm1027, adt7463, adt7468, emc6d100,
+                   emc6d102);
 
 /* The LM85 registers */
 
@@ -59,17 +60,26 @@ I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
 
 #define        LM85_REG_COMPANY                0x3e
 #define        LM85_REG_VERSTEP                0x3f
+
+#define        ADT7468_REG_CFG5                0x7c
+#define                ADT7468_OFF64           0x01
+#define        IS_ADT7468_OFF64(data)          \
+       ((data)->type == adt7468 && !((data)->cfg5 & ADT7468_OFF64))
+
 /* These are the recognized values for the above regs */
 #define        LM85_COMPANY_NATIONAL           0x01
 #define        LM85_COMPANY_ANALOG_DEV         0x41
 #define        LM85_COMPANY_SMSC               0x5c
 #define        LM85_VERSTEP_VMASK              0xf0
 #define        LM85_VERSTEP_GENERIC            0x60
+#define        LM85_VERSTEP_GENERIC2           0x70
 #define        LM85_VERSTEP_LM85C              0x60
 #define        LM85_VERSTEP_LM85B              0x62
 #define        LM85_VERSTEP_ADM1027            0x60
 #define        LM85_VERSTEP_ADT7463            0x62
 #define        LM85_VERSTEP_ADT7463C           0x6A
+#define        LM85_VERSTEP_ADT7468_1          0x71
+#define        LM85_VERSTEP_ADT7468_2          0x72
 #define        LM85_VERSTEP_EMC6D100_A0        0x60
 #define        LM85_VERSTEP_EMC6D100_A1        0x61
 #define        LM85_VERSTEP_EMC6D102           0x65
@@ -174,20 +184,13 @@ static int RANGE_TO_REG(int range)
 {
        int i;
 
-       if (range >= lm85_range_map[15])
-               return 15;
-
        /* Find the closest match */
-       for (i = 14; i >= 0; --i) {
-               if (range >= lm85_range_map[i]) {
-                       if ((lm85_range_map[i + 1] - range) <
-                                       (range - lm85_range_map[i]))
-                               return i + 1;
-                       return i;
-               }
+       for (i = 0; i < 15; ++i) {
+               if (range <= (lm85_range_map[i] + lm85_range_map[i + 1]) / 2)
+                       break;
        }
 
-       return 0;
+       return i;
 }
 #define RANGE_FROM_REG(val)    lm85_range_map[(val) & 0x0f]
 
@@ -289,7 +292,6 @@ struct lm85_autofan {
 /* For each registered chip, we need to keep some data in memory.
    The structure is dynamically allocated. */
 struct lm85_data {
-       struct i2c_client client;
        struct device *hwmon_dev;
        const int *freq_map;
        enum chips type;
@@ -314,26 +316,46 @@ struct lm85_data {
        u8 vid;                 /* Register value */
        u8 vrm;                 /* VRM version */
        u32 alarms;             /* Register encoding, combined */
+       u8 cfg5;                /* Config Register 5 on ADT7468 */
        struct lm85_autofan autofan[3];
        struct lm85_zone zone[3];
 };
 
-static int lm85_attach_adapter(struct i2c_adapter *adapter);
-static int lm85_detect(struct i2c_adapter *adapter, int address,
-                       int kind);
-static int lm85_detach_client(struct i2c_client *client);
+static int lm85_detect(struct i2c_client *client, int kind,
+                      struct i2c_board_info *info);
+static int lm85_probe(struct i2c_client *client,
+                     const struct i2c_device_id *id);
+static int lm85_remove(struct i2c_client *client);
 
 static int lm85_read_value(struct i2c_client *client, u8 reg);
 static void lm85_write_value(struct i2c_client *client, u8 reg, int value);
 static struct lm85_data *lm85_update_device(struct device *dev);
 
 
+static const struct i2c_device_id lm85_id[] = {
+       { "adm1027", adm1027 },
+       { "adt7463", adt7463 },
+       { "adt7468", adt7468 },
+       { "lm85", any_chip },
+       { "lm85b", lm85b },
+       { "lm85c", lm85c },
+       { "emc6d100", emc6d100 },
+       { "emc6d101", emc6d100 },
+       { "emc6d102", emc6d102 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, lm85_id);
+
 static struct i2c_driver lm85_driver = {
+       .class          = I2C_CLASS_HWMON,
        .driver = {
                .name   = "lm85",
        },
-       .attach_adapter = lm85_attach_adapter,
-       .detach_client  = lm85_detach_client,
+       .probe          = lm85_probe,
+       .remove         = lm85_remove,
+       .id_table       = lm85_id,
+       .detect         = lm85_detect,
+       .address_data   = &addr_data,
 };
 
 
@@ -388,7 +410,8 @@ static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
        struct lm85_data *data = lm85_update_device(dev);
        int vid;
 
-       if (data->type == adt7463 && (data->vid & 0x80)) {
+       if ((data->type == adt7463 || data->type == adt7468) &&
+           (data->vid & 0x80)) {
                /* 6-pin VID (VRM 10) */
                vid = vid_from_reg(data->vid & 0x3f, data->vrm);
        } else {
@@ -675,6 +698,9 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
        struct lm85_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
+       if (IS_ADT7468_OFF64(data))
+               val += 64;
+
        mutex_lock(&data->update_lock);
        data->temp_min[nr] = TEMP_TO_REG(val);
        lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
@@ -698,6 +724,9 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
        struct lm85_data *data = i2c_get_clientdata(client);
        long val = simple_strtol(buf, NULL, 10);
 
+       if (IS_ADT7468_OFF64(data))
+               val += 64;
+
        mutex_lock(&data->update_lock);
        data->temp_max[nr] = TEMP_TO_REG(val);
        lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
@@ -965,13 +994,6 @@ temp_auto(1);
 temp_auto(2);
 temp_auto(3);
 
-static int lm85_attach_adapter(struct i2c_adapter *adapter)
-{
-       if (!(adapter->class & I2C_CLASS_HWMON))
-               return 0;
-       return i2c_probe(adapter, &addr_data, lm85_detect);
-}
-
 static struct attribute *lm85_attributes[] = {
        &sensor_dev_attr_fan1_input.dev_attr.attr,
        &sensor_dev_attr_fan2_input.dev_attr.attr,
@@ -1111,30 +1133,19 @@ static void lm85_init_client(struct i2c_client *client)
                dev_warn(&client->dev, "Device is not ready\n");
 }
 
-static int lm85_detect(struct i2c_adapter *adapter, int address,
-               int kind)
+/* Return 0 if detection is successful, -ENODEV otherwise */
+static int lm85_detect(struct i2c_client *client, int kind,
+                      struct i2c_board_info *info)
 {
-       struct i2c_client *client;
-       struct lm85_data *data;
-       int err = 0;
+       struct i2c_adapter *adapter = client->adapter;
+       int address = client->addr;
        const char *type_name;
 
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
                /* We need to be able to do byte I/O */
-               goto ERROR0;
-       }
-
-       if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
-               err = -ENOMEM;
-               goto ERROR0;
+               return -ENODEV;
        }
 
-       client = &data->client;
-       i2c_set_clientdata(client, data);
-       client->addr = address;
-       client->adapter = adapter;
-       client->driver = &lm85_driver;
-
        /* If auto-detecting, determine the chip type */
        if (kind < 0) {
                int company = lm85_read_value(client, LM85_REG_COMPANY);
@@ -1145,10 +1156,11 @@ static int lm85_detect(struct i2c_adapter *adapter, int address,
                        address, company, verstep);
 
                /* All supported chips have the version in common */
-               if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC) {
+               if ((verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC &&
+                   (verstep & LM85_VERSTEP_VMASK) != LM85_VERSTEP_GENERIC2) {
                        dev_dbg(&adapter->dev, "Autodetection failed: "
                                "unsupported version\n");
-                       goto ERROR1;
+                       return -ENODEV;
                }
                kind = any_chip;
 
@@ -1171,6 +1183,10 @@ static int lm85_detect(struct i2c_adapter *adapter, int address,
                        case LM85_VERSTEP_ADT7463C:
                                kind = adt7463;
                                break;
+                       case LM85_VERSTEP_ADT7468_1:
+                       case LM85_VERSTEP_ADT7468_2:
+                               kind = adt7468;
+                               break;
                        }
                } else if (company == LM85_COMPANY_SMSC) {
                        switch (verstep) {
@@ -1186,50 +1202,65 @@ static int lm85_detect(struct i2c_adapter *adapter, int address,
                } else {
                        dev_dbg(&adapter->dev, "Autodetection failed: "
                                "unknown vendor\n");
-                       goto ERROR1;
+                       return -ENODEV;
                }
        }
 
-       /* Fill in the chip specific driver values */
        switch (kind) {
        case lm85b:
                type_name = "lm85b";
-               data->freq_map = lm85_freq_map;
                break;
        case lm85c:
                type_name = "lm85c";
-               data->freq_map = lm85_freq_map;
                break;
        case adm1027:
                type_name = "adm1027";
-               data->freq_map = adm1027_freq_map;
                break;
        case adt7463:
                type_name = "adt7463";
-               data->freq_map = adm1027_freq_map;
+               break;
+       case adt7468:
+               type_name = "adt7468";
                break;
        case emc6d100:
                type_name = "emc6d100";
-               data->freq_map = adm1027_freq_map;
                break;
        case emc6d102:
                type_name = "emc6d102";
-               data->freq_map = adm1027_freq_map;
                break;
        default:
                type_name = "lm85";
-               data->freq_map = lm85_freq_map;
        }
-       strlcpy(client->name, type_name, I2C_NAME_SIZE);
+       strlcpy(info->type, type_name, I2C_NAME_SIZE);
+
+       return 0;
+}
+
+static int lm85_probe(struct i2c_client *client,
+                     const struct i2c_device_id *id)
+{
+       struct lm85_data *data;
+       int err;
+
+       data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL);
+       if (!data)
+               return -ENOMEM;
 
-       /* Fill in the remaining client fields */
-       data->type = kind;
+       i2c_set_clientdata(client, data);
+       data->type = id->driver_data;
        mutex_init(&data->update_lock);
 
-       /* Tell the I2C layer a new client has arrived */
-       err = i2c_attach_client(client);
-       if (err)
-               goto ERROR1;
+       /* Fill in the chip specific driver values */
+       switch (data->type) {
+       case adm1027:
+       case adt7463:
+       case emc6d100:
+       case emc6d102:
+               data->freq_map = adm1027_freq_map;
+               break;
+       default:
+               data->freq_map = lm85_freq_map;
+       }
 
        /* Set the VRM version */
        data->vrm = vid_which_vrm();
@@ -1240,45 +1271,43 @@ static int lm85_detect(struct i2c_adapter *adapter, int address,
        /* Register sysfs hooks */
        err = sysfs_create_group(&client->dev.kobj, &lm85_group);
        if (err)
-               goto ERROR2;
+               goto err_kfree;
 
-       /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
+       /* The ADT7463/68 have an optional VRM 10 mode where pin 21 is used
           as a sixth digital VID input rather than an analog input. */
        data->vid = lm85_read_value(client, LM85_REG_VID);
-       if (!(kind == adt7463 && (data->vid & 0x80)))
+       if (!((data->type == adt7463 || data->type == adt7468) &&
+           (data->vid & 0x80)))
                if ((err = sysfs_create_group(&client->dev.kobj,
                                        &lm85_group_in4)))
-                       goto ERROR3;
+                       goto err_remove_files;
 
        /* The EMC6D100 has 3 additional voltage inputs */
-       if (kind == emc6d100)
+       if (data->type == emc6d100)
                if ((err = sysfs_create_group(&client->dev.kobj,
                                        &lm85_group_in567)))
-                       goto ERROR3;
+                       goto err_remove_files;
 
        data->hwmon_dev = hwmon_device_register(&client->dev);
        if (IS_ERR(data->hwmon_dev)) {
                err = PTR_ERR(data->hwmon_dev);
-               goto ERROR3;
+               goto err_remove_files;
        }
 
        return 0;
 
        /* Error out and cleanup code */
ERROR3:
err_remove_files:
        sysfs_remove_group(&client->dev.kobj, &lm85_group);
        sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
-       if (kind == emc6d100)
+       if (data->type == emc6d100)
                sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
- ERROR2:
-       i2c_detach_client(client);
- ERROR1:
+ err_kfree:
        kfree(data);
- ERROR0:
        return err;
 }
 
-static int lm85_detach_client(struct i2c_client *client)
+static int lm85_remove(struct i2c_client *client)
 {
        struct lm85_data *data = i2c_get_clientdata(client);
        hwmon_device_unregister(data->hwmon_dev);
@@ -1286,7 +1315,6 @@ static int lm85_detach_client(struct i2c_client *client)
        sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
        if (data->type == emc6d100)
                sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
-       i2c_detach_client(client);
        kfree(data);
        return 0;
 }
@@ -1357,7 +1385,8 @@ static struct lm85_data *lm85_update_device(struct device *dev)
                 * There are 2 additional resolution bits per channel and we
                 * have room for 4, so we shift them to the left.
                 */
-               if (data->type == adm1027 || data->type == adt7463) {
+               if (data->type == adm1027 || data->type == adt7463 ||
+                   data->type == adt7468) {
                        int ext1 = lm85_read_value(client,
                                                   ADM1027_REG_EXTEND_ADC1);
                        int ext2 =  lm85_read_value(client,
@@ -1382,16 +1411,23 @@ static struct lm85_data *lm85_update_device(struct device *dev)
                            lm85_read_value(client, LM85_REG_FAN(i));
                }
 
-               if (!(data->type == adt7463 && (data->vid & 0x80))) {
+               if (!((data->type == adt7463 || data->type == adt7468) &&
+                   (data->vid & 0x80))) {
                        data->in[4] = lm85_read_value(client,
                                      LM85_REG_IN(4));
                }
 
+               if (data->type == adt7468)
+                       data->cfg5 = lm85_read_value(client, ADT7468_REG_CFG5);
+
                for (i = 0; i <= 2; ++i) {
                        data->temp[i] =
                            lm85_read_value(client, LM85_REG_TEMP(i));
                        data->pwm[i] =
                            lm85_read_value(client, LM85_REG_PWM(i));
+
+                       if (IS_ADT7468_OFF64(data))
+                               data->temp[i] -= 64;
                }
 
                data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
@@ -1446,7 +1482,8 @@ static struct lm85_data *lm85_update_device(struct device *dev)
                            lm85_read_value(client, LM85_REG_FAN_MIN(i));
                }
 
-               if (!(data->type == adt7463 && (data->vid & 0x80))) {
+               if (!((data->type == adt7463 || data->type == adt7468) &&
+                   (data->vid & 0x80))) {
                        data->in_min[4] = lm85_read_value(client,
                                          LM85_REG_IN_MIN(4));
                        data->in_max[4] = lm85_read_value(client,
@@ -1481,6 +1518,13 @@ static struct lm85_data *lm85_update_device(struct device *dev)
                            lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
                        data->zone[i].critical =
                            lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
+
+                       if (IS_ADT7468_OFF64(data)) {
+                               data->temp_min[i] -= 64;
+                               data->temp_max[i] -= 64;
+                               data->zone[i].limit -= 64;
+                               data->zone[i].critical -= 64;
+                       }
                }
 
                i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);