xref: /linux/include/linux/ptp_clock_kernel.h (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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
5036180087SMiroslav Lichvar  */
5136180087SMiroslav Lichvar struct ptp_system_timestamp {
5236180087SMiroslav Lichvar 	struct timespec64 pre_ts;
5336180087SMiroslav Lichvar 	struct timespec64 post_ts;
5436180087SMiroslav Lichvar };
5536180087SMiroslav Lichvar 
56d94ba80eSRichard Cochran /**
57184ecc9eSVincent Cheng  * struct ptp_clock_info - describes a PTP hardware clock
58d94ba80eSRichard Cochran  *
59d94ba80eSRichard Cochran  * @owner:     The clock driver should set to THIS_MODULE.
60de465846SRichard Cochran  * @name:      A short "friendly name" to identify the clock and to
61de465846SRichard Cochran  *             help distinguish PHY based devices from MAC based ones.
62de465846SRichard Cochran  *             The string is not meant to be a unique id.
63d94ba80eSRichard Cochran  * @max_adj:   The maximum possible frequency adjustment, in parts per billon.
64d94ba80eSRichard Cochran  * @n_alarm:   The number of programmable alarms.
65d94ba80eSRichard Cochran  * @n_ext_ts:  The number of external time stamp channels.
66d94ba80eSRichard Cochran  * @n_per_out: The number of programmable periodic signals.
676092315dSRichard Cochran  * @n_pins:    The number of programmable pins.
68d94ba80eSRichard Cochran  * @pps:       Indicates whether the clock supports a PPS callback.
696092315dSRichard Cochran  * @pin_config: Array of length 'n_pins'. If the number of
706092315dSRichard Cochran  *              programmable pins is nonzero, then drivers must
716092315dSRichard Cochran  *              allocate and initialize this array.
72d94ba80eSRichard Cochran  *
73d94ba80eSRichard Cochran  * clock operations
74d94ba80eSRichard Cochran  *
75d8d26354SRichard Cochran  * @adjfine:  Adjusts the frequency of the hardware clock.
76d8d26354SRichard Cochran  *            parameter scaled_ppm: Desired frequency offset from
77d8d26354SRichard Cochran  *            nominal frequency in parts per million, but with a
78d8d26354SRichard Cochran  *            16 bit binary fractional field.
79d8d26354SRichard Cochran  *
80a05d070aSRahul Rameshbabu  * @adjphase:  Indicates that the PHC should use an internal servo
81a05d070aSRahul Rameshbabu  *             algorithm to correct the provided phase offset.
82a05d070aSRahul Rameshbabu  *             parameter delta: PHC servo phase adjustment target
83a05d070aSRahul Rameshbabu  *                              in nanoseconds.
84184ecc9eSVincent Cheng  *
85c3b60ab7SRahul Rameshbabu  * @getmaxphase:  Advertises maximum offset that can be provided
86c3b60ab7SRahul Rameshbabu  *                to the hardware clock's phase control functionality
87c3b60ab7SRahul Rameshbabu  *                through adjphase.
88c3b60ab7SRahul Rameshbabu  *
89d94ba80eSRichard Cochran  * @adjtime:  Shifts the time of the hardware clock.
90d94ba80eSRichard Cochran  *            parameter delta: Desired change in nanoseconds.
91d94ba80eSRichard Cochran  *
9292f17194SRichard Cochran  * @gettime64:  Reads the current time from the hardware clock.
93916444dfSMiroslav Lichvar  *              This method is deprecated.  New drivers should implement
94916444dfSMiroslav Lichvar  *              the @gettimex64 method instead.
9592f17194SRichard Cochran  *              parameter ts: Holds the result.
9692f17194SRichard Cochran  *
9736180087SMiroslav Lichvar  * @gettimex64:  Reads the current time from the hardware clock and optionally
9836180087SMiroslav Lichvar  *               also the system clock.
9936180087SMiroslav Lichvar  *               parameter ts: Holds the PHC timestamp.
10036180087SMiroslav Lichvar  *               parameter sts: If not NULL, it holds a pair of timestamps from
10136180087SMiroslav Lichvar  *               the system clock. The first reading is made right before
10236180087SMiroslav Lichvar  *               reading the lowest bits of the PHC timestamp and the second
10336180087SMiroslav Lichvar  *               reading immediately follows that.
10436180087SMiroslav Lichvar  *
105719f1aa4SChristopher S. Hall  * @getcrosststamp:  Reads the current time from the hardware clock and
106719f1aa4SChristopher S. Hall  *                   system clock simultaneously.
107719f1aa4SChristopher S. Hall  *                   parameter cts: Contains timestamp (device,system) pair,
108719f1aa4SChristopher S. Hall  *                   where system time is realtime and monotonic.
109719f1aa4SChristopher S. Hall  *
11092f17194SRichard Cochran  * @settime64:  Set the current time on the hardware clock.
111d94ba80eSRichard Cochran  *              parameter ts: Time value to set.
112d94ba80eSRichard Cochran  *
11342704b26SGerhard Engleder  * @getcycles64:  Reads the current free running cycle counter from the hardware
11442704b26SGerhard Engleder  *                clock.
11542704b26SGerhard Engleder  *                If @getcycles64 and @getcyclesx64 are not supported, then
11642704b26SGerhard Engleder  *                @gettime64 or @gettimex64 will be used as default
11742704b26SGerhard Engleder  *                implementation.
11842704b26SGerhard Engleder  *                parameter ts: Holds the result.
11942704b26SGerhard Engleder  *
12042704b26SGerhard Engleder  * @getcyclesx64:  Reads the current free running cycle counter from the
12142704b26SGerhard Engleder  *                 hardware clock and optionally also the system clock.
12242704b26SGerhard Engleder  *                 If @getcycles64 and @getcyclesx64 are not supported, then
12342704b26SGerhard Engleder  *                 @gettimex64 will be used as default implementation if
12442704b26SGerhard Engleder  *                 available.
12542704b26SGerhard Engleder  *                 parameter ts: Holds the PHC timestamp.
12642704b26SGerhard Engleder  *                 parameter sts: If not NULL, it holds a pair of timestamps
12742704b26SGerhard Engleder  *                 from the system clock. The first reading is made right before
12842704b26SGerhard Engleder  *                 reading the lowest bits of the PHC timestamp and the second
12942704b26SGerhard Engleder  *                 reading immediately follows that.
13042704b26SGerhard Engleder  *
13142704b26SGerhard Engleder  * @getcrosscycles:  Reads the current free running cycle counter from the
13242704b26SGerhard Engleder  *                   hardware clock and system clock simultaneously.
13342704b26SGerhard Engleder  *                   If @getcycles64 and @getcyclesx64 are not supported, then
13442704b26SGerhard Engleder  *                   @getcrosststamp will be used as default implementation if
13542704b26SGerhard Engleder  *                   available.
13642704b26SGerhard Engleder  *                   parameter cts: Contains timestamp (device,system) pair,
13742704b26SGerhard Engleder  *                   where system time is realtime and monotonic.
13842704b26SGerhard Engleder  *
139d94ba80eSRichard Cochran  * @enable:   Request driver to enable or disable an ancillary feature.
140d94ba80eSRichard Cochran  *            parameter request: Desired resource to enable or disable.
141d94ba80eSRichard Cochran  *            parameter on: Caller passes one to enable or zero to disable.
142d94ba80eSRichard Cochran  *
1436092315dSRichard Cochran  * @verify:   Confirm that a pin can perform a given function. The PTP
1446092315dSRichard Cochran  *            Hardware Clock subsystem maintains the 'pin_config'
1456092315dSRichard Cochran  *            array on behalf of the drivers, but the PHC subsystem
1466092315dSRichard Cochran  *            assumes that every pin can perform every function. This
1476092315dSRichard Cochran  *            hook gives drivers a way of telling the core about
1486092315dSRichard Cochran  *            limitations on specific pins. This function must return
1496092315dSRichard Cochran  *            zero if the function can be assigned to this pin, and
1506092315dSRichard Cochran  *            nonzero otherwise.
1516092315dSRichard Cochran  *            parameter pin: index of the pin in question.
1526092315dSRichard Cochran  *            parameter func: the desired function to use.
1536092315dSRichard Cochran  *            parameter chan: the function channel index to use.
1546092315dSRichard Cochran  *
1552c864c78SJacob Keller  * @do_aux_work:  Request driver to perform auxiliary (periodic) operations
1562c864c78SJacob Keller  *                Driver should return delay of the next auxiliary work
1572c864c78SJacob Keller  *                scheduling time (>=0) or negative value in case further
1582c864c78SJacob Keller  *                scheduling is not required.
159d9535cb7SGrygorii Strashko  *
160d94ba80eSRichard Cochran  * Drivers should embed their ptp_clock_info within a private
161d94ba80eSRichard Cochran  * structure, obtaining a reference to it using container_of().
162d94ba80eSRichard Cochran  *
163d94ba80eSRichard Cochran  * The callbacks must all return zero on success, non-zero otherwise.
164d94ba80eSRichard Cochran  */
165d94ba80eSRichard Cochran 
166d94ba80eSRichard Cochran struct ptp_clock_info {
167d94ba80eSRichard Cochran 	struct module *owner;
1685d43f951SYangbo Lu 	char name[PTP_CLOCK_NAME_LEN];
169d94ba80eSRichard Cochran 	s32 max_adj;
170d94ba80eSRichard Cochran 	int n_alarm;
171d94ba80eSRichard Cochran 	int n_ext_ts;
172d94ba80eSRichard Cochran 	int n_per_out;
1736092315dSRichard Cochran 	int n_pins;
174d94ba80eSRichard Cochran 	int pps;
1756092315dSRichard Cochran 	struct ptp_pin_desc *pin_config;
176d8d26354SRichard Cochran 	int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm);
177184ecc9eSVincent Cheng 	int (*adjphase)(struct ptp_clock_info *ptp, s32 phase);
178c3b60ab7SRahul Rameshbabu 	s32 (*getmaxphase)(struct ptp_clock_info *ptp);
179d94ba80eSRichard Cochran 	int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
18092f17194SRichard Cochran 	int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
18136180087SMiroslav Lichvar 	int (*gettimex64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
18236180087SMiroslav Lichvar 			  struct ptp_system_timestamp *sts);
183719f1aa4SChristopher S. Hall 	int (*getcrosststamp)(struct ptp_clock_info *ptp,
184719f1aa4SChristopher S. Hall 			      struct system_device_crosststamp *cts);
18592f17194SRichard Cochran 	int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
18642704b26SGerhard Engleder 	int (*getcycles64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
18742704b26SGerhard Engleder 	int (*getcyclesx64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
18842704b26SGerhard Engleder 			    struct ptp_system_timestamp *sts);
18942704b26SGerhard Engleder 	int (*getcrosscycles)(struct ptp_clock_info *ptp,
19042704b26SGerhard Engleder 			      struct system_device_crosststamp *cts);
191d94ba80eSRichard Cochran 	int (*enable)(struct ptp_clock_info *ptp,
192d94ba80eSRichard Cochran 		      struct ptp_clock_request *request, int on);
1936092315dSRichard Cochran 	int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
1946092315dSRichard Cochran 		      enum ptp_pin_function func, unsigned int chan);
195d9535cb7SGrygorii Strashko 	long (*do_aux_work)(struct ptp_clock_info *ptp);
196d94ba80eSRichard Cochran };
197d94ba80eSRichard Cochran 
198d94ba80eSRichard Cochran struct ptp_clock;
199d94ba80eSRichard Cochran 
200d94ba80eSRichard Cochran enum ptp_clock_events {
201d94ba80eSRichard Cochran 	PTP_CLOCK_ALARM,
202d94ba80eSRichard Cochran 	PTP_CLOCK_EXTTS,
203*ea1cc3eeSMin Li 	PTP_CLOCK_EXTOFF,
204d94ba80eSRichard Cochran 	PTP_CLOCK_PPS,
205220a60a4SBen Hutchings 	PTP_CLOCK_PPSUSR,
206d94ba80eSRichard Cochran };
207d94ba80eSRichard Cochran 
208d94ba80eSRichard Cochran /**
209d94ba80eSRichard Cochran  * struct ptp_clock_event - decribes a PTP hardware clock event
210d94ba80eSRichard Cochran  *
211d94ba80eSRichard Cochran  * @type:  One of the ptp_clock_events enumeration values.
212d94ba80eSRichard Cochran  * @index: Identifies the source of the event.
213220a60a4SBen Hutchings  * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
214*ea1cc3eeSMin Li  * @offset:    When the event occurred (%PTP_CLOCK_EXTOFF only).
215220a60a4SBen Hutchings  * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
216d94ba80eSRichard Cochran  */
217d94ba80eSRichard Cochran 
218d94ba80eSRichard Cochran struct ptp_clock_event {
219d94ba80eSRichard Cochran 	int type;
220d94ba80eSRichard Cochran 	int index;
221220a60a4SBen Hutchings 	union {
222d94ba80eSRichard Cochran 		u64 timestamp;
223*ea1cc3eeSMin Li 		s64 offset;
224220a60a4SBen Hutchings 		struct pps_event_time pps_times;
225220a60a4SBen Hutchings 	};
226d94ba80eSRichard Cochran };
227d94ba80eSRichard Cochran 
2289d9d415fSRadu Pirea (NXP OSS) /**
2299d9d415fSRadu Pirea (NXP OSS)  * scaled_ppm_to_ppb() - convert scaled ppm to ppb
2309d9d415fSRadu Pirea (NXP OSS)  *
2319d9d415fSRadu Pirea (NXP OSS)  * @ppm:    Parts per million, but with a 16 bit binary fractional field
2329d9d415fSRadu Pirea (NXP OSS)  */
scaled_ppm_to_ppb(long ppm)233adc2e56eSJakub Kicinski static inline long scaled_ppm_to_ppb(long ppm)
2349d9d415fSRadu Pirea (NXP OSS) {
2359d9d415fSRadu Pirea (NXP OSS) 	/*
2369d9d415fSRadu Pirea (NXP OSS) 	 * The 'freq' field in the 'struct timex' is in parts per
2379d9d415fSRadu Pirea (NXP OSS) 	 * million, but with a 16 bit binary fractional field.
2389d9d415fSRadu Pirea (NXP OSS) 	 *
2399d9d415fSRadu Pirea (NXP OSS) 	 * We want to calculate
2409d9d415fSRadu Pirea (NXP OSS) 	 *
2419d9d415fSRadu Pirea (NXP OSS) 	 *    ppb = scaled_ppm * 1000 / 2^16
2429d9d415fSRadu Pirea (NXP OSS) 	 *
2439d9d415fSRadu Pirea (NXP OSS) 	 * which simplifies to
2449d9d415fSRadu Pirea (NXP OSS) 	 *
2459d9d415fSRadu Pirea (NXP OSS) 	 *    ppb = scaled_ppm * 125 / 2^13
2469d9d415fSRadu Pirea (NXP OSS) 	 */
2479d9d415fSRadu Pirea (NXP OSS) 	s64 ppb = 1 + ppm;
2489d9d415fSRadu Pirea (NXP OSS) 
2499d9d415fSRadu Pirea (NXP OSS) 	ppb *= 125;
2509d9d415fSRadu Pirea (NXP OSS) 	ppb >>= 13;
251adc2e56eSJakub Kicinski 	return (long)ppb;
2529d9d415fSRadu Pirea (NXP OSS) }
2539d9d415fSRadu Pirea (NXP OSS) 
2541060707eSJacob Keller /**
2551060707eSJacob Keller  * diff_by_scaled_ppm - Calculate difference using scaled ppm
2561060707eSJacob Keller  * @base: the base increment value to adjust
2571060707eSJacob Keller  * @scaled_ppm: scaled parts per million to adjust by
2581060707eSJacob Keller  * @diff: on return, the absolute value of calculated diff
2591060707eSJacob Keller  *
2601060707eSJacob Keller  * Calculate the difference to adjust the base increment using scaled parts
2611060707eSJacob Keller  * per million.
2621060707eSJacob Keller  *
2631060707eSJacob Keller  * Use mul_u64_u64_div_u64 to perform the difference calculation in avoid
2641060707eSJacob Keller  * possible overflow.
2651060707eSJacob Keller  *
2661060707eSJacob Keller  * Returns: true if scaled_ppm is negative, false otherwise
2671060707eSJacob Keller  */
diff_by_scaled_ppm(u64 base,long scaled_ppm,u64 * diff)2681060707eSJacob Keller static inline bool diff_by_scaled_ppm(u64 base, long scaled_ppm, u64 *diff)
2691060707eSJacob Keller {
2701060707eSJacob Keller 	bool negative = false;
2711060707eSJacob Keller 
2721060707eSJacob Keller 	if (scaled_ppm < 0) {
2731060707eSJacob Keller 		negative = true;
2741060707eSJacob Keller 		scaled_ppm = -scaled_ppm;
2751060707eSJacob Keller 	}
2761060707eSJacob Keller 
2771060707eSJacob Keller 	*diff = mul_u64_u64_div_u64(base, (u64)scaled_ppm, 1000000ULL << 16);
2781060707eSJacob Keller 
2791060707eSJacob Keller 	return negative;
2801060707eSJacob Keller }
2811060707eSJacob Keller 
2821060707eSJacob Keller /**
2831060707eSJacob Keller  * adjust_by_scaled_ppm - Adjust a base increment by scaled parts per million
2841060707eSJacob Keller  * @base: the base increment value to adjust
2851060707eSJacob Keller  * @scaled_ppm: scaled parts per million frequency adjustment
2861060707eSJacob Keller  *
2871060707eSJacob Keller  * Helper function which calculates a new increment value based on the
2881060707eSJacob Keller  * requested scaled parts per million adjustment.
2891060707eSJacob Keller  */
adjust_by_scaled_ppm(u64 base,long scaled_ppm)2901060707eSJacob Keller static inline u64 adjust_by_scaled_ppm(u64 base, long scaled_ppm)
2911060707eSJacob Keller {
2921060707eSJacob Keller 	u64 diff;
2931060707eSJacob Keller 
2941060707eSJacob Keller 	if (diff_by_scaled_ppm(base, scaled_ppm, &diff))
2951060707eSJacob Keller 		return base - diff;
2961060707eSJacob Keller 
2971060707eSJacob Keller 	return base + diff;
2981060707eSJacob Keller }
2991060707eSJacob Keller 
300e5f31552SArnd Bergmann #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
301d1cbfd77SNicolas Pitre 
302d1cbfd77SNicolas Pitre /**
303d1cbfd77SNicolas Pitre  * ptp_clock_register() - register a PTP hardware clock driver
304d1cbfd77SNicolas Pitre  *
305d1cbfd77SNicolas Pitre  * @info:   Structure describing the new clock.
306d1cbfd77SNicolas Pitre  * @parent: Pointer to the parent device of the new clock.
307d1cbfd77SNicolas Pitre  *
308d1cbfd77SNicolas Pitre  * Returns a valid pointer on success or PTR_ERR on failure.  If PHC
309d1cbfd77SNicolas Pitre  * support is missing at the configuration level, this function
310d1cbfd77SNicolas Pitre  * returns NULL, and drivers are expected to gracefully handle that
311d1cbfd77SNicolas Pitre  * case separately.
312d1cbfd77SNicolas Pitre  */
313d1cbfd77SNicolas Pitre 
314d1cbfd77SNicolas Pitre extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
315d1cbfd77SNicolas Pitre 					    struct device *parent);
316d1cbfd77SNicolas Pitre 
317d1cbfd77SNicolas Pitre /**
318d1cbfd77SNicolas Pitre  * ptp_clock_unregister() - unregister a PTP hardware clock driver
319d1cbfd77SNicolas Pitre  *
320d1cbfd77SNicolas Pitre  * @ptp:  The clock to remove from service.
321d1cbfd77SNicolas Pitre  */
322d1cbfd77SNicolas Pitre 
323d1cbfd77SNicolas Pitre extern int ptp_clock_unregister(struct ptp_clock *ptp);
324d1cbfd77SNicolas Pitre 
325d94ba80eSRichard Cochran /**
326d94ba80eSRichard Cochran  * ptp_clock_event() - notify the PTP layer about an event
327d94ba80eSRichard Cochran  *
328d94ba80eSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
329d94ba80eSRichard Cochran  * @event:  Message structure describing the event.
330d94ba80eSRichard Cochran  */
331d94ba80eSRichard Cochran 
332d94ba80eSRichard Cochran extern void ptp_clock_event(struct ptp_clock *ptp,
333d94ba80eSRichard Cochran 			    struct ptp_clock_event *event);
334d94ba80eSRichard Cochran 
335995a9090SRichard Cochran /**
336995a9090SRichard Cochran  * ptp_clock_index() - obtain the device index of a PTP clock
337995a9090SRichard Cochran  *
338995a9090SRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
339995a9090SRichard Cochran  */
340995a9090SRichard Cochran 
341995a9090SRichard Cochran extern int ptp_clock_index(struct ptp_clock *ptp);
342995a9090SRichard Cochran 
3436092315dSRichard Cochran /**
3446092315dSRichard Cochran  * ptp_find_pin() - obtain the pin index of a given auxiliary function
3456092315dSRichard Cochran  *
34662582a7eSRichard Cochran  * The caller must hold ptp_clock::pincfg_mux.  Drivers do not have
34762582a7eSRichard Cochran  * access to that mutex as ptp_clock is an opaque type.  However, the
34862582a7eSRichard Cochran  * core code acquires the mutex before invoking the driver's
34962582a7eSRichard Cochran  * ptp_clock_info::enable() callback, and so drivers may call this
35062582a7eSRichard Cochran  * function from that context.
35162582a7eSRichard Cochran  *
3526092315dSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
3536092315dSRichard Cochran  * @func:   One of the ptp_pin_function enumerated values.
3546092315dSRichard Cochran  * @chan:   The particular functional channel to find.
3556092315dSRichard Cochran  * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
3566092315dSRichard Cochran  *          or -1 if the auxiliary function cannot be found.
3576092315dSRichard Cochran  */
3586092315dSRichard Cochran 
3596092315dSRichard Cochran int ptp_find_pin(struct ptp_clock *ptp,
3606092315dSRichard Cochran 		 enum ptp_pin_function func, unsigned int chan);
3616092315dSRichard Cochran 
362d9535cb7SGrygorii Strashko /**
36362582a7eSRichard Cochran  * ptp_find_pin_unlocked() - wrapper for ptp_find_pin()
36462582a7eSRichard Cochran  *
36562582a7eSRichard Cochran  * This function acquires the ptp_clock::pincfg_mux mutex before
36662582a7eSRichard Cochran  * invoking ptp_find_pin().  Instead of using this function, drivers
36762582a7eSRichard Cochran  * should most likely call ptp_find_pin() directly from their
36862582a7eSRichard Cochran  * ptp_clock_info::enable() method.
36962582a7eSRichard Cochran  *
370b9a61b97SJacob Keller * @ptp:    The clock obtained from ptp_clock_register().
371b9a61b97SJacob Keller * @func:   One of the ptp_pin_function enumerated values.
372b9a61b97SJacob Keller * @chan:   The particular functional channel to find.
373b9a61b97SJacob Keller * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
374b9a61b97SJacob Keller *          or -1 if the auxiliary function cannot be found.
37562582a7eSRichard Cochran  */
37662582a7eSRichard Cochran 
37762582a7eSRichard Cochran int ptp_find_pin_unlocked(struct ptp_clock *ptp,
37862582a7eSRichard Cochran 			  enum ptp_pin_function func, unsigned int chan);
37962582a7eSRichard Cochran 
38062582a7eSRichard Cochran /**
381d9535cb7SGrygorii Strashko  * ptp_schedule_worker() - schedule ptp auxiliary work
382d9535cb7SGrygorii Strashko  *
383d9535cb7SGrygorii Strashko  * @ptp:    The clock obtained from ptp_clock_register().
384d9535cb7SGrygorii Strashko  * @delay:  number of jiffies to wait before queuing
385d9535cb7SGrygorii Strashko  *          See kthread_queue_delayed_work() for more info.
386d9535cb7SGrygorii Strashko  */
387d9535cb7SGrygorii Strashko 
388d9535cb7SGrygorii Strashko int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay);
389d9535cb7SGrygorii Strashko 
390544fed47SVladimir Oltean /**
391544fed47SVladimir Oltean  * ptp_cancel_worker_sync() - cancel ptp auxiliary clock
392544fed47SVladimir Oltean  *
393544fed47SVladimir Oltean  * @ptp:     The clock obtained from ptp_clock_register().
394544fed47SVladimir Oltean  */
395544fed47SVladimir Oltean void ptp_cancel_worker_sync(struct ptp_clock *ptp);
396544fed47SVladimir Oltean 
397e5f31552SArnd Bergmann #else
ptp_clock_register(struct ptp_clock_info * info,struct device * parent)398e5f31552SArnd Bergmann static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
399e5f31552SArnd Bergmann 						   struct device *parent)
400e5f31552SArnd Bergmann { return NULL; }
ptp_clock_unregister(struct ptp_clock * ptp)401e5f31552SArnd Bergmann static inline int ptp_clock_unregister(struct ptp_clock *ptp)
402e5f31552SArnd Bergmann { return 0; }
ptp_clock_event(struct ptp_clock * ptp,struct ptp_clock_event * event)403e5f31552SArnd Bergmann static inline void ptp_clock_event(struct ptp_clock *ptp,
404e5f31552SArnd Bergmann 				   struct ptp_clock_event *event)
405e5f31552SArnd Bergmann { }
ptp_clock_index(struct ptp_clock * ptp)406e5f31552SArnd Bergmann static inline int ptp_clock_index(struct ptp_clock *ptp)
407e5f31552SArnd Bergmann { return -1; }
ptp_find_pin(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)408e5f31552SArnd Bergmann static inline int ptp_find_pin(struct ptp_clock *ptp,
409e5f31552SArnd Bergmann 			       enum ptp_pin_function func, unsigned int chan)
410e5f31552SArnd Bergmann { return -1; }
ptp_find_pin_unlocked(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)41148cec73aSHoratiu Vultur static inline int ptp_find_pin_unlocked(struct ptp_clock *ptp,
41248cec73aSHoratiu Vultur 					enum ptp_pin_function func,
41348cec73aSHoratiu Vultur 					unsigned int chan)
41448cec73aSHoratiu Vultur { return -1; }
ptp_schedule_worker(struct ptp_clock * ptp,unsigned long delay)415e5f31552SArnd Bergmann static inline int ptp_schedule_worker(struct ptp_clock *ptp,
416e5f31552SArnd Bergmann 				      unsigned long delay)
417e5f31552SArnd Bergmann { return -EOPNOTSUPP; }
ptp_cancel_worker_sync(struct ptp_clock * ptp)418e5f31552SArnd Bergmann static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp)
419e5f31552SArnd Bergmann { }
420e5f31552SArnd Bergmann #endif
421e5f31552SArnd Bergmann 
422e5f31552SArnd Bergmann #if IS_BUILTIN(CONFIG_PTP_1588_CLOCK)
423e5f31552SArnd Bergmann /*
424e5f31552SArnd Bergmann  * These are called by the network core, and don't work if PTP is in
425e5f31552SArnd Bergmann  * a loadable module.
426e5f31552SArnd Bergmann  */
427e5f31552SArnd Bergmann 
428acb288e8SYangbo Lu /**
429acb288e8SYangbo Lu  * ptp_get_vclocks_index() - get all vclocks index on pclock, and
430acb288e8SYangbo Lu  *                           caller is responsible to free memory
431acb288e8SYangbo Lu  *                           of vclock_index
432acb288e8SYangbo Lu  *
433acb288e8SYangbo Lu  * @pclock_index: phc index of ptp pclock.
434acb288e8SYangbo Lu  * @vclock_index: pointer to pointer of vclock index.
435acb288e8SYangbo Lu  *
436acb288e8SYangbo Lu  * return number of vclocks.
437acb288e8SYangbo Lu  */
438acb288e8SYangbo Lu int ptp_get_vclocks_index(int pclock_index, int **vclock_index);
439acb288e8SYangbo Lu 
440895487a3SYangbo Lu /**
441895487a3SYangbo Lu  * ptp_convert_timestamp() - convert timestamp to a ptp vclock time
442895487a3SYangbo Lu  *
443d58809d8SGerhard Engleder  * @hwtstamp:     timestamp
444895487a3SYangbo Lu  * @vclock_index: phc index of ptp vclock.
445007747a9SMiroslav Lichvar  *
446007747a9SMiroslav Lichvar  * Returns converted timestamp, or 0 on error.
447895487a3SYangbo Lu  */
448d58809d8SGerhard Engleder ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index);
449d1cbfd77SNicolas Pitre #else
ptp_get_vclocks_index(int pclock_index,int ** vclock_index)450acb288e8SYangbo Lu static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index)
451acb288e8SYangbo Lu { return 0; }
ptp_convert_timestamp(const ktime_t * hwtstamp,int vclock_index)452d58809d8SGerhard Engleder static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp,
453895487a3SYangbo Lu 					    int vclock_index)
454007747a9SMiroslav Lichvar { return 0; }
455d9535cb7SGrygorii Strashko 
456d1cbfd77SNicolas Pitre #endif
457d1cbfd77SNicolas Pitre 
ptp_read_system_prets(struct ptp_system_timestamp * sts)45836180087SMiroslav Lichvar static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts)
45936180087SMiroslav Lichvar {
46036180087SMiroslav Lichvar 	if (sts)
46136180087SMiroslav Lichvar 		ktime_get_real_ts64(&sts->pre_ts);
46236180087SMiroslav Lichvar }
46336180087SMiroslav Lichvar 
ptp_read_system_postts(struct ptp_system_timestamp * sts)46436180087SMiroslav Lichvar static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts)
46536180087SMiroslav Lichvar {
46636180087SMiroslav Lichvar 	if (sts)
46736180087SMiroslav Lichvar 		ktime_get_real_ts64(&sts->post_ts);
46836180087SMiroslav Lichvar }
46936180087SMiroslav Lichvar 
470d94ba80eSRichard Cochran #endif
471