blob: 80a980cc8d95f174a120fbd1aece4bdf7e55315b [file] [log] [blame]
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -07001/*
2 * PPS API kernel header
3 *
4 * Copyright (C) 2009 Rodolfo Giometti <giometti@linux.it>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -080021#ifndef LINUX_PPS_KERNEL_H
22#define LINUX_PPS_KERNEL_H
23
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070024#include <linux/pps.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070025#include <linux/cdev.h>
26#include <linux/device.h>
27#include <linux/time.h>
28
29/*
30 * Global defines
31 */
32
Alexander Gordeev5e196d32011-01-12 17:00:51 -080033struct pps_device;
34
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070035/* The specific PPS source info */
36struct pps_source_info {
Robert P. J. Daya2d81802017-09-08 16:17:19 -070037 char name[PPS_MAX_NAME_LEN]; /* symbolic name */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070038 char path[PPS_MAX_NAME_LEN]; /* path of connected device */
Robert P. J. Daya2d81802017-09-08 16:17:19 -070039 int mode; /* PPS allowed mode */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070040
Alexander Gordeev5e196d32011-01-12 17:00:51 -080041 void (*echo)(struct pps_device *pps,
42 int event, void *data); /* PPS echo function */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070043
44 struct module *owner;
George Spelvin513b0322013-02-10 04:08:32 -050045 struct device *dev; /* Parent device for device_create */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070046};
47
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080048struct pps_event_time {
Alexander Gordeeve2c18e42011-01-12 17:00:57 -080049#ifdef CONFIG_NTP_PPS
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020050 struct timespec64 ts_raw;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -080051#endif /* CONFIG_NTP_PPS */
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020052 struct timespec64 ts_real;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080053};
54
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070055/* The main struct */
56struct pps_device {
57 struct pps_source_info info; /* PSS source info */
58
Robert P. J. Daya2d81802017-09-08 16:17:19 -070059 struct pps_kparams params; /* PPS current params */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070060
Robert P. J. Daya2d81802017-09-08 16:17:19 -070061 __u32 assert_sequence; /* PPS assert event seq # */
62 __u32 clear_sequence; /* PPS clear event seq # */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070063 struct pps_ktime assert_tu;
64 struct pps_ktime clear_tu;
65 int current_mode; /* PPS mode at event time */
66
Alexander Gordeev3003d552011-01-12 17:00:50 -080067 unsigned int last_ev; /* last PPS event id */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070068 wait_queue_head_t queue; /* PPS event queue */
69
70 unsigned int id; /* PPS source unique ID */
Robert P. J. Daya2d81802017-09-08 16:17:19 -070071 void const *lookup_cookie; /* For pps_lookup_dev() only */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070072 struct cdev cdev;
73 struct device *dev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070074 struct fasync_struct *async_queue; /* fasync method */
75 spinlock_t lock;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070076};
77
78/*
79 * Global variables
80 */
81
Greg Kroah-Hartmanbd0eae42013-07-24 15:05:19 -070082extern const struct attribute_group *pps_groups[];
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070083
84/*
George Spelvin513b0322013-02-10 04:08:32 -050085 * Internal functions.
86 *
87 * These are not actually part of the exported API, but this is a
88 * convenient header file to put them in.
89 */
90
91extern int pps_register_cdev(struct pps_device *pps);
92extern void pps_unregister_cdev(struct pps_device *pps);
93
94/*
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070095 * Exported functions
96 */
97
Alexander Gordeev5e196d32011-01-12 17:00:51 -080098extern struct pps_device *pps_register_source(
99 struct pps_source_info *info, int default_params);
100extern void pps_unregister_source(struct pps_device *pps);
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800101extern void pps_event(struct pps_device *pps,
102 struct pps_event_time *ts, int event, void *data);
Robert P. J. Daya2d81802017-09-08 16:17:19 -0700103/* Look up a pps_device by magic cookie */
George Spelvin513b0322013-02-10 04:08:32 -0500104struct pps_device *pps_lookup_dev(void const *cookie);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800105
106static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200107 struct timespec64 ts)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800108{
109 kt->sec = ts.tv_sec;
110 kt->nsec = ts.tv_nsec;
111}
112
Christopher S. Hallba266212016-02-22 03:15:21 -0800113static inline void pps_get_ts(struct pps_event_time *ts)
114{
115 struct system_time_snapshot snap;
116
117 ktime_get_snapshot(&snap);
118 ts->ts_real = ktime_to_timespec64(snap.real);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800119#ifdef CONFIG_NTP_PPS
Christopher S. Hallba266212016-02-22 03:15:21 -0800120 ts->ts_raw = ktime_to_timespec64(snap.raw);
121#endif
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800122}
123
Ben Hutchings220a60a2012-09-03 11:34:58 +0100124/* Subtract known time delay from PPS event time(s) */
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200125static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta)
Ben Hutchings220a60a2012-09-03 11:34:58 +0100126{
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200127 ts->ts_real = timespec64_sub(ts->ts_real, delta);
Ben Hutchings220a60a2012-09-03 11:34:58 +0100128#ifdef CONFIG_NTP_PPS
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200129 ts->ts_raw = timespec64_sub(ts->ts_raw, delta);
Ben Hutchings220a60a2012-09-03 11:34:58 +0100130#endif
131}
132
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800133#endif /* LINUX_PPS_KERNEL_H */