1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * PTP 1588 clock support - private declarations for the core module.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2010 OMICRON electronics GmbH
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #ifndef _PTP_PRIVATE_H_
8*4882a593Smuzhiyun #define _PTP_PRIVATE_H_
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/cdev.h>
11*4882a593Smuzhiyun #include <linux/device.h>
12*4882a593Smuzhiyun #include <linux/kthread.h>
13*4882a593Smuzhiyun #include <linux/mutex.h>
14*4882a593Smuzhiyun #include <linux/posix-clock.h>
15*4882a593Smuzhiyun #include <linux/ptp_clock.h>
16*4882a593Smuzhiyun #include <linux/ptp_clock_kernel.h>
17*4882a593Smuzhiyun #include <linux/time.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define PTP_MAX_TIMESTAMPS 128
20*4882a593Smuzhiyun #define PTP_BUF_TIMESTAMPS 30
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun struct timestamp_event_queue {
23*4882a593Smuzhiyun struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
24*4882a593Smuzhiyun int head;
25*4882a593Smuzhiyun int tail;
26*4882a593Smuzhiyun spinlock_t lock;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun struct ptp_clock {
30*4882a593Smuzhiyun struct posix_clock clock;
31*4882a593Smuzhiyun struct device dev;
32*4882a593Smuzhiyun struct ptp_clock_info *info;
33*4882a593Smuzhiyun dev_t devid;
34*4882a593Smuzhiyun int index; /* index into clocks.map */
35*4882a593Smuzhiyun struct pps_device *pps_source;
36*4882a593Smuzhiyun long dialed_frequency; /* remembers the frequency adjustment */
37*4882a593Smuzhiyun struct timestamp_event_queue tsevq; /* simple fifo for time stamps */
38*4882a593Smuzhiyun struct mutex tsevq_mux; /* one process at a time reading the fifo */
39*4882a593Smuzhiyun struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
40*4882a593Smuzhiyun wait_queue_head_t tsev_wq;
41*4882a593Smuzhiyun int defunct; /* tells readers to go away when clock is being removed */
42*4882a593Smuzhiyun struct device_attribute *pin_dev_attr;
43*4882a593Smuzhiyun struct attribute **pin_attr;
44*4882a593Smuzhiyun struct attribute_group pin_attr_group;
45*4882a593Smuzhiyun /* 1st entry is a pointer to the real group, 2nd is NULL terminator */
46*4882a593Smuzhiyun const struct attribute_group *pin_attr_groups[2];
47*4882a593Smuzhiyun struct kthread_worker *kworker;
48*4882a593Smuzhiyun struct kthread_delayed_work aux_work;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * The function queue_cnt() is safe for readers to call without
53*4882a593Smuzhiyun * holding q->lock. Readers use this function to verify that the queue
54*4882a593Smuzhiyun * is nonempty before proceeding with a dequeue operation. The fact
55*4882a593Smuzhiyun * that a writer might concurrently increment the tail does not
56*4882a593Smuzhiyun * matter, since the queue remains nonempty nonetheless.
57*4882a593Smuzhiyun */
queue_cnt(struct timestamp_event_queue * q)58*4882a593Smuzhiyun static inline int queue_cnt(struct timestamp_event_queue *q)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun int cnt = q->tail - q->head;
61*4882a593Smuzhiyun return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * see ptp_chardev.c
66*4882a593Smuzhiyun */
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* caller must hold pincfg_mux */
69*4882a593Smuzhiyun int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
70*4882a593Smuzhiyun enum ptp_pin_function func, unsigned int chan);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun long ptp_ioctl(struct posix_clock *pc,
73*4882a593Smuzhiyun unsigned int cmd, unsigned long arg);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun int ptp_open(struct posix_clock *pc, fmode_t fmode);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun ssize_t ptp_read(struct posix_clock *pc,
78*4882a593Smuzhiyun uint flags, char __user *buf, size_t cnt);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun __poll_t ptp_poll(struct posix_clock *pc,
81*4882a593Smuzhiyun struct file *fp, poll_table *wait);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun * see ptp_sysfs.c
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun extern const struct attribute_group *ptp_groups[];
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun int ptp_populate_pin_groups(struct ptp_clock *ptp);
90*4882a593Smuzhiyun void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun #endif
93