* [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable
@ 2024-11-11 15:41 Philipp Rosenberger
2024-11-11 15:41 ` [PATCH v3 1/2] dt-bindings: rtc: pcf2127: Add nxp,battery-backed flag Philipp Rosenberger
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Philipp Rosenberger @ 2024-11-11 15:41 UTC (permalink / raw)
To: Alexandre Belloni, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
linux-rtc, linux-kernel, devicetree
Cc: Lino Sanfilippo, Thomas Böhler, Hugo Villeneuve,
Philipp Rosenberger
Hello,
This patch series adds the nxp,battery-backed device tree property to the
PCF2127 RTC driver. This flag indicates if the RTC is battery-backed, allowing
the driver to enable the battery switchover function if necessary.
Background
----------
The PCF2127 RTC driver currently supports the PCF2127, PCA2129, PCF2129, and
PCF2131 devices. Among these, only the newer PCF2131 has a different default
behavior for battery switchover: it is disabled by default, whereas it is
enabled on the other devices. If there is no firmware or bootloader setting
to enable battery switchover on the PCF2131, it will not automatically switch
to battery power, even if a battery is attached.
An alternative approach would be to enable battery switchover by default on
the PCF2131 without requiring a device tree property. However, this could be
undesirable, as it would make it impossible to disable battery
switchover.
Thank you.
Link [V2] https://patchwork.ozlabs.org/project/rtc-linux/list/?series=429144
Changes for V3:
- The nxp,battery-switch-over property was replaced with a boolean
nxp,battery-backed flag
- The register vales are not encoded in the device tree any more
- The driver enables battery switch-over in standard mode if the dt flag
is set.
Changes for V2:
- v1 was corrupted due to our company mail server settings. No functional
changes have been made; this version is identical to v1.
Philipp Rosenberger (2):
dt-bindings: rtc: pcf2127: Add nxp,battery-backed flag
rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable
battery switch-over
.../devicetree/bindings/rtc/nxp,pcf2127.yaml | 7 ++
drivers/rtc/rtc-pcf2127.c | 76 +++++++++++++++----
2 files changed, 68 insertions(+), 15 deletions(-)
--
2.39.5
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 1/2] dt-bindings: rtc: pcf2127: Add nxp,battery-backed flag
2024-11-11 15:41 [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable Philipp Rosenberger
@ 2024-11-11 15:41 ` Philipp Rosenberger
2024-11-12 16:40 ` Rob Herring
2024-11-11 15:41 ` [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over Philipp Rosenberger
2024-11-14 8:51 ` [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable Alexandre Belloni
2 siblings, 1 reply; 8+ messages in thread
From: Philipp Rosenberger @ 2024-11-11 15:41 UTC (permalink / raw)
To: Alexandre Belloni, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
linux-rtc, linux-kernel, devicetree
Cc: Lino Sanfilippo, Thomas Böhler, Hugo Villeneuve,
Philipp Rosenberger
This commit introduces the nxp,battery-backed property to the
nxp,pcf2127 Device Tree bindings. This flag indicates that the
RTC is battery-backed and forces the driver to enable the
battery switch-over function, but only if no other mode is already
configured.
With the PCF2131, the battery switch-over is disabled by default.
If the battery switch-over is not enabled by the bootloader or
firmware, this property ensures that the RTC can function correctly
when powered by the battery.
Signed-off-by: Philipp Rosenberger <p.rosenberger@kunbus.com>
---
Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml b/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml
index 2d9fe5a75b06..87ba16346fb4 100644
--- a/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml
+++ b/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml
@@ -30,6 +30,13 @@ properties:
reset-source: true
+ nxp,battery-backed:
+ description: |
+ Indicates that the RTC is battery-backed. This property forces
+ the driver to enable the battery switch-over function, but only if
+ no other mode is already configured.
+ $ref: /schemas/types.yaml#/definitions/flag
+
required:
- compatible
- reg
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
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-11 15:41 ` Philipp Rosenberger
2024-11-11 18:27 ` 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
2 siblings, 2 replies; 8+ messages in thread
From: Philipp Rosenberger @ 2024-11-11 15:41 UTC (permalink / raw)
To: Alexandre Belloni, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
linux-rtc, linux-kernel, devicetree
Cc: Lino Sanfilippo, Thomas Böhler, Hugo Villeneuve,
Philipp Rosenberger
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
2024-11-11 15:41 ` [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over Philipp Rosenberger
@ 2024-11-11 18:27 ` Christophe JAILLET
2024-11-12 16:38 ` Rob Herring
1 sibling, 0 replies; 8+ messages in thread
From: Christophe JAILLET @ 2024-11-11 18:27 UTC (permalink / raw)
To: Philipp Rosenberger
Cc: Lino Sanfilippo, Thomas Böhler, Hugo Villeneuve,
Philipp Rosenberger, Alexandre Belloni, Rob Herring,
Krzysztof Kozlowski, Conor Dooley, linux-rtc, linux-kernel,
devicetree
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over
2024-11-11 15:41 ` [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over Philipp Rosenberger
2024-11-11 18:27 ` Christophe JAILLET
@ 2024-11-12 16:38 ` Rob Herring
1 sibling, 0 replies; 8+ messages in thread
From: Rob Herring @ 2024-11-12 16:38 UTC (permalink / raw)
To: Philipp Rosenberger
Cc: Alexandre Belloni, Krzysztof Kozlowski, Conor Dooley, linux-rtc,
linux-kernel, devicetree, Lino Sanfilippo, Thomas Böhler,
Hugo Villeneuve
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(-)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 1/2] dt-bindings: rtc: pcf2127: Add nxp,battery-backed flag
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
0 siblings, 0 replies; 8+ messages in thread
From: Rob Herring @ 2024-11-12 16:40 UTC (permalink / raw)
To: Philipp Rosenberger
Cc: Alexandre Belloni, Krzysztof Kozlowski, Conor Dooley, linux-rtc,
linux-kernel, devicetree, Lino Sanfilippo, Thomas Böhler,
Hugo Villeneuve
On Mon, Nov 11, 2024 at 04:41:43PM +0100, Philipp Rosenberger wrote:
> This commit introduces the nxp,battery-backed property to the
> nxp,pcf2127 Device Tree bindings. This flag indicates that the
> RTC is battery-backed and forces the driver to enable the
> battery switch-over function, but only if no other mode is already
> configured.
>
> With the PCF2131, the battery switch-over is disabled by default.
> If the battery switch-over is not enabled by the bootloader or
> firmware, this property ensures that the RTC can function correctly
> when powered by the battery.
>
> Signed-off-by: Philipp Rosenberger <p.rosenberger@kunbus.com>
> ---
> Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml b/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml
> index 2d9fe5a75b06..87ba16346fb4 100644
> --- a/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml
> +++ b/Documentation/devicetree/bindings/rtc/nxp,pcf2127.yaml
> @@ -30,6 +30,13 @@ properties:
>
> reset-source: true
>
> + nxp,battery-backed:
> + description: |
> + Indicates that the RTC is battery-backed. This property forces
> + the driver to enable the battery switch-over function, but only if
> + no other mode is already configured.
> + $ref: /schemas/types.yaml#/definitions/flag
Seems like something that would be a common property? I didn't find any
prior art though.
Rob
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable
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-11 15:41 ` [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over Philipp Rosenberger
@ 2024-11-14 8:51 ` Alexandre Belloni
2024-11-27 12:58 ` Philipp Rosenberger
2 siblings, 1 reply; 8+ messages in thread
From: Alexandre Belloni @ 2024-11-14 8:51 UTC (permalink / raw)
To: Philipp Rosenberger
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, linux-rtc,
linux-kernel, devicetree, Lino Sanfilippo, Thomas Böhler,
Hugo Villeneuve
Hello,
On 11/11/2024 16:41:42+0100, Philipp Rosenberger wrote:
> Hello,
>
> This patch series adds the nxp,battery-backed device tree property to the
> PCF2127 RTC driver. This flag indicates if the RTC is battery-backed, allowing
> the driver to enable the battery switchover function if necessary.
>
> Background
> ----------
> The PCF2127 RTC driver currently supports the PCF2127, PCA2129, PCF2129, and
> PCF2131 devices. Among these, only the newer PCF2131 has a different default
> behavior for battery switchover: it is disabled by default, whereas it is
> enabled on the other devices. If there is no firmware or bootloader setting
> to enable battery switchover on the PCF2131, it will not automatically switch
> to battery power, even if a battery is attached.
>
> An alternative approach would be to enable battery switchover by default on
> the PCF2131 without requiring a device tree property. However, this could be
> undesirable, as it would make it impossible to disable battery
> switchover.
>
This has been discussed multiple times in the past, we can't have a DT
property for this as we need to be able to change it at runtime. There
is already a userspace interface to do this.
Below is my current patch for this that has been tested on pcf2127. I
didn't send it yet because we are losing information when switching from
standard or direct mode to disabled because when BSM is disabled, there
is no configuration where battery low detection function is enabled so
going from disabled to standard or direct will keep BLD disabled.
8<--------------------------------------------------------------------
From 7db70b33c3939a0ebe147c32f406b34a2f5f1be8 Mon Sep 17 00:00:00 2001
From: Alexandre Belloni <alexandre.belloni@bootlin.com>
Date: Sat, 24 Feb 2024 19:58:20 +0100
Subject: [PATCH] rtc: pcf2127: add BSM support
The pcf2127 encodes BSM, BLD and power fail detection in the same set of
bits so it is necessary to do some calculation when changing BSM to keep
the rest of the configuration as-is. However, when BSM is disabled, there
is no configuration with BLD enabled so this will be lost when coming back
to a mode with BSM enabled.
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
---
drivers/rtc/rtc-pcf2127.c | 81 +++++++++++++++++++++++++++++++++++++++
1 file changed, 81 insertions(+)
diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
index 9c04c4e1a49c..a7f73192d53d 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_CTRL3_PM GENMASK(7, 5)
/* Time and date registers */
#define PCF2127_REG_TIME_BASE 0x03
#define PCF2127_BIT_SC_OSF BIT(7)
@@ -331,6 +332,84 @@ static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
return 0;
}
+static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
+{
+ struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+ u32 value;
+ int ret;
+
+ switch (param->param) {
+ case RTC_PARAM_BACKUP_SWITCH_MODE:
+ ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &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)
+ param->uvalue = RTC_BSM_DIRECT;
+ else
+ param->uvalue = RTC_BSM_DISABLED;
+
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
+{
+ struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+ u8 mode = 0;
+ u32 value;
+ int ret;
+
+ switch (param->param) {
+ case RTC_PARAM_BACKUP_SWITCH_MODE:
+ ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
+ if (ret < 0)
+ return ret;
+
+ value = FIELD_GET(PCF2127_CTRL3_PM, value);
+
+ if (value > 5)
+ value -= 5;
+ else if (value > 2)
+ value -= 3;
+
+ switch (param->uvalue) {
+ case RTC_BSM_LEVEL:
+ break;
+ case RTC_BSM_DIRECT:
+ mode = 3;
+ break;
+ case RTC_BSM_DISABLED:
+ if (value == 0)
+ value = 1;
+ mode = 5;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+ PCF2127_CTRL3_PM,
+ FIELD_PREP(PCF2127_CTRL3_PM, mode + value));
+
+ break;
+
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
static int pcf2127_rtc_ioctl(struct device *dev,
unsigned int cmd, unsigned long arg)
{
@@ -741,6 +820,8 @@ static const struct rtc_class_ops pcf2127_rtc_ops = {
.read_alarm = pcf2127_rtc_read_alarm,
.set_alarm = pcf2127_rtc_set_alarm,
.alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
+ .param_get = pcf2127_param_get,
+ .param_set = pcf2127_param_set,
};
/* sysfs interface */
--
2.47.0
--
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/2] rtc: pcf2127: make battery switch-over configurable
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
0 siblings, 0 replies; 8+ messages in thread
From: Philipp Rosenberger @ 2024-11-27 12:58 UTC (permalink / raw)
To: Alexandre Belloni
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, linux-rtc,
linux-kernel, devicetree, Lino Sanfilippo, Thomas Böhler,
Hugo Villeneuve
Hi,
On 14.11.24 09:51, Alexandre Belloni wrote:
> Hello,
>
> This has been discussed multiple times in the past, we can't have a DT
> property for this as we need to be able to change it at runtime. There
> is already a userspace interface to do this.
>
> Below is my current patch for this that has been tested on pcf2127. I
> didn't send it yet because we are losing information when switching from
> standard or direct mode to disabled because when BSM is disabled, there
> is no configuration where battery low detection function is enabled so
> going from disabled to standard or direct will keep BLD disabled.
Sorry for my late response. I've tested the patch on our hardware. and
this would solve our problem. I understand, that it is not ideal, that
information is lost when switching modes. But I can't figure a way how
to avoid this.
Thank you and best regards,
Philipp
>
> 8<--------------------------------------------------------------------
>
> From 7db70b33c3939a0ebe147c32f406b34a2f5f1be8 Mon Sep 17 00:00:00 2001
> From: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Date: Sat, 24 Feb 2024 19:58:20 +0100
> Subject: [PATCH] rtc: pcf2127: add BSM support
>
> The pcf2127 encodes BSM, BLD and power fail detection in the same set of
> bits so it is necessary to do some calculation when changing BSM to keep
> the rest of the configuration as-is. However, when BSM is disabled, there
> is no configuration with BLD enabled so this will be lost when coming back
> to a mode with BSM enabled.
>
> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
> ---
> drivers/rtc/rtc-pcf2127.c | 81 +++++++++++++++++++++++++++++++++++++++
> 1 file changed, 81 insertions(+)
>
> diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
> index 9c04c4e1a49c..a7f73192d53d 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_CTRL3_PM GENMASK(7, 5)
> /* Time and date registers */
> #define PCF2127_REG_TIME_BASE 0x03
> #define PCF2127_BIT_SC_OSF BIT(7)
> @@ -331,6 +332,84 @@ static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
> return 0;
> }
>
> +static int pcf2127_param_get(struct device *dev, struct rtc_param *param)
> +{
> + struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
> + u32 value;
> + int ret;
> +
> + switch (param->param) {
> + case RTC_PARAM_BACKUP_SWITCH_MODE:
> + ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &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)
> + param->uvalue = RTC_BSM_DIRECT;
> + else
> + param->uvalue = RTC_BSM_DISABLED;
> +
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static int pcf2127_param_set(struct device *dev, struct rtc_param *param)
> +{
> + struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
> + u8 mode = 0;
> + u32 value;
> + int ret;
> +
> + switch (param->param) {
> + case RTC_PARAM_BACKUP_SWITCH_MODE:
> + ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &value);
> + if (ret < 0)
> + return ret;
> +
> + value = FIELD_GET(PCF2127_CTRL3_PM, value);
> +
> + if (value > 5)
> + value -= 5;
> + else if (value > 2)
> + value -= 3;
> +
> + switch (param->uvalue) {
> + case RTC_BSM_LEVEL:
> + break;
> + case RTC_BSM_DIRECT:
> + mode = 3;
> + break;
> + case RTC_BSM_DISABLED:
> + if (value == 0)
> + value = 1;
> + mode = 5;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + return regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
> + PCF2127_CTRL3_PM,
> + FIELD_PREP(PCF2127_CTRL3_PM, mode + value));
> +
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> static int pcf2127_rtc_ioctl(struct device *dev,
> unsigned int cmd, unsigned long arg)
> {
> @@ -741,6 +820,8 @@ static const struct rtc_class_ops pcf2127_rtc_ops = {
> .read_alarm = pcf2127_rtc_read_alarm,
> .set_alarm = pcf2127_rtc_set_alarm,
> .alarm_irq_enable = pcf2127_rtc_alarm_irq_enable,
> + .param_get = pcf2127_param_get,
> + .param_set = pcf2127_param_set,
> };
>
> /* sysfs interface */
> --
> 2.47.0
>
>
>
> --
> Alexandre Belloni, co-owner and COO, Bootlin
> Embedded Linux and Kernel engineering
> https://bootlin.com
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-11-27 12:58 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v3 2/2] rtc: pcf2127: Add 'nxp,battery-switchover' DT property to enable battery switch-over Philipp Rosenberger
2024-11-11 18:27 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).