From: Titus Rwantare <titusr@google.com>
To: peter.maydell@linaro.org
Cc: qemu-arm@nongnu.org, qemu-devel@nongnu.org, kfting@nuvoton.com,
imaginos32@gmail.com, wuhaotsh@google.com, philmd@mailo.com,
Titus Rwantare <titusr@google.com>
Subject: [PATCH 2/5] hw/i2c: pmbus: add milliunits linear mode functions
Date: Mon, 6 Jul 2026 23:00:51 +0000 [thread overview]
Message-ID: <20260706230056.1888992-3-titusr@google.com> (raw)
In-Reply-To: <20260706230056.1888992-1-titusr@google.com>
Because we are limited by unsigned ints, we need to be able to provide
inputs in milliunits which can get scaled up to fit in the linear mode
registers.
For example: 3.4 is a valid input, and now we can provide 3400 and scale
it into the register without losing the 0.4
Signed-off-by: Titus Rwantare <titusr@google.com>
---
hw/i2c/pmbus_device.c | 39 +++++++++++++++++++++++++++++++++++
include/hw/i2c/pmbus_device.h | 18 ++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/hw/i2c/pmbus_device.c b/hw/i2c/pmbus_device.c
index b1f9843f52..e2e1e0e945 100644
--- a/hw/i2c/pmbus_device.c
+++ b/hw/i2c/pmbus_device.c
@@ -8,6 +8,7 @@
#include "qemu/osdep.h"
#include <math.h>
+#include <stdint.h>
#include "hw/i2c/pmbus_device.h"
#include "migration/vmstate.h"
#include "qemu/module.h"
@@ -38,6 +39,25 @@ uint16_t pmbus_data2linear_mode(uint16_t value, int exp)
return value >> exp;
}
+uint16_t pmbus_milliunits2linear_mode(uint32_t value, int exp)
+{
+ uint32_t ret;
+
+ /* L = D * 2^(-e) */
+ if (exp < 0) {
+ ret = DIV_ROUND_CLOSEST((value << (-exp)), 1000);
+ } else {
+ ret = DIV_ROUND_CLOSEST((value >> exp), 1000);
+ }
+
+ /* clamp value to maximum if it exceeds representable value*/
+ if (ret > UINT16_MAX) {
+ return UINT16_MAX;
+ }
+
+ return ret;
+}
+
uint16_t pmbus_linear_mode2data(uint16_t value, int exp)
{
/* D = L * 2^e */
@@ -47,6 +67,25 @@ uint16_t pmbus_linear_mode2data(uint16_t value, int exp)
return value << exp;
}
+uint32_t pmbus_linear_mode2milliunits(uint16_t value, int exp)
+{
+ /* D = L * 2^e */
+ uint64_t v = (uint64_t)value;
+ uint64_t ret;
+
+ if (exp < 0) {
+ ret = (v * 1000) >> (-exp);
+ } else {
+ ret = (v << exp) * 1000;
+ }
+
+ if (ret > UINT32_MAX) {
+ return UINT32_MAX;
+ }
+
+ return (uint32_t)ret;
+}
+
void pmbus_send(PMBusDevice *pmdev, const uint8_t *data, uint16_t len)
{
if (pmdev->out_buf_len + len > SMBUS_DATA_MAX_LEN) {
diff --git a/include/hw/i2c/pmbus_device.h b/include/hw/i2c/pmbus_device.h
index f195c11384..9f3569e997 100644
--- a/include/hw/i2c/pmbus_device.h
+++ b/include/hw/i2c/pmbus_device.h
@@ -481,6 +481,15 @@ uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value);
*/
uint16_t pmbus_data2linear_mode(uint16_t value, int exp);
+/**
+ * Convert milliunit sensor value to linear mode format
+ *
+ * L = D * 2^(-e)
+ *
+ * @return uint16
+ */
+uint16_t pmbus_milliunits2linear_mode(uint32_t value, int exp);
+
/**
* Convert linear mode formatted data into sensor reading
*
@@ -490,6 +499,15 @@ uint16_t pmbus_data2linear_mode(uint16_t value, int exp);
*/
uint16_t pmbus_linear_mode2data(uint16_t value, int exp);
+/**
+ * Convert linear mode formatted data into sensor reading in milliunits
+ *
+ * D = L * 2^e
+ *
+ * @return uint32 value in milliunits
+ */
+uint32_t pmbus_linear_mode2milliunits(uint16_t value, int exp);
+
/**
* @brief Send a block of data over PMBus
* Assumes that the bytes in the block are already ordered correctly,
--
2.55.0.rc2.803.g1fd1e6609c-goog
next prev parent reply other threads:[~2026-07-06 23:02 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-06 23:00 [PATCH 0/5] hw/i2c: PMBus updates Titus Rwantare
2026-07-06 23:00 ` [PATCH 1/5] osdep: add DIV_ROUND_CLOSEST Titus Rwantare
2026-07-09 11:09 ` Peter Maydell
2026-07-09 11:16 ` Peter Maydell
2026-07-06 23:00 ` Titus Rwantare [this message]
2026-07-06 23:00 ` [PATCH 3/5] hw/i2c: pmbus: clear output buffer on write Titus Rwantare
2026-07-07 9:20 ` Philippe Mathieu-Daudé
2026-07-09 15:35 ` Peter Maydell
2026-07-06 23:00 ` [PATCH 4/5] hw/i2c: smbus: increase MAX_DATA_LEN Titus Rwantare
2026-07-07 9:18 ` Philippe Mathieu-Daudé
2026-07-09 11:25 ` Peter Maydell
2026-07-06 23:00 ` [PATCH 5/5] hw/i2c: pmbus: add Linear11 format helpers Titus Rwantare
2026-07-09 11:14 ` Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260706230056.1888992-3-titusr@google.com \
--to=titusr@google.com \
--cc=imaginos32@gmail.com \
--cc=kfting@nuvoton.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@mailo.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=wuhaotsh@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.