diff options
author | Scott Worley <scott.worley@microchip.corp-partner.google.com> | 2017-12-20 15:32:39 -0500 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-12-28 14:50:31 -0800 |
commit | 940dd625b9edaf1827c9c3a2349a66ec21cd59c8 (patch) | |
tree | e73ea5b5f1808549b66c2a7d66391d1d3d6f5a46 /driver/temp_sensor | |
parent | eb29ab7acdcb5b9d88794a3f5d36850f6e33a2a5 (diff) | |
download | chrome-ec-940dd625b9edaf1827c9c3a2349a66ec21cd59c8.tar.gz |
ec_driver: Add ADT7481 and TMP411 I2C sensors
Added I2C sensors ADT7481 and TMP411 with config
items and build rules.
BRANCH=none
BUG=
TEST=Define CONFIG_TEMP_SENSOR_ADT7481 or _TMP411
and build board.
Change-Id: I4d1eb55ee56ad3f42787538bb839193e683d0a60
Signed-off-by: Scott Worley <scott.worley@microchip.corp-partner.google.com>
Diffstat (limited to 'driver/temp_sensor')
-rw-r--r-- | driver/temp_sensor/adt7481.c | 349 | ||||
-rw-r--r-- | driver/temp_sensor/adt7481.h | 178 | ||||
-rw-r--r-- | driver/temp_sensor/tmp411.c | 329 | ||||
-rw-r--r-- | driver/temp_sensor/tmp411.h | 140 |
4 files changed, 996 insertions, 0 deletions
diff --git a/driver/temp_sensor/adt7481.c b/driver/temp_sensor/adt7481.c new file mode 100644 index 0000000000..22d7cc5f9a --- /dev/null +++ b/driver/temp_sensor/adt7481.c @@ -0,0 +1,349 @@ +/* Copyright 2017 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. + */ + +/* ADT7481 temperature sensor module for Chrome EC */ + +#include "common.h" +#include "console.h" +#include "adt7481.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; +static uint8_t is_sensor_shutdown; + +/** + * Determine whether the sensor is powered. + * + * @return non-zero the adt7481 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 !is_sensor_shutdown; +#endif +} + +static int raw_read8(const int offset, int *data_ptr) +{ + return i2c_read8(I2C_PORT_THERMAL, ADT7481_I2C_ADDR, offset, data_ptr); +} + +static int raw_write8(const int offset, int data) +{ + return i2c_write8(I2C_PORT_THERMAL, ADT7481_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; +} + +#ifdef CONFIG_CMD_TEMP_SENSOR +static int adt7481_set_temp(const int offset, int temp) +{ + if (temp < -127 || temp > 127) + return EC_ERROR_INVAL; + + return raw_write8(offset, (uint8_t)temp); +} +#endif + +int adt7481_get_val(int idx, int *temp_ptr) +{ + if (!has_power()) + return EC_ERROR_NOT_POWERED; + + switch (idx) { + case ADT7481_IDX_LOCAL: + *temp_ptr = temp_val_local; + break; + case ADT7481_IDX_REMOTE1: + *temp_ptr = temp_val_remote1; + break; + case ADT7481_IDX_REMOTE2: + *temp_ptr = temp_val_remote2; + break; + default: + return EC_ERROR_UNKNOWN; + } + + return EC_SUCCESS; +} + +static int adt7481_shutdown(uint8_t want_shutdown) +{ + int ret, value; + + if (want_shutdown == is_sensor_shutdown) + return EC_SUCCESS; + + ret = raw_read8(ADT7481_CONFIGURATION1_R, &value); + if (ret < 0) { + ccprintf("ERROR: Temp sensor I2C read8 error.\n"); + return ret; + } + + if (want_shutdown && !(value & ADT7481_CONFIG1_RUN_L)) { + /* adt7481 is running, and want it to shutdown */ + /* CONFIG REG1 BIT6: 0=Run, 1=Shutdown */ + /* shut it down */ + value |= ADT7481_CONFIG1_RUN_L; + ret = raw_write8(ADT7481_CONFIGURATION1_R, value); + } else if (!want_shutdown && (value & ADT7481_CONFIG1_RUN_L)) { + /* adt7481 is shutdown, and want turn it on */ + value &= ~ADT7481_CONFIG1_RUN_L; + ret = raw_write8(ADT7481_CONFIGURATION1_R, value); + } + /* else, the current setting is exactly what you want */ + + is_sensor_shutdown = want_shutdown; + return ret; +} + +static int adt7481_set_therm_mode(void) +{ + int ret = 0; + int data = 0; + + ret = raw_read8(ADT7481_CONFIGURATION1_R, &data); + if (ret) + return EC_ERROR_UNKNOWN; + + data |= ADT7481_CONFIG1_MODE; + ret = raw_write8(ADT7481_CONFIGURATION1_W, data); + if (ret) + return EC_ERROR_UNKNOWN; + + return EC_SUCCESS; +} + +int adt7481_set_therm_limit(int channel, int limit_c, int hysteresis) +{ + int ret = 0; + int reg = 0; + + if (channel >= ADT7481_CHANNEL_COUNT) + return EC_ERROR_INVAL; + + if (hysteresis > ADT7481_HYSTERESIS_HIGH_LIMIT || + hysteresis < ADT7481_HYSTERESIS_LOW_LIMIT) + return EC_ERROR_INVAL; + + /* hysteresis must be less than high limit */ + if (hysteresis > limit_c) + return EC_ERROR_INVAL; + + if (adt7481_set_therm_mode() != EC_SUCCESS) + return EC_ERROR_UNKNOWN; + + switch (channel) { + case ADT7481_CHANNEL_LOCAL: + reg = ADT7481_LOCAL_HIGH_LIMIT_W; + break; + case ADT7481_CHANNEL_REMOTE1: + reg = ADT7481_REMOTE1_HIGH_LIMIT_W; + break; + case ADT7481_CHANNEL_REMOTE2: + reg = ADT7481_REMOTE2_HIGH_LIMIT; + break; + } + + ret = raw_write8(reg, limit_c); + if (ret) + return EC_ERROR_UNKNOWN; + + ret = raw_write8(ADT7481_THERM_HYSTERESIS, hysteresis); + if (ret) + return EC_ERROR_UNKNOWN; + + return EC_SUCCESS; +} + +static void adt7481_temp_sensor_poll(void) +{ + int temp_c; + + if (!has_power()) + return; + + if (get_temp(ADT7481_LOCAL, &temp_c) == EC_SUCCESS) + temp_val_local = C_TO_K(temp_c); + + if (get_temp(ADT7481_REMOTE1, &temp_c) == EC_SUCCESS) + temp_val_remote1 = C_TO_K(temp_c); + + if (get_temp(ADT7481_REMOTE2, &temp_c) == EC_SUCCESS) + temp_val_remote2 = C_TO_K(temp_c); +} +DECLARE_HOOK(HOOK_SECOND, adt7481_temp_sensor_poll, HOOK_PRIO_TEMP_SENSOR); + +#ifdef CONFIG_CMD_TEMP_SENSOR +static void print_temps( + const char *name, + const int adt7481_temp_reg, + const int adt7481_therm_limit_reg, + const int adt7481_high_limit_reg, + const int adt7481_low_limit_reg) +{ + int value; + + if (!has_power()) { + ccprintf(" ADT7481 is shutdown\n"); + return; + } + + ccprintf("%s:\n", name); + + if (get_temp(adt7481_temp_reg, &value) == EC_SUCCESS) + ccprintf(" Temp %3dC\n", value); + + if (get_temp(adt7481_therm_limit_reg, &value) == EC_SUCCESS) + ccprintf(" Therm Trip %3dC\n", value); + + if (get_temp(adt7481_high_limit_reg, &value) == EC_SUCCESS) + ccprintf(" High Alarm %3dC\n", value); + + if (get_temp(adt7481_low_limit_reg, &value) == EC_SUCCESS) + ccprintf(" Low Alarm %3dC\n", value); +} + +static int print_status(void) +{ + int value; + + print_temps("Local", ADT7481_LOCAL, + ADT7481_LOCAL_THERM_LIMIT, + ADT7481_LOCAL_HIGH_LIMIT_R, + ADT7481_LOCAL_LOW_LIMIT_R); + + print_temps("Remote1", ADT7481_REMOTE1, + ADT7481_REMOTE1_THERM_LIMIT, + ADT7481_REMOTE1_HIGH_LIMIT_R, + ADT7481_REMOTE1_LOW_LIMIT_R); + + print_temps("Remote2", ADT7481_REMOTE2, + ADT7481_REMOTE2_THERM_LIMIT, + ADT7481_REMOTE2_HIGH_LIMIT, + ADT7481_REMOTE2_LOW_LIMIT); + + ccprintf("\n"); + + if (raw_read8(ADT7481_STATUS1_R, &value) == EC_SUCCESS) + ccprintf("STATUS1: %08b\n", value); + + if (raw_read8(ADT7481_STATUS2_R, &value) == EC_SUCCESS) + ccprintf("STATUS2: %08b\n", value); + + if (raw_read8(ADT7481_CONFIGURATION1_R, &value) == EC_SUCCESS) + ccprintf("CONFIG1: %08b\n", value); + + if (raw_read8(ADT7481_CONFIGURATION2, &value) == EC_SUCCESS) + ccprintf("CONFIG2: %08b\n", value); + + return EC_SUCCESS; +} + +static int command_adt7481(int argc, char **argv) +{ + char *command; + char *e; + char *power; + int data; + int offset; + int rv; + + /* handle "power" command before checking the power status. */ + if ((argc == 3) && !strcasecmp(argv[1], "power")) { + power = argv[2]; + if (!strncasecmp(power, "on", sizeof("on"))) { + rv = adt7481_set_power(ADT7481_POWER_ON); + if (!rv) + print_status(); + } else if (!strncasecmp(power, "off", sizeof("off"))) + rv = adt7481_set_power(ADT7481_POWER_OFF); + else + return EC_ERROR_PARAM2; + ccprintf("Set ADT7481 %s\n", power); + return 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 "adt7481 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 = adt7481_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(adt7481, command_adt7481, + "[settemp|setbyte <offset> <value>] or [getbyte <offset>] or" + "[power <on|off>]. " + "Temps in Celsius.", + "Print tmp432 temp sensor status or set parameters."); +#endif + +int adt7481_set_power(enum adt7481_power_state power_on) +{ +#ifndef CONFIG_TEMP_SENSOR_POWER_GPIO + uint8_t shutdown = (power_on == ADT7481_POWER_OFF) ? 1 : 0; + + return adt7481_shutdown(shutdown); +#else + gpio_set_level(CONFIG_TEMP_SENSOR_POWER_GPIO, power_on); + return EC_SUCCESS; +#endif +} + diff --git a/driver/temp_sensor/adt7481.h b/driver/temp_sensor/adt7481.h new file mode 100644 index 0000000000..11e9856b9c --- /dev/null +++ b/driver/temp_sensor/adt7481.h @@ -0,0 +1,178 @@ +/* Copyright 2017 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. + */ + +/* ADT7481 temperature sensor module for Chrome EC */ + +#ifndef __CROS_EC_ADT7481_H +#define __CROS_EC_ADT7481_H + +#define ADT7481_I2C_ADDR 0x96 /* 7-bit address is 0x4B */ + +#define ADT7481_IDX_LOCAL 0 +#define ADT7481_IDX_REMOTE1 1 +#define ADT7481_IDX_REMOTE2 2 + +/* Chip-specific registers */ +#define ADT7481_LOCAL 0x00 +#define ADT7481_REMOTE1 0x01 +#define ADT7481_STATUS1_R 0x02 +#define ADT7481_CONFIGURATION1_R 0x03 +#define ADT7481_CONVERSION_RATE_R 0x04 +#define ADT7481_LOCAL_HIGH_LIMIT_R 0x05 +#define ADT7481_LOCAL_LOW_LIMIT_R 0x06 +#define ADT7481_REMOTE1_HIGH_LIMIT_R 0x07 +#define ADT7481_REMOTE1_LOW_LIMIT_R 0x08 +#define ADT7481_CONFIGURATION1_W 0x09 +#define ADT7481_CONVERSION_RATE_W 0x0a +#define ADT7481_LOCAL_HIGH_LIMIT_W 0x0b +#define ADT7481_LOCAL_LOW_LIMIT_W 0x0c +#define ADT7481_REMOTE1_HIGH_LIMIT_W 0x0d +#define ADT7481_REMOTE1_LOW_LIMIT_W 0x0e +#define ADT7481_ONESHOT_W 0x0f +#define ADT7481_REMOTE1_EXTD_R 0x10 +#define ADT7481_REMOTE1_OFFSET 0x11 +#define ADT7481_REMOTE1_OFFSET_EXTD 0x12 +#define ADT7481_REMOTE1_HIGH_LIMIT_EXTD 0x13 +#define ADT7481_REMOTE1_LOW_LIMIT_EXTD 0x14 +#define ADT7481_REMOTE1_THERM_LIMIT 0x19 +#define ADT7481_LOCAL_THERM_LIMIT 0x20 +#define ADT7481_THERM_HYSTERESIS 0x21 +#define ADT7481_CONSECUTIVE_ALERT 0x22 +#define ADT7481_STATUS2_R 0x23 +#define ADT7481_CONFIGURATION2 0x24 +#define ADT7481_REMOTE2 0x30 +#define ADT7481_REMOTE2_HIGH_LIMIT 0x31 +#define ADT7481_REMOTE2_LOW_LIMIT 0x32 +#define ADT7481_REMOTE2_EXTD_R 0x33 +#define ADT7481_REMOTE2_OFFSET 0x34 +#define ADT7481_REMOTE2_OFFSET_EXTD 0x35 +#define ADT7481_REMOTE2_HIGH_LIMIT_EXTD 0x36 +#define ADT7481_REMOTE2_LOW_LIMIT_EXTD 0x37 +#define ADT7481_REMOTE2_THERM_LIMIT 0x39 +#define ADT7481_DEVICE_ID 0x3d +#define ADT7481_MANUFACTURER_ID 0x3e + +/* Config1 register bits */ +#define ADT7481_CONFIG1_REMOTE1_ALERT_MASK (1 << 0) +#define ADT7481_CONFIG1_REMOTE2_ALERT_MASK (1 << 1) +#define ADT7481_CONFIG1_TEMP_RANGE (1 << 2) +#define ADT7481_CONFIG1_SEL_REMOTE2 (1 << 3) +/* ADT7481_CONFIG1_MODE bit is use to enable THERM mode */ +#define ADT7481_CONFIG1_MODE (1 << 5) +#define ADT7481_CONFIG1_RUN_L (1 << 6) +/* mask all alerts on ALERT# pin */ +#define ADT7481_CONFIG1_ALERT_MASK_L (1 << 7) + +/* Config2 register bits */ +#define ADT7481_CONFIG2_LOCK (1 << 7) + +/* Conversion Rate/Channel Select Register */ +#define ADT7481_CONV_RATE_MASK (0x0f) +#define ADT7481_CONV_RATE_16S (0x00) +#define ADT7481_CONV_RATE_8S (0x01) +#define ADT7481_CONV_RATE_4S (0x02) +#define ADT7481_CONV_RATE_2S (0x03) +#define ADT7481_CONV_RATE_1S (0x04) +#define ADT7481_CONV_RATE_500MS (0x05) +#define ADT7481_CONV_RATE_250MS (0x06) +#define ADT7481_CONV_RATE_125MS (0x07) +#define ADT7481_CONV_RATE_62500US (0x08) +#define ADT7481_CONV_RATE_31250US (0x09) +#define ADT7481_CONV_RATE_15500US (0x0a) +/* continuous mode 73 ms averaging */ +#define ADT7481_CONV_RATE_73MS_AVE (0x0b) +#define ADT7481_CONV_CHAN_SELECT_MASK (0x30) +#define ADT7481_CONV_CHAN_SEL_ROUND_ROBIN (0 << 4) +#define ADT7481_CONV_CHAN_SEL_LOCAL (1 << 4) +#define ADT7481_CONV_CHAN_SEL_REMOTE1 (2 << 4) +#define ADT7481_CONV_CHAN_SEL_REMOTE2 (3 << 4) +#define ADT7481_CONV_AVERAGING_L (1 << 7) + + +/* Status1 register bits */ +#define ADT7481_STATUS1_LOCAL_THERM_ALARM (1 << 0) +#define ADT7481_STATUS1_REMOTE1_THERM_ALARM (1 << 1) +#define ADT7481_STATUS1_REMOTE1_OPEN (1 << 2) +#define ADT7481_STATUS1_REMOTE1_LOW_ALARM (1 << 3) +#define ADT7481_STATUS1_REMOTE1_HIGH_ALARM (1 << 4) +#define ADT7481_STATUS1_LOCAL_LOW_ALARM (1 << 5) +#define ADT7481_STATUS1_LOCAL_HIGH_ALARM (1 << 6) +#define ADT7481_STATUS1_BUSY (1 << 7) + +/* Status2 register bits */ +#define ADT7481_STATUS2_ALERT (1 << 0) +#define ADT7481_STATUS2_REMOTE2_THERM_ALARM (1 << 1) +#define ADT7481_STATUS2_REMOTE2_OPEN (1 << 2) +#define ADT7481_STATUS2_REMOTE2_LOW_ALARM (1 << 3) +#define ADT7481_STATUS2_REMOTE2_HIGH_ALARM (1 << 4) + +/* Consecutive Alert register */ +#define ADT7481_CONSEC_MASK (0xf) +#define ADT7481_CONSEC_1 (0x0) +#define ADT7481_CONSEC_2 (0x2) +#define ADT7481_CONSEC_3 (0x6) +#define ADT7481_CONSEC_4 (0xe) +#define ADT7481_CONSEC_EN_SCL_TIMEOUT (1 << 5) +#define ADT7481_CONSEC_EN_SDA_TIMEOUT (1 << 6) +#define ADT7481_CONSEC_MASK_LOCAL_ALERT (1 << 7) + + +/* Limits */ +#define ADT7481_HYSTERESIS_HIGH_LIMIT 255 +#define ADT7481_HYSTERESIS_LOW_LIMIT 0 + +enum adt7481_power_state { + ADT7481_POWER_OFF = 0, + ADT7481_POWER_ON, + ADT7481_POWER_COUNT +}; + +enum adt7481_channel_id { + ADT7481_CHANNEL_LOCAL, + ADT7481_CHANNEL_REMOTE1, + ADT7481_CHANNEL_REMOTE2, + + ADT7481_CHANNEL_COUNT +}; + +/** + * 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 adt7481_get_val(int idx, int *temp_ptr); + +/** + * Power control function of ADT7481 temperature sensor. + * + * @param power_on ADT7481_POWER_ON: turn ADT7481 sensor on. + * ADT7481_POWER_OFF: shut ADT7481 sensor down. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int adt7481_set_power(enum adt7481_power_state power_on); + +/* + * Set ADT7481 ALERT#/THERM2# pin to THERM mode, and give a limit + * for a specific channel. + * + * @param channel specific a channel + * + * @param limit_c High limit temperature, default: 85C + * + * @param hysteresis Hysteresis temperature, default: 10C + * All channels share the same hysteresis + * + * In THERM mode, ALERT# pin will trigger(Low) by itself when any + * channel's temperature is greater( >= )than channel's limit_c, + * and release(High) by itself when channel's temperature is lower + * than (limit_c - hysteresis) + */ +int adt7481_set_therm_limit(int channel, int limit_c, int hysteresis); +#endif /* __CROS_EC_ADT7481_H */ diff --git a/driver/temp_sensor/tmp411.c b/driver/temp_sensor/tmp411.c new file mode 100644 index 0000000000..9740e6e716 --- /dev/null +++ b/driver/temp_sensor/tmp411.c @@ -0,0 +1,329 @@ +/* Copyright (c) 2017 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. + */ + +/* TMP411 temperature sensor module for Chrome EC */ + +#include "common.h" +#include "console.h" +#include "tmp411.h" +#include "gpio.h" +#include "i2c.h" +#include "hooks.h" +#include "util.h" + +static int temp_val_local; +static int temp_val_remote1; +static uint8_t is_sensor_shutdown; + +/** + * Determine whether the sensor is powered. + * + * @return non-zero the tmp411 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 !is_sensor_shutdown; +#endif +} + +static int raw_read8(const int offset, int *data_ptr) +{ + return i2c_read8(I2C_PORT_THERMAL, TMP411_I2C_ADDR, offset, data_ptr); +} + +static int raw_write8(const int offset, int data) +{ + return i2c_write8(I2C_PORT_THERMAL, TMP411_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; +} + +#ifdef CONFIG_CMD_TEMP_SENSOR +static int tmp411_set_temp(const int offset, int temp) +{ + if (temp < -127 || temp > 127) + return EC_ERROR_INVAL; + + return raw_write8(offset, (uint8_t)temp); +} +#endif + +int tmp411_get_val(int idx, int *temp_ptr) +{ + if (!has_power()) + return EC_ERROR_NOT_POWERED; + + switch (idx) { + case TMP411_IDX_LOCAL: + *temp_ptr = temp_val_local; + break; + case TMP411_IDX_REMOTE1: + *temp_ptr = temp_val_remote1; + break; + default: + return EC_ERROR_UNKNOWN; + } + + return EC_SUCCESS; +} + +static int tmp411_shutdown(uint8_t want_shutdown) +{ + int ret, value; + + if (want_shutdown == is_sensor_shutdown) + return EC_SUCCESS; + + ret = raw_read8(TMP411_CONFIGURATION1_R, &value); + if (ret < 0) { + ccprintf("ERROR: Temp sensor I2C read8 error.\n"); + return ret; + } + + if (want_shutdown && !(value & TMP411_CONFIG1_RUN_L)) { + /* tmp411 is running, and want it to shutdown */ + /* CONFIG REG1 BIT6: 0=Run, 1=Shutdown */ + /* shut it down */ + value |= TMP411_CONFIG1_RUN_L; + ret = raw_write8(TMP411_CONFIGURATION1_R, value); + } else if (!want_shutdown && (value & TMP411_CONFIG1_RUN_L)) { + /* tmp411 is shutdown, and want turn it on */ + value &= ~TMP411_CONFIG1_RUN_L; + ret = raw_write8(TMP411_CONFIGURATION1_R, value); + } + /* else, the current setting is exactly what you want */ + + is_sensor_shutdown = want_shutdown; + return ret; +} + +static int tmp411_set_therm_mode(void) +{ + int ret = 0; + int data = 0; + + ret = raw_read8(TMP411_CONFIGURATION1_R, &data); + if (ret) + return EC_ERROR_UNKNOWN; + + data |= TMP411_CONFIG1_MODE; + ret = raw_write8(TMP411_CONFIGURATION1_W, data); + if (ret) + return EC_ERROR_UNKNOWN; + + return EC_SUCCESS; +} + +int tmp411_set_therm_limit(int channel, int limit_c, int hysteresis) +{ + int ret = 0; + int reg = 0; + + if (channel >= TMP411_CHANNEL_COUNT) + return EC_ERROR_INVAL; + + if (hysteresis > TMP411_HYSTERESIS_HIGH_LIMIT || + hysteresis < TMP411_HYSTERESIS_LOW_LIMIT) + return EC_ERROR_INVAL; + + /* hysteresis must be less than high limit */ + if (hysteresis > limit_c) + return EC_ERROR_INVAL; + + if (tmp411_set_therm_mode() != EC_SUCCESS) + return EC_ERROR_UNKNOWN; + + switch (channel) { + case TMP411_CHANNEL_LOCAL: + reg = TMP411_LOCAL_HIGH_LIMIT_W; + break; + case TMP411_CHANNEL_REMOTE1: + reg = TMP411_REMOTE1_HIGH_LIMIT_W; + break; + } + + ret = raw_write8(reg, limit_c); + if (ret) + return EC_ERROR_UNKNOWN; + + ret = raw_write8(TMP411_THERM_HYSTERESIS, hysteresis); + if (ret) + return EC_ERROR_UNKNOWN; + + return EC_SUCCESS; +} + +static void tmp411_temp_sensor_poll(void) +{ + int temp_c; + + if (!has_power()) + return; + + if (get_temp(TMP411_LOCAL, &temp_c) == EC_SUCCESS) + temp_val_local = C_TO_K(temp_c); + + if (get_temp(TMP411_REMOTE1, &temp_c) == EC_SUCCESS) + temp_val_remote1 = C_TO_K(temp_c); + +} +DECLARE_HOOK(HOOK_SECOND, tmp411_temp_sensor_poll, HOOK_PRIO_TEMP_SENSOR); + +#ifdef CONFIG_CMD_TEMP_SENSOR +static void print_temps( + const char *name, + const int tmp411_temp_reg, + const int tmp411_therm_limit_reg, + const int tmp411_high_limit_reg, + const int tmp411_low_limit_reg) +{ + int value; + + if (!has_power()) { + ccprintf(" TMP411 is shutdown\n"); + return; + } + + ccprintf("%s:\n", name); + + if (get_temp(tmp411_temp_reg, &value) == EC_SUCCESS) + ccprintf(" Temp %3dC\n", value); + + if (get_temp(tmp411_therm_limit_reg, &value) == EC_SUCCESS) + ccprintf(" Therm Trip %3dC\n", value); + + if (get_temp(tmp411_high_limit_reg, &value) == EC_SUCCESS) + ccprintf(" High Alarm %3dC\n", value); + + if (get_temp(tmp411_low_limit_reg, &value) == EC_SUCCESS) + ccprintf(" Low Alarm %3dC\n", value); +} + +static int print_status(void) +{ + int value; + + print_temps("Local", TMP411_LOCAL, + TMP411_LOCAL_THERM_LIMIT, + TMP411_LOCAL_HIGH_LIMIT_R, + TMP411_LOCAL_LOW_LIMIT_R); + + print_temps("Remote1", TMP411_REMOTE1, + TMP411_REMOTE1_THERM_LIMIT, + TMP411_REMOTE1_HIGH_LIMIT_R, + TMP411_REMOTE1_LOW_LIMIT_R); + + ccprintf("\n"); + + if (raw_read8(TMP411_STATUS_R, &value) == EC_SUCCESS) + ccprintf("STATUS: %08b\n", value); + + if (raw_read8(TMP411_CONFIGURATION1_R, &value) == EC_SUCCESS) + ccprintf("CONFIG1: %08b\n", value); + + return EC_SUCCESS; +} + +static int command_tmp411(int argc, char **argv) +{ + char *command; + char *e; + char *power; + int data; + int offset; + int rv; + + /* handle "power" command before checking the power status. */ + if ((argc == 3) && !strcasecmp(argv[1], "power")) { + power = argv[2]; + if (!strncasecmp(power, "on", sizeof("on"))) { + rv = tmp411_set_power(TMP411_POWER_ON); + if (!rv) + print_status(); + } else if (!strncasecmp(power, "off", sizeof("off"))) + rv = tmp411_set_power(TMP411_POWER_OFF); + else + return EC_ERROR_PARAM2; + ccprintf("Set TMP411 %s\n", power); + return 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 "tmp411 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 = tmp411_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(tmp411, command_tmp411, + "[settemp|setbyte <offset> <value>] or [getbyte <offset>] or" + "[power <on|off>]. " + "Temps in Celsius.", + "Print tmp411 temp sensor status or set parameters."); +#endif + +int tmp411_set_power(enum tmp411_power_state power_on) +{ +#ifndef CONFIG_TEMP_SENSOR_POWER_GPIO + uint8_t shutdown = (power_on == TMP411_POWER_OFF) ? 1 : 0; + + return tmp411_shutdown(shutdown); +#else + gpio_set_level(CONFIG_TEMP_SENSOR_POWER_GPIO, power_on); + return EC_SUCCESS; +#endif +} + diff --git a/driver/temp_sensor/tmp411.h b/driver/temp_sensor/tmp411.h new file mode 100644 index 0000000000..a0ad95a5d8 --- /dev/null +++ b/driver/temp_sensor/tmp411.h @@ -0,0 +1,140 @@ +/* Copyright (c) 2017 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. + */ + +/* TMP411 temperature sensor module for Chrome EC */ + +#ifndef __CROS_EC_TMP411_H +#define __CROS_EC_TMP411_H + +#define TMP411_I2C_ADDR 0x98 /* 7-bit address is 0x4C */ + +#define TMP411_IDX_LOCAL 0 +#define TMP411_IDX_REMOTE1 1 +#define TMP411_IDX_REMOTE2 2 + +/* Chip-specific registers */ +#define TMP411_LOCAL 0x00 +#define TMP411_REMOTE1 0x01 +#define TMP411_STATUS_R 0x02 +#define TMP411_CONFIGURATION1_R 0x03 +#define TMP411_CONVERSION_RATE_R 0x04 +#define TMP411_LOCAL_HIGH_LIMIT_R 0x05 +#define TMP411_LOCAL_LOW_LIMIT_R 0x06 +#define TMP411_REMOTE1_HIGH_LIMIT_R 0x07 +#define TMP411_REMOTE1_LOW_LIMIT_R 0x08 +#define TMP411_CONFIGURATION1_W 0x09 +#define TMP411_CONVERSION_RATE_W 0x0a +#define TMP411_LOCAL_HIGH_LIMIT_W 0x0b +#define TMP411_LOCAL_LOW_LIMIT_W 0x0c +#define TMP411_REMOTE1_HIGH_LIMIT_W 0x0d +#define TMP411_REMOTE1_LOW_LIMIT_W 0x0e +#define TMP411_ONESHOT 0x0f +#define TMP411_REMOTE1_EXTD 0x10 +#define TMP411_REMOTE1_HIGH_LIMIT_EXTD 0x13 +#define TMP411_REMOTE1_LOW_LIMIT_EXTD 0x14 +#define TMP411_REMOTE2_HIGH_LIMIT_R 0x15 +#define TMP411_REMOTE2_HIGH_LIMIT_W 0x15 +#define TMP411_REMOTE2_LOW_LIMIT_R 0x16 +#define TMP411_REMOTE2_LOW_LIMIT_W 0x16 +#define TMP411_REMOTE2_HIGH_LIMIT_EXTD 0x17 +#define TMP411_REMOTE2_LOW_LIMIT_EXTD 0x18 +#define TMP411_REMOTE1_THERM_LIMIT 0x19 +#define TMP411_REMOTE2_THERM_LIMIT 0x1a +#define TMP411_STATUS_FAULT 0x1b +#define TMP411_CHANNEL_MASK 0x1f +#define TMP411_LOCAL_THERM_LIMIT 0x20 +#define TMP411_THERM_HYSTERESIS 0x21 +#define TMP411_CONSECUTIVE_ALERT 0x22 +#define TMP411_REMOTE2 0x23 +#define TMP411_REMOTE2_EXTD 0x24 +#define TMP411_BETA_RANGE_CH1 0x25 +#define TMP411_BETA_RANGE_CH2 0x26 +#define TMP411_NFACTOR_REMOTE1 0x27 +#define TMP411_NFACTOR_REMOTE2 0x28 +#define TMP411_LOCAL_EXTD 0x29 +#define TMP411_STATUS_LIMIT_HIGH 0x35 +#define TMP411_STATUS_LIMIT_LOW 0x36 +#define TMP411_STATUS_THERM 0x37 +#define TMP411_RESET_W 0xfc +#define TMP411_MANUFACTURER_ID 0xfe +#define TMP411_DEVICE_ID 0xff + +#define TMP411A_DEVICE_ID_VAL 0x12 +#define TMP411B_DEVICE_ID_VAL 0x13 +#define TMP411C_DEVICE_ID_VAL 0x10 +#define TMP411d_DEVICE_ID_VAL 0x12 + +/* Config register bits */ +#define TMP411_CONFIG1_TEMP_RANGE (1 << 2) +/* TMP411_CONFIG1_MODE bit is use to enable THERM mode */ +#define TMP411_CONFIG1_MODE (1 << 5) +#define TMP411_CONFIG1_RUN_L (1 << 6) +#define TMP411_CONFIG1_ALERT_MASK_L (1 << 7) + +/* Status register bits */ +#define TMP411_STATUS_TEMP_THERM_ALARM (1 << 1) +#define TMP411_STATUS_OPEN (1 << 2) +#define TMP411_STATUS_TEMP_LOW_ALARM (1 << 3) +#define TMP411_STATUS_TEMP_HIGH_ALARM (1 << 4) +#define TMP411_STATUS_LOCAL_TEMP_LOW_ALARM (1 << 5) +#define TMP411_STATUS_LOCAL_TEMP_HIGH_ALARM (1 << 6) +#define TMP411_STATUS_BUSY (1 << 7) + +/* Limits */ +#define TMP411_HYSTERESIS_HIGH_LIMIT 255 +#define TMP411_HYSTERESIS_LOW_LIMIT 0 + +enum tmp411_power_state { + TMP411_POWER_OFF = 0, + TMP411_POWER_ON, + TMP411_POWER_COUNT +}; + +enum tmp411_channel_id { + TMP411_CHANNEL_LOCAL, + TMP411_CHANNEL_REMOTE1, + + TMP411_CHANNEL_COUNT +}; + +/** + * 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 tmp411_get_val(int idx, int *temp_ptr); + +/** + * Power control function of tmp411 temperature sensor. + * + * @param power_on TMP411_POWER_ON: turn tmp411 sensor on. + * TMP411_POWER_OFF: shut tmp411 sensor down. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int tmp411_set_power(enum tmp411_power_state power_on); + +/* + * Set TMP411 ALERT#/THERM2# pin to THERM mode, and give a limit + * for a specific channel. + * + * @param channel specific a channel + * + * @param limit_c High limit temperature, default: 85C + * + * @param hysteresis Hysteresis temperature, default: 10C + * All channels share the same hysteresis + * + * In THERM mode, ALERT# pin will trigger(Low) by itself when any + * channel's temperature is greater( >= )than channel's limit_c, + * and release(High) by itself when channel's temperature is lower + * than (limit_c - hysteresis) + */ +int tmp411_set_therm_limit(int channel, int limit_c, int hysteresis); +#endif /* __CROS_EC_TMP411_H */ |