public inbox for linux-rtc@vger.kernel.org
 help / color / mirror / Atom feed
From: Markus Burri <markus.burri@mt.com>
To: linux-kernel@vger.kernel.org
Cc: Markus Burri <markus.burri@mt.com>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Manuel Traut <manuel.traut@mt.com>, Marek Vasut <marex@denx.de>,
	linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
	Markus Burri <markus.burri@bbv.ch>
Subject: [PATCH v4 7/7] rtc-rv8803: make tamper function configurable via dt
Date: Wed, 21 May 2025 11:05:52 +0200	[thread overview]
Message-ID: <20250521090552.3173-8-markus.burri@mt.com> (raw)
In-Reply-To: <20250521090552.3173-1-markus.burri@mt.com>

The settings are depending on hardware connected to the input pin.
Make this settings configurable in the device-tree:
  - for the input pins: input resistor, trigger edge, de-jitter filter
  - for the buffer: overwrite or inhibit mode for the FIFO

Signed-off-by: Markus Burri <markus.burri@mt.com>
---
 drivers/rtc/rtc-rv8803.c | 95 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 92 insertions(+), 3 deletions(-)

diff --git a/drivers/rtc/rtc-rv8803.c b/drivers/rtc/rtc-rv8803.c
index 39d5881ba261..e3a4e9eaccb1 100644
--- a/drivers/rtc/rtc-rv8803.c
+++ b/drivers/rtc/rtc-rv8803.c
@@ -114,6 +114,11 @@ enum evin_trigger {
 	both_edges = 0b10,
 };
 
+enum evin_buffer_mode {
+	inhibit = 0,
+	overwrite = 1,
+};
+
 struct cfg_val_txt {
 	char *txt;
 	u8 val;
@@ -164,6 +169,16 @@ static const u8 evin_flt_regs[] = {
 	RX8901_EVIN3_FLT
 };
 
+struct tamper_cfg {
+	struct {
+		u8 enable;
+		u8 pull_resistor;
+		u8 trigger;
+		u8 filter;
+	} evin[NO_OF_EVIN];
+	u8 buffer_mode;
+};
+
 struct rv8803_data {
 	struct i2c_client *client;
 	struct rtc_device *rtc;
@@ -171,6 +186,7 @@ struct rv8803_data {
 	u8 ctrl;
 	u8 backup;
 	u8 alarm_invalid:1;
+	struct tamper_cfg tamper_cfg;
 	enum rv8803_type type;
 };
 
@@ -769,7 +785,10 @@ static int rv8803_ts_enable(struct rv8803_data *rv8803, u8 enable)
 
 	/* 3. set EVENTx pull-up edge trigger and noise filter */
 	for (i = 0; i < NO_OF_EVIN; ++i) {
-		ret = rv8803_ts_event_write_evin(i, rv8803, pull_up_1M, falling_edge, 0);
+		ret = rv8803_ts_event_write_evin(i, rv8803,
+						 rv8803->tamper_cfg.evin[i].pull_resistor,
+						 rv8803->tamper_cfg.evin[i].trigger,
+						 rv8803->tamper_cfg.evin[i].filter);
 		if (ret < 0)
 			return ret;
 	}
@@ -787,10 +806,11 @@ static int rv8803_ts_enable(struct rv8803_data *rv8803, u8 enable)
 	}
 
 	/*
-	 * 5. set BUF1 inhibit and interrupt every 1 event
+	 * 5. set BUF1 inhibit/overwrite mode and interrupt every 1 event
 	 *    NOTE: BUF2-3 are not used in FIFO-mode
 	 */
-	ret = rv8803_write_reg(client, RX8901_BUF1_CFG1, 0x01);
+	reg_mask = 0x01 | FIELD_PREP(BIT(6), rv8803->tamper_cfg.buffer_mode);
+	ret = rv8803_write_reg(client, RX8901_BUF1_CFG1, reg_mask);
 	if (ret < 0)
 		return ret;
 
@@ -1125,6 +1145,71 @@ static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
 					 flags);
 }
 
