public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] hwmon: (ads7871) Modernize and fix DMA safety
@ 2026-03-29  7:33 Tabrez Ahmed
  2026-03-29  7:33 ` [PATCH v2 1/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info Tabrez Ahmed
  2026-03-29  7:33 ` [PATCH v2 2/2] hwmon: (ads7871) Use DMA-safe buffer for SPI writes Tabrez Ahmed
  0 siblings, 2 replies; 5+ messages in thread
From: Tabrez Ahmed @ 2026-03-29  7:33 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel, shuah, me, Tabrez Ahmed

This series modernizes the ads7871 driver by migrating it to the
hwmon_device_register_with_info API and fixes a DMA safety issue.

v2:

    Dropped custom mutex in favor of native hwmon core serialization.

    Split API migration and DMA fix into separate, logical patches.

    Corrected output scaling and sign extension to meet hwmon standards.

Compile-tested only due to lack of hardware

Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>

Tabrez Ahmed (2):
  hwmon: (ads7871) Convert to hwmon_device_register_with_info
  hwmon: (ads7871) Use DMA-safe buffer for SPI writes

 drivers/hwmon/ads7871.c | 110 ++++++++++++++++++++++------------------
 1 file changed, 62 insertions(+), 48 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info
  2026-03-29  7:33 [PATCH v2 0/2] hwmon: (ads7871) Modernize and fix DMA safety Tabrez Ahmed
