1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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#include <uapi/linux/sched/types.h>
32
33#include "ptp_private.h"
34
35#define PTP_MAX_ALARMS 4
36#define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT)
37#define PTP_PPS_EVENT PPS_CAPTUREASSERT
38#define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
39
40
41
42static dev_t ptp_devt;
43static struct class *ptp_class;
44
45static DEFINE_IDA(ptp_clocks_map);
46
47
48
49static inline int queue_free(struct timestamp_event_queue *q)
50{
51 return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
52}
53
54static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
55 struct ptp_clock_event *src)
56{
57 struct ptp_extts_event *dst;
58 unsigned long flags;
59 s64 seconds;
60 u32 remainder;
61
62 seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
63
64 spin_lock_irqsave(&queue->lock, flags);
65
66 dst = &queue->buf[queue->tail];
67 dst->index = src->index;
68 dst->t.sec = seconds;
69 dst->t.nsec = remainder;
70
71 if (!queue_free(queue))
72 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
73
74 queue->tail = (queue->tail + 1) % PTP_MAX_TIMESTAMPS;
75
76 spin_unlock_irqrestore(&queue->lock, flags);
77}
78
79s32 scaled_ppm_to_ppb(long ppm)
80{
81
82
83
84
85
86
87
88
89
90
91
92
93 s64 ppb = 1 + ppm;
94 ppb *= 125;
95 ppb >>= 13;
96 return (s32) ppb;
97}
98EXPORT_SYMBOL(scaled_ppm_to_ppb);
99
100
101
102static int ptp_clock_getres(struct posix_clock *pc, struct timespec64 *tp)
103{
104 tp->tv_sec = 0;
105 tp->tv_nsec = 1;
106 return 0;
107}
108
109static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp)
110{
111 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
112
113 return ptp->info->settime64(ptp->info, tp);
114}
115
116static int ptp_clock_gettime(struct posix_clock *pc, struct timespec64 *tp)
117{
118 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
119 int err;
120
121 if (ptp->info->gettimex64)
122 err = ptp->info->gettimex64(ptp->info, tp, NULL);
123 else
124 err = ptp->info->gettime64(ptp->info, tp);
125 return err;
126}
127
128static int ptp_clock_adjtime(struct posix_clock *pc, struct timex *tx)
129{
130 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
131 struct ptp_clock_info *ops;
132 int err = -EOPNOTSUPP;
133
134 ops = ptp->info;
135
136 if (tx->modes & ADJ_SETOFFSET) {
137 struct timespec64 ts;
138 ktime_t kt;
139 s64 delta;
140
141 ts.tv_sec = tx->time.tv_sec;
142 ts.tv_nsec = tx->time.tv_usec;
143
144 if (!(tx->modes & ADJ_NANO))
145 ts.tv_nsec *= 1000;
146
147 if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
148 return -EINVAL;
149
150 kt = timespec64_to_ktime(ts);
151 delta = ktime_to_ns(kt);
152 err = ops->adjtime(ops, delta);
153 } else if (tx->modes & ADJ_FREQUENCY) {
154 s32 ppb = scaled_ppm_to_ppb(tx->freq);
155 if (ppb > ops->max_adj || ppb < -ops->max_adj)
156 return -ERANGE;
157 if (ops->adjfine)
158 err = ops->adjfine(ops, tx->freq);
159 else
160 err = ops->adjfreq(ops, ppb);
161 ptp->dialed_frequency = tx->freq;
162 } else if (tx->modes == 0) {
163 tx->freq = ptp->dialed_frequency;
164 err = 0;
165 }
166
167 return err;
168}
169
170static struct posix_clock_operations ptp_clock_ops = {
171 .owner = THIS_MODULE,
172 .clock_adjtime = ptp_clock_adjtime,
173 .clock_gettime = ptp_clock_gettime,
174 .clock_getres = ptp_clock_getres,
175 .clock_settime = ptp_clock_settime,
176 .ioctl = ptp_ioctl,
177 .open = ptp_open,
178 .poll = ptp_poll,
179 .read = ptp_read,
180};
181
182static void ptp_clock_release(struct device *dev)
183{
184 struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
185
186 ptp_cleanup_pin_groups(ptp);
187 mutex_destroy(&ptp->tsevq_mux);
188 mutex_destroy(&ptp->pincfg_mux);
189 ida_simple_remove(&ptp_clocks_map, ptp->index);
190 kfree(ptp);
191}
192
193static void ptp_aux_kworker(struct kthread_work *work)
194{
195 struct ptp_clock *ptp = container_of(work, struct ptp_clock,
196 aux_work.work);
197 struct ptp_clock_info *info = ptp->info;
198 long delay;
199
200 delay = info->do_aux_work(info);
201
202 if (delay >= 0)
203 kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay);
204}
205
206
207
208struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
209 struct device *parent)
210{
211 struct ptp_clock *ptp;
212 int err = 0, index, major = MAJOR(ptp_devt);
213
214 if (info->n_alarm > PTP_MAX_ALARMS)
215 return ERR_PTR(-EINVAL);
216
217
218 err = -ENOMEM;
219 ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
220 if (ptp == NULL)
221 goto no_memory;
222
223 index = ida_simple_get(&ptp_clocks_map, 0, MINORMASK + 1, GFP_KERNEL);
224 if (index < 0) {
225 err = index;
226 goto no_slot;
227 }
228
229 ptp->clock.ops = ptp_clock_ops;
230 ptp->info = info;
231 ptp->devid = MKDEV(major, index);
232 ptp->index = index;
233 spin_lock_init(&ptp->tsevq.lock);
234 mutex_init(&ptp->tsevq_mux);
235 mutex_init(&ptp->pincfg_mux);
236 init_waitqueue_head(&ptp->tsev_wq);
237
238 if (ptp->info->do_aux_work) {
239 char *worker_name = kasprintf(GFP_KERNEL, "ptp%d", ptp->index);
240
241 kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker);
242 ptp->kworker = kthread_create_worker(0, worker_name ?
243 worker_name : info->name);
244 kfree(worker_name);
245 if (IS_ERR(ptp->kworker)) {
246 err = PTR_ERR(ptp->kworker);
247 pr_err("failed to create ptp aux_worker %d\n", err);
248 goto kworker_err;
249 }
250 }
251
252 err = ptp_populate_pin_groups(ptp);
253 if (err)
254 goto no_pin_groups;
255
256
257 if (info->pps) {
258 struct pps_source_info pps;
259 memset(&pps, 0, sizeof(pps));
260 snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
261 pps.mode = PTP_PPS_MODE;
262 pps.owner = info->owner;
263 ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
264 if (!ptp->pps_source) {
265 err = -EINVAL;
266 pr_err("failed to register pps source\n");
267 goto no_pps;
268 }
269 }
270
271
272 device_initialize(&ptp->dev);
273 ptp->dev.devt = ptp->devid;
274 ptp->dev.class = ptp_class;
275 ptp->dev.parent = parent;
276 ptp->dev.groups = ptp->pin_attr_groups;
277 ptp->dev.release = ptp_clock_release;
278 dev_set_drvdata(&ptp->dev, ptp);
279 dev_set_name(&ptp->dev, "ptp%d", ptp->index);
280
281
282 err = posix_clock_register(&ptp->clock, &ptp->dev);
283 if (err) {
284 pr_err("failed to create posix clock\n");
285 goto no_clock;
286 }
287
288 return ptp;
289
290no_clock:
291 if (ptp->pps_source)
292 pps_unregister_source(ptp->pps_source);
293no_pps:
294 ptp_cleanup_pin_groups(ptp);
295no_pin_groups:
296 if (ptp->kworker)
297 kthread_destroy_worker(ptp->kworker);
298kworker_err:
299 mutex_destroy(&ptp->tsevq_mux);
300 mutex_destroy(&ptp->pincfg_mux);
301 ida_simple_remove(&ptp_clocks_map, index);
302no_slot:
303 kfree(ptp);
304no_memory:
305 return ERR_PTR(err);
306}
307EXPORT_SYMBOL(ptp_clock_register);
308
309int ptp_clock_unregister(struct ptp_clock *ptp)
310{
311 ptp->defunct = 1;
312 wake_up_interruptible(&ptp->tsev_wq);
313
314 if (ptp->kworker) {
315 kthread_cancel_delayed_work_sync(&ptp->aux_work);
316 kthread_destroy_worker(ptp->kworker);
317 }
318
319
320 if (ptp->pps_source)
321 pps_unregister_source(ptp->pps_source);
322
323 posix_clock_unregister(&ptp->clock);
324
325 return 0;
326}
327EXPORT_SYMBOL(ptp_clock_unregister);
328
329void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
330{
331 struct pps_event_time evt;
332
333 switch (event->type) {
334
335 case PTP_CLOCK_ALARM:
336 break;
337
338 case PTP_CLOCK_EXTTS:
339 enqueue_external_timestamp(&ptp->tsevq, event);
340 wake_up_interruptible(&ptp->tsev_wq);
341 break;
342
343 case PTP_CLOCK_PPS:
344 pps_get_ts(&evt);
345 pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
346 break;
347
348 case PTP_CLOCK_PPSUSR:
349 pps_event(ptp->pps_source, &event->pps_times,
350 PTP_PPS_EVENT, NULL);
351 break;
352 }
353}
354EXPORT_SYMBOL(ptp_clock_event);
355
356int ptp_clock_index(struct ptp_clock *ptp)
357{
358 return ptp->index;
359}
360EXPORT_SYMBOL(ptp_clock_index);
361
362int ptp_find_pin(struct ptp_clock *ptp,
363 enum ptp_pin_function func, unsigned int chan)
364{
365 struct ptp_pin_desc *pin = NULL;
366 int i;
367
368 for (i = 0; i < ptp->info->n_pins; i++) {
369 if (ptp->info->pin_config[i].func == func &&
370 ptp->info->pin_config[i].chan == chan) {
371 pin = &ptp->info->pin_config[i];
372 break;
373 }
374 }
375
376 return pin ? i : -1;
377}
378EXPORT_SYMBOL(ptp_find_pin);
379
380int ptp_find_pin_unlocked(struct ptp_clock *ptp,
381 enum ptp_pin_function func, unsigned int chan)
382{
383 int result;
384
385 mutex_lock(&ptp->pincfg_mux);
386
387 result = ptp_find_pin(ptp, func, chan);
388
389 mutex_unlock(&ptp->pincfg_mux);
390
391 return result;
392}
393EXPORT_SYMBOL(ptp_find_pin_unlocked);
394
395int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay)
396{
397 return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay);
398}
399EXPORT_SYMBOL(ptp_schedule_worker);
400
401
402
403static void __exit ptp_exit(void)
404{
405 class_destroy(ptp_class);
406 unregister_chrdev_region(ptp_devt, MINORMASK + 1);
407 ida_destroy(&ptp_clocks_map);
408}
409
410static int __init ptp_init(void)
411{
412 int err;
413
414 ptp_class = class_create(THIS_MODULE, "ptp");
415 if (IS_ERR(ptp_class)) {
416 pr_err("ptp: failed to allocate class\n");
417 return PTR_ERR(ptp_class);
418 }
419
420 err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
421 if (err < 0) {
422 pr_err("ptp: failed to allocate device region\n");
423 goto no_region;
424 }
425
426 ptp_class->dev_groups = ptp_groups;
427 pr_info("PTP clock support registered\n");
428 return 0;
429
430no_region:
431 class_destroy(ptp_class);
432 return err;
433}
434
435subsys_initcall(ptp_init);
436module_exit(ptp_exit);
437
438MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
439MODULE_DESCRIPTION("PTP clocks support");
440MODULE_LICENSE("GPL");
441