blob: 54bf1484d41f0333cc860c84307d23eda76477e5 [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>
25
26#include <linux/cdev.h>
27#include <linux/device.h>
28#include <linux/time.h>
29
30/*
31 * Global defines
32 */
33
Alexander Gordeev5e196d32011-01-12 17:00:51 -080034struct pps_device;
35
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070036/* The specific PPS source info */
37struct pps_source_info {
38 char name[PPS_MAX_NAME_LEN]; /* simbolic name */
39 char path[PPS_MAX_NAME_LEN]; /* path of connected device */
40 int mode; /* PPS's allowed mode */
41
Alexander Gordeev5e196d32011-01-12 17:00:51 -080042 void (*echo)(struct pps_device *pps,
43 int event, void *data); /* PPS echo function */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070044
45 struct module *owner;
George Spelvin513b0322013-02-10 04:08:32 -050046 struct device *dev; /* Parent device for device_create */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070047};
48
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080049struct pps_event_time {
Alexander Gordeeve2c18e42011-01-12 17:00:57 -080050#ifdef CONFIG_NTP_PPS
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020051 struct timespec64 ts_raw;
Alexander Gordeeve2c18e42011-01-12 17:00:57 -080052#endif /* CONFIG_NTP_PPS */
Arnd Bergmannade1bdf2015-09-28 22:21:31 +020053 struct timespec64 ts_real;
Alexander Gordeev6f4229b2011-01-12 17:00:50 -080054};
55
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070056/* The main struct */
57struct pps_device {
58 struct pps_source_info info; /* PSS source info */
59
60 struct pps_kparams params; /* PPS's current params */
61
62 __u32 assert_sequence; /* PPS' assert event seq # */
63 __u32 clear_sequence; /* PPS' clear event seq # */
64 struct pps_ktime assert_tu;
65 struct pps_ktime clear_tu;
66 int current_mode; /* PPS mode at event time */
67
Alexander Gordeev3003d552011-01-12 17:00:50 -080068 unsigned int last_ev; /* last PPS event id */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070069 wait_queue_head_t queue; /* PPS event queue */
70
71 unsigned int id; /* PPS source unique ID */
George Spelvin513b0322013-02-10 04:08:32 -050072 void const *lookup_cookie; /* pps_lookup_dev only */
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070073 struct cdev cdev;
74 struct device *dev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070075 struct fasync_struct *async_queue; /* fasync method */
76 spinlock_t lock;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070077};
78
79/*
80 * Global variables
81 */
82
Greg Kroah-Hartmanbd0eae42013-07-24 15:05:19 -070083extern const struct attribute_group *pps_groups[];
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070084
85/*
George Spelvin513b0322013-02-10 04:08:32 -050086 * Internal functions.
87 *
88 * These are not actually part of the exported API, but this is a
89 * convenient header file to put them in.
90 */
91
92extern int pps_register_cdev(struct pps_device *pps);
93extern void pps_unregister_cdev(struct pps_device *pps);
94
95/*
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070096 * Exported functions
97 */
98
Alexander Gordeev5e196d32011-01-12 17:00:51 -080099extern struct pps_device *pps_register_source(
100 struct pps_source_info *info, int default_params);
101extern void pps_unregister_source(struct pps_device *pps);
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800102extern void pps_event(struct pps_device *pps,
103 struct pps_event_time *ts, int event, void *data);
George Spelvin513b0322013-02-10 04:08:32 -0500104/* Look up a pps device by magic cookie */
105struct pps_device *pps_lookup_dev(void const *cookie);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800106
107static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200108 struct timespec64 ts)
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800109{
110 kt->sec = ts.tv_sec;
111 kt->nsec = ts.tv_nsec;
112}
113
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800114#ifdef CONFIG_NTP_PPS
115
116static inline void pps_get_ts(struct pps_event_time *ts)
117{
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200118 ktime_get_raw_and_real_ts64(&ts->ts_raw, &ts->ts_real);
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800119}
120
121#else /* CONFIG_NTP_PPS */
122
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800123static inline void pps_get_ts(struct pps_event_time *ts)
124{
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200125 ktime_get_real_ts64(&ts->ts_real);
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800126}
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800127
Alexander Gordeeve2c18e42011-01-12 17:00:57 -0800128#endif /* CONFIG_NTP_PPS */
129
Ben Hutchings220a60a2012-09-03 11:34:58 +0100130/* Subtract known time delay from PPS event time(s) */
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200131static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta)
Ben Hutchings220a60a2012-09-03 11:34:58 +0100132{
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200133 ts->ts_real = timespec64_sub(ts->ts_real, delta);
Ben Hutchings220a60a2012-09-03 11:34:58 +0100134#ifdef CONFIG_NTP_PPS
Arnd Bergmannade1bdf2015-09-28 22:21:31 +0200135 ts->ts_raw = timespec64_sub(ts->ts_raw, delta);
Ben Hutchings220a60a2012-09-03 11:34:58 +0100136#endif
137}
138
Alexander Gordeev7a21a3c2011-01-12 17:00:49 -0800139#endif /* LINUX_PPS_KERNEL_H */
Alexander Gordeev6f4229b2011-01-12 17:00:50 -0800140