Merge tag 'rtc-5.14' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
[linux-2.6-microblaze.git] / include / linux / ptp_clock_kernel.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * PTP 1588 clock support
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  */
7
8 #ifndef _PTP_CLOCK_KERNEL_H_
9 #define _PTP_CLOCK_KERNEL_H_
10
11 #include <linux/device.h>
12 #include <linux/pps_kernel.h>
13 #include <linux/ptp_clock.h>
14
15 /**
16  * struct ptp_clock_request - request PTP clock event
17  *
18  * @type:   The type of the request.
19  *          EXTTS:  Configure external trigger timestamping
20  *          PEROUT: Configure periodic output signal (e.g. PPS)
21  *          PPS:    trigger internal PPS event for input
22  *                  into kernel PPS subsystem
23  * @extts:  describes configuration for external trigger timestamping.
24  *          This is only valid when event == PTP_CLK_REQ_EXTTS.
25  * @perout: describes configuration for periodic output.
26  *          This is only valid when event == PTP_CLK_REQ_PEROUT.
27  */
28
29 struct ptp_clock_request {
30         enum {
31                 PTP_CLK_REQ_EXTTS,
32                 PTP_CLK_REQ_PEROUT,
33                 PTP_CLK_REQ_PPS,
34         } type;
35         union {
36                 struct ptp_extts_request extts;
37                 struct ptp_perout_request perout;
38         };
39 };
40
41 struct system_device_crosststamp;
42
43 /**
44  * struct ptp_system_timestamp - system time corresponding to a PHC timestamp
45  */
46 struct ptp_system_timestamp {
47         struct timespec64 pre_ts;
48         struct timespec64 post_ts;
49 };
50
51 /**
52  * struct ptp_clock_info - describes a PTP hardware clock
53  *
54  * @owner:     The clock driver should set to THIS_MODULE.
55  * @name:      A short "friendly name" to identify the clock and to
56  *             help distinguish PHY based devices from MAC based ones.
57  *             The string is not meant to be a unique id.
58  * @max_adj:   The maximum possible frequency adjustment, in parts per billon.
59  * @n_alarm:   The number of programmable alarms.
60  * @n_ext_ts:  The number of external time stamp channels.
61  * @n_per_out: The number of programmable periodic signals.
62  * @n_pins:    The number of programmable pins.
63  * @pps:       Indicates whether the clock supports a PPS callback.
64  * @pin_config: Array of length 'n_pins'. If the number of
65  *              programmable pins is nonzero, then drivers must
66  *              allocate and initialize this array.
67  *
68  * clock operations
69  *
70  * @adjfine:  Adjusts the frequency of the hardware clock.
71  *            parameter scaled_ppm: Desired frequency offset from
72  *            nominal frequency in parts per million, but with a
73  *            16 bit binary fractional field.
74  *
75  * @adjfreq:  Adjusts the frequency of the hardware clock.
76  *            This method is deprecated.  New drivers should implement
77  *            the @adjfine method instead.
78  *            parameter delta: Desired frequency offset from nominal frequency
79  *            in parts per billion
80  *
81  * @adjphase:  Adjusts the phase offset of the hardware clock.
82  *             parameter delta: Desired change in nanoseconds.
83  *
84  * @adjtime:  Shifts the time of the hardware clock.
85  *            parameter delta: Desired change in nanoseconds.
86  *
87  * @gettime64:  Reads the current time from the hardware clock.
88  *              This method is deprecated.  New drivers should implement
89  *              the @gettimex64 method instead.
90  *              parameter ts: Holds the result.
91  *
92  * @gettimex64:  Reads the current time from the hardware clock and optionally
93  *               also the system clock.
94  *               parameter ts: Holds the PHC timestamp.
95  *               parameter sts: If not NULL, it holds a pair of timestamps from
96  *               the system clock. The first reading is made right before
97  *               reading the lowest bits of the PHC timestamp and the second
98  *               reading immediately follows that.
99  *
100  * @getcrosststamp:  Reads the current time from the hardware clock and
101  *                   system clock simultaneously.
102  *                   parameter cts: Contains timestamp (device,system) pair,
103  *                   where system time is realtime and monotonic.
104  *
105  * @settime64:  Set the current time on the hardware clock.
106  *              parameter ts: Time value to set.
107  *
108  * @enable:   Request driver to enable or disable an ancillary feature.
109  *            parameter request: Desired resource to enable or disable.
110  *            parameter on: Caller passes one to enable or zero to disable.
111  *
112  * @verify:   Confirm that a pin can perform a given function. The PTP
113  *            Hardware Clock subsystem maintains the 'pin_config'
114  *            array on behalf of the drivers, but the PHC subsystem
115  *            assumes that every pin can perform every function. This
116  *            hook gives drivers a way of telling the core about
117  *            limitations on specific pins. This function must return
118  *            zero if the function can be assigned to this pin, and
119  *            nonzero otherwise.
120  *            parameter pin: index of the pin in question.
121  *            parameter func: the desired function to use.
122  *            parameter chan: the function channel index to use.
123  *
124  * @do_aux_work:  Request driver to perform auxiliary (periodic) operations
125  *                Driver should return delay of the next auxiliary work
126  *                scheduling time (>=0) or negative value in case further
127  *                scheduling is not required.
128  *
129  * Drivers should embed their ptp_clock_info within a private
130  * structure, obtaining a reference to it using container_of().
131  *
132  * The callbacks must all return zero on success, non-zero otherwise.
133  */
134
135 struct ptp_clock_info {
136         struct module *owner;
137         char name[16];
138         s32 max_adj;
139         int n_alarm;
140         int n_ext_ts;
141         int n_per_out;
142         int n_pins;
143         int pps;
144         struct ptp_pin_desc *pin_config;
145         int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm);
146         int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta);
147         int (*adjphase)(struct ptp_clock_info *ptp, s32 phase);
148         int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
149         int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
150         int (*gettimex64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
151                           struct ptp_system_timestamp *sts);
152         int (*getcrosststamp)(struct ptp_clock_info *ptp,
153                               struct system_device_crosststamp *cts);
154         int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
155         int (*enable)(struct ptp_clock_info *ptp,
156                       struct ptp_clock_request *request, int on);
157         int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
158                       enum ptp_pin_function func, unsigned int chan);
159         long (*do_aux_work)(struct ptp_clock_info *ptp);
160 };
161
162 struct ptp_clock;
163
164 enum ptp_clock_events {
165         PTP_CLOCK_ALARM,
166         PTP_CLOCK_EXTTS,
167         PTP_CLOCK_PPS,
168         PTP_CLOCK_PPSUSR,
169 };
170
171 /**
172  * struct ptp_clock_event - decribes a PTP hardware clock event
173  *
174  * @type:  One of the ptp_clock_events enumeration values.
175  * @index: Identifies the source of the event.
176  * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
177  * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
178  */
179
180 struct ptp_clock_event {
181         int type;
182         int index;
183         union {
184                 u64 timestamp;
185                 struct pps_event_time pps_times;
186         };
187 };
188
189 /**
190  * scaled_ppm_to_ppb() - convert scaled ppm to ppb
191  *
192  * @ppm:    Parts per million, but with a 16 bit binary fractional field
193  */
194 static inline long scaled_ppm_to_ppb(long ppm)
195 {
196         /*
197          * The 'freq' field in the 'struct timex' is in parts per
198          * million, but with a 16 bit binary fractional field.
199          *
200          * We want to calculate
201          *
202          *    ppb = scaled_ppm * 1000 / 2^16
203          *
204          * which simplifies to
205          *
206          *    ppb = scaled_ppm * 125 / 2^13
207          */
208         s64 ppb = 1 + ppm;
209
210         ppb *= 125;
211         ppb >>= 13;
212         return (long)ppb;
213 }
214
215 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
216
217 /**
218  * ptp_clock_register() - register a PTP hardware clock driver
219  *
220  * @info:   Structure describing the new clock.
221  * @parent: Pointer to the parent device of the new clock.
222  *
223  * Returns a valid pointer on success or PTR_ERR on failure.  If PHC
224  * support is missing at the configuration level, this function
225  * returns NULL, and drivers are expected to gracefully handle that
226  * case separately.
227  */
228
229 extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
230                                             struct device *parent);
231
232 /**
233  * ptp_clock_unregister() - unregister a PTP hardware clock driver
234  *
235  * @ptp:  The clock to remove from service.
236  */
237
238 extern int ptp_clock_unregister(struct ptp_clock *ptp);
239
240 /**
241  * ptp_clock_event() - notify the PTP layer about an event
242  *
243  * @ptp:    The clock obtained from ptp_clock_register().
244  * @event:  Message structure describing the event.
245  */
246
247 extern void ptp_clock_event(struct ptp_clock *ptp,
248                             struct ptp_clock_event *event);
249
250 /**
251  * ptp_clock_index() - obtain the device index of a PTP clock
252  *
253  * @ptp:    The clock obtained from ptp_clock_register().
254  */
255
256 extern int ptp_clock_index(struct ptp_clock *ptp);
257
258 /**
259  * ptp_find_pin() - obtain the pin index of a given auxiliary function
260  *
261  * The caller must hold ptp_clock::pincfg_mux.  Drivers do not have
262  * access to that mutex as ptp_clock is an opaque type.  However, the
263  * core code acquires the mutex before invoking the driver's
264  * ptp_clock_info::enable() callback, and so drivers may call this
265  * function from that context.
266  *
267  * @ptp:    The clock obtained from ptp_clock_register().
268  * @func:   One of the ptp_pin_function enumerated values.
269  * @chan:   The particular functional channel to find.
270  * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
271  *          or -1 if the auxiliary function cannot be found.
272  */
273
274 int ptp_find_pin(struct ptp_clock *ptp,
275                  enum ptp_pin_function func, unsigned int chan);
276
277 /**
278  * ptp_find_pin_unlocked() - wrapper for ptp_find_pin()
279  *
280  * This function acquires the ptp_clock::pincfg_mux mutex before
281  * invoking ptp_find_pin().  Instead of using this function, drivers
282  * should most likely call ptp_find_pin() directly from their
283  * ptp_clock_info::enable() method.
284  *
285  */
286
287 int ptp_find_pin_unlocked(struct ptp_clock *ptp,
288                           enum ptp_pin_function func, unsigned int chan);
289
290 /**
291  * ptp_schedule_worker() - schedule ptp auxiliary work
292  *
293  * @ptp:    The clock obtained from ptp_clock_register().
294  * @delay:  number of jiffies to wait before queuing
295  *          See kthread_queue_delayed_work() for more info.
296  */
297
298 int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay);
299
300 /**
301  * ptp_cancel_worker_sync() - cancel ptp auxiliary clock
302  *
303  * @ptp:     The clock obtained from ptp_clock_register().
304  */
305 void ptp_cancel_worker_sync(struct ptp_clock *ptp);
306
307 #else
308 static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
309                                                    struct device *parent)
310 { return NULL; }
311 static inline int ptp_clock_unregister(struct ptp_clock *ptp)
312 { return 0; }
313 static inline void ptp_clock_event(struct ptp_clock *ptp,
314                                    struct ptp_clock_event *event)
315 { }
316 static inline int ptp_clock_index(struct ptp_clock *ptp)
317 { return -1; }
318 static inline int ptp_find_pin(struct ptp_clock *ptp,
319                                enum ptp_pin_function func, unsigned int chan)
320 { return -1; }
321 static inline int ptp_schedule_worker(struct ptp_clock *ptp,
322                                       unsigned long delay)
323 { return -EOPNOTSUPP; }
324 static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp)
325 { }
326
327 #endif
328
329 static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts)
330 {
331         if (sts)
332                 ktime_get_real_ts64(&sts->pre_ts);
333 }
334
335 static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts)
336 {
337         if (sts)
338                 ktime_get_real_ts64(&sts->post_ts);
339 }
340
341 #endif