diff --git a/board/kunimitsu/board.c b/board/kunimitsu/board.c index 9601d86572..a173409c0d 100644 --- a/board/kunimitsu/board.c +++ b/board/kunimitsu/board.c @@ -102,8 +102,11 @@ const struct adc_t adc_channels[] = { [ADC_VBUS] = {"VBUS", 33000, 1024, 0, 1}, /* Adapter current output or battery discharging current */ [ADC_AMON_BMON] = {"AMON_BMON", 1, 1, 0, 3}, - /* System current consumption */ - [ADC_PSYS] = {"PSYS", 1, 1, 0, 4}, + /* + * System current consumption. Converted to mV, + * full ADC is equivalent to 100W + */ + [ADC_PSYS] = {"PSYS", 3000, 1024, 0, 4}, }; BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT); diff --git a/board/kunimitsu/board.h b/board/kunimitsu/board.h index 835058cb92..b9e3d1fcf5 100644 --- a/board/kunimitsu/board.h +++ b/board/kunimitsu/board.h @@ -29,15 +29,30 @@ #define CONFIG_CHARGER_ISL9237 #define CONFIG_CHARGER_ILIM_PIN_DISABLED #define CONFIG_CHARGER_INPUT_CURRENT 512 -#ifndef KUNIMITSU_BOARD_V3 +#ifndef BOARD_KUNIMITSU_V3 #define CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON 1 #endif +#define CONFIG_CHARGER_PSYS #define CONFIG_CHARGER_SENSE_RESISTOR 10 #define CONFIG_CHARGER_SENSE_RESISTOR_AC 20 -#ifndef KUNIMITSU_BOARD_V3 +#ifndef BOARD_KUNIMITSU_V3 #define CONFIG_TRICKLE_CHARGING #endif +#ifndef BOARD_KUNIMITSU_V3 +/* PSYS register 8.25KOhm */ +#define CHARGER_PSYS_REGISTER 8250 +#else +/* PSYS register 8.6KOhm */ +#define CHARGER_PSYS_REGISTER 8600 +#endif +#define CONFIG_CMD_PSYS +/* + * PSYS gain = 1 / (PYSY register * (1.44 or 0.36) uA/W) + */ +#define ISL9237_C2_PSYS_GAIN_1_44 (100000000ul / (CHARGER_PSYS_REGISTER * 144)) +#define ISL9237_C2_PSYS_GAIN_0_36 (100000000ul / (CHARGER_PSYS_REGISTER * 36)) + #define CONFIG_CHIPSET_SKYLAKE #define CONFIG_CLOCK_CRYSTAL #undef CONFIG_DEBUG_ASSERT diff --git a/driver/charger/isl9237.c b/driver/charger/isl9237.c index 4df9e53e48..0afb598dda 100644 --- a/driver/charger/isl9237.c +++ b/driver/charger/isl9237.c @@ -11,8 +11,10 @@ #include "charger.h" #include "console.h" #include "common.h" +#include "hooks.h" #include "i2c.h" #include "isl9237.h" +#include "timer.h" #include "util.h" #define DEFAULT_R_AC 20 @@ -243,6 +245,83 @@ int charger_discharge_on_ac(int enable) return raw_write16(ISL9237_REG_CONTROL1, control1); } +#ifdef CONFIG_CHARGER_PSYS +static void charger_enable_psys(void) +{ + int val; + + /* + * enable system power monitor PSYS function + */ + if (!raw_read16(ISL9237_REG_CONTROL1, &val)) { + val |= ISL9237_C1_ENABLE_PSYS; + raw_write16(ISL9237_REG_CONTROL1, val); + } +} +DECLARE_HOOK(HOOK_CHIPSET_STARTUP, charger_enable_psys, HOOK_PRIO_DEFAULT); + +static void charger_disable_psys(void) +{ + int val; + + /* + * disable system power monitor PSYS function + */ + if (!raw_read16(ISL9237_REG_CONTROL1, &val)) { + val &= ~ISL9237_C1_ENABLE_PSYS; + raw_write16(ISL9237_REG_CONTROL1, val); + } +} +DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, charger_disable_psys, HOOK_PRIO_DEFAULT); + +#ifdef CONFIG_CMD_PSYS +#define PSYS_ADC_READ_COUNT 100 +static int charger_get_system_power(void) +{ + int adc = 0; + int i; + int ret; + int val; + + ret = raw_read16(ISL9237_REG_CONTROL2, &val); + if (ret) + return ret; + + /* Read ADC */ + for (i = 0; i < PSYS_ADC_READ_COUNT; i++) { + adc += adc_read_channel(ADC_PSYS); + usleep(10); + } + + /* + * Calculate the power in mW (Power = adc * gain) + * + * System power monitor PSYS output gain + * [0]: 0 = 1.44 uA/W + * 1 = 0.36 uA/W + * + * Do not divide the constants first to ensure precision is not lost. + */ + if (val & ISL9237_C2_PSYS_GAIN) + return ((adc * ISL9237_C2_PSYS_GAIN_0_36) / + PSYS_ADC_READ_COUNT); + else + return ((adc * ISL9237_C2_PSYS_GAIN_1_44) / + PSYS_ADC_READ_COUNT); +} + +static int console_command_psys(int argc, char **argv) +{ + CPRINTF("system power = %d mW\n", charger_get_system_power()); + return 0; +} +DECLARE_CONSOLE_COMMAND(psys, console_command_psys, + NULL, + "Get the system power in mW", + NULL); +#endif /* CONFIG_CMD_PSYS */ +#endif /* CONFIG_CHARGER_PSYS */ + #ifdef CONFIG_CHARGER_ADC_AMON_BMON /** * Get charger AMON and BMON current. diff --git a/driver/charger/isl9237.h b/driver/charger/isl9237.h index f0b352e4b0..4bca168ff4 100644 --- a/driver/charger/isl9237.h +++ b/driver/charger/isl9237.h @@ -194,8 +194,7 @@ #define ISL9237_C2_WOC_OFF (1 << 1) /* Control2: PSYS gain in uA/W */ -#define ISL9237_C2_PSYS_GAIN_1_44 0 -#define ISL9237_C2_PSYS_GAIN_0_36 1 +#define ISL9237_C2_PSYS_GAIN (1 << 0) /* OTG voltage limit in mV, current limit in mA */ #define ISL9237_OTG_VOLTAGE_MIN 4864 diff --git a/include/config.h b/include/config.h index c5264c57b7..f87afc8f6b 100644 --- a/include/config.h +++ b/include/config.h @@ -341,6 +341,9 @@ */ #undef CONFIG_CHARGER_CURRENT_LIMIT +/* Enable/disable system power monitor PSYS function */ +#undef CONFIG_CHARGER_PSYS + /* * Board specific charging current termination limit, in mA. If defined and * charger supports setting termination current it should be set during charger @@ -502,6 +505,7 @@ #define CONFIG_CMD_POWER_AP #define CONFIG_CMD_POWERINDEBUG #undef CONFIG_CMD_POWERLED +#undef CONFIG_CMD_PSYS #undef CONFIG_CMD_RTC_ALARM #undef CONFIG_CMD_SCRATCHPAD #define CONFIG_CMD_SHMEM