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