linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Erik Rosen <erik.rosen@metormote.com>
To: Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>,
	Jonathan Corbet <corbet@lwn.net>,
	linux-hwmon@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Erik Rosen <erik.rosen@metormote.com>
Subject: [PATCH v2 3/6] hwmon: (pmbus/pim4328) Add support for reading direct format coefficients
Date: Sat, 22 May 2021 12:55:25 +0200	[thread overview]
Message-ID: <20210522105528.87629-4-erik.rosen@metormote.com> (raw)
In-Reply-To: <20210522105528.87629-1-erik.rosen@metormote.com>

Add support for reading and decoding direct format coefficients to
the PMBus core driver. If the new flag PMBUS_USE_COEFFICIENTS_CMD
is set, the driver will use the COEFFICIENTS register together with
the information in the pmbus_sensor_attr structs to initialize
relevant coefficients for the direct mode format.

Signed-off-by: Erik Rosen <erik.rosen@metormote.com>
---
 drivers/hwmon/pmbus/pmbus_core.c | 93 ++++++++++++++++++++++++++++++++
 include/linux/pmbus.h            |  8 +++
 2 files changed, 101 insertions(+)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index 460cbfd716e4..03c169bf5633 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -2177,6 +2177,38 @@ static int pmbus_find_attributes(struct i2c_client *client,
 	return ret;
 }
 
+static int pmbus_init_coefficients(struct i2c_client *client,
+				   struct pmbus_data *data, int page,
+				   enum pmbus_sensor_classes sensor_class,
+				   const struct pmbus_sensor_attr *attrs,
+				   int nattrs)
+{
+	int i, status;
+
+	for (i = 0; i < nattrs; i++) {
+		if (attrs->class == sensor_class &&
+		    (attrs->func & data->info->func[page])) {
+			status = pmbus_read_coefficients(client,
+							 (struct pmbus_driver_info *)data->info,
+							 sensor_class,
+							 attrs->reg);
+			if (status < 0) {
+				dev_err(&client->dev,
+					"Failed to read coefficients for register: %x\n",
+					attrs->reg);
+				return status;
+			}
+			return 0;
+		}
+		attrs++;
+	}
+
+	dev_err(&client->dev, "No coefficients found for register: %x\n",
+		attrs->reg);
+
+	return -ENODEV;
+}
+
 /*
  * Identify chip parameters.
  * This function is called for all chips.
@@ -2185,6 +2217,7 @@ static int pmbus_identify_common(struct i2c_client *client,
 				 struct pmbus_data *data, int page)
 {
 	int vout_mode = -1;
+	int ret;
 
 	if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
 		vout_mode = _pmbus_read_byte_data(client, page,
@@ -2214,6 +2247,66 @@ static int pmbus_identify_common(struct i2c_client *client,
 		}
 	}
 
+	if (data->flags & PMBUS_USE_COEFFICIENTS_CMD) {
+		if (!i2c_check_functionality(client->adapter,
+					     I2C_FUNC_SMBUS_BLOCK_PROC_CALL))
+			return -ENODEV;
+
+		if (data->info->format[PSC_VOLTAGE_IN] == direct) {
+			ret = pmbus_init_coefficients(client, data, page,
+						      PSC_VOLTAGE_IN,
+						      voltage_attributes,
+						      ARRAY_SIZE(voltage_attributes));
+			if (ret)
+				return ret;
+		}
+
+		if (data->info->format[PSC_VOLTAGE_OUT] == direct) {
+			ret = pmbus_init_coefficients(client, data, page,
+						      PSC_VOLTAGE_OUT,
+						      voltage_attributes,
+						      ARRAY_SIZE(voltage_attributes));
+			if (ret)
+				return ret;
+		}
+
+		if (data->info->format[PSC_CURRENT_IN] == direct) {
+			ret = pmbus_init_coefficients(client, data, page,
+						      PSC_CURRENT_IN,
+						      current_attributes,
+						      ARRAY_SIZE(current_attributes));
+			if (ret)
+				return ret;
+		}
+
+		if (data->info->format[PSC_CURRENT_OUT] == direct) {
+			ret = pmbus_init_coefficients(client, data, page,
+						      PSC_CURRENT_OUT,
+						      current_attributes,
+						      ARRAY_SIZE(current_attributes));
+			if (ret)
+				return ret;
+		}
+
+		if (data->info->format[PSC_POWER] == direct) {
+			ret = pmbus_init_coefficients(client, data, page,
+						      PSC_POWER,
+						      power_attributes,
+						      ARRAY_SIZE(power_attributes));
+			if (ret)
+				return ret;
+		}
+
+		if (data->info->format[PSC_TEMPERATURE] == direct) {
+			ret = pmbus_init_coefficients(client, data, page,
+						      PSC_TEMPERATURE,
+						      temp_attributes,
+						      ARRAY_SIZE(temp_attributes));
+			if (ret)
+				return ret;
+		}
+	}
+
 	pmbus_clear_fault_page(client, page);
 	return 0;
 }
diff --git a/include/linux/pmbus.h b/include/linux/pmbus.h
index f720470b1bab..7fdc282dab5a 100644
--- a/include/linux/pmbus.h
+++ b/include/linux/pmbus.h
@@ -52,6 +52,14 @@
  */
 #define PMBUS_NO_WRITE_PROTECT			BIT(4)
 
+/*
+ * PMBUS_USE_COEFFICIENTS_CMD
+ *
+ * When this flag is set the PMBus core driver will use the COEFFICIENTS
+ * register to initialize the coefficients for the direct mode format.
+ */
+#define PMBUS_USE_COEFFICIENTS_CMD		BIT(5)
+
 struct pmbus_platform_data {
 	u32 flags;		/* Device specific flags */
 
-- 
2.20.1


  parent reply	other threads:[~2021-05-22 10:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-22 10:55 [PATCH v2 0/6] hwmon: (pmbus/pim4328) Add pim4328 PMBus driver Erik Rosen
2021-05-22 10:55 ` [PATCH v2 1/6] hwmon: (pmbus/pim4328) Add new pmbus flag NO_WRITE_PROTECT Erik Rosen
2021-05-22 10:55 ` [PATCH v2 2/6] hwmon: (pmbus/pim4328) Add function for reading direct mode coefficients Erik Rosen
2021-05-22 10:55 ` Erik Rosen [this message]
2021-05-22 13:41   ` [PATCH v2 3/6] hwmon: (pmbus/pim4328) Add support for reading direct format coefficients Guenter Roeck
2021-05-23 17:52     ` Erik Rosen
2021-05-23 19:47       ` Guenter Roeck
2021-05-22 10:55 ` [PATCH v2 4/6] hwmon: (pmbus/pim4328) Allow phase function even if it's not on page Erik Rosen
2021-05-22 10:55 ` [PATCH v2 5/6] hwmon: (pmbus/pim4328) Add PMBus driver for PIM4006, PIM4328 and PIM4820 Erik Rosen
2021-05-22 10:55 ` [PATCH v2 6/6] hwmon: (pmbus/pim4328) Add documentation for the pim4328 PMBus driver Erik Rosen

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=20210522105528.87629-4-erik.rosen@metormote.com \
    --to=erik.rosen@metormote.com \
    --cc=corbet@lwn.net \
    --cc=jdelvare@suse.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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;
as well as URLs for NNTP newsgroup(s).