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