+static int rx8900_tamper_init(struct rv8803_data *rv8803)
+{
+	int i;
+	int err;
+	u8 flags;
+	struct device_node *of_tamper;
+	struct i2c_client *client = rv8803->client;
+	struct tamper_cfg *tamper_cfg = &rv8803->tamper_cfg;
+
+	rv8803->tamper_cfg.buffer_mode = inhibit;
+	for (i = 0; i < NO_OF_EVIN; ++i) {
+		tamper_cfg->evin[i].enable = true;
+		tamper_cfg->evin[i].pull_resistor = pull_up_1M;
+		tamper_cfg->evin[i].trigger = falling_edge;
+		tamper_cfg->evin[i].filter = 0;
+	}
+
+	of_tamper = of_get_child_by_name(client->dev.of_node, "tamper");
+	if (of_tamper) {
+		if (of_property_read_bool(of_tamper, "buffer-overwrite"))
+			tamper_cfg->buffer_mode = overwrite;
+
+		for (i = 0; i < NO_OF_EVIN; ++i) {
+			char of_evin_name[10];
+			u32 evin_val[3];
+
+			snprintf(of_evin_name, sizeof(of_evin_name), "evin-%d", i + 1);
+			if (!of_property_read_u32_array(of_tamper, of_evin_name, evin_val,
+							ARRAY_SIZE(evin_val))) {
+				tamper_cfg->evin[i].enable = true;
+				tamper_cfg->evin[i].pull_resistor = evin_val[0];
+				tamper_cfg->evin[i].trigger = evin_val[1];
+				tamper_cfg->evin[i].filter = evin_val[2];
+			} else {
+				tamper_cfg->evin[i].enable = false;
+			}
+		}
+		of_node_put(of_tamper);
+	}
+
+	scoped_guard(mutex, &rv8803->flags_lock) {
+		err = rv8803_read_reg(client, RX8901_BUF1_CFG1);
+		if (err < 0)
+			return err;
+		flags = (err & ~BIT(6)) | FIELD_PREP(BIT(6), tamper_cfg->buffer_mode);
+		err = rv8803_write_reg(client, RX8901_BUF1_CFG1, flags);
+		if (err < 0)
+			return err;
+	}
+
+	for (i = 0; i < NO_OF_EVIN; ++i) {
+		err = rv8803_ts_event_write_evin(i, rv8803,
+						 tamper_cfg->evin[i].pull_resistor,
+						 tamper_cfg->evin[i].trigger,
+						 tamper_cfg->evin[i].filter);
+		if (err)
+			return err;
+	}
+
+	if (of_tamper)
+		rv8803_ts_enable(rv8803, true);
+
+	return 0;
+}
+
 /* configure registers with values different than the Power-On reset defaults */
 static int rv8803_regs_configure(struct rv8803_data *rv8803)
 {
@@ -1266,6 +1351,10 @@ static int rv8803_probe(struct i2c_client *client)
 		return err;
 
 	if (rv8803->type == rx_8901) {
+		err = rx8900_tamper_init(rv8803);
+		if (err)
+			return err;
+
 		err = rtc_add_group(rv8803->rtc, &rv8803_rtc_sysfs_event_files);
 		if (err)
 			return err;
-- 
2.39.5


      parent reply	other threads:[~2025-05-21  9:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-21  9:05 [PATCH v4 0/7] rtc-rv8803: Implement timestamp trigger over event pins Markus Burri
2025-05-21  9:05 ` [PATCH v4 1/7] dt-bindings: rtc: add new type for epson,rx8901 Markus Burri
2025-05-27  6:43   ` Krzysztof Kozlowski
2025-05-21  9:05 ` [PATCH v4 2/7] rtc-rv8803: add new type for rv8901 Markus Burri
2025-05-21  9:05 ` [PATCH v4 3/7] rtc-rv8803: add tamper function to sysfs " Markus Burri
2025-05-21  9:05 ` [PATCH v4 4/7] rtc-rv8803: extend sysfs to trigger internal ts-event Markus Burri
2025-05-21  9:05 ` [PATCH v4 5/7] rtc-rv8803: extend sysfs to read status Markus Burri
2025-05-21  9:05 ` [PATCH v4 6/7] dt-bindings: rtc-rv8803: add tamper detection property node Markus Burri
2025-05-27  6:42   ` Krzysztof Kozlowski
2025-05-21  9:05 ` Markus Burri [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=20250521090552.3173-8-markus.burri@mt.com \
    --to=markus.burri@mt.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=manuel.traut@mt.com \
    --cc=marex@denx.de \
    --cc=markus.burri@bbv.ch \
    --cc=robh@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