174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2eae9d2baSRodolfo Giometti /*
3eae9d2baSRodolfo Giometti * kernel API
4eae9d2baSRodolfo Giometti *
5eae9d2baSRodolfo Giometti * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
6eae9d2baSRodolfo Giometti */
7eae9d2baSRodolfo Giometti
87f7cce74SAlexander Gordeev #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9eae9d2baSRodolfo Giometti
10eae9d2baSRodolfo Giometti #include <linux/kernel.h>
11eae9d2baSRodolfo Giometti #include <linux/module.h>
12eae9d2baSRodolfo Giometti #include <linux/init.h>
13eae9d2baSRodolfo Giometti #include <linux/sched.h>
14eae9d2baSRodolfo Giometti #include <linux/time.h>
15717c0336SAlexander Gordeev #include <linux/timex.h>
16eae9d2baSRodolfo Giometti #include <linux/spinlock.h>
17eae9d2baSRodolfo Giometti #include <linux/fs.h>
18eae9d2baSRodolfo Giometti #include <linux/pps_kernel.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20eae9d2baSRodolfo Giometti
21717c0336SAlexander Gordeev #include "kc.h"
22717c0336SAlexander Gordeev
23eae9d2baSRodolfo Giometti /*
24eae9d2baSRodolfo Giometti * Local functions
25eae9d2baSRodolfo Giometti */
26eae9d2baSRodolfo Giometti
pps_add_offset(struct pps_ktime * ts,struct pps_ktime * offset)27eae9d2baSRodolfo Giometti static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
28eae9d2baSRodolfo Giometti {
29eae9d2baSRodolfo Giometti ts->nsec += offset->nsec;
30eae9d2baSRodolfo Giometti while (ts->nsec >= NSEC_PER_SEC) {
31eae9d2baSRodolfo Giometti ts->nsec -= NSEC_PER_SEC;
32eae9d2baSRodolfo Giometti ts->sec++;
33eae9d2baSRodolfo Giometti }
34eae9d2baSRodolfo Giometti while (ts->nsec < 0) {
35eae9d2baSRodolfo Giometti ts->nsec += NSEC_PER_SEC;
36eae9d2baSRodolfo Giometti ts->sec--;
37eae9d2baSRodolfo Giometti }
38eae9d2baSRodolfo Giometti ts->sec += offset->sec;
39eae9d2baSRodolfo Giometti }
40eae9d2baSRodolfo Giometti
pps_echo_client_default(struct pps_device * pps,int event,void * data)41437c5341SJames Nuss static void pps_echo_client_default(struct pps_device *pps, int event,
42437c5341SJames Nuss void *data)
43437c5341SJames Nuss {
44*cd3bbcb6SCalvin Owens dev_info(&pps->dev, "echo %s %s\n",
45437c5341SJames Nuss event & PPS_CAPTUREASSERT ? "assert" : "",
46437c5341SJames Nuss event & PPS_CAPTURECLEAR ? "clear" : "");
47437c5341SJames Nuss }
48437c5341SJames Nuss
49eae9d2baSRodolfo Giometti /*
50eae9d2baSRodolfo Giometti * Exported functions
51eae9d2baSRodolfo Giometti */
52eae9d2baSRodolfo Giometti
53eae9d2baSRodolfo Giometti /* pps_register_source - add a PPS source in the system
54eae9d2baSRodolfo Giometti * @info: the PPS info struct
55eae9d2baSRodolfo Giometti * @default_params: the default PPS parameters of the new source
56eae9d2baSRodolfo Giometti *
57eae9d2baSRodolfo Giometti * This function is used to add a new PPS source in the system. The new
58eae9d2baSRodolfo Giometti * source is described by info's fields and it will have, as default PPS
59eae9d2baSRodolfo Giometti * parameters, the ones specified into default_params.
60eae9d2baSRodolfo Giometti *
613b1ad360SYueHaibing * The function returns, in case of success, the PPS device. Otherwise
623b1ad360SYueHaibing * ERR_PTR(errno).
63eae9d2baSRodolfo Giometti */
64eae9d2baSRodolfo Giometti
pps_register_source(struct pps_source_info * info,int default_params)655e196d34SAlexander Gordeev struct pps_device *pps_register_source(struct pps_source_info *info,
665e196d34SAlexander Gordeev int default_params)
67eae9d2baSRodolfo Giometti {
68eae9d2baSRodolfo Giometti struct pps_device *pps;
69eae9d2baSRodolfo Giometti int err;
70eae9d2baSRodolfo Giometti
71eae9d2baSRodolfo Giometti /* Sanity checks */
72eae9d2baSRodolfo Giometti if ((info->mode & default_params) != default_params) {
737f7cce74SAlexander Gordeev pr_err("%s: unsupported default parameters\n",
74eae9d2baSRodolfo Giometti info->name);
75eae9d2baSRodolfo Giometti err = -EINVAL;
76eae9d2baSRodolfo Giometti goto pps_register_source_exit;
77eae9d2baSRodolfo Giometti }
78eae9d2baSRodolfo Giometti if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
797f7cce74SAlexander Gordeev pr_err("%s: unspecified time format\n",
80eae9d2baSRodolfo Giometti info->name);
81eae9d2baSRodolfo Giometti err = -EINVAL;
82eae9d2baSRodolfo Giometti goto pps_register_source_exit;
83eae9d2baSRodolfo Giometti }
84eae9d2baSRodolfo Giometti
85eae9d2baSRodolfo Giometti /* Allocate memory for the new PPS source struct */
86eae9d2baSRodolfo Giometti pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
87eae9d2baSRodolfo Giometti if (pps == NULL) {
88eae9d2baSRodolfo Giometti err = -ENOMEM;
89eae9d2baSRodolfo Giometti goto pps_register_source_exit;
90eae9d2baSRodolfo Giometti }
91eae9d2baSRodolfo Giometti
9219dd2da3STejun Heo /* These initializations must be done before calling idr_alloc()
93eae9d2baSRodolfo Giometti * in order to avoid reces into pps_event().
94eae9d2baSRodolfo Giometti */
95eae9d2baSRodolfo Giometti pps->params.api_version = PPS_API_VERS;
96eae9d2baSRodolfo Giometti pps->params.mode = default_params;
97eae9d2baSRodolfo Giometti pps->info = *info;
98eae9d2baSRodolfo Giometti
99437c5341SJames Nuss /* check for default echo function */
100437c5341SJames Nuss if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
101437c5341SJames Nuss pps->info.echo == NULL)
102437c5341SJames Nuss pps->info.echo = pps_echo_client_default;
103437c5341SJames Nuss
104eae9d2baSRodolfo Giometti init_waitqueue_head(&pps->queue);
105eae9d2baSRodolfo Giometti spin_lock_init(&pps->lock);
106eae9d2baSRodolfo Giometti
107eae9d2baSRodolfo Giometti /* Create the char device */
108eae9d2baSRodolfo Giometti err = pps_register_cdev(pps);
109eae9d2baSRodolfo Giometti if (err < 0) {
1107f7cce74SAlexander Gordeev pr_err("%s: unable to create char device\n",
111eae9d2baSRodolfo Giometti info->name);
112083e5866SAlexander Gordeev goto kfree_pps;
113eae9d2baSRodolfo Giometti }
114eae9d2baSRodolfo Giometti
115*cd3bbcb6SCalvin Owens dev_dbg(&pps->dev, "new PPS source %s\n", info->name);
116eae9d2baSRodolfo Giometti
1175e196d34SAlexander Gordeev return pps;
118eae9d2baSRodolfo Giometti
119eae9d2baSRodolfo Giometti kfree_pps:
120eae9d2baSRodolfo Giometti kfree(pps);
121eae9d2baSRodolfo Giometti
122eae9d2baSRodolfo Giometti pps_register_source_exit:
1237f7cce74SAlexander Gordeev pr_err("%s: unable to register source\n", info->name);
124eae9d2baSRodolfo Giometti
1253b1ad360SYueHaibing return ERR_PTR(err);
126eae9d2baSRodolfo Giometti }
127eae9d2baSRodolfo Giometti EXPORT_SYMBOL(pps_register_source);
128eae9d2baSRodolfo Giometti
129eae9d2baSRodolfo Giometti /* pps_unregister_source - remove a PPS source from the system
1305e196d34SAlexander Gordeev * @pps: the PPS source
131eae9d2baSRodolfo Giometti *
132eae9d2baSRodolfo Giometti * This function is used to remove a previously registered PPS source from
133eae9d2baSRodolfo Giometti * the system.
134eae9d2baSRodolfo Giometti */
135eae9d2baSRodolfo Giometti
pps_unregister_source(struct pps_device * pps)1365e196d34SAlexander Gordeev void pps_unregister_source(struct pps_device *pps)
137eae9d2baSRodolfo Giometti {
138717c0336SAlexander Gordeev pps_kc_remove(pps);
139eae9d2baSRodolfo Giometti pps_unregister_cdev(pps);
1405e196d34SAlexander Gordeev
141083e5866SAlexander Gordeev /* don't have to kfree(pps) here because it will be done on
142083e5866SAlexander Gordeev * device destruction */
143eae9d2baSRodolfo Giometti }
144eae9d2baSRodolfo Giometti EXPORT_SYMBOL(pps_unregister_source);
145eae9d2baSRodolfo Giometti
146eae9d2baSRodolfo Giometti /* pps_event - register a PPS event into the system
1475e196d34SAlexander Gordeev * @pps: the PPS device
148eae9d2baSRodolfo Giometti * @ts: the event timestamp
149eae9d2baSRodolfo Giometti * @event: the event type
150eae9d2baSRodolfo Giometti * @data: userdef pointer
151eae9d2baSRodolfo Giometti *
152eae9d2baSRodolfo Giometti * This function is used by each PPS client in order to register a new
153eae9d2baSRodolfo Giometti * PPS event into the system (it's usually called inside an IRQ handler).
154eae9d2baSRodolfo Giometti *
1555e196d34SAlexander Gordeev * If an echo function is associated with the PPS device it will be called
156eae9d2baSRodolfo Giometti * as:
1575e196d34SAlexander Gordeev * pps->info.echo(pps, event, data);
158eae9d2baSRodolfo Giometti */
pps_event(struct pps_device * pps,struct pps_event_time * ts,int event,void * data)1595e196d34SAlexander Gordeev void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
1605e196d34SAlexander Gordeev void *data)
161eae9d2baSRodolfo Giometti {
162eae9d2baSRodolfo Giometti unsigned long flags;
163276b282eSRodolfo Giometti int captured = 0;
16499b0d365SAlexander Gordeev struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
165eae9d2baSRodolfo Giometti
16629f347c9SAlexander Gordeev /* check event type */
16729f347c9SAlexander Gordeev BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
168eae9d2baSRodolfo Giometti
169*cd3bbcb6SCalvin Owens dev_dbg(&pps->dev, "PPS event at %lld.%09ld\n",
170ade1bdffSArnd Bergmann (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
1716f4229b5SAlexander Gordeev
1726f4229b5SAlexander Gordeev timespec_to_pps_ktime(&ts_real, ts->ts_real);
173eae9d2baSRodolfo Giometti
174eae9d2baSRodolfo Giometti spin_lock_irqsave(&pps->lock, flags);
175eae9d2baSRodolfo Giometti
176eae9d2baSRodolfo Giometti /* Must call the echo function? */
177eae9d2baSRodolfo Giometti if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
1785e196d34SAlexander Gordeev pps->info.echo(pps, event, data);
179eae9d2baSRodolfo Giometti
180eae9d2baSRodolfo Giometti /* Check the event */
181eae9d2baSRodolfo Giometti pps->current_mode = pps->params.mode;
182818b9eefSAlexander Gordeev if (event & pps->params.mode & PPS_CAPTUREASSERT) {
183eae9d2baSRodolfo Giometti /* We have to add an offset? */
184eae9d2baSRodolfo Giometti if (pps->params.mode & PPS_OFFSETASSERT)
1856f4229b5SAlexander Gordeev pps_add_offset(&ts_real,
1866f4229b5SAlexander Gordeev &pps->params.assert_off_tu);
187eae9d2baSRodolfo Giometti
188eae9d2baSRodolfo Giometti /* Save the time stamp */
1896f4229b5SAlexander Gordeev pps->assert_tu = ts_real;
190eae9d2baSRodolfo Giometti pps->assert_sequence++;
191*cd3bbcb6SCalvin Owens dev_dbg(&pps->dev, "capture assert seq #%u\n",
1925e196d34SAlexander Gordeev pps->assert_sequence);
193276b282eSRodolfo Giometti
194276b282eSRodolfo Giometti captured = ~0;
195eae9d2baSRodolfo Giometti }
196818b9eefSAlexander Gordeev if (event & pps->params.mode & PPS_CAPTURECLEAR) {
197eae9d2baSRodolfo Giometti /* We have to add an offset? */
198eae9d2baSRodolfo Giometti if (pps->params.mode & PPS_OFFSETCLEAR)
1996f4229b5SAlexander Gordeev pps_add_offset(&ts_real,
2006f4229b5SAlexander Gordeev &pps->params.clear_off_tu);
201eae9d2baSRodolfo Giometti
202eae9d2baSRodolfo Giometti /* Save the time stamp */
2036f4229b5SAlexander Gordeev pps->clear_tu = ts_real;
204eae9d2baSRodolfo Giometti pps->clear_sequence++;
205*cd3bbcb6SCalvin Owens dev_dbg(&pps->dev, "capture clear seq #%u\n",
2065e196d34SAlexander Gordeev pps->clear_sequence);
207276b282eSRodolfo Giometti
208276b282eSRodolfo Giometti captured = ~0;
209eae9d2baSRodolfo Giometti }
210eae9d2baSRodolfo Giometti
211717c0336SAlexander Gordeev pps_kc_event(pps, ts, event);
212717c0336SAlexander Gordeev
2137a21a3ccSAlexander Gordeev /* Wake up if captured something */
214276b282eSRodolfo Giometti if (captured) {
2153003d55bSAlexander Gordeev pps->last_ev++;
2163003d55bSAlexander Gordeev wake_up_interruptible_all(&pps->queue);
217eae9d2baSRodolfo Giometti
218eae9d2baSRodolfo Giometti kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
219276b282eSRodolfo Giometti }
220eae9d2baSRodolfo Giometti
221eae9d2baSRodolfo Giometti spin_unlock_irqrestore(&pps->lock, flags);
222eae9d2baSRodolfo Giometti }
223eae9d2baSRodolfo Giometti EXPORT_SYMBOL(pps_event);
224