staging: iio: impedance-analyzer: ad5933: use div64_ul() instead of do_div()
Subsystems (4)
PATCH 1/1 staging: iio: impedance-analyzer: ad5933: use div64_ul() instead of do_div()
Replace do_div() with div64_ul() since the remainder is not used. div64_ul() is the preferred API for 64-bit by 32-bit division when only the quotient is needed, as it returns the result directly rather than modifying the dividend in-place.
Issue identified by coccicheck using do_div.cocci.
--- drivers/staging/iio/impedance-analyzer/ad5933.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +2 -2
@@ -194,8 +194,8 @@ static int ad5933_set_freq(struct ad5933_state *st,u8 d8[4 ]; } dat; - freqreg = (u64 )freq * (u64)(1 <<27 ); -do_div (freqreg, st->mclk_hz /4 ); + freqreg =div64_ul ((u64 )freq * (u64)(1 <<27 ), + st->mclk_hz /4 );switch (reg) {case AD5933_REG_FREQ_START :
Signature · 1 line
2.39.5
Quoted 6 lines · Archit Anant
Replace
do_div()withdiv64_ul()since the remainder is not used.div64_ul()is the preferred API for 64-bit by 32-bit division when only the quotient is needed, as it returns the result directly rather than modifying the dividend in-place.Issue identified by coccicheck using
do_div.cocci.
...
drivers/staging/iio/impedance-analyzer/ad5933.c static int ad5933_set_freq(struct ad5933_state *st, · line 198
Quoted 4 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-2- freqreg = (u64 )freq * (u64)(1 <<27 ); -do_div (freqreg, st->mclk_hz /4 ); + freqreg =div64_ul ((u64 )freq * (u64)(1 <<27 ), + st->mclk_hz /4 );
It can be one line to begin with.
Then drop that ugly castings and explicit big shifts.
freqreg = div64_ul(BIT_ULL(27) * freq, st->mclk_hz / 4);
Now you can see That 4 is only 2 bits, so this can be written in simpler way:
freqreg = div64_ul(BIT_ULL(29) * freq, st->mclk_hz);
which may give a better precision at the end of the day.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Jan 22, 2026 at 08:26:33PM +0530, Archit Anant wrote:
...
Quoted 8 lines
drivers/staging/iio/impedance-analyzer/ad5933.c static int ad5933_set_freq(struct ad5933_state *st, · line 198
Quoted 4 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-2- freqreg = (u64 )freq * (u64)(1 <<27 ); -do_div (freqreg, st->mclk_hz /4 ); + freqreg =div64_ul ((u64 )freq * (u64)(1 <<27 ), + st->mclk_hz /4 );It can be one line to begin with.
Then drop that ugly castings and explicit big shifts.
Quoted 8 lines
freqreg = div64_ul(BIT_ULL(27) * freq, st->mclk_hz / 4);Now you can see That 4 is only 2 bits, so this can be written in simpler way:
freqreg = div64_ul(BIT_ULL(29) * freq, st->mclk_hz);which may give a better precision at the end of the day.
It also might be worth to add a comment on top to explain (with given context I don't know if there is already one on top of the function, though).
And I think we want AD people to comment on this and maybe explain better the calculations done (and why the original code drops precision, was it deliberate?).
Signature · 2 lines
With Best Regards, Andy Shevchenko
A message that is not in the archive (sent off-list or not yet received): CADJHxWB3b+bH2+HBP+SG0jxhGNcozstBeWeDH_3dgS-4c2G-6g@mail.gmail.com
Quoted 1 line · Archit Anant
(Resending to the list, apologies for the private reply earlier.)
First of all, do no top post!
Quoted 12 lines
Thanks for the review and the detailed math breakdown.
I agree that
(freq * BIT_ULL(29)) / mclkis algebraically superior and improves precision.However, regarding your concern about the original code:
Quoted 1 line
"why the original code drops precision, was it deliberate?"
Since I do not have the AD5933 hardware to test if the register expects the truncated value from
(mclk / 4), I am hesitant to change the logic in this patch.
Quoted 5 lines
Would you prefer I:
1. Stick to a purely mechanical change (keep the logic equivalent, just usediv64_ulandBIT_ULL(27)for readability)?
2. Or proceed with theBIT_ULL(29)simplification assuming the precision loss was unintentional?
I definitely prefer #2. That's why I plead to AD people to confirm.
Quoted 35 lines
I'm leaning towards #1 for safety, but happy to do #2 if the maintainers (Lars/Michael) think it's safe.
Quoted 29 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Jan 22, 2026 at 08:26:33PM +0530, Archit Anant wrote:
...
Quoted 8 lines
drivers/staging/iio/impedance-analyzer/ad5933.c static int ad5933_set_freq(struct ad5933_state *st, · line 198
Quoted 4 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-2- freqreg = (u64 )freq * (u64)(1 <<27 ); -do_div (freqreg, st->mclk_hz /4 ); + freqreg =div64_ul ((u64 )freq * (u64)(1 <<27 ), + st->mclk_hz /4 );It can be one line to begin with.
Then drop that ugly castings and explicit big shifts.Quoted 8 lines
freqreg = div64_ul(BIT_ULL(27) * freq, st->mclk_hz / 4);Now you can see That 4 is only 2 bits, so this can be written in simpler way:
freqreg = div64_ul(BIT_ULL(29) * freq, st->mclk_hz);which may give a better precision at the end of the day.
It also might be worth to add a comment on top to explain (with given context
I don't know if there is already one on top of the function, though).And I think we want AD people to comment on this and maybe explain better the calculations done (and why the original code drops precision, was it deliberate?).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Gentle ping on this.
Has anyone from AD had a chance to confirm whether the original truncation via (mclk / 4) was intentional?
If there are no objections, I can prepare a v2 using the BIT_ULL(29) form as suggested.
Thanks,
Archit
Quoted 67 lines · Andy Shevchenko
Quoted 1 line · Archit Anant
(Resending to the list, apologies for the private reply earlier.)
First of all, do no top post!
Quoted 12 lines
Thanks for the review and the detailed math breakdown.
I agree that
(freq * BIT_ULL(29)) / mclkis algebraically superior and improves precision.However, regarding your concern about the original code:
Quoted 1 line
"why the original code drops precision, was it deliberate?"
Since I do not have the AD5933 hardware to test if the register expects the truncated value from
(mclk / 4), I am hesitant to change the logic in this patch.Quoted 5 lines
Would you prefer I:
1. Stick to a purely mechanical change (keep the logic equivalent, just usediv64_ulandBIT_ULL(27)for readability)?
2. Or proceed with theBIT_ULL(29)simplification assuming the precision loss was unintentional?I definitely prefer #2. That's why I plead to AD people to confirm.
Quoted 35 lines
I'm leaning towards #1 for safety, but happy to do #2 if the maintainers (Lars/Michael) think it's safe.
Quoted 29 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Jan 22, 2026 at 08:26:33PM +0530, Archit Anant wrote:
...
Quoted 8 lines
drivers/staging/iio/impedance-analyzer/ad5933.c static int ad5933_set_freq(struct ad5933_state *st, · line 198
Quoted 4 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-2- freqreg = (u64 )freq * (u64)(1 <<27 ); -do_div (freqreg, st->mclk_hz /4 ); + freqreg =div64_ul ((u64 )freq * (u64)(1 <<27 ), + st->mclk_hz /4 );It can be one line to begin with.
Then drop that ugly castings and explicit big shifts.Quoted 8 lines
freqreg = div64_ul(BIT_ULL(27) * freq, st->mclk_hz / 4);Now you can see That 4 is only 2 bits, so this can be written in simpler way:
freqreg = div64_ul(BIT_ULL(29) * freq, st->mclk_hz);which may give a better precision at the end of the day.
It also might be worth to add a comment on top to explain (with given context
I don't know if there is already one on top of the function, though).And I think we want AD people to comment on this and maybe explain better the calculations done (and why the original code drops precision, was it deliberate?).
--
With Best Regards,
Andy Shevchenko
Signature · 2 lines
Sincerely, Archit Anant
David Lechner
reviewerQuoted 27 lines · Archit Anant
Quoted 24 lines · Andy Shevchenko
Quoted 1 line · Archit Anant
(Resending to the list, apologies for the private reply earlier.)
First of all, do no top post!
Quoted 17 lines
Thanks for the review and the detailed math breakdown.
I agree that
(freq * BIT_ULL(29)) / mclkis algebraically superior and improves precision.However, regarding your concern about the original code:
Quoted 1 line
"why the original code drops precision, was it deliberate?"
Since I do not have the AD5933 hardware to test if the register expects the truncated value from
(mclk / 4), I am hesitant to change the logic in this patch.Would you prefer I:
1. Stick to a purely mechanical change (keep the logic equivalent, just usediv64_ulandBIT_ULL(27)for readability)?
2. Or proceed with theBIT_ULL(29)simplification assuming the precision loss was unintentional?
Quoted 6 lines
Quoted 4 lines
I definitely prefer #2. That's why I plead to AD people to confirm.
Quoted 2 lines
I'm leaning towards #1 for safety, but happy to do #2 if the maintainers (Lars/Michael) think it's safe.
Gentle ping on this.
As Andy said, please don't top-post. Put the reply in context to make it easy to see what "this" is referring to.
I moved your reply to show what we mean.
Quoted 9 lines
Has anyone from AD had a chance to confirm whether the original truncation via (mclk / 4) was intentional?
If there are no objections, I can prepare a v2 using the
BIT_ULL(29)form as suggested.Thanks,
Archit
Given how old this driver is, I would not expect an reply as the author has likely long forgotten what they were thinking at the time.
I would keep the (mclk / 4) as it is to be safe as you suggest. There could be userspace depending on exact values currently being returned.
Quoted 41 lines
Quoted 39 lines
Quoted 26 lines
Quoted 22 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Jan 22, 2026 at 08:26:33PM +0530, Archit Anant wrote:
...
Quoted 16 lines
drivers/staging/iio/impedance-analyzer/ad5933.c static int ad5933_set_freq(struct ad5933_state *st, · line 198
Quoted 4 lines
drivers/staging/iio/impedance-analyzer/ad5933.c +2
-2- freqreg = (u64 )freq * (u64)(1 <<27 ); -do_div (freqreg, st->mclk_hz /4 ); + freqreg =div64_ul ((u64 )freq * (u64)(1 <<27 ), + st->mclk_hz /4 );It can be one line to begin with.
Then drop that ugly castings and explicit big shifts.freqreg = div64_ul(BIT_ULL(27) * freq, st->mclk_hz / 4);Now you can see That 4 is only 2 bits, so this can be written in simpler way:
freqreg = div64_ul(BIT_ULL(29) * freq, st->mclk_hz);which may give a better precision at the end of the day.
Quoted 7 lines
Quoted 7 lines
It also might be worth to add a comment on top to explain (with given context
I don't know if there is already one on top of the function, though).And I think we want AD people to comment on this and maybe explain better the calculations done (and why the original code drops precision, was it deliberate?).
--
With Best Regards,
Andy Shevchenko
David Lechner
reviewerQuoted 52 lines · David Lechner
Quoted 27 lines · Archit Anant
Quoted 24 lines · Andy Shevchenko
Quoted 1 line · Archit Anant
(Resending to the list, apologies for the private reply earlier.)
First of all, do no top post!
Quoted 17 lines
Thanks for the review and the detailed math breakdown.
I agree that
(freq * BIT_ULL(29)) / mclkis algebraically superior and improves precision.However, regarding your concern about the original code:
Quoted 1 line
"why the original code drops precision, was it deliberate?"
Since I do not have the AD5933 hardware to test if the register expects the truncated value from
(mclk / 4), I am hesitant to change the logic in this patch.Would you prefer I:
1. Stick to a purely mechanical change (keep the logic equivalent, just usediv64_ulandBIT_ULL(27)for readability)?
2. Or proceed with theBIT_ULL(29)simplification assuming the precision loss was unintentional?Quoted 6 lines
Quoted 4 lines
I definitely prefer #2. That's why I plead to AD people to confirm.
Quoted 2 lines
I'm leaning towards #1 for safety, but happy to do #2 if the maintainers (Lars/Michael) think it's safe.
Gentle ping on this.
As Andy said, please don't top-post. Put the reply in context to make it easy to see what "this" is referring to.
I moved your reply to show what we mean.
Quoted 9 lines
Has anyone from AD had a chance to confirm whether the original truncation via (mclk / 4) was intentional?
If there are no objections, I can prepare a v2 using the
BIT_ULL(29)form as suggested.Thanks,
Archit
Quoted 7 lines
Given how old this driver is, I would not expect an reply as the author has likely long forgotten what they were thinking at the time.
I would keep the (mclk / 4) as it is to be safe as you suggest. There could be userspace depending on exact values currently being returned.
Actually, I just saw this was in staging, so I guess not breaking userspace isn't the top priority if there the other way is objectively better. I would assume that testing/measurement could show which is actually correct.
Quoted 42 lines
Quoted 41 lines
Quoted 39 lines
Quoted 26 lines
Quoted 22 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Thu, Jan 22, 2026 at 08:26:33PM +0530, Archit Anant wrote:
...
Quoted 16 lines
Review
Quoted 4 lines
(quoted diff) +2
-2- freqreg = (u64)freq * (u64)(1 << 27); - do_div(freqreg, st->mclk_hz / 4); + freqreg = div64_ul((u64)freq * (u64)(1 << 27), + st->mclk_hz / 4);It can be one line to begin with.
Then drop that ugly castings and explicit big shifts.freqreg = div64_ul(BIT_ULL(27) * freq, st->mclk_hz / 4);Now you can see That 4 is only 2 bits, so this can be written in simpler way:
freqreg = div64_ul(BIT_ULL(29) * freq, st->mclk_hz);which may give a better precision at the end of the day.
Quoted 7 lines
Quoted 7 lines
It also might be worth to add a comment on top to explain (with given context
I don't know if there is already one on top of the function, though).And I think we want AD people to comment on this and maybe explain better the calculations done (and why the original code drops precision, was it deliberate?).
--
With Best Regards,
Andy Shevchenko
Quoted 3 lines · David Lechner
Actually, I just saw this was in staging, so I guess not breaking userspace isn't the top priority if there the other way is objectively better. I would assume that testing/measurement could show which is actually correct.
Since I don't have the hardware to perform those measurements and confirm if the precision improvement is valid or a regression, I will take the conservative
approach for v2.
I will send a v2 that only replaces do_div() with div64_ul() and uses BIT_ULL(27), preserving the original (mclk / 4) logic to guarantee no behavioral change.
Signature · 2 lines
Sincerely, Archit Anant
Jonathan Cameron
maintainerQuoted 5 lines · On Tue, 17 Feb 2026 22:16:19 +0530 Archit Anant <[email protected]> wrote:
Quoted 3 lines · David Lechner
Actually, I just saw this was in staging, so I guess not breaking userspace isn't the top priority if there the other way is objectively better. I would assume that testing/measurement could show which is actually correct.
Quoted 9 lines
Since I don't have the hardware to perform those measurements and confirm if the precision improvement is valid or a regression, I will take the conservative
approach for v2.I will send a v2 that only replaces
do_div()withdiv64_ul()and usesBIT_ULL(27), preserving the original (mclk / 4) logic to guarantee no behavioral change.
I started writing that I didn't care either way and given the chances of not having to fix some ABI up on the way out of staging is near zero (which would break any userspace anyway) but then I took at look at the log. We've had a fixes for this fairly recently!
Might imply Gabriel has hardware? (or a 'healthy' interest in reading data sheets! :)
So +CC Gabriel who might have some insight.
Thanks,
Jonathan
Quoted 24 lines · Jonathan Cameron
Quoted 5 lines · On Tue, 17 Feb 2026 22:16:19 +0530 Archit Anant <[email protected]> wrote:
Quoted 3 lines · David Lechner
Actually, I just saw this was in staging, so I guess not breaking userspace isn't the top priority if there the other way is objectively better. I would assume that testing/measurement could show which is actually correct.
Quoted 9 lines
Since I don't have the hardware to perform those measurements and confirm if the precision improvement is valid or a regression, I will take the conservative
approach for v2.I will send a v2 that only replaces
do_div()withdiv64_ul()and usesBIT_ULL(27), preserving the original (mclk / 4) logic to guarantee no behavioral change.I started writing that I didn't care either way and given the chances of not having to fix some ABI up on the way out of staging is near zero (which would break any userspace anyway) but then I took at look at the log. We've had a fixes for this fairly recently!
Quoted 8 lines
Might imply Gabriel has hardware? (or a 'healthy' interest in reading data sheets! :)
So +CC Gabriel who might have some insight.
Thanks,
Jonathan
Hi Jonathan,
Thanks for the lead.
Hi Gabriel,
I sent out a v2 of this patch earlier today [1] using BIT_ULL(27) while preserving the original (mclk / 4) logic for safety.
If you have access to the hardware or documentation, I would appreciate any insight into whether Andy's suggested simplification to (mclk) instead of (mclk / 4) would be a valid precision improvement or if the original truncation was intentional.
[1] https://lore.kernel.org/all/[email protected]/
Signature · 2 lines
Sincerely, Archit Anant
Quoted 7 lines · Archit Anant
Gentle ping on this.
Has anyone from AD had a chance to confirm whether the original truncation via (mclk / 4) was intentional?
If there are no objections, I can prepare a v2 using the
BIT_ULL(29)form as suggested.
Gentle ping on reading and understanding the given comments, please!
Quoted 7 lines
Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Archit Anant
(Resending to the list, apologies for the private reply earlier.)
First of all, do no top post!
^^^ Have you understood this?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 15 lines · Andy Shevchenko
Quoted 7 lines · Archit Anant
Gentle ping on this.
Has anyone from AD had a chance to confirm whether the original truncation via (mclk / 4) was intentional?
If there are no objections, I can prepare a v2 using the
BIT_ULL(29)form as suggested.Gentle ping on reading and understanding the given comments, please!
Quoted 3 lines
Quoted 1 line · Andy Shevchenko
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines · Archit Anant
Quoted 1 line
(Resending to the list, apologies for the private reply earlier.)
First of all, do no top post!
^^^ Have you understood this?
Yes, understood. I’ll avoid top posting going forward.
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
Signature · 2 lines
Sincerely, Archit Anant