[PATCH v2 4/4] rtc: ds1307: add freq_test sysfs attribute to check tick on m41txx

From: Giulio Benetti
Date: Wed May 09 2018 - 14:28:38 EST


On m41txx you can enable open-drain OUT pin to check if offset is ok.
Enabling OUT pin with freq_test attribute, OUT pin will tick 512 times
faster than 1s tick base.

Enable or Disable FT bit on CONTROL register if freq_test is 1 or 0.

Signed-off-by: Giulio Benetti <giulio.benetti@xxxxxxxxxxxxxxxx>
---
V1 => V2: change frequency test from dt property to dev sysfs attribute
drivers/rtc/rtc-ds1307.c | 86 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)

diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index 33895668b363..da71000101da 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -1042,6 +1042,67 @@ static int m41txx_rtc_set_offset(struct device *dev, long offset)
return regmap_write(ds1307->regmap, M41TXX_REG_CONTROL, ctrl_reg);
}

+static ssize_t freq_test_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct ds1307 *ds1307 = dev_get_drvdata(dev);
+ unsigned long freq_test = 0;
+ unsigned int ctrl_reg;
+ int retval;
+
+ retval = kstrtoul(buf, 10, &freq_test);
+ if ((retval < 0) || (retval > 1)) {
+ dev_err(dev, "Failed to store RTC Frequency Test attribute\n");
+ return -EINVAL;
+ }
+
+ regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
+
+ if (freq_test)
+ ctrl_reg |= M41TXX_BIT_FT;
+ else
+ ctrl_reg &= ~M41TXX_BIT_FT;
+
+ retval = regmap_write(ds1307->regmap, M41TXX_REG_CONTROL, ctrl_reg);
+
+ return retval ? retval : count;
+}
+
+static ssize_t freq_test_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct ds1307 *ds1307 = dev_get_drvdata(dev);
+ int freq_test = 0;
+ unsigned int ctrl_reg;
+
+ regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg);
+
+ if (ctrl_reg & M41TXX_BIT_FT)
+ freq_test = true;
+ else
+ freq_test = false;
+
+ if ((freq_test < 0) || (freq_test > 1)) {
+ dev_err(dev, "Failed to read RTC Frequency Test\n");
+ sprintf(buf, "0\n");
+ return freq_test;
+ }
+
+ return sprintf(buf, "%d\n", freq_test);
+}
+
+static DEVICE_ATTR_RW(freq_test);
+
+static struct attribute *rtc_calib_attrs[] = {
+ &dev_attr_freq_test.attr,
+ NULL,
+};
+
+static const struct attribute_group rtc_calib_attr_group = {
+ .attrs = rtc_calib_attrs,
+};
+
/*----------------------------------------------------------------------*/

static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
@@ -1455,6 +1516,13 @@ static const struct regmap_config regmap_config = {
.val_bits = 8,
};

+static void rtc_calib_remove_sysfs_group(void *_dev)
+{
+ struct device *dev = _dev;
+
+ sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
+}
+
static int ds1307_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -1783,6 +1851,24 @@ static int ds1307_probe(struct i2c_client *client,
if (err)
return err;

+ /* Export sysfs entries */
+ err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
+ if (err) {
+ dev_err(&client->dev, "Failed to create sysfs group: %d\n",
+ err);
+ return err;
+ }
+
+ err = devm_add_action_or_reset(&client->dev,
+ rtc_calib_remove_sysfs_group,
+ &client->dev);
+ if (err) {
+ dev_err(&client->dev,
+ "Failed to add sysfs cleanup action: %d\n",
+ err);
+ return err;
+ }
+
if (chip->nvram_size) {
struct nvmem_config nvmem_cfg = {
.name = "ds1307_nvram",
--
2.17.0