From: Hugo Villeneuve <hugo@hugovil.com>
To: a.zummo@towertech.it, alexandre.belloni@bootlin.com,
robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org,
conor+dt@kernel.org
Cc: linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, hugo@hugovil.com,
bruno.thomsen@gmail.com,
Hugo Villeneuve <hvilleneuve@dimonoff.com>
Subject: [PATCH 2/2] rtc: pcf2127: add support for battery-related DT properties
Date: Wed, 2 Aug 2023 15:11:53 -0400 [thread overview]
Message-ID: <20230802191153.952667-3-hugo@hugovil.com> (raw)
In-Reply-To: <20230802191153.952667-1-hugo@hugovil.com>
From: Hugo Villeneuve <hvilleneuve@dimonoff.com>
Add support for "battery-switch-over-enable" DT property which can be
used to enable/disable the battery switch over function.
Also add support for "battery-low-detect-enable" DT property which can
be used to enable/disable the battery low detection function.
If any of these properties is not defined, then no alteration to the
PWRMNG field will occur.
These properties can be used to change the default power-on values
(PWRMNG) for battery-related functions. It is especially useful for
the PCF2131 where the default PWRMNG power-on values disable by
default the battery-related functions (contrary to the PCF2127).
Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
---
drivers/rtc/rtc-pcf2127.c | 59 +++++++++++++++++++++++++++++++++++++++
1 file changed, 59 insertions(+)
diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
index 78141bb06ab0..3bb3ad95c67e 100644
--- a/drivers/rtc/rtc-pcf2127.c
+++ b/drivers/rtc/rtc-pcf2127.c
@@ -20,6 +20,7 @@
#include <linux/i2c.h>
#include <linux/spi/spi.h>
#include <linux/bcd.h>
+#include <linux/bitfield.h>
#include <linux/rtc.h>
#include <linux/slab.h>
#include <linux/module.h>
@@ -48,6 +49,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_PWRMNG_MASK GENMASK(7, 5)
/* Time and date registers */
#define PCF2127_REG_TIME_BASE 0x03
#define PCF2127_BIT_SC_OSF BIT(7)
@@ -1080,6 +1082,57 @@ static int pcf2127_enable_ts(struct device *dev, int ts_id)
return ret;
}
+/*
+ * By default, do not reconfigure or set default power management mode,
+ * unless explicitly requested via DT properties:
+ * battery-switch-over
+ * battery-low-detect
+ */
+static int pcf2127_configure_power_management(struct device *dev)
+{
+ struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
+ int ret;
+ u8 pwrmng;
+ u32 bat_sw_over, bat_low_detect;
+
+ /*
+ * The PWRMNG field is defined in a peculiar way for PCF21XX
+ * devices: there is no individual bit defined for the
+ * battery-switch-over or battery-low-detect functions.
+ * Therefore, we require that both properties must be defined
+ * to alter the PWRMNG field.
+ */
+ if (device_property_read_u32(dev, "battery-switch-over", &bat_sw_over))
+ return 0;
+
+ if (device_property_read_u32(dev, "battery-low-detect",
+ &bat_low_detect))
+ return 0;
+
+ if (!bat_sw_over) {
+ /*
+ * If battery-switch-over is disabled, then the
+ * battery-low-detect function is always disabled.
+ */
+ pwrmng = BIT(2) | BIT(1) | BIT(0);
+ } else {
+ if (bat_low_detect)
+ pwrmng = 0;
+ else
+ pwrmng = BIT(0);
+ }
+
+ ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3,
+ PCF2127_CTRL3_PWRMNG_MASK,
+ FIELD_PREP(PCF2127_CTRL3_PWRMNG_MASK, pwrmng));
+ if (ret < 0) {
+ dev_dbg(dev, "PWRMNG config failed\n");
+ return ret;
+ }
+
+ return 0;
+}
+
/* Route all interrupt sources to INT A pin. */
static int pcf2127_configure_interrupt_pins(struct device *dev)
{
@@ -1163,6 +1216,12 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap,
pcf2127->irq_enabled = true;
}
+ ret = pcf2127_configure_power_management(dev);
+ if (ret) {
+ dev_err(dev, "failed to configure power management\n");
+ return ret;
+ }
+
if (alarm_irq > 0 || device_property_read_bool(dev, "wakeup-source")) {
device_init_wakeup(dev, true);
set_bit(RTC_FEATURE_ALARM, pcf2127->rtc->features);
--
2.30.2
prev parent reply other threads:[~2023-08-02 19:12 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-02 19:11 [PATCH 0/2] rtc: pcf2127: add default battery-related power-on values Hugo Villeneuve
2023-08-02 19:11 ` [PATCH 1/2] dt-bindings: rtc: add properties to set battery-related functions Hugo Villeneuve
2023-08-08 11:21 ` Conor Dooley
2023-08-08 12:25 ` Hugo Villeneuve
2023-08-08 12:32 ` Alexandre Belloni
2023-08-08 12:44 ` Hugo Villeneuve
2023-09-05 15:30 ` Hugo Villeneuve
2023-09-19 15:32 ` Hugo Villeneuve
2023-09-19 15:34 ` Hugo Villeneuve
2023-10-11 13:49 ` Hugo Villeneuve
2023-10-11 22:23 ` Hugo Villeneuve
2023-11-01 14:19 ` Hugo Villeneuve
2023-11-01 14:21 ` Hugo Villeneuve
2023-08-02 19:11 ` 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=20230802191153.952667-3-hugo@hugovil.com \
--to=hugo@hugovil.com \
--cc=a.zummo@towertech.it \
--cc=alexandre.belloni@bootlin.com \
--cc=bruno.thomsen@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=hvilleneuve@dimonoff.com \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=robh+dt@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).