Skip to content

Commit 695e5ea

Browse files
James Courtier-DuttonJames Courtier-Dutton
authored andcommitted
Add the ectool "charge_command_charge_get_regs".
This is to obtain some more Volt/Current/Watts meter readings from around the charger on the mainboard so we can watch the power flow through the charger and battery and cpu. Signed-off-by: James Courtier-Dutton <james@superbug.co.uk>
1 parent 8476f8b commit 695e5ea

6 files changed

Lines changed: 161 additions & 0 deletions

File tree

common/charge_state.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "hooks.h"
2424
#include "host_command.h"
2525
#include "i2c.h"
26+
#include "driver/ina2xx.h"
2627
#include "math_util.h"
2728
#include "power.h"
2829
#include "printf.h"
@@ -2168,6 +2169,34 @@ charge_command_charge_control3(struct host_cmd_handler_args *args)
21682169
DECLARE_HOST_COMMAND(EC_CMD_CHARGE_CONTROL3, charge_command_charge_control3,
21692170
EC_VER_MASK(3));
21702171

2172+
static enum ec_status
2173+
charge_command_charge_get_regs(struct host_cmd_handler_args *args)
2174+
{
2175+
const struct ec_params_charge_get_regs *p = args->params;
2176+
struct ec_response_charge_get_regs *r = args->response;
2177+
int rv;
2178+
int size = p->size; // This has to be 33
2179+
CPRINTS("p->size = %d\n", p->size);
2180+
int chgnum = p->chgnum;
2181+
CPRINTS("p->chgnum = %d\n", p->chgnum);
2182+
#pragma GCC diagnostic push
2183+
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
2184+
// Its the first variable in the structure, so it should be aligned.
2185+
uint32_t *regs = r->regs;
2186+
CPRINTS("regs = %p, r->regs = %p\n", regs, r->regs);
2187+
#pragma GCC diagnostic pop
2188+
rv = chg_chips[chgnum].drv->dump_registers_get(chgnum, regs, size);
2189+
CPRINTS("rv = %d\n", rv);
2190+
r->size = size;
2191+
r->pd_mV = ina2xx_get_voltage(0);
2192+
r->pd_mA = ina2xx_get_current2(0);
2193+
args->response_size = sizeof(*r);
2194+
2195+
return rv;
2196+
}
2197+
DECLARE_HOST_COMMAND(EC_CMD_CHARGE_GET_REGS, charge_command_charge_get_regs,
2198+
EC_VER_MASK(1));
2199+
21712200
static enum ec_status
21722201
charge_command_current_limit(struct host_cmd_handler_args *args)
21732202
{

driver/charger/isl9241.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,99 @@ static void command_isl9241_dump(int chgnum)
14541454
dump_reg_range(chgnum, 0x90, 0x91);
14551455
dump_reg_range(chgnum, 0xFE, 0xFF);
14561456
}
1457+
static enum ec_error_list command_isl9241_dump_get(int chgnum, uint32_t *regs, int size) {
1458+
int index = 0;
1459+
int reg;
1460+
int regval;
1461+
int rv;
1462+
if (size != 33) {
1463+
ccprintf("ERROR: Size != 31\n");
1464+
return EC_ERROR_PARAM2;
1465+
}
1466+
if (!regs) {
1467+
ccprintf("ERROR: Regs = NULL\n");
1468+
return EC_ERROR_PARAM1;
1469+
}
1470+
// 2
1471+
for (reg = 0x14; reg <= 0x15; reg++) {
1472+
rv = isl9241_read(chgnum, reg, &regval);
1473+
if (!rv)
1474+
ccprintf("0x%04x\n", regval);
1475+
else
1476+
ccprintf("ERR (%d)\n", rv);
1477+
regs[index] = regval;
1478+
index++;
1479+
}
1480+
// 8
1481+
for (reg = 0x38; reg <= 0x40; reg++) {
1482+
rv = isl9241_read(chgnum, reg, &regval);
1483+
if (!rv)
1484+
ccprintf("0x%04x\n", regval);
1485+
else
1486+
ccprintf("ERR (%d)\n", rv);
1487+
regs[index] = regval;
1488+
index++;
1489+
}
1490+
// 1
1491+
for (reg = 0x43; reg <= 0x43; reg++) {
1492+
rv = isl9241_read(chgnum, reg, &regval);
1493+
if (!rv)
1494+
ccprintf("0x%04x\n", regval);
1495+
else
1496+
ccprintf("ERR (%d)\n", rv);
1497+
regs[index] = regval;
1498+
index++;
1499+
}
1500+
1501+
// 8
1502+
for (reg = 0x47; reg <= 0x4F; reg++) {
1503+
rv = isl9241_read(chgnum, reg, &regval);
1504+
if (!rv)
1505+
ccprintf("0x%04x\n", regval);
1506+
else
1507+
ccprintf("ERR (%d)\n", rv);
1508+
regs[index] = regval;
1509+
index++;
1510+
}
1511+
1512+
// 8
1513+
for (reg = 0x80; reg <= 0x87; reg++) {
1514+
rv = isl9241_read(chgnum, reg, &regval);
1515+
if (!rv)
1516+
ccprintf("0x%04x\n", regval);
1517+
else
1518+
ccprintf("ERR (%d)\n", rv);
1519+
regs[index] = regval;
1520+
index++;
1521+
}
1522+
1523+
// 2
1524+
for (reg = 0x90; reg <= 0x91; reg++) {
1525+
rv = isl9241_read(chgnum, reg, &regval);
1526+
if (!rv)
1527+
ccprintf("0x%04x\n", regval);
1528+
else
1529+
ccprintf("ERR (%d)\n", rv);
1530+
regs[index] = regval;
1531+
index++;
1532+
}
1533+
1534+
// 2
1535+
for (reg = 0xFE; reg <= 0xFF; reg++) {
1536+
rv = isl9241_read(chgnum, reg, &regval);
1537+
if (!rv)
1538+
ccprintf("0x%04x\n", regval);
1539+
else
1540+
ccprintf("ERR (%d)\n", rv);
1541+
regs[index] = regval;
1542+
index++;
1543+
}
1544+
ccprintf("index=%d\n", index);
1545+
// Total: 2 + 9 + 1 + 9 + 8 + 2 + 2 = 33
1546+
cflush();
1547+
return EC_SUCCESS;
1548+
}
1549+
14571550
#endif /* CONFIG_CMD_CHARGER_DUMP */
14581551

