Linux IIO development
 help / color / mirror / Atom feed
From: Yuval Saar <thefireking@gmail.com>
To: linux-iio@vger.kernel.org
Cc: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	andy@kernel.org, maxwell@maxwelld.cc,
	Yuval Saar <thefireking@gmail.com>
Subject: [PATCH v3 2/2] iio: adc: mcp3422: use read_avail() for available attributes
Date: Sat,  3 Oct 2026 03:16:47 +0300	[thread overview]
Message-ID: <20261003001647.1975549-3-thefireking@gmail.com> (raw)
In-Reply-To: <20261003001647.1975549-1-thefireking@gmail.com>

Custom sysfs show functions only expose the sampling frequency and
scale lists to userspace. Use read_avail() so the IIO core creates
those attributes and in-kernel consumers can query the lists.

sampling_frequency_available is device-wide, so mark it shared by all.
The scale list is shared by type, which keeps the existing
in_voltage_scale_available attribute name.

Store each scale as an { integer, nano } pair and cast the table for
read_avail().

Compile tested. checkpatch --strict clean. Available-list text checked
against IIO core formatters. No hardware.

Assisted-by: LLM
Signed-off-by: Yuval Saar <thefireking@gmail.com>
---
 drivers/iio/adc/mcp3422.c | 90 +++++++++++++++++++--------------------
 1 file changed, 45 insertions(+), 45 deletions(-)

diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
index 92825aaf2..39599dfbc 100644
--- a/drivers/iio/adc/mcp3422.c
+++ b/drivers/iio/adc/mcp3422.c
@@ -19,11 +19,9 @@
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/delay.h>
-#include <linux/sysfs.h>
 #include <linux/unaligned.h>
 
 #include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
 
 #define MCP3422_CHANNEL_MASK	GENMASK(6, 5)
 #define MCP3422_SRATE_MASK	GENMASK(3, 2)
@@ -47,13 +45,27 @@
 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
 				| BIT(IIO_CHAN_INFO_SCALE), \
 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
+		.info_mask_shared_by_type_available = \
+				BIT(IIO_CHAN_INFO_SCALE), \
+		.info_mask_shared_by_all_available = \
+				BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 	}
 
