Skip to contentSkip to conversation

Fixes and cleanups for the AD5686 IIO driver

PATCH Applied Rodrigo Alencar 4 participants 29 messages May 5 – May 17

Subsystems (2)

v6 of 7 Applied Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 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"):

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;

---
Changes in v6:
- Protect powerdown masks on read access too.
- Minor changes to power down handling and constants.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Drop I2C read operation changes.
- Create helpers for reading and writing powerdown mask bits
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Address issues spotted by sashiko.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Misc changes like parenthesis removal and line breaks
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Bring fixes first and cleanups later
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Rodrigo Alencar (12):
iio: dac: ad5686: fix ref bit initialization for single-channel parts
iio: dac: ad5686: fix input raw value check
iio: dac: ad5686: acquire lock when doing powerdown control
iio: dac: ad5686: fix powerdown control on dual-channel devices
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: add helpers to handle powerdown masks
iio: dac: ad5686: add control_sync() for single-channel devices
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     | 542 ++++++++++++++++++++++---------------------
 drivers/iio/dac/ad5686.h     | 132 ++++++-----
 drivers/iio/dac/ad5696-i2c.c |  80 ++++---
 4 files changed, 438 insertions(+), 389 deletions(-)

---
base-commit: 39b80c5c9830d12d2d6531059001301c4265322a
change-id: 20260426-ad5686-fixes-63ea68811bdb

Best regards,

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

PATCH v6 1/12 iio: dac: ad5686: fix ref bit initialization for single-channel parts

The reference bit position was ignored when writing the register at the probe() function (!!val was used). When such bit is 1, internal voltage reference is disabled so that an external one can be used. For multi-channel devices, bit 0 of the Internal Reference Setup command behaves the same way, so AD5686_REF_BIT_MSK is created. The issue exists since support for single-channel devices were first introduced.

Fixes be1b24d24541 ("iio:dac:ad5686: Add AD5691R/AD5692R/AD5693/AD5693R support")
---
 drivers/iio/dac/ad5686.c | 6 +++---
 drivers/iio/dac/ad5686.h | 1 +
 2 files changed, 4 insertions(+), 3 deletions(-)
