1eae9d2baSRodolfo Giometti /* 2eae9d2baSRodolfo Giometti * PPS API kernel header 3eae9d2baSRodolfo Giometti * 4eae9d2baSRodolfo Giometti * Copyright (C) 2009 Rodolfo Giometti <[email protected]> 5eae9d2baSRodolfo Giometti * 6eae9d2baSRodolfo Giometti * This program is free software; you can redistribute it and/or modify 7eae9d2baSRodolfo Giometti * it under the terms of the GNU General Public License as published by 8eae9d2baSRodolfo Giometti * the Free Software Foundation; either version 2 of the License, or 9eae9d2baSRodolfo Giometti * (at your option) any later version. 10eae9d2baSRodolfo Giometti * 11eae9d2baSRodolfo Giometti * This program is distributed in the hope that it will be useful, 12eae9d2baSRodolfo Giometti * but WITHOUT ANY WARRANTY; without even the implied warranty of 13eae9d2baSRodolfo Giometti * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14eae9d2baSRodolfo Giometti * GNU General Public License for more details. 15eae9d2baSRodolfo Giometti * 16eae9d2baSRodolfo Giometti * You should have received a copy of the GNU General Public License 17eae9d2baSRodolfo Giometti * along with this program; if not, write to the Free Software 18eae9d2baSRodolfo Giometti * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19eae9d2baSRodolfo Giometti */ 20eae9d2baSRodolfo Giometti 217a21a3ccSAlexander Gordeev #ifndef LINUX_PPS_KERNEL_H 227a21a3ccSAlexander Gordeev #define LINUX_PPS_KERNEL_H 237a21a3ccSAlexander Gordeev 24eae9d2baSRodolfo Giometti #include <linux/pps.h> 25eae9d2baSRodolfo Giometti 26eae9d2baSRodolfo Giometti #include <linux/cdev.h> 27eae9d2baSRodolfo Giometti #include <linux/device.h> 28eae9d2baSRodolfo Giometti #include <linux/time.h> 29eae9d2baSRodolfo Giometti 30eae9d2baSRodolfo Giometti /* 31eae9d2baSRodolfo Giometti * Global defines 32eae9d2baSRodolfo Giometti */ 33eae9d2baSRodolfo Giometti 345e196d34SAlexander Gordeev struct pps_device; 355e196d34SAlexander Gordeev 36eae9d2baSRodolfo Giometti /* The specific PPS source info */ 37eae9d2baSRodolfo Giometti struct pps_source_info { 38eae9d2baSRodolfo Giometti char name[PPS_MAX_NAME_LEN]; /* simbolic name */ 39eae9d2baSRodolfo Giometti char path[PPS_MAX_NAME_LEN]; /* path of connected device */ 40eae9d2baSRodolfo Giometti int mode; /* PPS's allowed mode */ 41eae9d2baSRodolfo Giometti 425e196d34SAlexander Gordeev void (*echo)(struct pps_device *pps, 435e196d34SAlexander Gordeev int event, void *data); /* PPS echo function */ 44eae9d2baSRodolfo Giometti 45eae9d2baSRodolfo Giometti struct module *owner; 46513b032cSGeorge Spelvin struct device *dev; /* Parent device for device_create */ 47eae9d2baSRodolfo Giometti }; 48eae9d2baSRodolfo Giometti 496f4229b5SAlexander Gordeev struct pps_event_time { 50e2c18e49SAlexander Gordeev #ifdef CONFIG_NTP_PPS 51ade1bdffSArnd Bergmann struct timespec64 ts_raw; 52e2c18e49SAlexander Gordeev #endif /* CONFIG_NTP_PPS */ 53ade1bdffSArnd Bergmann struct timespec64 ts_real; 546f4229b5SAlexander Gordeev }; 556f4229b5SAlexander Gordeev 56eae9d2baSRodolfo Giometti /* The main struct */ 57eae9d2baSRodolfo Giometti struct pps_device { 58eae9d2baSRodolfo Giometti struct pps_source_info info; /* PSS source info */ 59eae9d2baSRodolfo Giometti 60eae9d2baSRodolfo Giometti struct pps_kparams params; /* PPS's current params */ 61eae9d2baSRodolfo Giometti 62eae9d2baSRodolfo Giometti __u32 assert_sequence; /* PPS' assert event seq # */ 63eae9d2baSRodolfo Giometti __u32 clear_sequence; /* PPS' clear event seq # */ 64eae9d2baSRodolfo Giometti struct pps_ktime assert_tu; 65eae9d2baSRodolfo Giometti struct pps_ktime clear_tu; 66eae9d2baSRodolfo Giometti int current_mode; /* PPS mode at event time */ 67eae9d2baSRodolfo Giometti 683003d55bSAlexander Gordeev unsigned int last_ev; /* last PPS event id */ 69eae9d2baSRodolfo Giometti wait_queue_head_t queue; /* PPS event queue */ 70eae9d2baSRodolfo Giometti 71eae9d2baSRodolfo Giometti unsigned int id; /* PPS source unique ID */ 72513b032cSGeorge Spelvin void const *lookup_cookie; /* pps_lookup_dev only */ 73eae9d2baSRodolfo Giometti struct cdev cdev; 74eae9d2baSRodolfo Giometti struct device *dev; 75eae9d2baSRodolfo Giometti struct fasync_struct *async_queue; /* fasync method */ 76eae9d2baSRodolfo Giometti spinlock_t lock; 77eae9d2baSRodolfo Giometti }; 78eae9d2baSRodolfo Giometti 79eae9d2baSRodolfo Giometti /* 80eae9d2baSRodolfo Giometti * Global variables 81eae9d2baSRodolfo Giometti */ 82eae9d2baSRodolfo Giometti 83bd0eae4eSGreg Kroah-Hartman extern const struct attribute_group *pps_groups[]; 84eae9d2baSRodolfo Giometti 85eae9d2baSRodolfo Giometti /* 86513b032cSGeorge Spelvin * Internal functions. 87513b032cSGeorge Spelvin * 88513b032cSGeorge Spelvin * These are not actually part of the exported API, but this is a 89513b032cSGeorge Spelvin * convenient header file to put them in. 90513b032cSGeorge Spelvin */ 91513b032cSGeorge Spelvin 92513b032cSGeorge Spelvin extern int pps_register_cdev(struct pps_device *pps); 93513b032cSGeorge Spelvin extern void pps_unregister_cdev(struct pps_device *pps); 94513b032cSGeorge Spelvin 95513b032cSGeorge Spelvin /* 96eae9d2baSRodolfo Giometti * Exported functions 97eae9d2baSRodolfo Giometti */ 98eae9d2baSRodolfo Giometti 995e196d34SAlexander Gordeev extern struct pps_device *pps_register_source( 1005e196d34SAlexander Gordeev struct pps_source_info *info, int default_params); 1015e196d34SAlexander Gordeev extern void pps_unregister_source(struct pps_device *pps); 1025e196d34SAlexander Gordeev extern void pps_event(struct pps_device *pps, 1035e196d34SAlexander Gordeev struct pps_event_time *ts, int event, void *data); 104513b032cSGeorge Spelvin /* Look up a pps device by magic cookie */ 105513b032cSGeorge Spelvin struct pps_device *pps_lookup_dev(void const *cookie); 1066f4229b5SAlexander Gordeev 1076f4229b5SAlexander Gordeev static inline void timespec_to_pps_ktime(struct pps_ktime *kt, 108ade1bdffSArnd Bergmann struct timespec64 ts) 1096f4229b5SAlexander Gordeev { 1106f4229b5SAlexander Gordeev kt->sec = ts.tv_sec; 1116f4229b5SAlexander Gordeev kt->nsec = ts.tv_nsec; 1126f4229b5SAlexander Gordeev } 1136f4229b5SAlexander Gordeev 114*ba26621eSChristopher S. Hall static inline void pps_get_ts(struct pps_event_time *ts) 115*ba26621eSChristopher S. Hall { 116*ba26621eSChristopher S. Hall struct system_time_snapshot snap; 117*ba26621eSChristopher S. Hall 118*ba26621eSChristopher S. Hall ktime_get_snapshot(&snap); 119*ba26621eSChristopher S. Hall ts->ts_real = ktime_to_timespec64(snap.real); 120e2c18e49SAlexander Gordeev #ifdef CONFIG_NTP_PPS 121*ba26621eSChristopher S. Hall ts->ts_raw = ktime_to_timespec64(snap.raw); 122*ba26621eSChristopher S. Hall #endif 123e2c18e49SAlexander Gordeev } 124e2c18e49SAlexander Gordeev 125220a60a4SBen Hutchings /* Subtract known time delay from PPS event time(s) */ 126ade1bdffSArnd Bergmann static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta) 127220a60a4SBen Hutchings { 128ade1bdffSArnd Bergmann ts->ts_real = timespec64_sub(ts->ts_real, delta); 129220a60a4SBen Hutchings #ifdef CONFIG_NTP_PPS 130ade1bdffSArnd Bergmann ts->ts_raw = timespec64_sub(ts->ts_raw, delta); 131220a60a4SBen Hutchings #endif 132220a60a4SBen Hutchings } 133220a60a4SBen Hutchings 1347a21a3ccSAlexander Gordeev #endif /* LINUX_PPS_KERNEL_H */ 1356f4229b5SAlexander Gordeev 136