Skip to contentSkip to conversation

iio: frequency: ad9832: cleanups

PATCH Superseded Joshua Crofts 3 participants 17 messages Apr 11 – Apr 15

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover iio: frequency: ad9832: cleanups

This series cleans up issues in the AD9832 driver, including proxy header removal and bit math simplification.

v2:
- PATCH 1: removed redundant bits.h include as it is implied in bitops.h
- PATCH 2: changed ull to u64 type
v3:
- PATCH 2: remove rogue vim typo

Joshua Crofts (2):
iio: frequency: ad9832: remove kernel.h proxy header.
iio: frequency: ad9832: simplify bitwise math

 drivers/staging/iio/frequency/ad9832.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/2 iio: frequency: ad9832: remove kernel.h proxy header.

Remove kernel.h proxy header and add bitops.h for
better dependency control and code clarity.

---
v2:
- removed redundant bits.h header

 drivers/staging/iio/frequency/ad9832.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9832.c +1 -2
index b87ea1781b..8873a6d11e 100644
@@ -8,11 +8,10 @@
 #include <asm/div64.h>
 
 #include <linux/bitfield.h>
-#include <linux/bits.h>
+#include <linux/bitops.h>
 #include <linux/clk.h>
 #include <linux/device.h>
 #include <linux/err.h>
-#include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/2] iio: frequency: ad9832: remove kernel.h proxy header.

Quoted 2 lines · Joshua Crofts

Remove kernel.h proxy header and add bitops.h for
better dependency control and code clarity.

...

Quoted 1 line

#include <asm/div64.h>

