public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: frequency: adf4350: replace loop with order_base_2()
@ 2026-03-11  2:01 Neel Bullywon
  2026-03-11 12:07 ` Andy Shevchenko
  2026-03-14 17:20 ` [PATCH v2] iio: frequency: adf4350: replace loop with fls_long() Neel Bullywon
  0 siblings, 2 replies; 10+ messages in thread
From: Neel Bullywon @ 2026-03-11  2:01 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23
  Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel, Neel Bullywon

Address the TODO in adf4350_set_freq() by replacing the iterative
power-of-2 shift loop with the standard order_base_2() macro.

By utilizing DIV_ROUND_UP_ULL(), we
can calculate the required RF divider selection in a single step.

This ensures freq is properly shifted to meet or exceed the minimum
VCO frequency.

Signed-off-by: Neel Bullywon <neelb2403@gmail.com>
---
 drivers/iio/frequency/adf4350.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/frequency/adf4350.c b/drivers/iio/frequency/adf4350.c
index ed1741165f55..e07be6d5e67e 100644
--- a/drivers/iio/frequency/adf4350.c
+++ b/drivers/iio/frequency/adf4350.c
@@ -17,6 +17,8 @@
 #include <linux/err.h>
 #include <linux/gcd.h>
 #include <linux/gpio/consumer.h>
+#include <linux/log2.h>
+#include <linux/math64.h>
 #include <asm/div64.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
@@ -149,18 +151,12 @@ static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
 	if (freq > ADF4350_MAX_OUT_FREQ || freq < st->min_out_freq)
 		return -EINVAL;
 
-	st->r4_rf_div_sel = 0;
-
 	/*
-	 * !\TODO: The below computation is making sure we get a power of 2
-	 * shift (st->r4_rf_div_sel) so that freq becomes higher or equal to
-	 * ADF4350_MIN_VCO_FREQ. This might be simplified with fls()/fls_long()
-	 * and friends.
+	 * Calculate the required RF divider selection (power of 2 shift)
+	 * to ensure the VCO frequency is >= ADF4350_MIN_VCO_FREQ.
 	 */
-	while (freq < ADF4350_MIN_VCO_FREQ) {
-		freq <<= 1;
-		st->r4_rf_div_sel++;
-	}
+	st->r4_rf_div_sel = order_base_2(DIV_ROUND_UP_ULL(ADF4350_MIN_VCO_FREQ, freq));
+	freq <<= st->r4_rf_div_sel;
 
 	if (freq > ADF4350_MAX_FREQ_45_PRESC) {
 		prescaler = ADF4350_REG1_PRESCALER;
-- 
2.44.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-03-23 13:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-11  2:01 [PATCH] iio: frequency: adf4350: replace loop with order_base_2() Neel Bullywon
2026-03-11 12:07 ` Andy Shevchenko
2026-03-14 17:20 ` [PATCH v2] iio: frequency: adf4350: replace loop with fls_long() Neel Bullywon
2026-03-15 13:00   ` Jonathan Cameron
2026-03-16 12:40   ` Andy Shevchenko
2026-03-22 11:51     ` Jonathan Cameron
2026-03-16 13:51   ` David Laight
2026-03-16 14:07     ` Andy Shevchenko
2026-03-23 11:08       ` Nuno Sá
2026-03-23 13:31         ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox