ptp_clock.c 10.0 KB

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