-static const int mcp3422_scales[4][4] = {
-	{ 1000000, 500000, 250000, 125000 },
-	{ 250000,  125000, 62500,  31250  },
-	{ 62500,   31250,  15625,  7812   },
-	{ 15625,   7812,   3906,   1953   } };
+/* { integer, nano } per PGA gain, for the current sample rate. */
+static const int mcp3422_scales[][4][2] = {
+	[MCP3422_SRATE_240] = {
+		{ 0, 1000000 }, { 0, 500000 }, { 0, 250000 }, { 0, 125000 },
+	},
+	[MCP3422_SRATE_60] = {
+		{ 0, 250000 }, { 0, 125000 }, { 0, 62500 }, { 0, 31250 },
+	},
+	[MCP3422_SRATE_15] = {
+		{ 0, 62500 }, { 0, 31250 }, { 0, 15625 }, { 0, 7812 },
+	},
+	[MCP3422_SRATE_3] = {
+		{ 0, 15625 }, { 0, 7812 }, { 0, 3906 }, { 0, 1953 },
+	},
+};
 
 /* Constant msleep times for data acquisitions */
 static const int mcp3422_read_times[4] = {
@@ -174,8 +186,8 @@ static int mcp3422_read_raw(struct iio_dev *iio,
 
 	case IIO_CHAN_INFO_SCALE:
 
-		*val1 = 0;
-		*val2 = mcp3422_scales[sample_rate][pga];
+		*val1 = mcp3422_scales[sample_rate][pga][0];
+		*val2 = mcp3422_scales[sample_rate][pga][1];
 		return IIO_VAL_INT_PLUS_NANO;
 
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -206,7 +218,8 @@ static int mcp3422_write_raw(struct iio_dev *iio,
 			return -EINVAL;
 
 		for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) {
-			if (val2 == mcp3422_scales[sample_rate][i]) {
+			if (val1 == mcp3422_scales[sample_rate][i][0] &&
+			    val2 == mcp3422_scales[sample_rate][i][1]) {
 				adc->pga[req_channel] = i;
 
 				FIELD_MODIFY(MCP3422_CHANNEL_MASK, &config, req_channel);
@@ -262,45 +275,32 @@ static int mcp3422_write_raw_get_fmt(struct iio_dev *indio_dev,
 	}
 }
 
-static ssize_t mcp3422_show_samp_freqs(struct device *dev,
-		struct device_attribute *attr, char *buf)
+static int mcp3422_read_avail(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      const int **vals, int *type, int *length,
+			      long mask)
 {
-	struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
-
-	if (!adc->chip_info->supports_3sps)
-		return sprintf(buf, "240 60 15\n");
-
-	return sprintf(buf, "240 60 15 3\n");
-}
-
-static ssize_t mcp3422_show_scales(struct device *dev,
-		struct device_attribute *attr, char *buf)
-{
-	struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
+	struct mcp3422 *adc = iio_priv(indio_dev);
 	u8 sample_rate = FIELD_GET(MCP3422_SRATE_MASK, adc->config);
 
-	return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n",
-		mcp3422_scales[sample_rate][0],
-		mcp3422_scales[sample_rate][1],
-		mcp3422_scales[sample_rate][2],
-		mcp3422_scales[sample_rate][3]);
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		*vals = (const int *)mcp3422_scales[sample_rate];
+		*type = IIO_VAL_INT_PLUS_NANO;
+		*length = ARRAY_SIZE(mcp3422_scales[0]) * 2;
+		return IIO_AVAIL_LIST;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*vals = mcp3422_sample_rates;
+		*type = IIO_VAL_INT;
+		*length = ARRAY_SIZE(mcp3422_sample_rates);
+		if (!adc->chip_info->supports_3sps)
+			*length -= 1;
+		return IIO_AVAIL_LIST;
+	default:
+		return -EINVAL;
+	}
 }
 
-static IIO_DEVICE_ATTR(sampling_frequency_available, S_IRUGO,
-		mcp3422_show_samp_freqs, NULL, 0);
-static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
-		mcp3422_show_scales, NULL, 0);
-
-static struct attribute *mcp3422_attributes[] = {
-	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
-	&iio_dev_attr_in_voltage_scale_available.dev_attr.attr,
-	NULL,
-};
-
-static const struct attribute_group mcp3422_attribute_group = {
-	.attrs = mcp3422_attributes,
-};
-
 static const struct iio_chan_spec mcp3421_channels[] = {
 	MCP3422_CHAN(0),
 };
@@ -367,9 +367,9 @@ static const struct mcp3422_chip_info mcp3428_chip_info = {
 
 static const struct iio_info mcp3422_info = {
 	.read_raw = mcp3422_read_raw,
+	.read_avail = mcp3422_read_avail,
 	.write_raw = mcp3422_write_raw,
 	.write_raw_get_fmt = mcp3422_write_raw_get_fmt,
-	.attrs = &mcp3422_attribute_group,
 };
 
 static int mcp3422_probe(struct i2c_client *client)
-- 
2.43.0


  parent reply	other threads:[~2026-10-03  0:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-19 19:55 [PATCH] iio: adc: mcp3422: use sysfs_emit() in show functions Angel2Eyes
2026-09-19 22:33 ` Maxwell Doose
2026-09-19 22:45   ` Joshua Crofts
2026-09-20  0:30 ` Jonathan Cameron
2026-09-20 16:00 ` [PATCH v2] iio: adc: mcp3422: use read_avail() for available attributes Yuval Saar
2026-09-21  3:47   ` Jonathan Cameron
2026-09-25 16:50     ` Andy Shevchenko
2026-10-03  0:16 ` [PATCH v3 0/2] iio: adc: mcp3422: chip info and read_avail() Yuval Saar
2026-10-03  0:16   ` [PATCH v3 1/2] iio: adc: mcp3422: describe parts with chip_info Yuval Saar
2026-10-03 20:37     ` Andy Shevchenko
2026-10-04 16:38     ` Jonathan Cameron
2026-10-03  0:16   ` Yuval Saar [this message]
2026-10-03 13:40   ` [PATCH v3 0/2] iio: adc: mcp3422: chip info and read_avail() Joshua Crofts

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=20261003001647.1975549-3-thefireking@gmail.com \
    --to=thefireking@gmail.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=maxwell@maxwelld.cc \
    --cc=nuno.sa@analog.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