Skip to contentSkip to conversation

rtc: add new VL flag for backup switchover

PATCH In review Alexandre Belloni 2 participants 7 messages May 5, 2020 Real time clock (RTC)

lore
Share & tools
lore

Alexandre Belloni

author maintainer
#0 lore raw

PATCH 1/5 rtc: add new VL flag for backup switchover

A new flag RTC_VL_BACKUP_SWITCH means that a backup switchover happened since last flag clear.

---
 include/uapi/linux/rtc.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/rtc.h +1 -0
index 83bba58d47f4..fa9aff91cbf2 100644
@@ -99,6 +99,7 @@ struct rtc_pll_info {
 #define RTC_VL_BACKUP_LOW	_BITUL(1) /* Backup voltage is low */
 #define RTC_VL_BACKUP_EMPTY	_BITUL(2) /* Backup empty or not present */
 #define RTC_VL_ACCURACY_LOW	_BITUL(3) /* Voltage is low, RTC accuracy is reduced */
+#define RTC_VL_BACKUP_SWITCH	_BITUL(4) /* Backup switchover happened */
 
 #define RTC_VL_READ	_IOR('p', 0x13, unsigned int)	/* Voltage low detection */
 #define RTC_VL_CLR	_IO('p', 0x14)		/* Clear voltage low information */
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH 2/5 rtc: pcf2127: let the core handle rtc range

Let the core handle offsetting and windowing the RTC range.

---
 drivers/rtc/rtc-pcf2127.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/rtc/rtc-pcf2127.c +5 -3
index 4e50d6768f13..136709baaa88 100644
@@ -137,8 +137,7 @@ static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
 	tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
 	tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
 	tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
-	if (tm->tm_year < 70)
-		tm->tm_year += 100;	/* assume we are in 1970...2069 */
+	tm->tm_year += 100;
 
 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
 		"mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -172,7 +171,7 @@ static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	buf[i++] = bin2bcd(tm->tm_mon + 1);
 
 	/* year */
-	buf[i++] = bin2bcd(tm->tm_year % 100);
+	buf[i++] = bin2bcd(tm->tm_year - 100);
 
 	/* write register's data */
 	err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
@@ -433,6 +432,9 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap,
 		return PTR_ERR(pcf2127->rtc);
 
 	pcf2127->rtc->ops = &pcf2127_rtc_ops;
+	pcf2127->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
+	pcf2127->rtc->range_max = RTC_TIMESTAMP_END_2099;
+	pcf2127->rtc->set_start_time = true; /* Sets actual start to 1970 */
 
 	pcf2127->wdd.parent = dev;
 	pcf2127->wdd.info = &pcf2127_wdt_info;
Signature · 1 line
2.26.2
author maintainer
#2 lore raw

PATCH 3/5 rtc: pcf2127: remove unnecessary #ifdef

In reply to Alexandre Belloni

There is not point in setting .ioctl to NULL when CONFIG_RTC_INTF_DEV is not defined because it would not be called anyway.

---
 drivers/rtc/rtc-pcf2127.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/rtc/rtc-pcf2127.c +0 -4
index 136709baaa88..5ac996578523 100644
@@ -184,7 +184,6 @@ static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
 	return 0;
 }
 
-#ifdef CONFIG_RTC_INTF_DEV
 static int pcf2127_rtc_ioctl(struct device *dev,
 				unsigned int cmd, unsigned long arg)
 {
@@ -205,9 +204,6 @@ static int pcf2127_rtc_ioctl(struct device *dev,
 		return -ENOIOCTLCMD;
 	}
 }
