[PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over

Philipp Rosenberger posted 2 patches 1 week, 5 days ago
[PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
Posted by Philipp Rosenberger 1 week, 5 days ago
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
Re: [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
Posted by Rob Herring 1 week, 4 days ago
On Mon, Nov 11, 2024 at 04:41:44PM +0100, Philipp Rosenberger wrote:
> 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.

Subject is wrong.

> 
> 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(-)
Re: [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
Posted by Christophe JAILLET 1 week, 5 days ago
Le 11/11/2024 à 16:41, Philipp Rosenberger a écrit :
> 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-pnUOlEj4XnTQT0dZR+AlfA@public.gmane.org>
> ---
>   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))

GENMASK(7, 5)?

>   /* 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__);

dev_err_probe() could be used.

> +		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__);

s/faild/failed/

dev_err_probe() could be used.

> +		return ret;
> +	}

...

CJ