xref: /linux/drivers/net/ethernet/intel/ice/ice_dpll.c (revision bb118e86dfcc096b8a3889c1a5c88f214e1f65fa)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_trace.h"
7 #include <linux/dpll.h>
8 
9 #define ICE_CGU_STATE_ACQ_ERR_THRESHOLD		50
10 #define ICE_DPLL_PIN_IDX_INVALID		0xff
11 #define ICE_DPLL_RCLK_NUM_PER_PF		1
12 
13 /**
14  * enum ice_dpll_pin_type - enumerate ice pin types:
15  * @ICE_DPLL_PIN_INVALID: invalid pin type
16  * @ICE_DPLL_PIN_TYPE_INPUT: input pin
17  * @ICE_DPLL_PIN_TYPE_OUTPUT: output pin
18  * @ICE_DPLL_PIN_TYPE_RCLK_INPUT: recovery clock input pin
19  */
20 enum ice_dpll_pin_type {
21 	ICE_DPLL_PIN_INVALID,
22 	ICE_DPLL_PIN_TYPE_INPUT,
23 	ICE_DPLL_PIN_TYPE_OUTPUT,
24 	ICE_DPLL_PIN_TYPE_RCLK_INPUT,
25 };
26 
27 static const char * const pin_type_name[] = {
28 	[ICE_DPLL_PIN_TYPE_INPUT] = "input",
29 	[ICE_DPLL_PIN_TYPE_OUTPUT] = "output",
30 	[ICE_DPLL_PIN_TYPE_RCLK_INPUT] = "rclk-input",
31 };
32 
33 /**
34  * ice_dpll_pin_freq_set - set pin's frequency
35  * @pf: private board structure
36  * @pin: pointer to a pin
37  * @pin_type: type of pin being configured
38  * @freq: frequency to be set
39  * @extack: error reporting
40  *
41  * Set requested frequency on a pin.
42  *
43  * Context: Called under pf->dplls.lock
44  * Return:
45  * * 0 - success
46  * * negative - error on AQ or wrong pin type given
47  */
48 static int
49 ice_dpll_pin_freq_set(struct ice_pf *pf, struct ice_dpll_pin *pin,
50 		      enum ice_dpll_pin_type pin_type, const u32 freq,
51 		      struct netlink_ext_ack *extack)
52 {
53 	u8 flags;
54 	int ret;
55 
56 	switch (pin_type) {
57 	case ICE_DPLL_PIN_TYPE_INPUT:
58 		flags = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_FREQ;
59 		ret = ice_aq_set_input_pin_cfg(&pf->hw, pin->idx, flags,
60 					       pin->flags[0], freq, 0);
61 		break;
62 	case ICE_DPLL_PIN_TYPE_OUTPUT:
63 		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_FREQ;
64 		ret = ice_aq_set_output_pin_cfg(&pf->hw, pin->idx, flags,
65 						0, freq, 0);
66 		break;
67 	default:
68 		return -EINVAL;
69 	}
70 	if (ret) {
71 		NL_SET_ERR_MSG_FMT(extack,
72 				   "err:%d %s failed to set pin freq:%u on pin:%u\n",
73 				   ret,
74 				   ice_aq_str(pf->hw.adminq.sq_last_status),
75 				   freq, pin->idx);
76 		return ret;
77 	}
78 	pin->freq = freq;
79 
80 	return 0;
81 }
82 
83 /**
84  * ice_dpll_frequency_set - wrapper for pin callback for set frequency
85  * @pin: pointer to a pin
86  * @pin_priv: private data pointer passed on pin registration
87  * @dpll: pointer to dpll
88  * @dpll_priv: private data pointer passed on dpll registration
89  * @frequency: frequency to be set
90  * @extack: error reporting
91  * @pin_type: type of pin being configured
92  *
93  * Wraps internal set frequency command on a pin.
94  *
95  * Context: Acquires pf->dplls.lock
96  * Return:
97  * * 0 - success
98  * * negative - error pin not found or couldn't set in hw
99  */
100 static int
101 ice_dpll_frequency_set(const struct dpll_pin *pin, void *pin_priv,
102 		       const struct dpll_device *dpll, void *dpll_priv,
103 		       const u32 frequency,
104 		       struct netlink_ext_ack *extack,
105 		       enum ice_dpll_pin_type pin_type)
106 {
107 	struct ice_dpll_pin *p = pin_priv;
108 	struct ice_dpll *d = dpll_priv;
109 	struct ice_pf *pf = d->pf;
110 	int ret;
111 
112 	mutex_lock(&pf->dplls.lock);
113 	ret = ice_dpll_pin_freq_set(pf, p, pin_type, frequency, extack);
114 	mutex_unlock(&pf->dplls.lock);
115 
116 	return ret;
117 }
118 
119 /**
120  * ice_dpll_input_frequency_set - input pin callback for set frequency
121  * @pin: pointer to a pin
122  * @pin_priv: private data pointer passed on pin registration
123  * @dpll: pointer to dpll
124  * @dpll_priv: private data pointer passed on dpll registration
125  * @frequency: frequency to be set
126  * @extack: error reporting
127  *
128  * Wraps internal set frequency command on a pin.
129  *
130  * Context: Calls a function which acquires pf->dplls.lock
131  * Return:
132  * * 0 - success
133  * * negative - error pin not found or couldn't set in hw
134  */
135 static int
136 ice_dpll_input_frequency_set(const struct dpll_pin *pin, void *pin_priv,
137 			     const struct dpll_device *dpll, void *dpll_priv,
138 			     u64 frequency, struct netlink_ext_ack *extack)
139 {
140 	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
141 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
142 }
143 
144 /**
145  * ice_dpll_output_frequency_set - output pin callback for set frequency
146  * @pin: pointer to a pin
147  * @pin_priv: private data pointer passed on pin registration
148  * @dpll: pointer to dpll
149  * @dpll_priv: private data pointer passed on dpll registration
150  * @frequency: frequency to be set
151  * @extack: error reporting
152  *
153  * Wraps internal set frequency command on a pin.
154  *
155  * Context: Calls a function which acquires pf->dplls.lock
156  * Return:
157  * * 0 - success
158  * * negative - error pin not found or couldn't set in hw
159  */
160 static int
161 ice_dpll_output_frequency_set(const struct dpll_pin *pin, void *pin_priv,
162 			      const struct dpll_device *dpll, void *dpll_priv,
163 			      u64 frequency, struct netlink_ext_ack *extack)
164 {
165 	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
166 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
167 }
168 
169 /**
170  * ice_dpll_frequency_get - wrapper for pin callback for get frequency
171  * @pin: pointer to a pin
172  * @pin_priv: private data pointer passed on pin registration
173  * @dpll: pointer to dpll
174  * @dpll_priv: private data pointer passed on dpll registration
175  * @frequency: on success holds pin's frequency
176  * @extack: error reporting
177  * @pin_type: type of pin being configured
178  *
179  * Wraps internal get frequency command of a pin.
180  *
181  * Context: Acquires pf->dplls.lock
182  * Return:
183  * * 0 - success
184  * * negative - error pin not found or couldn't get from hw
185  */
186 static int
187 ice_dpll_frequency_get(const struct dpll_pin *pin, void *pin_priv,
188 		       const struct dpll_device *dpll, void *dpll_priv,
189 		       u64 *frequency, struct netlink_ext_ack *extack,
190 		       enum ice_dpll_pin_type pin_type)
191 {
192 	struct ice_dpll_pin *p = pin_priv;
193 	struct ice_dpll *d = dpll_priv;
194 	struct ice_pf *pf = d->pf;
195 
196 	mutex_lock(&pf->dplls.lock);
197 	*frequency = p->freq;
198 	mutex_unlock(&pf->dplls.lock);
199 
200 	return 0;
201 }
202 
203 /**
204  * ice_dpll_input_frequency_get - input pin callback for get frequency
205  * @pin: pointer to a pin
206  * @pin_priv: private data pointer passed on pin registration
207  * @dpll: pointer to dpll
208  * @dpll_priv: private data pointer passed on dpll registration
209  * @frequency: on success holds pin's frequency
210  * @extack: error reporting
211  *
212  * Wraps internal get frequency command of a input pin.
213  *
214  * Context: Calls a function which acquires pf->dplls.lock
215  * Return:
216  * * 0 - success
217  * * negative - error pin not found or couldn't get from hw
218  */
219 static int
220 ice_dpll_input_frequency_get(const struct dpll_pin *pin, void *pin_priv,
221 			     const struct dpll_device *dpll, void *dpll_priv,
222 			     u64 *frequency, struct netlink_ext_ack *extack)
223 {
224 	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
225 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
226 }
227 
228 /**
229  * ice_dpll_output_frequency_get - output pin callback for get frequency
230  * @pin: pointer to a pin
231  * @pin_priv: private data pointer passed on pin registration
232  * @dpll: pointer to dpll
233  * @dpll_priv: private data pointer passed on dpll registration
234  * @frequency: on success holds pin's frequency
235  * @extack: error reporting
236  *
237  * Wraps internal get frequency command of a pin.
238  *
239  * Context: Calls a function which acquires pf->dplls.lock
240  * Return:
241  * * 0 - success
242  * * negative - error pin not found or couldn't get from hw
243  */
244 static int
245 ice_dpll_output_frequency_get(const struct dpll_pin *pin, void *pin_priv,
246 			      const struct dpll_device *dpll, void *dpll_priv,
247 			      u64 *frequency, struct netlink_ext_ack *extack)
248 {
249 	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
250 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
251 }
252 
253 /**
254  * ice_dpll_pin_enable - enable a pin on dplls
255  * @hw: board private hw structure
256  * @pin: pointer to a pin
257  * @pin_type: type of pin being enabled
258  * @extack: error reporting
259  *
260  * Enable a pin on both dplls. Store current state in pin->flags.
261  *
262  * Context: Called under pf->dplls.lock
263  * Return:
264  * * 0 - OK
265  * * negative - error
266  */
267 static int
268 ice_dpll_pin_enable(struct ice_hw *hw, struct ice_dpll_pin *pin,
269 		    enum ice_dpll_pin_type pin_type,
270 		    struct netlink_ext_ack *extack)
271 {
272 	u8 flags = 0;
273 	int ret;
274 
275 	switch (pin_type) {
276 	case ICE_DPLL_PIN_TYPE_INPUT:
277 		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
278 			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
279 		flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
280 		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
281 		break;
282 	case ICE_DPLL_PIN_TYPE_OUTPUT:
283 		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
284 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
285 		flags |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
286 		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, 0, 0, 0);
287 		break;
288 	default:
289 		return -EINVAL;
290 	}
291 	if (ret)
292 		NL_SET_ERR_MSG_FMT(extack,
293 				   "err:%d %s failed to enable %s pin:%u\n",
294 				   ret, ice_aq_str(hw->adminq.sq_last_status),
295 				   pin_type_name[pin_type], pin->idx);
296 
297 	return ret;
298 }
299 
300 /**
301  * ice_dpll_pin_disable - disable a pin on dplls
302  * @hw: board private hw structure
303  * @pin: pointer to a pin
304  * @pin_type: type of pin being disabled
305  * @extack: error reporting
306  *
307  * Disable a pin on both dplls. Store current state in pin->flags.
308  *
309  * Context: Called under pf->dplls.lock
310  * Return:
311  * * 0 - OK
312  * * negative - error
313  */
314 static int
315 ice_dpll_pin_disable(struct ice_hw *hw, struct ice_dpll_pin *pin,
316 		     enum ice_dpll_pin_type pin_type,
317 		     struct netlink_ext_ack *extack)
318 {
319 	u8 flags = 0;
320 	int ret;
321 
322 	switch (pin_type) {
323 	case ICE_DPLL_PIN_TYPE_INPUT:
324 		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
325 			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
326 		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
327 		break;
328 	case ICE_DPLL_PIN_TYPE_OUTPUT:
329 		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
330 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
331 		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, 0, 0, 0);
332 		break;
333 	default:
334 		return -EINVAL;
335 	}
336 	if (ret)
337 		NL_SET_ERR_MSG_FMT(extack,
338 				   "err:%d %s failed to disable %s pin:%u\n",
339 				   ret, ice_aq_str(hw->adminq.sq_last_status),
340 				   pin_type_name[pin_type], pin->idx);
341 
342 	return ret;
343 }
344 
345 /**
346  * ice_dpll_pin_state_update - update pin's state
347  * @pf: private board struct
348  * @pin: structure with pin attributes to be updated
349  * @pin_type: type of pin being updated
350  * @extack: error reporting
351  *
352  * Determine pin current state and frequency, then update struct
353  * holding the pin info. For input pin states are separated for each
354  * dpll, for rclk pins states are separated for each parent.
355  *
356  * Context: Called under pf->dplls.lock
357  * Return:
358  * * 0 - OK
359  * * negative - error
360  */
361 static int
362 ice_dpll_pin_state_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
363 			  enum ice_dpll_pin_type pin_type,
364 			  struct netlink_ext_ack *extack)
365 {
366 	u8 parent, port_num = ICE_AQC_SET_PHY_REC_CLK_OUT_CURR_PORT;
367 	int ret;
368 
369 	switch (pin_type) {
370 	case ICE_DPLL_PIN_TYPE_INPUT:
371 		ret = ice_aq_get_input_pin_cfg(&pf->hw, pin->idx, NULL, NULL,
372 					       NULL, &pin->flags[0],
373 					       &pin->freq, NULL);
374 		if (ret)
375 			goto err;
376 		if (ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN & pin->flags[0]) {
377 			if (pin->pin) {
378 				pin->state[pf->dplls.eec.dpll_idx] =
379 					pin->pin == pf->dplls.eec.active_input ?
380 					DPLL_PIN_STATE_CONNECTED :
381 					DPLL_PIN_STATE_SELECTABLE;
382 				pin->state[pf->dplls.pps.dpll_idx] =
383 					pin->pin == pf->dplls.pps.active_input ?
384 					DPLL_PIN_STATE_CONNECTED :
385 					DPLL_PIN_STATE_SELECTABLE;
386 			} else {
387 				pin->state[pf->dplls.eec.dpll_idx] =
388 					DPLL_PIN_STATE_SELECTABLE;
389 				pin->state[pf->dplls.pps.dpll_idx] =
390 					DPLL_PIN_STATE_SELECTABLE;
391 			}
392 		} else {
393 			pin->state[pf->dplls.eec.dpll_idx] =
394 				DPLL_PIN_STATE_DISCONNECTED;
395 			pin->state[pf->dplls.pps.dpll_idx] =
396 				DPLL_PIN_STATE_DISCONNECTED;
397 		}
398 		break;
399 	case ICE_DPLL_PIN_TYPE_OUTPUT:
400 		ret = ice_aq_get_output_pin_cfg(&pf->hw, pin->idx,
401 						&pin->flags[0], NULL,
402 						&pin->freq, NULL);
403 		if (ret)
404 			goto err;
405 		if (ICE_AQC_SET_CGU_OUT_CFG_OUT_EN & pin->flags[0])
406 			pin->state[0] = DPLL_PIN_STATE_CONNECTED;
407 		else
408 			pin->state[0] = DPLL_PIN_STATE_DISCONNECTED;
409 		break;
410 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
411 		for (parent = 0; parent < pf->dplls.rclk.num_parents;
412 		     parent++) {
413 			u8 p = parent;
414 
415 			ret = ice_aq_get_phy_rec_clk_out(&pf->hw, &p,
416 							 &port_num,
417 							 &pin->flags[parent],
418 							 NULL);
419 			if (ret)
420 				goto err;
421 			if (ICE_AQC_GET_PHY_REC_CLK_OUT_OUT_EN &
422 			    pin->flags[parent])
423 				pin->state[parent] = DPLL_PIN_STATE_CONNECTED;
424 			else
425 				pin->state[parent] =
426 					DPLL_PIN_STATE_DISCONNECTED;
427 		}
428 		break;
429 	default:
430 		return -EINVAL;
431 	}
432 
433 	return 0;
434 err:
435 	if (extack)
436 		NL_SET_ERR_MSG_FMT(extack,
437 				   "err:%d %s failed to update %s pin:%u\n",
438 				   ret,
439 				   ice_aq_str(pf->hw.adminq.sq_last_status),
440 				   pin_type_name[pin_type], pin->idx);
441 	else
442 		dev_err_ratelimited(ice_pf_to_dev(pf),
443 				    "err:%d %s failed to update %s pin:%u\n",
444 				    ret,
445 				    ice_aq_str(pf->hw.adminq.sq_last_status),
446 				    pin_type_name[pin_type], pin->idx);
447 	return ret;
448 }
449 
450 /**
451  * ice_dpll_hw_input_prio_set - set input priority value in hardware
452  * @pf: board private structure
453  * @dpll: ice dpll pointer
454  * @pin: ice pin pointer
455  * @prio: priority value being set on a dpll
456  * @extack: error reporting
457  *
458  * Internal wrapper for setting the priority in the hardware.
459  *
460  * Context: Called under pf->dplls.lock
461  * Return:
462  * * 0 - success
463  * * negative - failure
464  */
465 static int
466 ice_dpll_hw_input_prio_set(struct ice_pf *pf, struct ice_dpll *dpll,
467 			   struct ice_dpll_pin *pin, const u32 prio,
468 			   struct netlink_ext_ack *extack)
469 {
470 	int ret;
471 
472 	ret = ice_aq_set_cgu_ref_prio(&pf->hw, dpll->dpll_idx, pin->idx,
473 				      (u8)prio);
474 	if (ret)
475 		NL_SET_ERR_MSG_FMT(extack,
476 				   "err:%d %s failed to set pin prio:%u on pin:%u\n",
477 				   ret,
478 				   ice_aq_str(pf->hw.adminq.sq_last_status),
479 				   prio, pin->idx);
480 	else
481 		dpll->input_prio[pin->idx] = prio;
482 
483 	return ret;
484 }
485 
486 /**
487  * ice_dpll_lock_status_get - get dpll lock status callback
488  * @dpll: registered dpll pointer
489  * @dpll_priv: private data pointer passed on dpll registration
490  * @status: on success holds dpll's lock status
491  * @extack: error reporting
492  *
493  * Dpll subsystem callback, provides dpll's lock status.
494  *
495  * Context: Acquires pf->dplls.lock
496  * Return:
497  * * 0 - success
498  * * negative - failure
499  */
500 static int
501 ice_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
502 			 enum dpll_lock_status *status,
503 			 struct netlink_ext_ack *extack)
504 {
505 	struct ice_dpll *d = dpll_priv;
506 	struct ice_pf *pf = d->pf;
507 
508 	mutex_lock(&pf->dplls.lock);
509 	*status = d->dpll_state;
510 	mutex_unlock(&pf->dplls.lock);
511 
512 	return 0;
513 }
514 
515 /**
516  * ice_dpll_mode_supported - check if dpll's working mode is supported
517  * @dpll: registered dpll pointer
518  * @dpll_priv: private data pointer passed on dpll registration
519  * @mode: mode to be checked for support
520  * @extack: error reporting
521  *
522  * Dpll subsystem callback. Provides information if working mode is supported
523  * by dpll.
524  *
525  * Return:
526  * * true - mode is supported
527  * * false - mode is not supported
528  */
529 static bool ice_dpll_mode_supported(const struct dpll_device *dpll,
530 				    void *dpll_priv,
531 				    enum dpll_mode mode,
532 				    struct netlink_ext_ack *extack)
533 {
534 	if (mode == DPLL_MODE_AUTOMATIC)
535 		return true;
536 
537 	return false;
538 }
539 
540 /**
541  * ice_dpll_mode_get - get dpll's working mode
542  * @dpll: registered dpll pointer
543  * @dpll_priv: private data pointer passed on dpll registration
544  * @mode: on success holds current working mode of dpll
545  * @extack: error reporting
546  *
547  * Dpll subsystem callback. Provides working mode of dpll.
548  *
549  * Context: Acquires pf->dplls.lock
550  * Return:
551  * * 0 - success
552  * * negative - failure
553  */
554 static int ice_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
555 			     enum dpll_mode *mode,
556 			     struct netlink_ext_ack *extack)
557 {
558 	struct ice_dpll *d = dpll_priv;
559 	struct ice_pf *pf = d->pf;
560 
561 	mutex_lock(&pf->dplls.lock);
562 	*mode = d->mode;
563 	mutex_unlock(&pf->dplls.lock);
564 
565 	return 0;
566 }
567 
568 /**
569  * ice_dpll_pin_state_set - set pin's state on dpll
570  * @pin: pointer to a pin
571  * @pin_priv: private data pointer passed on pin registration
572  * @dpll: registered dpll pointer
573  * @dpll_priv: private data pointer passed on dpll registration
574  * @enable: if pin shalll be enabled
575  * @extack: error reporting
576  * @pin_type: type of a pin
577  *
578  * Set pin state on a pin.
579  *
580  * Context: Acquires pf->dplls.lock
581  * Return:
582  * * 0 - OK or no change required
583  * * negative - error
584  */
585 static int
586 ice_dpll_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
587 		       const struct dpll_device *dpll, void *dpll_priv,
588 		       bool enable, struct netlink_ext_ack *extack,
589 		       enum ice_dpll_pin_type pin_type)
590 {
591 	struct ice_dpll_pin *p = pin_priv;
592 	struct ice_dpll *d = dpll_priv;
593 	struct ice_pf *pf = d->pf;
594 	int ret;
595 
596 	mutex_lock(&pf->dplls.lock);
597 	if (enable)
598 		ret = ice_dpll_pin_enable(&pf->hw, p, pin_type, extack);
599 	else
600 		ret = ice_dpll_pin_disable(&pf->hw, p, pin_type, extack);
601 	if (!ret)
602 		ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
603 	mutex_unlock(&pf->dplls.lock);
604 
605 	return ret;
606 }
607 
608 /**
609  * ice_dpll_output_state_set - enable/disable output pin on dpll device
610  * @pin: pointer to a pin
611  * @pin_priv: private data pointer passed on pin registration
612  * @dpll: dpll being configured
613  * @dpll_priv: private data pointer passed on dpll registration
614  * @state: state of pin to be set
615  * @extack: error reporting
616  *
617  * Dpll subsystem callback. Set given state on output type pin.
618  *
619  * Context: Calls a function which acquires pf->dplls.lock
620  * Return:
621  * * 0 - successfully enabled mode
622  * * negative - failed to enable mode
623  */
624 static int
625 ice_dpll_output_state_set(const struct dpll_pin *pin, void *pin_priv,
626 			  const struct dpll_device *dpll, void *dpll_priv,
627 			  enum dpll_pin_state state,
628 			  struct netlink_ext_ack *extack)
629 {
630 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
631 
632 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
633 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
634 }
635 
636 /**
637  * ice_dpll_input_state_set - enable/disable input pin on dpll levice
638  * @pin: pointer to a pin
639  * @pin_priv: private data pointer passed on pin registration
640  * @dpll: dpll being configured
641  * @dpll_priv: private data pointer passed on dpll registration
642  * @state: state of pin to be set
643  * @extack: error reporting
644  *
645  * Dpll subsystem callback. Enables given mode on input type pin.
646  *
647  * Context: Calls a function which acquires pf->dplls.lock
648  * Return:
649  * * 0 - successfully enabled mode
650  * * negative - failed to enable mode
651  */
652 static int
653 ice_dpll_input_state_set(const struct dpll_pin *pin, void *pin_priv,
654 			 const struct dpll_device *dpll, void *dpll_priv,
655 			 enum dpll_pin_state state,
656 			 struct netlink_ext_ack *extack)
657 {
658 	bool enable = state == DPLL_PIN_STATE_SELECTABLE;
659 
660 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
661 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
662 }
663 
664 /**
665  * ice_dpll_pin_state_get - set pin's state on dpll
666  * @pin: pointer to a pin
667  * @pin_priv: private data pointer passed on pin registration
668  * @dpll: registered dpll pointer
669  * @dpll_priv: private data pointer passed on dpll registration
670  * @state: on success holds state of the pin
671  * @extack: error reporting
672  * @pin_type: type of questioned pin
673  *
674  * Determine pin state set it on a pin.
675  *
676  * Context: Acquires pf->dplls.lock
677  * Return:
678  * * 0 - success
679  * * negative - failed to get state
680  */
681 static int
682 ice_dpll_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
683 		       const struct dpll_device *dpll, void *dpll_priv,
684 		       enum dpll_pin_state *state,
685 		       struct netlink_ext_ack *extack,
686 		       enum ice_dpll_pin_type pin_type)
687 {
688 	struct ice_dpll_pin *p = pin_priv;
689 	struct ice_dpll *d = dpll_priv;
690 	struct ice_pf *pf = d->pf;
691 	int ret;
692 
693 	mutex_lock(&pf->dplls.lock);
694 	ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
695 	if (ret)
696 		goto unlock;
697 	if (pin_type == ICE_DPLL_PIN_TYPE_INPUT)
698 		*state = p->state[d->dpll_idx];
699 	else if (pin_type == ICE_DPLL_PIN_TYPE_OUTPUT)
700 		*state = p->state[0];
701 	ret = 0;
702 unlock:
703 	mutex_unlock(&pf->dplls.lock);
704 
705 	return ret;
706 }
707 
708 /**
709  * ice_dpll_output_state_get - get output pin state on dpll device
710  * @pin: pointer to a pin
711  * @pin_priv: private data pointer passed on pin registration
712  * @dpll: registered dpll pointer
713  * @dpll_priv: private data pointer passed on dpll registration
714  * @state: on success holds state of the pin
715  * @extack: error reporting
716  *
717  * Dpll subsystem callback. Check state of a pin.
718  *
719  * Context: Calls a function which acquires pf->dplls.lock
720  * Return:
721  * * 0 - success
722  * * negative - failed to get state
723  */
724 static int
725 ice_dpll_output_state_get(const struct dpll_pin *pin, void *pin_priv,
726 			  const struct dpll_device *dpll, void *dpll_priv,
727 			  enum dpll_pin_state *state,
728 			  struct netlink_ext_ack *extack)
729 {
730 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
731 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
732 }
733 
734 /**
735  * ice_dpll_input_state_get - get input pin state on dpll device
736  * @pin: pointer to a pin
737  * @pin_priv: private data pointer passed on pin registration
738  * @dpll: registered dpll pointer
739  * @dpll_priv: private data pointer passed on dpll registration
740  * @state: on success holds state of the pin
741  * @extack: error reporting
742  *
743  * Dpll subsystem callback. Check state of a input pin.
744  *
745  * Context: Calls a function which acquires pf->dplls.lock
746  * Return:
747  * * 0 - success
748  * * negative - failed to get state
749  */
750 static int
751 ice_dpll_input_state_get(const struct dpll_pin *pin, void *pin_priv,
752 			 const struct dpll_device *dpll, void *dpll_priv,
753 			 enum dpll_pin_state *state,
754 			 struct netlink_ext_ack *extack)
755 {
756 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
757 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
758 }
759 
760 /**
761  * ice_dpll_input_prio_get - get dpll's input prio
762  * @pin: pointer to a pin
763  * @pin_priv: private data pointer passed on pin registration
764  * @dpll: registered dpll pointer
765  * @dpll_priv: private data pointer passed on dpll registration
766  * @prio: on success - returns input priority on dpll
767  * @extack: error reporting
768  *
769  * Dpll subsystem callback. Handler for getting priority of a input pin.
770  *
771  * Context: Acquires pf->dplls.lock
772  * Return:
773  * * 0 - success
774  * * negative - failure
775  */
776 static int
777 ice_dpll_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
778 			const struct dpll_device *dpll, void *dpll_priv,
779 			u32 *prio, struct netlink_ext_ack *extack)
780 {
781 	struct ice_dpll_pin *p = pin_priv;
782 	struct ice_dpll *d = dpll_priv;
783 	struct ice_pf *pf = d->pf;
784 
785 	mutex_lock(&pf->dplls.lock);
786 	*prio = d->input_prio[p->idx];
787 	mutex_unlock(&pf->dplls.lock);
788 
789 	return 0;
790 }
791 
792 /**
793  * ice_dpll_input_prio_set - set dpll input prio
794  * @pin: pointer to a pin
795  * @pin_priv: private data pointer passed on pin registration
796  * @dpll: registered dpll pointer
797  * @dpll_priv: private data pointer passed on dpll registration
798  * @prio: input priority to be set on dpll
799  * @extack: error reporting
800  *
801  * Dpll subsystem callback. Handler for setting priority of a input pin.
802  *
803  * Context: Acquires pf->dplls.lock
804  * Return:
805  * * 0 - success
806  * * negative - failure
807  */
808 static int
809 ice_dpll_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
810 			const struct dpll_device *dpll, void *dpll_priv,
811 			u32 prio, struct netlink_ext_ack *extack)
812 {
813 	struct ice_dpll_pin *p = pin_priv;
814 	struct ice_dpll *d = dpll_priv;
815 	struct ice_pf *pf = d->pf;
816 	int ret;
817 
818 	mutex_lock(&pf->dplls.lock);
819 	ret = ice_dpll_hw_input_prio_set(pf, d, p, prio, extack);
820 	mutex_unlock(&pf->dplls.lock);
821 
822 	return ret;
823 }
824 
825 /**
826  * ice_dpll_input_direction - callback for get input pin direction
827  * @pin: pointer to a pin
828  * @pin_priv: private data pointer passed on pin registration
829  * @dpll: registered dpll pointer
830  * @dpll_priv: private data pointer passed on dpll registration
831  * @direction: holds input pin direction
832  * @extack: error reporting
833  *
834  * Dpll subsystem callback. Handler for getting direction of a input pin.
835  *
836  * Return:
837  * * 0 - success
838  */
839 static int
840 ice_dpll_input_direction(const struct dpll_pin *pin, void *pin_priv,
841 			 const struct dpll_device *dpll, void *dpll_priv,
842 			 enum dpll_pin_direction *direction,
843 			 struct netlink_ext_ack *extack)
844 {
845 	*direction = DPLL_PIN_DIRECTION_INPUT;
846 
847 	return 0;
848 }
849 
850 /**
851  * ice_dpll_output_direction - callback for get output pin direction
852  * @pin: pointer to a pin
853  * @pin_priv: private data pointer passed on pin registration
854  * @dpll: registered dpll pointer
855  * @dpll_priv: private data pointer passed on dpll registration
856  * @direction: holds output pin direction
857  * @extack: error reporting
858  *
859  * Dpll subsystem callback. Handler for getting direction of an output pin.
860  *
861  * Return:
862  * * 0 - success
863  */
864 static int
865 ice_dpll_output_direction(const struct dpll_pin *pin, void *pin_priv,
866 			  const struct dpll_device *dpll, void *dpll_priv,
867 			  enum dpll_pin_direction *direction,
868 			  struct netlink_ext_ack *extack)
869 {
870 	*direction = DPLL_PIN_DIRECTION_OUTPUT;
871 
872 	return 0;
873 }
874 
875 /**
876  * ice_dpll_pin_phase_adjust_get - callback for get pin phase adjust value
877  * @pin: pointer to a pin
878  * @pin_priv: private data pointer passed on pin registration
879  * @dpll: registered dpll pointer
880  * @dpll_priv: private data pointer passed on dpll registration
881  * @phase_adjust: on success holds pin phase_adjust value
882  * @extack: error reporting
883  *
884  * Dpll subsystem callback. Handler for getting phase adjust value of a pin.
885  *
886  * Context: Acquires pf->dplls.lock
887  * Return:
888  * * 0 - success
889  * * negative - error
890  */
891 static int
892 ice_dpll_pin_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
893 			      const struct dpll_device *dpll, void *dpll_priv,
894 			      s32 *phase_adjust,
895 			      struct netlink_ext_ack *extack)
896 {
897 	struct ice_dpll_pin *p = pin_priv;
898 	struct ice_pf *pf = p->pf;
899 
900 	mutex_lock(&pf->dplls.lock);
901 	*phase_adjust = p->phase_adjust;
902 	mutex_unlock(&pf->dplls.lock);
903 
904 	return 0;
905 }
906 
907 /**
908  * ice_dpll_pin_phase_adjust_set - helper for setting a pin phase adjust value
909  * @pin: pointer to a pin
910  * @pin_priv: private data pointer passed on pin registration
911  * @dpll: registered dpll pointer
912  * @dpll_priv: private data pointer passed on dpll registration
913  * @phase_adjust: phase_adjust to be set
914  * @extack: error reporting
915  * @type: type of a pin
916  *
917  * Helper for dpll subsystem callback. Handler for setting phase adjust value
918  * of a pin.
919  *
920  * Context: Acquires pf->dplls.lock
921  * Return:
922  * * 0 - success
923  * * negative - error
924  */
925 static int
926 ice_dpll_pin_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
927 			      const struct dpll_device *dpll, void *dpll_priv,
928 			      s32 phase_adjust,
929 			      struct netlink_ext_ack *extack,
930 			      enum ice_dpll_pin_type type)
931 {
932 	struct ice_dpll_pin *p = pin_priv;
933 	struct ice_dpll *d = dpll_priv;
934 	struct ice_pf *pf = d->pf;
935 	u8 flag, flags_en = 0;
936 	int ret;
937 
938 	mutex_lock(&pf->dplls.lock);
939 	switch (type) {
940 	case ICE_DPLL_PIN_TYPE_INPUT:
941 		flag = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_DELAY;
942 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
943 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
944 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
945 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
946 		ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, flag, flags_en,
947 					       0, phase_adjust);
948 		break;
949 	case ICE_DPLL_PIN_TYPE_OUTPUT:
950 		flag = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_PHASE;
951 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
952 			flag |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
953 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
954 			flag |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
955 		ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flag, 0, 0,
956 						phase_adjust);
957 		break;
958 	default:
959 		ret = -EINVAL;
960 	}
961 	if (!ret)
962 		p->phase_adjust = phase_adjust;
963 	mutex_unlock(&pf->dplls.lock);
964 	if (ret)
965 		NL_SET_ERR_MSG_FMT(extack,
966 				   "err:%d %s failed to set pin phase_adjust:%d for pin:%u on dpll:%u\n",
967 				   ret,
968 				   ice_aq_str(pf->hw.adminq.sq_last_status),
969 				   phase_adjust, p->idx, d->dpll_idx);
970 
971 	return ret;
972 }
973 
974 /**
975  * ice_dpll_input_phase_adjust_set - callback for set input pin phase adjust
976  * @pin: pointer to a pin
977  * @pin_priv: private data pointer passed on pin registration
978  * @dpll: registered dpll pointer
979  * @dpll_priv: private data pointer passed on dpll registration
980  * @phase_adjust: phase_adjust to be set
981  * @extack: error reporting
982  *
983  * Dpll subsystem callback. Wraps a handler for setting phase adjust on input
984  * pin.
985  *
986  * Context: Calls a function which acquires pf->dplls.lock
987  * Return:
988  * * 0 - success
989  * * negative - error
990  */
991 static int
992 ice_dpll_input_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
993 				const struct dpll_device *dpll, void *dpll_priv,
994 				s32 phase_adjust,
995 				struct netlink_ext_ack *extack)
996 {
997 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
998 					     phase_adjust, extack,
999 					     ICE_DPLL_PIN_TYPE_INPUT);
1000 }
1001 
1002 /**
1003  * ice_dpll_output_phase_adjust_set - callback for set output pin phase adjust
1004  * @pin: pointer to a pin
1005  * @pin_priv: private data pointer passed on pin registration
1006  * @dpll: registered dpll pointer
1007  * @dpll_priv: private data pointer passed on dpll registration
1008  * @phase_adjust: phase_adjust to be set
1009  * @extack: error reporting
1010  *
1011  * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
1012  * pin.
1013  *
1014  * Context: Calls a function which acquires pf->dplls.lock
1015  * Return:
1016  * * 0 - success
1017  * * negative - error
1018  */
1019 static int
1020 ice_dpll_output_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1021 				 const struct dpll_device *dpll, void *dpll_priv,
1022 				 s32 phase_adjust,
1023 				 struct netlink_ext_ack *extack)
1024 {
1025 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
1026 					     phase_adjust, extack,
1027 					     ICE_DPLL_PIN_TYPE_OUTPUT);
1028 }
1029 
1030 #define ICE_DPLL_PHASE_OFFSET_DIVIDER	100
1031 #define ICE_DPLL_PHASE_OFFSET_FACTOR		\
1032 	(DPLL_PHASE_OFFSET_DIVIDER / ICE_DPLL_PHASE_OFFSET_DIVIDER)
1033 /**
1034  * ice_dpll_phase_offset_get - callback for get dpll phase shift value
1035  * @pin: pointer to a pin
1036  * @pin_priv: private data pointer passed on pin registration
1037  * @dpll: registered dpll pointer
1038  * @dpll_priv: private data pointer passed on dpll registration
1039  * @phase_offset: on success holds pin phase_offset value
1040  * @extack: error reporting
1041  *
1042  * Dpll subsystem callback. Handler for getting phase shift value between
1043  * dpll's input and output.
1044  *
1045  * Context: Acquires pf->dplls.lock
1046  * Return:
1047  * * 0 - success
1048  * * negative - error
1049  */
1050 static int
1051 ice_dpll_phase_offset_get(const struct dpll_pin *pin, void *pin_priv,
1052 			  const struct dpll_device *dpll, void *dpll_priv,
1053 			  s64 *phase_offset, struct netlink_ext_ack *extack)
1054 {
1055 	struct ice_dpll *d = dpll_priv;
1056 	struct ice_pf *pf = d->pf;
1057 
1058 	mutex_lock(&pf->dplls.lock);
1059 	if (d->active_input == pin)
1060 		*phase_offset = d->phase_offset * ICE_DPLL_PHASE_OFFSET_FACTOR;
1061 	else
1062 		*phase_offset = 0;
1063 	mutex_unlock(&pf->dplls.lock);
1064 
1065 	return 0;
1066 }
1067 
1068 /**
1069  * ice_dpll_rclk_state_on_pin_set - set a state on rclk pin
1070  * @pin: pointer to a pin
1071  * @pin_priv: private data pointer passed on pin registration
1072  * @parent_pin: pin parent pointer
1073  * @parent_pin_priv: parent private data pointer passed on pin registration
1074  * @state: state to be set on pin
1075  * @extack: error reporting
1076  *
1077  * Dpll subsystem callback, set a state of a rclk pin on a parent pin
1078  *
1079  * Context: Acquires pf->dplls.lock
1080  * Return:
1081  * * 0 - success
1082  * * negative - failure
1083  */
1084 static int
1085 ice_dpll_rclk_state_on_pin_set(const struct dpll_pin *pin, void *pin_priv,
1086 			       const struct dpll_pin *parent_pin,
1087 			       void *parent_pin_priv,
1088 			       enum dpll_pin_state state,
1089 			       struct netlink_ext_ack *extack)
1090 {
1091 	struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
1092 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
1093 	struct ice_pf *pf = p->pf;
1094 	int ret = -EINVAL;
1095 	u32 hw_idx;
1096 
1097 	mutex_lock(&pf->dplls.lock);
1098 	hw_idx = parent->idx - pf->dplls.base_rclk_idx;
1099 	if (hw_idx >= pf->dplls.num_inputs)
1100 		goto unlock;
1101 
1102 	if ((enable && p->state[hw_idx] == DPLL_PIN_STATE_CONNECTED) ||
1103 	    (!enable && p->state[hw_idx] == DPLL_PIN_STATE_DISCONNECTED)) {
1104 		NL_SET_ERR_MSG_FMT(extack,
1105 				   "pin:%u state:%u on parent:%u already set",
1106 				   p->idx, state, parent->idx);
1107 		goto unlock;
1108 	}
1109 	ret = ice_aq_set_phy_rec_clk_out(&pf->hw, hw_idx, enable,
1110 					 &p->freq);
1111 	if (ret)
1112 		NL_SET_ERR_MSG_FMT(extack,
1113 				   "err:%d %s failed to set pin state:%u for pin:%u on parent:%u\n",
1114 				   ret,
1115 				   ice_aq_str(pf->hw.adminq.sq_last_status),
1116 				   state, p->idx, parent->idx);
1117 unlock:
1118 	mutex_unlock(&pf->dplls.lock);
1119 
1120 	return ret;
1121 }
1122 
1123 /**
1124  * ice_dpll_rclk_state_on_pin_get - get a state of rclk pin
1125  * @pin: pointer to a pin
1126  * @pin_priv: private data pointer passed on pin registration
1127  * @parent_pin: pin parent pointer
1128  * @parent_pin_priv: pin parent priv data pointer passed on pin registration
1129  * @state: on success holds pin state on parent pin
1130  * @extack: error reporting
1131  *
1132  * dpll subsystem callback, get a state of a recovered clock pin.
1133  *
1134  * Context: Acquires pf->dplls.lock
1135  * Return:
1136  * * 0 - success
1137  * * negative - failure
1138  */
1139 static int
1140 ice_dpll_rclk_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
1141 			       const struct dpll_pin *parent_pin,
1142 			       void *parent_pin_priv,
1143 			       enum dpll_pin_state *state,
1144 			       struct netlink_ext_ack *extack)
1145 {
1146 	struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
1147 	struct ice_pf *pf = p->pf;
1148 	int ret = -EINVAL;
1149 	u32 hw_idx;
1150 
1151 	mutex_lock(&pf->dplls.lock);
1152 	hw_idx = parent->idx - pf->dplls.base_rclk_idx;
1153 	if (hw_idx >= pf->dplls.num_inputs)
1154 		goto unlock;
1155 
1156 	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_RCLK_INPUT,
1157 					extack);
1158 	if (ret)
1159 		goto unlock;
1160 
1161 	*state = p->state[hw_idx];
1162 	ret = 0;
1163 unlock:
1164 	mutex_unlock(&pf->dplls.lock);
1165 
1166 	return ret;
1167 }
1168 
1169 static const struct dpll_pin_ops ice_dpll_rclk_ops = {
1170 	.state_on_pin_set = ice_dpll_rclk_state_on_pin_set,
1171 	.state_on_pin_get = ice_dpll_rclk_state_on_pin_get,
1172 	.direction_get = ice_dpll_input_direction,
1173 };
1174 
1175 static const struct dpll_pin_ops ice_dpll_input_ops = {
1176 	.frequency_get = ice_dpll_input_frequency_get,
1177 	.frequency_set = ice_dpll_input_frequency_set,
1178 	.state_on_dpll_get = ice_dpll_input_state_get,
1179 	.state_on_dpll_set = ice_dpll_input_state_set,
1180 	.prio_get = ice_dpll_input_prio_get,
1181 	.prio_set = ice_dpll_input_prio_set,
1182 	.direction_get = ice_dpll_input_direction,
1183 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
1184 	.phase_adjust_set = ice_dpll_input_phase_adjust_set,
1185 	.phase_offset_get = ice_dpll_phase_offset_get,
1186 };
1187 
1188 static const struct dpll_pin_ops ice_dpll_output_ops = {
1189 	.frequency_get = ice_dpll_output_frequency_get,
1190 	.frequency_set = ice_dpll_output_frequency_set,
1191 	.state_on_dpll_get = ice_dpll_output_state_get,
1192 	.state_on_dpll_set = ice_dpll_output_state_set,
1193 	.direction_get = ice_dpll_output_direction,
1194 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
1195 	.phase_adjust_set = ice_dpll_output_phase_adjust_set,
1196 };
1197 
1198 static const struct dpll_device_ops ice_dpll_ops = {
1199 	.lock_status_get = ice_dpll_lock_status_get,
1200 	.mode_supported = ice_dpll_mode_supported,
1201 	.mode_get = ice_dpll_mode_get,
1202 };
1203 
1204 /**
1205  * ice_generate_clock_id - generates unique clock_id for registering dpll.
1206  * @pf: board private structure
1207  *
1208  * Generates unique (per board) clock_id for allocation and search of dpll
1209  * devices in Linux dpll subsystem.
1210  *
1211  * Return: generated clock id for the board
1212  */
1213 static u64 ice_generate_clock_id(struct ice_pf *pf)
1214 {
1215 	return pci_get_dsn(pf->pdev);
1216 }
1217 
1218 /**
1219  * ice_dpll_notify_changes - notify dpll subsystem about changes
1220  * @d: pointer do dpll
1221  *
1222  * Once change detected appropriate event is submitted to the dpll subsystem.
1223  */
1224 static void ice_dpll_notify_changes(struct ice_dpll *d)
1225 {
1226 	bool pin_notified = false;
1227 
1228 	if (d->prev_dpll_state != d->dpll_state) {
1229 		d->prev_dpll_state = d->dpll_state;
1230 		dpll_device_change_ntf(d->dpll);
1231 	}
1232 	if (d->prev_input != d->active_input) {
1233 		if (d->prev_input)
1234 			dpll_pin_change_ntf(d->prev_input);
1235 		d->prev_input = d->active_input;
1236 		if (d->active_input) {
1237 			dpll_pin_change_ntf(d->active_input);
1238 			pin_notified = true;
1239 		}
1240 	}
1241 	if (d->prev_phase_offset != d->phase_offset) {
1242 		d->prev_phase_offset = d->phase_offset;
1243 		if (!pin_notified && d->active_input)
1244 			dpll_pin_change_ntf(d->active_input);
1245 	}
1246 }
1247 
1248 /**
1249  * ice_dpll_update_state - update dpll state
1250  * @pf: pf private structure
1251  * @d: pointer to queried dpll device
1252  * @init: if function called on initialization of ice dpll
1253  *
1254  * Poll current state of dpll from hw and update ice_dpll struct.
1255  *
1256  * Context: Called by kworker under pf->dplls.lock
1257  * Return:
1258  * * 0 - success
1259  * * negative - AQ failure
1260  */
1261 static int
1262 ice_dpll_update_state(struct ice_pf *pf, struct ice_dpll *d, bool init)
1263 {
1264 	struct ice_dpll_pin *p = NULL;
1265 	int ret;
1266 
1267 	ret = ice_get_cgu_state(&pf->hw, d->dpll_idx, d->prev_dpll_state,
1268 				&d->input_idx, &d->ref_state, &d->eec_mode,
1269 				&d->phase_offset, &d->dpll_state);
1270 
1271 	dev_dbg(ice_pf_to_dev(pf),
1272 		"update dpll=%d, prev_src_idx:%u, src_idx:%u, state:%d, prev:%d mode:%d\n",
1273 		d->dpll_idx, d->prev_input_idx, d->input_idx,
1274 		d->dpll_state, d->prev_dpll_state, d->mode);
1275 	if (ret) {
1276 		dev_err(ice_pf_to_dev(pf),
1277 			"update dpll=%d state failed, ret=%d %s\n",
1278 			d->dpll_idx, ret,
1279 			ice_aq_str(pf->hw.adminq.sq_last_status));
1280 		return ret;
1281 	}
1282 	if (init) {
1283 		if (d->dpll_state == DPLL_LOCK_STATUS_LOCKED ||
1284 		    d->dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ)
1285 			d->active_input = pf->dplls.inputs[d->input_idx].pin;
1286 		p = &pf->dplls.inputs[d->input_idx];
1287 		return ice_dpll_pin_state_update(pf, p,
1288 						 ICE_DPLL_PIN_TYPE_INPUT, NULL);
1289 	}
1290 	if (d->dpll_state == DPLL_LOCK_STATUS_HOLDOVER ||
1291 	    d->dpll_state == DPLL_LOCK_STATUS_UNLOCKED) {
1292 		d->active_input = NULL;
1293 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID)
1294 			p = &pf->dplls.inputs[d->input_idx];
1295 		d->prev_input_idx = ICE_DPLL_PIN_IDX_INVALID;
1296 		d->input_idx = ICE_DPLL_PIN_IDX_INVALID;
1297 		if (!p)
1298 			return 0;
1299 		ret = ice_dpll_pin_state_update(pf, p,
1300 						ICE_DPLL_PIN_TYPE_INPUT, NULL);
1301 	} else if (d->input_idx != d->prev_input_idx) {
1302 		if (d->prev_input_idx != ICE_DPLL_PIN_IDX_INVALID) {
1303 			p = &pf->dplls.inputs[d->prev_input_idx];
1304 			ice_dpll_pin_state_update(pf, p,
1305 						  ICE_DPLL_PIN_TYPE_INPUT,
1306 						  NULL);
1307 		}
1308 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID) {
1309 			p = &pf->dplls.inputs[d->input_idx];
1310 			d->active_input = p->pin;
1311 			ice_dpll_pin_state_update(pf, p,
1312 						  ICE_DPLL_PIN_TYPE_INPUT,
1313 						  NULL);
1314 		}
1315 		d->prev_input_idx = d->input_idx;
1316 	}
1317 
1318 	return ret;
1319 }
1320 
1321 /**
1322  * ice_dpll_periodic_work - DPLLs periodic worker
1323  * @work: pointer to kthread_work structure
1324  *
1325  * DPLLs periodic worker is responsible for polling state of dpll.
1326  * Context: Holds pf->dplls.lock
1327  */
1328 static void ice_dpll_periodic_work(struct kthread_work *work)
1329 {
1330 	struct ice_dplls *d = container_of(work, struct ice_dplls, work.work);
1331 	struct ice_pf *pf = container_of(d, struct ice_pf, dplls);
1332 	struct ice_dpll *de = &pf->dplls.eec;
1333 	struct ice_dpll *dp = &pf->dplls.pps;
1334 	int ret;
1335 
1336 	mutex_lock(&pf->dplls.lock);
1337 	ret = ice_dpll_update_state(pf, de, false);
1338 	if (!ret)
1339 		ret = ice_dpll_update_state(pf, dp, false);
1340 	if (ret) {
1341 		d->cgu_state_acq_err_num++;
1342 		/* stop rescheduling this worker */
1343 		if (d->cgu_state_acq_err_num >
1344 		    ICE_CGU_STATE_ACQ_ERR_THRESHOLD) {
1345 			dev_err(ice_pf_to_dev(pf),
1346 				"EEC/PPS DPLLs periodic work disabled\n");
1347 			mutex_unlock(&pf->dplls.lock);
1348 			return;
1349 		}
1350 	}
1351 	mutex_unlock(&pf->dplls.lock);
1352 	ice_dpll_notify_changes(de);
1353 	ice_dpll_notify_changes(dp);
1354 
1355 	/* Run twice a second or reschedule if update failed */
1356 	kthread_queue_delayed_work(d->kworker, &d->work,
1357 				   ret ? msecs_to_jiffies(10) :
1358 				   msecs_to_jiffies(500));
1359 }
1360 
1361 /**
1362  * ice_dpll_release_pins - release pins resources from dpll subsystem
1363  * @pins: pointer to pins array
1364  * @count: number of pins
1365  *
1366  * Release resources of given pins array in the dpll subsystem.
1367  */
1368 static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
1369 {
1370 	int i;
1371 
1372 	for (i = 0; i < count; i++)
1373 		dpll_pin_put(pins[i].pin);
1374 }
1375 
1376 /**
1377  * ice_dpll_get_pins - get pins from dpll subsystem
1378  * @pf: board private structure
1379  * @pins: pointer to pins array
1380  * @start_idx: get starts from this pin idx value
1381  * @count: number of pins
1382  * @clock_id: clock_id of dpll device
1383  *
1384  * Get pins - allocate - in dpll subsystem, store them in pin field of given
1385  * pins array.
1386  *
1387  * Return:
1388  * * 0 - success
1389  * * negative - allocation failure reason
1390  */
1391 static int
1392 ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
1393 		  int start_idx, int count, u64 clock_id)
1394 {
1395 	int i, ret;
1396 
1397 	for (i = 0; i < count; i++) {
1398 		pins[i].pin = dpll_pin_get(clock_id, i + start_idx, THIS_MODULE,
1399 					   &pins[i].prop);
1400 		if (IS_ERR(pins[i].pin)) {
1401 			ret = PTR_ERR(pins[i].pin);
1402 			goto release_pins;
1403 		}
1404 	}
1405 
1406 	return 0;
1407 
1408 release_pins:
1409 	while (--i >= 0)
1410 		dpll_pin_put(pins[i].pin);
1411 	return ret;
1412 }
1413 
1414 /**
1415  * ice_dpll_unregister_pins - unregister pins from a dpll
1416  * @dpll: dpll device pointer
1417  * @pins: pointer to pins array
1418  * @ops: callback ops registered with the pins
1419  * @count: number of pins
1420  *
1421  * Unregister pins of a given array of pins from given dpll device registered in
1422  * dpll subsystem.
1423  */
1424 static void
1425 ice_dpll_unregister_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
1426 			 const struct dpll_pin_ops *ops, int count)
1427 {
1428 	int i;
1429 
1430 	for (i = 0; i < count; i++)
1431 		dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
1432 }
1433 
1434 /**
1435  * ice_dpll_register_pins - register pins with a dpll
1436  * @dpll: dpll pointer to register pins with
1437  * @pins: pointer to pins array
1438  * @ops: callback ops registered with the pins
1439  * @count: number of pins
1440  *
1441  * Register pins of a given array with given dpll in dpll subsystem.
1442  *
1443  * Return:
1444  * * 0 - success
1445  * * negative - registration failure reason
1446  */
1447 static int
1448 ice_dpll_register_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
1449 		       const struct dpll_pin_ops *ops, int count)
1450 {
1451 	int ret, i;
1452 
1453 	for (i = 0; i < count; i++) {
1454 		ret = dpll_pin_register(dpll, pins[i].pin, ops, &pins[i]);
1455 		if (ret)
1456 			goto unregister_pins;
1457 	}
1458 
1459 	return 0;
1460 
1461 unregister_pins:
1462 	while (--i >= 0)
1463 		dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
1464 	return ret;
1465 }
1466 
1467 /**
1468  * ice_dpll_deinit_direct_pins - deinitialize direct pins
1469  * @cgu: if cgu is present and controlled by this NIC
1470  * @pins: pointer to pins array
1471  * @count: number of pins
1472  * @ops: callback ops registered with the pins
1473  * @first: dpll device pointer
1474  * @second: dpll device pointer
1475  *
1476  * If cgu is owned unregister pins from given dplls.
1477  * Release pins resources to the dpll subsystem.
1478  */
1479 static void
1480 ice_dpll_deinit_direct_pins(bool cgu, struct ice_dpll_pin *pins, int count,
1481 			    const struct dpll_pin_ops *ops,
1482 			    struct dpll_device *first,
1483 			    struct dpll_device *second)
1484 {
1485 	if (cgu) {
1486 		ice_dpll_unregister_pins(first, pins, ops, count);
1487 		ice_dpll_unregister_pins(second, pins, ops, count);
1488 	}
1489 	ice_dpll_release_pins(pins, count);
1490 }
1491 
1492 /**
1493  * ice_dpll_init_direct_pins - initialize direct pins
1494  * @pf: board private structure
1495  * @cgu: if cgu is present and controlled by this NIC
1496  * @pins: pointer to pins array
1497  * @start_idx: on which index shall allocation start in dpll subsystem
1498  * @count: number of pins
1499  * @ops: callback ops registered with the pins
1500  * @first: dpll device pointer
1501  * @second: dpll device pointer
1502  *
1503  * Allocate directly connected pins of a given array in dpll subsystem.
1504  * If cgu is owned register allocated pins with given dplls.
1505  *
1506  * Return:
1507  * * 0 - success
1508  * * negative - registration failure reason
1509  */
1510 static int
1511 ice_dpll_init_direct_pins(struct ice_pf *pf, bool cgu,
1512 			  struct ice_dpll_pin *pins, int start_idx, int count,
1513 			  const struct dpll_pin_ops *ops,
1514 			  struct dpll_device *first, struct dpll_device *second)
1515 {
1516 	int ret;
1517 
1518 	ret = ice_dpll_get_pins(pf, pins, start_idx, count, pf->dplls.clock_id);
1519 	if (ret)
1520 		return ret;
1521 	if (cgu) {
1522 		ret = ice_dpll_register_pins(first, pins, ops, count);
1523 		if (ret)
1524 			goto release_pins;
1525 		ret = ice_dpll_register_pins(second, pins, ops, count);
1526 		if (ret)
1527 			goto unregister_first;
1528 	}
1529 
1530 	return 0;
1531 
1532 unregister_first:
1533 	ice_dpll_unregister_pins(first, pins, ops, count);
1534 release_pins:
1535 	ice_dpll_release_pins(pins, count);
1536 	return ret;
1537 }
1538 
1539 /**
1540  * ice_dpll_deinit_rclk_pin - release rclk pin resources
1541  * @pf: board private structure
1542  *
1543  * Deregister rclk pin from parent pins and release resources in dpll subsystem.
1544  */
1545 static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
1546 {
1547 	struct ice_dpll_pin *rclk = &pf->dplls.rclk;
1548 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
1549 	struct dpll_pin *parent;
1550 	int i;
1551 
1552 	for (i = 0; i < rclk->num_parents; i++) {
1553 		parent = pf->dplls.inputs[rclk->parent_idx[i]].pin;
1554 		if (!parent)
1555 			continue;
1556 		dpll_pin_on_pin_unregister(parent, rclk->pin,
1557 					   &ice_dpll_rclk_ops, rclk);
1558 	}
1559 	if (WARN_ON_ONCE(!vsi || !vsi->netdev))
1560 		return;
1561 	netdev_dpll_pin_clear(vsi->netdev);
1562 	dpll_pin_put(rclk->pin);
1563 }
1564 
1565 /**
1566  * ice_dpll_init_rclk_pins - initialize recovered clock pin
1567  * @pf: board private structure
1568  * @pin: pin to register
1569  * @start_idx: on which index shall allocation start in dpll subsystem
1570  * @ops: callback ops registered with the pins
1571  *
1572  * Allocate resource for recovered clock pin in dpll subsystem. Register the
1573  * pin with the parents it has in the info. Register pin with the pf's main vsi
1574  * netdev.
1575  *
1576  * Return:
1577  * * 0 - success
1578  * * negative - registration failure reason
1579  */
1580 static int
1581 ice_dpll_init_rclk_pins(struct ice_pf *pf, struct ice_dpll_pin *pin,
1582 			int start_idx, const struct dpll_pin_ops *ops)
1583 {
1584 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
1585 	struct dpll_pin *parent;
1586 	int ret, i;
1587 
1588 	ret = ice_dpll_get_pins(pf, pin, start_idx, ICE_DPLL_RCLK_NUM_PER_PF,
1589 				pf->dplls.clock_id);
1590 	if (ret)
1591 		return ret;
1592 	for (i = 0; i < pf->dplls.rclk.num_parents; i++) {
1593 		parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[i]].pin;
1594 		if (!parent) {
1595 			ret = -ENODEV;
1596 			goto unregister_pins;
1597 		}
1598 		ret = dpll_pin_on_pin_register(parent, pf->dplls.rclk.pin,
1599 					       ops, &pf->dplls.rclk);
1600 		if (ret)
1601 			goto unregister_pins;
1602 	}
1603 	if (WARN_ON((!vsi || !vsi->netdev)))
1604 		return -EINVAL;
1605 	netdev_dpll_pin_set(vsi->netdev, pf->dplls.rclk.pin);
1606 
1607 	return 0;
1608 
1609 unregister_pins:
1610 	while (i) {
1611 		parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[--i]].pin;
1612 		dpll_pin_on_pin_unregister(parent, pf->dplls.rclk.pin,
1613 					   &ice_dpll_rclk_ops, &pf->dplls.rclk);
1614 	}
1615 	ice_dpll_release_pins(pin, ICE_DPLL_RCLK_NUM_PER_PF);
1616 	return ret;
1617 }
1618 
1619 /**
1620  * ice_dpll_deinit_pins - deinitialize direct pins
1621  * @pf: board private structure
1622  * @cgu: if cgu is controlled by this pf
1623  *
1624  * If cgu is owned unregister directly connected pins from the dplls.
1625  * Release resources of directly connected pins from the dpll subsystem.
1626  */
1627 static void ice_dpll_deinit_pins(struct ice_pf *pf, bool cgu)
1628 {
1629 	struct ice_dpll_pin *outputs = pf->dplls.outputs;
1630 	struct ice_dpll_pin *inputs = pf->dplls.inputs;
1631 	int num_outputs = pf->dplls.num_outputs;
1632 	int num_inputs = pf->dplls.num_inputs;
1633 	struct ice_dplls *d = &pf->dplls;
1634 	struct ice_dpll *de = &d->eec;
1635 	struct ice_dpll *dp = &d->pps;
1636 
1637 	ice_dpll_deinit_rclk_pin(pf);
1638 	if (cgu) {
1639 		ice_dpll_unregister_pins(dp->dpll, inputs, &ice_dpll_input_ops,
1640 					 num_inputs);
1641 		ice_dpll_unregister_pins(de->dpll, inputs, &ice_dpll_input_ops,
1642 					 num_inputs);
1643 	}
1644 	ice_dpll_release_pins(inputs, num_inputs);
1645 	if (cgu) {
1646 		ice_dpll_unregister_pins(dp->dpll, outputs,
1647 					 &ice_dpll_output_ops, num_outputs);
1648 		ice_dpll_unregister_pins(de->dpll, outputs,
1649 					 &ice_dpll_output_ops, num_outputs);
1650 		ice_dpll_release_pins(outputs, num_outputs);
1651 	}
1652 }
1653 
1654 /**
1655  * ice_dpll_init_pins - init pins and register pins with a dplls
1656  * @pf: board private structure
1657  * @cgu: if cgu is present and controlled by this NIC
1658  *
1659  * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
1660  * subsystem.
1661  *
1662  * Return:
1663  * * 0 - success
1664  * * negative - initialization failure reason
1665  */
1666 static int ice_dpll_init_pins(struct ice_pf *pf, bool cgu)
1667 {
1668 	u32 rclk_idx;
1669 	int ret;
1670 
1671 	ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.inputs, 0,
1672 					pf->dplls.num_inputs,
1673 					&ice_dpll_input_ops,
1674 					pf->dplls.eec.dpll, pf->dplls.pps.dpll);
1675 	if (ret)
1676 		return ret;
1677 	if (cgu) {
1678 		ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.outputs,
1679 						pf->dplls.num_inputs,
1680 						pf->dplls.num_outputs,
1681 						&ice_dpll_output_ops,
1682 						pf->dplls.eec.dpll,
1683 						pf->dplls.pps.dpll);
1684 		if (ret)
1685 			goto deinit_inputs;
1686 	}
1687 	rclk_idx = pf->dplls.num_inputs + pf->dplls.num_outputs + pf->hw.pf_id;
1688 	ret = ice_dpll_init_rclk_pins(pf, &pf->dplls.rclk, rclk_idx,
1689 				      &ice_dpll_rclk_ops);
1690 	if (ret)
1691 		goto deinit_outputs;
1692 
1693 	return 0;
1694 deinit_outputs:
1695 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.outputs,
1696 				    pf->dplls.num_outputs,
1697 				    &ice_dpll_output_ops, pf->dplls.pps.dpll,
1698 				    pf->dplls.eec.dpll);
1699 deinit_inputs:
1700 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.inputs, pf->dplls.num_inputs,
1701 				    &ice_dpll_input_ops, pf->dplls.pps.dpll,
1702 				    pf->dplls.eec.dpll);
1703 	return ret;
1704 }
1705 
1706 /**
1707  * ice_dpll_deinit_dpll - deinitialize dpll device
1708  * @pf: board private structure
1709  * @d: pointer to ice_dpll
1710  * @cgu: if cgu is present and controlled by this NIC
1711  *
1712  * If cgu is owned unregister the dpll from dpll subsystem.
1713  * Release resources of dpll device from dpll subsystem.
1714  */
1715 static void
1716 ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
1717 {
1718 	if (cgu)
1719 		dpll_device_unregister(d->dpll, &ice_dpll_ops, d);
1720 	dpll_device_put(d->dpll);
1721 }
1722 
1723 /**
1724  * ice_dpll_init_dpll - initialize dpll device in dpll subsystem
1725  * @pf: board private structure
1726  * @d: dpll to be initialized
1727  * @cgu: if cgu is present and controlled by this NIC
1728  * @type: type of dpll being initialized
1729  *
1730  * Allocate dpll instance for this board in dpll subsystem, if cgu is controlled
1731  * by this NIC, register dpll with the callback ops.
1732  *
1733  * Return:
1734  * * 0 - success
1735  * * negative - initialization failure reason
1736  */
1737 static int
1738 ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
1739 		   enum dpll_type type)
1740 {
1741 	u64 clock_id = pf->dplls.clock_id;
1742 	int ret;
1743 
1744 	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE);
1745 	if (IS_ERR(d->dpll)) {
1746 		ret = PTR_ERR(d->dpll);
1747 		dev_err(ice_pf_to_dev(pf),
1748 			"dpll_device_get failed (%p) err=%d\n", d, ret);
1749 		return ret;
1750 	}
1751 	d->pf = pf;
1752 	if (cgu) {
1753 		ice_dpll_update_state(pf, d, true);
1754 		ret = dpll_device_register(d->dpll, type, &ice_dpll_ops, d);
1755 		if (ret) {
1756 			dpll_device_put(d->dpll);
1757 			return ret;
1758 		}
1759 	}
1760 
1761 	return 0;
1762 }
1763 
1764 /**
1765  * ice_dpll_deinit_worker - deinitialize dpll kworker
1766  * @pf: board private structure
1767  *
1768  * Stop dpll's kworker, release it's resources.
1769  */
1770 static void ice_dpll_deinit_worker(struct ice_pf *pf)
1771 {
1772 	struct ice_dplls *d = &pf->dplls;
1773 
1774 	kthread_cancel_delayed_work_sync(&d->work);
1775 	kthread_destroy_worker(d->kworker);
1776 }
1777 
1778 /**
1779  * ice_dpll_init_worker - Initialize DPLLs periodic worker
1780  * @pf: board private structure
1781  *
1782  * Create and start DPLLs periodic worker.
1783  *
1784  * Context: Shall be called after pf->dplls.lock is initialized.
1785  * Return:
1786  * * 0 - success
1787  * * negative - create worker failure
1788  */
1789 static int ice_dpll_init_worker(struct ice_pf *pf)
1790 {
1791 	struct ice_dplls *d = &pf->dplls;
1792 	struct kthread_worker *kworker;
1793 
1794 	kthread_init_delayed_work(&d->work, ice_dpll_periodic_work);
1795 	kworker = kthread_create_worker(0, "ice-dplls-%s",
1796 					dev_name(ice_pf_to_dev(pf)));
1797 	if (IS_ERR(kworker))
1798 		return PTR_ERR(kworker);
1799 	d->kworker = kworker;
1800 	d->cgu_state_acq_err_num = 0;
1801 	kthread_queue_delayed_work(d->kworker, &d->work, 0);
1802 
1803 	return 0;
1804 }
1805 
1806 /**
1807  * ice_dpll_init_info_direct_pins - initializes direct pins info
1808  * @pf: board private structure
1809  * @pin_type: type of pins being initialized
1810  *
1811  * Init information for directly connected pins, cache them in pf's pins
1812  * structures.
1813  *
1814  * Return:
1815  * * 0 - success
1816  * * negative - init failure reason
1817  */
1818 static int
1819 ice_dpll_init_info_direct_pins(struct ice_pf *pf,
1820 			       enum ice_dpll_pin_type pin_type)
1821 {
1822 	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
1823 	int num_pins, i, ret = -EINVAL;
1824 	struct ice_hw *hw = &pf->hw;
1825 	struct ice_dpll_pin *pins;
1826 	unsigned long caps;
1827 	u8 freq_supp_num;
1828 	bool input;
1829 
1830 	switch (pin_type) {
1831 	case ICE_DPLL_PIN_TYPE_INPUT:
1832 		pins = pf->dplls.inputs;
1833 		num_pins = pf->dplls.num_inputs;
1834 		input = true;
1835 		break;
1836 	case ICE_DPLL_PIN_TYPE_OUTPUT:
1837 		pins = pf->dplls.outputs;
1838 		num_pins = pf->dplls.num_outputs;
1839 		input = false;
1840 		break;
1841 	default:
1842 		return -EINVAL;
1843 	}
1844 
1845 	for (i = 0; i < num_pins; i++) {
1846 		caps = 0;
1847 		pins[i].idx = i;
1848 		pins[i].prop.board_label = ice_cgu_get_pin_name(hw, i, input);
1849 		pins[i].prop.type = ice_cgu_get_pin_type(hw, i, input);
1850 		if (input) {
1851 			ret = ice_aq_get_cgu_ref_prio(hw, de->dpll_idx, i,
1852 						      &de->input_prio[i]);
1853 			if (ret)
1854 				return ret;
1855 			ret = ice_aq_get_cgu_ref_prio(hw, dp->dpll_idx, i,
1856 						      &dp->input_prio[i]);
1857 			if (ret)
1858 				return ret;
1859 			caps |= (DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
1860 				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE);
1861 			pins[i].prop.phase_range.min =
1862 				pf->dplls.input_phase_adj_max;
1863 			pins[i].prop.phase_range.max =
1864 				-pf->dplls.input_phase_adj_max;
1865 		} else {
1866 			pins[i].prop.phase_range.min =
1867 				pf->dplls.output_phase_adj_max;
1868 			pins[i].prop.phase_range.max =
1869 				-pf->dplls.output_phase_adj_max;
1870 			ret = ice_cgu_get_output_pin_state_caps(hw, i, &caps);
1871 			if (ret)
1872 				return ret;
1873 		}
1874 		pins[i].prop.capabilities = caps;
1875 		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
1876 		if (ret)
1877 			return ret;
1878 		pins[i].prop.freq_supported =
1879 			ice_cgu_get_pin_freq_supp(hw, i, input, &freq_supp_num);
1880 		pins[i].prop.freq_supported_num = freq_supp_num;
1881 		pins[i].pf = pf;
1882 	}
1883 
1884 	return ret;
1885 }
1886 
1887 /**
1888  * ice_dpll_init_info_rclk_pin - initializes rclk pin information
1889  * @pf: board private structure
1890  *
1891  * Init information for rclk pin, cache them in pf->dplls.rclk.
1892  *
1893  * Return:
1894  * * 0 - success
1895  * * negative - init failure reason
1896  */
1897 static int ice_dpll_init_info_rclk_pin(struct ice_pf *pf)
1898 {
1899 	struct ice_dpll_pin *pin = &pf->dplls.rclk;
1900 
1901 	pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
1902 	pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
1903 	pin->pf = pf;
1904 
1905 	return ice_dpll_pin_state_update(pf, pin,
1906 					 ICE_DPLL_PIN_TYPE_RCLK_INPUT, NULL);
1907 }
1908 
1909 /**
1910  * ice_dpll_init_pins_info - init pins info wrapper
1911  * @pf: board private structure
1912  * @pin_type: type of pins being initialized
1913  *
1914  * Wraps functions for pin initialization.
1915  *
1916  * Return:
1917  * * 0 - success
1918  * * negative - init failure reason
1919  */
1920 static int
1921 ice_dpll_init_pins_info(struct ice_pf *pf, enum ice_dpll_pin_type pin_type)
1922 {
1923 	switch (pin_type) {
1924 	case ICE_DPLL_PIN_TYPE_INPUT:
1925 	case ICE_DPLL_PIN_TYPE_OUTPUT:
1926 		return ice_dpll_init_info_direct_pins(pf, pin_type);
1927 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
1928 		return ice_dpll_init_info_rclk_pin(pf);
1929 	default:
1930 		return -EINVAL;
1931 	}
1932 }
1933 
1934 /**
1935  * ice_dpll_deinit_info - release memory allocated for pins info
1936  * @pf: board private structure
1937  *
1938  * Release memory allocated for pins by ice_dpll_init_info function.
1939  */
1940 static void ice_dpll_deinit_info(struct ice_pf *pf)
1941 {
1942 	kfree(pf->dplls.inputs);
1943 	kfree(pf->dplls.outputs);
1944 	kfree(pf->dplls.eec.input_prio);
1945 	kfree(pf->dplls.pps.input_prio);
1946 }
1947 
1948 /**
1949  * ice_dpll_init_info - prepare pf's dpll information structure
1950  * @pf: board private structure
1951  * @cgu: if cgu is present and controlled by this NIC
1952  *
1953  * Acquire (from HW) and set basic dpll information (on pf->dplls struct).
1954  *
1955  * Return:
1956  * * 0 - success
1957  * * negative - init failure reason
1958  */
1959 static int ice_dpll_init_info(struct ice_pf *pf, bool cgu)
1960 {
1961 	struct ice_aqc_get_cgu_abilities abilities;
1962 	struct ice_dpll *de = &pf->dplls.eec;
1963 	struct ice_dpll *dp = &pf->dplls.pps;
1964 	struct ice_dplls *d = &pf->dplls;
1965 	struct ice_hw *hw = &pf->hw;
1966 	int ret, alloc_size, i;
1967 
1968 	d->clock_id = ice_generate_clock_id(pf);
1969 	ret = ice_aq_get_cgu_abilities(hw, &abilities);
1970 	if (ret) {
1971 		dev_err(ice_pf_to_dev(pf),
1972 			"err:%d %s failed to read cgu abilities\n",
1973 			ret, ice_aq_str(hw->adminq.sq_last_status));
1974 		return ret;
1975 	}
1976 
1977 	de->dpll_idx = abilities.eec_dpll_idx;
1978 	dp->dpll_idx = abilities.pps_dpll_idx;
1979 	d->num_inputs = abilities.num_inputs;
1980 	d->num_outputs = abilities.num_outputs;
1981 	d->input_phase_adj_max = le32_to_cpu(abilities.max_in_phase_adj);
1982 	d->output_phase_adj_max = le32_to_cpu(abilities.max_out_phase_adj);
1983 
1984 	alloc_size = sizeof(*d->inputs) * d->num_inputs;
1985 	d->inputs = kzalloc(alloc_size, GFP_KERNEL);
1986 	if (!d->inputs)
1987 		return -ENOMEM;
1988 
1989 	alloc_size = sizeof(*de->input_prio) * d->num_inputs;
1990 	de->input_prio = kzalloc(alloc_size, GFP_KERNEL);
1991 	if (!de->input_prio)
1992 		return -ENOMEM;
1993 
1994 	dp->input_prio = kzalloc(alloc_size, GFP_KERNEL);
1995 	if (!dp->input_prio)
1996 		return -ENOMEM;
1997 
1998 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_INPUT);
1999 	if (ret)
2000 		goto deinit_info;
2001 
2002 	if (cgu) {
2003 		alloc_size = sizeof(*d->outputs) * d->num_outputs;
2004 		d->outputs = kzalloc(alloc_size, GFP_KERNEL);
2005 		if (!d->outputs) {
2006 			ret = -ENOMEM;
2007 			goto deinit_info;
2008 		}
2009 
2010 		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_OUTPUT);
2011 		if (ret)
2012 			goto deinit_info;
2013 	}
2014 
2015 	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
2016 					&pf->dplls.rclk.num_parents);
2017 	if (ret)
2018 		return ret;
2019 	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
2020 		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
2021 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
2022 	if (ret)
2023 		return ret;
2024 	de->mode = DPLL_MODE_AUTOMATIC;
2025 	dp->mode = DPLL_MODE_AUTOMATIC;
2026 
2027 	dev_dbg(ice_pf_to_dev(pf),
2028 		"%s - success, inputs:%u, outputs:%u rclk-parents:%u\n",
2029 		__func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
2030 
2031 	return 0;
2032 
2033 deinit_info:
2034 	dev_err(ice_pf_to_dev(pf),
2035 		"%s - fail: d->inputs:%p, de->input_prio:%p, dp->input_prio:%p, d->outputs:%p\n",
2036 		__func__, d->inputs, de->input_prio,
2037 		dp->input_prio, d->outputs);
2038 	ice_dpll_deinit_info(pf);
2039 	return ret;
2040 }
2041 
2042 /**
2043  * ice_dpll_deinit - Disable the driver/HW support for dpll subsystem
2044  * the dpll device.
2045  * @pf: board private structure
2046  *
2047  * Handles the cleanup work required after dpll initialization, freeing
2048  * resources and unregistering the dpll, pin and all resources used for
2049  * handling them.
2050  *
2051  * Context: Destroys pf->dplls.lock mutex. Call only if ICE_FLAG_DPLL was set.
2052  */
2053 void ice_dpll_deinit(struct ice_pf *pf)
2054 {
2055 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
2056 
2057 	clear_bit(ICE_FLAG_DPLL, pf->flags);
2058 	if (cgu)
2059 		ice_dpll_deinit_worker(pf);
2060 
2061 	ice_dpll_deinit_pins(pf, cgu);
2062 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
2063 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
2064 	ice_dpll_deinit_info(pf);
2065 	mutex_destroy(&pf->dplls.lock);
2066 }
2067 
2068 /**
2069  * ice_dpll_init - initialize support for dpll subsystem
2070  * @pf: board private structure
2071  *
2072  * Set up the device dplls, register them and pins connected within Linux dpll
2073  * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
2074  * configuration requests.
2075  *
2076  * Context: Initializes pf->dplls.lock mutex.
2077  */
2078 void ice_dpll_init(struct ice_pf *pf)
2079 {
2080 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
2081 	struct ice_dplls *d = &pf->dplls;
2082 	int err = 0;
2083 
2084 	err = ice_dpll_init_info(pf, cgu);
2085 	if (err)
2086 		goto err_exit;
2087 	err = ice_dpll_init_dpll(pf, &pf->dplls.eec, cgu, DPLL_TYPE_EEC);
2088 	if (err)
2089 		goto deinit_info;
2090 	err = ice_dpll_init_dpll(pf, &pf->dplls.pps, cgu, DPLL_TYPE_PPS);
2091 	if (err)
2092 		goto deinit_eec;
2093 	err = ice_dpll_init_pins(pf, cgu);
2094 	if (err)
2095 		goto deinit_pps;
2096 	mutex_init(&d->lock);
2097 	if (cgu) {
2098 		err = ice_dpll_init_worker(pf);
2099 		if (err)
2100 			goto deinit_pins;
2101 	}
2102 	set_bit(ICE_FLAG_DPLL, pf->flags);
2103 
2104 	return;
2105 
2106 deinit_pins:
2107 	ice_dpll_deinit_pins(pf, cgu);
2108 deinit_pps:
2109 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
2110 deinit_eec:
2111 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
2112 deinit_info:
2113 	ice_dpll_deinit_info(pf);
2114 err_exit:
2115 	mutex_destroy(&d->lock);
2116 	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
2117 }
2118