Skip to contentSkip to conversation

rtc: pcf2127: add support for battery low voltage detection

PATCH In review Hugo Villeneuve 4 participants 16 messages Mar 11 – Nov 23 Real time clock (RTC)

lore
Share & tools
lore
#0 lore raw

cover rtc: pcf2127: add support for battery low voltage detection

From: Hugo Villeneuve <[email protected]>

Hello,
this patch series adds support for battery low voltage detection configuration for RTC devicesm, with specifc changes targeted at the pcf2127/2131 devices.

The origin of this patch goes back to the initial commit to add support for the PCF2131 device. On the PCF2131, the battery low voltage detection is disabled by default at the hardware level, contrary to the PCF2127 where it is enabled by default. Because of that, a lot of people are stuck with a PCF2131 RTC device using a battery backup, but unable to use it!

A lot of people are writing to me in private emails to report this as a bug in the PCF2131 driver. Others (and me) have proposed to implement device tree properties to enable these functions, but this was rejected [1][2][3].

It is important to note that some projects do not have the luxury to modify the bootloder to enable that function at boot, and for these having a DT property that could be put in a DT overlay would simplify a lot their life. Also having to rely on a userspace application to configure the RTC is also not ideal, as some projects use stock Debian distros (for example), and adding a new application to their repository is not trivial or easy.

So as the next best thing, this patch aims to add what is missing in the driver, the ability to enable/disable the battery low voltage detection with ioctls, similarly to what is done with the BSM.

This patch has been tested on a custom board with a PCF2131 and using my userspace application:

git clone -b batlow_param [email protected]:repos/hvrtc.git

Thank you.

Hugo Villeneuve (4):
rtc: pcf2127: remove redundant break statement in switch-case
rtc: pcf2127: add pcf2127_pwrmng_get/set
rtc: add battery low voltage detection feature
rtc: pcf2127: support battery low voltage detection function

 drivers/rtc/rtc-pcf2127.c | 94 ++++++++++++++++++++++++++++++---------
 include/uapi/linux/rtc.h  |  7 ++-
 2 files changed, 80 insertions(+), 21 deletions(-)

base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/4 rtc: pcf2127: remove redundant break statement in switch-case

From: Hugo Villeneuve <[email protected]>

Remove unreachable break statement after return.

---
 drivers/rtc/rtc-pcf2127.c | 2 --
 1 file changed, 2 deletions(-)
drivers/rtc/rtc-pcf2127.c +0 -2
index bb4fe81d3d62c..e2e9746027348 100644
@@ -404,8 +404,6 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
 					  PCF2127_CTRL3_PM,
 					  FIELD_PREP(PCF2127_CTRL3_PM, mode + value));
 
-		break;
-
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/4 rtc: pcf2127: add pcf2127_pwrmng_get/set

In reply to Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Add common functions to get/set the pwrmng field in the CTRL3 register, used by pcf2127_param_get() and pcf2127_param_set().

---
These functions will also be used in the following patch to add battery low detection.

---
 drivers/rtc/rtc-pcf2127.c | 42 +++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 13 deletions(-)
drivers/rtc/rtc-pcf2127.c +29 -13
index e2e9746027348..0605295026564 100644
@@ -213,6 +213,30 @@ struct pcf2127 {
 	bool ts_valid[PCF2127_MAX_TS_SUPPORTED];  /* Timestamp valid indication. */
 };
 
