Lines Matching +full:tx +full:- +full:freq
1 // SPDX-License-Identifier: GPL-2.0-or-later
12 #include <linux/posix-clock.h>
43 return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1; in queue_free()
55 if (src->type == PTP_CLOCK_EXTTS) { in enqueue_external_timestamp()
56 seconds = div_u64_rem(src->timestamp, 1000000000, &remainder); in enqueue_external_timestamp()
57 } else if (src->type == PTP_CLOCK_EXTOFF) { in enqueue_external_timestamp()
58 offset_ts = ns_to_timespec64(src->offset); in enqueue_external_timestamp()
62 WARN(1, "%s: unknown type %d\n", __func__, src->type); in enqueue_external_timestamp()
66 spin_lock_irqsave(&queue->lock, flags); in enqueue_external_timestamp()
68 dst = &queue->buf[queue->tail]; in enqueue_external_timestamp()
69 dst->index = src->index; in enqueue_external_timestamp()
70 dst->flags = PTP_EXTTS_EVENT_VALID; in enqueue_external_timestamp()
71 dst->t.sec = seconds; in enqueue_external_timestamp()
72 dst->t.nsec = remainder; in enqueue_external_timestamp()
73 if (src->type == PTP_CLOCK_EXTOFF) in enqueue_external_timestamp()
74 dst->flags |= PTP_EXT_OFFSET; in enqueue_external_timestamp()
78 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); in enqueue_external_timestamp()
80 WRITE_ONCE(queue->tail, (queue->tail + 1) % PTP_MAX_TIMESTAMPS); in enqueue_external_timestamp()
82 spin_unlock_irqrestore(&queue->lock, flags); in enqueue_external_timestamp()
89 tp->tv_sec = 0; in ptp_clock_getres()
90 tp->tv_nsec = 1; in ptp_clock_getres()
100 return -EBUSY; in ptp_clock_settime()
103 return ptp->info->settime64(ptp->info, tp); in ptp_clock_settime()
111 if (ptp->info->gettimex64) in ptp_clock_gettime()
112 err = ptp->info->gettimex64(ptp->info, tp, NULL); in ptp_clock_gettime()
114 err = ptp->info->gettime64(ptp->info, tp); in ptp_clock_gettime()
118 static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) in ptp_clock_adjtime() argument
122 int err = -EOPNOTSUPP; in ptp_clock_adjtime()
126 return -EBUSY; in ptp_clock_adjtime()
129 ops = ptp->info; in ptp_clock_adjtime()
131 if (tx->modes & ADJ_SETOFFSET) { in ptp_clock_adjtime()
136 ts.tv_sec = tx->time.tv_sec; in ptp_clock_adjtime()
137 ts.tv_nsec = tx->time.tv_usec; in ptp_clock_adjtime()
139 if (!(tx->modes & ADJ_NANO)) in ptp_clock_adjtime()
143 return -EINVAL; in ptp_clock_adjtime()
147 err = ops->adjtime(ops, delta); in ptp_clock_adjtime()
148 } else if (tx->modes & ADJ_FREQUENCY) { in ptp_clock_adjtime()
149 long ppb = scaled_ppm_to_ppb(tx->freq); in ptp_clock_adjtime()
150 if (ppb > ops->max_adj || ppb < -ops->max_adj) in ptp_clock_adjtime()
151 return -ERANGE; in ptp_clock_adjtime()
152 err = ops->adjfine(ops, tx->freq); in ptp_clock_adjtime()
154 ptp->dialed_frequency = tx->freq; in ptp_clock_adjtime()
155 } else if (tx->modes & ADJ_OFFSET) { in ptp_clock_adjtime()
156 if (ops->adjphase) { in ptp_clock_adjtime()
157 s32 max_phase_adj = ops->getmaxphase(ops); in ptp_clock_adjtime()
158 s32 offset = tx->offset; in ptp_clock_adjtime()
160 if (!(tx->modes & ADJ_NANO)) in ptp_clock_adjtime()
163 if (offset > max_phase_adj || offset < -max_phase_adj) in ptp_clock_adjtime()
164 return -ERANGE; in ptp_clock_adjtime()
166 err = ops->adjphase(ops, offset); in ptp_clock_adjtime()
168 } else if (tx->modes == 0) { in ptp_clock_adjtime()
169 tx->freq = ptp->dialed_frequency; in ptp_clock_adjtime()
196 kfree(ptp->vclock_index); in ptp_clock_release()
197 mutex_destroy(&ptp->pincfg_mux); in ptp_clock_release()
198 mutex_destroy(&ptp->n_vclocks_mux); in ptp_clock_release()
200 spin_lock_irqsave(&ptp->tsevqs_lock, flags); in ptp_clock_release()
201 tsevq = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue, in ptp_clock_release()
203 list_del(&tsevq->qlist); in ptp_clock_release()
204 spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); in ptp_clock_release()
205 bitmap_free(tsevq->mask); in ptp_clock_release()
207 debugfs_remove(ptp->debugfs_root); in ptp_clock_release()
208 xa_erase(&ptp_clocks_map, ptp->index); in ptp_clock_release()
214 if (info->getcyclesx64) in ptp_getcycles64()
215 return info->getcyclesx64(info, ts, NULL); in ptp_getcycles64()
217 return info->gettime64(info, ts); in ptp_getcycles64()
224 struct ptp_clock_info *info = ptp->info; in ptp_aux_kworker()
227 delay = info->do_aux_work(info); in ptp_aux_kworker()
230 kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay); in ptp_aux_kworker()
244 if (info->n_alarm > PTP_MAX_ALARMS) in ptp_clock_register()
245 return ERR_PTR(-EINVAL); in ptp_clock_register()
250 err = -ENOMEM; in ptp_clock_register()
259 ptp->clock.ops = ptp_clock_ops; in ptp_clock_register()
260 ptp->info = info; in ptp_clock_register()
261 ptp->devid = MKDEV(major, index); in ptp_clock_register()
262 ptp->index = index; in ptp_clock_register()
263 INIT_LIST_HEAD(&ptp->tsevqs); in ptp_clock_register()
266 err = -ENOMEM; in ptp_clock_register()
269 list_add_tail(&queue->qlist, &ptp->tsevqs); in ptp_clock_register()
270 spin_lock_init(&ptp->tsevqs_lock); in ptp_clock_register()
271 queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL); in ptp_clock_register()
272 if (!queue->mask) { in ptp_clock_register()
273 err = -ENOMEM; in ptp_clock_register()
276 bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); in ptp_clock_register()
277 spin_lock_init(&queue->lock); in ptp_clock_register()
278 mutex_init(&ptp->pincfg_mux); in ptp_clock_register()
279 mutex_init(&ptp->n_vclocks_mux); in ptp_clock_register()
280 init_waitqueue_head(&ptp->tsev_wq); in ptp_clock_register()
282 if (ptp->info->getcycles64 || ptp->info->getcyclesx64) { in ptp_clock_register()
283 ptp->has_cycles = true; in ptp_clock_register()
284 if (!ptp->info->getcycles64 && ptp->info->getcyclesx64) in ptp_clock_register()
285 ptp->info->getcycles64 = ptp_getcycles64; in ptp_clock_register()
288 ptp->info->getcycles64 = ptp_getcycles64; in ptp_clock_register()
290 if (ptp->info->gettimex64) in ptp_clock_register()
291 ptp->info->getcyclesx64 = ptp->info->gettimex64; in ptp_clock_register()
293 if (ptp->info->getcrosststamp) in ptp_clock_register()
294 ptp->info->getcrosscycles = ptp->info->getcrosststamp; in ptp_clock_register()
297 if (ptp->info->do_aux_work) { in ptp_clock_register()
298 kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker); in ptp_clock_register()
299 ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index); in ptp_clock_register()
300 if (IS_ERR(ptp->kworker)) { in ptp_clock_register()
301 err = PTR_ERR(ptp->kworker); in ptp_clock_register()
308 if (parent && parent->class && parent->class->name && in ptp_clock_register()
309 strcmp(parent->class->name, "ptp") == 0) in ptp_clock_register()
310 ptp->is_virtual_clock = true; in ptp_clock_register()
312 if (!ptp->is_virtual_clock) { in ptp_clock_register()
313 ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS; in ptp_clock_register()
315 size = sizeof(int) * ptp->max_vclocks; in ptp_clock_register()
316 ptp->vclock_index = kzalloc(size, GFP_KERNEL); in ptp_clock_register()
317 if (!ptp->vclock_index) { in ptp_clock_register()
318 err = -ENOMEM; in ptp_clock_register()
328 if (info->pps) { in ptp_clock_register()
333 pps.owner = info->owner; in ptp_clock_register()
334 ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS); in ptp_clock_register()
335 if (IS_ERR(ptp->pps_source)) { in ptp_clock_register()
336 err = PTR_ERR(ptp->pps_source); in ptp_clock_register()
340 ptp->pps_source->lookup_cookie = ptp; in ptp_clock_register()
344 device_initialize(&ptp->dev); in ptp_clock_register()
345 ptp->dev.devt = ptp->devid; in ptp_clock_register()
346 ptp->dev.class = &ptp_class; in ptp_clock_register()
347 ptp->dev.parent = parent; in ptp_clock_register()
348 ptp->dev.groups = ptp->pin_attr_groups; in ptp_clock_register()
349 ptp->dev.release = ptp_clock_release; in ptp_clock_register()
350 dev_set_drvdata(&ptp->dev, ptp); in ptp_clock_register()
351 dev_set_name(&ptp->dev, "ptp%d", ptp->index); in ptp_clock_register()
354 err = posix_clock_register(&ptp->clock, &ptp->dev); in ptp_clock_register()
356 if (ptp->pps_source) in ptp_clock_register()
357 pps_unregister_source(ptp->pps_source); in ptp_clock_register()
359 if (ptp->kworker) in ptp_clock_register()
360 kthread_destroy_worker(ptp->kworker); in ptp_clock_register()
362 put_device(&ptp->dev); in ptp_clock_register()
369 snprintf(debugfsname, sizeof(debugfsname), "ptp%d", ptp->index); in ptp_clock_register()
370 ptp->debugfs_root = debugfs_create_dir(debugfsname, NULL); in ptp_clock_register()
377 kfree(ptp->vclock_index); in ptp_clock_register()
379 if (ptp->kworker) in ptp_clock_register()
380 kthread_destroy_worker(ptp->kworker); in ptp_clock_register()
382 mutex_destroy(&ptp->pincfg_mux); in ptp_clock_register()
383 mutex_destroy(&ptp->n_vclocks_mux); in ptp_clock_register()
384 bitmap_free(queue->mask); in ptp_clock_register()
386 list_del(&queue->qlist); in ptp_clock_register()
401 ptp_vclock_unregister(info_to_vclock(ptp->info)); in unregister_vclock()
408 device_for_each_child(&ptp->dev, NULL, unregister_vclock); in ptp_clock_unregister()
411 ptp->defunct = 1; in ptp_clock_unregister()
412 wake_up_interruptible(&ptp->tsev_wq); in ptp_clock_unregister()
414 if (ptp->kworker) { in ptp_clock_unregister()
415 kthread_cancel_delayed_work_sync(&ptp->aux_work); in ptp_clock_unregister()
416 kthread_destroy_worker(ptp->kworker); in ptp_clock_unregister()
420 if (ptp->pps_source) in ptp_clock_unregister()
421 pps_unregister_source(ptp->pps_source); in ptp_clock_unregister()
423 posix_clock_unregister(&ptp->clock); in ptp_clock_unregister()
435 switch (event->type) { in ptp_clock_event()
443 spin_lock_irqsave(&ptp->tsevqs_lock, flags); in ptp_clock_event()
444 list_for_each_entry(tsevq, &ptp->tsevqs, qlist) { in ptp_clock_event()
445 if (test_bit((unsigned int)event->index, tsevq->mask)) in ptp_clock_event()
448 spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); in ptp_clock_event()
449 wake_up_interruptible(&ptp->tsev_wq); in ptp_clock_event()
454 pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL); in ptp_clock_event()
458 pps_event(ptp->pps_source, &event->pps_times, in ptp_clock_event()
467 return ptp->index; in ptp_clock_index()
477 for (i = 0; i < ptp->info->n_pins; i++) { in ptp_find_pin()
478 if (ptp->info->pin_config[i].func == func && in ptp_find_pin()
479 ptp->info->pin_config[i].chan == chan) { in ptp_find_pin()
480 pin = &ptp->info->pin_config[i]; in ptp_find_pin()
485 return pin ? i : -1; in ptp_find_pin()
494 mutex_lock(&ptp->pincfg_mux); in ptp_find_pin_unlocked()
498 mutex_unlock(&ptp->pincfg_mux); in ptp_find_pin_unlocked()
506 return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay); in ptp_schedule_worker()
512 kthread_cancel_delayed_work_sync(&ptp->aux_work); in ptp_cancel_worker_sync()