1d94ba80eSRichard Cochran /*
2d94ba80eSRichard Cochran  * PTP 1588 clock support
3d94ba80eSRichard Cochran  *
4d94ba80eSRichard Cochran  * Copyright (C) 2010 OMICRON electronics GmbH
5d94ba80eSRichard Cochran  *
6d94ba80eSRichard Cochran  *  This program is free software; you can redistribute it and/or modify
7d94ba80eSRichard Cochran  *  it under the terms of the GNU General Public License as published by
8d94ba80eSRichard Cochran  *  the Free Software Foundation; either version 2 of the License, or
9d94ba80eSRichard Cochran  *  (at your option) any later version.
10d94ba80eSRichard Cochran  *
11d94ba80eSRichard Cochran  *  This program is distributed in the hope that it will be useful,
12d94ba80eSRichard Cochran  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13d94ba80eSRichard Cochran  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14d94ba80eSRichard Cochran  *  GNU General Public License for more details.
15d94ba80eSRichard Cochran  *
16d94ba80eSRichard Cochran  *  You should have received a copy of the GNU General Public License
17d94ba80eSRichard Cochran  *  along with this program; if not, write to the Free Software
18d94ba80eSRichard Cochran  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19d94ba80eSRichard Cochran  */
20d94ba80eSRichard Cochran 
21d94ba80eSRichard Cochran #ifndef _PTP_CLOCK_KERNEL_H_
22d94ba80eSRichard Cochran #define _PTP_CLOCK_KERNEL_H_
23d94ba80eSRichard Cochran 
241ef76158SRichard Cochran #include <linux/device.h>
25220a60a4SBen Hutchings #include <linux/pps_kernel.h>
26d94ba80eSRichard Cochran #include <linux/ptp_clock.h>
27d94ba80eSRichard Cochran 
28d94ba80eSRichard Cochran 
29d94ba80eSRichard Cochran struct ptp_clock_request {
30d94ba80eSRichard Cochran 	enum {
31d94ba80eSRichard Cochran 		PTP_CLK_REQ_EXTTS,
32d94ba80eSRichard Cochran 		PTP_CLK_REQ_PEROUT,
33d94ba80eSRichard Cochran 		PTP_CLK_REQ_PPS,
34d94ba80eSRichard Cochran 	} type;
35d94ba80eSRichard Cochran 	union {
36d94ba80eSRichard Cochran 		struct ptp_extts_request extts;
37d94ba80eSRichard Cochran 		struct ptp_perout_request perout;
38d94ba80eSRichard Cochran 	};
39d94ba80eSRichard Cochran };
40d94ba80eSRichard Cochran 
41719f1aa4SChristopher S. Hall struct system_device_crosststamp;
42d94ba80eSRichard Cochran /**
43d94ba80eSRichard Cochran  * struct ptp_clock_info - decribes a PTP hardware clock
44d94ba80eSRichard Cochran  *
45d94ba80eSRichard Cochran  * @owner:     The clock driver should set to THIS_MODULE.
46de465846SRichard Cochran  * @name:      A short "friendly name" to identify the clock and to
47de465846SRichard Cochran  *             help distinguish PHY based devices from MAC based ones.
48de465846SRichard Cochran  *             The string is not meant to be a unique id.
49d94ba80eSRichard Cochran  * @max_adj:   The maximum possible frequency adjustment, in parts per billon.
50d94ba80eSRichard Cochran  * @n_alarm:   The number of programmable alarms.
51d94ba80eSRichard Cochran  * @n_ext_ts:  The number of external time stamp channels.
52d94ba80eSRichard Cochran  * @n_per_out: The number of programmable periodic signals.
536092315dSRichard Cochran  * @n_pins:    The number of programmable pins.
54d94ba80eSRichard Cochran  * @pps:       Indicates whether the clock supports a PPS callback.
556092315dSRichard Cochran  * @pin_config: Array of length 'n_pins'. If the number of
566092315dSRichard Cochran  *              programmable pins is nonzero, then drivers must
576092315dSRichard Cochran  *              allocate and initialize this array.
58d94ba80eSRichard Cochran  *
59d94ba80eSRichard Cochran  * clock operations
60d94ba80eSRichard Cochran  *
61d8d26354SRichard Cochran  * @adjfine:  Adjusts the frequency of the hardware clock.
62d8d26354SRichard Cochran  *            parameter scaled_ppm: Desired frequency offset from
63d8d26354SRichard Cochran  *            nominal frequency in parts per million, but with a
64d8d26354SRichard Cochran  *            16 bit binary fractional field.
65d8d26354SRichard Cochran  *
66d94ba80eSRichard Cochran  * @adjfreq:  Adjusts the frequency of the hardware clock.
67d8d26354SRichard Cochran  *            This method is deprecated.  New drivers should implement
68d8d26354SRichard Cochran  *            the @adjfine method instead.
6987f4d7c1SJacob Keller  *            parameter delta: Desired frequency offset from nominal frequency
7087f4d7c1SJacob Keller  *            in parts per billion
71d94ba80eSRichard Cochran  *
72d94ba80eSRichard Cochran  * @adjtime:  Shifts the time of the hardware clock.
73d94ba80eSRichard Cochran  *            parameter delta: Desired change in nanoseconds.
74d94ba80eSRichard Cochran  *
7592f17194SRichard Cochran  * @gettime64:  Reads the current time from the hardware clock.
7692f17194SRichard Cochran  *              parameter ts: Holds the result.
7792f17194SRichard Cochran  *
78719f1aa4SChristopher S. Hall  * @getcrosststamp:  Reads the current time from the hardware clock and
79719f1aa4SChristopher S. Hall  *                   system clock simultaneously.
80719f1aa4SChristopher S. Hall  *                   parameter cts: Contains timestamp (device,system) pair,
81719f1aa4SChristopher S. Hall  *                   where system time is realtime and monotonic.
82719f1aa4SChristopher S. Hall  *
8392f17194SRichard Cochran  * @settime64:  Set the current time on the hardware clock.
84d94ba80eSRichard Cochran  *              parameter ts: Time value to set.
85d94ba80eSRichard Cochran  *
86d94ba80eSRichard Cochran  * @enable:   Request driver to enable or disable an ancillary feature.
87d94ba80eSRichard Cochran  *            parameter request: Desired resource to enable or disable.
88d94ba80eSRichard Cochran  *            parameter on: Caller passes one to enable or zero to disable.
89d94ba80eSRichard Cochran  *
906092315dSRichard Cochran  * @verify:   Confirm that a pin can perform a given function. The PTP
916092315dSRichard Cochran  *            Hardware Clock subsystem maintains the 'pin_config'
926092315dSRichard Cochran  *            array on behalf of the drivers, but the PHC subsystem
936092315dSRichard Cochran  *            assumes that every pin can perform every function. This
946092315dSRichard Cochran  *            hook gives drivers a way of telling the core about
956092315dSRichard Cochran  *            limitations on specific pins. This function must return
966092315dSRichard Cochran  *            zero if the function can be assigned to this pin, and
976092315dSRichard Cochran  *            nonzero otherwise.
986092315dSRichard Cochran  *            parameter pin: index of the pin in question.
996092315dSRichard Cochran  *            parameter func: the desired function to use.
1006092315dSRichard Cochran  *            parameter chan: the function channel index to use.
1016092315dSRichard Cochran  *
102*d9535cb7SGrygorii Strashko  * @do_work:  Request driver to perform auxiliary (periodic) operations
103*d9535cb7SGrygorii Strashko  *	      Driver should return delay of the next auxiliary work scheduling
104*d9535cb7SGrygorii Strashko  *	      time (>=0) or negative value in case further scheduling
105*d9535cb7SGrygorii Strashko  *	      is not required.
106*d9535cb7SGrygorii Strashko  *
107d94ba80eSRichard Cochran  * Drivers should embed their ptp_clock_info within a private
108d94ba80eSRichard Cochran  * structure, obtaining a reference to it using container_of().
109d94ba80eSRichard Cochran  *
110d94ba80eSRichard Cochran  * The callbacks must all return zero on success, non-zero otherwise.
111d94ba80eSRichard Cochran  */
112d94ba80eSRichard Cochran 
113d94ba80eSRichard Cochran struct ptp_clock_info {
114d94ba80eSRichard Cochran 	struct module *owner;
115d94ba80eSRichard Cochran 	char name[16];
116d94ba80eSRichard Cochran 	s32 max_adj;
117d94ba80eSRichard Cochran 	int n_alarm;
118d94ba80eSRichard Cochran 	int n_ext_ts;
119d94ba80eSRichard Cochran 	int n_per_out;
1206092315dSRichard Cochran 	int n_pins;
121d94ba80eSRichard Cochran 	int pps;
1226092315dSRichard Cochran 	struct ptp_pin_desc *pin_config;
123d8d26354SRichard Cochran 	int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm);
124d94ba80eSRichard Cochran 	int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta);
125d94ba80eSRichard Cochran 	int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
12692f17194SRichard Cochran 	int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
127719f1aa4SChristopher S. Hall 	int (*getcrosststamp)(struct ptp_clock_info *ptp,
128719f1aa4SChristopher S. Hall 			      struct system_device_crosststamp *cts);
12992f17194SRichard Cochran 	int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
130d94ba80eSRichard Cochran 	int (*enable)(struct ptp_clock_info *ptp,
131d94ba80eSRichard Cochran 		      struct ptp_clock_request *request, int on);
1326092315dSRichard Cochran 	int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
1336092315dSRichard Cochran 		      enum ptp_pin_function func, unsigned int chan);
134*d9535cb7SGrygorii Strashko 	long (*do_aux_work)(struct ptp_clock_info *ptp);
135d94ba80eSRichard Cochran };
136d94ba80eSRichard Cochran 
137d94ba80eSRichard Cochran struct ptp_clock;
138d94ba80eSRichard Cochran 
139d94ba80eSRichard Cochran enum ptp_clock_events {
140d94ba80eSRichard Cochran 	PTP_CLOCK_ALARM,
141d94ba80eSRichard Cochran 	PTP_CLOCK_EXTTS,
142d94ba80eSRichard Cochran 	PTP_CLOCK_PPS,
143220a60a4SBen Hutchings 	PTP_CLOCK_PPSUSR,
144d94ba80eSRichard Cochran };
145d94ba80eSRichard Cochran 
146d94ba80eSRichard Cochran /**
147d94ba80eSRichard Cochran  * struct ptp_clock_event - decribes a PTP hardware clock event
148d94ba80eSRichard Cochran  *
149d94ba80eSRichard Cochran  * @type:  One of the ptp_clock_events enumeration values.
150d94ba80eSRichard Cochran  * @index: Identifies the source of the event.
151220a60a4SBen Hutchings  * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
152220a60a4SBen Hutchings  * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
153d94ba80eSRichard Cochran  */
154d94ba80eSRichard Cochran 
155d94ba80eSRichard Cochran struct ptp_clock_event {
156d94ba80eSRichard Cochran 	int type;
157d94ba80eSRichard Cochran 	int index;
158220a60a4SBen Hutchings 	union {
159d94ba80eSRichard Cochran 		u64 timestamp;
160220a60a4SBen Hutchings 		struct pps_event_time pps_times;
161220a60a4SBen Hutchings 	};
162d94ba80eSRichard Cochran };
163d94ba80eSRichard Cochran 
164d1cbfd77SNicolas Pitre #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
165d1cbfd77SNicolas Pitre 
166d1cbfd77SNicolas Pitre /**
167d1cbfd77SNicolas Pitre  * ptp_clock_register() - register a PTP hardware clock driver
168d1cbfd77SNicolas Pitre  *
169d1cbfd77SNicolas Pitre  * @info:   Structure describing the new clock.
170d1cbfd77SNicolas Pitre  * @parent: Pointer to the parent device of the new clock.
171d1cbfd77SNicolas Pitre  *
172d1cbfd77SNicolas Pitre  * Returns a valid pointer on success or PTR_ERR on failure.  If PHC
173d1cbfd77SNicolas Pitre  * support is missing at the configuration level, this function
174d1cbfd77SNicolas Pitre  * returns NULL, and drivers are expected to gracefully handle that
175d1cbfd77SNicolas Pitre  * case separately.
176d1cbfd77SNicolas Pitre  */
177d1cbfd77SNicolas Pitre 
178d1cbfd77SNicolas Pitre extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
179d1cbfd77SNicolas Pitre 					    struct device *parent);
180d1cbfd77SNicolas Pitre 
181d1cbfd77SNicolas Pitre /**
182d1cbfd77SNicolas Pitre  * ptp_clock_unregister() - unregister a PTP hardware clock driver
183d1cbfd77SNicolas Pitre  *
184d1cbfd77SNicolas Pitre  * @ptp:  The clock to remove from service.
185d1cbfd77SNicolas Pitre  */
186d1cbfd77SNicolas Pitre 
187d1cbfd77SNicolas Pitre extern int ptp_clock_unregister(struct ptp_clock *ptp);
188d1cbfd77SNicolas Pitre 
189d94ba80eSRichard Cochran /**
190d94ba80eSRichard Cochran  * ptp_clock_event() - notify the PTP layer about an event
191d94ba80eSRichard Cochran  *
192d94ba80eSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
193d94ba80eSRichard Cochran  * @event:  Message structure describing the event.
194d94ba80eSRichard Cochran  */
195d94ba80eSRichard Cochran 
196d94ba80eSRichard Cochran extern void ptp_clock_event(struct ptp_clock *ptp,
197d94ba80eSRichard Cochran 			    struct ptp_clock_event *event);
198d94ba80eSRichard Cochran 
199995a9090SRichard Cochran /**
200995a9090SRichard Cochran  * ptp_clock_index() - obtain the device index of a PTP clock
201995a9090SRichard Cochran  *
202995a9090SRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
203995a9090SRichard Cochran  */
204995a9090SRichard Cochran 
205995a9090SRichard Cochran extern int ptp_clock_index(struct ptp_clock *ptp);
206995a9090SRichard Cochran 
2076092315dSRichard Cochran /**
2086092315dSRichard Cochran  * ptp_find_pin() - obtain the pin index of a given auxiliary function
2096092315dSRichard Cochran  *
2106092315dSRichard Cochran  * @ptp:    The clock obtained from ptp_clock_register().
2116092315dSRichard Cochran  * @func:   One of the ptp_pin_function enumerated values.
2126092315dSRichard Cochran  * @chan:   The particular functional channel to find.
2136092315dSRichard Cochran  * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
2146092315dSRichard Cochran  *          or -1 if the auxiliary function cannot be found.
2156092315dSRichard Cochran  */
2166092315dSRichard Cochran 
2176092315dSRichard Cochran int ptp_find_pin(struct ptp_clock *ptp,
2186092315dSRichard Cochran 		 enum ptp_pin_function func, unsigned int chan);
2196092315dSRichard Cochran 
220*d9535cb7SGrygorii Strashko /**
221*d9535cb7SGrygorii Strashko  * ptp_schedule_worker() - schedule ptp auxiliary work
222*d9535cb7SGrygorii Strashko  *
223*d9535cb7SGrygorii Strashko  * @ptp:    The clock obtained from ptp_clock_register().
224*d9535cb7SGrygorii Strashko  * @delay:  number of jiffies to wait before queuing
225*d9535cb7SGrygorii Strashko  *          See kthread_queue_delayed_work() for more info.
226*d9535cb7SGrygorii Strashko  */
227*d9535cb7SGrygorii Strashko 
228*d9535cb7SGrygorii Strashko int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay);
229*d9535cb7SGrygorii Strashko 
230d1cbfd77SNicolas Pitre #else
231d1cbfd77SNicolas Pitre static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
232d1cbfd77SNicolas Pitre 						   struct device *parent)
233d1cbfd77SNicolas Pitre { return NULL; }
234d1cbfd77SNicolas Pitre static inline int ptp_clock_unregister(struct ptp_clock *ptp)
235d1cbfd77SNicolas Pitre { return 0; }
236d1cbfd77SNicolas Pitre static inline void ptp_clock_event(struct ptp_clock *ptp,
237d1cbfd77SNicolas Pitre 				   struct ptp_clock_event *event)
238d1cbfd77SNicolas Pitre { }
239d1cbfd77SNicolas Pitre static inline int ptp_clock_index(struct ptp_clock *ptp)
240d1cbfd77SNicolas Pitre { return -1; }
241d1cbfd77SNicolas Pitre static inline int ptp_find_pin(struct ptp_clock *ptp,
242d1cbfd77SNicolas Pitre 			       enum ptp_pin_function func, unsigned int chan)
243d1cbfd77SNicolas Pitre { return -1; }
244*d9535cb7SGrygorii Strashko static inline int ptp_schedule_worker(struct ptp_clock *ptp,
245*d9535cb7SGrygorii Strashko 				      unsigned long delay)
246*d9535cb7SGrygorii Strashko { return -EOPNOTSUPP; }
247*d9535cb7SGrygorii Strashko 
248d1cbfd77SNicolas Pitre #endif
249d1cbfd77SNicolas Pitre 
250d94ba80eSRichard Cochran #endif
251