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