drivers/iio/dac/ad5686.c +3 -3
index 9a384c50929b..1fc54c38b629 100644
@@ -511,7 +511,7 @@ int ad5686_probe(struct device *dev,
 		break;
 	case AD5686_REGMAP:
 		cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
-		ref_bit_msk = 0;
+		ref_bit_msk = AD5686_REF_BIT_MSK;
 		break;
 	case AD5693_REGMAP:
 		cmd = AD5686_CMD_CONTROL_REG;
@@ -522,9 +522,9 @@ int ad5686_probe(struct device *dev,
 		return -EINVAL;
 	}
 
-	val = (has_external_vref | ref_bit_msk);
+	val = has_external_vref ? ref_bit_msk : 0;
 
-	ret = st->write(st, cmd, 0, !!val);
+	ret = st->write(st, cmd, 0, val);
 	if (ret)
 		return ret;
 
drivers/iio/dac/ad5686.h +1 -0
index e7d36bae3e59..36e16c5c4581 100644
@@ -46,6 +46,7 @@
 
 #define AD5310_REF_BIT_MSK			BIT(8)
 #define AD5683_REF_BIT_MSK			BIT(12)
+#define AD5686_REF_BIT_MSK			BIT(0)
 #define AD5693_REF_BIT_MSK			BIT(12)
 
 /**
Signature · 1 line
2.43.0
#2 lore raw

PATCH v6 2/12 iio: dac: ad5686: fix input raw value check

In reply to Rodrigo Alencar

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 1fc54c38b629..076fe8b8bd85 100644
@@ -154,7 +154,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 (val >= (1 << chan->scan_type.realbits) || val < 0)
 			return -EINVAL;
 
 		mutex_lock(&st->lock);
Signature · 1 line
2.43.0
#3 lore raw

PATCH v6 3/12 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. Each channel exposes their own attributes for controlling powerdown modes and powerdown state. This fixes potential race conditions as those the write functions perform non-atomic read-modify-write operations to those pwr_down_* fields. This 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iio/dac/ad5686.c +8 -0
index 076fe8b8bd85..9eb6b61c471d 100644
@@ -30,6 +30,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;
 }
 
@@ -39,6 +41,8 @@ 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));
 
@@ -57,6 +61,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))));
 }
@@ -77,6 +83,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
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v6 03/12] iio: dac: ad5686: acquire lock when doing powerdown control

Quoted 6 lines · On Tue, May 05, 2026 at 01:35:04PM +0100, Rodrigo Alencar via B4 Relay wrote:

Protect access of pwr_down_mode and pwr_down_mask fields with existing mutex lock. Each channel exposes their own attributes for controlling powerdown modes and powerdown state. This fixes potential race conditions as those the write functions perform non-atomic read-modify-write operations to those pwr_down_* fields. This issue exists since the ad5686 driver was first introduced.

...

drivers/iio/dac/ad5686.c static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev, · line 67

Quoted 8 lines

static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
{

drivers/iio/dac/ad5686.c +2 -0
 	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))));

Why not use ad5686_get_powerdown_mode() instead?

Quoted 1 line

}

...

drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 90

Quoted 9 lines

static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,

drivers/iio/dac/ad5686.c +2 -0
 	if (ret)
 		return ret;
 
+	guard(mutex)(&st->lock);
+
 	if (readin)
 		st->pwr_down_mask |= (0x3 << (chan->channel * 2));
 	else

Similar question here...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v6 03/12] iio: dac: ad5686: acquire lock when doing powerdown control

Quoted 15 lines · Andy Shevchenko
Quoted 6 lines · On Tue, May 05, 2026 at 01:35:04PM +0100, Rodrigo Alencar via B4 Relay wrote:

Protect access of pwr_down_mode and pwr_down_mask fields with existing mutex lock. Each channel exposes their own attributes for controlling powerdown modes and powerdown state. This fixes potential race conditions as those the write functions perform non-atomic read-modify-write operations to those pwr_down_* fields. This issue exists since the ad5686 driver was first introduced.

...

Quoted 4 lines

static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
{
struct ad5686_state *st = iio_priv(indio_dev);

Quoted 6 lines

drivers/iio/dac/ad5686.c static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev, · line 67

Quoted 4 lines
drivers/iio/dac/ad5686.c +2 -0
+	guard(mutex)(&st->lock);
+
 	return sysfs_emit(buf, "%d\n", !!(st->pwr_down_mask &
 				       (0x3 << (chan->channel * 2))));

Why not use ad5686_get_powerdown_mode() instead?

powerdown mode != powerdown mask. The powerdown mask controls the on-off behavior, while the mode controls the possible values for when it is off.

Quoted 8 lines
Quoted 1 line

}

...

Quoted 4 lines

static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,

drivers/iio/dac/ad5686.c +0 -0
 	if (ret)
 		return ret;
Quoted 7 lines

drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 90

Quoted 5 lines
drivers/iio/dac/ad5686.c +2 -0
+	guard(mutex)(&st->lock);
+
 	if (readin)
 		st->pwr_down_mask |= (0x3 << (chan->channel * 2));
 	else

Similar question here...

I suppose the above applies here as well.

Quoted 5 lines
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 3 lines
Kind regards,

Rodrigo Alencar
#6 lore raw

Re: [PATCH v6 03/12] iio: dac: ad5686: acquire lock when doing powerdown control

Quoted 2 lines · Rodrigo Alencar
Quoted 1 line · Andy Shevchenko

On Tue, May 05, 2026 at 01:35:04PM +0100, Rodrigo Alencar via B4 Relay wrote:

...

Quoted 11 lines
Quoted 10 lines

drivers/iio/dac/ad5686.c static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev, · line 67

Quoted 8 lines

static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,
{

drivers/iio/dac/ad5686.c +2 -0
 	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))));

Why not use ad5686_get_powerdown_mode() instead?

Quoted 2 lines

powerdown mode != powerdown mask. The powerdown mask controls the on-off behavior, while the mode controls the possible values for when it is off.

Ah, unfortunate naming collision (easy to mix up them).

Quoted 1 line
Quoted 1 line
Quoted 1 line

}

...

Quoted 12 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 90

Quoted 9 lines

static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,

drivers/iio/dac/ad5686.c +2 -0
 	if (ret)
 		return ret;
 
+	guard(mutex)(&st->lock);
+
 	if (readin)
 		st->pwr_down_mask |= (0x3 << (chan->channel * 2));
 	else

Similar question here...

Quoted 1 line

I suppose the above applies here as well.

Yep, thanks for clarification!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#7 lore raw

Re: [PATCH v6 03/12] iio: dac: ad5686: acquire lock when doing powerdown control

In reply to Rodrigo Alencar
Quoted 11 lines · On Tue, 05 May 2026 13:35:04 +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. Each channel exposes their own attributes for controlling powerdown modes and powerdown state. This fixes potential race conditions as those the write functions perform non-atomic read-modify-write operations to those pwr_down_* fields. This issue exists since the ad5686 driver was first introduced.

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

Applied to the fixes-togreg branch of iio.git (1 and 2 already on there)

Marked for stable.

#8 lore raw

PATCH v6 4/12 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. Mask as 0x3U is used rather than 0x3, because shift can reach value of 30 (last channel of a 16-channel device), which would mess with the sign bit. 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 | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/iio/dac/ad5686.c +30 -10
index 9eb6b61c471d..19440b17645b 100644
@@ -25,26 +25,37 @@ static const char * const ad5686_powerdown_modes[] = {
 	"three_state"
 };
 
+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) & 0x3U) - 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 &= ~(0x3U << shift);
+	st->pwr_down_mode |= (mode + 1) << shift;
 
 	return 0;
 }
@@ -59,12 +70,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 & (0x3U << shift)));
 }
 
 static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
@@ -86,9 +97,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 |= 0x3U << ad5686_pd_mask_shift(chan);
 	else
-		st->pwr_down_mask &= ~(0x3 << (chan->channel * 2));
+		st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan));
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
@@ -468,7 +479,7 @@ int ad5686_probe(struct device *dev,
 {
 	struct ad5686_state *st;
 	struct iio_dev *indio_dev;
-	unsigned int val, ref_bit_msk;
+	unsigned int val, ref_bit_msk, shift;
 	bool has_external_vref;
 	u8 cmd;
 	int ret, i;
@@ -492,9 +503,18 @@ int ad5686_probe(struct device *dev,
 	has_external_vref = ret != -ENODEV;
 	st->vref_mv = has_external_vref ? ret / 1000 : st->chip_info->int_vref_mv;
 
+	/* Initialize masks to all ones provided the max shift (last channel) */
+	shift = ad5686_pd_mask_shift(&st->chip_info->channels[st->chip_info->num_channels - 1]);
+	st->pwr_down_mask = GENMASK(shift + 1, 0);
+	st->pwr_down_mode = GENMASK(shift + 1, 0);
+
 	/* 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));
+	for (i = 0; i < st->chip_info->num_channels; i++) {
+		shift = ad5686_pd_mask_shift(&st->chip_info->channels[i]);
+		st->pwr_down_mask &= ~(0x3U << shift); /* powered up state */
+		st->pwr_down_mode &= ~(0x3U << shift);
+		st->pwr_down_mode |= 0x01U << shift;
+	}
 
 	indio_dev->name = name;
 	indio_dev->info = &ad5686_info;
