^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * kernel API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pps_kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "kc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Local functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ts->nsec += offset->nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) while (ts->nsec >= NSEC_PER_SEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ts->nsec -= NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ts->sec++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) while (ts->nsec < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ts->nsec += NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ts->sec--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ts->sec += offset->sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void pps_echo_client_default(struct pps_device *pps, int event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) dev_info(pps->dev, "echo %s %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) event & PPS_CAPTUREASSERT ? "assert" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) event & PPS_CAPTURECLEAR ? "clear" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Exported functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* pps_register_source - add a PPS source in the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @info: the PPS info struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @default_params: the default PPS parameters of the new source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * This function is used to add a new PPS source in the system. The new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * source is described by info's fields and it will have, as default PPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * parameters, the ones specified into default_params.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * The function returns, in case of success, the PPS device. Otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * ERR_PTR(errno).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct pps_device *pps_register_source(struct pps_source_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int default_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct pps_device *pps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* Sanity checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if ((info->mode & default_params) != default_params) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pr_err("%s: unsupported default parameters\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto pps_register_source_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pr_err("%s: unspecified time format\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) goto pps_register_source_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Allocate memory for the new PPS source struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (pps == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) goto pps_register_source_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* These initializations must be done before calling idr_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * in order to avoid reces into pps_event().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) pps->params.api_version = PPS_API_VERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pps->params.mode = default_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) pps->info = *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* check for default echo function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) pps->info.echo == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pps->info.echo = pps_echo_client_default;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) init_waitqueue_head(&pps->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_lock_init(&pps->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Create the char device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) err = pps_register_cdev(pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pr_err("%s: unable to create char device\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) goto kfree_pps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dev_info(pps->dev, "new PPS source %s\n", info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return pps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) kfree_pps:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) pps_register_source_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pr_err("%s: unable to register source\n", info->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) EXPORT_SYMBOL(pps_register_source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* pps_unregister_source - remove a PPS source from the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @pps: the PPS source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * This function is used to remove a previously registered PPS source from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void pps_unregister_source(struct pps_device *pps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pps_kc_remove(pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pps_unregister_cdev(pps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* don't have to kfree(pps) here because it will be done on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * device destruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) EXPORT_SYMBOL(pps_unregister_source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* pps_event - register a PPS event into the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * @pps: the PPS device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * @ts: the event timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * @event: the event type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * @data: userdef pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * This function is used by each PPS client in order to register a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * PPS event into the system (it's usually called inside an IRQ handler).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * If an echo function is associated with the PPS device it will be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * as:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * pps->info.echo(pps, event, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int captured = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* check event type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) timespec_to_pps_ktime(&ts_real, ts->ts_real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) spin_lock_irqsave(&pps->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Must call the echo function? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pps->info.echo(pps, event, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Check the event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) pps->current_mode = pps->params.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (event & pps->params.mode & PPS_CAPTUREASSERT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* We have to add an offset? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (pps->params.mode & PPS_OFFSETASSERT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pps_add_offset(&ts_real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) &pps->params.assert_off_tu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Save the time stamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pps->assert_tu = ts_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pps->assert_sequence++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dev_dbg(pps->dev, "capture assert seq #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pps->assert_sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) captured = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (event & pps->params.mode & PPS_CAPTURECLEAR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* We have to add an offset? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (pps->params.mode & PPS_OFFSETCLEAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pps_add_offset(&ts_real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) &pps->params.clear_off_tu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* Save the time stamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) pps->clear_tu = ts_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) pps->clear_sequence++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dev_dbg(pps->dev, "capture clear seq #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) pps->clear_sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) captured = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pps_kc_event(pps, ts, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* Wake up if captured something */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (captured) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) pps->last_ev++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) wake_up_interruptible_all(&pps->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) spin_unlock_irqrestore(&pps->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) EXPORT_SYMBOL(pps_event);