From: Kirill Kapranov <kirill.kapranov@compulab.co.il>
To: a.zummo@towertech.it, alexandre.belloni@bootlin.com,
phdm@macqel.be, linux-rtc@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Kirill Kapranov <kirill.kapranov@compulab.co.il>
Subject: [PATCH 4/4] rtc:abx80x: Enable xt digital calibration
Date: Mon, 29 Mar 2021 00:02:32 +0300 [thread overview]
Message-ID: <20210328210232.10395-5-kirill.kapranov@compulab.co.il> (raw)
In-Reply-To: <20210328210232.10395-1-kirill.kapranov@compulab.co.il>
The XT digital calibration feature allows to improve the RTC accuracy,
using a Distributed Digital Calibration function.
See ch. 5.9.1 of AB08XX Series Ultra Low Power RTC IC User's Guide
https://abracon.com/realtimeclock/AB08XX-Application-Manual.pdf
Signed-off-by: Kirill Kapranov <kirill.kapranov@compulab.co.il>
---
drivers/rtc/rtc-abx80x.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 145 insertions(+)
diff --git a/drivers/rtc/rtc-abx80x.c b/drivers/rtc/rtc-abx80x.c
index 153d89b56da9..e13fa88e87a5 100644
--- a/drivers/rtc/rtc-abx80x.c
+++ b/drivers/rtc/rtc-abx80x.c
@@ -54,6 +54,9 @@
#define ABX8XX_REG_SQW 0x13
#define ABX8XX_SQW_MODE_BITS 5
+#define ABX8XX_REG_CAL_XT 0x14
+#define ABX8XX_CAL_XT_OFFSETX_MASK GENMASK(6, 0)
+
#define ABX8XX_REG_CD_TIMER_CTL 0x18
#define ABX8XX_REG_OSC 0x1c
@@ -120,6 +123,22 @@ struct abx80x_priv {
struct watchdog_device wdog;
};
+union abx8xx_reg_cal_xt {
+ struct {
+ int offsetx:7;
+ unsigned int cmdx:1;
+ };
+ int val;
+} __packed;
+
+union abx8xx_reg_oss {
+ struct {
+ int:6;
+ unsigned int xtcal:2;
+ };
+ int val;
+} __packed;
+
union abx8xx_reg_sqw {
struct {
unsigned int sqws:5;
@@ -498,6 +517,123 @@ static ssize_t oscillator_show(struct device *dev,
static DEVICE_ATTR_RW(oscillator);
+static const int xt_freq_nom = 32768000; //Nominal XT frequency 32 kHz in mHz
+
+static int xt_frequency_set(struct i2c_client *client, u32 xt_freq)
+{
+ int retval;
+ long Adj;
+ union abx8xx_reg_cal_xt reg_cal_xt;
+ union abx8xx_reg_oss reg_oss;
+
+ retval = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
+ if (retval < 0)
+ goto err;
+ reg_oss.val = retval;
+
+ Adj = (xt_freq_nom - (int)xt_freq) * 16 / 1000;
+ if (Adj < -320) {
+ dev_err(&client->dev,
+ "The XT oscillator is too fast to be adjusted\n");
+ return -ERANGE;
+ } else if (Adj < -256) {
+ reg_oss.xtcal = 3;
+ reg_cal_xt.cmdx = 1;
+ reg_cal_xt.offsetx = (Adj + 192) / 2;
+ } else if (Adj < -192) {
+ reg_oss.xtcal = 3;
+ reg_cal_xt.cmdx = 0;
+ reg_cal_xt.offsetx = Adj + 192;
+ } else if (Adj < -128) {
+ reg_oss.xtcal = 2;
+ reg_cal_xt.cmdx = 0;
+ reg_cal_xt.offsetx = Adj + 128;
+ } else if (Adj < -64) {
+ reg_oss.xtcal = 1;
+ reg_cal_xt.cmdx = 0;
+ reg_cal_xt.offsetx = Adj + 64;
+ } else if (Adj < 64) {
+ reg_oss.xtcal = 0;
+ reg_cal_xt.cmdx = 0;
+ reg_cal_xt.offsetx = Adj;
+ } else if (Adj < 128) {
+ reg_oss.xtcal = 0;
+ reg_cal_xt.cmdx = 1;
+ reg_cal_xt.offsetx = Adj / 2;
+ } else {
+ dev_err(&client->dev,
+ "The XT oscillator is too slow to be adjusted\n");
+ return -ERANGE;
+ }
+
+ retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
+ reg_oss.val);
+ if (retval < 0)
+ goto err;
+
+ retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CAL_XT,
+ reg_cal_xt.val);
+ if (retval < 0)
+ goto err;
+
+ return 0;
+err:
+ dev_err(&client->dev, "Failed to access calibration data\n");
+ return retval;
+}
+
+static ssize_t xt_frequency_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ int retval;
+ unsigned long xt_freq;
+
+ retval = kstrtoul(buf, 10, &xt_freq);
+ if (retval < 0) {
+ dev_err(dev, "Invalid value of oscillator frequency\n");
+ return -EINVAL;
+ }
+
+ dev_info(dev, "Computed xt osc drift is %li ppm\n",
+ 1000000l * ((long)xt_freq - xt_freq_nom) / xt_freq_nom);
+
+ retval = xt_frequency_set(to_i2c_client(dev->parent), xt_freq);
+ if (retval)
+ return retval;
+ return count;
+}
+
+static DEVICE_ATTR_WO(xt_frequency);
+
+static ssize_t xt_calibration_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ int retval;
+ union abx8xx_reg_cal_xt reg_cal_xt;
+ union abx8xx_reg_oss reg_oss;
+ struct i2c_client *client = to_i2c_client(dev->parent);
+
+ retval = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
+ if (retval < 0)
+ goto err;
+ reg_oss.val = retval;
+
+ retval = i2c_smbus_read_byte_data(client, ABX8XX_REG_CAL_XT);
+ if (retval < 0)
+ goto err;
+ reg_cal_xt.val = retval;
+
+ return sprintf(buf, "XTCAL %x\nCMDX %x\nOFFSETX %i = 0x%lx\n",
+ reg_oss.xtcal, reg_cal_xt.cmdx, reg_cal_xt.offsetx,
+ reg_cal_xt.offsetx & ABX8XX_CAL_XT_OFFSETX_MASK);
+err:
+ dev_err(dev, "Failed to read calibration data\n");
+ sprintf(buf, "\n");
+ return retval;
+}
+static DEVICE_ATTR_RO(xt_calibration);
+
#define SQFS_COUNT (1 << ABX8XX_SQW_MODE_BITS)
/* The index of the array is the value to be written to the 'Square Wave Function
* Select' register to make the RTC generate the required square wave.
@@ -611,6 +747,8 @@ static struct attribute *rtc_calib_attrs[] = {
&dev_attr_autocalibration.attr,
&dev_attr_oscillator.attr,
&dev_attr_sqw.attr,
+ &dev_attr_xt_calibration.attr,
+ &dev_attr_xt_frequency.attr,
NULL,
};
@@ -809,6 +947,7 @@ static int abx80x_probe(struct i2c_client *client,
unsigned int wafer;
unsigned int uid;
const char *sqw_mode_name;
+ unsigned int xt_freq;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -ENODEV;
@@ -923,6 +1062,12 @@ static int abx80x_probe(struct i2c_client *client,
abx80x_enable_trickle_charger(client, trickle_cfg);
}
+ if (!of_property_read_u32(np, "xt-frequency", &xt_freq)) {
+ dev_info(&client->dev, "Calibrate XT %d mHz:\n",
+ xt_freq);
+ xt_frequency_set(client, xt_freq);
+ }
+
if (!of_property_read_string(np, "sqw", &sqw_mode_name))
sqw_set(client, sqw_mode_name);
--
2.11.0
next prev parent reply other threads:[~2021-03-28 21:30 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-28 21:02 [PATCH 0/4] rtc:abx80x: Enable distributed digital calibration Kirill Kapranov
2021-03-28 21:02 ` [PATCH 1/4] dt-bindings: rtc: abracon,abx80x: Add sqw property Kirill Kapranov
2021-03-28 21:02 ` [PATCH 2/4] rtc: abx80x: Enable SQW output Kirill Kapranov
2021-03-29 0:34 ` kernel test robot
2021-03-29 6:19 ` Dan Carpenter
2021-03-30 15:28 ` Kirill Kapranov
2021-03-28 21:02 ` [PATCH 3/4] dt-bindings: rtc: abracon,abx80x: Add xt-frequency property Kirill Kapranov
2021-03-28 21:02 ` Kirill Kapranov [this message]
2021-04-09 10:46 ` [PATCH 4/4] rtc:abx80x: Enable xt digital calibration Pavel Machek
2021-04-09 16:56 ` Kirill Kapranov
2021-03-28 21:14 ` [PATCH 0/4] rtc:abx80x: Enable distributed " 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=20210328210232.10395-5-kirill.kapranov@compulab.co.il \
--to=kirill.kapranov@compulab.co.il \
--cc=a.zummo@towertech.it \
--cc=alexandre.belloni@bootlin.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=phdm@macqel.be \
/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).