ptp_clock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PTP 1588 clock support
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. */
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/posix-clock.h>
  13. #include <linux/pps_kernel.h>
  14. #include <linux/property.h>
  15. #include <linux/slab.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/xarray.h>
  20. #include <uapi/linux/sched/types.h>
  21. #include "ptp_private.h"
  22. #define PTP_MAX_ALARMS 4
  23. #define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT)
  24. #define PTP_PPS_EVENT PPS_CAPTUREASSERT
  25. #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
  26. const struct class ptp_class = {
  27. .name = "ptp",
  28. .dev_groups = ptp_groups
  29. };
  30. /* private globals */
  31. static dev_t ptp_devt;
  32. static DEFINE_XARRAY_ALLOC(ptp_clocks_map);
  33. /* time stamp event queue operations */
  34. static inline int queue_free(struct timestamp_event_queue *q)
  35. {
  36. return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
  37. }
  38. static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
  39. struct ptp_clock_event *src)
  40. {
  41. struct ptp_extts_event *dst;
  42. struct timespec64 offset_ts;
  43. unsigned long flags;
  44. s64 seconds;
  45. u32 remainder;
  46. if (src->type == PTP_CLOCK_EXTTS) {
  47. seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
  48. } else if (src->type == PTP_CLOCK_EXTOFF) {
  49. offset_ts = ns_to_timespec64(src->offset);
  50. seconds = offset_ts.tv_sec;
  51. remainder = offset_ts.tv_nsec;
  52. } else {
  53. WARN(1, "%s: unknown type %d\n", __func__, src->type);
  54. return;
  55. }
  56. spin_lock_irqsave(&queue->lock, flags);
  57. dst = &queue->buf[queue->tail];
  58. dst->index = src->index;
  59. dst->flags = PTP_EXTTS_EVENT_VALID;
  60. dst->t.sec = seconds;
  61. dst->t.nsec = remainder;
  62. if (src->type == PTP_CLOCK_EXTOFF)
  63. dst->flags |= PTP_EXT_OFFSET;
  64. /* Both WRITE_ONCE() are paired with READ_ONCE() in queue_cnt() */
  65. if (!queue_free(queue))
  66. WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
  67. WRITE_ONCE(queue->tail, (queue->tail + 1) % PTP_MAX_TIMESTAMPS);
  68. spin_unlock_irqrestore(&queue->lock, flags);
  69. }
  70. /* posix clock implementation */
  71. static int ptp_clock_getres(struct posix_clock *pc, struct timespec64 *tp)
  72. {
  73. tp->tv_sec = 0;
  74. tp->tv_nsec = 1;
  75. return 0;
  76. }
  77. static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp)
  78. {
  79. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  80. if (ptp_clock_freerun(ptp)) {
  81. pr_err_ratelimited("ptp: physical clock is free running\n");
  82. return -EBUSY;
  83. }
  84. if (!timespec64_valid_settod(tp))
  85. return -EINVAL;
  86. return ptp->info->settime64(ptp->info, tp);
  87. }
  88. static int ptp_clock_gettime(struct posix_clock *pc, struct timespec64 *tp)
  89. {
  90. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  91. int err;
  92. if (ptp->info->gettimex64)
  93. err = ptp->info->gettimex64(ptp->info, tp, NULL);
  94. else
  95. err = ptp->info->gettime64(ptp->info, tp);
  96. return err;
  97. }
  98. static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
  99. {
  100. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  101. struct ptp_clock_info *ops;
  102. int err = -EOPNOTSUPP;
  103. if (tx->modes & (ADJ_SETOFFSET | ADJ_FREQUENCY | ADJ_OFFSET) &&
  104. ptp_clock_freerun(ptp)) {
  105. pr_err("ptp: physical clock is free running\n");
  106. return -EBUSY;
  107. }
  108. ops = ptp->info;
  109. if (tx->modes & ADJ_SETOFFSET) {
  110. struct timespec64 ts, ts2;
  111. ktime_t kt;
  112. s64 delta;
  113. ts.tv_sec = tx->time.tv_sec;
  114. ts.tv_nsec = tx->time.tv_usec;
  115. if (!(tx->modes & ADJ_NANO))
  116. ts.tv_nsec *= 1000;
  117. if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
  118. return -EINVAL;
  119. /* Make sure the offset is valid */
  120. err = ptp_clock_gettime(pc, &ts2);
  121. if (err)
  122. return err;
  123. ts2 = timespec64_add(ts2, ts);
  124. if (!timespec64_valid_settod(&ts2))
  125. return -EINVAL;
  126. kt = timespec64_to_ktime(ts);
  127. delta = ktime_to_ns(kt);
  128. err = ops->adjtime(ops, delta);
  129. } else if (tx->modes & ADJ_FREQUENCY) {
  130. long ppb = scaled_ppm_to_ppb(tx->freq);
  131. if (ppb > ops->max_adj || ppb < -ops->max_adj)
  132. return -ERANGE;
  133. err = ops->adjfine(ops, tx->freq);
  134. if (!err)
  135. ptp->dialed_frequency = tx->freq;
  136. } else if (tx->modes & ADJ_OFFSET) {
  137. if (ops->adjphase) {
  138. s32 max_phase_adj = ops->getmaxphase(ops);
  139. s32 offset = tx->offset;
  140. if (!(tx->modes & ADJ_NANO))
  141. offset *= NSEC_PER_USEC;
  142. if (offset > max_phase_adj || offset < -max_phase_adj)
  143. return -ERANGE;
  144. err = ops->adjphase(ops, offset);
  145. }
  146. } else if (tx->modes == 0) {
  147. tx->freq = ptp->dialed_frequency;
  148. err = 0;
  149. }
  150. return err;
  151. }
  152. static struct posix_clock_operations ptp_clock_ops = {
  153. .owner = THIS_MODULE,
  154. .clock_adjtime = ptp_clock_adjtime,
  155. .clock_gettime = ptp_clock_gettime,
  156. .clock_getres = ptp_clock_getres,
  157. .clock_settime = ptp_clock_settime,
  158. .ioctl = ptp_ioctl,
  159. .open = ptp_open,
  160. .release = ptp_release,
  161. .poll = ptp_poll,
  162. .read = ptp_read,
  163. };
  164. static void ptp_clock_release(struct device *dev)
  165. {
  166. struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
  167. struct timestamp_event_queue *tsevq;
  168. unsigned long flags;
  169. ptp_cleanup_pin_groups(ptp);
  170. kfree(ptp->vclock_index);
  171. mutex_destroy(&ptp->pincfg_mux);
  172. mutex_destroy(&ptp->n_vclocks_mux);
  173. /* Delete first entry */
  174. spin_lock_irqsave(&ptp->tsevqs_lock, flags);
  175. tsevq = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue,
  176. qlist);
  177. list_del(&tsevq->qlist);
  178. spin_unlock_irqrestore(&ptp->tsevqs_lock, flags);
  179. bitmap_free(tsevq->mask);
  180. kfree(tsevq);
  181. debugfs_remove(ptp->debugfs_root);
  182. xa_erase(&ptp_clocks_map, ptp->index);
  183. kfree(ptp);
  184. }
  185. static int ptp_getcycles64(struct ptp_clock_info *info, struct timespec64 *ts)
  186. {
  187. if (info->getcyclesx64)
  188. return info->getcyclesx64(info, ts, NULL);
  189. else
  190. return info->gettime64(info, ts);
  191. }
  192. static int ptp_enable(struct ptp_clock_info *ptp, struct ptp_clock_request *request, int on)
  193. {
  194. return -EOPNOTSUPP;
  195. }
  196. static void ptp_aux_kworker(struct kthread_work *work)
  197. {
  198. struct ptp_clock *ptp = container_of(work, struct ptp_clock,
  199. aux_work.work);
  200. struct ptp_clock_info *info = ptp->info;
  201. long delay;
  202. delay = info->do_aux_work(info);
  203. if (delay >= 0)
  204. kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay);
  205. }
  206. static ssize_t ptp_n_perout_loopback_read(struct file *filep,
  207. char __user *buffer,
  208. size_t count, loff_t *pos)
  209. {
  210. struct ptp_clock *ptp = filep->private_data;
  211. char buf[12] = {};
  212. snprintf(buf, sizeof(buf), "%d\n", ptp->info->n_per_lp);
  213. return simple_read_from_buffer(buffer, count, pos, buf, strlen(buf));
  214. }
  215. static const struct file_operations ptp_n_perout_loopback_fops = {
  216. .owner = THIS_MODULE,
  217. .open = simple_open,
  218. .read = ptp_n_perout_loopback_read,
  219. };
  220. static ssize_t ptp_perout_loopback_write(struct file *filep,
  221. const char __user *buffer,
  222. size_t count, loff_t *ppos)
  223. {
  224. struct ptp_clock *ptp = filep->private_data;
  225. struct ptp_clock_info *ops = ptp->info;
  226. unsigned int index, enable;
  227. int len, cnt, err;
  228. char buf[32] = {};
  229. if (*ppos || !count)
  230. return -EINVAL;
  231. if (count >= sizeof(buf))
  232. return -ENOSPC;
  233. len = simple_write_to_buffer(buf, sizeof(buf) - 1,
  234. ppos, buffer, count);
  235. if (len < 0)
  236. return len;
  237. buf[len] = '\0';
  238. cnt = sscanf(buf, "%u %u", &index, &enable);
  239. if (cnt != 2)
  240. return -EINVAL;
  241. if (index >= ops->n_per_lp)
  242. return -EINVAL;
  243. if (enable != 0 && enable != 1)
  244. return -EINVAL;
  245. err = ops->perout_loopback(ops, index, enable);
  246. if (err)
  247. return err;
  248. return count;
  249. }
  250. static const struct file_operations ptp_perout_loopback_ops = {
  251. .owner = THIS_MODULE,
  252. .open = simple_open,
  253. .write = ptp_perout_loopback_write,
  254. };
  255. /* public interface */
  256. struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  257. struct device *parent)
  258. {
  259. struct ptp_clock *ptp;
  260. struct timestamp_event_queue *queue = NULL;
  261. int err, index, major = MAJOR(ptp_devt);
  262. char debugfsname[16];
  263. size_t size;
  264. if (WARN_ON_ONCE(info->n_alarm > PTP_MAX_ALARMS ||
  265. (!info->gettimex64 && !info->gettime64) ||
  266. !info->settime64))
  267. return ERR_PTR(-EINVAL);
  268. /* Initialize a clock structure. */
  269. ptp = kzalloc_obj(struct ptp_clock);
  270. if (!ptp) {
  271. err = -ENOMEM;
  272. goto no_memory;
  273. }
  274. err = xa_alloc(&ptp_clocks_map, &index, ptp, xa_limit_31b,
  275. GFP_KERNEL);
  276. if (err)
  277. goto no_slot;
  278. ptp->clock.ops = ptp_clock_ops;
  279. ptp->info = info;
  280. ptp->devid = MKDEV(major, index);
  281. ptp->index = index;
  282. INIT_LIST_HEAD(&ptp->tsevqs);
  283. queue = kzalloc_obj(*queue);
  284. if (!queue) {
  285. err = -ENOMEM;
  286. goto no_memory_queue;
  287. }
  288. list_add_tail(&queue->qlist, &ptp->tsevqs);
  289. spin_lock_init(&ptp->tsevqs_lock);
  290. queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL);
  291. if (!queue->mask) {
  292. err = -ENOMEM;
  293. goto no_memory_bitmap;
  294. }
  295. bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS);
  296. spin_lock_init(&queue->lock);
  297. mutex_init(&ptp->pincfg_mux);
  298. mutex_init(&ptp->n_vclocks_mux);
  299. init_waitqueue_head(&ptp->tsev_wq);
  300. if (ptp->info->getcycles64 || ptp->info->getcyclesx64) {
  301. ptp->has_cycles = true;
  302. if (!ptp->info->getcycles64 && ptp->info->getcyclesx64)
  303. ptp->info->getcycles64 = ptp_getcycles64;
  304. } else {
  305. /* Free running cycle counter not supported, use time. */
  306. ptp->info->getcycles64 = ptp_getcycles64;
  307. if (ptp->info->gettimex64)
  308. ptp->info->getcyclesx64 = ptp->info->gettimex64;
  309. if (ptp->info->getcrosststamp)
  310. ptp->info->getcrosscycles = ptp->info->getcrosststamp;
  311. }
  312. if (!ptp->info->enable)
  313. ptp->info->enable = ptp_enable;
  314. if (ptp->info->do_aux_work) {
  315. kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker);
  316. ptp->kworker = kthread_run_worker(0, "ptp%d", ptp->index);
  317. if (IS_ERR(ptp->kworker)) {
  318. err = PTR_ERR(ptp->kworker);
  319. pr_err("failed to create ptp aux_worker %d\n", err);
  320. goto kworker_err;
  321. }
  322. }
  323. /* PTP virtual clock is being registered under physical clock */
  324. if (parent && parent->class && parent->class->name &&
  325. strcmp(parent->class->name, "ptp") == 0)
  326. ptp->is_virtual_clock = true;
  327. if (!ptp->is_virtual_clock) {
  328. ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS;
  329. size = sizeof(int) * ptp->max_vclocks;
  330. ptp->vclock_index = kzalloc(size, GFP_KERNEL);
  331. if (!ptp->vclock_index) {
  332. err = -ENOMEM;
  333. goto no_mem_for_vclocks;
  334. }
  335. }
  336. err = ptp_populate_pin_groups(ptp);
  337. if (err)
  338. goto no_pin_groups;
  339. /* Register a new PPS source. */
  340. if (info->pps) {
  341. struct pps_source_info pps;
  342. memset(&pps, 0, sizeof(pps));
  343. snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
  344. pps.mode = PTP_PPS_MODE;
  345. pps.owner = info->owner;
  346. ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
  347. if (IS_ERR(ptp->pps_source)) {
  348. err = PTR_ERR(ptp->pps_source);
  349. pr_err("failed to register pps source\n");
  350. goto no_pps;
  351. }
  352. ptp->pps_source->lookup_cookie = ptp;
  353. }
  354. /* Initialize a new device of our class in our clock structure. */
  355. device_initialize(&ptp->dev);
  356. ptp->dev.devt = ptp->devid;
  357. ptp->dev.class = &ptp_class;
  358. ptp->dev.parent = parent;
  359. ptp->dev.groups = ptp->pin_attr_groups;
  360. ptp->dev.release = ptp_clock_release;
  361. dev_set_drvdata(&ptp->dev, ptp);
  362. dev_set_name(&ptp->dev, "ptp%d", ptp->index);
  363. /* Create a posix clock and link it to the device. */
  364. err = posix_clock_register(&ptp->clock, &ptp->dev);
  365. if (err) {
  366. if (ptp->pps_source)
  367. pps_unregister_source(ptp->pps_source);
  368. if (ptp->kworker)
  369. kthread_destroy_worker(ptp->kworker);
  370. put_device(&ptp->dev);
  371. pr_err("failed to create posix clock\n");
  372. return ERR_PTR(err);
  373. }
  374. /* Debugfs initialization */
  375. snprintf(debugfsname, sizeof(debugfsname), "ptp%d", ptp->index);
  376. ptp->debugfs_root = debugfs_create_dir(debugfsname, NULL);
  377. if (info->n_per_lp > 0 && info->perout_loopback) {
  378. debugfs_create_file("n_perout_loopback", 0400, ptp->debugfs_root,
  379. ptp, &ptp_n_perout_loopback_fops);
  380. debugfs_create_file("perout_loopback", 0200, ptp->debugfs_root,
  381. ptp, &ptp_perout_loopback_ops);
  382. }
  383. return ptp;
  384. no_pps:
  385. ptp_cleanup_pin_groups(ptp);
  386. no_pin_groups:
  387. kfree(ptp->vclock_index);
  388. no_mem_for_vclocks:
  389. if (ptp->kworker)
  390. kthread_destroy_worker(ptp->kworker);
  391. kworker_err:
  392. mutex_destroy(&ptp->pincfg_mux);
  393. mutex_destroy(&ptp->n_vclocks_mux);
  394. bitmap_free(queue->mask);
  395. no_memory_bitmap:
  396. list_del(&queue->qlist);
  397. kfree(queue);
  398. no_memory_queue:
  399. xa_erase(&ptp_clocks_map, index);
  400. no_slot:
  401. kfree(ptp);
  402. no_memory:
  403. return ERR_PTR(err);
  404. }
  405. EXPORT_SYMBOL(ptp_clock_register);
  406. static int unregister_vclock(struct device *dev, void *data)
  407. {
  408. struct ptp_clock *ptp = dev_get_drvdata(dev);
  409. ptp_vclock_unregister(info_to_vclock(ptp->info));
  410. return 0;
  411. }
  412. int ptp_clock_unregister(struct ptp_clock *ptp)
  413. {
  414. if (ptp_vclock_in_use(ptp)) {
  415. device_for_each_child(&ptp->dev, NULL, unregister_vclock);
  416. }
  417. /* Get the device to stop posix_clock_unregister() doing the last put
  418. * and freeing the structure(s)
  419. */
  420. get_device(&ptp->dev);
  421. /* Wake up any userspace waiting for an event. */
  422. ptp->defunct = 1;
  423. wake_up_interruptible(&ptp->tsev_wq);
  424. /* Tear down the POSIX clock, which removes the user interface. */
  425. posix_clock_unregister(&ptp->clock);
  426. /* Disable all sources of event generation. */
  427. ptp_disable_all_events(ptp);
  428. if (ptp->kworker) {
  429. kthread_cancel_delayed_work_sync(&ptp->aux_work);
  430. kthread_destroy_worker(ptp->kworker);
  431. }
  432. /* Release the clock's resources. */
  433. if (ptp->pps_source)
  434. pps_unregister_source(ptp->pps_source);
  435. /* The final put, normally here, will invoke ptp_clock_release(). */
  436. put_device(&ptp->dev);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(ptp_clock_unregister);
  440. void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
  441. {
  442. struct timestamp_event_queue *tsevq;
  443. struct pps_event_time evt;
  444. unsigned long flags;
  445. switch (event->type) {
  446. case PTP_CLOCK_ALARM:
  447. break;
  448. case PTP_CLOCK_EXTTS:
  449. case PTP_CLOCK_EXTOFF:
  450. /* Enqueue timestamp on selected queues */
  451. spin_lock_irqsave(&ptp->tsevqs_lock, flags);
  452. list_for_each_entry(tsevq, &ptp->tsevqs, qlist) {
  453. if (test_bit((unsigned int)event->index, tsevq->mask))
  454. enqueue_external_timestamp(tsevq, event);
  455. }
  456. spin_unlock_irqrestore(&ptp->tsevqs_lock, flags);
  457. wake_up_interruptible(&ptp->tsev_wq);
  458. break;
  459. case PTP_CLOCK_PPS:
  460. pps_get_ts(&evt);
  461. pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
  462. break;
  463. case PTP_CLOCK_PPSUSR:
  464. pps_event(ptp->pps_source, &event->pps_times,
  465. PTP_PPS_EVENT, NULL);
  466. break;
  467. }
  468. }
  469. EXPORT_SYMBOL(ptp_clock_event);
  470. int ptp_clock_index(struct ptp_clock *ptp)
  471. {
  472. return ptp->index;
  473. }
  474. EXPORT_SYMBOL(ptp_clock_index);
  475. static int ptp_clock_of_node_match(struct device *dev, const void *data)
  476. {
  477. const struct device_node *parent_np = data;
  478. return (dev->parent && dev_of_node(dev->parent) == parent_np);
  479. }
  480. int ptp_clock_index_by_of_node(struct device_node *np)
  481. {
  482. struct ptp_clock *ptp;
  483. struct device *dev;
  484. int phc_index;
  485. dev = class_find_device(&ptp_class, NULL, np,
  486. ptp_clock_of_node_match);
  487. if (!dev)
  488. return -1;
  489. ptp = dev_get_drvdata(dev);
  490. phc_index = ptp_clock_index(ptp);
  491. put_device(dev);
  492. return phc_index;
  493. }
  494. EXPORT_SYMBOL_GPL(ptp_clock_index_by_of_node);
  495. static int ptp_clock_dev_match(struct device *dev, const void *data)
  496. {
  497. const struct device *parent = data;
  498. return dev->parent == parent;
  499. }
  500. int ptp_clock_index_by_dev(struct device *parent)
  501. {
  502. struct ptp_clock *ptp;
  503. struct device *dev;
  504. int phc_index;
  505. dev = class_find_device(&ptp_class, NULL, parent,
  506. ptp_clock_dev_match);
  507. if (!dev)
  508. return -1;
  509. ptp = dev_get_drvdata(dev);
  510. phc_index = ptp_clock_index(ptp);
  511. put_device(dev);
  512. return phc_index;
  513. }
  514. EXPORT_SYMBOL_GPL(ptp_clock_index_by_dev);
  515. int ptp_find_pin(struct ptp_clock *ptp,
  516. enum ptp_pin_function func, unsigned int chan)
  517. {
  518. struct ptp_pin_desc *pin = NULL;
  519. int i;
  520. for (i = 0; i < ptp->info->n_pins; i++) {
  521. if (ptp->info->pin_config[i].func == func &&
  522. ptp->info->pin_config[i].chan == chan) {
  523. pin = &ptp->info->pin_config[i];
  524. break;
  525. }
  526. }
  527. return pin ? i : -1;
  528. }
  529. EXPORT_SYMBOL(ptp_find_pin);
  530. int ptp_find_pin_unlocked(struct ptp_clock *ptp,
  531. enum ptp_pin_function func, unsigned int chan)
  532. {
  533. int result;
  534. mutex_lock(&ptp->pincfg_mux);
  535. result = ptp_find_pin(ptp, func, chan);
  536. mutex_unlock(&ptp->pincfg_mux);
  537. return result;
  538. }
  539. EXPORT_SYMBOL(ptp_find_pin_unlocked);
  540. int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay)
  541. {
  542. return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay);
  543. }
  544. EXPORT_SYMBOL(ptp_schedule_worker);
  545. void ptp_cancel_worker_sync(struct ptp_clock *ptp)
  546. {
  547. kthread_cancel_delayed_work_sync(&ptp->aux_work);
  548. }
  549. EXPORT_SYMBOL(ptp_cancel_worker_sync);
  550. /* module operations */
  551. static void __exit ptp_exit(void)
  552. {
  553. class_unregister(&ptp_class);
  554. unregister_chrdev_region(ptp_devt, MINORMASK + 1);
  555. xa_destroy(&ptp_clocks_map);
  556. }
  557. static int __init ptp_init(void)
  558. {
  559. int err;
  560. err = class_register(&ptp_class);
  561. if (err) {
  562. pr_err("ptp: failed to allocate class\n");
  563. return err;
  564. }
  565. err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
  566. if (err < 0) {
  567. pr_err("ptp: failed to allocate device region\n");
  568. goto no_region;
  569. }
  570. pr_info("PTP clock support registered\n");
  571. return 0;
  572. no_region:
  573. class_unregister(&ptp_class);
  574. return err;
  575. }
  576. subsys_initcall(ptp_init);
  577. module_exit(ptp_exit);
  578. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
  579. MODULE_DESCRIPTION("PTP clocks support");
  580. MODULE_LICENSE("GPL");