[PATCH 03/27] rtc: ac100: convert from divider_round_rate() to divider_determine_rate()

Brian Masney posted 27 patches 1 month ago
[PATCH 03/27] rtc: ac100: convert from divider_round_rate() to divider_determine_rate()
Posted by Brian Masney 1 month ago
The divider_round_rate() function is now deprecated, so let's migrate
to divider_determine_rate() instead so that this deprecated API can be
removed.

Signed-off-by: Brian Masney <bmasney@redhat.com>

---
To: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: linux-rtc@vger.kernel.org
---
 drivers/rtc/rtc-ac100.c | 75 +++++++++++++++++++++++++------------------------
 1 file changed, 38 insertions(+), 37 deletions(-)

diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
index 33626311fa781b5ce90dcc472f948dc933bbc897..16aca4431da8c029e6195d8a3c9fe75fa95d0bc0 100644
--- a/drivers/rtc/rtc-ac100.c
+++ b/drivers/rtc/rtc-ac100.c
@@ -140,42 +140,16 @@ static unsigned long ac100_clkout_recalc_rate(struct clk_hw *hw,
 				   AC100_CLKOUT_DIV_WIDTH);
 }
 
-static long ac100_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
-				    unsigned long prate)
-{
-	unsigned long best_rate = 0, tmp_rate, tmp_prate;
-	int i;
-
-	if (prate == AC100_RTC_32K_RATE)
-		return divider_round_rate(hw, rate, &prate, NULL,
-					  AC100_CLKOUT_DIV_WIDTH,
-					  CLK_DIVIDER_POWER_OF_TWO);
-
-	for (i = 0; ac100_clkout_prediv[i].div; i++) {
-		tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[i].val);
-		tmp_rate = divider_round_rate(hw, rate, &tmp_prate, NULL,
-					      AC100_CLKOUT_DIV_WIDTH,
-					      CLK_DIVIDER_POWER_OF_TWO);
-
-		if (tmp_rate > rate)
-			continue;
-		if (rate - tmp_rate < best_rate - tmp_rate)
-			best_rate = tmp_rate;
-	}
-
-	return best_rate;
-}
-
 static int ac100_clkout_determine_rate(struct clk_hw *hw,
 				       struct clk_rate_request *req)
 {
-	struct clk_hw *best_parent;
+	int i, ret, num_parents = clk_hw_get_num_parents(hw);
+	struct clk_hw *best_parent = NULL;
 	unsigned long best = 0;
-	int i, num_parents = clk_hw_get_num_parents(hw);
 
 	for (i = 0; i < num_parents; i++) {
 		struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
-		unsigned long tmp, prate;
+		unsigned long prate;
 
 		/*
 		 * The clock has two parents, one is a fixed clock which is
@@ -199,13 +173,40 @@ static int ac100_clkout_determine_rate(struct clk_hw *hw,
 
 		prate = clk_hw_get_rate(parent);
 
-		tmp = ac100_clkout_round_rate(hw, req->rate, prate);
-
-		if (tmp > req->rate)
-			continue;
-		if (req->rate - tmp < req->rate - best) {
-			best = tmp;
-			best_parent = parent;
+		if (prate == AC100_RTC_32K_RATE) {
+			struct clk_rate_request div_req = *req;
+
+			div_req.best_parent_rate = prate;
+
+			ret = divider_determine_rate(hw, &div_req, NULL,
+						     AC100_CLKOUT_DIV_WIDTH,
+						     CLK_DIVIDER_POWER_OF_TWO);
+			if (ret != 0 || div_req.rate > req->rate)
+				continue;
+			else if (req->rate - div_req.rate < req->rate - best) {
+				best = div_req.rate;
+				best_parent = parent;
+			}
+		} else {
+			int j;
+
+			for (j = 0; ac100_clkout_prediv[j].div; j++) {
+				struct clk_rate_request div_req = *req;
+				unsigned long tmp_prate;
+
+				tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[j].div);
+				div_req.best_parent_rate = tmp_prate;
+
+				ret = divider_determine_rate(hw, &div_req, NULL,
+							     AC100_CLKOUT_DIV_WIDTH,
+							     CLK_DIVIDER_POWER_OF_TWO);
+				if (ret != 0 || div_req.rate > req->rate)
+					continue;
+				else if (req->rate - div_req.rate < req->rate - best) {
+					best = div_req.rate;
+					best_parent = parent;
+				}
+			}
 		}
 	}
 
@@ -213,7 +214,7 @@ static int ac100_clkout_determine_rate(struct clk_hw *hw,
 		return -EINVAL;
 
 	req->best_parent_hw = best_parent;
-	req->best_parent_rate = best;
+	req->best_parent_rate = clk_hw_get_rate(best_parent);
 	req->rate = best;
 
 	return 0;

-- 
2.52.0
Re: [PATCH 03/27] rtc: ac100: convert from divider_round_rate() to divider_determine_rate()
Posted by Alexandre Belloni 2 weeks, 4 days ago
Hello,

On 08/01/2026 16:16:21-0500, Brian Masney wrote:
> The divider_round_rate() function is now deprecated, so let's migrate
> to divider_determine_rate() instead so that this deprecated API can be
> removed.
> 
> Signed-off-by: Brian Masney <bmasney@redhat.com>
> 
> ---
> To: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Cc: linux-rtc@vger.kernel.org
> ---
>  drivers/rtc/rtc-ac100.c | 75 +++++++++++++++++++++++++------------------------
>  1 file changed, 38 insertions(+), 37 deletions(-)
> 
> diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> index 33626311fa781b5ce90dcc472f948dc933bbc897..16aca4431da8c029e6195d8a3c9fe75fa95d0bc0 100644
> --- a/drivers/rtc/rtc-ac100.c
> +++ b/drivers/rtc/rtc-ac100.c
> @@ -140,42 +140,16 @@ static unsigned long ac100_clkout_recalc_rate(struct clk_hw *hw,
>  				   AC100_CLKOUT_DIV_WIDTH);
>  }
>  
> -static long ac100_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
> -				    unsigned long prate)
> -{
> -	unsigned long best_rate = 0, tmp_rate, tmp_prate;
> -	int i;
> -
> -	if (prate == AC100_RTC_32K_RATE)
> -		return divider_round_rate(hw, rate, &prate, NULL,
> -					  AC100_CLKOUT_DIV_WIDTH,
> -					  CLK_DIVIDER_POWER_OF_TWO);
> -
> -	for (i = 0; ac100_clkout_prediv[i].div; i++) {
> -		tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[i].val);
> -		tmp_rate = divider_round_rate(hw, rate, &tmp_prate, NULL,
> -					      AC100_CLKOUT_DIV_WIDTH,
> -					      CLK_DIVIDER_POWER_OF_TWO);
> -
> -		if (tmp_rate > rate)
> -			continue;
> -		if (rate - tmp_rate < best_rate - tmp_rate)
> -			best_rate = tmp_rate;
> -	}
> -
> -	return best_rate;
> -}
> -
>  static int ac100_clkout_determine_rate(struct clk_hw *hw,
>  				       struct clk_rate_request *req)
>  {
> -	struct clk_hw *best_parent;
> +	int i, ret, num_parents = clk_hw_get_num_parents(hw);
> +	struct clk_hw *best_parent = NULL;
>  	unsigned long best = 0;
> -	int i, num_parents = clk_hw_get_num_parents(hw);
>  
>  	for (i = 0; i < num_parents; i++) {
>  		struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
> -		unsigned long tmp, prate;
> +		unsigned long prate;
>  
>  		/*
>  		 * The clock has two parents, one is a fixed clock which is
> @@ -199,13 +173,40 @@ static int ac100_clkout_determine_rate(struct clk_hw *hw,
>  
>  		prate = clk_hw_get_rate(parent);
>  
> -		tmp = ac100_clkout_round_rate(hw, req->rate, prate);
> -
> -		if (tmp > req->rate)
> -			continue;
> -		if (req->rate - tmp < req->rate - best) {
> -			best = tmp;
> -			best_parent = parent;
> +		if (prate == AC100_RTC_32K_RATE) {
> +			struct clk_rate_request div_req = *req;
> +
> +			div_req.best_parent_rate = prate;
> +
> +			ret = divider_determine_rate(hw, &div_req, NULL,
> +						     AC100_CLKOUT_DIV_WIDTH,
> +						     CLK_DIVIDER_POWER_OF_TWO);
> +			if (ret != 0 || div_req.rate > req->rate)
> +				continue;

This leaves a braces inbalance

> +			else if (req->rate - div_req.rate < req->rate - best) {
> +				best = div_req.rate;
> +				best_parent = parent;
> +			}
> +		} else {
> +			int j;
> +
> +			for (j = 0; ac100_clkout_prediv[j].div; j++) {
> +				struct clk_rate_request div_req = *req;
> +				unsigned long tmp_prate;
> +
> +				tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[j].div);
> +				div_req.best_parent_rate = tmp_prate;
> +
> +				ret = divider_determine_rate(hw, &div_req, NULL,
> +							     AC100_CLKOUT_DIV_WIDTH,
> +							     CLK_DIVIDER_POWER_OF_TWO);
> +				if (ret != 0 || div_req.rate > req->rate)
> +					continue;

Ditto.

> +				else if (req->rate - div_req.rate < req->rate - best) {
> +					best = div_req.rate;
> +					best_parent = parent;
> +				}
> +			}
>  		}
>  	}


-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH 03/27] rtc: ac100: convert from divider_round_rate() to divider_determine_rate()
Posted by Brian Masney 2 weeks, 4 days ago
Hi Alexandre,

On Thu, Jan 22, 2026 at 01:26:09AM +0100, Alexandre Belloni wrote:
> On 08/01/2026 16:16:21-0500, Brian Masney wrote:
> > The divider_round_rate() function is now deprecated, so let's migrate
> > to divider_determine_rate() instead so that this deprecated API can be
> > removed.
> > 
> > Signed-off-by: Brian Masney <bmasney@redhat.com>
> > 
> > ---
> > To: Alexandre Belloni <alexandre.belloni@bootlin.com>
> > Cc: linux-rtc@vger.kernel.org
> > ---
> >  drivers/rtc/rtc-ac100.c | 75 +++++++++++++++++++++++++------------------------
> >  1 file changed, 38 insertions(+), 37 deletions(-)
> > 
> > diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> > index 33626311fa781b5ce90dcc472f948dc933bbc897..16aca4431da8c029e6195d8a3c9fe75fa95d0bc0 100644
> > --- a/drivers/rtc/rtc-ac100.c
> > +++ b/drivers/rtc/rtc-ac100.c
> > @@ -140,42 +140,16 @@ static unsigned long ac100_clkout_recalc_rate(struct clk_hw *hw,
> >  				   AC100_CLKOUT_DIV_WIDTH);
> >  }
> >  
> > -static long ac100_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
> > -				    unsigned long prate)
> > -{
> > -	unsigned long best_rate = 0, tmp_rate, tmp_prate;
> > -	int i;
> > -
> > -	if (prate == AC100_RTC_32K_RATE)
> > -		return divider_round_rate(hw, rate, &prate, NULL,
> > -					  AC100_CLKOUT_DIV_WIDTH,
> > -					  CLK_DIVIDER_POWER_OF_TWO);
> > -
> > -	for (i = 0; ac100_clkout_prediv[i].div; i++) {
> > -		tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[i].val);
> > -		tmp_rate = divider_round_rate(hw, rate, &tmp_prate, NULL,
> > -					      AC100_CLKOUT_DIV_WIDTH,
> > -					      CLK_DIVIDER_POWER_OF_TWO);
> > -
> > -		if (tmp_rate > rate)
> > -			continue;
> > -		if (rate - tmp_rate < best_rate - tmp_rate)
> > -			best_rate = tmp_rate;
> > -	}
> > -
> > -	return best_rate;
> > -}
> > -
> >  static int ac100_clkout_determine_rate(struct clk_hw *hw,
> >  				       struct clk_rate_request *req)
> >  {
> > -	struct clk_hw *best_parent;
> > +	int i, ret, num_parents = clk_hw_get_num_parents(hw);
> > +	struct clk_hw *best_parent = NULL;
> >  	unsigned long best = 0;
> > -	int i, num_parents = clk_hw_get_num_parents(hw);
> >  
> >  	for (i = 0; i < num_parents; i++) {
> >  		struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
> > -		unsigned long tmp, prate;
> > +		unsigned long prate;
> >  
> >  		/*
> >  		 * The clock has two parents, one is a fixed clock which is
> > @@ -199,13 +173,40 @@ static int ac100_clkout_determine_rate(struct clk_hw *hw,
> >  
> >  		prate = clk_hw_get_rate(parent);
> >  
> > -		tmp = ac100_clkout_round_rate(hw, req->rate, prate);
> > -
> > -		if (tmp > req->rate)
> > -			continue;
> > -		if (req->rate - tmp < req->rate - best) {
> > -			best = tmp;
> > -			best_parent = parent;
> > +		if (prate == AC100_RTC_32K_RATE) {
> > +			struct clk_rate_request div_req = *req;
> > +
> > +			div_req.best_parent_rate = prate;
> > +
> > +			ret = divider_determine_rate(hw, &div_req, NULL,
> > +						     AC100_CLKOUT_DIV_WIDTH,
> > +						     CLK_DIVIDER_POWER_OF_TWO);
> > +			if (ret != 0 || div_req.rate > req->rate)
> > +				continue;
> 
> This leaves a braces inbalance

To be clear, you also want the braces around the if like this:

if (ret != 0 || div_req.rate > req->rate) {
	continue;
} else if (req->rate - div_req.rate < req->rate - best) {
	best = div_req.rate;
	best_parent = parent;
}

Brian
Re: [PATCH 03/27] rtc: ac100: convert from divider_round_rate() to divider_determine_rate()
Posted by Alexandre Belloni 2 weeks, 4 days ago
On 21/01/2026 19:48:19-0500, Brian Masney wrote:
> Hi Alexandre,
> 
> On Thu, Jan 22, 2026 at 01:26:09AM +0100, Alexandre Belloni wrote:
> > On 08/01/2026 16:16:21-0500, Brian Masney wrote:
> > > The divider_round_rate() function is now deprecated, so let's migrate
> > > to divider_determine_rate() instead so that this deprecated API can be
> > > removed.
> > > 
> > > Signed-off-by: Brian Masney <bmasney@redhat.com>
> > > 
> > > ---
> > > To: Alexandre Belloni <alexandre.belloni@bootlin.com>
> > > Cc: linux-rtc@vger.kernel.org
> > > ---
> > >  drivers/rtc/rtc-ac100.c | 75 +++++++++++++++++++++++++------------------------
> > >  1 file changed, 38 insertions(+), 37 deletions(-)
> > > 
> > > diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c
> > > index 33626311fa781b5ce90dcc472f948dc933bbc897..16aca4431da8c029e6195d8a3c9fe75fa95d0bc0 100644
> > > --- a/drivers/rtc/rtc-ac100.c
> > > +++ b/drivers/rtc/rtc-ac100.c
> > > @@ -140,42 +140,16 @@ static unsigned long ac100_clkout_recalc_rate(struct clk_hw *hw,
> > >  				   AC100_CLKOUT_DIV_WIDTH);
> > >  }
> > >  
> > > -static long ac100_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
> > > -				    unsigned long prate)
> > > -{
> > > -	unsigned long best_rate = 0, tmp_rate, tmp_prate;
> > > -	int i;
> > > -
> > > -	if (prate == AC100_RTC_32K_RATE)
> > > -		return divider_round_rate(hw, rate, &prate, NULL,
> > > -					  AC100_CLKOUT_DIV_WIDTH,
> > > -					  CLK_DIVIDER_POWER_OF_TWO);
> > > -
> > > -	for (i = 0; ac100_clkout_prediv[i].div; i++) {
> > > -		tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[i].val);
> > > -		tmp_rate = divider_round_rate(hw, rate, &tmp_prate, NULL,
> > > -					      AC100_CLKOUT_DIV_WIDTH,
> > > -					      CLK_DIVIDER_POWER_OF_TWO);
> > > -
> > > -		if (tmp_rate > rate)
> > > -			continue;
> > > -		if (rate - tmp_rate < best_rate - tmp_rate)
> > > -			best_rate = tmp_rate;
> > > -	}
> > > -
> > > -	return best_rate;
> > > -}
> > > -
> > >  static int ac100_clkout_determine_rate(struct clk_hw *hw,
> > >  				       struct clk_rate_request *req)
> > >  {
> > > -	struct clk_hw *best_parent;
> > > +	int i, ret, num_parents = clk_hw_get_num_parents(hw);
> > > +	struct clk_hw *best_parent = NULL;
> > >  	unsigned long best = 0;
> > > -	int i, num_parents = clk_hw_get_num_parents(hw);
> > >  
> > >  	for (i = 0; i < num_parents; i++) {
> > >  		struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
> > > -		unsigned long tmp, prate;
> > > +		unsigned long prate;
> > >  
> > >  		/*
> > >  		 * The clock has two parents, one is a fixed clock which is
> > > @@ -199,13 +173,40 @@ static int ac100_clkout_determine_rate(struct clk_hw *hw,
> > >  
> > >  		prate = clk_hw_get_rate(parent);
> > >  
> > > -		tmp = ac100_clkout_round_rate(hw, req->rate, prate);
> > > -
> > > -		if (tmp > req->rate)
> > > -			continue;
> > > -		if (req->rate - tmp < req->rate - best) {
> > > -			best = tmp;
> > > -			best_parent = parent;
> > > +		if (prate == AC100_RTC_32K_RATE) {
> > > +			struct clk_rate_request div_req = *req;
> > > +
> > > +			div_req.best_parent_rate = prate;
> > > +
> > > +			ret = divider_determine_rate(hw, &div_req, NULL,
> > > +						     AC100_CLKOUT_DIV_WIDTH,
> > > +						     CLK_DIVIDER_POWER_OF_TWO);
> > > +			if (ret != 0 || div_req.rate > req->rate)
> > > +				continue;
> > 
> > This leaves a braces inbalance
> 
> To be clear, you also want the braces around the if like this:
> 
> if (ret != 0 || div_req.rate > req->rate) {
> 	continue;
> } else if (req->rate - div_req.rate < req->rate - best) {
> 	best = div_req.rate;
> 	best_parent = parent;
> }
> 

Yes, you can change that and add my ack on v2, I'm fine with this going
through clk.


-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com