Signature · 1 line
2.43.0
maintainer Applied
#9 lore raw

Re: [PATCH v6 04/12] iio: dac: ad5686: fix powerdown control on dual-channel devices

Quoted 14 lines · On Tue, 05 May 2026 13:35:05 +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. Mask as 0x3U is used rather than 0x3, because shift can reach value of 30 (last channel of a 16-channel device), which would mess with the sign bit. The issue was introduced when first adding support for dual-channel devices, which overlooked powerdown control differences.

Quoted 2 lines
Fixes 7dc8faeab3e3 ("iio: dac: ad5686: add support for AD5338R")

Applied to the fixes-togreg branch of iio.git and marked for stable.

The rest of the series will have to wait for these to be upstream. If I ever catch up with reviews and tests are fine I'll get a pull request out in next few days for that.

Thanks,

Jonathan

#10 lore raw

PATCH v6 5/12 iio: dac: ad5686: refactor include headers

In reply to Rodrigo Alencar

Apply IWYU principle, replacing unused/generic headers for specific/missing headers. The resulting include directive lists are 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, 22 insertions(+), 15 deletions(-)
drivers/iio/dac/ad5686-spi.c +7 -2
index df8619e0c092..cec784b617e6 100644
@@ -8,11 +8,16 @@
  * Copyright 2018 Analog Devices Inc.
  */
 
-#include "ad5686.h"
-
+#include <linux/array_size.h>
+#include <linux/errno.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 +6 -7
index 19440b17645b..6f403b68cbec 100644
@@ -5,17 +5,16 @@
  * Copyright 2011 Analog Devices Inc.
  */
 
-#include <linux/interrupt.h>
-#include <linux/fs.h>
-#include <linux/device.h>
+#include <linux/array_size.h>
+#include <linux/bitops.h>
+#include <linux/errno.h>
+#include <linux/export.h>
+#include <linux/kstrtox.h>
 #include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/sysfs.h>
 #include <linux/regulator/consumer.h>
+#include <linux/sysfs.h>
 
 #include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
 
 #include "ad5686.h"
 
drivers/iio/dac/ad5686.h +2 -3
index 36e16c5c4581..d08160e7fad9 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..20f04b74d831 100644
@@ -7,10 +7,14 @@
  * Copyright 2018 Analog Devices Inc.
  */
 
-#include "ad5686.h"
-
-#include <linux/module.h>
+#include <linux/errno.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
#11 lore raw

PATCH v6 6/12 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 6f403b68cbec..1df1145f35b3 100644
@@ -116,10 +116,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;
 	}
