Skip to contentSkip to conversation

rtc: ds1307: Fix relying on reset value for weekday

PATCH Applied Keerthy 3 participants 7 messages Jun 1, 2016 – Jul 8, 2016 Real time clock (RTC)

lore
Share & tools
lore

Keerthy

author
#0 lore raw

PATCH 1/1 rtc: ds1307: Fix relying on reset value for weekday

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Hence compute the wday using the current date/month/year values. Check if reset wday is any different from the computed wday, If different then set the wday which we computed using date/month/year values.

Document Referred:
http://ww1.microchip.com/downloads/en/DeviceDoc/20002266F.pdf

Fixes 1d1945d261a2af "drivers/rtc/rtc-ds1307.c: add alarm support for mcp7941x chips"
---
 drivers/rtc/rtc-ds1307.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/rtc/rtc-ds1307.c +27 -1
index 821d9c0..d9045cc 100644
@@ -602,6 +602,8 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
  * Alarm support for mcp794xx devices.
  */
 
+#define MCP794XX_REG_WEEKDAY		0x3
+#define MCP794XX_REG_WEEKDAY_WDAY_MASK	0x7
 #define MCP794XX_REG_CONTROL		0x07
 #	define MCP794XX_BIT_ALM0_EN	0x10
 #	define MCP794XX_BIT_ALM1_EN	0x20
