Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 2 | /* |
| 3 | * pps_gen_parport.c -- kernel parallel port PPS signal generator |
| 4 | * |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 5 | * Copyright (C) 2009 Alexander Gordeev <lasaine@lvk.cs.msu.su> |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| 9 | /* |
| 10 | * TODO: |
| 11 | * fix issues when realtime clock is adjusted in a leap |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/time.h> |
| 20 | #include <linux/hrtimer.h> |
| 21 | #include <linux/parport.h> |
| 22 | |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 23 | #define SIGNAL 0 |
| 24 | #define NO_SIGNAL PARPORT_CONTROL_STROBE |
| 25 | |
| 26 | /* module parameters */ |
| 27 | |
| 28 | #define SEND_DELAY_MAX 100000 |
| 29 | |
| 30 | static unsigned int send_delay = 30000; |
| 31 | MODULE_PARM_DESC(delay, |
| 32 | "Delay between setting and dropping the signal (ns)"); |
| 33 | module_param_named(delay, send_delay, uint, 0); |
| 34 | |
| 35 | |
| 36 | #define SAFETY_INTERVAL 3000 /* set the hrtimer earlier for safety (ns) */ |
| 37 | |
| 38 | /* internal per port structure */ |
| 39 | struct pps_generator_pp { |
| 40 | struct pardevice *pardev; /* parport device */ |
| 41 | struct hrtimer timer; |
| 42 | long port_write_time; /* calibrated port write time (ns) */ |
| 43 | }; |
| 44 | |
| 45 | static struct pps_generator_pp device = { |
| 46 | .pardev = NULL, |
| 47 | }; |
| 48 | |
| 49 | static int attached; |
| 50 | |
| 51 | /* calibrated time between a hrtimer event and the reaction */ |
| 52 | static long hrtimer_error = SAFETY_INTERVAL; |
| 53 | |
| 54 | /* the kernel hrtimer event */ |
| 55 | static enum hrtimer_restart hrtimer_event(struct hrtimer *timer) |
| 56 | { |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 57 | struct timespec64 expire_time, ts1, ts2, ts3, dts; |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 58 | struct pps_generator_pp *dev; |
| 59 | struct parport *port; |
| 60 | long lim, delta; |
| 61 | unsigned long flags; |
| 62 | |
Alexander Gordeev | 563558b | 2011-01-12 17:01:00 -0800 | [diff] [blame] | 63 | /* We have to disable interrupts here. The idea is to prevent |
| 64 | * other interrupts on the same processor to introduce random |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 65 | * lags while polling the clock. ktime_get_real_ts64() takes <1us on |
Alexander Gordeev | 563558b | 2011-01-12 17:01:00 -0800 | [diff] [blame] | 66 | * most machines while other interrupt handlers can take much |
| 67 | * more potentially. |
| 68 | * |
| 69 | * NB: approx time with blocked interrupts = |
| 70 | * send_delay + 3 * SAFETY_INTERVAL |
| 71 | */ |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 72 | local_irq_save(flags); |
| 73 | |
| 74 | /* first of all we get the time stamp... */ |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 75 | ktime_get_real_ts64(&ts1); |
| 76 | expire_time = ktime_to_timespec64(hrtimer_get_softexpires(timer)); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 77 | dev = container_of(timer, struct pps_generator_pp, timer); |
| 78 | lim = NSEC_PER_SEC - send_delay - dev->port_write_time; |
| 79 | |
| 80 | /* check if we are late */ |
| 81 | if (expire_time.tv_sec != ts1.tv_sec || ts1.tv_nsec > lim) { |
| 82 | local_irq_restore(flags); |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 83 | pr_err("we are late this time %lld.%09ld\n", |
| 84 | (s64)ts1.tv_sec, ts1.tv_nsec); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 85 | goto done; |
| 86 | } |
| 87 | |
| 88 | /* busy loop until the time is right for an assert edge */ |
| 89 | do { |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 90 | ktime_get_real_ts64(&ts2); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 91 | } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim); |
| 92 | |
| 93 | /* set the signal */ |
| 94 | port = dev->pardev->port; |
| 95 | port->ops->write_control(port, SIGNAL); |
| 96 | |
| 97 | /* busy loop until the time is right for a clear edge */ |
| 98 | lim = NSEC_PER_SEC - dev->port_write_time; |
| 99 | do { |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 100 | ktime_get_real_ts64(&ts2); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 101 | } while (expire_time.tv_sec == ts2.tv_sec && ts2.tv_nsec < lim); |
| 102 | |
| 103 | /* unset the signal */ |
| 104 | port->ops->write_control(port, NO_SIGNAL); |
| 105 | |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 106 | ktime_get_real_ts64(&ts3); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 107 | |
| 108 | local_irq_restore(flags); |
| 109 | |
| 110 | /* update calibrated port write time */ |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 111 | dts = timespec64_sub(ts3, ts2); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 112 | dev->port_write_time = |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 113 | (dev->port_write_time + timespec64_to_ns(&dts)) >> 1; |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 114 | |
| 115 | done: |
| 116 | /* update calibrated hrtimer error */ |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 117 | dts = timespec64_sub(ts1, expire_time); |
| 118 | delta = timespec64_to_ns(&dts); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 119 | /* If the new error value is bigger then the old, use the new |
| 120 | * value, if not then slowly move towards the new value. This |
| 121 | * way it should be safe in bad conditions and efficient in |
| 122 | * good conditions. |
| 123 | */ |
| 124 | if (delta >= hrtimer_error) |
| 125 | hrtimer_error = delta; |
| 126 | else |
| 127 | hrtimer_error = (3 * hrtimer_error + delta) >> 2; |
| 128 | |
| 129 | /* update the hrtimer expire time */ |
| 130 | hrtimer_set_expires(timer, |
| 131 | ktime_set(expire_time.tv_sec + 1, |
| 132 | NSEC_PER_SEC - (send_delay + |
| 133 | dev->port_write_time + SAFETY_INTERVAL + |
| 134 | 2 * hrtimer_error))); |
| 135 | |
| 136 | return HRTIMER_RESTART; |
| 137 | } |
| 138 | |
| 139 | /* calibrate port write time */ |
| 140 | #define PORT_NTESTS_SHIFT 5 |
| 141 | static void calibrate_port(struct pps_generator_pp *dev) |
| 142 | { |
| 143 | struct parport *port = dev->pardev->port; |
| 144 | int i; |
| 145 | long acc = 0; |
| 146 | |
| 147 | for (i = 0; i < (1 << PORT_NTESTS_SHIFT); i++) { |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 148 | struct timespec64 a, b; |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 149 | unsigned long irq_flags; |
| 150 | |
| 151 | local_irq_save(irq_flags); |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 152 | ktime_get_real_ts64(&a); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 153 | port->ops->write_control(port, NO_SIGNAL); |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 154 | ktime_get_real_ts64(&b); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 155 | local_irq_restore(irq_flags); |
| 156 | |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 157 | b = timespec64_sub(b, a); |
| 158 | acc += timespec64_to_ns(&b); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | dev->port_write_time = acc >> PORT_NTESTS_SHIFT; |
| 162 | pr_info("port write takes %ldns\n", dev->port_write_time); |
| 163 | } |
| 164 | |
| 165 | static inline ktime_t next_intr_time(struct pps_generator_pp *dev) |
| 166 | { |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 167 | struct timespec64 ts; |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 168 | |
Arnd Bergmann | 28f3a48 | 2018-02-06 15:40:21 -0800 | [diff] [blame] | 169 | ktime_get_real_ts64(&ts); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 170 | |
| 171 | return ktime_set(ts.tv_sec + |
| 172 | ((ts.tv_nsec > 990 * NSEC_PER_MSEC) ? 1 : 0), |
| 173 | NSEC_PER_SEC - (send_delay + |
| 174 | dev->port_write_time + 3 * SAFETY_INTERVAL)); |
| 175 | } |
| 176 | |
| 177 | static void parport_attach(struct parport *port) |
| 178 | { |
Sudip Mukherjee | b857cac | 2018-01-28 23:22:09 +0000 | [diff] [blame] | 179 | struct pardev_cb pps_cb; |
| 180 | |
Andy Shevchenko | e0de88d | 2022-02-10 15:49:43 +0200 | [diff] [blame] | 181 | if (send_delay > SEND_DELAY_MAX) { |
| 182 | pr_err("delay value should be not greater then %d\n", SEND_DELAY_MAX); |
| 183 | return; |
| 184 | } |
| 185 | |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 186 | if (attached) { |
| 187 | /* we already have a port */ |
| 188 | return; |
| 189 | } |
| 190 | |
Sudip Mukherjee | b857cac | 2018-01-28 23:22:09 +0000 | [diff] [blame] | 191 | memset(&pps_cb, 0, sizeof(pps_cb)); |
| 192 | pps_cb.private = &device; |
| 193 | pps_cb.flags = PARPORT_FLAG_EXCL; |
| 194 | device.pardev = parport_register_dev_model(port, KBUILD_MODNAME, |
| 195 | &pps_cb, 0); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 196 | if (!device.pardev) { |
| 197 | pr_err("couldn't register with %s\n", port->name); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (parport_claim_or_block(device.pardev) < 0) { |
| 202 | pr_err("couldn't claim %s\n", port->name); |
| 203 | goto err_unregister_dev; |
| 204 | } |
| 205 | |
| 206 | pr_info("attached to %s\n", port->name); |
| 207 | attached = 1; |
| 208 | |
| 209 | calibrate_port(&device); |
| 210 | |
| 211 | hrtimer_init(&device.timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
| 212 | device.timer.function = hrtimer_event; |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 213 | hrtimer_start(&device.timer, next_intr_time(&device), HRTIMER_MODE_ABS); |
| 214 | |
| 215 | return; |
| 216 | |
| 217 | err_unregister_dev: |
| 218 | parport_unregister_device(device.pardev); |
| 219 | } |
| 220 | |
| 221 | static void parport_detach(struct parport *port) |
| 222 | { |
| 223 | if (port->cad != device.pardev) |
| 224 | return; /* not our port */ |
| 225 | |
| 226 | hrtimer_cancel(&device.timer); |
| 227 | parport_release(device.pardev); |
| 228 | parport_unregister_device(device.pardev); |
| 229 | } |
| 230 | |
| 231 | static struct parport_driver pps_gen_parport_driver = { |
| 232 | .name = KBUILD_MODNAME, |
Sudip Mukherjee | b857cac | 2018-01-28 23:22:09 +0000 | [diff] [blame] | 233 | .match_port = parport_attach, |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 234 | .detach = parport_detach, |
Sudip Mukherjee | b857cac | 2018-01-28 23:22:09 +0000 | [diff] [blame] | 235 | .devmodel = true, |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 236 | }; |
Andy Shevchenko | e0de88d | 2022-02-10 15:49:43 +0200 | [diff] [blame] | 237 | module_parport_driver(pps_gen_parport_driver); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 238 | |
| 239 | MODULE_AUTHOR("Alexander Gordeev <lasaine@lvk.cs.msu.su>"); |
Andy Shevchenko | e0de88d | 2022-02-10 15:49:43 +0200 | [diff] [blame] | 240 | MODULE_DESCRIPTION("parallel port PPS signal generator"); |
Alexander Gordeev | 46b402a | 2011-01-12 17:00:59 -0800 | [diff] [blame] | 241 | MODULE_LICENSE("GPL"); |