@ 2026-03-29  7:33 ` Tabrez Ahmed
  2026-03-29 15:01   ` Guenter Roeck
  2026-03-29  7:33 ` [PATCH v2 2/2] hwmon: (ads7871) Use DMA-safe buffer for SPI writes Tabrez Ahmed
  1 sibling, 1 reply; 5+ messages in thread
From: Tabrez Ahmed @ 2026-03-29  7:33 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel, shuah, me, Tabrez Ahmed

Convert the ads7871 driver from the legacy hwmon_device_register() to the
modern hwmon_device_register_with_info() API. This migration simplifies
the driver by using the structured hwmon_channel_info approach and
prepares the codebase for the transition to a shared DMA-safe buffer.

Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
---
 drivers/hwmon/ads7871.c | 76 ++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 35 deletions(-)

diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
index 9bfdf9e6bcd77..41a1e9bbd4050 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>
 
@@ -65,10 +64,20 @@
 struct ads7871_data {
 	struct spi_device *spi;
 };
+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)
 {
 	int ret;
+
 	reg = reg | INST_READ_BM;
 	ret = spi_w8r8(spi, reg);
 	return ret;
@@ -77,6 +86,7 @@ static int ads7871_read_reg8(struct spi_device *spi, int reg)
 static int ads7871_read_reg16(struct spi_device *spi, int reg)
 {
 	int ret;
+
 	reg = reg | INST_READ_BM | INST_16BIT_BM;
 	ret = spi_w8r16(spi, reg);
 	return ret;
@@ -85,19 +95,20 @@ static int ads7871_read_reg16(struct spi_device *spi, int reg)
 static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
 {
 	u8 tmp[2] = {reg, 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;
 	/*
 	 * TODO: add support for conversions
 	 * other than single ended with a gain of 1
@@ -127,39 +138,34 @@ 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)
-			return val;
+		raw_val = ads7871_read_reg16(spi, REG_LS_BYTE);
+		if (raw_val < 0)
+			return raw_val;
+
 		/*result in volts*10000 = (val/8192)*2.5*10000*/
-		val = ((val >> 2) * 25000) / 8192;
-		return sysfs_emit(buf, "%d\n", val);
+		*val = ((raw_val >> 2) * 25000) / 8192;
+		return 0;
 	}
 
 	return -ETIMEDOUT;
 }
 
-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)
 {
@@ -194,10 +200,10 @@ static int ads7871_probe(struct spi_device *spi)
 		return -ENOMEM;
 
 	pdata->spi = spi;
-
-	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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] hwmon: (ads7871) Use DMA-safe buffer for SPI writes
  2026-03-29  7:33 [PATCH v2 0/2] hwmon: (ads7871) Modernize and fix DMA safety Tabrez Ahmed
  2026-03-29  7:33 ` [PATCH v2 1/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info Tabrez Ahmed
@ 2026-03-29  7:33 ` Tabrez Ahmed
  2026-03-29 14:52   ` Guenter Roeck
  1 sibling, 1 reply; 5+ messages in thread
From: Tabrez Ahmed @ 2026-03-29  7:33 UTC (permalink / raw)
  To: linux; +Cc: linux-hwmon, linux-kernel, shuah, me, Tabrez Ahmed

The driver currently passes a stack-allocated buffer to spi_write(),
which is incompatible with DMA on systems with CONFIG_VMAP_STACK
enabled.

Move the transfer buffer into the driver's private data structure
to ensure it is DMA-safe. Since this shared buffer now requires
serialization, this change depends on the previous commit which
migrated the driver to the hwmon 'with_info' API.

While moving the logic, also:
- Corrected the sign extension for 14-bit data by casting to s16.
- Scaled the output to millivolts (2500mV full scale
) to comply with the hwmon ABI.

Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
---
 drivers/hwmon/ads7871.c | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
index 41a1e9bbd4050..cd58c960690f1 100644
--- a/drivers/hwmon/ads7871.c
+++ b/drivers/hwmon/ads7871.c
@@ -63,6 +63,7 @@
 
 struct ads7871_data {
 	struct spi_device *spi;
+	u8 tx_buf[2] ____cacheline_aligned;
 };
 static umode_t ads7871_is_visible(const void *data,
 				  enum hwmon_sensor_types type,
@@ -78,6 +79,7 @@ static int ads7871_read_reg8(struct spi_device *spi, int reg)
 {
 	int ret;
 
+
 	reg = reg | INST_READ_BM;
 	ret = spi_w8r8(spi, reg);
 	return ret;
@@ -92,11 +94,12 @@ static int ads7871_read_reg16(struct spi_device *spi, int reg)
 	return ret;
 }
 
-static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
+static int ads7871_write_reg8(struct ads7871_data *pdata, int reg, u8 val)
 {
-	u8 tmp[2] = {reg, val};
+	pdata->tx_buf[0] = reg;
+	pdata->tx_buf[1] = val;
 
-	return spi_write(spi, tmp, sizeof(tmp));
+	return spi_write(pdata->spi, pdata->tx_buf, 2);
 }
 
 static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
@@ -115,7 +118,7 @@ static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
 	 */
 	/*MUX_M3_BM forces single ended*/
 	/*This is also where the gain of the PGA would be set*/
-	ret = ads7871_write_reg8(spi, REG_GAIN_MUX,
+	ret = ads7871_write_reg8(pdata, REG_GAIN_MUX,
 				 (MUX_CNV_BM | MUX_M3_BM | channel));
 	if (ret < 0)
 		return ret;
@@ -123,6 +126,7 @@ static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
 	ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
 	if (ret < 0)
 		return ret;
+
 	mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
 	/*
 	 * on 400MHz arm9 platform the conversion
@@ -142,8 +146,11 @@ static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
 		if (raw_val < 0)
 			return raw_val;
 
-		/*result in volts*10000 = (val/8192)*2.5*10000*/
-		*val = ((raw_val >> 2) * 25000) / 8192;
+		/*
+		 * Use (s16) to ensure the sign bit is preserved during the shift.
+		 * Report millivolts (2.5V = 2500mV).
+		 */
+		*val = ((s16)raw_val >> 2) * 2500 / 8192;
 		return 0;
 	}
 
@@ -180,11 +187,17 @@ static int ads7871_probe(struct spi_device *spi)
 	spi->bits_per_word = 8;
 	spi_setup(spi);
 
-	ads7871_write_reg8(spi, REG_SER_CONTROL, 0);
-	ads7871_write_reg8(spi, REG_AD_CONTROL, 0);
+	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return -ENOMEM;
+
+	pdata->spi = spi;
+
+	ads7871_write_reg8(pdata, REG_SER_CONTROL, 0);
+	ads7871_write_reg8(pdata, REG_AD_CONTROL, 0);
 
 	val = (OSC_OSCR_BM | OSC_OSCE_BM | OSC_REFE_BM | OSC_BUFE_BM);
-	ads7871_write_reg8(spi, REG_OSC_CONTROL, val);
+	ads7871_write_reg8(pdata, REG_OSC_CONTROL, val);
 	ret = ads7871_read_reg8(spi, REG_OSC_CONTROL);
 
 	dev_dbg(dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret);
@@ -195,11 +208,6 @@ static int ads7871_probe(struct spi_device *spi)
 	if (val != ret)
 		return -ENODEV;
 
-	pdata = devm_kzalloc(dev, sizeof(struct ads7871_data), GFP_KERNEL);
-	if (!pdata)
-		return -ENOMEM;
-
-	pdata->spi = spi;
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, spi->modalias,
 							 pdata,
 							 &ads7871_chip_info,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] hwmon: (ads7871) Use DMA-safe buffer for SPI writes
  2026-03-29  7:33 ` [PATCH v2 2/2] hwmon: (ads7871) Use DMA-safe buffer for SPI writes Tabrez Ahmed
@ 2026-03-29 14:52   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2026-03-29 14:52 UTC (permalink / raw)
  To: Tabrez Ahmed; +Cc: linux-hwmon, linux-kernel, shuah, me

On 3/29/26 00:33, Tabrez Ahmed wrote:
> The driver currently passes a stack-allocated buffer to spi_write(),
> which is incompatible with DMA on systems with CONFIG_VMAP_STACK
> enabled.
> 
> Move the transfer buffer into the driver's private data structure
> to ensure it is DMA-safe. Since this shared buffer now requires
> serialization, this change depends on the previous commit which
> migrated the driver to the hwmon 'with_info' API.
> 
> While moving the logic, also:
> - Corrected the sign extension for 14-bit data by casting to s16.
> - Scaled the output to millivolts (2500mV full scale
> ) to comply with the hwmon ABI.
> 
> Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
> ---
>   drivers/hwmon/ads7871.c | 36 ++++++++++++++++++++++--------------
>   1 file changed, 22 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
> index 41a1e9bbd4050..cd58c960690f1 100644
> --- a/drivers/hwmon/ads7871.c
> +++ b/drivers/hwmon/ads7871.c
> @@ -63,6 +63,7 @@
>   
>   struct ads7871_data {
>   	struct spi_device *spi;
> +	u8 tx_buf[2] ____cacheline_aligned;
>   };
>   static umode_t ads7871_is_visible(const void *data,
>   				  enum hwmon_sensor_types type,
> @@ -78,6 +79,7 @@ static int ads7871_read_reg8(struct spi_device *spi, int reg)
>   {
>   	int ret;
>   
> +

Please no whitespace changes. Also, please run checkpatch.pl --strict on your patches, since that
would have reported the resulting formatting violation:

CHECK: Please don't use multiple blank lines
#137: FILE: drivers/hwmon/ads7871.c:82:

+

>   	reg = reg | INST_READ_BM;
>   	ret = spi_w8r8(spi, reg);
>   	return ret;
> @@ -92,11 +94,12 @@ static int ads7871_read_reg16(struct spi_device *spi, int reg)
>   	return ret;
>   }
>   
> -static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
> +static int ads7871_write_reg8(struct ads7871_data *pdata, int reg, u8 val)
>   {
> -	u8 tmp[2] = {reg, val};
> +	pdata->tx_buf[0] = reg;
> +	pdata->tx_buf[1] = val;
>   
> -	return spi_write(spi, tmp, sizeof(tmp));
> +	return spi_write(pdata->spi, pdata->tx_buf, 2);
>   }
>   
>   static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
> @@ -115,7 +118,7 @@ static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
>   	 */
>   	/*MUX_M3_BM forces single ended*/
>   	/*This is also where the gain of the PGA would be set*/
> -	ret = ads7871_write_reg8(spi, REG_GAIN_MUX,
> +	ret = ads7871_write_reg8(pdata, REG_GAIN_MUX,
>   				 (MUX_CNV_BM | MUX_M3_BM | channel));
>   	if (ret < 0)
>   		return ret;
> @@ -123,6 +126,7 @@ static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
>   	ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
>   	if (ret < 0)
>   		return ret;
> +

Another whitespace change. Again, please refrain from making such changes.

Thanks,
Guenter

>   	mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
>   	/*
>   	 * on 400MHz arm9 platform the conversion
> @@ -142,8 +146,11 @@ static int ads7871_read(struct device *dev, enum hwmon_sensor_types type,
>   		if (raw_val < 0)
>   			return raw_val;
>   
> -		/*result in volts*10000 = (val/8192)*2.5*10000*/
> -		*val = ((raw_val >> 2) * 25000) / 8192;
> +		/*
> +		 * Use (s16) to ensure the sign bit is preserved during the shift.
> +		 * Report millivolts (2.5V = 2500mV).
> +		 */
> +		*val = ((s16)raw_val >> 2) * 2500 / 8192;
>   		return 0;
>   	}
>   
> @@ -180,11 +187,17 @@ static int ads7871_probe(struct spi_device *spi)
>   	spi->bits_per_word = 8;
>   	spi_setup(spi);
>   
> -	ads7871_write_reg8(spi, REG_SER_CONTROL, 0);
> -	ads7871_write_reg8(spi, REG_AD_CONTROL, 0);
> +	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
> +	if (!pdata)
> +		return -ENOMEM;
> +
> +	pdata->spi = spi;
> +
> +	ads7871_write_reg8(pdata, REG_SER_CONTROL, 0);
> +	ads7871_write_reg8(pdata, REG_AD_CONTROL, 0);
>   
>   	val = (OSC_OSCR_BM | OSC_OSCE_BM | OSC_REFE_BM | OSC_BUFE_BM);
> -	ads7871_write_reg8(spi, REG_OSC_CONTROL, val);
> +	ads7871_write_reg8(pdata, REG_OSC_CONTROL, val);
>   	ret = ads7871_read_reg8(spi, REG_OSC_CONTROL);
>   
>   	dev_dbg(dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret);
> @@ -195,11 +208,6 @@ static int ads7871_probe(struct spi_device *spi)
>   	if (val != ret)
>   		return -ENODEV;
>   
> -	pdata = devm_kzalloc(dev, sizeof(struct ads7871_data), GFP_KERNEL);
> -	if (!pdata)
> -		return -ENOMEM;
> -
> -	pdata->spi = spi;
>   	hwmon_dev = devm_hwmon_device_register_with_info(dev, spi->modalias,
>   							 pdata,
>   							 &ads7871_chip_info,


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info
  2026-03-29  7:33 ` [PATCH v2 1/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info Tabrez Ahmed
@ 2026-03-29 15:01   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2026-03-29 15:01 UTC (permalink / raw)
  To: Tabrez Ahmed; +Cc: linux-hwmon, linux-kernel, shuah, me

Hi,

On 3/29/26 00:33, Tabrez Ahmed wrote:
> Convert the ads7871 driver from the legacy hwmon_device_register() to the
> modern hwmon_device_register_with_info() API. This migration simplifies
> the driver by using the structured hwmon_channel_info approach and
> prepares the codebase for the transition to a shared DMA-safe buffer.
> 

Please add: "While at it, fix checkpatch violations" since you fixed the
"empty line after variable declarations" problem.

.. but please fix all of them. See inline.

Thanks,
Guenter

> Signed-off-by: Tabrez Ahmed <tabreztalks@gmail.com>
> ---
>   drivers/hwmon/ads7871.c | 76 ++++++++++++++++++++++-------------------
>   1 file changed, 41 insertions(+), 35 deletions(-)
> 
> diff --git a/drivers/hwmon/ads7871.c b/drivers/hwmon/ads7871.c
> index 9bfdf9e6bcd77..41a1e9bbd4050 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>
>   
> @@ -65,10 +64,20 @@
>   struct ads7871_data {
>   	struct spi_device *spi;
>   };

Add empty line (checkpatch reports that, plus a couple more formatting issues).

> +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)
>   {
>   	int ret;
> +
>   	reg = reg | INST_READ_BM;
>   	ret = spi_w8r8(spi, reg);
>   	return ret;
> @@ -77,6 +86,7 @@ static int ads7871_read_reg8(struct spi_device *spi, int reg)
>   static int ads7871_read_reg16(struct spi_device *spi, int reg)
>   {
>   	int ret;
> +
>   	reg = reg | INST_READ_BM | INST_16BIT_BM;
>   	ret = spi_w8r16(spi, reg);
>   	return ret;
> @@ -85,19 +95,20 @@ static int ads7871_read_reg16(struct spi_device *spi, int reg)
>   static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
>   {
>   	u8 tmp[2] = {reg, 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;

That doesn't hurt but isn't really needed here.

>   	/*
>   	 * TODO: add support for conversions
>   	 * other than single ended with a gain of 1
> @@ -127,39 +138,34 @@ 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)
> -			return val;
> +		raw_val = ads7871_read_reg16(spi, REG_LS_BYTE);
> +		if (raw_val < 0)
> +			return raw_val;
> +
>   		/*result in volts*10000 = (val/8192)*2.5*10000*/
> -		val = ((val >> 2) * 25000) / 8192;
> -		return sysfs_emit(buf, "%d\n", val);
> +		*val = ((raw_val >> 2) * 25000) / 8192;
> +		return 0;

Sashiko suggests to make the 16-bit operations endianness safe.

https://sashiko.dev/#/patchset/20260329073352.270451-1-tabreztalks%40gmail.com

Not mandatory, but just in case you have the time ...

>   	}
>   
>   	return -ETIMEDOUT;
>   }
>   
> -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)
>   {
> @@ -194,10 +200,10 @@ static int ads7871_probe(struct spi_device *spi)
>   		return -ENOMEM;
>   
>   	pdata->spi = spi;
> -
> -	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);
>   }
>   


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-29 15:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-29  7:33 [PATCH v2 0/2] hwmon: (ads7871) Modernize and fix DMA safety Tabrez Ahmed
2026-03-29  7:33 ` [PATCH v2 1/2] hwmon: (ads7871) Convert to hwmon_device_register_with_info Tabrez Ahmed
2026-03-29 15:01   ` Guenter Roeck
2026-03-29  7:33 ` [PATCH v2 2/2] hwmon: (ads7871) Use DMA-safe buffer for SPI writes Tabrez Ahmed
2026-03-29 14:52   ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox