linux/drivers/ptp/ptp_clock.c
<<
>>
Prefs
   1/*
   2 * PTP 1588 clock support
   3 *
   4 * Copyright (C) 2010 OMICRON electronics GmbH
   5 *
   6 *  This program is free software; you can redistribute it and/or modify
   7 *  it under the terms of the GNU General Public License as published by
   8 *  the Free Software Foundation; either version 2 of the License, or
   9 *  (at your option) any later version.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *  GNU General Public License for more details.
  15 *
  16 *  You should have received a copy of the GNU General Public License
  17 *  along with this program; if not, write to the Free Software
  18 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19 */
  20#include <linux/idr.h>
  21#include <linux/device.h>
  22#include <linux/err.h>
  23#include <linux/init.h>
  24#include <linux/kernel.h>
  25#include <linux/module.h>
  26#include <linux/posix-clock.h>
  27#include <linux/pps_kernel.h>
  28#include <linux/slab.h>
  29#include <linux/syscalls.h>
  30#include <linux/uaccess.h>
  31
  32#include "ptp_private.h"
  33
  34#define PTP_MAX_ALARMS 4
  35#define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT)
  36#define PTP_PPS_EVENT PPS_CAPTUREASSERT
  37#define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
  38
  39/* private globals */
  40
  41static dev_t ptp_devt;
  42static struct class *ptp_class;
  43
  44static DEFINE_IDA(ptp_clocks_map);
  45
  46/* time stamp event queue operations */
  47
  48static inline int queue_free(struct timestamp_event_queue *q)
  49{
  50        return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
  51}
  52
  53static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
  54                                       struct ptp_clock_event *src)
  55{
  56        struct ptp_extts_event *dst;
  57        unsigned long flags;
  58        s64 seconds;
  59        u32 remainder;
  60
  61        seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
  62
  63        spin_lock_irqsave(&queue->lock, flags);
  64
  65        dst = &queue->buf[queue->tail];
  66        dst->index = src->index;
  67        dst->t.sec = seconds;
  68        dst->t.nsec = remainder;
  69
  70        if (!queue_free(queue))
  71                queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  72
  73        queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS;
  74
  75        spin_unlock_irqrestore(&queue->lock, flags);
  76}
  77
  78static s32 scaled_ppm_to_ppb(long ppm)
  79{
  80        /*
  81         * The 'freq' field in the 'struct timex' is in parts per
  82         * million, but with a 16 bit binary fractional field.
  83         *
  84         * We want to calculate
  85         *
  86         *    ppb = scaled_ppm * 1000 / 2^16
  87         *
  88         * which simplifies to
  89         *
  90         *    ppb = scaled_ppm * 125 / 2^13
  91         */
  92        s64 ppb = 1 + ppm;
  93        ppb *= 125;
  94        ppb >>= 13;
  95        return (s32) ppb;
  96}
  97
  98/* posix clock implementation */
  99
 100static int ptp_clock_getres(struct posix_clock *pc, struct timespec *tp)
 101{
 102        tp->tv_sec = 0;
 103        tp->tv_nsec = 1;
 104        return 0;
 105}
 106
 107static int ptp_clock_settime(struct posix_clock *pc, const struct timespec *tp)
 108{
 109        struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
 110        struct timespec64 ts = timespec_to_timespec64(*tp);
 111
 112        return  ptp->info->settime64 ?
 113                ptp->info->settime64(ptp->info, &ts) :
 114                ptp->info->settime(ptp->info, tp);
 115}
 116
 117static int ptp_clock_gettime(struct posix_clock *pc, struct timespec *tp)
 118{
 119        struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
 120        struct timespec64 ts;
 121        int err;
 122
 123        if (ptp->info->gettime64) {
 124                err = ptp->info->gettime64(ptp->info, &ts);
 125                if (!err)
 126                        *tp = timespec64_to_timespec(ts);
 127        } else {
 128                err = ptp->info->gettime(ptp->info, tp);
 129        }
 130
 131        return err;
 132}
 133
 134static int ptp_clock_adjtime(struct posix_clock *pc, struct timex *tx)
 135{
 136        struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
 137        struct ptp_clock_info *ops;
 138        int err = -EOPNOTSUPP;
 139
 140        ops = ptp->info;
 141
 142        if (tx->modes & ADJ_SETOFFSET) {
 143                struct timespec ts;
 144                ktime_t kt;
 145                s64 delta;
 146
 147                ts.tv_sec  = tx->time.tv_sec;
 148                ts.tv_nsec = tx->time.tv_usec;
 149
 150                if (!(tx->modes & ADJ_NANO))
 151                        ts.tv_nsec *= 1000;
 152
 153                if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
 154                        return -EINVAL;
 155
 156                kt = timespec_to_ktime(ts);
 157                delta = ktime_to_ns(kt);
 158                err = ops->adjtime(ops, delta);
 159        } else if (tx->modes & ADJ_FREQUENCY) {
 160                s32 ppb = scaled_ppm_to_ppb(tx->freq);
 161                if (ppb > ops->max_adj || ppb < -ops->max_adj)
 162                        return -ERANGE;
 163                if (ops->adjfine)
 164                        err = ops->adjfine(ops, tx->freq);
 165                else
 166                        err = ops->adjfreq(ops, ppb);
 167                ptp->dialed_frequency = tx->freq;
 168        } else if (tx->modes == 0) {
 169                tx->freq = ptp->dialed_frequency;
 170                err = 0;
 171        }
 172
 173        return err;
 174}
 175
 176static struct posix_clock_operations ptp_clock_ops = {
 177        .owner          = THIS_MODULE,
 178        .clock_adjtime  = ptp_clock_adjtime,
 179        .clock_gettime  = ptp_clock_gettime,
 180        .clock_getres   = ptp_clock_getres,
 181        .clock_settime  = ptp_clock_settime,
 182        .ioctl          = ptp_ioctl,
 183        .open           = ptp_open,
 184        .poll           = ptp_poll,
 185        .read           = ptp_read,
 186};
 187
 188static void delete_ptp_clock(struct posix_clock *pc)
 189{
 190        struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
 191
 192        mutex_destroy(&ptp->tsevq_mux);
 193        mutex_destroy(&ptp->pincfg_mux);
 194        ida_simple_remove(&ptp_clocks_map, ptp->index);
 195        kfree(ptp);
 196}
 197
 198/* public interface */
 199
 200struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 201                                     struct device *parent)
 202{
 203        struct ptp_clock *ptp;
 204        int err = 0, index, major = MAJOR(ptp_devt);
 205
 206        if (info->n_alarm > PTP_MAX_ALARMS)
 207                return ERR_PTR(-EINVAL);
 208
 209        /* Initialize a clock structure. */
 210        err = -ENOMEM;
 211        ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
 212        if (ptp == NULL)
 213                goto no_memory;
 214
 215        index = ida_simple_get(&ptp_clocks_map, 0, MINORMASK + 1, GFP_KERNEL);
 216        if (index < 0) {
 217                err = index;
 218                goto no_slot;
 219        }
 220
 221        ptp->clock.ops = ptp_clock_ops;
 222        ptp->clock.release = delete_ptp_clock;
 223        ptp->info = info;
 224        ptp->devid = MKDEV(major, index);
 225        ptp->index = index;
 226        spin_lock_init(&ptp->tsevq.lock);
 227        mutex_init(&ptp->tsevq_mux);
 228        mutex_init(&ptp->pincfg_mux);
 229        init_waitqueue_head(&ptp->tsev_wq);
 230
 231        /* Create a new device in our class. */
 232        ptp->dev = device_create(ptp_class, parent, ptp->devid, ptp,
 233                                 "ptp%d", ptp->index);
 234        if (IS_ERR(ptp->dev))
 235                goto no_device;
 236
 237        dev_set_drvdata(ptp->dev, ptp);
 238
 239        err = ptp_populate_sysfs(ptp);
 240        if (err)
 241                goto no_sysfs;
 242
 243        /* Register a new PPS source. */
 244        if (info->pps) {
 245                struct pps_source_info pps;
 246                memset(&pps, 0, sizeof(pps));
 247                snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
 248                pps.mode = PTP_PPS_MODE;
 249                pps.owner = info->owner;
 250                ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
 251                if (!ptp->pps_source) {
 252                        pr_err("failed to register pps source\n");
 253                        goto no_pps;
 254                }
 255        }
 256
 257        /* Create a posix clock. */
 258        err = posix_clock_register(&ptp->clock, ptp->devid);
 259        if (err) {
 260                pr_err("failed to create posix clock\n");
 261                goto no_clock;
 262        }
 263
 264        return ptp;
 265
 266no_clock:
 267        if (ptp->pps_source)
 268                pps_unregister_source(ptp->pps_source);
 269no_pps:
 270        ptp_cleanup_sysfs(ptp);
 271no_sysfs:
 272        device_destroy(ptp_class, ptp->devid);
 273no_device:
 274        mutex_destroy(&ptp->tsevq_mux);
 275        mutex_destroy(&ptp->pincfg_mux);
 276no_slot:
 277        kfree(ptp);
 278no_memory:
 279        return ERR_PTR(err);
 280}
 281EXPORT_SYMBOL(ptp_clock_register);
 282
 283int ptp_clock_unregister(struct ptp_clock *ptp)
 284{
 285        ptp->defunct = 1;
 286        wake_up_interruptible(&ptp->tsev_wq);
 287
 288        /* Release the clock's resources. */
 289        if (ptp->pps_source)
 290                pps_unregister_source(ptp->pps_source);
 291        ptp_cleanup_sysfs(ptp);
 292        device_destroy(ptp_class, ptp->devid);
 293
 294        posix_clock_unregister(&ptp->clock);
 295        return 0;
 296}
 297EXPORT_SYMBOL(ptp_clock_unregister);
 298
 299void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
 300{
 301        struct pps_event_time evt;
 302
 303        switch (event->type) {
 304
 305        case PTP_CLOCK_ALARM:
 306                break;
 307
 308        case PTP_CLOCK_EXTTS:
 309                enqueue_external_timestamp(&ptp->tsevq, event);
 310                wake_up_interruptible(&ptp->tsev_wq);
 311                break;
 312
 313        case PTP_CLOCK_PPS:
 314                pps_get_ts(&evt);
 315                pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
 316                break;
 317
 318        case PTP_CLOCK_PPSUSR:
 319                pps_event(ptp->pps_source, &event->pps_times,
 320                          PTP_PPS_EVENT, NULL);
 321                break;
 322        }
 323}
 324EXPORT_SYMBOL(ptp_clock_event);
 325
 326int ptp_clock_index(struct ptp_clock *ptp)
 327{
 328        return ptp->index;
 329}
 330EXPORT_SYMBOL(ptp_clock_index);
 331
 332int ptp_find_pin(struct ptp_clock *ptp,
 333                 enum ptp_pin_function func, unsigned int chan)
 334{
 335        struct ptp_pin_desc *pin = NULL;
 336        int i;
 337
 338        mutex_lock(&ptp->pincfg_mux);
 339        for (i = 0; i < ptp->info->n_pins; i++) {
 340                if (ptp->info->pin_config[i].func == func &&
 341                    ptp->info->pin_config[i].chan == chan) {
 342                        pin = &ptp->info->pin_config[i];
 343                        break;
 344                }
 345        }
 346        mutex_unlock(&ptp->pincfg_mux);
 347
 348        return pin ? i : -1;
 349}
 350EXPORT_SYMBOL(ptp_find_pin);
 351
 352/* module operations */
 353
 354static void __exit ptp_exit(void)
 355{
 356        class_destroy(ptp_class);
 357        unregister_chrdev_region(ptp_devt, MINORMASK + 1);
 358        ida_destroy(&ptp_clocks_map);
 359}
 360
 361static int __init ptp_init(void)
 362{
 363        int err;
 364
 365        ptp_class = class_create(THIS_MODULE, "ptp");
 366        if (IS_ERR(ptp_class)) {
 367                pr_err("ptp: failed to allocate class\n");
 368                return PTR_ERR(ptp_class);
 369        }
 370
 371        err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
 372        if (err < 0) {
 373                pr_err("ptp: failed to allocate device region\n");
 374                goto no_region;
 375        }
 376
 377        ptp_class->dev_groups = ptp_groups;
 378        pr_info("PTP clock support registered\n");
 379        return 0;
 380
 381no_region:
 382        class_destroy(ptp_class);
 383        return err;
 384}
 385
 386subsys_initcall(ptp_init);
 387module_exit(ptp_exit);
 388
 389MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
 390MODULE_DESCRIPTION("PTP clocks support");
 391MODULE_LICENSE("GPL");
 392