+static int pcf2127_pwrmng_get(struct device *dev, u8 *pwrmng)
+{
+	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+	u32 value;
+	int ret;
+
+	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+	if (ret < 0)
+		return ret;
+
+	*pwrmng = FIELD_GET(PCF2127_CTRL3_PM, value);
+
+	return 0;
+}
+
+static int pcf2127_pwrmng_set(struct device *dev, u8 pwrmng)
+{
+	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+
+	return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+				  PCF2127_CTRL3_PM,
+				  FIELD_PREP(PCF2127_CTRL3_PM, pwrmng));
+}
+
 /*
  * In the routines that deal directly with the pcf2127 hardware, we use
  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -337,18 +361,15 @@ static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
 static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
 {
-	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
-	u32 value;
+	u8 value;
 	int ret;
 
 	switch (param->param) {
 	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+		ret = pcf2127_pwrmng_get(dev, &value);
 		if (ret < 0)
 			return ret;
 
-		value = FIELD_GET(PCF2127_CTRL3_PM, value);
-
 		if (value < 0x3)
 			param->uvalue = RTC_BSM_LEVEL;
 		else if (value < 0x6)
@@ -367,19 +388,16 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
 
 static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
 {
-	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
 	u8 mode = 0;
-	u32 value;
+	u8 value;
 	int ret;
 
 	switch (param->param) {
 	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+		ret = pcf2127_pwrmng_get(dev, &value);
 		if (ret < 0)
 			return ret;
 
-		value = FIELD_GET(PCF2127_CTRL3_PM, value);
-
 		if (value > 5)
 			value -= 5;
 		else if (value > 2)
@@ -400,9 +418,7 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
 			return -EINVAL;
 		}
 
-		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
-					  PCF2127_CTRL3_PM,
-					  FIELD_PREP(PCF2127_CTRL3_PM, mode + value));
+		return pcf2127_pwrmng_set(dev, mode + value);
 
 	default:
 		return -EINVAL;
Signature · 1 line
2.47.3
Reviewed-by on v1 2/4
#3 lore raw

Re: [PATCH 2/4] rtc: pcf2127: add pcf2127_pwrmng_get/set

Quoted 92 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Add common functions to get/set the pwrmng field in the CTRL3 register, used by pcf2127_param_get() and pcf2127_param_set().

---
These functions will also be used in the following patch to add battery low detection.

---
  drivers/rtc/rtc-pcf2127.c | 42 +++++++++++++++++++++++++++------------
  1 file changed, 29 insertions(+), 13 deletions(-)
drivers/rtc/rtc-pcf2127.c +28 -10
index e2e9746027348..0605295026564 100644
@@ -213,6 +213,30 @@ struct pcf2127 {
  	bool ts_valid[PCF2127_MAX_TS_SUPPORTED];  /* Timestamp valid indication. */
  };
 
+static int pcf2127_pwrmng_get(struct device *dev, u8 *pwrmng)
+{
+	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+	u32 value;
+	int ret;
+
+	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+	if (ret < 0)
+		return ret;
+
+	*pwrmng = FIELD_GET(PCF2127_CTRL3_PM, value);
+
+	return 0;
+}
+
+static int pcf2127_pwrmng_set(struct device *dev, u8 pwrmng)
+{
+	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+
+	return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+				  PCF2127_CTRL3_PM,
+				  FIELD_PREP(PCF2127_CTRL3_PM, pwrmng));
+}
+
  /*
   * In the routines that deal directly with the pcf2127 hardware, we use
   * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
@@ -337,18 +361,15 @@ static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
  static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
  {
-	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
-	u32 value;
+	u8 value;
  	int ret;
 
  	switch (param->param) {
  	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+		ret = pcf2127_pwrmng_get(dev, &value);
  		if (ret < 0)
  			return ret;
 
-		value = FIELD_GET(PCF2127_CTRL3_PM, value);
-
  		if (value < 0x3)
  			param->uvalue = RTC_BSM_LEVEL;
  		else if (value < 0x6)
@@ -367,19 +388,16 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
 
  static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
  {
-	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
  	u8 mode = 0;
-	u32 value;
+	u8 value;
  	int ret;
 
  	switch (param->param) {
  	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+		ret = pcf2127_pwrmng_get(dev, &value);
  		if (ret < 0)
  			return ret;
 
-		value = FIELD_GET(PCF2127_CTRL3_PM, value);
-
  		if (value > 5)
  			value -= 5;
  		else if (value > 2)

drivers/rtc/rtc-pcf2127.c static int pcf2127_param_set(struct device *dev, struct rtc_param *param) · line 424

Quoted 10 lines
drivers/rtc/rtc-pcf2127.c +1 -3
@@ -400,9 +418,7 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
  			return -EINVAL;
  		}
 
-		return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
-					  PCF2127_CTRL3_PM,
-					  FIELD_PREP(PCF2127_CTRL3_PM, mode + value));
+		return pcf2127_pwrmng_set(dev, mode + value);
 
  	default:
  		return -EINVAL;
Signature · 4 lines
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
#4 lore raw

PATCH 3/4 rtc: add battery low voltage detection feature

In reply to Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
 include/uapi/linux/rtc.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
include/uapi/linux/rtc.h +6 -1
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
 #define RTC_FEATURE_CORRECTION		5
 #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
 #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9
 
 /* parameter list */
 #define RTC_PARAM_FEATURES		0
 #define RTC_PARAM_CORRECTION		1
 #define RTC_PARAM_BACKUP_SWITCH_MODE	2
