Skip to contentSkip to conversation

rtc-rv8803: Implement timestamp trigger over event pins

PATCH In review Markus Burri 3 participants 10 messages May 21, 2025 – May 27, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover rtc-rv8803: Implement timestamp trigger over event pins

The RV8901 RTC chip provides a function to store timestamp events. There are three input pins (EVIN1-3) available for triggering. The timestamp can be read to detect tamper alerts, for example.

This patch series extends the sysfs interface to enable and read the timestamp events.

The data-sheet can be found here:
https://download.epsondevice.com/td/pdf/brief/RX8901CE_en.pdf

Changes in V4:
* fix warning in dt-binding
* reformat
* fix flags reset on read
* auto enable if configured in device-tree
* rebase to linux-6.15~rc1

Changes in V3:
* change permission for sysfs read to admin
* add device-tree bindings for event pin and buffer mode configuration

Changes in V2:
* remove separate patch for register definitions
* remove pin configuration over sysfs (use default settings)
* add sysfs documentation
* rework enable to simplify and remove big mutext scope
---
[V1] https://lore.kernel.org/lkml/[email protected]/
[V2] https://lore.kernel.org/lkml/[email protected]/
[V3] https://lore.kernel.org/lkml/[email protected]/

Markus Burri (7):
dt-bindings: rtc: add new type for epson,rx8901
rtc-rv8803: add new type for rv8901
rtc-rv8803: add tamper function to sysfs for rv8901
rtc-rv8803: extend sysfs to trigger internal ts-event
rtc-rv8803: extend sysfs to read status
dt-bindings: rtc-rv8803: add tamper detection property node
rtc-rv8803: make tamper function configurable via dt

 .../ABI/testing/sysfs-class-rtc-tamper        |  36 +
 .../devicetree/bindings/rtc/epson,rx8900.yaml |  39 +-
 drivers/rtc/rtc-rv8803.c                      | 639 +++++++++++++++++-
 3 files changed, 709 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-rtc-tamper

base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8

Signature · 1 line
2.39.5
#1 lore raw

PATCH v4 1/7 dt-bindings: rtc: add new type for epson,rx8901

Document compatibles for:
- RX8803: already used by the driver
- RX8901: new device supporting also tamper detection and pinctrl

---
 Documentation/devicetree/bindings/rtc/epson,rx8900.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +2 -0
index b770149c5fd6..03af81754482 100644
@@ -15,8 +15,10 @@ allOf:
 properties:
   compatible:
     enum:
+      - epson,rx8803
       - epson,rx8804
       - epson,rx8900
+      - epson,rx8901
       - microcrystal,rv8803
 
   reg:
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v4 1/7] dt-bindings: rtc: add new type for epson,rx8901

Quoted 16 lines · Markus Burri

Document compatibles for:
- RX8803: already used by the driver
- RX8901: new device supporting also tamper detection and pinctrl

---
 Documentation/devicetree/bindings/rtc/epson,rx8900.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +0 -0
index b770149c5fd6..03af81754482 100644

Documentation/devicetree/bindings/rtc/epson,rx8900.yaml allOf: · line 21

Quoted 7 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +2 -0
@@ -15,8 +15,10 @@ allOf:
 properties:
   compatible:
     enum:
+      - epson,rx8803
       - epson,rx8804
       - epson,rx8900
+      - epson,rx8901

This has to be squashed with your other patch. Adding new variant with new features is one commit.

Drop the tag and reviews then.

Best regards,
Krzysztof

#3 lore raw

PATCH v4 2/7 rtc-rv8803: add new type for rv8901

In reply to Markus Burri

The rtc-rv8901 has additional functionality (tamper detection). To support this additional functionality the type must be extended.

---
 drivers/rtc/rtc-rv8803.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/rtc/rtc-rv8803.c +9 -2