-#else
-#define pcf2127_rtc_ioctl NULL
-#endif
 
 static const struct rtc_class_ops pcf2127_rtc_ops = {
 	.ioctl		= pcf2127_rtc_ioctl,
Signature · 1 line
2.26.2
author maintainer
#3 lore raw

PATCH 4/5 rtc: pcf2127: set regmap max_register

In reply to Alexandre Belloni

Set regmap max_register to ease debugging and enforce the register range.

---
 drivers/rtc/rtc-pcf2127.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/rtc/rtc-pcf2127.c +2 -0
index 5ac996578523..039078029bd4 100644
@@ -634,6 +634,7 @@ static int pcf2127_i2c_probe(struct i2c_client *client,
 	static const struct regmap_config config = {
 		.reg_bits = 8,
 		.val_bits = 8,
+		.max_register = 0x1d,
 	};
 
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
@@ -701,6 +702,7 @@ static int pcf2127_spi_probe(struct spi_device *spi)
 		.val_bits = 8,
 		.read_flag_mask = 0xa0,
 		.write_flag_mask = 0x20,
+		.max_register = 0x1d,
 	};
 	struct regmap *regmap;
 
Signature · 1 line
2.26.2
author maintainer
#4 lore raw

PATCH 5/5 rtc: pcf2127: report battery switch over

In reply to Alexandre Belloni

Add support for the RTC_VL_BACKUP_SWITCH flag to report battery switch over events.

---
 drivers/rtc/rtc-pcf2127.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/rtc/rtc-pcf2127.c +12 -4
index 039078029bd4..967de68e1b03 100644
@@ -188,18 +188,27 @@ static int pcf2127_rtc_ioctl(struct device *dev,
 				unsigned int cmd, unsigned long arg)
 {
 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
-	int touser;
+	int val, touser = 0;
 	int ret;
 
 	switch (cmd) {
 	case RTC_VL_READ:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
+		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
 		if (ret)
 			return ret;
 
-		touser = touser & PCF2127_BIT_CTRL3_BLF ? RTC_VL_BACKUP_LOW : 0;
+		if (val & PCF2127_BIT_CTRL3_BLF)
+			touser = RTC_VL_BACKUP_LOW;
+
+		if (val & PCF2127_BIT_CTRL3_BF)
+			touser |= RTC_VL_BACKUP_SWITCH;
 
 		return put_user(touser, (unsigned int __user *)arg);
+
+	case RTC_VL_CLR:
+		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+					  PCF2127_BIT_CTRL3_BF, 0);
+
 	default:
 		return -ENOIOCTLCMD;
 	}
@@ -493,7 +502,6 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap,
 	 */
 	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
 				 PCF2127_BIT_CTRL3_BTSE |
-				 PCF2127_BIT_CTRL3_BF |
 				 PCF2127_BIT_CTRL3_BIE |
 				 PCF2127_BIT_CTRL3_BLIE, 0);
 	if (ret) {
Signature · 1 line
2.26.2
#5 lore raw

Re: [PATCH 5/5] rtc: pcf2127: report battery switch over

Quoted 23 lines · Alexandre Belloni

Add support for the RTC_VL_BACKUP_SWITCH flag to report battery switch over events.

---
 drivers/rtc/rtc-pcf2127.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/rtc/rtc-pcf2127.c +1 -2
index 039078029bd4..967de68e1b03 100644
@@ -188,18 +188,27 @@ static int pcf2127_rtc_ioctl(struct device *dev,
 				unsigned int cmd, unsigned long arg)
 {
 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
-	int touser;
+	int val, touser = 0;
 	int ret;
 
 	switch (cmd) {
 	case RTC_VL_READ:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);

drivers/rtc/rtc-pcf2127.c static int pcf2127_rtc_ioctl(struct device *dev, · line 204

Quoted 10 lines
drivers/rtc/rtc-pcf2127.c +6 -1
+		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
 		if (ret)
 			return ret;
 
-		touser = touser & PCF2127_BIT_CTRL3_BLF ? RTC_VL_BACKUP_LOW : 0;
+		if (val & PCF2127_BIT_CTRL3_BLF)
+			touser = RTC_VL_BACKUP_LOW;
+
+		if (val & PCF2127_BIT_CTRL3_BF)
+			touser |= RTC_VL_BACKUP_SWITCH;

I think it's a bit easier to read if you use |= in both cases.

Re patch 3, one saves a little .text by eliding the ioctl function when, as you say, it cannot be called anyway. No strong opinion either way, I don't think anybody actually builds without CONFIG_RTC_INTF_DEV, but those that do are probably the ones that care about having a tiny vmlinux.

Other than that, the series looks good to me.

Thanks,
Rasmus

author maintainer
#6 lore raw

Re: [PATCH 5/5] rtc: pcf2127: report battery switch over

Quoted 37 lines · Rasmus Villemoes
Quoted 23 lines · Alexandre Belloni

Add support for the RTC_VL_BACKUP_SWITCH flag to report battery switch over events.

---
 drivers/rtc/rtc-pcf2127.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/rtc/rtc-pcf2127.c +1 -2
index 039078029bd4..967de68e1b03 100644
@@ -188,18 +188,27 @@ static int pcf2127_rtc_ioctl(struct device *dev,
 				unsigned int cmd, unsigned long arg)
 {
 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
-	int touser;
+	int val, touser = 0;
 	int ret;
 
 	switch (cmd) {
 	case RTC_VL_READ:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);

drivers/rtc/rtc-pcf2127.c static int pcf2127_rtc_ioctl(struct device *dev, · line 204

Quoted 10 lines
drivers/rtc/rtc-pcf2127.c +6 -1
+		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &val);
 		if (ret)
 			return ret;
 
-		touser = touser & PCF2127_BIT_CTRL3_BLF ? RTC_VL_BACKUP_LOW : 0;
+		if (val & PCF2127_BIT_CTRL3_BLF)
+			touser = RTC_VL_BACKUP_LOW;
+
+		if (val & PCF2127_BIT_CTRL3_BF)
+			touser |= RTC_VL_BACKUP_SWITCH;

I think it's a bit easier to read if you use |= in both cases.

Quoted 5 lines

Re patch 3, one saves a little .text by eliding the ioctl function when, as you say, it cannot be called anyway. No strong opinion either way, I don't think anybody actually builds without CONFIG_RTC_INTF_DEV, but those that do are probably the ones that care about having a tiny vmlinux.

Honestly, I don't think it is worth doing that. On armv7, this only removes 248 bytes. Also, compiling without CONFIG_RTC_INTF_DEV simply makes the RTC unusable. There are no tools actually using the sysfs interface instead of the char device interface. I prefer keeping CONFIG_RTC_INTF_DEV private to the core.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com