While at it, you can move asm/* to go after generic linux/* ones as asm is more custom than linux.

drivers/staging/iio/frequency/ad9832.c line 13

Quoted 6 lines

#include <linux/bitfield.h>

drivers/staging/iio/frequency/ad9832.c +1 -1
-#include <linux/bits.h>
+#include <linux/bitops.h>
 #include <linux/clk.h>
 
 #include <linux/device.h>

What about this one? Is it used?

Many drivers just need dev_printk.h + device/devres.h. Some indeed require device.h. Please, double check.

Quoted 5 lines

#include <linux/err.h>
-#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>

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

Re: [PATCH v3 1/2] iio: frequency: ad9832: remove kernel.h proxy header.

Quoted 4 lines · Andy Shevchenko
Quoted 1 line

#include <asm/div64.h>

While at it, you can move asm/* to go after generic linux/* ones as asm is more custom than linux.

Sure.

Quoted 6 lines
Quoted 1 line

#include <linux/device.h>

What about this one? Is it used?

Many drivers just need dev_printk.h + device/devres.h. Some indeed require device.h. Please, double check.

I compiled it with the include combination above and it passed, but looking into the generated .i file I can see a full struct device definition, so I assume there's
some sort of transitive include behavior (spi.h or something)

Signature · 3 lines
Kind regards

CJD
#4 lore raw

Re: [PATCH v3 1/2] iio: frequency: ad9832: remove kernel.h proxy header.

Quoted 2 lines · Joshua Crofts

On Tue, 14 Apr 2026 at 11:38, Andy Shevchenko
<[email protected]> wrote:

...

Quoted 11 lines
Quoted 6 lines
Quoted 1 line

#include <linux/device.h>

What about this one? Is it used?

Many drivers just need dev_printk.h + device/devres.h. Some indeed require device.h. Please, double check.

I compiled it with the include combination above and it passed, but looking into the generated .i file I can see a full struct device definition, so I assume there's
some sort of transitive include behavior (spi.h or something)

Please, avoid following the suggestions blindly. This work needs a careful check of what's exactly is being used and which header should provide the necessary information. While it may compile it might be still wrong. You need to (briefly) read the code in full. I don't use iwyu tool with tweaks by Jonathan Cameron, old school guy, but you can also try it [1].

[1]: https://lore.kernel.org/linux-iio/[email protected]/
Read the whole thread, it's not big.

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

Re: [PATCH v3 1/2] iio: frequency: ad9832: remove kernel.h proxy header.

Quoted 5 lines · Andy Shevchenko

Please, avoid following the suggestions blindly. This work needs a careful check of what's exactly is being used and which header should provide the necessary information. While it may compile it might be still wrong. You need to (briefly) read the code in full. I don't use iwyu tool with tweaks by Jonathan Cameron, old school guy, but you can also try it [1].

Don't worry, I wasn't blindly following your suggestions. After some tweaking, IWYU recommends keeping device.h and removing slab.h. My question is whether the swap from asm/div64.h to linux/math64.h is valid? The asm implementation would be platform specific, so swapping to math64 seems like a good idea.

Signature · 3 lines
Kind regards

CJD
#6 lore raw

Re: [PATCH v3 1/2] iio: frequency: ad9832: remove kernel.h proxy header.

Quoted 8 lines · Joshua Crofts
Quoted 5 lines · Andy Shevchenko

Please, avoid following the suggestions blindly. This work needs a careful check of what's exactly is being used and which header should provide the necessary information. While it may compile it might be still wrong. You need to (briefly) read the code in full. I don't use iwyu tool with tweaks by Jonathan Cameron, old school guy, but you can also try it [1].

Quoted 5 lines

Don't worry, I wasn't blindly following your suggestions. After some tweaking, IWYU recommends keeping device.h and removing slab.h. My question is whether the swap from asm/div64.h to linux/math64.h is valid? The asm implementation would be platform specific, so swapping to math64 seems like a good idea.

Up to you, but that would need to be a separate patch, because it does the opposite to what your patch is doing. And justification is most likely that it's preferred to use linux/ generic over asm/ in the drivers. Or something in those lines...

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

Re: [PATCH v3 1/2] iio: frequency: ad9832: remove kernel.h proxy header.

Quoted 4 lines · Andy Shevchenko

Up to you, but that would need to be a separate patch, because it does the opposite to what your patch is doing. And justification is most likely that it's preferred to use linux/ generic over asm/ in the drivers. Or something in those lines...

Okay, I'll send it once I get this patch series done.

Signature · 3 lines
Kind regards

CJD
#8 lore raw

PATCH v3 2/2 iio: frequency: ad9832: simplify bitwise math

In reply to Joshua Crofts

Refactor the ad9832_calc_freqreg by adding a BIT_ULL() macro instead of manual bit shifting for better readability.

---
v2:
- changed ull to u64 type
v3:
- removed rogue vim typo

 drivers/staging/iio/frequency/ad9832.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9832.c +2 -2
index 8873a6d11e..778aaa8747 100644
@@ -112,8 +112,8 @@ struct ad9832_state {
 
 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 freqreg = (u64)fout * BIT_ULL(AD9832_FREQ_BITS);
+
 	do_div(freqreg, mclk);
 	return freqreg;
 }
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 3 lines · Joshua Crofts

Refactor the ad9832_calc_freqreg by adding a BIT_ULL() macro instead of manual bit shifting for better readability.

"While at it, switch to u64 type for the local variable
that may hold big values."

...

drivers/staging/iio/frequency/ad9832.c struct ad9832_state { · line 115

Quoted 3 lines
drivers/staging/iio/frequency/ad9832.c +1 -2
-	unsigned long long freqreg = (u64)fout *
-				     (u64)((u64)1L << AD9832_FREQ_BITS);
+	u64 freqreg = (u64)fout * BIT_ULL(AD9832_FREQ_BITS);

It can even be just like this:

	u64 freqreg = fout * BIT_ULL(AD9832_FREQ_BITS);

if I understand the integer promotion for unsigned types here and the * operator correctly.

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

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 6 lines · Andy Shevchenko

It can even be just like this:

        u64 freqreg = fout * BIT_ULL(AD9832_FREQ_BITS);

if I understand the integer promotion for unsigned types here and the * operator correctly.

Sure, at least I'll ensure that the reviewed-by tag is incorporated correctly.

Signature · 3 lines
Kind regards

CJD
#11 lore raw

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 11 lines · Joshua Crofts
Quoted 6 lines · Andy Shevchenko

It can even be just like this:

        u64 freqreg = fout * BIT_ULL(AD9832_FREQ_BITS);

if I understand the integer promotion for unsigned types here and the * operator correctly.

Sure, at least I'll ensure that the reviewed-by tag is incorporated correctly.

It's up to you. (u64) casting here is not bad per se. And it's more robust against changes in the second operand the type of which is hidden currently. (Reading again what I just wrote, it seems I objecting my own suggestion!)

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

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 3 lines · Andy Shevchenko

It's up to you. (u64) casting here is not bad per se. And it's more robust against changes in the second operand the type of which is hidden currently. (Reading again what I just wrote, it seems I objecting my own suggestion!)

If I'm not mistaken, the compiler would always do a type promotion of the "smaller" operand (in this case fout). By casting at this point we're just doing the work for it, so I guess it doesn't matter.

Signature · 3 lines
Kind regards

CJD
#13 lore raw

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 9 lines · Joshua Crofts
Quoted 3 lines · Andy Shevchenko

It's up to you. (u64) casting here is not bad per se. And it's more robust against changes in the second operand the type of which is hidden currently. (Reading again what I just wrote, it seems I objecting my own suggestion!)

If I'm not mistaken, the compiler would always do a type promotion of the "smaller" operand (in this case fout). By casting at this point we're just doing the work for it, so I guess it doesn't matter.

Yes, and the problem here that BIT_ULL() is (semi)hidden on what it returns for smaller values. Explicit casting helps with that (while not needed). The problem that might appear in the future if on some circumstances the both operands become of 32-bit types... You won't get 64-bit value out of that without casting.

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

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Joshua Crofts
Quoted 3 lines · Andy Shevchenko

It's up to you. (u64) casting here is not bad per se. And it's more robust against changes in the second operand the type of which is hidden currently. (Reading again what I just wrote, it seems I objecting my own suggestion!)

Quoted 6 lines
Quoted 3 lines

If I'm not mistaken, the compiler would always do a type promotion of the "smaller" operand (in this case fout). By casting at this point we're just doing the work for it, so I guess it doesn't matter.

Yes, and the problem here that BIT_ULL() is (semi)hidden on what it returns for smaller values.

There is no way we'd implement a BIT_ULL() that doesn't give you a ULL.

regards,
dan carpenter

#15 lore raw

Re: [PATCH v3 2/2] iio: frequency: ad9832: simplify bitwise math

Quoted 8 lines · Dan Carpenter
Quoted 7 lines · Andy Shevchenko
Quoted 5 lines · Joshua Crofts
Quoted 3 lines · Andy Shevchenko

It's up to you. (u64) casting here is not bad per se. And it's more robust against changes in the second operand the type of which is hidden currently. (Reading again what I just wrote, it seems I objecting my own suggestion!)

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

If I'm not mistaken, the compiler would always do a type promotion of the "smaller" operand (in this case fout). By casting at this point we're just doing the work for it, so I guess it doesn't matter.

Yes, and the problem here that BIT_ULL() is (semi)hidden on what it returns for smaller values.

There is no way we'd implement a BIT_ULL() that doesn't give you a ULL.

I meant that it's not crystal clear, one needs to know the naming convention of BIT_ULL() and/or check the implementation. Reading (u64) is much clearer in that sense. Also in case of BIT_ULL() parameter being < 32 it may be replaced with BIT().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author Reviewed-by Nuno Sá on the v3 series
#16 lore raw
In reply to Joshua Crofts
Quoted 5 lines · Joshua Crofts

This series cleans up issues in the AD9832 driver, including proxy header removal and bit math simplification.

Quoted 7 lines

v2:
- PATCH 1: removed redundant bits.h include as it is implied in bitops.h
- PATCH 2: changed ull to u64 type
v3:
- PATCH 2: remove rogue vim typo

FYI, adding Nuno's reviewed-by tag from v2 as v3 is just a typo fix.