diff mbox series

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

Message ID 20241111154144.163604-3-p.rosenberger@kunbus.com
State Rejected
Headers show
Series rtc: pcf2127: make battery switch-over configurable | expand

Commit Message

Philipp Rosenberger Nov. 11, 2024, 3:41 p.m. UTC
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(-)

Comments

Christophe JAILLET Nov. 11, 2024, 6:27 p.m. UTC | #1
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
Rob Herring (Arm) Nov. 12, 2024, 4:38 p.m. UTC | #2
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(-)
diff mbox series

Patch

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.