ptp_clock.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * PTP 1588 clock support
  3. *
  4. * Copyright (C) 2010 OMICRON electronics GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/device.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/posix-clock.h>
  27. #include <linux/pps_kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/uaccess.h>
  31. #include "ptp_private.h"
  32. #define PTP_MAX_ALARMS 4
  33. #define PTP_MAX_CLOCKS 8
  34. #define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT)
  35. #define PTP_PPS_EVENT PPS_CAPTUREASSERT
  36. #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
  37. /* private globals */
  38. static dev_t ptp_devt;
  39. static struct class *ptp_class;
  40. static DECLARE_BITMAP(ptp_clocks_map, PTP_MAX_CLOCKS);
  41. static DEFINE_MUTEX(ptp_clocks_mutex); /* protects 'ptp_clocks_map' */
  42. /* time stamp event queue operations */
  43. static inline int queue_free(struct timestamp_event_queue *q)
  44. {
  45. return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
  46. }
  47. static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
  48. struct ptp_clock_event *src)
  49. {
  50. struct ptp_extts_event *dst;
  51. unsigned long flags;
  52. s64 seconds;
  53. u32 remainder;
  54. seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
  55. spin_lock_irqsave(&queue->lock, flags);
  56. dst = &queue->buf[queue->tail];
  57. dst->index = src->index;
  58. dst->t.sec = seconds;
  59. dst->t.nsec = remainder;
  60. if (!queue_free(queue))
  61. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  62. queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS;
  63. spin_unlock_irqrestore(&queue->lock, flags);
  64. }
  65. static s32 scaled_ppm_to_ppb(long ppm)
  66. {
  67. /*
  68. * The 'freq' field in the 'struct timex' is in parts per
  69. * million, but with a 16 bit binary fractional field.
  70. *
  71. * We want to calculate
  72. *
  73. * ppb = scaled_ppm * 1000 / 2^16
  74. *
  75. * which simplifies to
  76. *
  77. * ppb = scaled_ppm * 125 / 2^13
  78. */
  79. s64 ppb = 1 + ppm;
  80. ppb *= 125;
  81. ppb >>= 13;
  82. return (s32) ppb;
  83. }
  84. /* posix clock implementation */
  85. static int ptp_clock_getres(struct posix_clock *pc, struct timespec *tp)
  86. {
  87. tp->tv_sec = 0;
  88. tp->tv_nsec = 1;
  89. return 0;
  90. }
  91. static int ptp_clock_settime(struct posix_clock *pc, const struct timespec *tp)
  92. {
  93. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  94. return ptp->info->settime(ptp->info, tp);
  95. }
  96. static int ptp_clock_gettime(struct posix_clock *pc, struct timespec *tp)
  97. {
  98. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  99. return ptp->info->gettime(ptp->info, tp);
  100. }
  101. static int ptp_clock_adjtime(struct posix_clock *pc, struct timex *tx)
  102. {
  103. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  104. struct ptp_clock_info *ops;
  105. int err = -EOPNOTSUPP;
  106. ops = ptp->info;
  107. if (tx->modes & ADJ_SETOFFSET) {
  108. struct timespec ts;
  109. ktime_t kt;
  110. s64 delta;
  111. ts.tv_sec = tx->time.tv_sec;
  112. ts.tv_nsec = tx->time.tv_usec;
  113. if (!(tx->modes & ADJ_NANO))
  114. ts.tv_nsec *= 1000;
  115. if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
  116. return -EINVAL;
  117. kt = timespec_to_ktime(ts);
  118. delta = ktime_to_ns(kt);
  119. err = ops->adjtime(ops, delta);
  120. } else if (tx->modes & ADJ_FREQUENCY) {
  121. err = ops->adjfreq(ops, scaled_ppm_to_ppb(tx->freq));
  122. ptp->dialed_frequency = tx->freq;
  123. } else if (tx->modes == 0) {
  124. tx->freq = ptp->dialed_frequency;
  125. err = 0;
  126. }
  127. return err;
  128. }
  129. static struct posix_clock_operations ptp_clock_ops = {
  130. .owner = THIS_MODULE,
  131. .clock_adjtime = ptp_clock_adjtime,
  132. .clock_gettime = ptp_clock_gettime,
  133. .clock_getres = ptp_clock_getres,
  134. .clock_settime = ptp_clock_settime,
  135. .ioctl = ptp_ioctl,
  136. .open = ptp_open,
  137. .poll = ptp_poll,
  138. .read = ptp_read,
  139. };
  140. static void delete_ptp_clock(struct posix_clock *pc)
  141. {
  142. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  143. mutex_destroy(&ptp->tsevq_mux);
  144. /* Remove the clock from the bit map. */
  145. mutex_lock(&ptp_clocks_mutex);
  146. clear_bit(ptp->index, ptp_clocks_map);
  147. mutex_unlock(&ptp_clocks_mutex);
  148. kfree(ptp);
  149. }
  150. /* public interface */
  151. struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  152. struct device *parent)
  153. {
  154. struct ptp_clock *ptp;
  155. int err = 0, index, major = MAJOR(ptp_devt);
  156. if (info->n_alarm > PTP_MAX_ALARMS)
  157. return ERR_PTR(-EINVAL);
  158. /* Find a free clock slot and reserve it. */
  159. err = -EBUSY;
  160. mutex_lock(&ptp_clocks_mutex);
  161. index = find_first_zero_bit(ptp_clocks_map, PTP_MAX_CLOCKS);
  162. if (index < PTP_MAX_CLOCKS)
  163. set_bit(index, ptp_clocks_map);
  164. else
  165. goto no_slot;
  166. /* Initialize a clock structure. */
  167. err = -ENOMEM;
  168. ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
  169. if (ptp == NULL)
  170. goto no_memory;
  171. ptp->clock.ops = ptp_clock_ops;
  172. ptp->clock.release = delete_ptp_clock;
  173. ptp->info = info;
  174. ptp->devid = MKDEV(major, index);
  175. ptp->index = index;
  176. spin_lock_init(&ptp->tsevq.lock);
  177. mutex_init(&ptp->tsevq_mux);
  178. init_waitqueue_head(&ptp->tsev_wq);
  179. /* Create a new device in our class. */
  180. ptp->dev = device_create(ptp_class, parent, ptp->devid, ptp,
  181. "ptp%d", ptp->index);
  182. if (IS_ERR(ptp->dev))
  183. goto no_device;
  184. dev_set_drvdata(ptp->dev, ptp);
  185. err = ptp_populate_sysfs(ptp);
  186. if (err)
  187. goto no_sysfs;
  188. /* Register a new PPS source. */
  189. if (info->pps) {
  190. struct pps_source_info pps;
  191. memset(&pps, 0, sizeof(pps));
  192. snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
  193. pps.mode = PTP_PPS_MODE;
  194. pps.owner = info->owner;
  195. ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
  196. if (!ptp->pps_source) {
  197. pr_err("failed to register pps source\n");
  198. goto no_pps;
  199. }
  200. }
  201. /* Create a posix clock. */
  202. err = posix_clock_register(&ptp->clock, ptp->devid);
  203. if (err) {
  204. pr_err("failed to create posix clock\n");
  205. goto no_clock;
  206. }
  207. mutex_unlock(&ptp_clocks_mutex);
  208. return ptp;
  209. no_clock:
  210. if (ptp->pps_source)
  211. pps_unregister_source(ptp->pps_source);
  212. no_pps:
  213. ptp_cleanup_sysfs(ptp);
  214. no_sysfs:
  215. device_destroy(ptp_class, ptp->devid);
  216. no_device:
  217. mutex_destroy(&ptp->tsevq_mux);
  218. kfree(ptp);
  219. no_memory:
  220. clear_bit(index, ptp_clocks_map);
  221. no_slot:
  222. mutex_unlock(&ptp_clocks_mutex);
  223. return ERR_PTR(err);
  224. }
  225. EXPORT_SYMBOL(ptp_clock_register);
  226. int ptp_clock_unregister(struct ptp_clock *ptp)
  227. {
  228. ptp->defunct = 1;
  229. wake_up_interruptible(&ptp->tsev_wq);
  230. /* Release the clock's resources. */
  231. if (ptp->pps_source)
  232. pps_unregister_source(ptp->pps_source);
  233. ptp_cleanup_sysfs(ptp);
  234. device_destroy(ptp_class, ptp->devid);
  235. posix_clock_unregister(&ptp->clock);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(ptp_clock_unregister);
  239. void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
  240. {
  241. struct pps_event_time evt;
  242. switch (event->type) {
  243. case PTP_CLOCK_ALARM:
  244. break;
  245. case PTP_CLOCK_EXTTS:
  246. enqueue_external_timestamp(&ptp->tsevq, event);
  247. wake_up_interruptible(&ptp->tsev_wq);
  248. break;
  249. case PTP_CLOCK_PPS:
  250. pps_get_ts(&evt);
  251. pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
  252. break;
  253. case PTP_CLOCK_PPSUSR:
  254. pps_event(ptp->pps_source, &event->pps_times,
  255. PTP_PPS_EVENT, NULL);
  256. break;
  257. }
  258. }
  259. EXPORT_SYMBOL(ptp_clock_event);
  260. int ptp_clock_index(struct ptp_clock *ptp)
  261. {
  262. return ptp->index;
  263. }
  264. EXPORT_SYMBOL(ptp_clock_index);
  265. /* module operations */
  266. static void __exit ptp_exit(void)
  267. {
  268. class_destroy(ptp_class);
  269. unregister_chrdev_region(ptp_devt, PTP_MAX_CLOCKS);
  270. }
  271. static int __init ptp_init(void)
  272. {
  273. int err;
  274. ptp_class = class_create(THIS_MODULE, "ptp");
  275. if (IS_ERR(ptp_class)) {
  276. pr_err("ptp: failed to allocate class\n");
  277. return PTR_ERR(ptp_class);
  278. }
  279. err = alloc_chrdev_region(&ptp_devt, 0, PTP_MAX_CLOCKS, "ptp");
  280. if (err < 0) {
  281. pr_err("ptp: failed to allocate device region\n");
  282. goto no_region;
  283. }
  284. ptp_class->dev_attrs = ptp_dev_attrs;
  285. pr_info("PTP clock support registered\n");
  286. return 0;
  287. no_region:
  288. class_destroy(ptp_class);
  289. return err;
  290. }
  291. subsys_initcall(ptp_init);
  292. module_exit(ptp_exit);
  293. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
  294. MODULE_DESCRIPTION("PTP clocks support");
  295. MODULE_LICENSE("GPL");