mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joshua Clayton <stillcompiling@gmail.com>
To: Alessandro Zummo <a.zummo@towertech.it>,
	Alexandre Belloni <alexandre.belloni@free-electrons.com>
Cc: rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org,
	Joshua Clayton <stillcompiling@gmail.com>
Subject: [PATCH 9/9] rtc-pcf2123: adjust the clock rate via sysfs
Date: Wed,  4 Nov 2015 07:36:40 -0800	[thread overview]
Message-ID: <e06c88aded2d6f06103d73a1020576e3e0772331.1446587705.git.stillcompiling@gmail.com> (raw)
In-Reply-To: <cover.1446587705.git.stillcompiling@gmail.com>
In-Reply-To: <cover.1446587705.git.stillcompiling@gmail.com>

pcf2123 has an offset register, which can be used to make minor
adjustments to the clock rate to compensate for temperature or
a crystal that is not exactly right.

The adjustment is calculated in parts per billion. The data sheet
uses parts per million (as do some others), but with 2 digits of
precision. Since floating point is forbidden, parts per billion is
a better fit.

Add a pair of sysfs files to seetand retrieve the offset in ppm.

Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>
---
 drivers/rtc/rtc-pcf2123.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/drivers/rtc/rtc-pcf2123.c b/drivers/rtc/rtc-pcf2123.c
index d494638..6d70860 100644
--- a/drivers/rtc/rtc-pcf2123.c
+++ b/drivers/rtc/rtc-pcf2123.c
@@ -77,11 +77,17 @@
 /* PCF2123_REG_SC BITS */
 #define OSC_HAS_STOPPED		(0x80)	/* Clock has been stopped */
 
+/* PCF2123_REG_OFFSET BITS */
+#define OFFSET_COARSE		(0x80)	/* Coarse Mode Offset */
+
 /* READ/WRITE ADDRESS BITS */
 #define PCF2123_SUBADDR		(1 << 4)
 #define PCF2123_WRITE		((0 << 7) | PCF2123_SUBADDR)
 #define PCF2123_READ		((1 << 7) | PCF2123_SUBADDR)
 
+/* offset granularity in parts per billion in fine mode */
+#define OFFSET_STEP		(2170)
+
 static struct spi_driver pcf2123_driver;
 
 /*
@@ -166,6 +172,65 @@ static ssize_t pcf2123_store(struct device *dev, struct device_attribute *attr,
 	return count;
 }
 
+static ssize_t pcf2123_adjust_show(struct device *dev,
+				   struct device_attribute *attr, char *buffer)
+{
+	ssize_t ret;
+	s8 reg;
+
+	ret = pcf2123_read(dev, PCF2123_REG_OFFSET, &reg, 1);
+	if (ret < 0)
+		return -EIO;
+
+	if (reg & OFFSET_COARSE) {
+		reg <<= 1;
+	} else {
+		reg &= ~OFFSET_COARSE;
+		reg |= (reg & 0x40) << 1; /* sign extend */
+	}
+
+	return sprintf(buffer, "%ld\n", ((long)reg) * OFFSET_STEP);
+}
+
+static ssize_t pcf2123_adjust_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buffer, size_t count)
+{
+	ssize_t ret;
+	long val;
+	s8 reg;
+
+	ret = kstrtol(buffer, 10, &val);
+	if (ret)
+		return ret;
+
+	if (val > OFFSET_STEP * 127)
+		reg = 127;
+	else if (val < OFFSET_STEP * -128)
+		reg = -128;
+	else
+		reg = (s8)((val + (OFFSET_STEP >> 1)) / OFFSET_STEP);
+
+/*
+ * Each even value of the fine adjust overlaps with a value of the coarse
+ * adjustment, and since the coarse adjsutment will spread the adjustments
+ * over both hours, we use coarse for all even values, as well as values
+ * that are beyond the range of fine adjustment
+ */
+	if (reg <= 63 && reg >= -64 && reg & 1) {
+		reg &= ~OFFSET_COARSE;
+	} else {
+		reg >>= 1;
+		reg |= OFFSET_COARSE;
+	}
+
+	pcf2123_write_reg(dev, PCF2123_REG_OFFSET, reg);
+	if (ret < 0)
+		return -EIO;
+
+	return count;
+}
+
 static int pcf2123_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
 	u8 rxbuf[7];
