[PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes

Gabriel Rondon posted 1 patch 1 week, 1 day ago
There is a newer version of this series
drivers/iio/adc/ti-ads8688.c | 70 +++++++++++++++++++-----------------
1 file changed, 36 insertions(+), 34 deletions(-)
[PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
Posted by Gabriel Rondon 1 week, 1 day ago
Convert the in_voltage_scale_available and in_voltage_offset_available
attributes from legacy IIO_DEVICE_ATTR with custom show functions to the
IIO framework's read_avail callback. This uses the framework's built-in
support for _available attributes, removing the need for manual sysfs
formatting.

Precompute the available scale values at probe time since they depend on
the reference voltage which does not change after initialization.

Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Signed-off-by: Gabriel Rondon <grondon@gmail.com>
---
Changes in v2:
- Use -BIT() macro instead of -(1 << ...) to avoid potential UB and
  improve readability (Andy Shevchenko)
- Remove trailing comma from offset_avail array (Andy Shevchenko)
- Declare loop counter as unsigned int in for() (Andy Shevchenko, Nuno Sá)
- Keep scale_avail assignment on a single line (Andy Shevchenko, Nuno Sá)

 drivers/iio/adc/ti-ads8688.c | 70 +++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 34 deletions(-)

diff --git a/drivers/iio/adc/ti-ads8688.c b/drivers/iio/adc/ti-ads8688.c
index b0bf46cae..31c43a4cb 100644
--- a/drivers/iio/adc/ti-ads8688.c
+++ b/drivers/iio/adc/ti-ads8688.c
@@ -6,7 +6,6 @@
 #include <linux/device.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
-#include <linux/sysfs.h>
 #include <linux/spi/spi.h>
 #include <linux/regulator/consumer.h>
 #include <linux/err.h>
@@ -17,7 +16,6 @@
 #include <linux/iio/buffer.h>
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/triggered_buffer.h>
-#include <linux/iio/sysfs.h>
 
 #define ADS8688_CMD_REG(x)		(x << 8)
 #define ADS8688_CMD_REG_NOOP		0x00
@@ -66,6 +64,7 @@ struct ads8688_state {
 	const struct ads8688_chip_info	*chip_info;
 	struct spi_device		*spi;
 	unsigned int			vref_mv;
+	int				scale_avail[3][2];
 	enum ads8688_range		range[8];
 	union {
 		__be32 d32;
@@ -114,37 +113,9 @@ static const struct ads8688_ranges ads8688_range_def[5] = {
 	}
 };
 
-static ssize_t ads8688_show_scales(struct device *dev,
-				   struct device_attribute *attr, char *buf)
-{
-	struct ads8688_state *st = iio_priv(dev_to_iio_dev(dev));
-
-	return sprintf(buf, "0.%09u 0.%09u 0.%09u\n",
-		       ads8688_range_def[0].scale * st->vref_mv,
-		       ads8688_range_def[1].scale * st->vref_mv,
-		       ads8688_range_def[2].scale * st->vref_mv);
-}
-
-static ssize_t ads8688_show_offsets(struct device *dev,
-				    struct device_attribute *attr, char *buf)
-{
-	return sprintf(buf, "%d %d\n", ads8688_range_def[0].offset,
-		       ads8688_range_def[3].offset);
-}
-
-static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
-		       ads8688_show_scales, NULL, 0);
-static IIO_DEVICE_ATTR(in_voltage_offset_available, S_IRUGO,
-		       ads8688_show_offsets, NULL, 0);
-
-static struct attribute *ads8688_attributes[] = {
-	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
-	&iio_dev_attr_in_voltage_offset_available.dev_attr.attr,
-	NULL,
-};
-
-static const struct attribute_group ads8688_attribute_group = {
-	.attrs = ads8688_attributes,
+static const int ads8688_offset_avail[] = {
+	-BIT(ADS8688_REALBITS - 1),
+	0
 };
 
 #define ADS8688_CHAN(index)					\
@@ -155,6 +126,9 @@ static const struct attribute_group ads8688_attribute_group = {
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)		\
 			      | BIT(IIO_CHAN_INFO_SCALE)	\
 			      | BIT(IIO_CHAN_INFO_OFFSET),	\
+	.info_mask_shared_by_type_available =			\
+			      BIT(IIO_CHAN_INFO_SCALE)		\
+			      | BIT(IIO_CHAN_INFO_OFFSET),	\
 	.scan_index = index,					\
 	.scan_type = {						\
 		.sign = 'u',					\
@@ -369,11 +343,34 @@ static int ads8688_write_raw_get_fmt(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
+static int ads8688_read_avail(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      const int **vals, int *type, int *length,
+			      long mask)
+{
+	struct ads8688_state *st = iio_priv(indio_dev);
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		*vals = (const int *)st->scale_avail;
+		*type = IIO_VAL_INT_PLUS_NANO;
+		*length = ARRAY_SIZE(st->scale_avail) * 2;
+		return IIO_AVAIL_LIST;
+	case IIO_CHAN_INFO_OFFSET:
+		*vals = ads8688_offset_avail;
+		*type = IIO_VAL_INT;
+		*length = ARRAY_SIZE(ads8688_offset_avail);
+		return IIO_AVAIL_LIST;
+	default:
+		return -EINVAL;
+	}
+}
+
 static const struct iio_info ads8688_info = {
 	.read_raw = &ads8688_read_raw,
+	.read_avail = &ads8688_read_avail,
 	.write_raw = &ads8688_write_raw,
 	.write_raw_get_fmt = &ads8688_write_raw_get_fmt,
-	.attrs = &ads8688_attribute_group,
 };
 
 static irqreturn_t ads8688_trigger_handler(int irq, void *p)
@@ -426,6 +423,11 @@ static int ads8688_probe(struct spi_device *spi)
 
 	st->vref_mv = ret == -ENODEV ? ADS8688_VREF_MV : ret / 1000;
 
+	for (unsigned int i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
+		st->scale_avail[i][0] = 0;
+		st->scale_avail[i][1] = ads8688_range_def[i].scale * st->vref_mv;
+	}
+
 	st->chip_info =	&ads8688_chip_info_tbl[spi_get_device_id(spi)->driver_data];
 
 	spi->mode = SPI_MODE_1;
-- 
2.33.0
Re: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
Posted by Andy Shevchenko 1 week ago
On Wed, Mar 25, 2026 at 11:09:42AM +0000, Gabriel Rondon wrote:
> Convert the in_voltage_scale_available and in_voltage_offset_available
> attributes from legacy IIO_DEVICE_ATTR with custom show functions to the

IIO_DEVICE_ATTR()

> IIO framework's read_avail callback. This uses the framework's built-in

.read_avail()

> support for _available attributes, removing the need for manual sysfs
> formatting.
> 
> Precompute the available scale values at probe time since they depend on
> the reference voltage which does not change after initialization.

...

> Changes in v2:
> - Use -BIT() macro instead of -(1 << ...) to avoid potential UB and
>   improve readability (Andy Shevchenko)

Not really (and LKP showed that), I suggested to use GENMASK().

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
Posted by Andy Shevchenko 1 week ago
On Thu, Mar 26, 2026 at 10:55:06AM +0200, Andy Shevchenko wrote:
> On Wed, Mar 25, 2026 at 11:09:42AM +0000, Gabriel Rondon wrote:

...

> > Changes in v2:
> > - Use -BIT() macro instead of -(1 << ...) to avoid potential UB and
> >   improve readability (Andy Shevchenko)
> 
> Not really (and LKP showed that), I suggested to use GENMASK().

But it will still cut the value, so you most likely would like to have
GENMASK_U16() or alike.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
Posted by grondon@gmail.com 3 days, 5 hours ago
From: Gabriel Rondon <grondon@gmail.com>

On Thu, 26 Mar 2026 10:57:44 +0200, Andy Shevchenko wrote:
> But it will still cut the value, so you most likely would like to have
> GENMASK_U16() or alike.

Jonathan suggested keeping the original expression -(1 << (ADS8688_REALBITS - 1))
to match the existing table, since -BIT() caused overflow warnings from the
kernel test robot. Will send v3 with the original expression.

Thanks,
Gabriel
Re: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
Posted by kernel test robot 1 week, 1 day ago
Hi Gabriel,

kernel test robot noticed the following build warnings:

[auto build test WARNING on jic23-iio/togreg]
[also build test WARNING on linus/master v7.0-rc5 next-20260325]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Gabriel-Rondon/iio-adc-ti-ads8688-use-read_avail-for-available-attributes/20260325-195933
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20260325110942.65337-1-grondon%40gmail.com
patch subject: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20260326/202603260401.2g0RDxuL-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260326/202603260401.2g0RDxuL-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202603260401.2g0RDxuL-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/iio/adc/ti-ads8688.c:117:2: warning: implicit conversion from 'unsigned long' to 'int' changes value from 18446744073709518848 to -32768 [-Wconstant-conversion]
     116 | static const int ads8688_offset_avail[] = {
         |                                           ~
     117 |         -BIT(ADS8688_REALBITS - 1),
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~
   1 warning generated.


vim +117 drivers/iio/adc/ti-ads8688.c

   115	
   116	static const int ads8688_offset_avail[] = {
 > 117		-BIT(ADS8688_REALBITS - 1),
   118		0
   119	};
   120	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
Posted by kernel test robot 1 week, 1 day ago
Hi Gabriel,

kernel test robot noticed the following build warnings:

[auto build test WARNING on jic23-iio/togreg]
[also build test WARNING on linus/master v7.0-rc5 next-20260325]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Gabriel-Rondon/iio-adc-ti-ads8688-use-read_avail-for-available-attributes/20260325-195933
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20260325110942.65337-1-grondon%40gmail.com
patch subject: [PATCH v2] iio: adc: ti-ads8688: use read_avail for available attributes
config: riscv-randconfig-001-20260326 (https://download.01.org/0day-ci/archive/20260326/202603260345.PUPcPFND-lkp@intel.com/config)
compiler: riscv64-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260326/202603260345.PUPcPFND-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202603260345.PUPcPFND-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> drivers/iio/adc/ti-ads8688.c:117:2: warning: overflow in conversion from 'long unsigned int' to 'int' changes value from '18446744073709518848' to '-32768' [-Woverflow]
     117 |  -BIT(ADS8688_REALBITS - 1),
         |  ^


vim +117 drivers/iio/adc/ti-ads8688.c

   115	
   116	static const int ads8688_offset_avail[] = {
 > 117		-BIT(ADS8688_REALBITS - 1),
   118		0
   119	};
   120	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki