pps_gen_parport.c 7.2 KB

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