@@ -300,7 +296,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,
@@ -422,24 +418,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,
@@ -540,11 +536,6 @@ int ad5686_probe(struct device *dev,
 		cmd = AD5686_CMD_INTERNAL_REFER_SETUP;
 		ref_bit_msk = AD5686_REF_BIT_MSK;
 		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 d08160e7fad9..af15994c01ad 100644
@@ -46,7 +46,6 @@
 #define AD5310_REF_BIT_MSK			BIT(8)
 #define AD5683_REF_BIT_MSK			BIT(12)
 #define AD5686_REF_BIT_MSK			BIT(0)
-#define AD5693_REF_BIT_MSK			BIT(12)
 
 /**
  * ad5686_supported_device_ids:
@@ -89,7 +88,6 @@ enum ad5686_regmap_type {
 	AD5310_REGMAP,
 	AD5683_REGMAP,
 	AD5686_REGMAP,
-	AD5693_REGMAP
 };
 
 struct ad5686_state;
Signature · 1 line
2.43.0
#12 lore raw

PATCH v6 7/12 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 core 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 cec784b617e6..5cf60a6e19d1 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
#13 lore raw

PATCH v6 8/12 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 5cf60a6e19d1..9493511d9e7f 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
#14 lore raw

PATCH v6 9/12 iio: dac: ad5686: add helpers to handle powerdown masks

In reply to Rodrigo Alencar

Add ad5686_pd_field_set() and ad5686_pd_field_get() helpers to cleanup powerdown mask control. Define AD5686_PD_* constants, e.g. AD5686_PD_MSK to hold powerdown mask value for a single channel. AD5686_LDAC_PWRDN_* macros are replaced by AD5686_PD_MODE_*, because they are unused and the LDAC feature for async load of DAC channel values is not related to power down control.

---
 drivers/iio/dac/ad5686.c | 42 ++++++++++++++++++++++++++----------------
 drivers/iio/dac/ad5686.h | 13 ++++++++-----
 2 files changed, 34 insertions(+), 21 deletions(-)
drivers/iio/dac/ad5686.c +26 -16
index 431e7650704e..222f1696affe 100644
@@ -33,28 +33,40 @@ static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan
 	return __ffs(chan->address) * 2;
 }
 
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;
+}
+
 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 >> shift) & 0x3U) - 1;
+	return ad5686_pd_field_get(chan, st->pwr_down_mode) - 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 &= ~(0x3U << shift);
-	st->pwr_down_mode |= (mode + 1) << shift;
+	ad5686_pd_field_set(chan, &st->pwr_down_mode, mode + 1);
 
 	return 0;
 }
@@ -69,12 +81,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 & (0x3U << shift)));
+	return sysfs_emit(buf, "%d\n",
+			  !!ad5686_pd_field_get(chan, st->pwr_down_mask));
 }
 
 static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
@@ -95,10 +107,8 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
 
 	guard(mutex)(&st->lock);
 
-	if (readin)
-		st->pwr_down_mask |= 0x3U << ad5686_pd_mask_shift(chan);
-	else
-		st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan));
+	ad5686_pd_field_set(chan, &st->pwr_down_mask,
+			    readin ? AD5686_PD_MSK_PWR_DOWN : AD5686_PD_MSK_PWR_UP);
 
 	switch (st->chip_info->regmap_type) {
 	case AD5310_REGMAP:
@@ -471,10 +481,10 @@ int ad5686_probe(struct device *dev,
 
 	/* Set all the power down mode for all channels to 1K pulldown */
 	for (i = 0; i < st->chip_info->num_channels; i++) {
-		shift = ad5686_pd_mask_shift(&st->chip_info->channels[i]);
-		st->pwr_down_mask &= ~(0x3U << shift); /* powered up state */
-		st->pwr_down_mode &= ~(0x3U << shift);
-		st->pwr_down_mode |= 0x01U << shift;
+		ad5686_pd_field_set(&st->chip_info->channels[i],
+				    &st->pwr_down_mask, AD5686_PD_MSK_PWR_UP);
+		ad5686_pd_field_set(&st->chip_info->channels[i],
+				    &st->pwr_down_mode, AD5686_PD_MODE_1K_TO_GND);
 	}
 
 	indio_dev->name = name;
drivers/iio/dac/ad5686.h +8 -5
index caadc7403da1..176d41966985 100644
@@ -35,11 +35,6 @@
 #define AD5686_CMD_DAISY_CHAIN_ENABLE		0x8
 #define AD5686_CMD_READBACK_ENABLE		0x9
 
-#define AD5686_LDAC_PWRDN_NONE			0x0
-#define AD5686_LDAC_PWRDN_1K			0x1
-#define AD5686_LDAC_PWRDN_100K			0x2
-#define AD5686_LDAC_PWRDN_3STATE		0x3
-
 #define AD5686_CMD_CONTROL_REG			0x4
 #define AD5686_CMD_READBACK_ENABLE_V2		0x5
 
@@ -47,6 +42,14 @@
 #define AD5683_REF_BIT_MSK			BIT(12)
 #define AD5686_REF_BIT_MSK			BIT(0)
 
+#define AD5686_PD_MSK				GENMASK(1, 0)
+
+#define AD5686_PD_MODE_1K_TO_GND		0x1
+#define AD5686_PD_MODE_100K_TO_GND		0x2
+#define AD5686_PD_MODE_THREE_STATE		0x3
+
+#define AD5686_PD_MSK_PWR_UP			0x0
+#define AD5686_PD_MSK_PWR_DOWN			0x3
 
 enum ad5686_regmap_type {
 	AD5310_REGMAP,
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 6 lines · On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

Add ad5686_pd_field_set() and ad5686_pd_field_get() helpers to cleanup powerdown mask control. Define AD5686_PD_* constants, e.g. AD5686_PD_MSK to hold powerdown mask value for a single channel. AD5686_LDAC_PWRDN_* macros are replaced by AD5686_PD_MODE_*, because they are unused and the LDAC feature for async load of DAC channel values is not related to power down control.

...

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 41

Quoted 6 lines
drivers/iio/dac/ad5686.c +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 49

Quoted 8 lines
drivers/iio/dac/ad5686.c +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;
	return (pd & AD5686_PD_MSK) >> shift;

accordingly.

Quoted 1 line

+}

...

drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 111

Quoted 6 lines
drivers/iio/dac/ad5686.c +2 -4
-	if (readin)
-		st->pwr_down_mask |= 0x3U << ad5686_pd_mask_shift(chan);
-	else
-		st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan));
+	ad5686_pd_field_set(chan, &st->pwr_down_mask,
+			    readin ? AD5686_PD_MSK_PWR_DOWN : AD5686_PD_MSK_PWR_UP);

TBH, I would leave the if-else untouched, only branches to change.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 22 lines · Andy Shevchenko
Quoted 6 lines · On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

Add ad5686_pd_field_set() and ad5686_pd_field_get() helpers to cleanup powerdown mask control. Define AD5686_PD_* constants, e.g. AD5686_PD_MSK to hold powerdown mask value for a single channel. AD5686_LDAC_PWRDN_* macros are replaced by AD5686_PD_MODE_*, because they are unused and the LDAC feature for async load of DAC channel values is not related to power down control.

