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()
153 ptp->dialed_frequency = tx->freq; in ptp_clock_adjtime()
154 } else if (tx->modes & ADJ_OFFSET) { in ptp_clock_adjtime()
155 if (ops->adjphase) { in ptp_clock_adjtime()
156 s32 max_phase_adj = ops->getmaxphase(ops); in ptp_clock_adjtime()
157 s32 offset = tx->offset; in ptp_clock_adjtime()
159 if (!(tx->modes & ADJ_NANO)) in ptp_clock_adjtime()
162 if (offset > max_phase_adj || offset < -max_phase_adj) in ptp_clock_adjtime()
163 return -ERANGE; in ptp_clock_adjtime()
165 err = ops->adjphase(ops, offset); in ptp_clock_adjtime()
167 } else if (tx->modes == 0) { in ptp_clock_adjtime()
168 tx->freq = ptp->dialed_frequency; in ptp_clock_adjtime()
195 kfree(ptp->vclock_index); in ptp_clock_release()
196 mutex_destroy(&ptp->pincfg_mux); in ptp_clock_release()
197 mutex_destroy(&ptp->n_vclocks_mux); in ptp_clock_release()
199 spin_lock_irqsave(&ptp->tsevqs_lock, flags); in ptp_clock_release()
200 tsevq = list_first_entry(&ptp->tsevqs, struct timestamp_event_queue, in ptp_clock_release()
202 list_del(&tsevq->qlist); in ptp_clock_release()
203 spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); in ptp_clock_release()
204 bitmap_free(tsevq->mask); in ptp_clock_release()
206 debugfs_remove(ptp->debugfs_root); in ptp_clock_release()
207 xa_erase(&ptp_clocks_map, ptp->index); in ptp_clock_release()
213 if (info->getcyclesx64) in ptp_getcycles64()
214 return info->getcyclesx64(info, ts, NULL); in ptp_getcycles64()
216 return info->gettime64(info, ts); in ptp_getcycles64()
223 struct ptp_clock_info *info = ptp->info; in ptp_aux_kworker()
226 delay = info->do_aux_work(info); in ptp_aux_kworker()
229 kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay); in ptp_aux_kworker()
243 if (info->n_alarm > PTP_MAX_ALARMS) in ptp_clock_register()
244 return ERR_PTR(-EINVAL); in ptp_clock_register()
249 err = -ENOMEM; in ptp_clock_register()
258 ptp->clock.ops = ptp_clock_ops; in ptp_clock_register()
259 ptp->info = info; in ptp_clock_register()
260 ptp->devid = MKDEV(major, index); in ptp_clock_register()
261 ptp->index = index; in ptp_clock_register()
262 INIT_LIST_HEAD(&ptp->tsevqs); in ptp_clock_register()
265 err = -ENOMEM; in ptp_clock_register()
268 list_add_tail(&queue->qlist, &ptp->tsevqs); in ptp_clock_register()
269 spin_lock_init(&ptp->tsevqs_lock); in ptp_clock_register()
270 queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL); in ptp_clock_register()
271 if (!queue->mask) { in ptp_clock_register()
272 err = -ENOMEM; in ptp_clock_register()
275 bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); in ptp_clock_register()
276 spin_lock_init(&queue->lock); in ptp_clock_register()
277 mutex_init(&ptp->pincfg_mux); in ptp_clock_register()
278 mutex_init(&ptp->n_vclocks_mux); in ptp_clock_register()
279 init_waitqueue_head(&ptp->tsev_wq); in ptp_clock_register()
281 if (ptp->info->getcycles64 || ptp->info->getcyclesx64) { in ptp_clock_register()
282 ptp->has_cycles = true; in ptp_clock_register()
283 if (!ptp->info->getcycles64 && ptp->info->getcyclesx64) in ptp_clock_register()
284 ptp->info->getcycles64 = ptp_getcycles64; in ptp_clock_register()
287 ptp->info->getcycles64 = ptp_getcycles64; in ptp_clock_register()
289 if (ptp->info->gettimex64) in ptp_clock_register()
290 ptp->info->getcyclesx64 = ptp->info->gettimex64; in ptp_clock_register()
292 if (ptp->info->getcrosststamp) in ptp_clock_register()
293 ptp->info->getcrosscycles = ptp->info->getcrosststamp; in ptp_clock_register()
296 if (ptp->info->do_aux_work) { in ptp_clock_register()
297 kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker); in ptp_clock_register()
298 ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index); in ptp_clock_register()
299 if (IS_ERR(ptp->kworker)) { in ptp_clock_register()
300 err = PTR_ERR(ptp->kworker); in ptp_clock_register()
307 if (parent && parent->class && parent->class->name && in ptp_clock_register()
308 strcmp(parent->class->name, "ptp") == 0) in ptp_clock_register()
309 ptp->is_virtual_clock = true; in ptp_clock_register()
311 if (!ptp->is_virtual_clock) { in ptp_clock_register()
312 ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS; in ptp_clock_register()
314 size = sizeof(int) * ptp->max_vclocks; in ptp_clock_register()
315 ptp->vclock_index = kzalloc(size, GFP_KERNEL); in ptp_clock_register()
316 if (!ptp->vclock_index) { in ptp_clock_register()
317 err = -ENOMEM; in ptp_clock_register()
327 if (info->pps) { in ptp_clock_register()
332 pps.owner = info->owner; in ptp_clock_register()
333 ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS); in ptp_clock_register()
334 if (IS_ERR(ptp->pps_source)) { in ptp_clock_register()
335 err = PTR_ERR(ptp->pps_source); in ptp_clock_register()
339 ptp->pps_source->lookup_cookie = ptp; in ptp_clock_register()
343 device_initialize(&ptp->dev); in ptp_clock_register()
344 ptp->dev.devt = ptp->devid; in ptp_clock_register()
345 ptp->dev.class = &ptp_class; in ptp_clock_register()
346 ptp->dev.parent = parent; in ptp_clock_register()
347 ptp->dev.groups = ptp->pin_attr_groups; in ptp_clock_register()
348 ptp->dev.release = ptp_clock_release; in ptp_clock_register()
349 dev_set_drvdata(&ptp->dev, ptp); in ptp_clock_register()
350 dev_set_name(&ptp->dev, "ptp%d", ptp->index); in ptp_clock_register()
353 err = posix_clock_register(&ptp->clock, &ptp->dev); in ptp_clock_register()
355 if (ptp->pps_source) in ptp_clock_register()
356 pps_unregister_source(ptp->pps_source); in ptp_clock_register()
358 if (ptp->kworker) in ptp_clock_register()
359 kthread_destroy_worker(ptp->kworker); in ptp_clock_register()
361 put_device(&ptp->dev); in ptp_clock_register()
368 snprintf(debugfsname, sizeof(debugfsname), "ptp%d", ptp->index); in ptp_clock_register()
369 ptp->debugfs_root = debugfs_create_dir(debugfsname, NULL); in ptp_clock_register()
376 kfree(ptp->vclock_index); in ptp_clock_register()
378 if (ptp->kworker) in ptp_clock_register()
379 kthread_destroy_worker(ptp->kworker); in ptp_clock_register()
381 mutex_destroy(&ptp->pincfg_mux); in ptp_clock_register()
382 mutex_destroy(&ptp->n_vclocks_mux); in ptp_clock_register()
383 bitmap_free(queue->mask); in ptp_clock_register()
385 list_del(&queue->qlist); in ptp_clock_register()
400 ptp_vclock_unregister(info_to_vclock(ptp->info)); in unregister_vclock()
407 device_for_each_child(&ptp->dev, NULL, unregister_vclock); in ptp_clock_unregister()
410 ptp->defunct = 1; in ptp_clock_unregister()
411 wake_up_interruptible(&ptp->tsev_wq); in ptp_clock_unregister()
413 if (ptp->kworker) { in ptp_clock_unregister()
414 kthread_cancel_delayed_work_sync(&ptp->aux_work); in ptp_clock_unregister()
415 kthread_destroy_worker(ptp->kworker); in ptp_clock_unregister()
419 if (ptp->pps_source) in ptp_clock_unregister()
420 pps_unregister_source(ptp->pps_source); in ptp_clock_unregister()
422 posix_clock_unregister(&ptp->clock); in ptp_clock_unregister()
434 switch (event->type) { in ptp_clock_event()
442 spin_lock_irqsave(&ptp->tsevqs_lock, flags); in ptp_clock_event()
443 list_for_each_entry(tsevq, &ptp->tsevqs, qlist) { in ptp_clock_event()
444 if (test_bit((unsigned int)event->index, tsevq->mask)) in ptp_clock_event()
447 spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); in ptp_clock_event()
448 wake_up_interruptible(&ptp->tsev_wq); in ptp_clock_event()
453 pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL); in ptp_clock_event()
457 pps_event(ptp->pps_source, &event->pps_times, in ptp_clock_event()
466 return ptp->index; in ptp_clock_index()
476 for (i = 0; i < ptp->info->n_pins; i++) { in ptp_find_pin()
477 if (ptp->info->pin_config[i].func == func && in ptp_find_pin()
478 ptp->info->pin_config[i].chan == chan) { in ptp_find_pin()
479 pin = &ptp->info->pin_config[i]; in ptp_find_pin()
484 return pin ? i : -1; in ptp_find_pin()
493 mutex_lock(&ptp->pincfg_mux); in ptp_find_pin_unlocked()
497 mutex_unlock(&ptp->pincfg_mux); in ptp_find_pin_unlocked()
505 return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay); in ptp_schedule_worker()
511 kthread_cancel_delayed_work_sync(&ptp->aux_work); in ptp_cancel_worker_sync()