Skip to contentSkip to conversation

Fixes and cleanups for the AD5686 IIO driver

PATCH Applied Rodrigo Alencar 3 participants 23 messages Apr 26 – Apr 27

Subsystems (2)

v1 of 7 Applied Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Fixes and cleanups for the AD5686 IIO driver

This is the first series of three on updating the AD5686 driver.

A bigger patch series was sent before ("Extend device support for AD5686 driver"), but this is not exactly a v2:

https://lore.kernel.org/r/[email protected]

This one adds a number of cleanups and fixes, like:
- Refactor include headers (IWYU);
- Remove redundant register definition;
- Drop enum chip id in favor of per-device chip_info structs;
- Fix internal voltage reference control for single-channel devices;
- Acquire lock when doing power down control;
- Fix powerdown control for dual-channel devices;

---
Rodrigo Alencar (10):
iio: dac: ad5686: refactor include headers
iio: dac: ad5686: remove redundant register definition
iio: dac: ad5686: drop enum id
iio: dac: ad5686: add of_match table to the spi driver
iio: dac: ad5686: fix ref bit initialization for single-channel parts
iio: dac: ad5686: acquire lock when doing powerdown control
iio: dac: ad5686: fix powerdown control on dual-channel devices
iio: dac: ad5686: fix input raw value check
iio: dac: ad5686: cleanup doc header of local structs
iio: dac: ad5686: create bus ops struct

 drivers/iio/dac/ad5686-spi.c |  73 ++++--
 drivers/iio/dac/ad5686.c     | 544 +++++++++++++++++++++----------------------
 drivers/iio/dac/ad5686.h     | 116 ++++-----
 drivers/iio/dac/ad5696-i2c.c |  80 ++++---
 4 files changed, 418 insertions(+), 395 deletions(-)

---
base-commit: d1b86ecd0b377b36aa6581d2bcf73ba43a9e3c8d
change-id: 20260426-ad5686-fixes-63ea68811bdb

Best regards,

Signature · 1 line
Rodrigo Alencar <[email protected]>
#1 lore raw

PATCH 1/10 iio: dac: ad5686: refactor include headers

Apply IWYU principle, replacing unused/generic headers for specific/missing headers. The resulting include directive list is sorted accordingly.

---
 drivers/iio/dac/ad5686-spi.c |  9 +++++++--
 drivers/iio/dac/ad5686.c     | 13 ++++---------
 drivers/iio/dac/ad5686.h     |  5 ++---
 drivers/iio/dac/ad5696-i2c.c | 10 +++++++---
 4 files changed, 20 insertions(+), 17 deletions(-)
drivers/iio/dac/ad5686-spi.c +7 -2
index df8619e0c092..695125238633 100644
@@ -8,11 +8,16 @@
  * Copyright 2018 Analog Devices Inc.
  */
 
-#include "ad5686.h"
-
+#include <linux/array_size.h>
+#include <linux/err.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/spi/spi.h>
 
