public inbox for devicetree@vger.kernel.org
 help / color / mirror / Atom feed
From: Philipp Rosenberger <p.rosenberger@kunbus.com>
To: Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org
Cc: "Lino Sanfilippo" <l.sanfilippo@kunbus.com>,
	"Thomas Böhler" <t.boehler@kunbus.com>,
	"Hugo Villeneuve" <hvilleneuve@dimonoff.com>,
	"Philipp Rosenberger" <p.rosenberger@kunbus.com>
Subject: [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
Date: Mon, 11 Nov 2024 16:41:44 +0100	[thread overview]
Message-ID: <20241111154144.163604-3-p.rosenberger@kunbus.com> (raw)
In-Reply-To: <20241111154144.163604-1-p.rosenberger@kunbus.com>

The PCF2127, PCF2129, and PCA2129 RTCs have the battery switch-over function
enabled by default. However, the newer PCF2131 RTC has the opposite default
behavior, requiring explicit enablement for battery backup.

Add support for the `nxp,battery-backed` device tree property to enable battery
switch-over in standard mode for the rtc-pcf2127 driver. If this property is set
and no battery switch-over mode is already configured, the driver will enable
standard mode; otherwise, existing configurations remain unchanged.

Signed-off-by: Philipp Rosenberger <p.rosenberger@kunbus.com>
---
 drivers/rtc/rtc-pcf2127.c | 76 +++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 15 deletions(-)

diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
index 9c04c4e1a49c..c80e31fec134 100644
--- a/drivers/rtc/rtc-pcf2127.c
+++ b/drivers/rtc/rtc-pcf2127.c
@@ -48,6 +48,7 @@
 #define PCF2127_BIT_CTRL3_BLF			BIT(2)
 #define PCF2127_BIT_CTRL3_BF			BIT(3)
 #define PCF2127_BIT_CTRL3_BTSE			BIT(4)
+#define PCF2127_BIT_CTRL3_PWRMNG_MASK		(BIT(5) | BIT(6) | BIT(7))
 /* Time and date registers */
 #define PCF2127_REG_TIME_BASE		0x03
 #define PCF2127_BIT_SC_OSF			BIT(7)
@@ -529,6 +530,64 @@ static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
 	return devm_watchdog_register_device(dev, &pcf2127->wdd);
 }
 
+static int pcf2127_battery_init(struct device *dev, struct pcf2127 *pcf2127)
+{
+	unsigned int ctrl3;
+	unsigned int pwrmng;
+	int ret;
+
+	/*
+	 * Disable battery low/switch-over timestamp and interrupts.
+	 * Clear battery interrupt flags which can block new trigger events.
+	 * Note: This is the default chip behaviour but added to ensure
+	 * correct tamper timestamp and interrupt function.
+	 */
+	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+				 PCF2127_BIT_CTRL3_BTSE |
+				 PCF2127_BIT_CTRL3_BIE |
+				 PCF2127_BIT_CTRL3_BLIE, 0);
+	if (ret) {
+		dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
+			__func__);
+		return ret;
+	}
+
+	if (!device_property_read_bool(dev, "nxp,battery-backed"))
+		return 0;
+
+	ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &ctrl3);
+	if (ret) {
+		dev_err(dev, "%s: read ctrl3 faild\n", __func__);
+		return ret;
+	}
+
+	/*
+	 * Don't touch the PWRNMNG bits if any kind of battery switch-over is
+	 * enabled. The PWRMNG values 0-2 use the battery switch-over standard
+	 * mode, while values 3-5 use direct switching mode.
+	 * Only values 6 and 7 have the battery switch-over function disabled.
+	 */
+	pwrmng = (PCF2127_BIT_CTRL3_PWRMNG_MASK & ctrl3) >> 5;
+	if (pwrmng < 6)
+		return 0;
+
+	/*
+	 * Enable battery switch-over function in standard mode.
+	 * Enable battery low detection function.
+	 * Enable extra power fail detection function (PCF2127 only).
+	 */
+	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+				 PCF2127_BIT_CTRL3_PWRMNG_MASK, 0);
+	if (ret) {
+		dev_err(dev,
+			"%s: battery switch-over config (ctrl3) failed\n",
+			__func__);
+		return ret;
+	}
+
+	return 0;
+}
+
 /* Alarm */
 static int pcf2127_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
@@ -1224,22 +1283,9 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap,
 	}
 
 	pcf2127_watchdog_init(dev, pcf2127);
-
-	/*
-	 * Disable battery low/switch-over timestamp and interrupts.
-	 * Clear battery interrupt flags which can block new trigger events.
-	 * Note: This is the default chip behaviour but added to ensure
-	 * correct tamper timestamp and interrupt function.
-	 */
-	ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
-				 PCF2127_BIT_CTRL3_BTSE |
-				 PCF2127_BIT_CTRL3_BIE |
-				 PCF2127_BIT_CTRL3_BLIE, 0);
-	if (ret) {
-		dev_err(dev, "%s: interrupt config (ctrl3) failed\n",
-			__func__);
+	ret = pcf2127_battery_init(dev, pcf2127);
+	if (ret < 0)
 		return ret;
-	}
 
 	/*
 	 * Enable timestamp functions 1 to 4.
-- 
2.39.5


  parent reply	other threads:[~2024-11-11 15:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-11 15:41 [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable Philipp Rosenberger
2024-11-11 15:41 ` [PATCH v3 1/2] dt-bindings: rtc: pcf2127: Add nxp,battery-backed flag Philipp Rosenberger
2024-11-12 16:40   ` Rob Herring
2024-11-11 15:41 ` Philipp Rosenberger [this message]
2024-11-11 18:27   ` [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over Christophe JAILLET
2024-11-12 16:38   ` Rob Herring
2024-11-14  8:51 ` [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable Alexandre Belloni
2024-11-27 12:58   ` Philipp Rosenberger

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=20241111154144.163604-3-p.rosenberger@kunbus.com \
    --to=p.rosenberger@kunbus.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=hvilleneuve@dimonoff.com \
    --cc=krzk+dt@kernel.org \
    --cc=l.sanfilippo@kunbus.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=t.boehler@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