]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/hwmon/lm90.c
hwmon: (lm90) Support the extra resolution bits of MAX6657
[linux-2.6-omap-h63xx.git] / drivers / hwmon / lm90.c
index c24fe36ac787f960b059abec674b68e5b5a905e2..16b99e0bdff0a970a7c05fd5f7dfd62040b2cedd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  *          monitoring
- * Copyright (C) 2003-2006  Jean Delvare <khali@linux-fr.org>
+ * Copyright (C) 2003-2008  Jean Delvare <khali@linux-fr.org>
  *
  * Based on the lm83 driver. The LM90 is a sensor chip made by National
  * Semiconductor. It reports up to two temperatures (its own plus up to
@@ -149,6 +149,10 @@ I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680);
 #define LM90_REG_R_TCRIT_HYST          0x21
 #define LM90_REG_W_TCRIT_HYST          0x21
 
+/* MAX6657-specific registers */
+
+#define MAX6657_REG_R_LOCAL_TEMPL      0x11
+
 /*
  * Conversions and various macros
  * For local temperatures and limits, critical limits and the hysteresis
@@ -239,15 +243,15 @@ struct lm90_data {
        int kind;
 
        /* registers values */
-       s8 temp8[5];    /* 0: local input
-                          1: local low limit
-                          2: local high limit
-                          3: local critical limit
-                          4: remote critical limit */
-       s16 temp11[4];  /* 0: remote input
+       s8 temp8[4];    /* 0: local low limit
+                          1: local high limit
+                          2: local critical limit
+                          3: remote critical limit */
+       s16 temp11[5];  /* 0: remote input
                           1: remote low limit
                           2: remote high limit
-                          3: remote offset (except max6657) */
+                          3: remote offset (except max6657)
+                          4: local input */
        u8 temp_hyst;
        u8 alarms; /* bitvector */
 };
@@ -285,7 +289,7 @@ static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
                data->temp8[nr] = TEMP1_TO_REG_ADT7461(val);
        else
                data->temp8[nr] = TEMP1_TO_REG(val);
-       i2c_smbus_write_byte_data(client, reg[nr - 1], data->temp8[nr]);
+       i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
        mutex_unlock(&data->update_lock);
        return count;
 }
@@ -347,7 +351,7 @@ static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
        long hyst;
 
        mutex_lock(&data->update_lock);
-       hyst = TEMP1_FROM_REG(data->temp8[3]) - val;
+       hyst = TEMP1_FROM_REG(data->temp8[2]) - val;
        i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
                                  HYST_TO_REG(hyst));
        mutex_unlock(&data->update_lock);
@@ -371,23 +375,23 @@ static ssize_t show_alarm(struct device *dev, struct device_attribute
        return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
 }
 
-static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
-       set_temp8, 1);
+       set_temp8, 0);
 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
        set_temp11, 1);
 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
-       set_temp8, 2);
+       set_temp8, 1);
 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
        set_temp11, 2);
 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
-       set_temp8, 3);
+       set_temp8, 2);
 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
-       set_temp8, 4);
+       set_temp8, 3);
 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
-       set_temphyst, 3);
-static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 4);
+       set_temphyst, 2);
+static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
        set_temp11, 3);
 
@@ -736,6 +740,38 @@ static int lm90_remove(struct i2c_client *client)
        return 0;
 }
 
+static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
+{
+       int err;
+       u8 oldh, newh, l;
+
+       /*
+        * There is a trick here. We have to read two registers to have the
+        * sensor temperature, but we have to beware a conversion could occur
+        * inbetween the readings. The datasheet says we should either use
+        * the one-shot conversion register, which we don't want to do
+        * (disables hardware monitoring) or monitor the busy bit, which is
+        * impossible (we can't read the values and monitor that bit at the
+        * exact same time). So the solution used here is to read the high
+        * byte once, then the low byte, then the high byte again. If the new
+        * high byte matches the old one, then we have a valid reading. Else
+        * we have to read the low byte again, and now we believe we have a
+        * correct reading.
+        */
+       if ((err = lm90_read_reg(client, regh, &oldh))
+        || (err = lm90_read_reg(client, regl, &l))
+        || (err = lm90_read_reg(client, regh, &newh)))
+               return err;
+       if (oldh != newh) {
+               err = lm90_read_reg(client, regl, &l);
+               if (err)
+                       return err;
+       }
+       *value = (newh << 8) | l;
+
+       return 0;
+}
+
 static struct lm90_data *lm90_update_device(struct device *dev)
 {
        struct i2c_client *client = to_i2c_client(dev);
@@ -744,49 +780,39 @@ static struct lm90_data *lm90_update_device(struct device *dev)
        mutex_lock(&data->update_lock);
 
        if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
-               u8 oldh, newh, l;
+               u8 h, l;
 
                dev_dbg(&client->dev, "Updating lm90 data.\n");
-               lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, &data->temp8[0]);
-               lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[1]);
-               lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[2]);
-               lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[3]);
-               lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[4]);
+               lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
+               lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
+               lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
+               lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
                lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
 
-               /*
-                * There is a trick here. We have to read two registers to
-                * have the remote sensor temperature, but we have to beware
-                * a conversion could occur inbetween the readings. The
-                * datasheet says we should either use the one-shot
-                * conversion register, which we don't want to do (disables
-                * hardware monitoring) or monitor the busy bit, which is
-                * impossible (we can't read the values and monitor that bit
-                * at the exact same time). So the solution used here is to
-                * read the high byte once, then the low byte, then the high
-                * byte again. If the new high byte matches the old one,
-                * then we have a valid reading. Else we have to read the low
-                * byte again, and now we believe we have a correct reading.
-                */
-               if (lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &oldh) == 0
-                && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0
-                && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &newh) == 0
-                && (newh == oldh
-                 || lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0))
-                       data->temp11[0] = (newh << 8) | l;
-
-               if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &newh) == 0
+               if (data->kind == max6657) {
+                       lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
+                                   MAX6657_REG_R_LOCAL_TEMPL,
+                                   &data->temp11[4]);
+               } else {
+                       if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
+                                         &h) == 0)
+                               data->temp11[4] = h << 8;
+               }
+               lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
+                           LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
+
+               if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0
                 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, &l) == 0)
-                       data->temp11[1] = (newh << 8) | l;
-               if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &newh) == 0
+                       data->temp11[1] = (h << 8) | l;
+               if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0
                 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, &l) == 0)
-                       data->temp11[2] = (newh << 8) | l;
+                       data->temp11[2] = (h << 8) | l;
                if (data->kind != max6657) {
                        if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
-                                         &newh) == 0
+                                         &h) == 0
                         && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
                                          &l) == 0)
-                               data->temp11[3] = (newh << 8) | l;
+                               data->temp11[3] = (h << 8) | l;
                }
                lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);