xref: /OK3568_Linux_fs/kernel/drivers/pps/kapi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * kernel API
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2005-2009   Rodolfo Giometti <giometti@linux.it>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/sched.h>
14*4882a593Smuzhiyun #include <linux/time.h>
15*4882a593Smuzhiyun #include <linux/timex.h>
16*4882a593Smuzhiyun #include <linux/spinlock.h>
17*4882a593Smuzhiyun #include <linux/fs.h>
18*4882a593Smuzhiyun #include <linux/pps_kernel.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "kc.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun  * Local functions
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
pps_add_offset(struct pps_ktime * ts,struct pps_ktime * offset)27*4882a593Smuzhiyun static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	ts->nsec += offset->nsec;
30*4882a593Smuzhiyun 	while (ts->nsec >= NSEC_PER_SEC) {
31*4882a593Smuzhiyun 		ts->nsec -= NSEC_PER_SEC;
32*4882a593Smuzhiyun 		ts->sec++;
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun 	while (ts->nsec < 0) {
35*4882a593Smuzhiyun 		ts->nsec += NSEC_PER_SEC;
36*4882a593Smuzhiyun 		ts->sec--;
37*4882a593Smuzhiyun 	}
38*4882a593Smuzhiyun 	ts->sec += offset->sec;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
pps_echo_client_default(struct pps_device * pps,int event,void * data)41*4882a593Smuzhiyun static void pps_echo_client_default(struct pps_device *pps, int event,
42*4882a593Smuzhiyun 		void *data)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	dev_info(pps->dev, "echo %s %s\n",
45*4882a593Smuzhiyun 		event & PPS_CAPTUREASSERT ? "assert" : "",
46*4882a593Smuzhiyun 		event & PPS_CAPTURECLEAR ? "clear" : "");
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * Exported functions
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /* pps_register_source - add a PPS source in the system
54*4882a593Smuzhiyun  * @info: the PPS info struct
55*4882a593Smuzhiyun  * @default_params: the default PPS parameters of the new source
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * This function is used to add a new PPS source in the system. The new
58*4882a593Smuzhiyun  * source is described by info's fields and it will have, as default PPS
59*4882a593Smuzhiyun  * parameters, the ones specified into default_params.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * The function returns, in case of success, the PPS device. Otherwise
62*4882a593Smuzhiyun  * ERR_PTR(errno).
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun 
pps_register_source(struct pps_source_info * info,int default_params)65*4882a593Smuzhiyun struct pps_device *pps_register_source(struct pps_source_info *info,
66*4882a593Smuzhiyun 		int default_params)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	struct pps_device *pps;
69*4882a593Smuzhiyun 	int err;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/* Sanity checks */
72*4882a593Smuzhiyun 	if ((info->mode & default_params) != default_params) {
73*4882a593Smuzhiyun 		pr_err("%s: unsupported default parameters\n",
74*4882a593Smuzhiyun 					info->name);
75*4882a593Smuzhiyun 		err = -EINVAL;
76*4882a593Smuzhiyun 		goto pps_register_source_exit;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 	if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
79*4882a593Smuzhiyun 		pr_err("%s: unspecified time format\n",
80*4882a593Smuzhiyun 					info->name);
81*4882a593Smuzhiyun 		err = -EINVAL;
82*4882a593Smuzhiyun 		goto pps_register_source_exit;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* Allocate memory for the new PPS source struct */
86*4882a593Smuzhiyun 	pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
87*4882a593Smuzhiyun 	if (pps == NULL) {
88*4882a593Smuzhiyun 		err = -ENOMEM;
89*4882a593Smuzhiyun 		goto pps_register_source_exit;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* These initializations must be done before calling idr_alloc()
93*4882a593Smuzhiyun 	 * in order to avoid reces into pps_event().
94*4882a593Smuzhiyun 	 */
95*4882a593Smuzhiyun 	pps->params.api_version = PPS_API_VERS;
96*4882a593Smuzhiyun 	pps->params.mode = default_params;
97*4882a593Smuzhiyun 	pps->info = *info;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* check for default echo function */
100*4882a593Smuzhiyun 	if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
101*4882a593Smuzhiyun 			pps->info.echo == NULL)
102*4882a593Smuzhiyun 		pps->info.echo = pps_echo_client_default;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	init_waitqueue_head(&pps->queue);
105*4882a593Smuzhiyun 	spin_lock_init(&pps->lock);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* Create the char device */
108*4882a593Smuzhiyun 	err = pps_register_cdev(pps);
109*4882a593Smuzhiyun 	if (err < 0) {
110*4882a593Smuzhiyun 		pr_err("%s: unable to create char device\n",
111*4882a593Smuzhiyun 					info->name);
112*4882a593Smuzhiyun 		goto kfree_pps;
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	dev_info(pps->dev, "new PPS source %s\n", info->name);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	return pps;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun kfree_pps:
120*4882a593Smuzhiyun 	kfree(pps);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun pps_register_source_exit:
123*4882a593Smuzhiyun 	pr_err("%s: unable to register source\n", info->name);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	return ERR_PTR(err);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun EXPORT_SYMBOL(pps_register_source);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* pps_unregister_source - remove a PPS source from the system
130*4882a593Smuzhiyun  * @pps: the PPS source
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  * This function is used to remove a previously registered PPS source from
133*4882a593Smuzhiyun  * the system.
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun 
pps_unregister_source(struct pps_device * pps)136*4882a593Smuzhiyun void pps_unregister_source(struct pps_device *pps)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	pps_kc_remove(pps);
139*4882a593Smuzhiyun 	pps_unregister_cdev(pps);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/* don't have to kfree(pps) here because it will be done on
142*4882a593Smuzhiyun 	 * device destruction */
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun EXPORT_SYMBOL(pps_unregister_source);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun /* pps_event - register a PPS event into the system
147*4882a593Smuzhiyun  * @pps: the PPS device
148*4882a593Smuzhiyun  * @ts: the event timestamp
149*4882a593Smuzhiyun  * @event: the event type
150*4882a593Smuzhiyun  * @data: userdef pointer
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * This function is used by each PPS client in order to register a new
153*4882a593Smuzhiyun  * PPS event into the system (it's usually called inside an IRQ handler).
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * If an echo function is associated with the PPS device it will be called
156*4882a593Smuzhiyun  * as:
157*4882a593Smuzhiyun  *	pps->info.echo(pps, event, data);
158*4882a593Smuzhiyun  */
pps_event(struct pps_device * pps,struct pps_event_time * ts,int event,void * data)159*4882a593Smuzhiyun void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
160*4882a593Smuzhiyun 		void *data)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	unsigned long flags;
163*4882a593Smuzhiyun 	int captured = 0;
164*4882a593Smuzhiyun 	struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* check event type */
167*4882a593Smuzhiyun 	BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
170*4882a593Smuzhiyun 			(s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	timespec_to_pps_ktime(&ts_real, ts->ts_real);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	spin_lock_irqsave(&pps->lock, flags);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* Must call the echo function? */
177*4882a593Smuzhiyun 	if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
178*4882a593Smuzhiyun 		pps->info.echo(pps, event, data);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	/* Check the event */
181*4882a593Smuzhiyun 	pps->current_mode = pps->params.mode;
182*4882a593Smuzhiyun 	if (event & pps->params.mode & PPS_CAPTUREASSERT) {
183*4882a593Smuzhiyun 		/* We have to add an offset? */
184*4882a593Smuzhiyun 		if (pps->params.mode & PPS_OFFSETASSERT)
185*4882a593Smuzhiyun 			pps_add_offset(&ts_real,
186*4882a593Smuzhiyun 					&pps->params.assert_off_tu);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		/* Save the time stamp */
189*4882a593Smuzhiyun 		pps->assert_tu = ts_real;
190*4882a593Smuzhiyun 		pps->assert_sequence++;
191*4882a593Smuzhiyun 		dev_dbg(pps->dev, "capture assert seq #%u\n",
192*4882a593Smuzhiyun 			pps->assert_sequence);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 		captured = ~0;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 	if (event & pps->params.mode & PPS_CAPTURECLEAR) {
197*4882a593Smuzhiyun 		/* We have to add an offset? */
198*4882a593Smuzhiyun 		if (pps->params.mode & PPS_OFFSETCLEAR)
199*4882a593Smuzhiyun 			pps_add_offset(&ts_real,
200*4882a593Smuzhiyun 					&pps->params.clear_off_tu);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 		/* Save the time stamp */
203*4882a593Smuzhiyun 		pps->clear_tu = ts_real;
204*4882a593Smuzhiyun 		pps->clear_sequence++;
205*4882a593Smuzhiyun 		dev_dbg(pps->dev, "capture clear seq #%u\n",
206*4882a593Smuzhiyun 			pps->clear_sequence);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 		captured = ~0;
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	pps_kc_event(pps, ts, event);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* Wake up if captured something */
214*4882a593Smuzhiyun 	if (captured) {
215*4882a593Smuzhiyun 		pps->last_ev++;
216*4882a593Smuzhiyun 		wake_up_interruptible_all(&pps->queue);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 		kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pps->lock, flags);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun EXPORT_SYMBOL(pps_event);
224