1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * PPS kernel consumer API
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2009-2010 Alexander Gordeev <lasaine@lvk.cs.msu.su>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/spinlock.h>
15*4882a593Smuzhiyun #include <linux/pps_kernel.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "kc.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun * Global variables
21*4882a593Smuzhiyun */
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* state variables to bind kernel consumer */
24*4882a593Smuzhiyun static DEFINE_SPINLOCK(pps_kc_hardpps_lock);
25*4882a593Smuzhiyun /* PPS API (RFC 2783): current source and mode for kernel consumer */
26*4882a593Smuzhiyun static struct pps_device *pps_kc_hardpps_dev; /* unique pointer to device */
27*4882a593Smuzhiyun static int pps_kc_hardpps_mode; /* mode bits for kernel consumer */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* pps_kc_bind - control PPS kernel consumer binding
30*4882a593Smuzhiyun * @pps: the PPS source
31*4882a593Smuzhiyun * @bind_args: kernel consumer bind parameters
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * This function is used to bind or unbind PPS kernel consumer according to
34*4882a593Smuzhiyun * supplied parameters. Should not be called in interrupt context.
35*4882a593Smuzhiyun */
pps_kc_bind(struct pps_device * pps,struct pps_bind_args * bind_args)36*4882a593Smuzhiyun int pps_kc_bind(struct pps_device *pps, struct pps_bind_args *bind_args)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun /* Check if another consumer is already bound */
39*4882a593Smuzhiyun spin_lock_irq(&pps_kc_hardpps_lock);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun if (bind_args->edge == 0)
42*4882a593Smuzhiyun if (pps_kc_hardpps_dev == pps) {
43*4882a593Smuzhiyun pps_kc_hardpps_mode = 0;
44*4882a593Smuzhiyun pps_kc_hardpps_dev = NULL;
45*4882a593Smuzhiyun spin_unlock_irq(&pps_kc_hardpps_lock);
46*4882a593Smuzhiyun dev_info(pps->dev, "unbound kernel"
47*4882a593Smuzhiyun " consumer\n");
48*4882a593Smuzhiyun } else {
49*4882a593Smuzhiyun spin_unlock_irq(&pps_kc_hardpps_lock);
50*4882a593Smuzhiyun dev_err(pps->dev, "selected kernel consumer"
51*4882a593Smuzhiyun " is not bound\n");
52*4882a593Smuzhiyun return -EINVAL;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun else
55*4882a593Smuzhiyun if (pps_kc_hardpps_dev == NULL ||
56*4882a593Smuzhiyun pps_kc_hardpps_dev == pps) {
57*4882a593Smuzhiyun pps_kc_hardpps_mode = bind_args->edge;
58*4882a593Smuzhiyun pps_kc_hardpps_dev = pps;
59*4882a593Smuzhiyun spin_unlock_irq(&pps_kc_hardpps_lock);
60*4882a593Smuzhiyun dev_info(pps->dev, "bound kernel consumer: "
61*4882a593Smuzhiyun "edge=0x%x\n", bind_args->edge);
62*4882a593Smuzhiyun } else {
63*4882a593Smuzhiyun spin_unlock_irq(&pps_kc_hardpps_lock);
64*4882a593Smuzhiyun dev_err(pps->dev, "another kernel consumer"
65*4882a593Smuzhiyun " is already bound\n");
66*4882a593Smuzhiyun return -EINVAL;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun return 0;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* pps_kc_remove - unbind kernel consumer on PPS source removal
73*4882a593Smuzhiyun * @pps: the PPS source
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * This function is used to disable kernel consumer on PPS source removal
76*4882a593Smuzhiyun * if this source was bound to PPS kernel consumer. Can be called on any
77*4882a593Smuzhiyun * source safely. Should not be called in interrupt context.
78*4882a593Smuzhiyun */
pps_kc_remove(struct pps_device * pps)79*4882a593Smuzhiyun void pps_kc_remove(struct pps_device *pps)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun spin_lock_irq(&pps_kc_hardpps_lock);
82*4882a593Smuzhiyun if (pps == pps_kc_hardpps_dev) {
83*4882a593Smuzhiyun pps_kc_hardpps_mode = 0;
84*4882a593Smuzhiyun pps_kc_hardpps_dev = NULL;
85*4882a593Smuzhiyun spin_unlock_irq(&pps_kc_hardpps_lock);
86*4882a593Smuzhiyun dev_info(pps->dev, "unbound kernel consumer"
87*4882a593Smuzhiyun " on device removal\n");
88*4882a593Smuzhiyun } else
89*4882a593Smuzhiyun spin_unlock_irq(&pps_kc_hardpps_lock);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* pps_kc_event - call hardpps() on PPS event
93*4882a593Smuzhiyun * @pps: the PPS source
94*4882a593Smuzhiyun * @ts: PPS event timestamp
95*4882a593Smuzhiyun * @event: PPS event edge
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * This function calls hardpps() when an event from bound PPS source occurs.
98*4882a593Smuzhiyun */
pps_kc_event(struct pps_device * pps,struct pps_event_time * ts,int event)99*4882a593Smuzhiyun void pps_kc_event(struct pps_device *pps, struct pps_event_time *ts,
100*4882a593Smuzhiyun int event)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun unsigned long flags;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* Pass some events to kernel consumer if activated */
105*4882a593Smuzhiyun spin_lock_irqsave(&pps_kc_hardpps_lock, flags);
106*4882a593Smuzhiyun if (pps == pps_kc_hardpps_dev && event & pps_kc_hardpps_mode)
107*4882a593Smuzhiyun hardpps(&ts->ts_real, &ts->ts_raw);
108*4882a593Smuzhiyun spin_unlock_irqrestore(&pps_kc_hardpps_lock, flags);
109*4882a593Smuzhiyun }
110