xref: /openbmc/linux/include/uapi/linux/ptp_clock.h (revision 657c45b3)
1 /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2 /*
3  * PTP 1588 clock support - user space interface
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #ifndef _PTP_CLOCK_H_
23 #define _PTP_CLOCK_H_
24 
25 #include <linux/ioctl.h>
26 #include <linux/types.h>
27 
28 /*
29  * Bits of the ptp_extts_request.flags field:
30  */
31 #define PTP_ENABLE_FEATURE (1<<0)
32 #define PTP_RISING_EDGE    (1<<1)
33 #define PTP_FALLING_EDGE   (1<<2)
34 #define PTP_STRICT_FLAGS   (1<<3)
35 #define PTP_EXTTS_EDGES    (PTP_RISING_EDGE | PTP_FALLING_EDGE)
36 
37 /*
38  * flag fields valid for the new PTP_EXTTS_REQUEST2 ioctl.
39  */
40 #define PTP_EXTTS_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
41 				 PTP_RISING_EDGE |	\
42 				 PTP_FALLING_EDGE |	\
43 				 PTP_STRICT_FLAGS)
44 
45 /*
46  * flag fields valid for the original PTP_EXTTS_REQUEST ioctl.
47  * DO NOT ADD NEW FLAGS HERE.
48  */
49 #define PTP_EXTTS_V1_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
50 					 PTP_RISING_EDGE |	\
51 					 PTP_FALLING_EDGE)
52 
53 /*
54  * Bits of the ptp_perout_request.flags field:
55  */
56 #define PTP_PEROUT_ONE_SHOT		(1<<0)
57 #define PTP_PEROUT_DUTY_CYCLE		(1<<1)
58 #define PTP_PEROUT_PHASE		(1<<2)
59 
60 /*
61  * flag fields valid for the new PTP_PEROUT_REQUEST2 ioctl.
62  */
63 #define PTP_PEROUT_VALID_FLAGS		(PTP_PEROUT_ONE_SHOT | \
64 					 PTP_PEROUT_DUTY_CYCLE | \
65 					 PTP_PEROUT_PHASE)
66 
67 /*
68  * No flags are valid for the original PTP_PEROUT_REQUEST ioctl
69  */
70 #define PTP_PEROUT_V1_VALID_FLAGS	(0)
71 
72 /*
73  * struct ptp_clock_time - represents a time value
74  *
75  * The sign of the seconds field applies to the whole value. The
76  * nanoseconds field is always unsigned. The reserved field is
77  * included for sub-nanosecond resolution, should the demand for
78  * this ever appear.
79  *
80  */
81 struct ptp_clock_time {
82 	__s64 sec;  /* seconds */
83 	__u32 nsec; /* nanoseconds */
84 	__u32 reserved;
85 };
86 
87 struct ptp_clock_caps {
88 	int max_adj;   /* Maximum frequency adjustment in parts per billon. */
89 	int n_alarm;   /* Number of programmable alarms. */
90 	int n_ext_ts;  /* Number of external time stamp channels. */
91 	int n_per_out; /* Number of programmable periodic signals. */
92 	int pps;       /* Whether the clock supports a PPS callback. */
93 	int n_pins;    /* Number of input/output pins. */
94 	/* Whether the clock supports precise system-device cross timestamps */
95 	int cross_timestamping;
96 	/* Whether the clock supports adjust phase */
97 	int adjust_phase;
98 	int max_phase_adj; /* Maximum phase adjustment in nanoseconds. */
99 	int rsv[11];       /* Reserved for future use. */
100 };
101 
102 struct ptp_extts_request {
103 	unsigned int index;  /* Which channel to configure. */
104 	unsigned int flags;  /* Bit field for PTP_xxx flags. */
105 	unsigned int rsv[2]; /* Reserved for future use. */
106 };
107 
108 struct ptp_perout_request {
109 	union {
110 		/*
111 		 * Absolute start time.
112 		 * Valid only if (flags & PTP_PEROUT_PHASE) is unset.
113 		 */
114 		struct ptp_clock_time start;
115 		/*
116 		 * Phase offset. The signal should start toggling at an
117 		 * unspecified integer multiple of the period, plus this value.
118 		 * The start time should be "as soon as possible".
119 		 * Valid only if (flags & PTP_PEROUT_PHASE) is set.
120 		 */
121 		struct ptp_clock_time phase;
122 	};
123 	struct ptp_clock_time period; /* Desired period, zero means disable. */
124 	unsigned int index;           /* Which channel to configure. */
125 	unsigned int flags;
126 	union {
127 		/*
128 		 * The "on" time of the signal.
129 		 * Must be lower than the period.
130 		 * Valid only if (flags & PTP_PEROUT_DUTY_CYCLE) is set.
131 		 */
132 		struct ptp_clock_time on;
133 		/* Reserved for future use. */
134 		unsigned int rsv[4];
135 	};
136 };
137 
138 #define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */
139 
140 struct ptp_sys_offset {
141 	unsigned int n_samples; /* Desired number of measurements. */
142 	unsigned int rsv[3];    /* Reserved for future use. */
143 	/*
144 	 * Array of interleaved system/phc time stamps. The kernel
145 	 * will provide 2*n_samples + 1 time stamps, with the last
146 	 * one as a system time stamp.
147 	 */
148 	struct ptp_clock_time ts[2 * PTP_MAX_SAMPLES + 1];
149 };
150 
151 struct ptp_sys_offset_extended {
152 	unsigned int n_samples; /* Desired number of measurements. */
153 	unsigned int rsv[3];    /* Reserved for future use. */
154 	/*
155 	 * Array of [system, phc, system] time stamps. The kernel will provide
156 	 * 3*n_samples time stamps.
157 	 */
158 	struct ptp_clock_time ts[PTP_MAX_SAMPLES][3];
159 };
160 
161 struct ptp_sys_offset_precise {
162 	struct ptp_clock_time device;
163 	struct ptp_clock_time sys_realtime;
164 	struct ptp_clock_time sys_monoraw;
165 	unsigned int rsv[4];    /* Reserved for future use. */
166 };
167 
168 enum ptp_pin_function {
169 	PTP_PF_NONE,
170 	PTP_PF_EXTTS,
171 	PTP_PF_PEROUT,
172 	PTP_PF_PHYSYNC,
173 };
174 
175 struct ptp_pin_desc {
176 	/*
177 	 * Hardware specific human readable pin name. This field is
178 	 * set by the kernel during the PTP_PIN_GETFUNC ioctl and is
179 	 * ignored for the PTP_PIN_SETFUNC ioctl.
180 	 */
181 	char name[64];
182 	/*
183 	 * Pin index in the range of zero to ptp_clock_caps.n_pins - 1.
184 	 */
185 	unsigned int index;
186 	/*
187 	 * Which of the PTP_PF_xxx functions to use on this pin.
188 	 */
189 	unsigned int func;
190 	/*
191 	 * The specific channel to use for this function.
192 	 * This corresponds to the 'index' field of the
193 	 * PTP_EXTTS_REQUEST and PTP_PEROUT_REQUEST ioctls.
194 	 */
195 	unsigned int chan;
196 	/*
197 	 * Reserved for future use.
198 	 */
199 	unsigned int rsv[5];
200 };
201 
202 #define PTP_CLK_MAGIC '='
203 
204 #define PTP_CLOCK_GETCAPS  _IOR(PTP_CLK_MAGIC, 1, struct ptp_clock_caps)
205 #define PTP_EXTTS_REQUEST  _IOW(PTP_CLK_MAGIC, 2, struct ptp_extts_request)
206 #define PTP_PEROUT_REQUEST _IOW(PTP_CLK_MAGIC, 3, struct ptp_perout_request)
207 #define PTP_ENABLE_PPS     _IOW(PTP_CLK_MAGIC, 4, int)
208 #define PTP_SYS_OFFSET     _IOW(PTP_CLK_MAGIC, 5, struct ptp_sys_offset)
209 #define PTP_PIN_GETFUNC    _IOWR(PTP_CLK_MAGIC, 6, struct ptp_pin_desc)
210 #define PTP_PIN_SETFUNC    _IOW(PTP_CLK_MAGIC, 7, struct ptp_pin_desc)
211 #define PTP_SYS_OFFSET_PRECISE \
212 	_IOWR(PTP_CLK_MAGIC, 8, struct ptp_sys_offset_precise)
213 #define PTP_SYS_OFFSET_EXTENDED \
214 	_IOWR(PTP_CLK_MAGIC, 9, struct ptp_sys_offset_extended)
215 
216 #define PTP_CLOCK_GETCAPS2  _IOR(PTP_CLK_MAGIC, 10, struct ptp_clock_caps)
217 #define PTP_EXTTS_REQUEST2  _IOW(PTP_CLK_MAGIC, 11, struct ptp_extts_request)
218 #define PTP_PEROUT_REQUEST2 _IOW(PTP_CLK_MAGIC, 12, struct ptp_perout_request)
219 #define PTP_ENABLE_PPS2     _IOW(PTP_CLK_MAGIC, 13, int)
220 #define PTP_SYS_OFFSET2     _IOW(PTP_CLK_MAGIC, 14, struct ptp_sys_offset)
221 #define PTP_PIN_GETFUNC2    _IOWR(PTP_CLK_MAGIC, 15, struct ptp_pin_desc)
222 #define PTP_PIN_SETFUNC2    _IOW(PTP_CLK_MAGIC, 16, struct ptp_pin_desc)
223 #define PTP_SYS_OFFSET_PRECISE2 \
224 	_IOWR(PTP_CLK_MAGIC, 17, struct ptp_sys_offset_precise)
225 #define PTP_SYS_OFFSET_EXTENDED2 \
226 	_IOWR(PTP_CLK_MAGIC, 18, struct ptp_sys_offset_extended)
227 
228 struct ptp_extts_event {
229 	struct ptp_clock_time t; /* Time event occured. */
230 	unsigned int index;      /* Which channel produced the event. */
231 	unsigned int flags;      /* Reserved for future use. */
232 	unsigned int rsv[2];     /* Reserved for future use. */
233 };
234 
235 #endif
236