driver/temp_sensor: Add power control API for TMP432

Some boards didn't define the CONFIG_TEMP_SENSOR_POWER_GPIO (such as: Oak),
Due to the hardware design, the power of temp sensor is always on.
But, we can enable/disable the temperature measurement circuitry of tmp432 by
setup the shutdown (SD) bit. Add a new API: tmp432_set_power() to let upper
layer to control the power of tmp432 by SW approach for power saving.

BRANCH=none
BUG=chrome-os-partner:44170
TEST=manual
1. make BOARD=oak -j
2. Turn off the TMP432:
   > tmp432 power off
3. check whether tmp432 is shutdown:
   > tmp432
   ERROR: Temp sensor not powered.
   Not Powered
4. Turn on the TMP432:
   > tmp432 power on
5. check whether tmp432 is running:
   > tmp432
   Local:
     Temp        29C
     Therm Trip  85C
     High Alarm  85C
     Low Alarm    0C
   Remote1:
     Temp        27C
     Therm Trip  85C
     High Alarm  85C
     Low Alarm    0C
   Remote2:
     Temp        27C
     Therm Trip  85C
     High Alarm  85C
     Low Alarm    0C

   STATUS:  10000000
   CONFIG1: 00000000
   CONFIG2: 00111100

Change-Id: Iab95c4c0b0130baf3bce380a8132e08ded8d159e
Signed-off-by: Ben Lok <ben.lok@mediatek.com>
Reviewed-on: https://chromium-review.googlesource.com/295058
Reviewed-by: Rong Chang <rongchang@chromium.org>
This commit is contained in:
Ben Lok
2015-08-21 18:39:13 +08:00
committed by chrome-bot
parent 45c28eed9b
commit a572c882e4
2 changed files with 83 additions and 4 deletions

View File

@@ -16,6 +16,7 @@
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.
@@ -27,7 +28,7 @@ static int has_power(void)
#ifdef CONFIG_TEMP_SENSOR_POWER_GPIO
return gpio_get_level(CONFIG_TEMP_SENSOR_POWER_GPIO);
#else
return 1;
return !is_sensor_shutdown;
#endif
}
@@ -36,12 +37,10 @@ static int raw_read8(const int offset, int *data_ptr)
return i2c_read8(I2C_PORT_THERMAL, TMP432_I2C_ADDR, offset, data_ptr);
}
#ifdef CONFIG_CMD_TEMP_SENSOR
static int raw_write8(const int offset, int data)
{
return i2c_write8(I2C_PORT_THERMAL, TMP432_I2C_ADDR, offset, data);
}
#endif
static int get_temp(const int offset, int *temp_ptr)
{
@@ -88,6 +87,36 @@ int tmp432_get_val(int idx, int *temp_ptr)
return EC_SUCCESS;
}
static int tmp432_shutdown(uint8_t want_shutdown)
{
int ret, value;
if (want_shutdown == is_sensor_shutdown)
return EC_SUCCESS;
ret = raw_read8(TMP432_CONFIGURATION1_R, &value);
if (ret < 0) {
ccprintf("ERROR: Temp sensor I2C read8 error.\n");
return ret;
}
if (want_shutdown && !(value & TMP432_CONFIG1_RUN_L)) {
/* tmp432 is running, and want it to shutdown */
/* CONFIG REG1 BIT6: 0=Run, 1=Shutdown */
/* shut it down */
value |= TMP432_CONFIG1_RUN_L;
ret = raw_write8(TMP432_CONFIGURATION1_R, value);
} else if (!want_shutdown && (value & TMP432_CONFIG1_RUN_L)) {
/* tmp432 is shutdown, and want turn it on */
value &= ~TMP432_CONFIG1_RUN_L;
ret = raw_write8(TMP432_CONFIGURATION1_R, value);
}
/* else, the current setting is exactly what you want */
is_sensor_shutdown = want_shutdown;
return ret;
}
static void temp_sensor_poll(void)
{
int temp_c;
@@ -116,6 +145,11 @@ static void print_temps(
{
int value;
if (!has_power()) {
ccprintf(" TMP432 is shutdown\n");
return;
}
ccprintf("%s:\n", name);
if (get_temp(tmp432_temp_reg, &value) == EC_SUCCESS)
@@ -168,10 +202,27 @@ static int command_tmp432(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 = tmp432_set_power(TMP432_POWER_ON);
if (!rv)
print_status();
}
else if (!strncasecmp(power, "off", sizeof("off")))
rv = tmp432_set_power(TMP432_POWER_OFF);
else
return EC_ERROR_PARAM2;
ccprintf("Set TMP432 %s\n", power);
return rv;
}
if (!has_power()) {
ccprintf("ERROR: Temp sensor not powered.\n");
return EC_ERROR_NOT_POWERED;
@@ -217,7 +268,20 @@ static int command_tmp432(int argc, char **argv)
return rv;
}
DECLARE_CONSOLE_COMMAND(tmp432, command_tmp432,
"[settemp|setbyte <offset> <value>] or [getbyte <offset>]. "
"[settemp|setbyte <offset> <value>] or [getbyte <offset>] or"
"[power <on|off>]. "
"Temps in Celsius.",
"Print tmp432 temp sensor status or set parameters.", NULL);
#endif
int tmp432_set_power(enum tmp432_power_state power_on)
{
#ifndef CONFIG_TEMP_SENSOR_POWER_GPIO
uint8_t shutdown = (power_on == TMP432_POWER_OFF) ? 1 : 0;
return tmp432_shutdown(shutdown);
#else
gpio_set_level(CONFIG_TEMP_SENSOR_POWER_GPIO, power_on);
return EC_SUCCESS;
#endif
}

View File

@@ -82,6 +82,12 @@
#define TMP432_STATUS_TEMP_HIGH_ALARM (1 << 4)
#define TMP432_STATUS_BUSY (1 << 7)
enum tmp432_power_state {
TMP432_POWER_OFF = 0,
TMP432_POWER_ON,
TMP432_POWER_COUNT
};
/**
* Get the last polled value of a sensor.
*
@@ -93,4 +99,13 @@
*/
int tmp432_get_val(int idx, int *temp_ptr);
/**
* Power control function of tmp432 temperature sensor.
*
* @param power_on TMP432_POWER_ON: turn tmp432 sensor on.
* TMP432_POWER_OFF: shut tmp432 sensor down.
*
* @return EC_SUCCESS if successful, non-zero if error.
*/
int tmp432_set_power(enum tmp432_power_state power_on);
#endif /* __CROS_EC_TMP432_H */