Re: [PATCH] iio: frequency: ad983x: replace do_div() with div64_ul().

From: Andy Shevchenko

Date: Thu Apr 09 2026 - 11:57:59 EST


On Thu, Apr 09, 2026 at 01:18:23PM +0000, Joshua Crofts wrote:
> Coccinelle reported the following in both ad983x drivers:
> do_div() does a 64-by-32 division, please consider using
> div64_ul instead.

div64_ul()

> Fix this by replacing do_div() with div64_ul(), which
> safely handles 64-bit dividends and unsigned long
> divisors across all architectures.

You should really dive into this code, it has more to work on...

...

> #include <linux/device.h>
> #include <linux/err.h>

> #include <linux/kernel.h>

Consider replacing "proxy" headers at some point. (Not related to this patch,
though.)

> +#include <linux/math64.h>
> #include <linux/module.h>
> #include <linux/regulator/consumer.h>
> #include <linux/slab.h>

...

> static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
> {
> unsigned long long freqreg = (u64)fout *
> (u64)((u64)1L << AD9832_FREQ_BITS);

(u64)1L is a fancy way of doing 1ULL. The second (u64) is also redundant and
altogether it's just a BIT_ULL(). But, check the value of _FREQ_BITS. If it's less than 32, the whole train of nonsense can be collapsed just to a BIT().

> - do_div(freqreg, mclk);
> + freqreg = div64_ul(freqreg, mclk);
> return freqreg;
> }

...

> --- a/drivers/staging/iio/frequency/ad9834.c
> +++ b/drivers/staging/iio/frequency/ad9834.c
> @@ -10,6 +10,7 @@
> #include <linux/workqueue.h>
> #include <linux/device.h>
> #include <linux/kernel.h>
> +#include <linux/math64.h>

Side note as per above.

> #include <linux/slab.h>
> #include <linux/sysfs.h>
> #include <linux/list.h>
> @@ -101,7 +102,7 @@ static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
> {
> unsigned long long freqreg = (u64)fout * (u64)BIT(AD9834_FREQ_BITS);
>
> - do_div(freqreg, mclk);
> + freqreg = div64_ul(freqreg, mclk);
> return freqreg;
> }

Ditto. Also split changes on per-driver basis.

...

So, if you go suggested way, I would expect to have 4 patches (2 series by 2
patches each) out of this one.

--
With Best Regards,
Andy Shevchenko