From: Paul Cercueil <paul@crapouillou.net>
To: Alessandro Zummo <a.zummo@towertech.it>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Rob Herring <robh+dt@kernel.org>,
Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>
Cc: linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org,
od@opendingux.net, Paul Cercueil <paul@crapouillou.net>
Subject: [PATCH v2 4/4] rtc: jz4740: Support for fine-tuning the RTC clock
Date: Fri, 28 Oct 2022 23:55:19 +0100 [thread overview]
Message-ID: <20221028225519.89210-5-paul@crapouillou.net> (raw)
In-Reply-To: <20221028225519.89210-1-paul@crapouillou.net>
Write the NC1HZ and ADJC register fields, which allow to tweak the
frequency of the RTC clock, so that it can run as accurately as
possible.
Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
drivers/rtc/rtc-jz4740.c | 45 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c
index 1602e8a4283a..70b63ce75e21 100644
--- a/drivers/rtc/rtc-jz4740.c
+++ b/drivers/rtc/rtc-jz4740.c
@@ -5,6 +5,7 @@
* JZ4740 SoC RTC driver
*/
+#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/io.h>
@@ -41,6 +42,9 @@
#define JZ_RTC_CTRL_AE BIT(2)
#define JZ_RTC_CTRL_ENABLE BIT(0)
+#define JZ_RTC_REGULATOR_NC1HZ_MASK GENMASK(15, 0)
+#define JZ_RTC_REGULATOR_ADJC_MASK GENMASK(25, 16)
+
/* Magic value to enable writes on jz4780 */
#define JZ_RTC_WENR_MAGIC 0xA55A
@@ -61,6 +65,7 @@ struct jz4740_rtc {
enum jz4740_rtc_type type;
struct rtc_device *rtc;
+ struct clk *clk;
struct clk_hw clk32k;
@@ -217,12 +222,51 @@ static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
return jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ, enable);
}
+static int jz4740_rtc_read_offset(struct device *dev, long *offset)
+{
+ struct jz4740_rtc *rtc = dev_get_drvdata(dev);
+ long rate = clk_get_rate(rtc->clk);
+ s32 nc1hz, adjc, offset1k;
+ u32 reg;
+
+ reg = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_REGULATOR);
+ nc1hz = FIELD_GET(JZ_RTC_REGULATOR_NC1HZ_MASK, reg);
+ adjc = FIELD_GET(JZ_RTC_REGULATOR_ADJC_MASK, reg);
+
+ offset1k = (nc1hz - rate + 1) * 1024L + adjc;
+ *offset = offset1k * 1000000L / (rate * 1024L);
+
+ return 0;
+}
+
+static int jz4740_rtc_set_offset(struct device *dev, long offset)
+{
+ struct jz4740_rtc *rtc = dev_get_drvdata(dev);
+ long rate = clk_get_rate(rtc->clk);
+ s32 offset1k, adjc, nc1hz;
+
+ offset1k = div_s64_rem(offset * rate * 1024LL, 1000000LL, &adjc);
+ nc1hz = rate - 1 + offset1k / 1024L;
+
+ if (adjc < 0) {
+ nc1hz--;
+ adjc += 1024;
+ }
+
+ nc1hz = FIELD_PREP(JZ_RTC_REGULATOR_NC1HZ_MASK, nc1hz);
+ adjc = FIELD_PREP(JZ_RTC_REGULATOR_ADJC_MASK, adjc);
+
+ return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_REGULATOR, nc1hz | adjc);
+}
+
static const struct rtc_class_ops jz4740_rtc_ops = {
.read_time = jz4740_rtc_read_time,
.set_time = jz4740_rtc_set_time,
.read_alarm = jz4740_rtc_read_alarm,
.set_alarm = jz4740_rtc_set_alarm,
.alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
+ .read_offset = jz4740_rtc_read_offset,
+ .set_offset = jz4740_rtc_set_offset,
};
static irqreturn_t jz4740_rtc_irq(int irq, void *data)
@@ -353,6 +397,7 @@ static int jz4740_rtc_probe(struct platform_device *pdev)
spin_lock_init(&rtc->lock);
+ rtc->clk = clk;
platform_set_drvdata(pdev, rtc);
device_init_wakeup(dev, 1);
--
2.35.1
next prev parent reply other threads:[~2022-10-28 22:56 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-28 22:55 [PATCH v2 0/4] rtc: ingenic: various updates Paul Cercueil
2022-10-28 22:55 ` [PATCH v2 1/4] dt-bindings: rtc: ingenic: Rework compatible strings and add #clock-cells Paul Cercueil
2022-10-31 19:23 ` Rob Herring
2022-10-31 19:40 ` Paul Cercueil
2022-11-01 17:32 ` Rob Herring
2022-10-28 22:55 ` [PATCH v2 2/4] rtc: jz4740: Use readl_poll_timeout Paul Cercueil
2022-10-28 22:55 ` [PATCH v2 3/4] rtc: jz4740: Register clock provider for the CLK32K pin Paul Cercueil
2022-10-28 22:55 ` Paul Cercueil [this message]
2022-10-29 8:51 ` [PATCH v2 4/4] rtc: jz4740: Support for fine-tuning the RTC clock kernel test robot
2022-10-29 11:52 ` Paul Cercueil
2022-11-02 17:40 ` [PATCH v2 0/4] rtc: ingenic: various updates Alexandre Belloni
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=20221028225519.89210-5-paul@crapouillou.net \
--to=paul@crapouillou.net \
--cc=a.zummo@towertech.it \
--cc=alexandre.belloni@bootlin.com \
--cc=devicetree@vger.kernel.org \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=od@opendingux.net \
--cc=robh+dt@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).