174ba9207SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2d94ba80eSRichard Cochran /*
3d94ba80eSRichard Cochran  * PTP 1588 clock support
4d94ba80eSRichard Cochran  *
5d94ba80eSRichard Cochran  * Copyright (C) 2010 OMICRON electronics GmbH
6d94ba80eSRichard Cochran  */
7d94ba80eSRichard Cochran 
8d94ba80eSRichard Cochran #ifndef _PTP_CLOCK_KERNEL_H_
9d94ba80eSRichard Cochran #define _PTP_CLOCK_KERNEL_H_
10d94ba80eSRichard Cochran 
111ef76158SRichard Cochran #include <linux/device.h>
12220a60a4SBen Hutchings #include <linux/pps_kernel.h>
13d94ba80eSRichard Cochran #include <linux/ptp_clock.h>
145d43f951SYangbo Lu #include <linux/timecounter.h>
15895487a3SYangbo Lu #include <linux/skbuff.h>
16d94ba80eSRichard Cochran 
175d43f951SYangbo Lu #define PTP_CLOCK_NAME_LEN	32
18d04a53b1SAhmad Fatoum /**
19d04a53b1SAhmad Fatoum  * struct ptp_clock_request - request PTP clock event
20d04a53b1SAhmad Fatoum  *
21d04a53b1SAhmad Fatoum  * @type:   The type of the request.
22d04a53b1SAhmad Fatoum  *	    EXTTS:  Configure external trigger timestamping
23d04a53b1SAhmad Fatoum  *	    PEROUT: Configure periodic output signal (e.g. PPS)
24d04a53b1SAhmad Fatoum  *	    PPS:    trigger internal PPS event for input
25d04a53b1SAhmad Fatoum  *	            into kernel PPS subsystem
26d04a53b1SAhmad Fatoum  * @extts:  describes configuration for external trigger timestamping.
27d04a53b1SAhmad Fatoum  *          This is only valid when event == PTP_CLK_REQ_EXTTS.
28d04a53b1SAhmad Fatoum  * @perout: describes configuration for periodic output.
29d04a53b1SAhmad Fatoum  *	    This is only valid when event == PTP_CLK_REQ_PEROUT.
30d04a53b1SAhmad Fatoum  */
31d94ba80eSRichard Cochran 
32d94ba80eSRichard Cochran struct ptp_clock_request {
33d94ba80eSRichard Cochran 	enum {
34d94ba80eSRichard Cochran 		PTP_CLK_REQ_EXTTS,
35d94ba80eSRichard Cochran 		PTP_CLK_REQ_PEROUT,
36d94ba80eSRichard Cochran 		PTP_CLK_REQ_PPS,
37d94ba80eSRichard Cochran 	} type;
38d94ba80eSRichard Cochran 	union {
39d94ba80eSRichard Cochran 		struct ptp_extts_request extts;
40d94ba80eSRichard Cochran 		struct ptp_perout_request perout;
41d94ba80eSRichard Cochran 	};
42d94ba80eSRichard Cochran };
43d94ba80eSRichard Cochran 
44719f1aa4SChristopher S. Hall struct system_device_crosststamp;
4536180087SMiroslav Lichvar 
4636180087SMiroslav Lichvar /**
4736180087SMiroslav Lichvar  * struct ptp_system_timestamp - system time corresponding to a PHC timestamp
48b9a61b97SJacob Keller  * @pre_ts: system timestamp before capturing PHC
49b9a61b97SJacob Keller  * @post_ts: system timestamp after capturing PHC
50c259acabSMahesh Bandewar  * @clockid: clock-base used for capturing the system timestamps
5136180087SMiroslav Lichvar  */
5236180087SMiroslav Lichvar struct ptp_system_timestamp {
5336180087SMiroslav Lichvar 	struct timespec64 pre_ts;
5436180087SMiroslav Lichvar 	struct timespec64 post_ts;
55c259acabSMahesh Bandewar 	clockid_t clockid;
5636180087SMiroslav Lichvar };
5736180087SMiroslav Lichvar 
58d94ba80eSRichard Cochran /**
59184ecc9eSVincent Cheng  * struct ptp_clock_info - describes a PTP hardware clock
60d94ba80eSRichard Cochran  *
61d94ba80eSRichard Cochran  * @owner:     The clock driver should set to THIS_MODULE.
62de465846SRichard Cochran  * @name:      A short "friendly name" to identify the clock and to
63de465846SRichard Cochran  *             help distinguish PHY based devices from MAC based ones.
64de465846SRichard Cochran  *             The string is not meant to be a unique id.
65d94ba80eSRichard Cochran  * @max_adj:   The maximum possible frequency adjustment, in parts per billon.
66d94ba80eSRichard Cochran  * @n_alarm:   The number of programmable alarms.
67d94ba80eSRichard Cochran  * @n_ext_ts:  The number of external time stamp channels.
68d94ba80eSRichard Cochran  * @n_per_out: The number of programmable periodic signals.
696092315dSRichard Cochran  * @n_pins:    The number of programmable pins.
70d94ba80eSRichard Cochran  * @pps:       Indicates whether the clock supports a PPS callback.
716092315dSRichard Cochran  * @pin_config: Array of length 'n_pins'. If the number of
726092315dSRichard Cochran  *              programmable pins is nonzero, then drivers must
736092315dSRichard Cochran  *              allocate and initialize this array.
74d94ba80eSRichard Cochran  *
75d94ba80eSRichard Cochran  * clock operations
76d94ba80eSRichard Cochran  *
77d8d26354SRichard Cochran  * @adjfine:  Adjusts the frequency of the hardware clock.
78d8d26354SRichard Cochran  *            parameter scaled_ppm: Desired frequency offset from
79d8d26354SRichard Cochran  *            nominal frequency in parts per million, but with a
80d8d26354SRichard Cochran  *            16 bit binary fractional field.
81d8d26354SRichard Cochran  *
82a05d070aSRahul Rameshbabu  * @adjphase:  Indicates that the PHC should use an internal servo
83a05d070aSRahul Rameshbabu  *             algorithm to correct the provided phase offset.
84a05d070aSRahul Rameshbabu  *             parameter delta: PHC servo phase adjustment target
85a05d070aSRahul Rameshbabu  *                              in nanoseconds.
86184ecc9eSVincent Cheng  *
87c3b60ab7SRahul Rameshbabu  * @getmaxphase:  Advertises maximum offset that can be provided
88c3b60ab7SRahul Rameshbabu  *                to the hardware clock's phase control functionality
89c3b60ab7SRahul Rameshbabu  *                through adjphase.
90c3b60ab7SRahul Rameshbabu  *
91d94ba80eSRichard Cochran  * @adjtime:  Shifts the time of the hardware clock.
92d94ba80eSRichard Cochran  *            parameter delta: Desired change in nanoseconds.
93d94ba80eSRichard Cochran  *
9492f17194SRichard Cochran  * @gettime64:  Reads the current time from the hardware clock.
95916444dfSMiroslav Lichvar  *              This method is deprecated.  New drivers should implement
96916444dfSMiroslav Lichvar  *              the @gettimex64 method instead.
9792f17194SRichard Cochran  *              parameter ts: Holds the result.
9892f17194SRichard Cochran  *
9936180087SMiroslav Lichvar  * @gettimex64:  Reads the current time from the hardware clock and optionally
10036180087SMiroslav Lichvar  *               also the system clock.
10136180087SMiroslav Lichvar  *               parameter ts: Holds the PHC timestamp.
10236180087SMiroslav Lichvar  *               parameter sts: If not NULL, it holds a pair of timestamps from
10336180087SMiroslav Lichvar  *               the system clock. The first reading is made right before
10436180087SMiroslav Lichvar  *               reading the lowest bits of the PHC timestamp and the second
10536180087SMiroslav Lichvar  *               reading immediately follows that.
10636180087SMiroslav Lichvar  *
107719f1aa4SChristopher S. Hall  * @getcrosststamp:  Reads the current time from the hardware clock and
108719f1aa4SChristopher S. Hall  *                   system clock simultaneously.
109719f1aa4SChristopher S. Hall  *                   parameter cts: Contains timestamp (device,system) pair,
110719f1aa4SChristopher S. Hall  *                   where system time is realtime and monotonic.
111719f1aa4SChristopher S. Hall  *
11292f17194SRichard Cochran  * @settime64:  Set the current time on the hardware clock.
113d94ba80eSRichard Cochran  *              parameter ts: Time value to set.
114d94ba80eSRichard Cochran  *
11542704b26SGerhard Engleder  * @getcycles64:  Reads the current free running cycle counter from the hardware
11642704b26SGerhard Engleder  *                clock.
11742704b26SGerhard Engleder  *                If @getcycles64 and @getcyclesx64 are not supported, then
11842704b26SGerhard Engleder  *                @gettime64 or @gettimex64 will be used as default
11942704b26SGerhard Engleder  *                implementation.
12042704b26SGerhard Engleder  *                parameter ts: Holds the result.
12142704b26SGerhard Engleder  *
12242704b26SGerhard Engleder  * @getcyclesx64:  Reads the current free running cycle counter from the
12342704b26SGerhard Engleder  *                 hardware clock and optionally also the system clock.
12442704b26SGerhard Engleder  *                 If @getcycles64 and @getcyclesx64 are not supported, then
12542704b26SGerhard Engleder  *                 @gettimex64 will be used as default implementation if
12642704b26SGerhard Engleder  *                 available.
12742704b26SGerhard Engleder  *                 parameter ts: Holds the PHC timestamp.
12842704b26SGerhard Engleder  *                 parameter sts: If not NULL, it holds a pair of timestamps
12942704b26SGerhard Engleder  *                 from the system clock. The first reading is made right before
13042704b26SGerhard Engleder  *                 reading the lowest bits of the PHC timestamp and the second
13142704b26SGerhard Engleder  *                 reading immediately follows that.
13242704b26SGerhard Engleder  *
13342704b26SGerhard Engleder  * @getcrosscycles:  Reads the current free running cycle counter from the
13442704b26SGerhard Engleder  *                   hardware clock and system clock simultaneously.
13542704b26SGerhard Engleder  *                   If @getcycles64 and @getcyclesx64 are not supported, then
13642704b26SGerhard Engleder  *                   @getcrosststamp will be used as default implementation if
13742704b26SGerhard Engleder  *                   available.
13842704b26SGerhard Engleder  *                   parameter cts: Contains timestamp (device,system) pair,
13942704b26SGerhard Engleder  *                   where system time is realtime and monotonic.
14042704b26SGerhard Engleder  *
141d94ba80eSRichard Cochran  * @enable:   Request driver to enable or disable an ancillary feature.
142d94ba80eSRichard Cochran  *            parameter request: Desired resource to enable or disable.
143d94ba80eSRichard Cochran  *            parameter on: Caller passes one to enable or zero to disable.
144d94ba80eSRichard Cochran  *
1456092315dSRichard Cochran  * @verify:   Confirm that a pin can perform a given function. The PTP
1466092315dSRichard Cochran  *            Hardware Clock subsystem maintains the 'pin_config'
1476092315dSRichard Cochran  *            array on behalf of the drivers, but the PHC subsystem
1486092315dSRichard Cochran  *            assumes that every pin can perform every function. This
1496092315dSRichard Cochran  *            hook gives drivers a way of telling the core about
1506092315dSRichard Cochran  *            limitations on specific pins. This function must return
1516092315dSRichard Cochran  *            zero if the function can be assigned to this pin, and
1526092315dSRichard Cochran  *            nonzero otherwise.
1536092315dSRichard Cochran  *            parameter pin: index of the pin in question.
1546092315dSRichard Cochran  *            parameter func: the desired function to use.
1556092315dSRichard Cochran  *            parameter chan: the function channel index to use.
1566092315dSRichard Cochran  *
1572c864c78SJacob Keller  * @do_aux_work:  Request driver to perform auxiliary (periodic) operations
1582c864c78SJacob Keller  *                Driver should return delay of the next auxiliary work
1592c864c78SJacob Keller  *                scheduling time (>=0) or negative value in case further
1602c864c78SJacob Keller  *                scheduling is not required.
161d9535cb7SGrygorii Strashko  *
162d94ba80eSRichard Cochran  * Drivers should embed their ptp_clock_info within a private
163d94ba80eSRichard Cochran  * structure, obtaining a reference to it using container_of().
164d94ba80eSRichard Cochran  *
165d94ba80eSRichard Cochran  * The callbacks must all return zero on success, non-zero otherwise.
166d94ba80eSRichard Cochran  */
167d94ba80eSRichard Cochran 
168d94ba80eSRichard Cochran struct ptp_clock_info {
169d94ba80eSRichard Cochran 	struct module *owner;
1705d43f951SYangbo Lu 	char name[PTP_CLOCK_NAME_LEN];
171d94ba80eSRichard Cochran 	s32 max_adj;
172d94ba80eSRichard Cochran 	int n_alarm;
173d94ba80eSRichard Cochran 	int n_ext_ts;
174d94ba80eSRichard Cochran 	int n_per_out;
1756092315dSRichard Cochran 	int n_pins;
176d94ba80eSRichard Cochran 	int pps;
1776092315dSRichard Cochran 	struct ptp_pin_desc *pin_config;
178d8d26354SRichard Cochran 	int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm);
179184ecc9eSVincent Cheng 	int (*adjphase)(struct ptp_clock_info *ptp, s32 phase);
180c3b60ab7SRahul Rameshbabu 	s32 (*getmaxphase)(struct ptp_clock_info *ptp);
181d94ba80eSRichard Cochran 	int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
18292f17194SRichard Cochran 	int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
18336180087SMiroslav Lichvar 	int (*gettimex64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
18436180087SMiroslav Lichvar 			  struct ptp_system_timestamp *sts);
185719f1aa4SChristopher S. Hall 	int (*getcrosststamp)(struct ptp_clock_info *ptp,
186719f1aa4SChristopher S. Hall 			      struct system_device_crosststamp *cts);
18792f17194SRichard Cochran 	int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
18842704b26SGerhard Engleder 	int (*getcycles64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
18942704b26SGerhard Engleder 	int (*getcyclesx64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
19042704b26SGerhard Engleder 			    struct ptp_system_timestamp *sts);
19142704b26SGerhard Engleder 	int (*getcrosscycles)(struct ptp_clock_info *ptp,
19242704b26SGerhard Engleder 			      struct system_device_crosststamp *cts);
193d94ba80eSRichard Cochran 	int (*enable)(struct ptp_clock_info *ptp,
194d94ba80eSRichard Cochran 		      struct ptp_clock_request *request, int on);
1956092315dSRichard Cochran 	int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
1966092315dSRichard Cochran 		      enum ptp_pin_function func, unsigned int chan);
197d9535cb7SGrygorii Strashko 	long (*do_aux_work)(struct ptp_clock_info *ptp);
198d94ba80eSRichard Cochran };
199d94ba80eSRichard Cochran 
200d94ba80eSRichard Cochran struct ptp_clock;
201d94ba80eSRichard Cochran 
202d94ba80eSRichard Cochran enum ptp_clock_events {
203d94ba80eSRichard Cochran 	PTP_CLOCK_ALARM,
204d94ba80eSRichard Cochran 	PTP_CLOCK_EXTTS,
205ea1cc3eeSMin Li 	PTP_CLOCK_EXTOFF,
206d94ba80eSRichard Cochran 	PTP_CLOCK_PPS,
207220a60a4SBen Hutchings 	PTP_CLOCK_PPSUSR,
208d94ba80eSRichard Cochran };
209d94ba80eSRichard Cochran 
210d94ba80eSRichard Cochran /**
211d94ba80eSRichard Cochran  * struct ptp_clock_event - decribes a PTP hardware clock event
212d94ba80eSRichard Cochran  *
213d94ba80eSRichard Cochran  * @type:  One of the ptp_clock_events enumeration values.
214d94ba80eSRichard Cochran  * @index: Identifies the source of the event.
215220a60a4SBen Hutchings  * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
216ea1cc3eeSMin Li  * @offset:    When the event occurred (%PTP_CLOCK_EXTOFF only).
217220a60a4SBen Hutchings  * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
218d94ba80eSRichard Cochran  */
219d94ba80eSRichard Cochran 
220d94ba80eSRichard Cochran struct ptp_clock_event {
221d94ba80eSRichard Cochran 	int type;
222d94ba80eSRichard Cochran 	int index;
223220a60a4SBen Hutchings 	union {
224d94ba80eSRichard Cochran 		u64 timestamp;
225ea1cc3eeSMin Li 		s64 offset;
226220a60a4SBen Hutchings 		struct pps_event_time pps_times;
227220a60a4SBen Hutchings 	};
228d94ba80eSRichard Cochran };
229d94ba80eSRichard Cochran 
2309d9d415fSRadu Pirea (NXP OSS) /**
2319d9d415fSRadu Pirea (NXP OSS)  * scaled_ppm_to_ppb() - convert scaled ppm to ppb
2329d9d415fSRadu Pirea (NXP OSS)  *
2339d9d415fSRadu Pirea (NXP OSS)  * @ppm:    Parts per million, but with a 16 bit binary fractional field
2349d9d415fSRadu Pirea (NXP OSS)  */
scaled_ppm_to_ppb(long ppm)235adc2e56eSJakub Kicinski static inline long scaled_ppm_to_ppb(long ppm)
2369d9d415fSRadu Pirea (NXP OSS) {
2379d9d415fSRadu Pirea (NXP OSS) 	/*
2389d9d415fSRadu Pirea (NXP OSS) 	 * The 'freq' field in the 'struct timex' is in parts per
2399d9d415fSRadu Pirea (NXP OSS) 	 * million, but with a 16 bit binary fractional field.
2409d9d415fSRadu Pirea (NXP OSS) 	 *
2419d9d415fSRadu Pirea (NXP OSS) 	 * We want to calculate
2429d9d415fSRadu Pirea (NXP OSS) 	 *
2439d9d415fSRadu Pirea (NXP OSS) 	 *    ppb = scaled_ppm * 1000 / 2^16
2449d9d415fSRadu Pirea (NXP OSS) 	 *
2459d9d415fSRadu Pirea (NXP OSS) 	 * which simplifies to
2469d9d415fSRadu Pirea (NXP OSS) 	 *
2479d9d415fSRadu Pirea (NXP OSS) 	 *    ppb = scaled_ppm * 125 / 2^13
2489d9d415fSRadu Pirea (NXP OSS) 	 */
2499d9d415fSRadu Pirea (NXP OSS) 	s64 ppb = 1 + ppm;
2509d9d415fSRadu Pirea (NXP OSS) 
2519d9d415fSRadu Pirea (NXP OSS) 	ppb *= 125;
2529d9d415fSRadu Pirea (NXP OSS) 	ppb >>= 13;
253adc2e56eSJakub Kicinski 	return (long)ppb;
2549d9d415fSRadu Pirea (NXP OSS) }
2559d9d415fSRadu Pirea (NXP OSS) 
2561060707eSJacob Keller /**
2571060707eSJacob Keller  * diff_by_scaled_ppm - Calculate difference using scaled ppm
2581060707eSJacob Keller  * @base: the base increment value to adjust
2591060707eSJacob Keller  * @scaled_ppm: scaled parts per million to adjust by
2601060707eSJacob Keller  * @diff: on return, the absolute value of calculated diff
2611060707eSJacob Keller  *
2621060707eSJacob Keller  * Calculate the difference to adjust the base increment using scaled parts
2631060707eSJacob Keller  * per million.
2641060707eSJacob Keller  *
2651060707eSJacob Keller  * Use mul_u64_u64_div_u64 to perform the difference calculation in avoid
2661060707eSJacob Keller  * possible overflow.
2671060707eSJacob Keller  *
2681060707eSJacob Keller  * Returns: true if scaled_ppm is negative, false otherwise
2691060707eSJacob Keller  */
diff_by_scaled_ppm(u64 base,long scaled_ppm,u64 * diff)2701060707eSJacob Keller static inline bool diff_by_scaled_ppm(u64 base, long scaled_ppm, u64 *diff)
2711060707eSJacob Keller {
2721060707eSJacob Keller 	bool negative = false;
2731060707eSJacob Keller 
2741060707eSJacob Keller 	if (scaled_ppm < 0) {
2751060707eSJacob Keller 		negative = true;
2761060707eSJacob Keller 		scaled_ppm = -scaled_ppm;
2771060707eSJacob Keller 	}
2781060707eSJacob Keller 
2791060707eSJacob Keller 	*diff = mul_u64_u64_div_u64(base, (u64)scaled_ppm, 1000000ULL << 16);
2801060707eSJacob Keller 
2811060707eSJacob Keller 	return negative;
2821060707eSJacob Keller }
2831060707eSJacob Keller 
2841060707eSJacob Keller /**
2851060707eSJacob Keller  * adjust_by_scaled_ppm - Adjust a base increment by scaled parts per million
2861060707eSJacob Keller  * @base: the base increment value to adjust
2871060707eSJacob Keller  * @scaled_ppm: scaled parts per million frequency adjustment
2881060707eSJacob Keller  *
2891060707eSJacob Keller  * Helper function which calculates a new increment value based on the
2901060707eSJacob Keller  * requested scaled parts per million adjustment.
2911060707eSJacob Keller  */
adjust_by_scaled_ppm(u64 base,long scaled_ppm)2921060707eSJacob Keller static inline u64 adjust_by_scaled_ppm(u64 base, long scaled_ppm)
2931060707eSJacob Keller {
2941060707eSJacob Keller 	u64 diff;
2951060707eSJacob Keller 
2961060707eSJacob Keller 	if (diff_by_scaled_ppm(base, scaled_ppm, &diff))
2971060707eSJacob Keller 		return base - diff;
2981060707eSJacob Keller 
2991060707eSJacob Keller 	return base + diff;
3001060707eSJacob Keller }
3011060707eSJacob Keller 
302e5f31552SArnd Bergmann #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
303d1cbfd77SNicolas Pitre 
304d1cbfd77SNicolas Pitre /**
305d1cbfd77SNicolas Pitre  * ptp_clock_register() - register a PTP hardware clock driver
306d1cbfd77SNicolas Pitre  *
307d1cbfd77SNicolas Pitre  * @info:   Structure describing the new clock.
308d1cbfd77SNicolas Pitre  * @parent: Pointer to the parent device of the new clock.
309d1cbfd77SNicolas Pitre  *
310*3f330db3SJakub Kicinski  * Returns: a valid pointer on success or PTR_ERR on failure.  If PHC
311d1cbfd77SNicolas Pitre  * support is missing at the configuration level, this function
312d1cbfd77SNicolas Pitre  * returns NULL, and drivers are expected to gracefully handle that
313d1cbfd77SNicolas Pitre  * case separately.
314d1cbfd77SNicolas Pitre  */
315d1cbfd77SNicolas Pitre 
316d1cbfd77SNicolas Pitre extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
317d1cbfd77SNicolas Pitre 					    struct device *parent);
318d1cbfd77SNicolas Pitre 
319d1cbfd77SNicolas Pitre /**
320d1cbfd77SNicolas Pitre  * ptp_clock_unregister() - unregister a PTP hardware clock driver
321d1cbfd77SNicolas Pitre  *
322d1cbfd77SNicolas Pitre  * @ptp:  The clock to remove from service.
323d1cbfd77SNicolas Pitre  */
324d1cbfd77SNicolas Pitre 
325d1cbfd77SNicolas Pitre extern int ptp_clock_unregister(struct ptp_clock *ptp);
326d1cbfd77SNicolas Pitre 
327d94ba80eSRichard Cochran /**
328d94ba80eSRichard Cochran  * ptp_clock_event() - notify the PTP layer about an event
329d94ba80eSRichard Cochran  *
330d94ba80eSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
331d94ba80eSRichard Cochran  * @event:  Message structure describing the event.
332d94ba80eSRichard Cochran  */
333d94ba80eSRichard Cochran 
334d94ba80eSRichard Cochran extern void ptp_clock_event(struct ptp_clock *ptp,
335d94ba80eSRichard Cochran 			    struct ptp_clock_event *event);
336d94ba80eSRichard Cochran 
337995a9090SRichard Cochran /**
338995a9090SRichard Cochran  * ptp_clock_index() - obtain the device index of a PTP clock
339995a9090SRichard Cochran  *
340995a9090SRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
341995a9090SRichard Cochran  */
342995a9090SRichard Cochran 
343995a9090SRichard Cochran extern int ptp_clock_index(struct ptp_clock *ptp);
344995a9090SRichard Cochran 
3456092315dSRichard Cochran /**
3466092315dSRichard Cochran  * ptp_find_pin() - obtain the pin index of a given auxiliary function
3476092315dSRichard Cochran  *
34862582a7eSRichard Cochran  * The caller must hold ptp_clock::pincfg_mux.  Drivers do not have
34962582a7eSRichard Cochran  * access to that mutex as ptp_clock is an opaque type.  However, the
35062582a7eSRichard Cochran  * core code acquires the mutex before invoking the driver's
35162582a7eSRichard Cochran  * ptp_clock_info::enable() callback, and so drivers may call this
35262582a7eSRichard Cochran  * function from that context.
35362582a7eSRichard Cochran  *
3546092315dSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
3556092315dSRichard Cochran  * @func:   One of the ptp_pin_function enumerated values.
3566092315dSRichard Cochran  * @chan:   The particular functional channel to find.
3576092315dSRichard Cochran  * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
3586092315dSRichard Cochran  *          or -1 if the auxiliary function cannot be found.
3596092315dSRichard Cochran  */
3606092315dSRichard Cochran 
3616092315dSRichard Cochran int ptp_find_pin(struct ptp_clock *ptp,
3626092315dSRichard Cochran 		 enum ptp_pin_function func, unsigned int chan);
3636092315dSRichard Cochran 
364d9535cb7SGrygorii Strashko /**
36562582a7eSRichard Cochran  * ptp_find_pin_unlocked() - wrapper for ptp_find_pin()
36662582a7eSRichard Cochran  *
36762582a7eSRichard Cochran  * This function acquires the ptp_clock::pincfg_mux mutex before
36862582a7eSRichard Cochran  * invoking ptp_find_pin().  Instead of using this function, drivers
36962582a7eSRichard Cochran  * should most likely call ptp_find_pin() directly from their
37062582a7eSRichard Cochran  * ptp_clock_info::enable() method.
37162582a7eSRichard Cochran  *
372b9a61b97SJacob Keller * @ptp:    The clock obtained from ptp_clock_register().
373b9a61b97SJacob Keller * @func:   One of the ptp_pin_function enumerated values.
374b9a61b97SJacob Keller * @chan:   The particular functional channel to find.
375b9a61b97SJacob Keller * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
376b9a61b97SJacob Keller *          or -1 if the auxiliary function cannot be found.
37762582a7eSRichard Cochran  */
37862582a7eSRichard Cochran 
37962582a7eSRichard Cochran int ptp_find_pin_unlocked(struct ptp_clock *ptp,
38062582a7eSRichard Cochran 			  enum ptp_pin_function func, unsigned int chan);
38162582a7eSRichard Cochran 
38262582a7eSRichard Cochran /**
383d9535cb7SGrygorii Strashko  * ptp_schedule_worker() - schedule ptp auxiliary work
384d9535cb7SGrygorii Strashko  *
385d9535cb7SGrygorii Strashko  * @ptp:    The clock obtained from ptp_clock_register().
386d9535cb7SGrygorii Strashko  * @delay:  number of jiffies to wait before queuing
387d9535cb7SGrygorii Strashko  *          See kthread_queue_delayed_work() for more info.
388d9535cb7SGrygorii Strashko  */
389d9535cb7SGrygorii Strashko 
390d9535cb7SGrygorii Strashko int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay);
391d9535cb7SGrygorii Strashko 
392544fed47SVladimir Oltean /**
393544fed47SVladimir Oltean  * ptp_cancel_worker_sync() - cancel ptp auxiliary clock
394544fed47SVladimir Oltean  *
395544fed47SVladimir Oltean  * @ptp:     The clock obtained from ptp_clock_register().
396544fed47SVladimir Oltean  */
397544fed47SVladimir Oltean void ptp_cancel_worker_sync(struct ptp_clock *ptp);
398544fed47SVladimir Oltean 
399e5f31552SArnd Bergmann #else
ptp_clock_register(struct ptp_clock_info * info,struct device * parent)400e5f31552SArnd Bergmann static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
401e5f31552SArnd Bergmann 						   struct device *parent)
402e5f31552SArnd Bergmann { return NULL; }
ptp_clock_unregister(struct ptp_clock * ptp)403e5f31552SArnd Bergmann static inline int ptp_clock_unregister(struct ptp_clock *ptp)
404e5f31552SArnd Bergmann { return 0; }
ptp_clock_event(struct ptp_clock * ptp,struct ptp_clock_event * event)405e5f31552SArnd Bergmann static inline void ptp_clock_event(struct ptp_clock *ptp,
406e5f31552SArnd Bergmann 				   struct ptp_clock_event *event)
407e5f31552SArnd Bergmann { }
ptp_clock_index(struct ptp_clock * ptp)408e5f31552SArnd Bergmann static inline int ptp_clock_index(struct ptp_clock *ptp)
409e5f31552SArnd Bergmann { return -1; }
ptp_find_pin(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)410e5f31552SArnd Bergmann static inline int ptp_find_pin(struct ptp_clock *ptp,
411e5f31552SArnd Bergmann 			       enum ptp_pin_function func, unsigned int chan)
412e5f31552SArnd Bergmann { return -1; }
ptp_find_pin_unlocked(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)41348cec73aSHoratiu Vultur static inline int ptp_find_pin_unlocked(struct ptp_clock *ptp,
41448cec73aSHoratiu Vultur 					enum ptp_pin_function func,
41548cec73aSHoratiu Vultur 					unsigned int chan)
41648cec73aSHoratiu Vultur { return -1; }
ptp_schedule_worker(struct ptp_clock * ptp,unsigned long delay)417e5f31552SArnd Bergmann static inline int ptp_schedule_worker(struct ptp_clock *ptp,
418e5f31552SArnd Bergmann 				      unsigned long delay)
419e5f31552SArnd Bergmann { return -EOPNOTSUPP; }
ptp_cancel_worker_sync(struct ptp_clock * ptp)420e5f31552SArnd Bergmann static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp)
421e5f31552SArnd Bergmann { }
422e5f31552SArnd Bergmann #endif
423e5f31552SArnd Bergmann 
424e5f31552SArnd Bergmann #if IS_BUILTIN(CONFIG_PTP_1588_CLOCK)
425e5f31552SArnd Bergmann /*
426e5f31552SArnd Bergmann  * These are called by the network core, and don't work if PTP is in
427e5f31552SArnd Bergmann  * a loadable module.
428e5f31552SArnd Bergmann  */
429e5f31552SArnd Bergmann 
430acb288e8SYangbo Lu /**
431acb288e8SYangbo Lu  * ptp_get_vclocks_index() - get all vclocks index on pclock, and
432acb288e8SYangbo Lu  *                           caller is responsible to free memory
433acb288e8SYangbo Lu  *                           of vclock_index
434acb288e8SYangbo Lu  *
435acb288e8SYangbo Lu  * @pclock_index: phc index of ptp pclock.
436acb288e8SYangbo Lu  * @vclock_index: pointer to pointer of vclock index.
437acb288e8SYangbo Lu  *
438acb288e8SYangbo Lu  * return number of vclocks.
439acb288e8SYangbo Lu  */
440acb288e8SYangbo Lu int ptp_get_vclocks_index(int pclock_index, int **vclock_index);
441acb288e8SYangbo Lu 
442895487a3SYangbo Lu /**
443895487a3SYangbo Lu  * ptp_convert_timestamp() - convert timestamp to a ptp vclock time
444895487a3SYangbo Lu  *
445d58809d8SGerhard Engleder  * @hwtstamp:     timestamp
446895487a3SYangbo Lu  * @vclock_index: phc index of ptp vclock.
447007747a9SMiroslav Lichvar  *
448*3f330db3SJakub Kicinski  * Returns: converted timestamp, or 0 on error.
449895487a3SYangbo Lu  */
450d58809d8SGerhard Engleder ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index);
451d1cbfd77SNicolas Pitre #else
ptp_get_vclocks_index(int pclock_index,int ** vclock_index)452acb288e8SYangbo Lu static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index)
453acb288e8SYangbo Lu { return 0; }
ptp_convert_timestamp(const ktime_t * hwtstamp,int vclock_index)454d58809d8SGerhard Engleder static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp,
455895487a3SYangbo Lu 					    int vclock_index)
456007747a9SMiroslav Lichvar { return 0; }
457d9535cb7SGrygorii Strashko 
458d1cbfd77SNicolas Pitre #endif
459d1cbfd77SNicolas Pitre 
ptp_read_system_prets(struct ptp_system_timestamp * sts)46036180087SMiroslav Lichvar static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts)
46136180087SMiroslav Lichvar {
462c259acabSMahesh Bandewar 	if (sts) {
463c259acabSMahesh Bandewar 		switch (sts->clockid) {
464c259acabSMahesh Bandewar 		case CLOCK_REALTIME:
46536180087SMiroslav Lichvar 			ktime_get_real_ts64(&sts->pre_ts);
466c259acabSMahesh Bandewar 			break;
467c259acabSMahesh Bandewar 		case CLOCK_MONOTONIC:
468c259acabSMahesh Bandewar 			ktime_get_ts64(&sts->pre_ts);
469c259acabSMahesh Bandewar 			break;
470c259acabSMahesh Bandewar 		case CLOCK_MONOTONIC_RAW:
471c259acabSMahesh Bandewar 			ktime_get_raw_ts64(&sts->pre_ts);
472c259acabSMahesh Bandewar 			break;
473c259acabSMahesh Bandewar 		default:
474c259acabSMahesh Bandewar 			break;
475c259acabSMahesh Bandewar 		}
476c259acabSMahesh Bandewar 	}
47736180087SMiroslav Lichvar }
47836180087SMiroslav Lichvar 
ptp_read_system_postts(struct ptp_system_timestamp * sts)47936180087SMiroslav Lichvar static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts)
48036180087SMiroslav Lichvar {
481c259acabSMahesh Bandewar 	if (sts) {
482c259acabSMahesh Bandewar 		switch (sts->clockid) {
483c259acabSMahesh Bandewar 		case CLOCK_REALTIME:
48436180087SMiroslav Lichvar 			ktime_get_real_ts64(&sts->post_ts);
485c259acabSMahesh Bandewar 			break;
486c259acabSMahesh Bandewar 		case CLOCK_MONOTONIC:
487c259acabSMahesh Bandewar 			ktime_get_ts64(&sts->post_ts);
488c259acabSMahesh Bandewar 			break;
489c259acabSMahesh Bandewar 		case CLOCK_MONOTONIC_RAW:
490c259acabSMahesh Bandewar 			ktime_get_raw_ts64(&sts->post_ts);
491c259acabSMahesh Bandewar 			break;
492c259acabSMahesh Bandewar 		default:
493c259acabSMahesh Bandewar 			break;
494c259acabSMahesh Bandewar 		}
495c259acabSMahesh Bandewar 	}
49636180087SMiroslav Lichvar }
49736180087SMiroslav Lichvar 
498d94ba80eSRichard Cochran #endif
499