2 * pps_gen_parport.c -- kernel parallel port PPS signal generator
5 * Copyright (C) 2009 Alexander Gordeev <lasaine@lvk.cs.msu.su>
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.
25 * fix issues when realtime clock is adjusted in a leap
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/time.h>
34 #include <linux/hrtimer.h>
35 #include <linux/parport.h>
37 #define DRVDESC "parallel port PPS signal generator"
40 #define NO_SIGNAL PARPORT_CONTROL_STROBE
42 /* module parameters */
44 #define SEND_DELAY_MAX 100000
46 static unsigned int send_delay
= 30000;
47 MODULE_PARM_DESC(delay
,
48 "Delay between setting and dropping the signal (ns)");
49 module_param_named(delay
, send_delay
, uint
, 0);
52 #define SAFETY_INTERVAL 3000 /* set the hrtimer earlier for safety (ns) */
54 /* internal per port structure */
55 struct pps_generator_pp
{
56 struct pardevice
*pardev
; /* parport device */
58 long port_write_time
; /* calibrated port write time (ns) */
61 static struct pps_generator_pp device
= {
67 /* calibrated time between a hrtimer event and the reaction */
68 static long hrtimer_error
= SAFETY_INTERVAL
;
70 /* the kernel hrtimer event */
71 static enum hrtimer_restart
hrtimer_event(struct hrtimer
*timer
)
73 struct timespec64 expire_time
, ts1
, ts2
, ts3
, dts
;
74 struct pps_generator_pp
*dev
;
79 /* We have to disable interrupts here. The idea is to prevent
80 * other interrupts on the same processor to introduce random
81 * lags while polling the clock. ktime_get_real_ts64() takes <1us on
82 * most machines while other interrupt handlers can take much
85 * NB: approx time with blocked interrupts =
86 * send_delay + 3 * SAFETY_INTERVAL
88 local_irq_save(flags
);
90 /* first of all we get the time stamp... */
91 ktime_get_real_ts64(&ts1
);
92 expire_time
= ktime_to_timespec64(hrtimer_get_softexpires(timer
));
93 dev
= container_of(timer
, struct pps_generator_pp
, timer
);
94 lim
= NSEC_PER_SEC
- send_delay
- dev
->port_write_time
;
96 /* check if we are late */
97 if (expire_time
.tv_sec
!= ts1
.tv_sec
|| ts1
.tv_nsec
> lim
) {
98 local_irq_restore(flags
);
99 pr_err("we are late this time %lld.%09ld\n",
100 (s64
)ts1
.tv_sec
, ts1
.tv_nsec
);
104 /* busy loop until the time is right for an assert edge */
106 ktime_get_real_ts64(&ts2
);
107 } while (expire_time
.tv_sec
== ts2
.tv_sec
&& ts2
.tv_nsec
< lim
);
110 port
= dev
->pardev
->port
;
111 port
->ops
->write_control(port
, SIGNAL
);
113 /* busy loop until the time is right for a clear edge */
114 lim
= NSEC_PER_SEC
- dev
->port_write_time
;
116 ktime_get_real_ts64(&ts2
);
117 } while (expire_time
.tv_sec
== ts2
.tv_sec
&& ts2
.tv_nsec
< lim
);
119 /* unset the signal */
120 port
->ops
->write_control(port
, NO_SIGNAL
);
122 ktime_get_real_ts64(&ts3
);
124 local_irq_restore(flags
);
126 /* update calibrated port write time */
127 dts
= timespec64_sub(ts3
, ts2
);
128 dev
->port_write_time
=
129 (dev
->port_write_time
+ timespec64_to_ns(&dts
)) >> 1;
132 /* update calibrated hrtimer error */
133 dts
= timespec64_sub(ts1
, expire_time
);
134 delta
= timespec64_to_ns(&dts
);
135 /* If the new error value is bigger then the old, use the new
136 * value, if not then slowly move towards the new value. This
137 * way it should be safe in bad conditions and efficient in
140 if (delta
>= hrtimer_error
)
141 hrtimer_error
= delta
;
143 hrtimer_error
= (3 * hrtimer_error
+ delta
) >> 2;
145 /* update the hrtimer expire time */
146 hrtimer_set_expires(timer
,
147 ktime_set(expire_time
.tv_sec
+ 1,
148 NSEC_PER_SEC
- (send_delay
+
149 dev
->port_write_time
+ SAFETY_INTERVAL
+
150 2 * hrtimer_error
)));
152 return HRTIMER_RESTART
;
155 /* calibrate port write time */
156 #define PORT_NTESTS_SHIFT 5
157 static void calibrate_port(struct pps_generator_pp
*dev
)
159 struct parport
*port
= dev
->pardev
->port
;
163 for (i
= 0; i
< (1 << PORT_NTESTS_SHIFT
); i
++) {
164 struct timespec64 a
, b
;
165 unsigned long irq_flags
;
167 local_irq_save(irq_flags
);
168 ktime_get_real_ts64(&a
);
169 port
->ops
->write_control(port
, NO_SIGNAL
);
170 ktime_get_real_ts64(&b
);
171 local_irq_restore(irq_flags
);
173 b
= timespec64_sub(b
, a
);
174 acc
+= timespec64_to_ns(&b
);
177 dev
->port_write_time
= acc
>> PORT_NTESTS_SHIFT
;
178 pr_info("port write takes %ldns\n", dev
->port_write_time
);
181 static inline ktime_t
next_intr_time(struct pps_generator_pp
*dev
)
183 struct timespec64 ts
;
185 ktime_get_real_ts64(&ts
);
187 return ktime_set(ts
.tv_sec
+
188 ((ts
.tv_nsec
> 990 * NSEC_PER_MSEC
) ? 1 : 0),
189 NSEC_PER_SEC
- (send_delay
+
190 dev
->port_write_time
+ 3 * SAFETY_INTERVAL
));
193 static void parport_attach(struct parport
*port
)
195 struct pardev_cb pps_cb
;
198 /* we already have a port */
202 memset(&pps_cb
, 0, sizeof(pps_cb
));
203 pps_cb
.private = &device
;
204 pps_cb
.flags
= PARPORT_FLAG_EXCL
;
205 device
.pardev
= parport_register_dev_model(port
, KBUILD_MODNAME
,
207 if (!device
.pardev
) {
208 pr_err("couldn't register with %s\n", port
->name
);
212 if (parport_claim_or_block(device
.pardev
) < 0) {
213 pr_err("couldn't claim %s\n", port
->name
);
214 goto err_unregister_dev
;
217 pr_info("attached to %s\n", port
->name
);
220 calibrate_port(&device
);
222 hrtimer_init(&device
.timer
, CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
223 device
.timer
.function
= hrtimer_event
;
224 hrtimer_start(&device
.timer
, next_intr_time(&device
), HRTIMER_MODE_ABS
);
229 parport_unregister_device(device
.pardev
);
232 static void parport_detach(struct parport
*port
)
234 if (port
->cad
!= device
.pardev
)
235 return; /* not our port */
237 hrtimer_cancel(&device
.timer
);
238 parport_release(device
.pardev
);
239 parport_unregister_device(device
.pardev
);
242 static struct parport_driver pps_gen_parport_driver
= {
243 .name
= KBUILD_MODNAME
,
244 .match_port
= parport_attach
,
245 .detach
= parport_detach
,
251 static int __init
pps_gen_parport_init(void)
255 pr_info(DRVDESC
"\n");
257 if (send_delay
> SEND_DELAY_MAX
) {
258 pr_err("delay value should be not greater"
259 " then %d\n", SEND_DELAY_MAX
);
263 ret
= parport_register_driver(&pps_gen_parport_driver
);
265 pr_err("unable to register with parport\n");
272 static void __exit
pps_gen_parport_exit(void)
274 parport_unregister_driver(&pps_gen_parport_driver
);
275 pr_info("hrtimer avg error is %ldns\n", hrtimer_error
);
278 module_init(pps_gen_parport_init
);
279 module_exit(pps_gen_parport_exit
);
281 MODULE_AUTHOR("Alexander Gordeev <lasaine@lvk.cs.msu.su>");
282 MODULE_DESCRIPTION(DRVDESC
);
283 MODULE_LICENSE("GPL");