130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/*
230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * PTP 1588 clock support - user space interface
330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * Copyright (C) 2010 OMICRON electronics GmbH
530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  This program is free software; you can redistribute it and/or modify
730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  it under the terms of the GNU General Public License as published by
830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  the Free Software Foundation; either version 2 of the License, or
930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  (at your option) any later version.
1030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
1130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  This program is distributed in the hope that it will be useful,
1230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  GNU General Public License for more details.
1530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
1630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  You should have received a copy of the GNU General Public License
1730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  along with this program; if not, write to the Free Software
1830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
2030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
2130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#ifndef _PTP_CLOCK_H_
2230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define _PTP_CLOCK_H_
2330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
2430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/ioctl.h>
2530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#include <linux/types.h>
2630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
2730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/* PTP_xxx bits, for the flags field within the request structures. */
2830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_ENABLE_FEATURE (1<<0)
2930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_RISING_EDGE    (1<<1)
3030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_FALLING_EDGE   (1<<2)
3130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
3230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng/*
3330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * struct ptp_clock_time - represents a time value
3430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
3530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * The sign of the seconds field applies to the whole value. The
3630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * nanoseconds field is always unsigned. The reserved field is
3730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * included for sub-nanosecond resolution, should the demand for
3830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng * this ever appear.
3930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng *
4030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng */
4130692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct ptp_clock_time {
4230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__s64 sec;  /* seconds */
4330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 nsec; /* nanoseconds */
4430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	__u32 reserved;
4530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
4630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
4730692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct ptp_clock_caps {
4830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	int max_adj;   /* Maximum frequency adjustment in parts per billon. */
4930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	int n_alarm;   /* Number of programmable alarms. */
5030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	int n_ext_ts;  /* Number of external time stamp channels. */
5130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	int n_per_out; /* Number of programmable periodic signals. */
5230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	int pps;       /* Whether the clock supports a PPS callback. */
5330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	int rsv[15];   /* Reserved for future use. */
5430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
5530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
5630692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct ptp_extts_request {
5730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int index;  /* Which channel to configure. */
5830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int flags;  /* Bit field for PTP_xxx flags. */
5930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int rsv[2]; /* Reserved for future use. */
6030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
6130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
6230692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct ptp_perout_request {
6330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	struct ptp_clock_time start;  /* Absolute start time. */
6430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	struct ptp_clock_time period; /* Desired period, zero means disable. */
6530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int index;           /* Which channel to configure. */
6630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int flags;           /* Reserved for future use. */
6730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int rsv[4];          /* Reserved for future use. */
6830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
6930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
7030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */
7130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
7230692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct ptp_sys_offset {
7330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int n_samples; /* Desired number of measurements. */
7430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int rsv[3];    /* Reserved for future use. */
7530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	/*
7630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	 * Array of interleaved system/phc time stamps. The kernel
7730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	 * will provide 2*n_samples + 1 time stamps, with the last
7830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	 * one as a system time stamp.
7930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	 */
8030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	struct ptp_clock_time ts[2 * PTP_MAX_SAMPLES + 1];
8130692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
8230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
8330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_CLK_MAGIC '='
8430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
8530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_CLOCK_GETCAPS  _IOR(PTP_CLK_MAGIC, 1, struct ptp_clock_caps)
8630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_EXTTS_REQUEST  _IOW(PTP_CLK_MAGIC, 2, struct ptp_extts_request)
8730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_PEROUT_REQUEST _IOW(PTP_CLK_MAGIC, 3, struct ptp_perout_request)
8830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_ENABLE_PPS     _IOW(PTP_CLK_MAGIC, 4, int)
8930692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#define PTP_SYS_OFFSET     _IOW(PTP_CLK_MAGIC, 5, struct ptp_sys_offset)
9030692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
9130692c65c4174412c90e79489e98ab85c1a7412fBen Chengstruct ptp_extts_event {
9230692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	struct ptp_clock_time t; /* Time event occured. */
9330692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int index;      /* Which channel produced the event. */
9430692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int flags;      /* Reserved for future use. */
9530692c65c4174412c90e79489e98ab85c1a7412fBen Cheng	unsigned int rsv[2];     /* Reserved for future use. */
9630692c65c4174412c90e79489e98ab85c1a7412fBen Cheng};
9730692c65c4174412c90e79489e98ab85c1a7412fBen Cheng
9830692c65c4174412c90e79489e98ab85c1a7412fBen Cheng#endif
99