| 1 | |
| 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 |
|
| 36 |
|
| 37 | static dev_t ptp_devt;
|
| 38 |
|
| 39 | static DEFINE_XARRAY_ALLOC(ptp_clocks_map);
|
| 40 |
|
| 41 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 | if (ptp_vclock_in_use(ptp)) {
|
| 512 | device_for_each_child(&ptp->dev, NULL, unregister_vclock);
|
| 513 | }
|
| 514 |
|
| 515 | |
| 516 | * and freeing the structure(s)
|
| 517 |
|
| 518 | get_device(&ptp->dev);
|
| 519 |
|
| 520 |
|
| 521 | ptp->defunct = 1;
|
| 522 | wake_up_interruptible(&ptp->tsev_wq);
|
| 523 |
|
| 524 |
|
| 525 | posix_clock_unregister(&ptp->clock);
|
| 526 |
|
| 527 |
|
| 528 | ptp_disable_all_events(ptp);
|
| 529 |
|
| 530 | if (ptp->kworker) {
|
| 531 | kthread_cancel_delayed_work_sync(&ptp->aux_work);
|
| 532 | kthread_destroy_worker(ptp->kworker);
|
| 533 | }
|
| 534 |
|
| 535 |
|
| 536 | if (ptp->pps_source)
|
| 537 | pps_unregister_source(ptp->pps_source);
|
| 538 |
|
| 539 |
|
| 540 | put_device(&ptp->dev);
|
| 541 |
|
| 542 | return 0;
|
| 543 | }
|
| 544 | EXPORT_SYMBOL(ptp_clock_unregister);
|
| 545 |
|
| 546 | void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
|
| 547 | {
|
| 548 | struct timestamp_event_queue *tsevq;
|
| 549 | struct pps_event_time evt;
|
| 550 | unsigned long flags;
|
| 551 |
|
| 552 | switch (event->type) {
|
| 553 |
|
| 554 | case PTP_CLOCK_ALARM:
|
| 555 | break;
|
| 556 |
|
| 557 | case PTP_CLOCK_EXTTS:
|
| 558 | case PTP_CLOCK_EXTOFF:
|
| 559 |
|
| 560 | spin_lock_irqsave(&ptp->tsevqs_lock, flags);
|
| 561 | list_for_each_entry(tsevq, &ptp->tsevqs, qlist) {
|
| 562 | if (test_bit((unsigned int)event->index, tsevq->mask))
|
| 563 | enqueue_external_timestamp(tsevq, event);
|
| 564 | }
|
| 565 | spin_unlock_irqrestore(&ptp->tsevqs_lock, flags);
|
| 566 | wake_up_interruptible(&ptp->tsev_wq);
|
| 567 | break;
|
| 568 |
|
| 569 | case PTP_CLOCK_PPS:
|
| 570 | pps_get_ts(&evt);
|
| 571 | pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
|
| 572 | break;
|
| 573 |
|
| 574 | case PTP_CLOCK_PPSUSR:
|
| 575 | pps_event(ptp->pps_source, &event->pps_times,
|
| 576 | PTP_PPS_EVENT, NULL);
|
| 577 | break;
|
| 578 | }
|
| 579 | }
|
| 580 | EXPORT_SYMBOL(ptp_clock_event);
|
| 581 |
|
| 582 | int ptp_clock_index(struct ptp_clock *ptp)
|
| 583 | {
|
| 584 | return ptp->index;
|
| 585 | }
|
| 586 | EXPORT_SYMBOL(ptp_clock_index);
|
| 587 |
|
| 588 | static int ptp_clock_of_node_match(struct device *dev, const void *data)
|
| 589 | {
|
| 590 | const struct device_node *parent_np = data;
|
| 591 |
|
| 592 | return (dev->parent && dev_of_node(dev->parent) == parent_np);
|
| 593 | }
|
| 594 |
|
| 595 | int ptp_clock_index_by_of_node(struct device_node *np)
|
| 596 | {
|
| 597 | struct ptp_clock *ptp;
|
| 598 | struct device *dev;
|
| 599 | int phc_index;
|
| 600 |
|
| 601 | dev = class_find_device(&ptp_class, NULL, np,
|
| 602 | ptp_clock_of_node_match);
|
| 603 | if (!dev)
|
| 604 | return -1;
|
| 605 |
|
| 606 | ptp = dev_get_drvdata(dev);
|
| 607 | phc_index = ptp_clock_index(ptp);
|
| 608 | put_device(dev);
|
| 609 |
|
| 610 | return phc_index;
|
| 611 | }
|
| 612 | EXPORT_SYMBOL_GPL(ptp_clock_index_by_of_node);
|
| 613 |
|
| 614 | static int ptp_clock_dev_match(struct device *dev, const void *data)
|
| 615 | {
|
| 616 | const struct device *parent = data;
|
| 617 |
|
| 618 | return dev->parent == parent;
|
| 619 | }
|
| 620 |
|
| 621 | int ptp_clock_index_by_dev(struct device *parent)
|
| 622 | {
|
| 623 | struct ptp_clock *ptp;
|
| 624 | struct device *dev;
|
| 625 | int phc_index;
|
| 626 |
|
| 627 | dev = class_find_device(&ptp_class, NULL, parent,
|
| 628 | ptp_clock_dev_match);
|
| 629 | if (!dev)
|
| 630 | return -1;
|
| 631 |
|
| 632 | ptp = dev_get_drvdata(dev);
|
| 633 | phc_index = ptp_clock_index(ptp);
|
| 634 | put_device(dev);
|
| 635 |
|
| 636 | return phc_index;
|
| 637 | }
|
| 638 | EXPORT_SYMBOL_GPL(ptp_clock_index_by_dev);
|
| 639 |
|
| 640 | int ptp_find_pin(struct ptp_clock *ptp,
|
| 641 | enum ptp_pin_function func, unsigned int chan)
|
| 642 | {
|
| 643 | struct ptp_pin_desc *pin = NULL;
|
| 644 | int i;
|
| 645 |
|
| 646 | for (i = 0; i < ptp->info->n_pins; i++) {
|
| 647 | if (ptp->info->pin_config[i].func == func &&
|
| 648 | ptp->info->pin_config[i].chan == chan) {
|
| 649 | pin = &ptp->info->pin_config[i];
|
| 650 | break;
|
| 651 | }
|
| 652 | }
|
| 653 |
|
| 654 | return pin ? i : -1;
|
| 655 | }
|
| 656 | EXPORT_SYMBOL(ptp_find_pin);
|
| 657 |
|
| 658 | int ptp_find_pin_unlocked(struct ptp_clock *ptp,
|
| 659 | enum ptp_pin_function func, unsigned int chan)
|
| 660 | {
|
| 661 | int result;
|
| 662 |
|
| 663 | mutex_lock(&ptp->pincfg_mux);
|
| 664 |
|
| 665 | result = ptp_find_pin(ptp, func, chan);
|
| 666 |
|
| 667 | mutex_unlock(&ptp->pincfg_mux);
|
| 668 |
|
| 669 | return result;
|
| 670 | }
|
| 671 | EXPORT_SYMBOL(ptp_find_pin_unlocked);
|
| 672 |
|
| 673 | int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay)
|
| 674 | {
|
| 675 | return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay);
|
| 676 | }
|
| 677 | EXPORT_SYMBOL(ptp_schedule_worker);
|
| 678 |
|
| 679 | void ptp_cancel_worker_sync(struct ptp_clock *ptp)
|
| 680 | {
|
| 681 | kthread_cancel_delayed_work_sync(&ptp->aux_work);
|
| 682 | }
|
| 683 | EXPORT_SYMBOL(ptp_cancel_worker_sync);
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 | static void __exit ptp_exit(void)
|
| 688 | {
|
| 689 | class_unregister(&ptp_class);
|
| 690 | unregister_chrdev_region(ptp_devt, MINORMASK + 1);
|
| 691 | xa_destroy(&ptp_clocks_map);
|
| 692 | }
|
| 693 |
|
| 694 | static int __init ptp_init(void)
|
| 695 | {
|
| 696 | int err;
|
| 697 |
|
| 698 | err = class_register(&ptp_class);
|
| 699 | if (err) {
|
| 700 | pr_err("ptp: failed to allocate class\n");
|
| 701 | return err;
|
| 702 | }
|
| 703 |
|
| 704 | err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
|
| 705 | if (err < 0) {
|
| 706 | pr_err("ptp: failed to allocate device region\n");
|
| 707 | goto no_region;
|
| 708 | }
|
| 709 |
|
| 710 | pr_info("PTP clock support registered\n");
|
| 711 | return 0;
|
| 712 |
|
| 713 | no_region:
|
| 714 | class_unregister(&ptp_class);
|
| 715 | return err;
|
| 716 | }
|
| 717 |
|
| 718 | subsys_initcall(ptp_init);
|
| 719 | module_exit(ptp_exit);
|
| 720 |
|
| 721 | MODULE_AUTHOR("Richard Cochran <[email protected]>");
|
| 722 | MODULE_DESCRIPTION("PTP clocks support");
|
| 723 | MODULE_LICENSE("GPL");
|