diff options
Diffstat (limited to 'chip/stm32/usb_power.c')
-rw-r--r-- | chip/stm32/usb_power.c | 278 |
1 files changed, 130 insertions, 148 deletions
diff --git a/chip/stm32/usb_power.c b/chip/stm32/usb_power.c index 3e159d646f..589767f15c 100644 --- a/chip/stm32/usb_power.c +++ b/chip/stm32/usb_power.c @@ -1,4 +1,4 @@ -/* Copyright 2016 The Chromium OS Authors. All rights reserved. +/* Copyright 2016 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -14,7 +14,7 @@ #include "usb_power.h" #include "util.h" -#define CPRINTS(format, args...) cprints(CC_I2C, format, ## args) +#define CPRINTS(format, args...) cprints(CC_I2C, format, ##args) static int usb_power_init_inas(struct usb_power_config const *config); static int usb_power_read(struct usb_power_config const *config); @@ -42,16 +42,15 @@ void usb_power_deferred_tx(struct usb_power_config const *config) state->reports_xmit_active = state->reports_tail; /* Wait for the next command */ - usb_read_ep(config->endpoint, - config->ep->out_databuffer_max, - config->ep->out_databuffer); + usb_read_ep(config->endpoint, config->ep->out_databuffer_max, + config->ep->out_databuffer); return; } } /* Reset stream */ void usb_power_event(struct usb_power_config const *config, - enum usb_ep_event evt) + enum usb_ep_event evt) { if (evt != USB_EVENT_RESET) return; @@ -68,15 +67,15 @@ void usb_power_event(struct usb_power_config const *config, hook_call_deferred(config->ep->tx_deferred, 0); } - /* Write one or more power records to USB */ static int usb_power_write_line(struct usb_power_config const *config) { struct usb_power_state *state = config->state; - struct usb_power_report *r = (struct usb_power_report *)( - state->reports_data_area + - (USB_POWER_RECORD_SIZE(state->ina_count) - * state->reports_tail)); + struct usb_power_report *r = + (struct usb_power_report *)(state->reports_data_area + + (USB_POWER_RECORD_SIZE( + state->ina_count) * + state->reports_tail)); /* status + size + timestamps + power list */ size_t bytes = USB_POWER_RECORD_SIZE(state->ina_count); @@ -89,12 +88,12 @@ static int usb_power_write_line(struct usb_power_config const *config) recordcount = config->state->reports_head - config->state->reports_tail; else - recordcount = state->max_cached - - config->state->reports_tail; + recordcount = + state->max_cached - config->state->reports_tail; state->reports_xmit_active = state->reports_tail; - state->reports_tail = (state->reports_tail + recordcount) % - state->max_cached; + state->reports_tail = + (state->reports_tail + recordcount) % state->max_cached; usb_write_ep(config->endpoint, bytes * recordcount, r); return bytes; @@ -103,7 +102,6 @@ static int usb_power_write_line(struct usb_power_config const *config) return 0; } - static int usb_power_state_reset(struct usb_power_config const *config) { struct usb_power_state *state = config->state; @@ -117,7 +115,6 @@ static int usb_power_state_reset(struct usb_power_config const *config) return USB_POWER_SUCCESS; } - static int usb_power_state_stop(struct usb_power_config const *config) { struct usb_power_state *state = config->state; @@ -137,8 +134,6 @@ static int usb_power_state_stop(struct usb_power_config const *config) return USB_POWER_SUCCESS; } - - static int usb_power_state_start(struct usb_power_config const *config, union usb_power_command_data *cmd, int count) { @@ -182,13 +177,12 @@ static int usb_power_state_start(struct usb_power_config const *config, return USB_POWER_SUCCESS; } - static int usb_power_state_settime(struct usb_power_config const *config, - union usb_power_command_data *cmd, int count) + union usb_power_command_data *cmd, int count) { if (count != sizeof(struct usb_power_command_settime)) { - CPRINTS("[SETTIME] Error: count %d is not %d", - (int)count, sizeof(struct usb_power_command_settime)); + CPRINTS("[SETTIME] Error: count %d is not %d", (int)count, + sizeof(struct usb_power_command_settime)); return USB_POWER_ERROR_READ_SIZE; } @@ -201,9 +195,8 @@ static int usb_power_state_settime(struct usb_power_config const *config, return USB_POWER_SUCCESS; } - static int usb_power_state_addina(struct usb_power_config const *config, - union usb_power_command_data *cmd, int count) + union usb_power_command_data *cmd, int count) { struct usb_power_state *state = config->state; struct usb_power_ina_cfg *ina; @@ -217,8 +210,8 @@ static int usb_power_state_addina(struct usb_power_config const *config, } if (count != sizeof(struct usb_power_command_addina)) { - CPRINTS("[ADDINA] Error count %d is not %d", - (int)count, sizeof(struct usb_power_command_addina)); + CPRINTS("[ADDINA] Error count %d is not %d", (int)count, + sizeof(struct usb_power_command_addina)); return USB_POWER_ERROR_READ_SIZE; } @@ -286,8 +279,8 @@ static int usb_power_read(struct usb_power_config const *config) * If there is a USB packet waiting we process it and generate a * response. */ - uint8_t count = rx_ep_pending(config->endpoint); - uint8_t result = USB_POWER_SUCCESS; + uint8_t count = rx_ep_pending(config->endpoint); + uint8_t result = USB_POWER_SUCCESS; union usb_power_command_data *cmd = (union usb_power_command_data *)config->ep->out_databuffer; @@ -314,14 +307,14 @@ static int usb_power_read(struct usb_power_config const *config) result = usb_power_state_start(config, cmd, count); if (result == USB_POWER_SUCCESS) { /* Send back actual integration time. */ - ep->in_databuffer[1] = - (state->integration_us >> 0) & 0xff; - ep->in_databuffer[2] = - (state->integration_us >> 8) & 0xff; - ep->in_databuffer[3] = - (state->integration_us >> 16) & 0xff; - ep->in_databuffer[4] = - (state->integration_us >> 24) & 0xff; + ep->in_databuffer[1] = (state->integration_us >> 0) & + 0xff; + ep->in_databuffer[2] = (state->integration_us >> 8) & + 0xff; + ep->in_databuffer[3] = (state->integration_us >> 16) & + 0xff; + ep->in_databuffer[4] = (state->integration_us >> 24) & + 0xff; in_msgsize += 4; } break; @@ -363,8 +356,6 @@ static int usb_power_read(struct usb_power_config const *config) return EC_SUCCESS; } - - /****************************************************************************** * INA231 interface. * List the registers and fields here. @@ -374,20 +365,19 @@ static int usb_power_read(struct usb_power_config const *config) #define INA231_REG_CONF 0 #define INA231_REG_RSHV 1 #define INA231_REG_BUSV 2 -#define INA231_REG_PWR 3 +#define INA231_REG_PWR 3 #define INA231_REG_CURR 4 -#define INA231_REG_CAL 5 -#define INA231_REG_EN 6 - - -#define INA231_CONF_AVG(val) (((int)(val & 0x7)) << 9) -#define INA231_CONF_BUS_TIME(val) (((int)(val & 0x7)) << 6) -#define INA231_CONF_SHUNT_TIME(val) (((int)(val & 0x7)) << 3) -#define INA231_CONF_MODE(val) (((int)(val & 0x7)) << 0) -#define INA231_MODE_OFF 0x0 -#define INA231_MODE_SHUNT 0x5 -#define INA231_MODE_BUS 0x6 -#define INA231_MODE_BOTH 0x7 +#define INA231_REG_CAL 5 +#define INA231_REG_EN 6 + +#define INA231_CONF_AVG(val) (((int)(val & 0x7)) << 9) +#define INA231_CONF_BUS_TIME(val) (((int)(val & 0x7)) << 6) +#define INA231_CONF_SHUNT_TIME(val) (((int)(val & 0x7)) << 3) +#define INA231_CONF_MODE(val) (((int)(val & 0x7)) << 0) +#define INA231_MODE_OFF 0x0 +#define INA231_MODE_SHUNT 0x5 +#define INA231_MODE_BUS 0x6 +#define INA231_MODE_BOTH 0x7 int reg_type_mapping(enum usb_power_ina_type ina_type) { @@ -411,36 +401,32 @@ uint16_t ina2xx_readagain(uint8_t port, uint16_t addr_flags) int res; uint16_t val; - res = i2c_xfer(port, addr_flags, - NULL, 0, (uint8_t *)&val, sizeof(uint16_t)); + res = i2c_xfer(port, addr_flags, NULL, 0, (uint8_t *)&val, + sizeof(uint16_t)); if (res) { - CPRINTS("INA2XX I2C readagain failed p:%d a:%02x", - (int)port, (int)I2C_STRIP_FLAGS(addr_flags)); + CPRINTS("INA2XX I2C readagain failed p:%d a:%02x", (int)port, + (int)I2C_STRIP_FLAGS(addr_flags)); return 0x0bad; } return (val >> 8) | ((val & 0xff) << 8); } - -uint16_t ina2xx_read(uint8_t port, uint16_t addr_flags, - uint8_t reg) +uint16_t ina2xx_read(uint8_t port, uint16_t addr_flags, uint8_t reg) { int res; int val; res = i2c_read16(port, addr_flags, reg, &val); if (res) { - CPRINTS("INA2XX I2C read failed p:%d a:%02x, r:%02x", - (int)port, (int)I2C_STRIP_FLAGS(addr_flags), - (int)reg); + CPRINTS("INA2XX I2C read failed p:%d a:%02x, r:%02x", (int)port, + (int)I2C_STRIP_FLAGS(addr_flags), (int)reg); return 0x0bad; } return (val >> 8) | ((val & 0xff) << 8); } -int ina2xx_write(uint8_t port, uint16_t addr_flags, - uint8_t reg, uint16_t val) +int ina2xx_write(uint8_t port, uint16_t addr_flags, uint8_t reg, uint16_t val) { int res; uint16_t be_val = (val >> 8) | ((val & 0xff) << 8); @@ -451,8 +437,6 @@ int ina2xx_write(uint8_t port, uint16_t addr_flags, return res; } - - /****************************************************************************** * Background tasks * @@ -462,11 +446,10 @@ int ina2xx_write(uint8_t port, uint16_t addr_flags, */ /* INA231 integration and averaging time presets, indexed by register value */ -#define NELEMS(x) (sizeof(x) / sizeof((x)[0])) -static const int average_settings[] = { - 1, 4, 16, 64, 128, 256, 512, 1024}; -static const int conversion_time_us[] = { - 140, 204, 332, 588, 1100, 2116, 4156, 8244}; +#define NELEMS(x) (sizeof(x) / sizeof((x)[0])) +static const int average_settings[] = { 1, 4, 16, 64, 128, 256, 512, 1024 }; +static const int conversion_time_us[] = { 140, 204, 332, 588, + 1100, 2116, 4156, 8244 }; static int usb_power_init_inas(struct usb_power_config const *config) { @@ -491,8 +474,7 @@ static int usb_power_init_inas(struct usb_power_config const *config) /* Find an averaging setting from the INA presets that fits. */ while (avg < (NELEMS(average_settings) - 1)) { if ((conversion_time_us[shunt_time] * - average_settings[avg + 1]) - > target_us) + average_settings[avg + 1]) > target_us) break; avg++; } @@ -507,15 +489,15 @@ static int usb_power_init_inas(struct usb_power_config const *config) #ifdef USB_POWER_VERBOSE { - int conf, cal; - - conf = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_CONF); - cal = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_CAL); - CPRINTS("[CAP] %d (%d,0x%02x): conf:%x, cal:%x", - i, ina->port, I2C_STRIP_FLAGS(ina->addr_flags), - conf, cal); + int conf, cal; + + conf = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_CONF); + cal = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_CAL); + CPRINTS("[CAP] %d (%d,0x%02x): conf:%x, cal:%x", i, + ina->port, I2C_STRIP_FLAGS(ina->addr_flags), + conf, cal); } #endif /* @@ -536,54 +518,53 @@ static int usb_power_init_inas(struct usb_power_config const *config) if (ina->scale == 0) return -1; value = (5120000 * 100) / (ina->scale * ina->rs); - ret = ina2xx_write(ina->port, ina->addr_flags, - INA231_REG_CAL, value); + ret = ina2xx_write(ina->port, ina->addr_flags, INA231_REG_CAL, + value); if (ret != EC_SUCCESS) { CPRINTS("[CAP] usb_power_init_inas CAL FAIL: %d", ret); return ret; } #ifdef USB_POWER_VERBOSE { - int actual; + int actual; - actual = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_CAL); - CPRINTS("[CAP] scale: %d uA/div, %d uW/div, cal:%x act:%x", - ina->scale / 100, ina->scale*25/100, value, actual); + actual = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_CAL); + CPRINTS("[CAP] scale: %d uA/div, %d uW/div, cal:%x act:%x", + ina->scale / 100, ina->scale * 25 / 100, value, + actual); } #endif /* Conversion time, shunt + bus, set average. */ value = INA231_CONF_MODE(INA231_MODE_BOTH) | INA231_CONF_SHUNT_TIME(shunt_time) | - INA231_CONF_BUS_TIME(shunt_time) | - INA231_CONF_AVG(avg); - ret = ina2xx_write(ina->port, ina->addr_flags, - INA231_REG_CONF, value); + INA231_CONF_BUS_TIME(shunt_time) | INA231_CONF_AVG(avg); + ret = ina2xx_write(ina->port, ina->addr_flags, INA231_REG_CONF, + value); if (ret != EC_SUCCESS) { CPRINTS("[CAP] usb_power_init_inas CONF FAIL: %d", ret); return ret; } #ifdef USB_POWER_VERBOSE { - int actual; + int actual; - actual = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_CONF); - CPRINTS("[CAP] %d (%d,0x%02x): conf:%x, act:%x", - i, ina->port, I2C_STRIP_FLAGS(ina->addr_flags), - value, actual); + actual = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_CONF); + CPRINTS("[CAP] %d (%d,0x%02x): conf:%x, act:%x", i, + ina->port, I2C_STRIP_FLAGS(ina->addr_flags), + value, actual); } #endif #ifdef USB_POWER_VERBOSE { - int busv_mv = - (ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_BUSV) - * 125) / 100; - - CPRINTS("[CAP] %d (%d,0x%02x): busv:%dmv", - i, ina->port, I2C_STRIP_FLAGS(ina->addr_flags), - busv_mv); + int busv_mv = (ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_BUSV) * + 125) / + 100; + + CPRINTS("[CAP] %d (%d,0x%02x): busv:%dmv", i, ina->port, + I2C_STRIP_FLAGS(ina->addr_flags), busv_mv); } #endif /* Initialize read from power register. This register address @@ -600,7 +581,6 @@ static int usb_power_init_inas(struct usb_power_config const *config) return EC_SUCCESS; } - /* * Read each INA's power integration measurement. * @@ -614,19 +594,20 @@ static int usb_power_get_samples(struct usb_power_config const *config) { uint64_t time = get_time().val; struct usb_power_state *state = config->state; - struct usb_power_report *r = (struct usb_power_report *)( - state->reports_data_area + - (USB_POWER_RECORD_SIZE(state->ina_count) - * state->reports_head)); + struct usb_power_report *r = + (struct usb_power_report *)(state->reports_data_area + + (USB_POWER_RECORD_SIZE( + state->ina_count) * + state->reports_head)); struct usb_power_ina_cfg *inas = state->ina_cfg; int i; /* TODO(nsanders): Would we prefer to evict oldest? */ - if (((state->reports_head + 1) % USB_POWER_MAX_CACHED(state->ina_count)) - == state->reports_xmit_active) { - CPRINTS("Overflow! h:%d a:%d t:%d (%d)", - state->reports_head, state->reports_xmit_active, - state->reports_tail, + if (((state->reports_head + 1) % + USB_POWER_MAX_CACHED(state->ina_count)) == + state->reports_xmit_active) { + CPRINTS("Overflow! h:%d a:%d t:%d (%d)", state->reports_head, + state->reports_xmit_active, state->reports_tail, USB_POWER_MAX_CACHED(state->ina_count)); return USB_POWER_ERROR_OVERFLOW; } @@ -650,47 +631,48 @@ static int usb_power_get_samples(struct usb_power_config const *config) */ if (ina->shared) regval = ina2xx_read(ina->port, ina->addr_flags, - reg_type_mapping(ina->type)); + reg_type_mapping(ina->type)); else - regval = ina2xx_readagain(ina->port, - ina->addr_flags); + regval = ina2xx_readagain(ina->port, ina->addr_flags); r->power[i] = regval; #ifdef USB_POWER_VERBOSE { - int current; - int power; - int voltage; - int bvoltage; - - voltage = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_RSHV); - bvoltage = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_BUSV); - current = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_CURR); - power = ina2xx_read(ina->port, ina->addr_flags, - INA231_REG_PWR); - { - int uV = ((int)voltage * 25) / 10; - int mV = ((int)bvoltage * 125) / 100; - int uA = (uV * 1000) / ina->rs; - int CuA = (((int)current * ina->scale) / 100); - int uW = (((int)power * ina->scale*25)/100); - - CPRINTS("[CAP] %d (%d,0x%02x): %dmV / %dmO = %dmA", - i, ina->port, I2C_STRIP_FLAGS(ina->addr_flags), - uV/1000, ina->rs, uA/1000); - CPRINTS("[CAP] %duV %dmV %duA %dCuA " - "%duW v:%04x, b:%04x, p:%04x", - uV, mV, uA, CuA, uW, voltage, bvoltage, power); - } + int current; + int power; + int voltage; + int bvoltage; + + voltage = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_RSHV); + bvoltage = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_BUSV); + current = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_CURR); + power = ina2xx_read(ina->port, ina->addr_flags, + INA231_REG_PWR); + { + int uV = ((int)voltage * 25) / 10; + int mV = ((int)bvoltage * 125) / 100; + int uA = (uV * 1000) / ina->rs; + int CuA = (((int)current * ina->scale) / 100); + int uW = (((int)power * ina->scale * 25) / 100); + + CPRINTS("[CAP] %d (%d,0x%02x): %dmV / %dmO = %dmA", + i, ina->port, + I2C_STRIP_FLAGS(ina->addr_flags), + uV / 1000, ina->rs, uA / 1000); + CPRINTS("[CAP] %duV %dmV %duA %dCuA " + "%duW v:%04x, b:%04x, p:%04x", + uV, mV, uA, CuA, uW, voltage, bvoltage, + power); + } } #endif } /* Mark this slot as used. */ state->reports_head = (state->reports_head + 1) % - USB_POWER_MAX_CACHED(state->ina_count); + USB_POWER_MAX_CACHED(state->ina_count); return EC_SUCCESS; } |