Add helper apis to:
- Enable external timestamps
- Disable external timestamps
- Read external timestamps

Signed-off-by: Lokesh Vutla <lokeshvu...@ti.com>
---
 phc.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 phc.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)

diff --git a/phc.c b/phc.c
index 14132db..d7a688e 100644
--- a/phc.c
+++ b/phc.c
@@ -19,6 +19,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/ptp_clock.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/ioctl.h>
@@ -27,6 +28,7 @@
 #include <unistd.h>
 
 #include "phc.h"
+#include "print.h"
 
 /*
  * On 32 bit platforms, the PHC driver's maximum adjustment (type
@@ -127,3 +129,49 @@ int phc_has_pps(clockid_t clkid)
                return 0;
        return caps.pps;
 }
+
+int phc_enable_extts(clockid_t clkid, int chan_index)
+{
+       struct ptp_extts_request extts_request;
+       int fd = CLOCKID_TO_FD(clkid), err;
+
+       memset(&extts_request, 0, sizeof(extts_request));
+       extts_request.index = chan_index;
+       extts_request.flags = PTP_ENABLE_FEATURE;
+       err = ioctl(fd, PTP_EXTTS_REQUEST, &extts_request);
+       if (err)
+               pr_err("Enabling PTP_EXTTS_REQUEST on ptp dev: FAILED\n");
+
+       return err;
+}
+
+int phc_disable_extts(clockid_t clkid, int chan_index)
+{
+       struct ptp_extts_request extts_request;
+       int fd = CLOCKID_TO_FD(clkid), err;
+
+       memset(&extts_request, 0, sizeof(extts_request));
+       extts_request.index = chan_index;
+       extts_request.flags = 0;
+       err = ioctl(fd, PTP_EXTTS_REQUEST, &extts_request);
+       if (err)
+               pr_err("Disabling PTP_EXTTS_REQUEST on ptp dev: FAILED\n");
+
+       return err;
+}
+
+int phc_read_extts(clockid_t clkid, uint64_t *ts)
+{
+       int fd = CLOCKID_TO_FD(clkid), count;
+       struct ptp_extts_event event;
+
+       count = read(fd, &event, sizeof(event));
+       if (count != sizeof(event)) {
+               pr_err("PTP event read %d: FAILED\n", count);
+               return -EINVAL;
+       }
+
+       *ts = event.t.sec * 1000000000ULL + event.t.nsec;
+
+       return 0;
+}
diff --git a/phc.h b/phc.h
index 4dbc374..befa1dc 100644
--- a/phc.h
+++ b/phc.h
@@ -77,4 +77,33 @@ int phc_pin_setfunc(clockid_t clkid, struct ptp_pin_desc 
*desc);
  */
 int phc_has_pps(clockid_t clkid);
 
+/**
+ * Enables support for PTP external timestamp requests.
+ *
+ * @param clkid: A clock ID obtained using phc_open().
+ * @param chan_index: Channel in which external timestamps are to be enabled.
+ *
+ * @return Zero if external timestamp is enabled else appropriate error value.
+ */
+int phc_enable_extts(clockid_t clkid, int chan_index);
+
+/**
+ * Disables support for PTP external timestamp requests.
+ *
+ * @param clkid: A clock ID obtained using phc_open().
+ * @param chan_index: Channel in which external timestamps are to be disabled.
+ *
+ * @return Zero if external timestamp is enabled else appropriate error value.
+ */
+int phc_disable_extts(clockid_t clkid, int chan_index);
+
+/**
+ * Reads the external timestamp from PTP device.
+ *
+ * @param clkid: A clock ID obtained using phc_open().
+ * @param ts: Pointer to variable in which the timestamp is stored.
+ *
+ * @return Zero if successfully read from ptp else appropriate error value.
+ */
+int phc_read_extts(clockid_t clkid, uint64_t *ts);
 #endif
-- 
2.23.0



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to