From: Naresh Solanki <naresh.solanki@9elements.com>
To: devicetree@vger.kernel.org, Guenter Roeck <linux@roeck-us.net>,
Jean Delvare <jdelvare@suse.com>
Cc: linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org,
Patrick Rudolph <patrick.rudolph@9elements.com>,
Naresh Solanki <Naresh.Solanki@9elements.com>
Subject: [PATCH 8/8] hwmon: (pmbus/mp2975) Add OCP limit
Date: Wed, 12 Jul 2023 13:47:49 +0200 [thread overview]
Message-ID: <20230712114754.500477-8-Naresh.Solanki@9elements.com> (raw)
In-Reply-To: <20230712114754.500477-1-Naresh.Solanki@9elements.com>
From: Patrick Rudolph <patrick.rudolph@9elements.com>
Add support for PMBUS_IOUT_OC_FAULT_LIMIT.
Add a helper function to convert the limit to LINEAR11 format
and read data->info.phases[0] on MP2971 and MP2973 as well.
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Naresh Solanki <Naresh.Solanki@9elements.com>
---
drivers/hwmon/pmbus/mp2975.c | 76 ++++++++++++++++++++++++++++++------
1 file changed, 65 insertions(+), 11 deletions(-)
diff --git a/drivers/hwmon/pmbus/mp2975.c b/drivers/hwmon/pmbus/mp2975.c
index 27a9724aed68..0a6669f865f6 100644
--- a/drivers/hwmon/pmbus/mp2975.c
+++ b/drivers/hwmon/pmbus/mp2975.c
@@ -65,6 +65,10 @@
#define MP2973_VID_STEP_SEL_R2 BIT(3)
#define MP2973_IMVP9_EN_R2 BIT(13)
+#define MP2973_MFR_OCP_TOTAL_SET 0x5f
+#define MP2973_OCP_TOTAL_CUR_MASK GENMASK(6, 0)
+#define MP2973_MFR_OCP_LEVEL_RES BIT(15)
+
#define MP2973_MFR_READ_IOUT_PK 0x90
#define MP2973_MFR_READ_POUT_PK 0x91
@@ -153,6 +157,41 @@ mp2975_vid2direct(int vrf, int val)
return 0;
}
+#define MAX_LIN_MANTISSA (1023 * 1000)
+#define MIN_LIN_MANTISSA (511 * 1000)
+
+/* Converts a milli-unit DIRECT value to LINEAR11 format */
+static u16 mp2975_data2reg_linear11(s64 val)
+{
+ s16 exponent = 0, mantissa;
+ bool negative = false;
+
+ /* simple case */
+ if (val == 0)
+ return 0;
+
+ /* Reduce large mantissa until it fits into 10 bit */
+ while (val >= MAX_LIN_MANTISSA && exponent < 15) {
+ exponent++;
+ val >>= 1;
+ }
+ /* Increase small mantissa to improve precision */
+ while (val < MIN_LIN_MANTISSA && exponent > -15) {
+ exponent--;
+ val <<= 1;
+ }
+
+ /* Convert mantissa from milli-units to units */
+ mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
+
+ /* restore sign */
+ if (negative)
+ mantissa = -mantissa;
+
+ /* Convert to 5 bit exponent, 11 bit mantissa */
+ return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
+}
+
static int
mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data,
int page, int phase, u8 reg)
@@ -297,6 +336,20 @@ static int mp2973_read_word_data(struct i2c_client *client, int page,
ret = pmbus_read_word_data(client, page, phase,
MP2973_MFR_READ_IOUT_PK);
break;
+ case PMBUS_IOUT_OC_FAULT_LIMIT:
+ ret = mp2975_read_word_helper(client, page, phase,
+ MP2973_MFR_OCP_TOTAL_SET,
+ GENMASK(15, 0));
+ if (ret < 0)
+ return ret;
+
+ if (ret & MP2973_MFR_OCP_LEVEL_RES)
+ ret = 2 * (ret & MP2973_OCP_TOTAL_CUR_MASK);
+ else
+ ret = ret & MP2973_OCP_TOTAL_CUR_MASK;
+
+ ret = mp2975_data2reg_linear11(ret * info->phases[page] * 1000);
+ break;
case PMBUS_UT_WARN_LIMIT:
case PMBUS_UT_FAULT_LIMIT:
case PMBUS_VIN_UV_WARN_LIMIT:
@@ -307,7 +360,6 @@ static int mp2973_read_word_data(struct i2c_client *client, int page,
case PMBUS_IIN_OC_FAULT_LIMIT:
case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
case PMBUS_IOUT_OC_WARN_LIMIT:
- case PMBUS_IOUT_OC_FAULT_LIMIT:
case PMBUS_IOUT_UC_FAULT_LIMIT:
case PMBUS_POUT_OP_FAULT_LIMIT:
case PMBUS_POUT_OP_WARN_LIMIT:
@@ -481,11 +533,13 @@ mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data,
if (info->phases[0] > data->max_phases[0])
return -EINVAL;
- mp2975_set_phase_rail1(info);
- num_phases2 = min(data->max_phases[0] - info->phases[0],
- data->max_phases[1]);
- if (info->phases[1] && info->phases[1] <= num_phases2)
- mp2975_set_phase_rail2(info, num_phases2);
+ if (data->chip_id == mp2975) {
+ mp2975_set_phase_rail1(info);
+ num_phases2 = min(data->max_phases[0] - info->phases[0],
+ data->max_phases[1]);
+ if (info->phases[1] && info->phases[1] <= num_phases2)
+ mp2975_set_phase_rail2(info, num_phases2);
+ }
return 0;
}
@@ -879,12 +933,12 @@ static int mp2975_probe(struct i2c_client *client)
data->info.num_regulators = MP2975_PAGE_NUM;
}
- if (data->chip_id == mp2975) {
- /* Identify multiphase configuration. */
- ret = mp2975_identify_multiphase(client, data, info);
- if (ret)
- return ret;
+ /* Identify multiphase configuration. */
+ ret = mp2975_identify_multiphase(client, data, info);
+ if (ret)
+ return ret;
+ if (data->chip_id == mp2975) {
/* Identify VID setting per rail. */
ret = mp2975_identify_rails_vid(client, data, info);
if (ret < 0)
--
2.41.0
prev parent reply other threads:[~2023-07-12 11:50 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-12 11:47 [PATCH 1/8] hwmon: (pmbus/mp2975) Fix whitespace error Naresh Solanki
2023-07-12 11:47 ` [PATCH 2/8] dt-bindings: trivial-devices: Add MPS MP2971 and MP2973 Naresh Solanki
2023-07-12 11:51 ` Krzysztof Kozlowski
2023-07-12 11:47 ` [PATCH 3/8] hwmon: (pmbus/mp2975) Prepare for MP2973 and MP2971 Naresh Solanki
2023-07-12 11:47 ` [PATCH 4/8] hwmon: (pmbus/mp2975) Simplify VOUT code Naresh Solanki
2023-07-12 15:49 ` Guenter Roeck
2023-07-14 11:23 ` Naresh Solanki
2023-07-12 11:47 ` [PATCH 5/8] hwmon: (pmbus/mp2975) Make phase count variable Naresh Solanki
2023-07-12 11:47 ` [PATCH 6/8] hwmon: (pmbus/mp2975) Add support for MP2971 and MP2973 Naresh Solanki
2023-07-12 17:58 ` Guenter Roeck
2023-07-14 12:25 ` Naresh Solanki
2023-07-12 11:47 ` [PATCH 7/8] hwmon: (pmbus/mp2975) Add regulator support Naresh Solanki
2023-07-12 11:47 ` Naresh Solanki [this message]
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=20230712114754.500477-8-Naresh.Solanki@9elements.com \
--to=naresh.solanki@9elements.com \
--cc=devicetree@vger.kernel.org \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=patrick.rudolph@9elements.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 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).