...

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 41

Quoted 6 lines
drivers/iio/dac/ad5686.c +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

Quoted 1 line
	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);

We cannot do this as the mask would depend on the shift too. AD5686_PD_MSK is being defined with no shift, so that any shift needs to be applied at runtime.

Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 49

Quoted 8 lines
drivers/iio/dac/ad5686.c +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;
	return (pd & AD5686_PD_MSK) >> shift;
Quoted 1 line

accordingly.

Same here...

Quoted 4 lines
Quoted 1 line

+}

...

Quoted 8 lines

drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 111

Quoted 6 lines
drivers/iio/dac/ad5686.c +2 -4
-	if (readin)
-		st->pwr_down_mask |= 0x3U << ad5686_pd_mask_shift(chan);
-	else
-		st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan));
+	ad5686_pd_field_set(chan, &st->pwr_down_mask,
+			    readin ? AD5686_PD_MSK_PWR_DOWN : AD5686_PD_MSK_PWR_UP);

TBH, I would leave the if-else untouched, only branches to change.

What would be the difference compared to this:

	if (readin)
		ad5686_pd_field_set(chan, &st->pwr_down_mask, AD5686_PD_MSK_PWR_DOWN);
	else
		ad5686_pd_field_set(chan, &st->pwr_down_mask, AD5686_PD_MSK_PWR_UP);
Quoted 5 lines
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 3 lines
Kind regards,

Rodrigo Alencar
#17 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 2 lines · Rodrigo Alencar
Quoted 1 line · Andy Shevchenko

On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

...

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 41

Quoted 6 lines
drivers/iio/dac/ad5686.c +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

Quoted 4 lines
Quoted 1 line
	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);

We cannot do this as the mask would depend on the shift too. AD5686_PD_MSK is being defined with no shift, so that any shift needs to be applied at runtime.

In my proposal I thought of

#define AD5686_PD_MSK(shift) (GENMASK(...) << (shift))

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 49

Quoted 8 lines
drivers/iio/dac/ad5686.c +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;
	return (pd & AD5686_PD_MSK) >> shift;
Quoted 3 lines
Quoted 1 line

accordingly.

Same here...

But maybe it's over engineered, and your version is okay... Maybe we can even switch to field_get()/field_prep()/u32_replace_bits()/u32_encode_bits()/u32_get_bits() from bitfield.h.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

...

Quoted 9 lines
Quoted 8 lines

drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 111

Quoted 6 lines
drivers/iio/dac/ad5686.c +2 -4
-	if (readin)
-		st->pwr_down_mask |= 0x3U << ad5686_pd_mask_shift(chan);
-	else
-		st->pwr_down_mask &= ~(0x3U << ad5686_pd_mask_shift(chan));
+	ad5686_pd_field_set(chan, &st->pwr_down_mask,
+			    readin ? AD5686_PD_MSK_PWR_DOWN : AD5686_PD_MSK_PWR_UP);

TBH, I would leave the if-else untouched, only branches to change.

Quoted 6 lines

What would be the difference compared to this:

	if (readin)
		ad5686_pd_field_set(chan, &st->pwr_down_mask, AD5686_PD_MSK_PWR_DOWN);
	else
		ad5686_pd_field_set(chan, &st->pwr_down_mask, AD5686_PD_MSK_PWR_UP);

That's what I thought about...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 37 lines · Andy Shevchenko
Quoted 2 lines · Rodrigo Alencar
Quoted 1 line · Andy Shevchenko

On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

...

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 41

Quoted 6 lines
drivers/iio/dac/ad5686.c +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

Quoted 4 lines
Quoted 1 line
	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);

We cannot do this as the mask would depend on the shift too. AD5686_PD_MSK is being defined with no shift, so that any shift needs to be applied at runtime.

In my proposal I thought of

#define AD5686_PD_MSK(shift) (GENMASK(...) << (shift))

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 49

Quoted 8 lines
drivers/iio/dac/ad5686.c +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;
	return (pd & AD5686_PD_MSK) >> shift;
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

accordingly.

Same here...

But maybe it's over engineered, and your version is okay... Maybe we can even switch to field_get()/field_prep()/u32_replace_bits()/u32_encode_bits()/u32_get_bits() from bitfield.h.

That is indeed better, thanks! will adjust...

Signature · 3 lines
Kind regards,

Rodrigo Alencar
#19 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 40 lines · Rodrigo Alencar
Quoted 39 lines · Andy Shevchenko
Quoted 2 lines · Rodrigo Alencar
Quoted 1 line · Andy Shevchenko

On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

...

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 41

Quoted 6 lines
drivers/iio/dac/ad5686.c +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

Quoted 4 lines
Quoted 1 line
	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);

We cannot do this as the mask would depend on the shift too. AD5686_PD_MSK is being defined with no shift, so that any shift needs to be applied at runtime.

In my proposal I thought of

#define AD5686_PD_MSK(shift) (GENMASK(...) << (shift))

Quoted 13 lines
Quoted 12 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 49

Quoted 8 lines
drivers/iio/dac/ad5686.c +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;
	return (pd & AD5686_PD_MSK) >> shift;

accordingly.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Same here...