@@ -1231,13 +1233,16 @@ static int ds1307_probe(struct i2c_client *client,
 {
 	struct ds1307		*ds1307;
 	int			err = -ENODEV;
-	int			tmp;
+	int			tmp, wday;
 	struct chip_desc	*chip = &chips[id->driver_data];
 	struct i2c_adapter	*adapter = to_i2c_adapter(client->dev.parent);
 	bool			want_irq = false;
 	bool			ds1307_can_wakeup_device = false;
 	unsigned char		*buf;
 	struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
+	struct rtc_time		tm;
+	unsigned long		timestamp;
+
 	irq_handler_t	irq_handler = ds1307_irq;
 
 	static const int	bbsqi_bitpos[] = {
@@ -1526,6 +1531,27 @@ read_rtc:
 				bin2bcd(tmp));
 	}
 
+	/*
+	 * Some IPs have weekday reset value = 0x1 which might not correct
+	 * hence compute the wday using the current date/month/year values
+	 */
+	ds1307_get_time(&client->dev, &tm);
+	wday = tm.tm_wday;
+	timestamp = rtc_tm_to_time64(&tm);
+	rtc_time64_to_tm(timestamp, &tm);
+
+	/*
+	 * Check if reset wday is different from the computed wday
+	 * If different then set the wday which we computed using
+	 * timestamp
+	 */
+	if (wday != tm.tm_wday) {
+		wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
+		wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
+		wday = wday | (tm.tm_wday + 1);
+		i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
+	}
+
 	if (want_irq) {
 		device_set_wakeup_capable(&client->dev, true);
 		set_bit(HAS_ALARM, &ds1307->flags);
Signature · 11 lines
1.9.1

-- 
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- 
You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#1 lore raw

Hi,

Quoted 6 lines · Keerthy

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Hum, what are the chances that the reset value is actually the correct date/time?
Won't that be corrected after the first call to set_time? Until then, the date is not correct so, do we care anyway?

Maybe I'm missing something here.

Quoted 75 lines

Hence compute the wday using the current date/month/year values. Check if reset wday is any different from the computed wday, If different then set the wday which we computed using date/month/year values.

Document Referred:
http://ww1.microchip.com/downloads/en/DeviceDoc/20002266F.pdf

Fixes 1d1945d261a2af "drivers/rtc/rtc-ds1307.c: add alarm support for mcp7941x chips"
---
 drivers/rtc/rtc-ds1307.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/rtc/rtc-ds1307.c +27 -1
index 821d9c0..d9045cc 100644
@@ -602,6 +602,8 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
  * Alarm support for mcp794xx devices.
  */
 
+#define MCP794XX_REG_WEEKDAY		0x3
+#define MCP794XX_REG_WEEKDAY_WDAY_MASK	0x7
 #define MCP794XX_REG_CONTROL		0x07
 #	define MCP794XX_BIT_ALM0_EN	0x10
 #	define MCP794XX_BIT_ALM1_EN	0x20
@@ -1231,13 +1233,16 @@ static int ds1307_probe(struct i2c_client *client,
 {
 	struct ds1307		*ds1307;
 	int			err = -ENODEV;
-	int			tmp;
+	int			tmp, wday;
 	struct chip_desc	*chip = &chips[id->driver_data];
 	struct i2c_adapter	*adapter = to_i2c_adapter(client->dev.parent);
 	bool			want_irq = false;
 	bool			ds1307_can_wakeup_device = false;
 	unsigned char		*buf;
 	struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
+	struct rtc_time		tm;
+	unsigned long		timestamp;
+
 	irq_handler_t	irq_handler = ds1307_irq;
 
 	static const int	bbsqi_bitpos[] = {
@@ -1526,6 +1531,27 @@ read_rtc:
 				bin2bcd(tmp));
 	}
 
+	/*
+	 * Some IPs have weekday reset value = 0x1 which might not correct
+	 * hence compute the wday using the current date/month/year values
+	 */
+	ds1307_get_time(&client->dev, &tm);
+	wday = tm.tm_wday;
+	timestamp = rtc_tm_to_time64(&tm);
+	rtc_time64_to_tm(timestamp, &tm);
+
+	/*
+	 * Check if reset wday is different from the computed wday
+	 * If different then set the wday which we computed using
+	 * timestamp
+	 */
+	if (wday != tm.tm_wday) {
+		wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
+		wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
+		wday = wday | (tm.tm_wday + 1);
+		i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
+	}
+
 	if (want_irq) {
 		device_set_wakeup_capable(&client->dev, true);
 		set_bit(HAS_ALARM, &ds1307->flags);
Signature · 1 line
1.9.1
Signature · 13 lines
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

-- 
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- 
You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#2 lore raw
Quoted 10 lines · Alexandre Belloni

Hi,

Quoted 6 lines · Keerthy

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Quoted 4 lines

Hum, what are the chances that the reset value is actually the correct date/time?
Won't that be corrected after the first call to set_time? Until then, the date is not correct so, do we care anyway?

Yes if an alarm is programmed without set_time.

ex: rtcwake -d /dev/rtc0 -s 5

Even the basic rtctest under tools/testing/selftests/timers/rtctest.c fails as the wday is wrong and no alarm fires.

Instead of relying on some one to call set_time before programming alarm its better to fix the wday to reflect the current day right?

Quoted 79 lines

Maybe I'm missing something here.

Quoted 75 lines

Hence compute the wday using the current date/month/year values. Check if reset wday is any different from the computed wday, If different then set the wday which we computed using date/month/year values.

Document Referred:
http://ww1.microchip.com/downloads/en/DeviceDoc/20002266F.pdf

Fixes 1d1945d261a2af "drivers/rtc/rtc-ds1307.c: add alarm support for mcp7941x chips"
---
  drivers/rtc/rtc-ds1307.c | 28 +++++++++++++++++++++++++++-
  1 file changed, 27 insertions(+), 1 deletion(-)
drivers/rtc/rtc-ds1307.c +27 -2
index 821d9c0..d9045cc 100644
@@ -602,6 +602,8 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
   * Alarm support for mcp794xx devices.
   */
 
+#define MCP794XX_REG_WEEKDAY		0x3
+#define MCP794XX_REG_WEEKDAY_WDAY_MASK	0x7
  #define MCP794XX_REG_CONTROL		0x07
  #	define MCP794XX_BIT_ALM0_EN	0x10
  #	define MCP794XX_BIT_ALM1_EN	0x20
@@ -1231,13 +1233,16 @@ static int ds1307_probe(struct i2c_client *client,
  {
  	struct ds1307		*ds1307;
  	int			err = -ENODEV;
-	int			tmp;
+	int			tmp, wday;
  	struct chip_desc	*chip = &chips[id->driver_data];
  	struct i2c_adapter	*adapter = to_i2c_adapter(client->dev.parent);
  	bool			want_irq = false;
  	bool			ds1307_can_wakeup_device = false;
  	unsigned char		*buf;
  	struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
+	struct rtc_time		tm;
+	unsigned long		timestamp;
+
  	irq_handler_t	irq_handler = ds1307_irq;
 
  	static const int	bbsqi_bitpos[] = {
@@ -1526,6 +1531,27 @@ read_rtc:
  				bin2bcd(tmp));
  	}
 
+	/*
+	 * Some IPs have weekday reset value = 0x1 which might not correct
+	 * hence compute the wday using the current date/month/year values
+	 */
+	ds1307_get_time(&client->dev, &tm);
+	wday = tm.tm_wday;
+	timestamp = rtc_tm_to_time64(&tm);
+	rtc_time64_to_tm(timestamp, &tm);
+
+	/*
+	 * Check if reset wday is different from the computed wday
+	 * If different then set the wday which we computed using
+	 * timestamp
+	 */
+	if (wday != tm.tm_wday) {
+		wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
+		wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
+		wday = wday | (tm.tm_wday + 1);
+		i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
+	}
+
  	if (want_irq) {
  		device_set_wakeup_capable(&client->dev, true);
  		set_bit(HAS_ALARM, &ds1307->flags);
--

1.9.1

Signature · 7 lines
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#3 lore raw

Hi Alexandre,

Quoted 20 lines · Keerthy
Quoted 10 lines · Alexandre Belloni

Hi,

Quoted 6 lines · Keerthy

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Quoted 4 lines

Hum, what are the chances that the reset value is actually the correct date/time?
Won't that be corrected after the first call to set_time? Until then, the date is not correct so, do we care anyway?

Yes if an alarm is programmed without set_time.

Quoted 11 lines

ex: rtcwake -d /dev/rtc0 -s 5

Even the basic rtctest under tools/testing/selftests/timers/rtctest.c fails as the wday is wrong and no alarm fires.

Instead of relying on some one to call set_time before programming alarm its better to fix the wday to reflect the current day right?

Quoted 2 lines

Maybe I'm missing something here.

Let me know if you feel this is a valid fix.

Regards,
Keerthy

Quoted 81 lines
Quoted 81 lines
Quoted 80 lines

Hence compute the wday using the current date/month/year values. Check if reset wday is any different from the computed wday, If different then set the wday which we computed using date/month/year values.

Document Referred:
http://ww1.microchip.com/downloads/en/DeviceDoc/20002266F.pdf

Fixes 1d1945d261a2af "drivers/rtc/rtc-ds1307.c: add alarm support

for mcp7941x chips"

---
  drivers/rtc/rtc-ds1307.c | 28 +++++++++++++++++++++++++++-
  1 file changed, 27 insertions(+), 1 deletion(-)
drivers/rtc/rtc-ds1307.c +6 -1
index 821d9c0..d9045cc 100644
@@ -602,6 +602,8 @@ static const struct rtc_class_ops ds13xx_rtc_ops = {
   * Alarm support for mcp794xx devices.
   */
 
+#define MCP794XX_REG_WEEKDAY        0x3
+#define MCP794XX_REG_WEEKDAY_WDAY_MASK    0x7
  #define MCP794XX_REG_CONTROL        0x07
  #    define MCP794XX_BIT_ALM0_EN    0x10
  #    define MCP794XX_BIT_ALM1_EN    0x20
@@ -1231,13 +1233,16 @@ static int ds1307_probe(struct i2c_client
 *client,
  {
      struct ds1307        *ds1307;
      int            err = -ENODEV;
-    int            tmp;
+    int            tmp, wday;
      struct chip_desc    *chip = &chips[id->driver_data];
      struct i2c_adapter    *adapter =
 to_i2c_adapter(client->dev.parent);
      bool            want_irq = false;
      bool            ds1307_can_wakeup_device = false;
      unsigned char        *buf;
      struct ds1307_platform_data *pdata =
 dev_get_platdata(&client->dev);
+    struct rtc_time        tm;
+    unsigned long        timestamp;
+
      irq_handler_t    irq_handler = ds1307_irq;

      static const int    bbsqi_bitpos[] = {
drivers/rtc/rtc-ds1307.c +21 -1
@@ -1526,6 +1531,27 @@ read_rtc:
                  bin2bcd(tmp));
      }
 
+    /*
+     * Some IPs have weekday reset value = 0x1 which might not correct
+     * hence compute the wday using the current date/month/year values
+     */
+    ds1307_get_time(&client->dev, &tm);
+    wday = tm.tm_wday;
+    timestamp = rtc_tm_to_time64(&tm);
+    rtc_time64_to_tm(timestamp, &tm);
+
+    /*
+     * Check if reset wday is different from the computed wday
+     * If different then set the wday which we computed using
+     * timestamp
+     */
+    if (wday != tm.tm_wday) {
+        wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
+        wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
+        wday = wday | (tm.tm_wday + 1);
+        i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
+    }
+
      if (want_irq) {
          device_set_wakeup_capable(&client->dev, true);
          set_bit(HAS_ALARM, &ds1307->flags);
--

1.9.1

Signature · 7 lines
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#4 lore raw
Quoted 28 lines · Keerthy

Hi Alexandre,

Quoted 25 lines · Keerthy
Quoted 10 lines · Alexandre Belloni

Hi,

Quoted 6 lines · Keerthy

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Quoted 4 lines

Hum, what are the chances that the reset value is actually the correct date/time?
Won't that be corrected after the first call to set_time? Until then, the date is not correct so, do we care anyway?

Yes if an alarm is programmed without set_time.

ex: rtcwake -d /dev/rtc0 -s 5

Even the basic rtctest under tools/testing/selftests/timers/rtctest.c fails as the wday is wrong and no alarm fires.

Quoted 9 lines
Quoted 6 lines

Instead of relying on some one to call set_time before programming alarm its better to fix the wday to reflect the current day right?

Quoted 2 lines

Maybe I'm missing something here.

Let me know if you feel this is a valid fix.

Well, this is probably a valid fix and I'll take it as is if I don't find the time to make something more generic.

Signature · 13 lines
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com

-- 
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- 
You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#5 lore raw
Quoted 34 lines · Alexandre Belloni
Quoted 33 lines · Keerthy

Hi Alexandre,

Quoted 30 lines · Keerthy
Quoted 10 lines · Alexandre Belloni

Hi,

Quoted 6 lines · Keerthy

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Quoted 4 lines

Hum, what are the chances that the reset value is actually the correct date/time?
Won't that be corrected after the first call to set_time? Until then, the date is not correct so, do we care anyway?

Yes if an alarm is programmed without set_time.

ex: rtcwake -d /dev/rtc0 -s 5

Even the basic rtctest under tools/testing/selftests/timers/rtctest.c fails as the wday is wrong and no alarm fires.

Instead of relying on some one to call set_time before programming alarm its better to fix the wday to reflect the current day right?

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Maybe I'm missing something here.

Let me know if you feel this is a valid fix.

Well, this is probably a valid fix and I'll take it as is if I don't find the time to make something more generic.

Okay Thanks.

Quoted 1 line
Signature · 7 lines
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
#6 lore raw
In reply to Keerthy
Quoted 11 lines · Keerthy

The reset value of weekday is 0x1. This is wrong since the reset values of the day/month/year make up to Jan 1 2001. When computed weekday comes out to be Monday. On a scale of 1-7(Sunday - Saturday) it should be 0x2. So we should not be relying on the reset value.

Hence compute the wday using the current date/month/year values. Check if reset wday is any different from the computed wday, If different then set the wday which we computed using date/month/year values.

Quoted 9 lines

Document Referred:
http://ww1.microchip.com/downloads/en/DeviceDoc/20002266F.pdf

Fixes 1d1945d261a2af "drivers/rtc/rtc-ds1307.c: add alarm support for mcp7941x chips"
---
 drivers/rtc/rtc-ds1307.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

Applied, thanks.

Signature · 13 lines
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

-- 
You received this message because you are subscribed to "rtc-linux".
Membership options at http://groups.google.com/group/rtc-linux .
Please read http://groups.google.com/group/rtc-linux/web/checklist
before submitting a driver.
--- 
You received this message because you are subscribed to the Google Groups "rtc-linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.