2014-01-29 16:25:17

by Maxime Coquelin

[permalink] [raw]
Subject: [PATCH 0/3] CCF clock divider changes

Hi Mike,

First patch fixes round_rate for power-of-two and table-based dividers when
the CLK_SET_RATE_PARENT flag isn't set.

Second patch introduces a new flag to give the possibility to round the
divider to the closest one, instead of rounding up by default.

Third patch optimizes the for-loop when CLK_SET_RATE_PARENT is set for
power-of-two and table-based dividers.

The series applies on top of v3.13 tag.

Maxime Coquelin (3):
clk: divider: Fix best div calculation for power-of-two and table
dividers
clk: divider: Add round to closest divider
clk: divider: Optimize clk_divider_bestdiv loop

drivers/clk/clk-divider.c | 118 ++++++++++++++++++++++++++++++++++++++++--
include/linux/clk-provider.h | 3 ++
2 files changed, 118 insertions(+), 3 deletions(-)

--
1.7.9.5


2014-01-29 16:25:15

by Maxime Coquelin

[permalink] [raw]
Subject: [PATCH 2/3] clk: divider: Add round to closest divider

In some cases, we want to be able to round the divider to the closest one,
instead than rounding up.

This patch adds a new CLK_DIVIDER_ROUND_CLOSEST flag to specify the divider
has to round to closest div, keeping rounding up as de default behaviour.

Signed-off-by: Maxime Coquelin <[email protected]>
---
drivers/clk/clk-divider.c | 69 ++++++++++++++++++++++++++++++++++++++++--
include/linux/clk-provider.h | 3 ++
2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 2137d58..1eaa5d8 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -43,6 +43,17 @@ static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
return maxdiv;
}

+static unsigned int _get_table_mindiv(const struct clk_div_table *table)
+{
+ unsigned int mindiv = UINT_MAX;
+ const struct clk_div_table *clkt;
+
+ for (clkt = table; clkt->div; clkt++)
+ if (clkt->div < mindiv)
+ mindiv = clkt->div;
+ return mindiv;
+}
+
static unsigned int _get_maxdiv(struct clk_divider *divider)
{
if (divider->flags & CLK_DIVIDER_ONE_BASED)
@@ -162,6 +173,24 @@ static int _round_up_table(const struct clk_div_table *table, int div)
return up;
}