14591552
const struct charger_drv isl9241_drv = {
@@ -1487,6 +1580,7 @@ const struct charger_drv isl9241_drv = {
14871580
#endif
14881581
#ifdef CONFIG_CMD_CHARGER_DUMP
14891582
.dump_registers = &command_isl9241_dump,
1583+
.dump_registers_get = &command_isl9241_dump_get,
14901584
#endif
14911585
#ifdef CONFIG_CHARGER_DUMP_PROCHOT
14921586
.dump_prochot = &isl9241_dump_prochot_status,

driver/ina2xx.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "system.h"
1313
#include "timer.h"
1414
#include "util.h"
15+
#include "board_adc.h"
1516

1617
/* Console output macros */
1718
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ##args)
@@ -69,6 +70,21 @@ int ina2xx_get_current(uint8_t idx)
6970
return (int)curr;
7071
}
7172

73+
int ina2xx_get_current2(uint8_t idx)
74+
{
75+
static int shunt_register;
76+
int16_t sv = ina2xx_read(0, INA2XX_REG_SHUNT_VOLT);
77+
if (board_get_version() >= BOARD_VERSION_7)
78+
shunt_register = 10;
79+
else
80+
shunt_register = 5;
81+
int shunt = INA2XX_SHUNT_UV(sv);
82+
int curr = shunt / shunt_register;
83+
84+
return curr;
85+
}
86+
87+
7288
int ina2xx_get_power(uint8_t idx)
7389
{
7490
uint16_t pow = ina2xx_read(idx, INA2XX_REG_POWER);

driver/ina2xx.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ int ina2xx_get_voltage(uint8_t idx);
120120
/* Return current in milliAmps */
121121
int ina2xx_get_current(uint8_t idx);
122122

123+
/* Return shunt current in milliAmps */
124+
int ina2xx_get_current2(uint8_t idx);
125+
123126
/* Return power in milliWatts */
124127
int ina2xx_get_power(uint8_t idx);
125128

include/charger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct charger_drv {
163163

164164
/* Dumps charger registers */
165165
void (*dump_registers)(int chgnum);
166+
enum ec_error_list (*dump_registers_get)(int chgnum, uint32_t *reg, int size);
166167

167168
/* Dumps prochot status information */
168169
void (*dump_prochot)(int chgnum);

include/ec_commands.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,6 +4728,24 @@ struct ec_response_charge_control3 {
47284728
uint8_t reserved2;
47294729
} __ec_align4;
47304730

4731+
4732+
/* Charge get_regs commands */
4733+
4734+
#define EC_CMD_CHARGE_GET_REGS 0x3e97
4735+
#define EC_VER_CHARGE_GET_REGS 1
4736+
4737+
struct ec_params_charge_get_regs {
4738+
int size; /* enum charge_control_mode */
4739+
int chgnum; /* charger number */
4740+
} __ec_align4;
4741+
4742+
struct ec_response_charge_get_regs {
4743+
uint32_t regs[33];
4744+
int size; /* number of regs returned */
4745+
int pd_mV;
4746+
int pd_mA;
4747+
} __ec_align4;
4748+
47314749
/*****************************************************************************/
47324750

47334751
/* Snapshot console output buffer for use by EC_CMD_CONSOLE_READ. */

0 commit comments

Comments
 (0)