+#include <asm/byteorder.h>
+
+#include "ad5686.h"
+
 static int ad5686_spi_write(struct ad5686_state *st,
 			    u8 cmd, u8 addr, u16 val)
 {
drivers/iio/dac/ad5686.c +4 -9
index 9a384c50929b..1a6eb3832f22 100644
@@ -5,17 +5,12 @@
  * Copyright 2011 Analog Devices Inc.
  */
 
-#include <linux/interrupt.h>
-#include <linux/fs.h>
-#include <linux/device.h>
+#include <linux/array_size.h>
+#include <linux/err.h>
+#include <linux/export.h>
 #include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/sysfs.h>
 #include <linux/regulator/consumer.h>
-
-#include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
+#include <linux/sysfs.h>
 
 #include "ad5686.h"
 
drivers/iio/dac/ad5686.h +2 -3
index e7d36bae3e59..3fcc792ea656 100644
@@ -8,10 +8,9 @@
 #ifndef __DRIVERS_IIO_DAC_AD5686_H__
 #define __DRIVERS_IIO_DAC_AD5686_H__
 
-#include <linux/types.h>
-#include <linux/cache.h>
+#include <linux/bits.h>
 #include <linux/mutex.h>
-#include <linux/kernel.h>
+#include <linux/types.h>
 
 #include <linux/iio/iio.h>
 
drivers/iio/dac/ad5696-i2c.c +7 -3
index d3327bca0e07..e7e1b8a6fc71 100644
@@ -7,10 +7,14 @@
  * Copyright 2018 Analog Devices Inc.
  */
 
-#include "ad5686.h"
-
-#include <linux/module.h>
+#include <linux/err.h>
 #include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+
+#include <asm/byteorder.h>
+
+#include "ad5686.h"
 
 static int ad5686_i2c_read(struct ad5686_state *st, u8 addr)
 {
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH 01/10] iio: dac: ad5686: refactor include headers

Quoted 5 lines · On Sun, 26 Apr 2026 09:38:02 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Apply IWYU principle, replacing unused/generic headers for specific/missing headers. The resulting include directive list is sorted accordingly.

As below, I would expect some headers to be included in the c file even if they are also in the local header.

Quoted 43 lines
---
 drivers/iio/dac/ad5686-spi.c |  9 +++++++--
 drivers/iio/dac/ad5686.c     | 13 ++++---------
 drivers/iio/dac/ad5686.h     |  5 ++---
 drivers/iio/dac/ad5696-i2c.c | 10 +++++++---
 4 files changed, 20 insertions(+), 17 deletions(-)
drivers/iio/dac/ad5686-spi.c +7 -2
index df8619e0c092..695125238633 100644
@@ -8,11 +8,16 @@
  * Copyright 2018 Analog Devices Inc.
  */
 
-#include "ad5686.h"
-
+#include <linux/array_size.h>
+#include <linux/err.h>
+#include <linux/mod_devicetable.h>
 #include <linux/module.h>
 #include <linux/spi/spi.h>
 
+#include <asm/byteorder.h>
+
+#include "ad5686.h"
+
 static int ad5686_spi_write(struct ad5686_state *st,
 			    u8 cmd, u8 addr, u16 val)
 {
drivers/iio/dac/ad5686.c +0 -3
index 9a384c50929b..1a6eb3832f22 100644
@@ -5,17 +5,12 @@
  * Copyright 2011 Analog Devices Inc.
  */
 
-#include <linux/interrupt.h>
-#include <linux/fs.h>
-#include <linux/device.h>

drivers/iio/dac/ad5686.c line 17

Quoted 10 lines
drivers/iio/dac/ad5686.c +3 -5
+#include <linux/array_size.h>
+#include <linux/err.h>
+#include <linux/export.h>
 #include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/sysfs.h>
 #include <linux/regulator/consumer.h>
-
-#include <linux/iio/iio.h>

Keep this one in the c files. IWYU does not allow for removing includes just because they are in another header. The fact this one is included in ad5686.h is down to using IIO_DMA_MINALIGN which is not 'obvious' from point of view of the c files including it.

Quoted 46 lines
drivers/iio/dac/ad5686.c +1 -1
-#include <linux/iio/sysfs.h>
+#include <linux/sysfs.h>
 
 #include "ad5686.h"
 
drivers/iio/dac/ad5686.h +2 -3
index e7d36bae3e59..3fcc792ea656 100644
@@ -8,10 +8,9 @@
 #ifndef __DRIVERS_IIO_DAC_AD5686_H__
 #define __DRIVERS_IIO_DAC_AD5686_H__
 
-#include <linux/types.h>
-#include <linux/cache.h>
+#include <linux/bits.h>
 #include <linux/mutex.h>
-#include <linux/kernel.h>
+#include <linux/types.h>
 
 #include <linux/iio/iio.h>
 
drivers/iio/dac/ad5696-i2c.c +7 -3
index d3327bca0e07..e7e1b8a6fc71 100644
@@ -7,10 +7,14 @@
  * Copyright 2018 Analog Devices Inc.
  */
 
-#include "ad5686.h"
-
-#include <linux/module.h>
+#include <linux/err.h>
 #include <linux/i2c.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+
+#include <asm/byteorder.h>
+
+#include "ad5686.h"
 
 static int ad5686_i2c_read(struct ad5686_state *st, u8 addr)
 {
#3 lore raw

PATCH 2/10 iio: dac: ad5686: remove redundant register definition

In reply to Rodrigo Alencar

AD5683_REGMAP and AD5693_REGMAP behave the same way in the common code, and that is because they target single channel devices from the same sub-family. There is no reason to separate them and it will make things simpler when refactoring the chip info table.

---
 drivers/iio/dac/ad5686.c | 19 +++++--------------
 drivers/iio/dac/ad5686.h |  2 --
 2 files changed, 5 insertions(+), 16 deletions(-)
drivers/iio/dac/ad5686.c +5 -14
index 1a6eb3832f22..0bc925bd827e 100644
@@ -93,10 +93,6 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 		if (chan->channel > 0x7)
 			address = 0x8;
 		break;
-	case AD5693_REGMAP:
-		shift = 13;
-		ref_bit_msk = AD5693_REF_BIT_MSK;
-		break;
 	default:
 		return -EINVAL;
 	}
@@ -277,7 +273,7 @@ static const struct ad5686_chip_info ad5686_chip_info_tbl[] = {
 		.channels = ad5311r_channels,
 		.int_vref_mv = 2500,
 		.num_channels = 1,
-		.regmap_type = AD5693_REGMAP,
+		.regmap_type = AD5683_REGMAP,
 	},
 	[ID_AD5337R] = {
 		.channels = ad5337r_channels,
@@ -399,24 +395,24 @@ static const struct ad5686_chip_info ad5686_chip_info_tbl[] = {
 		.channels = ad5691r_channels,
 		.int_vref_mv = 2500,
 		.num_channels = 1,
-		.regmap_type = AD5693_REGMAP,
+		.regmap_type = AD5683_REGMAP,
 	},
 	[ID_AD5692R] = {
 		.channels = ad5692r_channels,
 		.int_vref_mv = 2500,
 		.num_channels = 1,
-		.regmap_type = AD5693_REGMAP,
+		.regmap_type = AD5683_REGMAP,
 	},
 	[ID_AD5693] = {
 		.channels = ad5693_channels,
 		.num_channels = 1,
-		.regmap_type = AD5693_REGMAP,
+		.regmap_type = AD5683_REGMAP,
 	},
 	[ID_AD5693R] = {
 		.channels = ad5693_channels,
 		.int_vref_mv = 2500,
 		.num_channels = 1,
-		.regmap_type = AD5693_REGMAP,
+		.regmap_type = AD5683_REGMAP,
 	},
 	[ID_AD5694] = {
 		.channels = ad5684_channels,
@@ -508,11 +504,6 @@ int ad5686_probe(struct device *dev,
 		cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
 		ref_bit_msk = 0;
 		break;
-	case AD5693_REGMAP:
-		cmd = AD5686_CMD_CONTROL_REG;
-		ref_bit_msk = AD5693_REF_BIT_MSK;
-		st->use_internal_vref = !has_external_vref;
-		break;
 	default:
 		return -EINVAL;
 	}
drivers/iio/dac/ad5686.h +0 -2
index 3fcc792ea656..fe6482e84178 100644
@@ -45,7 +45,6 @@
 
 #define AD5310_REF_BIT_MSK			BIT(8)
 #define AD5683_REF_BIT_MSK			BIT(12)
-#define AD5693_REF_BIT_MSK			BIT(12)
 
 /**
  * ad5686_supported_device_ids:
@@ -88,7 +87,6 @@ enum ad5686_regmap_type {
 	AD5310_REGMAP,
 	AD5683_REGMAP,
 	AD5686_REGMAP,
-	AD5693_REGMAP
 };
 
 struct ad5686_state;
Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH 02/10] iio: dac: ad5686: remove redundant register definition

Quoted 9 lines · On Sun, 26 Apr 2026 09:38:03 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

AD5683_REGMAP and AD5693_REGMAP behave the same way in the common code, and that is because they target single channel devices from the same sub-family. There is no reason to separate them and it will make things simpler when refactoring the chip info table.

This one stands well on it's own, so applied to the testing branch of iio.git.

IIO is very busy at the moment, so I'm trying to pick up anything that is ready, including individual patches from larger series.

Thanks,

Jonathan

maintainer
#5 lore raw

Re: [PATCH 02/10] iio: dac: ad5686: remove redundant register definition

Quoted 10 lines · On Sun, 26 Apr 2026 14:32:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 7 lines · On Sun, 26 Apr 2026 09:38:03 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

AD5683_REGMAP and AD5693_REGMAP behave the same way in the common code, and that is because they target single channel devices from the same sub-family. There is no reason to separate them and it will make things simpler when refactoring the chip info table.

Quoted 7 lines
Quoted 2 lines

This one stands well on it's own, so applied to the testing branch of iio.git.

IIO is very busy at the moment, so I'm trying to pick up anything that is ready, including individual patches from larger series.

Dropped again. I didn't read on far enough. The fixes in this series need to come before all this stuff so they are easier to backport.

So dropped 2-4 from my tree pending that reorganization.

Quoted 3 lines

Thanks,

Jonathan

#6 lore raw

Re: [PATCH 02/10] iio: dac: ad5686: remove redundant register definition

Quoted 17 lines · Jonathan Cameron
Quoted 14 lines · On Sun, 26 Apr 2026 14:32:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sun, 26 Apr 2026 09:38:03 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

AD5683_REGMAP and AD5693_REGMAP behave the same way in the common code, and that is because they target single channel devices from the same sub-family. There is no reason to separate them and it will make things simpler when refactoring the chip info table.

This one stands well on it's own, so applied to the testing branch of iio.git.

Quoted 7 lines
Quoted 3 lines

IIO is very busy at the moment, so I'm trying to pick up anything that is ready, including individual patches from larger series.

Dropped again. I didn't read on far enough. The fixes in this series need to come before all this stuff so they are easier to backport.

So dropped 2-4 from my tree pending that reorganization.

I am getting the fixes first. But I still see commit 2 and 3 in the testing branch.

Signature · 3 lines
Kind regards,

Rodrigo Alencar
maintainer
#7 lore raw

Re: [PATCH 02/10] iio: dac: ad5686: remove redundant register definition

Quoted 21 lines · On Mon, 27 Apr 2026 11:09:59 +0100 Rodrigo Alencar…
Quoted 20 lines · Jonathan Cameron
Quoted 17 lines · On Sun, 26 Apr 2026 14:32:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sun, 26 Apr 2026 09:38:03 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

AD5683_REGMAP and AD5693_REGMAP behave the same way in the common code, and that is because they target single channel devices from the same sub-family. There is no reason to separate them and it will make things simpler when refactoring the chip info table.

This one stands well on it's own, so applied to the testing branch of iio.git.

IIO is very busy at the moment, so I'm trying to pick up anything that is ready, including individual patches from larger series.

Quoted 8 lines
Quoted 4 lines

Dropped again. I didn't read on far enough. The fixes in this series need to come before all this stuff so they are easier to backport.

So dropped 2-4 from my tree pending that reorganization.

I am getting the fixes first. But I still see commit 2 and 3 in the testing branch.

ah. Guess I forgot to push out. Done now (along with rebase on rc1)

#8 lore raw

PATCH 3/10 iio: dac: ad5686: drop enum id

In reply to Rodrigo Alencar

Split chip info table into separate structs and expose them to the spi i2c drivers. That is the preferrable approach and allows for the drivers to have knowledge of the device info before the common probe function gets called. Those chip info structs may be shared by SPI and I2C driver variants.
Channel declaration definitions are grouped according to channel count and DECLARE_AD5693_CHANNELS() macro is renamed to DECLARE_AD5683_CHANNELS() to match the regmap_type enum.
Use spi_get_device_match_data() and i2c_get_match_data() to get chip info struct reference, passing it as parameter to the common probe function.

---
 drivers/iio/dac/ad5686-spi.c |  38 +++--
 drivers/iio/dac/ad5686.c     | 358 ++++++++++++++++++++-----------------------
 drivers/iio/dac/ad5686.h     |  66 ++++----
 drivers/iio/dac/ad5696-i2c.c |  65 ++++----
 4 files changed, 241 insertions(+), 286 deletions(-)
drivers/iio/dac/ad5686-spi.c +18 -20
index 695125238633..73e6c998add0 100644
@@ -94,29 +94,27 @@ static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
 
 static int ad5686_spi_probe(struct spi_device *spi)
 {
-	const struct spi_device_id *id = spi_get_device_id(spi);
-
-	return ad5686_probe(&spi->dev, id->driver_data, id->name,
-			    ad5686_spi_write, ad5686_spi_read);
+	return ad5686_probe(&spi->dev, spi_get_device_match_data(spi),
+			    spi->modalias, ad5686_spi_write, ad5686_spi_read);
 }
 
 static const struct spi_device_id ad5686_spi_id[] = {
-	{"ad5310r", ID_AD5310R},
-	{"ad5672r", ID_AD5672R},
-	{"ad5674r", ID_AD5674R},
-	{"ad5676", ID_AD5676},
-	{"ad5676r", ID_AD5676R},
-	{"ad5679r", ID_AD5679R},
-	{"ad5681r", ID_AD5681R},
-	{"ad5682r", ID_AD5682R},
-	{"ad5683", ID_AD5683},
-	{"ad5683r", ID_AD5683R},
-	{"ad5684", ID_AD5684},
-	{"ad5684r", ID_AD5684R},
-	{"ad5685", ID_AD5685R}, /* Does not exist */
-	{"ad5685r", ID_AD5685R},
-	{"ad5686", ID_AD5686},
-	{"ad5686r", ID_AD5686R},
+	{ "ad5310r",  (kernel_ulong_t)&ad5310r_chip_info },
+	{ "ad5672r",  (kernel_ulong_t)&ad5672r_chip_info },
+	{ "ad5674r",  (kernel_ulong_t)&ad5674r_chip_info },
+	{ "ad5676",   (kernel_ulong_t)&ad5676_chip_info },
+	{ "ad5676r",  (kernel_ulong_t)&ad5676r_chip_info },
+	{ "ad5679r",  (kernel_ulong_t)&ad5679r_chip_info },
+	{ "ad5681r",  (kernel_ulong_t)&ad5681r_chip_info },
+	{ "ad5682r",  (kernel_ulong_t)&ad5682r_chip_info },
+	{ "ad5683",   (kernel_ulong_t)&ad5683_chip_info },
+	{ "ad5683r",  (kernel_ulong_t)&ad5683r_chip_info },
+	{ "ad5684",   (kernel_ulong_t)&ad5684_chip_info },
+	{ "ad5684r",  (kernel_ulong_t)&ad5684r_chip_info },
+	{ "ad5685",   (kernel_ulong_t)&ad5685r_chip_info }, /* Does not exist */
+	{ "ad5685r",  (kernel_ulong_t)&ad5685r_chip_info },
+	{ "ad5686",   (kernel_ulong_t)&ad5686_chip_info },
+	{ "ad5686r",  (kernel_ulong_t)&ad5686r_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(spi, ad5686_spi_id);
drivers/iio/dac/ad5686.c +162 -196

382 lines not on this page · raw message

drivers/iio/dac/ad5686.h +29 -37

84 lines not on this page · raw message

drivers/iio/dac/ad5696-i2c.c +32 -33

79 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [PATCH 03/10] iio: dac: ad5686: drop enum id

Quoted 13 lines · On Sun, 26 Apr 2026 09:38:04 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Split chip info table into separate structs and expose them to the spi i2c drivers. That is the preferrable approach and allows for the drivers to have knowledge of the device info before the common probe function gets called. Those chip info structs may be shared by SPI and I2C driver variants.
Channel declaration definitions are grouped according to channel count and DECLARE_AD5693_CHANNELS() macro is renamed to DECLARE_AD5683_CHANNELS() to match the regmap_type enum.
Use spi_get_device_match_data() and i2c_get_match_data() to get chip info struct reference, passing it as parameter to the common probe function.

Quoted 1 line

Nice. Given no one had any comments on v1 I'll assume everyone is fine with this one.

Applied to the testing branch of iio.git.

thanks,

Jonathan

#10 lore raw

PATCH 4/10 iio: dac: ad5686: add of_match table to the spi driver

In reply to Rodrigo Alencar

Add of_match table for the SPI device variants to be consistent with the AD5696 I2C driver.

---
 drivers/iio/dac/ad5686-spi.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/iio/dac/ad5686-spi.c +21 -0
index 73e6c998add0..8fef0e6d33ff 100644
@@ -119,9 +119,30 @@ static const struct spi_device_id ad5686_spi_id[] = {
 };
 MODULE_DEVICE_TABLE(spi, ad5686_spi_id);
 
+static const struct of_device_id ad5686_of_match[] = {
+	{ .compatible = "adi,ad5310r", .data = &ad5310r_chip_info },
+	{ .compatible = "adi,ad5672r", .data = &ad5672r_chip_info },
+	{ .compatible = "adi,ad5674r", .data = &ad5674r_chip_info },
+	{ .compatible = "adi,ad5676",  .data = &ad5676_chip_info },
+	{ .compatible = "adi,ad5676r", .data = &ad5676r_chip_info },
+	{ .compatible = "adi,ad5679r", .data = &ad5679r_chip_info },
+	{ .compatible = "adi,ad5681r", .data = &ad5681r_chip_info },
+	{ .compatible = "adi,ad5682r", .data = &ad5682r_chip_info },
+	{ .compatible = "adi,ad5683",  .data = &ad5683_chip_info },
+	{ .compatible = "adi,ad5683r", .data = &ad5683r_chip_info },
+	{ .compatible = "adi,ad5684",  .data = &ad5684_chip_info },
+	{ .compatible = "adi,ad5684r", .data = &ad5684r_chip_info },
+	{ .compatible = "adi,ad5685r", .data = &ad5685r_chip_info },
+	{ .compatible = "adi,ad5686",  .data = &ad5686_chip_info },
+	{ .compatible = "adi,ad5686r", .data = &ad5686r_chip_info },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, ad5686_of_match);
+
 static struct spi_driver ad5686_spi_driver = {
 	.driver = {
 		.name = "ad5686",
+		.of_match_table = ad5686_of_match,
 	},
 	.probe = ad5686_spi_probe,
 	.id_table = ad5686_spi_id,
Signature · 1 line
2.43.0
maintainer Applied
#11 lore raw

Re: [PATCH 04/10] iio: dac: ad5686: add of_match table to the spi driver

Quoted 6 lines · On Sun, 26 Apr 2026 09:38:05 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Add of_match table for the SPI device variants to be consistent with the AD5696 I2C driver.

Applied. Note I'll be rebasing anyway in a day or two so if anyone else does want to give tags I can probably add them.

(or feedback that means I pull this out of my tree!)

Quoted 40 lines
---
 drivers/iio/dac/ad5686-spi.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/iio/dac/ad5686-spi.c +21 -0
index 73e6c998add0..8fef0e6d33ff 100644
@@ -119,9 +119,30 @@ static const struct spi_device_id ad5686_spi_id[] = {
 };
 MODULE_DEVICE_TABLE(spi, ad5686_spi_id);
 
+static const struct of_device_id ad5686_of_match[] = {
+	{ .compatible = "adi,ad5310r", .data = &ad5310r_chip_info },
+	{ .compatible = "adi,ad5672r", .data = &ad5672r_chip_info },
+	{ .compatible = "adi,ad5674r", .data = &ad5674r_chip_info },
+	{ .compatible = "adi,ad5676",  .data = &ad5676_chip_info },
+	{ .compatible = "adi,ad5676r", .data = &ad5676r_chip_info },
+	{ .compatible = "adi,ad5679r", .data = &ad5679r_chip_info },
+	{ .compatible = "adi,ad5681r", .data = &ad5681r_chip_info },
+	{ .compatible = "adi,ad5682r", .data = &ad5682r_chip_info },
+	{ .compatible = "adi,ad5683",  .data = &ad5683_chip_info },
+	{ .compatible = "adi,ad5683r", .data = &ad5683r_chip_info },
+	{ .compatible = "adi,ad5684",  .data = &ad5684_chip_info },
+	{ .compatible = "adi,ad5684r", .data = &ad5684r_chip_info },
+	{ .compatible = "adi,ad5685r", .data = &ad5685r_chip_info },
+	{ .compatible = "adi,ad5686",  .data = &ad5686_chip_info },
+	{ .compatible = "adi,ad5686r", .data = &ad5686r_chip_info },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, ad5686_of_match);
+
 static struct spi_driver ad5686_spi_driver = {
 	.driver = {
 		.name = "ad5686",
+		.of_match_table = ad5686_of_match,
 	},
 	.probe = ad5686_spi_probe,
 	.id_table = ad5686_spi_id,
#12 lore raw

PATCH 5/10 iio: dac: ad5686: fix ref bit initialization for single-channel parts

In reply to Rodrigo Alencar

For single-channel parts the control register is used to enable the internal voltage reference and perform powerdown control. The reference enable bit position was ignored when writing the register at the probe function (!!val was used). This patch adds a control_sync() function that properly consumes the mask definitions with FIELD_PREP(). As further cleanup, the created functions and definitions are also used in ad5686_write_dac_powerdown(). Some local variables ended up being unused (so removed) and st->use_internal_vref is initialized earlier in probe, because it is also applicable to the AD5686_REGMAP case.

Fixes be1b24d24541 ("iio:dac:ad5686: Add AD5691R/AD5692R/AD5693/AD5693R support")
---
 drivers/iio/dac/ad5686.c | 89 ++++++++++++++++++++++++++++--------------------
 drivers/iio/dac/ad5686.h |  3 +-
 2 files changed, 54 insertions(+), 38 deletions(-)
drivers/iio/dac/ad5686.c +52 -37
index e67faef91164..843e425f656f 100644
@@ -6,11 +6,13 @@
  */
 
 #include <linux/array_size.h>
+#include <linux/bitfield.h>
 #include <linux/err.h>
 #include <linux/export.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/sysfs.h>
+#include <linux/wordpart.h>
 
 #include "ad5686.h"
 
@@ -20,6 +22,24 @@ static const char * const ad5686_powerdown_modes[] = {
 	"three_state"
 };
 
+static int ad5310_control_sync(struct ad5686_state *st)
+{
+	unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
+
+	return st->write(st, AD5686_CMD_CONTROL_REG, 0,
+			 FIELD_PREP(AD5310_PD_MSK, pd_val) |
+			 FIELD_PREP(AD5310_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
+}
+
+static int ad5683_control_sync(struct ad5686_state *st)
+{
+	unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
+
+	return st->write(st, AD5686_CMD_CONTROL_REG, 0,
+			 FIELD_PREP(AD5683_PD_MSK, pd_val) |
+			 FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
+}
+
 static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,
 				     const struct iio_chan_spec *chan)
 {
@@ -65,8 +85,8 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 	bool readin;
 	int ret;
 	struct ad5686_state *st = iio_priv(indio_dev);
-	unsigned int val, ref_bit_msk;
-	u8 shift, address = 0;
+	unsigned int val;
+	u8 address;
 
 	ret = kstrtobool(buf, &readin);
 	if (ret)
@@ -79,32 +99,34 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
-		shift = 9;
-		ref_bit_msk = AD5310_REF_BIT_MSK;
+		ret = ad5310_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5683_REGMAP:
-		shift = 13;
-		ref_bit_msk = AD5683_REF_BIT_MSK;
+		ret = ad5683_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5686_REGMAP:
-		shift = 0;
-		ref_bit_msk = 0;
 		/* AD5674R/AD5679R have 16 channels and 2 powerdown registers */
-		if (chan->channel > 0x7)
+		val = st->pwr_down_mask & st->pwr_down_mode;
+		if (chan->channel > 0x7) {
 			address = 0x8;
+			val = upper_16_bits(val);
+		} else {
+			address = 0x0;
+			val = lower_16_bits(val);
+		}
+		ret = st->write(st, AD5686_CMD_POWERDOWN_DAC, address, val);
+		if (ret)
+			return ret;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	val = ((st->pwr_down_mask & st->pwr_down_mode) << shift);
-	if (!st->use_internal_vref)
-		val |= ref_bit_msk;
-
-	ret = st->write(st, AD5686_CMD_POWERDOWN_DAC,
-			address, val >> (address * 2));
-
-	return ret ? ret : len;
+	return len;
 }
 
 static int ad5686_read_raw(struct iio_dev *indio_dev,
@@ -416,11 +438,8 @@ int ad5686_probe(struct device *dev,
 		 const char *name, ad5686_write_func write,
 		 ad5686_read_func read)
 {
-	struct ad5686_state *st;
 	struct iio_dev *indio_dev;
-	unsigned int val, ref_bit_msk;
-	bool has_external_vref;
-	u8 cmd;
+	struct ad5686_state *st;
 	int ret, i;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
@@ -438,8 +457,8 @@ int ad5686_probe(struct device *dev,
 	if (ret < 0 && ret != -ENODEV)
 		return ret;
 
-	has_external_vref = ret != -ENODEV;
-	st->vref_mv = has_external_vref ? ret / 1000 : st->chip_info->int_vref_mv;
+	st->use_internal_vref = ret == -ENODEV;
+	st->vref_mv = st->use_internal_vref ? st->chip_info->int_vref_mv : ret / 1000;
 
 	/* Set all the power down mode for all channels to 1K pulldown */
 	for (i = 0; i < st->chip_info->num_channels; i++)
@@ -457,29 +476,25 @@ int ad5686_probe(struct device *dev,
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
-		cmd = AD5686_CMD_CONTROL_REG;
-		ref_bit_msk = AD5310_REF_BIT_MSK;
-		st->use_internal_vref = !has_external_vref;
+		ret = ad5310_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5683_REGMAP:
-		cmd = AD5686_CMD_CONTROL_REG;
-		ref_bit_msk = AD5683_REF_BIT_MSK;
-		st->use_internal_vref = !has_external_vref;
+		ret = ad5683_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5686_REGMAP:
-		cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
-		ref_bit_msk = 0;
+		ret = st->write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
+				st->use_internal_vref ? 0 : 1);
+		if (ret)
+			return ret;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	val = (has_external_vref | ref_bit_msk);
-
-	ret = st->write(st, cmd, 0, !!val);
-	if (ret)
-		return ret;
-
 	return devm_iio_device_register(dev, indio_dev);
 }
 EXPORT_SYMBOL_NS_GPL(ad5686_probe, "IIO_AD5686");
drivers/iio/dac/ad5686.h +2 -1
index 6e3552c92e4b..7004d0d1d97a 100644
@@ -44,8 +44,9 @@
 #define AD5686_CMD_READBACK_ENABLE_V2		0x5
 
 #define AD5310_REF_BIT_MSK			BIT(8)
+#define AD5310_PD_MSK				GENMASK(10, 9)
 #define AD5683_REF_BIT_MSK			BIT(12)
-
+#define AD5683_PD_MSK				GENMASK(14, 13)
 
 enum ad5686_regmap_type {
 	AD5310_REGMAP,
Signature · 1 line
2.43.0
maintainer
#13 lore raw

Re: [PATCH 05/10] iio: dac: ad5686: fix ref bit initialization for single-channel parts

Quoted 9 lines · On Sun, 26 Apr 2026 09:38:06 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

For single-channel parts the control register is used to enable the internal voltage reference and perform powerdown control. The reference enable bit position was ignored when writing the register at the probe function (!!val was used). This patch adds a control_sync() function that properly consumes the mask definitions with FIELD_PREP(). As further cleanup, the created functions and definitions are also used in ad5686_write_dac_powerdown().

Probably split this out unless it is very trivial. Just include stuff needed for fix in this patch. Then follow (after all the fixes) with the reuse.

Quoted 5 lines

Some local variables ended up being unused
(so removed) and st->use_internal_vref is initialized earlier in probe, because it is also applicable to the AD5686_REGMAP case.

Fixes be1b24d24541 ("iio:dac:ad5686: Add AD5691R/AD5692R/AD5693/AD5693R support")

Why is this not at the start of the series? Fixes always come first even if that means they are a bit messier. We need to be able to backport them and don't want to have to mess with the large refactors that you have before them, to do so.

Quoted 111 lines
---
 drivers/iio/dac/ad5686.c | 89 ++++++++++++++++++++++++++++--------------------
 drivers/iio/dac/ad5686.h |  3 +-
 2 files changed, 54 insertions(+), 38 deletions(-)
drivers/iio/dac/ad5686.c +39 -17
index e67faef91164..843e425f656f 100644
@@ -6,11 +6,13 @@
  */
 
 #include <linux/array_size.h>
+#include <linux/bitfield.h>
 #include <linux/err.h>
 #include <linux/export.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/sysfs.h>
+#include <linux/wordpart.h>
 
 #include "ad5686.h"
 
@@ -20,6 +22,24 @@ static const char * const ad5686_powerdown_modes[] = {
 	"three_state"
 };
 
+static int ad5310_control_sync(struct ad5686_state *st)
+{
+	unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
+
+	return st->write(st, AD5686_CMD_CONTROL_REG, 0,
+			 FIELD_PREP(AD5310_PD_MSK, pd_val) |
+			 FIELD_PREP(AD5310_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
+}
+
+static int ad5683_control_sync(struct ad5686_state *st)
+{
+	unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
+
+	return st->write(st, AD5686_CMD_CONTROL_REG, 0,
+			 FIELD_PREP(AD5683_PD_MSK, pd_val) |
+			 FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
+}
+
 static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,
 				     const struct iio_chan_spec *chan)
 {
@@ -65,8 +85,8 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 	bool readin;
 	int ret;
 	struct ad5686_state *st = iio_priv(indio_dev);
-	unsigned int val, ref_bit_msk;
-	u8 shift, address = 0;
+	unsigned int val;
+	u8 address;
 
 	ret = kstrtobool(buf, &readin);
 	if (ret)
@@ -79,32 +99,34 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
-		shift = 9;
-		ref_bit_msk = AD5310_REF_BIT_MSK;
+		ret = ad5310_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5683_REGMAP:
-		shift = 13;
-		ref_bit_msk = AD5683_REF_BIT_MSK;
+		ret = ad5683_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5686_REGMAP:
-		shift = 0;
-		ref_bit_msk = 0;
 		/* AD5674R/AD5679R have 16 channels and 2 powerdown registers */
-		if (chan->channel > 0x7)
+		val = st->pwr_down_mask & st->pwr_down_mode;
+		if (chan->channel > 0x7) {
 			address = 0x8;
+			val = upper_16_bits(val);
+		} else {
+			address = 0x0;
+			val = lower_16_bits(val);
+		}
+		ret = st->write(st, AD5686_CMD_POWERDOWN_DAC, address, val);
+		if (ret)
+			return ret;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	val = ((st->pwr_down_mask & st->pwr_down_mode) << shift);
-	if (!st->use_internal_vref)
-		val |= ref_bit_msk;
-
-	ret = st->write(st, AD5686_CMD_POWERDOWN_DAC,
-			address, val >> (address * 2));
-
-	return ret ? ret : len;
+	return len;
 }
 
 static int ad5686_read_raw(struct iio_dev *indio_dev,

drivers/iio/dac/ad5686.c int ad5686_probe(struct device *dev, · line 442

Quoted 9 lines
drivers/iio/dac/ad5686.c +1 -4
@@ -416,11 +438,8 @@ int ad5686_probe(struct device *dev,
 		 const char *name, ad5686_write_func write,
 		 ad5686_read_func read)
 {
-	struct ad5686_state *st;
 	struct iio_dev *indio_dev;
-	unsigned int val, ref_bit_msk;
-	bool has_external_vref;
-	u8 cmd;
+	struct ad5686_state *st;

I'd not bother with fixing up reverse xmas tree here. That's an unrelated change and it makes this patch noisier than ideal.

Quoted 53 lines
drivers/iio/dac/ad5686.c +12 -16
 	int ret, i;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
@@ -438,8 +457,8 @@ int ad5686_probe(struct device *dev,
 	if (ret < 0 && ret != -ENODEV)
 		return ret;
 
-	has_external_vref = ret != -ENODEV;
-	st->vref_mv = has_external_vref ? ret / 1000 : st->chip_info->int_vref_mv;
+	st->use_internal_vref = ret == -ENODEV;
+	st->vref_mv = st->use_internal_vref ? st->chip_info->int_vref_mv : ret / 1000;
 
 	/* Set all the power down mode for all channels to 1K pulldown */
 	for (i = 0; i < st->chip_info->num_channels; i++)
@@ -457,29 +476,25 @@ int ad5686_probe(struct device *dev,
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
-		cmd = AD5686_CMD_CONTROL_REG;
-		ref_bit_msk = AD5310_REF_BIT_MSK;
-		st->use_internal_vref = !has_external_vref;
+		ret = ad5310_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5683_REGMAP:
-		cmd = AD5686_CMD_CONTROL_REG;
-		ref_bit_msk = AD5683_REF_BIT_MSK;
-		st->use_internal_vref = !has_external_vref;
+		ret = ad5683_control_sync(st);
+		if (ret)
+			return ret;
 		break;
 	case AD5686_REGMAP:
-		cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
-		ref_bit_msk = 0;
+		ret = st->write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
+				st->use_internal_vref ? 0 : 1);
+		if (ret)
+			return ret;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	val = (has_external_vref | ref_bit_msk);
-
-	ret = st->write(st, cmd, 0, !!val);
-	if (ret)
-		return ret;
-
 	return devm_iio_device_register(dev, indio_dev);
 }
#14 lore raw

PATCH 6/10 iio: dac: ad5686: acquire lock when doing powerdown control

In reply to Rodrigo Alencar

Protect access of pwr_down_mode and pwr_down_mask fields with existing mutex lock. This issue exists since the ad5686 driver was first introduced. Acquire mutex with guard(mutex)() throughout for consistency, which allows for early returns, facilitating the review of further changes.

Fixes c2f37c8dcadc ("iio: dac: New driver for AD5686R, AD5685R, AD5684R Digital to analog converters")
---
 drivers/iio/dac/ad5686.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/iio/dac/ad5686.c +14 -13
index 843e425f656f..c7d5ee344a6f 100644
@@ -45,6 +45,8 @@ static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,
 {
 	struct ad5686_state *st = iio_priv(indio_dev);
 
+	guard(mutex)(&st->lock);
+
 	return ((st->pwr_down_mode >> (chan->channel * 2)) & 0x3) - 1;
 }
 
@@ -54,6 +56,7 @@ static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev,
 {
 	struct ad5686_state *st = iio_priv(indio_dev);
 
+	guard(mutex)(&st->lock);
 	st->pwr_down_mode &= ~(0x3 << (chan->channel * 2));
 	st->pwr_down_mode |= ((mode + 1) << (chan->channel * 2));
 
@@ -72,6 +75,8 @@ static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
 {
 	struct ad5686_state *st = iio_priv(indio_dev);
 
+	guard(mutex)(&st->lock);
+
 	return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask &
 				       (0x3 << (chan->channel * 2))));
 }
@@ -92,6 +97,8 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 	if (ret)
 		return ret;
 
+	guard(mutex)(&st->lock);
+
 	if (readin)
 		st->pwr_down_mask |= (0x3 << (chan->channel * 2));
 	else
@@ -138,11 +145,11 @@ static int ad5686_read_raw(struct iio_dev *indio_dev,
 	struct ad5686_state *st = iio_priv(indio_dev);
 	int ret;
 
+	guard(mutex)(&st->lock);
+
 	switch (m) {
 	case IIO_CHAN_INFO_RAW:
-		mutex_lock(&st->lock);
 		ret = st->read(st, chan->address);
-		mutex_unlock(&st->lock);
 		if (ret < 0)
 			return ret;
 		*val = (ret >> chan->scan_type.shift) &
@@ -163,25 +170,19 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
 			    long mask)
 {
 	struct ad5686_state *st = iio_priv(indio_dev);
-	int ret;
+
+	guard(mutex)(&st->lock);
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (val > (1 << chan->scan_type.realbits) || val < 0)
 			return -EINVAL;
 
-		mutex_lock(&st->lock);
-		ret = st->write(st,
-				AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
-				chan->address,
-				val << chan->scan_type.shift);
-		mutex_unlock(&st->lock);
-		break;
+		return st->write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
+				 chan->address, val << chan->scan_type.shift);
 	default:
-		ret = -EINVAL;
+		return -EINVAL;
 	}
-
-	return ret;
 }
 
 static const struct iio_info ad5686_info = {
Signature · 1 line
2.43.0
maintainer
#15 lore raw

Re: [PATCH 06/10] iio: dac: ad5686: acquire lock when doing powerdown control

Quoted 5 lines · On Sun, 26 Apr 2026 09:38:07 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Protect access of pwr_down_mode and pwr_down_mask fields with existing mutex lock. This issue exists since the ad5686 driver was first introduced.

This doesn't say what the issue is. You definitely need to mention why those two elements need protecting.

Quoted 5 lines

Acquire mutex with guard(mutex)() throughout for consistency, which allows for early returns, facilitating the review of further changes.

Fixes c2f37c8dcadc ("iio: dac: New driver for AD5686R, AD5685R, AD5684R Digital to analog converters")

Again, fixes at start of patch set. Or given there are a few here, as a set on their own.

Quoted 11 lines
---
 drivers/iio/dac/ad5686.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/iio/dac/ad5686.c +0 -0
index 843e425f656f..c7d5ee344a6f 100644

drivers/iio/dac/ad5686.c static int ad5686_write_raw(struct iio_dev *indio_dev, · line 174

Quoted 6 lines
drivers/iio/dac/ad5686.c +2 -1
@@ -163,25 +170,19 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
 			    long mask)
 {
 	struct ad5686_state *st = iio_priv(indio_dev);
-	int ret;
+
+	guard(mutex)(&st->lock);

Guard changes don't appear related to the thing being fixed so do that in a separate patch. Fine to have an intermediate state where the fix includes guard() only in the new places the lock is taken then a follow up patch that brings everything else in line with that.

Quoted 25 lines
drivers/iio/dac/ad5686.c +3 -10
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (val > (1 << chan->scan_type.realbits) || val < 0)
 			return -EINVAL;
 
-		mutex_lock(&st->lock);
-		ret = st->write(st,
-				AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
-				chan->address,
-				val << chan->scan_type.shift);
-		mutex_unlock(&st->lock);
-		break;
+		return st->write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
+				 chan->address, val << chan->scan_type.shift);
 	default:
-		ret = -EINVAL;
+		return -EINVAL;
 	}
-
-	return ret;
 }
 
 static const struct iio_info ad5686_info = {
 
#16 lore raw

PATCH 7/10 iio: dac: ad5686: fix powerdown control on dual-channel devices

In reply to Rodrigo Alencar

Fix powerdown control by using a proper bit shift for the powerdown mask values. During initialization, powerdown bits are initialized so that unused bits are set to 1 and the correct bit shift is used. Dual-channel devices use one-hot encoding in the address and that reflects on the position of the powerdown bits, which are not channel-index based for that case. Quad-channel devices also use one-hot encoding for the channel address but the result of log2(address) coincides with the channel index value. The issue was introduced when first adding support for dual-channel devices, which overlooked powerdown control differences.

Fixes 7dc8faeab3e3 ("iio: dac: ad5686: add support for AD5338R")
---
 drivers/iio/dac/ad5686.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)
drivers/iio/dac/ad5686.c +28 -10
index c7d5ee344a6f..040a24c62c9a 100644
@@ -40,25 +40,36 @@ static int ad5683_control_sync(struct ad5686_state *st)
 			 FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
 }
 
+static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan)
+{
+	if (chan->channel == chan->address)
+		return chan->channel * 2;
+
+	/* one-hot encoding is used in dual/quad channel devices */
+	return __ffs(chan->address) * 2;
+}
+
 static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,
 				     const struct iio_chan_spec *chan)
 {
+	unsigned int shift = ad5686_pd_mask_shift(chan);
 	struct ad5686_state *st = iio_priv(indio_dev);
 
 	guard(mutex)(&st->lock);
 
-	return ((st->pwr_down_mode >> (chan->channel * 2)) & 0x3) - 1;
+	return ((st->pwr_down_mode >> shift) & 0x3) - 1;
 }
 
 static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev,
 				     const struct iio_chan_spec *chan,
 				     unsigned int mode)
 {
+	unsigned int shift = ad5686_pd_mask_shift(chan);
 	struct ad5686_state *st = iio_priv(indio_dev);
 
 	guard(mutex)(&st->lock);
-	st->pwr_down_mode &= ~(0x3 << (chan->channel * 2));
-	st->pwr_down_mode |= ((mode + 1) << (chan->channel * 2));
+	st->pwr_down_mode &= ~(0x3 << shift);
+	st->pwr_down_mode |= ((mode + 1) << shift);
 
 	return 0;
 }
@@ -73,12 +84,12 @@ static const struct iio_enum ad5686_powerdown_mode_enum = {
 static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
 		uintptr_t private, const struct iio_chan_spec *chan, char *buf)
 {
+	unsigned int shift = ad5686_pd_mask_shift(chan);
 	struct ad5686_state *st = iio_priv(indio_dev);
 
 	guard(mutex)(&st->lock);
 
-	return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask &
-				       (0x3 << (chan->channel * 2))));
+	return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask & (0x3 << shift)));
 }
 
 static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
@@ -100,9 +111,9 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 	guard(mutex)(&st->lock);
 
 	if (readin)
-		st->pwr_down_mask |= (0x3 << (chan->channel * 2));
+		st->pwr_down_mask |= (0x3 << ad5686_pd_mask_shift(chan));
 	else
-		st->pwr_down_mask &= ~(0x3 << (chan->channel * 2));
+		st->pwr_down_mask &= ~(0x3 << ad5686_pd_mask_shift(chan));
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
@@ -441,7 +452,8 @@ int ad5686_probe(struct device *dev,
 {
 	struct iio_dev *indio_dev;
 	struct ad5686_state *st;
-	int ret, i;
+	unsigned int i, shift;
+	int ret;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
 	if (indio_dev == NULL)
@@ -462,8 +474,14 @@ int ad5686_probe(struct device *dev,
 	st->vref_mv = st->use_internal_vref ? st->chip_info->int_vref_mv : ret / 1000;
 
 	/* Set all the power down mode for all channels to 1K pulldown */
-	for (i = 0; i < st->chip_info->num_channels; i++)
-		st->pwr_down_mode |= (0x01 << (i * 2));
+	st->pwr_down_mode = ~0U;
+	st->pwr_down_mask = ~0U;
+	for (i = 0; i < st->chip_info->num_channels; i++) {
+		shift = ad5686_pd_mask_shift(&st->chip_info->channels[i]);
+		st->pwr_down_mask &= ~(0x3 << shift); /* powered up state */
+		st->pwr_down_mode &= ~(0x3 << shift);
+		st->pwr_down_mode |= (0x01 << shift);
+	}
 
 	indio_dev->name = name;
 	indio_dev->info = &ad5686_info;
Signature · 1 line
2.43.0
maintainer
#17 lore raw

Re: [PATCH 07/10] iio: dac: ad5686: fix powerdown control on dual-channel devices

Quoted 12 lines · On Sun, 26 Apr 2026 09:38:08 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Fix powerdown control by using a proper bit shift for the powerdown mask values. During initialization, powerdown bits are initialized so that unused bits are set to 1 and the correct bit shift is used. Dual-channel devices use one-hot encoding in the address and that reflects on the position of the powerdown bits, which are not channel-index based for that case. Quad-channel devices also use one-hot encoding for the channel address but the result of log2(address) coincides with the channel index value. The issue was introduced when first adding support for dual-channel devices, which overlooked powerdown control differences.

Quoted 1 line
Fixes 7dc8faeab3e3 ("iio: dac: ad5686: add support for AD5338R")

Move to start of set.

Quoted 1 line
#18 lore raw

PATCH 8/10 iio: dac: ad5686: fix input raw value check

In reply to Rodrigo Alencar

Use in_range() to fix range check for input raw value, which is off by one, i.e., for a 10-bit DAC the max valid value is 1023, but 1 << 10 equals 1024, which passes the previous check, allowing an out-of-range write. The issue exists since the ad5686 driver was first introduced.

Fixes c2f37c8dcadc ("iio: dac: New driver for AD5686R, AD5685R, AD5684R Digital to analog converters")
---
 drivers/iio/dac/ad5686.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad5686.c +1 -1
index 040a24c62c9a..ab498ec2ed5c 100644
@@ -186,7 +186,7 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		if (val > (1 << chan->scan_type.realbits) || val < 0)
+		if (!in_range(val, 0, 1 << chan->scan_type.realbits))
 			return -EINVAL;
 
 		return st->write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
Signature · 1 line
2.43.0
maintainer
#19 lore raw

Re: [PATCH 08/10] iio: dac: ad5686: fix input raw value check

Quoted 18 lines · On Sun, 26 Apr 2026 09:38:09 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Use in_range() to fix range check for input raw value, which is off by one, i.e., for a 10-bit DAC the max valid value is 1023, but 1 << 10 equals 1024, which passes the previous check, allowing an out-of-range write. The issue exists since the ad5686 driver was first introduced.

Fixes c2f37c8dcadc ("iio: dac: New driver for AD5686R, AD5685R, AD5684R Digital to analog converters")
---
 drivers/iio/dac/ad5686.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad5686.c +0 -0
index 040a24c62c9a..ab498ec2ed5c 100644

drivers/iio/dac/ad5686.c static int ad5686_write_raw(struct iio_dev *indio_dev, · line 190

Quoted 6 lines
drivers/iio/dac/ad5686.c +1 -1
@@ -186,7 +186,7 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		if (val > (1 << chan->scan_type.realbits) || val < 0)
+		if (!in_range(val, 0, 1 << chan->scan_type.realbits))
 			return -EINVAL;

Fix to >= rather than in_range()

Using in_range when we have a start of 0 is overly complex.

Quoted 3 lines
drivers/iio/dac/ad5686.c +0 -0
 		return st->write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
 
#20 lore raw

PATCH 9/10 iio: dac: ad5686: cleanup doc header of local structs

In reply to Rodrigo Alencar

Review documentation comment header for ad5686_chip_info and ad5686_state. Update variable names and description and remove unnecessary blank line between comment and struct declaration.

---
 drivers/iio/dac/ad5686.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/iio/dac/ad5686.h +5 -6
index 7004d0d1d97a..8043cb4ab3cf 100644
@@ -63,12 +63,11 @@ typedef int (*ad5686_read_func)(struct ad5686_state *st, u8 addr);
 
 /**
  * struct ad5686_chip_info - chip specific information
- * @int_vref_mv:	AD5620/40/60: the internal reference voltage
+ * @int_vref_mv:	the internal reference voltage
  * @num_channels:	number of channels
  * @channel:		channel specification
  * @regmap_type:	register map layout variant
  */
-
 struct ad5686_chip_info {
 	u16				int_vref_mv;
 	unsigned int			num_channels;
@@ -106,16 +105,16 @@ extern const struct ad5686_chip_info ad5679r_chip_info;
 
 /**
  * struct ad5686_state - driver instance specific data
- * @spi:		spi_device
+ * @dev:		device instance
  * @chip_info:		chip model specific constants, available modes etc
  * @vref_mv:		actual reference voltage used
  * @pwr_down_mask:	power down mask
  * @pwr_down_mode:	current power down mode
  * @use_internal_vref:	set to true if the internal reference voltage is used
- * @lock		lock to protect the data buffer during regmap ops
- * @data:		spi transfer buffers
+ * @lock:		lock to protect access to state fields, which includes
+ *			the data buffer during regmap ops
+ * @data:		transfer buffers
  */
-
 struct ad5686_state {
 	struct device			*dev;
 	const struct ad5686_chip_info	*chip_info;
Signature · 1 line
2.43.0
#21 lore raw

PATCH 10/10 iio: dac: ad5686: create bus ops struct

In reply to Rodrigo Alencar

Create struct with bus operations, which will be used to extend bus implementation features. Auxiliary functions ad5686_write() and ad5686_read() are created and ad5686_probe() now receives an ops struct pointer rather than individual read and write functions.

---
 drivers/iio/dac/ad5686-spi.c |  7 ++++++-
 drivers/iio/dac/ad5686.c     | 30 ++++++++++++++----------------
 drivers/iio/dac/ad5686.h     | 29 +++++++++++++++++++++--------
 drivers/iio/dac/ad5696-i2c.c |  7 ++++++-
 4 files changed, 47 insertions(+), 26 deletions(-)
drivers/iio/dac/ad5686-spi.c +6 -1
index 8fef0e6d33ff..857b9e4f54ab 100644
@@ -92,10 +92,15 @@ static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
 	return be32_to_cpu(st->data[2].d32);
 }
 
+static const struct ad5686_bus_ops ad5686_spi_ops = {
+	.write = ad5686_spi_write,
+	.read = ad5686_spi_read,
+};
+
 static int ad5686_spi_probe(struct spi_device *spi)
 {
 	return ad5686_probe(&spi->dev, spi_get_device_match_data(spi),
-			    spi->modalias, ad5686_spi_write, ad5686_spi_read);
+			    spi->modalias, &ad5686_spi_ops);
 }
 
 static const struct spi_device_id ad5686_spi_id[] = {
drivers/iio/dac/ad5686.c +14 -16
index ab498ec2ed5c..490faed85976 100644
@@ -26,18 +26,18 @@ static int ad5310_control_sync(struct ad5686_state *st)
 {
 	unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
 
-	return st->write(st, AD5686_CMD_CONTROL_REG, 0,
-			 FIELD_PREP(AD5310_PD_MSK, pd_val) |
-			 FIELD_PREP(AD5310_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
+	return ad5686_write(st, AD5686_CMD_CONTROL_REG, 0,
+			    FIELD_PREP(AD5310_PD_MSK, pd_val) |
+			    FIELD_PREP(AD5310_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
 }
 
 static int ad5683_control_sync(struct ad5686_state *st)
 {
 	unsigned int pd_val = st->pwr_down_mask & st->pwr_down_mode;
 
-	return st->write(st, AD5686_CMD_CONTROL_REG, 0,
-			 FIELD_PREP(AD5683_PD_MSK, pd_val) |
-			 FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
+	return ad5686_write(st, AD5686_CMD_CONTROL_REG, 0,
+			    FIELD_PREP(AD5683_PD_MSK, pd_val) |
+			    FIELD_PREP(AD5683_REF_BIT_MSK, st->use_internal_vref ? 0 : 1));
 }
 
 static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan)
@@ -136,7 +136,7 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 			address = 0x0;
 			val = lower_16_bits(val);
 		}
-		ret = st->write(st, AD5686_CMD_POWERDOWN_DAC, address, val);
+		ret = ad5686_write(st, AD5686_CMD_POWERDOWN_DAC, address, val);
 		if (ret)
 			return ret;
 		break;
@@ -160,7 +160,7 @@ static int ad5686_read_raw(struct iio_dev *indio_dev,
 
 	switch (m) {
 	case IIO_CHAN_INFO_RAW:
-		ret = st->read(st, chan->address);
+		ret = ad5686_read(st, chan->address);
 		if (ret < 0)
 			return ret;
 		*val = (ret >> chan->scan_type.shift) &
@@ -189,8 +189,8 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
 		if (!in_range(val, 0, 1 << chan->scan_type.realbits))
 			return -EINVAL;
 
-		return st->write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
-				 chan->address, val << chan->scan_type.shift);
+		return ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
+				    chan->address, val << chan->scan_type.shift);
 	default:
 		return -EINVAL;
 	}
@@ -447,8 +447,7 @@ EXPORT_SYMBOL_NS_GPL(ad5679r_chip_info, "IIO_AD5686");
 
 int ad5686_probe(struct device *dev,
 		 const struct ad5686_chip_info *chip_info,
-		 const char *name, ad5686_write_func write,
-		 ad5686_read_func read)
+		 const char *name, const struct ad5686_bus_ops *ops)
 {
 	struct iio_dev *indio_dev;
 	struct ad5686_state *st;
@@ -462,8 +461,7 @@ int ad5686_probe(struct device *dev,
 	st = iio_priv(indio_dev);
 
 	st->dev = dev;
-	st->write = write;
-	st->read = read;
+	st->ops = ops;
 	st->chip_info = chip_info;
 
 	ret = devm_regulator_get_enable_read_voltage(dev, "vcc");
@@ -505,8 +503,8 @@ int ad5686_probe(struct device *dev,
 			return ret;
 		break;
 	case AD5686_REGMAP:
-		ret = st->write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
-				st->use_internal_vref ? 0 : 1);
+		ret = ad5686_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
+				   st->use_internal_vref ? 0 : 1);
 		if (ret)
 			return ret;
 		break;
drivers/iio/dac/ad5686.h +21 -8
index 8043cb4ab3cf..679831881ae2 100644
@@ -56,10 +56,15 @@ enum ad5686_regmap_type {
 
 struct ad5686_state;
 
-typedef int (*ad5686_write_func)(struct ad5686_state *st,
-				 u8 cmd, u8 addr, u16 val);
-
-typedef int (*ad5686_read_func)(struct ad5686_state *st, u8 addr);
+/**
+ * struct ad5686_bus_ops - bus specific read/write operations
+ * @read: read a register value at the given address
+ * @write: write a command, address and value to the device
+ */
+struct ad5686_bus_ops {
+	int (*read)(struct ad5686_state *st, u8 addr);
+	int (*write)(struct ad5686_state *st, u8 cmd, u8 addr, u16 val);
+};
 
 /**
  * struct ad5686_chip_info - chip specific information
@@ -107,6 +112,7 @@ extern const struct ad5686_chip_info ad5679r_chip_info;
  * struct ad5686_state - driver instance specific data
  * @dev:		device instance
  * @chip_info:		chip model specific constants, available modes etc
+ * @ops:		bus specific operations
  * @vref_mv:		actual reference voltage used
  * @pwr_down_mask:	power down mask
  * @pwr_down_mode:	current power down mode
@@ -118,11 +124,10 @@ extern const struct ad5686_chip_info ad5679r_chip_info;
 struct ad5686_state {
 	struct device			*dev;
 	const struct ad5686_chip_info	*chip_info;
+	const struct ad5686_bus_ops	*ops;
 	unsigned short			vref_mv;
 	unsigned int			pwr_down_mask;
 	unsigned int			pwr_down_mode;
-	ad5686_write_func		write;
-	ad5686_read_func		read;
 	bool				use_internal_vref;
 	struct mutex			lock;
 
@@ -141,8 +146,16 @@ struct ad5686_state {
 
 int ad5686_probe(struct device *dev,
 		 const struct ad5686_chip_info *chip_info,
-		 const char *name, ad5686_write_func write,
-		 ad5686_read_func read);
+		 const char *name, const struct ad5686_bus_ops *ops);
 
+static inline int ad5686_write(struct ad5686_state *st, u8 cmd, u8 addr, u16 val)
+{
+	return st->ops->write(st, cmd, addr, val);
+}
+
+static inline int ad5686_read(struct ad5686_state *st, u8 addr)
+{
+	return st->ops->read(st, addr);
+}
 
 #endif /* __DRIVERS_IIO_DAC_AD5686_H__ */
drivers/iio/dac/ad5696-i2c.c +6 -1
index fd047c01f8f6..30c48d937a38 100644
@@ -62,10 +62,15 @@ static int ad5686_i2c_write(struct ad5686_state *st,
 	return (ret != 3) ? -EIO : 0;
 }
 
+static const struct ad5686_bus_ops ad5686_i2c_ops = {
+	.write = ad5686_i2c_write,
+	.read = ad5686_i2c_read,
+};
+
 static int ad5686_i2c_probe(struct i2c_client *i2c)
 {
 	return ad5686_probe(&i2c->dev, i2c_get_match_data(i2c),
-			    i2c->name, ad5686_i2c_write, ad5686_i2c_read);
+			    i2c->name, &ad5686_i2c_ops);
 }
 
 static const struct i2c_device_id ad5686_i2c_id[] = {
Signature · 1 line
2.43.0
maintainer
#22 lore raw

Re: [PATCH 10/10] iio: dac: ad5686: create bus ops struct

Quoted 8 lines · On Sun, 26 Apr 2026 09:38:11 +0100 Rodrigo Alencar via B4 Relay…

From: Rodrigo Alencar <[email protected]>

Create struct with bus operations, which will be used to extend bus implementation features. Auxiliary functions ad5686_write() and ad5686_read() are created and ad5686_probe() now receives an ops struct pointer rather than individual read and write functions.

LGTM

Jonathan