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