5 * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/sched.h>
28 #include <linux/time.h>
29 #include <linux/spinlock.h>
31 #include <linux/pps_kernel.h>
32 #include <linux/slab.h>
38 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
40 ts->nsec += offset->nsec;
41 while (ts->nsec >= NSEC_PER_SEC) {
42 ts->nsec -= NSEC_PER_SEC;
45 while (ts->nsec < 0) {
46 ts->nsec += NSEC_PER_SEC;
49 ts->sec += offset->sec;
56 /* pps_register_source - add a PPS source in the system
57 * @info: the PPS info struct
58 * @default_params: the default PPS parameters of the new source
60 * This function is used to add a new PPS source in the system. The new
61 * source is described by info's fields and it will have, as default PPS
62 * parameters, the ones specified into default_params.
64 * The function returns, in case of success, the PPS device. Otherwise NULL.
67 struct pps_device *pps_register_source(struct pps_source_info *info,
70 struct pps_device *pps;
74 if ((info->mode & default_params) != default_params) {
75 pr_err("%s: unsupported default parameters\n",
78 goto pps_register_source_exit;
80 if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
82 pr_err("%s: echo function is not defined\n",
85 goto pps_register_source_exit;
87 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
88 pr_err("%s: unspecified time format\n",
91 goto pps_register_source_exit;
94 /* Allocate memory for the new PPS source struct */
95 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
98 goto pps_register_source_exit;
101 /* These initializations must be done before calling idr_get_new()
102 * in order to avoid reces into pps_event().
104 pps->params.api_version = PPS_API_VERS;
105 pps->params.mode = default_params;
108 init_waitqueue_head(&pps->queue);
109 spin_lock_init(&pps->lock);
111 /* Create the char device */
112 err = pps_register_cdev(pps);
114 pr_err("%s: unable to create char device\n",
119 dev_info(pps->dev, "new PPS source %s\n", info->name);
126 pps_register_source_exit:
127 pr_err("%s: unable to register source\n", info->name);
131 EXPORT_SYMBOL(pps_register_source);
133 /* pps_unregister_source - remove a PPS source from the system
134 * @pps: the PPS source
136 * This function is used to remove a previously registered PPS source from
140 void pps_unregister_source(struct pps_device *pps)
142 pps_unregister_cdev(pps);
144 /* don't have to kfree(pps) here because it will be done on
145 * device destruction */
147 EXPORT_SYMBOL(pps_unregister_source);
149 /* pps_event - register a PPS event into the system
150 * @pps: the PPS device
151 * @ts: the event timestamp
152 * @event: the event type
153 * @data: userdef pointer
155 * This function is used by each PPS client in order to register a new
156 * PPS event into the system (it's usually called inside an IRQ handler).
158 * If an echo function is associated with the PPS device it will be called
160 * pps->info.echo(pps, event, data);
162 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
167 struct pps_ktime ts_real;
169 /* check event type */
170 BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
172 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
173 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
175 timespec_to_pps_ktime(&ts_real, ts->ts_real);
177 spin_lock_irqsave(&pps->lock, flags);
179 /* Must call the echo function? */
180 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
181 pps->info.echo(pps, event, data);
183 /* Check the event */
184 pps->current_mode = pps->params.mode;
185 if (event & pps->params.mode & PPS_CAPTUREASSERT) {
186 /* We have to add an offset? */
187 if (pps->params.mode & PPS_OFFSETASSERT)
188 pps_add_offset(&ts_real,
189 &pps->params.assert_off_tu);
191 /* Save the time stamp */
192 pps->assert_tu = ts_real;
193 pps->assert_sequence++;
194 dev_dbg(pps->dev, "capture assert seq #%u\n",
195 pps->assert_sequence);
199 if (event & pps->params.mode & PPS_CAPTURECLEAR) {
200 /* We have to add an offset? */
201 if (pps->params.mode & PPS_OFFSETCLEAR)
202 pps_add_offset(&ts_real,
203 &pps->params.clear_off_tu);
205 /* Save the time stamp */
206 pps->clear_tu = ts_real;
207 pps->clear_sequence++;
208 dev_dbg(pps->dev, "capture clear seq #%u\n",
209 pps->clear_sequence);
214 /* Wake up if captured something */
217 wake_up_interruptible_all(&pps->queue);
219 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
222 spin_unlock_irqrestore(&pps->lock, flags);
224 EXPORT_SYMBOL(pps_event);