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> 14d94ba80eSRichard Cochran 15d04a53b1SAhmad Fatoum /** 16d04a53b1SAhmad Fatoum * struct ptp_clock_request - request PTP clock event 17d04a53b1SAhmad Fatoum * 18d04a53b1SAhmad Fatoum * @type: The type of the request. 19d04a53b1SAhmad Fatoum * EXTTS: Configure external trigger timestamping 20d04a53b1SAhmad Fatoum * PEROUT: Configure periodic output signal (e.g. PPS) 21d04a53b1SAhmad Fatoum * PPS: trigger internal PPS event for input 22d04a53b1SAhmad Fatoum * into kernel PPS subsystem 23d04a53b1SAhmad Fatoum * @extts: describes configuration for external trigger timestamping. 24d04a53b1SAhmad Fatoum * This is only valid when event == PTP_CLK_REQ_EXTTS. 25d04a53b1SAhmad Fatoum * @perout: describes configuration for periodic output. 26d04a53b1SAhmad Fatoum * This is only valid when event == PTP_CLK_REQ_PEROUT. 27d04a53b1SAhmad Fatoum */ 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; 4236180087SMiroslav Lichvar 4336180087SMiroslav Lichvar /** 4436180087SMiroslav Lichvar * struct ptp_system_timestamp - system time corresponding to a PHC timestamp 4536180087SMiroslav Lichvar */ 4636180087SMiroslav Lichvar struct ptp_system_timestamp { 4736180087SMiroslav Lichvar struct timespec64 pre_ts; 4836180087SMiroslav Lichvar struct timespec64 post_ts; 4936180087SMiroslav Lichvar }; 5036180087SMiroslav Lichvar 51d94ba80eSRichard Cochran /** 52184ecc9eSVincent Cheng * struct ptp_clock_info - describes a PTP hardware clock 53d94ba80eSRichard Cochran * 54d94ba80eSRichard Cochran * @owner: The clock driver should set to THIS_MODULE. 55de465846SRichard Cochran * @name: A short "friendly name" to identify the clock and to 56de465846SRichard Cochran * help distinguish PHY based devices from MAC based ones. 57de465846SRichard Cochran * The string is not meant to be a unique id. 58d94ba80eSRichard Cochran * @max_adj: The maximum possible frequency adjustment, in parts per billon. 59d94ba80eSRichard Cochran * @n_alarm: The number of programmable alarms. 60d94ba80eSRichard Cochran * @n_ext_ts: The number of external time stamp channels. 61d94ba80eSRichard Cochran * @n_per_out: The number of programmable periodic signals. 626092315dSRichard Cochran * @n_pins: The number of programmable pins. 63d94ba80eSRichard Cochran * @pps: Indicates whether the clock supports a PPS callback. 646092315dSRichard Cochran * @pin_config: Array of length 'n_pins'. If the number of 656092315dSRichard Cochran * programmable pins is nonzero, then drivers must 666092315dSRichard Cochran * allocate and initialize this array. 67d94ba80eSRichard Cochran * 68d94ba80eSRichard Cochran * clock operations 69d94ba80eSRichard Cochran * 70d8d26354SRichard Cochran * @adjfine: Adjusts the frequency of the hardware clock. 71d8d26354SRichard Cochran * parameter scaled_ppm: Desired frequency offset from 72d8d26354SRichard Cochran * nominal frequency in parts per million, but with a 73d8d26354SRichard Cochran * 16 bit binary fractional field. 74d8d26354SRichard Cochran * 75d94ba80eSRichard Cochran * @adjfreq: Adjusts the frequency of the hardware clock. 76d8d26354SRichard Cochran * This method is deprecated. New drivers should implement 77d8d26354SRichard Cochran * the @adjfine method instead. 7887f4d7c1SJacob Keller * parameter delta: Desired frequency offset from nominal frequency 7987f4d7c1SJacob Keller * in parts per billion 80d94ba80eSRichard Cochran * 81184ecc9eSVincent Cheng * @adjphase: Adjusts the phase offset of the hardware clock. 82184ecc9eSVincent Cheng * parameter delta: Desired change in nanoseconds. 83184ecc9eSVincent Cheng * 84d94ba80eSRichard Cochran * @adjtime: Shifts the time of the hardware clock. 85d94ba80eSRichard Cochran * parameter delta: Desired change in nanoseconds. 86d94ba80eSRichard Cochran * 8792f17194SRichard Cochran * @gettime64: Reads the current time from the hardware clock. 88916444dfSMiroslav Lichvar * This method is deprecated. New drivers should implement 89916444dfSMiroslav Lichvar * the @gettimex64 method instead. 9092f17194SRichard Cochran * parameter ts: Holds the result. 9192f17194SRichard Cochran * 9236180087SMiroslav Lichvar * @gettimex64: Reads the current time from the hardware clock and optionally 9336180087SMiroslav Lichvar * also the system clock. 9436180087SMiroslav Lichvar * parameter ts: Holds the PHC timestamp. 9536180087SMiroslav Lichvar * parameter sts: If not NULL, it holds a pair of timestamps from 9636180087SMiroslav Lichvar * the system clock. The first reading is made right before 9736180087SMiroslav Lichvar * reading the lowest bits of the PHC timestamp and the second 9836180087SMiroslav Lichvar * reading immediately follows that. 9936180087SMiroslav Lichvar * 100719f1aa4SChristopher S. Hall * @getcrosststamp: Reads the current time from the hardware clock and 101719f1aa4SChristopher S. Hall * system clock simultaneously. 102719f1aa4SChristopher S. Hall * parameter cts: Contains timestamp (device,system) pair, 103719f1aa4SChristopher S. Hall * where system time is realtime and monotonic. 104719f1aa4SChristopher S. Hall * 10592f17194SRichard Cochran * @settime64: Set the current time on the hardware clock. 106d94ba80eSRichard Cochran * parameter ts: Time value to set. 107d94ba80eSRichard Cochran * 108d94ba80eSRichard Cochran * @enable: Request driver to enable or disable an ancillary feature. 109d94ba80eSRichard Cochran * parameter request: Desired resource to enable or disable. 110d94ba80eSRichard Cochran * parameter on: Caller passes one to enable or zero to disable. 111d94ba80eSRichard Cochran * 1126092315dSRichard Cochran * @verify: Confirm that a pin can perform a given function. The PTP 1136092315dSRichard Cochran * Hardware Clock subsystem maintains the 'pin_config' 1146092315dSRichard Cochran * array on behalf of the drivers, but the PHC subsystem 1156092315dSRichard Cochran * assumes that every pin can perform every function. This 1166092315dSRichard Cochran * hook gives drivers a way of telling the core about 1176092315dSRichard Cochran * limitations on specific pins. This function must return 1186092315dSRichard Cochran * zero if the function can be assigned to this pin, and 1196092315dSRichard Cochran * nonzero otherwise. 1206092315dSRichard Cochran * parameter pin: index of the pin in question. 1216092315dSRichard Cochran * parameter func: the desired function to use. 1226092315dSRichard Cochran * parameter chan: the function channel index to use. 1236092315dSRichard Cochran * 1242c864c78SJacob Keller * @do_aux_work: Request driver to perform auxiliary (periodic) operations 1252c864c78SJacob Keller * Driver should return delay of the next auxiliary work 1262c864c78SJacob Keller * scheduling time (>=0) or negative value in case further 1272c864c78SJacob Keller * scheduling is not required. 128d9535cb7SGrygorii Strashko * 129d94ba80eSRichard Cochran * Drivers should embed their ptp_clock_info within a private 130d94ba80eSRichard Cochran * structure, obtaining a reference to it using container_of(). 131d94ba80eSRichard Cochran * 132d94ba80eSRichard Cochran * The callbacks must all return zero on success, non-zero otherwise. 133d94ba80eSRichard Cochran */ 134d94ba80eSRichard Cochran 135d94ba80eSRichard Cochran struct ptp_clock_info { 136d94ba80eSRichard Cochran struct module *owner; 137d94ba80eSRichard Cochran char name[16]; 138d94ba80eSRichard Cochran s32 max_adj; 139d94ba80eSRichard Cochran int n_alarm; 140d94ba80eSRichard Cochran int n_ext_ts; 141d94ba80eSRichard Cochran int n_per_out; 1426092315dSRichard Cochran int n_pins; 143d94ba80eSRichard Cochran int pps; 1446092315dSRichard Cochran struct ptp_pin_desc *pin_config; 145d8d26354SRichard Cochran int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm); 146d94ba80eSRichard Cochran int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta); 147184ecc9eSVincent Cheng int (*adjphase)(struct ptp_clock_info *ptp, s32 phase); 148d94ba80eSRichard Cochran int (*adjtime)(struct ptp_clock_info *ptp, s64 delta); 14992f17194SRichard Cochran int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts); 15036180087SMiroslav Lichvar int (*gettimex64)(struct ptp_clock_info *ptp, struct timespec64 *ts, 15136180087SMiroslav Lichvar struct ptp_system_timestamp *sts); 152719f1aa4SChristopher S. Hall int (*getcrosststamp)(struct ptp_clock_info *ptp, 153719f1aa4SChristopher S. Hall struct system_device_crosststamp *cts); 15492f17194SRichard Cochran int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts); 155d94ba80eSRichard Cochran int (*enable)(struct ptp_clock_info *ptp, 156d94ba80eSRichard Cochran struct ptp_clock_request *request, int on); 1576092315dSRichard Cochran int (*verify)(struct ptp_clock_info *ptp, unsigned int pin, 1586092315dSRichard Cochran enum ptp_pin_function func, unsigned int chan); 159d9535cb7SGrygorii Strashko long (*do_aux_work)(struct ptp_clock_info *ptp); 160d94ba80eSRichard Cochran }; 161d94ba80eSRichard Cochran 162d94ba80eSRichard Cochran struct ptp_clock; 163d94ba80eSRichard Cochran 164d94ba80eSRichard Cochran enum ptp_clock_events { 165d94ba80eSRichard Cochran PTP_CLOCK_ALARM, 166d94ba80eSRichard Cochran PTP_CLOCK_EXTTS, 167d94ba80eSRichard Cochran PTP_CLOCK_PPS, 168220a60a4SBen Hutchings PTP_CLOCK_PPSUSR, 169d94ba80eSRichard Cochran }; 170d94ba80eSRichard Cochran 171d94ba80eSRichard Cochran /** 172d94ba80eSRichard Cochran * struct ptp_clock_event - decribes a PTP hardware clock event 173d94ba80eSRichard Cochran * 174d94ba80eSRichard Cochran * @type: One of the ptp_clock_events enumeration values. 175d94ba80eSRichard Cochran * @index: Identifies the source of the event. 176220a60a4SBen Hutchings * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only). 177220a60a4SBen Hutchings * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only). 178d94ba80eSRichard Cochran */ 179d94ba80eSRichard Cochran 180d94ba80eSRichard Cochran struct ptp_clock_event { 181d94ba80eSRichard Cochran int type; 182d94ba80eSRichard Cochran int index; 183220a60a4SBen Hutchings union { 184d94ba80eSRichard Cochran u64 timestamp; 185220a60a4SBen Hutchings struct pps_event_time pps_times; 186220a60a4SBen Hutchings }; 187d94ba80eSRichard Cochran }; 188d94ba80eSRichard Cochran 189*9d9d415fSRadu Pirea (NXP OSS) /** 190*9d9d415fSRadu Pirea (NXP OSS) * scaled_ppm_to_ppb() - convert scaled ppm to ppb 191*9d9d415fSRadu Pirea (NXP OSS) * 192*9d9d415fSRadu Pirea (NXP OSS) * @ppm: Parts per million, but with a 16 bit binary fractional field 193*9d9d415fSRadu Pirea (NXP OSS) */ 194adc2e56eSJakub Kicinski static inline long scaled_ppm_to_ppb(long ppm) 195*9d9d415fSRadu Pirea (NXP OSS) { 196*9d9d415fSRadu Pirea (NXP OSS) /* 197*9d9d415fSRadu Pirea (NXP OSS) * The 'freq' field in the 'struct timex' is in parts per 198*9d9d415fSRadu Pirea (NXP OSS) * million, but with a 16 bit binary fractional field. 199*9d9d415fSRadu Pirea (NXP OSS) * 200*9d9d415fSRadu Pirea (NXP OSS) * We want to calculate 201*9d9d415fSRadu Pirea (NXP OSS) * 202*9d9d415fSRadu Pirea (NXP OSS) * ppb = scaled_ppm * 1000 / 2^16 203*9d9d415fSRadu Pirea (NXP OSS) * 204*9d9d415fSRadu Pirea (NXP OSS) * which simplifies to 205*9d9d415fSRadu Pirea (NXP OSS) * 206*9d9d415fSRadu Pirea (NXP OSS) * ppb = scaled_ppm * 125 / 2^13 207*9d9d415fSRadu Pirea (NXP OSS) */ 208*9d9d415fSRadu Pirea (NXP OSS) s64 ppb = 1 + ppm; 209*9d9d415fSRadu Pirea (NXP OSS) 210*9d9d415fSRadu Pirea (NXP OSS) ppb *= 125; 211*9d9d415fSRadu Pirea (NXP OSS) ppb >>= 13; 212adc2e56eSJakub Kicinski return (long)ppb; 213*9d9d415fSRadu Pirea (NXP OSS) } 214*9d9d415fSRadu Pirea (NXP OSS) 215d1cbfd77SNicolas Pitre #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK) 216d1cbfd77SNicolas Pitre 217d1cbfd77SNicolas Pitre /** 218d1cbfd77SNicolas Pitre * ptp_clock_register() - register a PTP hardware clock driver 219d1cbfd77SNicolas Pitre * 220d1cbfd77SNicolas Pitre * @info: Structure describing the new clock. 221d1cbfd77SNicolas Pitre * @parent: Pointer to the parent device of the new clock. 222d1cbfd77SNicolas Pitre * 223d1cbfd77SNicolas Pitre * Returns a valid pointer on success or PTR_ERR on failure. If PHC 224d1cbfd77SNicolas Pitre * support is missing at the configuration level, this function 225d1cbfd77SNicolas Pitre * returns NULL, and drivers are expected to gracefully handle that 226d1cbfd77SNicolas Pitre * case separately. 227d1cbfd77SNicolas Pitre */ 228d1cbfd77SNicolas Pitre 229d1cbfd77SNicolas Pitre extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, 230d1cbfd77SNicolas Pitre struct device *parent); 231d1cbfd77SNicolas Pitre 232d1cbfd77SNicolas Pitre /** 233d1cbfd77SNicolas Pitre * ptp_clock_unregister() - unregister a PTP hardware clock driver 234d1cbfd77SNicolas Pitre * 235d1cbfd77SNicolas Pitre * @ptp: The clock to remove from service. 236d1cbfd77SNicolas Pitre */ 237d1cbfd77SNicolas Pitre 238d1cbfd77SNicolas Pitre extern int ptp_clock_unregister(struct ptp_clock *ptp); 239d1cbfd77SNicolas Pitre 240d94ba80eSRichard Cochran /** 241d94ba80eSRichard Cochran * ptp_clock_event() - notify the PTP layer about an event 242d94ba80eSRichard Cochran * 243d94ba80eSRichard Cochran * @ptp: The clock obtained from ptp_clock_register(). 244d94ba80eSRichard Cochran * @event: Message structure describing the event. 245d94ba80eSRichard Cochran */ 246d94ba80eSRichard Cochran 247d94ba80eSRichard Cochran extern void ptp_clock_event(struct ptp_clock *ptp, 248d94ba80eSRichard Cochran struct ptp_clock_event *event); 249d94ba80eSRichard Cochran 250995a9090SRichard Cochran /** 251995a9090SRichard Cochran * ptp_clock_index() - obtain the device index of a PTP clock 252995a9090SRichard Cochran * 253995a9090SRichard Cochran * @ptp: The clock obtained from ptp_clock_register(). 254995a9090SRichard Cochran */ 255995a9090SRichard Cochran 256995a9090SRichard Cochran extern int ptp_clock_index(struct ptp_clock *ptp); 257995a9090SRichard Cochran 2586092315dSRichard Cochran /** 2596092315dSRichard Cochran * ptp_find_pin() - obtain the pin index of a given auxiliary function 2606092315dSRichard Cochran * 26162582a7eSRichard Cochran * The caller must hold ptp_clock::pincfg_mux. Drivers do not have 26262582a7eSRichard Cochran * access to that mutex as ptp_clock is an opaque type. However, the 26362582a7eSRichard Cochran * core code acquires the mutex before invoking the driver's 26462582a7eSRichard Cochran * ptp_clock_info::enable() callback, and so drivers may call this 26562582a7eSRichard Cochran * function from that context. 26662582a7eSRichard Cochran * 2676092315dSRichard Cochran * @ptp: The clock obtained from ptp_clock_register(). 2686092315dSRichard Cochran * @func: One of the ptp_pin_function enumerated values. 2696092315dSRichard Cochran * @chan: The particular functional channel to find. 2706092315dSRichard Cochran * Return: Pin index in the range of zero to ptp_clock_caps.n_pins - 1, 2716092315dSRichard Cochran * or -1 if the auxiliary function cannot be found. 2726092315dSRichard Cochran */ 2736092315dSRichard Cochran 2746092315dSRichard Cochran int ptp_find_pin(struct ptp_clock *ptp, 2756092315dSRichard Cochran enum ptp_pin_function func, unsigned int chan); 2766092315dSRichard Cochran 277d9535cb7SGrygorii Strashko /** 27862582a7eSRichard Cochran * ptp_find_pin_unlocked() - wrapper for ptp_find_pin() 27962582a7eSRichard Cochran * 28062582a7eSRichard Cochran * This function acquires the ptp_clock::pincfg_mux mutex before 28162582a7eSRichard Cochran * invoking ptp_find_pin(). Instead of using this function, drivers 28262582a7eSRichard Cochran * should most likely call ptp_find_pin() directly from their 28362582a7eSRichard Cochran * ptp_clock_info::enable() method. 28462582a7eSRichard Cochran * 28562582a7eSRichard Cochran */ 28662582a7eSRichard Cochran 28762582a7eSRichard Cochran int ptp_find_pin_unlocked(struct ptp_clock *ptp, 28862582a7eSRichard Cochran enum ptp_pin_function func, unsigned int chan); 28962582a7eSRichard Cochran 29062582a7eSRichard Cochran /** 291d9535cb7SGrygorii Strashko * ptp_schedule_worker() - schedule ptp auxiliary work 292d9535cb7SGrygorii Strashko * 293d9535cb7SGrygorii Strashko * @ptp: The clock obtained from ptp_clock_register(). 294d9535cb7SGrygorii Strashko * @delay: number of jiffies to wait before queuing 295d9535cb7SGrygorii Strashko * See kthread_queue_delayed_work() for more info. 296d9535cb7SGrygorii Strashko */ 297d9535cb7SGrygorii Strashko 298d9535cb7SGrygorii Strashko int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay); 299d9535cb7SGrygorii Strashko 300544fed47SVladimir Oltean /** 301544fed47SVladimir Oltean * ptp_cancel_worker_sync() - cancel ptp auxiliary clock 302544fed47SVladimir Oltean * 303544fed47SVladimir Oltean * @ptp: The clock obtained from ptp_clock_register(). 304544fed47SVladimir Oltean */ 305544fed47SVladimir Oltean void ptp_cancel_worker_sync(struct ptp_clock *ptp); 306544fed47SVladimir Oltean 307d1cbfd77SNicolas Pitre #else 308d1cbfd77SNicolas Pitre static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, 309d1cbfd77SNicolas Pitre struct device *parent) 310d1cbfd77SNicolas Pitre { return NULL; } 311d1cbfd77SNicolas Pitre static inline int ptp_clock_unregister(struct ptp_clock *ptp) 312d1cbfd77SNicolas Pitre { return 0; } 313d1cbfd77SNicolas Pitre static inline void ptp_clock_event(struct ptp_clock *ptp, 314d1cbfd77SNicolas Pitre struct ptp_clock_event *event) 315d1cbfd77SNicolas Pitre { } 316d1cbfd77SNicolas Pitre static inline int ptp_clock_index(struct ptp_clock *ptp) 317d1cbfd77SNicolas Pitre { return -1; } 318d1cbfd77SNicolas Pitre static inline int ptp_find_pin(struct ptp_clock *ptp, 319d1cbfd77SNicolas Pitre enum ptp_pin_function func, unsigned int chan) 320d1cbfd77SNicolas Pitre { return -1; } 321d9535cb7SGrygorii Strashko static inline int ptp_schedule_worker(struct ptp_clock *ptp, 322d9535cb7SGrygorii Strashko unsigned long delay) 323d9535cb7SGrygorii Strashko { return -EOPNOTSUPP; } 324544fed47SVladimir Oltean static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp) 325544fed47SVladimir Oltean { } 326d9535cb7SGrygorii Strashko 327d1cbfd77SNicolas Pitre #endif 328d1cbfd77SNicolas Pitre 32936180087SMiroslav Lichvar static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts) 33036180087SMiroslav Lichvar { 33136180087SMiroslav Lichvar if (sts) 33236180087SMiroslav Lichvar ktime_get_real_ts64(&sts->pre_ts); 33336180087SMiroslav Lichvar } 33436180087SMiroslav Lichvar 33536180087SMiroslav Lichvar static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts) 33636180087SMiroslav Lichvar { 33736180087SMiroslav Lichvar if (sts) 33836180087SMiroslav Lichvar ktime_get_real_ts64(&sts->post_ts); 33936180087SMiroslav Lichvar } 34036180087SMiroslav Lichvar 341d94ba80eSRichard Cochran #endif 342