[PATCH v5 06/13] iio: dac: ds4424: use device match data for chip info

Oleksij Rempel posted 13 patches 5 days, 6 hours ago
[PATCH v5 06/13] iio: dac: ds4424: use device match data for chip info
Posted by Oleksij Rempel 5 days, 6 hours ago
Refactor the driver to use device match data instead of checking ID enums
in a switch statement.

Define a `ds4424_chip_info` structure to hold variant-specific attributes
(currently just the channel count) and attach it directly to the I2C and
OF device ID tables.

This simplifies the probe function and makes it easier to add support for
new variants like DS4402/DS4404.

Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
changes v5:
- drop client->name change
changes v4:
- New patch
---
 drivers/iio/dac/ds4424.c | 41 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/drivers/iio/dac/ds4424.c b/drivers/iio/dac/ds4424.c
index 3385f39521d9..b6b8ab2d4b1f 100644
--- a/drivers/iio/dac/ds4424.c
+++ b/drivers/iio/dac/ds4424.c
@@ -34,9 +34,16 @@
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
 }
 
-enum ds4424_device_ids {
-	ID_DS4422,
-	ID_DS4424,
+struct ds4424_chip_info {
+	u8 num_channels;
+};
+
+static const struct ds4424_chip_info ds4422_info = {
+	.num_channels = DS4422_MAX_DAC_CHANNELS,
+};
+
+static const struct ds4424_chip_info ds4424_info = {
+	.num_channels = DS4424_MAX_DAC_CHANNELS,
 };
 
 struct ds4424_data {
@@ -205,10 +212,15 @@ static const struct iio_info ds4424_iio_info = {
 static int ds4424_probe(struct i2c_client *client)
 {
 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
+	const struct ds4424_chip_info *chip_info;
 	struct ds4424_data *data;
 	struct iio_dev *indio_dev;
 	int ret;
 
+	chip_info = i2c_get_match_data(client);
+	if (!chip_info)
+		return -ENODEV;
+
 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 	if (!indio_dev)
 		return -ENOMEM;
@@ -236,20 +248,7 @@ static int ds4424_probe(struct i2c_client *client)
 	if (ret < 0)
 		goto fail;
 
-	switch (id->driver_data) {
-	case ID_DS4422:
-		indio_dev->num_channels = DS4422_MAX_DAC_CHANNELS;
-		break;
-	case ID_DS4424:
-		indio_dev->num_channels = DS4424_MAX_DAC_CHANNELS;
-		break;
-	default:
-		dev_err(&client->dev,
-				"ds4424: Invalid chip id.\n");
-		ret = -ENXIO;
-		goto fail;
-	}
-
+	indio_dev->num_channels = chip_info->num_channels;
 	indio_dev->channels = ds4424_channels;
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->info = &ds4424_iio_info;
@@ -278,16 +277,16 @@ static void ds4424_remove(struct i2c_client *client)
 }
 
 static const struct i2c_device_id ds4424_id[] = {
-	{ "ds4422", ID_DS4422 },
-	{ "ds4424", ID_DS4424 },
+	{ "ds4422", (kernel_ulong_t)&ds4422_info },
+	{ "ds4424", (kernel_ulong_t)&ds4424_info },
 	{ }
 };
 
 MODULE_DEVICE_TABLE(i2c, ds4424_id);
 
 static const struct of_device_id ds4424_of_match[] = {
-	{ .compatible = "maxim,ds4422" },
-	{ .compatible = "maxim,ds4424" },
+	{ .compatible = "maxim,ds4422", .data = &ds4422_info },
+	{ .compatible = "maxim,ds4424", .data = &ds4424_info },
 	{ }
 };
 
-- 
2.47.3
Re: [PATCH v5 06/13] iio: dac: ds4424: use device match data for chip info
Posted by Andy Shevchenko 5 days, 5 hours ago
On Wed, Feb 04, 2026 at 03:00:38PM +0100, Oleksij Rempel wrote:
> Refactor the driver to use device match data instead of checking ID enums
> in a switch statement.
> 
> Define a `ds4424_chip_info` structure to hold variant-specific attributes
> (currently just the channel count) and attach it directly to the I2C and
> OF device ID tables.
> 
> This simplifies the probe function and makes it easier to add support for
> new variants like DS4402/DS4404.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
with a hope that we get rid an I²C ID requirement at some point.

...

> +struct ds4424_chip_info {
> +	u8 num_channels;

If you wish, you can provide name here that will go to ->name field.
Linker dedups string literals, so it will be just an additional runtime
pointer.

> +};

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v5 06/13] iio: dac: ds4424: use device match data for chip info
Posted by Jonathan Cameron 3 days, 23 hours ago
On Wed, 4 Feb 2026 16:20:13 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Wed, Feb 04, 2026 at 03:00:38PM +0100, Oleksij Rempel wrote:
> > Refactor the driver to use device match data instead of checking ID enums
> > in a switch statement.
> > 
> > Define a `ds4424_chip_info` structure to hold variant-specific attributes
> > (currently just the channel count) and attach it directly to the I2C and
> > OF device ID tables.
> > 
> > This simplifies the probe function and makes it easier to add support for
> > new variants like DS4402/DS4404.  
> 
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> with a hope that we get rid an I²C ID requirement at some point.
> 
> ...
> 
> > +struct ds4424_chip_info {
> > +	u8 num_channels;  
> 
> If you wish, you can provide name here that will go to ->name field.
> Linker dedups string literals, so it will be just an additional runtime
> pointer.

I'd prefer we did this now.  That avoids any future issue where we get
subtle mismatches between DT compatibles listed and the i2c_device_id
names.

We've been bitten by bugs around this before, hence tend to push names
into the chip_info structure instead of using id->name.  IIRC correctly
the fun starts when we have a fallback compatible.

Jonathan


> 
> > +};  
>