1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PTP virtual clock driver
7 #include <linux/slab.h>
8 #include "ptp_private.h"
10 #define PTP_VCLOCK_CC_SHIFT 31
11 #define PTP_VCLOCK_CC_MULT (1 << PTP_VCLOCK_CC_SHIFT)
12 #define PTP_VCLOCK_FADJ_SHIFT 9
13 #define PTP_VCLOCK_FADJ_DENOMINATOR 15625ULL
14 #define PTP_VCLOCK_REFRESH_INTERVAL (HZ * 2)
16 static int ptp_vclock_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
18 struct ptp_vclock *vclock = info_to_vclock(ptp);
22 adj = (s64)scaled_ppm << PTP_VCLOCK_FADJ_SHIFT;
23 adj = div_s64(adj, PTP_VCLOCK_FADJ_DENOMINATOR);
25 spin_lock_irqsave(&vclock->lock, flags);
26 timecounter_read(&vclock->tc);
27 vclock->cc.mult = PTP_VCLOCK_CC_MULT + adj;
28 spin_unlock_irqrestore(&vclock->lock, flags);
33 static int ptp_vclock_adjtime(struct ptp_clock_info *ptp, s64 delta)
35 struct ptp_vclock *vclock = info_to_vclock(ptp);
38 spin_lock_irqsave(&vclock->lock, flags);
39 timecounter_adjtime(&vclock->tc, delta);
40 spin_unlock_irqrestore(&vclock->lock, flags);
45 static int ptp_vclock_gettime(struct ptp_clock_info *ptp,
46 struct timespec64 *ts)
48 struct ptp_vclock *vclock = info_to_vclock(ptp);
52 spin_lock_irqsave(&vclock->lock, flags);
53 ns = timecounter_read(&vclock->tc);
54 spin_unlock_irqrestore(&vclock->lock, flags);
55 *ts = ns_to_timespec64(ns);
60 static int ptp_vclock_settime(struct ptp_clock_info *ptp,
61 const struct timespec64 *ts)
63 struct ptp_vclock *vclock = info_to_vclock(ptp);
64 u64 ns = timespec64_to_ns(ts);
67 spin_lock_irqsave(&vclock->lock, flags);
68 timecounter_init(&vclock->tc, &vclock->cc, ns);
69 spin_unlock_irqrestore(&vclock->lock, flags);
74 static long ptp_vclock_refresh(struct ptp_clock_info *ptp)
76 struct ptp_vclock *vclock = info_to_vclock(ptp);
79 ptp_vclock_gettime(&vclock->info, &ts);
81 return PTP_VCLOCK_REFRESH_INTERVAL;
84 static const struct ptp_clock_info ptp_vclock_info = {
86 .name = "ptp virtual clock",
87 /* The maximum ppb value that long scaled_ppm can support */
89 .adjfine = ptp_vclock_adjfine,
90 .adjtime = ptp_vclock_adjtime,
91 .gettime64 = ptp_vclock_gettime,
92 .settime64 = ptp_vclock_settime,
93 .do_aux_work = ptp_vclock_refresh,
96 static u64 ptp_vclock_read(const struct cyclecounter *cc)
98 struct ptp_vclock *vclock = cc_to_vclock(cc);
99 struct ptp_clock *ptp = vclock->pclock;
100 struct timespec64 ts = {};
102 if (ptp->info->gettimex64)
103 ptp->info->gettimex64(ptp->info, &ts, NULL);
105 ptp->info->gettime64(ptp->info, &ts);
107 return timespec64_to_ns(&ts);
110 static const struct cyclecounter ptp_vclock_cc = {
111 .read = ptp_vclock_read,
112 .mask = CYCLECOUNTER_MASK(32),
113 .mult = PTP_VCLOCK_CC_MULT,
114 .shift = PTP_VCLOCK_CC_SHIFT,
117 struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock)
119 struct ptp_vclock *vclock;
121 vclock = kzalloc(sizeof(*vclock), GFP_KERNEL);
125 vclock->pclock = pclock;
126 vclock->info = ptp_vclock_info;
127 vclock->cc = ptp_vclock_cc;
129 snprintf(vclock->info.name, PTP_CLOCK_NAME_LEN, "ptp%d_virt",
132 spin_lock_init(&vclock->lock);
134 vclock->clock = ptp_clock_register(&vclock->info, &pclock->dev);
135 if (IS_ERR_OR_NULL(vclock->clock)) {
140 timecounter_init(&vclock->tc, &vclock->cc, 0);
141 ptp_schedule_worker(vclock->clock, PTP_VCLOCK_REFRESH_INTERVAL);
146 void ptp_vclock_unregister(struct ptp_vclock *vclock)
148 ptp_clock_unregister(vclock->clock);
152 #if IS_BUILTIN(CONFIG_PTP_1588_CLOCK)
153 int ptp_get_vclocks_index(int pclock_index, int **vclock_index)
155 char name[PTP_CLOCK_NAME_LEN] = "";
156 struct ptp_clock *ptp;
160 if (pclock_index < 0)
163 snprintf(name, PTP_CLOCK_NAME_LEN, "ptp%d", pclock_index);
164 dev = class_find_device_by_name(ptp_class, name);
168 ptp = dev_get_drvdata(dev);
170 if (mutex_lock_interruptible(&ptp->n_vclocks_mux)) {
175 *vclock_index = kzalloc(sizeof(int) * ptp->n_vclocks, GFP_KERNEL);
176 if (!(*vclock_index))
179 memcpy(*vclock_index, ptp->vclock_index, sizeof(int) * ptp->n_vclocks);
180 num = ptp->n_vclocks;
182 mutex_unlock(&ptp->n_vclocks_mux);
186 EXPORT_SYMBOL(ptp_get_vclocks_index);
188 void ptp_convert_timestamp(struct skb_shared_hwtstamps *hwtstamps,
191 char name[PTP_CLOCK_NAME_LEN] = "";
192 struct ptp_vclock *vclock;
193 struct ptp_clock *ptp;
198 snprintf(name, PTP_CLOCK_NAME_LEN, "ptp%d", vclock_index);
199 dev = class_find_device_by_name(ptp_class, name);
203 ptp = dev_get_drvdata(dev);
204 if (!ptp->is_virtual_clock) {
209 vclock = info_to_vclock(ptp->info);
211 ns = ktime_to_ns(hwtstamps->hwtstamp);
213 spin_lock_irqsave(&vclock->lock, flags);
214 ns = timecounter_cyc2time(&vclock->tc, ns);
215 spin_unlock_irqrestore(&vclock->lock, flags);
218 hwtstamps->hwtstamp = ns_to_ktime(ns);
220 EXPORT_SYMBOL(ptp_convert_timestamp);