Skip to contentSkip to conversation

rtc-rv8803: Implement timestamp trigger over event pins

PATCH Superseded Markus Burri 2 participants 10 messages May 13, 2025 – May 14, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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

This series applies on 'linux-6.13~rc7'

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]/

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 |  42 +-
 drivers/rtc/rtc-rv8803.c                      | 599 +++++++++++++++++-
 3 files changed, 672 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-class-rtc-tamper

base-commit: 92a09c47464d040866cf2b4cd052bc60555185fb

Signature · 1 line
2.39.5
#1 lore raw

PATCH v3 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

PATCH v3 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
#3 lore raw

PATCH v3 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                      | 366 ++++++++++++++++++
 2 files changed, 387 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..f035d0fa5fde
@@ -0,0 +1,21 @@
+What:		/sys/class/rtc/rtcX/tamper/enable
+Date:		January 2025
+KernelVersion:	6.13
+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:		January 2025
+KernelVersion:	6.13
+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 +366 -0

396 lines not on this page · raw message

Signature · 1 line
2.39.5
#4 lore raw

PATCH v3 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 f035d0fa5fde..2fd6578a6bd9 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:		January 2025
+KernelVersion:	6.13
+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 ef2231e5abc9..8f9bcce6351c 100644
@@ -904,12 +904,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
#5 lore raw

PATCH v3 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                      | 106 ++++++++++++++++++
 2 files changed, 114 insertions(+)
Documentation/ABI/testing/sysfs-class-rtc-tamper +8 -0
index 2fd6578a6bd9..b3aa73c81c6b 100644
@@ -26,3 +26,11 @@ KernelVersion:	6.13
 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:		January 2025
+KernelVersion:	6.13
+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 +106 -0

140 lines not on this page · raw message

Signature · 1 line
2.39.5
#6 lore raw

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

In reply to Markus Burri

Document tamper detection property for epson,rx8901 rtc chip.

---
 .../devicetree/bindings/rtc/epson,rx8900.yaml | 40 +++++++++++++++++--
 1 file changed, 37 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +37 -3
index 03af81754482..c2e542c9bdc6 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,43 @@ properties:
 
   wakeup-source: true
 
+  tamper:
+    description: Subnode for tamper configuration. This
+      subnode is only available for epson,rx8901.
+    type: object
+    additionalProperties: false
+    properties:
+      buffer-mode:
+        description: Set the buffer mode to inhibit (0) or overwrite (1).
+        minimum: 0
+        maximum: 1
+    patternProperties:
+      "^evin-[0-3]$":
+        $ref: /schemas/types.yaml#/definitions/uint32-array
+        minItems: 3
+        maxItems: 3
+        description: |
+          Event input pin configuration.
+          The configuration is an array of tree values and contains
+          "pull-resistore", "trigger" and "filter".
+          For a detaild description, see epson-rx8901 datasheet.
+
+allOf:
+  - $ref: rtc.yaml#
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - epson,rx8901
+    then:
+      properties:
+        tamper: true
+    else:
+      # property is not allowed:
+      properties:
+        tamper: false
+
 required:
   - compatible
   - reg
Signature · 1 line
2.39.5
#7 lore raw

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

Quoted 7 lines · Markus Burri

Document tamper detection property for epson,rx8901 rtc chip.

---
 .../devicetree/bindings/rtc/epson,rx8900.yaml | 40 +++++++++++++++++--
 1 file changed, 37 insertions(+), 3 deletions(-)

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/rtc/epson,rx8900.yaml: buffer-mode: missing type definition

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.

maintainer
#8 lore raw

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

In reply to Markus Burri
Quoted 1 line · Markus Burri

Document tamper detection property for epson,rx8901 rtc chip.

Looks like a lot more than 1 property. Explain the feature and why it is needed. What the change is is obvious reading the diff.

Quoted 21 lines
---
 .../devicetree/bindings/rtc/epson,rx8900.yaml | 40 +++++++++++++++++--
 1 file changed, 37 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +0 -3
index 03af81754482..c2e542c9bdc6 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,43 @@ properties:
 
   wakeup-source: true
 
+  tamper:
+    description: Subnode for tamper configuration. This
+      subnode is only available for epson,rx8901.

Wrap at 80 char.

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

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

blank line

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

Quoted 5 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +5 -0
+    properties:
+      buffer-mode:
+        description: Set the buffer mode to inhibit (0) or overwrite (1).
+        minimum: 0
+        maximum: 1

Could be boolean?

blank line

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

Quoted 9 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +9 -0
+    patternProperties:
+      "^evin-[0-3]$":
+        $ref: /schemas/types.yaml#/definitions/uint32-array
+        minItems: 3
+        maxItems: 3
+        description: |
+          Event input pin configuration.
+          The configuration is an array of tree values and contains
+          "pull-resistore", "trigger" and "filter".

pull-resistor

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

Quoted 1 line
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +1 -0
+          For a detaild description, see epson-rx8901 datasheet.

detailed

blank line between paragraphs.

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

Quoted 12 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +12 -0
+
+allOf:
+  - $ref: rtc.yaml#
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - epson,rx8901
+    then:
+      properties:
+        tamper: true

Don't need this. It is allowed by default. Invert the if.

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

Quoted 2 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +2 -0
+    else:
+      # property is not allowed:

Drop comment

Quoted 9 lines
Documentation/devicetree/bindings/rtc/epson,rx8900.yaml +3 -0
+      properties:
+        tamper: false
+
 required:
   - compatible
   - reg
Signature · 1 line
2.39.5
#9 lore raw

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

In reply to Markus Burri

This settings are 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 | 89 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 86 insertions(+), 3 deletions(-)
drivers/rtc/rtc-rv8803.c +86 -3
index d4b1e04c97de..de28f7846dd7 100644
@@ -113,6 +113,11 @@ enum evin_trigger {
 	both_edges = 0b10,
 };
 
+enum evin_buffer_mode {
+	inhibit = 0,
+	overwrite = 1,
+};
+
 struct cfg_val_txt {
 	char *txt;
 	u8 val;
@@ -163,6 +168,15 @@ static const u8 evin_flt_regs[] = {
 	RX8901_EVIN3_FLT
 };
 
+struct tamper_cfg {
+	struct {
+		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;
@@ -170,6 +184,7 @@ struct rv8803_data {
 	u8 ctrl;
 	u8 backup;
 	u8 alarm_invalid:1;
+	struct tamper_cfg tamper_cfg;
 	enum rv8803_type type;
 };
 
@@ -772,7 +787,10 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, c
 
 	/* 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;
 	}
@@ -790,10 +808,11 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, c
 	}
 
 	/*
-	 * 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;
 
@@ -1091,6 +1110,66 @@ 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].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) {
+		u32 node_value;
+
+		if (of_property_read_u32(of_tamper, "buffer-mode", &node_value))
+			tamper_cfg->buffer_mode = node_value;
+
+		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].pull_resistor = evin_val[0];
+				tamper_cfg->evin[i].trigger = evin_val[1];
+				tamper_cfg->evin[i].filter = evin_val[2];
+			}
+		}
+		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;
+	}
+
+	return 0;
+}
+
 /* configure registers with values different than the Power-On reset defaults */
 static int rv8803_regs_configure(struct rv8803_data *rv8803)
 {
@@ -1232,6 +1311,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