blob: 78c8ac4951b581e551de0395eeae8ae573ea63f0 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -07002/*
3 * PPS API kernel header
4 *
5 * Copyright (C) 2009 Rodolfo Giometti <giometti@linux.it>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -07006 */
7
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -08008#ifndef LINUX_PPS_KERNEL_H
9#define LINUX_PPS_KERNEL_H
10
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070011#include <linux/pps.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070012#include <linux/cdev.h>
13#include <linux/device.h>
14#include <linux/time.h>
15
16/*
17 * Global defines
18 */
19
Alexander Gordeev5e196d32011-01-12 17:00:51 -080020struct pps_device;
21
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070022/* The specific PPS source info */
23struct pps_source_info {
Robert P. J. Daya2d81802017-09-08 16:17:19 -070024 char name[PPS_MAX_NAME_LEN]; /* symbolic name */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070025 char path[PPS_MAX_NAME_LEN]; /* path of connected device */
Robert P. J. Daya2d81802017-09-08 16:17:19 -070026 int mode; /* PPS allowed mode */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070027
Alexander Gordeev5e196d32011-01-12 17:00:51 -080028 void (*echo)(struct pps_device *pps,
29 int event, void *data); /* PPS echo function */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070030
31 struct module *owner;
George Spelvin513b0322013-02-10 04:08:32 -050032 struct device *dev; /* Parent device for device_create */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070033};
34
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080035struct pps_event_time {
Alexander Gordeeve2c18e42011-01-12 17:00:57 -080036#ifdef CONFIG_NTP_PPS
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020037 struct timespec64 ts_raw;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -080038#endif /* CONFIG_NTP_PPS */
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020039 struct timespec64 ts_real;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080040};
41
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070042/* The main struct */
43struct pps_device {
44 struct pps_source_info info; /* PSS source info */
45
Robert P. J. Daya2d81802017-09-08 16:17:19 -070046 struct pps_kparams params; /* PPS current params */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070047
Robert P. J. Daya2d81802017-09-08 16:17:19 -070048 __u32 assert_sequence; /* PPS assert event seq # */
49 __u32 clear_sequence; /* PPS clear event seq # */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070050 struct pps_ktime assert_tu;
51 struct pps_ktime clear_tu;
52 int current_mode; /* PPS mode at event time */
53
Alexander Gordeev3003d552011-01-12 17:00:50 -080054 unsigned int last_ev; /* last PPS event id */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070055 wait_queue_head_t queue; /* PPS event queue */
56
57 unsigned int id; /* PPS source unique ID */
Robert P. J. Daya2d81802017-09-08 16:17:19 -070058 void const *lookup_cookie; /* For pps_lookup_dev() only */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070059 struct cdev cdev;
60 struct device *dev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070061 struct fasync_struct *async_queue; /* fasync method */
62 spinlock_t lock;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070063};
64
65/*
66 * Global variables
67 */
68
Greg Kroah-Hartmanbd0eae42013-07-24 15:05:19 -070069extern const struct attribute_group *pps_groups[];
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070070
71/*
George Spelvin513b0322013-02-10 04:08:32 -050072 * Internal functions.
73 *
74 * These are not actually part of the exported API, but this is a
75 * convenient header file to put them in.
76 */
77
78extern int pps_register_cdev(struct pps_device *pps);
79extern void pps_unregister_cdev(struct pps_device *pps);
80
81/*
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070082 * Exported functions
83 */
84
Alexander Gordeev5e196d32011-01-12 17:00:51 -080085extern struct pps_device *pps_register_source(
86 struct pps_source_info *info, int default_params);
87extern void pps_unregister_source(struct pps_device *pps);
Alexander Gordeev5e196d32011-01-12 17:00:51 -080088extern void pps_event(struct pps_device *pps,
89 struct pps_event_time *ts, int event, void *data);
Robert P. J. Daya2d81802017-09-08 16:17:19 -070090/* Look up a pps_device by magic cookie */
George Spelvin513b0322013-02-10 04:08:32 -050091struct pps_device *pps_lookup_dev(void const *cookie);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080092
93static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020094 struct timespec64 ts)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080095{
96 kt->sec = ts.tv_sec;
97 kt->nsec = ts.tv_nsec;
98}
99
Christopher S. Hallba266212016-02-22 03:15:21 -0800100static inline void pps_get_ts(struct pps_event_time *ts)
101{
102 struct system_time_snapshot snap;
103
104 ktime_get_snapshot(&snap);
105 ts->ts_real = ktime_to_timespec64(snap.real);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800106#ifdef CONFIG_NTP_PPS
Christopher S. Hallba266212016-02-22 03:15:21 -0800107 ts->ts_raw = ktime_to_timespec64(snap.raw);
108#endif
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800109}
110
Ben Hutchings220a60a2012-09-03 11:34:58 +0100111/* Subtract known time delay from PPS event time(s) */
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200112static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta)
Ben Hutchings220a60a2012-09-03 11:34:58 +0100113{
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200114 ts->ts_real = timespec64_sub(ts->ts_real, delta);
Ben Hutchings220a60a2012-09-03 11:34:58 +0100115#ifdef CONFIG_NTP_PPS
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200116 ts->ts_raw = timespec64_sub(ts->ts_raw, delta);
Ben Hutchings220a60a2012-09-03 11:34:58 +0100117#endif
118}
119
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800120#endif /* LINUX_PPS_KERNEL_H */