xref: /linux/drivers/dpll/zl3073x/dpll.c (revision ff124bbbca1d3a07fa1392ffdbbdeece71f68ece)
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/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/netlink.h>
15 #include <linux/platform_device.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  * @label: package label
34  * @dir: pin direction
35  * @id: pin id
36  * @prio: pin priority <0, 14>
37  * @selectable: pin is selectable in automatic mode
38  * @esync_control: embedded sync is controllable
39  * @phase_gran: phase adjustment granularity
40  * @pin_state: last saved pin state
41  * @phase_offset: last saved pin phase offset
42  * @freq_offset: last saved fractional frequency offset
43  */
44 struct zl3073x_dpll_pin {
45 	struct list_head	list;
46 	struct zl3073x_dpll	*dpll;
47 	struct dpll_pin		*dpll_pin;
48 	dpll_tracker		tracker;
49 	char			label[8];
50 	enum dpll_pin_direction	dir;
51 	u8			id;
52 	u8			prio;
53 	bool			selectable;
54 	bool			esync_control;
55 	s32			phase_gran;
56 	enum dpll_pin_state	pin_state;
57 	s64			phase_offset;
58 	s64			freq_offset;
59 };
60 
61 /*
62  * Supported esync ranges for input and for output per output pair type
63  */
64 static const struct dpll_pin_frequency esync_freq_ranges[] = {
65 	DPLL_PIN_FREQUENCY_RANGE(0, 1),
66 };
67 
68 /**
69  * zl3073x_dpll_is_input_pin - check if the pin is input one
70  * @pin: pin to check
71  *
72  * Return: true if pin is input, false if pin is output.
73  */
74 static bool
75 zl3073x_dpll_is_input_pin(struct zl3073x_dpll_pin *pin)
76 {
77 	return pin->dir == DPLL_PIN_DIRECTION_INPUT;
78 }
79 
80 /**
81  * zl3073x_dpll_is_p_pin - check if the pin is P-pin
82  * @pin: pin to check
83  *
84  * Return: true if the pin is P-pin, false if it is N-pin
85  */
86 static bool
87 zl3073x_dpll_is_p_pin(struct zl3073x_dpll_pin *pin)
88 {
89 	return zl3073x_is_p_pin(pin->id);
90 }
91 
92 static int
93 zl3073x_dpll_pin_direction_get(const struct dpll_pin *dpll_pin, void *pin_priv,
94 			       const struct dpll_device *dpll, void *dpll_priv,
95 			       enum dpll_pin_direction *direction,
96 			       struct netlink_ext_ack *extack)
97 {
98 	struct zl3073x_dpll_pin *pin = pin_priv;
99 
100 	*direction = pin->dir;
101 
102 	return 0;
103 }
104 
105 static struct zl3073x_dpll_pin *
106 zl3073x_dpll_pin_get_by_ref(struct zl3073x_dpll *zldpll, u8 ref_id)
107 {
108 	struct zl3073x_dpll_pin *pin;
109 
110 	list_for_each_entry(pin, &zldpll->pins, list) {
111 		if (zl3073x_dpll_is_input_pin(pin) &&
112 		    zl3073x_input_pin_ref_get(pin->id) == ref_id)
113 			return pin;
114 	}
115 
116 	return NULL;
117 }
118 
119 static int
120 zl3073x_dpll_input_pin_esync_get(const struct dpll_pin *dpll_pin,
121 				 void *pin_priv,
122 				 const struct dpll_device *dpll,
123 				 void *dpll_priv,
124 				 struct dpll_pin_esync *esync,
125 				 struct netlink_ext_ack *extack)
126 {
127 	struct zl3073x_dpll *zldpll = dpll_priv;
128 	struct zl3073x_dev *zldev = zldpll->dev;
129 	struct zl3073x_dpll_pin *pin = pin_priv;
130 	const struct zl3073x_ref *ref;
131 	u8 ref_id;
132 
133 	ref_id = zl3073x_input_pin_ref_get(pin->id);
134 	ref = zl3073x_ref_state_get(zldev, ref_id);
135 
136 	switch (FIELD_GET(ZL_REF_SYNC_CTRL_MODE, ref->sync_ctrl)) {
137 	case ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75:
138 		esync->freq = ref->esync_n_div == ZL_REF_ESYNC_DIV_1HZ ? 1 : 0;
139 		esync->pulse = 25;
140 		break;
141 	default:
142 		esync->freq = 0;
143 		esync->pulse = 0;
144 		break;
145 	}
146 
147 	/* If the pin supports esync control expose its range but only
148 	 * if the current reference frequency is > 1 Hz.
149 	 */
150 	if (pin->esync_control && zl3073x_ref_freq_get(ref) > 1) {
151 		esync->range = esync_freq_ranges;
152 		esync->range_num = ARRAY_SIZE(esync_freq_ranges);
153 	} else {
154 		esync->range = NULL;
155 		esync->range_num = 0;
156 	}
157 
158 	return 0;
159 }
160 
161 static int
162 zl3073x_dpll_input_pin_esync_set(const struct dpll_pin *dpll_pin,
163 				 void *pin_priv,
164 				 const struct dpll_device *dpll,
165 				 void *dpll_priv, u64 freq,
166 				 struct netlink_ext_ack *extack)
167 {
168 	struct zl3073x_dpll *zldpll = dpll_priv;
169 	struct zl3073x_dev *zldev = zldpll->dev;
170 	struct zl3073x_dpll_pin *pin = pin_priv;
171 	struct zl3073x_ref ref;
172 	u8 ref_id, sync_mode;
173 
174 	ref_id = zl3073x_input_pin_ref_get(pin->id);
175 	ref = *zl3073x_ref_state_get(zldev, ref_id);
176 
177 	/* Use freq == 0 to disable esync */
178 	if (!freq)
179 		sync_mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF;
180 	else
181 		sync_mode = ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75;
182 
183 	ref.sync_ctrl &= ~ZL_REF_SYNC_CTRL_MODE;
184 	ref.sync_ctrl |= FIELD_PREP(ZL_REF_SYNC_CTRL_MODE, sync_mode);
185 
186 	if (freq) {
187 		/* 1 Hz is only supported frequency now */
188 		ref.esync_n_div = ZL_REF_ESYNC_DIV_1HZ;
189 	}
190 
191 	/* Update reference configuration */
192 	return zl3073x_ref_state_set(zldev, ref_id, &ref);
193 }
194 
195 static int
196 zl3073x_dpll_input_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv,
197 			       const struct dpll_device *dpll, void *dpll_priv,
198 			       s64 *ffo, struct netlink_ext_ack *extack)
199 {
200 	struct zl3073x_dpll_pin *pin = pin_priv;
201 
202 	*ffo = pin->freq_offset;
203 
204 	return 0;
205 }
206 
207 static int
208 zl3073x_dpll_input_pin_frequency_get(const struct dpll_pin *dpll_pin,
209 				     void *pin_priv,
210 				     const struct dpll_device *dpll,
211 				     void *dpll_priv, u64 *frequency,
212 				     struct netlink_ext_ack *extack)
213 {
214 	struct zl3073x_dpll *zldpll = dpll_priv;
215 	struct zl3073x_dpll_pin *pin = pin_priv;
216 	u8 ref_id;
217 
218 	ref_id = zl3073x_input_pin_ref_get(pin->id);
219 	*frequency = zl3073x_dev_ref_freq_get(zldpll->dev, ref_id);
220 
221 	return 0;
222 }
223 
224 static int
225 zl3073x_dpll_input_pin_frequency_set(const struct dpll_pin *dpll_pin,
226 				     void *pin_priv,
227 				     const struct dpll_device *dpll,
228 				     void *dpll_priv, u64 frequency,
229 				     struct netlink_ext_ack *extack)
230 {
231 	struct zl3073x_dpll *zldpll = dpll_priv;
232 	struct zl3073x_dev *zldev = zldpll->dev;
233 	struct zl3073x_dpll_pin *pin = pin_priv;
234 	struct zl3073x_ref ref;
235 	u8 ref_id;
236 
237 	/* Get reference state */
238 	ref_id = zl3073x_input_pin_ref_get(pin->id);
239 	ref = *zl3073x_ref_state_get(zldev, ref_id);
240 
241 	/* Update frequency */
242 	zl3073x_ref_freq_set(&ref, frequency);
243 
244 	/* Commit reference state */
245 	return zl3073x_ref_state_set(zldev, ref_id, &ref);
246 }
247 
248 /**
249  * zl3073x_dpll_selected_ref_get - get currently selected reference
250  * @zldpll: pointer to zl3073x_dpll
251  * @ref: place to store selected reference
252  *
253  * Check for currently selected reference the DPLL should be locked to
254  * and stores its index to given @ref.
255  *
256  * Return: 0 on success, <0 on error
257  */
258 static int
259 zl3073x_dpll_selected_ref_get(struct zl3073x_dpll *zldpll, u8 *ref)
260 {
261 	struct zl3073x_dev *zldev = zldpll->dev;
262 	u8 state, value;
263 	int rc;
264 
265 	switch (zldpll->refsel_mode) {
266 	case ZL_DPLL_MODE_REFSEL_MODE_AUTO:
267 		/* For automatic mode read refsel_status register */
268 		rc = zl3073x_read_u8(zldev,
269 				     ZL_REG_DPLL_REFSEL_STATUS(zldpll->id),
270 				     &value);
271 		if (rc)
272 			return rc;
273 
274 		/* Extract reference state */
275 		state = FIELD_GET(ZL_DPLL_REFSEL_STATUS_STATE, value);
276 
277 		/* Return the reference only if the DPLL is locked to it */
278 		if (state == ZL_DPLL_REFSEL_STATUS_STATE_LOCK)
279 			*ref = FIELD_GET(ZL_DPLL_REFSEL_STATUS_REFSEL, value);
280 		else
281 			*ref = ZL3073X_DPLL_REF_NONE;
282 		break;
283 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
284 		/* For manual mode return stored value */
285 		*ref = zldpll->forced_ref;
286 		break;
287 	default:
288 		/* For other modes like NCO, freerun... there is no input ref */
289 		*ref = ZL3073X_DPLL_REF_NONE;
290 		break;
291 	}
292 
293 	return 0;
294 }
295 
296 /**
297  * zl3073x_dpll_selected_ref_set - select reference in manual mode
298  * @zldpll: pointer to zl3073x_dpll
299  * @ref: input reference to be selected
300  *
301  * Selects the given reference for the DPLL channel it should be
302  * locked to.
303  *
304  * Return: 0 on success, <0 on error
305  */
306 static int
307 zl3073x_dpll_selected_ref_set(struct zl3073x_dpll *zldpll, u8 ref)
308 {
309 	struct zl3073x_dev *zldev = zldpll->dev;
310 	u8 mode, mode_refsel;
311 	int rc;
312 
313 	mode = zldpll->refsel_mode;
314 
315 	switch (mode) {
316 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
317 		/* Manual mode with ref selected */
318 		if (ref == ZL3073X_DPLL_REF_NONE) {
319 			switch (zldpll->lock_status) {
320 			case DPLL_LOCK_STATUS_LOCKED_HO_ACQ:
321 			case DPLL_LOCK_STATUS_HOLDOVER:
322 				/* Switch to forced holdover */
323 				mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER;
324 				break;
325 			default:
326 				/* Switch to freerun */
327 				mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN;
328 				break;
329 			}
330 			/* Keep selected reference */
331 			ref = zldpll->forced_ref;
332 		} else if (ref == zldpll->forced_ref) {
333 			/* No register update - same mode and same ref */
334 			return 0;
335 		}
336 		break;
337 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
338 	case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER:
339 		/* Manual mode without no ref */
340 		if (ref == ZL3073X_DPLL_REF_NONE)
341 			/* No register update - keep current mode */
342 			return 0;
343 
344 		/* Switch to reflock mode and update ref selection */
345 		mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK;
346 		break;
347 	default:
348 		/* For other modes like automatic or NCO ref cannot be selected
349 		 * manually
350 		 */
351 		return -EOPNOTSUPP;
352 	}
353 
354 	/* Build mode_refsel value */
355 	mode_refsel = FIELD_PREP(ZL_DPLL_MODE_REFSEL_MODE, mode) |
356 		      FIELD_PREP(ZL_DPLL_MODE_REFSEL_REF, ref);
357 
358 	/* Update dpll_mode_refsel register */
359 	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MODE_REFSEL(zldpll->id),
360 			      mode_refsel);
361 	if (rc)
362 		return rc;
363 
364 	/* Store new mode and forced reference */
365 	zldpll->refsel_mode = mode;
366 	zldpll->forced_ref = ref;
367 
368 	return rc;
369 }
370 
371 /**
372  * zl3073x_dpll_connected_ref_get - get currently connected reference
373  * @zldpll: pointer to zl3073x_dpll
374  * @ref: place to store selected reference
375  *
376  * Looks for currently connected the DPLL is locked to and stores its index
377  * to given @ref.
378  *
379  * Return: 0 on success, <0 on error
380  */
381 static int
382 zl3073x_dpll_connected_ref_get(struct zl3073x_dpll *zldpll, u8 *ref)
383 {
384 	struct zl3073x_dev *zldev = zldpll->dev;
385 	int rc;
386 
387 	/* Get currently selected input reference */
388 	rc = zl3073x_dpll_selected_ref_get(zldpll, ref);
389 	if (rc)
390 		return rc;
391 
392 	/* If the monitor indicates an error nothing is connected */
393 	if (ZL3073X_DPLL_REF_IS_VALID(*ref) &&
394 	    !zl3073x_dev_ref_is_status_ok(zldev, *ref))
395 		*ref = ZL3073X_DPLL_REF_NONE;
396 
397 	return 0;
398 }
399 
400 static int
401 zl3073x_dpll_input_pin_phase_offset_get(const struct dpll_pin *dpll_pin,
402 					void *pin_priv,
403 					const struct dpll_device *dpll,
404 					void *dpll_priv, s64 *phase_offset,
405 					struct netlink_ext_ack *extack)
406 {
407 	struct zl3073x_dpll *zldpll = dpll_priv;
408 	struct zl3073x_dev *zldev = zldpll->dev;
409 	struct zl3073x_dpll_pin *pin = pin_priv;
410 	const struct zl3073x_ref *ref;
411 	u8 conn_id, ref_id;
412 	s64 ref_phase;
413 	int rc;
414 
415 	/* Get currently connected reference */
416 	rc = zl3073x_dpll_connected_ref_get(zldpll, &conn_id);
417 	if (rc)
418 		return rc;
419 
420 	/* Report phase offset only for currently connected pin if the phase
421 	 * monitor feature is disabled and only if the input pin signal is
422 	 * present.
423 	 */
424 	ref_id = zl3073x_input_pin_ref_get(pin->id);
425 	ref = zl3073x_ref_state_get(zldev, ref_id);
426 	if ((!zldpll->phase_monitor && ref_id != conn_id) ||
427 	    !zl3073x_ref_is_status_ok(ref)) {
428 		*phase_offset = 0;
429 		return 0;
430 	}
431 
432 	ref_phase = pin->phase_offset;
433 
434 	/* The DPLL being locked to a higher freq than the current ref
435 	 * the phase offset is modded to the period of the signal
436 	 * the dpll is locked to.
437 	 */
438 	if (ZL3073X_DPLL_REF_IS_VALID(conn_id) && conn_id != ref_id) {
439 		u32 conn_freq, ref_freq;
440 
441 		/* Get frequency of connected and given ref */
442 		conn_freq = zl3073x_dev_ref_freq_get(zldev, conn_id);
443 		ref_freq = zl3073x_ref_freq_get(ref);
444 
445 		if (conn_freq > ref_freq) {
446 			s64 conn_period, div_factor;
447 
448 			conn_period = div_s64(PSEC_PER_SEC, conn_freq);
449 			div_factor = div64_s64(ref_phase, conn_period);
450 			ref_phase -= conn_period * div_factor;
451 		}
452 	}
453 
454 	*phase_offset = ref_phase * DPLL_PHASE_OFFSET_DIVIDER;
455 
456 	return rc;
457 }
458 
459 static int
460 zl3073x_dpll_input_pin_phase_adjust_get(const struct dpll_pin *dpll_pin,
461 					void *pin_priv,
462 					const struct dpll_device *dpll,
463 					void *dpll_priv,
464 					s32 *phase_adjust,
465 					struct netlink_ext_ack *extack)
466 {
467 	struct zl3073x_dpll *zldpll = dpll_priv;
468 	struct zl3073x_dev *zldev = zldpll->dev;
469 	struct zl3073x_dpll_pin *pin = pin_priv;
470 	const struct zl3073x_ref *ref;
471 	s64 phase_comp;
472 	u8 ref_id;
473 
474 	/* Read reference configuration */
475 	ref_id = zl3073x_input_pin_ref_get(pin->id);
476 	ref = zl3073x_ref_state_get(zldev, ref_id);
477 
478 	/* Perform sign extension for 48bit signed value */
479 	phase_comp = sign_extend64(ref->phase_comp, 47);
480 
481 	/* Reverse two's complement negation applied during set and convert
482 	 * to 32bit signed int
483 	 */
484 	*phase_adjust = (s32)-phase_comp;
485 
486 	return 0;
487 }
488 
489 static int
490 zl3073x_dpll_input_pin_phase_adjust_set(const struct dpll_pin *dpll_pin,
491 					void *pin_priv,
492 					const struct dpll_device *dpll,
493 					void *dpll_priv,
494 					s32 phase_adjust,
495 					struct netlink_ext_ack *extack)
496 {
497 	struct zl3073x_dpll *zldpll = dpll_priv;
498 	struct zl3073x_dev *zldev = zldpll->dev;
499 	struct zl3073x_dpll_pin *pin = pin_priv;
500 	struct zl3073x_ref ref;
501 	u8 ref_id;
502 
503 	/* Read reference configuration */
504 	ref_id = zl3073x_input_pin_ref_get(pin->id);
505 	ref = *zl3073x_ref_state_get(zldev, ref_id);
506 
507 	/* The value in the register is stored as two's complement negation
508 	 * of requested value.
509 	 */
510 	ref.phase_comp = -phase_adjust;
511 
512 	/* Update reference configuration */
513 	return zl3073x_ref_state_set(zldev, ref_id, &ref);
514 }
515 
516 /**
517  * zl3073x_dpll_ref_prio_get - get priority for given input pin
518  * @pin: pointer to pin
519  * @prio: place to store priority
520  *
521  * Reads current priority for the given input pin and stores the value
522  * to @prio.
523  *
524  * Return: 0 on success, <0 on error
525  */
526 static int
527 zl3073x_dpll_ref_prio_get(struct zl3073x_dpll_pin *pin, u8 *prio)
528 {
529 	struct zl3073x_dpll *zldpll = pin->dpll;
530 	struct zl3073x_dev *zldev = zldpll->dev;
531 	u8 ref, ref_prio;
532 	int rc;
533 
534 	guard(mutex)(&zldev->multiop_lock);
535 
536 	/* Read DPLL configuration */
537 	rc = zl3073x_mb_op(zldev, ZL_REG_DPLL_MB_SEM, ZL_DPLL_MB_SEM_RD,
538 			   ZL_REG_DPLL_MB_MASK, BIT(zldpll->id));
539 	if (rc)
540 		return rc;
541 
542 	/* Read reference priority - one value for P&N pins (4 bits/pin) */
543 	ref = zl3073x_input_pin_ref_get(pin->id);
544 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_REF_PRIO(ref / 2),
545 			     &ref_prio);
546 	if (rc)
547 		return rc;
548 
549 	/* Select nibble according pin type */
550 	if (zl3073x_dpll_is_p_pin(pin))
551 		*prio = FIELD_GET(ZL_DPLL_REF_PRIO_REF_P, ref_prio);
552 	else
553 		*prio = FIELD_GET(ZL_DPLL_REF_PRIO_REF_N, ref_prio);
554 
555 	return rc;
556 }
557 
558 /**
559  * zl3073x_dpll_ref_prio_set - set priority for given input pin
560  * @pin: pointer to pin
561  * @prio: place to store priority
562  *
563  * Sets priority for the given input pin.
564  *
565  * Return: 0 on success, <0 on error
566  */
567 static int
568 zl3073x_dpll_ref_prio_set(struct zl3073x_dpll_pin *pin, u8 prio)
569 {
570 	struct zl3073x_dpll *zldpll = pin->dpll;
571 	struct zl3073x_dev *zldev = zldpll->dev;
572 	u8 ref, ref_prio;
573 	int rc;
574 
575 	guard(mutex)(&zldev->multiop_lock);
576 
577 	/* Read DPLL configuration into mailbox */
578 	rc = zl3073x_mb_op(zldev, ZL_REG_DPLL_MB_SEM, ZL_DPLL_MB_SEM_RD,
579 			   ZL_REG_DPLL_MB_MASK, BIT(zldpll->id));
580 	if (rc)
581 		return rc;
582 
583 	/* Read reference priority - one value shared between P&N pins */
584 	ref = zl3073x_input_pin_ref_get(pin->id);
585 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_REF_PRIO(ref / 2), &ref_prio);
586 	if (rc)
587 		return rc;
588 
589 	/* Update nibble according pin type */
590 	if (zl3073x_dpll_is_p_pin(pin)) {
591 		ref_prio &= ~ZL_DPLL_REF_PRIO_REF_P;
592 		ref_prio |= FIELD_PREP(ZL_DPLL_REF_PRIO_REF_P, prio);
593 	} else {
594 		ref_prio &= ~ZL_DPLL_REF_PRIO_REF_N;
595 		ref_prio |= FIELD_PREP(ZL_DPLL_REF_PRIO_REF_N, prio);
596 	}
597 
598 	/* Update reference priority */
599 	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_REF_PRIO(ref / 2), ref_prio);
600 	if (rc)
601 		return rc;
602 
603 	/* Commit configuration */
604 	return zl3073x_mb_op(zldev, ZL_REG_DPLL_MB_SEM, ZL_DPLL_MB_SEM_WR,
605 			     ZL_REG_DPLL_MB_MASK, BIT(zldpll->id));
606 }
607 
608 /**
609  * zl3073x_dpll_ref_state_get - get status for given input pin
610  * @pin: pointer to pin
611  * @state: place to store status
612  *
613  * Checks current status for the given input pin and stores the value
614  * to @state.
615  *
616  * Return: 0 on success, <0 on error
617  */
618 static int
619 zl3073x_dpll_ref_state_get(struct zl3073x_dpll_pin *pin,
620 			   enum dpll_pin_state *state)
621 {
622 	struct zl3073x_dpll *zldpll = pin->dpll;
623 	struct zl3073x_dev *zldev = zldpll->dev;
624 	u8 ref, ref_conn;
625 	int rc;
626 
627 	ref = zl3073x_input_pin_ref_get(pin->id);
628 
629 	/* Get currently connected reference */
630 	rc = zl3073x_dpll_connected_ref_get(zldpll, &ref_conn);
631 	if (rc)
632 		return rc;
633 
634 	if (ref == ref_conn) {
635 		*state = DPLL_PIN_STATE_CONNECTED;
636 		return 0;
637 	}
638 
639 	/* If the DPLL is running in automatic mode and the reference is
640 	 * selectable and its monitor does not report any error then report
641 	 * pin as selectable.
642 	 */
643 	if (zldpll->refsel_mode == ZL_DPLL_MODE_REFSEL_MODE_AUTO &&
644 	    zl3073x_dev_ref_is_status_ok(zldev, ref) && pin->selectable) {
645 		*state = DPLL_PIN_STATE_SELECTABLE;
646 		return 0;
647 	}
648 
649 	/* Otherwise report the pin as disconnected */
650 	*state = DPLL_PIN_STATE_DISCONNECTED;
651 
652 	return 0;
653 }
654 
655 static int
656 zl3073x_dpll_input_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin,
657 					 void *pin_priv,
658 					 const struct dpll_device *dpll,
659 					 void *dpll_priv,
660 					 enum dpll_pin_state *state,
661 					 struct netlink_ext_ack *extack)
662 {
663 	struct zl3073x_dpll_pin *pin = pin_priv;
664 
665 	return zl3073x_dpll_ref_state_get(pin, state);
666 }
667 
668 static int
669 zl3073x_dpll_input_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin,
670 					 void *pin_priv,
671 					 const struct dpll_device *dpll,
672 					 void *dpll_priv,
673 					 enum dpll_pin_state state,
674 					 struct netlink_ext_ack *extack)
675 {
676 	struct zl3073x_dpll *zldpll = dpll_priv;
677 	struct zl3073x_dpll_pin *pin = pin_priv;
678 	u8 new_ref;
679 	int rc;
680 
681 	switch (zldpll->refsel_mode) {
682 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
683 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
684 	case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER:
685 		if (state == DPLL_PIN_STATE_CONNECTED) {
686 			/* Choose the pin as new selected reference */
687 			new_ref = zl3073x_input_pin_ref_get(pin->id);
688 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
689 			/* No reference */
690 			new_ref = ZL3073X_DPLL_REF_NONE;
691 		} else {
692 			NL_SET_ERR_MSG_MOD(extack,
693 					   "Invalid pin state for manual mode");
694 			return -EINVAL;
695 		}
696 
697 		rc = zl3073x_dpll_selected_ref_set(zldpll, new_ref);
698 		break;
699 
700 	case ZL_DPLL_MODE_REFSEL_MODE_AUTO:
701 		if (state == DPLL_PIN_STATE_SELECTABLE) {
702 			if (pin->selectable)
703 				return 0; /* Pin is already selectable */
704 
705 			/* Restore pin priority in HW */
706 			rc = zl3073x_dpll_ref_prio_set(pin, pin->prio);
707 			if (rc)
708 				return rc;
709 
710 			/* Mark pin as selectable */
711 			pin->selectable = true;
712 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
713 			if (!pin->selectable)
714 				return 0; /* Pin is already disconnected */
715 
716 			/* Set pin priority to none in HW */
717 			rc = zl3073x_dpll_ref_prio_set(pin,
718 						       ZL_DPLL_REF_PRIO_NONE);
719 			if (rc)
720 				return rc;
721 
722 			/* Mark pin as non-selectable */
723 			pin->selectable = false;
724 		} else {
725 			NL_SET_ERR_MSG(extack,
726 				       "Invalid pin state for automatic mode");
727 			return -EINVAL;
728 		}
729 		break;
730 
731 	default:
732 		/* In other modes we cannot change input reference */
733 		NL_SET_ERR_MSG(extack,
734 			       "Pin state cannot be changed in current mode");
735 		rc = -EOPNOTSUPP;
736 		break;
737 	}
738 
739 	return rc;
740 }
741 
742 static int
743 zl3073x_dpll_input_pin_prio_get(const struct dpll_pin *dpll_pin, void *pin_priv,
744 				const struct dpll_device *dpll, void *dpll_priv,
745 				u32 *prio, struct netlink_ext_ack *extack)
746 {
747 	struct zl3073x_dpll_pin *pin = pin_priv;
748 
749 	*prio = pin->prio;
750 
751 	return 0;
752 }
753 
754 static int
755 zl3073x_dpll_input_pin_prio_set(const struct dpll_pin *dpll_pin, void *pin_priv,
756 				const struct dpll_device *dpll, void *dpll_priv,
757 				u32 prio, struct netlink_ext_ack *extack)
758 {
759 	struct zl3073x_dpll_pin *pin = pin_priv;
760 	int rc;
761 
762 	if (prio > ZL_DPLL_REF_PRIO_MAX)
763 		return -EINVAL;
764 
765 	/* If the pin is selectable then update HW registers */
766 	if (pin->selectable) {
767 		rc = zl3073x_dpll_ref_prio_set(pin, prio);
768 		if (rc)
769 			return rc;
770 	}
771 
772 	/* Save priority */
773 	pin->prio = prio;
774 
775 	return 0;
776 }
777 
778 static int
779 zl3073x_dpll_output_pin_esync_get(const struct dpll_pin *dpll_pin,
780 				  void *pin_priv,
781 				  const struct dpll_device *dpll,
782 				  void *dpll_priv,
783 				  struct dpll_pin_esync *esync,
784 				  struct netlink_ext_ack *extack)
785 {
786 	struct zl3073x_dpll *zldpll = dpll_priv;
787 	struct zl3073x_dev *zldev = zldpll->dev;
788 	struct zl3073x_dpll_pin *pin = pin_priv;
789 	const struct zl3073x_synth *synth;
790 	const struct zl3073x_out *out;
791 	u8 clock_type, out_id;
792 	u32 synth_freq;
793 
794 	out_id = zl3073x_output_pin_out_get(pin->id);
795 	out = zl3073x_out_state_get(zldev, out_id);
796 
797 	/* If N-division is enabled, esync is not supported. The register used
798 	 * for N-division is also used for the esync divider so both cannot
799 	 * be used.
800 	 */
801 	switch (zl3073x_out_signal_format_get(out)) {
802 	case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV:
803 	case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV_INV:
804 		return -EOPNOTSUPP;
805 	default:
806 		break;
807 	}
808 
809 	/* Get attached synth frequency */
810 	synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(out));
811 	synth_freq = zl3073x_synth_freq_get(synth);
812 
813 	clock_type = FIELD_GET(ZL_OUTPUT_MODE_CLOCK_TYPE, out->mode);
814 	if (clock_type != ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC) {
815 		/* No need to read esync data if it is not enabled */
816 		esync->freq = 0;
817 		esync->pulse = 0;
818 
819 		goto finish;
820 	}
821 
822 	/* Compute esync frequency */
823 	esync->freq = synth_freq / out->div / out->esync_n_period;
824 
825 	/* By comparing the esync_pulse_width to the half of the pulse width
826 	 * the esync pulse percentage can be determined.
827 	 * Note that half pulse width is in units of half synth cycles, which
828 	 * is why it reduces down to be output_div.
829 	 */
830 	esync->pulse = (50 * out->esync_n_width) / out->div;
831 
832 finish:
833 	/* Set supported esync ranges if the pin supports esync control and
834 	 * if the output frequency is > 1 Hz.
835 	 */
836 	if (pin->esync_control && (synth_freq / out->div) > 1) {
837 		esync->range = esync_freq_ranges;
838 		esync->range_num = ARRAY_SIZE(esync_freq_ranges);
839 	} else {
840 		esync->range = NULL;
841 		esync->range_num = 0;
842 	}
843 
844 	return 0;
845 }
846 
847 static int
848 zl3073x_dpll_output_pin_esync_set(const struct dpll_pin *dpll_pin,
849 				  void *pin_priv,
850 				  const struct dpll_device *dpll,
851 				  void *dpll_priv, u64 freq,
852 				  struct netlink_ext_ack *extack)
853 {
854 	struct zl3073x_dpll *zldpll = dpll_priv;
855 	struct zl3073x_dev *zldev = zldpll->dev;
856 	struct zl3073x_dpll_pin *pin = pin_priv;
857 	const struct zl3073x_synth *synth;
858 	struct zl3073x_out out;
859 	u8 clock_type, out_id;
860 	u32 synth_freq;
861 
862 	out_id = zl3073x_output_pin_out_get(pin->id);
863 	out = *zl3073x_out_state_get(zldev, out_id);
864 
865 	/* If N-division is enabled, esync is not supported. The register used
866 	 * for N-division is also used for the esync divider so both cannot
867 	 * be used.
868 	 */
869 	switch (zl3073x_out_signal_format_get(&out)) {
870 	case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV:
871 	case ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV_INV:
872 		return -EOPNOTSUPP;
873 	default:
874 		break;
875 	}
876 
877 	/* Select clock type */
878 	if (freq)
879 		clock_type = ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC;
880 	else
881 		clock_type = ZL_OUTPUT_MODE_CLOCK_TYPE_NORMAL;
882 
883 	/* Update clock type in output mode */
884 	out.mode &= ~ZL_OUTPUT_MODE_CLOCK_TYPE;
885 	out.mode |= FIELD_PREP(ZL_OUTPUT_MODE_CLOCK_TYPE, clock_type);
886 
887 	/* If esync is being disabled just write mailbox and finish */
888 	if (!freq)
889 		goto write_mailbox;
890 
891 	/* Get attached synth frequency */
892 	synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out));
893 	synth_freq = zl3073x_synth_freq_get(synth);
894 
895 	/* Compute and update esync period */
896 	out.esync_n_period = synth_freq / (u32)freq / out.div;
897 
898 	/* Half of the period in units of 1/2 synth cycle can be represented by
899 	 * the output_div. To get the supported esync pulse width of 25% of the
900 	 * period the output_div can just be divided by 2. Note that this
901 	 * assumes that output_div is even, otherwise some resolution will be
902 	 * lost.
903 	 */
904 	out.esync_n_width = out.div / 2;
905 
906 write_mailbox:
907 	/* Commit output configuration */
908 	return zl3073x_out_state_set(zldev, out_id, &out);
909 }
910 
911 static int
912 zl3073x_dpll_output_pin_frequency_get(const struct dpll_pin *dpll_pin,
913 				      void *pin_priv,
914 				      const struct dpll_device *dpll,
915 				      void *dpll_priv, u64 *frequency,
916 				      struct netlink_ext_ack *extack)
917 {
918 	struct zl3073x_dpll *zldpll = dpll_priv;
919 	struct zl3073x_dpll_pin *pin = pin_priv;
920 
921 	*frequency = zl3073x_dev_output_pin_freq_get(zldpll->dev, pin->id);
922 
923 	return 0;
924 }
925 
926 static int
927 zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin,
928 				      void *pin_priv,
929 				      const struct dpll_device *dpll,
930 				      void *dpll_priv, u64 frequency,
931 				      struct netlink_ext_ack *extack)
932 {
933 	struct zl3073x_dpll *zldpll = dpll_priv;
934 	struct zl3073x_dev *zldev = zldpll->dev;
935 	struct zl3073x_dpll_pin *pin = pin_priv;
936 	const struct zl3073x_synth *synth;
937 	u8 out_id, signal_format;
938 	u32 new_div, synth_freq;
939 	struct zl3073x_out out;
940 
941 	out_id = zl3073x_output_pin_out_get(pin->id);
942 	out = *zl3073x_out_state_get(zldev, out_id);
943 
944 	/* Get attached synth frequency and compute new divisor */
945 	synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out));
946 	synth_freq = zl3073x_synth_freq_get(synth);
947 	new_div = synth_freq / (u32)frequency;
948 
949 	/* Get used signal format for the given output */
950 	signal_format = zl3073x_out_signal_format_get(&out);
951 
952 	/* Check signal format */
953 	if (signal_format != ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV &&
954 	    signal_format != ZL_OUTPUT_MODE_SIGNAL_FORMAT_2_NDIV_INV) {
955 		/* For non N-divided signal formats the frequency is computed
956 		 * as division of synth frequency and output divisor.
957 		 */
958 		out.div = new_div;
959 
960 		/* For 50/50 duty cycle the divisor is equal to width */
961 		out.width = new_div;
962 
963 		/* Commit output configuration */
964 		return zl3073x_out_state_set(zldev, out_id, &out);
965 	}
966 
967 	if (zl3073x_dpll_is_p_pin(pin)) {
968 		/* We are going to change output frequency for P-pin but
969 		 * if the requested frequency is less than current N-pin
970 		 * frequency then indicate a failure as we are not able
971 		 * to compute N-pin divisor to keep its frequency unchanged.
972 		 *
973 		 * Update divisor for N-pin to keep N-pin frequency.
974 		 */
975 		out.esync_n_period = (out.esync_n_period * out.div) / new_div;
976 		if (!out.esync_n_period)
977 			return -EINVAL;
978 
979 		/* Update the output divisor */
980 		out.div = new_div;
981 
982 		/* For 50/50 duty cycle the divisor is equal to width */
983 		out.width = out.div;
984 	} else {
985 		/* We are going to change frequency of N-pin but if
986 		 * the requested freq is greater or equal than freq of P-pin
987 		 * in the output pair we cannot compute divisor for the N-pin.
988 		 * In this case indicate a failure.
989 		 *
990 		 * Update divisor for N-pin
991 		 */
992 		out.esync_n_period = div64_u64(synth_freq, frequency * out.div);
993 		if (!out.esync_n_period)
994 			return -EINVAL;
995 	}
996 
997 	/* For 50/50 duty cycle the divisor is equal to width */
998 	out.esync_n_width = out.esync_n_period;
999 
1000 	/* Commit output configuration */
1001 	return zl3073x_out_state_set(zldev, out_id, &out);
1002 }
1003 
1004 static int
1005 zl3073x_dpll_output_pin_phase_adjust_get(const struct dpll_pin *dpll_pin,
1006 					 void *pin_priv,
1007 					 const struct dpll_device *dpll,
1008 					 void *dpll_priv,
1009 					 s32 *phase_adjust,
1010 					 struct netlink_ext_ack *extack)
1011 {
1012 	struct zl3073x_dpll *zldpll = dpll_priv;
1013 	struct zl3073x_dev *zldev = zldpll->dev;
1014 	struct zl3073x_dpll_pin *pin = pin_priv;
1015 	const struct zl3073x_out *out;
1016 	u8 out_id;
1017 
1018 	out_id = zl3073x_output_pin_out_get(pin->id);
1019 	out = zl3073x_out_state_get(zldev, out_id);
1020 
1021 	/* The value in the register is expressed in half synth clock cycles. */
1022 	*phase_adjust = out->phase_comp * pin->phase_gran;
1023 
1024 	return 0;
1025 }
1026 
1027 static int
1028 zl3073x_dpll_output_pin_phase_adjust_set(const struct dpll_pin *dpll_pin,
1029 					 void *pin_priv,
1030 					 const struct dpll_device *dpll,
1031 					 void *dpll_priv,
1032 					 s32 phase_adjust,
1033 					 struct netlink_ext_ack *extack)
1034 {
1035 	struct zl3073x_dpll *zldpll = dpll_priv;
1036 	struct zl3073x_dev *zldev = zldpll->dev;
1037 	struct zl3073x_dpll_pin *pin = pin_priv;
1038 	struct zl3073x_out out;
1039 	u8 out_id;
1040 
1041 	out_id = zl3073x_output_pin_out_get(pin->id);
1042 	out = *zl3073x_out_state_get(zldev, out_id);
1043 
1044 	/* The value in the register is expressed in half synth clock cycles. */
1045 	out.phase_comp = phase_adjust / pin->phase_gran;
1046 
1047 	/* Update output configuration from mailbox */
1048 	return zl3073x_out_state_set(zldev, out_id, &out);
1049 }
1050 
1051 static int
1052 zl3073x_dpll_output_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin,
1053 					  void *pin_priv,
1054 					  const struct dpll_device *dpll,
1055 					  void *dpll_priv,
1056 					  enum dpll_pin_state *state,
1057 					  struct netlink_ext_ack *extack)
1058 {
1059 	/* If the output pin is registered then it is always connected */
1060 	*state = DPLL_PIN_STATE_CONNECTED;
1061 
1062 	return 0;
1063 }
1064 
1065 static int
1066 zl3073x_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
1067 			     enum dpll_lock_status *status,
1068 			     enum dpll_lock_status_error *status_error,
1069 			     struct netlink_ext_ack *extack)
1070 {
1071 	struct zl3073x_dpll *zldpll = dpll_priv;
1072 	struct zl3073x_dev *zldev = zldpll->dev;
1073 	u8 mon_status, state;
1074 	int rc;
1075 
1076 	switch (zldpll->refsel_mode) {
1077 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
1078 	case ZL_DPLL_MODE_REFSEL_MODE_NCO:
1079 		/* In FREERUN and NCO modes the DPLL is always unlocked */
1080 		*status = DPLL_LOCK_STATUS_UNLOCKED;
1081 
1082 		return 0;
1083 	default:
1084 		break;
1085 	}
1086 
1087 	/* Read DPLL monitor status */
1088 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MON_STATUS(zldpll->id),
1089 			     &mon_status);
1090 	if (rc)
1091 		return rc;
1092 	state = FIELD_GET(ZL_DPLL_MON_STATUS_STATE, mon_status);
1093 
1094 	switch (state) {
1095 	case ZL_DPLL_MON_STATUS_STATE_LOCK:
1096 		if (FIELD_GET(ZL_DPLL_MON_STATUS_HO_READY, mon_status))
1097 			*status = DPLL_LOCK_STATUS_LOCKED_HO_ACQ;
1098 		else
1099 			*status = DPLL_LOCK_STATUS_LOCKED;
1100 		break;
1101 	case ZL_DPLL_MON_STATUS_STATE_HOLDOVER:
1102 	case ZL_DPLL_MON_STATUS_STATE_ACQUIRING:
1103 		*status = DPLL_LOCK_STATUS_HOLDOVER;
1104 		break;
1105 	default:
1106 		dev_warn(zldev->dev, "Unknown DPLL monitor status: 0x%02x\n",
1107 			 mon_status);
1108 		*status = DPLL_LOCK_STATUS_UNLOCKED;
1109 		break;
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 static int
1116 zl3073x_dpll_supported_modes_get(const struct dpll_device *dpll,
1117 				 void *dpll_priv, unsigned long *modes,
1118 				 struct netlink_ext_ack *extack)
1119 {
1120 	struct zl3073x_dpll *zldpll = dpll_priv;
1121 
1122 	/* We support switching between automatic and manual mode, except in
1123 	 * a case where the DPLL channel is configured to run in NCO mode.
1124 	 * In this case, report only the manual mode to which the NCO is mapped
1125 	 * as the only supported one.
1126 	 */
1127 	if (zldpll->refsel_mode != ZL_DPLL_MODE_REFSEL_MODE_NCO)
1128 		__set_bit(DPLL_MODE_AUTOMATIC, modes);
1129 
1130 	__set_bit(DPLL_MODE_MANUAL, modes);
1131 
1132 	return 0;
1133 }
1134 
1135 static int
1136 zl3073x_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
1137 		      enum dpll_mode *mode, struct netlink_ext_ack *extack)
1138 {
1139 	struct zl3073x_dpll *zldpll = dpll_priv;
1140 
1141 	switch (zldpll->refsel_mode) {
1142 	case ZL_DPLL_MODE_REFSEL_MODE_FREERUN:
1143 	case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER:
1144 	case ZL_DPLL_MODE_REFSEL_MODE_NCO:
1145 	case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK:
1146 		/* Use MANUAL for device FREERUN, HOLDOVER, NCO and
1147 		 * REFLOCK modes
1148 		 */
1149 		*mode = DPLL_MODE_MANUAL;
1150 		break;
1151 	case ZL_DPLL_MODE_REFSEL_MODE_AUTO:
1152 		/* Use AUTO for device AUTO mode */
1153 		*mode = DPLL_MODE_AUTOMATIC;
1154 		break;
1155 	default:
1156 		return -EINVAL;
1157 	}
1158 
1159 	return 0;
1160 }
1161 
1162 static int
1163 zl3073x_dpll_phase_offset_avg_factor_get(const struct dpll_device *dpll,
1164 					 void *dpll_priv, u32 *factor,
1165 					 struct netlink_ext_ack *extack)
1166 {
1167 	struct zl3073x_dpll *zldpll = dpll_priv;
1168 
1169 	*factor = zl3073x_dev_phase_avg_factor_get(zldpll->dev);
1170 
1171 	return 0;
1172 }
1173 
1174 static void
1175 zl3073x_dpll_change_work(struct work_struct *work)
1176 {
1177 	struct zl3073x_dpll *zldpll;
1178 
1179 	zldpll = container_of(work, struct zl3073x_dpll, change_work);
1180 	dpll_device_change_ntf(zldpll->dpll_dev);
1181 }
1182 
1183 static int
1184 zl3073x_dpll_phase_offset_avg_factor_set(const struct dpll_device *dpll,
1185 					 void *dpll_priv, u32 factor,
1186 					 struct netlink_ext_ack *extack)
1187 {
1188 	struct zl3073x_dpll *item, *zldpll = dpll_priv;
1189 	int rc;
1190 
1191 	if (factor > 15) {
1192 		NL_SET_ERR_MSG_FMT(extack,
1193 				   "Phase offset average factor has to be from range <0,15>");
1194 		return -EINVAL;
1195 	}
1196 
1197 	rc = zl3073x_dev_phase_avg_factor_set(zldpll->dev, factor);
1198 	if (rc) {
1199 		NL_SET_ERR_MSG_FMT(extack,
1200 				   "Failed to set phase offset averaging factor");
1201 		return rc;
1202 	}
1203 
1204 	/* The averaging factor is common for all DPLL channels so after change
1205 	 * we have to send a notification for other DPLL devices.
1206 	 */
1207 	list_for_each_entry(item, &zldpll->dev->dplls, list) {
1208 		if (item != zldpll)
1209 			schedule_work(&item->change_work);
1210 	}
1211 
1212 	return 0;
1213 }
1214 
1215 static int
1216 zl3073x_dpll_mode_set(const struct dpll_device *dpll, void *dpll_priv,
1217 		      enum dpll_mode mode, struct netlink_ext_ack *extack)
1218 {
1219 	struct zl3073x_dpll *zldpll = dpll_priv;
1220 	u8 hw_mode, mode_refsel, ref;
1221 	int rc;
1222 
1223 	rc = zl3073x_dpll_selected_ref_get(zldpll, &ref);
1224 	if (rc) {
1225 		NL_SET_ERR_MSG_MOD(extack, "failed to get selected reference");
1226 		return rc;
1227 	}
1228 
1229 	if (mode == DPLL_MODE_MANUAL) {
1230 		/* We are switching from automatic to manual mode:
1231 		 * - if we have a valid reference selected during auto mode then
1232 		 *   we will switch to forced reference lock mode and use this
1233 		 *   reference for selection
1234 		 * - if NO valid reference is selected, we will switch to forced
1235 		 *   holdover mode or freerun mode, depending on the current
1236 		 *   lock status
1237 		 */
1238 		if (ZL3073X_DPLL_REF_IS_VALID(ref))
1239 			hw_mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK;
1240 		else if (zldpll->lock_status == DPLL_LOCK_STATUS_UNLOCKED)
1241 			hw_mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN;
1242 		else
1243 			hw_mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER;
1244 	} else {
1245 		/* We are switching from manual to automatic mode:
1246 		 * - if there is a valid reference selected then ensure that
1247 		 *   it is selectable after switch to automatic mode
1248 		 * - switch to automatic mode
1249 		 */
1250 		struct zl3073x_dpll_pin *pin;
1251 
1252 		pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref);
1253 		if (pin && !pin->selectable) {
1254 			/* Restore pin priority in HW */
1255 			rc = zl3073x_dpll_ref_prio_set(pin, pin->prio);
1256 			if (rc) {
1257 				NL_SET_ERR_MSG_MOD(extack,
1258 						   "failed to restore pin priority");
1259 				return rc;
1260 			}
1261 
1262 			pin->selectable = true;
1263 		}
1264 
1265 		hw_mode = ZL_DPLL_MODE_REFSEL_MODE_AUTO;
1266 	}
1267 
1268 	/* Build mode_refsel value */
1269 	mode_refsel = FIELD_PREP(ZL_DPLL_MODE_REFSEL_MODE, hw_mode);
1270 
1271 	if (ZL3073X_DPLL_REF_IS_VALID(ref))
1272 		mode_refsel |= FIELD_PREP(ZL_DPLL_MODE_REFSEL_REF, ref);
1273 
1274 	/* Update dpll_mode_refsel register */
1275 	rc = zl3073x_write_u8(zldpll->dev, ZL_REG_DPLL_MODE_REFSEL(zldpll->id),
1276 			      mode_refsel);
1277 	if (rc) {
1278 		NL_SET_ERR_MSG_MOD(extack,
1279 				   "failed to set reference selection mode");
1280 		return rc;
1281 	}
1282 
1283 	zldpll->refsel_mode = hw_mode;
1284 
1285 	if (ZL3073X_DPLL_REF_IS_VALID(ref))
1286 		zldpll->forced_ref = ref;
1287 
1288 	return 0;
1289 }
1290 
1291 static int
1292 zl3073x_dpll_phase_offset_monitor_get(const struct dpll_device *dpll,
1293 				      void *dpll_priv,
1294 				      enum dpll_feature_state *state,
1295 				      struct netlink_ext_ack *extack)
1296 {
1297 	struct zl3073x_dpll *zldpll = dpll_priv;
1298 
1299 	if (zldpll->phase_monitor)
1300 		*state = DPLL_FEATURE_STATE_ENABLE;
1301 	else
1302 		*state = DPLL_FEATURE_STATE_DISABLE;
1303 
1304 	return 0;
1305 }
1306 
1307 static int
1308 zl3073x_dpll_phase_offset_monitor_set(const struct dpll_device *dpll,
1309 				      void *dpll_priv,
1310 				      enum dpll_feature_state state,
1311 				      struct netlink_ext_ack *extack)
1312 {
1313 	struct zl3073x_dpll *zldpll = dpll_priv;
1314 
1315 	zldpll->phase_monitor = (state == DPLL_FEATURE_STATE_ENABLE);
1316 
1317 	return 0;
1318 }
1319 
1320 static const struct dpll_pin_ops zl3073x_dpll_input_pin_ops = {
1321 	.direction_get = zl3073x_dpll_pin_direction_get,
1322 	.esync_get = zl3073x_dpll_input_pin_esync_get,
1323 	.esync_set = zl3073x_dpll_input_pin_esync_set,
1324 	.ffo_get = zl3073x_dpll_input_pin_ffo_get,
1325 	.frequency_get = zl3073x_dpll_input_pin_frequency_get,
1326 	.frequency_set = zl3073x_dpll_input_pin_frequency_set,
1327 	.phase_offset_get = zl3073x_dpll_input_pin_phase_offset_get,
1328 	.phase_adjust_get = zl3073x_dpll_input_pin_phase_adjust_get,
1329 	.phase_adjust_set = zl3073x_dpll_input_pin_phase_adjust_set,
1330 	.prio_get = zl3073x_dpll_input_pin_prio_get,
1331 	.prio_set = zl3073x_dpll_input_pin_prio_set,
1332 	.state_on_dpll_get = zl3073x_dpll_input_pin_state_on_dpll_get,
1333 	.state_on_dpll_set = zl3073x_dpll_input_pin_state_on_dpll_set,
1334 };
1335 
1336 static const struct dpll_pin_ops zl3073x_dpll_output_pin_ops = {
1337 	.direction_get = zl3073x_dpll_pin_direction_get,
1338 	.esync_get = zl3073x_dpll_output_pin_esync_get,
1339 	.esync_set = zl3073x_dpll_output_pin_esync_set,
1340 	.frequency_get = zl3073x_dpll_output_pin_frequency_get,
1341 	.frequency_set = zl3073x_dpll_output_pin_frequency_set,
1342 	.phase_adjust_get = zl3073x_dpll_output_pin_phase_adjust_get,
1343 	.phase_adjust_set = zl3073x_dpll_output_pin_phase_adjust_set,
1344 	.state_on_dpll_get = zl3073x_dpll_output_pin_state_on_dpll_get,
1345 };
1346 
1347 static const struct dpll_device_ops zl3073x_dpll_device_ops = {
1348 	.lock_status_get = zl3073x_dpll_lock_status_get,
1349 	.mode_get = zl3073x_dpll_mode_get,
1350 	.mode_set = zl3073x_dpll_mode_set,
1351 	.phase_offset_avg_factor_get = zl3073x_dpll_phase_offset_avg_factor_get,
1352 	.phase_offset_avg_factor_set = zl3073x_dpll_phase_offset_avg_factor_set,
1353 	.phase_offset_monitor_get = zl3073x_dpll_phase_offset_monitor_get,
1354 	.phase_offset_monitor_set = zl3073x_dpll_phase_offset_monitor_set,
1355 	.supported_modes_get = zl3073x_dpll_supported_modes_get,
1356 };
1357 
1358 /**
1359  * zl3073x_dpll_pin_alloc - allocate DPLL pin
1360  * @zldpll: pointer to zl3073x_dpll
1361  * @dir: pin direction
1362  * @id: pin id
1363  *
1364  * Allocates and initializes zl3073x_dpll_pin structure for given
1365  * pin id and direction.
1366  *
1367  * Return: pointer to allocated structure on success, error pointer on error
1368  */
1369 static struct zl3073x_dpll_pin *
1370 zl3073x_dpll_pin_alloc(struct zl3073x_dpll *zldpll, enum dpll_pin_direction dir,
1371 		       u8 id)
1372 {
1373 	struct zl3073x_dpll_pin *pin;
1374 
1375 	pin = kzalloc_obj(*pin);
1376 	if (!pin)
1377 		return ERR_PTR(-ENOMEM);
1378 
1379 	pin->dpll = zldpll;
1380 	pin->dir = dir;
1381 	pin->id = id;
1382 
1383 	return pin;
1384 }
1385 
1386 /**
1387  * zl3073x_dpll_pin_free - deallocate DPLL pin
1388  * @pin: pin to free
1389  *
1390  * Deallocates DPLL pin previously allocated by @zl3073x_dpll_pin_alloc.
1391  */
1392 static void
1393 zl3073x_dpll_pin_free(struct zl3073x_dpll_pin *pin)
1394 {
1395 	WARN(pin->dpll_pin, "DPLL pin is still registered\n");
1396 
1397 	kfree(pin);
1398 }
1399 
1400 /**
1401  * zl3073x_dpll_pin_register - register DPLL pin
1402  * @pin: pointer to DPLL pin
1403  * @index: absolute pin index for registration
1404  *
1405  * Registers given DPLL pin into DPLL sub-system.
1406  *
1407  * Return: 0 on success, <0 on error
1408  */
1409 static int
1410 zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index)
1411 {
1412 	struct zl3073x_dpll *zldpll = pin->dpll;
1413 	struct zl3073x_pin_props *props;
1414 	const struct dpll_pin_ops *ops;
1415 	int rc;
1416 
1417 	/* Get pin properties */
1418 	props = zl3073x_pin_props_get(zldpll->dev, pin->dir, pin->id);
1419 	if (IS_ERR(props))
1420 		return PTR_ERR(props);
1421 
1422 	/* Save package label, esync capability and phase adjust granularity */
1423 	strscpy(pin->label, props->package_label);
1424 	pin->esync_control = props->esync_control;
1425 	pin->phase_gran = props->dpll_props.phase_gran;
1426 
1427 	if (zl3073x_dpll_is_input_pin(pin)) {
1428 		rc = zl3073x_dpll_ref_prio_get(pin, &pin->prio);
1429 		if (rc)
1430 			goto err_prio_get;
1431 
1432 		if (pin->prio == ZL_DPLL_REF_PRIO_NONE) {
1433 			/* Clamp prio to max value & mark pin non-selectable */
1434 			pin->prio = ZL_DPLL_REF_PRIO_MAX;
1435 			pin->selectable = false;
1436 		} else {
1437 			/* Mark pin as selectable */
1438 			pin->selectable = true;
1439 		}
1440 	}
1441 
1442 	/* Create or get existing DPLL pin */
1443 	pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, index, THIS_MODULE,
1444 				     &props->dpll_props, &pin->tracker);
1445 	if (IS_ERR(pin->dpll_pin)) {
1446 		rc = PTR_ERR(pin->dpll_pin);
1447 		goto err_pin_get;
1448 	}
1449 	dpll_pin_fwnode_set(pin->dpll_pin, props->fwnode);
1450 
1451 	if (zl3073x_dpll_is_input_pin(pin))
1452 		ops = &zl3073x_dpll_input_pin_ops;
1453 	else
1454 		ops = &zl3073x_dpll_output_pin_ops;
1455 
1456 	/* Register the pin */
1457 	rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin, ops, pin);
1458 	if (rc)
1459 		goto err_register;
1460 
1461 	/* Free pin properties */
1462 	zl3073x_pin_props_put(props);
1463 
1464 	return 0;
1465 
1466 err_register:
1467 	dpll_pin_put(pin->dpll_pin, &pin->tracker);
1468 err_prio_get:
1469 	pin->dpll_pin = NULL;
1470 err_pin_get:
1471 	zl3073x_pin_props_put(props);
1472 
1473 	return rc;
1474 }
1475 
1476 /**
1477  * zl3073x_dpll_pin_unregister - unregister DPLL pin
1478  * @pin: pointer to DPLL pin
1479  *
1480  * Unregisters pin previously registered by @zl3073x_dpll_pin_register.
1481  */
1482 static void
1483 zl3073x_dpll_pin_unregister(struct zl3073x_dpll_pin *pin)
1484 {
1485 	struct zl3073x_dpll *zldpll = pin->dpll;
1486 	const struct dpll_pin_ops *ops;
1487 
1488 	WARN(!pin->dpll_pin, "DPLL pin is not registered\n");
1489 
1490 	if (zl3073x_dpll_is_input_pin(pin))
1491 		ops = &zl3073x_dpll_input_pin_ops;
1492 	else
1493 		ops = &zl3073x_dpll_output_pin_ops;
1494 
1495 	/* Unregister the pin */
1496 	dpll_pin_unregister(zldpll->dpll_dev, pin->dpll_pin, ops, pin);
1497 
1498 	dpll_pin_put(pin->dpll_pin, &pin->tracker);
1499 	pin->dpll_pin = NULL;
1500 }
1501 
1502 /**
1503  * zl3073x_dpll_pins_unregister - unregister all registered DPLL pins
1504  * @zldpll: pointer to zl3073x_dpll structure
1505  *
1506  * Enumerates all DPLL pins registered to given DPLL device and
1507  * unregisters them.
1508  */
1509 static void
1510 zl3073x_dpll_pins_unregister(struct zl3073x_dpll *zldpll)
1511 {
1512 	struct zl3073x_dpll_pin *pin, *next;
1513 
1514 	list_for_each_entry_safe(pin, next, &zldpll->pins, list) {
1515 		zl3073x_dpll_pin_unregister(pin);
1516 		list_del(&pin->list);
1517 		zl3073x_dpll_pin_free(pin);
1518 	}
1519 }
1520 
1521 /**
1522  * zl3073x_dpll_pin_is_registrable - check if the pin is registrable
1523  * @zldpll: pointer to zl3073x_dpll structure
1524  * @dir: pin direction
1525  * @index: pin index
1526  *
1527  * Checks if the given pin can be registered to given DPLL. For both
1528  * directions the pin can be registered if it is enabled. In case of
1529  * differential signal type only P-pin is reported as registrable.
1530  * And additionally for the output pin, the pin can be registered only
1531  * if it is connected to synthesizer that is driven by given DPLL.
1532  *
1533  * Return: true if the pin is registrable, false if not
1534  */
1535 static bool
1536 zl3073x_dpll_pin_is_registrable(struct zl3073x_dpll *zldpll,
1537 				enum dpll_pin_direction dir, u8 index)
1538 {
1539 	struct zl3073x_dev *zldev = zldpll->dev;
1540 	bool is_diff, is_enabled;
1541 	const char *name;
1542 
1543 	if (dir == DPLL_PIN_DIRECTION_INPUT) {
1544 		u8 ref_id = zl3073x_input_pin_ref_get(index);
1545 		const struct zl3073x_ref *ref;
1546 
1547 		/* Skip the pin if the DPLL is running in NCO mode */
1548 		if (zldpll->refsel_mode == ZL_DPLL_MODE_REFSEL_MODE_NCO)
1549 			return false;
1550 
1551 		name = "REF";
1552 		ref = zl3073x_ref_state_get(zldev, ref_id);
1553 		is_diff = zl3073x_ref_is_diff(ref);
1554 		is_enabled = zl3073x_ref_is_enabled(ref);
1555 	} else {
1556 		/* Output P&N pair shares single HW output */
1557 		u8 out = zl3073x_output_pin_out_get(index);
1558 
1559 		/* Skip the pin if it is connected to different DPLL channel */
1560 		if (zl3073x_dev_out_dpll_get(zldev, out) != zldpll->id) {
1561 			dev_dbg(zldev->dev,
1562 				"OUT%u is driven by different DPLL\n", out);
1563 
1564 			return false;
1565 		}
1566 
1567 		name = "OUT";
1568 		is_diff = zl3073x_dev_out_is_diff(zldev, out);
1569 		is_enabled = zl3073x_dev_output_pin_is_enabled(zldev, index);
1570 	}
1571 
1572 	/* Skip N-pin if the corresponding input/output is differential */
1573 	if (is_diff && zl3073x_is_n_pin(index)) {
1574 		dev_dbg(zldev->dev, "%s%u is differential, skipping N-pin\n",
1575 			name, index / 2);
1576 
1577 		return false;
1578 	}
1579 
1580 	/* Skip the pin if it is disabled */
1581 	if (!is_enabled) {
1582 		dev_dbg(zldev->dev, "%s%u%c is disabled\n", name, index / 2,
1583 			zl3073x_is_p_pin(index) ? 'P' : 'N');
1584 
1585 		return false;
1586 	}
1587 
1588 	return true;
1589 }
1590 
1591 /**
1592  * zl3073x_dpll_pins_register - register all registerable DPLL pins
1593  * @zldpll: pointer to zl3073x_dpll structure
1594  *
1595  * Enumerates all possible input/output pins and registers all of them
1596  * that are registrable.
1597  *
1598  * Return: 0 on success, <0 on error
1599  */
1600 static int
1601 zl3073x_dpll_pins_register(struct zl3073x_dpll *zldpll)
1602 {
1603 	struct zl3073x_dpll_pin *pin;
1604 	enum dpll_pin_direction dir;
1605 	u8 id, index;
1606 	int rc;
1607 
1608 	/* Process input pins */
1609 	for (index = 0; index < ZL3073X_NUM_PINS; index++) {
1610 		/* First input pins and then output pins */
1611 		if (index < ZL3073X_NUM_INPUT_PINS) {
1612 			id = index;
1613 			dir = DPLL_PIN_DIRECTION_INPUT;
1614 		} else {
1615 			id = index - ZL3073X_NUM_INPUT_PINS;
1616 			dir = DPLL_PIN_DIRECTION_OUTPUT;
1617 		}
1618 
1619 		/* Check if the pin registrable to this DPLL */
1620 		if (!zl3073x_dpll_pin_is_registrable(zldpll, dir, id))
1621 			continue;
1622 
1623 		pin = zl3073x_dpll_pin_alloc(zldpll, dir, id);
1624 		if (IS_ERR(pin)) {
1625 			rc = PTR_ERR(pin);
1626 			goto error;
1627 		}
1628 
1629 		rc = zl3073x_dpll_pin_register(pin, index);
1630 		if (rc)
1631 			goto error;
1632 
1633 		list_add(&pin->list, &zldpll->pins);
1634 	}
1635 
1636 	return 0;
1637 
1638 error:
1639 	zl3073x_dpll_pins_unregister(zldpll);
1640 
1641 	return rc;
1642 }
1643 
1644 /**
1645  * zl3073x_dpll_device_register - register DPLL device
1646  * @zldpll: pointer to zl3073x_dpll structure
1647  *
1648  * Registers given DPLL device into DPLL sub-system.
1649  *
1650  * Return: 0 on success, <0 on error
1651  */
1652 static int
1653 zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll)
1654 {
1655 	struct zl3073x_dev *zldev = zldpll->dev;
1656 	u8 dpll_mode_refsel;
1657 	int rc;
1658 
1659 	/* Read DPLL mode and forcibly selected reference */
1660 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MODE_REFSEL(zldpll->id),
1661 			     &dpll_mode_refsel);
1662 	if (rc)
1663 		return rc;
1664 
1665 	/* Extract mode and selected input reference */
1666 	zldpll->refsel_mode = FIELD_GET(ZL_DPLL_MODE_REFSEL_MODE,
1667 					dpll_mode_refsel);
1668 	zldpll->forced_ref = FIELD_GET(ZL_DPLL_MODE_REFSEL_REF,
1669 				       dpll_mode_refsel);
1670 
1671 	zldpll->dpll_dev = dpll_device_get(zldev->clock_id, zldpll->id,
1672 					   THIS_MODULE, &zldpll->tracker);
1673 	if (IS_ERR(zldpll->dpll_dev)) {
1674 		rc = PTR_ERR(zldpll->dpll_dev);
1675 		zldpll->dpll_dev = NULL;
1676 
1677 		return rc;
1678 	}
1679 
1680 	rc = dpll_device_register(zldpll->dpll_dev,
1681 				  zl3073x_prop_dpll_type_get(zldev, zldpll->id),
1682 				  &zl3073x_dpll_device_ops, zldpll);
1683 	if (rc) {
1684 		dpll_device_put(zldpll->dpll_dev, &zldpll->tracker);
1685 		zldpll->dpll_dev = NULL;
1686 	}
1687 
1688 	return rc;
1689 }
1690 
1691 /**
1692  * zl3073x_dpll_device_unregister - unregister DPLL device
1693  * @zldpll: pointer to zl3073x_dpll structure
1694  *
1695  * Unregisters given DPLL device from DPLL sub-system previously registered
1696  * by @zl3073x_dpll_device_register.
1697  */
1698 static void
1699 zl3073x_dpll_device_unregister(struct zl3073x_dpll *zldpll)
1700 {
1701 	WARN(!zldpll->dpll_dev, "DPLL device is not registered\n");
1702 
1703 	cancel_work_sync(&zldpll->change_work);
1704 
1705 	dpll_device_unregister(zldpll->dpll_dev, &zl3073x_dpll_device_ops,
1706 			       zldpll);
1707 	dpll_device_put(zldpll->dpll_dev, &zldpll->tracker);
1708 	zldpll->dpll_dev = NULL;
1709 }
1710 
1711 /**
1712  * zl3073x_dpll_pin_phase_offset_check - check for pin phase offset change
1713  * @pin: pin to check
1714  *
1715  * Check for the change of DPLL to connected pin phase offset change.
1716  *
1717  * Return: true on phase offset change, false otherwise
1718  */
1719 static bool
1720 zl3073x_dpll_pin_phase_offset_check(struct zl3073x_dpll_pin *pin)
1721 {
1722 	struct zl3073x_dpll *zldpll = pin->dpll;
1723 	struct zl3073x_dev *zldev = zldpll->dev;
1724 	unsigned int reg;
1725 	s64 phase_offset;
1726 	u8 ref_id;
1727 	int rc;
1728 
1729 	/* No phase offset if the ref monitor reports signal errors */
1730 	ref_id = zl3073x_input_pin_ref_get(pin->id);
1731 	if (!zl3073x_dev_ref_is_status_ok(zldev, ref_id))
1732 		return false;
1733 
1734 	/* Select register to read phase offset value depending on pin and
1735 	 * phase monitor state:
1736 	 * 1) For connected pin use dpll_phase_err_data register
1737 	 * 2) For other pins use appropriate ref_phase register if the phase
1738 	 *    monitor feature is enabled.
1739 	 */
1740 	if (pin->pin_state == DPLL_PIN_STATE_CONNECTED)
1741 		reg = ZL_REG_DPLL_PHASE_ERR_DATA(zldpll->id);
1742 	else if (zldpll->phase_monitor)
1743 		reg = ZL_REG_REF_PHASE(ref_id);
1744 	else
1745 		return false;
1746 
1747 	/* Read measured phase offset value */
1748 	rc = zl3073x_read_u48(zldev, reg, &phase_offset);
1749 	if (rc) {
1750 		dev_err(zldev->dev, "Failed to read ref phase offset: %pe\n",
1751 			ERR_PTR(rc));
1752 
1753 		return false;
1754 	}
1755 
1756 	/* Convert to ps */
1757 	phase_offset = div_s64(sign_extend64(phase_offset, 47), 100);
1758 
1759 	/* Compare with previous value */
1760 	if (phase_offset != pin->phase_offset) {
1761 		dev_dbg(zldev->dev, "%s phase offset changed: %lld -> %lld\n",
1762 			pin->label, pin->phase_offset, phase_offset);
1763 		pin->phase_offset = phase_offset;
1764 
1765 		return true;
1766 	}
1767 
1768 	return false;
1769 }
1770 
1771 /**
1772  * zl3073x_dpll_pin_ffo_check - check for pin fractional frequency offset change
1773  * @pin: pin to check
1774  *
1775  * Check for the given pin's fractional frequency change.
1776  *
1777  * Return: true on fractional frequency offset change, false otherwise
1778  */
1779 static bool
1780 zl3073x_dpll_pin_ffo_check(struct zl3073x_dpll_pin *pin)
1781 {
1782 	struct zl3073x_dpll *zldpll = pin->dpll;
1783 	struct zl3073x_dev *zldev = zldpll->dev;
1784 	const struct zl3073x_ref *ref;
1785 	u8 ref_id;
1786 
1787 	/* Get reference monitor status */
1788 	ref_id = zl3073x_input_pin_ref_get(pin->id);
1789 	ref = zl3073x_ref_state_get(zldev, ref_id);
1790 
1791 	/* Do not report ffo changes if the reference monitor report errors */
1792 	if (!zl3073x_ref_is_status_ok(ref))
1793 		return false;
1794 
1795 	/* Compare with previous value */
1796 	if (pin->freq_offset != ref->ffo) {
1797 		dev_dbg(zldev->dev, "%s freq offset changed: %lld -> %lld\n",
1798 			pin->label, pin->freq_offset, ref->ffo);
1799 		pin->freq_offset = ref->ffo;
1800 
1801 		return true;
1802 	}
1803 
1804 	return false;
1805 }
1806 
1807 /**
1808  * zl3073x_dpll_changes_check - check for changes and send notifications
1809  * @zldpll: pointer to zl3073x_dpll structure
1810  *
1811  * Checks for changes on given DPLL device and its registered DPLL pins
1812  * and sends notifications about them.
1813  *
1814  * This function is periodically called from @zl3073x_dev_periodic_work.
1815  */
1816 void
1817 zl3073x_dpll_changes_check(struct zl3073x_dpll *zldpll)
1818 {
1819 	struct zl3073x_dev *zldev = zldpll->dev;
1820 	enum dpll_lock_status lock_status;
1821 	struct device *dev = zldev->dev;
1822 	struct zl3073x_dpll_pin *pin;
1823 	int rc;
1824 
1825 	zldpll->check_count++;
1826 
1827 	/* Get current lock status for the DPLL */
1828 	rc = zl3073x_dpll_lock_status_get(zldpll->dpll_dev, zldpll,
1829 					  &lock_status, NULL, NULL);
1830 	if (rc) {
1831 		dev_err(dev, "Failed to get DPLL%u lock status: %pe\n",
1832 			zldpll->id, ERR_PTR(rc));
1833 		return;
1834 	}
1835 
1836 	/* If lock status was changed then notify DPLL core */
1837 	if (zldpll->lock_status != lock_status) {
1838 		zldpll->lock_status = lock_status;
1839 		dpll_device_change_ntf(zldpll->dpll_dev);
1840 	}
1841 
1842 	/* Input pin monitoring does make sense only in automatic
1843 	 * or forced reference modes.
1844 	 */
1845 	if (zldpll->refsel_mode != ZL_DPLL_MODE_REFSEL_MODE_AUTO &&
1846 	    zldpll->refsel_mode != ZL_DPLL_MODE_REFSEL_MODE_REFLOCK)
1847 		return;
1848 
1849 	/* Update phase offset latch registers for this DPLL if the phase
1850 	 * offset monitor feature is enabled.
1851 	 */
1852 	if (zldpll->phase_monitor) {
1853 		rc = zl3073x_ref_phase_offsets_update(zldev, zldpll->id);
1854 		if (rc) {
1855 			dev_err(zldev->dev,
1856 				"Failed to update phase offsets: %pe\n",
1857 				ERR_PTR(rc));
1858 			return;
1859 		}
1860 	}
1861 
1862 	list_for_each_entry(pin, &zldpll->pins, list) {
1863 		enum dpll_pin_state state;
1864 		bool pin_changed = false;
1865 
1866 		/* Output pins change checks are not necessary because output
1867 		 * states are constant.
1868 		 */
1869 		if (!zl3073x_dpll_is_input_pin(pin))
1870 			continue;
1871 
1872 		rc = zl3073x_dpll_ref_state_get(pin, &state);
1873 		if (rc) {
1874 			dev_err(dev,
1875 				"Failed to get %s on DPLL%u state: %pe\n",
1876 				pin->label, zldpll->id, ERR_PTR(rc));
1877 			return;
1878 		}
1879 
1880 		if (state != pin->pin_state) {
1881 			dev_dbg(dev, "%s state changed: %u->%u\n", pin->label,
1882 				pin->pin_state, state);
1883 			pin->pin_state = state;
1884 			pin_changed = true;
1885 		}
1886 
1887 		/* Check for phase offset and ffo change once per second */
1888 		if (zldpll->check_count % 2 == 0) {
1889 			if (zl3073x_dpll_pin_phase_offset_check(pin))
1890 				pin_changed = true;
1891 
1892 			if (zl3073x_dpll_pin_ffo_check(pin))
1893 				pin_changed = true;
1894 		}
1895 
1896 		if (pin_changed)
1897 			dpll_pin_change_ntf(pin->dpll_pin);
1898 	}
1899 }
1900 
1901 /**
1902  * zl3073x_dpll_init_fine_phase_adjust - do initial fine phase adjustments
1903  * @zldev: pointer to zl3073x device
1904  *
1905  * Performs initial fine phase adjustments needed per datasheet.
1906  *
1907  * Return: 0 on success, <0 on error
1908  */
1909 int
1910 zl3073x_dpll_init_fine_phase_adjust(struct zl3073x_dev *zldev)
1911 {
1912 	int rc;
1913 
1914 	rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_MASK, 0x1f);
1915 	if (rc)
1916 		return rc;
1917 
1918 	rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_INTVL, 0x01);
1919 	if (rc)
1920 		return rc;
1921 
1922 	rc = zl3073x_write_u16(zldev, ZL_REG_SYNTH_PHASE_SHIFT_DATA, 0xffff);
1923 	if (rc)
1924 		return rc;
1925 
1926 	rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_CTRL, 0x01);
1927 	if (rc)
1928 		return rc;
1929 
1930 	return rc;
1931 }
1932 
1933 /**
1934  * zl3073x_dpll_alloc - allocate DPLL device
1935  * @zldev: pointer to zl3073x device
1936  * @ch: DPLL channel number
1937  *
1938  * Allocates DPLL device structure for given DPLL channel.
1939  *
1940  * Return: pointer to DPLL device on success, error pointer on error
1941  */
1942 struct zl3073x_dpll *
1943 zl3073x_dpll_alloc(struct zl3073x_dev *zldev, u8 ch)
1944 {
1945 	struct zl3073x_dpll *zldpll;
1946 
1947 	zldpll = kzalloc_obj(*zldpll);
1948 	if (!zldpll)
1949 		return ERR_PTR(-ENOMEM);
1950 
1951 	zldpll->dev = zldev;
1952 	zldpll->id = ch;
1953 	INIT_LIST_HEAD(&zldpll->pins);
1954 	INIT_WORK(&zldpll->change_work, zl3073x_dpll_change_work);
1955 
1956 	return zldpll;
1957 }
1958 
1959 /**
1960  * zl3073x_dpll_free - free DPLL device
1961  * @zldpll: pointer to zl3073x_dpll structure
1962  *
1963  * Deallocates given DPLL device previously allocated by @zl3073x_dpll_alloc.
1964  */
1965 void
1966 zl3073x_dpll_free(struct zl3073x_dpll *zldpll)
1967 {
1968 	WARN(zldpll->dpll_dev, "DPLL device is still registered\n");
1969 
1970 	kfree(zldpll);
1971 }
1972 
1973 /**
1974  * zl3073x_dpll_register - register DPLL device and all its pins
1975  * @zldpll: pointer to zl3073x_dpll structure
1976  *
1977  * Registers given DPLL device and all its pins into DPLL sub-system.
1978  *
1979  * Return: 0 on success, <0 on error
1980  */
1981 int
1982 zl3073x_dpll_register(struct zl3073x_dpll *zldpll)
1983 {
1984 	int rc;
1985 
1986 	rc = zl3073x_dpll_device_register(zldpll);
1987 	if (rc)
1988 		return rc;
1989 
1990 	rc = zl3073x_dpll_pins_register(zldpll);
1991 	if (rc) {
1992 		zl3073x_dpll_device_unregister(zldpll);
1993 		return rc;
1994 	}
1995 
1996 	return 0;
1997 }
1998 
1999 /**
2000  * zl3073x_dpll_unregister - unregister DPLL device and its pins
2001  * @zldpll: pointer to zl3073x_dpll structure
2002  *
2003  * Unregisters given DPLL device and all its pins from DPLL sub-system
2004  * previously registered by @zl3073x_dpll_register.
2005  */
2006 void
2007 zl3073x_dpll_unregister(struct zl3073x_dpll *zldpll)
2008 {
2009 	/* Unregister all pins and dpll */
2010 	zl3073x_dpll_pins_unregister(zldpll);
2011 	zl3073x_dpll_device_unregister(zldpll);
2012 }
2013