From: Tabrez Ahmed <tabreztalks@gmail.com>
To: linux@roeck-us.net
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
shuah@kernel.org, me@brighamcampbell.com,
Tabrez Ahmed <tabreztalks@gmail.com>
Subject: [PATCH 2/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info
Date: Sat, 28 Mar 2026 23:50:15 +0530 [thread overview]
Message-ID: <20260328182015.220298-3-tabreztalks@gmail.com> (raw)
In-Reply-To: <20260328182015.220298-1-tabreztalks@gmail.com>
Convert the driver to use the modern hwmon_device_register_with_info()
API. This removes the need for manual sysfs attribute creation and
simplifies the driver by utilizing the hwmon core's standard callbacks.
Remove the SENSOR_DEVICE_ATTR_RO macros and the attribute group arrays.
Replace the voltage_show() function with an ads7871_read() callback
and define the supported channels via hwmon_channel_info.
Suggested-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
---
drivers/hwmon/ads7871.c | 71 +++++++++++++++++++++--------------------
1 file changed, 37 insertions(+), 34 deletions(-)
diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
index 02e69bef2ca12..8e93e58e9298c 100644
--- a/drivers/hwmon/ads7871.c
+++ b/drivers/hwmon/ads7871.c
@@ -56,7 +56,6 @@
#include <linux/init.h>
#include <linux/spi/spi.h>
#include <linux/hwmon.h>
-#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/mutex.h>
@@ -67,6 +66,15 @@ struct ads7871_data {
struct spi_device *spi;
struct mutex lock;
};
+static umode_t ads7871_is_visible(const void *data,
+ enum hwmon_sensor_types type,
+ u32 attr, int channel)
+{
+ if (type == hwmon_in && attr == hwmon_in_input)
+ return 0444;
+
+ return 0;
+}
static int ads7871_read_reg8(struct spi_device *spi, int reg)
{
@@ -90,16 +98,16 @@ static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
return spi_write(spi, tmp, sizeof(tmp));
}
-static ssize_t voltage_show(struct device *dev, struct device_attribute *da,
- char *buf)
+static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
+ u32 attr, int channel, long *val)
{
struct ads7871_data *pdata = dev_get_drvdata(dev);
struct spi_device *spi = pdata->spi;
- struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
- int ret, val, i = 0;
- uint8_t channel, mux_cnv;
+ int ret, raw_val, i = 0;
+ uint8_t mux_cnv;
- channel = attr->index;
+ if (type != hwmon_in || attr != hwmon_in_input)
+ return -EOPNOTSUPP;
mutex_lock(&pdata->lock);
/*
@@ -131,14 +139,14 @@ static ssize_t voltage_show(struct device *dev, struct device_attribute *da,
}
if (mux_cnv == 0) {
- val = ads7871_read_reg16(spi, REG_LS_BYTE);
- if (val < 0) {
- ret = val;
+ raw_val = ads7871_read_reg16(spi, REG_LS_BYTE);
+ if (raw_val < 0) {
+ ret = raw_val;
goto out_unlock;
}
/*result in volts*10000 = (val/8192)*2.5*10000*/
- val = ((val >> 2) * 25000) / 8192;
- ret = sysfs_emit(buf, "%d\n", val);
+ *val = ((raw_val >> 2) * 25000) / 8192;
+ ret = 0;
goto out_unlock;
}
@@ -148,28 +156,22 @@ static ssize_t voltage_show(struct device *dev, struct device_attribute *da,
return ret;
}
-static SENSOR_DEVICE_ATTR_RO(in0_input, voltage, 0);
-static SENSOR_DEVICE_ATTR_RO(in1_input, voltage, 1);
-static SENSOR_DEVICE_ATTR_RO(in2_input, voltage, 2);
-static SENSOR_DEVICE_ATTR_RO(in3_input, voltage, 3);
-static SENSOR_DEVICE_ATTR_RO(in4_input, voltage, 4);
-static SENSOR_DEVICE_ATTR_RO(in5_input, voltage, 5);
-static SENSOR_DEVICE_ATTR_RO(in6_input, voltage, 6);
-static SENSOR_DEVICE_ATTR_RO(in7_input, voltage, 7);
-
-static struct attribute *ads7871_attrs[] = {
- &sensor_dev_attr_in0_input.dev_attr.attr,
- &sensor_dev_attr_in1_input.dev_attr.attr,
- &sensor_dev_attr_in2_input.dev_attr.attr,
- &sensor_dev_attr_in3_input.dev_attr.attr,
- &sensor_dev_attr_in4_input.dev_attr.attr,
- &sensor_dev_attr_in5_input.dev_attr.attr,
- &sensor_dev_attr_in6_input.dev_attr.attr,
- &sensor_dev_attr_in7_input.dev_attr.attr,
+static const struct hwmon_channel_info * const ads7871_info[] = {
+ HWMON_CHANNEL_INFO(in,
+ HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT,
+ HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT, HWMON_I_INPUT),
NULL
};
-ATTRIBUTE_GROUPS(ads7871);
+static const struct hwmon_ops ads7871_hwmon_ops = {
+ .is_visible = ads7871_is_visible,
+ .read = ads7871_read,
+};
+
+static const struct hwmon_chip_info ads7871_chip_info = {
+ .ops = &ads7871_hwmon_ops,
+ .info = ads7871_info,
+};
static int ads7871_probe(struct spi_device *spi)
{
@@ -205,9 +207,10 @@ static int ads7871_probe(struct spi_device *spi)
pdata->spi = spi;
mutex_init(&pdata->lock);
- hwmon_dev = devm_hwmon_device_register_with_groups(dev, spi->modalias,
- pdata,
- ads7871_groups);
+ hwmon_dev = devm_hwmon_device_register_with_info(dev, spi->modalias,
+ pdata,
+ &ads7871_chip_info,
+ NULL);
return PTR_ERR_OR_ZERO(hwmon_dev);
}
--
2.43.0
prev parent reply other threads:[~2026-03-28 18:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-28 18:20 [PATCH 0/2] hwmon: (ads7871) Fix concurrency and modernize API Tabrez Ahmed
2026-03-28 18:20 ` [PATCH 1/2] hwmon: (ads7871) Add mutex to serialize SPI transactions Tabrez Ahmed
2026-03-29 3:16 ` Guenter Roeck
2026-03-29 5:00 ` Tabrez Ahmed
2026-03-28 18:20 ` Tabrez Ahmed [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=20260328182015.220298-3-tabreztalks@gmail.com \
--to=tabreztalks@gmail.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=me@brighamcampbell.com \
--cc=shuah@kernel.org \
/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.