linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adam Ford <aford173@gmail.com>
To: linux-clk@vger.kernel.org
Cc: aford@beaconembedded.com, Adam Ford <aford173@gmail.com>,
	Luca Ceresoli <luca@lucaceresoli.net>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>, Rob Herring <robh+dt@kernel.org>,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC 2/2] clk: vc5: Add support for optional load capacitance
Date: Wed,  6 Jan 2021 11:39:00 -0600	[thread overview]
Message-ID: <20210106173900.388758-2-aford173@gmail.com> (raw)
In-Reply-To: <20210106173900.388758-1-aford173@gmail.com>

There are two registers which can set the load capacitance for
XTAL1 and XTAL2. These are optional registers when using an
external crystal.  Parse the device tree and set the
corresponding registers accordingly.

Signed-off-by: Adam Ford <aford173@gmail.com>
---
 drivers/clk/clk-versaclock5.c | 64 +++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/drivers/clk/clk-versaclock5.c b/drivers/clk/clk-versaclock5.c
index 43db67337bc0..445abc3731fb 100644
--- a/drivers/clk/clk-versaclock5.c
+++ b/drivers/clk/clk-versaclock5.c
@@ -759,6 +759,63 @@ static int vc5_update_power(struct device_node *np_output,
 	return 0;
 }
 
+static int vc5_map_cap_value(u32 femtofarads)
+{
+	int mapped_value;
+
+	/* The datasheet explicitly states 9000 - 25000 */
+	if ((femtofarads < 9000) || (femtofarads > 25000))
+		return -EINVAL;
+
+	/* The lowest target we can hit is 9430, so exit if it's less */
+	if (femtofarads < 9430)
+		return 0;
+
+	/*
+	 * According to VersaClock 6E Programming Guide, there are 6
+	 * bits which translate to 64 entries in XTAL registers 12 and
+	 * 13. Because bits 0 and 1 increase the capacitance the
+	 * same, some of the values can be repeated.  Plugging this
+	 * into a spreadsheet and generating a trendline, the output
+	 * equation becomes x = (y-9098.29) / 216.44, where 'y' is
+	 * the desired capacitance in femtofarads, and x is the value
+	 * of XTAL[5:0].
+	 * To help with rounding, do fixed point math
+	 */
+	femtofarads *= 100;
+	mapped_value = (femtofarads - 909829) / 21644;
+
+	/*
+	 * The datasheet states, the maximum capacitance is 25000,
+	 * but the programmer guide shows a max value is 22832,
+	 * so values higher values could overflow, so cap it.
+	 */
+	mapped_value = max(mapped_value/100, 0x3f);
+
+	return mapped_value;
+}
+static int vc5_update_cap_load(struct device_node *node, struct vc5_driver_data *vc5)
+{
+	u32 value, mapped_value;
+
+	if (!of_property_read_u32(node, "idt,xtal1-load-femtofarads", &value)) {
+		mapped_value = vc5_map_cap_value(value);
+		if (mapped_value < 0)
+			return mapped_value;
+
+		regmap_write(vc5->regmap, VC5_XTAL_X1_LOAD_CAP, (mapped_value << 2));
+	}
+
+	if (!of_property_read_u32(node, "idt,xtal2-load-femtofarads", &value)) {
+		mapped_value = vc5_map_cap_value(value);
+		if (mapped_value < 0)
+			return mapped_value;
+		regmap_write(vc5->regmap, VC5_XTAL_X2_LOAD_CAP, (mapped_value << 2));
+	}
+
+	return 0;
+}
+
 static int vc5_update_slew(struct device_node *np_output,
 			   struct vc5_out_data *clk_out)
 {
@@ -884,6 +941,13 @@ static int vc5_probe(struct i2c_client *client, const struct i2c_device_id *id)
 		return -EINVAL;
 	}
 
+	/* Configure Optional Loading Capacitance for external XTAL */
+	if (!(vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)) {
+		ret = vc5_update_cap_load(client->dev.of_node, vc5);
+		if (ret)
+			goto err_clk_register;
+	}
+
 	init.name = kasprintf(GFP_KERNEL, "%pOFn.mux", client->dev.of_node);
 	init.ops = &vc5_mux_ops;
 	init.flags = 0;
-- 
2.25.1


  reply	other threads:[~2021-01-06 17:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-06 17:38 [RFC 1/2] dt-bindings: clk: versaclock5: Add load capacitance properties Adam Ford
2021-01-06 17:39 ` Adam Ford [this message]
2021-01-08 22:49   ` [RFC 2/2] clk: vc5: Add support for optional load capacitance Luca Ceresoli
2021-01-09  3:00     ` Adam Ford
2021-01-09 18:02       ` Luca Ceresoli
2021-01-11 16:40         ` Adam Ford
2021-01-12 16:45           ` Luca Ceresoli
2021-01-12 17:00             ` Adam Ford
2021-01-13 14:12               ` Luca Ceresoli
2021-01-13  7:23   ` Stephen Boyd
2021-01-08 22:49 ` [RFC 1/2] dt-bindings: clk: versaclock5: Add load capacitance properties Luca Ceresoli
2021-01-09  2:48   ` Adam Ford
2021-01-13 14:39     ` Luca Ceresoli
2021-01-13  3:16 ` Rob Herring
2021-01-13 12:31   ` Adam Ford
2021-01-13 14:36     ` Luca Ceresoli

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=20210106173900.388758-2-aford173@gmail.com \
    --to=aford173@gmail.com \
    --cc=aford@beaconembedded.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luca@lucaceresoli.net \
    --cc=mturquette@baylibre.com \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).