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/idr.h>
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/posix-clock.h>
14 #include <linux/pps_kernel.h>
15 #include <linux/slab.h>
16 #include <linux/syscalls.h>
17 #include <linux/uaccess.h>
18 #include <uapi/linux/sched/types.h>
19
20 #include "ptp_private.h"
21
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
27 struct class *ptp_class;
28
29 /* private globals */
30
31 static dev_t ptp_devt;
32
33 static DEFINE_IDA(ptp_clocks_map);
34
35 /* time stamp event queue operations */
36
queue_free(struct timestamp_event_queue * q)37 static inline int queue_free(struct timestamp_event_queue *q)
38 {
39 return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
40 }
41
enqueue_external_timestamp(struct timestamp_event_queue * queue,struct ptp_clock_event * src)42 static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
43 struct ptp_clock_event *src)
44 {
45 struct ptp_extts_event *dst;
46 unsigned long flags;
47 s64 seconds;
48 u32 remainder;
49
50 seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
51
52 spin_lock_irqsave(&queue->lock, flags);
53
54 dst = &queue->buf[queue->tail];
55 dst->index = src->index;
56 dst->t.sec = seconds;
57 dst->t.nsec = remainder;
58
59 /* Both WRITE_ONCE() are paired with READ_ONCE() in queue_cnt() */
60 if (!queue_free(queue))
61 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
62
63 WRITE_ONCE(queue->tail, (queue->tail + 1) % PTP_MAX_TIMESTAMPS);
64
65 spin_unlock_irqrestore(&queue->lock, flags);
66 }
67
68 /* posix clock implementation */
69
ptp_clock_getres(struct posix_clock * pc,struct timespec64 * tp)70 static int ptp_clock_getres(struct posix_clock *pc, struct timespec64 *tp)
71 {
72 tp->tv_sec = 0;
73 tp->tv_nsec = 1;
74 return 0;
75 }
76
ptp_clock_settime(struct posix_clock * pc,const struct timespec64 * tp)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
81 if (ptp_vclock_in_use(ptp)) {
82 pr_err("ptp: virtual clock in use\n");
83 return -EBUSY;
84 }
85
86 return ptp->info->settime64(ptp->info, tp);
87 }
88
ptp_clock_gettime(struct posix_clock * pc,struct timespec64 * tp)89 static int ptp_clock_gettime(struct posix_clock *pc, struct timespec64 *tp)
90 {
91 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
92 int err;
93
94 if (ptp->info->gettimex64)
95 err = ptp->info->gettimex64(ptp->info, tp, NULL);
96 else
97 err = ptp->info->gettime64(ptp->info, tp);
98 return err;
99 }
100
ptp_clock_adjtime(struct posix_clock * pc,struct __kernel_timex * tx)101 static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
102 {
103 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
104 struct ptp_clock_info *ops;
105 int err = -EOPNOTSUPP;
106
107 if (ptp_vclock_in_use(ptp)) {
108 pr_err("ptp: virtual clock in use\n");
109 return -EBUSY;
110 }
111
112 ops = ptp->info;
113
114 if (tx->modes & ADJ_SETOFFSET) {
115 struct timespec64 ts;
116 ktime_t kt;
117 s64 delta;
118
119 ts.tv_sec = tx->time.tv_sec;
120 ts.tv_nsec = tx->time.tv_usec;
121
122 if (!(tx->modes & ADJ_NANO))
123 ts.tv_nsec *= 1000;
124
125 if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
126 return -EINVAL;
127
128 kt = timespec64_to_ktime(ts);
129 delta = ktime_to_ns(kt);
130 err = ops->adjtime(ops, delta);
131 } else if (tx->modes & ADJ_FREQUENCY) {
132 long ppb = scaled_ppm_to_ppb(tx->freq);
133 if (ppb > ops->max_adj || ppb < -ops->max_adj)
134 return -ERANGE;
135 if (ops->adjfine)
136 err = ops->adjfine(ops, tx->freq);
137 else
138 err = ops->adjfreq(ops, ppb);
139 ptp->dialed_frequency = tx->freq;
140 } else if (tx->modes & ADJ_OFFSET) {
141 if (ops->adjphase) {
142 s32 offset = tx->offset;
143
144 if (!(tx->modes & ADJ_NANO))
145 offset *= NSEC_PER_USEC;
146
147 err = ops->adjphase(ops, offset);
148 }
149 } else if (tx->modes == 0) {
150 tx->freq = ptp->dialed_frequency;
151 err = 0;
152 }
153
154 return err;
155 }
156
157 static struct posix_clock_operations ptp_clock_ops = {
158 .owner = THIS_MODULE,
159 .clock_adjtime = ptp_clock_adjtime,
160 .clock_gettime = ptp_clock_gettime,
161 .clock_getres = ptp_clock_getres,
162 .clock_settime = ptp_clock_settime,
163 .ioctl = ptp_ioctl,
164 .open = ptp_open,
165 .poll = ptp_poll,
166 .read = ptp_read,
167 };
168
ptp_clock_release(struct device * dev)169 static void ptp_clock_release(struct device *dev)
170 {
171 struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
172
173 ptp_cleanup_pin_groups(ptp);
174 kfree(ptp->vclock_index);
175 mutex_destroy(&ptp->tsevq_mux);
176 mutex_destroy(&ptp->pincfg_mux);
177 mutex_destroy(&ptp->n_vclocks_mux);
178 ida_simple_remove(&ptp_clocks_map, ptp->index);
179 kfree(ptp);
180 }
181
ptp_aux_kworker(struct kthread_work * work)182 static void ptp_aux_kworker(struct kthread_work *work)
183 {
184 struct ptp_clock *ptp = container_of(work, struct ptp_clock,
185 aux_work.work);
186 struct ptp_clock_info *info = ptp->info;
187 long delay;
188
189 delay = info->do_aux_work(info);
190
191 if (delay >= 0)
192 kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay);
193 }
194
195 /* public interface */
196
ptp_clock_register(struct ptp_clock_info * info,struct device * parent)197 struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
198 struct device *parent)
199 {
200 struct ptp_clock *ptp;
201 int err = 0, index, major = MAJOR(ptp_devt);
202 size_t size;
203
204 if (info->n_alarm > PTP_MAX_ALARMS)
205 return ERR_PTR(-EINVAL);
206
207 /* Initialize a clock structure. */
208 err = -ENOMEM;
209 ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
210 if (ptp == NULL)
211 goto no_memory;
212
213 index = ida_simple_get(&ptp_clocks_map, 0, MINORMASK + 1, GFP_KERNEL);
214 if (index < 0) {
215 err = index;
216 goto no_slot;
217 }
218
219 ptp->clock.ops = ptp_clock_ops;
220 ptp->info = info;
221 ptp->devid = MKDEV(major, index);
222 ptp->index = index;
223 spin_lock_init(&ptp->tsevq.lock);
224 mutex_init(&ptp->tsevq_mux);
225 mutex_init(&ptp->pincfg_mux);
226 mutex_init(&ptp->n_vclocks_mux);
227 init_waitqueue_head(&ptp->tsev_wq);
228
229 if (ptp->info->do_aux_work) {
230 kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker);
231 ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index);
232 if (IS_ERR(ptp->kworker)) {
233 err = PTR_ERR(ptp->kworker);
234 pr_err("failed to create ptp aux_worker %d\n", err);
235 goto kworker_err;
236 }
237 }
238
239 /* PTP virtual clock is being registered under physical clock */
240 if (parent && parent->class && parent->class->name &&
241 strcmp(parent->class->name, "ptp") == 0)
242 ptp->is_virtual_clock = true;
243
244 if (!ptp->is_virtual_clock) {
245 ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS;
246
247 size = sizeof(int) * ptp->max_vclocks;
248 ptp->vclock_index = kzalloc(size, GFP_KERNEL);
249 if (!ptp->vclock_index) {
250 err = -ENOMEM;
251 goto no_mem_for_vclocks;
252 }
253 }
254
255 err = ptp_populate_pin_groups(ptp);
256 if (err)
257 goto no_pin_groups;
258
259 /* Register a new PPS source. */
260 if (info->pps) {
261 struct pps_source_info pps;
262 memset(&pps, 0, sizeof(pps));
263 snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
264 pps.mode = PTP_PPS_MODE;
265 pps.owner = info->owner;
266 ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
267 if (IS_ERR(ptp->pps_source)) {
268 err = PTR_ERR(ptp->pps_source);
269 pr_err("failed to register pps source\n");
270 goto no_pps;
271 }
272 ptp->pps_source->lookup_cookie = ptp;
273 }
274
275 /* Initialize a new device of our class in our clock structure. */
276 device_initialize(&ptp->dev);
277 ptp->dev.devt = ptp->devid;
278 ptp->dev.class = ptp_class;
279 ptp->dev.parent = parent;
280 ptp->dev.groups = ptp->pin_attr_groups;
281 ptp->dev.release = ptp_clock_release;
282 dev_set_drvdata(&ptp->dev, ptp);
283 dev_set_name(&ptp->dev, "ptp%d", ptp->index);
284
285 /* Create a posix clock and link it to the device. */
286 err = posix_clock_register(&ptp->clock, &ptp->dev);
287 if (err) {
288 if (ptp->pps_source)
289 pps_unregister_source(ptp->pps_source);
290
291 if (ptp->kworker)
292 kthread_destroy_worker(ptp->kworker);
293
294 put_device(&ptp->dev);
295
296 pr_err("failed to create posix clock\n");
297 return ERR_PTR(err);
298 }
299
300 return ptp;
301
302 no_pps:
303 ptp_cleanup_pin_groups(ptp);
304 no_pin_groups:
305 kfree(ptp->vclock_index);
306 no_mem_for_vclocks:
307 if (ptp->kworker)
308 kthread_destroy_worker(ptp->kworker);
309 kworker_err:
310 mutex_destroy(&ptp->tsevq_mux);
311 mutex_destroy(&ptp->pincfg_mux);
312 mutex_destroy(&ptp->n_vclocks_mux);
313 ida_simple_remove(&ptp_clocks_map, index);
314 no_slot:
315 kfree(ptp);
316 no_memory:
317 return ERR_PTR(err);
318 }
319 EXPORT_SYMBOL(ptp_clock_register);
320
unregister_vclock(struct device * dev,void * data)321 static int unregister_vclock(struct device *dev, void *data)
322 {
323 struct ptp_clock *ptp = dev_get_drvdata(dev);
324
325 ptp_vclock_unregister(info_to_vclock(ptp->info));
326 return 0;
327 }
328
ptp_clock_unregister(struct ptp_clock * ptp)329 int ptp_clock_unregister(struct ptp_clock *ptp)
330 {
331 if (ptp_vclock_in_use(ptp)) {
332 device_for_each_child(&ptp->dev, NULL, unregister_vclock);
333 }
334
335 ptp->defunct = 1;
336 wake_up_interruptible(&ptp->tsev_wq);
337
338 if (ptp->kworker) {
339 kthread_cancel_delayed_work_sync(&ptp->aux_work);
340 kthread_destroy_worker(ptp->kworker);
341 }
342
343 /* Release the clock's resources. */
344 if (ptp->pps_source)
345 pps_unregister_source(ptp->pps_source);
346
347 posix_clock_unregister(&ptp->clock);
348
349 return 0;
350 }
351 EXPORT_SYMBOL(ptp_clock_unregister);
352
ptp_clock_event(struct ptp_clock * ptp,struct ptp_clock_event * event)353 void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
354 {
355 struct pps_event_time evt;
356
357 switch (event->type) {
358
359 case PTP_CLOCK_ALARM:
360 break;
361
362 case PTP_CLOCK_EXTTS:
363 enqueue_external_timestamp(&ptp->tsevq, event);
364 wake_up_interruptible(&ptp->tsev_wq);
365 break;
366
367 case PTP_CLOCK_PPS:
368 pps_get_ts(&evt);
369 pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
370 break;
371
372 case PTP_CLOCK_PPSUSR:
373 pps_event(ptp->pps_source, &event->pps_times,
374 PTP_PPS_EVENT, NULL);
375 break;
376 }
377 }
378 EXPORT_SYMBOL(ptp_clock_event);
379
ptp_clock_index(struct ptp_clock * ptp)380 int ptp_clock_index(struct ptp_clock *ptp)
381 {
382 return ptp->index;
383 }
384 EXPORT_SYMBOL(ptp_clock_index);
385
ptp_find_pin(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)386 int ptp_find_pin(struct ptp_clock *ptp,
387 enum ptp_pin_function func, unsigned int chan)
388 {
389 struct ptp_pin_desc *pin = NULL;
390 int i;
391
392 for (i = 0; i < ptp->info->n_pins; i++) {
393 if (ptp->info->pin_config[i].func == func &&
394 ptp->info->pin_config[i].chan == chan) {
395 pin = &ptp->info->pin_config[i];
396 break;
397 }
398 }
399
400 return pin ? i : -1;
401 }
402 EXPORT_SYMBOL(ptp_find_pin);
403
ptp_find_pin_unlocked(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)404 int ptp_find_pin_unlocked(struct ptp_clock *ptp,
405 enum ptp_pin_function func, unsigned int chan)
406 {
407 int result;
408
409 mutex_lock(&ptp->pincfg_mux);
410
411 result = ptp_find_pin(ptp, func, chan);
412
413 mutex_unlock(&ptp->pincfg_mux);
414
415 return result;
416 }
417 EXPORT_SYMBOL(ptp_find_pin_unlocked);
418
ptp_schedule_worker(struct ptp_clock * ptp,unsigned long delay)419 int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay)
420 {
421 return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay);
422 }
423 EXPORT_SYMBOL(ptp_schedule_worker);
424
ptp_cancel_worker_sync(struct ptp_clock * ptp)425 void ptp_cancel_worker_sync(struct ptp_clock *ptp)
426 {
427 kthread_cancel_delayed_work_sync(&ptp->aux_work);
428 }
429 EXPORT_SYMBOL(ptp_cancel_worker_sync);
430
431 /* module operations */
432
ptp_exit(void)433 static void __exit ptp_exit(void)
434 {
435 class_destroy(ptp_class);
436 unregister_chrdev_region(ptp_devt, MINORMASK + 1);
437 ida_destroy(&ptp_clocks_map);
438 }
439
ptp_init(void)440 static int __init ptp_init(void)
441 {
442 int err;
443
444 ptp_class = class_create(THIS_MODULE, "ptp");
445 if (IS_ERR(ptp_class)) {
446 pr_err("ptp: failed to allocate class\n");
447 return PTR_ERR(ptp_class);
448 }
449
450 err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
451 if (err < 0) {
452 pr_err("ptp: failed to allocate device region\n");
453 goto no_region;
454 }
455
456 ptp_class->dev_groups = ptp_groups;
457 pr_info("PTP clock support registered\n");
458 return 0;
459
460 no_region:
461 class_destroy(ptp_class);
462 return err;
463 }
464
465 subsys_initcall(ptp_init);
466 module_exit(ptp_exit);
467
468 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
469 MODULE_DESCRIPTION("PTP clocks support");
470 MODULE_LICENSE("GPL");
471