summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2014-10-17 12:18:25 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-10-22 00:30:31 +0000
commitf36a859e06f10f5bb2482d827797c35c3b758cd3 (patch)
tree3c628763dd984dcea4ab91f112e3e4df21b7de72
parent60ddb3987a60a4a8b3a9daad9150021d017d0497 (diff)
downloadchrome-ec-f36a859e06f10f5bb2482d827797c35c3b758cd3.tar.gz
samus: new algorithm for tmp006 object temperature
The original algorithm is given in the TMP006 User's Guide (SBOU107.pdf). The algorithm we previously implemented is that, plus some additional and completely undocumented massaging of the Tdie and Vobj registers. The original meaning of that hack is now lost in the mists of time, thanks to our email retention policy. This CL introduces a new algorithm variant, but at least this time the details are in the bug report. It's essentially the same as the User's Guide algorithm, except that we apply one-stage FIR filters to the Tdie input and the Tobj output. There are five new parameters: d0, d1, ds, e0, e1. Refer to tmp006_read_object_temp_k() in ec/driver/temp_sensor/tmp006.c to see how these new parameters are applied. CAUTION: The tmp006 sensor algorithm is mostly math and magic numbers. The spreadsheet attached to the bug report has six sheets with wildly varying values for those parameters. Since the correct parameter values haven't yet been determined for Samus, all I can be sure of with this CL is that it seems to work and isn't any worse than the old one. Oh, and note that the EC's 't6cal' console command has been disabled until/unless we add support for floating point IO. Use ectool from the host to get and set the params instead. BUG=chrome-os-partner:32260 BRANCH=ToT,Samus TEST=manual After booting, look at the sensor values using ectool: localhost ~ # ectool temps all 0: 312 1: 314 2: 313 Sensor 3 not calibrated 4: 311 Sensor 5 not calibrated 6: 305 Sensor 7 not calibrated 8: 306 Sensor 9 not calibrated 10: 307 Sensor 11 not calibrated 12: 312 Sensor 13 not calibrated localhost ~ # localhost ~ # ectool tempsinfo all 0: 0 PECI 1: 1 ECInternal 2: 1 I2C-Charger-Die 3: 2 I2C-Charger-Object 4: 1 I2C-CPU-Die 5: 2 I2C-CPU-Object 6: 1 I2C-Left C-Die 7: 2 I2C-Left C-Object 8: 1 I2C-Right C-Die 9: 2 I2C-Right C-Object 10: 1 I2C-Right D-Die 11: 2 I2C-Right D-Object 12: 1 I2C-Left D-Die 13: 2 I2C-Left D-Object EC result 2 (ERROR) ... localhost ~ # There are six tmp006 object temps that need calibrating. The index used for the calibration is for the tmp006 objects, not the 3,5,7,.. numbers reported for all temp sensors. See the current values with tmp006cal: localhost ~ # /tmp/ectool tmp006cal 5 algorithm: 1 params: s0 0.000000e+00 a1 1.750000e-03 a2 -1.678000e-05 b0 -2.940000e-05 b1 -5.700000e-07 b2 4.630000e-09 c2 1.340000e+01 d0 2.000000e-01 d1 8.000000e-01 ds 1.480000e-04 e0 1.000000e-01 e1 9.000000e-01 localhost ~ # If the s0 param is zero, this sensor is uncalibrated. The params are entered in the order in which they're displayed You can change any or all of the parameters. Skip the ones you don't want to update by specifying '-' for its position. (Note: throw in an extra '--' first so that ectool doesn't think that negative numbers are command options). For example, to change s0 and b0: localhost ~ # ectool -- tmp006cal 5 1.0 - - -3.0 localhost ~ # localhost ~ # ectool tmp006cal 5 algorithm: 1 params: s0 1.000000e+00 a1 1.750000e-03 a2 -1.678000e-05 b0 -3.000000e+00 b1 -5.700000e-07 b2 4.630000e-09 c2 1.340000e+01 d0 2.000000e-01 d1 8.000000e-01 ds 1.480000e-04 e0 1.000000e-01 e1 9.000000e-01 localhost ~ # Now sensor 13 (tmp006 object index 5) is calibrated: localhost ~ # ectool temps all 0: 310 1: 315 2: 313 Sensor 3 not calibrated 4: 310 Sensor 5 not calibrated 6: 305 Sensor 7 not calibrated 8: 307 Sensor 9 not calibrated 10: 307 Sensor 11 not calibrated 12: 312 13: 313 Change-Id: I61b5da486f5e053a028c533ca9e00b9a82a91615 Signed-off-by: Bill Richardson <wfrichar@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/224409 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--board/samus/board.c1
-rw-r--r--driver/temp_sensor/tmp006.c442
-rw-r--r--driver/temp_sensor/tmp006.h13
-rw-r--r--include/ec_commands.h40
-rw-r--r--util/comm-dev.c44
-rw-r--r--util/ectool.c183
6 files changed, 457 insertions, 266 deletions
diff --git a/board/samus/board.c b/board/samus/board.c
index 5580f5d658..9d2feb93a8 100644
--- a/board/samus/board.c
+++ b/board/samus/board.c
@@ -152,6 +152,7 @@ const struct tmp006_t tmp006_sensors[TMP006_COUNT] = {
{"Right D", TEMP_U115_ADDR},
{"Left D", TEMP_U116_ADDR},
};
+BUILD_ASSERT(ARRAY_SIZE(tmp006_sensors) == TMP006_COUNT);
/* Temperature sensors data; must be in same order as enum temp_sensor_id. */
const struct temp_sensor_t temp_sensors[] = {
diff --git a/driver/temp_sensor/tmp006.c b/driver/temp_sensor/tmp006.c
index 6bd30f70bf..52cd01a85a 100644
--- a/driver/temp_sensor/tmp006.c
+++ b/driver/temp_sensor/tmp006.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
@@ -21,16 +21,13 @@
#define CPUTS(outstr) cputs(CC_THERMAL, outstr)
#define CPRINTS(format, args...) cprints(CC_THERMAL, format, ## args)
-/* Constants for calculating target object temperatures */
-static const float A1 = 1.75e-3f;
-static const float A2 = -1.678e-5f;
-static const float B0 = -2.94e-5f;
-static const float B1 = -5.7e-7f;
-static const float B2 = 4.63e-9f;
-static const float C2 = 13.4f;
-
-/* Defined in board_temp_sensor.c. */
-extern const struct tmp006_t tmp006_sensors[TMP006_COUNT];
+/*
+ * Alg 0 was what's in the TMP006 User's Guide. Alg 1 is Alg 0, but with
+ * some filters applied to the Tdie input and Tobj output (see
+ * crosbug.com/p/32260).
+ */
+#define ALGORITHM_NUM 1
+#define ALGORITHM_PARAMS 12
/* Flags for tdata->fail */
#define FAIL_INIT (1 << 0) /* Just initialized */
@@ -38,24 +35,44 @@ extern const struct tmp006_t tmp006_sensors[TMP006_COUNT];
#define FAIL_I2C (1 << 2) /* I2C communication error */
#define FAIL_NOT_READY (1 << 3) /* Data not ready */
+/* State and conversion factors to track for each sensor */
struct tmp006_data_t {
- int v; /* Object voltage */
- int t[4]; /* Circular buffer of last four die temperatures */
- int tidx; /* Index of the current value in t[] */
- int fail; /* Fail flags; non-zero if last read failed */
- float s0; /* Sensitivity factor */
- float b0, b1, b2; /* Coefficients for self-heating correction */
+ /* chip info */
+ int16_t v_raw; /* TMP006_REG_VOBJ */
+ int16_t t_raw0; /* TMP006_REG_TDIE */
+ int fail; /* Fail flags; non-zero if last read failed */
+ /* calibration params */
+ float s0, a1, a2; /* Sensitivity factors */
+ float b0, b1, b2; /* Self-heating correction */
+ float c2; /* Seebeck effect */
+ float d0, d1, ds; /* Tdie filter and slope adjustment */
+ float e0, e1; /* Tobj output filter */
+ /* FIR filter stages */
+ float tdie1, tobj1;
};
-
static struct tmp006_data_t tmp006_data[TMP006_COUNT];
-/**
- * Check if sensor has power
- *
- * @param idx Sensor index
- *
- * @return non-zero if sensor has power.
- */
+/* Default state and conversion factors */
+static const struct tmp006_data_t tmp006_data_default = {
+ .fail = FAIL_INIT,
+
+ /* Alg 0 params from User's Guide */
+ .s0 = 0.0f, /* zero == "uncalibrated" */
+ .a1 = 1.75e-3f,
+ .a2 = -1.678e-5f,
+ .b0 = -2.94e-5f,
+ .b1 = -5.7e-7f,
+ .b2 = 4.63e-9f,
+ .c2 = 13.4f,
+
+ /* Additional Alg 1 filter params */
+ .d0 = 0.2f,
+ .d1 = 0.8f,
+ .ds = 1.48e-4,
+ .e0 = 0.1f,
+ .e1 = 0.9f,
+};
+
static int tmp006_has_power(int idx)
{
#ifdef CONFIG_TEMP_SENSOR_POWER_GPIO
@@ -65,156 +82,151 @@ static int tmp006_has_power(int idx)
#endif
}
-static int tmp006_read_die_temp(const struct tmp006_data_t *tdata,
- int *temp_ptr)
+static void tmp006_poll_sensor(int sensor_id)
{
- if (tdata->fail)
- return EC_ERROR_UNKNOWN;
+ struct tmp006_data_t *tdata = tmp006_data + sensor_id;
+ int t, v, rv;
+ int addr = tmp006_sensors[sensor_id].addr;
- /* Return previous die temperature */
- *temp_ptr = tdata->t[(tdata->tidx - 1) & 0x3] / 100;
- return EC_SUCCESS;
-}
+ /* Invalidate the filter history if there is any error */
+ if (tdata->fail) {
+ tdata->tdie1 = 0.0f;
+ tdata->tobj1 = 0.0;
+ }
-/**
- * Calculate the remote object temperature.
- *
- * @param tdie_i Die temperature in 1/100 K.
- * @param vobj_i Voltage read from register 0. In nV.
- * @param tdata TMP006 data for this sensor.
- *
- * @return Object temperature in 1/100 K.
- */
-static int tmp006_calculate_object_temp(int tdie_i, int vobj_i,
- const struct tmp006_data_t *tdata)
-{
- float tdie, vobj;
- float tx, s, vos, vx, fv, tobj, t4;
- int tobj_i;
+ if (!tmp006_has_power(sensor_id)) {
+ tdata->fail |= FAIL_POWER;
+ return;
+ }
- tdie = (float)tdie_i * 1e-2f;
- vobj = (float)vobj_i * 1e-9f;
+ /*
+ * If sensor has just initialized and/or has lost power, wait for
+ * data ready; otherwise, we read garbage data.
+ */
+ if (tdata->fail & (FAIL_POWER | FAIL_INIT)) {
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_CONFIG, &v);
+ if (rv) {
+ tdata->fail |= FAIL_I2C;
+ return;
+ } else if (!(v & 0x80)) {
+ /* Bit 7 is the Data Ready bit */
+ tdata->fail |= FAIL_NOT_READY;
+ return;
+ }
+ }
- /* Calculate according to TMP006 users guide. */
- tx = tdie - 298.15f;
- /* s is the sensitivity */
- s = tdata->s0 * (1.0f + A1 * tx + A2 * tx * tx);
- /* vos is the offset voltage */
- vos = tdata->b0 + tdata->b1 * tx + tdata->b2 * tx * tx;
- vx = vobj - vos;
- /* fv is Seebeck coefficient f(vobj) */
- fv = vx + C2 * vx * vx;
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_TDIE, &t);
+ if (rv) {
+ tdata->fail |= FAIL_I2C;
+ return;
+ }
- t4 = tdie * tdie * tdie * tdie + fv / s;
- tobj = sqrtf(sqrtf(t4));
- tobj_i = (int32_t)(tobj * 100.0f);
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_VOBJ, &v);
+ if (rv) {
+ tdata->fail |= FAIL_I2C;
+ return;
+ }
+
+ tdata->t_raw0 = t;
+ tdata->v_raw = v;
- return tobj_i;
+ tdata->fail = 0;
}
-/**
- * Apply TMP006 temporal correction.
- *
- * @param t1-t4 Four die temperature readings separated by 1s in 1/100K.
- * @param vobj Voltage read from register 0, in nV.
- *
- * @return Corrected object voltage in 1/100K.
- */
-static int tmp006_correct_object_voltage(int t1, int t2, int t3, int t4,
- int vobj)
+/*****************************************************************************/
+/* Hooks */
+
+static void tmp006_init(void)
{
- int tslope = 3 * t1 + t2 - t3 - 3 * t4;
- return vobj + 296 * tslope;
+ int i;
+
+ for (i = 0; i < TMP006_COUNT; ++i)
+ tmp006_data[i] = tmp006_data_default;
}
+DECLARE_HOOK(HOOK_INIT, tmp006_init, HOOK_PRIO_DEFAULT);
-static int tmp006_read_object_temp(const struct tmp006_data_t *tdata,
- int *temp_ptr)
+static void tmp006_poll(void)
{
- int pidx = (tdata->tidx - 1) & 0x3;
- int t = tdata->t[pidx];
- int v = tdata->v;
-
- if (tdata->fail)
- return EC_ERROR_UNKNOWN;
+ int i;
- if (!tdata->s0)
- return EC_ERROR_NOT_CALIBRATED;
+ for (i = 0; i < TMP006_COUNT; ++i)
+ tmp006_poll_sensor(i);
+}
+DECLARE_HOOK(HOOK_SECOND, tmp006_poll, HOOK_PRIO_TEMP_SENSOR);
- v = tmp006_correct_object_voltage(
- t,
- tdata->t[(pidx + 3) & 3],
- tdata->t[(pidx + 2) & 3],
- tdata->t[(pidx + 1) & 3],
- v);
+/*****************************************************************************/
+/* Interface to the rest of the EC */
- *temp_ptr = tmp006_calculate_object_temp(t, v, tdata) / 100;
+/* This just returns Tdie */
+static int tmp006_read_die_temp_k(const struct tmp006_data_t *tdata,
+ int *temp_ptr)
+{
+ if (tdata->fail)
+ return EC_ERROR_UNKNOWN;
+ /* Tdie reg is signed 1/128 degrees C, resolution 1/32 degrees */
+ *temp_ptr = (int)tdata->t_raw0 / 128 + 273;
return EC_SUCCESS;
}
-static int tmp006_poll_sensor(int sensor_id)
+/*
+ * This uses Tdie and Vobj and a bunch of magic parameters to calulate the
+ * object temperature, Tobj.
+ */
+static int tmp006_read_object_temp_k(struct tmp006_data_t *tdata,
+ int *temp_ptr)
{
- struct tmp006_data_t *tdata = tmp006_data + sensor_id;
- int traw, t;
- int vraw, v;
- int rv;
- int addr = tmp006_sensors[sensor_id].addr;
- int idx;
+ float tdie, vobj;
+ float tx, s, vos, vx, fv, tobj, t4;
+ float tdie_filtered, tdie_slope, tobj_filtered;
- if (!tmp006_has_power(sensor_id)) {
- tdata->fail |= FAIL_POWER;
+ if (tdata->fail)
return EC_ERROR_UNKNOWN;
- }
- /*
- * If sensor has just initialized and/or has lost power, wait for
- * data ready; otherwise, we read garbage data.
- */
- if (tdata->fail && (FAIL_POWER | FAIL_INIT)) {
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0x02, &v);
- if (rv) {
- tdata->fail |= FAIL_I2C;
- return EC_ERROR_UNKNOWN;
- } else if (!(v & 0x80)) {
- tdata->fail |= FAIL_NOT_READY;
- return EC_ERROR_UNKNOWN;
- }
- }
+ if (!tdata->s0)
+ return EC_ERROR_NOT_CALIBRATED;
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0x01, &traw);
- if (rv) {
- tdata->fail |= FAIL_I2C;
- return EC_ERROR_UNKNOWN;
- }
+ /* Tdie reg is signed 1/128 degrees C, resolution 1/32 degrees
+ * We need degrees K */
+ tdie = (float)tdata->t_raw0 / 128.0f + 273.15f;
+ /* Vobj reg is signed int, LSB = 156.25 nV
+ * We need volts */
+ vobj = (float)tdata->v_raw / 156.25f * 1e-9f;
+
+ /* Alg1: apply filter to tdie. If tdie1 is 0K, initialize it. */
+ if (tdata->tdie1 == 0.0f)
+ tdata->tdie1 = tdie;
+ tdie_filtered = tdata->d0 * tdie + tdata->d1 * tdata->tdie1;
+ tdie_slope = tdie - tdie_filtered;
+ /* Remember the current Tdie for next time */
+ tdata->tdie1 = tdie;
- /* Convert temperature from raw to 1/100 K */
- t = ((int)(int16_t)traw * 100) / 128 + 27300;
+ /* Calculate according to TMP006 users guide. */
+ tx = tdie - 298.15f;
+ /* s is the sensitivity */
+ s = tdata->s0 * (1.0f + tdata->a1 * tx + tdata->a2 * tx * tx);
+ /* vos is the offset voltage */
+ vos = tdata->b0 + tdata->b1 * tx + tdata->b2 * tx * tx;
+ /* Alg1: use Tdie FIR here */
+ vx = vobj - vos + tdie_slope * tdata->ds;
+ /* fv is Seebeck coefficient f(vobj) */
+ fv = vx + tdata->c2 * vx * vx;
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0x00, &vraw);
- if (rv) {
- tdata->fail |= FAIL_I2C;
- return EC_ERROR_UNKNOWN;
- }
+ t4 = tdie * tdie * tdie * tdie + fv / s;
+ tobj = sqrtf(sqrtf(t4));
- /* Convert voltage from raw to nV */
- v = ((int)(int16_t)vraw * 15625) / 100;
+ /* Alg1: apply another filter on the calculated tobj. */
+ if (tdata->tobj1 == 0.0f)
+ tdata->tobj1 = tobj;
- /*
- * If last read failed, set the entire temperature history to the
- * current temperature. This keeps us from making inaccurate temporal
- * corrections based on stale data.
- */
- if (tdata->fail) {
- for (idx = 0; idx < 4; idx++)
- tdata->t[idx] = t;
- } else {
- idx = tdata->tidx;
- tdata->t[idx] = t;
- tdata->tidx = (idx + 1) & 3;
- }
+ tobj_filtered = tdata->e0 * tobj + tdata->e1 * tdata->tobj1;
+ tdata->tobj1 = tobj;
- tdata->v = v;
- tdata->fail = 0;
+ /* return integer degrees K */
+ *temp_ptr = tobj_filtered;
return EC_SUCCESS;
}
@@ -226,7 +238,7 @@ int tmp006_get_val(int idx, int *temp_ptr)
* TMP006 index and the bottom bit is (0=die, 1=remote).
*/
int tidx = idx >> 1;
- const struct tmp006_data_t *tdata = tmp006_data + tidx;
+ struct tmp006_data_t *tdata = tmp006_data + tidx;
if (tdata->fail & FAIL_POWER) {
/*
@@ -241,40 +253,10 @@ int tmp006_get_val(int idx, int *temp_ptr)
/* Check the low bit to determine which temperature to read. */
if ((idx & 0x1) == 0)
- return tmp006_read_die_temp(tdata, temp_ptr);
+ return tmp006_read_die_temp_k(tdata, temp_ptr);
else
- return tmp006_read_object_temp(tdata, temp_ptr);
-}
-
-/*****************************************************************************/
-/* Hooks */
-
-static void tmp006_poll(void)
-{
- int i;
-
- for (i = 0; i < TMP006_COUNT; ++i)
- tmp006_poll_sensor(i);
-}
-DECLARE_HOOK(HOOK_SECOND, tmp006_poll, HOOK_PRIO_TEMP_SENSOR);
-
-static void tmp006_init(void)
-{
- int i;
-
- for (i = 0; i < TMP006_COUNT; ++i) {
- struct tmp006_data_t *tdata = tmp006_data + i;
-
- /* Report error until we actually read the sensor */
- tdata->fail = FAIL_INIT;
-
- /* Use defaults for Bn params */
- tdata->b0 = B0;
- tdata->b1 = B1;
- tdata->b2 = B2;
- }
+ return tmp006_read_object_temp_k(tdata, temp_ptr);
}
-DECLARE_HOOK(HOOK_INIT, tmp006_init, HOOK_PRIO_DEFAULT);
/*****************************************************************************/
/* Host commands */
@@ -282,7 +264,7 @@ DECLARE_HOOK(HOOK_INIT, tmp006_init, HOOK_PRIO_DEFAULT);
int tmp006_get_calibration(struct host_cmd_handler_args *args)
{
const struct ec_params_tmp006_get_calibration *p = args->params;
- struct ec_response_tmp006_get_calibration *r = args->response;
+ struct ec_response_tmp006_get_calibration_v1 *r1 = args->response;
const struct tmp006_data_t *tdata;
if (p->index >= TMP006_COUNT)
@@ -290,39 +272,63 @@ int tmp006_get_calibration(struct host_cmd_handler_args *args)
tdata = tmp006_data + p->index;
- r->s0 = tdata->s0;
- r->b0 = tdata->b0;
- r->b1 = tdata->b1;
- r->b2 = tdata->b2;
-
- args->response_size = sizeof(*r);
+ r1->algorithm = ALGORITHM_NUM;
+ r1->num_params = ALGORITHM_PARAMS;
+ r1->val[0] = tdata->s0;
+ r1->val[1] = tdata->a1;
+ r1->val[2] = tdata->a2;
+ r1->val[3] = tdata->b0;
+ r1->val[4] = tdata->b1;
+ r1->val[5] = tdata->b2;
+ r1->val[6] = tdata->c2;
+ r1->val[7] = tdata->d0;
+ r1->val[8] = tdata->d1;
+ r1->val[9] = tdata->ds;
+ r1->val[10] = tdata->e0;
+ r1->val[11] = tdata->e1;
+
+ args->response_size = sizeof(*r1) +
+ r1->num_params * sizeof(r1->val[0]);
return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_TMP006_GET_CALIBRATION,
tmp006_get_calibration,
- EC_VER_MASK(0));
+ EC_VER_MASK(1));
int tmp006_set_calibration(struct host_cmd_handler_args *args)
{
- const struct ec_params_tmp006_set_calibration *p = args->params;
+ const struct ec_params_tmp006_set_calibration_v1 *p1 = args->params;
struct tmp006_data_t *tdata;
- if (p->index >= TMP006_COUNT)
+ if (p1->index >= TMP006_COUNT)
return EC_RES_INVALID_PARAM;
- tdata = tmp006_data + p->index;
+ /* We only have one algorithm today */
+ if (p1->algorithm != ALGORITHM_NUM ||
+ p1->num_params != ALGORITHM_PARAMS)
+ return EC_RES_INVALID_PARAM;
- tdata->s0 = p->s0;
- tdata->b0 = p->b0;
- tdata->b1 = p->b1;
- tdata->b2 = p->b2;
+ tdata = tmp006_data + p1->index;
+
+ tdata->s0 = p1->val[0];
+ tdata->a1 = p1->val[1];
+ tdata->a2 = p1->val[2];
+ tdata->b0 = p1->val[3];
+ tdata->b1 = p1->val[4];
+ tdata->b2 = p1->val[5];
+ tdata->c2 = p1->val[6];
+ tdata->d0 = p1->val[7];
+ tdata->d1 = p1->val[8];
+ tdata->ds = p1->val[9];
+ tdata->e0 = p1->val[10];
+ tdata->e1 = p1->val[11];
return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_TMP006_SET_CALIBRATION,
tmp006_set_calibration,
- EC_VER_MASK(0));
+ EC_VER_MASK(1));
int tmp006_get_raw(struct host_cmd_handler_args *args)
{
@@ -335,8 +341,13 @@ int tmp006_get_raw(struct host_cmd_handler_args *args)
tdata = tmp006_data + p->index;
- r->v = tdata->v;
- r->t = tdata->t[(tdata->tidx - 1) & 0x3];
+ /* Vobj reg is signed int, LSB = 156.25 nV
+ * response units are nV */
+ r->v = ((int)tdata->v_raw * 15625) / 100;
+
+ /* Tdie reg is signed 1/128 degrees C, resolution 1/32 degrees
+ * response units are 1/100 degrees K */
+ r->t = ((int)tdata->t_raw0 * 100) / 128 + 27315;
args->response_size = sizeof(*r);
@@ -368,25 +379,31 @@ static int tmp006_print(int idx)
return EC_ERROR_UNKNOWN;
}
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0xfe, &d);
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_MANUFACTURER_ID, &d);
if (rv)
return rv;
ccprintf(" Manufacturer ID: 0x%04x\n", d);
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0xff, &d);
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_DEVICE_ID, &d);
ccprintf(" Device ID: 0x%04x\n", d);
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0x02, &d);
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_CONFIG, &d);
ccprintf(" Config: 0x%04x\n", d);
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0x00, &vraw);
- v = ((int)(int16_t)vraw * 15625) / 100;
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_VOBJ, &vraw);
+
+ v = ((int)vraw * 15625) / 100;
ccprintf(" Voltage: 0x%04x = %d nV\n", vraw, v);
- rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr), 0x01, &traw);
- t = ((int)(int16_t)traw * 100) / 128;
+ rv = i2c_read16(TMP006_PORT(addr), TMP006_REG(addr),
+ TMP006_REG_TDIE, &traw);
+ t = (int)traw;
ccprintf(" Temperature: 0x%04x = %d.%02d C\n",
- traw, t / 100, t > 0 ? t % 100 : 100 - (t % 100));
+ traw, t / 128, t > 0 ? t % 128 : 128 - (t % 128));
return EC_SUCCESS;
}
@@ -395,9 +412,19 @@ static int command_sensor_info(int argc, char **argv)
{
int i;
int rv, rv1;
+ int a = 0, b = TMP006_COUNT;
+
+ if (argc > 1) {
+ char *e = 0;
+ i = strtoi(argv[1], &e, 0);
+ if (*e || i < 0 || i >= TMP006_COUNT)
+ return EC_ERROR_PARAM1;
+ a = i;
+ b = i + 1;
+ }
rv1 = EC_SUCCESS;
- for (i = 0; i < TMP006_COUNT; i++) {
+ for (i = a; i < b; i++) {
rv = tmp006_print(i);
if (rv != EC_SUCCESS)
rv1 = rv;
@@ -407,10 +434,12 @@ static int command_sensor_info(int argc, char **argv)
return rv1;
}
DECLARE_CONSOLE_COMMAND(tmp006, command_sensor_info,
- NULL,
+ "[ <index> ]",
"Print TMP006 sensors",
NULL);
+/* Disable the t6cal command until/unless we have FP support in printf */
+#if 0
static int command_t6cal(int argc, char **argv)
{
struct tmp006_data_t *tdata;
@@ -464,3 +493,4 @@ DECLARE_CONSOLE_COMMAND(t6cal, command_t6cal,
"[<index> <coeff_name> <radix>]",
"Set/print TMP006 calibration",
NULL);
+#endif
diff --git a/driver/temp_sensor/tmp006.h b/driver/temp_sensor/tmp006.h
index a8c4f19001..df99d1c1ca 100644
--- a/driver/temp_sensor/tmp006.h
+++ b/driver/temp_sensor/tmp006.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
@@ -8,6 +8,14 @@
#ifndef __CROS_EC_TMP006_H
#define __CROS_EC_TMP006_H
+/* Registers within the TMP006 chip */
+#define TMP006_REG_VOBJ 0x00
+#define TMP006_REG_TDIE 0x01
+#define TMP006_REG_CONFIG 0x02
+#define TMP006_REG_MANUFACTURER_ID 0xfe
+#define TMP006_REG_DEVICE_ID 0xff
+
+/* I2C address components */
#define TMP006_ADDR(PORT,REG) ((PORT << 16) + REG)
#define TMP006_PORT(ADDR) (ADDR >> 16)
#define TMP006_REG(ADDR) (ADDR & 0xffff)
@@ -17,6 +25,9 @@ struct tmp006_t {
int addr; /* I2C address formed by TMP006_ADDR macro. */
};
+/* Names and addresses of the sensors we have */
+extern const struct tmp006_t tmp006_sensors[];
+
/**
* Get the last polled value of a sensor.
*
diff --git a/include/ec_commands.h b/include/ec_commands.h
index 14abf48c09..3675dfa6d9 100644
--- a/include/ec_commands.h
+++ b/include/ec_commands.h
@@ -1612,32 +1612,58 @@ struct ec_params_auto_fan_ctrl_v1 {
uint8_t fan_idx;
} __packed;
-/* Get TMP006 calibration data */
+/* Get/Set TMP006 calibration data */
#define EC_CMD_TMP006_GET_CALIBRATION 0x53
+#define EC_CMD_TMP006_SET_CALIBRATION 0x54
+
+/*
+ * The original TMP006 calibration only needed four params, but now we need
+ * more. Since the algorithm is nothing but magic numbers anyway, we'll leave
+ * the params opaque. The v1 "get" response will include the algorithm number
+ * and how many params it requires. That way we can change the EC code without
+ * needing to update this file. We can also use a different algorithm on each
+ * sensor.
+ */
+/* This is the same struct for both v0 and v1. */
struct ec_params_tmp006_get_calibration {
uint8_t index;
} __packed;
-struct ec_response_tmp006_get_calibration {
+/* Version 0 */
+struct ec_response_tmp006_get_calibration_v0 {
float s0;
float b0;
float b1;
float b2;
} __packed;
-/* Set TMP006 calibration data */
-#define EC_CMD_TMP006_SET_CALIBRATION 0x54
-
-struct ec_params_tmp006_set_calibration {
+struct ec_params_tmp006_set_calibration_v0 {
uint8_t index;
- uint8_t reserved[3]; /* Reserved; set 0 */
+ uint8_t reserved[3];
float s0;
float b0;
float b1;
float b2;
} __packed;
+/* Version 1 */
+struct ec_response_tmp006_get_calibration_v1 {
+ uint8_t algorithm;
+ uint8_t num_params;
+ uint8_t reserved[2];
+ float val[0];
+} __packed;
+
+struct ec_params_tmp006_set_calibration_v1 {
+ uint8_t index;
+ uint8_t algorithm;
+ uint8_t num_params;
+ uint8_t reserved;
+ float val[0];
+} __packed;
+
+
/* Read raw TMP006 data */
#define EC_CMD_TMP006_GET_RAW 0x55
diff --git a/util/comm-dev.c b/util/comm-dev.c
index daa2402c20..cdbbbdf48e 100644
--- a/util/comm-dev.c
+++ b/util/comm-dev.c
@@ -19,6 +19,37 @@
static int fd = -1;
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(t) (sizeof(t) / sizeof(t[0]))
+#endif
+
+static const char const *meanings[] = {
+ "SUCCESS",
+ "INVALID_COMMAND",
+ "ERROR",
+ "INVALID_PARAM",
+ "ACCESS_DENIED",
+ "INVALID_RESPONSE",
+ "INVALID_VERSION",
+ "INVALID_CHECKSUM",
+ "IN_PROGRESS",
+ "UNAVAILABLE",
+ "TIMEOUT",
+ "OVERFLOW",
+ "INVALID_HEADER",
+ "REQUEST_TRUNCATED",
+ "RESPONSE_TOO_BIG",
+ "BUS_ERROR"
+};
+
+static const char *strresult(int i)
+{
+ if (i < 0 || i >= ARRAY_SIZE(meanings))
+ return "<unknown>";
+ return meanings[i];
+}
+
+
static int ec_command_dev(int command, int version,
const void *outdata, int outsize,
void *indata, int insize)
@@ -36,17 +67,20 @@ static int ec_command_dev(int command, int version,
r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
if (r < 0) {
- fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d\n",
- r, errno, strerror(errno), s_cmd.result);
+ fprintf(stderr, "ioctl %d, errno %d (%s), EC result %d (%s)\n",
+ r, errno, strerror(errno), s_cmd.result,
+ strresult(s_cmd.result));
if (errno == EAGAIN && s_cmd.result == EC_RES_IN_PROGRESS) {
s_cmd.command = EC_CMD_RESEND_RESPONSE;
r = ioctl(fd, CROS_EC_DEV_IOCXCMD, &s_cmd);
fprintf(stderr,
- "ioctl %d, errno %d (%s), EC result %d\n",
- r, errno, strerror(errno), s_cmd.result);
+ "ioctl %d, errno %d (%s), EC result %d (%s)\n",
+ r, errno, strerror(errno), s_cmd.result,
+ strresult(s_cmd.result));
}
} else if (s_cmd.result != EC_RES_SUCCESS) {
- fprintf(stderr, "EC result %d\n", s_cmd.result);
+ fprintf(stderr, "EC result %d (%s)\n", s_cmd.result,
+ strresult(s_cmd.result));
return -EECRESULT - s_cmd.result;
}
diff --git a/util/ectool.c b/util/ectool.c
index b166e11f64..c873677438 100644
--- a/util/ectool.c
+++ b/util/ectool.c
@@ -180,7 +180,7 @@ const char help_str[] =
" Get the threshold temperature values from the thermal engine.\n"
" thermalset <platform-specific args>\n"
" Set the threshold temperature values for the thermal engine.\n"
- " tmp006cal <tmp006_index> [<S0> <b0> <b1> <b2>]\n"
+ " tmp006cal <tmp006_index> [params...]\n"
" Get/set TMP006 calibration\n"
" tmp006raw <tmp006_index>\n"
" Get raw TMP006 data\n"
@@ -4633,76 +4633,165 @@ static int cmd_keyconfig(int argc, char *argv[])
return 0;
}
-int cmd_tmp006cal(int argc, char *argv[])
+/* Index is already checked. argv[0] is first param value */
+static int cmd_tmp006cal_v0(int idx, int argc, char *argv[])
{
- struct ec_params_tmp006_set_calibration p;
+ struct ec_params_tmp006_get_calibration pg;
+ struct ec_response_tmp006_get_calibration_v0 rg;
+ struct ec_params_tmp006_set_calibration_v0 ps;
+ float val;
char *e;
- int idx;
- int rv;
+ int i, rv;
- if (argc < 2) {
- fprintf(stderr, "Must specify tmp006 index.\n");
- return -1;
+ /* Get current values */
+ pg.index = idx;
+ rv = ec_command(EC_CMD_TMP006_GET_CALIBRATION, 0,
+ &pg, sizeof(pg), &rg, sizeof(rg));
+ if (rv < 0)
+ return rv;
+
+ if (!argc) {
+ /* If no new values are given, just print what we have */
+ printf("S0: %e\n", rg.s0);
+ printf("b0: %e\n", rg.b0);
+ printf("b1: %e\n", rg.b1);
+ printf("b2: %e\n", rg.b2);
+ return EC_SUCCESS;
}
- idx = strtol(argv[1], &e, 0);
- if ((e && *e) || idx < 0 || idx > 255) {
- fprintf(stderr, "Bad index.\n");
- return -1;
+ /* Prepare to reuse the current values */
+ memset(&ps, 0, sizeof(ps));
+ ps.index = idx;
+ ps.s0 = rg.s0;
+ ps.b0 = rg.b0;
+ ps.b1 = rg.b1;
+ ps.b2 = rg.b2;
+
+ /* Parse up to four args, skipping any that are just "-" */
+ for (i = 0; i < argc && i < 4; i++) {
+ if (!strcmp(argv[i], "-"))
+ continue;
+ val = strtod(argv[i], &e);
+ if (e && *e) {
+ fprintf(stderr,
+ "Bad arg \"%s\". Use \"-\" to skip a param.\n",
+ argv[i]);
+ return -1;
+ }
+ switch (i) {
+ case 0:
+ ps.s0 = val;
+ break;
+ case 1:
+ ps.b0 = val;
+ break;
+ case 2:
+ ps.b1 = val;
+ break;
+ case 3:
+ ps.b2 = val;
+ break;
+ }
}
- if (argc == 2) {
- struct ec_params_tmp006_get_calibration pg;
- struct ec_response_tmp006_get_calibration r;
+ /* Set 'em */
+ return ec_command(EC_CMD_TMP006_SET_CALIBRATION, 0,
+ &ps, sizeof(ps), NULL, 0);
+}
- pg.index = idx;
+/* Index is already checked. argv[0] is first param value */
+static int cmd_tmp006cal_v1(int idx, int argc, char *argv[])
+{
+ struct ec_params_tmp006_get_calibration pg;
+ struct ec_response_tmp006_get_calibration_v1 *rg = ec_inbuf;
+ struct ec_params_tmp006_set_calibration_v1 *ps = ec_outbuf;
+ float val;
+ char *e;
+ int i, rv, cmdsize;
- rv = ec_command(EC_CMD_TMP006_GET_CALIBRATION, 0,
- &pg, sizeof(pg), &r, sizeof(r));
- if (rv < 0)
- return rv;
+ /* Algorithm 1 parameter names */
+ static const char const *alg1_pname[] = {
+ "s0", "a1", "a2", "b0", "b1", "b2", "c2",
+ "d0", "d1", "ds", "e0", "e1",
+ };
- printf("S0: %e\n", r.s0);
- printf("b0: %e\n", r.b0);
- printf("b1: %e\n", r.b1);
- printf("b2: %e\n", r.b2);
+ /* Get current values */
+ pg.index = idx;
+ rv = ec_command(EC_CMD_TMP006_GET_CALIBRATION, 1,
+ &pg, sizeof(pg), rg, ec_max_insize);
+ if (rv < 0)
+ return rv;
+
+ if (!argc) {
+ /* If no new values are given, just print what we have */
+ printf("algorithm: %d\n", rg->algorithm);
+ printf("params:\n");
+ /* We only know about alg 1 at the moment */
+ if (rg->algorithm == 1)
+ for (i = 0; i < rg->num_params; i++)
+ printf(" %s %e\n", alg1_pname[i], rg->val[i]);
+ else
+ for (i = 0; i < rg->num_params; i++)
+ printf(" param%d %e\n", i, rg->val[i]);
return EC_SUCCESS;
}
- if (argc != 6) {
- fprintf(stderr, "Must specify all calibration params.\n");
- return -1;
+ /* Prepare to reuse the current values */
+ memset(ps, 0, ec_max_outsize);
+ ps->index = idx;
+ ps->algorithm = rg->algorithm;
+ ps->num_params = rg->num_params;
+ for (i = 0; i < rg->num_params; i++)
+ ps->val[i] = rg->val[i];
+
+ /* Parse the args, skipping any that are just "-" */
+ for (i = 0; i < argc && i < rg->num_params; i++) {
+ if (!strcmp(argv[i], "-"))
+ continue;
+ val = strtod(argv[i], &e);
+ if (e && *e) {
+ fprintf(stderr,
+ "Bad arg \"%s\". Use \"-\" to skip a param.\n",
+ argv[i]);
+ return -1;
+ }
+ ps->val[i] = val;
}
- memset(&p, 0, sizeof(p));
- p.index = idx;
+ /* Set 'em */
+ cmdsize = sizeof(*ps) + ps->num_params * sizeof(ps->val[0]);
+ return ec_command(EC_CMD_TMP006_SET_CALIBRATION, 1,
+ ps, cmdsize, NULL, 0);
+}
- p.s0 = strtod(argv[2], &e);
- if (e && *e) {
- fprintf(stderr, "Bad S0.\n");
- return -1;
- }
+int cmd_tmp006cal(int argc, char *argv[])
+{
+ char *e;
+ int idx;
- p.b0 = strtod(argv[3], &e);
- if (e && *e) {
- fprintf(stderr, "Bad b0.\n");
+ if (argc < 2) {
+ fprintf(stderr, "Must specify tmp006 index.\n");
return -1;
}
- p.b1 = strtod(argv[4], &e);
- if (e && *e) {
- fprintf(stderr, "Bad b1.\n");
+ idx = strtol(argv[1], &e, 0);
+ if ((e && *e) || idx < 0 || idx > 255) {
+ fprintf(stderr, "Bad index.\n");
return -1;
}
- p.b2 = strtod(argv[5], &e);
- if (e && *e) {
- fprintf(stderr, "Bad b2.\n");
- return -1;
- }
+ /* Pass just the params (if any) to the helper function */
+ argc -= 2;
+ argv += 2;
- return ec_command(EC_CMD_TMP006_SET_CALIBRATION, 0,
- &p, sizeof(p), NULL, 0);
+ if (ec_cmd_version_supported(EC_CMD_TMP006_GET_CALIBRATION, 1))
+ return cmd_tmp006cal_v1(idx, argc, argv);
+
+ if (ec_cmd_version_supported(EC_CMD_TMP006_GET_CALIBRATION, 0))
+ return cmd_tmp006cal_v0(idx, argc, argv);
+
+ printf("The EC is being stupid\n");
+ return -1;
}
int cmd_tmp006raw(int argc, char *argv[])