+static int _round_down_table(const struct clk_div_table *table, int div)
+{
+ const struct clk_div_table *clkt;
+ int down = _get_table_mindiv(table);
+
+ for (clkt = table; clkt->div; clkt++) {
+ if (clkt->div == div)
+ return clkt->div;
+ else if (clkt->div > div)
+ continue;
+
+ if ((div - clkt->div) < (div - down))
+ down = clkt->div;
+ }
+
+ return down;
+}
+
static int _div_round_up(struct clk_divider *divider,
unsigned long parent_rate, unsigned long rate)
{
@@ -175,6 +204,42 @@ static int _div_round_up(struct clk_divider *divider,
return div;
}

+static int _div_round_closest(struct clk_divider *divider,
+ unsigned long parent_rate, unsigned long rate)
+{
+ int up, down, div;
+
+ up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
+
+ if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) {
+ up = __roundup_pow_of_two(div);
+ down = __rounddown_pow_of_two(div);
+ } else if (divider->table) {
+ up = _round_up_table(divider->table, div);
+ down = _round_down_table(divider->table, div);
+ }
+
+ return (up - div) <= (div - down) ? up : down;
+}
+
+static int _div_round(struct clk_divider *divider, unsigned long parent_rate,
+ unsigned long rate)
+{
+ if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
+ return _div_round_closest(divider, parent_rate, rate);
+
+ return _div_round_up(divider, parent_rate, rate);
+}
+
+static bool _is_best_div(struct clk_divider *divider,
+ int rate, int now, int best)
+{
+ if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
+ return abs(rate - now) < abs(rate - best);
+
+ return now <= rate && now > best;
+}
+
static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
unsigned long *best_parent_rate)
{
@@ -190,7 +255,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,

if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
parent_rate = *best_parent_rate;
- bestdiv = _div_round_up(divider, parent_rate, rate);
+ bestdiv = _div_round(divider, parent_rate, rate);
bestdiv = bestdiv == 0 ? 1 : bestdiv;
bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
return bestdiv;
@@ -217,7 +282,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
MULT_ROUND_UP(rate, i));
now = parent_rate / i;
- if (now <= rate && now > best) {
+ if (_is_best_div(divider, rate, now, best)) {
bestdiv = i;
best = now;
*best_parent_rate = parent_rate;
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 7e59253..595f0ba 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -275,6 +275,8 @@ struct clk_div_table {
* of this register, and mask of divider bits are in higher 16-bit of this
* register. While setting the divider bits, higher 16-bit should also be
* updated to indicate changing divider bits.
+ * CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
+ * to the closest integer instead of the up one.
*/
struct clk_divider {
struct clk_hw hw;
@@ -290,6 +292,7 @@ struct clk_divider {
#define CLK_DIVIDER_POWER_OF_TWO BIT(1)
#define CLK_DIVIDER_ALLOW_ZERO BIT(2)
#define CLK_DIVIDER_HIWORD_MASK BIT(3)
+#define CLK_DIVIDER_ROUND_CLOSEST BIT(4)

extern const struct clk_ops clk_divider_ops;
struct clk *clk_register_divider(struct device *dev, const char *name,
--
1.7.9.5

2014-01-29 16:25:14

by Maxime Coquelin

[permalink] [raw]
Subject: [PATCH 3/3] clk: divider: Optimize clk_divider_bestdiv loop

Currently, the for-loop used to try all the different dividers to find the
one that best fit tries all the values from 1 to max_div, incrementing by one.
In case of power-of-two, or table based divider, the loop isn't optimal.

Instead of incrementing by one, this patch provides directly the next divider.

Signed-off-by: Maxime Coquelin <[email protected]>
---
drivers/clk/clk-divider.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 1eaa5d8..4aabf4f 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -240,6 +240,18 @@ static bool _is_best_div(struct clk_divider *divider,
return now <= rate && now > best;
}

+static int _next_div(struct clk_divider *divider, int div)
+{
+ div++;
+
+ if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
+ return __roundup_pow_of_two(div);
+ if (divider->table)
+ return _round_up_table(divider->table, div);
+
+ return div;
+}
+
static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
unsigned long *best_parent_rate)
{
@@ -267,7 +279,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
*/
maxdiv = min(ULONG_MAX / rate, maxdiv);

- for (i = 1; i <= maxdiv; i++) {
+ for (i = 1; i <= maxdiv; i = _next_div(divider, i)) {
if (!_is_valid_div(divider, i))
continue;
if (rate * i == parent_rate_saved) {
--
1.7.9.5

2014-01-29 16:48:29

by Maxime Coquelin

[permalink] [raw]
Subject: [PATCH 1/3] clk: divider: Fix best div calculation for power-of-two and table dividers

The divider returned by clk_divider_bestdiv() is likely to be invalid in case
of power-of-two and table dividers when CLK_SET_RATE_PARENT flag isn't set.

Signed-off-by: Maxime Coquelin <[email protected]>
---
drivers/clk/clk-divider.c | 37 ++++++++++++++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 5543b7d..2137d58 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -144,6 +144,37 @@ static bool _is_valid_div(struct clk_divider *divider, unsigned int div)
return true;
}

+static int _round_up_table(const struct clk_div_table *table, int div)
+{
+ const struct clk_div_table *clkt;
+ int up = _get_table_maxdiv(table);
+
+ for (clkt = table; clkt->div; clkt++) {
+ if (clkt->div == div)
+ return clkt->div;
+ else if (clkt->div < div)
+ continue;
+
+ if ((clkt->div - div) < (up - div))
+ up = clkt->div;
+ }
+
+ return up;
+}
+
+static int _div_round_up(struct clk_divider *divider,
+ unsigned long parent_rate, unsigned long rate)
+{
+ int div = DIV_ROUND_UP(parent_rate, rate);
+
+ if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
+ div = __roundup_pow_of_two(div);
+ if (divider->table)
+ div = _round_up_table(divider->table, div);
+
+ return div;
+}
+
static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
unsigned long *best_parent_rate)
{
@@ -159,7 +190,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,

if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
parent_rate = *best_parent_rate;
- bestdiv = DIV_ROUND_UP(parent_rate, rate);
+ bestdiv = _div_round_up(divider, parent_rate, rate);
bestdiv = bestdiv == 0 ? 1 : bestdiv;
bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
return bestdiv;
@@ -219,6 +250,10 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
u32 val;

div = parent_rate / rate;
+
+ if (!_is_valid_div(divider, div))
+ return -EINVAL;
+
value = _get_val(divider, div);

if (value > div_mask(divider))
--
1.7.9.5

2014-02-11 10:20:22

by Maxime Coquelin

[permalink] [raw]
Subject: Re: [PATCH 0/3] CCF clock divider changes

Hi Mike,

Did you have a look at this series?

If patch 1 gets accepted, should it also be submitted to the stable
branches?

Thanks,
Maxime

On 01/29/2014 05:24 PM, Maxime COQUELIN wrote:
> Hi Mike,
>
> First patch fixes round_rate for power-of-two and table-based dividers when
> the CLK_SET_RATE_PARENT flag isn't set.
>
> Second patch introduces a new flag to give the possibility to round the
> divider to the closest one, instead of rounding up by default.
>
> Third patch optimizes the for-loop when CLK_SET_RATE_PARENT is set for
> power-of-two and table-based dividers.
>
> The series applies on top of v3.13 tag.
>
> Maxime Coquelin (3):
> clk: divider: Fix best div calculation for power-of-two and table
> dividers
> clk: divider: Add round to closest divider
> clk: divider: Optimize clk_divider_bestdiv loop
>
> drivers/clk/clk-divider.c | 118 ++++++++++++++++++++++++++++++++++++++++--
> include/linux/clk-provider.h | 3 ++
> 2 files changed, 118 insertions(+), 3 deletions(-)
>

2014-02-11 10:59:22

by Maxime Coquelin

[permalink] [raw]
Subject: Re: [PATCH 0/3] CCF clock divider changes

Hi Mike,


Did you have a look at this series?

If patch 1 gets accepted, should it also be submitted to the stable
branches?

Thanks,
Maxime

ps: Mail resent because our company mail server IP is blocked by Spamhaus.

On 01/29/2014 05:24 PM, Maxime COQUELIN wrote:
> Hi Mike,
>
> First patch fixes round_rate for power-of-two and table-based dividers when
> the CLK_SET_RATE_PARENT flag isn't set.
>
> Second patch introduces a new flag to give the possibility to round the
> divider to the closest one, instead of rounding up by default.
>
> Third patch optimizes the for-loop when CLK_SET_RATE_PARENT is set for
> power-of-two and table-based dividers.
>
> The series applies on top of v3.13 tag.
>
> Maxime Coquelin (3):
> clk: divider: Fix best div calculation for power-of-two and table
> dividers
> clk: divider: Add round to closest divider
> clk: divider: Optimize clk_divider_bestdiv loop
>
> drivers/clk/clk-divider.c | 118 ++++++++++++++++++++++++++++++++++++++++--
> include/linux/clk-provider.h | 3 ++
> 2 files changed, 118 insertions(+), 3 deletions(-)
>

2014-04-24 15:37:53

by Mike Turquette

[permalink] [raw]
Subject: Re: [PATCH 2/3] clk: divider: Add round to closest divider

Quoting Maxime COQUELIN (2014-01-29 08:24:07)
> In some cases, we want to be able to round the divider to the closest one,
> instead than rounding up.
>
> This patch adds a new CLK_DIVIDER_ROUND_CLOSEST flag to specify the divider
> has to round to closest div, keeping rounding up as de default behaviour.

I've taken patch #2 and #3 into clk-next for 3.16.

Thanks!
Mike

>
> Signed-off-by: Maxime Coquelin <[email protected]>
> ---
> drivers/clk/clk-divider.c | 69 ++++++++++++++++++++++++++++++++++++++++--
> include/linux/clk-provider.h | 3 ++
> 2 files changed, 70 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
> index 2137d58..1eaa5d8 100644
> --- a/drivers/clk/clk-divider.c
> +++ b/drivers/clk/clk-divider.c
> @@ -43,6 +43,17 @@ static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
> return maxdiv;
> }
>
> +static unsigned int _get_table_mindiv(const struct clk_div_table *table)
> +{
> + unsigned int mindiv = UINT_MAX;
> + const struct clk_div_table *clkt;
> +
> + for (clkt = table; clkt->div; clkt++)
> + if (clkt->div < mindiv)
> + mindiv = clkt->div;
> + return mindiv;
> +}
> +
> static unsigned int _get_maxdiv(struct clk_divider *divider)
> {
> if (divider->flags & CLK_DIVIDER_ONE_BASED)
> @@ -162,6 +173,24 @@ static int _round_up_table(const struct clk_div_table *table, int div)
> return up;
> }
>
> +static int _round_down_table(const struct clk_div_table *table, int div)
> +{
> + const struct clk_div_table *clkt;
> + int down = _get_table_mindiv(table);
> +
> + for (clkt = table; clkt->div; clkt++) {
> + if (clkt->div == div)
> + return clkt->div;
> + else if (clkt->div > div)
> + continue;
> +
> + if ((div - clkt->div) < (div - down))
> + down = clkt->div;
> + }
> +
> + return down;
> +}
> +
> static int _div_round_up(struct clk_divider *divider,
> unsigned long parent_rate, unsigned long rate)
> {
> @@ -175,6 +204,42 @@ static int _div_round_up(struct clk_divider *divider,
> return div;
> }
>
> +static int _div_round_closest(struct clk_divider *divider,
> + unsigned long parent_rate, unsigned long rate)
> +{
> + int up, down, div;
> +
> + up = down = div = DIV_ROUND_CLOSEST(parent_rate, rate);
> +
> + if (divider->flags & CLK_DIVIDER_POWER_OF_TWO) {
> + up = __roundup_pow_of_two(div);
> + down = __rounddown_pow_of_two(div);
> + } else if (divider->table) {
> + up = _round_up_table(divider->table, div);
> + down = _round_down_table(divider->table, div);
> + }
> +
> + return (up - div) <= (div - down) ? up : down;
> +}
> +
> +static int _div_round(struct clk_divider *divider, unsigned long parent_rate,
> + unsigned long rate)
> +{
> + if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
> + return _div_round_closest(divider, parent_rate, rate);
> +
> + return _div_round_up(divider, parent_rate, rate);
> +}
> +
> +static bool _is_best_div(struct clk_divider *divider,
> + int rate, int now, int best)
> +{
> + if (divider->flags & CLK_DIVIDER_ROUND_CLOSEST)
> + return abs(rate - now) < abs(rate - best);
> +
> + return now <= rate && now > best;
> +}
> +
> static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
> unsigned long *best_parent_rate)
> {
> @@ -190,7 +255,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
>
> if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
> parent_rate = *best_parent_rate;
> - bestdiv = _div_round_up(divider, parent_rate, rate);
> + bestdiv = _div_round(divider, parent_rate, rate);
> bestdiv = bestdiv == 0 ? 1 : bestdiv;
> bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
> return bestdiv;
> @@ -217,7 +282,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
> parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
> MULT_ROUND_UP(rate, i));
> now = parent_rate / i;
> - if (now <= rate && now > best) {
> + if (_is_best_div(divider, rate, now, best)) {
> bestdiv = i;
> best = now;
> *best_parent_rate = parent_rate;
> diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
> index 7e59253..595f0ba 100644
> --- a/include/linux/clk-provider.h
> +++ b/include/linux/clk-provider.h
> @@ -275,6 +275,8 @@ struct clk_div_table {
> * of this register, and mask of divider bits are in higher 16-bit of this
> * register. While setting the divider bits, higher 16-bit should also be
> * updated to indicate changing divider bits.
> + * CLK_DIVIDER_ROUND_CLOSEST - Makes the best calculated divider to be rounded
> + * to the closest integer instead of the up one.
> */
> struct clk_divider {
> struct clk_hw hw;
> @@ -290,6 +292,7 @@ struct clk_divider {
> #define CLK_DIVIDER_POWER_OF_TWO BIT(1)
> #define CLK_DIVIDER_ALLOW_ZERO BIT(2)
> #define CLK_DIVIDER_HIWORD_MASK BIT(3)
> +#define CLK_DIVIDER_ROUND_CLOSEST BIT(4)
>
> extern const struct clk_ops clk_divider_ops;
> struct clk *clk_register_divider(struct device *dev, const char *name,
> --
> 1.7.9.5
>

2014-04-25 15:31:38

by Mike Turquette

[permalink] [raw]
Subject: Re: [PATCH 1/3] clk: divider: Fix best div calculation for power-of-two and table dividers

Quoting Maxime COQUELIN (2014-01-29 08:24:06)
> The divider returned by clk_divider_bestdiv() is likely to be invalid in case
> of power-of-two and table dividers when CLK_SET_RATE_PARENT flag isn't set.
>
> Signed-off-by: Maxime Coquelin <[email protected]>

I've taken this into clk-fixes for 3.15-rcX.

Thanks,
Mike

> ---
> drivers/clk/clk-divider.c | 37 ++++++++++++++++++++++++++++++++++++-
> 1 file changed, 36 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
> index 5543b7d..2137d58 100644
> --- a/drivers/clk/clk-divider.c
> +++ b/drivers/clk/clk-divider.c
> @@ -144,6 +144,37 @@ static bool _is_valid_div(struct clk_divider *divider, unsigned int div)
> return true;
> }
>
> +static int _round_up_table(const struct clk_div_table *table, int div)
> +{
> + const struct clk_div_table *clkt;
> + int up = _get_table_maxdiv(table);
> +
> + for (clkt = table; clkt->div; clkt++) {
> + if (clkt->div == div)
> + return clkt->div;
> + else if (clkt->div < div)
> + continue;
> +
> + if ((clkt->div - div) < (up - div))
> + up = clkt->div;
> + }
> +
> + return up;
> +}
> +
> +static int _div_round_up(struct clk_divider *divider,
> + unsigned long parent_rate, unsigned long rate)
> +{
> + int div = DIV_ROUND_UP(parent_rate, rate);
> +
> + if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
> + div = __roundup_pow_of_two(div);
> + if (divider->table)
> + div = _round_up_table(divider->table, div);
> +
> + return div;
> +}
> +
> static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
> unsigned long *best_parent_rate)
> {
> @@ -159,7 +190,7 @@ static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
>
> if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
> parent_rate = *best_parent_rate;
> - bestdiv = DIV_ROUND_UP(parent_rate, rate);
> + bestdiv = _div_round_up(divider, parent_rate, rate);
> bestdiv = bestdiv == 0 ? 1 : bestdiv;
> bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
> return bestdiv;
> @@ -219,6 +250,10 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
> u32 val;
>
> div = parent_rate / rate;
> +
> + if (!_is_valid_div(divider, div))
> + return -EINVAL;
> +
> value = _get_val(divider, div);
>
> if (value > div_mask(divider))
> --
> 1.7.9.5
>