diff options
Diffstat (limited to 'driver/temp_sensor/tmp006.c')
-rw-r--r-- | driver/temp_sensor/tmp006.c | 442 |
1 files changed, 236 insertions, 206 deletions
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 |