xref: /linux-6.15/include/linux/pps_kernel.h (revision a2d81803)
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 #include <linux/cdev.h>
26eae9d2baSRodolfo Giometti #include <linux/device.h>
27eae9d2baSRodolfo Giometti #include <linux/time.h>
28eae9d2baSRodolfo Giometti 
29eae9d2baSRodolfo Giometti /*
30eae9d2baSRodolfo Giometti  * Global defines
31eae9d2baSRodolfo Giometti  */
32eae9d2baSRodolfo Giometti 
335e196d34SAlexander Gordeev struct pps_device;
345e196d34SAlexander Gordeev 
35eae9d2baSRodolfo Giometti /* The specific PPS source info */
36eae9d2baSRodolfo Giometti struct pps_source_info {
37*a2d81803SRobert P. J. Day 	char name[PPS_MAX_NAME_LEN];		/* symbolic name */
38eae9d2baSRodolfo Giometti 	char path[PPS_MAX_NAME_LEN];		/* path of connected device */
39*a2d81803SRobert P. J. Day 	int mode;				/* PPS allowed mode */
40eae9d2baSRodolfo Giometti 
415e196d34SAlexander Gordeev 	void (*echo)(struct pps_device *pps,
425e196d34SAlexander Gordeev 			int event, void *data);	/* PPS echo function */
43eae9d2baSRodolfo Giometti 
44eae9d2baSRodolfo Giometti 	struct module *owner;
45513b032cSGeorge Spelvin 	struct device *dev;		/* Parent device for device_create */
46eae9d2baSRodolfo Giometti };
47eae9d2baSRodolfo Giometti 
486f4229b5SAlexander Gordeev struct pps_event_time {
49e2c18e49SAlexander Gordeev #ifdef CONFIG_NTP_PPS
50ade1bdffSArnd Bergmann 	struct timespec64 ts_raw;
51e2c18e49SAlexander Gordeev #endif /* CONFIG_NTP_PPS */
52ade1bdffSArnd Bergmann 	struct timespec64 ts_real;
536f4229b5SAlexander Gordeev };
546f4229b5SAlexander Gordeev 
55eae9d2baSRodolfo Giometti /* The main struct */
56eae9d2baSRodolfo Giometti struct pps_device {
57eae9d2baSRodolfo Giometti 	struct pps_source_info info;		/* PSS source info */
58eae9d2baSRodolfo Giometti 
59*a2d81803SRobert P. J. Day 	struct pps_kparams params;		/* PPS current params */
60eae9d2baSRodolfo Giometti 
61*a2d81803SRobert P. J. Day 	__u32 assert_sequence;			/* PPS assert event seq # */
62*a2d81803SRobert P. J. Day 	__u32 clear_sequence;			/* PPS clear event seq # */
63eae9d2baSRodolfo Giometti 	struct pps_ktime assert_tu;
64eae9d2baSRodolfo Giometti 	struct pps_ktime clear_tu;
65eae9d2baSRodolfo Giometti 	int current_mode;			/* PPS mode at event time */
66eae9d2baSRodolfo Giometti 
673003d55bSAlexander Gordeev 	unsigned int last_ev;			/* last PPS event id */
68eae9d2baSRodolfo Giometti 	wait_queue_head_t queue;		/* PPS event queue */
69eae9d2baSRodolfo Giometti 
70eae9d2baSRodolfo Giometti 	unsigned int id;			/* PPS source unique ID */
71*a2d81803SRobert P. J. Day 	void const *lookup_cookie;		/* For pps_lookup_dev() only */
72eae9d2baSRodolfo Giometti 	struct cdev cdev;
73eae9d2baSRodolfo Giometti 	struct device *dev;
74eae9d2baSRodolfo Giometti 	struct fasync_struct *async_queue;	/* fasync method */
75eae9d2baSRodolfo Giometti 	spinlock_t lock;
76eae9d2baSRodolfo Giometti };
77eae9d2baSRodolfo Giometti 
78eae9d2baSRodolfo Giometti /*
79eae9d2baSRodolfo Giometti  * Global variables
80eae9d2baSRodolfo Giometti  */
81eae9d2baSRodolfo Giometti 
82bd0eae4eSGreg Kroah-Hartman extern const struct attribute_group *pps_groups[];
83eae9d2baSRodolfo Giometti 
84eae9d2baSRodolfo Giometti /*
85513b032cSGeorge Spelvin  * Internal functions.
86513b032cSGeorge Spelvin  *
87513b032cSGeorge Spelvin  * These are not actually part of the exported API, but this is a
88513b032cSGeorge Spelvin  * convenient header file to put them in.
89513b032cSGeorge Spelvin  */
90513b032cSGeorge Spelvin 
91513b032cSGeorge Spelvin extern int pps_register_cdev(struct pps_device *pps);
92513b032cSGeorge Spelvin extern void pps_unregister_cdev(struct pps_device *pps);
93513b032cSGeorge Spelvin 
94513b032cSGeorge Spelvin /*
95eae9d2baSRodolfo Giometti  * Exported functions
96eae9d2baSRodolfo Giometti  */
97eae9d2baSRodolfo Giometti 
985e196d34SAlexander Gordeev extern struct pps_device *pps_register_source(
995e196d34SAlexander Gordeev 		struct pps_source_info *info, int default_params);
1005e196d34SAlexander Gordeev extern void pps_unregister_source(struct pps_device *pps);
1015e196d34SAlexander Gordeev extern void pps_event(struct pps_device *pps,
1025e196d34SAlexander Gordeev 		struct pps_event_time *ts, int event, void *data);
103*a2d81803SRobert P. J. Day /* Look up a pps_device by magic cookie */
104513b032cSGeorge Spelvin struct pps_device *pps_lookup_dev(void const *cookie);
1056f4229b5SAlexander Gordeev 
1066f4229b5SAlexander Gordeev static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
107ade1bdffSArnd Bergmann 		struct timespec64 ts)
1086f4229b5SAlexander Gordeev {
1096f4229b5SAlexander Gordeev 	kt->sec = ts.tv_sec;
1106f4229b5SAlexander Gordeev 	kt->nsec = ts.tv_nsec;
1116f4229b5SAlexander Gordeev }
1126f4229b5SAlexander Gordeev 
113ba26621eSChristopher S. Hall static inline void pps_get_ts(struct pps_event_time *ts)
114ba26621eSChristopher S. Hall {
115ba26621eSChristopher S. Hall 	struct system_time_snapshot snap;
116ba26621eSChristopher S. Hall 
117ba26621eSChristopher S. Hall 	ktime_get_snapshot(&snap);
118ba26621eSChristopher S. Hall 	ts->ts_real = ktime_to_timespec64(snap.real);
119e2c18e49SAlexander Gordeev #ifdef CONFIG_NTP_PPS
120ba26621eSChristopher S. Hall 	ts->ts_raw = ktime_to_timespec64(snap.raw);
121ba26621eSChristopher S. Hall #endif
122e2c18e49SAlexander Gordeev }
123e2c18e49SAlexander Gordeev 
124220a60a4SBen Hutchings /* Subtract known time delay from PPS event time(s) */
125ade1bdffSArnd Bergmann static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta)
126220a60a4SBen Hutchings {
127ade1bdffSArnd Bergmann 	ts->ts_real = timespec64_sub(ts->ts_real, delta);
128220a60a4SBen Hutchings #ifdef CONFIG_NTP_PPS
129ade1bdffSArnd Bergmann 	ts->ts_raw = timespec64_sub(ts->ts_raw, delta);
130220a60a4SBen Hutchings #endif
131220a60a4SBen Hutchings }
132220a60a4SBen Hutchings 
1337a21a3ccSAlexander Gordeev #endif /* LINUX_PPS_KERNEL_H */
134