refactor(core): improve composability of power manager
What changed, and why it matters
This is a large internal refactoring of the Trezor firmware's power-management code. It renames directories, splits the PMIC (power-management IC) interface into a core part and a charger-specific extension, and moves battery brownout logic into a chemistry-specific fuel-gauge module. There is no direct evidence this commit fixes a security vulnerability; it appears to be architectural cleanup to support future hardware variants (e.g., a power-latch board without a charger).
Treat as a normal code-quality/architecture review. Verify that the refactored brownout thresholds and charging current limits compile to identical values for existing T3W1 boards, and that the new `ensure!` checks correctly fail the build for misconfigured boards. No urgent security response is indicated by the available materials.
Security signals we found
Refactoring of safety-critical power/battery code (brownout, charging limits, thermal control).
Build-time hardening added: `ensure!` guards against zero or multiple PMIC/fuel-gauge selections and missing charger dependencies.
Brownout logic moved from generic policy into chemistry-specific `bat_eval_critical()` with explicit hysteresis design comments.
Chargerless PMIC path introduced: charging API returns `PM_REQUEST_REJECTED` and charging controller becomes a no-op when `USE_CHARGER` is undefined.
No changelog entry and no CVE/advisory references in commit message or diff.
Evidence from the diff
The commit refactors the power_manager subsystem: replaces the single power_manager_npm1300 feature with a generic managed policy plus separate pmic and fuel_gauge selectors; moves charger-specific APIs (pmic_set_charging, pmic_set_charging_limit, etc.) from io/pmic.h into a new pmic/pmic_charger.h gated by USE_CHARGER; relocates fuel-gauge/battery code under fuel_gauge/lifepo4/ and introduces bat_eval_critical() so brownout thresholds are chemistry-specific; updates board TOML files to use fuel_gauge = "io/fuel_gauge_lifepo4" instead of driver = "io/power_manager_npm1300"; and adds build-time ensure! checks that exactly one PMIC and one fuel gauge are selected. The functional behavior for the existing npm1300 + LiFePO4 configuration is intended to remain equivalent.
Changed components
core/embed/io/power_managercore/embed/io/power_manager/managed/*core/embed/io/power_manager/pmic/npm1300/*core/embed/io/power_manager/fuel_gauge/lifepo4/*core/embed/io/power_manager/wireless/stwlc38/*core/embed/models/T3W1/boards/revA.tomlcore/embed/models/T3W1/boards/revB.tomlcore/embed/models/T3W1/boards/revC.tomlcore/embed/projects/prodtest/cmd/prodtest_wpc.cInspect captured patch +313 / −98
### core/embed/io/Cargo.toml
@@ -59,7 +59,7 @@ nrf_auth = ["sec/nrf_auth"]
pmic = []
pmic_npm1300 = ["pmic", "sys/i2c_bus"]
power_manager = []
-power_manager_npm1300 = ["pmic_npm1300"]
+fuel_gauge_lifepo4 = []
production = ["sec/production"]
raspi_emulator = []
rgb_led = []
### core/embed/io/power_manager/build.rs
@@ -1,36 +1,65 @@
-use xbuild::{CLibrary, Result, bail_unsupported};
+use xbuild::{CLibrary, Result, bail_unsupported, ensure};
pub fn def_module(lib: &mut CLibrary) -> Result<()> {
lib.add_include("power_manager/inc");
+ // power_manager.h pulls in <io/suspend.h> for wakeup_flags_t; make that
+ // header reachable even on boards that don't compile the suspend module.
+ lib.add_include("suspend/inc");
// --- PMIC driver (low-level power IC) ---------------------------------
// Handled before the power_manager early-return below, because pmic-only
// builds (e.g. the boardloader) need the driver without the full backend.
// The board selects a concrete driver via the `[power_manager]` peripheral
- // `pmic = "io/pmic_..."` specifier; currently only npm1300 exists.
+ // `pmic = "io/pmic_..."` specifier.
+ //
+ // Selection is a table + "exactly one" check rather than a chain of
+ // independent `if`s, so a mis-mapped or duplicate feature is a clear build
+ // error instead of two drivers compiling into duplicate symbols.
if cfg!(feature = "pmic") {
lib.add_define("USE_PMIC", Some("1"));
- }
- if cfg!(feature = "pmic_npm1300") {
- // npm1300 is an STM32U5 part with no emulator implementation.
+ // (feature name, hardware source) for each enabled concrete PMIC
+ // driver. A new driver adds its own arm here.
+ let mut drivers: Vec<(&str, &str)> = Vec::new();
+ if cfg!(feature = "pmic_npm1300") {
+ drivers.push(("pmic_npm1300", "power_manager/pmic/npm1300/npm1300.c"));
+ }
+
+ ensure!(
+ drivers.len() == 1,
+ "power_manager: exactly one PMIC driver must be selected \
+ (set `pmic = \"io/pmic_...\"` in the board's [power_manager] \
+ section), found {}: {:?}",
+ drivers.len(),
+ drivers.iter().map(|(f, _)| *f).collect::<Vec<_>>()
+ );
+ let (_feature, source) = drivers[0];
+
+ // The concrete PMIC drivers are STM32U5 parts with no emulator build.
if cfg!(feature = "emulator") {
// no hardware PMIC in the emulator
} else if cfg!(feature = "mcu_stm32u5") {
- lib.add_source("power_manager/npm1300/npm1300.c");
+ lib.add_source(source);
} else {
bail_unsupported!();
}
+
+ // Charger capability of the selected PMIC. Gates the `managed` policy's
+ // charging controller (see USE_CHARGER in power_monitoring.c). npm1300
+ // has an integrated charger; a chargerless PMIC (npm2100, a bare GPIO
+ // power latch) simply doesn't define it.
+ if cfg!(feature = "pmic_npm1300") {
+ lib.add_define("USE_CHARGER", Some("1"));
+ }
}
if cfg!(not(feature = "power_manager")) {
- // if only PMIC is needed, we don't need the rest of
- //the power manager sources
+ // Boardloader and similar only need the PMIC driver, not the rest of
+ // the power manager.
return Ok(());
}
lib.add_define("USE_POWER_MANAGER", Some("1"));
-
lib.add_source("power_manager/power_manager_poll.c");
// --- Optional wireless charger ----------------------------------------
@@ -39,30 +68,55 @@ pub fn def_module(lib: &mut CLibrary) -> Result<()> {
lib.add_define("USE_WIRELESS_CHARGER", Some("1"));
if cfg!(feature = "mcu_stm32u5") {
lib.add_sources([
- "power_manager/stwlc38/stwlc38.c",
- "power_manager/stwlc38/stwlc38_patching.c",
+ "power_manager/wireless/stwlc38/stwlc38.c",
+ "power_manager/wireless/stwlc38/stwlc38_patching.c",
]);
}
}
// --- Power manager backend --------------------------------------------
- // The board selects a backend via the `[power_manager] driver = "io/..."`
- // specifier. The full npm1300 backend (charging state machine + fuel gauge)
- // is one option; future boards (e.g. a GPIO power latch) plug in their own
- // minimal backend here.
+ // There is a single hardware policy - `managed` - for every PMIC. It
+ // implements power_manager.h on top of the core PMIC interface and gates
+ // charger-only logic behind USE_CHARGER (set above per PMIC capability).
+ // A latch board simply runs `managed` with USE_CHARGER off.
+ //
+ // There is therefore no per-backend feature: the board selects only a PMIC
+ // (`pmic = "io/pmic_..."`), and the project opting into `power_manager`
+ // (this point in the code) is enough to build the policy on top of it. The
+ // only requirement is that a PMIC driver was actually selected.
+ //
+ // The emulator is a special case: it reimplements power_manager.h on the
+ // host (SDL) and takes precedence over whatever hardware backend the model
+ // nominally enables.
if cfg!(feature = "emulator") {
lib.add_source("power_manager/unix/power_manager.c");
- } else if cfg!(feature = "power_manager_npm1300") {
+ } else {
+ ensure!(
+ cfg!(feature = "pmic"),
+ "power_manager: a PMIC driver must be selected \
+ (set `pmic = \"io/pmic_...\"` in the board's [power_manager] section)"
+ );
lib.add_sources([
- "power_manager/npm1300/power_manager.c",
- "power_manager/npm1300/power_monitoring.c",
- "power_manager/npm1300/power_states.c",
- "power_manager/battery/battery.c",
- "power_manager/battery/fuel_gauge.c",
- "power_manager/battery/battery_model.c",
+ "power_manager/managed/power_manager.c",
+ "power_manager/managed/power_monitoring.c",
+ "power_manager/managed/power_states.c",
+ ]);
+
+ // Fuel gauge (SoC estimator). The `managed` policy speaks only to the
+ // chemistry-neutral fuel_gauge/battery.h interface; the board picks a
+ // concrete implementation via `fuel_gauge = "io/fuel_gauge_..."`, an axis
+ // mirroring the PMIC selector above. LiFePO4 is the only impl today.
+ ensure!(
+ cfg!(feature = "fuel_gauge_lifepo4"),
+ "power_manager: a fuel gauge must be selected \
+ (set `fuel_gauge = \"io/fuel_gauge_lifepo4\"` in the board's \
+ [power_manager] section)"
+ );
+ lib.add_sources([
+ "power_manager/fuel_gauge/lifepo4/battery.c",
+ "power_manager/fuel_gauge/lifepo4/fuel_gauge.c",
+ "power_manager/fuel_gauge/lifepo4/battery_model.c",
]);
- } else {
- bail_unsupported!();
}
Ok(())
### core/embed/io/power_manager/fuel_gauge/battery.h
@@ -19,12 +19,20 @@
/**
* @file battery.h
- * @brief Battery management driver with Extended Kalman Filter fuel gauge
+ * @brief Fuel-gauge (battery state-of-charge) interface.
*
- * This driver provides battery state estimation using an Extended Kalman Filter
- * (EKF) based fuel gauge algorithm. It estimates the State of Charge (SOC) by
- * processing battery voltage, current, and temperature measurements along with
- * a battery model.
+ * Chemistry-neutral interface between the `managed` power-manager policy and a
+ * concrete fuel-gauge implementation. The board selects an implementation via
+ * `fuel_gauge = "io/fuel_gauge_..."`; the policy speaks only to this header and
+ * never reaches into an implementation's directory. Current implementations:
+ * - `fuel_gauge/lifepo4/` - Extended Kalman Filter estimator for LiFePO4
+ * cells (the reference implementation; `P` below is its error covariance).
+ * - `fuel_gauge/mock/` - trivial stub for boards with no gauged battery
+ * (e.g. a latch board); reports a fixed healthy state.
+ *
+ * `P` is treated as opaque estimator state by the policy: it is persisted to
+ * backup RAM and handed back verbatim, but never interpreted. An estimator that
+ * has no covariance (e.g. the mock) simply ignores it.
*
* ## Usage:
* 1. Initialize the driver with `bat_init()`
@@ -153,6 +161,34 @@ ts_t bat_fg_update(uint32_t dt_ms, float voltage_V, float current_mA,
ts_t bat_fg_compensate_soc(float* soc, uint32_t elapsed_s,
float avg_bat_current_mA, float avg_temp_C);
+/**
+ * @brief Evaluate the battery-critical (brownout) condition for this chemistry.
+ *
+ * Brownout protection is a *voltage-domain* concern and deliberately does NOT
+ * depend on fuel-gauge precision the chemistry may not be able to deliver. Each
+ * gauge implements the set/clear with hysteresis appropriate to its cell:
+ * - LiFePO4 (this impl): undervoltage sets it and snaps the SoC estimate to
+ * empty; recovery is SOC-based, because the flat discharge curve and large
+ * voltage relaxation make raw voltage misleading at rest, and the persisted
+ * SoC is what prevents a false boot after hibernation. Only a genuine SoC
+ * rise (recharge) clears it.
+ * - Primary cells (future, low-precision gauge): voltage hysteresis + a
+ * sustained-time debounce, ideally on a load-compensated voltage. There,
+ * "recovery" means the dip was a transient load sag, not a recharge - which
+ * needs no SoC precision.
+ *
+ * The caller handles the external-power (USB) override separately, as that is
+ * chemistry-agnostic.
+ *
+ * @param currently_critical Latched critical state (for hysteresis)
+ * @param voltage_V Latest battery voltage [V]
+ * @param current_mA Latest battery current [mA] (positive for discharge)
+ * @param temp_C Latest battery temperature [°C]
+ * @return New critical state
+ */
+bool bat_eval_critical(bool currently_critical, float voltage_V,
+ float current_mA, float temp_C);
+
/**
* @brief Fetch battery cycle count increment from the fuel gauge.
*
### core/embed/io/power_manager/fuel_gauge/lifepo4/battery.c
@@ -23,7 +23,7 @@
#include <sys/irq.h>
-#include "battery.h"
+#include "../battery.h"
#include "battery_model.h"
#include "fuel_gauge.h"
#include "math.h"
@@ -244,6 +244,35 @@ ts_t bat_fg_compensate_soc(float* soc, uint32_t elapsed_s,
return TS_OK;
}
+// LiFePO4 brownout thresholds. Kept with the chemistry, not the policy.
+#define BAT_UNDERVOLT_THR_V 3.0f // set critical below this
+#define BAT_CRITICAL_RECOVERY_SOC 0.02f // clear critical at/above this SoC
+
+bool bat_eval_critical(bool currently_critical, float voltage_V,
+ float current_mA, float temp_C) {
+ bat_driver_t* drv = &g_bat_driver;
+ (void)current_mA;
+ (void)temp_C;
+
+ // Set on undervoltage. Voltage is a reliable SoC indicator in the critical
+ // region (steep curve), so snap the estimate to empty - keeping the
+ // covariance - as bat_fg_set_soc() does.
+ if (voltage_V < BAT_UNDERVOLT_THR_V && !currently_critical) {
+ bat_fg_set_soc(0.0f, drv->fg_state.P);
+ return true;
+ }
+
+ // Recover only on a genuine SoC rise (recharge). Raw voltage rebounds on
+ // relaxation without the cell actually recovering, so a voltage-based clear
+ // would falsely un-latch at rest; the persisted SoC also guards boot across
+ // hibernation.
+ if (drv->fg_state.soc_latched >= BAT_CRITICAL_RECOVERY_SOC) {
+ return false;
+ }
+
+ return currently_critical;
+}
+
float bat_fetch_cycle_increment(void) {
bat_driver_t* drv = &g_bat_driver;
### core/embed/io/power_manager/fuel_gauge/lifepo4/battery_data/hcf343837ncz.h
[binary or diff unavailable]
### core/embed/io/power_manager/fuel_gauge/lifepo4/battery_data/jyhpfl333838.h
[binary or diff unavailable]
### core/embed/io/power_manager/fuel_gauge/lifepo4/battery_model.c
[binary or diff unavailable]
### core/embed/io/power_manager/fuel_gauge/lifepo4/battery_model.h
[binary or diff unavailable]
### core/embed/io/power_manager/fuel_gauge/lifepo4/fuel_gauge.c
[binary or diff unavailable]
### core/embed/io/power_manager/fuel_gauge/lifepo4/fuel_gauge.h
[binary or diff unavailable]
### core/embed/io/power_manager/inc/io/pmic.h
@@ -21,12 +21,12 @@
#include <trezor_types.h>
-// Charging current limits
-// - range of pmic is 32-800mA
-// - used battery limit is 180mA
-#define PMIC_CHARGING_LIMIT_MIN 32 // mA
-#define PMIC_CHARGING_LIMIT_MAX 180 // mA
-#define PMIC_CHARGING_LIMIT_DEFAULT 180 // mA
+// Core PMIC interface: init/measure/suspend/ship-mode - the surface every PMIC
+// driver implements (npm1300, npm2100, power latch, ...).
+//
+// The charger / buck-regulator surface (charging control, current limits, buck
+// mode) lives in pmic/pmic_charger.h and is implemented only by charger-capable
+// PMICs. See that header.
typedef struct {
// Battery voltage [V]
@@ -41,6 +41,10 @@ typedef struct {
float ntc_temp;
// Die temperature [°C]
float die_temp;
+ // --- Charger-specific fields ---------------------------------------------
+ // Populated only by charger-capable PMICs (npm1300); other drivers leave
+ // them zero. (A future cleanup may split these into a separate report; kept
+ // inline for now to avoid churning every consumer.)
// IBAT_MEAS_STATUS register value
// (for debugging purposes, see the datasheet)
uint8_t ibat_meas_status;
@@ -122,30 +126,5 @@ bool pmic_measure(pmic_report_callback_t callback, void* context);
// is stored in the `report` structure.
bool pmic_measure_sync(pmic_report_t* report);
-// Enables or disables the charging.
-//
-// The function returns `false` if the operation cannot be performed.
-bool pmic_set_charging(bool enable);
-
-// Sets the charging current limit [mA].
-//
-// The current value must be in the range defined by the
-// `NPM1300_CHARGING_LIMIT_MIN` and `NPM1300_CHARGING_LIMIT_MAX` constants.
-//
-// The function returns `false` if the operation cannot be performed.
-bool pmic_set_charging_limit(int i_charge);
-
-// Gets the charging current limit [mA].
-int pmic_get_charging_limit(void);
-
-typedef enum {
- PMIC_BUCK_MODE_AUTO,
- PMIC_BUCK_MODE_PWM,
- PMIC_BUCK_MODE_PFM,
-} pmic_buck_mode_t;
-
-// Set the buck voltage regulator mode
-bool pmic_set_buck_mode(pmic_buck_mode_t buck_mode);
-
-// Clears all battery charger errors.
-bool pmic_clear_charger_errors(void);
+// Charging control, current limits and buck-regulator mode are part of the
+// charger extension - see pmic/pmic_charger.h.
### core/embed/io/power_manager/managed/power_manager.c
@@ -36,9 +36,9 @@
#include <sec/telemetry.h>
#endif
-#include "../battery/battery.h"
+#include "../fuel_gauge/battery.h"
#include "../power_manager_poll.h"
-#include "../stwlc38/stwlc38.h"
+#include "../wireless/stwlc38/stwlc38.h"
#include "power_manager_internal.h"
// Global driver instance
@@ -169,7 +169,9 @@ pm_status_t pm_init(bool inherit_state) {
// Set default SOC target and max charging current limit
drv->soc_target = 100;
+#ifdef USE_CHARGER
drv->i_chg_max_limit_ma = PM_BATTERY_CHARGING_CURRENT_MAX;
+#endif
#ifdef PM_ENABLE_TEMP_CONTROL
drv->i_chg_temp_limit_ma = PM_BATTERY_CHARGING_CURRENT_MAX;
@@ -485,6 +487,12 @@ pm_status_t pm_charging_set_max_current(uint16_t current_ma) {
return PM_NOT_INITIALIZED;
}
+#ifndef USE_CHARGER
+ // No charger on this PMIC, so there is no charging current to limit. Reject
+ // rather than report success for a limit that would never be applied.
+ (void)current_ma;
+ return PM_REQUEST_REJECTED;
+#else
if (current_ma > PM_BATTERY_CHARGING_CURRENT_MAX) {
return PM_REQUEST_REJECTED;
}
@@ -499,6 +507,7 @@ pm_status_t pm_charging_set_max_current(uint16_t current_ma) {
irq_unlock(irq_key);
return PM_OK;
+#endif
}
pm_status_t pm_store_data_to_backup_ram() {
### core/embed/io/power_manager/managed/power_manager_internal.h
@@ -26,16 +26,30 @@
#include <sys/rtc_scheduler.h>
#include <sys/systimer.h>
-#include "../stwlc38/stwlc38.h"
+#include "../wireless/stwlc38/stwlc38.h"
+
+#ifdef USE_CHARGER
+// Charger extension to the core PMIC interface. Implemented only by
+// charger-capable PMICs, so the policy may only reach for it - including for
+// its current limits below - when USE_CHARGER is set.
+#include "../pmic/pmic_charger.h"
+#endif
// Power manager thresholds & timings
#define PM_TIMER_PERIOD_MS 100
#define PM_SHUTDOWN_TIMEOUT_MS 15000
-#define PM_BATTERY_UNDERVOLT_THR_V 3.0f
-#define PM_BATTERY_CRITICAL_RECOVERY_SOC 0.02f
+// Battery-critical (brownout) thresholds moved into the chemistry-specific
+// gauge (see bat_eval_critical() in fuel_gauge/*), since the correct set/clear
+// mechanism depends on the cell and the gauge's precision.
#define PM_BATTERY_LOW_THRESHOLD_SOC 15
+
+#ifdef USE_CHARGER
+// Charging-current bounds are a property of the charger, so they come from the
+// selected PMIC's charger extension rather than being restated by the policy.
+// Undefined without a charger: every user is charger-only code.
#define PM_BATTERY_CHARGING_CURRENT_MAX PMIC_CHARGING_LIMIT_MAX
#define PM_BATTERY_CHARGING_CURRENT_MIN PMIC_CHARGING_LIMIT_MIN
+#endif
#define PM_BATTERY_DISCONNECTED_THR_V 0.5f // battery disconnection detection
#define PM_BATTERY_DISCONNECTED_REC_V \
@@ -50,8 +64,12 @@
#define PM_STABILIZATION_TIMEOUT_MS 2000
-// Thermal controller switch, comment out to disable the thermal controller
+// Thermal controller switch, comment out to disable the thermal controller.
+// The thermal controller limits the *charging* current by temperature, so it
+// is only meaningful when a charger is present.
+#ifdef USE_CHARGER
#define PM_ENABLE_TEMP_CONTROL
+#endif
// Temperature controller parameters
#define PM_TEMP_CONTROL_IDLE_PERIOD_MS 2 * 60 * 1000 // 2 minutes
### core/embed/io/power_manager/managed/power_monitoring.c
@@ -29,10 +29,15 @@
#include <sec/telemetry.h>
#endif
-#include "../battery/battery.h"
-#include "../stwlc38/stwlc38.h"
+#include "../fuel_gauge/battery.h"
+#include "../wireless/stwlc38/stwlc38.h"
#include "power_manager_internal.h"
+#ifdef USE_CHARGER
+// Charging control is only reachable from the charger-gated code below.
+#include "../pmic/pmic_charger.h"
+#endif
+
#ifdef PM_ENABLE_TEMP_CONTROL
static void pm_temperature_controller(pm_driver_t* drv);
#endif
@@ -183,6 +188,13 @@ void pm_pmic_data_ready(void* context, pmic_report_t* report) {
}
}
+#ifndef USE_CHARGER
+// No charger on this PMIC (e.g. power latch): the charging controller is a
+// no-op. The public charging API (pm_charging_enable/disable/set_max_current)
+// still exists but does nothing, and the state machine never enters
+// PM_STATE_CHARGING because usb/wireless are never reported connected.
+void pm_charging_controller(pm_driver_t* drv) { (void)drv; }
+#else
void pm_charging_controller(pm_driver_t* drv) {
if (drv->charging_enabled == false) {
// Charging is disabled
@@ -267,6 +279,7 @@ void pm_charging_controller(pm_driver_t* drv) {
pmic_set_charging(true);
}
}
+#endif // USE_CHARGER
#ifdef PM_ENABLE_TEMP_CONTROL
@@ -329,20 +342,20 @@ static void pm_parse_power_source_state(pm_driver_t* drv) {
}
}
- bat_fg_state_t fg_state;
- bat_fg_get_state(&fg_state);
-
- // Check battery voltage for critical (undervoltage) threshold
- if ((drv->pmic_data.vbat < PM_BATTERY_UNDERVOLT_THR_V) &&
- !drv->battery_critical && !drv->usb_connected) {
- // Force Fuel gauge to 0, keep the covariance
- bat_fg_set_soc(0.0f, fg_state.P);
- drv->battery_critical = true;
-
- } else if (fg_state.soc_latched >= (PM_BATTERY_CRITICAL_RECOVERY_SOC) ||
- drv->usb_connected) {
- // Restore the battery critical state
+ // Battery-critical (brownout) evaluation.
+ //
+ // Brownout is a safety concern and is kept out of the fuel-gauge precision
+ // budget: USB present means we are externally powered, so never
+ // brownout-critical; otherwise the decision (including hysteresis and the
+ // set/clear thresholds) is delegated to the chemistry-specific gauge, which
+ // knows what its cell and precision can actually support. See
+ // bat_eval_critical().
+ if (drv->usb_connected) {
drv->battery_critical = false;
+ } else {
+ drv->battery_critical =
+ bat_eval_critical(drv->battery_critical, drv->pmic_data.vbat,
+ drv->pmic_data.ibat, drv->pmic_data.ntc_temp);
}
}
### core/embed/io/power_manager/managed/power_states.c
[binary or diff unavailable]
### core/embed/io/power_manager/pmic/npm1300/npm1300.c
@@ -32,6 +32,7 @@
#include <io/suspend.h>
#endif
+#include "../pmic_charger.h"
#include "npm1300_defs.h"
#ifdef KERNEL_MODE
@@ -148,9 +149,9 @@ typedef struct {
bool charging_requested;
// Buck voltage regulator mode
- pmic_buck_mode_t buck_mode; // written value
- pmic_buck_mode_t buck_mode_requested; // requested value
- pmic_buck_mode_t buck_mode_set; // value beeing written
+ npm1300_buck_mode_t buck_mode; // written value
+ npm1300_buck_mode_t buck_mode_requested; // requested value
+ npm1300_buck_mode_t buck_mode_set; // value beeing written
// Enter ship mode
bool shipmode_requested;
@@ -377,9 +378,9 @@ bool pmic_init(void) {
drv->i_charge_set = drv->i_charge;
drv->i_charge_requested = drv->i_charge;
- drv->buck_mode_requested = PMIC_BUCK_MODE_AUTO;
- drv->buck_mode_set = PMIC_BUCK_MODE_AUTO;
- drv->buck_mode = PMIC_BUCK_MODE_AUTO;
+ drv->buck_mode_requested = NPM1300_BUCK_MODE_AUTO;
+ drv->buck_mode_set = NPM1300_BUCK_MODE_AUTO;
+ drv->buck_mode = NPM1300_BUCK_MODE_AUTO;
drv->i2c_bus = i2c_bus_open(NPM1300_I2C_INSTANCE);
if (drv->i2c_bus == NULL) {
@@ -551,7 +552,7 @@ bool pmic_set_charging(bool enable) {
return true;
}
-bool pmic_set_buck_mode(pmic_buck_mode_t buck_mode) {
+bool npm1300_set_buck_mode(npm1300_buck_mode_t buck_mode) {
npm1300_driver_t* drv = &g_npm1300_driver;
if (!drv->initialized) {
@@ -1066,9 +1067,9 @@ static void npm1300_fsm_continue(npm1300_driver_t* drv) {
}
} else if (drv->buck_mode != drv->buck_mode_requested) {
drv->buck_mode_set = drv->buck_mode_requested;
- if (drv->buck_mode_set == PMIC_BUCK_MODE_PWM) {
+ if (drv->buck_mode_set == NPM1300_BUCK_MODE_PWM) {
npm1300_i2c_submit(drv, npm1300_ops_buck_pwm);
- } else if (drv->buck_mode_set == PMIC_BUCK_MODE_PFM) {
+ } else if (drv->buck_mode_set == NPM1300_BUCK_MODE_PFM) {
npm1300_i2c_submit(drv, npm1300_ops_buck_pfm);
} else {
npm1300_i2c_submit(drv, npm1300_ops_buck_auto);
### core/embed/io/power_manager/pmic/npm1300/npm1300_defs.h
@@ -20,9 +20,24 @@
#ifndef TREZORHAL_NPM1300_DEFS_H
#define TREZORHAL_NPM1300_DEFS_H
+#include <trezor_types.h>
+
// I2C address of the NPM1300 on the I2C bus.
#define NPM1300_I2C_ADDRESS 0x6B
+// Buck voltage regulator mode. This is an npm1300-private control: it has no
+// cross-chip consumer, so it is not part of the public PMIC interface. If a
+// power-manager policy ever needs to control regulator mode, promote it to an
+// agnostic <io/pmic_regulator.h> instead of widening this driver header.
+typedef enum {
+ NPM1300_BUCK_MODE_AUTO,
+ NPM1300_BUCK_MODE_PWM,
+ NPM1300_BUCK_MODE_PFM,
+} npm1300_buck_mode_t;
+
+// Set the buck voltage regulator mode.
+bool npm1300_set_buck_mode(npm1300_buck_mode_t buck_mode);
+
// Event and interrupt registers
#define NPM1300_TASKSWRESET 0x0001
### core/embed/io/power_manager/pmic/pmic_charger.h
@@ -0,0 +1,61 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <trezor_types.h>
+
+// Charger extension to the core PMIC interface (<io/pmic.h>).
+//
+// Only charger-capable PMICs implement this surface (currently the npm1300).
+// PMICs without a charger - e.g. the npm2100 boost regulator for primary cells,
+// or a bare GPIO power latch - do not implement it, and it is called only from
+// charger-enabled power-manager policy code (the `managed` policy's charging
+// controller).
+//
+// Regulator/buck-mode control is deliberately NOT here: it currently has no
+// cross-chip consumer, so it lives as an npm1300-private detail
+// (npm1300_defs.h) rather than a public interface. Promote it to an agnostic
+// <io/pmic_regulator.h> if and when a policy needs to control regulator mode.
+
+// Charging current limits
+// - range of pmic is 32-800mA
+// - used battery limit is 180mA
+#define PMIC_CHARGING_LIMIT_MIN 32 // mA
+#define PMIC_CHARGING_LIMIT_MAX 180 // mA
+#define PMIC_CHARGING_LIMIT_DEFAULT 180 // mA
+
+// Enables or disables the charging.
+//
+// The function returns `false` if the operation cannot be performed.
+bool pmic_set_charging(bool enable);
+
+// Sets the charging current limit [mA].
+//
+// The current value must be in the range defined by the
+// `PMIC_CHARGING_LIMIT_MIN` and `PMIC_CHARGING_LIMIT_MAX` constants.
+//
+// The function returns `false` if the operation cannot be performed.
+bool pmic_set_charging_limit(int i_charge);
+
+// Gets the charging current limit [mA].
+int pmic_get_charging_limit(void);
+
+// Clears all battery charger errors.
+bool pmic_clear_charger_errors(void);
### core/embed/io/power_manager/wireless/stwlc38/nvm_data_cfg_v4_cut_1_2.h
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/nvm_data_cfg_v4_cut_1_3.h
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/nvm_data_fw_patch_v4.h
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/stwlc38.c
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/stwlc38.h
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/stwlc38_defs.h
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/stwlc38_internal.h
[binary or diff unavailable]
### core/embed/io/power_manager/wireless/stwlc38/stwlc38_patching.c
[binary or diff unavailable]
### core/embed/models/T3W1/boards/revA.toml
@@ -26,7 +26,7 @@ panel = "io/touch_panel_lx250a2410a"
[power_manager]
pmic = "io/pmic_npm1300"
-driver = "io/power_manager_npm1300"
+fuel_gauge = "io/fuel_gauge_lifepo4"
wireless = "io/wireless_stwlc38"
[nfc]
### core/embed/models/T3W1/boards/revB.toml
@@ -26,7 +26,7 @@ panel = "io/touch_panel_lx250a2410a"
[power_manager]
pmic = "io/pmic_npm1300"
-driver = "io/power_manager_npm1300"
+fuel_gauge = "io/fuel_gauge_lifepo4"
wireless = "io/wireless_stwlc38"
[nfc]
### core/embed/models/T3W1/boards/revC.toml
@@ -26,7 +26,7 @@ panel = "io/touch_panel_lx250a2410a"
[power_manager]
pmic = "io/pmic_npm1300"
-driver = "io/power_manager_npm1300"
+fuel_gauge = "io/fuel_gauge_lifepo4"
wireless = "io/wireless_stwlc38"
[nfc]
### core/embed/projects/prodtest/cmd/prodtest_wpc.c
@@ -26,7 +26,7 @@
#include <sys/systick.h>
#include <stdlib.h>
-#include "../stwlc38/stwlc38.h"
+#include "../wireless/stwlc38/stwlc38.h"
#include "prodtest_error_codes.h"
Why this scored 17/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.