LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed.
@ 2021-11-18  3:22 Alexander Komrakov
  2021-11-18  3:22 ` Alexander Komrakov
  2022-02-07 14:07 ` Greg KH
  0 siblings, 2 replies; 6+ messages in thread
From: Alexander Komrakov @ 2021-11-18  3:22 UTC (permalink / raw)
  To: linux-kernel; +Cc: alexander.komrakov

[-- Attachment #1: Type: text/plain, Size: 1352 bytes --]

From: alexkom <alexander.komrakov@broadcom.com>

*** BLURB HERE ***

alexkom (1):
  Calculate the monotonic clock from the timespec clock to generate PPS
    elapsed real-time event value and stores the result into
    /sys/class/pps/pps0/assert_elapsed.

 Documentation/ABI/testing/sysfs-pps | 24 ++++++++++++++++++
 drivers/pps/kapi.c                  | 38 ++++++++++++++++++++++++++---
 drivers/pps/sysfs.c                 | 33 +++++++++++++++++++++++++
 include/linux/pps_kernel.h          |  2 ++
 4 files changed, 94 insertions(+), 3 deletions(-)

-- 
2.25.1


-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4222 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed.
  2021-11-18  3:22 [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed Alexander Komrakov
@ 2021-11-18  3:22 ` Alexander Komrakov
  2021-11-26 10:49   ` Rodolfo Giometti
  2022-02-07 14:08   ` Greg KH
  2022-02-07 14:07 ` Greg KH
  1 sibling, 2 replies; 6+ messages in thread
From: Alexander Komrakov @ 2021-11-18  3:22 UTC (permalink / raw)
  To: linux-kernel; +Cc: alexander.komrakov

[-- Attachment #1: Type: text/plain, Size: 8147 bytes --]

From: alexkom <alexander.komrakov@broadcom.com>

Because we have requirements to make sure the delta between standard time, say the GPS Time, and elapsedRealtime < 1 millisecond, regular linux clock timestamp is not enough for our use case.
The pin PPS will generate elapsedRealtime event at 1 sec boundary which is an exact value of the monotonic clock from the kernel PPS driver (/sys/class/pps/pps0/assert_elapsed).

Whenever AP receives this pulse, kernel's pps driver timestamp this elapsedRealtime event and let this time available via sysfs node (/sys/class/pps/pps0/assert_elapsed).

Signed-off-by: alexkom <alexander.komrakov@broadcom.com>
---
 Documentation/ABI/testing/sysfs-pps | 24 ++++++++++++++++++
 drivers/pps/kapi.c                  | 38 ++++++++++++++++++++++++++---
 drivers/pps/sysfs.c                 | 33 +++++++++++++++++++++++++
 include/linux/pps_kernel.h          |  2 ++
 4 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-pps b/Documentation/ABI/testing/sysfs-pps
index 25028c7bc37d..054617abeebd 100644
--- a/Documentation/ABI/testing/sysfs-pps
+++ b/Documentation/ABI/testing/sysfs-pps
@@ -1,3 +1,27 @@
+What:		/sys/class/pps/pps0/assert_elapsed
+Date:		October 2021
+Contact:	Alexander Komrakov <alexander.komrakov@broadcom.com>
+Description:
+		The /sys/class/pps/ppsX/assert_elapsed file reports the elapsed real-time assert events
+		and the elapsed real-time assert sequence number of the X-th source in the form:
+
+			<secs>.<nsec>#<sequence>
+
+		If the source has no elapsed real-time assert events the content of this file
+		is empty.
+
+What:		/sys/class/pps/ppsX/clear_elapsed
+Date:		October 2021
+Contact:	Alexander Komrakov <alexander.komrakov@broadcom.com>
+Description:
+		The /sys/class/pps/ppsX/clear_elapsed file reports the elapsed real-time clear events
+		and the elapsed real-time clear sequence number of the X-th source in the form:
+
+			<secs>.<nsec>#<sequence>
+
+		If the source has no elapsed real-time clear events the content of this file
+		is empty.
+
 What:		/sys/class/pps/
 Date:		February 2008
 Contact:	Rodolfo Giometti <giometti@linux.it>
diff --git a/drivers/pps/kapi.c b/drivers/pps/kapi.c
index d9d566f70ed1..149c29e498fc 100644
--- a/drivers/pps/kapi.c
+++ b/drivers/pps/kapi.c
@@ -23,6 +23,26 @@
 /*
  * Local functions
  */
+ #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
+
+/**
+ * clock_gettime - get the monotonic clock in pps_ktime format
+ * @kt:		pointer to the pps_ktime to be set to raw monotonic time
+ *
+ * The function calculates the monotonic clock from the timespec clock
+ * and stores the result in pps_ktime format in the variable pointed to by @kt.
+ *
+ * The function returns the monotonic clock normalized format in nanosec.
+ */
+static __u64 clock_gettime(struct pps_ktime *kt)
+{
+	struct timespec64 ts = { .tv_sec = 0, .tv_nsec = 0 };
+
+	ktime_get_ts64(&ts);
+	kt->sec = ts.tv_sec;
+	kt->nsec = ts.tv_nsec;
+	return (__u64) ts.tv_sec * NANOSEC_PER_SEC + ts.tv_nsec;
+}
 
 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
 {
@@ -162,11 +182,15 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	unsigned long flags;
 	int captured = 0;
 	struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
+	struct pps_ktime ts_real_elapsed = { .sec = 0, .nsec = 0, .flags = 0 };
 
 	/* check event type */
 	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
+	clock_gettime(&ts_real_elapsed);
+	dev_dbg(pps->dev, "PPS event (monotonic) at %lld.%09d\n",
+			(s64)ts_real_elapsed.sec, ts_real_elapsed.nsec);
 
-	dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
+	dev_dbg(pps->dev, "PPS event (timestamp) at %lld.%09ld\n",
 			(s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
 
 	timespec_to_pps_ktime(&ts_real, ts->ts_real);
@@ -181,11 +205,15 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	pps->current_mode = pps->params.mode;
 	if (event & pps->params.mode & PPS_CAPTUREASSERT) {
 		/* We have to add an offset? */
-		if (pps->params.mode & PPS_OFFSETASSERT)
+		if (pps->params.mode & PPS_OFFSETASSERT) {
+			pps_add_offset(&ts_real_elapsed,
+					&pps->params.assert_off_tu);
 			pps_add_offset(&ts_real,
 					&pps->params.assert_off_tu);
+		}
 
 		/* Save the time stamp */
+		pps->assert_elapsed_tu = ts_real_elapsed;
 		pps->assert_tu = ts_real;
 		pps->assert_sequence++;
 		dev_dbg(pps->dev, "capture assert seq #%u\n",
@@ -195,11 +223,15 @@ void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
 	}
 	if (event & pps->params.mode & PPS_CAPTURECLEAR) {
 		/* We have to add an offset? */
-		if (pps->params.mode & PPS_OFFSETCLEAR)
+		if (pps->params.mode & PPS_OFFSETCLEAR)	{
+			pps_add_offset(&ts_real_elapsed,
+					&pps->params.clear_off_tu);
 			pps_add_offset(&ts_real,
 					&pps->params.clear_off_tu);
+		}
 
 		/* Save the time stamp */
+		pps->clear_elapsed_tu = ts_real_elapsed;
 		pps->clear_tu = ts_real;
 		pps->clear_sequence++;
 		dev_dbg(pps->dev, "capture clear seq #%u\n",
diff --git a/drivers/pps/sysfs.c b/drivers/pps/sysfs.c
index 134bc33f6ad0..97721cb01695 100644
--- a/drivers/pps/sysfs.c
+++ b/drivers/pps/sysfs.c
@@ -29,6 +29,21 @@ static ssize_t assert_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(assert);
 
+static ssize_t assert_elapsed_show(struct device *dev,
+			   struct device_attribute *attr, char *buf)
+{
+	struct pps_device *pps = dev_get_drvdata(dev);
+
+	if (!(pps->info.mode & PPS_CAPTUREASSERT))
+		return 0;
+
+	return sprintf(buf, "%lld.%09d#%d\n",
+			(long long) pps->assert_elapsed_tu.sec,
+			pps->assert_elapsed_tu.nsec,
+			pps->assert_sequence);
+}
+static DEVICE_ATTR_RO(assert_elapsed);
+
 static ssize_t clear_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {
@@ -43,6 +58,22 @@ static ssize_t clear_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(clear);
 
+static ssize_t clear_elapsed_show(struct device *dev,
+			  struct device_attribute *attr,
+			  char *buf)
+{
+	struct pps_device *pps = dev_get_drvdata(dev);
+
+	if (!(pps->info.mode & PPS_CAPTURECLEAR))
+		return 0;
+
+	return sprintf(buf, "%lld.%09d#%d\n",
+			(long long) pps->clear_elapsed_tu.sec,
+			pps->clear_elapsed_tu.nsec,
+			pps->clear_sequence);
+}
+static DEVICE_ATTR_RO(clear_elapsed);
+
 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
 			 char *buf)
 {
@@ -81,7 +112,9 @@ static DEVICE_ATTR_RO(path);
 
 static struct attribute *pps_attrs[] = {
 	&dev_attr_assert.attr,
+	&dev_attr_assert_elapsed.attr,
 	&dev_attr_clear.attr,
+	&dev_attr_clear_elapsed.attr,
 	&dev_attr_mode.attr,
 	&dev_attr_echo.attr,
 	&dev_attr_name.attr,
diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h
index 78c8ac4951b5..1fecaaf4c8b9 100644
--- a/include/linux/pps_kernel.h
+++ b/include/linux/pps_kernel.h
@@ -47,6 +47,8 @@ struct pps_device {
 
 	__u32 assert_sequence;			/* PPS assert event seq # */
 	__u32 clear_sequence;			/* PPS clear event seq # */
+	struct pps_ktime assert_elapsed_tu; /* PPS elapsed rt assert seq # */
+	struct pps_ktime clear_elapsed_tu;  /* PPS elapsed rt clear event seq */
 	struct pps_ktime assert_tu;
 	struct pps_ktime clear_tu;
 	int current_mode;			/* PPS mode at event time */
-- 
2.25.1


-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4222 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed.
  2021-11-18  3:22 ` Alexander Komrakov
@ 2021-11-26 10:49   ` Rodolfo Giometti
  2022-02-07 14:08   ` Greg KH
  1 sibling, 0 replies; 6+ messages in thread
From: Rodolfo Giometti @ 2021-11-26 10:49 UTC (permalink / raw)
  To: Alexander Komrakov, linux-kernel

On 18/11/21 04:22, Alexander Komrakov wrote:
> From: alexkom <alexander.komrakov@broadcom.com>
> 
> Because we have requirements to make sure the delta between standard time, say the GPS Time, and elapsedRealtime < 1 millisecond, regular linux clock timestamp is not enough for our use case.
> The pin PPS will generate elapsedRealtime event at 1 sec boundary which is an exact value of the monotonic clock from the kernel PPS driver (/sys/class/pps/pps0/assert_elapsed).
> 
> Whenever AP receives this pulse, kernel's pps driver timestamp this elapsedRealtime event and let this time available via sysfs node (/sys/class/pps/pps0/assert_elapsed).
> 
> Signed-off-by: alexkom <alexander.komrakov@broadcom.com>

Acked-by: Rodolfo Giometti <giometti@enneenne.com>

-- 
GNU/Linux Solutions                  e-mail: giometti@enneenne.com
Linux Device Driver                          giometti@linux.it
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed.
  2021-11-18  3:22 [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed Alexander Komrakov
  2021-11-18  3:22 ` Alexander Komrakov
@ 2022-02-07 14:07 ` Greg KH
  1 sibling, 0 replies; 6+ messages in thread
From: Greg KH @ 2022-02-07 14:07 UTC (permalink / raw)
  To: Alexander Komrakov; +Cc: linux-kernel

On Wed, Nov 17, 2021 at 07:22:16PM -0800, Alexander Komrakov wrote:
> -- 
> This electronic communication and the information and any files transmitted 
> with it, or attached to it, are confidential and are intended solely for 
> the use of the individual or entity to whom it is addressed and may contain 
> information that is confidential, legally privileged, protected by privacy 
> laws, or otherwise restricted from disclosure to anyone else. If you are 
> not the intended recipient or the person responsible for delivering the 
> e-mail to the intended recipient, you are hereby notified that any use, 
> copying, distributing, dissemination, forwarding, printing, or copying of 
> this e-mail is strictly prohibited. If you received this e-mail in error, 
> please return the e-mail to the sender, delete it from your computer, and 
> destroy any printed copy of it.

Email is now deleted.  This is not compatible with Linux kernel
development for the obvious reasons :(

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed.
  2021-11-18  3:22 ` Alexander Komrakov
  2021-11-26 10:49   ` Rodolfo Giometti
@ 2022-02-07 14:08   ` Greg KH
  1 sibling, 0 replies; 6+ messages in thread
From: Greg KH @ 2022-02-07 14:08 UTC (permalink / raw)
  To: Rodolfo Giometti, Alexander Komrakov; +Cc: linux-kernel

On Wed, Nov 17, 2021 at 07:22:17PM -0800, Alexander Komrakov wrote:
> -- 
> This electronic communication and the information and any files transmitted 
> with it, or attached to it, are confidential and are intended solely for 
> the use of the individual or entity to whom it is addressed and may contain 
> information that is confidential, legally privileged, protected by privacy 
> laws, or otherwise restricted from disclosure to anyone else. If you are 
> not the intended recipient or the person responsible for delivering the 
> e-mail to the intended recipient, you are hereby notified that any use, 
> copying, distributing, dissemination, forwarding, printing, or copying of 
> this e-mail is strictly prohibited. If you received this e-mail in error, 
> please return the e-mail to the sender, delete it from your computer, and 
> destroy any printed copy of it.

Now deleted, we can't take patches with this footer, as per your legal
team's requirements.  Please fix this.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed.
@ 2022-01-10  4:22 Alexander Komrakov
  0 siblings, 0 replies; 6+ messages in thread
From: Alexander Komrakov @ 2022-01-10  4:22 UTC (permalink / raw)
  To: linux-kernel; +Cc: giometti, alexander komrakov

[-- Attachment #1: Type: text/plain, Size: 1210 bytes --]

From: alexander komrakov <alexander.komrakov@broadcom.com>

*** BLURB HERE ***

alexander komrakov (1):
  Signed-off-by: alexander komrakov <alexander.komrakov@broadcom.com>
    Replacing ktime_get_ts64 by ktime_get_boottime_ts64 because
    elapsedRealtime includes time spent in sleep and pps should be also
    including sleep time.

 drivers/pps/kapi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

-- 
2.25.1


-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4222 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-02-07 14:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-18  3:22 [PATCH 1/1] Calculate the monotonic clock from the timespec clock to generate PPS elapsed real-time event value and stores the result into /sys/class/pps/pps0/assert_elapsed Alexander Komrakov
2021-11-18  3:22 ` Alexander Komrakov
2021-11-26 10:49   ` Rodolfo Giometti
2022-02-07 14:08   ` Greg KH
2022-02-07 14:07 ` Greg KH
2022-01-10  4:22 Alexander Komrakov

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).