174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
246b402a0SAlexander Gordeev /*
346b402a0SAlexander Gordeev * pps_gen_parport.c -- kernel parallel port PPS signal generator
446b402a0SAlexander Gordeev *
546b402a0SAlexander Gordeev * Copyright (C) 2009 Alexander Gordeev <lasaine@lvk.cs.msu.su>
646b402a0SAlexander Gordeev */
746b402a0SAlexander Gordeev
846b402a0SAlexander Gordeev
946b402a0SAlexander Gordeev /*
1046b402a0SAlexander Gordeev * TODO:
1146b402a0SAlexander Gordeev * fix issues when realtime clock is adjusted in a leap
1246b402a0SAlexander Gordeev */
1346b402a0SAlexander Gordeev
1446b402a0SAlexander Gordeev #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1546b402a0SAlexander Gordeev
1646b402a0SAlexander Gordeev #include <linux/kernel.h>
1746b402a0SAlexander Gordeev #include <linux/module.h>
1846b402a0SAlexander Gordeev #include <linux/init.h>
1946b402a0SAlexander Gordeev #include <linux/time.h>
2046b402a0SAlexander Gordeev #include <linux/hrtimer.h>
2146b402a0SAlexander Gordeev #include <linux/parport.h>
2246b402a0SAlexander Gordeev
2346b402a0SAlexander Gordeev #define SIGNAL 0
2446b402a0SAlexander Gordeev #define NO_SIGNAL PARPORT_CONTROL_STROBE
2546b402a0SAlexander Gordeev
2646b402a0SAlexander Gordeev /* module parameters */
2746b402a0SAlexander Gordeev
2846b402a0SAlexander Gordeev #define SEND_DELAY_MAX 100000
2946b402a0SAlexander Gordeev
3046b402a0SAlexander Gordeev static unsigned int send_delay = 30000;
3146b402a0SAlexander Gordeev MODULE_PARM_DESC(delay,
3246b402a0SAlexander Gordeev "Delay between setting and dropping the signal (ns)");
3346b402a0SAlexander Gordeev module_param_named(delay, send_delay, uint, 0);
3446b402a0SAlexander Gordeev
3546b402a0SAlexander Gordeev
3646b402a0SAlexander Gordeev #define SAFETY_INTERVAL 3000 /* set the hrtimer earlier for safety (ns) */
3746b402a0SAlexander Gordeev
3846b402a0SAlexander Gordeev /* internal per port structure */
3946b402a0SAlexander Gordeev struct pps_generator_pp {
4046b402a0SAlexander Gordeev struct pardevice *pardev; /* parport device */
4146b402a0SAlexander Gordeev struct hrtimer timer;
4246b402a0SAlexander Gordeev long port_write_time; /* calibrated port write time (ns) */
4346b402a0SAlexander Gordeev };
4446b402a0SAlexander Gordeev
4546b402a0SAlexander Gordeev static struct pps_generator_pp device = {
4646b402a0SAlexander Gordeev .pardev = NULL,
4746b402a0SAlexander Gordeev };
4846b402a0SAlexander Gordeev
4946b402a0SAlexander Gordeev static int attached;
5046b402a0SAlexander Gordeev
5146b402a0SAlexander Gordeev /* calibrated time between a hrtimer event and the reaction */
5246b402a0SAlexander Gordeev static long hrtimer_error = SAFETY_INTERVAL;
5346b402a0SAlexander Gordeev
5446b402a0SAlexander Gordeev /* the kernel hrtimer event */
hrtimer_event(struct hrtimer * timer)5546b402a0SAlexander Gordeev static enum hrtimer_restart hrtimer_event(struct hrtimer *timer)
5646b402a0SAlexander Gordeev {
5728f3a488SArnd Bergmann struct timespec64 expire_time, ts1, ts2, ts3, dts;
5846b402a0SAlexander Gordeev struct pps_generator_pp *dev;
5946b402a0SAlexander Gordeev struct parport *port;
6046b402a0SAlexander Gordeev long lim, delta;
6146b402a0SAlexander Gordeev unsigned long flags;
6246b402a0SAlexander Gordeev
63563558b2SAlexander Gordeev /* We have to disable interrupts here. The idea is to prevent
64563558b2SAlexander Gordeev * other interrupts on the same processor to introduce random
6528f3a488SArnd Bergmann * lags while polling the clock. ktime_get_real_ts64() takes <1us on
66563558b2SAlexander Gordeev * most machines while other interrupt handlers can take much
67563558b2SAlexander Gordeev * more potentially.
68563558b2SAlexander Gordeev *
69563558b2SAlexander Gordeev * NB: approx time with blocked interrupts =
70563558b2SAlexander Gordeev * send_delay + 3 * SAFETY_INTERVAL
71563558b2SAlexander Gordeev */
7246b402a0SAlexander Gordeev local_irq_save(flags);
7346b402a0SAlexander Gordeev
7446b402a0SAlexander Gordeev /* first of all we get the time stamp... */
7528f3a488SArnd Bergmann ktime_get_real_ts64(&ts1);
7628f3a488SArnd Bergmann expire_time = ktime_to_timespec64(hrtimer_get_softexpires(timer));
7746b402a0SAlexander Gordeev dev = container_of(timer, struct pps_generator_pp, timer);
7846b402a0SAlexander Gordeev lim = NSEC_PER_SEC - send_delay - dev->port_write_time;
7946b402a0SAlexander Gordeev
8046b402a0SAlexander Gordeev /* check if we are late */
8146b402a0SAlexander Gordeev if (expire_time.tv_sec != ts1.tv_sec || ts1.tv_nsec > lim) {
8246b402a0SAlexander Gordeev local_irq_restore(flags);
8328f3a488SArnd Bergmann pr_err("we are late this time %lld.%09ld\n",
8428f3a488SArnd Bergmann (s64)ts1.tv_sec, ts1.tv_nsec);
8546b402a0SAlexander Gordeev goto done;
8646b402a0SAlexander Gordeev }
8746b402a0SAlexander Gordeev
8846b402a0SAlexander Gordeev /* busy loop until the time is right for an assert edge */
8946b402a0SAlexander Gordeev do {
9028f3a488SArnd Bergmann ktime_get_real_ts64(&ts2);
9146b402a0SAlexander Gordeev } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim);
9246b402a0SAlexander Gordeev
9346b402a0SAlexander Gordeev /* set the signal */
9446b402a0SAlexander Gordeev port = dev->pardev->port;
9546b402a0SAlexander Gordeev port->ops->write_control(port, SIGNAL);
9646b402a0SAlexander Gordeev
9746b402a0SAlexander Gordeev /* busy loop until the time is right for a clear edge */
9846b402a0SAlexander Gordeev lim = NSEC_PER_SEC - dev->port_write_time;
9946b402a0SAlexander Gordeev do {
10028f3a488SArnd Bergmann ktime_get_real_ts64(&ts2);
10146b402a0SAlexander Gordeev } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim);
10246b402a0SAlexander Gordeev
10346b402a0SAlexander Gordeev /* unset the signal */
10446b402a0SAlexander Gordeev port->ops->write_control(port, NO_SIGNAL);
10546b402a0SAlexander Gordeev
10628f3a488SArnd Bergmann ktime_get_real_ts64(&ts3);
10746b402a0SAlexander Gordeev
10846b402a0SAlexander Gordeev local_irq_restore(flags);
10946b402a0SAlexander Gordeev
11046b402a0SAlexander Gordeev /* update calibrated port write time */
11128f3a488SArnd Bergmann dts = timespec64_sub(ts3, ts2);
11246b402a0SAlexander Gordeev dev->port_write_time =
11328f3a488SArnd Bergmann (dev->port_write_time + timespec64_to_ns(&dts)) >> 1;
11446b402a0SAlexander Gordeev
11546b402a0SAlexander Gordeev done:
11646b402a0SAlexander Gordeev /* update calibrated hrtimer error */
11728f3a488SArnd Bergmann dts = timespec64_sub(ts1, expire_time);
11828f3a488SArnd Bergmann delta = timespec64_to_ns(&dts);
11946b402a0SAlexander Gordeev /* If the new error value is bigger then the old, use the new
12046b402a0SAlexander Gordeev * value, if not then slowly move towards the new value. This
12146b402a0SAlexander Gordeev * way it should be safe in bad conditions and efficient in
12246b402a0SAlexander Gordeev * good conditions.
12346b402a0SAlexander Gordeev */
12446b402a0SAlexander Gordeev if (delta >= hrtimer_error)
12546b402a0SAlexander Gordeev hrtimer_error = delta;
12646b402a0SAlexander Gordeev else
12746b402a0SAlexander Gordeev hrtimer_error = (3 * hrtimer_error + delta) >> 2;
12846b402a0SAlexander Gordeev
12946b402a0SAlexander Gordeev /* update the hrtimer expire time */
13046b402a0SAlexander Gordeev hrtimer_set_expires(timer,
13146b402a0SAlexander Gordeev ktime_set(expire_time.tv_sec + 1,
13246b402a0SAlexander Gordeev NSEC_PER_SEC - (send_delay +
13346b402a0SAlexander Gordeev dev->port_write_time + SAFETY_INTERVAL +
13446b402a0SAlexander Gordeev 2 * hrtimer_error)));
13546b402a0SAlexander Gordeev
13646b402a0SAlexander Gordeev return HRTIMER_RESTART;
13746b402a0SAlexander Gordeev }
13846b402a0SAlexander Gordeev
13946b402a0SAlexander Gordeev /* calibrate port write time */
14046b402a0SAlexander Gordeev #define PORT_NTESTS_SHIFT 5
calibrate_port(struct pps_generator_pp * dev)14146b402a0SAlexander Gordeev static void calibrate_port(struct pps_generator_pp *dev)
14246b402a0SAlexander Gordeev {
14346b402a0SAlexander Gordeev struct parport *port = dev->pardev->port;
14446b402a0SAlexander Gordeev int i;
14546b402a0SAlexander Gordeev long acc = 0;
14646b402a0SAlexander Gordeev
14746b402a0SAlexander Gordeev for (i = 0; i < (1 << PORT_NTESTS_SHIFT); i++) {
14828f3a488SArnd Bergmann struct timespec64 a, b;
14946b402a0SAlexander Gordeev unsigned long irq_flags;
15046b402a0SAlexander Gordeev
15146b402a0SAlexander Gordeev local_irq_save(irq_flags);
15228f3a488SArnd Bergmann ktime_get_real_ts64(&a);
15346b402a0SAlexander Gordeev port->ops->write_control(port, NO_SIGNAL);
15428f3a488SArnd Bergmann ktime_get_real_ts64(&b);
15546b402a0SAlexander Gordeev local_irq_restore(irq_flags);
15646b402a0SAlexander Gordeev
15728f3a488SArnd Bergmann b = timespec64_sub(b, a);
15828f3a488SArnd Bergmann acc += timespec64_to_ns(&b);
15946b402a0SAlexander Gordeev }
16046b402a0SAlexander Gordeev
16146b402a0SAlexander Gordeev dev->port_write_time = acc >> PORT_NTESTS_SHIFT;
16246b402a0SAlexander Gordeev pr_info("port write takes %ldns\n", dev->port_write_time);
16346b402a0SAlexander Gordeev }
16446b402a0SAlexander Gordeev
next_intr_time(struct pps_generator_pp * dev)16546b402a0SAlexander Gordeev static inline ktime_t next_intr_time(struct pps_generator_pp *dev)
16646b402a0SAlexander Gordeev {
16728f3a488SArnd Bergmann struct timespec64 ts;
16846b402a0SAlexander Gordeev
16928f3a488SArnd Bergmann ktime_get_real_ts64(&ts);
17046b402a0SAlexander Gordeev
17146b402a0SAlexander Gordeev return ktime_set(ts.tv_sec +
17246b402a0SAlexander Gordeev ((ts.tv_nsec > 990 * NSEC_PER_MSEC) ? 1 : 0),
17346b402a0SAlexander Gordeev NSEC_PER_SEC - (send_delay +
17446b402a0SAlexander Gordeev dev->port_write_time + 3 * SAFETY_INTERVAL));
17546b402a0SAlexander Gordeev }
17646b402a0SAlexander Gordeev
parport_attach(struct parport * port)17746b402a0SAlexander Gordeev static void parport_attach(struct parport *port)
17846b402a0SAlexander Gordeev {
179b857cacdSSudip Mukherjee struct pardev_cb pps_cb;
180b857cacdSSudip Mukherjee
181*e0de88dcSAndy Shevchenko if (send_delay > SEND_DELAY_MAX) {
182*e0de88dcSAndy Shevchenko pr_err("delay value should be not greater then %d\n", SEND_DELAY_MAX);
183*e0de88dcSAndy Shevchenko return;
184*e0de88dcSAndy Shevchenko }
185*e0de88dcSAndy Shevchenko
18646b402a0SAlexander Gordeev if (attached) {
18746b402a0SAlexander Gordeev /* we already have a port */
18846b402a0SAlexander Gordeev return;
18946b402a0SAlexander Gordeev }
19046b402a0SAlexander Gordeev
191b857cacdSSudip Mukherjee memset(&pps_cb, 0, sizeof(pps_cb));
192b857cacdSSudip Mukherjee pps_cb.private = &device;
193b857cacdSSudip Mukherjee pps_cb.flags = PARPORT_FLAG_EXCL;
194b857cacdSSudip Mukherjee device.pardev = parport_register_dev_model(port, KBUILD_MODNAME,
195b857cacdSSudip Mukherjee &pps_cb, 0);
19646b402a0SAlexander Gordeev if (!device.pardev) {
19746b402a0SAlexander Gordeev pr_err("couldn't register with %s\n", port->name);
19846b402a0SAlexander Gordeev return;
19946b402a0SAlexander Gordeev }
20046b402a0SAlexander Gordeev
20146b402a0SAlexander Gordeev if (parport_claim_or_block(device.pardev) < 0) {
20246b402a0SAlexander Gordeev pr_err("couldn't claim %s\n", port->name);
20346b402a0SAlexander Gordeev goto err_unregister_dev;
20446b402a0SAlexander Gordeev }
20546b402a0SAlexander Gordeev
20646b402a0SAlexander Gordeev pr_info("attached to %s\n", port->name);
20746b402a0SAlexander Gordeev attached = 1;
20846b402a0SAlexander Gordeev
20946b402a0SAlexander Gordeev calibrate_port(&device);
21046b402a0SAlexander Gordeev
21146b402a0SAlexander Gordeev hrtimer_init(&device.timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
21246b402a0SAlexander Gordeev device.timer.function = hrtimer_event;
21346b402a0SAlexander Gordeev hrtimer_start(&device.timer, next_intr_time(&device), HRTIMER_MODE_ABS);
21446b402a0SAlexander Gordeev
21546b402a0SAlexander Gordeev return;
21646b402a0SAlexander Gordeev
21746b402a0SAlexander Gordeev err_unregister_dev:
21846b402a0SAlexander Gordeev parport_unregister_device(device.pardev);
21946b402a0SAlexander Gordeev }
22046b402a0SAlexander Gordeev
parport_detach(struct parport * port)22146b402a0SAlexander Gordeev static void parport_detach(struct parport *port)
22246b402a0SAlexander Gordeev {
22346b402a0SAlexander Gordeev if (port->cad != device.pardev)
22446b402a0SAlexander Gordeev return; /* not our port */
22546b402a0SAlexander Gordeev
22646b402a0SAlexander Gordeev hrtimer_cancel(&device.timer);
22746b402a0SAlexander Gordeev parport_release(device.pardev);
22846b402a0SAlexander Gordeev parport_unregister_device(device.pardev);
22946b402a0SAlexander Gordeev }
23046b402a0SAlexander Gordeev
23146b402a0SAlexander Gordeev static struct parport_driver pps_gen_parport_driver = {
23246b402a0SAlexander Gordeev .name = KBUILD_MODNAME,
233b857cacdSSudip Mukherjee .match_port = parport_attach,
23446b402a0SAlexander Gordeev .detach = parport_detach,
235b857cacdSSudip Mukherjee .devmodel = true,
23646b402a0SAlexander Gordeev };
237*e0de88dcSAndy Shevchenko module_parport_driver(pps_gen_parport_driver);
23846b402a0SAlexander Gordeev
23946b402a0SAlexander Gordeev MODULE_AUTHOR("Alexander Gordeev <lasaine@lvk.cs.msu.su>");
240*e0de88dcSAndy Shevchenko MODULE_DESCRIPTION("parallel port PPS signal generator");
24146b402a0SAlexander Gordeev MODULE_LICENSE("GPL");
242