But maybe it's over engineered, and your version is okay... Maybe we can even switch to field_get()/field_prep()/u32_replace_bits()/u32_encode_bits()/u32_get_bits() from bitfield.h.

That is indeed better, thanks! will adjust...

Now I see that the "over engineering" is that field_get()/field_prep() recomputes the shift based on the provided mask, which was manually shifted. The u32_*_bits() variants seem to rely on constant masks, as I get compile-time errors.

Signature · 3 lines
Kind regards,

Rodrigo Alencar
maintainer
#20 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 48 lines · On Tue, 5 May 2026 17:31:48 +0100 Rodrigo Alencar…
Quoted 47 lines · Rodrigo Alencar
Quoted 45 lines · Andy Shevchenko
Quoted 2 lines · Rodrigo Alencar
Quoted 1 line · Andy Shevchenko

On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

...

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 41

Quoted 6 lines
drivers/iio/dac/ad5686.c +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);  

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

Quoted 4 lines
Quoted 1 line
	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);  

We cannot do this as the mask would depend on the shift too. AD5686_PD_MSK is being defined with no shift, so that any shift needs to be applied at runtime.

In my proposal I thought of

#define AD5686_PD_MSK(shift) (GENMASK(...) << (shift))

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5686.c static inline unsigned int ad5686_pd_mask_shift(const struct iio_chan_spec *chan · line 49

Quoted 8 lines
drivers/iio/dac/ad5686.c +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;  
	return (pd & AD5686_PD_MSK) >> shift;
Quoted 3 lines
Quoted 1 line

accordingly.

Same here...

But maybe it's over engineered, and your version is okay... Maybe we can even switch to field_get()/field_prep()/u32_replace_bits()/u32_encode_bits()/u32_get_bits() from bitfield.h.

Quoted 5 lines
Quoted 1 line

That is indeed better, thanks! will adjust...

Now I see that the "over engineering" is that field_get()/field_prep() recomputes the shift based on the provided mask, which was manually shifted. The u32_*_bits() variants seem to rely on constant masks, as I get compile-time errors.

I still have these stalled on the fixes making it up stream (and into an rc) but in meantime can you confirm that I read this right and plan is leave this patch as it stands?

Thanks,

Jonathan

Quoted 1 line
#21 lore raw

Re: [PATCH v6 09/12] iio: dac: ad5686: add helpers to handle powerdown masks

Quoted 53 lines · Jonathan Cameron
Quoted 50 lines · On Tue, 5 May 2026 17:31:48 +0100 Rodrigo Alencar…
Quoted 48 lines · Rodrigo Alencar
Quoted 37 lines · Andy Shevchenko
Quoted 2 lines · Rodrigo Alencar
Quoted 1 line · Andy Shevchenko

On Tue, May 05, 2026 at 01:35:10PM +0100, Rodrigo Alencar via B4 Relay wrote:

...

Quoted 11 lines
Quoted 11 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static inline void ad5686_pd_field_set(const struct iio_chan_spec *chan,
+				       unsigned int *pd, unsigned int val)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	*pd = (*pd & ~(AD5686_PD_MSK << shift)) | ((val & AD5686_PD_MSK) << shift);  

Just noticed that semantically this is more like _field_modify(). Besides that I would consider adding a shifted mask variable or definition

Quoted 4 lines
Quoted 1 line
	*pd = (*pd & ~AD5686_PD_MSK) | ((val << shift) & AD5686_PD_MSK);  

We cannot do this as the mask would depend on the shift too. AD5686_PD_MSK is being defined with no shift, so that any shift needs to be applied at runtime.

In my proposal I thought of

#define AD5686_PD_MSK(shift) (GENMASK(...) << (shift))

Quoted 11 lines
Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+}
+
+static inline unsigned int ad5686_pd_field_get(const struct iio_chan_spec *chan,
+					       unsigned int pd)
+{
+	unsigned int shift = ad5686_pd_mask_shift(chan);
+
+	return (pd >> shift) & AD5686_PD_MSK;  
	return (pd & AD5686_PD_MSK) >> shift;
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

accordingly.

Same here...

But maybe it's over engineered, and your version is okay... Maybe we can even switch to field_get()/field_prep()/u32_replace_bits()/u32_encode_bits()/u32_get_bits() from bitfield.h.

That is indeed better, thanks! will adjust...

Quoted 7 lines
Quoted 3 lines

Now I see that the "over engineering" is that field_get()/field_prep() recomputes the shift based on the provided mask, which was manually shifted. The u32_*_bits() variants seem to rely on constant masks, as I get compile-time errors.

I still have these stalled on the fixes making it up stream (and into an rc) but in meantime can you confirm that I read this right and plan is leave this patch as it stands?

Yeah, I suppose we would not need to use field_get()/field_prep() because they recompute the shift based on the mask, but we already create the mask from the shift. I think the helpers are clear enough and there is no real benefit on the change.

Signature · 3 lines
Kind regards,

Rodrigo Alencar
#22 lore raw

PATCH v6 10/12 iio: dac: ad5686: add control_sync() for single-channel devices

In reply to Rodrigo Alencar

Create ad5310_control_sync() and ad5683_control_sync() functions that properly consume the mask definitions with FIELD_PREP(). This allows to reuse a function that updates the control register with cached values, without relying on confusing logic that depends on st->use_internal_vref, which is initialized earlier in ad5686_probe() because it is also applicable to the AD5686_REGMAP case, removing the need for the has_external_vref. Powerdown masks initialization is simplified as *_control_sync() masks outs any unused bits for the single-channel case. The change cleans up ad5686_write_dac_powerdown() and ad5686_probe(), organizing the code for feature extension, e.g. gain control support for single-channel devices.

---
 drivers/iio/dac/ad5686.c | 94 +++++++++++++++++++++++++++---------------------
 drivers/iio/dac/ad5686.h |  7 ++--
 2 files changed, 59 insertions(+), 42 deletions(-)
drivers/iio/dac/ad5686.c +54 -40
index 222f1696affe..feac19e7e306 100644
@@ -6,6 +6,7 @@
  */
 
 #include <linux/array_size.h>
+#include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/errno.h>
 #include <linux/export.h>
@@ -13,6 +14,7 @@
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/sysfs.h>
+#include <linux/wordpart.h>
 
 #include <linux/iio/iio.h>
 
@@ -24,6 +26,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 & AD5686_PD_MSK) |
+			 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 & AD5686_PD_MSK) |
+			 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)
@@ -98,8 +118,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)
@@ -112,32 +132,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,
@@ -451,9 +473,6 @@ int ad5686_probe(struct device *dev,
 {
 	struct ad5686_state *st;
 	struct iio_dev *indio_dev;
-	unsigned int val, ref_bit_msk, shift;
-	bool has_external_vref;
-	u8 cmd;
 	int ret, i;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
@@ -471,13 +490,12 @@ 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;
 
-	/* Initialize masks to all ones provided the max shift (last channel) */
-	shift = ad5686_pd_mask_shift(&st->chip_info->channels[st->chip_info->num_channels - 1]);
-	st->pwr_down_mask = GENMASK(shift + 1, 0);
-	st->pwr_down_mode = GENMASK(shift + 1, 0);
+	/* Initialize masks to all ones */
+	st->pwr_down_mask = ~0;
+	st->pwr_down_mode = ~0;
 
 	/* Set all the power down mode for all channels to 1K pulldown */
 	for (i = 0; i < st->chip_info->num_channels; i++) {
@@ -499,29 +517,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 = AD5686_REF_BIT_MSK;
+		ret = st->write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
+				st->use_internal_vref ? 0 : AD5686_REF_BIT_MSK);
+		if (ret)
+			return ret;
 		break;
 	default:
 		return -EINVAL;
 	}
 
-	val = has_external_vref ? ref_bit_msk : 0;
-
-	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 +5 -2
index 176d41966985..17980c54839c 100644
@@ -39,9 +39,12 @@
 #define AD5686_CMD_READBACK_ENABLE_V2		0x5
 
 #define AD5310_REF_BIT_MSK			BIT(8)
-#define AD5683_REF_BIT_MSK			BIT(12)
-#define AD5686_REF_BIT_MSK			BIT(0)
+#define AD5310_PD_MSK				GENMASK(10, 9)
 
+#define AD5683_REF_BIT_MSK			BIT(12)
+#define AD5683_PD_MSK				GENMASK(14, 13)
+
+#define AD5686_REF_BIT_MSK			BIT(0)
 #define AD5686_PD_MSK				GENMASK(1, 0)
 
 #define AD5686_PD_MODE_1K_TO_GND		0x1
Signature · 1 line
2.43.0
#23 lore raw

Re: [PATCH v6 10/12] iio: dac: ad5686: add control_sync() for single-channel devices

Quoted 13 lines · On 26/05/05 01:35PM, Rodrigo Alencar via B4 Relay wrote:

From: Rodrigo Alencar <[email protected]>

Create ad5310_control_sync() and ad5683_control_sync() functions that properly consume the mask definitions with FIELD_PREP(). This allows to reuse a function that updates the control register with cached values, without relying on confusing logic that depends on st->use_internal_vref, which is initialized earlier in ad5686_probe() because it is also applicable to the AD5686_REGMAP case, removing the need for the has_external_vref. Powerdown masks initialization is simplified as *_control_sync() masks outs any unused bits for the single-channel case. The change cleans up ad5686_write_dac_powerdown() and ad5686_probe(), organizing the code for feature extension, e.g. gain control support for single-channel devices.

...

Quoted 6 lines
drivers/iio/dac/ad5686.c +0 -2
@@ -471,13 +490,12 @@ 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;

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

Quoted 10 lines
drivers/iio/dac/ad5686.c +5 -4
+	st->use_internal_vref = ret == -ENODEV;
+	st->vref_mv = st->use_internal_vref ? st->chip_info->int_vref_mv : ret / 1000;
 
-	/* Initialize masks to all ones provided the max shift (last channel) */
-	shift = ad5686_pd_mask_shift(&st->chip_info->channels[st->chip_info->num_channels - 1]);
-	st->pwr_down_mask = GENMASK(shift + 1, 0);
-	st->pwr_down_mode = GENMASK(shift + 1, 0);
+	/* Initialize masks to all ones */
+	st->pwr_down_mask = ~0;
+	st->pwr_down_mode = ~0;

Sashiko's feedback:

https://sashiko.dev/#/patchset/20260505-ad5686-fixes-v6-0-c2d5f7be32be%40analog.com?part=10

Does initializing these masks to ~0 result in writing 1s to reserved hardware bits for multi-channel DACs?
The initialization loop in the probe function only updates the bits for active channels. For devices with fewer than 8 channels per register, such as the 4-channel AD5686, the upper bits (8-31) remain set to 1. Earlier in ad5686_write_dac_powerdown(), the transmission payload for AD5686_REGMAP multi-channel devices is extracted using:
val = lower_16_bits(val);
For a 4-channel device, this results in bits 8-15 being set to 1. When st->write() transmits this value to the AD5686_CMD_POWERDOWN_DAC register, it appears to violate the hardware specification which states that bits 15-8 are reserved and must be written as 0.

In fact, DB15 to DB8 are don't care, so this is not a concern. That is also the case for dual-channel devices, which requires unused bits to be one (gap between channel 0 and channel 1).

...

Signature · 3 lines
Kind regards,

Rodrigo Alencar
#24 lore raw

PATCH v6 11/12 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 17980c54839c..3945f5fb6b7e 100644
@@ -69,12 +69,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;
@@ -112,16 +111,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
#25 lore raw

PATCH v6 12/12 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     | 32 ++++++++++++++------------------
 drivers/iio/dac/ad5686.h     | 29 +++++++++++++++++++++--------
 drivers/iio/dac/ad5696-i2c.c |  7 ++++++-
 4 files changed, 47 insertions(+), 28 deletions(-)
drivers/iio/dac/ad5686-spi.c +6 -1
index 9493511d9e7f..7409624b954b 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 -18
index feac19e7e306..a730dbfb4b0d 100644
@@ -30,18 +30,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 & AD5686_PD_MSK) |
-			 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 & AD5686_PD_MSK) |
+			    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 & AD5686_PD_MSK) |
-			 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 & AD5686_PD_MSK) |
+			    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)
@@ -151,7 +151,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;
@@ -174,7 +174,7 @@ static int ad5686_read_raw(struct iio_dev *indio_dev,
 	switch (m) {
 	case IIO_CHAN_INFO_RAW:
 		mutex_lock(&st->lock);
-		ret = st->read(st, chan->address);
+		ret = ad5686_read(st, chan->address);
 		mutex_unlock(&st->lock);
 		if (ret < 0)
 			return ret;
@@ -204,10 +204,8 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 
 		mutex_lock(&st->lock);
-		ret = st->write(st,
-				AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
-				chan->address,
-				val << chan->scan_type.shift);
+		ret = ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
+				   chan->address, val << chan->scan_type.shift);
 		mutex_unlock(&st->lock);
 		break;
 	default:
@@ -468,8 +466,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 ad5686_state *st;
 	struct iio_dev *indio_dev;
@@ -482,8 +479,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");
@@ -527,8 +523,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 : AD5686_REF_BIT_MSK);
+		ret = ad5686_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
+				   st->use_internal_vref ? 0 : AD5686_REF_BIT_MSK);
 		if (ret)
 			return ret;
 		break;
