public inbox for linux-rtc@vger.kernel.org
 help / color / mirror / Atom feed
From: Hugo Villeneuve <hugo@hugovil.com>
To: alexandre.belloni@bootlin.com
Cc: linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org,
	hugo@hugovil.com, bruno.thomsen@gmail.com,
	giampiero@sferalabs.cc, p.rosenberger@kunbus.com,
	antonio@amsobr.com, Hugo Villeneuve <hvilleneuve@dimonoff.com>
Subject: [PATCH 4/4] rtc: pcf2127: support battery low voltage detection function
Date: Wed, 11 Mar 2026 16:02:30 -0400	[thread overview]
Message-ID: <20260311200237.3531981-5-hugo@hugovil.com> (raw)
In-Reply-To: <20260311200237.3531981-1-hugo@hugovil.com>

From: Hugo Villeneuve <hvilleneuve@dimonoff.com>

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.

Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
---
 drivers/rtc/rtc-pcf2127.c | 60 ++++++++++++++++++++++++++++++++-------
 1 file changed, 50 insertions(+), 10 deletions(-)

diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
index 0605295026564..05b08867ffcb3 100644
--- a/drivers/rtc/rtc-pcf2127.c
+++ b/drivers/rtc/rtc-pcf2127.c
@@ -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,
-- 
2.47.3


      parent reply	other threads:[~2026-03-11 20:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-11 20:02 [PATCH 0/4] rtc: pcf2127: add support for battery low voltage detection Hugo Villeneuve
2026-03-11 20:02 ` [PATCH 1/4] rtc: pcf2127: remove redundant break statement in switch-case Hugo Villeneuve
2026-03-11 20:02 ` [PATCH 2/4] rtc: pcf2127: add pcf2127_pwrmng_get/set Hugo Villeneuve
2026-03-11 20:02 ` [PATCH 3/4] rtc: add battery low voltage detection feature Hugo Villeneuve
2026-03-11 20:02 ` Hugo Villeneuve [this message]

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=20260311200237.3531981-5-hugo@hugovil.com \
    --to=hugo@hugovil.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=antonio@amsobr.com \
    --cc=bruno.thomsen@gmail.com \
    --cc=giampiero@sferalabs.cc \
    --cc=hvilleneuve@dimonoff.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=p.rosenberger@kunbus.com \
    /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