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