drivers/iio/dac/ad5686.h +21 -8
index 3945f5fb6b7e..a06fe7d89305 100644
@@ -62,10 +62,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
@@ -113,6 +118,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
@@ -124,11 +130,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;
 
@@ -147,8 +152,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 e15b01d0b768..6250c227293c 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
#26 lore raw
In reply to Rodrigo Alencar
Quoted 14 lines · On Tue, May 05, 2026 at 01:35:01PM +0100, Rodrigo Alencar via B4 Relay wrote:

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"):

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;

Quoted 4 lines

---
Changes in v6:
- Protect powerdown masks on read access too.
- Minor changes to power down handling and constants.

You were too fast, Jonathan applied some patches already.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#27 lore raw
Quoted 15 lines · Andy Shevchenko
Quoted 13 lines · On Tue, May 05, 2026 at 01:35:01PM +0100, Rodrigo Alencar via B4 Relay wrote:

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"):

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;

Quoted 6 lines
Quoted 4 lines

---
Changes in v6:
- Protect powerdown masks on read access too.
- Minor changes to power down handling and constants.

You were too fast, Jonathan applied some patches already.

I understood He had applied the some of the fixes and was to wait on the fix for the lockless read access issue to proceed. Also, most of the series have been reviewed already. Indeed, I suppose I've been hurrying up things... will slow down!

Signature · 3 lines
Kind regards,

Rodrigo Alencar
maintainer
#28 lore raw
Quoted 21 lines · On Tue, 5 May 2026 15:29:55 +0100 Rodrigo Alencar…
Quoted 20 lines · Andy Shevchenko
Quoted 18 lines · On Tue, May 05, 2026 at 01:35:01PM +0100, Rodrigo Alencar via B4 Relay wrote:

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"):

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;

---
Changes in v6:
- Protect powerdown masks on read access too.
- Minor changes to power down handling and constants.

Quoted 7 lines
Quoted 1 line

You were too fast, Jonathan applied some patches already.

I understood He had applied the some of the fixes and was to wait on the fix for the lockless read access issue to proceed. Also, most of the series have been reviewed already. Indeed, I suppose I've been hurrying up things... will slow down!

If you do a new version (I didn't have any other comments to add on the remaining patches) then please just drop the fixes I've applied off the start and note that the series is dependent on the tag in the fixes-togreg branch. No idea if sashiko will cope with that but humans will ;)