@@ -320,6 +385,8 @@ static DEVICE_ATTR(c, S_IRUGO | S_IWUSR, pcf2123_show, pcf2123_store);
 static DEVICE_ATTR(d, S_IRUGO | S_IWUSR, pcf2123_show, pcf2123_store);
 static DEVICE_ATTR(e, S_IRUGO | S_IWUSR, pcf2123_show, pcf2123_store);
 static DEVICE_ATTR(f, S_IRUGO | S_IWUSR, pcf2123_show, pcf2123_store);
+static DEVICE_ATTR(adjust, S_IRUGO | S_IWUSR, pcf2123_adjust_show,
+		   pcf2123_adjust_store);
 
 static struct attribute *pcf2123_attrs[] = {
 	&dev_attr_0.attr,
@@ -338,6 +405,7 @@ static struct attribute *pcf2123_attrs[] = {
 	&dev_attr_d.attr,
 	&dev_attr_e.attr,
 	&dev_attr_f.attr,
+	&dev_attr_adjust.attr,
 	NULL
 };
 
-- 
2.5.0


  parent reply	other threads:[~2015-11-04 15:37 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-04 15:36 [PATCH 0/9] rtc-2123: access the clock offset feature Joshua Clayton
2015-11-04 15:36 ` [PATCH 1/9] rtc-pcf2123: Document all registers and useful bits Joshua Clayton
2015-11-24 21:51   ` Alexandre Belloni
2015-12-01 18:13     ` Joshua Clayton
2015-11-04 15:36 ` [PATCH 2/9] rtc-pcf2123: clean up reads from the chip Joshua Clayton
2015-11-04 15:36 ` [PATCH 3/9] rtc-pcf2123: clean up writes to the rtc chip Joshua Clayton
2015-11-24 22:16   ` Alexandre Belloni
2015-12-01 18:19     ` Joshua Clayton
2015-11-04 15:36 ` [PATCH 4/9] rtc-pcf2123: replace magic numbers with defines Joshua Clayton
2015-11-04 15:36 ` [PATCH 5/9] rtc-pcf2123: put the chip reset into a function Joshua Clayton
2015-11-24 23:17   ` Alexandre Belloni
2015-12-01 18:22     ` Joshua Clayton
2015-11-04 15:36 ` [PATCH 6/9] rtc-pcf2123: avoid resetting the clock if possible Joshua Clayton
2015-11-24 23:25   ` Alexandre Belloni
2015-12-01 20:23     ` Joshua Clayton
2015-12-01 21:04       ` Alexandre Belloni
2015-11-04 15:36 ` [PATCH 7/9] rtc-pcf2123: allow sysfs to accept hexidecimal Joshua Clayton
2015-11-04 15:36 ` [PATCH 8/9] rtc-pcf2123: use sysfs groups Joshua Clayton
2015-11-18 23:52   ` Joshua Clayton
2015-11-24 23:31   ` Alexandre Belloni
2015-12-01 20:28     ` Joshua Clayton
2015-12-01 20:47       ` Alexandre Belloni
2015-11-04 15:36 ` Joshua Clayton [this message]
2015-11-18 23:51   ` [PATCH 9/9] rtc-pcf2123: adjust the clock rate via sysfs Joshua Clayton
2015-11-17 15:30 ` [PATCH 0/9] rtc-2123: access the clock offset feature Joshua Clayton
2015-11-17 16:25   ` Alexandre Belloni
2015-11-19  0:25     ` Joshua Clayton

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=e06c88aded2d6f06103d73a1020576e3e0772331.1446587705.git.stillcompiling@gmail.com \
    --to=stillcompiling@gmail.com \
    --cc=a.zummo@towertech.it \
    --cc=alexandre.belloni@free-electrons.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rtc-linux@googlegroups.com \
    /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

all inboxes | Powered by JetHome®