master
  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_EXT_OFFSET     (1<<4)
 36#define PTP_EXTTS_EDGES    (PTP_RISING_EDGE | PTP_FALLING_EDGE)
 37
 38/*
 39 * flag fields valid for the new PTP_EXTTS_REQUEST2 ioctl.
 40 *
 41 * Note: PTP_STRICT_FLAGS is always enabled by the kernel for
 42 * PTP_EXTTS_REQUEST2 regardless of whether it is set by userspace.
 43 */
 44#define PTP_EXTTS_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
 45				 PTP_RISING_EDGE |	\
 46				 PTP_FALLING_EDGE |	\
 47				 PTP_STRICT_FLAGS |	\
 48				 PTP_EXT_OFFSET)
 49
 50/*
 51 * flag fields valid for the original PTP_EXTTS_REQUEST ioctl.
 52 * DO NOT ADD NEW FLAGS HERE.
 53 */
 54#define PTP_EXTTS_V1_VALID_FLAGS	(PTP_ENABLE_FEATURE |	\
 55					 PTP_RISING_EDGE |	\
 56					 PTP_FALLING_EDGE)
 57
 58/*
 59 * flag fields valid for the ptp_extts_event report.
 60 */
 61#define PTP_EXTTS_EVENT_VALID	(PTP_ENABLE_FEATURE)
 62
 63/*
 64 * Bits of the ptp_perout_request.flags field:
 65 */
 66#define PTP_PEROUT_ONE_SHOT		(1<<0)
 67#define PTP_PEROUT_DUTY_CYCLE		(1<<1)
 68#define PTP_PEROUT_PHASE		(1<<2)
 69
 70/*
 71 * flag fields valid for the new PTP_PEROUT_REQUEST2 ioctl.
 72 */
 73#define PTP_PEROUT_VALID_FLAGS		(PTP_PEROUT_ONE_SHOT | \
 74					 PTP_PEROUT_DUTY_CYCLE | \
 75					 PTP_PEROUT_PHASE)
 76
 77/*
 78 * No flags are valid for the original PTP_PEROUT_REQUEST ioctl
 79 */
 80#define PTP_PEROUT_V1_VALID_FLAGS	(0)
 81
 82/*
 83 * struct ptp_clock_time - represents a time value
 84 *
 85 * The sign of the seconds field applies to the whole value. The
 86 * nanoseconds field is always unsigned. The reserved field is
 87 * included for sub-nanosecond resolution, should the demand for
 88 * this ever appear.
 89 *
 90 */
 91struct ptp_clock_time {
 92	__s64 sec;  /* seconds */
 93	__u32 nsec; /* nanoseconds */
 94	__u32 reserved;
 95};
 96
 97struct ptp_clock_caps {
 98	int max_adj;   /* Maximum frequency adjustment in parts per billon. */
 99	int n_alarm;   /* Number of programmable alarms. */
100	int n_ext_ts;  /* Number of external time stamp channels. */
101	int n_per_out; /* Number of programmable periodic signals. */
102	int pps;       /* Whether the clock supports a PPS callback. */
103	int n_pins;    /* Number of input/output pins. */
104	/* Whether the clock supports precise system-device cross timestamps */
105	int cross_timestamping;
106	/* Whether the clock supports adjust phase */
107	int adjust_phase;
108	int max_phase_adj; /* Maximum phase adjustment in nanoseconds. */
109	int rsv[11];       /* Reserved for future use. */
110};
111
112struct ptp_extts_request {
113	unsigned int index;  /* Which channel to configure. */
114	unsigned int flags;  /* Bit field for PTP_xxx flags. */
115	unsigned int rsv[2]; /* Reserved for future use. */
116};
117
118struct ptp_perout_request {
119	union {
120		/*
121		 * Absolute start time.
122		 * Valid only if (flags & PTP_PEROUT_PHASE) is unset.
123		 */
124		struct ptp_clock_time start;
125		/*
126		 * Phase offset. The signal should start toggling at an
127		 * unspecified integer multiple of the period, plus this value.
128		 * The start time should be "as soon as possible".
129		 * Valid only if (flags & PTP_PEROUT_PHASE) is set.
130		 */
131		struct ptp_clock_time phase;
132	};
133	struct ptp_clock_time period; /* Desired period, zero means disable. */
134	unsigned int index;           /* Which channel to configure. */
135	unsigned int flags;
136	union {
137		/*
138		 * The "on" time of the signal.
139		 * Must be lower than the period.
140		 * Valid only if (flags & PTP_PEROUT_DUTY_CYCLE) is set.
141		 */
142		struct ptp_clock_time on;
143		/* Reserved for future use. */
144		unsigned int rsv[4];
145	};
146};
147
148#define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */
149
150struct ptp_sys_offset {
151	unsigned int n_samples; /* Desired number of measurements. */
152	unsigned int rsv[3];    /* Reserved for future use. */
153	/*
154	 * Array of interleaved system/phc time stamps. The kernel
155	 * will provide 2*n_samples + 1 time stamps, with the last
156	 * one as a system time stamp.
157	 */
158	struct ptp_clock_time ts[2 * PTP_MAX_SAMPLES + 1];
159};
160
161/*
162 * ptp_sys_offset_extended - data structure for IOCTL operation
163 *			     PTP_SYS_OFFSET_EXTENDED
164 *
165 * @n_samples:	Desired number of measurements.
166 * @clockid:	clockid of a clock-base used for pre/post timestamps.
167 * @rsv:	Reserved for future use.
168 * @ts:		Array of samples in the form [pre-TS, PHC, post-TS]. The
169 *		kernel provides @n_samples.
170 *
171 * Starting from kernel 6.12 and onwards, the first word of the reserved-field
172 * is used for @clockid. That's backward compatible since previous kernel
173 * expect all three reserved words (@rsv[3]) to be 0 while the clockid (first
174 * word in the new structure) for CLOCK_REALTIME is '0'.
175 */
176struct ptp_sys_offset_extended {
177	unsigned int n_samples;
178	__kernel_clockid_t clockid;
179	unsigned int rsv[2];
180	struct ptp_clock_time ts[PTP_MAX_SAMPLES][3];
181};
182
183struct ptp_sys_offset_precise {
184	struct ptp_clock_time device;
185	struct ptp_clock_time sys_realtime;
186	struct ptp_clock_time sys_monoraw;
187	unsigned int rsv[4];    /* Reserved for future use. */
188};
189
190enum ptp_pin_function {
191	PTP_PF_NONE,
192	PTP_PF_EXTTS,
193	PTP_PF_PEROUT,
194	PTP_PF_PHYSYNC,
195};
196
197struct ptp_pin_desc {
198	/*
199	 * Hardware specific human readable pin name. This field is
200	 * set by the kernel during the PTP_PIN_GETFUNC ioctl and is
201	 * ignored for the PTP_PIN_SETFUNC ioctl.
202	 */
203	char name[64];
204	/*
205	 * Pin index in the range of zero to ptp_clock_caps.n_pins - 1.
206	 */
207	unsigned int index;
208	/*
209	 * Which of the PTP_PF_xxx functions to use on this pin.
210	 */
211	unsigned int func;
212	/*
213	 * The specific channel to use for this function.
214	 * This corresponds to the 'index' field of the
215	 * PTP_EXTTS_REQUEST and PTP_PEROUT_REQUEST ioctls.
216	 */
217	unsigned int chan;
218	/*
219	 * Reserved for future use.
220	 */
221	unsigned int rsv[5];
222};
223
224#define PTP_CLK_MAGIC '='
225
226#define PTP_CLOCK_GETCAPS  _IOR(PTP_CLK_MAGIC, 1, struct ptp_clock_caps)
227#define PTP_EXTTS_REQUEST  _IOW(PTP_CLK_MAGIC, 2, struct ptp_extts_request)
228#define PTP_PEROUT_REQUEST _IOW(PTP_CLK_MAGIC, 3, struct ptp_perout_request)
229#define PTP_ENABLE_PPS     _IOW(PTP_CLK_MAGIC, 4, int)
230#define PTP_SYS_OFFSET     _IOW(PTP_CLK_MAGIC, 5, struct ptp_sys_offset)
231#define PTP_PIN_GETFUNC    _IOWR(PTP_CLK_MAGIC, 6, struct ptp_pin_desc)
232#define PTP_PIN_SETFUNC    _IOW(PTP_CLK_MAGIC, 7, struct ptp_pin_desc)
233#define PTP_SYS_OFFSET_PRECISE \
234	_IOWR(PTP_CLK_MAGIC, 8, struct ptp_sys_offset_precise)
235#define PTP_SYS_OFFSET_EXTENDED \
236	_IOWR(PTP_CLK_MAGIC, 9, struct ptp_sys_offset_extended)
237
238#define PTP_CLOCK_GETCAPS2  _IOR(PTP_CLK_MAGIC, 10, struct ptp_clock_caps)
239#define PTP_EXTTS_REQUEST2  _IOW(PTP_CLK_MAGIC, 11, struct ptp_extts_request)
240#define PTP_PEROUT_REQUEST2 _IOW(PTP_CLK_MAGIC, 12, struct ptp_perout_request)
241#define PTP_ENABLE_PPS2     _IOW(PTP_CLK_MAGIC, 13, int)
242#define PTP_SYS_OFFSET2     _IOW(PTP_CLK_MAGIC, 14, struct ptp_sys_offset)
243#define PTP_PIN_GETFUNC2    _IOWR(PTP_CLK_MAGIC, 15, struct ptp_pin_desc)
244#define PTP_PIN_SETFUNC2    _IOW(PTP_CLK_MAGIC, 16, struct ptp_pin_desc)
245#define PTP_SYS_OFFSET_PRECISE2 \
246	_IOWR(PTP_CLK_MAGIC, 17, struct ptp_sys_offset_precise)
247#define PTP_SYS_OFFSET_EXTENDED2 \
248	_IOWR(PTP_CLK_MAGIC, 18, struct ptp_sys_offset_extended)
249#define PTP_MASK_CLEAR_ALL  _IO(PTP_CLK_MAGIC, 19)
250#define PTP_MASK_EN_SINGLE  _IOW(PTP_CLK_MAGIC, 20, unsigned int)
251
252struct ptp_extts_event {
253	struct ptp_clock_time t; /* Time event occurred. */
254	unsigned int index;      /* Which channel produced the event. */
255	unsigned int flags;      /* Event type. */
256	unsigned int rsv[2];     /* Reserved for future use. */
257};
258
259#endif