From d53fe7b79b18441ee88058c09010be44c7b43761 Mon Sep 17 00:00:00 2001 From: ChromeOS Developer Date: Sat, 9 Nov 2013 16:48:44 -0800 Subject: Basic driver for tmp432 temperature sensor This allows local and remote temp values to be added to a board's list of temp sensors. It also adds a 'tmp432' EC console command to query temps and set alert thresholds. Fractional degrees are not supported. DPTF support is not addressed. BUG=chrome-os-partner:23985 BRANCH=none TEST=Add tmp432 support to a board with the sensor then run the 'tmp432' and 'temps' EC console commands. Signed-off-by: Dave Parker Change-Id: Ifee47cf4d4cf5eedef9ef2bfa2149f183f1d7a7b Reviewed-on: https://chromium-review.googlesource.com/178688 Reviewed-by: Randall Spangler Commit-Queue: Dave Parker Tested-by: Dave Parker --- driver/build.mk | 1 + driver/temp_sensor/tmp432.c | 217 ++++++++++++++++++++++++++++++++++++++++++++ driver/temp_sensor/tmp432.h | 96 ++++++++++++++++++++ include/config.h | 1 + 4 files changed, 315 insertions(+) create mode 100644 driver/temp_sensor/tmp432.c create mode 100644 driver/temp_sensor/tmp432.h diff --git a/driver/build.mk b/driver/build.mk index 59e27f1efa..f981b6942c 100644 --- a/driver/build.mk +++ b/driver/build.mk @@ -32,6 +32,7 @@ driver-$(CONFIG_REGULATOR_IR357X)+=regulator_ir357x.o # Temperature sensors driver-$(CONFIG_TEMP_SENSOR_G781)+=temp_sensor/g781.o driver-$(CONFIG_TEMP_SENSOR_TMP006)+=temp_sensor/tmp006.o +driver-$(CONFIG_TEMP_SENSOR_TMP432)+=temp_sensor/tmp432.o # USB switches driver-$(CONFIG_USB_SWITCH_TSU6721)+=usb_switch_tsu6721.o diff --git a/driver/temp_sensor/tmp432.c b/driver/temp_sensor/tmp432.c new file mode 100644 index 0000000000..b0f1c056f2 --- /dev/null +++ b/driver/temp_sensor/tmp432.c @@ -0,0 +1,217 @@ +/* Copyright (c) 2013 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. + */ + +/* TMP432 temperature sensor module for Chrome EC */ + +#include "common.h" +#include "console.h" +#include "tmp432.h" +#include "gpio.h" +#include "i2c.h" +#include "hooks.h" +#include "util.h" + +static int temp_val_local; +static int temp_val_remote1; +static int temp_val_remote2; + +/** + * Determine whether the sensor is powered. + * + * @return non-zero the tmp432 sensor is powered. + */ +static int has_power(void) +{ +#ifdef CONFIG_TEMP_SENSOR_POWER_GPIO + return gpio_get_level(CONFIG_TEMP_SENSOR_POWER_GPIO); +#else + return 1; +#endif +} + +static int raw_read8(const int offset, int *data_ptr) +{ + return i2c_read8(I2C_PORT_THERMAL, TMP432_I2C_ADDR, offset, data_ptr); +} + +static int raw_write8(const int offset, int data) +{ + return i2c_write8(I2C_PORT_THERMAL, TMP432_I2C_ADDR, offset, data); +} + +static int get_temp(const int offset, int *temp_ptr) +{ + int rv; + int temp_raw = 0; + + rv = raw_read8(offset, &temp_raw); + if (rv < 0) + return rv; + + *temp_ptr = (int)(int8_t)temp_raw; + return EC_SUCCESS; +} + +static int tmp432_set_temp(const int offset, int temp) +{ + if (temp < -127 || temp > 127) + return EC_ERROR_INVAL; + + return raw_write8(offset, (uint8_t)temp); +} + +int tmp432_get_val(int idx, int *temp_ptr) +{ + if (!has_power()) + return EC_ERROR_NOT_POWERED; + + switch (idx) { + case TMP432_IDX_LOCAL: + *temp_ptr = temp_val_local; + break; + case TMP432_IDX_REMOTE1: + *temp_ptr = temp_val_remote1; + break; + case TMP432_IDX_REMOTE2: + *temp_ptr = temp_val_remote2; + break; + default: + return EC_ERROR_UNKNOWN; + } + + return EC_SUCCESS; +} + +static void temp_sensor_poll(void) +{ + int temp_c; + + if (!has_power()) + return; + + if (get_temp(TMP432_LOCAL, &temp_c) == EC_SUCCESS) + temp_val_local = C_TO_K(temp_c); + + if (get_temp(TMP432_REMOTE1, &temp_c) == EC_SUCCESS) + temp_val_remote1 = C_TO_K(temp_c); + + if (get_temp(TMP432_REMOTE2, &temp_c) == EC_SUCCESS) + temp_val_remote2 = C_TO_K(temp_c); +} +DECLARE_HOOK(HOOK_SECOND, temp_sensor_poll, HOOK_PRIO_TEMP_SENSOR); + +static void print_temps( + const char *name, + const int tmp432_temp_reg, + const int tmp432_therm_limit_reg, + const int tmp432_high_limit_reg, + const int tmp432_low_limit_reg) +{ + int value; + + ccprintf("%s:\n", name); + + if (get_temp(tmp432_temp_reg, &value) == EC_SUCCESS) + ccprintf(" Temp %3dC\n", value); + + if (get_temp(tmp432_therm_limit_reg, &value) == EC_SUCCESS) + ccprintf(" Therm Trip %3dC\n", value); + + if (get_temp(tmp432_high_limit_reg, &value) == EC_SUCCESS) + ccprintf(" High Alarm %3dC\n", value); + + if (get_temp(tmp432_low_limit_reg, &value) == EC_SUCCESS) + ccprintf(" Low Alarm %3dC\n", value); +} + +static int print_status(void) +{ + int value; + + print_temps("Local", TMP432_LOCAL, + TMP432_LOCAL_THERM_LIMIT, + TMP432_LOCAL_HIGH_LIMIT_R, + TMP432_LOCAL_LOW_LIMIT_R); + + print_temps("Remote1", TMP432_REMOTE1, + TMP432_REMOTE1_THERM_LIMIT, + TMP432_REMOTE1_HIGH_LIMIT_R, + TMP432_REMOTE1_LOW_LIMIT_R); + + print_temps("Remote2", TMP432_REMOTE2, + TMP432_REMOTE2_THERM_LIMIT, + TMP432_REMOTE2_HIGH_LIMIT_R, + TMP432_REMOTE2_LOW_LIMIT_R); + + ccprintf("\n"); + + if (raw_read8(TMP432_STATUS, &value) == EC_SUCCESS) + ccprintf("STATUS: %08b\n", value); + + if (raw_read8(TMP432_CONFIGURATION1_R, &value) == EC_SUCCESS) + ccprintf("CONFIG1: %08b\n", value); + + if (raw_read8(TMP432_CONFIGURATION2_R, &value) == EC_SUCCESS) + ccprintf("CONFIG2: %08b\n", value); + + return EC_SUCCESS; +} + +static int command_tmp432(int argc, char **argv) +{ + char *command; + char *e; + int data; + int offset; + int rv; + + if (!has_power()) { + ccprintf("ERROR: Temp sensor not powered.\n"); + return EC_ERROR_NOT_POWERED; + } + + /* If no args just print status */ + if (argc == 1) + return print_status(); + + if (argc < 3) + return EC_ERROR_PARAM_COUNT; + + command = argv[1]; + offset = strtoi(argv[2], &e, 0); + if (*e || offset < 0 || offset > 255) + return EC_ERROR_PARAM2; + + if (!strcasecmp(command, "getbyte")) { + rv = raw_read8(offset, &data); + if (rv < 0) + return rv; + ccprintf("Byte at offset 0x%02x is %08b\n", offset, data); + return rv; + } + + /* Remaining commands are "tmp432 set-command offset data" */ + if (argc != 4) + return EC_ERROR_PARAM_COUNT; + + data = strtoi(argv[3], &e, 0); + if (*e) + return EC_ERROR_PARAM3; + + if (!strcasecmp(command, "settemp")) { + ccprintf("Setting 0x%02x to %dC\n", offset, data); + rv = tmp432_set_temp(offset, data); + } else if (!strcasecmp(command, "setbyte")) { + ccprintf("Setting 0x%02x to 0x%02x\n", offset, data); + rv = raw_write8(offset, data); + } else + return EC_ERROR_PARAM1; + + return rv; +} +DECLARE_CONSOLE_COMMAND(tmp432, command_tmp432, + "[settemp|setbyte ] or [getbyte ]. " + "Temps in Celsius.", + "Print tmp432 temp sensor status or set parameters.", NULL); diff --git a/driver/temp_sensor/tmp432.h b/driver/temp_sensor/tmp432.h new file mode 100644 index 0000000000..acfaf39503 --- /dev/null +++ b/driver/temp_sensor/tmp432.h @@ -0,0 +1,96 @@ +/* Copyright (c) 2013 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. + */ + +/* TMP432 temperature sensor module for Chrome EC */ + +#ifndef __CROS_EC_TEMP_SENSOR_TMP432_H +#define __CROS_EC_TEMP_SENSOR_TMP432_H + +#define TMP432_I2C_ADDR 0x98 /* 7-bit address is 0x4C */ + +#define TMP432_IDX_LOCAL 0 +#define TMP432_IDX_REMOTE1 1 +#define TMP432_IDX_REMOTE2 2 + +/* Chip-specific registers */ +#define TMP432_LOCAL 0x00 +#define TMP432_REMOTE1 0x01 +#define TMP432_STATUS 0x02 +#define TMP432_CONFIGURATION1_R 0x03 +#define TMP432_CONVERSION_RATE_R 0x04 +#define TMP432_LOCAL_HIGH_LIMIT_R 0x05 +#define TMP432_LOCAL_LOW_LIMIT_R 0x06 +#define TMP432_REMOTE1_HIGH_LIMIT_R 0x07 +#define TMP432_REMOTE1_LOW_LIMIT_R 0x08 +#define TMP432_CONFIGURATION1_W 0x09 +#define TMP432_CONVERSION_RATE_W 0x0a +#define TMP432_LOCAL_HIGH_LIMIT_W 0x0b +#define TMP432_LOCAL_LOW_LIMIT_W 0x0c +#define TMP432_REMOTE1_HIGH_LIMIT_W 0x0d +#define TMP432_REMOTE1_LOW_LIMIT_W 0x0e +#define TMP432_ONESHOT 0x0f +#define TMP432_REMOTE1_EXTD 0x10 +#define TMP432_REMOTE1_HIGH_LIMIT_EXTD 0x13 +#define TMP432_REMOTE1_LOW_LIMIT_EXTD 0x14 +#define TMP432_REMOTE2_HIGH_LIMIT_R 0x15 +#define TMP432_REMOTE2_HIGH_LIMIT_W 0x15 +#define TMP432_REMOTE2_LOW_LIMIT_R 0x16 +#define TMP432_REMOTE2_LOW_LIMIT_W 0x16 +#define TMP432_REMOTE2_HIGH_LIMIT_EXTD 0x17 +#define TMP432_REMOTE2_LOW_LIMIT_EXTD 0x18 +#define TMP432_REMOTE1_THERM_LIMIT 0x19 +#define TMP432_REMOTE2_THERM_LIMIT 0x1a +#define TMP432_STATUS_FAULT 0x1b +#define TMP432_CHANNEL_MASK 0x1f +#define TMP432_LOCAL_THERM_LIMIT 0x20 +#define TMP432_THERM_HYSTERESIS 0x21 +#define TMP432_CONSECUTIVE_ALERT 0x22 +#define TMP432_REMOTE2 0x23 +#define TMP432_REMOTE2_EXTD 0x24 +#define TMP432_BETA_RANGE_CH1 0x25 +#define TMP432_BETA_RANGE_CH2 0x26 +#define TMP432_NFACTOR_REMOTE1 0x27 +#define TMP432_NFACTOR_REMOTE2 0x28 +#define TMP432_LOCAL_EXTD 0x29 +#define TMP432_STATUS_LIMIT_HIGH 0x35 +#define TMP432_STATUS_LIMIT_LOW 0x36 +#define TMP432_STATUS_THERM 0x37 +#define TMP432_LOCAL_HIGH_LIMIT_EXTD 0x3d +#define TMP432_LOCAL_LOW_LIMIT_EXTD 0x3e +#define TMP432_CONFIGURATION2_R 0x3f +#define TMP432_CONFIGURATION2_W 0x3f +#define TMP432_RESET_W 0xfc +#define TMP432_DEVICE_ID 0xfd +#define TMP432_MANUFACTURER_ID 0xfe + +/* Config register bits */ +#define TMP432_CONFIG1_TEMP_RANGE (1 << 2) +#define TMP432_CONFIG1_MODE (1 << 5) +#define TMP432_CONFIG1_RUN_L (1 << 6) +#define TMP432_CONFIG1_ALERT_MASK_L (1 << 7) +#define TMP432_CONFIG2_RESISTANCE_CORRECTION (1 << 2) +#define TMP432_CONFIG2_LOCAL_ENABLE (1 << 3) +#define TMP432_CONFIG2_REMOTE1_ENABLE (1 << 4) +#define TMP432_CONFIG2_REMOTE2_ENABLE (1 << 5) + +/* Status register bits */ +#define TMP432_STATUS_TEMP_THERM_ALARM (1 << 1) +#define TMP432_STATUS_OPEN (1 << 2) +#define TMP432_STATUS_TEMP_LOW_ALARM (1 << 3) +#define TMP432_STATUS_TEMP_HIGH_ALARM (1 << 4) +#define TMP432_STATUS_BUSY (1 << 7) + +/** + * Get the last polled value of a sensor. + * + * @param idx Index to read. Idx indicates whether to read die + * temperature or external temperature. + * @param temp_ptr Destination for temperature in K. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int tmp432_get_val(int idx, int *temp_ptr); + +#endif /* __CROS_EC_TEMP_SENSOR_TMP432_H */ diff --git a/include/config.h b/include/config.h index bc5e176e52..c1d5c351cd 100644 --- a/include/config.h +++ b/include/config.h @@ -662,6 +662,7 @@ /* Support particular temperature sensor chips */ #undef CONFIG_TEMP_SENSOR_G781 /* G781 sensor, on I2C bus */ #undef CONFIG_TEMP_SENSOR_TMP006 /* TI TMP006 sensor, on I2C bus */ +#undef CONFIG_TEMP_SENSOR_TMP432 /* TI TMP432 sensor, on I2C bus */ /* * If defined, active-high GPIO which indicates temperature sensor chips are -- cgit v1.2.1