index 1327251e527c..50fbae931cb2 100644
@@ -62,7 +62,8 @@ enum rv8803_type {
 	rv_8803,
 	rx_8803,
 	rx_8804,
-	rx_8900
+	rx_8900,
+	rx_8901,
 };
 
 struct rv8803_data {
@@ -173,7 +174,8 @@ static int rv8803_regs_reset(struct rv8803_data *rv8803, bool full)
 	 * The RV-8803 resets all registers to POR defaults after voltage-loss,
 	 * the Epson RTCs don't, so we manually reset the remainder here.
 	 */
-	if (full || rv8803->type == rx_8803 || rv8803->type == rx_8900) {
+	if (full || rv8803->type == rx_8803 || rv8803->type == rx_8900 ||
+	    rv8803->type == rx_8901) {
 		int ret = rv8803_regs_init(rv8803);
 		if (ret)
 			return ret;
@@ -635,6 +637,7 @@ static const struct i2c_device_id rv8803_id[] = {
 	{ "rv8804", rx_8804 },
 	{ "rx8803", rx_8803 },
 	{ "rx8900", rx_8900 },
+	{ "rx8901", rx_8901 },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, rv8803_id);
@@ -760,6 +763,10 @@ static const __maybe_unused struct of_device_id rv8803_of_match[] = {
 		.compatible = "epson,rx8900",
 		.data = (void *)rx_8900
 	},
+	{
+		.compatible = "epson,rx8901",
+		.data = (void *)rx_8901
+	},
 	{ }
 };
 MODULE_DEVICE_TABLE(of, rv8803_of_match);
Signature · 1 line
2.39.5
#4 lore raw

PATCH v4 3/7 rtc-rv8803: add tamper function to sysfs for rv8901

In reply to Markus Burri

Add sysfs interface to enable the EVINx pins and read the time-stamp events from EVINX.

---
 .../ABI/testing/sysfs-class-rtc-tamper        |  21 +
 drivers/rtc/rtc-rv8803.c                      | 391 ++++++++++++++++++
 2 files changed, 412 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-rtc-tamper
Documentation/ABI/testing/sysfs-class-rtc-tamper new file +21 -0
new file mode 100644
index 000000000000..3e61ed628c17
@@ -0,0 +1,21 @@
+What:		/sys/class/rtc/rtcX/tamper/enable
+Date:		May 2025
+KernelVersion:	6.15
+Contact:	Markus Burri <[email protected]>
+Description:	(WO) Attribute to enable and disable the rtc tamper function.
+		Write a '1' to enable tamper detection or a '0' to disable.
+
+What:		/sys/class/rtc/rtcX/tamper/read
+Date:		May 2025
+KernelVersion:	6.15
+Contact:	Markus Burri <[email protected]>
+Description:	(RO) Attribute to read the stored timestamps form buffer FIFO.
+		The timestamps are returned one by one
+		Format is 'seconds.milliseconds' since the epoch followed by the trigger events.
+		The value of the event is the current pin value.
+
+		Example values:
+		- "1234.567 EVIN1=1" for a trigger from EVIN1 changed from low to high
+		- "1234.567 EVIN1=0 EVIN2=1 for a simultaneous trigger of EVIN1 changed to low and
+		  EVIN2 changed to high.
+
drivers/rtc/rtc-rv8803.c +391 -0

427 lines not on this page · raw message

Signature · 1 line
2.39.5
#5 lore raw

PATCH v4 4/7 rtc-rv8803: extend sysfs to trigger internal ts-event

In reply to Markus Burri

Extend sysfs to trigger an internal time-stamp event.

The trigger function can be used from an application to trigger an internal time-stamp event.

---
 .../ABI/testing/sysfs-class-rtc-tamper        |  7 ++++
 drivers/rtc/rtc-rv8803.c                      | 33 +++++++++++++++++++
 2 files changed, 40 insertions(+)
Documentation/ABI/testing/sysfs-class-rtc-tamper +7 -0
index 3e61ed628c17..83b2fae1fc6e 100644
@@ -18,4 +18,11 @@ Description:	(RO) Attribute to read the stored timestamps form buffer FIFO.
 		- "1234.567 EVIN1=1" for a trigger from EVIN1 changed from low to high
 		- "1234.567 EVIN1=0 EVIN2=1 for a simultaneous trigger of EVIN1 changed to low and
 		  EVIN2 changed to high.
+		- "1234.567 CMD=0" for a internal trigger
 
+What:		/sys/class/rtc/rtcX/tamper/trigger
+Date:		May 2025
+KernelVersion:	6.15
+Contact:	Markus Burri <[email protected]>
+Description:	(WO) Attribute to trigger an internal timestamp event
+		Write a '1' to trigger an internal event and store a timestamp.
drivers/rtc/rtc-rv8803.c +33 -0
index e367d37e6a8f..d0aac250774a 100644
@@ -929,12 +929,45 @@ static ssize_t read_show(struct device *dev, struct device_attribute *attr, char
 	return offset;
 }
 
+static ssize_t trigger_store(struct device *dev, struct device_attribute *attr, const char *buf,
+			     size_t count)
+{
+	struct rv8803_data *rv8803 = dev_get_drvdata(dev->parent);
+	struct i2c_client *client = rv8803->client;
+	int ret;
+	unsigned long tmo;
+
+	guard(mutex)(&rv8803->flags_lock);
+
+	/* CMDTRGEN */
+	ret = rv8803_write_reg(client, RX8901_WRCMD_CFG, BIT(0));
+	if (ret < 0)
+		return ret;
+	ret = rv8803_write_reg(client, RX8901_WRCMD_TRG, 0xFF);
+	if (ret < 0)
+		return ret;
+
+	tmo = jiffies + msecs_to_jiffies(100); /* timeout 100ms */
+	do {
+		usleep_range(10, 2000);
+		ret = rv8803_read_reg(client, RX8901_WRCMD_TRG);
+		if (ret < 0)
+			return ret;
+		if (time_after(jiffies, tmo))
+			return -EBUSY;
+	} while (ret);
+
+	return count;
+}
+
 static DEVICE_ATTR_WO(enable);
 static DEVICE_ATTR_ADMIN_RO(read);
+static DEVICE_ATTR_WO(trigger);
 
 static struct attribute *rv8803_rtc_event_attrs[] = {
 	&dev_attr_enable.attr,
 	&dev_attr_read.attr,
+	&dev_attr_trigger.attr,
 	NULL
 };
 
Signature · 1 line
2.39.5
#6 lore raw

PATCH v4 5/7 rtc-rv8803: extend sysfs to read status

In reply to Markus Burri

Add sysfs functionality to read the status and configuration. The functions provide the number of stored timestamp events, the current EVIN pin configuration and the enabled/disabled status.

---
 .../ABI/testing/sysfs-class-rtc-tamper        |   8 ++
 drivers/rtc/rtc-rv8803.c                      | 115 ++++++++++++++++++
 2 files changed, 123 insertions(+)
Documentation/ABI/testing/sysfs-class-rtc-tamper +8 -0
index 83b2fae1fc6e..ef6d5f43abd8 100644
@@ -26,3 +26,11 @@ KernelVersion:	6.15
 Contact:	Markus Burri <[email protected]>
 Description:	(WO) Attribute to trigger an internal timestamp event
 		Write a '1' to trigger an internal event and store a timestamp.
+
+What:		/sys/class/rtc/rtcX/tamper/status
+Date:		May 2025
+KernelVersion:	6.15
+Contact:	Markus Burri <[email protected]>
+Description:	(RO) Attribute to read configuration and status for EVINx and buffer.
+		Provide the number of stored timestamp events, the current EVIN pin configuration
+		and the enabled/disabled status.
drivers/rtc/rtc-rv8803.c +115 -0

149 lines not on this page · raw message

Signature · 1 line
2.39.5
#7 lore raw

PATCH v4 6/7 dt-bindings: rtc-rv8803: add tamper detection property node

In reply to Markus Burri

The RV8901 RTC chip provides a function to store timestamp events. There are three input pins (EVIN1-3) available for triggering. The input pins can be configured and adapted according to the connected hardware.
Add document of tamper detection properties for epson,rx8901 rtc chip.

---
 .../devicetree/bindings/rtc/epson,rx8900.yaml | 37 +++++++++++++++++--
 1 file changed, 34 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +34 -3
index 03af81754482..2682cbb9097d 100644
@@ -9,9 +9,6 @@ title: EPSON RX8900 / Microcrystal RV8803 Real-Time Clock
 maintainers:
   - Marek Vasut <[email protected]>
 
-allOf:
-  - $ref: rtc.yaml#
-
 properties:
   compatible:
     enum:
@@ -33,6 +30,40 @@ properties:
 
   wakeup-source: true
 
+  tamper:
+    description: Subnode for tamper configuration.
+      This subnode is only available for epson,rx8901.
+    type: object
+    additionalProperties: false
+
+    properties:
+      buffer-overwrite:
+        type: boolean
+        description: Set the buffer mode to overwrite. Default is inhibit.
+
+    patternProperties:
+      "^evin-[0-3]$":
+        $ref: /schemas/types.yaml#/definitions/uint32-array
+        minItems: 3
+        maxItems: 3
+        description: External event input pin configuration.
+          The configuration is an array of tree values and contains
+          "pull-resistor", "trigger" and "filter".
+          For a detailed description, see epson-rx8901 datasheet.
+
+allOf:
+  - $ref: rtc.yaml#
+  - if:
+      properties:
+        compatible:
+          not:
+            contains:
+              enum:
+                - epson,rx8901
+    then:
+      properties:
+        tamper: false
+
 required:
   - compatible
   - reg
Signature · 1 line
2.39.5
#8 lore raw

Re: [PATCH v4 6/7] dt-bindings: rtc-rv8803: add tamper detection property node

Quoted 26 lines · Markus Burri

The RV8901 RTC chip provides a function to store timestamp events. There are three input pins (EVIN1-3) available for triggering. The input pins can be configured and adapted according to the connected hardware.
Add document of tamper detection properties for epson,rx8901 rtc chip.

---
 .../devicetree/bindings/rtc/epson,rx8900.yaml | 37 +++++++++++++++++--
 1 file changed, 34 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +0 -3
index 03af81754482..2682cbb9097d 100644
@@ -9,9 +9,6 @@ title: EPSON RX8900 / Microcrystal RV8803 Real-Time Clock
 maintainers:
   - Marek Vasut <[email protected]>
 
-allOf:
-  - $ref: rtc.yaml#
-
 properties:
   compatible:
     enum:

Documentation/devicetree/bindings/rtc/epson,rx8900.yaml properties: · line 35

Quoted 6 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +3 -0
@@ -33,6 +30,40 @@ properties:
 
   wakeup-source: true
 
+  tamper:
+    description: Subnode for tamper configuration.
+      This subnode is only available for epson,rx8901.

Drop last sentence. Don't repeat constraints in free form text.

Documentation/devicetree/bindings/rtc/epson,rx8900.yaml properties: · line 40

Quoted 5 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +5 -0
+    type: object
+    additionalProperties: false
+
+    properties:
+      buffer-overwrite:

Missing vendor prefix or where is this property defined?

Documentation/devicetree/bindings/rtc/epson,rx8900.yaml properties: · line 42

Quoted 2 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +2 -0
+        type: boolean
+        description: Set the buffer mode to overwrite. Default is inhibit.

Why woould this be a board configuration? You described the desired Linux feature or behavior, not the actual hardware. The bindings are about the latter, so instead you need to rephrase the property and its description to match actual hardware capabilities/features/configuration etc.

Documentation/devicetree/bindings/rtc/epson,rx8900.yaml properties: · line 45

Quoted 3 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +3 -0
+
+    patternProperties:
+      "^evin-[0-3]$":

Commit says three, schema says four.

Where is this property defined? Which common schema? Or is it vendor property?

Documentation/devicetree/bindings/rtc/epson,rx8900.yaml properties: · line 51

Quoted 6 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +6 -0
+        $ref: /schemas/types.yaml#/definitions/uint32-array
+        minItems: 3
+        maxItems: 3
+        description: External event input pin configuration.
+          The configuration is an array of tree values and contains
+          "pull-resistor", "trigger" and "filter".

What are the values here? Missing constraints, missing defaults.

Best regards,
Krzysztof

#9 lore raw

PATCH v4 7/7 rtc-rv8803: make tamper function configurable via dt

In reply to Markus Burri

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

---
 drivers/rtc/rtc-rv8803.c | 95 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 92 insertions(+), 3 deletions(-)
drivers/rtc/rtc-rv8803.c +92 -3
index 39d5881ba261..e3a4e9eaccb1 100644
@@ -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;
Signature · 1 line
2.39.5