xref: /linux/include/uapi/linux/ptp_clock.h (revision 100c85421b52e41269ada88f7d71a6b8a06c7a11)
1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2607ca46eSDavid Howells /*
3607ca46eSDavid Howells  * PTP 1588 clock support - user space interface
4607ca46eSDavid Howells  *
5607ca46eSDavid Howells  * Copyright (C) 2010 OMICRON electronics GmbH
6607ca46eSDavid Howells  *
7607ca46eSDavid Howells  *  This program is free software; you can redistribute it and/or modify
8607ca46eSDavid Howells  *  it under the terms of the GNU General Public License as published by
9607ca46eSDavid Howells  *  the Free Software Foundation; either version 2 of the License, or
10607ca46eSDavid Howells  *  (at your option) any later version.
11607ca46eSDavid Howells  *
12607ca46eSDavid Howells  *  This program is distributed in the hope that it will be useful,
13607ca46eSDavid Howells  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14607ca46eSDavid Howells  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15607ca46eSDavid Howells  *  GNU General Public License for more details.
16607ca46eSDavid Howells  *
17607ca46eSDavid Howells  *  You should have received a copy of the GNU General Public License
18607ca46eSDavid Howells  *  along with this program; if not, write to the Free Software
19607ca46eSDavid Howells  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20607ca46eSDavid Howells  */
21607ca46eSDavid Howells 
22607ca46eSDavid Howells #ifndef _PTP_CLOCK_H_
23607ca46eSDavid Howells #define _PTP_CLOCK_H_
24607ca46eSDavid Howells 
25607ca46eSDavid Howells #include <linux/ioctl.h>
26607ca46eSDavid Howells #include <linux/types.h>
27607ca46eSDavid Howells 
2841560658SFelipe Balbi /*
2941560658SFelipe Balbi  * Bits of the ptp_extts_request.flags field:
3041560658SFelipe Balbi  */
31607ca46eSDavid Howells #define PTP_ENABLE_FEATURE (1<<0)
32607ca46eSDavid Howells #define PTP_RISING_EDGE    (1<<1)
33607ca46eSDavid Howells #define PTP_FALLING_EDGE   (1<<2)
346138e687SRichard Cochran #define PTP_STRICT_FLAGS   (1<<3)
35*ea1cc3eeSMin Li #define PTP_EXT_OFFSET     (1<<4)
36cd734d54SRichard Cochran #define PTP_EXTTS_EDGES    (PTP_RISING_EDGE | PTP_FALLING_EDGE)
372df4de16SJacob Keller 
382df4de16SJacob Keller /*
392df4de16SJacob Keller  * flag fields valid for the new PTP_EXTTS_REQUEST2 ioctl.
402df4de16SJacob Keller  */
4141560658SFelipe Balbi #define PTP_EXTTS_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
4241560658SFelipe Balbi 				 PTP_RISING_EDGE |	\
436138e687SRichard Cochran 				 PTP_FALLING_EDGE |	\
44*ea1cc3eeSMin Li 				 PTP_STRICT_FLAGS |	\
45*ea1cc3eeSMin Li 				 PTP_EXT_OFFSET)
4641560658SFelipe Balbi 
4741560658SFelipe Balbi /*
482df4de16SJacob Keller  * flag fields valid for the original PTP_EXTTS_REQUEST ioctl.
492df4de16SJacob Keller  * DO NOT ADD NEW FLAGS HERE.
502df4de16SJacob Keller  */
512df4de16SJacob Keller #define PTP_EXTTS_V1_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
522df4de16SJacob Keller 					 PTP_RISING_EDGE |	\
532df4de16SJacob Keller 					 PTP_FALLING_EDGE)
542df4de16SJacob Keller 
552df4de16SJacob Keller /*
56*ea1cc3eeSMin Li  * flag fields valid for the ptp_extts_event report.
57*ea1cc3eeSMin Li  */
58*ea1cc3eeSMin Li #define PTP_EXTTS_EVENT_VALID	(PTP_ENABLE_FEATURE)
59*ea1cc3eeSMin Li 
60*ea1cc3eeSMin Li /*
6141560658SFelipe Balbi  * Bits of the ptp_perout_request.flags field:
6241560658SFelipe Balbi  */
63823eb2a3SFelipe Balbi #define PTP_PEROUT_ONE_SHOT		(1<<0)
64f65b71aaSVladimir Oltean #define PTP_PEROUT_DUTY_CYCLE		(1<<1)
65b6bd4136SVladimir Oltean #define PTP_PEROUT_PHASE		(1<<2)
662df4de16SJacob Keller 
672df4de16SJacob Keller /*
682df4de16SJacob Keller  * flag fields valid for the new PTP_PEROUT_REQUEST2 ioctl.
692df4de16SJacob Keller  */
70f65b71aaSVladimir Oltean #define PTP_PEROUT_VALID_FLAGS		(PTP_PEROUT_ONE_SHOT | \
71b6bd4136SVladimir Oltean 					 PTP_PEROUT_DUTY_CYCLE | \
72b6bd4136SVladimir Oltean 					 PTP_PEROUT_PHASE)
732df4de16SJacob Keller 
742df4de16SJacob Keller /*
752df4de16SJacob Keller  * No flags are valid for the original PTP_PEROUT_REQUEST ioctl
762df4de16SJacob Keller  */
772df4de16SJacob Keller #define PTP_PEROUT_V1_VALID_FLAGS	(0)
782df4de16SJacob Keller 
79607ca46eSDavid Howells /*
80607ca46eSDavid Howells  * struct ptp_clock_time - represents a time value
81607ca46eSDavid Howells  *
82607ca46eSDavid Howells  * The sign of the seconds field applies to the whole value. The
83607ca46eSDavid Howells  * nanoseconds field is always unsigned. The reserved field is
84607ca46eSDavid Howells  * included for sub-nanosecond resolution, should the demand for
85607ca46eSDavid Howells  * this ever appear.
86607ca46eSDavid Howells  *
87607ca46eSDavid Howells  */
88607ca46eSDavid Howells struct ptp_clock_time {
89607ca46eSDavid Howells 	__s64 sec;  /* seconds */
90607ca46eSDavid Howells 	__u32 nsec; /* nanoseconds */
91607ca46eSDavid Howells 	__u32 reserved;
92607ca46eSDavid Howells };
93607ca46eSDavid Howells 
94607ca46eSDavid Howells struct ptp_clock_caps {
95607ca46eSDavid Howells 	int max_adj;   /* Maximum frequency adjustment in parts per billon. */
96607ca46eSDavid Howells 	int n_alarm;   /* Number of programmable alarms. */
97607ca46eSDavid Howells 	int n_ext_ts;  /* Number of external time stamp channels. */
98607ca46eSDavid Howells 	int n_per_out; /* Number of programmable periodic signals. */
99607ca46eSDavid Howells 	int pps;       /* Whether the clock supports a PPS callback. */
1006092315dSRichard Cochran 	int n_pins;    /* Number of input/output pins. */
101719f1aa4SChristopher S. Hall 	/* Whether the clock supports precise system-device cross timestamps */
102719f1aa4SChristopher S. Hall 	int cross_timestamping;
103d3f1cbd2SVincent Cheng 	/* Whether the clock supports adjust phase */
104d3f1cbd2SVincent Cheng 	int adjust_phase;
105c3b60ab7SRahul Rameshbabu 	int max_phase_adj; /* Maximum phase adjustment in nanoseconds. */
106c3b60ab7SRahul Rameshbabu 	int rsv[11];       /* Reserved for future use. */
107607ca46eSDavid Howells };
108607ca46eSDavid Howells 
109607ca46eSDavid Howells struct ptp_extts_request {
110607ca46eSDavid Howells 	unsigned int index;  /* Which channel to configure. */
111607ca46eSDavid Howells 	unsigned int flags;  /* Bit field for PTP_xxx flags. */
112607ca46eSDavid Howells 	unsigned int rsv[2]; /* Reserved for future use. */
113607ca46eSDavid Howells };
114607ca46eSDavid Howells 
115607ca46eSDavid Howells struct ptp_perout_request {
116b6bd4136SVladimir Oltean 	union {
117b6bd4136SVladimir Oltean 		/*
118b6bd4136SVladimir Oltean 		 * Absolute start time.
119b6bd4136SVladimir Oltean 		 * Valid only if (flags & PTP_PEROUT_PHASE) is unset.
120b6bd4136SVladimir Oltean 		 */
121b6bd4136SVladimir Oltean 		struct ptp_clock_time start;
122b6bd4136SVladimir Oltean 		/*
123b6bd4136SVladimir Oltean 		 * Phase offset. The signal should start toggling at an
124b6bd4136SVladimir Oltean 		 * unspecified integer multiple of the period, plus this value.
125b6bd4136SVladimir Oltean 		 * The start time should be "as soon as possible".
126b6bd4136SVladimir Oltean 		 * Valid only if (flags & PTP_PEROUT_PHASE) is set.
127b6bd4136SVladimir Oltean 		 */
128b6bd4136SVladimir Oltean 		struct ptp_clock_time phase;
129b6bd4136SVladimir Oltean 	};
130607ca46eSDavid Howells 	struct ptp_clock_time period; /* Desired period, zero means disable. */
131607ca46eSDavid Howells 	unsigned int index;           /* Which channel to configure. */
132823eb2a3SFelipe Balbi 	unsigned int flags;
133f65b71aaSVladimir Oltean 	union {
134f65b71aaSVladimir Oltean 		/*
135f65b71aaSVladimir Oltean 		 * The "on" time of the signal.
136f65b71aaSVladimir Oltean 		 * Must be lower than the period.
137f65b71aaSVladimir Oltean 		 * Valid only if (flags & PTP_PEROUT_DUTY_CYCLE) is set.
138f65b71aaSVladimir Oltean 		 */
139f65b71aaSVladimir Oltean 		struct ptp_clock_time on;
140f65b71aaSVladimir Oltean 		/* Reserved for future use. */
141f65b71aaSVladimir Oltean 		unsigned int rsv[4];
142f65b71aaSVladimir Oltean 	};
143607ca46eSDavid Howells };
144607ca46eSDavid Howells 
145215b13ddSRichard Cochran #define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */
146215b13ddSRichard Cochran 
147215b13ddSRichard Cochran struct ptp_sys_offset {
148215b13ddSRichard Cochran 	unsigned int n_samples; /* Desired number of measurements. */
149215b13ddSRichard Cochran 	unsigned int rsv[3];    /* Reserved for future use. */
150215b13ddSRichard Cochran 	/*
151215b13ddSRichard Cochran 	 * Array of interleaved system/phc time stamps. The kernel
152215b13ddSRichard Cochran 	 * will provide 2*n_samples + 1 time stamps, with the last
153215b13ddSRichard Cochran 	 * one as a system time stamp.
154215b13ddSRichard Cochran 	 */
155215b13ddSRichard Cochran 	struct ptp_clock_time ts[2 * PTP_MAX_SAMPLES + 1];
156215b13ddSRichard Cochran };
157215b13ddSRichard Cochran 
15836180087SMiroslav Lichvar struct ptp_sys_offset_extended {
15936180087SMiroslav Lichvar 	unsigned int n_samples; /* Desired number of measurements. */
16036180087SMiroslav Lichvar 	unsigned int rsv[3];    /* Reserved for future use. */
16136180087SMiroslav Lichvar 	/*
16236180087SMiroslav Lichvar 	 * Array of [system, phc, system] time stamps. The kernel will provide
16336180087SMiroslav Lichvar 	 * 3*n_samples time stamps.
16436180087SMiroslav Lichvar 	 */
16536180087SMiroslav Lichvar 	struct ptp_clock_time ts[PTP_MAX_SAMPLES][3];
16636180087SMiroslav Lichvar };
16736180087SMiroslav Lichvar 
168719f1aa4SChristopher S. Hall struct ptp_sys_offset_precise {
169719f1aa4SChristopher S. Hall 	struct ptp_clock_time device;
170719f1aa4SChristopher S. Hall 	struct ptp_clock_time sys_realtime;
171719f1aa4SChristopher S. Hall 	struct ptp_clock_time sys_monoraw;
172719f1aa4SChristopher S. Hall 	unsigned int rsv[4];    /* Reserved for future use. */
173719f1aa4SChristopher S. Hall };
174719f1aa4SChristopher S. Hall 
1756092315dSRichard Cochran enum ptp_pin_function {
1766092315dSRichard Cochran 	PTP_PF_NONE,
1776092315dSRichard Cochran 	PTP_PF_EXTTS,
1786092315dSRichard Cochran 	PTP_PF_PEROUT,
1796092315dSRichard Cochran 	PTP_PF_PHYSYNC,
1806092315dSRichard Cochran };
1816092315dSRichard Cochran 
1826092315dSRichard Cochran struct ptp_pin_desc {
1836092315dSRichard Cochran 	/*
1846092315dSRichard Cochran 	 * Hardware specific human readable pin name. This field is
1856092315dSRichard Cochran 	 * set by the kernel during the PTP_PIN_GETFUNC ioctl and is
1866092315dSRichard Cochran 	 * ignored for the PTP_PIN_SETFUNC ioctl.
1876092315dSRichard Cochran 	 */
1886092315dSRichard Cochran 	char name[64];
1896092315dSRichard Cochran 	/*
1906092315dSRichard Cochran 	 * Pin index in the range of zero to ptp_clock_caps.n_pins - 1.
1916092315dSRichard Cochran 	 */
1926092315dSRichard Cochran 	unsigned int index;
1936092315dSRichard Cochran 	/*
1946092315dSRichard Cochran 	 * Which of the PTP_PF_xxx functions to use on this pin.
1956092315dSRichard Cochran 	 */
1966092315dSRichard Cochran 	unsigned int func;
1976092315dSRichard Cochran 	/*
1986092315dSRichard Cochran 	 * The specific channel to use for this function.
1996092315dSRichard Cochran 	 * This corresponds to the 'index' field of the
2006092315dSRichard Cochran 	 * PTP_EXTTS_REQUEST and PTP_PEROUT_REQUEST ioctls.
2016092315dSRichard Cochran 	 */
2026092315dSRichard Cochran 	unsigned int chan;
2036092315dSRichard Cochran 	/*
2046092315dSRichard Cochran 	 * Reserved for future use.
2056092315dSRichard Cochran 	 */
2066092315dSRichard Cochran 	unsigned int rsv[5];
2076092315dSRichard Cochran };
2086092315dSRichard Cochran 
209607ca46eSDavid Howells #define PTP_CLK_MAGIC '='
210607ca46eSDavid Howells 
211607ca46eSDavid Howells #define PTP_CLOCK_GETCAPS  _IOR(PTP_CLK_MAGIC, 1, struct ptp_clock_caps)
212607ca46eSDavid Howells #define PTP_EXTTS_REQUEST  _IOW(PTP_CLK_MAGIC, 2, struct ptp_extts_request)
213607ca46eSDavid Howells #define PTP_PEROUT_REQUEST _IOW(PTP_CLK_MAGIC, 3, struct ptp_perout_request)
214607ca46eSDavid Howells #define PTP_ENABLE_PPS     _IOW(PTP_CLK_MAGIC, 4, int)
215215b13ddSRichard Cochran #define PTP_SYS_OFFSET     _IOW(PTP_CLK_MAGIC, 5, struct ptp_sys_offset)
2166092315dSRichard Cochran #define PTP_PIN_GETFUNC    _IOWR(PTP_CLK_MAGIC, 6, struct ptp_pin_desc)
2176092315dSRichard Cochran #define PTP_PIN_SETFUNC    _IOW(PTP_CLK_MAGIC, 7, struct ptp_pin_desc)
218719f1aa4SChristopher S. Hall #define PTP_SYS_OFFSET_PRECISE \
219719f1aa4SChristopher S. Hall 	_IOWR(PTP_CLK_MAGIC, 8, struct ptp_sys_offset_precise)
22036180087SMiroslav Lichvar #define PTP_SYS_OFFSET_EXTENDED \
221b7ea4894SEugene Syromiatnikov 	_IOWR(PTP_CLK_MAGIC, 9, struct ptp_sys_offset_extended)
222607ca46eSDavid Howells 
22341560658SFelipe Balbi #define PTP_CLOCK_GETCAPS2  _IOR(PTP_CLK_MAGIC, 10, struct ptp_clock_caps)
22441560658SFelipe Balbi #define PTP_EXTTS_REQUEST2  _IOW(PTP_CLK_MAGIC, 11, struct ptp_extts_request)
22541560658SFelipe Balbi #define PTP_PEROUT_REQUEST2 _IOW(PTP_CLK_MAGIC, 12, struct ptp_perout_request)
22641560658SFelipe Balbi #define PTP_ENABLE_PPS2     _IOW(PTP_CLK_MAGIC, 13, int)
22741560658SFelipe Balbi #define PTP_SYS_OFFSET2     _IOW(PTP_CLK_MAGIC, 14, struct ptp_sys_offset)
22841560658SFelipe Balbi #define PTP_PIN_GETFUNC2    _IOWR(PTP_CLK_MAGIC, 15, struct ptp_pin_desc)
22941560658SFelipe Balbi #define PTP_PIN_SETFUNC2    _IOW(PTP_CLK_MAGIC, 16, struct ptp_pin_desc)
23041560658SFelipe Balbi #define PTP_SYS_OFFSET_PRECISE2 \
23141560658SFelipe Balbi 	_IOWR(PTP_CLK_MAGIC, 17, struct ptp_sys_offset_precise)
23241560658SFelipe Balbi #define PTP_SYS_OFFSET_EXTENDED2 \
23341560658SFelipe Balbi 	_IOWR(PTP_CLK_MAGIC, 18, struct ptp_sys_offset_extended)
234c5a445b1SXabier Marquiegui #define PTP_MASK_CLEAR_ALL  _IO(PTP_CLK_MAGIC, 19)
235c5a445b1SXabier Marquiegui #define PTP_MASK_EN_SINGLE  _IOW(PTP_CLK_MAGIC, 20, unsigned int)
23641560658SFelipe Balbi 
237607ca46eSDavid Howells struct ptp_extts_event {
238*ea1cc3eeSMin Li 	struct ptp_clock_time t; /* Time event occurred. */
239607ca46eSDavid Howells 	unsigned int index;      /* Which channel produced the event. */
240*ea1cc3eeSMin Li 	unsigned int flags;      /* Event type. */
241607ca46eSDavid Howells 	unsigned int rsv[2];     /* Reserved for future use. */
242607ca46eSDavid Howells };
243607ca46eSDavid Howells 
244607ca46eSDavid Howells #endif
245