All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peng Fan <peng.fan@nxp.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH V3 06/16] clk: divider set rate supporrt
Date: Wed, 31 Jul 2019 07:01:37 +0000	[thread overview]
Message-ID: <20190731071654.9970-6-peng.fan@nxp.com> (raw)
In-Reply-To: <20190731071654.9970-1-peng.fan@nxp.com>

Signed-off-by: Peng Fan <peng.fan@nxp.com>
---

V3:
 None
V2:
 Rebase

 drivers/clk/clk-divider.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 2ed9ed6ab8..822e09b084 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -18,6 +18,7 @@
 #include <dm/lists.h>
 #include <dm/device-internal.h>
 #include <linux/clk-provider.h>
+#include <linux/log2.h>
 #include <div64.h>
 #include <clk.h>
 #include "clk.h"
@@ -86,8 +87,95 @@ static ulong clk_divider_recalc_rate(struct clk *clk)
 				   divider->flags, divider->width);
 }
 
+static bool _is_valid_table_div(const struct clk_div_table *table,
+				unsigned int div)
+{
+	const struct clk_div_table *clkt;
+
+	for (clkt = table; clkt->div; clkt++)
+		if (clkt->div == div)
+			return true;
+	return false;
+}
+
+static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
+			  unsigned long flags)
+{
+	if (flags & CLK_DIVIDER_POWER_OF_TWO)
+		return is_power_of_2(div);
+	if (table)
+		return _is_valid_table_div(table, div);
+	return true;
+}
+
+static unsigned int _get_table_val(const struct clk_div_table *table,
+				   unsigned int div)
+{
+	const struct clk_div_table *clkt;
+
+	for (clkt = table; clkt->div; clkt++)
+		if (clkt->div == div)
+			return clkt->val;
+	return 0;
+}
+
+static unsigned int _get_val(const struct clk_div_table *table,
+			     unsigned int div, unsigned long flags, u8 width)
+{
+	if (flags & CLK_DIVIDER_ONE_BASED)
+		return div;
+	if (flags & CLK_DIVIDER_POWER_OF_TWO)
+		return __ffs(div);
+	if (flags & CLK_DIVIDER_MAX_AT_ZERO)
+		return (div == clk_div_mask(width) + 1) ? 0 : div;
+	if (table)
+		return  _get_table_val(table, div);
+	return div - 1;
+}
+int divider_get_val(unsigned long rate, unsigned long parent_rate,
+		    const struct clk_div_table *table, u8 width,
+		    unsigned long flags)
+{
+	unsigned int div, value;
+
+	div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
+
+	if (!_is_valid_div(table, div, flags))
+		return -EINVAL;
+
+	value = _get_val(table, div, flags, width);
+
+	return min_t(unsigned int, value, clk_div_mask(width));
+}
+
+static ulong clk_divider_set_rate(struct clk *clk, unsigned long rate)
+{
+	struct clk_divider *divider = to_clk_divider(clk_dev_binded(clk) ?
+			dev_get_clk_ptr(clk->dev) : clk);
+	unsigned long parent_rate = clk_get_parent_rate(clk);
+	int value;
+	u32 val;
+
+	value = divider_get_val(rate, parent_rate, divider->table,
+				divider->width, divider->flags);
+	if (value < 0)
+		return value;
+
+	if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
+		val = clk_div_mask(divider->width) << (divider->shift + 16);
+	} else {
+		val = readl(divider->reg);
+		val &= ~(clk_div_mask(divider->width) << divider->shift);
+	}
+	val |= (u32)value << divider->shift;
+	writel(val, divider->reg);
+
+	return clk_get_rate(clk);
+}
+
 const struct clk_ops clk_divider_ops = {
 	.get_rate = clk_divider_recalc_rate,
+	.set_rate = clk_divider_set_rate,
 };
 
 static struct clk *_register_divider(struct device *dev, const char *name,
-- 
2.16.4

  parent reply	other threads:[~2019-07-31  7:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-31  7:01 [U-Boot] [PATCH V3 01/16] clk: introduce clk_dev_binded Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 02/16] clk: use clk_dev_binded Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 03/16] clk: mux: add set parent support Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 04/16] clk: export mux/divider ops Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 05/16] clk: add clk-gate support Peng Fan
2019-07-31  7:01 ` Peng Fan [this message]
2019-07-31  7:01 ` [U-Boot] [PATCH V3 07/16] clk: fixed_rate: export clk_fixed_rate Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 08/16] clk: imx: import clk heplers Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 09/16] clk: imx: gate2 add set rate Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 10/16] dm: clk: ignore default settings when node not valid Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 11/16] clk-provider: include clk-uclass.h Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 12/16] clk: add composite clk support Peng Fan
2019-07-31  7:01 ` [U-Boot] [PATCH V3 13/16] clk: gate: support sandbox Peng Fan
2019-07-31  7:02 ` [U-Boot] [PATCH V3 14/16] configs: sandbox: Enable composite clk Peng Fan
2019-07-31  7:02 ` [U-Boot] [PATCH V3 15/16] clk: sandbox: add " Peng Fan
2019-07-31  7:02 ` [U-Boot] [PATCH V3 16/16] test: dm: clk_ccf: test " Peng Fan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190731071654.9970-6-peng.fan@nxp.com \
    --to=peng.fan@nxp.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.