mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
To: patrick@stwcx.xyz
Cc: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>,
	Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] hwmon: Add support for ina233
Date: Tue, 14 May 2024 17:22:40 +0800	[thread overview]
Message-ID: <20240514092242.2739662-1-Delphine_CC_Chiu@wiwynn.com> (raw)

Support ina233 with vshunt

Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
---
 drivers/hwmon/pmbus/ina233.c | 37 +++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/pmbus/ina233.c b/drivers/hwmon/pmbus/ina233.c
index d5c7d7408ac3..33736e5049a9 100644
--- a/drivers/hwmon/pmbus/ina233.c
+++ b/drivers/hwmon/pmbus/ina233.c
@@ -11,9 +11,43 @@
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/hwmon-sysfs.h>
 #include "pmbus.h"
 
 #define MFR_CALIBRATION	0xd4
+#define MFR_READ_VSHUNT 0xd1
+
+#define HIGHEST_BIT 15
+
+static ssize_t ina233_vshunt_show(struct device *dev,
+				  struct device_attribute *devattr, char *buf)
+{
+	int shunt_volt;
+	struct i2c_client *client = to_i2c_client(dev->parent);
+
+	shunt_volt = i2c_smbus_read_word_data(client, MFR_READ_VSHUNT);
+	if (shunt_volt < 0)
+		return shunt_volt;
+
+	// Is negative
+	if (shunt_volt & (1 << HIGHEST_BIT))
+		shunt_volt = ~(shunt_volt & ~(1 << HIGHEST_BIT)) + 1;
+
+	// This unit is mV.
+	// LSB 2.5 μV is reference from spec 7.6.3.2 MFR_READ_VSHUNT
+	int val = DIV_ROUND_UP(shunt_volt * 25, 10000);
+
+	return sysfs_emit(buf, "%d\n", val);
+}
+
+static SENSOR_DEVICE_ATTR_RO(vshunt, ina233_vshunt, 1);
+
+static struct attribute *ina233_attrs[] = {
+	&sensor_dev_attr_vshunt.dev_attr.attr,
+	NULL,
+};
+
+ATTRIBUTE_GROUPS(ina233);
 
 struct pmbus_driver_info ina233_info = {
 	.pages = 1,
@@ -29,6 +63,7 @@ struct pmbus_driver_info ina233_info = {
 	.m[PSC_VOLTAGE_OUT] = 8,
 	.R[PSC_VOLTAGE_IN] = 2,
 	.R[PSC_VOLTAGE_OUT] = 2,
+	.groups = ina233_groups,
 };
 
 static int ina233_probe(struct i2c_client *client)
@@ -82,7 +117,7 @@ static struct i2c_driver ina233_driver = {
 
 module_i2c_driver(ina233_driver);
 
-MODULE_AUTHOR("Eli Huang <eli_huang@wiwynn.com>");
+MODULE_AUTHOR("Delphine_CC_Chiu <Delphine_CC_Chiu@wiwynn.com>");
 MODULE_DESCRIPTION("PMBus driver for Texas Instruments INA233 and compatible chips");
 MODULE_LICENSE("GPL");
 MODULE_IMPORT_NS(PMBUS);
-- 
2.25.1


             reply	other threads:[~2024-05-14  9:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-14  9:22 Delphine CC Chiu [this message]
2024-05-14 16:44 ` Guenter Roeck

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=20240514092242.2739662-1-Delphine_CC_Chiu@wiwynn.com \
    --to=delphine_cc_chiu@wiwynn.com \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=patrick@stwcx.xyz \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®