Skip to contentSkip to conversation

Allow GPIO timestamps also via real-time clock

PATCH New Jan Kiszka 1 participant 4 messages Aug 31, 2021

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

v2 cover Allow GPIO timestamps also via real-time clock

Changes in v2:
- respect selected mode also in gpio_pin_interrupt
- select timestamp mode explicitly in gpiobench

As discussed, we need to add a separate control to manage whether the monotonic or the real-time clock should be used for GPIO timestamping.

Patch 1 adds this, keeping the default to monotonic. This can be added to 3.1.x stable as well. Patch 3 aligns the default to the rest of Xenomai, using real-time from now on. That's 3.2 material.

Jan

Jan Kiszka (3):
drivers/gpio: Add support for selecting real-time clock timestamps testsuite/gpiobench: Explicitly request monotonic timestamps drivers/gpio: Flip default for GPIO_RTIOC_TS to real-time clock

 include/cobalt/kernel/rtdm/gpio.h |  1 +
 include/rtdm/uapi/gpio.h          |  4 +++-
 kernel/drivers/gpio/gpio-core.c   | 19 +++++++++++++++----
 testsuite/gpiobench/gpiobench.c   |  2 +-
 4 files changed, 20 insertions(+), 6 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 1/3 drivers/gpio: Add support for selecting real-time clock timestamps

From: Jan Kiszka <[email protected]>

This allows to compare timestamps of GPIO pins against other devices and ensures that the stamps are synchronized cross-systems in case PTP is in use (dovetail-based kernels). Keep default to monotonic for now.

---
 include/cobalt/kernel/rtdm/gpio.h |  1 +
 include/rtdm/uapi/gpio.h          |  4 +++-
 kernel/drivers/gpio/gpio-core.c   | 19 +++++++++++++++----
 3 files changed, 19 insertions(+), 5 deletions(-)
include/cobalt/kernel/rtdm/gpio.h +1 -0
index 00055ec0a9..72cc3a0356 100644
@@ -34,6 +34,7 @@ struct rtdm_gpio_pin {
 	char *name;
 	struct gpio_desc *desc;
 	nanosecs_abs_t timestamp;
+	bool monotonic_timestamp;
 };
 
 struct rtdm_gpio_chip {
include/rtdm/uapi/gpio.h +3 -1
index 2839f7d51c..fb84274453 100644
@@ -29,7 +29,9 @@ struct rtdm_gpio_readout {
 #define GPIO_RTIOC_IRQDIS	_IO(RTDM_CLASS_GPIO, 3)
 #define GPIO_RTIOC_REQS		_IO(RTDM_CLASS_GPIO, 4)
 #define GPIO_RTIOC_RELS		_IO(RTDM_CLASS_GPIO, 5)
-#define GPIO_RTIOC_TS		_IOR(RTDM_CLASS_GPIO, 7, int)
+#define GPIO_RTIOC_TS_MONO	_IOR(RTDM_CLASS_GPIO, 7, int)
+#define GPIO_RTIOC_TS		GPIO_RTIOC_TS_MONO
+#define GPIO_RTIOC_TS_REAL	_IOR(RTDM_CLASS_GPIO, 8, int)
 
 #define GPIO_TRIGGER_NONE		0x0 /* unspecified */
 #define GPIO_TRIGGER_EDGE_RISING	0x1
kernel/drivers/gpio/gpio-core.c +15 -4
index 600ef9789a..5f5ee3d0f7 100644
@@ -42,7 +42,10 @@ static int gpio_pin_interrupt(rtdm_irq_t *irqh)
 
 	pin = rtdm_irq_get_arg(irqh, struct rtdm_gpio_pin);
 
-	pin->timestamp = rtdm_clock_read_monotonic();
+	if (pin->monotonic_timestamp)
+		pin->timestamp = rtdm_clock_read_monotonic();
+	else
+		pin->timestamp = rtdm_clock_read();
 	rtdm_event_signal(&pin->event);
 
 	return RTDM_IRQ_HANDLED;
@@ -189,11 +192,13 @@ static int gpio_pin_ioctl_nrt(struct rtdm_fd *fd,
 		gpio_free(gpio);
 		chan->requested = false;
 		break;
-	case GPIO_RTIOC_TS:
+	case GPIO_RTIOC_TS_MONO:
+	case GPIO_RTIOC_TS_REAL:
 		ret = rtdm_safe_copy_from_user(fd, &val, arg, sizeof(val));
 		if (ret)
 			return ret;
 		chan->want_timestamp = !!val;
+		pin->monotonic_timestamp = request == GPIO_RTIOC_TS_MONO;
 		break;
 	default:
 		return -EINVAL;
@@ -228,8 +233,11 @@ static ssize_t gpio_pin_read_rt(struct rtdm_fd *fd,
 			if (ret)
 				return ret;
 			rdo.timestamp = pin->timestamp;
-		} else
+		} else if (pin->monotonic_timestamp) {
 			rdo.timestamp = rtdm_clock_read_monotonic();
+		} else {
+			rdo.timestamp = rtdm_clock_read();
+		}
 
 		len = sizeof(rdo);
 		rdo.value = gpiod_get_raw_value(pin->desc);
@@ -489,7 +497,10 @@ int rtdm_gpiochip_post_event(struct rtdm_gpio_chip *rgc,
 		return -EINVAL;
 
 	pin = rgc->pins + offset;
-	pin->timestamp = rtdm_clock_read_monotonic();
+	if (pin->monotonic_timestamp)
+		pin->timestamp = rtdm_clock_read_monotonic();
+	else
+		pin->timestamp = rtdm_clock_read();
 	rtdm_event_signal(&pin->event);
 	
 	return 0;
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v2 2/3 testsuite/gpiobench: Explicitly request monotonic timestamps

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

The API's default will change, so select monotonic explicitly.

---
 testsuite/gpiobench/gpiobench.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testsuite/gpiobench/gpiobench.c +1 -1
index f83d7689f4..50afbd418a 100644
@@ -619,7 +619,7 @@ int main(int argc, char **argv)
 			goto out;
 		}
 
-		ret = ioctl(ti.fd_dev_intr, GPIO_RTIOC_TS, &value);
+		ret = ioctl(ti.fd_dev_intr, GPIO_RTIOC_TS_MONO, &value);
 		if (ret) {
 			printf("ioctl gpio port ts, failed\n");
 			goto out;
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH v2 3/3 drivers/gpio: Flip default for GPIO_RTIOC_TS to real-time clock

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

This is in line with other drivers and should avoid surprises like [1] in the future.

[1] https://xenomai.org/pipermail/xenomai/2021-June/045661.html

---
 include/rtdm/uapi/gpio.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/rtdm/uapi/gpio.h +1 -1
index fb84274453..82612a53d3 100644
@@ -30,8 +30,8 @@ struct rtdm_gpio_readout {
 #define GPIO_RTIOC_REQS		_IO(RTDM_CLASS_GPIO, 4)
 #define GPIO_RTIOC_RELS		_IO(RTDM_CLASS_GPIO, 5)
 #define GPIO_RTIOC_TS_MONO	_IOR(RTDM_CLASS_GPIO, 7, int)
-#define GPIO_RTIOC_TS		GPIO_RTIOC_TS_MONO
 #define GPIO_RTIOC_TS_REAL	_IOR(RTDM_CLASS_GPIO, 8, int)
+#define GPIO_RTIOC_TS		GPIO_RTIOC_TS_REAL
 
 #define GPIO_TRIGGER_NONE		0x0 /* unspecified */
 #define GPIO_TRIGGER_EDGE_RISING	0x1
Signature · 1 line
2.31.1