+#define RTC_PARAM_BATTERY_LOW_DETECT	3
 
 #define RTC_BSM_DISABLED	0
 #define RTC_BSM_DIRECT		1
 #define RTC_BSM_LEVEL		2
 #define RTC_BSM_STANDBY		3
 
+#define RTC_BATTERY_LOW_DETECT_DISABLED	0
+#define RTC_BATTERY_LOW_DETECT_ENABLED	1
+
 #define RTC_MAX_FREQ	8192
 
 
Signature · 1 line
2.47.3
Reviewed-by Tested-by on v1 3/4
#5 lore raw

Re: [PATCH 3/4] rtc: add battery low voltage detection feature

Quoted 29 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
  include/uapi/linux/rtc.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
include/uapi/linux/rtc.h +3 -1
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
  #define RTC_FEATURE_CORRECTION		5
  #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
  #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9
 
  /* parameter list */
  #define RTC_PARAM_FEATURES		0
  #define RTC_PARAM_CORRECTION		1
  #define RTC_PARAM_BACKUP_SWITCH_MODE	2
+#define RTC_PARAM_BATTERY_LOW_DETECT	3
 
  #define RTC_BSM_DISABLED	0

include/uapi/linux/rtc.h struct rtc_param { · line 156

Quoted 10 lines
include/uapi/linux/rtc.h +3 -0
  #define RTC_BSM_DIRECT		1
  #define RTC_BSM_LEVEL		2
  #define RTC_BSM_STANDBY		3
 
+#define RTC_BATTERY_LOW_DETECT_DISABLED	0
+#define RTC_BATTERY_LOW_DETECT_ENABLED	1
+
  #define RTC_MAX_FREQ	8192
 
 

I have opened a PR against util-linux to add support and tested the patchset with the extended hwclock binary.

See: https://github.com/util-linux/util-linux/pull/4513

Signature · 4 lines
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
#6 lore raw

Re: [PATCH 3/4] rtc: add battery low voltage detection feature

Hello,

Quoted 13 lines · Stefan Kerkmann
Quoted 10 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
  include/uapi/linux/rtc.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 11 lines

include/uapi/linux/rtc.h struct rtc_param { · line 138

Quoted 11 lines
include/uapi/linux/rtc.h +2 -1
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
  #define RTC_FEATURE_CORRECTION		5
  #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
  #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9

This is adding a new ABI and it took me a while to find enough time to review the datasheet for the RTCs we support and others. What we need instead of a boolean is a voltage threshold. So maybe this should be better named something like RTC_FEATURE_BATTERY_LOW_THRES_MV

Quoted 11 lines

include/uapi/linux/rtc.h struct rtc_param { · line 152

Quoted 11 lines
include/uapi/linux/rtc.h +3 -0
  /* parameter list */
  #define RTC_PARAM_FEATURES		0
  #define RTC_PARAM_CORRECTION		1
  #define RTC_PARAM_BACKUP_SWITCH_MODE	2
+#define RTC_PARAM_BATTERY_LOW_DETECT	3
  #define RTC_BSM_DISABLED	0
  #define RTC_BSM_DIRECT		1
  #define RTC_BSM_LEVEL		2
  #define RTC_BSM_STANDBY		3
+#define RTC_BATTERY_LOW_DETECT_DISABLED	0
+#define RTC_BATTERY_LOW_DETECT_ENABLED	1

And so those wouldn't be needed.

Quoted 17 lines

include/uapi/linux/rtc.h struct rtc_param { · line 154

Quoted 2 lines
include/uapi/linux/rtc.h +1 -0
+
  #define RTC_MAX_FREQ	8192

I have opened a PR against util-linux to add support and tested the patchset with the extended hwclock binary.

See: https://github.com/util-linux/util-linux/pull/4513

Signature · 4 lines
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#7 lore raw

Re: [PATCH 3/4] rtc: add battery low voltage detection feature

Quoted 33 lines · Alexandre Belloni

Hello,

Quoted 13 lines · Stefan Kerkmann
Quoted 10 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
  include/uapi/linux/rtc.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 11 lines

include/uapi/linux/rtc.h struct rtc_param { · line 138

Quoted 11 lines
include/uapi/linux/rtc.h +2 -1
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
  #define RTC_FEATURE_CORRECTION		5
  #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
  #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9

This is adding a new ABI and it took me a while to find enough time to review the datasheet for the RTCs we support and others. What we need instead of a boolean is a voltage threshold. So maybe this should be better named something like RTC_FEATURE_BATTERY_LOW_THRES_MV

Quoted 14 lines
Quoted 11 lines

include/uapi/linux/rtc.h struct rtc_param { · line 152

Quoted 11 lines
include/uapi/linux/rtc.h +3 -0
  /* parameter list */
  #define RTC_PARAM_FEATURES		0
  #define RTC_PARAM_CORRECTION		1
  #define RTC_PARAM_BACKUP_SWITCH_MODE	2
+#define RTC_PARAM_BATTERY_LOW_DETECT	3
  #define RTC_BSM_DISABLED	0
  #define RTC_BSM_DIRECT		1
  #define RTC_BSM_LEVEL		2
  #define RTC_BSM_STANDBY		3
+#define RTC_BATTERY_LOW_DETECT_DISABLED	0
+#define RTC_BATTERY_LOW_DETECT_ENABLED	1

And so those wouldn't be needed.

To be clear, this is my only comment, with this change, I'll apply the series.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#8 lore raw

Re: [PATCH 3/4] rtc: add battery low voltage detection feature

Hi Alexandre,

Quoted 17 lines · On Fri, 20 Nov 2026 17:12:49 +0100 Alexandre Belloni…
Quoted 16 lines · Alexandre Belloni

Hello,

Quoted 13 lines · Stefan Kerkmann
Quoted 10 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
  include/uapi/linux/rtc.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
include/uapi/linux/rtc.h +0 -0
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
  #define RTC_FEATURE_CORRECTION		5
  #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
  #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
Quoted 8 lines
Quoted 3 lines

include/uapi/linux/rtc.h struct rtc_param { · line 138

Quoted 3 lines
include/uapi/linux/rtc.h +2 -1
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9

This is adding a new ABI and it took me a while to find enough time to review the datasheet for the RTCs we support and others. What we need instead of a boolean is a voltage threshold. So maybe this should be better named something like RTC_FEATURE_BATTERY_LOW_THRES_MV

I am not sure to understand that proposal, because with the pcf2127/31, you can enable the low battery detection function, but you cannot select the threshold voltage...

Quoted 16 lines
Quoted 15 lines
Quoted 12 lines

include/uapi/linux/rtc.h struct rtc_param { · line 152

Quoted 12 lines
include/uapi/linux/rtc.h +3 -0
  /* parameter list */
  #define RTC_PARAM_FEATURES		0
  #define RTC_PARAM_CORRECTION		1
  #define RTC_PARAM_BACKUP_SWITCH_MODE	2
+#define RTC_PARAM_BATTERY_LOW_DETECT	3
  #define RTC_BSM_DISABLED	0
  #define RTC_BSM_DIRECT		1
  #define RTC_BSM_LEVEL		2
  #define RTC_BSM_STANDBY		3
+#define RTC_BATTERY_LOW_DETECT_DISABLED	0
+#define RTC_BATTERY_LOW_DETECT_ENABLED	1

And so those wouldn't be needed.

Quoted 2 lines

To be clear, this is my only comment, with this change, I'll apply the series.

Glad to hear that, thank you.

Quoted 6 lines
Signature · 4 lines
-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Signature · 1 line
Hugo Villeneuve
#9 lore raw

Re: [PATCH 3/4] rtc: add battery low voltage detection feature

Quoted 39 lines · Hugo Villeneuve

Hi Alexandre,

Quoted 17 lines · On Fri, 20 Nov 2026 17:12:49 +0100 Alexandre Belloni…
Quoted 16 lines · Alexandre Belloni

Hello,

Quoted 13 lines · Stefan Kerkmann
Quoted 10 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
  include/uapi/linux/rtc.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
include/uapi/linux/rtc.h +0 -0
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
  #define RTC_FEATURE_CORRECTION		5
  #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
  #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
Quoted 8 lines
Quoted 3 lines

include/uapi/linux/rtc.h struct rtc_param { · line 138

Quoted 3 lines
include/uapi/linux/rtc.h +2 -1
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9

This is adding a new ABI and it took me a while to find enough time to review the datasheet for the RTCs we support and others. What we need instead of a boolean is a voltage threshold. So maybe this should be better named something like RTC_FEATURE_BATTERY_LOW_THRES_MV

Quoted 3 lines

I am not sure to understand that proposal, because with the pcf2127/31, you can enable the low battery detection function, but you cannot select the threshold voltage...

This is exactly my point, there are RTCs where you can select the threshold voltage so we have to design for them. We can have 0mv mean disabled because we are never going to have a 0 or negative threshold.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#10 lore raw

Re: [PATCH 3/4] rtc: add battery low voltage detection feature

Hi Alexandre,

Quoted 40 lines · On Sat, 21 Nov 2026 13:36:32 +0100 Alexandre Belloni…
Quoted 39 lines · Hugo Villeneuve

Hi Alexandre,

Quoted 33 lines · On Fri, 20 Nov 2026 17:12:49 +0100 Alexandre Belloni…
Quoted 32 lines · Alexandre Belloni

Hello,

Quoted 13 lines · Stefan Kerkmann
Quoted 10 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Some RTCs have a battery low voltage detection function. Add new feature so that it can be enabled, disabled or queried at runtime.

---
  include/uapi/linux/rtc.h | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 11 lines

include/uapi/linux/rtc.h struct rtc_param { · line 138

Quoted 11 lines
include/uapi/linux/rtc.h +2 -1
index 97aca4503a6a3..596eec119bb3a 100644
@@ -134,18 +134,23 @@ struct rtc_param {
  #define RTC_FEATURE_CORRECTION		5
  #define RTC_FEATURE_BACKUP_SWITCH_MODE	6
  #define RTC_FEATURE_ALARM_WAKEUP_ONLY	7
-#define RTC_FEATURE_CNT			8
+#define RTC_FEATURE_BATTERY_LOW_DETECT	8
+#define RTC_FEATURE_CNT			9

This is adding a new ABI and it took me a while to find enough time to review the datasheet for the RTCs we support and others. What we need instead of a boolean is a voltage threshold. So maybe this should be better named something like RTC_FEATURE_BATTERY_LOW_THRES_MV

Quoted 7 lines
Quoted 3 lines

I am not sure to understand that proposal, because with the pcf2127/31, you can enable the low battery detection function, but you cannot select the threshold voltage...

This is exactly my point, there are RTCs where you can select the threshold voltage so we have to design for them. We can have 0mv mean disabled because we are never going to have a 0 or negative threshold.

For me, it is confusing and weird to have a threshold voltage parameter that you can set on ICs that a have a fixed threshold voltage.

Also, once you set a threshold voltage for an IC that have that capability, setting it to 0 to disable it seems more complicated than simply setting the parameter to enable or disable. This also means that if some application/service at startup were to set a threshold voltage, then disabling it later by setting it to 0mv would lose the threshold voltage previously set.

So, why not simply add a new parameter like you suggested to set the threshold voltage on ICs that support it, RTC_FEATURE_BATTERY_LOW_THRES_MV? Then you can reuse the enable/disable boolean to enable/disable the function and keep the threshold voltage unaffected/unmodified.

Hugo.

#11 lore raw

PATCH 4/4 rtc: pcf2127: support battery low voltage detection function

In reply to Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Add support for parameter RTC_PARAM_BATTERY_LOW_DETECT in RTC_PARAM_SET ioctl to enable/disable/query battery low voltage detection. This is especially relevant on the pcf2131 where this function is disabled by default, contrary to the pcf2127.

---
 drivers/rtc/rtc-pcf2127.c | 60 ++++++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 10 deletions(-)
drivers/rtc/rtc-pcf2127.c +50 -10
index 0605295026564..05b08867ffcb3 100644
@@ -364,12 +364,12 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
 	u8 value;
 	int ret;
 
+	ret = pcf2127_pwrmng_get(dev, &value);
+	if (ret < 0)
+		return ret;
+
 	switch (param->param) {
 	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = pcf2127_pwrmng_get(dev, &value);
-		if (ret < 0)
-			return ret;
-
 		if (value < 0x3)
 			param->uvalue = RTC_BSM_LEVEL;
 		else if (value < 0x6)
@@ -379,6 +379,14 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
 
 		break;
 
+	case RTC_PARAM_BATTERY_LOW_DETECT:
+		if (value == 0x0 || value == 0x3)
+			param->uvalue = RTC_BATTERY_LOW_DETECT_ENABLED;
+		else
+			param->uvalue = RTC_BATTERY_LOW_DETECT_DISABLED;
+
+		break;
+
 	default:
 		return -EINVAL;
 	}
@@ -392,12 +400,12 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
 	u8 value;
 	int ret;
 
+	ret = pcf2127_pwrmng_get(dev, &value);
+	if (ret < 0)
+		return ret;
+
 	switch (param->param) {
 	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = pcf2127_pwrmng_get(dev, &value);
-		if (ret < 0)
-			return ret;
-
 		if (value > 5)
 			value -= 5;
 		else if (value > 2)
@@ -418,13 +426,45 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
 			return -EINVAL;
 		}
 
-		return pcf2127_pwrmng_set(dev, mode + value);
+		break;
+
+	case RTC_PARAM_BATTERY_LOW_DETECT:
+		if (value > 5) {
+			value -= 5;
+			mode = 5;
+		} else if (value > 2) {
+			value -= 3;
+			mode = 3;
+		}
+
+		switch (param->uvalue) {
+		case RTC_BATTERY_LOW_DETECT_DISABLED:
+			if (mode != 5)
+				if (value == 0)
+					value = 1;
+
+			break;
+		case RTC_BATTERY_LOW_DETECT_ENABLED:
+			if (mode != 5)
+				value = 0; /* Enable battery low detection. */
+			else
+				return -EINVAL; /*
+						 * battery low detection can't be enabled if
+						 * battery switch over is disabled.
+						 */
+			break;
+
+		default:
+			return -EINVAL;
+		}
+
+		break;
 
 	default:
 		return -EINVAL;
 	}
 
-	return 0;
+	return pcf2127_pwrmng_set(dev, mode + value);
 }
 
 static int pcf2127_rtc_ioctl(struct device *dev,
Signature · 1 line
2.47.3
Reviewed-by Tested-by on v1 4/4
#12 lore raw

Re: [PATCH 4/4] rtc: pcf2127: support battery low voltage detection function

Quoted 103 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Add support for parameter RTC_PARAM_BATTERY_LOW_DETECT in RTC_PARAM_SET ioctl to enable/disable/query battery low voltage detection. This is especially relevant on the pcf2131 where this function is disabled by default, contrary to the pcf2127.

---
  drivers/rtc/rtc-pcf2127.c | 60 ++++++++++++++++++++++++++++++++-------
  1 file changed, 50 insertions(+), 10 deletions(-)
drivers/rtc/rtc-pcf2127.c +49 -9
index 0605295026564..05b08867ffcb3 100644
@@ -364,12 +364,12 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
  	u8 value;
  	int ret;
 
+	ret = pcf2127_pwrmng_get(dev, &value);
+	if (ret < 0)
+		return ret;
+
  	switch (param->param) {
  	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = pcf2127_pwrmng_get(dev, &value);
-		if (ret < 0)
-			return ret;
-
  		if (value < 0x3)
  			param->uvalue = RTC_BSM_LEVEL;
  		else if (value < 0x6)
@@ -379,6 +379,14 @@ static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
 
  		break;
 
+	case RTC_PARAM_BATTERY_LOW_DETECT:
+		if (value == 0x0 || value == 0x3)
+			param->uvalue = RTC_BATTERY_LOW_DETECT_ENABLED;
+		else
+			param->uvalue = RTC_BATTERY_LOW_DETECT_DISABLED;
+
+		break;
+
  	default:
  		return -EINVAL;
  	}
@@ -392,12 +400,12 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
  	u8 value;
  	int ret;
 
+	ret = pcf2127_pwrmng_get(dev, &value);
+	if (ret < 0)
+		return ret;
+
  	switch (param->param) {
  	case RTC_PARAM_BACKUP_SWITCH_MODE:
-		ret = pcf2127_pwrmng_get(dev, &value);
-		if (ret < 0)
-			return ret;
-
  		if (value > 5)
  			value -= 5;
  		else if (value > 2)
@@ -418,13 +426,45 @@ static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
  			return -EINVAL;
  		}
 
-		return pcf2127_pwrmng_set(dev, mode + value);
+		break;
+
+	case RTC_PARAM_BATTERY_LOW_DETECT:
+		if (value > 5) {
+			value -= 5;
+			mode = 5;
+		} else if (value > 2) {
+			value -= 3;
+			mode = 3;
+		}
+
+		switch (param->uvalue) {
+		case RTC_BATTERY_LOW_DETECT_DISABLED:
+			if (mode != 5)
+				if (value == 0)
+					value = 1;
+
+			break;
+		case RTC_BATTERY_LOW_DETECT_ENABLED:
+			if (mode != 5)
+				value = 0; /* Enable battery low detection. */
+			else
+				return -EINVAL; /*
+						 * battery low detection can't be enabled if
+						 * battery switch over is disabled.
+						 */
+			break;
+
+		default:
+			return -EINVAL;
+		}
+
+		break;

drivers/rtc/rtc-pcf2127.c static int pcf2127_param_set(struct device *dev, struct rtc_param *param) · line 470

Quoted 10 lines
drivers/rtc/rtc-pcf2127.c +1 -1
 
  	default:
  		return -EINVAL;
  	}
 
-	return 0;
+	return pcf2127_pwrmng_set(dev, mode + value);
  }
 
  static int pcf2127_rtc_ioctl(struct device *dev,

Works as expected, thank you!

Signature · 4 lines
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
#13 lore raw
In reply to Hugo Villeneuve

Hello Hugo,

Quoted 23 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Hello,
this patch series adds support for battery low voltage detection configuration for RTC devicesm, with specifc changes targeted at the pcf2127/2131 devices.

The origin of this patch goes back to the initial commit to add support for the PCF2131 device. On the PCF2131, the battery low voltage detection is disabled by default at the hardware level, contrary to the PCF2127 where it is enabled by default. Because of that, a lot of people are stuck with a PCF2131 RTC device using a battery backup, but unable to use it!

A lot of people are writing to me in private emails to report this as a bug in the PCF2131 driver. Others (and me) have proposed to implement device tree properties to enable these functions, but this was rejected [1][2][3].

It is important to note that some projects do not have the luxury to modify the bootloder to enable that function at boot, and for these having a DT property that could be put in a DT overlay would simplify a lot their life. Also having to rely on a userspace application to configure the RTC is also not ideal, as some projects use stock Debian distros (for example), and adding a new application to their repository is not trivial or easy.

Quoted 4 lines

So as the next best thing, this patch aims to add what is missing in the driver, the ability to enable/disable the battery low voltage detection with ioctls, similarly to what is done with the BSM.

Thank you for the series. We have customer that has this exact use case:

Enabling the backup battery switch-over and low battery detection on a pcf2131 rtc. With util-linux's hwclock the former can already be enabled e.g. hwclock --param-set bsm <xyz> and for the later this patch series would enable it as well.

Quoted 5 lines

This patch has been tested on a custom board with a PCF2131 and using my userspace application:

git clone -b batlow_param [email protected]:repos/hvrtc.git

If this series gets merged I can sent a PR to util-linux to enable the low battery detection via hwclock as well.

Quoted 12 lines

Thank you.

Hugo Villeneuve (4):
rtc: pcf2127: remove redundant break statement in switch-case
rtc: pcf2127: add pcf2127_pwrmng_get/set
rtc: add battery low voltage detection feature
rtc: pcf2127: support battery low voltage detection function

Quoted 6 lines
  drivers/rtc/rtc-pcf2127.c | 94 ++++++++++++++++++++++++++++++---------
  include/uapi/linux/rtc.h  |  7 ++-
  2 files changed, 80 insertions(+), 21 deletions(-)

base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f

Best regards,
Stefan

Signature · 4 lines
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
#14 lore raw

Hi Stefan,

Quoted 26 lines · On Wed, 29 Jul 2026 17:18:59 +0200 Stefan Kerkmann <[email protected]>…

Hello Hugo,

Quoted 23 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Hello,
this patch series adds support for battery low voltage detection configuration for RTC devicesm, with specifc changes targeted at the pcf2127/2131 devices.

The origin of this patch goes back to the initial commit to add support for the PCF2131 device. On the PCF2131, the battery low voltage detection is disabled by default at the hardware level, contrary to the PCF2127 where it is enabled by default. Because of that, a lot of people are stuck with a PCF2131 RTC device using a battery backup, but unable to use it!

A lot of people are writing to me in private emails to report this as a bug in the PCF2131 driver. Others (and me) have proposed to implement device tree properties to enable these functions, but this was rejected [1][2][3].

It is important to note that some projects do not have the luxury to modify the bootloder to enable that function at boot, and for these having a DT property that could be put in a DT overlay would simplify a lot their life. Also having to rely on a userspace application to configure the RTC is also not ideal, as some projects use stock Debian distros (for example), and adding a new application to their repository is not trivial or easy.

Quoted 6 lines
Quoted 4 lines

So as the next best thing, this patch aims to add what is missing in the driver, the ability to enable/disable the battery low voltage detection with ioctls, similarly to what is done with the BSM.

Thank you for the series. We have customer that has this exact use case:

Ok, a lot of people affected by this...

Quoted 5 lines

Enabling the backup battery switch-over and low battery detection on a pcf2131 rtc. With util-linux's hwclock the former can already be enabled e.g. hwclock --param-set bsm <xyz> and for the later this patch series would enable it as well.

Quoted 8 lines
Quoted 5 lines

This patch has been tested on a custom board with a PCF2131 and using my userspace application:

git clone -b batlow_param [email protected]:repos/hvrtc.git

If this series gets merged I can sent a PR to util-linux to enable the low battery detection via hwclock as well.

Good.

Unfortunately, I haven't received any feedback from the RTC maintainer(s) about this series, nor have I received any answers to my previous inquiries about this problem.

I send patches to other kernel subsystem, and eventually I always receive feedback, but RTC seems to be in a
different category. I sometimes wonder if there is still an active RTC maintainer...

Hugo.

Quoted 27 lines
Quoted 12 lines

Thank you.

Hugo Villeneuve (4):
rtc: pcf2127: remove redundant break statement in switch-case
rtc: pcf2127: add pcf2127_pwrmng_get/set
rtc: add battery low voltage detection feature
rtc: pcf2127: support battery low voltage detection function

Quoted 6 lines
  drivers/rtc/rtc-pcf2127.c | 94 ++++++++++++++++++++++++++++++---------
  include/uapi/linux/rtc.h  |  7 ++-
  2 files changed, 80 insertions(+), 21 deletions(-)

base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f

Best regards,
Stefan

Signature · 4 lines
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |
Signature · 1 line
Hugo Villeneuve
#15 lore raw
In reply to Hugo Villeneuve

Hello Alexandre,

can you pick up this patch? It has been tested by Stefan Kerkmann and it's working for our use case.

regards,
Marc

Quoted 54 lines · Hugo Villeneuve

From: Hugo Villeneuve <[email protected]>

Hello,
this patch series adds support for battery low voltage detection configuration for RTC devicesm, with specifc changes targeted at the pcf2127/2131 devices.

The origin of this patch goes back to the initial commit to add support for the PCF2131 device. On the PCF2131, the battery low voltage detection is disabled by default at the hardware level, contrary to the PCF2127 where it is enabled by default. Because of that, a lot of people are stuck with a PCF2131 RTC device using a battery backup, but unable to use it!

A lot of people are writing to me in private emails to report this as a bug in the PCF2131 driver. Others (and me) have proposed to implement device tree properties to enable these functions, but this was rejected [1][2][3].

It is important to note that some projects do not have the luxury to modify the bootloder to enable that function at boot, and for these having a DT property that could be put in a DT overlay would simplify a lot their life. Also having to rely on a userspace application to configure the RTC is also not ideal, as some projects use stock Debian distros (for example), and adding a new application to their repository is not trivial or easy.

So as the next best thing, this patch aims to add what is missing in the driver, the ability to enable/disable the battery low voltage detection with ioctls, similarly to what is done with the BSM.

This patch has been tested on a custom board with a PCF2131 and using my userspace application:

git clone -b batlow_param [email protected]:repos/hvrtc.git

Thank you.

Hugo Villeneuve (4):
rtc: pcf2127: remove redundant break statement in switch-case
rtc: pcf2127: add pcf2127_pwrmng_get/set
rtc: add battery low voltage detection feature
rtc: pcf2127: support battery low voltage detection function

 drivers/rtc/rtc-pcf2127.c | 94 ++++++++++++++++++++++++++++++---------
 include/uapi/linux/rtc.h  |  7 ++-
 2 files changed, 80 insertions(+), 21 deletions(-)

base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f --
2.47.3

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |