From: Naresh Solanki <naresh.solanki@9elements.com>
To: Liam Girdwood <lgirdwood@gmail.com>,
Mark Brown <broonie@kernel.org>, Jean Delvare <jdelvare@suse.com>,
Guenter Roeck <linux@roeck-us.net>
Cc: zev@bewilderbeest.net,
Naresh Solanki <Naresh.Solanki@9elements.com>,
linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org
Subject: [PATCH] regulator (max5970): Add hwmon support
Date: Wed, 30 Aug 2023 13:13:18 +0200 [thread overview]
Message-ID: <20230830111319.3882281-1-Naresh.Solanki@9elements.com> (raw)
Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage
and current monitoring. This feature is seamlessly integrated through
the hwmon subsystem.
Signed-off-by: Naresh Solanki <Naresh.Solanki@9elements.com>
---
drivers/regulator/max5970-regulator.c | 119 ++++++++++++++++++++++++++
1 file changed, 119 insertions(+)
diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
index b56a174cde3d..3a6f7c293526 100644
--- a/drivers/regulator/max5970-regulator.c
+++ b/drivers/regulator/max5970-regulator.c
@@ -10,6 +10,7 @@
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/err.h>
+#include <linux/hwmon.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/of.h>
@@ -32,6 +33,116 @@ enum max597x_regulator_id {
MAX597X_SW1,
};
+static int max5970_read_adc(struct regmap *regmap, int reg, long *val)
+{
+ u8 reg_data[2];
+ int ret;
+
+ ret = regmap_bulk_read(regmap, reg, ®_data[0], 2);
+ if (ret < 0)
+ return ret;
+
+ *val = (reg_data[0] << 2) | (reg_data[1] & 3);
+
+ return 0;
+}
+
+static int max5970_read(struct device *dev, enum hwmon_sensor_types type,
+ u32 attr, int channel, long *val)
+{
+ struct max5970_data *ddata = dev_get_drvdata(dev);
+ struct regmap *regmap = dev_get_regmap(dev->parent, NULL);
+ int ret;
+
+ switch (type) {
+ case hwmon_curr:
+ switch (attr) {
+ case hwmon_curr_input:
+ ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val);
+ /*
+ * Calculate current from ADC value, IRNG range & shunt resistor value.
+ * ddata->irng holds the voltage corresponding to the maximum value the
+ * 10-bit ADC can measure.
+ * To obtain the output, multiply the ADC value by the IRNG range (in
+ * millivolts) and then divide it by the maximum value of the 10-bit ADC.
+ */
+ *val = (*val * ddata->irng[channel]) >> 10;
+ /* Convert the voltage meansurement across shunt resistor to current */
+ *val = (*val * 1000) / ddata->shunt_micro_ohms[channel];
+ return ret;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ case hwmon_in:
+ switch (attr) {
+ case hwmon_in_input:
+ ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val);
+ /*
+ * Calculate voltage from ADC value and MON range.
+ * ddata->mon_rng holds the voltage corresponding to the maximum value the
+ * 10-bit ADC can measure.
+ * To obtain the output, multiply the ADC value by the MON range (in
+ * microvolts) and then divide it by the maximum value of the 10-bit ADC.
+ */
+ *val = mul_u64_u32_shr(*val, ddata->mon_rng[channel], 10);
+ /* uV to mV */
+ *val = *val / 1000;
+ return ret;
+ default:
+ return -EOPNOTSUPP;
+ }
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+static umode_t max5970_is_visible(const void *data,
+ enum hwmon_sensor_types type,
+ u32 attr, int channel)
+{
+ struct max5970_data *ddata = (struct max5970_data *)data;
+
+ if (channel >= ddata->num_switches)
+ return 0;
+
+ switch (type) {
+ case hwmon_in:
+ switch (attr) {
+ case hwmon_in_input:
+ return 0444;
+ }
+ break;
+ case hwmon_curr:
+ switch (attr) {
+ case hwmon_curr_input:
+ /* Current measurement requires knowledge of the shunt resistor value. */
+ if (ddata->shunt_micro_ohms[channel])
+ return 0444;
+ }
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
+
+static const struct hwmon_ops max5970_hwmon_ops = {
+ .is_visible = max5970_is_visible,
+ .read = max5970_read,
+};
+
+static const struct hwmon_channel_info *max5970_info[] = {
+ HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT),
+ HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT),
+ NULL
+};
+
+static const struct hwmon_chip_info max5970_chip_info = {
+ .ops = &max5970_hwmon_ops,
+ .info = max5970_info,
+};
+
static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity)
{
int ret, reg;
@@ -432,6 +543,7 @@ static int max597x_regulator_probe(struct platform_device *pdev)
struct regulator_config config = { };
struct regulator_dev *rdev;
struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
+ struct device *hwmon_dev;
int num_switches;
int ret, i;
@@ -485,6 +597,13 @@ static int max597x_regulator_probe(struct platform_device *pdev)
max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
}
+ hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970_hwmon", max597x,
+ &max5970_chip_info, NULL);
+ if (IS_ERR(hwmon_dev)) {
+ return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev), \
+ "Unable to register hwmon device\n");
+ }
+
if (i2c->irq) {
ret =
max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches,
base-commit: 35d0d2350d774fecf596cfb2fb050559fe5e1850
--
2.41.0
next reply other threads:[~2023-08-30 19:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-30 11:13 Naresh Solanki [this message]
2023-08-30 15:56 ` [PATCH] regulator (max5970): Add hwmon support Guenter Roeck
2023-08-30 19:14 ` Naresh Solanki
2023-08-30 19:32 ` Guenter Roeck
2023-08-31 8:56 ` Naresh Solanki
2023-09-01 8:00 ` kernel test robot
2023-09-01 8:11 ` kernel test robot
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=20230830111319.3882281-1-Naresh.Solanki@9elements.com \
--to=naresh.solanki@9elements.com \
--cc=broonie@kernel.org \
--cc=jdelvare@suse.com \
--cc=lgirdwood@gmail.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=zev@bewilderbeest.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