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