staging: iio: impedance-analyzer: ad5933: buffer and sysfs fixes
Subsystems (4)
cover staging: iio: impedance-analyzer: ad5933: buffer and sysfs fixes
Four bug fixes for ad5933.
Patch 1 fixes buffer data when only one of the real and imag channels is enabled. The driver reads the wrong register, so you get the other value and no error. This has been broken since 2011, so patch 1 is tagged for stable.
Patches 2 to 4 fix sysfs attributes that don't match the chip. Patch 2 rejects frequencies that overflow the 24-bit frequency registers. That only happens with an external MCLK of 3.2 MHz or less. Patch 3 makes out_altvoltage0_settling_cycles report the count the chip uses. Patch 4 keeps the old value when a write to the chip fails. These change what userspace sees, so they are not tagged for stable.
I don't have AD5933 hardware. I tested in a virtme-ng VM with i2c-stub, KASAN and lockdep. The stub can't run a real sweep or give real ADC data, so the tests cover register values and the sysfs and buffer paths. Each patch shows its test results after the --- line.
Every patch builds with W=1 and no new warnings on x86_64 (gcc and clang) and on i386. sparse, smatch, coccicheck and checkpatch --strict are clean.
I used Opus 5.5 assistant to help find these bugs and write the fixes, and I reviewed every patch before sending.
Punnay Sharma (4):
staging: iio: impedance-analyzer: ad5933: fix swapped real and imag
data
staging: iio: impedance-analyzer: ad5933: reject frequency code
overflow
staging: iio: impedance-analyzer: ad5933: fix settling cycles readback
staging: iio: impedance-analyzer: ad5933: keep old values on failed
writes
.../staging/iio/impedance-analyzer/ad5933.c | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-)
base-commit: 39508411b82d4dc29dd245bb471ec9ecc1111c9d
Signature · 1 line
2.56.0
PATCH 1/4 staging: iio: impedance-analyzer: ad5933: fix swapped real and imag data
With only in_voltage_real enabled, every buffer sample is the imag value. With only in_voltage_imag enabled, every sample is the real value. Nothing reports an error.
ad5933_work() picks the register to read withtest_bit(1, indio_dev->active_scan_mask). But real is scan index 0 and imag is scan index 1. With only real enabled, bit 1 is clear, so the driver reads AD5933_REG_IMAG_DATA (0x96). With only imag enabled, bit 1 is set, so it reads AD5933_REG_REAL_DATA (0x94). With both enabled, the read starts at 0x94 and covers both registers, so that case works.
The driver tested bit 0 until the bitmap conversion in the commit below changed it to bit 1.
Test bit 0 instead.
---
Notes:
Tested in a virtme-ng VM with i2c-stub. I have no AD5933 hardware, so
there is no real sweep and no real ADC data here. The stub holds 0x1234
in REAL_DATA and 0x5678 in IMAG_DATA.
Before: with only real enabled, the sample is 0x5678. With only imag
enabled, it is 0x1234.
After: only real gives 0x1234, and only imag gives 0x5678. Both enabled
gives 0x1234 0x5678, before and after.drivers/staging/iio/impedance-analyzer/ad5933.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +1 -1
@@ -624,7 +624,7 @@ static void ad5933_work(struct work_struct *work)int scan_count =bitmap_weight (indio_dev->active_scan_mask,iio_get_masklength (indio_dev)); ret =ad5933_i2c_read (st->client, -test_bit (1 , indio_dev->active_scan_mask) ? +test_bit (0 , indio_dev->active_scan_mask) ?AD5933_REG_REAL_DATA :AD5933_REG_IMAG_DATA , scan_count *2 , (u8 *)buf);if (ret)
Signature · 1 line
2.56.0
PATCH 2/4 staging: iio: impedance-analyzer: ad5933: reject frequency code overflow
In reply to Punnay SharmaWith a 500 kHz external MCLK, writing 30000 to out_altvoltage0_frequency_start succeeds, but the chip starts at 14374 Hz. The attribute then reads back 14374. out_altvoltage0_frequency_increment has the same problem.
The start and increment registers hold 24 bits. ad5933_set_freq() turns the frequency into a code, freq * 2^27 / (MCLK / 4), and writes only the low 24 bits. A bigger code silently loses its top bits. ad5933_store_frequency() only checks the fixed 100 kHz limit. With an MCLK of 3.2 MHz or less, 100 kHz already overflows. The datasheet suggests a 500 kHz MCLK for measurements between 100 Hz and 1 kHz.
Move the conversion into a helper, ad5933_freq_to_code(), and call it from both ad5933_set_freq() and ad5933_store_frequency(). Make ad5933_store_frequency() return -EINVAL when the code does not fit in 24 bits. With one helper, the check and the value sent to the chip use the same math.
Keep the 10 kHz probe default, because a probe failure breaks boards with a very low MCLK that work today.
---
Notes:
Tested in a virtme-ng VM with i2c-stub and a fake 500 kHz "mclk" clock. I have no AD5933 hardware, so this does not check the real output frequency.
Before: a write of 30000 to frequency_start returns 0 and reads back 14374. A write of 15625 returns 0 and reads back 0.
After: both writes fail with -EINVAL, and the register keeps its old value. 15624 still works and reads back 15623. frequency_increment behaves the same. With the internal clock, 100000 works and 100001 fails, before and after.
drivers/staging/iio/impedance-analyzer/ad5933.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +9 -1
@@ -187,6 +187,11 @@ static int ad5933_wait_busy(struct ad5933_state *st, unsigned char event)return -EAGAIN ; } +static u64 ad5933_freq_to_code (struct ad5933_state *st,unsigned long freq) +{ +return div64_ul (BIT_ULL (27 ) * freq, st->mclk_hz /4 ); +} +static int ad5933_set_freq (struct ad5933_state *st,unsigned int reg,unsigned long freq) {
@@ -196,7 +201,7 @@ static int ad5933_set_freq(struct ad5933_state *st,u8 d8[4 ]; } dat; - freqreg =div64_ul (BIT_ULL (27 ) * freq, st->mclk_hz /4 ); + freqreg =ad5933_freq_to_code (st, freq);switch (reg) {case AD5933_REG_FREQ_START :
@@ -307,6 +312,9 @@ static ssize_t ad5933_store_frequency(struct device *dev,if (val > AD5933_MAX_OUTPUT_FREQ_Hz)return -EINVAL ; +if (ad5933_freq_to_code (st, val) >GENMASK (23 ,0 )) +return -EINVAL ; +if (!iio_device_claim_direct (indio_dev))return -EBUSY ;
Signature · 1 line
2.56.0
Re: [PATCH 2/4] staging: iio: impedance-analyzer: ad5933: reject frequency code overflow
Quoted 20 lines · Punnay Sharma
With a 500 kHz external MCLK, writing 30000 to
out_altvoltage0_frequency_startsucceeds, but the chip starts at 14374 Hz. The attribute then reads back 14374.out_altvoltage0_frequency_incrementhas the same problem.The start and increment registers hold 24 bits.
ad5933_set_freq()turns the frequency into a code, freq * 2^27 / (MCLK / 4), and writes only the low 24 bits. A bigger code silently loses its top bits.ad5933_store_frequency()only checks the fixed 100 kHz limit. With an MCLK of 3.2 MHz or less, 100 kHz already overflows. The datasheet suggests a 500 kHz MCLK for measurements between 100 Hz and 1 kHz.Move the conversion into a helper,
ad5933_freq_to_code(), and call it from bothad5933_set_freq()andad5933_store_frequency(). Makead5933_store_frequency()return-EINVALwhen the code does not fit in 24 bits. With one helper, the check and the value sent to the chip use the same math.Keep the 10 kHz probe default, because a probe failure breaks boards with a very low MCLK that work today.
...
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_store_frequency(struct device *dev, · line 315
Quoted 1 line
drivers/staging/iio/impedance-analyzer/ad5933.c +1
-0+if (ad5933_freq_to_code (st, val) >GENMASK (23 ,0 ))
Perhaps >= BIT_ULL(24) ?
Then it will be in align with the commit message and BIT_ULL(27) already used in the code.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 3/4 staging: iio: impedance-analyzer: ad5933: fix settling cycles readback
In reply to Punnay SharmaWriting 1023 to out_altvoltage0_settling_cycles reads back 1023, but the chip uses 1020. The cycle count in the register is 9 bits, so the driver programs 255 with the x4 multiplier. Odd values from 513 to 1021 read back 1 too high. Values from 1023 to 2044 that are not a multiple of 4 read back up to 3 too high.
ad5933_store() saves the value in st->settling_cycles before it divides it by 2 or 4 for the register. ad5933_show() then returns the saved value, not the count the chip uses. The ABI says the attribute is the number of cycles before the ADC starts a conversion.
Save the value rounded down to a multiple of 2 or 4, which is the count the chip uses.
---
Notes:
Tested in a virtme-ng VM with i2c-stub. I have no AD5933 hardware, so this checks the register value with i2cget but not the real settling time.
Before: 1023 reads back 1023, but 0x8A/0x8B hold 0x06FF, which is 255 x 4 = 1020. A write of 513 reads back 513, but the chip uses 512.
After: 1023 reads back 1020, and 513 reads back 512. 1022 and 2044 read back the same as before.
drivers/staging/iio/impedance-analyzer/ad5933.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +7 -3
@@ -414,13 +414,17 @@ static ssize_t ad5933_store(struct device *dev,break ;case AD5933_OUT_SETTLING_CYCLES : val =clamp (val, (u16 )0 , (u16 )0x7FC ); - st->settling_cycles = val;/* 2x, 4x handling, see datasheet */ -if (val >1022 ) +if (val >1022 ) { + st->settling_cycles =round_down (val,4 ); val = (val >>2 ) | (3 <<9 ); -else if (val >511 ) + }else if (val >511 ) { + st->settling_cycles =round_down (val,2 ); val = (val >>1 ) |BIT (9 ); + }else { + st->settling_cycles = val; + } dat =cpu_to_be16 (val); ret = ad5933_i2c_write(st->client,
Signature · 1 line
2.56.0
Re: [PATCH 3/4] staging: iio: impedance-analyzer: ad5933: fix settling cycles readback
Quoted 13 lines · Punnay Sharma
Writing 1023 to
out_altvoltage0_settling_cyclesreads back 1023, but the chip uses 1020. The cycle count in the register is 9 bits, so the driver programs 255 with the x4 multiplier. Odd values from 513 to 1021 read back 1 too high. Values from 1023 to 2044 that are not a multiple of 4 read back up to 3 too high.
ad5933_store()saves the value inst->settling_cyclesbefore it divides it by 2 or 4 for the register.ad5933_show()then returns the saved value, not the count the chip uses. The ABI says the attribute is the number of cycles before the ADC starts a conversion.Save the value rounded down to a multiple of 2 or 4, which is the count the chip uses.
...
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_store(struct device *dev, · line 416
Quoted 2 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +0
-0case AD5933_OUT_SETTLING_CYCLES : val =clamp (val, (u16 )0 , (u16 )0x7FC );
This really needs to drop castings.
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_store(struct device *dev, · line 419
Quoted 5 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +1
-2- st->settling_cycles = val;/* 2x, 4x handling, see datasheet */ -if (val >1022 ) +if (val >1022 ) {
Are 511 and 1022 mentioned in datasheet explicitly in plain numbers?
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_store(struct device *dev, · line 427
Quoted 9 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +6
-1+ st->settling_cycles =round_down (val,4 ); val = (val >>2 ) | (3 <<9 ); - else if (val > 511) + }else if (val >511 ) { + st->settling_cycles =round_down (val,2 ); val = (val >>1 ) |BIT (9 ); + }else { + st->settling_cycles = val; + }
This is a mess with shift by 9 bits and plain numbers like 512 (511) and 1024 (1022). I'm wondering what datasheet says about all this.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 4/4 staging: iio: impedance-analyzer: ad5933: keep old values on failed writes
In reply to Punnay Sharmaad5933_store() updates the driver's copy of a setting before it writes the setting to the chip. If the I2C write fails, userspace gets the error, but the driver keeps the new value.
For out_altvoltage0_raw (output range) and in_voltage0_scale (PGA gain), the new bits stay in st->ctrl_hb. ad5933_cmd() sends ctrl_hb with every command, so the next buffer enable programs the failed setting anyway. For example, if echo 198 > out_altvoltage0_raw fails, the next sweep still runs with the 198 mV range. For out_altvoltage0_settling_cycles and out_altvoltage0_frequency_points, the attribute reads back a value that never reached the chip.
Save the old values before the switch and put them back when the write fails.
---
Notes:
Tested in a virtme-ng VM with a throwaway copy of i2c-stub that makes the next write fail with -EIO. The copy is test code only. I have no AD5933 hardware, so this does not show a real bus error.
Before: echo 198 > out_altvoltage0_raw fails with -EIO but reads back
198. echo 1 > in_voltage0_scale fails but reads back 1. After a buffer enable and disable, 0x80 is 0xa3, so the chip got both failed settings. A failed settling_cycles write of 1023 reads back 1020, but 0x8A/0x8B still hold 100. A failed frequency_points write of 200 reads back 200, but 0x88/0x89 still hold 100.
After: each failed write reads back the old value, and 0x80 stays 0xa0. The same writes without a failure give the same results as before.
drivers/staging/iio/impedance-analyzer/ad5933.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/staging/iio/impedance-analyzer/ad5933.c +13 -0
@@ -379,6 +379,9 @@ static ssize_t ad5933_store(struct device *dev,u16 val;int i, ret =0 ;__be16 dat; +unsigned char old_ctrl_hb; +unsigned short old_settling_cycles; +unsigned short old_freq_points;if (this_attr->address !=AD5933_IN_PGA_GAIN ) { ret =kstrtou16 (buf,10 , &val);
@@ -390,6 +393,10 @@ static ssize_t ad5933_store(struct device *dev,return -EBUSY ;mutex_lock (&st->lock); + old_ctrl_hb = st->ctrl_hb; + old_settling_cycles = st->settling_cycles; + old_freq_points = st->freq_points; +switch ((u32 )this_attr->address) {case AD5933_OUT_RANGE : ret = -EINVAL ;
@@ -443,6 +450,12 @@ static ssize_t ad5933_store(struct device *dev, ret = -EINVAL ; } +if (ret) { + st->ctrl_hb = old_ctrl_hb; + st->settling_cycles = old_settling_cycles; + st->freq_points = old_freq_points; + } +mutex_unlock (&st->lock);iio_device_release_direct (indio_dev);
Signature · 1 line
2.56.0
Re: [PATCH 4/4] staging: iio: impedance-analyzer: ad5933: keep old values on failed writes
Quoted 14 lines · Punnay Sharma
ad5933_store()updates the driver's copy of a setting before it writes the setting to the chip. If the I2C write fails, userspace gets the error, but the driver keeps the new value.For
out_altvoltage0_raw(output range) andin_voltage0_scale(PGA gain), the new bits stay inst->ctrl_hb.ad5933_cmd()sendsctrl_hbwith every command, so the next buffer enable programs the failed setting anyway. For example, if echo 198 >out_altvoltage0_rawfails, the next sweep still runs with the 198 mV range. Forout_altvoltage0_settling_cyclesandout_altvoltage0_frequency_points, the attribute reads back a value that never reached the chip.Save the old values before the switch and put them back when the write fails.
...
drivers/staging/iio/impedance-analyzer/ad5933.c static ssize_t ad5933_store(struct device *dev, · line 384
Quoted 8 lines
static
ssize_tad5933_store(struct device *dev,drivers/staging/iio/impedance-analyzer/ad5933.c +3
-0u16 val;int i, ret =0 ;__be16 dat; +unsigned char old_ctrl_hb; +unsigned short old_settling_cycles; +unsigned short old_freq_points;
Even if the original code doesn't follow reversed xmas tree order, still please try to follow with touched lines.
Quoted 10 lines
(quoted diff) +3
-0if (this_attr->address != AD5933_IN_PGA_GAIN) { ret = kstrtou16(buf, 10, &val); mutex_lock(&st->lock); + old_ctrl_hb = st->ctrl_hb; + old_settling_cycles = st->settling_cycles; + old_freq_points = st->freq_points; switch ((u32)this_attr->address) { case AD5933_OUT_RANGE:
Review
Quoted 10 lines
(quoted diff) +6
-0} + if (ret) { + st->ctrl_hb = old_ctrl_hb; + st->settling_cycles = old_settling_cycles; + st->freq_points = old_freq_points; + } + mutex_unlock(&st->lock);
It's better to split the three members to a separate data structure and copy it as a whole.
Signature · 2 lines
With Best Regards, Andy Shevchenko