xref: /linux/drivers/dpll/zl3073x/dpll.c (revision 0748e473b65a8c8690e250cdf7cc4f5a93252493)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/bits.h>
4 #include <linux/bitfield.h>
5 #include <linux/cleanup.h>
6 #include <linux/bug.h>
7 #include <linux/container_of.h>
8 #include <linux/dev_printk.h>
9 #include <linux/dpll.h>
10 #include <linux/err.h>
11 #include <linux/kthread.h>
12 #include <linux/math64.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/ptp_clock_kernel.h>
18 #include <linux/slab.h>
19 #include <linux/sprintf.h>
20 
21 #include "core.h"
22 #include "dpll.h"
23 #include "prop.h"
24 #include "regs.h"
25 
26 #define ZL3073X_DPLL_REF_NONE		ZL3073X_NUM_REFS
27 #define ZL3073X_DPLL_REF_IS_VALID(_ref)	((_ref) != ZL3073X_DPLL_REF_NONE)
28 
29 /**
30  * struct zl3073x_dpll_pin - DPLL pin
31  * @list: this DPLL pin list entry
32  * @dpll: DPLL the pin is registered to
33  * @dpll_pin: pointer to registered dpll_pin
34  * @tracker: tracking object for the acquired reference
35  * @fwnode: firmware node handle
36  * @label: package label
37  * @dir: pin direction
38  * @id: pin id
39  * @prio: pin priority <0, 14>
40  * @esync_control: embedded sync is controllable
41  * @phase_gran: phase adjustment granularity
42  * @operstate: last saved operational state
43  * @phase_offset: last saved pin phase offset
44  * @freq_offset: last saved fractional frequency offset
45  * @measured_freq: last saved measured frequency
46  */
47 struct zl3073x_dpll_pin {
48 	struct list_head	list;
49 	struct zl3073x_dpll	*dpll;
50 	struct dpll_pin		*dpll_pin;
51 	dpll_tracker		tracker;
52 	struct fwnode_handle	*fwnode;
53 	char			label[8];
54 	enum dpll_pin_direction	dir;
55 	u8			id;
56 	u8			prio;
57 	bool			esync_control;
58 	s32			phase_gran;
59 	enum dpll_pin_operstate	operstate;
60 	s64			phase_offset;
61 	s64			freq_offset;
62 	u32			measured_freq;
63 };
64 
65 /*
66  * Supported esync ranges for input and for output per output pair type
67  */
68 static const struct dpll_pin_frequency esync_freq_ranges[] = {
69 	DPLL_PIN_FREQUENCY_RANGE(0, 1),
70 };
71 
72 /**
73  * zl3073x_dpll_is_input_pin - check if the pin is input one
74  * @pin: pin to check
75  *
76  * Return: true if pin is input, false if pin is output.
77  */
78 static bool
79 zl3073x_dpll_is_input_pin(struct zl3073x_dpll_pin *pin)
80 {
81 	return pin->dir == DPLL_PIN_DIRECTION_INPUT;
82 }
83 
84 /**
85  * zl3073x_dpll_is_nco_pin - check if the pin is a virtual NCO pin
86  * @pin: pin to check
87  *
88  * Return: true if pin is a virtual NCO pin, false otherwise.
89  */
90 static bool
91 zl3073x_dpll_is_nco_pin(struct zl3073x_dpll_pin *pin)
92 {
93 	return pin->id == ZL3073X_NCO_PIN_ID;
94 }
95 
96 /**
97  * zl3073x_dpll_is_p_pin - check if the pin is P-pin
98  * @pin: pin to check
99  *
100  * Return: true if the pin is P-pin, false if it is N-pin
101  */
102 static bool
103 zl3073x_dpll_is_p_pin(struct zl3073x_dpll_pin *pin)
104 {
105 	return zl3073x_is_p_pin(pin->id);
106 }
107 
108 static int
109 zl3073x_dpll_pin_direction_get(const struct dpll_pin *dpll_pin, void *pin_priv,
110 			       const struct dpll_device *dpll, void *dpll_priv,
111 			       enum dpll_pin_direction *direction,
112 			       struct netlink_ext_ack *extack)
113 {
114 	struct zl3073x_dpll_pin *pin = pin_priv;
115 
116 	*direction = pin->dir;
117 
118 	return 0;
119 }
120 
121 static struct zl3073x_dpll_pin *
122 zl3073x_dpll_pin_get_by_ref(struct zl3073x_dpll *zldpll, u8 ref_id)
123 {
124 	struct zl3073x_dpll_pin *pin;
125 
126 	lockdep_assert_held(&zldpll->lock);
127 
128 	list_for_each_entry(pin, &zldpll->pins, list) {
129 		if (zl3073x_dpll_is_input_pin(pin) &&
130 		    zl3073x_input_pin_ref_get(pin->id) == ref_id)
131 			return pin;
132 	}
133 
134 	return NULL;
135 }
136 
137 /**
138  * zl3073x_dpll_output_pin_sibling_get - get the other pin of an output pair
139  * @pin: output pin whose sibling is sought
140  *
141  * Output pin ids are allocated in P/N pairs (P even, N odd) that share a
142  * single HW output. Looks up the other pin of the pair in the pin list
143  * of this DPLL. A pin is on the list from just before its registration
144  * until just after its unregistration, so a registered sibling is always
145  * found, but the returned pin may also be one that is not (yet or any
146  * longer) registered.
147  *
148  * Return: pointer to sibling pin, or NULL if it is not on the pin list
149  */
150 static struct zl3073x_dpll_pin *
151 zl3073x_dpll_output_pin_sibling_get(struct zl3073x_dpll_pin *pin)
152 {
153 	struct zl3073x_dpll_pin *sibling;
154 
155 	lockdep_assert_held(&pin->dpll->lock);
156 
157 	list_for_each_entry(sibling, &pin->dpll->pins, list) {
158 		if (!zl3073x_dpll_is_input_pin(sibling) &&
159 		    sibling->id == (pin->id ^ 1))
160 			return sibling;
161 	}
162 
163 	return NULL;
164 }
165 
166 static struct zl3073x_dpll_pin *
167 zl3073x_dpll_nco_pin_get(struct zl3073x_dpll *zldpll)
168 {
169 	struct zl3073x_dpll_pin *pin;
170 
171 	lockdep_assert_held(&zldpll->lock);
172 
173 	list_for_each_entry(pin, &zldpll->pins, list) {
174 		if (zl3073x_dpll_is_nco_pin(pin))
175 			return pin;
176 	}
177 
178 	return NULL;
179 }
180 
181 static int
182 zl3073x_dpll_input_pin_esync_get(const struct dpll_pin *dpll_pin,
183 				 void *pin_priv,
184 				 const struct dpll_device *dpll,
185 				 void *dpll_priv,
186 				 struct dpll_pin_esync *esync,
187 				 struct netlink_ext_ack *extack)
188 {
189 	struct zl3073x_dpll *zldpll = dpll_priv;
190 	struct zl3073x_dev *zldev = zldpll->dev;
191 	struct zl3073x_dpll_pin *pin = pin_priv;
192 	const struct zl3073x_ref *ref;
193 	u8 ref_id;
194 
195 	guard(mutex)(&zldpll->lock);
196 
197 	ref_id = zl3073x_input_pin_ref_get(pin->id);
198 	ref = zl3073x_ref_state_get(zldev, ref_id);
199 
200 	if (!pin->esync_control || zl3073x_ref_freq_get(ref) <= 1)
201 		return -EOPNOTSUPP;
202 
203 	esync->range = esync_freq_ranges;
204 	esync->range_num = ARRAY_SIZE(esync_freq_ranges);
205 
206 	switch (zl3073x_ref_sync_mode_get(ref)) {
207 	case ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75:
208 		esync->freq = ref->esync_n_div == ZL_REF_ESYNC_DIV_1HZ ? 1 : 0;
209 		esync->pulse = 25;
210 		break;
211 	default:
212 		esync->freq = 0;
213 		esync->pulse = 0;
214 		break;
215 	}
216 
217 	return 0;
218 }
219 
220 static int
221 zl3073x_dpll_input_pin_esync_set(const struct dpll_pin *dpll_pin,
222 				 void *pin_priv,
223 				 const struct dpll_device *dpll,
224 				 void *dpll_priv, u64 freq,
225 				 struct netlink_ext_ack *extack)
226 {
227 	struct zl3073x_dpll *zldpll = dpll_priv;
228 	struct zl3073x_dev *zldev = zldpll->dev;
229 	struct zl3073x_dpll_pin *pin = pin_priv;
230 	struct zl3073x_ref ref;
231 	u8 ref_id, sync_mode;
232 
233 	guard(mutex)(&zldpll->lock);
234 
235 	ref_id = zl3073x_input_pin_ref_get(pin->id);
236 	ref = *zl3073x_ref_state_get(zldev, ref_id);
237 
238 	/* Use freq == 0 to disable esync */
239 	if (!freq)
240 		sync_mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF;
241 	else
242 		sync_mode = ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75;
243 
244 	zl3073x_ref_sync_mode_set(&ref, sync_mode);
245 
246 	if (freq) {
247 		/* 1 Hz is only supported frequency now */
248 		ref.esync_n_div = ZL_REF_ESYNC_DIV_1HZ;
249 	}
250 
251 	/* Update reference configuration */
252 	return zl3073x_ref_state_set(zldev, ref_id, &ref);
253 }
254 
255 static int
256 zl3073x_dpll_input_pin_ref_sync_get(const struct dpll_pin *dpll_pin,
257 				    void *pin_priv,
258 				    const struct dpll_pin *ref_sync_pin,
259 				    void *ref_sync_pin_priv,
260 				    enum dpll_pin_state *state,
261 				    struct netlink_ext_ack *extack)
262 {
263 	struct zl3073x_dpll_pin *sync_pin = ref_sync_pin_priv;
264 	struct zl3073x_dpll_pin *pin = pin_priv;
265 	struct zl3073x_dpll *zldpll = pin->dpll;
266 	struct zl3073x_dev *zldev = zldpll->dev;
267 	const struct zl3073x_ref *ref;
268 	u8 ref_id, mode, pair;
269 
270 	guard(mutex)(&zldpll->lock);
271 
272 	ref_id = zl3073x_input_pin_ref_get(pin->id);
273 	ref = zl3073x_ref_state_get(zldev, ref_id);
274 	mode = zl3073x_ref_sync_mode_get(ref);
275 	pair = zl3073x_ref_sync_pair_get(ref);
276 
277 	if (mode == ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR &&
278 	    pair == zl3073x_input_pin_ref_get(sync_pin->id))
279 		*state = DPLL_PIN_STATE_CONNECTED;
280 	else
281 		*state = DPLL_PIN_STATE_DISCONNECTED;
282 
283 	return 0;
284 }
285 
286 static int
287 zl3073x_dpll_input_pin_ref_sync_set(const struct dpll_pin *dpll_pin,
288 				    void *pin_priv,
289 				    const struct dpll_pin *ref_sync_pin,
290 				    void *ref_sync_pin_priv,
291 				    const enum dpll_pin_state state,
292 				    struct netlink_ext_ack *extack)
293 {
294 	struct zl3073x_dpll_pin *sync_pin = ref_sync_pin_priv;
295 	struct zl3073x_dpll_pin *pin = pin_priv;
296 	struct zl3073x_dpll *zldpll = pin->dpll;
297 	struct zl3073x_dev *zldev = zldpll->dev;
298 	u8 mode, ref_id, sync_ref_id;
299 	struct zl3073x_chan chan;
300 	struct zl3073x_ref ref;
301 	bool sync_ntf = false;
302 	int rc;
303 
304 	mutex_lock(&zldpll->lock);
305 
306 	ref_id = zl3073x_input_pin_ref_get(pin->id);
307 	sync_ref_id = zl3073x_input_pin_ref_get(sync_pin->id);
308 	ref = *zl3073x_ref_state_get(zldev, ref_id);
309 
310 	if (state == DPLL_PIN_STATE_CONNECTED) {
311 		const struct zl3073x_ref *sync_ref;
312 		u32 ref_freq, sync_freq;
313 
314 		sync_ref = zl3073x_ref_state_get(zldev, sync_ref_id);
315 		ref_freq = zl3073x_ref_freq_get(&ref);
316 		sync_freq = zl3073x_ref_freq_get(sync_ref);
317 
318 		/* Sync signal must be 8 kHz or less and clock reference
319 		 * must be 1 kHz or more and higher than the sync signal.
320 		 */
321 		if (sync_freq > 8000) {
322 			NL_SET_ERR_MSG(extack,
323 				       "sync frequency must be 8 kHz or less");
324 			rc = -EINVAL;
325 			goto unlock;
326 		}
327 		if (ref_freq < 1000) {
328 			NL_SET_ERR_MSG(extack,
329 				       "clock frequency must be 1 kHz or more");
330 			rc = -EINVAL;
331 			goto unlock;
332 		}
333 		if (ref_freq <= sync_freq) {
334 			NL_SET_ERR_MSG(extack,
335 				       "clock frequency must be higher than sync frequency");
336 			rc = -EINVAL;
337 			goto unlock;
338 		}
339 
340 		zl3073x_ref_sync_pair_set(&ref, sync_ref_id);
341 		mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR;
342 	} else {
343 		mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF;
344 	}
345 
346 	zl3073x_ref_sync_mode_set(&ref, mode);
347 
348 	rc = zl3073x_ref_state_set(zldev, ref_id, &ref);
349 	if (rc)
350 		goto unlock;
351 
352 	/* All code paths accessing per-channel reference priorities are
353 	 * serialized by the subsystem dpll_lock, so it is safe to release
354 	 * our lock here before iterating over the other channels.
355 	 */
356 	mutex_unlock(&zldpll->lock);
357 
358 	if (state != DPLL_PIN_STATE_CONNECTED)
359 		return 0;
360 
361 	/* The datasheet recommends excluding the sync source from automatic
362 	 * reference selection by setting its priority to NONE on all DPLL
363 	 * channels. This is advisory - the ref sync pair is already
364 	 * configured, so a failure here is not fatal. On disconnect the
365 	 * priority is left as NONE and the user must explicitly make the
366 	 * pin selectable again.
367 	 */
368 	list_for_each_entry(zldpll, &zldev->dplls, list) {
369 		u8 prio;
370 
371 		mutex_lock(&zldpll->lock);
372 
373 		chan = *zl3073x_chan_state_get(zldev, zldpll->id);
374 		prio = zl3073x_chan_ref_prio_get(&chan, sync_ref_id);
375 		if (prio == ZL_DPLL_REF_PRIO_NONE) {
376 			mutex_unlock(&zldpll->lock);
377 			continue; /* Ref is already non-selectable */
378 		}
379 
380 		zl3073x_chan_ref_prio_set(&chan, sync_ref_id,
381 					  ZL_DPLL_REF_PRIO_NONE);
382 		if (zl3073x_chan_state_set(zldev, zldpll->id, &chan))
383 			dev_warn(zldev->dev,
384 				 "Failed to set ref prio on DPLL%u\n",
385 				 zldpll->id);
386 		else
387 			sync_ntf = true;
388 
389 		mutex_unlock(&zldpll->lock);
390 	}
391 	if (sync_ntf)
392 		__dpll_pin_change_ntf(sync_pin->dpll_pin);
393 
394 	return 0;
395 unlock:
396 	mutex_unlock(&zldpll->lock);
397 	return rc;
398 }
399 
400 static int
401 zl3073x_dpll_input_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv,
402 			       const struct dpll_device *dpll, void *dpll_priv,
403 			       struct dpll_ffo_param *ffo,
404 			       struct netlink_ext_ack *extack)
405 {
406 	struct zl3073x_dpll *zldpll = dpll_priv;
407 	struct zl3073x_dpll_pin *pin = pin_priv;
408 
409 	guard(mutex)(&zldpll->lock);
410 
411 	if (pin->operstate != DPLL_PIN_OPERSTATE_ACTIVE)
412 		return -ENODATA;
413 
414 	ffo->ffo = pin->freq_offset;
415 
416 	return 0;
417 }
418 
419 static int
420 zl3073x_dpll_input_pin_measured_freq_get(const struct dpll_pin *dpll_pin,
421 					 void *pin_priv,
422 					 const struct dpll_device *dpll,
423 					 void *dpll_priv, u64 *measured_freq,
424 					 struct netlink_ext_ack *extack)
425 {
426 	struct zl3073x_dpll *zldpll = dpll_priv;
427 	struct zl3073x_dpll_pin *pin = pin_priv;
428 
429 	guard(mutex)(&zldpll->lock);
430 
431 	*measured_freq = pin->measured_freq;
432 	*measured_freq *= DPLL_PIN_MEASURED_FREQUENCY_DIVIDER;
433 
434 	return 0;
435 }
436 
437 static int
438 zl3073x_dpll_input_pin_frequency_get(const struct dpll_pin *dpll_pin,
439 				     void *pin_priv,
440 				     const struct dpll_device *dpll,
441 				     void *dpll_priv, u64 *frequency,
442 				     struct netlink_ext_ack *extack)
443 {
444 	struct zl3073x_dpll *zldpll = dpll_priv;
445 	struct zl3073x_dpll_pin *pin = pin_priv;
446 	u8 ref_id;
447 
448 	guard(mutex)(&zldpll->lock);
449 
450 	ref_id = zl3073x_input_pin_ref_get(pin->id);
451 	*frequency = zl3073x_dev_ref_freq_get(zldpll->dev, ref_id);
452 
453 	return 0;
454 }
455 
456 static int
457 zl3073x_dpll_input_pin_frequency_set(const struct dpll_pin *dpll_pin,
458 				     void *pin_priv,
459 				     const struct dpll_device *dpll,
460 				     void *dpll_priv, u64 frequency,
461 				     struct netlink_ext_ack *extack)
462 {
463 	struct zl3073x_dpll *zldpll = dpll_priv;
464 	struct zl3073x_dev *zldev = zldpll->dev;
465 	struct zl3073x_dpll_pin *pin = pin_priv;
466 	struct zl3073x_ref ref;
467 	u8 ref_id;
468 
469 	guard(mutex)(&zldpll->lock);
470 
471 	/* Get reference state */
472 	ref_id = zl3073x_input_pin_ref_get(pin->id);
473 	ref = *zl3073x_ref_state_get(zldev, ref_id);
474 
475 	/* Update frequency */
476 	zl3073x_ref_freq_set(&ref, frequency);
477 
478 	/* Commit reference state */
479 	return zl3073x_ref_state_set(zldev, ref_id, &ref);
480 }
481 
482 /**
483  * zl3073x_dpll_connected_ref_get - get currently connected reference
484  * @zldpll: pointer to zl3073x_dpll
485  *
486  * Looks for currently connected reference the DPLL is locked to.
487  *
488  * Return: reference index if locked, ZL3073X_DPLL_REF_NONE otherwise
489  */
490 static u8
491 zl3073x_dpll_connected_ref_get(struct zl3073x_dpll *zldpll)
492 {
493 	const struct zl3073x_chan *chan = zl3073x_chan_state_get(zldpll->dev,
494 								 zldpll->id);
495 	u8 state;
496 
497 	/* A reference is connected only when the DPLL is locked to it */
498 	state = zl3073x_chan_refsel_state_get(chan);
499 	if (state == ZL_DPLL_REFSEL_STATUS_STATE_LOCK)
500 		return zl3073x_chan_refsel_ref_get(chan);
501 
502 	return ZL3073X_DPLL_REF_NONE;
503 }
504 
505 static int
506 zl3073x_dpll_input_pin_phase_offset_get(const struct dpll_pin *dpll_pin,
507 					void *pin_priv,
508 					const struct dpll_device *dpll,
509 					void *dpll_priv, s64 *phase_offset,
510 					struct netlink_ext_ack *extack)
511 {
512 	struct zl3073x_dpll *zldpll = dpll_priv;
513 	struct zl3073x_dev *zldev = zldpll->dev;
514 	struct zl3073x_dpll_pin *pin = pin_priv;
515 	const struct zl3073x_ref *ref;
516 	u8 conn_id, ref_id;
517 	s64 ref_phase;
518 
519 	guard(mutex)(&zldpll->lock);
520 
521 	/* Get currently connected reference */
522 	conn_id = zl3073x_dpll_connected_ref_get(zldpll);
523 
524 	/* Report phase offset only for currently connected pin if the phase
525 	 * monitor feature is disabled and only if the input pin signal is
526 	 * present.
527 	 */
528 	ref_id = zl3073x_input_pin_ref_get(pin->id);
529 	ref = zl3073x_ref_state_get(zldev, ref_id);
530 	if ((!zldpll->phase_monitor && ref_id != conn_id) ||
531 	    !zl3073x_ref_is_status_ok(ref)) {
532 		*phase_offset = 0;
533 		return 0;
534 	}
535 
536 	ref_phase = pin->phase_offset;
537 
538 	/* The DPLL being locked to a higher freq than the current ref
539 	 * the phase offset is modded to the period of the signal
540 	 * the dpll is locked to.
541 	 */
542 	if (ZL3073X_DPLL_REF_IS_VALID(conn_id) && conn_id != ref_id) {
543 		u32 conn_freq, ref_freq;
544 
545 		/* Get frequency of connected and given ref */
546 		conn_freq = zl3073x_dev_ref_freq_get(zldev, conn_id);
547 		ref_freq = zl3073x_ref_freq_get(ref);
548 
549 		if (conn_freq > ref_freq) {
550 			s64 conn_period, div_factor;
551 
552 			conn_period = div_s64(PSEC_PER_SEC, conn_freq);
553 			div_factor = div64_s64(ref_phase, conn_period);
554 			ref_phase -= conn_period * div_factor;
555 		}
556 	}
557 
558 	*phase_offset = ref_phase * DPLL_PHASE_OFFSET_DIVIDER;
559 
560 	return 0;
561 }
562 
563 static int
564 zl3073x_dpll_input_pin_phase_adjust_get(const struct dpll_pin *dpll_pin,
565 					void *pin_priv,
566 					const struct dpll_device *dpll,
567 					void *dpll_priv,
568 					s32 *phase_adjust,
569 					struct netlink_ext_ack *extack)
570 {
571 	struct zl3073x_dpll *zldpll = dpll_priv;
572 	struct zl3073x_dev *zldev = zldpll->dev;
573 	struct zl3073x_dpll_pin *pin = pin_priv;
574 	const struct zl3073x_ref *ref;
575 	s64 phase_comp;
576 	u8 ref_id;
577 
578 	guard(mutex)(&zldpll->lock);
579 
580 	/* Read reference configuration */
581 	ref_id = zl3073x_input_pin_ref_get(pin->id);
582 	ref = zl3073x_ref_state_get(zldev, ref_id);
583 
584 	/* Perform sign extension based on register width */
585 	if (zl3073x_dev_is_ref_phase_comp_32bit(zldev))
586 		phase_comp = sign_extend64(ref->phase_comp, 31);
587 	else
588 		phase_comp = sign_extend64(ref->phase_comp, 47);
589 
590 	/* Reverse two's complement negation applied during set and convert
591 	 * to 32bit signed int
592 	 */
593 	*phase_adjust = (s32)-phase_comp;
594 
595 	return 0;
596 }
597 
598 static int
599 zl3073x_dpll_input_pin_phase_adjust_set(const struct dpll_pin *dpll_pin,
600 					void *pin_priv,
601 					const struct dpll_device *dpll,
602 					void *dpll_priv,
603 					s32 phase_adjust,
604 					struct netlink_ext_ack *extack)
605 {
606 	struct zl3073x_dpll *zldpll = dpll_priv;
607 	struct zl3073x_dev *zldev = zldpll->dev;
608 	struct zl3073x_dpll_pin *pin = pin_priv;
609 	struct zl3073x_ref ref;
610 	u8 ref_id;
611 
612 	guard(mutex)(&zldpll->lock);
613 
614 	/* Read reference configuration */
615 	ref_id = zl3073x_input_pin_ref_get(pin->id);
616 	ref = *zl3073x_ref_state_get(zldev, ref_id);
617 
618 	/* The value in the register is stored as two's complement negation
619 	 * of requested value.
620 	 */
621 	ref.phase_comp = -phase_adjust;
622 
623 	/* Update reference configuration */
624 	return zl3073x_ref_state_set(zldev, ref_id, &ref);
625 }
626 
627 /**
628  * zl3073x_dpll_ref_operstate_get - get operational state for input pin
629  * @pin: pointer to pin
630  * @operstate: place to store operational state
631  *
632  * Returns the actual hardware state of the pin: whether it is actively
633  * used by the DPLL, has no signal, failed qualification, or is simply
634  * not in use.
635  *
636  * Return: 0 on success, <0 on error
637  */
638 static int
639 zl3073x_dpll_ref_operstate_get(struct zl3073x_dpll_pin *pin,
640 			       enum dpll_pin_operstate *operstate)
641 {
642 	struct zl3073x_dpll *zldpll = pin->dpll;
643 	struct zl3073x_dev *zldev = zldpll->dev;
644 	const struct zl3073x_ref *ref;
645 	u8 ref_id;
646 
647 	lockdep_assert_held(&zldpll->lock);
648 
649 	ref_id = zl3073x_input_pin_ref_get(pin->id);
650 
651 	/* Check if this pin is the currently locked reference */
652 	if (ref_id == zl3073x_dpll_connected_ref_get(zldpll)) {
653 		*operstate = DPLL_PIN_OPERSTATE_ACTIVE;
654 		return 0;
655 	}
656 
657 	/* Check reference monitor status */
658 	ref = zl3073x_ref_state_get(zldev, ref_id);
659 	if (ref->mon_status & ZL_REF_MON_STATUS_LOS)
660 		*operstate = DPLL_PIN_OPERSTATE_NO_SIGNAL;
661 	else if (!zl3073x_ref_is_status_ok(ref))
662 		*operstate = DPLL_PIN_OPERSTATE_QUAL_FAILED;
663 	else
664 		*operstate = DPLL_PIN_OPERSTATE_STANDBY;
665 
666 	return 0;
667 }
668 
669 static int
670 zl3073x_dpll_input_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin,
671 					 void *pin_priv,
672 					 const struct dpll_device *dpll,
673 					 void *dpll_priv,
674 					 enum dpll_pin_state *state,
675 					 struct netlink_ext_ack *extack)
676 {
677 	struct zl3073x_dpll *zldpll = dpll_priv;
678 	struct zl3073x_dpll_pin *pin = pin_priv;
679 	const struct zl3073x_chan *chan;
680 	u8 mode, ref;
681 
682 	guard(mutex)(&zldpll->lock);
683 
684 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
685 	ref = zl3073x_input_pin_ref_get(pin->id);
686 	mode = zl3073x_chan_mode_get(chan);
687 
688 	switch (mode) {
689 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
690 		if (ref == zl3073x_chan_ref_get(chan))
691 			*state = DPLL_PIN_STATE_CONNECTED;
692 		else
693 			*state = DPLL_PIN_STATE_DISCONNECTED;
694 		break;
695 	case ZL_DPLL_MODE_REFSEL_MODE_AUTO:
696 		if (zl3073x_chan_ref_is_selectable(chan, ref))
697 			*state = DPLL_PIN_STATE_SELECTABLE;
698 		else
699 			*state = DPLL_PIN_STATE_DISCONNECTED;
700 		break;
701 	default:
702 		*state = DPLL_PIN_STATE_DISCONNECTED;
703 		break;
704 	}
705 
706 	return 0;
707 }
708 
709 static int
710 zl3073x_dpll_input_pin_operstate_on_dpll_get(const struct dpll_pin *dpll_pin,
711 					     void *pin_priv,
712 					     const struct dpll_device *dpll,
713 					     void *dpll_priv,
714 					     enum dpll_pin_operstate *operstate,
715 					     struct netlink_ext_ack *extack)
716 {
717 	struct zl3073x_dpll *zldpll = dpll_priv;
718 	struct zl3073x_dpll_pin *pin = pin_priv;
719 
720 	guard(mutex)(&zldpll->lock);
721 
722 	return zl3073x_dpll_ref_operstate_get(pin, operstate);
723 }
724 
725 static int
726 zl3073x_dpll_input_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin,
727 					 void *pin_priv,
728 					 const struct dpll_device *dpll,
729 					 void *dpll_priv,
730 					 enum dpll_pin_state state,
731 					 struct netlink_ext_ack *extack)
732 {
733 	struct zl3073x_dpll *zldpll = dpll_priv;
734 	struct zl3073x_dpll_pin *pin = pin_priv;
735 	struct zl3073x_dpll_pin *nco_pin = NULL;
736 	struct zl3073x_chan chan;
737 	u8 mode, ref;
738 	int rc = 0;
739 
740 	mutex_lock(&zldpll->lock);
741 
742 	chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id);
743 	ref = zl3073x_input_pin_ref_get(pin->id);
744 	mode = zl3073x_chan_mode_get(&chan);
745 
746 	switch (mode) {
747 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
748 		if (state == DPLL_PIN_STATE_CONNECTED) {
749 			/* Choose the pin as new selected reference */
750 			zl3073x_chan_ref_set(&chan, ref);
751 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
752 			/* Choose new mode based on lock status */
753 			switch (zldpll->lock_status) {
754 			case DPLL_LOCK_STATUS_LOCKED_HO_ACQ:
755 			case DPLL_LOCK_STATUS_HOLDOVER:
756 				mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER;
757 				break;
758 			default:
759 				mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN;
760 				break;
761 			}
762 			zl3073x_chan_mode_set(&chan, mode);
763 		} else {
764 			goto invalid_state;
765 		}
766 		break;
767 	case ZL_DPLL_MODE_REFSEL_MODE_NCO:
768 		if (state == DPLL_PIN_STATE_CONNECTED)
769 			nco_pin = zl3073x_dpll_nco_pin_get(zldpll);
770 		fallthrough;
771 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
772 	case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER:
773 		if (state == DPLL_PIN_STATE_CONNECTED) {
774 			/* Choose the pin as new selected reference */
775 			zl3073x_chan_ref_set(&chan, ref);
776 			/* Switch to reflock mode */
777 			zl3073x_chan_mode_set(&chan,
778 					      ZL_DPLL_MODE_REFSEL_MODE_REFLOCK);
779 		} else if (state != DPLL_PIN_STATE_DISCONNECTED) {
780 			goto invalid_state;
781 		}
782 		break;
783 	case ZL_DPLL_MODE_REFSEL_MODE_AUTO:
784 		if (state == DPLL_PIN_STATE_SELECTABLE) {
785 			if (zl3073x_chan_ref_is_selectable(&chan, ref))
786 				goto unlock; /* Pin is already selectable */
787 
788 			/* Restore pin priority in HW */
789 			zl3073x_chan_ref_prio_set(&chan, ref, pin->prio);
790 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
791 			if (!zl3073x_chan_ref_is_selectable(&chan, ref))
792 				goto unlock; /* Pin is already disconnected */
793 
794 			/* Set pin priority to none in HW */
795 			zl3073x_chan_ref_prio_set(&chan, ref,
796 						  ZL_DPLL_REF_PRIO_NONE);
797 		} else {
798 			goto invalid_state;
799 		}
800 		break;
801 	default:
802 		/* In other modes we cannot change input reference */
803 		NL_SET_ERR_MSG(extack,
804 			       "Pin state cannot be changed in current mode");
805 		rc = -EOPNOTSUPP;
806 		goto unlock;
807 	}
808 
809 	/* Commit DPLL channel changes */
810 	rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan);
811 	goto unlock;
812 
813 invalid_state:
814 	NL_SET_ERR_MSG_MOD(extack, "Invalid pin state for this device mode");
815 	rc = -EINVAL;
816 unlock:
817 	mutex_unlock(&zldpll->lock);
818 
819 	/* If leaving NCO mode, notify userspace about the NCO pin
820 	 * state change - the periodic worker skips the NCO pin.
821 	 */
822 	if (!rc && nco_pin)
823 		__dpll_pin_change_ntf(nco_pin->dpll_pin);
824 
825 	return rc;
826 }
827 
828 static int
829 zl3073x_dpll_input_pin_prio_get(const struct dpll_pin *dpll_pin, void *pin_priv,
830 				const struct dpll_device *dpll, void *dpll_priv,
831 				u32 *prio, struct netlink_ext_ack *extack)
832 {
833 	struct zl3073x_dpll *zldpll = dpll_priv;
834 	struct zl3073x_dpll_pin *pin = pin_priv;
835 
836 	guard(mutex)(&zldpll->lock);
837 
838 	*prio = pin->prio;
839 
840 	return 0;
841 }
842 
843 static int
844 zl3073x_dpll_input_pin_prio_set(const struct dpll_pin *dpll_pin, void *pin_priv,
845 				const struct dpll_device *dpll, void *dpll_priv,
846 				u32 prio, struct netlink_ext_ack *extack)
847 {
848 	struct zl3073x_dpll *zldpll = dpll_priv;
849 	struct zl3073x_dpll_pin *pin = pin_priv;
850 	struct zl3073x_chan chan;
851 	u8 ref;
852 	int rc;
853 
854 	guard(mutex)(&zldpll->lock);
855 
856 	if (prio > ZL_DPLL_REF_PRIO_MAX)
857 		return -EINVAL;
858 
859 	/* If the pin is selectable then update HW registers */
860 	chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id);
861 	ref = zl3073x_input_pin_ref_get(pin->id);
862 	if (zl3073x_chan_ref_is_selectable(&chan, ref)) {
863 		zl3073x_chan_ref_prio_set(&chan, ref, prio);
864 		rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan);
865 		if (rc)
866 			return rc;
867 	}
868 
869 	/* Save priority */
870 	pin->prio = prio;
871 
872 	return 0;
873 }
874 
875 static int
876 zl3073x_dpll_output_pin_esync_get(const struct dpll_pin *dpll_pin,
877 				  void *pin_priv,
878 				  const struct dpll_device *dpll,
879 				  void *dpll_priv,
880 				  struct dpll_pin_esync *esync,
881 				  struct netlink_ext_ack *extack)
882 {
883 	struct zl3073x_dpll *zldpll = dpll_priv;
884 	struct zl3073x_dev *zldev = zldpll->dev;
885 	struct zl3073x_dpll_pin *pin = pin_priv;
886 	const struct zl3073x_synth *synth;
887 	const struct zl3073x_out *out;
888 	u32 synth_freq, out_freq;
889 	u8 out_id;
890 
891 	guard(mutex)(&zldpll->lock);
892 
893 	out_id = zl3073x_output_pin_out_get(pin->id);
894 	out = zl3073x_out_state_get(zldev, out_id);
895 
896 	/* If N-division is enabled, esync is not supported. The register used
897 	 * for N-division is also used for the esync divider so both cannot
898 	 * be used.
899 	 */
900 	if (zl3073x_out_is_ndiv(out))
901 		return -EOPNOTSUPP;
902 
903 	/* Get attached synth frequency */
904 	synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(out));
905 	synth_freq = zl3073x_synth_freq_get(synth);
906 	out_freq = synth_freq / out->div;
907 
908 	if (!pin->esync_control || out_freq <= 1)
909 		return -EOPNOTSUPP;
910 
911 	esync->range = esync_freq_ranges;
912 	esync->range_num = ARRAY_SIZE(esync_freq_ranges);
913 
914 	if (!zl3073x_out_esync_is_enabled(out)) {
915 		/* No need to read esync data if it is not enabled */
916 		esync->freq = 0;
917 		esync->pulse = 0;
918 
919 		return 0;
920 	}
921 
922 	/* Compute esync frequency */
923 	esync->freq = out_freq / out->esync_n_period;
924 
925 	/* By comparing the esync_pulse_width to the half of the pulse width
926 	 * the esync pulse percentage can be determined.
927 	 * Note that half pulse width is in units of half synth cycles, which
928 	 * is why it reduces down to be output_div.
929 	 */
930 	esync->pulse = div_u64(mul_u32_u32(50, out->esync_n_width), out->div);
931 
932 	return 0;
933 }
934 
935 static int
936 zl3073x_dpll_output_pin_esync_set(const struct dpll_pin *dpll_pin,
937 				  void *pin_priv,
938 				  const struct dpll_device *dpll,
939 				  void *dpll_priv, u64 freq,
940 				  struct netlink_ext_ack *extack)
941 {
942 	struct zl3073x_dpll *zldpll = dpll_priv;
943 	struct zl3073x_dev *zldev = zldpll->dev;
944 	struct zl3073x_dpll_pin *pin = pin_priv;
945 	struct zl3073x_dpll_pin *sibling = NULL;
946 	const struct zl3073x_synth *synth;
947 	struct zl3073x_out out;
948 	u32 synth_freq;
949 	u8 out_id;
950 	int rc;
951 
952 	mutex_lock(&zldpll->lock);
953 
954 	out_id = zl3073x_output_pin_out_get(pin->id);
955 	out = *zl3073x_out_state_get(zldev, out_id);
956 
957 	/* If N-division is enabled, esync is not supported. The register used
958 	 * for N-division is also used for the esync divider so both cannot
959 	 * be used.
960 	 */
961 	if (zl3073x_out_is_ndiv(&out)) {
962 		rc = -EOPNOTSUPP;
963 		goto unlock;
964 	}
965 
966 	if (!freq) {
967 		zl3073x_out_esync_disable(&out);
968 		goto commit;
969 	}
970 
971 	/* Get attached synth frequency */
972 	synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out));
973 	synth_freq = zl3073x_synth_freq_get(synth);
974 
975 	/* Enable 1PPS eSync for this pin frequency */
976 	zl3073x_out_esync_enable(&out, synth_freq / out.div);
977 commit:
978 	/* Commit output configuration */
979 	rc = zl3073x_out_state_set(zldev, out_id, &out);
980 	if (rc)
981 		goto unlock;
982 
983 	/* The clock type, esync period and esync width are all shared by
984 	 * both pins of the output pair, so the sibling pin's esync
985 	 * configuration changes too and userspace has to be notified.
986 	 */
987 	sibling = zl3073x_dpll_output_pin_sibling_get(pin);
988 unlock:
989 	mutex_unlock(&zldpll->lock);
990 
991 	if (!rc && sibling)
992 		__dpll_pin_change_ntf(sibling->dpll_pin);
993 
994 	return rc;
995 }
996 
997 static int
998 zl3073x_dpll_output_pin_frequency_get(const struct dpll_pin *dpll_pin,
999 				      void *pin_priv,
1000 				      const struct dpll_device *dpll,
1001 				      void *dpll_priv, u64 *frequency,
1002 				      struct netlink_ext_ack *extack)
1003 {
1004 	struct zl3073x_dpll *zldpll = dpll_priv;
1005 	struct zl3073x_dpll_pin *pin = pin_priv;
1006 
1007 	guard(mutex)(&zldpll->lock);
1008 
1009 	*frequency = zl3073x_dev_output_pin_freq_get(zldpll->dev, pin->id);
1010 
1011 	return 0;
1012 }
1013 
1014 static int
1015 zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin,
1016 				      void *pin_priv,
1017 				      const struct dpll_device *dpll,
1018 				      void *dpll_priv, u64 frequency,
1019 				      struct netlink_ext_ack *extack)
1020 {
1021 	struct zl3073x_dpll *zldpll = dpll_priv;
1022 	struct zl3073x_dev *zldev = zldpll->dev;
1023 	struct zl3073x_dpll_pin *pin = pin_priv;
1024 	struct zl3073x_dpll_pin *sibling = NULL;
1025 	const struct zl3073x_synth *synth;
1026 	u32 new_div, synth_freq;
1027 	struct zl3073x_out out;
1028 	u64 n_period, rem;
1029 	u8 out_id;
1030 	int rc;
1031 
1032 	mutex_lock(&zldpll->lock);
1033 
1034 	out_id = zl3073x_output_pin_out_get(pin->id);
1035 	out = *zl3073x_out_state_get(zldev, out_id);
1036 
1037 	/* Get attached synth frequency and compute new divisor */
1038 	synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out));
1039 	synth_freq = zl3073x_synth_freq_get(synth);
1040 	new_div = synth_freq / (u32)frequency;
1041 
1042 	/* Check signal format */
1043 	if (!zl3073x_out_is_ndiv(&out)) {
1044 		/* For non N-divided signal formats the frequency is computed
1045 		 * as division of synth frequency and output divisor, which
1046 		 * is shared by both pins of the output pair.
1047 		 */
1048 		out.div = new_div;
1049 
1050 		/* For 50/50 duty cycle the divisor is equal to width */
1051 		out.width = new_div;
1052 
1053 		/* The embedded sync period and width are computed relative to
1054 		 * the output carrier so they have to be adjusted to follow the
1055 		 * new frequency.
1056 		 */
1057 		if (zl3073x_out_esync_is_enabled(&out)) {
1058 			if (frequency == 1) {
1059 				/* eSync cannot work on a 1 Hz carrier */
1060 				zl3073x_out_esync_disable(&out);
1061 			} else {
1062 				/* Update esync period and width according to
1063 				 * the new frequency.
1064 				 */
1065 				zl3073x_out_esync_enable(&out,
1066 							 synth_freq / out.div);
1067 			}
1068 		}
1069 
1070 		/* Commit output configuration */
1071 		rc = zl3073x_out_state_set(zldev, out_id, &out);
1072 		if (rc)
1073 			goto unlock;
1074 
1075 		/* The other pin's frequency changed too - it has to be
1076 		 * notified about the change.
1077 		 */
1078 		sibling = zl3073x_dpll_output_pin_sibling_get(pin);
1079 
1080 		goto unlock;
1081 	}
1082 
1083 	if (zl3073x_dpll_is_p_pin(pin)) {
1084 		/* Keep the N-pin frequency: the new P-pin frequency has to be
1085 		 * a multiple of it, at least twice as high.
1086 		 */
1087 		n_period = mul_u32_u32(out.esync_n_period, out.div);
1088 		n_period = div64_u64_rem(n_period, new_div, &rem);
1089 
1090 		/* Update the output divisor */
1091 		out.div = new_div;
1092 
1093 		/* For 50/50 duty cycle the divisor is equal to width */
1094 		out.width = out.div;
1095 	} else {
1096 		/* The N-pin frequency has to divide the P-pin frequency and
1097 		 * be at most half of it.
1098 		 */
1099 		n_period = frequency * out.div;
1100 		n_period = div64_u64_rem(synth_freq, n_period, &rem);
1101 	}
1102 	if (n_period < 2) {
1103 		NL_SET_ERR_MSG_FMT(extack,
1104 				   "OUT%uN freq must be at most half of OUT%uP freq",
1105 				   out_id, out_id);
1106 		rc = -EINVAL;
1107 		goto unlock;
1108 	}
1109 	if (n_period > U32_MAX) {
1110 		NL_SET_ERR_MSG_FMT(extack,
1111 				   "OUT%uN freq is too low for OUT%uP freq",
1112 				   out_id, out_id);
1113 		rc = -EINVAL;
1114 		goto unlock;
1115 	}
1116 	if (rem != 0) {
1117 		NL_SET_ERR_MSG_FMT(extack,
1118 				   "OUT%uN freq must divide OUT%uP freq",
1119 				   out_id, out_id);
1120 		rc = -EINVAL;
1121 		goto unlock;
1122 	}
1123 	out.esync_n_period = n_period;
1124 
1125 	/* For 50/50 duty cycle the divisor is equal to width */
1126 	out.esync_n_width = out.esync_n_period;
1127 
1128 	/* Commit output configuration */
1129 	rc = zl3073x_out_state_set(zldev, out_id, &out);
1130 unlock:
1131 	mutex_unlock(&zldpll->lock);
1132 
1133 	if (!rc && sibling)
1134 		__dpll_pin_change_ntf(sibling->dpll_pin);
1135 
1136 	return rc;
1137 }
1138 
1139 static int
1140 zl3073x_dpll_output_pin_phase_adjust_get(const struct dpll_pin *dpll_pin,
1141 					 void *pin_priv,
1142 					 const struct dpll_device *dpll,
1143 					 void *dpll_priv,
1144 					 s32 *phase_adjust,
1145 					 struct netlink_ext_ack *extack)
1146 {
1147 	struct zl3073x_dpll *zldpll = dpll_priv;
1148 	struct zl3073x_dev *zldev = zldpll->dev;
1149 	struct zl3073x_dpll_pin *pin = pin_priv;
1150 	const struct zl3073x_out *out;
1151 	u8 out_id;
1152 
1153 	guard(mutex)(&zldpll->lock);
1154 
1155 	out_id = zl3073x_output_pin_out_get(pin->id);
1156 	out = zl3073x_out_state_get(zldev, out_id);
1157 
1158 	/* The value in the register is expressed in half synth clock cycles. */
1159 	*phase_adjust = out->phase_comp * pin->phase_gran;
1160 
1161 	return 0;
1162 }
1163 
1164 static int
1165 zl3073x_dpll_output_pin_phase_adjust_set(const struct dpll_pin *dpll_pin,
1166 					 void *pin_priv,
1167 					 const struct dpll_device *dpll,
1168 					 void *dpll_priv,
1169 					 s32 phase_adjust,
1170 					 struct netlink_ext_ack *extack)
1171 {
1172 	struct zl3073x_dpll *zldpll = dpll_priv;
1173 	struct zl3073x_dev *zldev = zldpll->dev;
1174 	struct zl3073x_dpll_pin *pin = pin_priv;
1175 	struct zl3073x_dpll_pin *sibling = NULL;
1176 	struct zl3073x_out out;
1177 	u8 out_id;
1178 	int rc;
1179 
1180 	mutex_lock(&zldpll->lock);
1181 
1182 	out_id = zl3073x_output_pin_out_get(pin->id);
1183 	out = *zl3073x_out_state_get(zldev, out_id);
1184 
1185 	/* The value in the register is expressed in half synth clock cycles. */
1186 	out.phase_comp = phase_adjust / pin->phase_gran;
1187 
1188 	/* Update output configuration from mailbox */
1189 	rc = zl3073x_out_state_set(zldev, out_id, &out);
1190 	if (rc)
1191 		goto unlock;
1192 
1193 	/* The phase compensation register is shared by both pins of the
1194 	 * output pair, so the sibling pin's phase adjustment changes too.
1195 	 */
1196 	sibling = zl3073x_dpll_output_pin_sibling_get(pin);
1197 unlock:
1198 	mutex_unlock(&zldpll->lock);
1199 
1200 	if (!rc && sibling)
1201 		__dpll_pin_change_ntf(sibling->dpll_pin);
1202 
1203 	return rc;
1204 }
1205 
1206 static int
1207 zl3073x_dpll_output_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin,
1208 					  void *pin_priv,
1209 					  const struct dpll_device *dpll,
1210 					  void *dpll_priv,
1211 					  enum dpll_pin_state *state,
1212 					  struct netlink_ext_ack *extack)
1213 {
1214 	/* If the output pin is registered then it is always connected */
1215 	*state = DPLL_PIN_STATE_CONNECTED;
1216 
1217 	return 0;
1218 }
1219 
1220 static int
1221 zl3073x_dpll_nco_pin_operstate_on_dpll_get(const struct dpll_pin *dpll_pin,
1222 					   void *pin_priv,
1223 					   const struct dpll_device *dpll,
1224 					   void *dpll_priv,
1225 					   enum dpll_pin_operstate *operstate,
1226 					   struct netlink_ext_ack *extack)
1227 {
1228 	struct zl3073x_dpll *zldpll = dpll_priv;
1229 	const struct zl3073x_chan *chan;
1230 
1231 	guard(mutex)(&zldpll->lock);
1232 
1233 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1234 	if (zl3073x_chan_mode_is_nco(chan))
1235 		*operstate = DPLL_PIN_OPERSTATE_ACTIVE;
1236 	else
1237 		*operstate = DPLL_PIN_OPERSTATE_STANDBY;
1238 
1239 	return 0;
1240 }
1241 
1242 static int
1243 zl3073x_dpll_nco_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin,
1244 				       void *pin_priv,
1245 				       const struct dpll_device *dpll,
1246 				       void *dpll_priv,
1247 				       enum dpll_pin_state *state,
1248 				       struct netlink_ext_ack *extack)
1249 {
1250 	struct zl3073x_dpll *zldpll = dpll_priv;
1251 	const struct zl3073x_chan *chan;
1252 
1253 	guard(mutex)(&zldpll->lock);
1254 
1255 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1256 	if (zl3073x_chan_mode_is_nco(chan))
1257 		*state = DPLL_PIN_STATE_CONNECTED;
1258 	else
1259 		*state = DPLL_PIN_STATE_DISCONNECTED;
1260 
1261 	return 0;
1262 }
1263 
1264 static int
1265 zl3073x_dpll_nco_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin,
1266 				       void *pin_priv,
1267 				       const struct dpll_device *dpll,
1268 				       void *dpll_priv,
1269 				       enum dpll_pin_state state,
1270 				       struct netlink_ext_ack *extack)
1271 {
1272 	struct zl3073x_dpll_pin *ref_pin = NULL;
1273 	struct zl3073x_dpll *zldpll = dpll_priv;
1274 	struct zl3073x_chan chan;
1275 	u8 ref;
1276 	int rc;
1277 
1278 	mutex_lock(&zldpll->lock);
1279 
1280 	chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1281 
1282 	switch (state) {
1283 	case DPLL_PIN_STATE_CONNECTED:
1284 		if (zl3073x_chan_mode_is_nco(&chan)) {
1285 			mutex_unlock(&zldpll->lock);
1286 			return 0;
1287 		}
1288 		if (zl3073x_chan_mode_is_auto(&chan)) {
1289 			NL_SET_ERR_MSG(extack,
1290 				       "NCO pin cannot be connected in automatic mode");
1291 			mutex_unlock(&zldpll->lock);
1292 			return -EINVAL;
1293 		}
1294 		if (zl3073x_chan_mode_is_reflock(&chan)) {
1295 			/* Get currently connected pin */
1296 			ref = zl3073x_chan_ref_get(&chan);
1297 			ref_pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref);
1298 		}
1299 		rc = zl3073x_chan_nco_mode_set(zldpll->dev, zldpll->id);
1300 		break;
1301 	case DPLL_PIN_STATE_DISCONNECTED:
1302 		if (!zl3073x_chan_mode_is_nco(&chan)) {
1303 			mutex_unlock(&zldpll->lock);
1304 			return 0;
1305 		}
1306 		/* Switch to freerun - holdover averaging was not
1307 		 * updated during NCO mode.
1308 		 */
1309 		zl3073x_chan_mode_set(&chan,
1310 				      ZL_DPLL_MODE_REFSEL_MODE_FREERUN);
1311 		rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan);
1312 		break;
1313 	default:
1314 		NL_SET_ERR_MSG(extack, "invalid pin state for NCO pin");
1315 		mutex_unlock(&zldpll->lock);
1316 		return -EINVAL;
1317 	}
1318 
1319 	mutex_unlock(&zldpll->lock);
1320 
1321 	if (!rc && ref_pin)
1322 		__dpll_pin_change_ntf(ref_pin->dpll_pin);
1323 
1324 	return rc;
1325 }
1326 
1327 static int
1328 zl3073x_dpll_nco_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv,
1329 			     const struct dpll_device *dpll, void *dpll_priv,
1330 			     struct dpll_ffo_param *ffo,
1331 			     struct netlink_ext_ack *extack)
1332 {
1333 	struct zl3073x_dpll *zldpll = dpll_priv;
1334 	const struct zl3073x_chan *chan;
1335 	s64 df_offset;
1336 
1337 	guard(mutex)(&zldpll->lock);
1338 
1339 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1340 	if (!zl3073x_chan_mode_is_nco(chan))
1341 		return -ENODATA;
1342 
1343 	/* Do not report FFO if a failure occurred during switching to NCO. */
1344 	df_offset = zl3073x_chan_df_offset_get(chan);
1345 	if (df_offset == ZL_DPLL_DF_OFFSET_UNKNOWN)
1346 		return -ENODATA;
1347 
1348 	/* dpll_df_offset register has inverted sign per datasheet:
1349 	 * f_offset = f_nom * (-df_offset) / 2^48
1350 	 * NCO pin reports DPLL output offset from nominal, so negate.
1351 	 * Convert to PPT: ppt = -df * 5^12 / 2^36
1352 	 */
1353 	ffo->ffo = -mul_s64_u64_shr(df_offset, 244140625, 36);
1354 
1355 	return 0;
1356 }
1357 
1358 static int
1359 zl3073x_dpll_temp_get(const struct dpll_device *dpll, void *dpll_priv,
1360 		      s32 *temp, struct netlink_ext_ack *extack)
1361 {
1362 	struct zl3073x_dpll *zldpll = dpll_priv;
1363 	struct zl3073x_dev *zldev = zldpll->dev;
1364 	u16 val;
1365 	int rc;
1366 
1367 	guard(mutex)(&zldpll->lock);
1368 
1369 	rc = zl3073x_read_u16(zldev, ZL_REG_DIE_TEMP_STATUS, &val);
1370 	if (rc)
1371 		return rc;
1372 
1373 	/* Register value is in units of 0.1 C, convert to millidegrees */
1374 	*temp = (s16)val * 100;
1375 
1376 	return 0;
1377 }
1378 
1379 static int
1380 __zl3073x_dpll_lock_status_get(struct zl3073x_dpll *zldpll,
1381 			       enum dpll_lock_status *status)
1382 {
1383 	const struct zl3073x_chan *chan;
1384 
1385 	lockdep_assert_held(&zldpll->lock);
1386 
1387 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1388 
1389 	switch (zl3073x_chan_mode_get(chan)) {
1390 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
1391 	case ZL_DPLL_MODE_REFSEL_MODE_NCO:
1392 		/* In FREERUN and NCO modes the DPLL is always unlocked */
1393 		*status = DPLL_LOCK_STATUS_UNLOCKED;
1394 
1395 		return 0;
1396 	default:
1397 		break;
1398 	}
1399 
1400 	switch (zl3073x_chan_lock_state_get(chan)) {
1401 	case ZL_DPLL_MON_STATUS_STATE_LOCK:
1402 		if (zl3073x_chan_is_ho_ready(chan))
1403 			*status = DPLL_LOCK_STATUS_LOCKED_HO_ACQ;
1404 		else
1405 			*status = DPLL_LOCK_STATUS_LOCKED;
1406 		break;
1407 	case ZL_DPLL_MON_STATUS_STATE_HOLDOVER:
1408 	case ZL_DPLL_MON_STATUS_STATE_ACQUIRING:
1409 		*status = DPLL_LOCK_STATUS_HOLDOVER;
1410 		break;
1411 	default:
1412 		dev_warn(zldpll->dev->dev,
1413 			 "Unknown DPLL monitor status: 0x%02x\n",
1414 			 chan->mon_status);
1415 		*status = DPLL_LOCK_STATUS_UNLOCKED;
1416 		break;
1417 	}
1418 
1419 	return 0;
1420 }
1421 
1422 static int
1423 zl3073x_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
1424 			     enum dpll_lock_status *status,
1425 			     enum dpll_lock_status_error *status_error,
1426 			     struct netlink_ext_ack *extack)
1427 {
1428 	struct zl3073x_dpll *zldpll = dpll_priv;
1429 
1430 	guard(mutex)(&zldpll->lock);
1431 
1432 	return __zl3073x_dpll_lock_status_get(zldpll, status);
1433 }
1434 
1435 static int
1436 zl3073x_dpll_supported_modes_get(const struct dpll_device *dpll,
1437 				 void *dpll_priv, unsigned long *modes,
1438 				 struct netlink_ext_ack *extack)
1439 {
1440 	__set_bit(DPLL_MODE_AUTOMATIC, modes);
1441 	__set_bit(DPLL_MODE_MANUAL, modes);
1442 
1443 	return 0;
1444 }
1445 
1446 static int
1447 zl3073x_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
1448 		      enum dpll_mode *mode, struct netlink_ext_ack *extack)
1449 {
1450 	struct zl3073x_dpll *zldpll = dpll_priv;
1451 	const struct zl3073x_chan *chan;
1452 
1453 	guard(mutex)(&zldpll->lock);
1454 
1455 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1456 
1457 	switch (zl3073x_chan_mode_get(chan)) {
1458 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
1459 	case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER:
1460 	case ZL_DPLL_MODE_REFSEL_MODE_NCO:
1461 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
1462 		/* Use MANUAL for device FREERUN, HOLDOVER, NCO and
1463 		 * REFLOCK modes
1464 		 */
1465 		*mode = DPLL_MODE_MANUAL;
1466 		break;
1467 	case ZL_DPLL_MODE_REFSEL_MODE_AUTO:
1468 		/* Use AUTO for device AUTO mode */
1469 		*mode = DPLL_MODE_AUTOMATIC;
1470 		break;
1471 	default:
1472 		return -EINVAL;
1473 	}
1474 
1475 	return 0;
1476 }
1477 
1478 static int
1479 zl3073x_dpll_phase_offset_avg_factor_get(const struct dpll_device *dpll,
1480 					 void *dpll_priv, u32 *factor,
1481 					 struct netlink_ext_ack *extack)
1482 {
1483 	struct zl3073x_dpll *zldpll = dpll_priv;
1484 
1485 	*factor = zl3073x_dev_phase_avg_factor_get(zldpll->dev);
1486 
1487 	return 0;
1488 }
1489 
1490 static int
1491 zl3073x_dpll_phase_offset_avg_factor_set(const struct dpll_device *dpll,
1492 					 void *dpll_priv, u32 factor,
1493 					 struct netlink_ext_ack *extack)
1494 {
1495 	struct zl3073x_dpll *item, *zldpll = dpll_priv;
1496 	int rc;
1497 
1498 	if (factor > 15) {
1499 		NL_SET_ERR_MSG_FMT(extack,
1500 				   "Phase offset average factor has to be from range <0,15>");
1501 		return -EINVAL;
1502 	}
1503 
1504 	rc = zl3073x_dev_phase_avg_factor_set(zldpll->dev, factor);
1505 	if (rc) {
1506 		NL_SET_ERR_MSG_FMT(extack,
1507 				   "Failed to set phase offset averaging factor");
1508 		return rc;
1509 	}
1510 
1511 	/* The averaging factor is common for all DPLL channels so after
1512 	 * change we have to send a notification for other DPLL devices.
1513 	 */
1514 	list_for_each_entry(item, &zldpll->dev->dplls, list) {
1515 		struct dpll_device *dpll_dev = READ_ONCE(item->dpll_dev);
1516 
1517 		if (item != zldpll && dpll_dev)
1518 			__dpll_device_change_ntf(dpll_dev);
1519 	}
1520 
1521 	return 0;
1522 }
1523 
1524 static int
1525 zl3073x_dpll_mode_set(const struct dpll_device *dpll, void *dpll_priv,
1526 		      enum dpll_mode mode, struct netlink_ext_ack *extack)
1527 {
1528 	struct zl3073x_dpll *zldpll = dpll_priv;
1529 	struct zl3073x_dpll_pin *nco_pin = NULL;
1530 	struct zl3073x_chan chan;
1531 	u8 hw_mode, ref;
1532 	int rc;
1533 
1534 	mutex_lock(&zldpll->lock);
1535 
1536 	chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1537 	ref = zl3073x_chan_refsel_ref_get(&chan);
1538 
1539 	if (mode == DPLL_MODE_MANUAL) {
1540 		/* We are switching from automatic to manual mode:
1541 		 * - if we have a valid reference selected during auto mode then
1542 		 *   we will switch to forced reference lock mode and use this
1543 		 *   reference for selection
1544 		 * - if NO valid reference is selected, we will switch to forced
1545 		 *   holdover mode or freerun mode, depending on the current
1546 		 *   lock status
1547 		 */
1548 		if (ZL3073X_DPLL_REF_IS_VALID(ref))
1549 			hw_mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK;
1550 		else if (zldpll->lock_status == DPLL_LOCK_STATUS_UNLOCKED)
1551 			hw_mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN;
1552 		else
1553 			hw_mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER;
1554 	} else {
1555 		if (zl3073x_chan_mode_is_nco(&chan))
1556 			nco_pin = zl3073x_dpll_nco_pin_get(zldpll);
1557 
1558 		/* We are switching from manual to automatic mode:
1559 		 * - if there is a valid reference selected then ensure that
1560 		 *   it is selectable after switch to automatic mode
1561 		 * - switch to automatic mode
1562 		 */
1563 		if (ZL3073X_DPLL_REF_IS_VALID(ref) &&
1564 		    !zl3073x_chan_ref_is_selectable(&chan, ref)) {
1565 			struct zl3073x_dpll_pin *pin;
1566 
1567 			pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref);
1568 			if (pin) {
1569 				/* Restore pin priority in HW */
1570 				zl3073x_chan_ref_prio_set(&chan, ref,
1571 							  pin->prio);
1572 			}
1573 		}
1574 
1575 		hw_mode = ZL_DPLL_MODE_REFSEL_MODE_AUTO;
1576 	}
1577 
1578 	zl3073x_chan_mode_set(&chan, hw_mode);
1579 	if (ZL3073X_DPLL_REF_IS_VALID(ref))
1580 		zl3073x_chan_ref_set(&chan, ref);
1581 
1582 	rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan);
1583 	if (rc) {
1584 		NL_SET_ERR_MSG_MOD(extack,
1585 				   "failed to set reference selection mode");
1586 		mutex_unlock(&zldpll->lock);
1587 		return rc;
1588 	}
1589 
1590 	mutex_unlock(&zldpll->lock);
1591 
1592 	/* If leaving NCO mode, notify userspace about the NCO pin
1593 	 * state change - the periodic worker skips the NCO pin.
1594 	 */
1595 	if (nco_pin)
1596 		__dpll_pin_change_ntf(nco_pin->dpll_pin);
1597 
1598 	return 0;
1599 }
1600 
1601 static int
1602 zl3073x_dpll_phase_offset_monitor_get(const struct dpll_device *dpll,
1603 				      void *dpll_priv,
1604 				      enum dpll_feature_state *state,
1605 				      struct netlink_ext_ack *extack)
1606 {
1607 	struct zl3073x_dpll *zldpll = dpll_priv;
1608 
1609 	guard(mutex)(&zldpll->lock);
1610 
1611 	if (zldpll->phase_monitor)
1612 		*state = DPLL_FEATURE_STATE_ENABLE;
1613 	else
1614 		*state = DPLL_FEATURE_STATE_DISABLE;
1615 
1616 	return 0;
1617 }
1618 
1619 static int
1620 zl3073x_dpll_phase_offset_monitor_set(const struct dpll_device *dpll,
1621 				      void *dpll_priv,
1622 				      enum dpll_feature_state state,
1623 				      struct netlink_ext_ack *extack)
1624 {
1625 	struct zl3073x_dpll *zldpll = dpll_priv;
1626 
1627 	guard(mutex)(&zldpll->lock);
1628 
1629 	zldpll->phase_monitor = (state == DPLL_FEATURE_STATE_ENABLE);
1630 
1631 	return 0;
1632 }
1633 
1634 static int
1635 zl3073x_dpll_freq_monitor_get(const struct dpll_device *dpll,
1636 			      void *dpll_priv,
1637 			      enum dpll_feature_state *state,
1638 			      struct netlink_ext_ack *extack)
1639 {
1640 	struct zl3073x_dpll *zldpll = dpll_priv;
1641 
1642 	if (READ_ONCE(zldpll->dev->freq_monitor))
1643 		*state = DPLL_FEATURE_STATE_ENABLE;
1644 	else
1645 		*state = DPLL_FEATURE_STATE_DISABLE;
1646 
1647 	return 0;
1648 }
1649 
1650 static int
1651 zl3073x_dpll_freq_monitor_set(const struct dpll_device *dpll,
1652 			      void *dpll_priv,
1653 			      enum dpll_feature_state state,
1654 			      struct netlink_ext_ack *extack)
1655 {
1656 	struct zl3073x_dpll *item, *zldpll = dpll_priv;
1657 	struct zl3073x_dev *zldev = zldpll->dev;
1658 
1659 	WRITE_ONCE(zldev->freq_monitor, state == DPLL_FEATURE_STATE_ENABLE);
1660 
1661 	/* The frequency monitoring is common for all DPLL channels so after
1662 	 * change we have to send a notification for other DPLL devices.
1663 	 */
1664 	list_for_each_entry(item, &zldev->dplls, list) {
1665 		struct dpll_device *dpll_dev = READ_ONCE(item->dpll_dev);
1666 
1667 		if (item != zldpll && dpll_dev)
1668 			__dpll_device_change_ntf(dpll_dev);
1669 	}
1670 
1671 	return 0;
1672 }
1673 
1674 static const struct dpll_pin_ops zl3073x_dpll_input_pin_ops = {
1675 	.supported_ffo = BIT(DPLL_FFO_PIN_DEVICE),
1676 	.direction_get = zl3073x_dpll_pin_direction_get,
1677 	.esync_get = zl3073x_dpll_input_pin_esync_get,
1678 	.esync_set = zl3073x_dpll_input_pin_esync_set,
1679 	.ffo_get = zl3073x_dpll_input_pin_ffo_get,
1680 	.frequency_get = zl3073x_dpll_input_pin_frequency_get,
1681 	.frequency_set = zl3073x_dpll_input_pin_frequency_set,
1682 	.measured_freq_get = zl3073x_dpll_input_pin_measured_freq_get,
1683 	.operstate_on_dpll_get = zl3073x_dpll_input_pin_operstate_on_dpll_get,
1684 	.phase_offset_get = zl3073x_dpll_input_pin_phase_offset_get,
1685 	.phase_adjust_get = zl3073x_dpll_input_pin_phase_adjust_get,
1686 	.phase_adjust_set = zl3073x_dpll_input_pin_phase_adjust_set,
1687 	.prio_get = zl3073x_dpll_input_pin_prio_get,
1688 	.prio_set = zl3073x_dpll_input_pin_prio_set,
1689 	.ref_sync_get = zl3073x_dpll_input_pin_ref_sync_get,
1690 	.ref_sync_set = zl3073x_dpll_input_pin_ref_sync_set,
1691 	.state_on_dpll_get = zl3073x_dpll_input_pin_state_on_dpll_get,
1692 	.state_on_dpll_set = zl3073x_dpll_input_pin_state_on_dpll_set,
1693 };
1694 
1695 static const struct dpll_pin_ops zl3073x_dpll_output_pin_ops = {
1696 	.direction_get = zl3073x_dpll_pin_direction_get,
1697 	.esync_get = zl3073x_dpll_output_pin_esync_get,
1698 	.esync_set = zl3073x_dpll_output_pin_esync_set,
1699 	.frequency_get = zl3073x_dpll_output_pin_frequency_get,
1700 	.frequency_set = zl3073x_dpll_output_pin_frequency_set,
1701 	.phase_adjust_get = zl3073x_dpll_output_pin_phase_adjust_get,
1702 	.phase_adjust_set = zl3073x_dpll_output_pin_phase_adjust_set,
1703 	.state_on_dpll_get = zl3073x_dpll_output_pin_state_on_dpll_get,
1704 };
1705 
1706 static const struct dpll_pin_ops zl3073x_dpll_nco_pin_ops = {
1707 	.supported_ffo = BIT(DPLL_FFO_PIN_DEVICE),
1708 	.direction_get = zl3073x_dpll_pin_direction_get,
1709 	.ffo_get = zl3073x_dpll_nco_pin_ffo_get,
1710 	.operstate_on_dpll_get = zl3073x_dpll_nco_pin_operstate_on_dpll_get,
1711 	.state_on_dpll_get = zl3073x_dpll_nco_pin_state_on_dpll_get,
1712 	.state_on_dpll_set = zl3073x_dpll_nco_pin_state_on_dpll_set,
1713 };
1714 
1715 static const struct dpll_device_ops zl3073x_dpll_device_ops = {
1716 	.lock_status_get = zl3073x_dpll_lock_status_get,
1717 	.mode_get = zl3073x_dpll_mode_get,
1718 	.mode_set = zl3073x_dpll_mode_set,
1719 	.phase_offset_avg_factor_get = zl3073x_dpll_phase_offset_avg_factor_get,
1720 	.phase_offset_avg_factor_set = zl3073x_dpll_phase_offset_avg_factor_set,
1721 	.phase_offset_monitor_get = zl3073x_dpll_phase_offset_monitor_get,
1722 	.phase_offset_monitor_set = zl3073x_dpll_phase_offset_monitor_set,
1723 	.freq_monitor_get = zl3073x_dpll_freq_monitor_get,
1724 	.freq_monitor_set = zl3073x_dpll_freq_monitor_set,
1725 	.supported_modes_get = zl3073x_dpll_supported_modes_get,
1726 };
1727 
1728 /**
1729  * zl3073x_dpll_pin_alloc - allocate DPLL pin
1730  * @zldpll: pointer to zl3073x_dpll
1731  * @dir: pin direction
1732  * @id: pin id
1733  *
1734  * Allocates and initializes zl3073x_dpll_pin structure for given
1735  * pin id and direction.
1736  *
1737  * Return: pointer to allocated structure on success, error pointer on error
1738  */
1739 static struct zl3073x_dpll_pin *
1740 zl3073x_dpll_pin_alloc(struct zl3073x_dpll *zldpll, enum dpll_pin_direction dir,
1741 		       u8 id)
1742 {
1743 	struct zl3073x_dpll_pin *pin;
1744 
1745 	pin = kzalloc_obj(*pin);
1746 	if (!pin)
1747 		return ERR_PTR(-ENOMEM);
1748 
1749 	pin->dpll = zldpll;
1750 	pin->dir = dir;
1751 	pin->id = id;
1752 
1753 	return pin;
1754 }
1755 
1756 /**
1757  * zl3073x_dpll_pin_free - deallocate DPLL pin
1758  * @pin: pin to free
1759  *
1760  * Deallocates DPLL pin previously allocated by @zl3073x_dpll_pin_alloc.
1761  */
1762 static void
1763 zl3073x_dpll_pin_free(struct zl3073x_dpll_pin *pin)
1764 {
1765 	WARN(pin->dpll_pin, "DPLL pin is still registered\n");
1766 
1767 	kfree(pin);
1768 }
1769 
1770 /**
1771  * zl3073x_dpll_pin_register - register DPLL pin
1772  * @pin: pointer to DPLL pin
1773  * @index: absolute pin index for registration
1774  *
1775  * Registers given DPLL pin into DPLL sub-system.
1776  *
1777  * Return: 0 on success, <0 on error
1778  */
1779 static int
1780 zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index)
1781 {
1782 	struct zl3073x_dpll *zldpll = pin->dpll;
1783 	struct zl3073x_pin_props *props;
1784 	const struct dpll_pin_ops *ops;
1785 	int rc;
1786 
1787 	/* Get pin properties */
1788 	props = zl3073x_pin_props_get(zldpll->dev, pin->dir, pin->id);
1789 	if (IS_ERR(props))
1790 		return PTR_ERR(props);
1791 
1792 	/* Save package label, fwnode, esync capability and phase adjust
1793 	 * granularity.
1794 	 */
1795 	strscpy(pin->label, props->package_label);
1796 	pin->fwnode = fwnode_handle_get(props->fwnode);
1797 	pin->esync_control = props->esync_control;
1798 	pin->phase_gran = props->dpll_props.phase_gran;
1799 
1800 	if (zl3073x_dpll_is_input_pin(pin)) {
1801 		const struct zl3073x_chan *chan;
1802 		u8 ref;
1803 
1804 		chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
1805 		ref = zl3073x_input_pin_ref_get(pin->id);
1806 		pin->prio = zl3073x_chan_ref_prio_get(chan, ref);
1807 
1808 		if (pin->prio == ZL_DPLL_REF_PRIO_NONE)
1809 			/* Clamp prio to max value */
1810 			pin->prio = ZL_DPLL_REF_PRIO_MAX;
1811 	}
1812 
1813 	/* Create or get existing DPLL pin */
1814 	pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, index, THIS_MODULE,
1815 				     &props->dpll_props, &pin->tracker);
1816 	if (IS_ERR(pin->dpll_pin)) {
1817 		rc = PTR_ERR(pin->dpll_pin);
1818 		goto err_pin_get;
1819 	}
1820 	dpll_pin_fwnode_set(pin->dpll_pin, props->fwnode);
1821 
1822 	if (zl3073x_dpll_is_input_pin(pin))
1823 		ops = &zl3073x_dpll_input_pin_ops;
1824 	else
1825 		ops = &zl3073x_dpll_output_pin_ops;
1826 
1827 	/* Add the pin to the list before registering it with the DPLL core so
1828 	 * that it is findable as a sibling as soon as the core publishes it.
1829 	 */
1830 	mutex_lock(&zldpll->lock);
1831 	list_add(&pin->list, &zldpll->pins);
1832 	mutex_unlock(&zldpll->lock);
1833 
1834 	/* Register the pin */
1835 	rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin, ops, pin);
1836 	if (rc)
1837 		goto err_register;
1838 
1839 	/* Free pin properties */
1840 	zl3073x_pin_props_put(props);
1841 
1842 	return 0;
1843 
1844 err_register:
1845 	mutex_lock(&zldpll->lock);
1846 	list_del(&pin->list);
1847 	mutex_unlock(&zldpll->lock);
1848 	dpll_pin_put(pin->dpll_pin, &pin->tracker);
1849 err_pin_get:
1850 	pin->dpll_pin = NULL;
1851 	fwnode_handle_put(pin->fwnode);
1852 	pin->fwnode = NULL;
1853 	zl3073x_pin_props_put(props);
1854 
1855 	return rc;
1856 }
1857 
1858 /**
1859  * zl3073x_dpll_pin_unregister - unregister DPLL pin
1860  * @pin: pointer to DPLL pin
1861  *
1862  * Unregisters pin previously registered by @zl3073x_dpll_pin_register.
1863  */
1864 static void
1865 zl3073x_dpll_pin_unregister(struct zl3073x_dpll_pin *pin)
1866 {
1867 	struct zl3073x_dpll *zldpll = pin->dpll;
1868 	const struct dpll_pin_ops *ops;
1869 
1870 	WARN(!pin->dpll_pin, "DPLL pin is not registered\n");
1871 
1872 	if (zl3073x_dpll_is_nco_pin(pin))
1873 		ops = &zl3073x_dpll_nco_pin_ops;
1874 	else if (zl3073x_dpll_is_input_pin(pin))
1875 		ops = &zl3073x_dpll_input_pin_ops;
1876 	else
1877 		ops = &zl3073x_dpll_output_pin_ops;
1878 
1879 	/* Unregister the pin */
1880 	dpll_pin_unregister(zldpll->dpll_dev, pin->dpll_pin, ops, pin);
1881 
1882 	/* Remove the pin from the list only after it has been unregistered so
1883 	 * that a still-registered pin is always findable as a sibling.
1884 	 */
1885 	mutex_lock(&zldpll->lock);
1886 	list_del(&pin->list);
1887 	mutex_unlock(&zldpll->lock);
1888 
1889 	dpll_pin_put(pin->dpll_pin, &pin->tracker);
1890 	pin->dpll_pin = NULL;
1891 
1892 	fwnode_handle_put(pin->fwnode);
1893 	pin->fwnode = NULL;
1894 }
1895 
1896 /**
1897  * zl3073x_dpll_pins_unregister - unregister all registered DPLL pins
1898  * @zldpll: pointer to zl3073x_dpll structure
1899  *
1900  * Enumerates all DPLL pins registered to given DPLL device and
1901  * unregisters them.
1902  */
1903 static void
1904 zl3073x_dpll_pins_unregister(struct zl3073x_dpll *zldpll)
1905 {
1906 	struct zl3073x_dpll_pin *pin, *next;
1907 
1908 	/* Unregister each pin before removing it from the list so that a
1909 	 * still-registered pin is always findable as a sibling.
1910 	 */
1911 	list_for_each_entry_safe(pin, next, &zldpll->pins, list) {
1912 		zl3073x_dpll_pin_unregister(pin);
1913 		zl3073x_dpll_pin_free(pin);
1914 	}
1915 }
1916 
1917 /**
1918  * zl3073x_dpll_pin_is_registrable - check if the pin is registrable
1919  * @zldpll: pointer to zl3073x_dpll structure
1920  * @dir: pin direction
1921  * @index: pin index
1922  *
1923  * Checks if the given pin can be registered to given DPLL. For both
1924  * directions the pin can be registered if it is enabled. In case of
1925  * differential signal type only P-pin is reported as registrable.
1926  * And additionally for the output pin, the pin can be registered only
1927  * if it is connected to synthesizer that is driven by given DPLL.
1928  *
1929  * Return: true if the pin is registrable, false if not
1930  */
1931 static bool
1932 zl3073x_dpll_pin_is_registrable(struct zl3073x_dpll *zldpll,
1933 				enum dpll_pin_direction dir, u8 index)
1934 {
1935 	struct zl3073x_dev *zldev = zldpll->dev;
1936 	bool is_diff, is_enabled;
1937 	const char *name;
1938 
1939 	if (dir == DPLL_PIN_DIRECTION_INPUT) {
1940 		u8 ref_id = zl3073x_input_pin_ref_get(index);
1941 		const struct zl3073x_ref *ref;
1942 
1943 		name = "REF";
1944 		ref = zl3073x_ref_state_get(zldev, ref_id);
1945 		is_diff = zl3073x_ref_is_diff(ref);
1946 		is_enabled = zl3073x_ref_is_enabled(ref);
1947 	} else {
1948 		/* Output P&N pair shares single HW output */
1949 		u8 out = zl3073x_output_pin_out_get(index);
1950 
1951 		/* Skip the pin if it is connected to different DPLL channel */
1952 		if (zl3073x_dev_out_dpll_get(zldev, out) != zldpll->id) {
1953 			dev_dbg(zldev->dev,
1954 				"OUT%u is driven by different DPLL\n", out);
1955 
1956 			return false;
1957 		}
1958 
1959 		name = "OUT";
1960 		is_diff = zl3073x_dev_out_is_diff(zldev, out);
1961 		is_enabled = zl3073x_dev_output_pin_is_enabled(zldev, index);
1962 	}
1963 
1964 	/* Skip N-pin if the corresponding input/output is differential */
1965 	if (is_diff && zl3073x_is_n_pin(index)) {
1966 		dev_dbg(zldev->dev, "%s%u is differential, skipping N-pin\n",
1967 			name, index / 2);
1968 
1969 		return false;
1970 	}
1971 
1972 	/* Skip the pin if it is disabled */
1973 	if (!is_enabled) {
1974 		dev_dbg(zldev->dev, "%s%u%c is disabled\n", name, index / 2,
1975 			zl3073x_is_p_pin(index) ? 'P' : 'N');
1976 
1977 		return false;
1978 	}
1979 
1980 	return true;
1981 }
1982 
1983 static const struct dpll_pin_properties zl3073x_dpll_nco_pin_props = {
1984 	.type = DPLL_PIN_TYPE_INT_NCO,
1985 	.package_label = "NCO",
1986 	.capabilities = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE,
1987 };
1988 
1989 static int
1990 zl3073x_dpll_nco_pin_register(struct zl3073x_dpll *zldpll)
1991 {
1992 	struct zl3073x_dpll_pin *pin;
1993 	struct zl3073x_chan chan;
1994 	int rc;
1995 
1996 	/* Ensure that ctrl bits are configured for NCO operation:
1997 	 * - nco_auto_read: auto-capture tracking offset on NCO entry
1998 	 * - tod_step_reset: apply negated ToD step on NCO exit
1999 	 * - tie_clear: PPS DPLLs re-align outputs on NCO exit
2000 	 */
2001 	mutex_lock(&zldpll->lock);
2002 	chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id);
2003 	FIELD_MODIFY(ZL_DPLL_CTRL_NCO_AUTO_READ, &chan.ctrl, 1);
2004 	FIELD_MODIFY(ZL_DPLL_CTRL_TOD_STEP_RST, &chan.ctrl, 1);
2005 	FIELD_MODIFY(ZL_DPLL_CTRL_TIE_CLEAR, &chan.ctrl,
2006 		     zldpll->type == DPLL_TYPE_PPS ? 1 : 0);
2007 	rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan);
2008 	mutex_unlock(&zldpll->lock);
2009 	if (rc)
2010 		return rc;
2011 
2012 	pin = zl3073x_dpll_pin_alloc(zldpll, DPLL_PIN_DIRECTION_INPUT,
2013 				     ZL3073X_NCO_PIN_ID);
2014 	if (IS_ERR(pin))
2015 		return PTR_ERR(pin);
2016 
2017 	pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, ZL3073X_NCO_PIN_ID,
2018 				     THIS_MODULE, &zl3073x_dpll_nco_pin_props,
2019 				     &pin->tracker);
2020 	if (IS_ERR(pin->dpll_pin)) {
2021 		rc = PTR_ERR(pin->dpll_pin);
2022 		goto err_pin_get;
2023 	}
2024 
2025 	/* Add the pin to the list before registering it with the DPLL core so
2026 	 * that a registered pin is always on the list.
2027 	 */
2028 	mutex_lock(&zldpll->lock);
2029 	list_add(&pin->list, &zldpll->pins);
2030 	mutex_unlock(&zldpll->lock);
2031 
2032 	rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin,
2033 			       &zl3073x_dpll_nco_pin_ops, pin);
2034 	if (rc)
2035 		goto err_register;
2036 
2037 	return 0;
2038 
2039 err_register:
2040 	mutex_lock(&zldpll->lock);
2041 	list_del(&pin->list);
2042 	mutex_unlock(&zldpll->lock);
2043 	dpll_pin_put(pin->dpll_pin, &pin->tracker);
2044 err_pin_get:
2045 	pin->dpll_pin = NULL;
2046 	kfree(pin);
2047 
2048 	return rc;
2049 }
2050 
2051 /**
2052  * zl3073x_dpll_pins_register - register all registerable DPLL pins
2053  * @zldpll: pointer to zl3073x_dpll structure
2054  *
2055  * Enumerates all possible input/output pins and registers all of them
2056  * that are registrable.
2057  *
2058  * Return: 0 on success, <0 on error
2059  */
2060 static int
2061 zl3073x_dpll_pins_register(struct zl3073x_dpll *zldpll)
2062 {
2063 	struct zl3073x_dpll_pin *pin;
2064 	enum dpll_pin_direction dir;
2065 	u8 id, index;
2066 	int rc;
2067 
2068 	/* Process input pins */
2069 	for (index = 0; index < ZL3073X_NUM_PINS; index++) {
2070 		/* First input pins and then output pins */
2071 		if (index < ZL3073X_NUM_INPUT_PINS) {
2072 			id = index;
2073 			dir = DPLL_PIN_DIRECTION_INPUT;
2074 		} else {
2075 			id = index - ZL3073X_NUM_INPUT_PINS;
2076 			dir = DPLL_PIN_DIRECTION_OUTPUT;
2077 		}
2078 
2079 		/* Check if the pin registrable to this DPLL */
2080 		if (!zl3073x_dpll_pin_is_registrable(zldpll, dir, id))
2081 			continue;
2082 
2083 		pin = zl3073x_dpll_pin_alloc(zldpll, dir, id);
2084 		if (IS_ERR(pin)) {
2085 			rc = PTR_ERR(pin);
2086 			goto error;
2087 		}
2088 
2089 		rc = zl3073x_dpll_pin_register(pin, index);
2090 		if (rc) {
2091 			zl3073x_dpll_pin_free(pin);
2092 			goto error;
2093 		}
2094 	}
2095 
2096 	/* Register NCO virtual input pin */
2097 	rc = zl3073x_dpll_nco_pin_register(zldpll);
2098 	if (rc)
2099 		goto error;
2100 
2101 	return 0;
2102 
2103 error:
2104 	zl3073x_dpll_pins_unregister(zldpll);
2105 
2106 	return rc;
2107 }
2108 
2109 /**
2110  * zl3073x_dpll_device_register - register DPLL device
2111  * @zldpll: pointer to zl3073x_dpll structure
2112  *
2113  * Registers given DPLL device into DPLL sub-system.
2114  *
2115  * Return: 0 on success, <0 on error
2116  */
2117 static int
2118 zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll)
2119 {
2120 	struct zl3073x_dev *zldev = zldpll->dev;
2121 	int rc;
2122 
2123 	zldpll->ops = zl3073x_dpll_device_ops;
2124 	if (zldev->info->flags & ZL3073X_FLAG_DIE_TEMP)
2125 		zldpll->ops.temp_get = zl3073x_dpll_temp_get;
2126 
2127 	zldpll->dpll_dev = dpll_device_get(zldev->clock_id, zldpll->id,
2128 					   THIS_MODULE, &zldpll->tracker);
2129 	if (IS_ERR(zldpll->dpll_dev)) {
2130 		rc = PTR_ERR(zldpll->dpll_dev);
2131 		zldpll->dpll_dev = NULL;
2132 
2133 		return rc;
2134 	}
2135 
2136 	zldpll->type = zl3073x_prop_dpll_type_get(zldev, zldpll->id);
2137 	rc = dpll_device_register(zldpll->dpll_dev, zldpll->type,
2138 				  &zldpll->ops, zldpll);
2139 	if (rc) {
2140 		dpll_device_put(zldpll->dpll_dev, &zldpll->tracker);
2141 		zldpll->dpll_dev = NULL;
2142 	}
2143 
2144 	return rc;
2145 }
2146 
2147 /**
2148  * zl3073x_dpll_device_unregister - unregister DPLL device
2149  * @zldpll: pointer to zl3073x_dpll structure
2150  *
2151  * Unregisters given DPLL device from DPLL sub-system previously registered
2152  * by @zl3073x_dpll_device_register.
2153  */
2154 static void
2155 zl3073x_dpll_device_unregister(struct zl3073x_dpll *zldpll)
2156 {
2157 	struct dpll_device *dpll_dev = READ_ONCE(zldpll->dpll_dev);
2158 
2159 	WARN(!dpll_dev, "DPLL device is not registered\n");
2160 
2161 	WRITE_ONCE(zldpll->dpll_dev, NULL);
2162 	dpll_device_unregister(dpll_dev, &zldpll->ops, zldpll);
2163 	dpll_device_put(dpll_dev, &zldpll->tracker);
2164 }
2165 
2166 /**
2167  * zl3073x_dpll_pin_phase_offset_check - check for pin phase offset change
2168  * @pin: pin to check
2169  *
2170  * Check for the change of DPLL to connected pin phase offset change.
2171  *
2172  * Return: true on phase offset change, false otherwise
2173  */
2174 static bool
2175 zl3073x_dpll_pin_phase_offset_check(struct zl3073x_dpll_pin *pin)
2176 {
2177 	struct zl3073x_dpll *zldpll = pin->dpll;
2178 	struct zl3073x_dev *zldev = zldpll->dev;
2179 	unsigned int reg;
2180 	s64 phase_offset;
2181 	u8 ref_id;
2182 	int rc;
2183 
2184 	lockdep_assert_held(&zldpll->lock);
2185 
2186 	/* No phase offset if the ref monitor reports signal errors */
2187 	ref_id = zl3073x_input_pin_ref_get(pin->id);
2188 	if (!zl3073x_dev_ref_is_status_ok(zldev, ref_id))
2189 		return false;
2190 
2191 	/* Select register to read phase offset value depending on pin and
2192 	 * phase monitor state:
2193 	 * 1) For connected pin use dpll_phase_err_data register
2194 	 * 2) For other pins use appropriate ref_phase register if the phase
2195 	 *    monitor feature is enabled.
2196 	 */
2197 	if (pin->operstate == DPLL_PIN_OPERSTATE_ACTIVE)
2198 		reg = ZL_REG_DPLL_PHASE_ERR_DATA(zldpll->id);
2199 	else if (zldpll->phase_monitor)
2200 		reg = ZL_REG_REF_PHASE(ref_id);
2201 	else
2202 		return false;
2203 
2204 	/* Read measured phase offset value */
2205 	rc = zl3073x_read_u48(zldev, reg, &phase_offset);
2206 	if (rc) {
2207 		dev_err(zldev->dev, "Failed to read ref phase offset: %pe\n",
2208 			ERR_PTR(rc));
2209 
2210 		return false;
2211 	}
2212 
2213 	/* Convert to ps */
2214 	phase_offset = div_s64(sign_extend64(phase_offset, 47), 100);
2215 
2216 	/* Compare with previous value */
2217 	if (phase_offset != pin->phase_offset) {
2218 		dev_dbg(zldev->dev, "%s phase offset changed: %lld -> %lld\n",
2219 			pin->label, pin->phase_offset, phase_offset);
2220 		pin->phase_offset = phase_offset;
2221 
2222 		return true;
2223 	}
2224 
2225 	return false;
2226 }
2227 
2228 /**
2229  * zl3073x_dpll_pin_ffo_check - check for FFO change on active pin
2230  * @pin: pin to check
2231  *
2232  * Return: true on change, false otherwise
2233  */
2234 static bool
2235 zl3073x_dpll_pin_ffo_check(struct zl3073x_dpll_pin *pin)
2236 {
2237 	struct zl3073x_dpll *zldpll = pin->dpll;
2238 	struct zl3073x_dev *zldev = zldpll->dev;
2239 	const struct zl3073x_chan *chan;
2240 	s64 ffo;
2241 
2242 	lockdep_assert_held(&zldpll->lock);
2243 
2244 	if (pin->operstate != DPLL_PIN_OPERSTATE_ACTIVE)
2245 		return false;
2246 
2247 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
2248 	if (zl3073x_chan_df_offset_get(chan) == ZL_DPLL_DF_OFFSET_UNKNOWN)
2249 		return false;
2250 
2251 	/* dpll_df_offset register has inverted sign per datasheet:
2252 	 * f_offset = f_nom * (-df_offset) / 2^48
2253 	 * Input pin FFO is pin-vs-DPLL (opposite of DPLL-vs-reference),
2254 	 * so the two inversions cancel out: ppt = df * 5^12 / 2^36
2255 	 */
2256 	ffo = mul_s64_u64_shr(zl3073x_chan_df_offset_get(chan),
2257 			      244140625, 36);
2258 
2259 	if (pin->freq_offset != ffo) {
2260 		dev_dbg(zldev->dev, "%s freq offset changed to: %lld\n",
2261 			pin->label, ffo);
2262 		pin->freq_offset = ffo;
2263 		return true;
2264 	}
2265 
2266 	return false;
2267 }
2268 
2269 /**
2270  * zl3073x_dpll_pin_measured_freq_check - check for pin measured frequency
2271  * change
2272  * @pin: pin to check
2273  *
2274  * Check for the given pin's measured frequency change.
2275  *
2276  * Return: true on measured frequency change, false otherwise
2277  */
2278 static bool
2279 zl3073x_dpll_pin_measured_freq_check(struct zl3073x_dpll_pin *pin)
2280 {
2281 	struct zl3073x_dpll *zldpll = pin->dpll;
2282 	struct zl3073x_dev *zldev = zldpll->dev;
2283 	const struct zl3073x_ref *ref;
2284 	u8 ref_id;
2285 	u32 freq;
2286 
2287 	lockdep_assert_held(&zldpll->lock);
2288 
2289 	if (!READ_ONCE(zldpll->dev->freq_monitor))
2290 		return false;
2291 
2292 	ref_id = zl3073x_input_pin_ref_get(pin->id);
2293 	ref = zl3073x_ref_state_get(zldev, ref_id);
2294 
2295 	freq = zl3073x_ref_meas_freq_get(ref);
2296 	if (pin->measured_freq != freq) {
2297 		dev_dbg(zldev->dev, "%s measured freq changed: %u -> %u\n",
2298 			pin->label, pin->measured_freq, freq);
2299 		pin->measured_freq = freq;
2300 
2301 		return true;
2302 	}
2303 
2304 	return false;
2305 }
2306 
2307 /**
2308  * zl3073x_dpll_changes_check - check for changes and send notifications
2309  * @zldpll: pointer to zl3073x_dpll structure
2310  *
2311  * Checks for changes on given DPLL device and its registered DPLL pins
2312  * and sends notifications about them.
2313  *
2314  * This function is periodically called from @zl3073x_dev_periodic_work.
2315  */
2316 void
2317 zl3073x_dpll_changes_check(struct zl3073x_dpll *zldpll)
2318 {
2319 	DECLARE_BITMAP(changed_pins, ZL3073X_NUM_INPUT_PINS);
2320 	struct zl3073x_dev *zldev = zldpll->dev;
2321 	enum dpll_lock_status lock_status;
2322 	struct device *dev = zldev->dev;
2323 	struct zl3073x_dpll_pin *pin;
2324 	bool dev_changed = false;
2325 	int rc;
2326 
2327 	bitmap_zero(changed_pins, ZL3073X_NUM_INPUT_PINS);
2328 
2329 	mutex_lock(&zldpll->lock);
2330 
2331 	zldpll->check_count++;
2332 
2333 	rc = zl3073x_chan_state_update(zldev, zldpll->id);
2334 	if (rc) {
2335 		dev_err(dev, "Failed to get DPLL%u state: %pe\n",
2336 			zldpll->id, ERR_PTR(rc));
2337 		goto unlock;
2338 	}
2339 
2340 	rc = __zl3073x_dpll_lock_status_get(zldpll, &lock_status);
2341 	if (rc) {
2342 		dev_err(dev, "Failed to get DPLL%u lock status: %pe\n",
2343 			zldpll->id, ERR_PTR(rc));
2344 		goto unlock;
2345 	}
2346 
2347 	if (zldpll->lock_status != lock_status) {
2348 		zldpll->lock_status = lock_status;
2349 		dev_changed = true;
2350 	}
2351 
2352 	/* Update phase offset latch registers for this DPLL if the phase
2353 	 * offset monitor feature is enabled.
2354 	 */
2355 	if (zldpll->phase_monitor) {
2356 		rc = zl3073x_ref_phase_offsets_update(zldev, zldpll->id);
2357 		if (rc) {
2358 			dev_err(zldev->dev,
2359 				"Failed to update phase offsets: %pe\n",
2360 				ERR_PTR(rc));
2361 			goto unlock;
2362 		}
2363 	}
2364 
2365 	list_for_each_entry(pin, &zldpll->pins, list) {
2366 		enum dpll_pin_operstate operstate;
2367 
2368 		/* Only physical input pins need monitoring */
2369 		if (!zl3073x_dpll_is_input_pin(pin) ||
2370 		    zl3073x_dpll_is_nco_pin(pin))
2371 			continue;
2372 
2373 		rc = zl3073x_dpll_ref_operstate_get(pin, &operstate);
2374 		if (rc) {
2375 			dev_err(dev,
2376 				"Failed to get %s on DPLL%u oper state: %pe\n",
2377 				pin->label, zldpll->id, ERR_PTR(rc));
2378 			goto unlock;
2379 		}
2380 
2381 		if (operstate != pin->operstate) {
2382 			dev_dbg(dev, "%s oper state changed: %u->%u\n",
2383 				pin->label, pin->operstate, operstate);
2384 			pin->operstate = operstate;
2385 			set_bit(pin->id, changed_pins);
2386 		}
2387 
2388 		if (zldpll->check_count % 2 == 0) {
2389 			if (zl3073x_dpll_pin_phase_offset_check(pin))
2390 				set_bit(pin->id, changed_pins);
2391 
2392 			if (zl3073x_dpll_pin_ffo_check(pin))
2393 				set_bit(pin->id, changed_pins);
2394 
2395 			if (zl3073x_dpll_pin_measured_freq_check(pin))
2396 				set_bit(pin->id, changed_pins);
2397 		}
2398 	}
2399 
2400 unlock:
2401 	mutex_unlock(&zldpll->lock);
2402 
2403 	/* Send notifications outside the lock to avoid ABBA deadlock
2404 	 * with dpll_lock taken by notification functions.
2405 	 */
2406 	if (dev_changed)
2407 		dpll_device_change_ntf(zldpll->dpll_dev);
2408 
2409 	list_for_each_entry(pin, &zldpll->pins, list) {
2410 		if (zl3073x_dpll_is_input_pin(pin) &&
2411 		    !zl3073x_dpll_is_nco_pin(pin) &&
2412 		    test_bit(pin->id, changed_pins))
2413 			dpll_pin_change_ntf(pin->dpll_pin);
2414 	}
2415 }
2416 
2417 /**
2418  * zl3073x_dpll_init_fine_phase_adjust - do initial fine phase adjustments
2419  * @zldev: pointer to zl3073x device
2420  *
2421  * Performs initial fine phase adjustments needed per datasheet.
2422  *
2423  * Return: 0 on success, <0 on error
2424  */
2425 int
2426 zl3073x_dpll_init_fine_phase_adjust(struct zl3073x_dev *zldev)
2427 {
2428 	int rc;
2429 
2430 	rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_MASK, 0x1f);
2431 	if (rc)
2432 		return rc;
2433 
2434 	rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_INTVL, 0x01);
2435 	if (rc)
2436 		return rc;
2437 
2438 	rc = zl3073x_write_u16(zldev, ZL_REG_SYNTH_PHASE_SHIFT_DATA, 0xffff);
2439 	if (rc)
2440 		return rc;
2441 
2442 	return zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_CTRL, 0x01);
2443 }
2444 
2445 /* Maximum frequency adjustment: +-1% of nominal in ppb */
2446 #define ZL3073X_DPLL_PTP_MAX_ADJ	10000000
2447 
2448 /**
2449  * zl3073x_dpll_ptp_gettimex64 - read current time from ToD counters
2450  * @info: PTP clock info
2451  * @ts: timespec to store current time
2452  * @sts: optional system timestamp pair for cross-timestamping
2453  *
2454  * Return: 0 on success, <0 on error
2455  */
2456 static int zl3073x_dpll_ptp_gettimex64(struct ptp_clock_info *info,
2457 				       struct timespec64 *ts,
2458 				       struct ptp_system_timestamp *sts)
2459 {
2460 	struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll,
2461 						   ptp_info);
2462 
2463 	guard(mutex)(&zldpll->lock);
2464 
2465 	return zl3073x_chan_tod_read(zldpll->dev, zldpll->id, false, ts, sts);
2466 }
2467 
2468 /**
2469  * zl3073x_dpll_ptp_settime64 - set ToD counters to given time
2470  * @info: PTP clock info
2471  * @ts: timespec with time to set
2472  *
2473  * Return: 0 on success, <0 on error
2474  */
2475 static int zl3073x_dpll_ptp_settime64(struct ptp_clock_info *info,
2476 				      const struct timespec64 *ts)
2477 {
2478 	struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll,
2479 						   ptp_info);
2480 
2481 	guard(mutex)(&zldpll->lock);
2482 
2483 	return zl3073x_chan_tod_write(zldpll->dev, zldpll->id, *ts);
2484 }
2485 
2486 /**
2487  * zl3073x_dpll_ptp_adjtime_phase_step - adjust sub-second time via phase step
2488  * @zldpll: DPLL channel
2489  * @delta: time adjustment in nanoseconds (must be within (-NSEC_PER_SEC,
2490  *         NSEC_PER_SEC))
2491  *
2492  * Uses the output phase step mechanism with tod_step=1 to adjust both
2493  * the output clock phase and the ToD counter simultaneously. This keeps
2494  * outputs and ToD coherent. Only valid for NCO.
2495  *
2496  * Outputs are grouped by synthesizer since the phase step value is in
2497  * synthesizer clock cycles. The first synth group with enabled outputs
2498  * uses tod_step to adjust both outputs and the ToD counter. Remaining
2499  * groups step outputs only. If no synth has enabled outputs, the ToD
2500  * counter is stepped alone using an empty output mask (the FW uses
2501  * the first enabled synth's period for the conversion).
2502  *
2503  * Return:
2504  * * %0			- success (or partial success if a later synth group
2505  *			  failed after the first was already stepped)
2506  * * %-EOPNOTSUPP	- no synths available
2507  * * negative		- error
2508  */
2509 static int zl3073x_dpll_ptp_adjtime_phase_step(struct zl3073x_dpll *zldpll,
2510 					       s64 delta)
2511 {
2512 	u16 synth_mask[ZL3073X_NUM_SYNTHS] = {};
2513 	struct zl3073x_dev *zldev = zldpll->dev;
2514 	const struct zl3073x_synth *synth;
2515 	struct zl3073x_dpll_pin *pin;
2516 	u32 first_synth_freq = 0;
2517 	bool tod_stepped = false;
2518 	s32 step_cycles;
2519 	u32 synth_freq;
2520 	int rc;
2521 	u8 i;
2522 
2523 	/* Build per-synth output masks from registered output pins */
2524 	list_for_each_entry(pin, &zldpll->pins, list) {
2525 		u8 out_id, synth_id;
2526 
2527 		if (zl3073x_dpll_is_input_pin(pin))
2528 			continue;
2529 
2530 		out_id = zl3073x_output_pin_out_get(pin->id);
2531 
2532 		if (!zl3073x_dev_out_is_stepped(zldev, out_id))
2533 			continue;
2534 
2535 		synth_id = zl3073x_dev_out_synth_get(zldev, out_id);
2536 		if (synth_id >= ZL3073X_NUM_SYNTHS) {
2537 			dev_warn(zldev->dev, "Unexpected synth id for OUT%u\n",
2538 				 out_id);
2539 			continue;
2540 		}
2541 		synth_mask[synth_id] |= BIT(out_id);
2542 	}
2543 
2544 	/* Process each synth group */
2545 	for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) {
2546 		synth = zl3073x_synth_state_get(zldev, i);
2547 		if (!zl3073x_synth_is_enabled(synth) ||
2548 		    zl3073x_synth_dpll_get(synth) != zldpll->id)
2549 			continue;
2550 
2551 		synth_freq = zl3073x_synth_freq_get(synth);
2552 
2553 		/* Remember first enabled synth freq for ToD-only fallback */
2554 		if (!first_synth_freq)
2555 			first_synth_freq = synth_freq;
2556 
2557 		if (!synth_mask[i])
2558 			continue;
2559 
2560 		/* Safe for s32: max synth freq is 750 MHz */
2561 		step_cycles = div_s64(delta * synth_freq, NSEC_PER_SEC);
2562 
2563 		rc = zl3073x_chan_phase_step(zldev, zldpll->id,
2564 					     synth_mask[i], step_cycles,
2565 					     !tod_stepped);
2566 		if (rc) {
2567 			if (tod_stepped) {
2568 				dev_warn(zldev->dev,
2569 					 "Partial phase step failure\n");
2570 				return 0;
2571 			}
2572 			return rc;
2573 		}
2574 		tod_stepped = true;
2575 	}
2576 
2577 	if (!first_synth_freq)
2578 		return -EOPNOTSUPP;
2579 
2580 	/* No enabled outputs found; step ToD counter only using the
2581 	 * first enabled synth's period (empty output mask).
2582 	 */
2583 	if (!tod_stepped) {
2584 		step_cycles = div_s64(delta * first_synth_freq, NSEC_PER_SEC);
2585 		return zl3073x_chan_phase_step(zldev, zldpll->id, 0,
2586 					       step_cycles, true);
2587 	}
2588 
2589 	return 0;
2590 }
2591 
2592 /**
2593  * zl3073x_dpll_ptp_adjtime - adjust PTP clock time
2594  * @info: PTP clock info
2595  * @delta: time adjustment in nanoseconds
2596  *
2597  * For NCO, large deltas (>= 1 second) are split into a ToD
2598  * read-modify-write for the seconds part and an output phase step for
2599  * the sub-second remainder. Sub-second deltas use phase step directly,
2600  * falling back to ToD read-modify-write if phase step or TIE write
2601  * fails. In AUTO/REFLOCK modes, large deltas are split into ToD
2602  * read-modify-write for seconds and TIE write for the sub-second
2603  * remainder. Sub-second deltas use TIE write directly.
2604  *
2605  * If the seconds part was already committed when the sub-second
2606  * mechanism fails, returns 0 to prevent the PTP servo from retrying
2607  * the full delta and applying seconds again.
2608  *
2609  * Return: 0 on success (or partial success), <0 on error
2610  */
2611 static int zl3073x_dpll_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
2612 {
2613 	struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll,
2614 						   ptp_info);
2615 	struct zl3073x_dev *zldev = zldpll->dev;
2616 	const struct zl3073x_chan *chan;
2617 	bool sec_adjusted = false;
2618 	struct timespec64 ts;
2619 	int rc;
2620 
2621 	if (!delta)
2622 		return 0;
2623 
2624 	guard(mutex)(&zldpll->lock);
2625 
2626 	/* Modes without phase step or TIE use plain ToD adjust */
2627 	chan = zl3073x_chan_state_get(zldev, zldpll->id);
2628 	if (!zl3073x_chan_mode_is_nco(chan) &&
2629 	    !zl3073x_chan_mode_supports_tie(chan))
2630 		return zl3073x_chan_tod_adjust(zldev, zldpll->id,
2631 					       ns_to_timespec64(delta));
2632 
2633 	/* Split off seconds via ToD read-modify-write so the sub-second
2634 	 * remainder can be applied through the output-coherent mechanism
2635 	 * (phase step or TIE write).
2636 	 */
2637 	if (delta >= NSEC_PER_SEC || delta <= -NSEC_PER_SEC) {
2638 		s32 remainder;
2639 
2640 		ts.tv_sec = div_s64_rem(delta, NSEC_PER_SEC, &remainder);
2641 		ts.tv_nsec = 0;
2642 		delta = remainder;
2643 
2644 		rc = zl3073x_chan_tod_adjust(zldev, zldpll->id, ts);
2645 		if (rc)
2646 			return rc;
2647 
2648 		/* No sub-second remainder, done */
2649 		if (!delta)
2650 			return 0;
2651 
2652 		/* Wait for the ToD write to be applied at the 1 Hz edge
2653 		 * before issuing phase step or TIE write, so the pending
2654 		 * WR_NEXT_1HZ does not overwrite the sub-second adjustment.
2655 		 */
2656 		rc = zl3073x_chan_tod_ready_wait(zldev, zldpll->id);
2657 		if (rc)
2658 			return rc;
2659 
2660 		sec_adjusted = true;
2661 	}
2662 
2663 	/* Apply sub-second delta via phase step (NCO) or TIE write */
2664 	if (zl3073x_chan_mode_is_nco(chan)) {
2665 		rc = zl3073x_dpll_ptp_adjtime_phase_step(zldpll, delta);
2666 		if (!rc)
2667 			return 0;
2668 	} else {
2669 		rc = zl3073x_chan_tie_write(zldev, zldpll->id, delta);
2670 		if (!rc)
2671 			return 0;
2672 	}
2673 
2674 	/* Phase step or TIE write failed, fall back to ToD adjust */
2675 	rc = zl3073x_chan_tod_adjust(zldev, zldpll->id,
2676 				     ns_to_timespec64(delta));
2677 
2678 	/* In the unlikely event that both phase step/TIE write and fallback
2679 	 * ToD adjust fail after seconds were already committed, return
2680 	 * success to prevent the PTP servo from retrying the full delta and
2681 	 * applying seconds again. The sub-second residual will self-correct
2682 	 * in the next servo cycle.
2683 	 */
2684 	if (rc && sec_adjusted) {
2685 		dev_warn(zldev->dev,
2686 			 "Sub-second adjustment failed after seconds applied\n");
2687 		return 0;
2688 	}
2689 
2690 	return rc;
2691 }
2692 
2693 /**
2694  * zl3073x_dpll_ptp_adjfine - adjust PTP clock frequency
2695  * @info: PTP clock info
2696  * @scaled_ppm: frequency adjustment in scaled ppm (ppm * 2^16)
2697  *
2698  * Only supported for NCO. Writes the delta frequency offset register.
2699  *
2700  * Return:
2701  * * %0			- success or @scaled_ppm is zero (no-op)
2702  * * %-EOPNOTSUPP	- NCO pin is not connected and @scaled_ppm is non-zero
2703  * * negative		- other error
2704  */
2705 static int
2706 zl3073x_dpll_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
2707 {
2708 	struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll,
2709 						   ptp_info);
2710 	const struct zl3073x_chan *chan;
2711 	s64 offset;
2712 
2713 	/* Convert scaled_ppm to df_offset in 2^-48 steps:
2714 	 *   df_offset = -(scaled_ppm * 2^32) / 10^6
2715 	 *
2716 	 * Simplify to avoid overflow:
2717 	 *   df_offset = -(scaled_ppm * 2^26) / 5^6
2718 	 *   df_offset = -(scaled_ppm * 67108864) / 15625
2719 	 */
2720 	offset = -div_s64((s64)scaled_ppm * 67108864LL, 15625);
2721 
2722 	guard(mutex)(&zldpll->lock);
2723 
2724 	chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id);
2725 	if (!zl3073x_chan_mode_is_nco(chan))
2726 		return scaled_ppm ? -EOPNOTSUPP : 0;
2727 	if (offset == chan->df_offset)
2728 		return 0;
2729 
2730 	return zl3073x_chan_df_offset_set(zldpll->dev, zldpll->id, offset);
2731 }
2732 
2733 /**
2734  * zl3073x_dpll_ptp_adjphase - adjust PTP clock phase
2735  * @info: PTP clock info
2736  * @delta: phase adjustment in nanoseconds
2737  *
2738  * Only supported in AUTO and REFLOCK modes. Uses TIE write for
2739  * nanosecond resolution phase adjustment.
2740  *
2741  * Return:
2742  * * %0			- success or @delta is zero (no-op)
2743  * * %-EOPNOTSUPP	- mode does not support TIE and @delta is non-zero
2744  * * negative		- other error
2745  */
2746 static int zl3073x_dpll_ptp_adjphase(struct ptp_clock_info *info, s32 delta)
2747 {
2748 	struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll,
2749 						   ptp_info);
2750 	struct zl3073x_dev *zldev = zldpll->dev;
2751 	const struct zl3073x_chan *chan;
2752 
2753 	if (!delta)
2754 		return 0;
2755 
2756 	guard(mutex)(&zldpll->lock);
2757 
2758 	chan = zl3073x_chan_state_get(zldev, zldpll->id);
2759 
2760 	if (!zl3073x_chan_mode_supports_tie(chan))
2761 		return -EOPNOTSUPP;
2762 
2763 	return zl3073x_chan_tie_write(zldev, zldpll->id, delta);
2764 }
2765 
2766 static s32
2767 zl3073x_dpll_ptp_getmaxphase(struct ptp_clock_info *info __always_unused)
2768 {
2769 	/* HW limits TIE write to +-1 second. Return the constant HW
2770 	 * limit and let adjphase handle mode-specific checks.
2771 	 */
2772 	return NSEC_PER_SEC - 1;
2773 }
2774 
2775 static const struct ptp_clock_info zl3073x_dpll_ptp_clock_info = {
2776 	.owner		= THIS_MODULE,
2777 	.max_adj	= ZL3073X_DPLL_PTP_MAX_ADJ,
2778 	.gettimex64	= zl3073x_dpll_ptp_gettimex64,
2779 	.settime64	= zl3073x_dpll_ptp_settime64,
2780 	.adjtime	= zl3073x_dpll_ptp_adjtime,
2781 	.adjfine	= zl3073x_dpll_ptp_adjfine,
2782 	.adjphase	= zl3073x_dpll_ptp_adjphase,
2783 	.getmaxphase	= zl3073x_dpll_ptp_getmaxphase,
2784 };
2785 
2786 /**
2787  * zl3073x_dpll_ptp_register - register PTP clock for a DPLL channel
2788  * @zldpll: DPLL channel to register PTP clock for
2789  *
2790  * Return: 0 on success, <0 on error
2791  */
2792 static int zl3073x_dpll_ptp_register(struct zl3073x_dpll *zldpll)
2793 {
2794 	struct zl3073x_dev *zldev = zldpll->dev;
2795 	struct ptp_clock *ptp_clock;
2796 
2797 	zldpll->ptp_info = zl3073x_dpll_ptp_clock_info;
2798 	snprintf(zldpll->ptp_info.name, sizeof(zldpll->ptp_info.name),
2799 		 "%s-dpll%u", dev_name(zldev->dev), zldpll->id);
2800 
2801 	ptp_clock = ptp_clock_register(&zldpll->ptp_info, zldev->dev);
2802 	if (IS_ERR(ptp_clock)) {
2803 		dev_err(zldev->dev, "Failed to register PTP clock for DPLL%u\n",
2804 			zldpll->id);
2805 		return PTR_ERR(ptp_clock);
2806 	}
2807 
2808 	zldpll->ptp_clock = ptp_clock;
2809 
2810 	return 0;
2811 }
2812 
2813 /**
2814  * zl3073x_dpll_ptp_unregister - unregister PTP clock for a DPLL channel
2815  * @zldpll: DPLL channel to unregister PTP clock for
2816  */
2817 static void zl3073x_dpll_ptp_unregister(struct zl3073x_dpll *zldpll)
2818 {
2819 	if (!IS_ERR_OR_NULL(zldpll->ptp_clock)) {
2820 		ptp_clock_unregister(zldpll->ptp_clock);
2821 		zldpll->ptp_clock = NULL;
2822 	}
2823 }
2824 
2825 /**
2826  * zl3073x_dpll_ref_sync_pair_register - register ref_sync pairs for a pin
2827  * @pin: pointer to zl3073x_dpll_pin structure
2828  *
2829  * Iterates 'ref-sync-sources' phandles in the pin's firmware node and
2830  * registers each declared pairing.
2831  *
2832  * Return: 0 on success, <0 on error
2833  */
2834 static int
2835 zl3073x_dpll_ref_sync_pair_register(struct zl3073x_dpll_pin *pin)
2836 {
2837 	struct zl3073x_dev *zldev = pin->dpll->dev;
2838 	struct fwnode_handle *fwnode;
2839 	struct dpll_pin *sync_pin;
2840 	dpll_tracker tracker;
2841 	int n, rc;
2842 
2843 	for (n = 0; ; n++) {
2844 		/* Get n'th ref-sync source */
2845 		fwnode = fwnode_find_reference(pin->fwnode, "ref-sync-sources",
2846 					       n);
2847 		if (IS_ERR(fwnode)) {
2848 			rc = PTR_ERR(fwnode);
2849 			break;
2850 		}
2851 
2852 		/* Find associated dpll pin */
2853 		sync_pin = fwnode_dpll_pin_find(fwnode, &tracker);
2854 		fwnode_handle_put(fwnode);
2855 		if (!sync_pin) {
2856 			dev_warn(zldev->dev, "%s: ref-sync source %d not found",
2857 				 pin->label, n);
2858 			continue;
2859 		}
2860 
2861 		/* Register new ref-sync pair */
2862 		rc = dpll_pin_ref_sync_pair_add(pin->dpll_pin, sync_pin);
2863 		dpll_pin_put(sync_pin, &tracker);
2864 
2865 		/* -EBUSY means pairing already exists from another DPLL's
2866 		 * registration.
2867 		 */
2868 		if (rc && rc != -EBUSY) {
2869 			dev_err(zldev->dev,
2870 				"%s: failed to add ref-sync source %d: %pe",
2871 				pin->label, n, ERR_PTR(rc));
2872 			break;
2873 		}
2874 	}
2875 
2876 	return rc != -ENOENT ? rc : 0;
2877 }
2878 
2879 /**
2880  * zl3073x_dpll_ref_sync_pairs_register - register ref_sync pairs for a DPLL
2881  * @zldpll: pointer to zl3073x_dpll structure
2882  *
2883  * Iterates all registered input pins of the given DPLL and establishes
2884  * ref_sync pairings declared by 'ref-sync-sources' phandles in the
2885  * device tree.
2886  *
2887  * Return: 0 on success, <0 on error
2888  */
2889 static int
2890 zl3073x_dpll_ref_sync_pairs_register(struct zl3073x_dpll *zldpll)
2891 {
2892 	struct zl3073x_dpll_pin *pin;
2893 	int rc;
2894 
2895 	list_for_each_entry(pin, &zldpll->pins, list) {
2896 		if (!zl3073x_dpll_is_input_pin(pin) || !pin->fwnode)
2897 			continue;
2898 
2899 		rc = zl3073x_dpll_ref_sync_pair_register(pin);
2900 		if (rc)
2901 			return rc;
2902 	}
2903 
2904 	return 0;
2905 }
2906 
2907 /**
2908  * zl3073x_dpll_alloc - allocate DPLL device
2909  * @zldev: pointer to zl3073x device
2910  * @ch: DPLL channel number
2911  *
2912  * Allocates DPLL device structure for given DPLL channel.
2913  *
2914  * Return: pointer to DPLL device on success, error pointer on error
2915  */
2916 struct zl3073x_dpll *
2917 zl3073x_dpll_alloc(struct zl3073x_dev *zldev, u8 ch)
2918 {
2919 	struct zl3073x_dpll *zldpll;
2920 
2921 	zldpll = kzalloc_obj(*zldpll);
2922 	if (!zldpll)
2923 		return ERR_PTR(-ENOMEM);
2924 
2925 	zldpll->dev = zldev;
2926 	zldpll->id = ch;
2927 	mutex_init(&zldpll->lock);
2928 	INIT_LIST_HEAD(&zldpll->pins);
2929 
2930 	return zldpll;
2931 }
2932 
2933 /**
2934  * zl3073x_dpll_free - free DPLL device
2935  * @zldpll: pointer to zl3073x_dpll structure
2936  *
2937  * Deallocates given DPLL device previously allocated by @zl3073x_dpll_alloc.
2938  */
2939 void
2940 zl3073x_dpll_free(struct zl3073x_dpll *zldpll)
2941 {
2942 	WARN(zldpll->dpll_dev, "DPLL device is still registered\n");
2943 
2944 	mutex_destroy(&zldpll->lock);
2945 	kfree(zldpll);
2946 }
2947 
2948 /**
2949  * zl3073x_dpll_register - register DPLL device and all its pins
2950  * @zldpll: pointer to zl3073x_dpll structure
2951  *
2952  * Registers given DPLL device and all its pins into DPLL sub-system.
2953  *
2954  * Return: 0 on success, <0 on error
2955  */
2956 int
2957 zl3073x_dpll_register(struct zl3073x_dpll *zldpll)
2958 {
2959 	int rc;
2960 
2961 	rc = zl3073x_dpll_device_register(zldpll);
2962 	if (rc)
2963 		return rc;
2964 
2965 	rc = zl3073x_dpll_pins_register(zldpll);
2966 	if (rc) {
2967 		zl3073x_dpll_device_unregister(zldpll);
2968 		return rc;
2969 	}
2970 
2971 	rc = zl3073x_dpll_ref_sync_pairs_register(zldpll);
2972 	if (rc) {
2973 		zl3073x_dpll_pins_unregister(zldpll);
2974 		zl3073x_dpll_device_unregister(zldpll);
2975 		return rc;
2976 	}
2977 
2978 	rc = zl3073x_dpll_ptp_register(zldpll);
2979 	if (rc) {
2980 		zl3073x_dpll_pins_unregister(zldpll);
2981 		zl3073x_dpll_device_unregister(zldpll);
2982 		return rc;
2983 	}
2984 
2985 	return 0;
2986 }
2987 
2988 /**
2989  * zl3073x_dpll_unregister - unregister DPLL device and its pins
2990  * @zldpll: pointer to zl3073x_dpll structure
2991  *
2992  * Unregisters given DPLL device and all its pins from DPLL sub-system
2993  * previously registered by @zl3073x_dpll_register.
2994  */
2995 void
2996 zl3073x_dpll_unregister(struct zl3073x_dpll *zldpll)
2997 {
2998 	zl3073x_dpll_ptp_unregister(zldpll);
2999 	zl3073x_dpll_pins_unregister(zldpll);
3000 	zl3073x_dpll_device_unregister(zldpll);
3001 }
3002