ptp_clock.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/idr.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_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT)
  34. #define PTP_PPS_EVENT PPS_CAPTUREASSERT
  35. #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
  36. /* private globals */
  37. static dev_t ptp_devt;
  38. static struct class *ptp_class;
  39. static DEFINE_IDA(ptp_clocks_map);
  40. /* time stamp event queue operations */
  41. static inline int queue_free(struct timestamp_event_queue *q)
  42. {
  43. return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
  44. }
  45. static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
  46. struct ptp_clock_event *src)
  47. {
  48. struct ptp_extts_event *dst;
  49. unsigned long flags;
  50. s64 seconds;
  51. u32 remainder;
  52. seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
  53. spin_lock_irqsave(&queue->lock, flags);
  54. dst = &queue->buf[queue->tail];
  55. dst->index = src->index;
  56. dst->t.sec = seconds;
  57. dst->t.nsec = remainder;
  58. if (!queue_free(queue))
  59. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  60. queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS;
  61. spin_unlock_irqrestore(&queue->lock, flags);
  62. }
  63. static s32 scaled_ppm_to_ppb(long ppm)
  64. {
  65. /*
  66. * The 'freq' field in the 'struct timex' is in parts per
  67. * million, but with a 16 bit binary fractional field.
  68. *
  69. * We want to calculate
  70. *
  71. * ppb = scaled_ppm * 1000 / 2^16
  72. *
  73. * which simplifies to
  74. *
  75. * ppb = scaled_ppm * 125 / 2^13
  76. */
  77. s64 ppb = 1 + ppm;
  78. ppb *= 125;
  79. ppb >>= 13;
  80. return (s32) ppb;
  81. }
  82. /* posix clock implementation */
  83. static int ptp_clock_getres(struct posix_clock *pc, struct timespec *tp)
  84. {
  85. tp->tv_sec = 0;
  86. tp->tv_nsec = 1;
  87. return 0;
  88. }
  89. static int ptp_clock_settime(struct posix_clock *pc, const struct timespec *tp)
  90. {
  91. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  92. struct timespec64 ts = timespec_to_timespec64(*tp);
  93. return ptp->info->settime64(ptp->info, &ts);
  94. }
  95. static int ptp_clock_gettime(struct posix_clock *pc, struct timespec *tp)
  96. {
  97. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  98. struct timespec64 ts;
  99. int err;
  100. err = ptp->info->gettime64(ptp->info, &ts);
  101. if (!err)
  102. *tp = timespec64_to_timespec(ts);
  103. return err;
  104. }
  105. static int ptp_clock_adjtime(struct posix_clock *pc, struct timex *tx)
  106. {
  107. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  108. struct ptp_clock_info *ops;
  109. int err = -EOPNOTSUPP;
  110. ops = ptp->info;
  111. if (tx->modes & ADJ_SETOFFSET) {
  112. struct timespec ts;
  113. ktime_t kt;
  114. s64 delta;
  115. ts.tv_sec = tx->time.tv_sec;
  116. ts.tv_nsec = tx->time.tv_usec;
  117. if (!(tx->modes & ADJ_NANO))
  118. ts.tv_nsec *= 1000;
  119. if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
  120. return -EINVAL;
  121. kt = timespec_to_ktime(ts);
  122. delta = ktime_to_ns(kt);
  123. err = ops->adjtime(ops, delta);
  124. } else if (tx->modes & ADJ_FREQUENCY) {
  125. s32 ppb = scaled_ppm_to_ppb(tx->freq);
  126. if (ppb > ops->max_adj || ppb < -ops->max_adj)
  127. return -ERANGE;
  128. if (ops->adjfine)
  129. err = ops->adjfine(ops, tx->freq);
  130. else
  131. err = ops->adjfreq(ops, ppb);
  132. ptp->dialed_frequency = tx->freq;
  133. } else if (tx->modes == 0) {
  134. tx->freq = ptp->dialed_frequency;
  135. err = 0;
  136. }
  137. return err;
  138. }
  139. static struct posix_clock_operations ptp_clock_ops = {
  140. .owner = THIS_MODULE,
  141. .clock_adjtime = ptp_clock_adjtime,
  142. .clock_gettime = ptp_clock_gettime,
  143. .clock_getres = ptp_clock_getres,
  144. .clock_settime = ptp_clock_settime,
  145. .ioctl = ptp_ioctl,
  146. .open = ptp_open,
  147. .poll = ptp_poll,
  148. .read = ptp_read,
  149. };
  150. static void delete_ptp_clock(struct posix_clock *pc)
  151. {
  152. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  153. mutex_destroy(&ptp->tsevq_mux);
  154. mutex_destroy(&ptp->pincfg_mux);
  155. ida_simple_remove(&ptp_clocks_map, ptp->index);
  156. kfree(ptp);
  157. }
  158. /* public interface */
  159. struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  160. struct device *parent)
  161. {
  162. struct ptp_clock *ptp;
  163. int err = 0, index, major = MAJOR(ptp_devt);
  164. if (info->n_alarm > PTP_MAX_ALARMS)
  165. return ERR_PTR(-EINVAL);
  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. index = ida_simple_get(&ptp_clocks_map, 0, MINORMASK + 1, GFP_KERNEL);
  172. if (index < 0) {
  173. err = index;
  174. goto no_slot;
  175. }
  176. ptp->clock.ops = ptp_clock_ops;
  177. ptp->clock.release = delete_ptp_clock;
  178. ptp->info = info;
  179. ptp->devid = MKDEV(major, index);
  180. ptp->index = index;
  181. spin_lock_init(&ptp->tsevq.lock);
  182. mutex_init(&ptp->tsevq_mux);
  183. mutex_init(&ptp->pincfg_mux);
  184. init_waitqueue_head(&ptp->tsev_wq);
  185. /* Create a new device in our class. */
  186. ptp->dev = device_create(ptp_class, parent, ptp->devid, ptp,
  187. "ptp%d", ptp->index);
  188. if (IS_ERR(ptp->dev))
  189. goto no_device;
  190. dev_set_drvdata(ptp->dev, ptp);
  191. err = ptp_populate_sysfs(ptp);
  192. if (err)
  193. goto no_sysfs;
  194. /* Register a new PPS source. */
  195. if (info->pps) {
  196. struct pps_source_info pps;
  197. memset(&pps, 0, sizeof(pps));
  198. snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
  199. pps.mode = PTP_PPS_MODE;
  200. pps.owner = info->owner;
  201. ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
  202. if (!ptp->pps_source) {
  203. pr_err("failed to register pps source\n");
  204. goto no_pps;
  205. }
  206. }
  207. /* Create a posix clock. */
  208. err = posix_clock_register(&ptp->clock, ptp->devid);
  209. if (err) {
  210. pr_err("failed to create posix clock\n");
  211. goto no_clock;
  212. }
  213. return ptp;
  214. no_clock:
  215. if (ptp->pps_source)
  216. pps_unregister_source(ptp->pps_source);
  217. no_pps:
  218. ptp_cleanup_sysfs(ptp);
  219. no_sysfs:
  220. device_destroy(ptp_class, ptp->devid);
  221. no_device:
  222. mutex_destroy(&ptp->tsevq_mux);
  223. mutex_destroy(&ptp->pincfg_mux);
  224. ida_simple_remove(&ptp_clocks_map, index);
  225. no_slot:
  226. kfree(ptp);
  227. no_memory:
  228. return ERR_PTR(err);
  229. }
  230. EXPORT_SYMBOL(ptp_clock_register);
  231. int ptp_clock_unregister(struct ptp_clock *ptp)
  232. {
  233. ptp->defunct = 1;
  234. wake_up_interruptible(&ptp->tsev_wq);
  235. /* Release the clock's resources. */
  236. if (ptp->pps_source)
  237. pps_unregister_source(ptp->pps_source);
  238. ptp_cleanup_sysfs(ptp);
  239. device_destroy(ptp_class, ptp->devid);
  240. posix_clock_unregister(&ptp->clock);
  241. return 0;
  242. }
  243. EXPORT_SYMBOL(ptp_clock_unregister);
  244. void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
  245. {
  246. struct pps_event_time evt;
  247. switch (event->type) {
  248. case PTP_CLOCK_ALARM:
  249. break;
  250. case PTP_CLOCK_EXTTS:
  251. enqueue_external_timestamp(&ptp->tsevq, event);
  252. wake_up_interruptible(&ptp->tsev_wq);
  253. break;
  254. case PTP_CLOCK_PPS:
  255. pps_get_ts(&evt);
  256. pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
  257. break;
  258. case PTP_CLOCK_PPSUSR:
  259. pps_event(ptp->pps_source, &event->pps_times,
  260. PTP_PPS_EVENT, NULL);
  261. break;
  262. }
  263. }
  264. EXPORT_SYMBOL(ptp_clock_event);
  265. int ptp_clock_index(struct ptp_clock *ptp)
  266. {
  267. return ptp->index;
  268. }
  269. EXPORT_SYMBOL(ptp_clock_index);
  270. int ptp_find_pin(struct ptp_clock *ptp,
  271. enum ptp_pin_function func, unsigned int chan)
  272. {
  273. struct ptp_pin_desc *pin = NULL;
  274. int i;
  275. mutex_lock(&ptp->pincfg_mux);
  276. for (i = 0; i < ptp->info->n_pins; i++) {
  277. if (ptp->info->pin_config[i].func == func &&
  278. ptp->info->pin_config[i].chan == chan) {
  279. pin = &ptp->info->pin_config[i];
  280. break;
  281. }
  282. }
  283. mutex_unlock(&ptp->pincfg_mux);
  284. return pin ? i : -1;
  285. }
  286. EXPORT_SYMBOL(ptp_find_pin);
  287. /* module operations */
  288. static void __exit ptp_exit(void)
  289. {
  290. class_destroy(ptp_class);
  291. unregister_chrdev_region(ptp_devt, MINORMASK + 1);
  292. ida_destroy(&ptp_clocks_map);
  293. }
  294. static int __init ptp_init(void)
  295. {
  296. int err;
  297. ptp_class = class_create(THIS_MODULE, "ptp");
  298. if (IS_ERR(ptp_class)) {
  299. pr_err("ptp: failed to allocate class\n");
  300. return PTR_ERR(ptp_class);
  301. }
  302. err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
  303. if (err < 0) {
  304. pr_err("ptp: failed to allocate device region\n");
  305. goto no_region;
  306. }
  307. ptp_class->dev_groups = ptp_groups;
  308. pr_info("PTP clock support registered\n");
  309. return 0;
  310. no_region:
  311. class_destroy(ptp_class);
  312. return err;
  313. }
  314. subsys_initcall(ptp_init);
  315. module_exit(ptp_exit);
  316. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
  317. MODULE_DESCRIPTION("PTP clocks support");
  318. MODULE_LICENSE("GPL");