From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870133; cv=none; d=zohomail.com; s=zohoarc; b=FyZglVO2nW9spl9WswL+52gC9jlpVTWjGYhS6FaU4XfK+s79m/8tO40XFRQY49ZHyd2phyCTNTdl+aeSudENz1S10vy4z46yF2u5+HRonN5DFHYeolgLF4Xgsd0CVvMSXgdtd4QIxOiogD4kjRh6Ej4JzVkvkl1Wmi/Y7SDfS5g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870133; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=EP67QgvJbt/o3LubOHcWEgqt0rWN98GOcbIURTl2h2E=; b=b6a4abD3DA7px8+oeUx7PpZNigwzJBfCvIozfXG6CoJrdFzUOFlaTZYiCN7ggXQTpiZizky+/qcByKpDgetghPEHgmSFjPKt/KCbFHFOldOj3Wd5fhP6uoqftpzmfyxMNMjXqkVT1tA6Cls8E1cbQBD6VOF4+IU9TRhSvHJzoMc= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870132993951.0955582706024; Thu, 5 Nov 2026 01:15:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtYA-0005FP-SP; Thu, 05 Nov 2026 04:14:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtXY-00054j-IU for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:13:54 -0500 Received: from mail-pj1-f48.google.com ([209.85.216.48]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtXU-0005PE-J2 for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:13:48 -0500 Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-3ad41b47541so1243320a91.1 for ; Thu, 05 Nov 2026 01:13:44 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b32812a6sm2846627a91.11.2026.11.05.01.13.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:13:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870023; x=1794474823; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=EP67QgvJbt/o3LubOHcWEgqt0rWN98GOcbIURTl2h2E=; b=ehAv+mb+RzN2FfoUpbUsKqN/lJZgb6KvMTcEgglgHASOfcl/hOsBmz1QUDgBqObVae s9l/aojbgls+RGHCXqEYbd4t2EKVptKOoy7ISb39DO02FuaDbLyryyFp6fLCXSqE0hdS L+RMNs8H1cnUq2YEcww6AR7Fm6D7JfNae347YJ/k6H1q8nYupH6z6/VHWeh4VVpe+/Kf /3lPcEywrwSE01YgFvAX3U/mdgV5kiTXEMF967Fx3hQvVTBrH6IHciOpiLmB3cNC2nxy 00CDtNreOaB6+Hf7QuLZ0MVbI3KsTZZAWpfU/8DETBvdUj0mCe7PjF+IoSULB3Twchg2 cmkA== X-Gm-Message-State: APuU4Z9NkEmD3BormUiIPr+fNTx4PCuaBJUIv5Mt425UdzGRYL0oi8og X8VN84cXwMC279IORZVgx4XSkOG6wi5s0Y/WuO1z3oc0D94NpDrs+iIIrmJr7A== X-Gm-Gg: AYBFou3e6tjxGuq3ZgpIBDNmL3mG7LA1IIhROnn7cCQXgAC2yDVmYcc10AfjTaM9Ucl 1hLZVgFmtK+0gKz98Gq37Ri+MfEJ5iHa6EXr+MQkPWE/EDt34J2xYqd2S41r7/0xo2qeEOsyUIK loGMRwLT7I5/3XrR3F8XpqetR+J2kQVhe1nSKqu/hCZy/SNMNGrKLzQPnR078c+74kspWcOTN1a AWG+u/EmeH9Zg/6xwQjCgxK/Ora0hmDKvj1dhi2X9RPZtemqt0EoJfT4RaQKRjrrry7DPv4cdV+ lQ1G8ZSDWxCX4swCaxtIlykz5tzYrfRMHoaywIvgWvJl1kznaDoqjtxMGSl24Hut+G75ttn9+F3 v9xGjeeTvsktmESezWjF0HkYkFZIBHixYGtYasXg35TW8Hc8D3/klJh/oBdjZhPzCrwOjxH+TYS IoTPyXnFEFn3jlAwJWzcxCGfvhy+WmsyqqIdMPkONGk/nlX5Hi+ITCH3UOdGmuv7kLGopxkuQ4Z nH1im5bi7mAz23ATKecQzumF4MiMq0YWd3KIsQdCgz5ig9S194UXupXflX8O7IUKSqMrZh87R/X Nvn/g7lSrQ1MFp4UTvlYuJwBC4m8IxBhyuTKsWq9yHXG0oJBblSO5Q/5YLmdS7gn8DpqcGOljXx ZJgBVeg== X-Received: by 2002:a17:90b:4c85:b0:3a4:b418:94ce with SMTP id 98e67ed59e1d1-3ae89574db4mr14647580a91.1.1793870022752; Thu, 05 Nov 2026 01:13:42 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:53 +0800 Subject: [PATCH 01/16] hw/sensor: ina230: add current/voltage/power monitor MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-1-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=23298; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=rrDqzS1o86qFO4FxuK0O2brpp3NlCUfb4+nnIeRJfLw=; b=Jnp+mYD0w/FGjz23Pqg03ZjhdGMMwmPmrZ+JGLIUvHlWF0VyHni19RUd6uor/NdHNC9fNHYXx 6mg3j7PGBePCFdQ+s99UNrDCKHilcclG55T6ZPQ30ESJTk/s4+0RoSS X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.48; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f48.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870134016158500 Add a model for the Texas Instruments INA230, a 36V, 16-bit I2C current-shunt and power monitor. The device measures a differential shunt voltage and a bus voltage; the current and power registers are not measured directly but derived by the device from the shunt voltage and the guest-programmed Calibration register. The alert is modelled. Shunt and bus voltages can be injected through the "shunt-voltage" and "bus-voltage" QOM properties (in nV and uV respectively). Note: Conversion is instantaneous: the averaging and conversion-time fields are stored but have no timing effect. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- hw/arm/Kconfig | 1 + hw/sensor/Kconfig | 4 + hw/sensor/ina230.c | 598 +++++++++++++++++++++++++++++++++++++++++= ++++ hw/sensor/meson.build | 1 + hw/sensor/trace-events | 7 + include/hw/sensor/ina230.h | 14 ++ 6 files changed, 625 insertions(+) diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig index e9f796f718..876acc0e43 100644 --- a/hw/arm/Kconfig +++ b/hw/arm/Kconfig @@ -557,6 +557,7 @@ config ASPEED_SOC select TMP105 select TMP421 select EMC141X + select INA230 select OR_IRQ select UNIMP select LED diff --git a/hw/sensor/Kconfig b/hw/sensor/Kconfig index b459ac2240..da519159ac 100644 --- a/hw/sensor/Kconfig +++ b/hw/sensor/Kconfig @@ -2,6 +2,10 @@ config ADC128D818 bool depends on I2C =20 +config INA230 + bool + depends on I2C + config TMP105 bool depends on I2C diff --git a/hw/sensor/ina230.c b/hw/sensor/ina230.c new file mode 100644 index 0000000000..eb6991b0b9 --- /dev/null +++ b/hw/sensor/ina230.c @@ -0,0 +1,598 @@ +/* + * Texas Instruments INA230 36V, 16-bit current/voltage/power monitor + * with an I2C interface. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * The INA230 measures a differential shunt voltage and a bus voltage. + * Current and power are not measured directly: they are derived by the + * device from the measured shunt voltage. + * + * Limitations: + * - Conversion timing is instantaneous. A conversion runs synchronously w= hen an + * input, the calibration or the configuration changes; the per-channel + * conversion times (VSH_CT/VBUS_CT), the averaging count (AVG) and cont= inuous + * re-conversion on a timer are not modelled. A read always returns the = value + * derived from the current inputs and CVRF is set once a conversion has= run. + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "qapi/qapi-builtin-type-infos.h" +#include "qapi/visitor.h" +#include "qom/object.h" +#include "hw/sensor/ina230.h" +#include "hw/core/irq.h" +#include "hw/core/qdev-properties.h" +#include "hw/core/registerfields.h" +#include "hw/i2c/i2c.h" +#include "migration/vmstate.h" +#include "trace.h" + +#define INA230_REG_CONFIG 0x00 +#define INA230_REG_SHUNT 0x01 +#define INA230_REG_BUS 0x02 +#define INA230_REG_POWER 0x03 +#define INA230_REG_CURRENT 0x04 +#define INA230_REG_CALIBRATION 0x05 +#define INA230_REG_MASK_ENABLE 0x06 +#define INA230_REG_ALERT_LIMIT 0x07 +#define INA230_REG_DIE_ID 0xFF + +FIELD(INA230_CONFIG, MODE, 0, 3) +FIELD(INA230_CONFIG, VSH_CT, 3, 3) +FIELD(INA230_CONFIG, VBUS_CT, 6, 3) +FIELD(INA230_CONFIG, AVG, 9, 3) +FIELD(INA230_CONFIG, RST, 15, 1) + +#define INA230_CONFIG_POR 0x4127 + +FIELD(INA230_MASK_ENABLE, LEN, 0, 1) /* alert latch enable */ +FIELD(INA230_MASK_ENABLE, APOL, 1, 1) /* alert polarity */ +FIELD(INA230_MASK_ENABLE, OVF, 2, 1) /* math overflow flag (status) */ +FIELD(INA230_MASK_ENABLE, CVRF, 3, 1) /* conversion ready flag (status) = */ +FIELD(INA230_MASK_ENABLE, AFF, 4, 1) /* alert function flag (status) */ +FIELD(INA230_MASK_ENABLE, CNVR, 10, 1) /* conversion ready alert */ +FIELD(INA230_MASK_ENABLE, POL, 11, 1) /* power over-limit */ +FIELD(INA230_MASK_ENABLE, BUL, 12, 1) /* bus under-limit */ +FIELD(INA230_MASK_ENABLE, BOL, 13, 1) /* bus over-limit */ +FIELD(INA230_MASK_ENABLE, SUL, 14, 1) /* shunt under-limit */ +FIELD(INA230_MASK_ENABLE, SOL, 15, 1) /* shunt over-limit */ + +/* Writable Mask/Enable bits: alert-function selects + APOL + LEN */ +#define INA230_ME_WMASK (R_INA230_MASK_ENABLE_SOL_MASK | \ + R_INA230_MASK_ENABLE_SUL_MASK | \ + R_INA230_MASK_ENABLE_BOL_MASK | \ + R_INA230_MASK_ENABLE_BUL_MASK | \ + R_INA230_MASK_ENABLE_POL_MASK | \ + R_INA230_MASK_ENABLE_CNVR_MASK | \ + R_INA230_MASK_ENABLE_APOL_MASK | \ + R_INA230_MASK_ENABLE_LEN_MASK) +/* Read-only status bits: AFF, CVRF, OVF */ +#define INA230_ME_STATUS_MASK (R_INA230_MASK_ENABLE_AFF_MASK | \ + R_INA230_MASK_ENABLE_CVRF_MASK | \ + R_INA230_MASK_ENABLE_OVF_MASK) + +FIELD(INA230_CALIBRATION, FS, 0, 15) +#define INA230_CAL_MASK R_INA230_CALIBRATION_FS_MASK + +#define INA230_SHUNT_LSB_NV 2500 /* 2.5 uV per LSB */ +#define INA230_BUS_LSB_UV 1250 /* 1.25 mV per LSB */ +#define INA230_SHUNT_MIN_NV (-81920000) /* -32768 * 2.5 uV */ +#define INA230_SHUNT_MAX_NV 81917500 /* 32767 * 2.5 uV */ +/* + * A real rail should not drive the pin past 36 V, so bus codes above 0x70= 80 + * are intentionally unreachable in the model. + */ +#define INA230_BUS_MAX_UV 36000000 /* bus pin rated 0..36 V */ + +#define INA230_CURRENT_DIV 2048 +#define INA230_POWER_DIV 20000 + +#define INA230_DIE_ID_VAL 0x2310 + +OBJECT_DECLARE_SIMPLE_TYPE(INA230State, INA230) + +struct INA230State { + I2CSlave parent_obj; + + qemu_irq alert; /* ALERT open-drain output */ + + /* I2C transfer state */ + uint8_t ptr; /* register pointer */ + uint8_t len; /* bytes received in current transfer */ + uint8_t wr_hi; /* pending write MSB */ + uint8_t rx_byte; /* 0: return MSB next, 1: return LSB next */ + uint16_t rx_word; /* value latched at the start of a read */ + + /* Register file */ + uint16_t config; + uint16_t calibration; + uint16_t mask_enable; + uint16_t alert_limit; + + /* Derived measurement registers */ + uint16_t shunt_reg; + uint16_t bus_reg; + uint16_t current_reg; + uint16_t power_reg; + + /* Injected physical inputs */ + int32_t shunt_nv; /* shunt voltage, nanovolts (signed: bidirectional)= */ + uint32_t bus_uv; /* bus voltage, microvolts (never negative) */ + + /* + * In latch mode AFF and the ALERT pin stay asserted until the + * Configuration register is written or the Mask/Enable register is re= ad + * (datasheet 7.3.3). + */ + bool alert_latched; + + char *description; +}; + +/* + * Evaluate the selected alert function. @shunt_fresh and @bus_fresh tell = which + * of the measurement registers hold a fresh value; a function fed by a ch= annel + * that did not convert cannot assert. + */ +static bool ina230_limit_alert(const INA230State *s, bool shunt_fresh, + bool bus_fresh) +{ + uint16_t me =3D s->mask_enable; + int16_t shunt =3D (int16_t)s->shunt_reg; + int16_t limit_s =3D (int16_t)s->alert_limit; + uint16_t limit_u =3D s->alert_limit; + + if (FIELD_EX16(me, INA230_MASK_ENABLE, SOL)) { + return shunt_fresh && shunt > limit_s; + } + if (FIELD_EX16(me, INA230_MASK_ENABLE, SUL)) { + return shunt_fresh && shunt < limit_s; + } + if (FIELD_EX16(me, INA230_MASK_ENABLE, BOL)) { + return bus_fresh && s->bus_reg > limit_u; + } + if (FIELD_EX16(me, INA230_MASK_ENABLE, BUL)) { + return bus_fresh && s->bus_reg < limit_u; + } + if (FIELD_EX16(me, INA230_MASK_ENABLE, POL)) { + return bus_fresh && s->power_reg > limit_u; + } + return false; +} + +static void ina230_refresh_alert(INA230State *s) +{ + bool latch =3D FIELD_EX16(s->mask_enable, INA230_MASK_ENABLE, LEN); + bool aff =3D latch ? s->alert_latched : ina230_limit_alert(s, true, tr= ue); + bool cnvr; + bool active; + int level; + + s->mask_enable =3D FIELD_DP16(s->mask_enable, INA230_MASK_ENABLE, AFF,= aff); + + cnvr =3D FIELD_EX16(s->mask_enable, INA230_MASK_ENABLE, CNVR) && + FIELD_EX16(s->mask_enable, INA230_MASK_ENABLE, CVRF); + + active =3D aff || cnvr; + + level =3D FIELD_EX16(s->mask_enable, INA230_MASK_ENABLE, APOL) ? + active : !active; + + trace_ina230_alert(s->description, level); + qemu_set_irq(s->alert, level); +} + +static void ina230_sample_alert(INA230State *s, bool shunt_fresh, + bool bus_fresh) +{ + if (FIELD_EX16(s->mask_enable, INA230_MASK_ENABLE, LEN) && + ina230_limit_alert(s, shunt_fresh, bus_fresh)) { + s->alert_latched =3D true; + } + ina230_refresh_alert(s); +} + +static void ina230_convert(INA230State *s) +{ + uint8_t mode =3D FIELD_EX16(s->config, INA230_CONFIG, MODE); + bool shunt_en =3D mode & 0x1; + bool bus_en =3D mode & 0x2; + int64_t sr, br, cur, pwr; + bool ovf =3D false; + + if (!shunt_en && !bus_en) { + return; + } + + if (shunt_en) { + sr =3D s->shunt_nv / INA230_SHUNT_LSB_NV; + sr =3D MAX(INT16_MIN, MIN(INT16_MAX, sr)); + s->shunt_reg =3D (uint16_t)(int16_t)sr; + + cur =3D (int64_t)(int16_t)s->shunt_reg * s->calibration / + INA230_CURRENT_DIV; + if (cur > INT16_MAX || cur < INT16_MIN) { + ovf =3D true; + cur =3D MAX(INT16_MIN, MIN(INT16_MAX, cur)); + } + s->current_reg =3D (uint16_t)(int16_t)cur; + } + + if (bus_en) { + br =3D s->bus_uv / INA230_BUS_LSB_UV; + br =3D MAX(0, MIN(INT16_MAX, br)); + s->bus_reg =3D (uint16_t)br; + } + + /* + * In bus-only mode the current register is retained from the last shu= nt + * conversion rather than measured anew, but the power register is sti= ll + * refreshed. + */ + if (bus_en) { + pwr =3D (int64_t)(int16_t)s->current_reg * s->bus_reg / INA230_POW= ER_DIV; + /* POWER is an unsigned, positive-only register (datasheet). */ + if (pwr < 0) { + pwr =3D 0; + } else if (pwr > UINT16_MAX) { + ovf =3D true; + pwr =3D UINT16_MAX; + } + s->power_reg =3D (uint16_t)pwr; + } + + /* Without a calibration the device reports neither current nor power.= */ + if (s->calibration =3D=3D 0) { + s->current_reg =3D 0; + s->power_reg =3D 0; + } + + s->mask_enable =3D FIELD_DP16(s->mask_enable, INA230_MASK_ENABLE, OVF,= ovf); + s->mask_enable =3D FIELD_DP16(s->mask_enable, INA230_MASK_ENABLE, CVRF= , 1); + + trace_ina230_convert(s->description, s->shunt_reg, s->bus_reg, + s->current_reg, s->power_reg); + + ina230_sample_alert(s, shunt_en, bus_en); +} + +/* + * A physical input or the calibration changed. + * - in continuous mode the device keeps converting and immediately tracks + * the new value + * - in a triggered mode it performs exactly one conversion per trigger + */ +static void ina230_input_update(INA230State *s) +{ + if (FIELD_EX16(s->config, INA230_CONFIG, MODE) & 0x4) { + ina230_convert(s); + } +} + +static void ina230_reset_regs(INA230State *s) +{ + s->config =3D INA230_CONFIG_POR; + s->calibration =3D 0; + s->mask_enable =3D 0; + s->alert_limit =3D 0; + s->alert_latched =3D false; + + s->ptr =3D 0; + s->len =3D 0; + s->wr_hi =3D 0; + s->rx_byte =3D 0; + s->rx_word =3D 0; + + s->shunt_reg =3D 0; + s->bus_reg =3D 0; + s->current_reg =3D 0; + s->power_reg =3D 0; + + ina230_refresh_alert(s); +} + +static uint16_t ina230_read_reg(INA230State *s, uint8_t reg) +{ + uint16_t val; + + switch (reg) { + case INA230_REG_CONFIG: + val =3D s->config; + break; + case INA230_REG_SHUNT: + val =3D s->shunt_reg; + break; + case INA230_REG_BUS: + val =3D s->bus_reg; + break; + case INA230_REG_POWER: + val =3D s->power_reg; + break; + case INA230_REG_CURRENT: + val =3D s->current_reg; + break; + case INA230_REG_CALIBRATION: + val =3D s->calibration; + break; + case INA230_REG_MASK_ENABLE: + val =3D s->mask_enable; + s->mask_enable =3D FIELD_DP16(s->mask_enable, INA230_MASK_ENABLE, + CVRF, 0); + s->alert_latched =3D false; + ina230_refresh_alert(s); + break; + case INA230_REG_ALERT_LIMIT: + val =3D s->alert_limit; + break; + case INA230_REG_DIE_ID: + val =3D INA230_DIE_ID_VAL; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: read from undefined register 0x%02x\n", + __func__, s->description, reg); + val =3D 0; + break; + } + + trace_ina230_read(s->description, reg, val); + return val; +} + +static void ina230_write_reg(INA230State *s, uint8_t reg, uint16_t val) +{ + trace_ina230_write(s->description, reg, val); + + switch (reg) { + case INA230_REG_CONFIG: + if (val & R_INA230_CONFIG_RST_MASK) { + trace_ina230_reset(s->description, "reg"); + ina230_reset_regs(s); + break; + } + s->config =3D val & ~R_INA230_CONFIG_RST_MASK; + /* + * The write itself releases a latched alert, so the release reach= es + * the ALERT pin before the conversion it triggers can relatch it. + */ + s->alert_latched =3D false; + ina230_refresh_alert(s); + ina230_convert(s); + break; + case INA230_REG_CALIBRATION: + s->calibration =3D val & INA230_CAL_MASK; + ina230_input_update(s); + break; + case INA230_REG_MASK_ENABLE: + s->mask_enable =3D (val & INA230_ME_WMASK) | + (s->mask_enable & INA230_ME_STATUS_MASK); + ina230_refresh_alert(s); + break; + case INA230_REG_ALERT_LIMIT: + s->alert_limit =3D val; + ina230_refresh_alert(s); + break; + case INA230_REG_SHUNT: + case INA230_REG_BUS: + case INA230_REG_POWER: + case INA230_REG_CURRENT: + case INA230_REG_DIE_ID: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: write to read-only register 0x%02x\n", + __func__, s->description, reg); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: write to undefined register 0x%02x\n", + __func__, s->description, reg); + break; + } +} + +static uint8_t ina230_recv(I2CSlave *i2c) +{ + INA230State *s =3D INA230(i2c); + uint8_t byte; + + if (s->rx_byte =3D=3D 0) { + s->rx_word =3D ina230_read_reg(s, s->ptr); + byte =3D s->rx_word >> 8; + s->rx_byte =3D 1; + } else { + byte =3D s->rx_word & 0xff; + s->rx_byte =3D 0; + } + + return byte; +} + +static int ina230_send(I2CSlave *i2c, uint8_t data) +{ + INA230State *s =3D INA230(i2c); + + if (s->len =3D=3D 0) { + s->ptr =3D data; + } else if ((s->len & 1) =3D=3D 1) { + s->wr_hi =3D data; + } else { + ina230_write_reg(s, s->ptr, ((uint16_t)s->wr_hi << 8) | data); + } + s->len++; + + return 0; +} + +static int ina230_event(I2CSlave *i2c, enum i2c_event event) +{ + INA230State *s =3D INA230(i2c); + + s->len =3D 0; + s->rx_byte =3D 0; + + return 0; +} + +static void ina230_get_shunt(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA230State *s =3D INA230(obj); + int64_t value =3D s->shunt_nv; + + visit_type_int(v, name, &value, errp); +} + +static void ina230_set_shunt(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA230State *s =3D INA230(obj); + int64_t value; + + if (!visit_type_int(v, name, &value, errp)) { + return; + } + + if (value < INA230_SHUNT_MIN_NV || value > INA230_SHUNT_MAX_NV) { + error_setg(errp, + "%s: shunt-voltage %" PRId64 " out of range (%d..%d nV)= ", + s->description, value, + INA230_SHUNT_MIN_NV, INA230_SHUNT_MAX_NV); + return; + } + + s->shunt_nv =3D (int32_t)value; + ina230_input_update(s); +} + +static void ina230_get_bus(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA230State *s =3D INA230(obj); + int64_t value =3D s->bus_uv; + + visit_type_int(v, name, &value, errp); +} + +static void ina230_set_bus(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA230State *s =3D INA230(obj); + int64_t value; + + if (!visit_type_int(v, name, &value, errp)) { + return; + } + + if (value < 0 || value > INA230_BUS_MAX_UV) { + error_setg(errp, + "%s: bus-voltage %" PRId64 " out of range (0..%d uV)", + s->description, value, INA230_BUS_MAX_UV); + return; + } + + s->bus_uv =3D (uint32_t)value; + ina230_input_update(s); +} + +static void ina230_reset_hold(Object *obj, ResetType type) +{ + INA230State *s =3D INA230(obj); + + trace_ina230_reset(s->description, "hw"); + ina230_reset_regs(s); +} + +static int ina230_post_load(void *opaque, int version_id) +{ + INA230State *s =3D opaque; + + ina230_refresh_alert(s); + + return 0; +} + +static const VMStateDescription ina230_vmstate =3D { + .name =3D "INA230", + .version_id =3D 1, + .minimum_version_id =3D 1, + .post_load =3D ina230_post_load, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT8(ptr, INA230State), + VMSTATE_UINT8(len, INA230State), + VMSTATE_UINT8(wr_hi, INA230State), + VMSTATE_UINT8(rx_byte, INA230State), + VMSTATE_UINT16(rx_word, INA230State), + VMSTATE_UINT16(config, INA230State), + VMSTATE_UINT16(calibration, INA230State), + VMSTATE_UINT16(mask_enable, INA230State), + VMSTATE_UINT16(alert_limit, INA230State), + VMSTATE_UINT16(shunt_reg, INA230State), + VMSTATE_UINT16(bus_reg, INA230State), + VMSTATE_UINT16(current_reg, INA230State), + VMSTATE_UINT16(power_reg, INA230State), + VMSTATE_INT32(shunt_nv, INA230State), + VMSTATE_UINT32(bus_uv, INA230State), + VMSTATE_BOOL(alert_latched, INA230State), + VMSTATE_I2C_SLAVE(parent_obj, INA230State), + VMSTATE_END_OF_LIST() + } +}; + +static void ina230_initfn(Object *obj) +{ + object_property_add(obj, "shunt-voltage", "int", &int_type_info, + ina230_get_shunt, ina230_set_shunt, NULL, NULL); + object_property_set_description(obj, "shunt-voltage", + "Injected shunt (differential) voltage= , " + "in nanovolts"); + object_property_add(obj, "bus-voltage", "int", &int_type_info, + ina230_get_bus, ina230_set_bus, NULL, NULL); + object_property_set_description(obj, "bus-voltage", + "Injected bus voltage, in microvolts"); +} + +static void ina230_realize(DeviceState *dev, Error **errp) +{ + INA230State *s =3D INA230(dev); + + if (!s->description) { + s->description =3D g_strdup(object_get_typename(OBJECT(dev))); + } + + qdev_init_gpio_out(dev, &s->alert, 1); +} + +static const Property ina230_properties[] =3D { + DEFINE_PROP_STRING("description", INA230State, description), +}; + +static void ina230_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + I2CSlaveClass *ic =3D I2C_SLAVE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + + ic->event =3D ina230_event; + ic->recv =3D ina230_recv; + ic->send =3D ina230_send; + dc->realize =3D ina230_realize; + rc->phases.hold =3D ina230_reset_hold; + dc->vmsd =3D &ina230_vmstate; + device_class_set_props(dc, ina230_properties); +} + +static const TypeInfo ina230_types[] =3D { + { + .name =3D TYPE_INA230, + .parent =3D TYPE_I2C_SLAVE, + .instance_init =3D ina230_initfn, + .instance_size =3D sizeof(INA230State), + .class_init =3D ina230_class_init, + }, +}; + +DEFINE_TYPES(ina230_types) diff --git a/hw/sensor/meson.build b/hw/sensor/meson.build index fe36c9ef91..b81f001fdf 100644 --- a/hw/sensor/meson.build +++ b/hw/sensor/meson.build @@ -1,4 +1,5 @@ system_ss.add(when: 'CONFIG_ADC128D818', if_true: files('adc128d818.c')) +system_ss.add(when: 'CONFIG_INA230', if_true: files('ina230.c')) system_ss.add(when: 'CONFIG_TMP105', if_true: files('tmp105.c')) system_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.c')) system_ss.add(when: 'CONFIG_DPS310', if_true: files('dps310.c')) diff --git a/hw/sensor/trace-events b/hw/sensor/trace-events index 5a3630f7bb..f809b6b506 100644 --- a/hw/sensor/trace-events +++ b/hw/sensor/trace-events @@ -8,6 +8,13 @@ adc128d818_convert(const char *id, uint8_t channel, uint16= _t value) "%s ch %u va adc128d818_irq(const char *id, bool level) "%s level %u" adc128d818_reset(const char *id, const char *source) "%s %s" =20 +# ina230.c +ina230_read(const char *id, uint8_t reg, uint16_t value) "%s reg 0x%02x va= l 0x%04x" +ina230_write(const char *id, uint8_t reg, uint16_t value) "%s reg 0x%02x v= al 0x%04x" +ina230_convert(const char *id, uint16_t shunt, uint16_t bus, uint16_t curr= ent, uint16_t power) "%s shunt 0x%04x bus 0x%04x current 0x%04x power 0x%04= x" +ina230_alert(const char *id, bool level) "%s level %u" +ina230_reset(const char *id, const char *source) "%s %s" + # tmp105.c tmp105_read(uint8_t dev, uint8_t addr) "device: 0x%02x, addr: 0x%02x" tmp105_write(uint8_t dev, uint8_t addr) "device: 0x%02x, addr 0x%02x" diff --git a/include/hw/sensor/ina230.h b/include/hw/sensor/ina230.h new file mode 100644 index 0000000000..4394727d47 --- /dev/null +++ b/include/hw/sensor/ina230.h @@ -0,0 +1,14 @@ +/* + * Texas Instruments INA230 current/voltage/power monitor with I2C interfa= ce + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_SENSOR_INA230_H +#define HW_SENSOR_INA230_H + +#define TYPE_INA230 "ina230" + +#endif --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870094; cv=none; d=zohomail.com; s=zohoarc; b=LkVumDllDSAJ4xdGxpJ7z/pvF4YzOEzipW0J5dxLHvpC1r98/L8aJkY8oj5ZDmxAuRGm24JdxRdobM8EP/rS66d4QCAwVTfBC9jd8xCzic6fSmM2/K7GX4Fttl78Z5F8Z/AN5FLt2XWWwSK/Uag7k/cVC51OaDWR0UrD5FSkhTU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870094; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=9a8tVmiMXzx1D2s3RdJddbAUFJlNjdVF7U3oIP6iTDE=; b=SElwWyAjOSgFepZr7+5mGvexP/LQMFdg7d65DOB1vPcN6OxtgC6yrcoy7PV9VprOeKEQiiocxoJSQga+fkN4At6kUBT2bolkBkJeR0XxCx6ER+qPRqxn4C1xCSMwMyrOBXFnasR7iJxtu3yCluLZapwsQvvldgNjOgC3xX7ueVo= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870094897292.39873546245394; Thu, 5 Nov 2026 01:14:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtY5-00059o-7F; Thu, 05 Nov 2026 04:14:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtXe-000555-Kg for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:13:57 -0500 Received: from mail-pg1-f181.google.com ([209.85.215.181]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtXc-0005Q8-4c for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:13:54 -0500 Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-cd62c570b5dso1085927a12.2 for ; Thu, 05 Nov 2026 01:13:51 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cdb46443d6esm1974481a12.2.2026.11.05.01.13.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:13:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870030; x=1794474830; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=9a8tVmiMXzx1D2s3RdJddbAUFJlNjdVF7U3oIP6iTDE=; b=m7HCk+VwHABv1m0zjkfJCD2Pvnaqr+iPlevUkI0M1XJS2AQzBragnOIcs/Dbrvo0RK Lw8R4QSLtXmhNtsFwYh/IEg1zyPhtltJNrOaTV70QOO/Ky5df2XHQoZXgJ+J9u6emMpE +zo8MCEW/RKV1WNNrv7vQrp9v+E82/LxqGOdp6Mwf1wEfWnhjr8X98Z2C/CDGW9oJ3Hy EMLU+Gt/335svUYOynTjo5BYN39YZdD2NCEiY6k0Bmtoquf7HHtLgjMaZBULLa1+AiSW Kjf/SWIdfuQV4YtyKTNkNUrOC8LU+Biihc++bGJIFpFI+qt/qXAKqxQOWgbWM4mChgnT jTmg== X-Gm-Message-State: APuU4Z+6Ax0F9oOCbiDE8i8SIo2ntmioLmzHeFC4BQyH5PYPftrO/r1A y9e4RouRwXmy7PAni8SdcPyrt/9bcpb6xqTqv+3EIF5wREzsLJLFSAnjb7mvsMTqu8A= X-Gm-Gg: AYBFou2QCy/b5QAnjYUA16K63OorGOdEtjzwwGmKwLtWAJ10LEvB8uLk/c+MXGzUWGD lP8VVN6sILvXrRYiVSElgMIt+EHQPJB5hpta3dpDH+6onDcN/1dCxAKN1d4Rek2DIsHAI/az+RG vpB1p2wcF/ZE99YviFwY+2VbsOR1G/Ow+j+5zR5ynp25mogzzE74QXCVrLjzGBfdvHoPx9rTZES B5wl/7zTqxpzU8RK/ZkZxFi34yg2y/jkx+IzdOqLcKdRnfWjRHRbZELY3QZB/+dnn+eLdyT2RyL 00p7TsvU6qTTNm8x5cJJzjP/YjcCb1Pc7RpINg1xAHUBeHm+0hhfo0adJoZiRrywsJAxDBsfJGM HHyIXjiRS1k5Z0b1GgRL0J4b0yTVz2VOEKg2adQGnT+F4b71h0DF5hsVgMfy+spAmbOW48Npy4x hccaZPSSh1Z3Clc50FdJ1kJQggSqyLKm9EJPC/0Lg9iifg2Y8PUupjROIoue7lLOvkc9r+Y8e7e 061I/ihiNveaq69fKW/UrzJBroYgySa5lXVgnZgDFBDa2wMRv+J9snl6NuBiWyeUo1X9xPnW2FW tvWUZ10cPQrirTZ/C617h5Xj15PZDnLBjLujYo4xJgiGYhSjB4LHdqtungJ7ElHb/7l/L0CIcxW vn6rhUw== X-Received: by 2002:a17:90b:4b92:b0:3ac:86a0:5766 with SMTP id 98e67ed59e1d1-3ae897fb684mr15264726a91.63.1793870030247; Thu, 05 Nov 2026 01:13:50 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:54 +0800 Subject: [PATCH 02/16] tests/qtest: ina230: add current/voltage/power monitor test MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-2-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=5246; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=s1nFTgdA+hlQv/WxtG23fzT50LLyFvY6GqK1Nb3vghE=; b=xDzh4GbA/5pPwhR2rS4DsL4+FFCA28MW9O13VeNBGr5rQejM0UrxfOBKCJiHsWzRcGAWORd6r sREMVFbgRoCBIdT/CIUckKYTdkVkukxFBIOVRZ2yJ+XCpTNLn3jYXkS X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.215.181; envelope-from=eblot.ml@gmail.com; helo=mail-pg1-f181.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870096997158500 Introduce the INA230 qtest: the qgraph node and the register map. Cover the power-on-reset defaults and Die ID, the software reset, and the Configuration register write mask. The QMP voltage-injection helpers arrive with the following commit that exercises them. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 115 ++++++++++++++++++++++++++++++++++++++++++= ++++ tests/qtest/meson.build | 1 + 2 files changed, 116 insertions(+) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c new file mode 100644 index 0000000000..f7aefd2d97 --- /dev/null +++ b/tests/qtest/ina230-test.c @@ -0,0 +1,115 @@ +/* + * QTest testcase for the INA230 current/voltage/power monitor + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "libqos/i2c.h" +#include "libqos/qgraph.h" +#include "libqtest-single.h" +#include "qobject/qdict.h" + +#define INA230_TEST_ID "ina230-test" +#define INA230_TEST_ADDR 0x40 + +/* Register pointer addresses */ +#define REG_CONFIG 0x00 +#define REG_SHUNT 0x01 +#define REG_BUS 0x02 +#define REG_POWER 0x03 +#define REG_CURRENT 0x04 +#define REG_CALIBRATION 0x05 +#define REG_MASK_ENABLE 0x06 +#define REG_ALERT_LIMIT 0x07 +#define REG_DIE_ID 0xFF + +/* Configuration register bits */ +#define CONFIG_POR 0x4127 +#define CONFIG_RST 0x8000 + +/* Mask/Enable register bits */ +#define ME_LEN 0x0001 +#define ME_APOL 0x0002 +#define ME_OVF 0x0004 +#define ME_CVRF 0x0008 +#define ME_AFF 0x0010 +#define ME_CNVR 0x0400 +#define ME_POL 0x0800 +#define ME_BUL 0x1000 +#define ME_BOL 0x2000 +#define ME_SUL 0x4000 +#define ME_SOL 0x8000 + +/* Injected-input scaling (datasheet fixed LSBs) */ +#define SHUNT_LSB_NV 2500 +#define BUS_LSB_UV 1250 + +#define DIE_ID_VAL 0x2310 + +/* Power-on-reset default values and the Die ID */ +static void test_defaults(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_POR); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_CALIBRATION), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_ALERT_LIMIT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_DIE_ID), =3D=3D, DIE_ID_VAL); +} + +/* Software reset via the Configuration RST bit */ +static void test_soft_reset(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CALIBRATION, 0x1234); + i2c_set16(dev, REG_MASK_ENABLE, ME_SOL); + i2c_set16(dev, REG_ALERT_LIMIT, 0x0500); + + g_assert_cmphex(i2c_get16(dev, REG_CALIBRATION), =3D=3D, 0x1234); + g_assert_cmphex(i2c_get16(dev, REG_ALERT_LIMIT), =3D=3D, 0x0500); + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_POR); + g_assert_cmphex(i2c_get16(dev, REG_CALIBRATION), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_ALERT_LIMIT), =3D=3D, 0x0000); +} + +/* Every bit except the self-clearing RST is writable and reads back */ +static void test_config_wmask(void *obj, void *data, QGuestAllocator *allo= c) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + /* Reserved bits are not forced: the guest value is retained */ + i2c_set16(dev, REG_CONFIG, 0x0007); + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, 0x0007); + + i2c_set16(dev, REG_CONFIG, 0x7FFF); + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, 0x7FFF); +} + +static void ina230_register_nodes(void) +{ + QOSGraphEdgeOptions opts =3D { + .extra_device_opts =3D "id=3D" INA230_TEST_ID ",address=3D0x40" + }; + add_qi2c_address(&opts, &(QI2CAddress) { INA230_TEST_ADDR }); + + qos_node_create_driver("ina230", i2c_device_create); + qos_node_consumes("ina230", "i2c-bus", &opts); + + qos_add_test("defaults", "ina230", test_defaults, NULL); + qos_add_test("soft-reset", "ina230", test_soft_reset, NULL); + qos_add_test("config-wmask", "ina230", test_config_wmask, NULL); +} +libqos_init(ina230_register_nodes); diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index ff4bf9528a..bfebe136ef 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -338,6 +338,7 @@ qos_test_ss.add( 'e1000-test.c', 'eepro100-test.c', 'es1370-test.c', + 'ina230-test.c', 'lsm303dlhc-mag-test.c', 'isl_pmbus_vr-test.c', 'max34451-test.c', --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870090; cv=none; d=zohomail.com; s=zohoarc; b=U3Ve0IFgMHGobpcCKqwsmd8d0lAeNJKjJK5Y4irq1fvdrRwQGrLQRZN7nyIGs2yCxmtpp/CNhXOmrDVUGELtp5efptOnTlRCIWAeavMMyMGcQIPUbNlpE+fRBBJFmVaHPmVzMM1EdzpmV4EH54M0QoduOskwffRT7ndLepAeIZs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870090; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=YHMfEpRBCghPOlLiattQp/tdJSULFxB9dFeHDoCkMiY=; b=blakDI/spud0aVegFp2HcyqrYJdzogc01kK/wA+Nc+ZDsNrhz0sM8U2Vh46VhzzikyE5tnl5CkNBZdUjP1ZM2VpIbrsmIxLpCakAtB0rS+m49nuDpv9mHxvsfzrYB6BMw7Ty8iF2RE+rnazR+p3/0D98gOcSR8zBy4ZqHffEJ1A= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870090537501.3634704791791; Thu, 5 Nov 2026 01:14:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtYG-0005So-Ke; Thu, 05 Nov 2026 04:14:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtXl-00056o-On for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:06 -0500 Received: from mail-pl1-f171.google.com ([209.85.214.171]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtXj-0005Qe-Ms for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:01 -0500 Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2ed2b4348fcso1393975ad.2 for ; Thu, 05 Nov 2026 01:13:59 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ed091b5267sm15675255ad.62.2026.11.05.01.13.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:13:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870038; x=1794474838; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=YHMfEpRBCghPOlLiattQp/tdJSULFxB9dFeHDoCkMiY=; b=QEap/T3kHmPGPXZ9PvQ1Iqz8fSUbk33mkVweyXR4tBM0LMp5swdODwO+yYkCcl9qTf Z6WJzpQhvldRPK18MCR5chfDinjKFxt6/8mW3sDvpc8v2KWcBL31eldzmT2oLdc8mDs8 plup/KM3xIHWY6mj1W44arSalpCQRUZIcWGVX74IjLFmv9qUejf+ibEKVoSIvDsV+elR 1Avgu0knrnaOW7WIRuBmUJw1r8G+2+lCVqAvRp8rr54JFtLVub/sb7+af+zOTQ71aGf+ ZciMtAolrtm1FdbP/Afj9WTD2ALQsH9Q9QFb2YNE2SCvTo4MBKslsC3+rsk48NuUM8Ci i5VA== X-Gm-Message-State: APuU4Z+rLYGuUoIbANJxutCM7f6j5OQGYUdx4a7E6LD2JL3FbLJO8QqF wtacyvOlMuatLJg7RAjpWyfH+FE8Y7NCcb26eqkVu18k6X2Dez8ngFtglTWq711tcp8= X-Gm-Gg: AYBFou1y3Veupx5m0tC2ypPAq/tK8W+d/R7KV1wqfu4l3FflliYUaj/pxdRFQHyXKY+ AEiXaZtik6tWsaDMo35ll7ggMsZvpsblO6Z5cmUTZSsyeQLoop1/RTbwMztMCHhxb2XMJihvkKS hjYz5LEQ0a/uTfAXKPdwuZw4eOh+uL6ulCKdMTPh7OANuQ1BC3QTqY0wuRmEEjJ3OsWs8ncmDWs ceh69tlSvdgYMFv2LF284EwVBD9SKwZCOcIfxwgDu1Yw5KGynnOOxD91JYSR0GZRIq8sv8gJ0eN m0Uwn1BTlL1qeL+98oF3d4pjFSlsvKP0xAXEAQXOCN350bnXTcRuQVihBREOQFgsJ0G1o0NMHvX ByAEwqoovTZjpnSF6e+K9zRG7eNCdpOUlZiCUT1QeL6I8PfvM3ZJowGCvUPOxc3BnOW+rVRpgI9 VaB1opd7azsJRXZDNAQpxwLo6EdrVWZj9RRY1IzOHmqPZr3WlPChknDvcOkCWZabS4ow2xqrsUm hrru22PONoyhwUGFHnnEpHBWvCj5ArfmLpSX+ZCIctW2/RZ724iixOuwYjFG7zUMG68vK8hZ3NS aM1pOlBRBi2IePuhwwTZZb/8XMCmp74Vjm1fSo2B/jHtVuEKbOOWYkIRuhTadb03pRLnKsA4aJk S7FGT1A== X-Received: by 2002:a17:903:1c7:b0:2ed:1e4e:cf3e with SMTP id d9443c01a7336-2ed1e4ecf92mr26026975ad.29.1793870038170; Thu, 05 Nov 2026 01:13:58 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:55 +0800 Subject: [PATCH 03/16] tests/qtest: ina230: cover voltage injection and power derivation MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-3-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=5805; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=NCCZ3C0XT97Dof+ZcjxjWcN4sAhVvyKJXvbHGJ+/418=; b=ah1xyy0TKPIP1tY6f2ghQ1O4CkmAD/cIokJ2o5Lttmb5M/7CTt2H3JMCwpsIakM6A1vVl1E3v O4gjYBszuT5BQeE+KJ+D3MUiLecZi7Lmopr/vOwztx+U9BeuI3CxL+K X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.214.171; envelope-from=eblot.ml@gmail.com; helo=mail-pl1-f171.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870090989158500 Cover the shunt- and bus-voltage injection properties and the current and power values the device derives from them through the Calibration register, including the zero-calibration case and the clamp/overflow edges. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 124 ++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 124 insertions(+) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c index f7aefd2d97..b01f9445d1 100644 --- a/tests/qtest/ina230-test.c +++ b/tests/qtest/ina230-test.c @@ -49,6 +49,33 @@ =20 #define DIE_ID_VAL 0x2310 =20 +/* QMP helpers for injecting the physical inputs */ + +static void qmp_ina230_set(const char *property, int value) +{ + QDict *resp; + + resp =3D qmp("{ 'execute': 'qom-set', 'arguments':" + " { 'path': %s, 'property': %s, 'value': %d } }", + INA230_TEST_ID, property, value); + g_assert(qdict_haskey(resp, "return")); + qobject_unref(resp); +} + +static int qmp_ina230_get(const char *property) +{ + QDict *resp; + int ret; + + resp =3D qmp("{ 'execute': 'qom-get', 'arguments':" + " { 'path': %s, 'property': %s } }", + INA230_TEST_ID, property); + g_assert(qdict_haskey(resp, "return")); + ret =3D qdict_get_int(resp, "return"); + qobject_unref(resp); + return ret; +} + /* Power-on-reset default values and the Die ID */ static void test_defaults(void *obj, void *data, QGuestAllocator *alloc) { @@ -98,6 +125,96 @@ static void test_config_wmask(void *obj, void *data, QG= uestAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, 0x7FFF); } =20 +/* Shunt voltage register, including two's-complement negatives */ +static void test_shunt_injection(void *obj, void *data, QGuestAllocator *a= lloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x1F40); + + qmp_ina230_set("shunt-voltage", -80000000); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x8300); + + g_assert_cmpint(qmp_ina230_get("shunt-voltage"), =3D=3D, -80000000); +} + +/* Bus voltage register */ +static void test_bus_injection(void *obj, void *data, QGuestAllocator *all= oc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + qmp_ina230_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x2580); + + qmp_ina230_set("bus-voltage", 11980000); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x2570); +} + +/* Calibration-derived current, using the datasheet worked example */ +static void test_calibration_current(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x2710); +} + +/* Calibration-derived power, using the datasheet worked example */ +static void test_power(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + + g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, 0x12B8); +} + +/* With Calibration =3D=3D 0 the Current and Power registers stay at zero = */ +static void test_zero_cal(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, 0x0000); +} + +/* Clamping at full scale and the OVF (math overflow) flag */ +static void test_edges_overflow(void *obj, void *data, QGuestAllocator *al= loc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("shunt-voltage", 81917500); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x7FFF); + qmp_ina230_set("shunt-voltage", -81920000); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x8000); + + qmp_ina230_set("bus-voltage", 36000000); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x7080); + + qmp_ina230_set("shunt-voltage", 20000000); + i2c_set16(dev, REG_CALIBRATION, 0x7FFF); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x7FFF); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_OVF, =3D=3D, ME_O= VF); +} + static void ina230_register_nodes(void) { QOSGraphEdgeOptions opts =3D { @@ -111,5 +228,12 @@ static void ina230_register_nodes(void) qos_add_test("defaults", "ina230", test_defaults, NULL); qos_add_test("soft-reset", "ina230", test_soft_reset, NULL); qos_add_test("config-wmask", "ina230", test_config_wmask, NULL); + qos_add_test("shunt-injection", "ina230", test_shunt_injection, NULL); + qos_add_test("bus-injection", "ina230", test_bus_injection, NULL); + qos_add_test("calibration-current", "ina230", test_calibration_current, + NULL); + qos_add_test("power", "ina230", test_power, NULL); + qos_add_test("zero-cal", "ina230", test_zero_cal, NULL); + qos_add_test("edges-overflow", "ina230", test_edges_overflow, NULL); } libqos_init(ina230_register_nodes); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870181; cv=none; d=zohomail.com; s=zohoarc; b=UbiTQ75fPr40Wx7rQIyL3ldwxvP6bt1CeCyKUf6/QINJXWEWGrrhLGVfeaSdcZp2z9GJX++so6o+p6u/8MAonRT2MIpP39zG9gbXRbpZ/FIKjJ/C1kpI+iO5B4XikkD8YPZF7DcDbVBeCtukgCozA83MF+CaCLoG7gVAevywaFs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870181; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=DeiJYZqRfQOfLFJzRerKdRRjmQabvVc3EFQMHa8VS4A=; b=CCfgBXN/GhK2P/uH8lja0tbSlvOzw2kcZ9E/R++tx/bTwup3aj8Jkl9MwO+KBFZ4Vp/Xg6gJrB3chYYQruYLPAskmRDtFyAZs4ZJE0SDgY9jAHHhYiXtiWGBkERNayXebwheDVqmakWPZXPVFWU/VIz/vGZOTbd+qJxRI+Ba0xY= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870181672801.3032739083098; Thu, 5 Nov 2026 01:16:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtYI-0005a8-7l; Thu, 05 Nov 2026 04:14:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtXu-00057q-Di for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:13 -0500 Received: from mail-pl1-f169.google.com ([209.85.214.169]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtXr-0005RF-Hv for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:09 -0500 Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2e946155442so14223955ad.3 for ; Thu, 05 Nov 2026 01:14:07 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ed0908587dsm14858935ad.36.2026.11.05.01.14.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870045; x=1794474845; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=DeiJYZqRfQOfLFJzRerKdRRjmQabvVc3EFQMHa8VS4A=; b=tKZdBP/yRQFqH4Givc45z30gHE71N30qR6haQfiZ58blizGh2rknML9E2oJocVjEcf 4+81kqibLG8nLbkl6loNa5sri3fIEvGzfWetYgwmcDJf9bl42iKNKZ7eWJarfER4rn8R KNJ1UqEOm62+FiZW7IYOwUvB1JhlfyZih1hEb8s10dzbikvohyQsAIv8fuCwduswEqOf aC4jEA3ELN17RvZCexR4jnIqrOtsthpK/5jhaNRCwoFKzo1C7ruldqE/WRAJrmDl+LkH Cn6t+ieFz3DzgZGp3FxN4dPivbRgXY9/kw48ckIZf5Mh4SaItP2cNPe3vCOWFMLsnFy6 pzVg== X-Gm-Message-State: APuU4Z9C5nFMvtpN/5zPngUG/pyV4vAQJ94ARRLZb+39/zqqs2I2GbJV Df6O1UFi++S77JP7Sw/lJkPl0DkYhwxerm7QioH9lTeYs2/6c/cNJtoYg/Qnc6P9GGs= X-Gm-Gg: AYBFou00obMM9MPpfwu389jCCR9U7nyZuAeFIKUfOs18peoHz3Zp5D/9J91ZUJ2QUJp hYEGG3VWJlU7WnJq7kBtdgz/eZWA8cMKXHJ7L3NPtySJAJEdXMLzooX8ERq+WAhp1FdLclmd3cx TBb+l9wVMPw1t6LvJAoK9s5CIPSDVAsDMThOedH48jQ3ZMH/IwvzdVyktP9n1mhwfnbFFLxohgy 8jFGR3OEO9fQ5hcq3Vo9irfuPwW5y9TtUHncii/IT+875qMB2vdXwScoztFNVTRxjuu08XF1lqo HMC3MCO0WaMRbCDyuypPkptKxCRGd3gQbj0uOoIAQ/zA9xPdSVQy7bdm8Z3bDzLZcbt6/ZtrR/v /Z2RhNUVaM50vjheJWpBebsemnt0lDQIdciPD9YNkIu04r6LiQF4St9oQTCctx2Iaeq/KxWuXVT G+PtdxKLkdsf6OvkRMb/GjqgZqy4F7ODWdiyt+WnX+W7Ol4PudYP7NID1O6kf/WNsedlfg7i/To HG6LX58WSIoquHl/rGiwkGoXIFWo3trLgX0DoFuFZz6THCaB6e5wdjjFUZMqNBya3k4AwdlencV 0hcW1DVp6k08QsyzYusP6TqrK3d5o4BwV8Wu2bAXafrTQpqf1X/8FN3rl16gOcuEtsip9Wqkuco z3ewxWA== X-Received: by 2002:a17:902:ce8a:b0:2d7:107c:917b with SMTP id d9443c01a7336-2eb36332024mr176198365ad.0.1793870045573; Thu, 05 Nov 2026 01:14:05 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:56 +0800 Subject: [PATCH 04/16] tests/qtest: ina230: cover the ALERT limit functions and CVRF MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-4-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=4653; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=yU/ASNKVFYSLSDsLa8e4k+jfJS9UQPvsF84TMUUNANI=; b=4tOjud+fCdbZb4WevjO1TSXlTh+gTknSjTeQwMzztSEgjPgyy2Ty1ih0TmligVbQvB3UegSiL Q1uyiyY1jvWC+DkCOqV2k5RExk7ZUB6qW8B0WDal7PWEHF5SGikwhsV X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.214.169; envelope-from=eblot.ml@gmail.com; helo=mail-pl1-f169.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870182630158500 Cover the shunt/bus/power alert-limit functions, the alert latch behaviour and the conversion-ready flag. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 97 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 97 insertions(+) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c index b01f9445d1..1c55028fcb 100644 --- a/tests/qtest/ina230-test.c +++ b/tests/qtest/ina230-test.c @@ -215,6 +215,98 @@ static void test_edges_overflow(void *obj, void *data,= QGuestAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_OVF, =3D=3D, ME_O= VF); } =20 +/* Shunt over-limit alert (transparent mode) sets the AFF flag */ +static void test_alert_shunt_over(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + i2c_set16(dev, REG_ALERT_LIMIT, 0x1000); + i2c_set16(dev, REG_MASK_ENABLE, ME_SOL); + + qmp_ina230_set("shunt-voltage", 2500000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); + + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); + + qmp_ina230_set("shunt-voltage", 2500000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); +} + +/* Latch mode holds the alert until the Mask/Enable register is read */ +static void test_alert_latch(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + uint16_t me; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + i2c_set16(dev, REG_ALERT_LIMIT, 0x1000); + i2c_set16(dev, REG_MASK_ENABLE, ME_SOL | ME_LEN); + + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("shunt-voltage", 2500000); + + me =3D i2c_get16(dev, REG_MASK_ENABLE); + g_assert_cmphex(me & ME_AFF, =3D=3D, ME_AFF); + + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); +} + +/* Bus over-limit alert */ +static void test_alert_bus(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + i2c_set16(dev, REG_ALERT_LIMIT, 0x2000); + i2c_set16(dev, REG_MASK_ENABLE, ME_BOL); + + qmp_ina230_set("bus-voltage", 5000000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); + + qmp_ina230_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); +} + +/* Power over-limit alert */ +static void test_alert_power(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + + i2c_set16(dev, REG_ALERT_LIMIT, 0x1000); + i2c_set16(dev, REG_MASK_ENABLE, ME_POL); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); + + i2c_set16(dev, REG_ALERT_LIMIT, 0x2000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); +} + +/* Conversion Ready Flag: set on conversion, cleared by reading Mask/Enabl= e */ +static void test_cvrf(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + /* A conversion (triggered by injection) sets CVRF */ + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_CVRF, =3D=3D, ME_= CVRF); + + /* The previous read cleared CVRF; no conversion since, so it stays cl= ear */ + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_CVRF, =3D=3D, 0); +} + static void ina230_register_nodes(void) { QOSGraphEdgeOptions opts =3D { @@ -235,5 +327,10 @@ static void ina230_register_nodes(void) qos_add_test("power", "ina230", test_power, NULL); qos_add_test("zero-cal", "ina230", test_zero_cal, NULL); qos_add_test("edges-overflow", "ina230", test_edges_overflow, NULL); + qos_add_test("alert-shunt-over", "ina230", test_alert_shunt_over, NULL= ); + qos_add_test("alert-latch", "ina230", test_alert_latch, NULL); + qos_add_test("alert-bus", "ina230", test_alert_bus, NULL); + qos_add_test("alert-power", "ina230", test_alert_power, NULL); + qos_add_test("cvrf", "ina230", test_cvrf, NULL); } libqos_init(ina230_register_nodes); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870155; cv=none; d=zohomail.com; s=zohoarc; b=k5Hc+AEnp33tGY5HJY39Kxhnn0Nmi+R/GHoRe/gZNwOsRytFZgE+9EhgGxdjCer5+2OCPlr3J/GgGn3aY0NmlzoRPJ62Qlyymj9Qcc7bOldNwACsNlv8I3QHecnutScGyZXlwSbrnzgzq6a0L1jamboPz2bkWPjvylhWfq4cbtU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870155; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=AHne2JcHx+O7G4KfyAccP7ggw7u1GmKaKZ5l2ViY6Cs=; b=VLV/4MKiXC/yL5W6kjEbg+EXCy10UF/VOlwliVDHG5kA96HJs2jk8um/BmvIqQAtrHKa8nj0iwh+9eYCaUJTBcXKBfHSwOnzm6tQy7tE7bgnZOt8p5DlOlRX98zVXK4pJe7zhiSLv1+oUfS0CQ/gA9hhbS9+rkgE53FnqbCQJuk= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870155881920.0866847157514; Thu, 5 Nov 2026 01:15:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZF-0006wK-SD; Thu, 05 Nov 2026 04:15:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtY1-00059m-9X for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:18 -0500 Received: from mail-pg1-f179.google.com ([209.85.215.179]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtXz-0005Rj-AL for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:17 -0500 Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-cc4aa0246b7so798571a12.3 for ; Thu, 05 Nov 2026 01:14:14 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0991c13dcsm3789814a91.3.2026.11.05.01.14.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870053; x=1794474853; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=AHne2JcHx+O7G4KfyAccP7ggw7u1GmKaKZ5l2ViY6Cs=; b=qDqR42YAELBg4oQAqBVmungSDGrZ4ix7yppMHEENx3OwNEy4zmEnA2TXm5EB0AfQWD ZkQE724+rzVakF8gKiClBJJ2/vx5/66jBjK8n56wHRVOp6qMCTvO9+sAciavW4XFJrYA MDu1XONVyZScIhsjqV4VKzyigR1pxTiNpsuwRBdbNQvWTzMbUtVAz8oCsccWUFMVxxFW Q66PoyzkNbOq7qZNYyS7Te+ot+EaoOYxAg3hzeYQV5pHxQ61xpkMi1tWtAJyDa67rGlz h2EN4NR66H2gIzdHPJ6ibpUqfjqdjEGKAHkvqBDQKm5Ysd9Nmvum0xyDdkobVw7AESA1 37EA== X-Gm-Message-State: APuU4Z/ntLg+TtqOQuufwntebTm96MwBvZW72PpyfuP3TcbZz6O84y4H 328qA3IvIWzG77RrgRq6guZUfctf7gFfqU0rplywhJfd2MalhX2sLUcd95LGqaxO2hY= X-Gm-Gg: AYBFou1HzN72vuTgwhn1/juNbqIvrSu2YsPId/WMqYlOr881o0jYMK5om/pdjZQbDaQ oh7xMrUtvxIQ1/klAbuxDua9IEEg5p6L5OkS4XAte6iRqSHRsK5blVuUXfB/H3fq1pBamChqc+t YGd4s6iWcsD9FRnuLRjD+ckdEVHobCf2Rl4qkhR8Y+7kh7QuoRr+hIIi5uldz4gtXbppRXun1Fg pzvzOGyZRETMgyeRKfAGyfZ/6lcn2XqsTRINXORZ1gNicKo3KthI44ZzzPIVccF+QPM/ACPBpaZ vPp1LJrcAUB54X9J/fLBoXu0UVZS+QakhlgdyAMH+cGqAR30eZr993Pi5Lm5XDrQ0tyNU8f4ZY0 8txfL7uLRdYQupR6U2RsE10/fMPJ3popvYXDMXBOS6VaEZf5TrI63FRc55fdn1MmpczXWz/RXdB M4M/XMioLQrxZ49dd/b2lgoOW6T+0yL709eDZ8iLp047UXJFIyB/0jk+dRDxqAvYaXbzh1Id/Ve 3yCSdGU2HY5kYOSnilbGZUtcuCYaDOJB9nVIiqeLRxFRfvIyqze0Terws83YMmqDT9gj0OzUj5L XH1njLwrW3qCETaLifugkiFVNzkbSOYKHNp8DAk9RtwGHy2bw0mWtYs8KECkwiOIVHZuV83aa/0 tLJJHzw== X-Received: by 2002:a17:90b:2fd0:b0:3ac:af8b:6f3 with SMTP id 98e67ed59e1d1-3ae898149e2mr13957413a91.52.1793870053310; Thu, 05 Nov 2026 01:14:13 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:57 +0800 Subject: [PATCH 05/16] tests/qtest: ina230: cover the injection-property limits MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-5-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=4069; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=3UD9l/AEvavR8S7pGFg9evzwIlCmssJcXuU0S21eH1k=; b=vR+gD93r/yoe2J8+z6SuJuPnXXYk+GoOx9si90ZxklZZz/X3rkosbw4/iVcmbUUjNFcKjoEBL ZXyniqx/zsiDLX5yxTorBhHkZEhp6ReIvo6DW4gdEd3Gh9yHv2YXPiu X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.215.179; envelope-from=eblot.ml@gmail.com; helo=mail-pg1-f179.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870158207158500 Verify that the extremes of the shunt- and bus-voltage injection ranges round-trip through the QOM property without truncation or wraparound, and that values just outside are rejected rather than silently wrapped. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 59 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 59 insertions(+) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c index 1c55028fcb..36c899efee 100644 --- a/tests/qtest/ina230-test.c +++ b/tests/qtest/ina230-test.c @@ -47,6 +47,11 @@ #define SHUNT_LSB_NV 2500 #define BUS_LSB_UV 1250 =20 +/* Injection-property limits (see ina230.c) */ +#define SHUNT_MIN_NV (-81920000) +#define SHUNT_MAX_NV 81917500 +#define BUS_MAX_UV 36000000 + #define DIE_ID_VAL 0x2310 =20 /* QMP helpers for injecting the physical inputs */ @@ -76,6 +81,18 @@ static int qmp_ina230_get(const char *property) return ret; } =20 +/* A qom-set expected to be rejected (value outside the supported range) */ +static void qmp_ina230_set_fail(const char *property, int64_t value) +{ + QDict *resp; + + resp =3D qmp("{ 'execute': 'qom-set', 'arguments':" + " { 'path': %s, 'property': %s, 'value': %lld } }", + INA230_TEST_ID, property, (long long)value); + g_assert(qdict_haskey(resp, "error")); + qobject_unref(resp); +} + /* Power-on-reset default values and the Die ID */ static void test_defaults(void *obj, void *data, QGuestAllocator *alloc) { @@ -307,6 +324,46 @@ static void test_cvrf(void *obj, void *data, QGuestAll= ocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_CVRF, =3D=3D, 0); } =20 +/* Shunt-voltage injection limits */ +static void test_shunt_limits(void *obj, void *data, QGuestAllocator *allo= c) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("shunt-voltage", SHUNT_MAX_NV); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x7FFF); + g_assert_cmpint(qmp_ina230_get("shunt-voltage"), =3D=3D, SHUNT_MAX_NV); + + qmp_ina230_set("shunt-voltage", SHUNT_MIN_NV); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x8000); + g_assert_cmpint(qmp_ina230_get("shunt-voltage"), =3D=3D, SHUNT_MIN_NV); + + qmp_ina230_set_fail("shunt-voltage", (int64_t)SHUNT_MAX_NV + 1); + qmp_ina230_set_fail("shunt-voltage", (int64_t)SHUNT_MIN_NV - 1); + g_assert_cmpint(qmp_ina230_get("shunt-voltage"), =3D=3D, SHUNT_MIN_NV); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x8000); +} + +/* Bus-voltage injection limits. */ +static void test_bus_limits(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina230_set("bus-voltage", 0); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x0000); + + qmp_ina230_set("bus-voltage", BUS_MAX_UV); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x7080); + g_assert_cmpint(qmp_ina230_get("bus-voltage"), =3D=3D, BUS_MAX_UV); + + qmp_ina230_set_fail("bus-voltage", (int64_t)BUS_MAX_UV + 1); + qmp_ina230_set_fail("bus-voltage", -1); + g_assert_cmpint(qmp_ina230_get("bus-voltage"), =3D=3D, BUS_MAX_UV); +} + static void ina230_register_nodes(void) { QOSGraphEdgeOptions opts =3D { @@ -322,6 +379,8 @@ static void ina230_register_nodes(void) qos_add_test("config-wmask", "ina230", test_config_wmask, NULL); qos_add_test("shunt-injection", "ina230", test_shunt_injection, NULL); qos_add_test("bus-injection", "ina230", test_bus_injection, NULL); + qos_add_test("shunt-limits", "ina230", test_shunt_limits, NULL); + qos_add_test("bus-limits", "ina230", test_bus_limits, NULL); qos_add_test("calibration-current", "ina230", test_calibration_current, NULL); qos_add_test("power", "ina230", test_power, NULL); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870132; cv=none; d=zohomail.com; s=zohoarc; b=KFHEf1/ktUovKlMJ14vLfKp2VD7lBkMFOEa5Ui5P+r59wLHeao9wI+OwwkSVbDxu3zavS4CSu9yJicBmPWuUSAUNbSDGJykT2HzuUDM62HahGDyPJ9MmeYM8DQ2sB0G11rkp4ackTDsEftedlayvoPq4pM9yvSl5zNv6i650NcQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870132; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=AkDYJqCHPtazeSM46theCM6V5x6E3b7HmLtKVDIMVqg=; b=Vhtgeu5hEYgQ2Cks3w+OCOa1jvqwQvP/7xYU5MzEB82zOPFcJ3Mu69yAjAHJknxUSI1wSP80tT5Fyicm8UQhYMwTOfasWbagAckquik4RCc/hL1xCaSXgpQRfW9PkjyNVc9510taDD83MHkHC4k/rlTSeyhkKSmPKzwpoPaXBxA= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870132283980.7848646657577; Thu, 5 Nov 2026 01:15:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZ9-0006bB-CQ; Thu, 05 Nov 2026 04:15:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtY9-0005G4-13 for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:26 -0500 Received: from mail-pj1-f52.google.com ([209.85.216.52]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtY6-0005SQ-O5 for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:24 -0500 Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3b08a162d4bso1152454a91.3 for ; Thu, 05 Nov 2026 01:14:22 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b31aacedsm3158935a91.12.2026.11.05.01.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870061; x=1794474861; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=AkDYJqCHPtazeSM46theCM6V5x6E3b7HmLtKVDIMVqg=; b=p9eNUiWWAmkG8XT98WM/HbMHp+RKXTkcZF4DuKyUZGq67cauKBhQipB9x/6zNoDRBz d6ijeAt9XJWgTeP6r976VIfOsCZakTWGA0PfhavB6VyMowavfLbeElD6Z0dFBpKPDSuw VAXOjuRXL4aIzGbVng5bfrY5SFHOnwBnKlWcGVzXl3KYnUe7eUXUxYeZXqn/NduLDAvx rZKuu/ClcpqkskcW/GdJGVG096Qyzt9aUZRsmFhykYgMKhL0yd19iGrFb2ZiaU6NONFV rQi1agaYbwWThSw9PBoN80lvg9pylSba0jp9O0A4JJ6hLHucG3BMzUyLoTXxUuqluX8b Jklw== X-Gm-Message-State: APuU4Z+0B3mkk6a14MmL8C7HgoieGjDHp5mnoveHQRA3WkDvNpNKa3n4 VfRK+7ougTjdgoWBwFyNGzL0//Hf7enkiILD8ceWS0T5mboQT8t0YxFx+XA8v/Y0wLM= X-Gm-Gg: AYBFou3yNUBkzjq9+Qxg4/0TP0hH21ehPwcis2qJjK1ieMKuqeYY7cvQ8bWpj/Or/Kw uMP3LYIRa9YaIOoH9knmLDAmQFwiyC3Ce0EjlneApziHt08dKfPCxbuqZpBoZl8V3XfncsqkhfG W9V5yfLBwDdsoII4znjlxjzpTNlOlUro+hFFRZ7zbMtloZ10qgsJxysJ2G9d5pCx1gAueAUt7QM 4x3WMWyi2d9hr/rm/+sJRgPwrsRkAtOlJHn8kQgo0R6pHlwTgVOTatFEbnhMyiHTT4l8ak31UKO H5O1oDJ3q9Z8whytouKeCTmymdMSsoRfEE3B+m08DwPFwDCabzwhYl1p4z3Z02jFjbH3Zh/2TT5 LoJJEYXXRQDdxa++n6NUbAgUOxlCDYWmafP4Hec0/OnEq7Ckjik73edLZ+Y5KvoQKJa3QdwpFAZ BHVlmkhiYUNs/YrXS2Vf5au/qYlbXlC3+MfscXsfqprSKTh/nBuPyzZ+lG4HXRIjgBrKc1mqdli hBE0wVn6biTigl6UDl2+BY7ZPUVk5Tc1UFqRwG8/MZQh9ezCsKknFmMM+qB0DYhPVP88rUrJaXb 3ksXIBFB81pv2WufDzFxuR44u4JCAsR2g6ZXSbkTTqNOaLaClrLYkYtLY/0hihxMzm/xviCZCuo RAlcQZA== X-Received: by 2002:a17:90b:1b08:b0:3ab:884b:65ab with SMTP id 98e67ed59e1d1-3ae89857dadmr15979536a91.55.1793870061167; Thu, 05 Nov 2026 01:14:21 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:58 +0800 Subject: [PATCH 06/16] tests/qtest: ina230: cover operating modes and triggered conversions MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-6-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=5317; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=m8VC3AM0fSi1VDqiG42MezD8q4BvQ3Fjn35GMbgGH0A=; b=G0HN9+mXk77KzqdjJauFCgzYb0kTxlVgtzDsZX9oBlUkRUgHXHlz61DG52LpaN4s8M7x7XmcA mvq7n65bD/6CaNsgpSAyTZLZfbq6Nofv/VAZdp16NqUCFQuo9ITA4io X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.52; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f52.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870133879158500 Add coverage for the MODE field: power-down retains the last results, the shunt/bus channel-enable bits gate which registers track new inputs, triggered modes convert once per Config write, and bus-only mode still derives power from the retained current. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 95 +++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c index 36c899efee..4d601c74b5 100644 --- a/tests/qtest/ina230-test.c +++ b/tests/qtest/ina230-test.c @@ -142,7 +142,97 @@ static void test_config_wmask(void *obj, void *data, Q= GuestAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, 0x7FFF); } =20 -/* Shunt voltage register, including two's-complement negatives */ +/* Operating modes */ +static void test_modes(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + static const struct { + uint8_t mode; + uint16_t shunt, bus, current, power; + } cases[] =3D { + /* Power-down: no conversion, every register retains the baseline = */ + { 0x0, 0x1F40, 0x2570, 0x2710, 0x12B8 }, + { 0x4, 0x1F40, 0x2570, 0x2710, 0x12B8 }, + /* Shunt only (continuous): shunt and current track the probe */ + { 0x5, 0x0FA0, 0x2570, 0x1388, 0x12B8 }, + /* Shunt and bus (continuous): every register tracks the probe */ + { 0x7, 0x0FA0, 0x12C0, 0x1388, 0x04B0 }, + }; + size_t idx; + + for (idx =3D 0; idx < ARRAY_SIZE(cases); idx++) { + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + + i2c_set16(dev, REG_CONFIG, cases[idx].mode); + qmp_ina230_set("shunt-voltage", 10000000); + qmp_ina230_set("bus-voltage", 6000000); + + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, cases[idx].shun= t); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, cases[idx].bus); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, cases[idx].cu= rrent); + g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, cases[idx].powe= r); + } +} + +/* + * Triggered modes perform exactly one conversion per trigger, where the + * trigger is a Configuration-register write. Injected input changes + * between triggers are ignored until the next write. + */ +static void test_triggered(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + + i2c_set16(dev, REG_CONFIG, 0x03); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x1F40); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x2570); + + qmp_ina230_set("shunt-voltage", 10000000); + qmp_ina230_set("bus-voltage", 6000000); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x1F40); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x2570); + + i2c_set16(dev, REG_CONFIG, 0x03); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x0FA0); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x12C0); + + i2c_set16(dev, REG_CONFIG, 0x07); + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT), =3D=3D, 0x1F40); +} + +/* + * In a bus-only mode the current register is retained, but the power regi= ster + * is still refreshed on each bus conversion from that retained current. + */ +static void test_bus_only_power(void *obj, void *data, QGuestAllocator *al= loc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x2710); + + i2c_set16(dev, REG_CONFIG, 0x06); + qmp_ina230_set("bus-voltage", 6000000); + + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x2710); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x12C0); + g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, 0x0960); +} + +/* Shunt voltage register */ static void test_shunt_injection(void *obj, void *data, QGuestAllocator *a= lloc) { QI2CDevice *dev =3D (QI2CDevice *)obj; @@ -377,6 +467,9 @@ static void ina230_register_nodes(void) qos_add_test("defaults", "ina230", test_defaults, NULL); qos_add_test("soft-reset", "ina230", test_soft_reset, NULL); qos_add_test("config-wmask", "ina230", test_config_wmask, NULL); + qos_add_test("modes", "ina230", test_modes, NULL); + qos_add_test("triggered", "ina230", test_triggered, NULL); + qos_add_test("bus-only-power", "ina230", test_bus_only_power, NULL); qos_add_test("shunt-injection", "ina230", test_shunt_injection, NULL); qos_add_test("bus-injection", "ina230", test_bus_injection, NULL); qos_add_test("shunt-limits", "ina230", test_shunt_limits, NULL); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870214; cv=none; d=zohomail.com; s=zohoarc; b=Vj726eVVSD6soryQAWf5+rgKhD7nHWznOWZtMOS7rLCSJS5HDCrmXOZVRQz6gU673XvJ4cmWp/F93Y+ZLWfAFzGsSytBplqmp1tyXZgDMHusA8wek0Wn7k9uBIJEqaO3H5YoComsHGA+R+5sODWCx6/ybAFG62QWFHgDwnjcIH8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870214; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=SXVkV6BmIYa4CM52mZdbIuwD6cqbidYPD1oEQTmNrJw=; b=Q0PUD6VmPcrEhChq5zU7Qy/2ScON199WaeXoBxiwz9w5jXRTwhjAz7aFY0zpnZfeT2rOTZF7nqOqwYUGa0MYXZUoGsPWOtK8M4RUe/5sz0QdNx6uNWqN6LhWqQczHpzxO1fSyVjbnZ+yleSu5QQqRM4wWwAudSS+x8oaOhi2jAI= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870214113707.6944970710614; Thu, 5 Nov 2026 01:16:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZ7-0006LU-SF; Thu, 05 Nov 2026 04:15:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtYG-0005Zw-GJ for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:32 -0500 Received: from mail-pf1-f175.google.com ([209.85.210.175]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtYE-0005Tv-BU for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:32 -0500 Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-8a06210b29fso875909b3a.1 for ; Thu, 05 Nov 2026 01:14:29 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8b09c024a45sm1778162b3a.56.2026.11.05.01.14.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870068; x=1794474868; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=SXVkV6BmIYa4CM52mZdbIuwD6cqbidYPD1oEQTmNrJw=; b=2bYAix3DP4e7hS0uct8LW9MsEPCXzCCimvWSI9i5UwH0dTu1TzwTFX0Eh++D6E80fe wVk2iGJ/FT0et9x34Y58wb/K+fHzvvTTT4bO1ewHWXkuo9cuk+PTwcMSFPDkuI9zo9Hl l5F0n7hEu/i6X+0CcyPjtm2eDNWWpW0bF4wAAg5FSUt1Y4uWblPEINEdqv2GfyW5wYoQ WwbaRzlywTOePLtjBL+5L6cYQPlEzgBg+Jv7vB2Y+w/2KUAa9x5dXZJ5AiGlEN3Nxix9 k79KM1Lp2ESkbgHXBGFgnygd5OU7aPt9HvJ6lV8fSIIz4Q15ZYrMgGkaFq3mUEC0IK9Q IjMg== X-Gm-Message-State: APuU4Z+gs8oa7ywBZjKG9/f6dS3ccC4K5tyNB0GvbCw308F1Zy3Me02/ epEbYKWeO1S8C1nlhgHSIN3o0t/7kehUoA7kcHg3/u+QOlQix7YNeFeKuVBLVAMHyyo= X-Gm-Gg: AYBFou13onSRvFSWXpQhxt4cjeb1wMlxHVSVKDDC49lg8s9zYER3cEfenhTnsIvxyti 0OykvbGcGpQSHXuy3OfVJWGUQemsjbzC8W/og153uUbrRF2F3LbQGWtplxXvV0mQzqe+bXvPcjs Dne63Nu/+0AhvQi+Ck7sBqiq6pfKMkQobvPw6zzbbd1dWPTEzbPnz0NYI2k1EvaRQZsqc7FYTxT 6S9YpQWYua2UToKUd8s64S2CjIyu9/bzF0ssPUcL0oUvV6nZ6kUiTvaOOsi5GVU7Fe3H2CoFUbs 2Slj6lemZWHHeDL2ufvrY1dZUE7D/nqh5ui6ySVR0q6oSCFGZPwCr+wcMgACgJVwKvVV7/iGJ0E MWXyjw+7zunAxmJzJIols6gjOXcuUj48VIuroDXo+FPjEVbQF4ba8UcR7qBzGNwuO9i2Fwaa1VV 8svaLhBuw+47YupMr7Db2s6e3Q6ikSKBNjTlAfBerZDwINwF+Z00qIdyJaG8JDM0/8iFeVkof09 Otp4O2snqmi8Wy1JOG8NGUc3HgREmlolNsfKJJs8GuqEEfbgYcHA+ihtxIvWI0EeT+T5v5nvU76 yw7D1vbXrNUr8hPAHzf/kuqT6ezwlWizwEUxAP2nBl7jbqGOuPJY+EImf3z5NnkmblfRAknPopC Z7bAeUA== X-Received: by 2002:a05:6a21:9cc8:b0:3e2:ab48:c3e0 with SMTP id adf61e73a8af0-3e928c8f144mr16807537637.25.1793870068516; Thu, 05 Nov 2026 01:14:28 -0800 (PST) Date: Thu, 05 Nov 2026 17:12:59 +0800 Subject: [PATCH 07/16] tests/qtest: ina230: cover ALERT latch persistence MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-7-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869975; l=4970; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=W8bVV49/k/VdCQ886gBN1IQ0GLO/G1VK52xof55CHLg=; b=jjJhkCbtup9uUvlQ54dTHCLDEEpAPJBQz6AnzG7AzbX/OSHuRtw9Id3O9vdANNLNn4HzOgG2Q mt0kQiYdDnzApmTm34YijGDZzi7ajJaX6L9HauXAha2c+o9t72cQguh X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.210.175; envelope-from=eblot.ml@gmail.com; helo=mail-pf1-f175.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870215223158500 Verify what releases a latched alert and what can bring it back: a Mask/Enable read and a Configuration write both clear it, and only a conversion that re-samples the fault on a channel it converted asserts it again. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 89 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 89 insertions(+) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c index 4d601c74b5..cda52bb815 100644 --- a/tests/qtest/ina230-test.c +++ b/tests/qtest/ina230-test.c @@ -364,6 +364,88 @@ static void test_alert_latch(void *obj, void *data, QG= uestAllocator *alloc) } =20 /* Bus over-limit alert */ +static void test_alert_latch_persist(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_ALERT_LIMIT, 0x1000); + i2c_set16(dev, REG_MASK_ENABLE, ME_SOL | ME_LEN); + + qmp_ina230_set("shunt-voltage", 20000000); + + /* First read shows the latched AFF and clears the latch */ + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); + + /* Fault still present but no new conversion: AFF stays cleared */ + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); + + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); +} + +/* A Configuration write releases the latch as a Mask/Enable read does. */ +static void test_alert_latch_config(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_ALERT_LIMIT, 0x1000); + i2c_set16(dev, REG_MASK_ENABLE, ME_SOL | ME_LEN); + + /* Latch a fault, then bring the input back below the limit */ + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("shunt-voltage", 2500000); + + i2c_set16(dev, REG_CONFIG, CONFIG_POR); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); + + /* With the fault present, the reconversion latches it again */ + qmp_ina230_set("shunt-voltage", 20000000); + i2c_set16(dev, REG_CONFIG, CONFIG_POR); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); +} + +/* The releasing write need not reconvert: a power-down leaves it clear. */ +static void test_alert_latch_shutdown(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_ALERT_LIMIT, 0x1000); + i2c_set16(dev, REG_MASK_ENABLE, ME_SOL | ME_LEN); + + qmp_ina230_set("shunt-voltage", 20000000); + + i2c_set16(dev, REG_CONFIG, 0x0000); /* power-down */ + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); +} + +/* + * A conversion only re-samples the alert of a channel it converted: the b= us + * register a shunt-only conversion left stale must not re-latch. + */ +static void test_alert_channel(void *obj, void *data, QGuestAllocator *all= oc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_ALERT_LIMIT, 0x2000); + i2c_set16(dev, REG_MASK_ENABLE, ME_BOL | ME_LEN); + + qmp_ina230_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, ME_A= FF); + + /* Shunt-only mode: the bus register keeps its over-limit value */ + i2c_set16(dev, REG_CONFIG, 0x0005); + qmp_ina230_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_BUS), =3D=3D, 0x2580); + g_assert_cmphex(i2c_get16(dev, REG_MASK_ENABLE) & ME_AFF, =3D=3D, 0); +} + static void test_alert_bus(void *obj, void *data, QGuestAllocator *alloc) { QI2CDevice *dev =3D (QI2CDevice *)obj; @@ -481,6 +563,13 @@ static void ina230_register_nodes(void) qos_add_test("edges-overflow", "ina230", test_edges_overflow, NULL); qos_add_test("alert-shunt-over", "ina230", test_alert_shunt_over, NULL= ); qos_add_test("alert-latch", "ina230", test_alert_latch, NULL); + qos_add_test("alert-latch-persist", "ina230", test_alert_latch_persist, + NULL); + qos_add_test("alert-latch-config", "ina230", test_alert_latch_config, + NULL); + qos_add_test("alert-latch-shutdown", "ina230", test_alert_latch_shutdo= wn, + NULL); + qos_add_test("alert-channel", "ina230", test_alert_channel, NULL); qos_add_test("alert-bus", "ina230", test_alert_bus, NULL); qos_add_test("alert-power", "ina230", test_alert_power, NULL); qos_add_test("cvrf", "ina230", test_cvrf, NULL); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870156; cv=none; d=zohomail.com; s=zohoarc; b=Ad0dZk0HNMpKqA4mI4nhBjAmh4tfY8JbsCN5bVd7VqAs97mrtmcoLwO4pvaA4EGBf4QASkrsz/mLBNnO/Y8iG/TwCeGmLRtQbt5LNcSrxPx4V5D/mOqqA3V58xesaVSvg4qHaL0NP1152yXc8b0lIpopVcWwgAncitE/R0ff2VY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870156; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=eT68nxMn01vW7O3JJprDmDkWOIfn9yjPcnQ/rqWRCTw=; b=byo/Dr/E4VEfJsITrCLwB3uOJnt+fMrCtxKTpnOrXzG5id0VqyhG4Nwm8ImwCLWFthrPOwMuMK7FVZyVwGid5lW1rJq50Iiiq+FSHEa0fRzGdd8NTvExIIi6wCw3AIy7sKEeengbbC2at6Ik7SXuBpSz2QsJi+HOltG9QL7vARU= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870156919726.9339362205176; Thu, 5 Nov 2026 01:15:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZF-0006vb-AL; Thu, 05 Nov 2026 04:15:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtYO-0005et-5Y for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:41 -0500 Received: from mail-pj1-f53.google.com ([209.85.216.53]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtYM-0005V0-C6 for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:39 -0500 Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3b09a83d12aso861726a91.2 for ; Thu, 05 Nov 2026 01:14:38 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b312ccf1sm3041917a91.4.2026.11.05.01.14.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870077; x=1794474877; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=eT68nxMn01vW7O3JJprDmDkWOIfn9yjPcnQ/rqWRCTw=; b=a+vzQrywuF75aDLUdpWZ4zzV4iuf9+n9kBaKltefi6oFVc5BYQ/jFl8DhJiu5KFmHP w4GScl1ZL599dAPt0i7Gl82mdIjveCeQOrfFsQ8lD2kYo/q1IkqdBNoZVBofj3voVKax FwitjRlMi5N1zx7g51tcOIjepFWiERUd5unZyLmADSgBToVVQXV0nFDoZfzBPxnjkccN CyBjPXaNR6K1VkdoI7o0TnjVkZhgWFeaf2KZRNq49A9MFhuQMvRyKDtc4Q+rmIWlZvQj /F5LqI7OIAAJ9YmSzoZ7oLm6Dv2Uis9S7nctvSUfDfQn3zC52V+8aqK1/orjZUb8oeRN Fu/w== X-Gm-Message-State: APuU4Z+WJ2O/xD25JGr6hDtfkeWNl6JzlIlzu/EkqrEeaZn/juCgDR+B 5nL8VwIf+8DbhkMv18zMu57xBJRLIo5rxr44ms2R2pxuBR9Jro35RZstVZ09CsvNojI= X-Gm-Gg: AYBFou3f5DYEP0SQ7FFd/KZQME3CJwoMuGz9hgAvfrDn2qLqF9OPLk9p0IRP4FcjmyW by7jzFb7lrrM0Pv00/xO9sT43LMwwE/VfJSqWEdeTgndRJlUoVomuKvdLgY4K44E5CjkxlGMbIM DWzeBiSMezKs+/7N41tS86tu4G69fYbmDvQTWRt8MzMnC/gC4H8XMc1uqcD5by1CQUy1r4isS+4 DCWCXue7Ky3OrQhNDf8dgSdP4+gHdbClGZ8W3a+I79IVut0fP3HWtK5DZGipYlqTVQwOIJmVG5h ECDplRDe7hImupVtBWbHjvNET+WNrr6B0hXB9KZRRBOtdvIPke+Xx4cnXmJnwxCi8VhyFNy7yZC 826q3v+TnLLjuFYDCXvn50db3pNM8pf46tuoe+jFvQd/lAVfG/PYcXOtrbDGTI4Whqpkoj1t+vF TX0iv3CH0XbX6keOvO1ji/LlPB2mIODk4WkC/epdZAjiNPYoUM78G/Hq3bL2/xZh6S12X1/o50H Py+cbiUfTuD6GCfSER+V4+AX2IdsHA51xVWbCn78Fg/ELF9k6nNcK7o2JKwEpPiKDtJh7/l6d7N 8l1a0iyPhsWt2QioqeJSXmhh+NCGs8iBkJ4JldlCYFgwqRA3seJguqbQBQqCEWzi8XnLVpC34hP FbFsLSUI= X-Received: by 2002:a17:90b:3142:b0:3ab:4f1:4f7e with SMTP id 98e67ed59e1d1-3ae897f3919mr16515486a91.28.1793870076726; Thu, 05 Nov 2026 01:14:36 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:00 +0800 Subject: [PATCH 08/16] tests/qtest: ina230: cover a cleared calibration per channel MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-8-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=2313; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=PzxGOCXpiAe3gOLqCnGKqAPPjR61GoJKj4uRIt53Dm4=; b=dw+HLS3qQ8EcVN16hejxQOg25AAbwHowtHtJdk5TUCGrqpys9CvMfiepCRDWk2BUrL1THRFLZ kNJXxJBtpRPCnAvSGbjKzosx2aLgOey6GH0Q1Fj+ZEXYjgRnjirGv7y X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.53; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f53.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870158169158500 Verify that without a calibration value the current and power registers read zero, whichever channels the mode converts. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina230-test.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/qtest/ina230-test.c b/tests/qtest/ina230-test.c index cda52bb815..f9e8d3ab9f 100644 --- a/tests/qtest/ina230-test.c +++ b/tests/qtest/ina230-test.c @@ -301,6 +301,30 @@ static void test_zero_cal(void *obj, void *data, QGues= tAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, 0x0000); } =20 +/* Clearing the Calibration zeroes both derived registers in any mode */ +static void test_zero_cal_channels(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + static const uint8_t modes[] =3D { 0x5, 0x6, 0x7 }; + size_t idx; + + for (idx =3D 0; idx < ARRAY_SIZE(modes); idx++) { + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_CALIBRATION, 0x0A00); + qmp_ina230_set("shunt-voltage", 20000000); + qmp_ina230_set("bus-voltage", 11980000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), !=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_POWER), !=3D, 0x0000); + + i2c_set16(dev, REG_CONFIG, modes[idx]); + i2c_set16(dev, REG_CALIBRATION, 0x0000); + + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_POWER), =3D=3D, 0x0000); + } +} + /* Clamping at full scale and the OVF (math overflow) flag */ static void test_edges_overflow(void *obj, void *data, QGuestAllocator *al= loc) { @@ -560,6 +584,7 @@ static void ina230_register_nodes(void) NULL); qos_add_test("power", "ina230", test_power, NULL); qos_add_test("zero-cal", "ina230", test_zero_cal, NULL); + qos_add_test("zero-cal-channels", "ina230", test_zero_cal_channels, NU= LL); qos_add_test("edges-overflow", "ina230", test_edges_overflow, NULL); qos_add_test("alert-shunt-over", "ina230", test_alert_shunt_over, NULL= ); qos_add_test("alert-latch", "ina230", test_alert_latch, NULL); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870189; cv=none; d=zohomail.com; s=zohoarc; b=mB2zaEqPqRSP9kfFf1UyR6EDIJEdypIfYxJViU4JBN2tU5cRkz9Fb5IubNwhcV2NOcLyS6lNvIAivW60U67Qxo2vid/VuxnJTuMFsfAzhf9uQyBeZUi5wx0M4UxF4cRCxQGb3LXiEiYVBOiL9Nn6Sl6wRW9dk2GWYtgodiOnWEM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870189; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=CXR+/KAAps9Z3XarCtKZG/TlbEwKd0ihMMlC+D78HX8=; b=Sa2r7AWsoZ9Ho8tIfK3irBMC4DbKtmQrq7nUy7xdYlEtDD8CDWq7DEKpbqze/6Wb/O7ooDsZ6YW+SH7V1rdRWH3E7U+dfu1xm2Mit8TrLwjYwTsgOj1EWeuCxey78tUCMsp1lDyG30NuD11Kql4dKQ7+eG6WMtRSiIfaWVlKiNw= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870189469744.0394081019404; Thu, 5 Nov 2026 01:16:29 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZK-0007Cr-Ij; Thu, 05 Nov 2026 04:15:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtYX-000635-Og for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:55 -0500 Received: from mail-pj1-f43.google.com ([209.85.216.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtYT-0005Vg-MZ for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:49 -0500 Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-3b0b3438540so413735a91.0 for ; Thu, 05 Nov 2026 01:14:45 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b31cd6c0sm2915351a91.13.2026.11.05.01.14.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870084; x=1794474884; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=CXR+/KAAps9Z3XarCtKZG/TlbEwKd0ihMMlC+D78HX8=; b=VT2VhuXTgA6DtyCqiGDufSIqnmteJpelrv5DrGxLF0jxxm+BLYCLFszvZmPzq5F3k3 QE1e3TUdlNUg3UtfJ+Qbb0p14XJi3CHhDJSvYEBsAxeduT7P7EWEFdQoAg84QsI6Emls XA38iynKNQDu1t06l6Siek+H6XFsg2PAjhpfW3DEm5NX02HJ1F7UQYbxQz16NfuxoRDN vYDIYrGCdQFe1Pyt/n3RsUxHhrXLhYZw4dltNvG6dsmGcp/bIkS6WJ5ROaJDiOmy412k 0+cx73X+Mglq0HrScqvfFd4XbVyBZ55BYH+9PfqiQNazYVqMhxUE/yL78BaJRdBJZ/53 tcmw== X-Gm-Message-State: APuU4Z88oihJyW9nL5s3mH+Zfz8JjUZe/Ur7AlQuQyaKcLDcMbXWtYsL VWXml3njI78dKv5F1zAv4+0Y43A23KHeNLXEYWS8jnYlBloMFJCX8fFVSOzU44i23DM= X-Gm-Gg: AYBFou12R2zQdACBhBmWv711LDMKu/31Ur2vVkxBDkAA5kIqM/2SwKHAVjR1oen4Qdy 56FQp4kR/ZYEzDBloRaYsvEeVg2uYKViOHx752dWLoPcwFvT8msnyOrEVR1b7TDm4Z2y7gkB5en L3LwPxf2UacieFo6qPAOFG5GjdoGEg/JHrFPDzvM/G4wgfioQS0GzFoUaeWTuohZALa9RKWFQDu TMUV6A/6voT/8S051d+8DaE0p6hQLpo2DcnXSLWTGBGDAwdDyYSLkXBi731Q4tJ+TvSgtPtztIL L6/QucXDxadzZ0Y0Puh5rQrcQwdvMoRXpqPtwoOER+eSHCMl/rvFn7LFndruQ7n+pIJWz3FwBy4 PfSWwo/4tfF60mU4vHiuyfTlwKbS5JoVwjhpVi161cwswXLkur+3Wkbp8Xxu/LzZv1xYWuM358w 0u3Csvsh4tdRU/7GmdOBJCS5A2l8SRQCwFZh8aSOBaSBwCzqlQlBLhFR6C6UmEIlVlEOsKgmEuA AXoXKRc5/GwEx37GS9V5O+E6t5BfuWAwCCLnwcui5sG9z8fg/x2T20xOvb9YhK1cCIkmxBM5uGj oMQsEAFWCVMayovYApttKaE0zu+GIwpFWwa9VpO2zx2S/dGT0xXWej4PDfJpaQFQlMnrL9TCq8H z4cVDjDxK0DlonRrKj2pm0+qyef8= X-Received: by 2002:a17:90a:eb03:b0:3b0:92b2:2e74 with SMTP id 98e67ed59e1d1-3b092b2333dmr3254207a91.62.1793870084104; Thu, 05 Nov 2026 01:14:44 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:01 +0800 Subject: [PATCH 09/16] hw/sensor: ina238: add current/voltage/power monitor MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-9-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=32872; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=Qe/fjpUqvzX8eLWqIkypb57dSJT+r1k5PfSeezFNzpo=; b=1CWij5me2pCkQx6ImeiAIZeN9IVTLyxDmBuApX9us3ABr8Xbel80qY4FavpNnA+OMR9dimZ0/ Eij/z2CZp+hBSHPJaXcNphSUDxIgaisysSWKm5x2xMxUoqfXezoHRzu X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.43; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f43.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870190899158500 Add a model for the Texas Instruments INA238, an 85V, 16-bit I2C current-shunt, power and temperature monitor. The device measures a differential shunt voltage, a bus voltage and its own die temperature; the current and power registers are not measured directly but derived by the device from the shunt voltage and the guest-programmed Shunt Calibration register. The alert is modelled. Shunt and bus voltages and the die temperature can be injected through the "shunt-voltage", "bus-voltage" and "die-temperature" QOM properties. Note: Conversion is instantaneous: the averaging and conversion-time fields are stored but have no timing effect. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- hw/arm/Kconfig | 1 + hw/sensor/Kconfig | 4 + hw/sensor/ina238.c | 803 +++++++++++++++++++++++++++++++++++++++++= ++++ hw/sensor/meson.build | 1 + hw/sensor/trace-events | 7 + include/hw/sensor/ina238.h | 14 + 6 files changed, 830 insertions(+) diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig index 876acc0e43..bef5f65727 100644 --- a/hw/arm/Kconfig +++ b/hw/arm/Kconfig @@ -558,6 +558,7 @@ config ASPEED_SOC select TMP421 select EMC141X select INA230 + select INA238 select OR_IRQ select UNIMP select LED diff --git a/hw/sensor/Kconfig b/hw/sensor/Kconfig index da519159ac..9b7c76fcf5 100644 --- a/hw/sensor/Kconfig +++ b/hw/sensor/Kconfig @@ -6,6 +6,10 @@ config INA230 bool depends on I2C =20 +config INA238 + bool + depends on I2C + config TMP105 bool depends on I2C diff --git a/hw/sensor/ina238.c b/hw/sensor/ina238.c new file mode 100644 index 0000000000..57734e7935 --- /dev/null +++ b/hw/sensor/ina238.c @@ -0,0 +1,803 @@ +/* + * Texas Instruments INA238 85V, 16-bit current/voltage/power monitor + * with an I2C interface. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * The INA238 measures a differential shunt voltage, a bus voltage and its= own + * die temperature. Current and power are not measured directly: they are + * derived by the device from the measured shunt voltage and the programma= ble + * Shunt Calibration register. The INA238 exposes six independent limit + * registers, all monitored simultaneously (multi-alert), a die-temperature + * sensor, a 24-bit power register and an ADCRANGE-dependent shunt LSB. + * + * Limitations (not modelled): + * - Conversion timing is instantaneous. Conversions run synchronously whe= n an + * input, the calibration, the ADC range or the ADC configuration change= s; the + * ADC conversion times (VBUSCT/VSHCT/VTCT), the averaging count (AVG) a= nd the + * CONVDLY start delay are not modelled, and continuous mode does not + * re-convert on a timer. A read always returns the value derived from t= he + * current inputs and CNVRF is set once a conversion has run. + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "qapi/qapi-builtin-type-infos.h" +#include "qapi/visitor.h" +#include "qom/object.h" +#include "hw/sensor/ina238.h" +#include "hw/core/irq.h" +#include "hw/core/qdev-properties.h" +#include "hw/core/registerfields.h" +#include "hw/i2c/i2c.h" +#include "migration/vmstate.h" +#include "trace.h" + +#define INA238_REG_CONFIG 0x00 +#define INA238_REG_ADC_CONFIG 0x01 +#define INA238_REG_SHUNT_CAL 0x02 +#define INA238_REG_VSHUNT 0x04 +#define INA238_REG_VBUS 0x05 +#define INA238_REG_DIETEMP 0x06 +#define INA238_REG_CURRENT 0x07 +#define INA238_REG_POWER 0x08 +#define INA238_REG_DIAG_ALRT 0x0B +#define INA238_REG_SOVL 0x0C +#define INA238_REG_SUVL 0x0D +#define INA238_REG_BOVL 0x0E +#define INA238_REG_BUVL 0x0F +#define INA238_REG_TEMP_LIMIT 0x10 +#define INA238_REG_PWR_LIMIT 0x11 +#define INA238_REG_MANUFACTURER_ID 0x3E +#define INA238_REG_DEVICE_ID 0x3F + +FIELD(INA238_CONFIG, ADCRANGE, 4, 1) +FIELD(INA238_CONFIG, CONVDLY, 6, 8) +FIELD(INA238_CONFIG, RST, 15, 1) + +#define INA238_CONFIG_POR 0x0000 +#define INA238_CONFIG_WMASK (R_INA238_CONFIG_ADCRANGE_MASK | \ + R_INA238_CONFIG_CONVDLY_MASK) + +FIELD(INA238_ADC_CONFIG, AVG, 0, 3) +FIELD(INA238_ADC_CONFIG, VTCT, 3, 3) +FIELD(INA238_ADC_CONFIG, VSHCT, 6, 3) +FIELD(INA238_ADC_CONFIG, VBUSCT, 9, 3) +FIELD(INA238_ADC_CONFIG, MODE, 12, 4) + +#define INA238_ADC_CONFIG_POR 0xFB68 + +#define INA238_SHUNT_CAL_POR 0x1000 +#define INA238_SHUNT_CAL_MASK 0x7FFF /* bit 15 reserved */ + +FIELD(INA238_DIAG_ALRT, MEMSTAT, 0, 1) /* memory checksum ok (always 1)= */ +FIELD(INA238_DIAG_ALRT, CNVRF, 1, 1) /* conversion ready flag */ +FIELD(INA238_DIAG_ALRT, POL, 2, 1) /* power over-limit */ +FIELD(INA238_DIAG_ALRT, BUSUL, 3, 1) /* bus under-limit */ +FIELD(INA238_DIAG_ALRT, BUSOL, 4, 1) /* bus over-limit */ +FIELD(INA238_DIAG_ALRT, SHNTUL, 5, 1) /* shunt under-limit */ +FIELD(INA238_DIAG_ALRT, SHNTOL, 6, 1) /* shunt over-limit */ +FIELD(INA238_DIAG_ALRT, TMPOL, 7, 1) /* over temperature */ +FIELD(INA238_DIAG_ALRT, MATHOF, 9, 1) /* arithmetic overflow */ +FIELD(INA238_DIAG_ALRT, APOL, 12, 1) /* alert polarity */ +FIELD(INA238_DIAG_ALRT, SLOWALERT, 13, 1) /* alert on averaged value */ +FIELD(INA238_DIAG_ALRT, CNVR, 14, 1) /* conversion-ready on ALERT pin= */ +FIELD(INA238_DIAG_ALRT, ALATCH, 15, 1) /* alert latch enable */ + +#define INA238_DIAG_ALRT_POR 0x0001 /* MEMSTAT =3D 1 */ + +/* Control (writable) bits */ +#define INA238_DIAG_CTRL_MASK (R_INA238_DIAG_ALRT_ALATCH_MASK | \ + R_INA238_DIAG_ALRT_CNVR_MASK | \ + R_INA238_DIAG_ALRT_SLOWALERT_MASK | \ + R_INA238_DIAG_ALRT_APOL_MASK) +/* Read-only status bits (preserved across a control-bit write) */ +#define INA238_DIAG_STATUS_MASK (R_INA238_DIAG_ALRT_MATHOF_MASK | \ + R_INA238_DIAG_ALRT_TMPOL_MASK | \ + R_INA238_DIAG_ALRT_SHNTOL_MASK | \ + R_INA238_DIAG_ALRT_SHNTUL_MASK | \ + R_INA238_DIAG_ALRT_BUSOL_MASK | \ + R_INA238_DIAG_ALRT_BUSUL_MASK | \ + R_INA238_DIAG_ALRT_POL_MASK | \ + R_INA238_DIAG_ALRT_CNVRF_MASK | \ + R_INA238_DIAG_ALRT_MEMSTAT_MASK) +/* Threshold flags cleared when the DIAG_ALRT register is read */ +#define INA238_DIAG_LATCH_MASK (R_INA238_DIAG_ALRT_TMPOL_MASK | \ + R_INA238_DIAG_ALRT_SHNTOL_MASK | \ + R_INA238_DIAG_ALRT_SHNTUL_MASK | \ + R_INA238_DIAG_ALRT_BUSOL_MASK | \ + R_INA238_DIAG_ALRT_BUSUL_MASK | \ + R_INA238_DIAG_ALRT_POL_MASK) + +#define INA238_SOVL_POR 0x7FFF +#define INA238_SUVL_POR 0x8000 +#define INA238_BOVL_POR 0x7FFF +#define INA238_BUVL_POR 0x0000 +/* + * The datasheet is self-inconsistent on the TEMP_LIMIT reset value. + * Writes mask with INA238_TEMP_LIMIT_MASK and the alert comparison shifts= both + * operands >>4. + */ +#define INA238_TEMP_LIMIT_POR 0x7FF0 +#define INA238_PWR_LIMIT_POR 0xFFFF + +#define INA238_BUS_LIMIT_MASK 0x7FFF /* BOVL/BUVL are 15-bit, positiv= e */ +#define INA238_TEMP_LIMIT_MASK 0xFFF0 /* TOL occupies bits [15:4] */ + +#define INA238_MANUFACTURER_ID_VAL 0x5449 /* "TI" */ +#define INA238_DEVICE_ID_VAL 0x2381 /* DIEID 0x238, REV 0x1 */ + +/* Shunt voltage LSB depends on ADCRANGE; bus and temperature are fixed */ +#define INA238_SHUNT_LSB_NV_R0 5000 /* 5 uV per LSB (ADCRANGE =3D 0) = */ +#define INA238_SHUNT_LSB_NV_R1 1250 /* 1.25 uV per LSB (ADCRANGE =3D = 1) */ +#define INA238_BUS_LSB_UV 3125 /* 3.125 mV per LSB */ +#define INA238_TEMP_LSB_MC 125 /* 125 m-degC per LSB */ + +/* + * current =3D shunt_reg * K / SHUNT_CAL. K derives from the fixed 819.2e6 + * internal constant and the ADCRANGE=3D0 shunt LSB: 5e-6 * 819.2e6 =3D 40= 96. + * power =3D current * bus / 64 (0.2 / 3.125mV expressed in the register d= omain). + */ +#define INA238_CURRENT_K 4096 +#define INA238_POWER_DIV 64 +#define INA238_POWER_MAX 0xFFFFFF /* POWER is a 24-bit register */ + +/* Injection property ranges */ +#define INA238_SHUNT_MIN_NV (-163840000) /* -32768 * 5 uV */ +#define INA238_SHUNT_MAX_NV 163835000 /* 32767 * 5 uV */ +#define INA238_BUS_MAX_UV 85000000 /* 85 V full common-mode ra= nge */ +#define INA238_TEMP_MIN_MC (-40000) /* package limited */ +#define INA238_TEMP_MAX_MC 125000 + +OBJECT_DECLARE_SIMPLE_TYPE(INA238State, INA238) + +struct INA238State { + I2CSlave parent_obj; + + qemu_irq alert; /* ALERT open-drain output */ + + /* I2C transfer state */ + uint8_t ptr; /* register pointer */ + uint8_t len; /* bytes received in current transfer */ + uint8_t wr_hi; /* pending write MSB */ + uint8_t rx_byte; /* index of the next byte to return within rx_val */ + uint8_t rx_width; /* width in bytes of the register being read (2 or 3= ) */ + uint32_t rx_val; /* value latched at the start of a read */ + + /* Register file */ + uint16_t config; + uint16_t adc_config; + uint16_t shunt_cal; + uint16_t diag_alrt; + uint16_t sovl; + uint16_t suvl; + uint16_t bovl; + uint16_t buvl; + uint16_t temp_limit; + uint16_t pwr_limit; + + /* Derived measurement registers */ + uint16_t shunt_reg; + uint16_t bus_reg; + uint16_t dietemp_reg; + uint16_t current_reg; + uint32_t power_reg; /* 24-bit */ + + /* Injected physical inputs */ + int32_t shunt_nv; /* shunt voltage, nanovolts (signed: bidirectional)= */ + uint32_t bus_uv; /* bus voltage, microvolts (never negative) */ + int32_t temp_mc; /* die temperature, millidegrees Celsius (signed) */ + + char *description; +}; + +/* + * Update the _field_ limit flag in _diag_ from the current comparison _co= nd_. + * In latch mode (_latch_) a flag that is already set stays set until the + * register is read; in transparent mode it tracks _cond_ directly. A flag + * whose measurement register was not refreshed (_fresh_) keeps its value. + */ +#define INA238_UPD_FLAG(_diag_, _latch_, _fresh_, _field_, _cond_) = \ + do { = \ + if (_fresh_) { = \ + uint16_t _old_ =3D FIELD_EX16(_diag_, INA238_DIAG_ALRT, _field= _); \ + uint16_t _new_ =3D (uint16_t)((_cond_) || ((_latch_) && _old_)= ); \ + (_diag_) =3D FIELD_DP16(_diag_, INA238_DIAG_ALRT, _field_, _ne= w_); \ + } = \ + } while (0) + +static void ina238_refresh_alert(INA238State *s) +{ + uint16_t diag =3D s->diag_alrt; + bool active; + bool cnvr; + int level; + + /* MATHOF is a diagnostic; it does not drive the ALERT pin. */ + active =3D FIELD_EX16(diag, INA238_DIAG_ALRT, SHNTOL) || + FIELD_EX16(diag, INA238_DIAG_ALRT, SHNTUL) || + FIELD_EX16(diag, INA238_DIAG_ALRT, BUSOL) || + FIELD_EX16(diag, INA238_DIAG_ALRT, BUSUL) || + FIELD_EX16(diag, INA238_DIAG_ALRT, TMPOL) || + FIELD_EX16(diag, INA238_DIAG_ALRT, POL); + + cnvr =3D FIELD_EX16(diag, INA238_DIAG_ALRT, CNVR) && + FIELD_EX16(diag, INA238_DIAG_ALRT, CNVRF); + + active =3D active || cnvr; + /* APOL: 0 =3D active-low (default), 1 =3D active-high */ + level =3D FIELD_EX16(diag, INA238_DIAG_ALRT, APOL) ? active : !active; + + trace_ina238_alert(s->description, level); + qemu_set_irq(s->alert, level); +} + +/* + * @shunt_fresh, @bus_fresh and @temp_fresh tell which measurement registe= rs + * hold a fresh value: after a partial conversion the flags fed by the oth= er + * channels must not be re-evaluated against their stale registers. + */ +static void ina238_update_alert(INA238State *s, bool shunt_fresh, + bool bus_fresh, bool temp_fresh, + bool power_fresh) +{ + bool latch =3D FIELD_EX16(s->diag_alrt, INA238_DIAG_ALRT, ALATCH); + int16_t shunt =3D (int16_t)s->shunt_reg; + int16_t temp =3D (int16_t)s->dietemp_reg >> 4; /* sign-extend [15= :4] */ + int16_t temp_lim =3D (int16_t)s->temp_limit >> 4; + uint16_t diag =3D s->diag_alrt; + + INA238_UPD_FLAG(diag, latch, shunt_fresh, SHNTOL, shunt > (int16_t)s->= sovl); + INA238_UPD_FLAG(diag, latch, shunt_fresh, SHNTUL, shunt < (int16_t)s->= suvl); + INA238_UPD_FLAG(diag, latch, bus_fresh, BUSOL, + s->bus_reg > (s->bovl & INA238_BUS_LIMIT_MASK)); + INA238_UPD_FLAG(diag, latch, bus_fresh, BUSUL, + s->bus_reg < (s->buvl & INA238_BUS_LIMIT_MASK)); + INA238_UPD_FLAG(diag, latch, temp_fresh, TMPOL, temp > temp_lim); + INA238_UPD_FLAG(diag, latch, power_fresh, POL, + s->power_reg > ((uint32_t)s->pwr_limit << 8)); + + s->diag_alrt =3D diag; + + ina238_refresh_alert(s); +} + +static void ina238_convert(INA238State *s) +{ + uint8_t mode =3D FIELD_EX16(s->adc_config, INA238_ADC_CONFIG, MODE); + bool bus_en =3D mode & 0x1; + bool shunt_en =3D mode & 0x2; + bool temp_en =3D mode & 0x4; + bool adcrange =3D FIELD_EX16(s->config, INA238_CONFIG, ADCRANGE); + int shunt_lsb =3D adcrange ? INA238_SHUNT_LSB_NV_R1 : INA238_SHUNT_LSB= _NV_R0; + int64_t sr, br, tr, cur, pwr; + uint16_t mathof =3D 0; + bool power_fresh =3D bus_en; + + if (!bus_en && !shunt_en && !temp_en) { + return; + } + + if (shunt_en) { + sr =3D s->shunt_nv / shunt_lsb; + sr =3D MAX(INT16_MIN, MIN(INT16_MAX, sr)); + s->shunt_reg =3D (uint16_t)(int16_t)sr; + + if (s->shunt_cal =3D=3D 0) { + cur =3D 0; + } else { + cur =3D (int64_t)(int16_t)s->shunt_reg * INA238_CURRENT_K / + s->shunt_cal; + } + if (cur > INT16_MAX || cur < INT16_MIN) { + mathof =3D 1; + cur =3D MAX(INT16_MIN, MIN(INT16_MAX, cur)); + } + s->current_reg =3D (uint16_t)(int16_t)cur; + } + + if (bus_en) { + br =3D s->bus_uv / INA238_BUS_LSB_UV; + br =3D MAX(0, MIN(INT16_MAX, br)); + s->bus_reg =3D (uint16_t)br; + } + + if (temp_en) { + /* Die temperature: 12-bit signed value stored in bits [15:4] */ + tr =3D s->temp_mc / INA238_TEMP_LSB_MC; + tr =3D MAX(-2048, MIN(2047, tr)); + s->dietemp_reg =3D ((uint16_t)(int16_t)tr << 4) & INA238_TEMP_LIMI= T_MASK; + } + + /* + * In bus-only mode the current register is retained from the last shu= nt + * conversion rather than measured anew, but the power register is sti= ll + * refreshed. + */ + if (bus_en) { + cur =3D (int16_t)s->current_reg; + if (cur < 0) { + cur =3D -cur; + } + pwr =3D cur * (int64_t)s->bus_reg / INA238_POWER_DIV; + if (pwr > INA238_POWER_MAX) { + mathof =3D 1; + pwr =3D INA238_POWER_MAX; + } + s->power_reg =3D (uint32_t)pwr; + } + + /* Without a calibration the device reports neither current nor power.= */ + if (s->shunt_cal =3D=3D 0) { + s->current_reg =3D 0; + s->power_reg =3D 0; + power_fresh =3D true; + } + + s->diag_alrt =3D FIELD_DP16(s->diag_alrt, INA238_DIAG_ALRT, MATHOF, ma= thof); + s->diag_alrt =3D FIELD_DP16(s->diag_alrt, INA238_DIAG_ALRT, CNVRF, 1); + + trace_ina238_convert(s->description, s->shunt_reg, s->bus_reg, + s->dietemp_reg, s->current_reg, s->power_reg); + + ina238_update_alert(s, shunt_en, bus_en, temp_en, power_fresh); +} + +/* + * A physical input, the calibration or the ADC range changed. + * - in continuous mode the device keeps converting and immediately tracks + * the new value + * - in a triggered mode it performs exactly one conversion per trigger + */ +static void ina238_input_update(INA238State *s) +{ + if (FIELD_EX16(s->adc_config, INA238_ADC_CONFIG, MODE) & 0x8) { + ina238_convert(s); + } +} + +static void ina238_reset_regs(INA238State *s) +{ + s->config =3D INA238_CONFIG_POR; + s->adc_config =3D INA238_ADC_CONFIG_POR; + s->shunt_cal =3D INA238_SHUNT_CAL_POR; + s->diag_alrt =3D INA238_DIAG_ALRT_POR; + s->sovl =3D INA238_SOVL_POR; + s->suvl =3D INA238_SUVL_POR; + s->bovl =3D INA238_BOVL_POR; + s->buvl =3D INA238_BUVL_POR; + s->temp_limit =3D INA238_TEMP_LIMIT_POR; + s->pwr_limit =3D INA238_PWR_LIMIT_POR; + + s->ptr =3D 0; + s->len =3D 0; + s->wr_hi =3D 0; + s->rx_byte =3D 0; + s->rx_width =3D 0; + s->rx_val =3D 0; + + /* + * The measurement registers power up at zero and stay zero until the = next + * conversion. The injected physical inputs are retained, but no conve= rsion + * is performed here, so a software reset does not resurrect a previous + * measurement. + */ + s->shunt_reg =3D 0; + s->bus_reg =3D 0; + s->dietemp_reg =3D 0; + s->current_reg =3D 0; + s->power_reg =3D 0; + + ina238_update_alert(s, true, true, true, true); +} + +static uint32_t ina238_read_reg(INA238State *s, uint8_t reg) +{ + uint32_t val; + + switch (reg) { + case INA238_REG_CONFIG: + val =3D s->config; + break; + case INA238_REG_ADC_CONFIG: + val =3D s->adc_config; + break; + case INA238_REG_SHUNT_CAL: + val =3D s->shunt_cal; + break; + case INA238_REG_VSHUNT: + val =3D s->shunt_reg; + break; + case INA238_REG_VBUS: + val =3D s->bus_reg; + break; + case INA238_REG_DIETEMP: + val =3D s->dietemp_reg; + break; + case INA238_REG_CURRENT: + val =3D s->current_reg; + break; + case INA238_REG_POWER: + val =3D s->power_reg; + break; + case INA238_REG_DIAG_ALRT: + val =3D s->diag_alrt; + /* + * A read always clears CNVRF; the latched limit flags clear too, = but + * only in latch mode. + */ + s->diag_alrt &=3D ~R_INA238_DIAG_ALRT_CNVRF_MASK; + if (FIELD_EX16(s->diag_alrt, INA238_DIAG_ALRT, ALATCH)) { + s->diag_alrt &=3D ~INA238_DIAG_LATCH_MASK; + } + ina238_refresh_alert(s); + break; + case INA238_REG_SOVL: + val =3D s->sovl; + break; + case INA238_REG_SUVL: + val =3D s->suvl; + break; + case INA238_REG_BOVL: + val =3D s->bovl; + break; + case INA238_REG_BUVL: + val =3D s->buvl; + break; + case INA238_REG_TEMP_LIMIT: + val =3D s->temp_limit; + break; + case INA238_REG_PWR_LIMIT: + val =3D s->pwr_limit; + break; + case INA238_REG_MANUFACTURER_ID: + val =3D INA238_MANUFACTURER_ID_VAL; + break; + case INA238_REG_DEVICE_ID: + val =3D INA238_DEVICE_ID_VAL; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: read from undefined register 0x%02x\n", + __func__, s->description, reg); + val =3D 0; + break; + } + + trace_ina238_read(s->description, reg, val); + return val; +} + +static void ina238_write_reg(INA238State *s, uint8_t reg, uint16_t val) +{ + trace_ina238_write(s->description, reg, val); + + switch (reg) { + case INA238_REG_CONFIG: + if (val & R_INA238_CONFIG_RST_MASK) { + trace_ina238_reset(s->description, "reg"); + ina238_reset_regs(s); + break; + } + s->config =3D val & INA238_CONFIG_WMASK; + ina238_input_update(s); + break; + case INA238_REG_ADC_CONFIG: + s->adc_config =3D val; + /* Writing the MODE bits (re)starts a conversion: it is the trigge= r. */ + ina238_convert(s); + break; + case INA238_REG_SHUNT_CAL: + s->shunt_cal =3D val & INA238_SHUNT_CAL_MASK; + ina238_input_update(s); + break; + case INA238_REG_DIAG_ALRT: { + uint16_t status =3D s->diag_alrt & INA238_DIAG_STATUS_MASK; + /* + * In latch mode a write clears the latched limit flags and the + * conversion-ready flag. In transparent mode the flags track the = live + * condition and are preserved. The pin is only refreshed, so a cl= eared + * flag re-asserts on the next conversion rather than immediately. + */ + if (FIELD_EX16(s->diag_alrt, INA238_DIAG_ALRT, ALATCH)) { + status &=3D ~(R_INA238_DIAG_ALRT_CNVRF_MASK | INA238_DIAG_LATC= H_MASK); + } + s->diag_alrt =3D (val & INA238_DIAG_CTRL_MASK) | status; + ina238_refresh_alert(s); + break; + } + case INA238_REG_SOVL: + s->sovl =3D val; + ina238_update_alert(s, true, true, true, true); + break; + case INA238_REG_SUVL: + s->suvl =3D val; + ina238_update_alert(s, true, true, true, true); + break; + case INA238_REG_BOVL: + s->bovl =3D val & INA238_BUS_LIMIT_MASK; + ina238_update_alert(s, true, true, true, true); + break; + case INA238_REG_BUVL: + s->buvl =3D val & INA238_BUS_LIMIT_MASK; + ina238_update_alert(s, true, true, true, true); + break; + case INA238_REG_TEMP_LIMIT: + s->temp_limit =3D val & INA238_TEMP_LIMIT_MASK; + ina238_update_alert(s, true, true, true, true); + break; + case INA238_REG_PWR_LIMIT: + s->pwr_limit =3D val; + ina238_update_alert(s, true, true, true, true); + break; + case INA238_REG_VSHUNT: + case INA238_REG_VBUS: + case INA238_REG_DIETEMP: + case INA238_REG_CURRENT: + case INA238_REG_POWER: + case INA238_REG_MANUFACTURER_ID: + case INA238_REG_DEVICE_ID: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: write to read-only register 0x%02x\n", + __func__, s->description, reg); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: write to undefined register 0x%02x\n", + __func__, s->description, reg); + break; + } +} + +static uint8_t ina238_recv(I2CSlave *i2c) +{ + INA238State *s =3D INA238(i2c); + uint8_t byte; + + if (s->rx_byte =3D=3D 0) { + s->rx_val =3D ina238_read_reg(s, s->ptr); + s->rx_width =3D (s->ptr =3D=3D INA238_REG_POWER) ? 3 : 2; + } + + byte =3D (s->rx_val >> (8 * (s->rx_width - 1 - s->rx_byte))) & 0xff; + s->rx_byte++; + if (s->rx_byte >=3D s->rx_width) { + s->rx_byte =3D 0; + } + + return byte; +} + +static int ina238_send(I2CSlave *i2c, uint8_t data) +{ + INA238State *s =3D INA238(i2c); + + if (s->len =3D=3D 0) { + s->ptr =3D data; + } else if ((s->len & 1) =3D=3D 1) { + s->wr_hi =3D data; + } else { + ina238_write_reg(s, s->ptr, ((uint16_t)s->wr_hi << 8) | data); + } + s->len++; + + return 0; +} + +static int ina238_event(I2CSlave *i2c, enum i2c_event event) +{ + INA238State *s =3D INA238(i2c); + + s->len =3D 0; + s->rx_byte =3D 0; + + return 0; +} + +static void ina238_get_shunt(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA238State *s =3D INA238(obj); + int64_t value =3D s->shunt_nv; + + visit_type_int(v, name, &value, errp); +} + +static void ina238_set_shunt(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA238State *s =3D INA238(obj); + int64_t value; + + if (!visit_type_int(v, name, &value, errp)) { + return; + } + + if (value < INA238_SHUNT_MIN_NV || value > INA238_SHUNT_MAX_NV) { + error_setg(errp, + "%s: shunt-voltage %" PRId64 " out of range (%d..%d nV)= ", + s->description, value, + INA238_SHUNT_MIN_NV, INA238_SHUNT_MAX_NV); + return; + } + + s->shunt_nv =3D (int32_t)value; + ina238_input_update(s); +} + +static void ina238_get_bus(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA238State *s =3D INA238(obj); + int64_t value =3D s->bus_uv; + + visit_type_int(v, name, &value, errp); +} + +static void ina238_set_bus(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA238State *s =3D INA238(obj); + int64_t value; + + if (!visit_type_int(v, name, &value, errp)) { + return; + } + + if (value < 0 || value > INA238_BUS_MAX_UV) { + error_setg(errp, + "%s: bus-voltage %" PRId64 " out of range (0..%d uV)", + s->description, value, INA238_BUS_MAX_UV); + return; + } + + s->bus_uv =3D (uint32_t)value; + ina238_input_update(s); +} + +static void ina238_get_temp(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA238State *s =3D INA238(obj); + int64_t value =3D s->temp_mc; + + visit_type_int(v, name, &value, errp); +} + +static void ina238_set_temp(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + INA238State *s =3D INA238(obj); + int64_t value; + + if (!visit_type_int(v, name, &value, errp)) { + return; + } + + if (value < INA238_TEMP_MIN_MC || value > INA238_TEMP_MAX_MC) { + error_setg(errp, + "%s: die-temperature %" PRId64 + " out of range (%d..%d m-degC)", + s->description, value, + INA238_TEMP_MIN_MC, INA238_TEMP_MAX_MC); + return; + } + + s->temp_mc =3D (int32_t)value; + ina238_input_update(s); +} + +static void ina238_reset_hold(Object *obj, ResetType type) +{ + INA238State *s =3D INA238(obj); + + trace_ina238_reset(s->description, "hw"); + ina238_reset_regs(s); +} + +static int ina238_post_load(void *opaque, int version_id) +{ + INA238State *s =3D opaque; + + ina238_refresh_alert(s); + + return 0; +} + +static const VMStateDescription ina238_vmstate =3D { + .name =3D "INA238", + .version_id =3D 1, + .minimum_version_id =3D 1, + .post_load =3D ina238_post_load, + .fields =3D (const VMStateField[]) { + VMSTATE_UINT8(ptr, INA238State), + VMSTATE_UINT8(len, INA238State), + VMSTATE_UINT8(wr_hi, INA238State), + VMSTATE_UINT8(rx_byte, INA238State), + VMSTATE_UINT8(rx_width, INA238State), + VMSTATE_UINT32(rx_val, INA238State), + VMSTATE_UINT16(config, INA238State), + VMSTATE_UINT16(adc_config, INA238State), + VMSTATE_UINT16(shunt_cal, INA238State), + VMSTATE_UINT16(diag_alrt, INA238State), + VMSTATE_UINT16(sovl, INA238State), + VMSTATE_UINT16(suvl, INA238State), + VMSTATE_UINT16(bovl, INA238State), + VMSTATE_UINT16(buvl, INA238State), + VMSTATE_UINT16(temp_limit, INA238State), + VMSTATE_UINT16(pwr_limit, INA238State), + VMSTATE_UINT16(shunt_reg, INA238State), + VMSTATE_UINT16(bus_reg, INA238State), + VMSTATE_UINT16(dietemp_reg, INA238State), + VMSTATE_UINT16(current_reg, INA238State), + VMSTATE_UINT32(power_reg, INA238State), + VMSTATE_INT32(shunt_nv, INA238State), + VMSTATE_UINT32(bus_uv, INA238State), + VMSTATE_INT32(temp_mc, INA238State), + VMSTATE_I2C_SLAVE(parent_obj, INA238State), + VMSTATE_END_OF_LIST() + } +}; + +static void ina238_initfn(Object *obj) +{ + object_property_add(obj, "shunt-voltage", "int", &int_type_info, + ina238_get_shunt, ina238_set_shunt, NULL, NULL); + object_property_set_description(obj, "shunt-voltage", + "Injected shunt (differential) voltage= , " + "in nanovolts"); + object_property_add(obj, "bus-voltage", "int", &int_type_info, + ina238_get_bus, ina238_set_bus, NULL, NULL); + object_property_set_description(obj, "bus-voltage", + "Injected bus voltage, in microvolts"); + object_property_add(obj, "die-temperature", "int", &int_type_info, + ina238_get_temp, ina238_set_temp, NULL, NULL); + object_property_set_description(obj, "die-temperature", + "Injected die temperature, " + "in millidegrees Celsius"); +} + +static void ina238_realize(DeviceState *dev, Error **errp) +{ + INA238State *s =3D INA238(dev); + + if (!s->description) { + s->description =3D g_strdup(object_get_typename(OBJECT(dev))); + } + + qdev_init_gpio_out(dev, &s->alert, 1); +} + +static const Property ina238_properties[] =3D { + DEFINE_PROP_STRING("description", INA238State, description), +}; + +static void ina238_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + I2CSlaveClass *ic =3D I2C_SLAVE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + + ic->event =3D ina238_event; + ic->recv =3D ina238_recv; + ic->send =3D ina238_send; + dc->realize =3D ina238_realize; + rc->phases.hold =3D ina238_reset_hold; + dc->vmsd =3D &ina238_vmstate; + device_class_set_props(dc, ina238_properties); +} + +static const TypeInfo ina238_types[] =3D { + { + .name =3D TYPE_INA238, + .parent =3D TYPE_I2C_SLAVE, + .instance_init =3D ina238_initfn, + .instance_size =3D sizeof(INA238State), + .class_init =3D ina238_class_init, + }, +}; + +DEFINE_TYPES(ina238_types) diff --git a/hw/sensor/meson.build b/hw/sensor/meson.build index b81f001fdf..7e6fd64488 100644 --- a/hw/sensor/meson.build +++ b/hw/sensor/meson.build @@ -1,5 +1,6 @@ system_ss.add(when: 'CONFIG_ADC128D818', if_true: files('adc128d818.c')) system_ss.add(when: 'CONFIG_INA230', if_true: files('ina230.c')) +system_ss.add(when: 'CONFIG_INA238', if_true: files('ina238.c')) system_ss.add(when: 'CONFIG_TMP105', if_true: files('tmp105.c')) system_ss.add(when: 'CONFIG_TMP421', if_true: files('tmp421.c')) system_ss.add(when: 'CONFIG_DPS310', if_true: files('dps310.c')) diff --git a/hw/sensor/trace-events b/hw/sensor/trace-events index f809b6b506..98158e37c6 100644 --- a/hw/sensor/trace-events +++ b/hw/sensor/trace-events @@ -15,6 +15,13 @@ ina230_convert(const char *id, uint16_t shunt, uint16_t = bus, uint16_t current, u ina230_alert(const char *id, bool level) "%s level %u" ina230_reset(const char *id, const char *source) "%s %s" =20 +# ina238.c +ina238_read(const char *id, uint8_t reg, uint32_t value) "%s reg 0x%02x va= l 0x%06x" +ina238_write(const char *id, uint8_t reg, uint16_t value) "%s reg 0x%02x v= al 0x%04x" +ina238_convert(const char *id, uint16_t shunt, uint16_t bus, uint16_t diet= emp, uint16_t current, uint32_t power) "%s shunt 0x%04x bus 0x%04x dietemp = 0x%04x current 0x%04x power 0x%06x" +ina238_alert(const char *id, bool level) "%s level %u" +ina238_reset(const char *id, const char *source) "%s %s" + # tmp105.c tmp105_read(uint8_t dev, uint8_t addr) "device: 0x%02x, addr: 0x%02x" tmp105_write(uint8_t dev, uint8_t addr) "device: 0x%02x, addr 0x%02x" diff --git a/include/hw/sensor/ina238.h b/include/hw/sensor/ina238.h new file mode 100644 index 0000000000..3bc3c02073 --- /dev/null +++ b/include/hw/sensor/ina238.h @@ -0,0 +1,14 @@ +/* + * Texas Instruments INA238 current/voltage/power monitor with I2C interfa= ce + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_SENSOR_INA238_H +#define HW_SENSOR_INA238_H + +#define TYPE_INA238 "ina238" + +#endif --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870188; cv=none; d=zohomail.com; s=zohoarc; b=EDwG6OPWmJNTUK6EG6rNtULLcP8HjYfSUzlixJk2hgf88tF3McFDCvoXVP1aYluupTCKZYurrhMyz+CmOt+zUigK7OirADHcLIgmTm+nqSOmIy1yLMQ6r1Zu0VEAl38iCeqR6IOO0PTtkJoyRTgD4378Df3gCWTSJNtbS7vaEwA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870188; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=WqbZUjEcz/I/D1nVZz+I6I8/ReCtwIeQack+dKdCEoE=; b=I/DWC+RpVrzAdGaqBma2pg0matm0pzVKU5Ocb7NSdcVUdkc4Q8OxHpPcuhYL4BsHztIBr+dsV2zX2mSRQeYBR2jMf0olwtxsayybkJwUq4igne9nGNl4hc9DRfTW5J5rmrdULxRDk5rrJyhp35BBnWhqpZwyDSv0bVoHsO9XJ2I= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870188478766.6187111218032; Thu, 5 Nov 2026 01:16:28 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZZ-00006f-PV; Thu, 05 Nov 2026 04:15:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtYe-00066A-Md for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:58 -0500 Received: from mail-pf1-f170.google.com ([209.85.210.170]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtYc-0005WC-JO for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:14:56 -0500 Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-8a064fac8cbso1292427b3a.1 for ; Thu, 05 Nov 2026 01:14:54 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cdb46972143sm1863673a12.22.2026.11.05.01.14.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870093; x=1794474893; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=WqbZUjEcz/I/D1nVZz+I6I8/ReCtwIeQack+dKdCEoE=; b=plYLVOgvIYDPf9hl2MC5WXPVEqoq1zpj1R48k6Anko1724G3P3s1Byq9Y7S2XaSaoV j1W0VEpT/17dpC/dEVdRvjoBYMujbyRR7ssuc8JmwkZCwGQTkW0IHb0D1emUhjYU0Nl3 /KPxY4pOk1mIZR3bVddROchHh01zkxYlP2vQSjMAaLH9v1uGbL2tc+tnm/+j0rkdESXL 9KH3EV2HyBBg8wzy6wumA0LGsmSSLlZ3wzEfCvre/J65CBpABwiHoeKOz4BBiIigc2zv z1dmI2J/RJCU7P54qhx/85XPtG2kF+NsCiUoV55TYT4Qq9h/rMPx+Z3DNKewHbMrEF9C uMtA== X-Gm-Message-State: APuU4Z+MGVetF7Vnv5Jumak8Vx9sKnvMEzFtslkhu1WNgpCNIhl67TOX wktVRa8QBtbaN6OUPL/CtzTnzw01Ffv+a9/BTpqDiJ9mfW+WXx/QOdrnBuY9RgOL21Q= X-Gm-Gg: AYBFou2uGKwZ5Deu8C3mQ8TmFPna+26JEFrmxclsbwBy2wp0zmD12QNYgPJ9Ajswggu BpGhcUOFvD9nO1zstUnAVzlbySCNUcq3S6YuCMOzoXczwONia/D6z0T6fQgwpEoyIUmlJHlgbgc T95isQryeTV8AKp208o7pHqlXdQ5zNB8Q4ZhZWRTVwYyStAPl+9kXdgtJQK+WQYHkgWYvmv4jiH Ps32vPCqhtiQrVqfbLX7sxxA0f80GNIC2Fp20rLfdEKqI/3QS0dqixtH5Z/gDE2cmmyv89A13Uw D886vCPslJJSNuoqeCivBdLamdBBSyIZTp1XzHlORbd3NEDIKftGf9U4rEZm62a/28krUWOb+ZP K9gjDraf93sC5YMcuB01qBAYOSREL+IbBhVp3eSCSGPVh3p+w9+mHXSqRv4PmmR9Ca+4jVKkdyE UH9WpqEjLVaGd2Xll3KGV2JNhSGA8BFIT0Oy09Y33AJSFBgQgCnjZfUMh5qWMDIs0t8gCTySRS4 PnV2GPJsOykAg42ECMEI/mEcC5YPxVopn+g9xIf1QN3Qj+zOTdfkF+SSedXRXEYbHsbBDxTW7fu CKNNx/8FLoaOM+yze9zMmw7JVp4aK23C41QqLID5UxX6qTdSC5OTMktG7df7lUa8m3R0po3/tCM JJVp6gS/oUtPUeLGm3RWSx/3bEX8= X-Received: by 2002:a05:6a21:150c:b0:3de:557e:5ea6 with SMTP id adf61e73a8af0-3e9289859d6mr15607994637.27.1793870093106; Thu, 05 Nov 2026 01:14:53 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:02 +0800 Subject: [PATCH 10/16] tests/qtest: ina238: add current/voltage/power monitor test MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-10-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=7751; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=rF9YxoN8ES1gaJVSzaclqYjGbIV9qLP30Q9j6d+MnNc=; b=5r4TWHTWowFiz+3zBq4OpO6zgxzG4YzMfl2xXD1P94CTTfbfOmc+LYy0dk0m8/DC2SonSnRFX A7hM2MjcT4wAf80gC4101HO+QZrfc7a4YVFx20UM976ubTyogPiZe/+ X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.210.170; envelope-from=eblot.ml@gmail.com; helo=mail-pf1-f170.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870188806158500 Introduce the INA238 qtest: the qgraph node, the QMP injection helpers and the register map. Cover the reset defaults, the software reset, and the Configuration register write mask. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 177 ++++++++++++++++++++++++++++++++++++++++++= ++++ tests/qtest/meson.build | 1 + 2 files changed, 178 insertions(+) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c new file mode 100644 index 0000000000..6e59ed30b5 --- /dev/null +++ b/tests/qtest/ina238-test.c @@ -0,0 +1,177 @@ +/* + * QTest testcase for the INA238 current/voltage/power monitor + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "libqos/i2c.h" +#include "libqos/qgraph.h" +#include "libqtest-single.h" +#include "qobject/qdict.h" + +#define INA238_TEST_ID "ina238-test" +#define INA238_TEST_ADDR 0x40 + +/* Register pointer addresses */ +#define REG_CONFIG 0x00 +#define REG_ADC_CONFIG 0x01 +#define REG_SHUNT_CAL 0x02 +#define REG_VSHUNT 0x04 +#define REG_VBUS 0x05 +#define REG_DIETEMP 0x06 +#define REG_CURRENT 0x07 +#define REG_POWER 0x08 +#define REG_DIAG_ALRT 0x0B +#define REG_SOVL 0x0C +#define REG_SUVL 0x0D +#define REG_BOVL 0x0E +#define REG_BUVL 0x0F +#define REG_TEMP_LIMIT 0x10 +#define REG_PWR_LIMIT 0x11 +#define REG_MANUFACTURER_ID 0x3E +#define REG_DEVICE_ID 0x3F + +/* Configuration register bits */ +#define CONFIG_RST 0x8000 +#define CONFIG_ADCRANGE 0x0010 + +/* DIAG_ALRT register bits */ +#define DIAG_MEMSTAT 0x0001 +#define DIAG_CNVRF 0x0002 +#define DIAG_POL 0x0004 +#define DIAG_BUSUL 0x0008 +#define DIAG_BUSOL 0x0010 +#define DIAG_SHNTUL 0x0020 +#define DIAG_SHNTOL 0x0040 +#define DIAG_TMPOL 0x0080 +#define DIAG_MATHOF 0x0200 +#define DIAG_APOL 0x1000 +#define DIAG_SLOWALERT 0x2000 +#define DIAG_CNVR 0x4000 +#define DIAG_ALATCH 0x8000 + +/* Reset values */ +#define CONFIG_POR 0x0000 +#define ADC_CONFIG_POR 0xFB68 +#define SHUNT_CAL_POR 0x1000 +#define DIAG_ALRT_POR 0x0001 +#define SOVL_POR 0x7FFF +#define SUVL_POR 0x8000 +#define BOVL_POR 0x7FFF +#define BUVL_POR 0x0000 +#define TEMP_LIMIT_POR 0x7FF0 +#define PWR_LIMIT_POR 0xFFFF +#define MANUFACTURER_ID_VAL 0x5449 +#define DEVICE_ID_VAL 0x2381 + +/* Read the 24-bit POWER register (MSB first) */ +static uint32_t i2c_get24(QI2CDevice *dev, uint8_t reg) +{ + uint8_t resp[3]; + + i2c_read_block(dev, reg, resp, sizeof(resp)); + return (resp[0] << 16) | (resp[1] << 8) | resp[2]; +} + +/* QMP helpers for injecting the physical inputs */ + +static void qmp_ina238_set(const char *property, int value) +{ + QDict *resp; + + resp =3D qmp("{ 'execute': 'qom-set', 'arguments':" + " { 'path': %s, 'property': %s, 'value': %d } }", + INA238_TEST_ID, property, value); + g_assert(qdict_haskey(resp, "return")); + qobject_unref(resp); +} + +/* Power-on-reset default values and the ID registers */ +static void test_defaults(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_POR); + g_assert_cmphex(i2c_get16(dev, REG_ADC_CONFIG), =3D=3D, ADC_CONFIG_POR= ); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT_CAL), =3D=3D, SHUNT_CAL_POR); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get24(dev, REG_POWER), =3D=3D, 0x000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT), =3D=3D, DIAG_ALRT_POR); + g_assert_cmphex(i2c_get16(dev, REG_SOVL), =3D=3D, SOVL_POR); + g_assert_cmphex(i2c_get16(dev, REG_SUVL), =3D=3D, SUVL_POR); + g_assert_cmphex(i2c_get16(dev, REG_BOVL), =3D=3D, BOVL_POR); + g_assert_cmphex(i2c_get16(dev, REG_BUVL), =3D=3D, BUVL_POR); + g_assert_cmphex(i2c_get16(dev, REG_TEMP_LIMIT), =3D=3D, TEMP_LIMIT_POR= ); + g_assert_cmphex(i2c_get16(dev, REG_PWR_LIMIT), =3D=3D, PWR_LIMIT_POR); + g_assert_cmphex(i2c_get16(dev, REG_MANUFACTURER_ID), =3D=3D, + MANUFACTURER_ID_VAL); + g_assert_cmphex(i2c_get16(dev, REG_DEVICE_ID), =3D=3D, DEVICE_ID_VAL); +} + +/* Software reset via the Configuration RST bit */ +static void test_soft_reset(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_SHUNT_CAL, 0x1234); + i2c_set16(dev, REG_SOVL, 0x0500); + i2c_set16(dev, REG_TEMP_LIMIT, 0x0A00); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + qmp_ina238_set("die-temperature", 25000); + + g_assert_cmphex(i2c_get16(dev, REG_SHUNT_CAL), =3D=3D, 0x1234); + g_assert_cmphex(i2c_get16(dev, REG_SOVL), =3D=3D, 0x0500); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x4BF0); + g_assert_cmpuint(i2c_get16(dev, REG_CURRENT), !=3D, 0); + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_POR); + g_assert_cmphex(i2c_get16(dev, REG_SHUNT_CAL), =3D=3D, SHUNT_CAL_POR); + g_assert_cmphex(i2c_get16(dev, REG_SOVL), =3D=3D, SOVL_POR); + g_assert_cmphex(i2c_get16(dev, REG_TEMP_LIMIT), =3D=3D, TEMP_LIMIT_POR= ); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT), =3D=3D, DIAG_ALRT_POR); + + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmpuint(i2c_get24(dev, REG_POWER), =3D=3D, 0); +} + +/* CONFIG keeps only the writable ADCRANGE/CONVDLY bits and self-clears RS= T */ +static void test_config_wmask(void *obj, void *data, QGuestAllocator *allo= c) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_ADCRANGE | 0x0040); + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_ADCRANGE | = 0x0040); + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_POR); +} + +static void ina238_register_nodes(void) +{ + QOSGraphEdgeOptions opts =3D { + .extra_device_opts =3D "id=3D" INA238_TEST_ID ",address=3D0x40" + }; + add_qi2c_address(&opts, &(QI2CAddress) { INA238_TEST_ADDR }); + + qos_node_create_driver("ina238", i2c_device_create); + qos_node_consumes("ina238", "i2c-bus", &opts); + + qos_add_test("defaults", "ina238", test_defaults, NULL); + qos_add_test("soft-reset", "ina238", test_soft_reset, NULL); + qos_add_test("config-wmask", "ina238", test_config_wmask, NULL); +} +libqos_init(ina238_register_nodes); diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index bfebe136ef..9b8e2521bf 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -339,6 +339,7 @@ qos_test_ss.add( 'eepro100-test.c', 'es1370-test.c', 'ina230-test.c', + 'ina238-test.c', 'lsm303dlhc-mag-test.c', 'isl_pmbus_vr-test.c', 'max34451-test.c', --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870178; cv=none; d=zohomail.com; s=zohoarc; b=hB45yfAJR9DDpHqkRa7EOWhUQTj4YoTqrCsgU4T4O3RIbd9OHbgh2AyLXC99qwQzxRnEWNGeC3lsoY6/K+cfsA5a70amI2XG7xxoXuznjgoDaaNQLGxjS+vP3yaXLXV2FbzmEMibnmFTjPV7BN9XrvczIFaT/VZmcf9mcVcOF+s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870178; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=fQuO1OoAvVLPWqu+N7yVLkIN0/A7JAcl4F1lEKElbWw=; b=KhA9wMMflk6MBwXnbvVECRvKw0zBsRYMbLe2joSARTJ4ovth3WOnTO3OVjll+7EK7u0v7MACwC/ZiyZhQfelKBtDBn9bIxjxuonBqZkZPVuCaWwru/bHtM8QkyT8in2vrDu67WbiX95EYuPHtFMuMQDZXw+tWuC+8spVmC1Mh5U= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870178162567.5316512427191; Thu, 5 Nov 2026 01:16:18 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZP-0007Ve-5N; Thu, 05 Nov 2026 04:15:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtYm-0006B8-Vs for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:07 -0500 Received: from mail-pj1-f50.google.com ([209.85.216.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtYk-0005Ww-84 for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:03 -0500 Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-3b0bbdeae06so207337a91.1 for ; Thu, 05 Nov 2026 01:15:01 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b312ccf1sm3043518a91.4.2026.11.05.01.14.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:14:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870100; x=1794474900; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fQuO1OoAvVLPWqu+N7yVLkIN0/A7JAcl4F1lEKElbWw=; b=m+LbgXu90KwR/Gn/xfahVfTXwY1DobFXb12F8OaYMPgEarcU2RlULDo6HTV7PegXV4 RW3LwbJW7rqzxRpNrLxMTLaD6kAz8GPjeU8YX+a/1NSVADK48w2WSElJ+uuxh9Cscfw0 jklSG5/2oRch2Dxc7neoW/48hE5r9DUht1BYuDOgijb0d2UwGA3UmhC3DsDj3xW01Szc HT958OLlKxTCbYihOC1RL5mQwy7GruPEiDk/uFIXSpf/Mydt8lF/T9TvnBDC5rdNW5Y+ jI2HP2NIdo0b0+ttgWVjHdBEAjA5Gx3VivsjUNJeCQPdD17U9TJZHhhxWvDSuq/1adzQ eJqA== X-Gm-Message-State: APuU4Z/e/+QAAa6C5ScV+BNVOFSGYotBidQgZbmInG3g3Qst9gHpf8uA IhW+SS04i4PT3kFEsse0/X8Y/EpxvGNrXMgcR5tUZhQyWpYbNxNmuWFde7Dd4QDZd48= X-Gm-Gg: AYBFou3HCCG13b/yemjtP0+lNh44C7OqOOPa3VzHAHtWD7hzDncSjIsSYYRIfqiF8xi Ac+8w3AaCNFF7uA4sqvEBBwPr4Ijoz2z6tRQSvna+OF6j76kXOTiT1FP4Xv8BfiTOv8EqPYz1IH 11ocNRDANEcdnde1sZMJi7l7sZ7rHspBsi8K0+8lkP2HUgtCGQYk0WNCIbRDOFgrm7fhS0b/ylW /VCzICuZ2Z7nlX0u4DWejso4rgndDl1vcSeqtGB7srvFFSgzFPPm8vlaLTaCYSUU6wA1R+AaQ+q GnYptXj+ze8GVoiB++bTFrrWLVlDIx31iSGoLweLvixSaepqWYpj8fURTShkSdueHE2w+qDT4Km 0ggXE2oE+tBf1Zn8MlzYskHox2bJltCqfgWzzpRb6ZyhNVOstDvWY8G2d0X+SBfvovwYYaaNbR+ hFhvz5AKoLuyNR8Ny49jeQ9I+SG8hvgn68qadCKRvnGVtgxfpTYNxaKxJIjnVjMCO3SGfH9f3kX ZmuALlrSampVqwCrADTYQlTJpUeoxq3mSHek3xnUNvTJ2Vqvj8bhjn6c1I42OvFy8zkVf8hYC4w 2LDvDgBD9ApJfbFDuQFemO6xilef2M5Q8Xw//AK7cHiXkqLxlHOyvryfsci1TwFlbQSdyIMH+Bo 0599rLA== X-Received: by 2002:a17:90b:118a:b0:3b0:9e2c:9b61 with SMTP id 98e67ed59e1d1-3b09e2c9eecmr2216016a91.35.1793870100402; Thu, 05 Nov 2026 01:15:00 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:03 +0800 Subject: [PATCH 11/16] tests/qtest: ina238: cover voltage/temperature injection and derivation MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-11-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=7015; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=NE+vwOg7NY0/COEtyB9HSra4PfJKCB6NE5KYmmh/nfk=; b=Fs3wWegfLLwDAU3NHPRcZhE+VmtOFF39kClfRHdO9X9H1nFI+ba3LKhIEH4FqXRXBkRzIyg14 CgqbvxoAprbBZAaxfKOVyKsJxz8/5lk/RvnWbYkwozMsbFJxtjJgiav X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.50; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f50.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870178605158500 Cover the shunt-, bus- and die-temperature injection properties and the current and power values derived through the Shunt Calibration register, including the zero-calibration case, the ADCRANGE rescaling and the clamp/overflow edges. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 156 ++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 156 insertions(+) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index 6e59ed30b5..693139f988 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -89,6 +89,20 @@ static void qmp_ina238_set(const char *property, int val= ue) qobject_unref(resp); } =20 +static int qmp_ina238_get(const char *property) +{ + QDict *resp; + int ret; + + resp =3D qmp("{ 'execute': 'qom-get', 'arguments':" + " { 'path': %s, 'property': %s } }", + INA238_TEST_ID, property); + g_assert(qdict_haskey(resp, "return")); + ret =3D qdict_get_int(resp, "return"); + qobject_unref(resp); + return ret; +} + /* Power-on-reset default values and the ID registers */ static void test_defaults(void *obj, void *data, QGuestAllocator *alloc) { @@ -160,6 +174,139 @@ static void test_config_wmask(void *obj, void *data, = QGuestAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_CONFIG), =3D=3D, CONFIG_POR); } =20 +/* Shunt voltage register */ +static void test_shunt_injection(void *obj, void *data, QGuestAllocator *a= lloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x4BF0); + + qmp_ina238_set("shunt-voltage", -80000000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0xC180); + + g_assert_cmpint(qmp_ina238_get("shunt-voltage"), =3D=3D, -80000000); +} + +/* Bus voltage register */ +static void test_bus_injection(void *obj, void *data, QGuestAllocator *all= oc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("bus-voltage", 48000000); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x3C00); + + qmp_ina238_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x0F00); +} + +/* Die temperature register */ +static void test_temp_injection(void *obj, void *data, QGuestAllocator *al= loc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("die-temperature", 25000); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0x0C80); + + qmp_ina238_set("die-temperature", -25000); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0xF380); + + g_assert_cmpint(qmp_ina238_get("die-temperature"), =3D=3D, -25000); +} + +/* Calibration-derived current */ +static void test_calibration_current(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 97200000); + + i2c_set16(dev, REG_SHUNT_CAL, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + + i2c_set16(dev, REG_SHUNT_CAL, 0x0FD2); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x4CCC); +} + +/* Calibration-derived power */ +static void test_power(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + i2c_set16(dev, REG_SHUNT_CAL, 0x0FD2); + + g_assert_cmpuint(i2c_get24(dev, REG_POWER), =3D=3D, 4718400); +} + +/* With SHUNT_CAL =3D=3D 0 the current and power registers stay zero */ +static void test_zero_cal(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SHUNT_CAL, 0x0000); + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmpuint(i2c_get24(dev, REG_POWER), =3D=3D, 0); +} + +/* ADCRANGE changes the shunt LSB, not the current divisor. */ +static void test_adcrange(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0FA0); + + i2c_set16(dev, REG_SHUNT_CAL, 0x0800); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x1F40); + + i2c_set16(dev, REG_CONFIG, CONFIG_ADCRANGE); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x3E80); + + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x7D00); + + i2c_set16(dev, REG_SHUNT_CAL, 0x2000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x1F40); +} + +/* Clamping at full scale and the MATHOF flag */ +static void test_overflow(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + i2c_set16(dev, REG_CONFIG, CONFIG_ADCRANGE); + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x7FFF); + qmp_ina238_set("shunt-voltage", -97200000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x8000); + + i2c_set16(dev, REG_CONFIG, CONFIG_POR); + qmp_ina238_set("shunt-voltage", 163835000); + i2c_set16(dev, REG_SHUNT_CAL, 0x0001); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x7FFF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_MATHOF, =3D=3D, + DIAG_MATHOF); +} + +/* Shunt over-limit alert (transparent mode) */ static void ina238_register_nodes(void) { QOSGraphEdgeOptions opts =3D { @@ -173,5 +320,14 @@ static void ina238_register_nodes(void) qos_add_test("defaults", "ina238", test_defaults, NULL); qos_add_test("soft-reset", "ina238", test_soft_reset, NULL); qos_add_test("config-wmask", "ina238", test_config_wmask, NULL); + qos_add_test("shunt-injection", "ina238", test_shunt_injection, NULL); + qos_add_test("bus-injection", "ina238", test_bus_injection, NULL); + qos_add_test("temp-injection", "ina238", test_temp_injection, NULL); + qos_add_test("calibration-current", "ina238", test_calibration_current, + NULL); + qos_add_test("power", "ina238", test_power, NULL); + qos_add_test("zero-cal", "ina238", test_zero_cal, NULL); + qos_add_test("adcrange", "ina238", test_adcrange, NULL); + qos_add_test("overflow", "ina238", test_overflow, NULL); } libqos_init(ina238_register_nodes); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870138; cv=none; d=zohomail.com; s=zohoarc; b=lzCWc4zyRsuR6URQgYf+PTQYVSwF3lqPgPZDn8L/apXZfn3AuIDosxDGcjrpSgNuZYvLKN4rzEIMI9oTd2dsfjNUm8NDq9AVcRjUrdZRgS5eRWbUwYuGbO/3i4L5Wsr/ACdmr1f3CdUjfPQehuGLr4/7Jf4DSrmuZ8R8PZXyi/o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870138; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=dLcH89Q+3v52wJ3aHqyuR198A/y0zPJ5UzMGy5C22Zs=; b=fgkbipfJvk0N1k/V+v5tx8vLvaH1+AvSZ5YAmVsa6VZ5NccfC0cIxy3X4VzH36u2Fg0toZDv8hMDMhfreU5jtIxR38kYfIJnbpOpiZJxe/qnfbM0yZNwjBiZF/LTBbre/7IkeUfsaHWoNFp2Q69PbEXFxOtaUXSGspewPWIN/Bs= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870138494619.4507278632445; Thu, 5 Nov 2026 01:15:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZB-0006lZ-Ac; Thu, 05 Nov 2026 04:15:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtYw-0006Fg-51 for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:18 -0500 Received: from mail-pj1-f45.google.com ([209.85.216.45]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtYs-0005Xt-CX for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:13 -0500 Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-3b0a2a44a0cso896208a91.1 for ; Thu, 05 Nov 2026 01:15:09 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cdb46981369sm1942540a12.25.2026.11.05.01.15.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:15:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870108; x=1794474908; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=dLcH89Q+3v52wJ3aHqyuR198A/y0zPJ5UzMGy5C22Zs=; b=S+ZS2vyN9Zpnmbbf/iNiFZVYyj0c2ujDcR1rrsw+ruNNbesk1zTx+hymFqWOBBYAR/ pKwAbtQi0baeReGCPFRp6t2k2EFZPKSXr6wH1d3bKP8C9C81QVNxCwcNV5NkzvYszoJB eazLipsbAa+VaoSU4OLwe48tU26hFy2Xbj5vhYtO+IirRkVOG2KKimLHDAh6pXxQ9+98 S0DB3AqQ94TA+kbXkNDBUgHFX9zLdyX6WnmkOZ5aZI+uhe6aKTP3Ovk8/YjmxTCQxp88 rmbMIxOI/0QQeZhyn4EcI6feFq1e7uge4+e2OSYpKntaG9e/NMEMmvp8JKTq/954lDUU 4Kdg== X-Gm-Message-State: APuU4Z/whUhihorJJt+HABnvbkVIBlAN/dmaMycDuNWTKxx8dB/YDQim 68pCYjAnByEMKtkg4BBL+XFAoTtJzTHT1dRbkysYD0167Vw2JUAlAGqgnetyCA== X-Gm-Gg: AYBFou2gjrRJvfyIugk9nROvr8p1GnQXTG2DCY0HX19siGfML+NbFV5eUk4UaSJ7Zys aHcSUqlJwhzAxR8OMgAdkdTubscL8rl6B9BG1HuFyxi4GUe0Qz8JCBhmGcGoM7Mk0LVxtVfOQ36 oq3jsTRu7BlbLCjFdOm3kcaW58vwTlBMhKzFiNdfUwPXGpI4cY+0eUkdODk2DcvOhhoVqEWhReX E2He98UHByThcKqAJj2xtFxaycnG2p2l5QFqR2ugVjrkgh9SYEMr6snZlYDiN+mg5iXubEmDRTl rD6HiwY6FdvtJ2nalZeBxnV1NYBFvVBbjUn6MXptysgIzZHdnxyikzMlM0uiEUIkgZMKCYlyMOt 4Nrq8MgwCYooK44qFk6DT4QofgEHeFtdVKlWL2ESg1tYBxZ4UtQ4G/6Bw3AWMybL/giCf1REQ// xdNMMwq6Yhvbg8WPvA9/dTBGQc9dZFjdwjsbInR/wzY3j/SnoGG0tLTyayRWReJ56SGVs6xlQlg IJiTXUMtKMbpzT5E7Bw0Q9d5fgylJcmS6vKQi/hNHzwOxjb8gHBQnFVNoBYXPFO0DySEgMFnOnt 8bAnJdfoduhPZ+rykZhAHqzD9EmEcAAwUh2RaBNC47Lmw/s4J7n2w/YnUlAYLTm/tWl3BwSiV0x VkY0A6g== X-Received: by 2002:a17:90b:3ec6:b0:3a4:ba6e:65c9 with SMTP id 98e67ed59e1d1-3ae8982640fmr13997959a91.59.1793870108470; Thu, 05 Nov 2026 01:15:08 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:04 +0800 Subject: [PATCH 12/16] tests/qtest: ina238: cover the ALERT path and conversion-ready flag MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-12-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=6719; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=ZC7CGjlcMtlHZ+/DXyYJ8lCNdfyQ5Stw/MJqODCAIbA=; b=gRQlRMsfBHPrzmLSeFAxD23OdbuhdWRp6Bjx/VKs2jG1mxENA2oSsGLUE/ASk7IncWDudhmG2 H87W1VE6vuZDG95GU0hArxRD2AvranXHbqJDuQ+4p8ZclajbOC58gnI X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.45; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f45.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870140118158500 Cover the shunt/bus/temperature/power alert thresholds, the multi-alert OR behaviour, the alert latch, the DIAG_ALRT control bits and the conversion-ready (CNVRF) flag. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 146 ++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 145 insertions(+), 1 deletion(-) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index 693139f988..08813377c9 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -306,7 +306,143 @@ static void test_overflow(void *obj, void *data, QGue= stAllocator *alloc) DIAG_MATHOF); } =20 -/* Shunt over-limit alert (transparent mode) */ +/* Shunt over-limit alert */ +static void test_alert_shunt(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + + qmp_ina238_set("shunt-voltage", 5000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + qmp_ina238_set("shunt-voltage", 5000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); +} + +/* Bus over-limit alert */ +static void test_alert_bus(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_BOVL, 0x1000); + + qmp_ina238_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_BUSOL, =3D=3D, 0); + + qmp_ina238_set("bus-voltage", 48000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_BUSOL, =3D=3D, DI= AG_BUSOL); +} + +/* Over-temperature alert */ +static void test_alert_temp(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_TEMP_LIMIT, 0x0A00); + + qmp_ina238_set("die-temperature", 10000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_TMPOL, =3D=3D, 0); + + qmp_ina238_set("die-temperature", 25000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_TMPOL, =3D=3D, DI= AG_TMPOL); +} + +/* Power over-limit alert */ +static void test_alert_power(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + i2c_set16(dev, REG_SHUNT_CAL, 0x0FD2); + + i2c_set16(dev, REG_PWR_LIMIT, 0x0100); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, =3D=3D, DIAG= _POL); + + i2c_set16(dev, REG_PWR_LIMIT, 0x7FFF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, =3D=3D, 0); +} + +/* Two thresholds crossed at once are reported simultaneously (multi-alert= ) */ +static void test_alert_multi(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + uint16_t diag; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_BOVL, 0x1000); + + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + + diag =3D i2c_get16(dev, REG_DIAG_ALRT); + g_assert_cmphex(diag & DIAG_SHNTOL, =3D=3D, DIAG_SHNTOL); + g_assert_cmphex(diag & DIAG_BUSOL, =3D=3D, DIAG_BUSOL); +} + +/* Latch mode holds the flag until the DIAG_ALRT register is read */ +static void test_alert_latch(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("shunt-voltage", 5000000); + + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); +} + +/* DIAG_ALRT control bits are writable; status bits are read-only */ +static void test_diag_control(void *obj, void *data, QGuestAllocator *allo= c) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + uint16_t diag; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + i2c_set16(dev, REG_DIAG_ALRT, + DIAG_ALATCH | DIAG_CNVR | DIAG_SLOWALERT | DIAG_APOL); + diag =3D i2c_get16(dev, REG_DIAG_ALRT); + g_assert_cmphex(diag & DIAG_ALATCH, =3D=3D, DIAG_ALATCH); + g_assert_cmphex(diag & DIAG_CNVR, =3D=3D, DIAG_CNVR); + g_assert_cmphex(diag & DIAG_SLOWALERT, =3D=3D, DIAG_SLOWALERT); + g_assert_cmphex(diag & DIAG_APOL, =3D=3D, DIAG_APOL); + /* MEMSTAT stays set */ + g_assert_cmphex(diag & DIAG_MEMSTAT, =3D=3D, DIAG_MEMSTAT); +} + +/* Every DIAG_ALRT read clears CNVRF, whatever the alert-latch mode. */ +static void test_cnvrf(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, DI= AG_CNVRF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, 0); + + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + qmp_ina238_set("shunt-voltage", 20000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, DI= AG_CNVRF); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, 0); +} + static void ina238_register_nodes(void) { QOSGraphEdgeOptions opts =3D { @@ -329,5 +465,13 @@ static void ina238_register_nodes(void) qos_add_test("zero-cal", "ina238", test_zero_cal, NULL); qos_add_test("adcrange", "ina238", test_adcrange, NULL); qos_add_test("overflow", "ina238", test_overflow, NULL); + qos_add_test("alert-shunt", "ina238", test_alert_shunt, NULL); + qos_add_test("alert-bus", "ina238", test_alert_bus, NULL); + qos_add_test("alert-temp", "ina238", test_alert_temp, NULL); + qos_add_test("alert-power", "ina238", test_alert_power, NULL); + qos_add_test("alert-multi", "ina238", test_alert_multi, NULL); + qos_add_test("alert-latch", "ina238", test_alert_latch, NULL); + qos_add_test("diag-control", "ina238", test_diag_control, NULL); + qos_add_test("cnvrf", "ina238", test_cnvrf, NULL); } libqos_init(ina238_register_nodes); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870158; cv=none; d=zohomail.com; s=zohoarc; b=FMcuxxcwKu/k+1xRA+B4CEK9vW4jC/s1Wtk+bMDjzAmKaPNH8za2BPmu+mxWSESXJir+eb6iSnCKtgpUdhD1DwK1ocXw1ZLi5rBBc3FjBByujH7AX2V5wwqvwZ523GtfK+k1j3eP0UAEcQpUXqZYsWZkPVBB1sK2o2yN99Acj9A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870158; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=fK1yTBZ+R4UZ9jGLZP5etFoRte2w8wAdC3Wr5b6SMuI=; b=G4idSe6nw84kTCiBFF9dm8pMEge4+DXxPIqZMYeJfIurugpHe21EPNtbkcTGLDQJjTtkptPZ22dyZJM3Z/d4B+T/KO003DHhdPdW3fPiSa5SLTaDgovJG6qdOd1HoRUnfyhRO+OMxvWFioq4k+k4oLRQoMzw1B+4hwhxRxpim/U= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870158855988.6621701090726; Thu, 5 Nov 2026 01:15:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZT-00088D-Qw; Thu, 05 Nov 2026 04:15:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtZ3-0006IK-0Q for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:21 -0500 Received: from mail-pf1-f172.google.com ([209.85.210.172]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtZ0-0005jN-Vd for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:20 -0500 Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-8a05effaf8dso1033882b3a.2 for ; Thu, 05 Nov 2026 01:15:17 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8b09bb47f6bsm1855235b3a.47.2026.11.05.01.15.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:15:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870116; x=1794474916; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fK1yTBZ+R4UZ9jGLZP5etFoRte2w8wAdC3Wr5b6SMuI=; b=Bp1rlkOihCdaGOJ8F6UtAoyCKl1NIsdK02HEVNNnAEIBBr7pZDPJp2QXr4YsGQmf41 dvtWI1o8haEFUZSmB8wnoOkrLo/TDvvcJ6Q6gK0cklLJ1ONK3cxpmRGHdouCm8wX4LHb Wku6WqfMQwRcoiWAWXCZyB3eFxu/cfwnR2xrwutGLnQ2b1cWtCKmqznkP8uJRXVKTKKN ESoUm+hrzNktuGdGGrNbN2tf+YvOz0T7cYOyMKjW39jqQ4zbr8ct0FLjTvECE58l6MEw 14eMMzQ1yMEN/0Jh/Wb22MzjVSAQzSR210YkYSTgGoklf5QdGVLoi8AW8fRR5Be9L4nE cX2w== X-Gm-Message-State: APuU4Z+Da09zT6kMMcCsg7huwy6Ib8OsvbFqwP6ZRvSnmi1NnuVOfbdT 1jISwErLd9BkkIVJRhZ3Qr4kiws43hSiGef64ThDnisMTLTy9s3OSLXmkARkDIx1frw= X-Gm-Gg: AYBFou1eDmPSH4hIzTVL6f+eXdYkzq+YprgDZwzACOiAIAo1EPlLztkJZMHTJyhpOTS DSCOZxEIWtL8LLgIbMVhUJ9tHQkwW9SkjVuZDyd0QHrIS1zPl0qgt8zQMrTUaKqeUu3L65jhFS7 04lcjqN1hiAIFwJo7DKinlTw7/XVb/FZnZxewpT8pLBlb1weAGCwIR2Zuey9wl8xLQ1bJxCw7zY NrnbtfG0qsyO974aytL8qUkwwM1e8IWWP05lN8rdMWYH7Uwl68249MwQ87qRzLnXzDtbHIrVQYc Mf9CaUy2CkGFe5emPHOro9OIE3lrAgqYIL3wqCEbV1ab26AArnzpDdCmw5N1yfnRgAHaNtWmX1v wc5ZaZWeQ9nkoCIzgdSTqC1D7lyJ8JKCF8q3Lerkw1HbzTcX0cDVAZEwF7ytWwofASdfIDXW7t6 sS2CgpoKA8BrCQdI/I0r2B+dG3a+YqqdKPtpcwxsxx0uKInuKuPoPtH0HRNnKzRdJxOb2OdnX+W Y2V9sV4rguEtjPTtx7sJnlwB/a28jhKDTl8eCan4qv50Cow9f30YmO9gPGwY6EjBEkNBrbsaF6n SWQFtVwcNqb2ZJto5ZxYHWHWDib0wNUhVQLWmQRDxr0GUyEk33Xq2JaFY5InAxCixp/jP6Eu373 /bMIPoQ== X-Received: by 2002:a05:6a21:2982:b0:3dd:a197:edf4 with SMTP id adf61e73a8af0-3e928e21c8dmr17241587637.67.1793870116287; Thu, 05 Nov 2026 01:15:16 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:05 +0800 Subject: [PATCH 13/16] tests/qtest: ina238: cover the injection-property limits MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-13-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=5375; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=cK0KtEZakGGTiCMBuvt6S8R0Yj1qS5lBpstYZd6Z9WY=; b=3I3AwO0zF12w7UYNrVfmBdodPaNw4hI0q3yY25WAsMToiVcAydsa27HzqHe3oQAHs/Mkgr/Rf s/phemzgpTvDMDN0MjvHr/c7gh5hWbiUZxBUJRIWQPzBStc3q2utjht X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.210.172; envelope-from=eblot.ml@gmail.com; helo=mail-pf1-f172.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870160253158500 Verify that the extremes of the shunt-voltage, bus-voltage and die-temperature injection ranges round-trip through the QOM property without truncation or wraparound, and that values just outside are rejected rather than silently wrapped. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 84 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 84 insertions(+) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index 08813377c9..74912424c0 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -67,6 +67,13 @@ #define MANUFACTURER_ID_VAL 0x5449 #define DEVICE_ID_VAL 0x2381 =20 +/* Injection-property limits (see ina238.c) */ +#define SHUNT_MIN_NV (-163840000) /* -32768 * 5 uV */ +#define SHUNT_MAX_NV 163835000 /* 32767 * 5 uV */ +#define BUS_MAX_UV 85000000 /* 27200 * 3.125 mV */ +#define TEMP_MIN_MC (-40000) /* -320 * 125 m-degC */ +#define TEMP_MAX_MC 125000 /* 1000 * 125 m-degC */ + /* Read the 24-bit POWER register (MSB first) */ static uint32_t i2c_get24(QI2CDevice *dev, uint8_t reg) { @@ -103,6 +110,18 @@ static int qmp_ina238_get(const char *property) return ret; } =20 +/* A qom-set expected to be rejected (value outside the supported range) */ +static void qmp_ina238_set_fail(const char *property, int64_t value) +{ + QDict *resp; + + resp =3D qmp("{ 'execute': 'qom-set', 'arguments':" + " { 'path': %s, 'property': %s, 'value': %lld } }", + INA238_TEST_ID, property, (long long)value); + g_assert(qdict_haskey(resp, "error")); + qobject_unref(resp); +} + /* Power-on-reset default values and the ID registers */ static void test_defaults(void *obj, void *data, QGuestAllocator *alloc) { @@ -443,6 +462,68 @@ static void test_cnvrf(void *obj, void *data, QGuestAl= locator *alloc) g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, 0); } =20 +/* Shunt-voltage injection limits */ +static void test_shunt_limits(void *obj, void *data, QGuestAllocator *allo= c) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("shunt-voltage", SHUNT_MAX_NV); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x7FFF); + g_assert_cmpint(qmp_ina238_get("shunt-voltage"), =3D=3D, SHUNT_MAX_NV); + + qmp_ina238_set("shunt-voltage", SHUNT_MIN_NV); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x8000); + g_assert_cmpint(qmp_ina238_get("shunt-voltage"), =3D=3D, SHUNT_MIN_NV); + + qmp_ina238_set_fail("shunt-voltage", (int64_t)SHUNT_MAX_NV + 1); + qmp_ina238_set_fail("shunt-voltage", (int64_t)SHUNT_MIN_NV - 1); + g_assert_cmpint(qmp_ina238_get("shunt-voltage"), =3D=3D, SHUNT_MIN_NV); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x8000); +} + +/* Bus-voltage injection limits */ +static void test_bus_limits(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("bus-voltage", 0); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x0000); + + qmp_ina238_set("bus-voltage", BUS_MAX_UV); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x6A40); + g_assert_cmpint(qmp_ina238_get("bus-voltage"), =3D=3D, BUS_MAX_UV); + + qmp_ina238_set_fail("bus-voltage", (int64_t)BUS_MAX_UV + 1); + qmp_ina238_set_fail("bus-voltage", -1); + g_assert_cmpint(qmp_ina238_get("bus-voltage"), =3D=3D, BUS_MAX_UV); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x6A40); +} + +/* Die-temperature injection limits */ +static void test_temp_limits(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + + qmp_ina238_set("die-temperature", TEMP_MAX_MC); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0x3E80); + g_assert_cmpint(qmp_ina238_get("die-temperature"), =3D=3D, TEMP_MAX_MC= ); + + qmp_ina238_set("die-temperature", TEMP_MIN_MC); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0xEC00); + g_assert_cmpint(qmp_ina238_get("die-temperature"), =3D=3D, TEMP_MIN_MC= ); + + qmp_ina238_set_fail("die-temperature", (int64_t)TEMP_MAX_MC + 1); + qmp_ina238_set_fail("die-temperature", (int64_t)TEMP_MIN_MC - 1); + g_assert_cmpint(qmp_ina238_get("die-temperature"), =3D=3D, TEMP_MIN_MC= ); + g_assert_cmphex(i2c_get16(dev, REG_DIETEMP), =3D=3D, 0xEC00); +} + static void ina238_register_nodes(void) { QOSGraphEdgeOptions opts =3D { @@ -459,6 +540,9 @@ static void ina238_register_nodes(void) qos_add_test("shunt-injection", "ina238", test_shunt_injection, NULL); qos_add_test("bus-injection", "ina238", test_bus_injection, NULL); qos_add_test("temp-injection", "ina238", test_temp_injection, NULL); + qos_add_test("shunt-limits", "ina238", test_shunt_limits, NULL); + qos_add_test("bus-limits", "ina238", test_bus_limits, NULL); + qos_add_test("temp-limits", "ina238", test_temp_limits, NULL); qos_add_test("calibration-current", "ina238", test_calibration_current, NULL); qos_add_test("power", "ina238", test_power, NULL); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870204; cv=none; d=zohomail.com; s=zohoarc; b=Gxs3xgSGYwKelumAgUH/YSnPO27hca5Bc+fbjbFqn+lgbEBYFFdcFCsMnsOKfvDuaNYlQrQwtdGB9fIdQDTFTq200zetCPxn8nfM3hveu3a37XPEEOc9/j9SIGmJ28fdcIJHH8hIPK79sbD0aZK3ztt5Xw51VsED9k1ifXLqTy8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870204; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=tktjGaYiiPphflc+jwBY/yh3JfaTUnJhW1nAnyAEoO4=; b=J/DS+ae1yRpFgp0ZSpzpYc/gQ4/c3Sc9L6LCg/Knw8i8swVj8TcpBM61v8+GtWbVYF60XOK/pLbctuaWqD6fKwNZLkcK8H8KGod54gzN+ydDbHlm1cTxQ5T9iZJvCd9qF+1hvLfWQnQcE4RE7Dl41xtz2Rl+PhsLtQkQsc9dJz8= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179387020484430.56258891800303; Thu, 5 Nov 2026 01:16:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZd-0000U1-KF; Thu, 05 Nov 2026 04:15:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtZ9-0006eN-Ck for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:27 -0500 Received: from mail-pj1-f52.google.com ([209.85.216.52]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtZ7-0005kN-4c for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:26 -0500 Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3ae1b34e0d2so233967a91.1 for ; Thu, 05 Nov 2026 01:15:24 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b3160634sm3112356a91.6.2026.11.05.01.15.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:15:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870123; x=1794474923; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=tktjGaYiiPphflc+jwBY/yh3JfaTUnJhW1nAnyAEoO4=; b=jhgG0dl4p8lgLMwfD7WRHVQ+xLfYTHkWDNssr0NdkBBALhuHs/d/5YkVCHadQS4AVA YdVz/PHrHoeo3zl2tKzQv2NJvJgcOD2uPysbcpjp42UXUIrDQdsSuIb7TD3QWYgr2cZ4 DSKFW7nFfBdGlb6E7CgzRg5TO9rj1pdyf+6rgR0ZiF/wYlz3Y7mfWngx4ypGZHkAV4+Y yHaPOlvkyG2ifrevWqIcb7zfGKTcZ9esins7xvBWo/tU/TBQ7GxWWoxWubHYypx4dE6c UR6emSsZsSYSCfgaZ58TkQhclcxT8QEHzuS4UZckS5e/z7WqnA954N2dLfocpZOEjVCj ki/Q== X-Gm-Message-State: APuU4Z9eMskbqPVUSo3N5kHUnoXDPsO7ZRCPY9D6LS+aMUf7YFzzoZcU e2G+SjbEBCQM7OAuI/pVjS1OLkYKn312AE1VmdCfyS6XvHmkq3iS02f4r/2DfO9nrmw= X-Gm-Gg: AYBFou0BAubiT+mg6FYABwvJEzFIjhxfZDgTHCFrH5fXmS1Q2v/Ofi8VIl9hbiYZICy xnQR1UoRMC33rnL8tXrm5JWP68RpEdC1a1/sD66CpPeIEP1FBCBy9CTzrsXMk6Ry3AcgZzd9q+i spvbc2lE+AA9OcXz32pOQUQkBWeMxuBpSyytAH8aTJ1336W5occALVasyAeX0QJNYVMVhYix6BR 5l5ftM3+foTyMGVvjPsUdPWQ08MLLtaz7FLr/QqcV5m3EYvqHkTbt6myLWDrOUU7tFmh4FtzZFa 6DlvYabZz2WA4lwYBEc3zJia/PlXSz89chgRQTAro78vdCf0uy7KKdtiAgKkAQAtjc+eaukz/e8 go/8RUgjh741+F0RS58ON0ei1YZylz0VPv3A3qF9gOd7x1jWDzTHVmra9ACDCHgPWhnQmvIes82 ejtdn0w20MDXOh7mnjfUO/OBbyHQ9mEbO1+i1U6SZoXVWpighKrwlvKMdO7OreIpk7DhAKZcF+U 8Fmy3J0NMygV+SHdkk8suaG6YorkBq0Bd3ZD3k3fcB6cGg/tQUjDNkYFpnjGtRcPDRAVW9tPVEI saPt6YB6pcaNS48idDJ6pYxxr3lSdEfEI6n5mlJ+J4c3e1oVGsP5EPLVEsK3mVAAcS4hT3c6p8J 8m7pIVg== X-Received: by 2002:a17:90b:39a6:b0:3ac:b6eb:e0de with SMTP id 98e67ed59e1d1-3b0afbf6942mr1312128a91.4.1793870123361; Thu, 05 Nov 2026 01:15:23 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:06 +0800 Subject: [PATCH 14/16] tests/qtest: ina238: cover the operating modes MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-14-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=4362; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=hpkKt0gJgZXEPALCwyXp/2SRP8/YiYX8+r6RcSGYDn4=; b=ZpB258RQdGACxUzFILGfC6NtxVavtYoFyXlLhtwxM/izkJ5mSUaZdipZJ68IAs4NwHFI0ZrZu nW59V/lPcA1BFVPA4ejZEA/DwoVAqpFQ1nDZt3vcOqevpZnZbr+L+e+ X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.52; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f52.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870207273158500 Add coverage for the ADC_CONFIG MODE field: shutdown modes perform no conversion and leave the measurement registers and CNVRF untouched, triggered modes convert once per ADC_CONFIG write, and continuous channel-select modes refresh only the enabled measurement registers. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 70 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 70 insertions(+) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index 74912424c0..fb7bcc265c 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -304,6 +304,73 @@ static void test_adcrange(void *obj, void *data, QGues= tAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x1F40); } =20 +/* Shutdown converts nothing: registers are held, CNVRF stays clear. */ +static void test_mode_shutdown(void *obj, void *data, QGuestAllocator *all= oc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SHUNT_CAL, 0x1000); + qmp_ina238_set("shunt-voltage", 20000000); + qmp_ina238_set("bus-voltage", 12000000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0FA0); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x0F00); + + /* Clear CNVRF, then enter shutdown (MODE =3D 0). */ + (void)i2c_get16(dev, REG_DIAG_ALRT); + i2c_set16(dev, REG_ADC_CONFIG, 0x0000); + + /* Injecting while shut down performs no conversion: registers are hel= d. */ + qmp_ina238_set("shunt-voltage", 40000000); + qmp_ina238_set("bus-voltage", 24000000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0FA0); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x0F00); + + /* No conversion completed, so CNVRF stays clear. */ + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, 0); +} + +/* Triggered modes convert once per ADC_CONFIG write. */ +static void test_mode_triggered(void *obj, void *data, QGuestAllocator *al= loc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SHUNT_CAL, 0x1000); + qmp_ina238_set("shunt-voltage", 20000000); + + /* Enter triggered bus+shunt+temp mode: one conversion. */ + i2c_set16(dev, REG_ADC_CONFIG, 0x7000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0FA0); + + /* A new input does not convert until the next trigger. */ + qmp_ina238_set("shunt-voltage", 40000000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0FA0); + + /* Re-triggering with another ADC_CONFIG write samples the new input. = */ + i2c_set16(dev, REG_ADC_CONFIG, 0x7000); + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x1F40); +} + +/* A bus-only mode refreshes the bus register alone; the rest are held. */ +static void test_mode_channels(void *obj, void *data, QGuestAllocator *all= oc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SHUNT_CAL, 0x1000); + qmp_ina238_set("shunt-voltage", 20000000); + qmp_ina238_set("bus-voltage", 12000000); + + /* Continuous bus-only mode. */ + i2c_set16(dev, REG_ADC_CONFIG, 0x9000); + qmp_ina238_set("shunt-voltage", 40000000); + qmp_ina238_set("bus-voltage", 24000000); + + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x0FA0); + g_assert_cmphex(i2c_get16(dev, REG_VBUS), =3D=3D, 0x1E00); +} + /* Clamping at full scale and the MATHOF flag */ static void test_overflow(void *obj, void *data, QGuestAllocator *alloc) { @@ -548,6 +615,9 @@ static void ina238_register_nodes(void) qos_add_test("power", "ina238", test_power, NULL); qos_add_test("zero-cal", "ina238", test_zero_cal, NULL); qos_add_test("adcrange", "ina238", test_adcrange, NULL); + qos_add_test("mode-shutdown", "ina238", test_mode_shutdown, NULL); + qos_add_test("mode-triggered", "ina238", test_mode_triggered, NULL); + qos_add_test("mode-channels", "ina238", test_mode_channels, NULL); qos_add_test("overflow", "ina238", test_overflow, NULL); qos_add_test("alert-shunt", "ina238", test_alert_shunt, NULL); qos_add_test("alert-bus", "ina238", test_alert_bus, NULL); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870206; cv=none; d=zohomail.com; s=zohoarc; b=JxfSjBaJ5QdiIFT2OHix1FabMl/ivueUTG6bpSkPnLHj4AslwVH5kaEkE3tYpy7rHcTG9sq6ur8Uek2vvuLw1XLDgr+MJE1n11dypkk97JLhOHJPUELn9DcdZ8hL847tmMEAK+hvVDFPU1htbsto1G/ZzHGg0s8sztf3I3Zb8V8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870206; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=c6Lwpzu4bQyFqEheBTwdIlkXZjUIiiE3va3aldXb5fM=; b=MkKZMkRgsh2oVPpiOhrKFFJvDq8WXxIKhI20wQ3ZDT3ujUpvUqcLtpAh0XygeR6HV1JU0KkvVe5HxcDvt9vRL647LpDGdSz3GkE/fdAi3OSBh0B1go6Xzo4DPiHWB4gfS4Dzz0PiB01HpZ2NLfgZIJ3Z5h3i+Z/JYepaR0UgvVQ= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870206654529.330431439624; Thu, 5 Nov 2026 01:16:46 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZd-0000TM-90; Thu, 05 Nov 2026 04:15:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtZH-00070M-9d for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:35 -0500 Received: from mail-pj1-f41.google.com ([209.85.216.41]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtZF-0005l2-Ax for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:35 -0500 Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3ac77270fbfso995737a91.2 for ; Thu, 05 Nov 2026 01:15:32 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0969cf5d7sm3795244a91.3.2026.11.05.01.15.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:15:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870132; x=1794474932; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=c6Lwpzu4bQyFqEheBTwdIlkXZjUIiiE3va3aldXb5fM=; b=fz01NeGpppjTgt3jOp4LxNY5QuKDjlybC+6khg1hWz7DcjO4oqW8hm7dDxoltgLw0N SoYON+SZS9EddR8S1ITkXxUnCzfPYeAs0rOeX09Cj+rWTT2SquA6R+MzZVN0OIj1zoQH BGcq9aBJe5rig4oLisMcik55qmLM+zfeO+dgowYXDTZyMyVNR8WD89yGcU9/zhzhU/Xo R7qpWkXLVhxSI5RN5cHgJFzo+Rs5ku+za6teaXLj4jGjghn8THbMTTo36d3lGBJ1vfh4 x/k0QR/2sfz+jriVIWvJjWDiXh/GQdgKjgzDIVc9WMv6+HZB/h2C838j6u8MaeroeKX+ f7wQ== X-Gm-Message-State: APuU4Z92y9qJy5yVkQmyJXCDrrWLLcEWBc1zQ+koxwAY04O2V250XDvi S5/boUMK7IriWsPIV4DXZchbZCt7rbAEv7YApepg3ypzZDC3Lu2GaWLD2uI6U+meIqE= X-Gm-Gg: AYBFou3JKE+mLGnYZhyuAduk19G8GynmPAJ1DzkPszC8/EMERiKW9NYzuDsLSmBAxbe wPM+OLacDDXTDXqLgRX8D+osBjhGXRP8diA/9yHn2nREI0TkW7sr6tMAozCAXJ6ucx1sUFG8xRu F1eEpx0hgYMwVg/fdyA47mRRkMV7MC/QmsRZQvvDYl1Q9aDFzL9l8zi+Cc1gKkXDlDukivmBFFt OpGHR7TT1xmSYyJdoWogBXkTRMO4P/upqMfFQ4TZe66XN9HFukmW9pgpV/asHn3vTcSwwHLBdNf iFK56NIatTO6dz6vnafaEh08T7wwQ5oAOuTgurGAva3i+VuE/scHVrr10oBf9ef+JrQ8WUKQXDw UwgTEBocsv3xKXKXT1grRsRZjA3Cm647hS9Bff0UjTULmfmoPsztIS3TBWBB/eYQo3j8roNPPE1 wrbIcLDMObPfWQz4ORHzc0YcN14Mbu2tKW8VObYafY3txf53Y2640N7+fJfjEGA1TDpeS/03KPz Mwu1bmQR0xtvyOFPomga5Auzn3Br/6dAEtvzWuDS5r07A1RFbb6pTl+e6SPHfG0XAI4XV5t+vjk oqFa+PUHE2vB24C9q3KVF+z1e5PZTuLjx50hhxbfreZDTe1ZGj6UYKC77hsjvnIr55NAAFTv2qm nnvcqGg== X-Received: by 2002:a17:90b:4c52:b0:3b0:a4d1:6d7a with SMTP id 98e67ed59e1d1-3b0a4d1700dmr2760498a91.31.1793870131570; Thu, 05 Nov 2026 01:15:31 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:07 +0800 Subject: [PATCH 15/16] tests/qtest: ina238: cover conversion-ready and alert-latch semantics MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-15-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=6851; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=RdMmfGACFu+w8i2ytQE/+wzOyY16K5WkLJ6oJOceA5s=; b=o5X3mJFRnFExcSirkSOkdRfGWzm6q/3xm9gKiBAmWgWKPFvUxfHYtSFDsCIiY64jUjkqFAlks Cxd5hC4j/cBCHt8AjC2kRFUOcItsF3UeYgR39J10GUdnRWkLGY083tU X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.216.41; envelope-from=eblot.ml@gmail.com; helo=mail-pj1-f41.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870209058158500 Add coverage for the DIAG_ALRT flag-clearing rules: which of a read and a write clears CNVRF and the limit flags in each alert mode, and that a conversion only re-evaluates the flags of the channels it converted. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 127 ++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 127 insertions(+) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index fb7bcc265c..e7dd50a822 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -494,6 +494,106 @@ static void test_alert_latch(void *obj, void *data, Q= GuestAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); } =20 +/* In latch mode a read releases the flag even while the fault persists. */ +static void test_alert_latch_persist(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + + qmp_ina238_set("shunt-voltage", 97200000); + + /* First read shows the latched flag and clears it */ + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + /* Fault still present but no new conversion: the flag stays cleared */ + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); +} + +/* + * In transparent mode a limit flag follows the live condition: a read lea= ves + * it set while the fault persists. + */ +static void test_alert_transparent_persist(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + /* Repeated reads do not clear the flag while the fault is still prese= nt. */ + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + /* A conversion with the fault gone clears it. */ + qmp_ina238_set("shunt-voltage", 5000000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); +} + +/* A DIAG_ALRT write releases a latched limit flag as a read does. */ +static void test_alert_latch_write_clears(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + /* The read above cleared it; re-latch it with another conversion. */ + qmp_ina238_set("shunt-voltage", 97200000); + + /* A write (keeping latch mode) clears the flag while the fault persis= ts. */ + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); + + /* The next conversion re-asserts the still-present fault. */ + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); +} + +/* + * A conversion only re-evaluates the flags of the channels it converted: = the + * shunt register a bus-only conversion left stale must not resurrect one. + */ +static void test_alert_channel(void *obj, void *data, QGuestAllocator *all= oc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SOVL, 0x1000); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + + qmp_ina238_set("shunt-voltage", 97200000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, + DIAG_SHNTOL); + + /* Continuous bus-only mode */ + i2c_set16(dev, REG_ADC_CONFIG, 0x9000); + qmp_ina238_set("bus-voltage", 12000000); + + g_assert_cmphex(i2c_get16(dev, REG_VSHUNT), =3D=3D, 0x4BF0); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_SHNTOL, =3D=3D, 0= ); +} + /* DIAG_ALRT control bits are writable; status bits are read-only */ static void test_diag_control(void *obj, void *data, QGuestAllocator *allo= c) { @@ -529,6 +629,25 @@ static void test_cnvrf(void *obj, void *data, QGuestAl= locator *alloc) g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, 0); } =20 +/* A DIAG_ALRT write clears CNVRF only in latch mode. */ +static void test_cnvrf_write(void *obj, void *data, QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + /* Latch mode: a write clears CNVRF. */ + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + qmp_ina238_set("shunt-voltage", 20000000); + i2c_set16(dev, REG_DIAG_ALRT, DIAG_ALATCH); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, 0); + + /* Transparent mode: a write does not clear CNVRF. */ + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 20000000); + i2c_set16(dev, REG_DIAG_ALRT, 0x0000); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_CNVRF, =3D=3D, DI= AG_CNVRF); +} + /* Shunt-voltage injection limits */ static void test_shunt_limits(void *obj, void *data, QGuestAllocator *allo= c) { @@ -625,7 +744,15 @@ static void ina238_register_nodes(void) qos_add_test("alert-power", "ina238", test_alert_power, NULL); qos_add_test("alert-multi", "ina238", test_alert_multi, NULL); qos_add_test("alert-latch", "ina238", test_alert_latch, NULL); + qos_add_test("alert-latch-persist", "ina238", test_alert_latch_persist, + NULL); + qos_add_test("alert-transparent-persist", "ina238", + test_alert_transparent_persist, NULL); + qos_add_test("alert-latch-write-clears", "ina238", + test_alert_latch_write_clears, NULL); + qos_add_test("alert-channel", "ina238", test_alert_channel, NULL); qos_add_test("diag-control", "ina238", test_diag_control, NULL); qos_add_test("cnvrf", "ina238", test_cnvrf, NULL); + qos_add_test("cnvrf-write", "ina238", test_cnvrf_write, NULL); } libqos_init(ina238_register_nodes); --=20 2.53.0-Meta From nobody Mon Nov 23 04:30:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1793870163; cv=none; d=zohomail.com; s=zohoarc; b=WR1uM1v8FgDS9PcWyO04GpZy0OzzdhqjgxhmB53thZEa6j4w1jtx1ISzg/136a9W4+V7SqViHiY8yvnsSMF8s3N2+o5X07hLuZJHIHDQ1tKN7U2S4CEwxyeRrom3HyzLbdoiB1Q74GrKwCgDWaYdUkWyQsOOIcRZjabEGCCi63A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793870163; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=GgVTUxuGD9oTRYIDiMSJ0REbd9D3oR+ntYCQl5qLzoc=; b=dYLRwAT2+C+O08D1D3Xqbe9NkcFvEv2z8TxzfQvsoZ4ZIdKVlyNfZUND4HFa0bMy0AqHrCe+q+V8hSQMhfMMl+KOrl2AxCqFF4lvZNhqAz9x0IXtf5dhqTuPrhqIiXgDyzuHCb0gUm8uzsjvBeaZFTHeaJBrvloh0RkzBIi33f8= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793870163063476.46061230898954; Thu, 5 Nov 2026 01:16:03 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOtZU-0008CU-PV; Thu, 05 Nov 2026 04:15:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOtZP-0007a7-Kg for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:44 -0500 Received: from mail-pg1-f172.google.com ([209.85.215.172]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xOtZN-0005lr-Bm for qemu-devel@nongnu.org; Thu, 05 Nov 2026 04:15:43 -0500 Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-cc4c02ddd62so1067196a12.0 for ; Thu, 05 Nov 2026 01:15:40 -0800 (PST) Received: from localhost (118-163-122-149.hinet-ip.hinet.net. [118.163.122.149]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b3131f8csm3195613a91.2.2026.11.05.01.15.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2026 01:15:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793870139; x=1794474939; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=GgVTUxuGD9oTRYIDiMSJ0REbd9D3oR+ntYCQl5qLzoc=; b=tMkhtN5+0KZpC7YOHHYYtlaR4vw0VGzuC/KjBJ4n7topqtJAuXv1A0exR+2I6n/Z38 tclU4Af5hvJcPjDWccaJ6meq76sEaZFeOYICgZeZb8CKQywZ52u3XG12ua4gG2g4Xqqg SgBoz4+Xlu6ISXNFZrzdi3VNBm2o/QfhVQjUoOAAkY9A0d6WDpU2Sc2AcWTso3E87Oy2 TfHUzJywH00LL4GWtdo+tFAFjzitRUKMizEVJInqJRrNEFXLFunYYs+lgh/Mf6uI7j+I TlegluSPQ2o+0EbdDPMkSp8sQx8EAJSu9Cp3zqWRSef52Ut7RpfkApY2XRFS/JRc51E6 f3jQ== X-Gm-Message-State: APuU4Z874ApLE0iwUy3N++3pzBLap7LkvmkgaYD/WUnyAkFW53id0VT/ 7qiReVZ0vwee2eKwcf9+Fw/YTkALQZdUO9q0XPQGQUg435AE2mY67uQ0Q4jp1JDlMfc= X-Gm-Gg: AYBFou2oMNgjPV9Bpmb5bGNox2iQZ7QKsxErMXt/eSusZClS3/kpyeE2eUMAvRJjsJB KoR9zS8YMOKmICPF3O4WBhhck/Hp6SyEKSe6w/e/NYhi3gYry6QcbpTj1eJQI6cHTm5S8M5Fdn1 1lIFbsXJEen8B2j8CG/DkaRrzHXz/xPfMWUtCkBSfkVfOBfpXZGvgWHxRLX1A3mI4eShST9CNFv Xq7VtAV/49B3ve4xBT2StJJG9DeDKhz3Kg4ly2l871sOFuiH+8xe+cXuGWnVhXDudWzk+i1SXQr S6Vl2xA5+ecvmH+9+l2mFqsba3GOhrx8pSGHhIElN7ZKTVtjua066FVyS4zNZ5seWvZBicsCllF BjD03+9EEype1+p1zsS6Z33uolPIAVZ4ie3BNJsAf6ANBzIYEVS4mq+GYFNnoZPgREwt29300yB pG4cN7pgFtxfE0kSoMWTEqCukIomXER7uMqYgXpoiUv8n7mWatisIDP/+Wu1FAJ5zz5HyjO11Xm ThDIIqD7sfW/bR7VErjWSE3B5CludoJhyH7OmoQD4TlE0SE7q7i/bAxqJGZpe/2qi5UPz7LGfj0 1tU3lBvPqUxy1tSAbEiCnw0kp3MvKIZ7aUH07yyJo9n2NjULZ3ZwAI/N0xVDCxkgEbW0SZm+9tL cTXNWmA== X-Received: by 2002:a17:90b:3811:b0:3b0:adba:45e9 with SMTP id 98e67ed59e1d1-3b0adc98d2emr2059059a91.58.1793870139343; Thu, 05 Nov 2026 01:15:39 -0800 (PST) Date: Thu, 05 Nov 2026 17:13:08 +0800 Subject: [PATCH 16/16] tests/qtest: ina238: cover a cleared calibration per channel MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20261105-sensor-ina23x-v1-16-5433ed104990@free.fr> References: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> In-Reply-To: <20261105-sensor-ina23x-v1-0-5433ed104990@free.fr> To: qemu-devel@nongnu.org Cc: =?utf-8?q?C=C3=A9dric_Le_Goater?= , eblot@meta.com, Paolo Bonzini , Peter Maydell , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-arm@nongnu.org, Fabiano Rosas , Laurent Vivier , Emmanuel Blot , Glenn Miles X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1793869976; l=4102; i=emmanuel.blot@free.fr; s=20260602; h=from:subject:message-id; bh=2gZSb8/zG9OIbMPLJl7bRBev6cgnZtrY0rkeYgdq44I=; b=6CZ3iFAartqcYhtQ3iug3b5RkJDKGtbt9LqMMbzhkiR5B08rXxn4SUPoaEYqHavyaKWkRPEFr 29A1MGYGlDlCeUBXxpo68khC06uFpJuvZAzWGZzbdFI22JeP6nIt+WI X-Developer-Key: i=emmanuel.blot@free.fr; a=ed25519; pk=0qFrZ21FAAN8OfVsSoVcCFTk/NMYOWzxJz4paSAVm0g= Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=209.85.215.172; envelope-from=eblot.ml@gmail.com; helo=mail-pg1-f172.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Emmanuel Blot From: Emmanuel Blot via qemu development Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1793870164345158501 Verify that without a calibration value the current and power registers read zero, whichever channels the mode converts. Reviewed-by: Glenn Miles Signed-off-by: Emmanuel Blot --- tests/qtest/ina238-test.c | 48 +++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 48 insertions(+) diff --git a/tests/qtest/ina238-test.c b/tests/qtest/ina238-test.c index e7dd50a822..4e4146461c 100644 --- a/tests/qtest/ina238-test.c +++ b/tests/qtest/ina238-test.c @@ -282,6 +282,30 @@ static void test_zero_cal(void *obj, void *data, QGues= tAllocator *alloc) g_assert_cmpuint(i2c_get24(dev, REG_POWER), =3D=3D, 0); } =20 +/* Clearing SHUNT_CAL zeroes both derived registers in any mode */ +static void test_zero_cal_channels(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + static const uint16_t modes[] =3D { 0x9000, 0xA000, 0xF000 }; + size_t idx; + + for (idx =3D 0; idx < ARRAY_SIZE(modes); idx++) { + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + i2c_set16(dev, REG_SHUNT_CAL, 0x0FD2); + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), !=3D, 0x0000); + g_assert_cmpuint(i2c_get24(dev, REG_POWER), !=3D, 0); + + i2c_set16(dev, REG_ADC_CONFIG, modes[idx]); + i2c_set16(dev, REG_SHUNT_CAL, 0x0000); + + g_assert_cmphex(i2c_get16(dev, REG_CURRENT), =3D=3D, 0x0000); + g_assert_cmpuint(i2c_get24(dev, REG_POWER), =3D=3D, 0); + } +} + /* ADCRANGE changes the shunt LSB, not the current divisor. */ static void test_adcrange(void *obj, void *data, QGuestAllocator *alloc) { @@ -458,6 +482,27 @@ static void test_alert_power(void *obj, void *data, QG= uestAllocator *alloc) g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, =3D=3D, 0); } =20 +/* Clearing SHUNT_CAL drops the power alert with the power reading */ +static void test_alert_power_zero_cal(void *obj, void *data, + QGuestAllocator *alloc) +{ + QI2CDevice *dev =3D (QI2CDevice *)obj; + + i2c_set16(dev, REG_CONFIG, CONFIG_RST); + qmp_ina238_set("shunt-voltage", 97200000); + qmp_ina238_set("bus-voltage", 48000000); + i2c_set16(dev, REG_SHUNT_CAL, 0x0FD2); + i2c_set16(dev, REG_PWR_LIMIT, 0x0100); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, =3D=3D, DIAG= _POL); + + /* Continuous shunt-only: the power register is not reconverted. */ + i2c_set16(dev, REG_ADC_CONFIG, 0xA000); + i2c_set16(dev, REG_SHUNT_CAL, 0x0000); + + g_assert_cmpuint(i2c_get24(dev, REG_POWER), =3D=3D, 0); + g_assert_cmphex(i2c_get16(dev, REG_DIAG_ALRT) & DIAG_POL, =3D=3D, 0); +} + /* Two thresholds crossed at once are reported simultaneously (multi-alert= ) */ static void test_alert_multi(void *obj, void *data, QGuestAllocator *alloc) { @@ -733,6 +778,7 @@ static void ina238_register_nodes(void) NULL); qos_add_test("power", "ina238", test_power, NULL); qos_add_test("zero-cal", "ina238", test_zero_cal, NULL); + qos_add_test("zero-cal-channels", "ina238", test_zero_cal_channels, NU= LL); qos_add_test("adcrange", "ina238", test_adcrange, NULL); qos_add_test("mode-shutdown", "ina238", test_mode_shutdown, NULL); qos_add_test("mode-triggered", "ina238", test_mode_triggered, NULL); @@ -742,6 +788,8 @@ static void ina238_register_nodes(void) qos_add_test("alert-bus", "ina238", test_alert_bus, NULL); qos_add_test("alert-temp", "ina238", test_alert_temp, NULL); qos_add_test("alert-power", "ina238", test_alert_power, NULL); + qos_add_test("alert-power-zero-cal", "ina238", test_alert_power_zero_c= al, + NULL); qos_add_test("alert-multi", "ina238", test_alert_multi, NULL); qos_add_test("alert-latch", "ina238", test_alert_latch, NULL); qos_add_test("alert-latch-persist", "ina238", test_alert_latch_persist, --=20 2.53.0-Meta