Fixes and cleanups for the AD5686 IIO driver
Subsystems (2)
v4 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;
---
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 (13):
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: fix overlapping DMA buffers in I2C read
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: remove powerdown mask magic number
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 | 526 +++++++++++++++++++++---------------------- drivers/iio/dac/ad5686.h | 120 +++++----- drivers/iio/dac/ad5696-i2c.c | 84 +++---- 4 files changed, 419 insertions(+), 384 deletions(-)
---
base-commit: d86db1905add39f905cf9f04252804b359914ed6
change-id: 20260426-ad5686-fixes-63ea68811bdb
Best regards,
Signature · 1 line
Rodrigo Alencar <[email protected]>
PATCH v4 1/13 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.
--- 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
@@ -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
@@ -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
PATCH v4 2/13 iio: dac: ad5686: fix input raw value check
In reply to Rodrigo AlencarFix 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.
--- drivers/iio/dac/ad5686.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/dac/ad5686.c +1 -1
@@ -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
PATCH v4 3/13 iio: dac: ad5686: acquire lock when doing powerdown control
In reply to Rodrigo AlencarProtect write 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 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 | 4 ++++ 1 file changed, 4 insertions(+)
drivers/iio/dac/ad5686.c +4 -0
@@ -39,6 +39,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 ));
@@ -77,6 +79,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
PATCH v4 4/13 iio: dac: ad5686: fix powerdown control on dual-channel devices
In reply to Rodrigo AlencarFix 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.
--- drivers/iio/dac/ad5686.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/iio/dac/ad5686.c +30 -10
@@ -25,24 +25,35 @@ 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); -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 ; }
@@ -57,10 +68,10 @@ 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); -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,
@@ -82,9 +93,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 :
@@ -464,7 +475,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;
@@ -488,9 +499,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 &= ~(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
Re: [PATCH v4 04/13] iio: dac: ad5686: fix powerdown control on dual-channel devices
Quoted 9 lines · On Wed, Apr 29, 2026 at 02:07:34PM +0100, Rodrigo Alencar via B4 Relay wrote:
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.
...
drivers/iio/dac/ad5686.c int ad5686_probe(struct device *dev, · line 505
Quoted 4 lines
drivers/iio/dac/ad5686.c +4
-0+/* 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 );
Why do we care about upper bits being cleared? Can we simply use here
st->pwr_down_mask = GENMASK($MAX_CHAN * 2 + 1, 0); // or even ~0, U32_MAX, et cetera st->pwr_down_mask = ~0; st->pwr_down_mode = ...same...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 04/13] iio: dac: ad5686: fix powerdown control on dual-channel devices
Quoted 12 lines · Andy Shevchenko
Quoted 9 lines · On Wed, Apr 29, 2026 at 02:07:34PM +0100, Rodrigo Alencar via B4 Relay wrote:
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 8 lines
...
drivers/iio/dac/ad5686.c int ad5686_probe(struct device *dev, · line 505
Quoted 4 lines
drivers/iio/dac/ad5686.c +4
-0+/* 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 );Why do we care about upper bits being cleared? Can we simply use here
With this weird dual-channel use case, the shift of the second channel is 6 and unused bits in-between needs to be one (probably because the dual channel hardware is the same as the quad-channel (but using the first and the last channel) and it will consume more power if those are zero). Then for this case we need the masks initialized to 0b00111100 in the end, so we can have those "ghost" channels disabled.
Quoted 5 lines
st->pwr_down_mask = GENMASK($MAX_CHAN * 2 + 1, 0); // or even ~0, U32_MAX, et cetera st->pwr_down_mask = ~0; st->pwr_down_mode = ...same...
setting it all to ones, breaks the single-channel case, because of the weird logic in ad5686_write_dac_powerdown(), I get to tidy this up only when introducing the control_sync() function.
Signature · 3 lines
Kind regards, Rodrigo Alencar
PATCH v4 5/13 iio: dac: ad5686: fix overlapping DMA buffers in I2C read
In reply to Rodrigo AlencarThe TX and RX buffers in ad5686_i2c_read() both reference data[0], causing byte d8[1] to be shared between the TX buffer and the RX buffer. I2C controller drivers that map all message buffers for DMA before initiating the hardware transaction will map overlapping memory ranges with conflicting DMA directions (DMA_TO_DEVICE and DMA_FROM_DEVICE). This issue was reported by sashiko.
--- drivers/iio/dac/ad5696-i2c.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/dac/ad5696-i2c.c +2 -2
@@ -26,7 +26,7 @@ static int ad5686_i2c_read(struct ad5686_state *st, u8 addr) .addr = i2c->addr, .flags = i2c->flags | I2C_M_RD, .len = 2, - .buf = (char *)&st->data[0].d16, + .buf = (char *)&st->data[1].d16, }, };int ret;
@@ -39,7 +39,7 @@ static int ad5686_i2c_read(struct ad5686_state *st, u8 addr)if (ret <0 )return ret; -return be16_to_cpu (st->data[0 ].d16); +return be16_to_cpu (st->data[1 ].d16); } static int ad5686_i2c_write(struct ad5686_state *st,
Signature · 1 line
2.43.0
Re: [PATCH v4 05/13] iio: dac: ad5686: fix overlapping DMA buffers in I2C read
Quoted 8 lines · On 26/04/29 02:07PM, Rodrigo Alencar via B4 Relay wrote:
From: Rodrigo Alencar <[email protected]>
The TX and RX buffers in
ad5686_i2c_read()both reference data[0], causing byte d8[1] to be shared between the TX buffer and the RX buffer. I2C controller drivers that map all message buffers for DMA before initiating the hardware transaction will map overlapping memory ranges with conflicting DMA directions (DMA_TO_DEVICEandDMA_FROM_DEVICE). This issue was reported by sashiko.
Do we need a proper reported-by tag here?
Quoted 33 lines
Fixes 4177381b4401 ("iio:dac:ad5686: Add AD5671R/75R/94/94R/95R/96/96R support")Signed-off-by Rodrigo Alencar <[email protected]>--- drivers/iio/dac/ad5696-i2c.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/dac/ad5696-i2c.c +2
-2@@ -26,7 +26,7 @@ static int ad5686_i2c_read(struct ad5686_state *st, u8 addr) .addr = i2c->addr, .flags = i2c->flags | I2C_M_RD, .len = 2, - .buf = (char *)&st->data[0].d16, + .buf = (char *)&st->data[1].d16, }, };int ret;@@ -39,7 +39,7 @@ static int ad5686_i2c_read(struct ad5686_state *st, u8 addr)if (ret <0 )return ret; -return be16_to_cpu (st->data[0 ].d16); +return be16_to_cpu (st->data[1 ].d16); } static int ad5686_i2c_write(struct ad5686_state *st,Signature · 2 lines
-- 2.43.0
Signature · 3 lines
Kind regards, Rodrigo Alencar
Re: [PATCH v4 05/13] iio: dac: ad5686: fix overlapping DMA buffers in I2C read
Quoted 10 lines · Rodrigo Alencar
Quoted 7 lines · On 26/04/29 02:07PM, Rodrigo Alencar via B4 Relay wrote:
The TX and RX buffers in
ad5686_i2c_read()both reference data[0], causing byte d8[1] to be shared between the TX buffer and the RX buffer. I2C controller drivers that map all message buffers for DMA before initiating the hardware transaction will map overlapping memory ranges with conflicting DMA directions (DMA_TO_DEVICEandDMA_FROM_DEVICE). This issue was reported by sashiko.Do we need a proper reported-by tag here?
I asked Roman (in another email thread) about dedicated email for that. Let's see what he is going to answer.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v4 05/13] iio: dac: ad5686: fix overlapping DMA buffers in I2C read
Quoted 10 lines · On Wed, 29 Apr 2026 16:50:19 +0300 Andy Shevchenko…
Quoted 9 lines · Rodrigo Alencar
Quoted 7 lines · On 26/04/29 02:07PM, Rodrigo Alencar via B4 Relay wrote:
The TX and RX buffers in
ad5686_i2c_read()both reference data[0], causing byte d8[1] to be shared between the TX buffer and the RX buffer. I2C controller drivers that map all message buffers for DMA before initiating the hardware transaction will map overlapping memory ranges with conflicting DMA directions (DMA_TO_DEVICEandDMA_FROM_DEVICE). This issue was reported by sashiko.
Quoted 5 lines
Quoted 1 line
Do we need a proper reported-by tag here?
I asked Roman (in another email thread) about dedicated email for that. Let's see what he is going to answer.
From that other thread use:
[email protected]
Re: [PATCH v4 05/13] iio: dac: ad5686: fix overlapping DMA buffers in I2C read
In reply to Rodrigo AlencarQuoted 7 lines · On Wed, Apr 29, 2026 at 02:07:35PM +0100, Rodrigo Alencar via B4 Relay wrote:
The TX and RX buffers in
ad5686_i2c_read()both reference data[0], causing byte d8[1] to be shared between the TX buffer and the RX buffer. I2C controller drivers that map all message buffers for DMA before initiating the hardware transaction will map overlapping memory ranges with conflicting DMA directions (DMA_TO_DEVICEandDMA_FROM_DEVICE). This issue was reported by sashiko.
Not sure how this patch helps with that. The minimum granularity for DMA is a cache line size. Do we have data[0] and data[1] be cache line separated?
It might be I miss something obvious, but this topic is always confusing to me (DMA alignment).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v4 05/13] iio: dac: ad5686: fix overlapping DMA buffers in I2C read
Quoted 9 lines · On Wed, 29 Apr 2026 17:07:09 +0300 Andy Shevchenko…
Quoted 7 lines · On Wed, Apr 29, 2026 at 02:07:35PM +0100, Rodrigo Alencar via B4 Relay wrote:
The TX and RX buffers in
ad5686_i2c_read()both reference data[0], causing byte d8[1] to be shared between the TX buffer and the RX buffer. I2C controller drivers that map all message buffers for DMA before initiating the hardware transaction will map overlapping memory ranges with conflicting DMA directions (DMA_TO_DEVICEandDMA_FROM_DEVICE). This issue was reported by sashiko.
Quoted 6 lines
Not sure how this patch helps with that. The minimum granularity for DMA is a cache line size. Do we have data[0] and data[1] be cache line separated?
It might be I miss something obvious, but this topic is always confusing to me (DMA alignment).
I thought I understood :( From a real hardware point of view the cache flushes that occur for non coherent DMA should be safe with tx and rx overlap but 'maybe' there is a framework problem if they buffer is simultaneously marked in both directions or indeed there might be spi controller drivers that can't cope with this.
Flush wise need to flush the tx and rx at start to ensure the tx data is written back and any copies of rx in the CPU caches are clean. Then after DMA need to flush rx again to ensure an prefetched cachelines (which will be clean because of earlier flush) are dropped and new reads come from the device. These will all happen on a cacheline but shouldn't matter if it's the same one as the post DMA flushes are just to drop clean lines and it's clean either way.
+CC Mark for whether this corner has come up in SPI before.
Short question is can we pass same buffer for rx and tx on an spi transfer?
https://lore.kernel.org/all/[email protected]/
Is the patch, based on a bug report from an LLM.
hmm. In the docs for struct spi_transfer we have a not entirely confident sounding comment on this.
/*
* It's okay if tx_buf == rx_buf (right?).
* For MicroWire, one buffer must be NULL.
* Buffers must work with dma_*map_single() calls. */
We don't care about MicroWire here, but the other two lines matter.
This particular case is even weirder as I think they are offset and overlapping but if it works for them equal I'd assume that's fine too.
Jonathan
PATCH v4 6/13 iio: dac: ad5686: refactor include headers
In reply to Rodrigo AlencarApply 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 3 lines · On Wed, Apr 29, 2026 at 02:07:36PM +0100, Rodrigo Alencar via B4 Relay wrote:
Apply IWYU principle, replacing unused/generic headers for specific/missing headers. The resulting include directive lists are sorted accordingly.
I haven't checked this deeply, but I hope at some point we will have more or less usable state of iwyu tool that may do this automatically. At least current result looks better than the original.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 5 lines · Andy Shevchenko
I haven't checked this deeply, but I hope at some point we will have more or less usable state of
iwyutool that may do this automatically. At least current result looks better than the original.Reviewed-by Andy Shevchenko <[email protected]>
I did find a "filter" file of sorts for IWYU that Jonathan had on the mailing list.
It does a good job of omitting reports of very low level headers but at the same time IWYU still tells you to include things like <linux/stddef.h> and remove anything <asm/*> from your includes. Perhaps it needs more tweaking?
Signature · 3 lines
Kind regards CJD
Jonathan Cameron
maintainerRe: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 6 lines · On Wed, 29 Apr 2026 16:38:02 +0200 Joshua Crofts <[email protected]>…
Quoted 4 lines · Andy Shevchenko
I haven't checked this deeply, but I hope at some point we will have more or less usable state of
iwyutool that may do this automatically. At least current result looks better than the original.
Quoted 9 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>I did find a "filter" file of sorts for IWYU that Jonathan had on the mailing list.
It does a good job of omitting reports of very low level headers but at the same time IWYU still tells you to include things like <linux/stddef.h> and remove anything <asm/*> from your includes. Perhaps it needs more tweaking?
I ran into a few corners that I couldn't get to work right
+ probably didn't cover enough of the asm files with specific rules.
I'd love a canonical config file (at least for IIO).
Also turns out the LLMs are quite good at reviewing these patches
Jonathan
Re: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
In reply to Joshua CroftsQuoted 6 lines · Joshua Crofts
Quoted 4 lines · Andy Shevchenko
I haven't checked this deeply, but I hope at some point we will have more or less usable state of
iwyutool that may do this automatically. At least current result looks better than the original.
Quoted 8 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>I did find a "filter" file of sorts for IWYU that Jonathan had on the mailing list.
It does a good job of omitting reports of very low level headers but at the same time IWYU still tells you to include things like <linux/stddef.h> and remove anything <asm/*> from your includes. Perhaps it needs more tweaking?
You need to check that manually and then decide if we need more amendments either in the patch or in the iwyu "filter" file or in both. In case of filter file changes, please share them to Jonathan and mailing list, so other people can adjust their work in case they use the tool.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 17 lines · Andy Shevchenko
Quoted 6 lines · Joshua Crofts
Quoted 4 lines · Andy Shevchenko
I haven't checked this deeply, but I hope at some point we will have more or less usable state of
iwyutool that may do this automatically. At least current result looks better than the original.Quoted 8 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>I did find a "filter" file of sorts for IWYU that Jonathan had on the mailing list.
It does a good job of omitting reports of very low level headers but at the same time IWYU still tells you to include things like <linux/stddef.h> and remove anything <asm/*> from your includes. Perhaps it needs more tweaking?
Quoted 4 lines
You need to check that manually and then decide if we need more amendments either in the patch or in the
iwyu"filter" file or in both. In case of filter file changes, please share them to Jonathan and mailing list, so other people can adjust their work in case they use the tool.
I have made some changes, so I'll send an update where people can get the file. Also I've noticed that there is a /tools/iio/ directory in the repo, perhaps we could
add it there so people don't have to search lore?
Signature · 3 lines
Kind regards CJD
Re: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 23 lines · Joshua Crofts
Quoted 16 lines · Andy Shevchenko
Quoted 6 lines · Joshua Crofts
Quoted 4 lines · Andy Shevchenko
I haven't checked this deeply, but I hope at some point we will have more or less usable state of
iwyutool that may do this automatically. At least current result looks better than the original.Quoted 8 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>I did find a "filter" file of sorts for IWYU that Jonathan had on the mailing list.
It does a good job of omitting reports of very low level headers but at the same time IWYU still tells you to include things like <linux/stddef.h> and remove anything <asm/*> from your includes. Perhaps it needs more tweaking?Quoted 4 lines
You need to check that manually and then decide if we need more amendments either in the patch or in the
iwyu"filter" file or in both. In case of filter file changes, please share them to Jonathan and mailing list, so other people can adjust their work in case they use the tool.
Quoted 4 lines
I have made some changes, so I'll send an update where people can get the file. Also I've noticed that there is a /tools/iio/ directory in the repo, perhaps we could
add it there so people don't have to search lore?
Perhaps, but it should be approved by Jonathan. Sounds good to me, nevertheless.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 24 lines · On Thu, 30 Apr 2026 14:47:47 +0300 Andy Shevchenko…
Quoted 23 lines · Joshua Crofts
Quoted 20 lines · Andy Shevchenko
Quoted 6 lines · Joshua Crofts
Quoted 4 lines · Andy Shevchenko
I haven't checked this deeply, but I hope at some point we will have more or less usable state of
iwyutool that may do this automatically. At least current result looks better than the original.Quoted 8 lines
Quoted 1 line
Reviewed-by Andy Shevchenko <[email protected]>I did find a "filter" file of sorts for IWYU that Jonathan had on the mailing list.
It does a good job of omitting reports of very low level headers but at the same time IWYU still tells you to include things like <linux/stddef.h> and remove anything <asm/*> from your includes. Perhaps it needs more tweaking?You need to check that manually and then decide if we need more amendments either in the patch or in the
iwyu"filter" file or in both. In case of filter file changes, please share them to Jonathan and mailing list, so other people can adjust their work in case they use the tool.
Quoted 8 lines
Quoted 4 lines
I have made some changes, so I'll send an update where people can get the file. Also I've noticed that there is a /tools/iio/ directory in the repo, perhaps we could
add it there so people don't have to search lore?Perhaps, but it should be approved by Jonathan. Sounds good to me, nevertheless.
It's not really a tool so the overall tools maintainers may not want it there. Is there any precedence for similar config files?
Jonathan
Re: [PATCH v4 06/13] iio: dac: ad5686: refactor include headers
Quoted 5 lines · Jonathan Cameron
Quoted 3 lines · On Thu, 30 Apr 2026 14:47:47 +0300 Andy Shevchenko…
Quoted 2 lines · Joshua Crofts
On Wed, 29 Apr 2026 at 20:19, Andy Shevchenko
<[email protected]> wrote:
...
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Also I've noticed that there is a /tools/iio/ directory in the repo, perhaps we could
add it there so people don't have to search lore?Perhaps, but it should be approved by Jonathan. Sounds good to me, nevertheless.
It's not really a tool so the overall tools maintainers may not want it there.
Not yet a tool that is accommodated by Linux kernel project.
Quoted 1 line
Is there any precedence for similar config files?
I haven't seen such.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v4 7/13 iio: dac: ad5686: remove redundant register definition
In reply to Rodrigo AlencarAD5683_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
@@ -112,10 +112,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 ; }
@@ -296,7 +292,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,
@@ -418,24 +414,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,
@@ -536,11 +532,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
@@ -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
PATCH v4 8/13 iio: dac: ad5686: drop enum id
In reply to Rodrigo AlencarSplit 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
@@ -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
PATCH v4 9/13 iio: dac: ad5686: add of_match table to the spi driver
In reply to Rodrigo AlencarAdd 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
@@ -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
PATCH v4 10/13 iio: dac: ad5686: remove powerdown mask magic number
In reply to Rodrigo AlencarDefine macro AD5686_PD_MSK to hold powerdown mask value.
--- drivers/iio/dac/ad5686.c | 16 ++++++++-------- drivers/iio/dac/ad5686.h | 1 + 2 files changed, 9 insertions(+), 8 deletions(-)
drivers/iio/dac/ad5686.c +8 -8
@@ -39,7 +39,7 @@ static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev,unsigned int shift =ad5686_pd_mask_shift (chan);struct ad5686_state *st =iio_priv (indio_dev); -return ((st->pwr_down_mode >> shift) &0x3 ) -1 ; +return ((st->pwr_down_mode >> shift) &AD5686_PD_MSK ) -1 ; } static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev,
@@ -51,8 +51,8 @@ static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev,guard (mutex)(&st->lock); - st->pwr_down_mode &= ~(0x3 << shift); - st->pwr_down_mode |= (mode +1 ) << shift; + st->pwr_down_mode &= ~(AD5686_PD_MSK << shift); + st->pwr_down_mode |= ((mode +1 ) &AD5686_PD_MSK ) << shift;return 0 ; }
@@ -70,7 +70,7 @@ static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev,unsigned int shift =ad5686_pd_mask_shift (chan);struct ad5686_state *st =iio_priv (indio_dev); -return sysfs_emit (buf,"%d\n" , !!(st->pwr_down_mask & (0x3 << shift))); +return sysfs_emit (buf,"%d\n" , !!(st->pwr_down_mask & (AD5686_PD_MSK << shift))); } static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,
@@ -92,9 +92,9 @@ static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev,guard (mutex)(&st->lock);if (readin) - st->pwr_down_mask |=0x3 <<ad5686_pd_mask_shift (chan); + st->pwr_down_mask |=AD5686_PD_MSK <<ad5686_pd_mask_shift (chan);else - st->pwr_down_mask &= ~(0x3 <<ad5686_pd_mask_shift (chan)); + st->pwr_down_mask &= ~(AD5686_PD_MSK <<ad5686_pd_mask_shift (chan));switch (st->chip_info->regmap_type) {case AD5310_REGMAP :
@@ -468,8 +468,8 @@ 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 &= ~(0x3 << shift);/* powered up state */ - st->pwr_down_mode &= ~(0x3 << shift); + st->pwr_down_mask &= ~(AD5686_PD_MSK << shift);/* powered up state */ + st->pwr_down_mode &= ~(AD5686_PD_MSK << shift); st->pwr_down_mode |=0x01 << shift; }
drivers/iio/dac/ad5686.h +1 -0
@@ -47,6 +47,7 @@#define AD5683_REF_BIT_MSK BIT(12)#define AD5686_REF_BIT_MSK BIT(0) +#define AD5686_PD_MSK GENMASK(1, 0)enum ad5686_regmap_type {AD5310_REGMAP ,
Signature · 1 line
2.43.0
Re: [PATCH v4 10/13] iio: dac: ad5686: remove powerdown mask magic number
Quoted 1 line · On Wed, Apr 29, 2026 at 02:07:40PM +0100, Rodrigo Alencar via B4 Relay wrote:
Define macro
AD5686_PD_MSKto hold powerdown mask value.
...
drivers/iio/dac/ad5686.c static int ad5686_get_powerdown_mode(struct iio_dev *indio_dev, · line 42
Quoted 2 lines
drivers/iio/dac/ad5686.c +1
-1-return ((st->pwr_down_mode >> shift) &0x3 ) -1 ; +return ((st->pwr_down_mode >> shift) &AD5686_PD_MSK ) -1 ;
I believe you want two macros that shifts that accordingly.
#define AD5686_PD_MSK GENMASK(1, 0)
#define AD5686_GET_PD_MSK(shift) (AD5686_PD_MSK << shift)
#define AD5686_GET_PD_BITS(mode, shift) (((mode) >> shift) & AD5686_PD_MSK)
But this all seems like reinventing a wheel, id est FIELD_GET()/FIELD_PREP()/FIELD_MODIFY() from bitfield.h.
...
drivers/iio/dac/ad5686.c static int ad5686_set_powerdown_mode(struct iio_dev *indio_dev, · line 55
Quoted 4 lines
drivers/iio/dac/ad5686.c +2
-2- st->pwr_down_mode &= ~(0x3 << shift); - st->pwr_down_mode |= (mode +1 ) << shift; + st->pwr_down_mode &= ~(AD5686_PD_MSK << shift); + st->pwr_down_mode |= ((mode +1 ) &AD5686_PD_MSK ) << shift;
This is FIELD_MODIFY()
...
drivers/iio/dac/ad5686.c static ssize_t ad5686_read_dac_powerdown(struct iio_dev *indio_dev, · line 73
Quoted 2 lines
drivers/iio/dac/ad5686.c +1
-1-return sysfs_emit (buf,"%d\n" , !!(st->pwr_down_mask & (0x3 << shift))); +return sysfs_emit (buf,"%d\n" , !!(st->pwr_down_mask & (AD5686_PD_MSK << shift)));
FIELD_GET()
...
drivers/iio/dac/ad5686.c static ssize_t ad5686_write_dac_powerdown(struct iio_dev *indio_dev, · line 97
Quoted 6 lines
drivers/iio/dac/ad5686.c +2
-2if (readin) - st->pwr_down_mask |=0x3 <<ad5686_pd_mask_shift (chan); + st->pwr_down_mask |=AD5686_PD_MSK <<ad5686_pd_mask_shift (chan);else - st->pwr_down_mask &= ~(0x3 <<ad5686_pd_mask_shift (chan)); + st->pwr_down_mask &= ~(AD5686_PD_MSK <<ad5686_pd_mask_shift (chan));
FIELD_MODIFY()
...
drivers/iio/dac/ad5686.c int ad5686_probe(struct device *dev, · line 472
Quoted 5 lines
drivers/iio/dac/ad5686.c +2
-2shift =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_mask &= ~(AD5686_PD_MSK << shift);/* powered up state */ + st->pwr_down_mode &= ~(AD5686_PD_MSK << shift);
FIELD_MODIFY()
Quoted 1 line
}
...
Since these require some shifts at run-time perhaps better to have helpers that will do all needed stuff in one call.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v4 11/13 iio: dac: ad5686: add control_sync() for single-channel devices
In reply to Rodrigo AlencarCreate ad5310_control_sync() and ad5683_control_sync() functions that properly consumes 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. 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 | 88 ++++++++++++++++++++++++++++-------------------- drivers/iio/dac/ad5686.h | 7 ++-- 2 files changed, 57 insertions(+), 38 deletions(-)
drivers/iio/dac/ad5686.c +52 -36
@@ -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) | +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 inline unsigned int ad5686_pd_mask_shift (const struct iio_chan_spec *chan) {if (chan->channel == chan->address)
@@ -82,8 +102,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)
@@ -98,32 +118,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,
@@ -437,9 +459,7 @@ 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; +unsigned int shift;int ret, i; indio_dev =devm_iio_device_alloc (dev,sizeof (*st));
@@ -457,8 +477,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 ;/* 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 ]);
@@ -485,29 +505,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
@@ -44,9 +44,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)enum ad5686_regmap_type {
Signature · 1 line
2.43.0
PATCH v4 12/13 iio: dac: ad5686: cleanup doc header of local structs
In reply to Rodrigo AlencarReview 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
@@ -67,12 +67,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;
@@ -110,16 +109,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
PATCH v4 13/13 iio: dac: ad5686: create bus ops struct
In reply to Rodrigo AlencarCreate 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
@@ -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
@@ -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) | -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)
@@ -137,7 +137,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 :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;
@@ -190,10 +190,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 :
@@ -454,8 +452,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;
@@ -469,8 +466,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" );
@@ -515,8 +511,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
@@ -60,10 +60,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
@@ -111,6 +116,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
@@ -122,11 +128,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;
@@ -145,8 +150,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
@@ -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