[PATCH v3 6/7] test: dm: pmbus: add a sandbox PMBus chip emulator

Simon Glass sjg at chromium.org
Thu Jul 9 20:57:25 CEST 2026


Hi Vincent,

On 2026-07-06T10:50:51, Vincent Jardin <vjardin at free.fr> wrote:
> test: dm: pmbus: add a sandbox PMBus chip emulator
>
> Add a UCLASS_I2C_EMUL driver that emulates a PMBus 1.x compliant chip
> behind the sandbox I2C bus, plus the test.dts wiring and sandbox
> defconfig that bind it to the generic PMBus regulator
> (compatible = 'pmbus'). This design is a stub only: it lets the
> follow-up dm unit test drive lib/pmbus.c, the generic regulator and
> the pmbus CLI command with no real hardware.
>
> The emulator models a flat per-command 16-bit register file and the
> three identification block strings (MFR_ID / MFR_MODEL /
> MFR_REVISION). READ_IIN and READ_POUT are deliberately left
> unimplemented (the chip NAKs them) so the telemetry printer's
> "(not supported)" path is exercised, mirroring a real buck that only
> calibrates a subset of the sensor classes.
>
> Signed-off-by: Vincent Jardin <vjardin at free.fr>
>
> MAINTAINERS                             |   1 +
>  arch/sandbox/dts/test.dts               |  10 ++
>  configs/sandbox_defconfig               |   6 ++
>  drivers/power/regulator/Kconfig         |  10 ++
>  drivers/power/regulator/Makefile        |   1 +
>  drivers/power/regulator/sandbox_pmbus.c | 160 ++++++++++++++++++++++++++++++++
>  6 files changed, 188 insertions(+)

> diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
> @@ -395,3 +395,9 @@ CONFIG_UTHREAD=y
>  CONFIG_UNIT_TEST=y
>  CONFIG_UT_TIME=y
>  CONFIG_UT_DM=y
> +CONFIG_PMBUS=y
> +CONFIG_CMD_PMBUS=y
> +CONFIG_DM_REGULATOR_PMBUS_HELPER=y
> +CONFIG_DM_REGULATOR_PMBUS_GENERIC=y
> +CONFIG_SANDBOX_PMBUS=y
> +CONFIG_PMBUS_THERMAL=y

Please run 'make sandbox_defconfig savedefconfig' and copy the result
back - these are appended rather than merged into Kconfig order. Tom
does this sync from time to time, but having it approximately right
will simplify that.

> diff --git a/drivers/power/regulator/sandbox_pmbus.c b/drivers/power/regulator/sandbox_pmbus.c
> @@ -0,0 +1,160 @@
> +static int sandbox_pmbus_read(struct sandbox_pmbus_priv *priv, u8 cmd,
> +                           u8 *buf, int len)
> +{
> +     const char *str = pmbus_emul_string(cmd);
> +     int i;
> +
> +     if (str) {
> +             int slen = strlen(str);
> +
> +             /* Block payload: [length][bytes...]. */
> +             buf[0] = (u8)slen;
> +             for (i = 1; i < len; i++)
> +                     buf[i] = (i - 1 < slen) ? (u8)str[i - 1] : 0;
> +             return 0;
> +     }

Two things. Please guard buf[0] with 'if (len < 1) return -EINVAL;' so
a zero-length block read cannot walk off the buffer. Also, if a caller
asks for fewer bytes than slen + 1 you still report the full length in
buf[0] but return a truncated payload; either cap slen to len - 1 or
reject the short read, otherwise the length byte lies.

> diff --git a/drivers/power/regulator/sandbox_pmbus.c b/drivers/power/regulator/sandbox_pmbus.c
> @@ -0,0 +1,160 @@
> +     sandbox_pmbus_support(priv, PMBUS_CAPABILITY, 0xb0);

CAPABILITY 0xb0 advertises PEC (bit 7) and SMBALERT#, but the emulator
does not implement PEC. Please either drop the PEC bit or add a
comment noting the emulator is deliberately lying about it.

With those fixed:

Reviewed-by: Simon Glass <sjg at chromium.org>

Regards,
Simon


More information about the U-Boot mailing list