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 1/2] iio: adc: mcp3422: describe parts with chip_info
Date: Sat,  3 Oct 2026 03:16:46 +0300	[thread overview]
Message-ID: <20261003001647.1975549-2-thefireking@gmail.com> (raw)
In-Reply-To: <20261003001647.1975549-1-thefireking@gmail.com>

The driver encodes MCP3421-8 differences as integer ids and switches
on them in probe. Point i2c_device_id.driver_data at a per-chip
structure instead, and keep the channel list and 3 SPS support there.

Compile tested. No hardware.

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

diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
index 36ba00edf..92825aaf2 100644
--- a/drivers/iio/adc/mcp3422.c
+++ b/drivers/iio/adc/mcp3422.c
@@ -76,10 +76,16 @@ static const int mcp3422_sign_extend[4] = {
 	[MCP3422_SRATE_15] = 15,
 	[MCP3422_SRATE_3] = 17 };
 
+struct mcp3422_chip_info {
+	const struct iio_chan_spec *channels;
+	unsigned int num_channels;
+	bool supports_3sps;
+};
+
 /* Client data (each client gets its own) */
 struct mcp3422 {
 	struct i2c_client *i2c;
-	u8 id;
+	const struct mcp3422_chip_info *chip_info;
 	u8 config;
 	u8 pga[4];
 	struct mutex lock;
@@ -223,7 +229,7 @@ static int mcp3422_write_raw(struct iio_dev *iio,
 			temp = MCP3422_SRATE_15;
 			break;
 		case 3:
-			if (adc->id > 4)
+			if (!adc->chip_info->supports_3sps)
 				return -EINVAL;
 			temp = MCP3422_SRATE_3;
 			break;
@@ -261,7 +267,7 @@ static ssize_t mcp3422_show_samp_freqs(struct device *dev,
 {
 	struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev));
 
-	if (adc->id > 4)
+	if (!adc->chip_info->supports_3sps)
 		return sprintf(buf, "240 60 15\n");
 
 	return sprintf(buf, "240 60 15 3\n");
@@ -311,6 +317,54 @@ static const struct iio_chan_spec mcp3424_channels[] = {
 	MCP3422_CHAN(3),
 };
 
+static const struct mcp3422_chip_info mcp3421_chip_info = {
+	.channels = mcp3421_channels,
+	.num_channels = ARRAY_SIZE(mcp3421_channels),
+	.supports_3sps = true,
+};
+
+static const struct mcp3422_chip_info mcp3422_chip_info = {
+	.channels = mcp3422_channels,
+	.num_channels = ARRAY_SIZE(mcp3422_channels),
+	.supports_3sps = true,
+};
+
+static const struct mcp3422_chip_info mcp3423_chip_info = {
+	.channels = mcp3422_channels,
+	.num_channels = ARRAY_SIZE(mcp3422_channels),
+	.supports_3sps = true,
+};
+
+static const struct mcp3422_chip_info mcp3424_chip_info = {
+	.channels = mcp3424_channels,
+	.num_channels = ARRAY_SIZE(mcp3424_channels),
+	.supports_3sps = true,
+};
+
+static const struct mcp3422_chip_info mcp3425_chip_info = {
+	.channels = mcp3421_channels,
+	.num_channels = ARRAY_SIZE(mcp3421_channels),
+	.supports_3sps = false,
+};
+
+static const struct mcp3422_chip_info mcp3426_chip_info = {
+	.channels = mcp3422_channels,
+	.num_channels = ARRAY_SIZE(mcp3422_channels),
+	.supports_3sps = false,
+};
+
+static const struct mcp3422_chip_info mcp3427_chip_info = {
+	.channels = mcp3422_channels,
+	.num_channels = ARRAY_SIZE(mcp3422_channels),
+	.supports_3sps = false,
+};
+
+static const struct mcp3422_chip_info mcp3428_chip_info = {
+	.channels = mcp3424_channels,
+	.num_channels = ARRAY_SIZE(mcp3424_channels),
+	.supports_3sps = false,
+};
+
 static const struct iio_info mcp3422_info = {
 	.read_raw = mcp3422_read_raw,
 	.write_raw = mcp3422_write_raw,
@@ -320,7 +374,6 @@ static const struct iio_info mcp3422_info = {
 
 static int mcp3422_probe(struct i2c_client *client)
 {
-	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	struct iio_dev *indio_dev;
 	struct mcp3422 *adc;
 	int err;
@@ -335,33 +388,17 @@ static int mcp3422_probe(struct i2c_client *client)
 
 	adc = iio_priv(indio_dev);
 	adc->i2c = client;
-	adc->id = (u8)(id->driver_data);
+	adc->chip_info = i2c_get_match_data(client);
+	if (!adc->chip_info)
+		return -ENODEV;
 
 	mutex_init(&adc->lock);
 
 	indio_dev->name = dev_name(&client->dev);
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->info = &mcp3422_info;
-
-	switch (adc->id) {
-	case 1:
-	case 5:
-		indio_dev->channels = mcp3421_channels;
-		indio_dev->num_channels = ARRAY_SIZE(mcp3421_channels);
-		break;
-	case 2:
-	case 3:
-	case 6:
-	case 7:
-		indio_dev->channels = mcp3422_channels;
-		indio_dev->num_channels = ARRAY_SIZE(mcp3422_channels);
-		break;
-	case 4:
-	case 8:
-		indio_dev->channels = mcp3424_channels;
-		indio_dev->num_channels = ARRAY_SIZE(mcp3424_channels);
-		break;
-	}
+	indio_dev->channels = adc->chip_info->channels;
+	indio_dev->num_channels = adc->chip_info->num_channels;
 
 	/* meaningful default configuration */
 	config = MCP3422_CONT_SAMPLING |
@@ -382,14 +419,14 @@ static int mcp3422_probe(struct i2c_client *client)
 }
 
 static const struct i2c_device_id mcp3422_id[] = {
-	{ .name = "mcp3421", .driver_data = 1 },
-	{ .name = "mcp3422", .driver_data = 2 },
-	{ .name = "mcp3423", .driver_data = 3 },
-	{ .name = "mcp3424", .driver_data = 4 },
-	{ .name = "mcp3425", .driver_data = 5 },
-	{ .name = "mcp3426", .driver_data = 6 },
-	{ .name = "mcp3427", .driver_data = 7 },
-	{ .name = "mcp3428", .driver_data = 8 },
+	{ .name = "mcp3421", .driver_data = (kernel_ulong_t)&mcp3421_chip_info },
+	{ .name = "mcp3422", .driver_data = (kernel_ulong_t)&mcp3422_chip_info },
+	{ .name = "mcp3423", .driver_data = (kernel_ulong_t)&mcp3423_chip_info },
+	{ .name = "mcp3424", .driver_data = (kernel_ulong_t)&mcp3424_chip_info },
+	{ .name = "mcp3425", .driver_data = (kernel_ulong_t)&mcp3425_chip_info },
+	{ .name = "mcp3426", .driver_data = (kernel_ulong_t)&mcp3426_chip_info },
+	{ .name = "mcp3427", .driver_data = (kernel_ulong_t)&mcp3427_chip_info },
+	{ .name = "mcp3428", .driver_data = (kernel_ulong_t)&mcp3428_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, mcp3422_id);
-- 
2.43.0


  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   ` Yuval Saar [this message]
2026-10-03 20:37     ` [PATCH v3 1/2] iio: adc: mcp3422: describe parts with chip_info Andy Shevchenko
2026-10-04 16:38     ` Jonathan Cameron
2026-10-03  0:16   ` [PATCH v3 2/2] iio: adc: mcp3422: use read_avail() for available attributes Yuval Saar
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-2-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