xref: /linux/drivers/net/ethernet/intel/ice/ice_dpll.c (revision bdce82e960d1205d118662f575cec39379984e34)
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  * @status_error: status error value
492  * @extack: error reporting
493  *
494  * Dpll subsystem callback, provides dpll's lock status.
495  *
496  * Context: Acquires pf->dplls.lock
497  * Return:
498  * * 0 - success
499  * * negative - failure
500  */
501 static int
502 ice_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
503 			 enum dpll_lock_status *status,
504 			 enum dpll_lock_status_error *status_error,
505 			 struct netlink_ext_ack *extack)
506 {
507 	struct ice_dpll *d = dpll_priv;
508 	struct ice_pf *pf = d->pf;
509 
510 	mutex_lock(&pf->dplls.lock);
511 	*status = d->dpll_state;
512 	mutex_unlock(&pf->dplls.lock);
513 
514 	return 0;
515 }
516 
517 /**
518  * ice_dpll_mode_get - get dpll's working mode
519  * @dpll: registered dpll pointer
520  * @dpll_priv: private data pointer passed on dpll registration
521  * @mode: on success holds current working mode of dpll
522  * @extack: error reporting
523  *
524  * Dpll subsystem callback. Provides working mode of dpll.
525  *
526  * Context: Acquires pf->dplls.lock
527  * Return:
528  * * 0 - success
529  * * negative - failure
530  */
531 static int ice_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
532 			     enum dpll_mode *mode,
533 			     struct netlink_ext_ack *extack)
534 {
535 	struct ice_dpll *d = dpll_priv;
536 	struct ice_pf *pf = d->pf;
537 
538 	mutex_lock(&pf->dplls.lock);
539 	*mode = d->mode;
540 	mutex_unlock(&pf->dplls.lock);
541 
542 	return 0;
543 }
544 
545 /**
546  * ice_dpll_pin_state_set - set pin's state on dpll
547  * @pin: pointer to a pin
548  * @pin_priv: private data pointer passed on pin registration
549  * @dpll: registered dpll pointer
550  * @dpll_priv: private data pointer passed on dpll registration
551  * @enable: if pin shalll be enabled
552  * @extack: error reporting
553  * @pin_type: type of a pin
554  *
555  * Set pin state on a pin.
556  *
557  * Context: Acquires pf->dplls.lock
558  * Return:
559  * * 0 - OK or no change required
560  * * negative - error
561  */
562 static int
563 ice_dpll_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
564 		       const struct dpll_device *dpll, void *dpll_priv,
565 		       bool enable, struct netlink_ext_ack *extack,
566 		       enum ice_dpll_pin_type pin_type)
567 {
568 	struct ice_dpll_pin *p = pin_priv;
569 	struct ice_dpll *d = dpll_priv;
570 	struct ice_pf *pf = d->pf;
571 	int ret;
572 
573 	mutex_lock(&pf->dplls.lock);
574 	if (enable)
575 		ret = ice_dpll_pin_enable(&pf->hw, p, pin_type, extack);
576 	else
577 		ret = ice_dpll_pin_disable(&pf->hw, p, pin_type, extack);
578 	if (!ret)
579 		ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
580 	mutex_unlock(&pf->dplls.lock);
581 
582 	return ret;
583 }
584 
585 /**
586  * ice_dpll_output_state_set - enable/disable output pin on dpll device
587  * @pin: pointer to a pin
588  * @pin_priv: private data pointer passed on pin registration
589  * @dpll: dpll being configured
590  * @dpll_priv: private data pointer passed on dpll registration
591  * @state: state of pin to be set
592  * @extack: error reporting
593  *
594  * Dpll subsystem callback. Set given state on output type pin.
595  *
596  * Context: Calls a function which acquires pf->dplls.lock
597  * Return:
598  * * 0 - successfully enabled mode
599  * * negative - failed to enable mode
600  */
601 static int
602 ice_dpll_output_state_set(const struct dpll_pin *pin, void *pin_priv,
603 			  const struct dpll_device *dpll, void *dpll_priv,
604 			  enum dpll_pin_state state,
605 			  struct netlink_ext_ack *extack)
606 {
607 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
608 
609 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
610 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
611 }
612 
613 /**
614  * ice_dpll_input_state_set - enable/disable input pin on dpll levice
615  * @pin: pointer to a pin
616  * @pin_priv: private data pointer passed on pin registration
617  * @dpll: dpll being configured
618  * @dpll_priv: private data pointer passed on dpll registration
619  * @state: state of pin to be set
620  * @extack: error reporting
621  *
622  * Dpll subsystem callback. Enables given mode on input type pin.
623  *
624  * Context: Calls a function which acquires pf->dplls.lock
625  * Return:
626  * * 0 - successfully enabled mode
627  * * negative - failed to enable mode
628  */
629 static int
630 ice_dpll_input_state_set(const struct dpll_pin *pin, void *pin_priv,
631 			 const struct dpll_device *dpll, void *dpll_priv,
632 			 enum dpll_pin_state state,
633 			 struct netlink_ext_ack *extack)
634 {
635 	bool enable = state == DPLL_PIN_STATE_SELECTABLE;
636 
637 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
638 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
639 }
640 
641 /**
642  * ice_dpll_pin_state_get - set pin's state on dpll
643  * @pin: pointer to a pin
644  * @pin_priv: private data pointer passed on pin registration
645  * @dpll: registered dpll pointer
646  * @dpll_priv: private data pointer passed on dpll registration
647  * @state: on success holds state of the pin
648  * @extack: error reporting
649  * @pin_type: type of questioned pin
650  *
651  * Determine pin state set it on a pin.
652  *
653  * Context: Acquires pf->dplls.lock
654  * Return:
655  * * 0 - success
656  * * negative - failed to get state
657  */
658 static int
659 ice_dpll_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
660 		       const struct dpll_device *dpll, void *dpll_priv,
661 		       enum dpll_pin_state *state,
662 		       struct netlink_ext_ack *extack,
663 		       enum ice_dpll_pin_type pin_type)
664 {
665 	struct ice_dpll_pin *p = pin_priv;
666 	struct ice_dpll *d = dpll_priv;
667 	struct ice_pf *pf = d->pf;
668 	int ret;
669 
670 	mutex_lock(&pf->dplls.lock);
671 	ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
672 	if (ret)
673 		goto unlock;
674 	if (pin_type == ICE_DPLL_PIN_TYPE_INPUT)
675 		*state = p->state[d->dpll_idx];
676 	else if (pin_type == ICE_DPLL_PIN_TYPE_OUTPUT)
677 		*state = p->state[0];
678 	ret = 0;
679 unlock:
680 	mutex_unlock(&pf->dplls.lock);
681 
682 	return ret;
683 }
684 
685 /**
686  * ice_dpll_output_state_get - get output pin state on dpll device
687  * @pin: pointer to a pin
688  * @pin_priv: private data pointer passed on pin registration
689  * @dpll: registered dpll pointer
690  * @dpll_priv: private data pointer passed on dpll registration
691  * @state: on success holds state of the pin
692  * @extack: error reporting
693  *
694  * Dpll subsystem callback. Check state of a pin.
695  *
696  * Context: Calls a function which acquires pf->dplls.lock
697  * Return:
698  * * 0 - success
699  * * negative - failed to get state
700  */
701 static int
702 ice_dpll_output_state_get(const struct dpll_pin *pin, void *pin_priv,
703 			  const struct dpll_device *dpll, void *dpll_priv,
704 			  enum dpll_pin_state *state,
705 			  struct netlink_ext_ack *extack)
706 {
707 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
708 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
709 }
710 
711 /**
712  * ice_dpll_input_state_get - get input pin state on dpll device
713  * @pin: pointer to a pin
714  * @pin_priv: private data pointer passed on pin registration
715  * @dpll: registered dpll pointer
716  * @dpll_priv: private data pointer passed on dpll registration
717  * @state: on success holds state of the pin
718  * @extack: error reporting
719  *
720  * Dpll subsystem callback. Check state of a input pin.
721  *
722  * Context: Calls a function which acquires pf->dplls.lock
723  * Return:
724  * * 0 - success
725  * * negative - failed to get state
726  */
727 static int
728 ice_dpll_input_state_get(const struct dpll_pin *pin, void *pin_priv,
729 			 const struct dpll_device *dpll, void *dpll_priv,
730 			 enum dpll_pin_state *state,
731 			 struct netlink_ext_ack *extack)
732 {
733 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
734 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
735 }
736 
737 /**
738  * ice_dpll_input_prio_get - get dpll's input prio
739  * @pin: pointer to a pin
740  * @pin_priv: private data pointer passed on pin registration
741  * @dpll: registered dpll pointer
742  * @dpll_priv: private data pointer passed on dpll registration
743  * @prio: on success - returns input priority on dpll
744  * @extack: error reporting
745  *
746  * Dpll subsystem callback. Handler for getting priority of a input pin.
747  *
748  * Context: Acquires pf->dplls.lock
749  * Return:
750  * * 0 - success
751  * * negative - failure
752  */
753 static int
754 ice_dpll_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
755 			const struct dpll_device *dpll, void *dpll_priv,
756 			u32 *prio, struct netlink_ext_ack *extack)
757 {
758 	struct ice_dpll_pin *p = pin_priv;
759 	struct ice_dpll *d = dpll_priv;
760 	struct ice_pf *pf = d->pf;
761 
762 	mutex_lock(&pf->dplls.lock);
763 	*prio = d->input_prio[p->idx];
764 	mutex_unlock(&pf->dplls.lock);
765 
766 	return 0;
767 }
768 
769 /**
770  * ice_dpll_input_prio_set - set dpll input prio
771  * @pin: pointer to a pin
772  * @pin_priv: private data pointer passed on pin registration
773  * @dpll: registered dpll pointer
774  * @dpll_priv: private data pointer passed on dpll registration
775  * @prio: input priority to be set on dpll
776  * @extack: error reporting
777  *
778  * Dpll subsystem callback. Handler for setting priority of a input pin.
779  *
780  * Context: Acquires pf->dplls.lock
781  * Return:
782  * * 0 - success
783  * * negative - failure
784  */
785 static int
786 ice_dpll_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
787 			const struct dpll_device *dpll, void *dpll_priv,
788 			u32 prio, struct netlink_ext_ack *extack)
789 {
790 	struct ice_dpll_pin *p = pin_priv;
791 	struct ice_dpll *d = dpll_priv;
792 	struct ice_pf *pf = d->pf;
793 	int ret;
794 
795 	mutex_lock(&pf->dplls.lock);
796 	ret = ice_dpll_hw_input_prio_set(pf, d, p, prio, extack);
797 	mutex_unlock(&pf->dplls.lock);
798 
799 	return ret;
800 }
801 
802 /**
803  * ice_dpll_input_direction - callback for get input pin direction
804  * @pin: pointer to a pin
805  * @pin_priv: private data pointer passed on pin registration
806  * @dpll: registered dpll pointer
807  * @dpll_priv: private data pointer passed on dpll registration
808  * @direction: holds input pin direction
809  * @extack: error reporting
810  *
811  * Dpll subsystem callback. Handler for getting direction of a input pin.
812  *
813  * Return:
814  * * 0 - success
815  */
816 static int
817 ice_dpll_input_direction(const struct dpll_pin *pin, void *pin_priv,
818 			 const struct dpll_device *dpll, void *dpll_priv,
819 			 enum dpll_pin_direction *direction,
820 			 struct netlink_ext_ack *extack)
821 {
822 	*direction = DPLL_PIN_DIRECTION_INPUT;
823 
824 	return 0;
825 }
826 
827 /**
828  * ice_dpll_output_direction - callback for get output pin direction
829  * @pin: pointer to a pin
830  * @pin_priv: private data pointer passed on pin registration
831  * @dpll: registered dpll pointer
832  * @dpll_priv: private data pointer passed on dpll registration
833  * @direction: holds output pin direction
834  * @extack: error reporting
835  *
836  * Dpll subsystem callback. Handler for getting direction of an output pin.
837  *
838  * Return:
839  * * 0 - success
840  */
841 static int
842 ice_dpll_output_direction(const struct dpll_pin *pin, void *pin_priv,
843 			  const struct dpll_device *dpll, void *dpll_priv,
844 			  enum dpll_pin_direction *direction,
845 			  struct netlink_ext_ack *extack)
846 {
847 	*direction = DPLL_PIN_DIRECTION_OUTPUT;
848 
849 	return 0;
850 }
851 
852 /**
853  * ice_dpll_pin_phase_adjust_get - callback for get pin phase adjust value
854  * @pin: pointer to a pin
855  * @pin_priv: private data pointer passed on pin registration
856  * @dpll: registered dpll pointer
857  * @dpll_priv: private data pointer passed on dpll registration
858  * @phase_adjust: on success holds pin phase_adjust value
859  * @extack: error reporting
860  *
861  * Dpll subsystem callback. Handler for getting phase adjust value of a pin.
862  *
863  * Context: Acquires pf->dplls.lock
864  * Return:
865  * * 0 - success
866  * * negative - error
867  */
868 static int
869 ice_dpll_pin_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
870 			      const struct dpll_device *dpll, void *dpll_priv,
871 			      s32 *phase_adjust,
872 			      struct netlink_ext_ack *extack)
873 {
874 	struct ice_dpll_pin *p = pin_priv;
875 	struct ice_pf *pf = p->pf;
876 
877 	mutex_lock(&pf->dplls.lock);
878 	*phase_adjust = p->phase_adjust;
879 	mutex_unlock(&pf->dplls.lock);
880 
881 	return 0;
882 }
883 
884 /**
885  * ice_dpll_pin_phase_adjust_set - helper for setting a pin phase adjust value
886  * @pin: pointer to a pin
887  * @pin_priv: private data pointer passed on pin registration
888  * @dpll: registered dpll pointer
889  * @dpll_priv: private data pointer passed on dpll registration
890  * @phase_adjust: phase_adjust to be set
891  * @extack: error reporting
892  * @type: type of a pin
893  *
894  * Helper for dpll subsystem callback. Handler for setting phase adjust value
895  * of a pin.
896  *
897  * Context: Acquires pf->dplls.lock
898  * Return:
899  * * 0 - success
900  * * negative - error
901  */
902 static int
903 ice_dpll_pin_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
904 			      const struct dpll_device *dpll, void *dpll_priv,
905 			      s32 phase_adjust,
906 			      struct netlink_ext_ack *extack,
907 			      enum ice_dpll_pin_type type)
908 {
909 	struct ice_dpll_pin *p = pin_priv;
910 	struct ice_dpll *d = dpll_priv;
911 	struct ice_pf *pf = d->pf;
912 	u8 flag, flags_en = 0;
913 	int ret;
914 
915 	mutex_lock(&pf->dplls.lock);
916 	switch (type) {
917 	case ICE_DPLL_PIN_TYPE_INPUT:
918 		flag = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_DELAY;
919 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
920 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
921 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
922 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
923 		ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, flag, flags_en,
924 					       0, phase_adjust);
925 		break;
926 	case ICE_DPLL_PIN_TYPE_OUTPUT:
927 		flag = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_PHASE;
928 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
929 			flag |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
930 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
931 			flag |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
932 		ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flag, 0, 0,
933 						phase_adjust);
934 		break;
935 	default:
936 		ret = -EINVAL;
937 	}
938 	if (!ret)
939 		p->phase_adjust = phase_adjust;
940 	mutex_unlock(&pf->dplls.lock);
941 	if (ret)
942 		NL_SET_ERR_MSG_FMT(extack,
943 				   "err:%d %s failed to set pin phase_adjust:%d for pin:%u on dpll:%u\n",
944 				   ret,
945 				   ice_aq_str(pf->hw.adminq.sq_last_status),
946 				   phase_adjust, p->idx, d->dpll_idx);
947 
948 	return ret;
949 }
950 
951 /**
952  * ice_dpll_input_phase_adjust_set - callback for set input pin phase adjust
953  * @pin: pointer to a pin
954  * @pin_priv: private data pointer passed on pin registration
955  * @dpll: registered dpll pointer
956  * @dpll_priv: private data pointer passed on dpll registration
957  * @phase_adjust: phase_adjust to be set
958  * @extack: error reporting
959  *
960  * Dpll subsystem callback. Wraps a handler for setting phase adjust on input
961  * pin.
962  *
963  * Context: Calls a function which acquires pf->dplls.lock
964  * Return:
965  * * 0 - success
966  * * negative - error
967  */
968 static int
969 ice_dpll_input_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
970 				const struct dpll_device *dpll, void *dpll_priv,
971 				s32 phase_adjust,
972 				struct netlink_ext_ack *extack)
973 {
974 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
975 					     phase_adjust, extack,
976 					     ICE_DPLL_PIN_TYPE_INPUT);
977 }
978 
979 /**
980  * ice_dpll_output_phase_adjust_set - callback for set output pin phase adjust
981  * @pin: pointer to a pin
982  * @pin_priv: private data pointer passed on pin registration
983  * @dpll: registered dpll pointer
984  * @dpll_priv: private data pointer passed on dpll registration
985  * @phase_adjust: phase_adjust to be set
986  * @extack: error reporting
987  *
988  * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
989  * pin.
990  *
991  * Context: Calls a function which acquires pf->dplls.lock
992  * Return:
993  * * 0 - success
994  * * negative - error
995  */
996 static int
997 ice_dpll_output_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
998 				 const struct dpll_device *dpll, void *dpll_priv,
999 				 s32 phase_adjust,
1000 				 struct netlink_ext_ack *extack)
1001 {
1002 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
1003 					     phase_adjust, extack,
1004 					     ICE_DPLL_PIN_TYPE_OUTPUT);
1005 }
1006 
1007 #define ICE_DPLL_PHASE_OFFSET_DIVIDER	100
1008 #define ICE_DPLL_PHASE_OFFSET_FACTOR		\
1009 	(DPLL_PHASE_OFFSET_DIVIDER / ICE_DPLL_PHASE_OFFSET_DIVIDER)
1010 /**
1011  * ice_dpll_phase_offset_get - callback for get dpll phase shift value
1012  * @pin: pointer to a pin
1013  * @pin_priv: private data pointer passed on pin registration
1014  * @dpll: registered dpll pointer
1015  * @dpll_priv: private data pointer passed on dpll registration
1016  * @phase_offset: on success holds pin phase_offset value
1017  * @extack: error reporting
1018  *
1019  * Dpll subsystem callback. Handler for getting phase shift value between
1020  * dpll's input and output.
1021  *
1022  * Context: Acquires pf->dplls.lock
1023  * Return:
1024  * * 0 - success
1025  * * negative - error
1026  */
1027 static int
1028 ice_dpll_phase_offset_get(const struct dpll_pin *pin, void *pin_priv,
1029 			  const struct dpll_device *dpll, void *dpll_priv,
1030 			  s64 *phase_offset, struct netlink_ext_ack *extack)
1031 {
1032 	struct ice_dpll *d = dpll_priv;
1033 	struct ice_pf *pf = d->pf;
1034 
1035 	mutex_lock(&pf->dplls.lock);
1036 	if (d->active_input == pin)
1037 		*phase_offset = d->phase_offset * ICE_DPLL_PHASE_OFFSET_FACTOR;
1038 	else
1039 		*phase_offset = 0;
1040 	mutex_unlock(&pf->dplls.lock);
1041 
1042 	return 0;
1043 }
1044 
1045 /**
1046  * ice_dpll_rclk_state_on_pin_set - set a state on rclk pin
1047  * @pin: pointer to a pin
1048  * @pin_priv: private data pointer passed on pin registration
1049  * @parent_pin: pin parent pointer
1050  * @parent_pin_priv: parent private data pointer passed on pin registration
1051  * @state: state to be set on pin
1052  * @extack: error reporting
1053  *
1054  * Dpll subsystem callback, set a state of a rclk pin on a parent pin
1055  *
1056  * Context: Acquires pf->dplls.lock
1057  * Return:
1058  * * 0 - success
1059  * * negative - failure
1060  */
1061 static int
1062 ice_dpll_rclk_state_on_pin_set(const struct dpll_pin *pin, void *pin_priv,
1063 			       const struct dpll_pin *parent_pin,
1064 			       void *parent_pin_priv,
1065 			       enum dpll_pin_state state,
1066 			       struct netlink_ext_ack *extack)
1067 {
1068 	struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
1069 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
1070 	struct ice_pf *pf = p->pf;
1071 	int ret = -EINVAL;
1072 	u32 hw_idx;
1073 
1074 	mutex_lock(&pf->dplls.lock);
1075 	hw_idx = parent->idx - pf->dplls.base_rclk_idx;
1076 	if (hw_idx >= pf->dplls.num_inputs)
1077 		goto unlock;
1078 
1079 	if ((enable && p->state[hw_idx] == DPLL_PIN_STATE_CONNECTED) ||
1080 	    (!enable && p->state[hw_idx] == DPLL_PIN_STATE_DISCONNECTED)) {
1081 		NL_SET_ERR_MSG_FMT(extack,
1082 				   "pin:%u state:%u on parent:%u already set",
1083 				   p->idx, state, parent->idx);
1084 		goto unlock;
1085 	}
1086 	ret = ice_aq_set_phy_rec_clk_out(&pf->hw, hw_idx, enable,
1087 					 &p->freq);
1088 	if (ret)
1089 		NL_SET_ERR_MSG_FMT(extack,
1090 				   "err:%d %s failed to set pin state:%u for pin:%u on parent:%u\n",
1091 				   ret,
1092 				   ice_aq_str(pf->hw.adminq.sq_last_status),
1093 				   state, p->idx, parent->idx);
1094 unlock:
1095 	mutex_unlock(&pf->dplls.lock);
1096 
1097 	return ret;
1098 }
1099 
1100 /**
1101  * ice_dpll_rclk_state_on_pin_get - get a state of rclk pin
1102  * @pin: pointer to a pin
1103  * @pin_priv: private data pointer passed on pin registration
1104  * @parent_pin: pin parent pointer
1105  * @parent_pin_priv: pin parent priv data pointer passed on pin registration
1106  * @state: on success holds pin state on parent pin
1107  * @extack: error reporting
1108  *
1109  * dpll subsystem callback, get a state of a recovered clock pin.
1110  *
1111  * Context: Acquires pf->dplls.lock
1112  * Return:
1113  * * 0 - success
1114  * * negative - failure
1115  */
1116 static int
1117 ice_dpll_rclk_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
1118 			       const struct dpll_pin *parent_pin,
1119 			       void *parent_pin_priv,
1120 			       enum dpll_pin_state *state,
1121 			       struct netlink_ext_ack *extack)
1122 {
1123 	struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
1124 	struct ice_pf *pf = p->pf;
1125 	int ret = -EINVAL;
1126 	u32 hw_idx;
1127 
1128 	mutex_lock(&pf->dplls.lock);
1129 	hw_idx = parent->idx - pf->dplls.base_rclk_idx;
1130 	if (hw_idx >= pf->dplls.num_inputs)
1131 		goto unlock;
1132 
1133 	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_RCLK_INPUT,
1134 					extack);
1135 	if (ret)
1136 		goto unlock;
1137 
1138 	*state = p->state[hw_idx];
1139 	ret = 0;
1140 unlock:
1141 	mutex_unlock(&pf->dplls.lock);
1142 
1143 	return ret;
1144 }
1145 
1146 static const struct dpll_pin_ops ice_dpll_rclk_ops = {
1147 	.state_on_pin_set = ice_dpll_rclk_state_on_pin_set,
1148 	.state_on_pin_get = ice_dpll_rclk_state_on_pin_get,
1149 	.direction_get = ice_dpll_input_direction,
1150 };
1151 
1152 static const struct dpll_pin_ops ice_dpll_input_ops = {
1153 	.frequency_get = ice_dpll_input_frequency_get,
1154 	.frequency_set = ice_dpll_input_frequency_set,
1155 	.state_on_dpll_get = ice_dpll_input_state_get,
1156 	.state_on_dpll_set = ice_dpll_input_state_set,
1157 	.prio_get = ice_dpll_input_prio_get,
1158 	.prio_set = ice_dpll_input_prio_set,
1159 	.direction_get = ice_dpll_input_direction,
1160 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
1161 	.phase_adjust_set = ice_dpll_input_phase_adjust_set,
1162 	.phase_offset_get = ice_dpll_phase_offset_get,
1163 };
1164 
1165 static const struct dpll_pin_ops ice_dpll_output_ops = {
1166 	.frequency_get = ice_dpll_output_frequency_get,
1167 	.frequency_set = ice_dpll_output_frequency_set,
1168 	.state_on_dpll_get = ice_dpll_output_state_get,
1169 	.state_on_dpll_set = ice_dpll_output_state_set,
1170 	.direction_get = ice_dpll_output_direction,
1171 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
1172 	.phase_adjust_set = ice_dpll_output_phase_adjust_set,
1173 };
1174 
1175 static const struct dpll_device_ops ice_dpll_ops = {
1176 	.lock_status_get = ice_dpll_lock_status_get,
1177 	.mode_get = ice_dpll_mode_get,
1178 };
1179 
1180 /**
1181  * ice_generate_clock_id - generates unique clock_id for registering dpll.
1182  * @pf: board private structure
1183  *
1184  * Generates unique (per board) clock_id for allocation and search of dpll
1185  * devices in Linux dpll subsystem.
1186  *
1187  * Return: generated clock id for the board
1188  */
1189 static u64 ice_generate_clock_id(struct ice_pf *pf)
1190 {
1191 	return pci_get_dsn(pf->pdev);
1192 }
1193 
1194 /**
1195  * ice_dpll_notify_changes - notify dpll subsystem about changes
1196  * @d: pointer do dpll
1197  *
1198  * Once change detected appropriate event is submitted to the dpll subsystem.
1199  */
1200 static void ice_dpll_notify_changes(struct ice_dpll *d)
1201 {
1202 	bool pin_notified = false;
1203 
1204 	if (d->prev_dpll_state != d->dpll_state) {
1205 		d->prev_dpll_state = d->dpll_state;
1206 		dpll_device_change_ntf(d->dpll);
1207 	}
1208 	if (d->prev_input != d->active_input) {
1209 		if (d->prev_input)
1210 			dpll_pin_change_ntf(d->prev_input);
1211 		d->prev_input = d->active_input;
1212 		if (d->active_input) {
1213 			dpll_pin_change_ntf(d->active_input);
1214 			pin_notified = true;
1215 		}
1216 	}
1217 	if (d->prev_phase_offset != d->phase_offset) {
1218 		d->prev_phase_offset = d->phase_offset;
1219 		if (!pin_notified && d->active_input)
1220 			dpll_pin_change_ntf(d->active_input);
1221 	}
1222 }
1223 
1224 /**
1225  * ice_dpll_update_state - update dpll state
1226  * @pf: pf private structure
1227  * @d: pointer to queried dpll device
1228  * @init: if function called on initialization of ice dpll
1229  *
1230  * Poll current state of dpll from hw and update ice_dpll struct.
1231  *
1232  * Context: Called by kworker under pf->dplls.lock
1233  * Return:
1234  * * 0 - success
1235  * * negative - AQ failure
1236  */
1237 static int
1238 ice_dpll_update_state(struct ice_pf *pf, struct ice_dpll *d, bool init)
1239 {
1240 	struct ice_dpll_pin *p = NULL;
1241 	int ret;
1242 
1243 	ret = ice_get_cgu_state(&pf->hw, d->dpll_idx, d->prev_dpll_state,
1244 				&d->input_idx, &d->ref_state, &d->eec_mode,
1245 				&d->phase_offset, &d->dpll_state);
1246 
1247 	dev_dbg(ice_pf_to_dev(pf),
1248 		"update dpll=%d, prev_src_idx:%u, src_idx:%u, state:%d, prev:%d mode:%d\n",
1249 		d->dpll_idx, d->prev_input_idx, d->input_idx,
1250 		d->dpll_state, d->prev_dpll_state, d->mode);
1251 	if (ret) {
1252 		dev_err(ice_pf_to_dev(pf),
1253 			"update dpll=%d state failed, ret=%d %s\n",
1254 			d->dpll_idx, ret,
1255 			ice_aq_str(pf->hw.adminq.sq_last_status));
1256 		return ret;
1257 	}
1258 	if (init) {
1259 		if (d->dpll_state == DPLL_LOCK_STATUS_LOCKED ||
1260 		    d->dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ)
1261 			d->active_input = pf->dplls.inputs[d->input_idx].pin;
1262 		p = &pf->dplls.inputs[d->input_idx];
1263 		return ice_dpll_pin_state_update(pf, p,
1264 						 ICE_DPLL_PIN_TYPE_INPUT, NULL);
1265 	}
1266 	if (d->dpll_state == DPLL_LOCK_STATUS_HOLDOVER ||
1267 	    d->dpll_state == DPLL_LOCK_STATUS_UNLOCKED) {
1268 		d->active_input = NULL;
1269 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID)
1270 			p = &pf->dplls.inputs[d->input_idx];
1271 		d->prev_input_idx = ICE_DPLL_PIN_IDX_INVALID;
1272 		d->input_idx = ICE_DPLL_PIN_IDX_INVALID;
1273 		if (!p)
1274 			return 0;
1275 		ret = ice_dpll_pin_state_update(pf, p,
1276 						ICE_DPLL_PIN_TYPE_INPUT, NULL);
1277 	} else if (d->input_idx != d->prev_input_idx) {
1278 		if (d->prev_input_idx != ICE_DPLL_PIN_IDX_INVALID) {
1279 			p = &pf->dplls.inputs[d->prev_input_idx];
1280 			ice_dpll_pin_state_update(pf, p,
1281 						  ICE_DPLL_PIN_TYPE_INPUT,
1282 						  NULL);
1283 		}
1284 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID) {
1285 			p = &pf->dplls.inputs[d->input_idx];
1286 			d->active_input = p->pin;
1287 			ice_dpll_pin_state_update(pf, p,
1288 						  ICE_DPLL_PIN_TYPE_INPUT,
1289 						  NULL);
1290 		}
1291 		d->prev_input_idx = d->input_idx;
1292 	}
1293 
1294 	return ret;
1295 }
1296 
1297 /**
1298  * ice_dpll_periodic_work - DPLLs periodic worker
1299  * @work: pointer to kthread_work structure
1300  *
1301  * DPLLs periodic worker is responsible for polling state of dpll.
1302  * Context: Holds pf->dplls.lock
1303  */
1304 static void ice_dpll_periodic_work(struct kthread_work *work)
1305 {
1306 	struct ice_dplls *d = container_of(work, struct ice_dplls, work.work);
1307 	struct ice_pf *pf = container_of(d, struct ice_pf, dplls);
1308 	struct ice_dpll *de = &pf->dplls.eec;
1309 	struct ice_dpll *dp = &pf->dplls.pps;
1310 	int ret;
1311 
1312 	mutex_lock(&pf->dplls.lock);
1313 	ret = ice_dpll_update_state(pf, de, false);
1314 	if (!ret)
1315 		ret = ice_dpll_update_state(pf, dp, false);
1316 	if (ret) {
1317 		d->cgu_state_acq_err_num++;
1318 		/* stop rescheduling this worker */
1319 		if (d->cgu_state_acq_err_num >
1320 		    ICE_CGU_STATE_ACQ_ERR_THRESHOLD) {
1321 			dev_err(ice_pf_to_dev(pf),
1322 				"EEC/PPS DPLLs periodic work disabled\n");
1323 			mutex_unlock(&pf->dplls.lock);
1324 			return;
1325 		}
1326 	}
1327 	mutex_unlock(&pf->dplls.lock);
1328 	ice_dpll_notify_changes(de);
1329 	ice_dpll_notify_changes(dp);
1330 
1331 	/* Run twice a second or reschedule if update failed */
1332 	kthread_queue_delayed_work(d->kworker, &d->work,
1333 				   ret ? msecs_to_jiffies(10) :
1334 				   msecs_to_jiffies(500));
1335 }
1336 
1337 /**
1338  * ice_dpll_release_pins - release pins resources from dpll subsystem
1339  * @pins: pointer to pins array
1340  * @count: number of pins
1341  *
1342  * Release resources of given pins array in the dpll subsystem.
1343  */
1344 static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
1345 {
1346 	int i;
1347 
1348 	for (i = 0; i < count; i++)
1349 		dpll_pin_put(pins[i].pin);
1350 }
1351 
1352 /**
1353  * ice_dpll_get_pins - get pins from dpll subsystem
1354  * @pf: board private structure
1355  * @pins: pointer to pins array
1356  * @start_idx: get starts from this pin idx value
1357  * @count: number of pins
1358  * @clock_id: clock_id of dpll device
1359  *
1360  * Get pins - allocate - in dpll subsystem, store them in pin field of given
1361  * pins array.
1362  *
1363  * Return:
1364  * * 0 - success
1365  * * negative - allocation failure reason
1366  */
1367 static int
1368 ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
1369 		  int start_idx, int count, u64 clock_id)
1370 {
1371 	int i, ret;
1372 
1373 	for (i = 0; i < count; i++) {
1374 		pins[i].pin = dpll_pin_get(clock_id, i + start_idx, THIS_MODULE,
1375 					   &pins[i].prop);
1376 		if (IS_ERR(pins[i].pin)) {
1377 			ret = PTR_ERR(pins[i].pin);
1378 			goto release_pins;
1379 		}
1380 	}
1381 
1382 	return 0;
1383 
1384 release_pins:
1385 	while (--i >= 0)
1386 		dpll_pin_put(pins[i].pin);
1387 	return ret;
1388 }
1389 
1390 /**
1391  * ice_dpll_unregister_pins - unregister pins from a dpll
1392  * @dpll: dpll device pointer
1393  * @pins: pointer to pins array
1394  * @ops: callback ops registered with the pins
1395  * @count: number of pins
1396  *
1397  * Unregister pins of a given array of pins from given dpll device registered in
1398  * dpll subsystem.
1399  */
1400 static void
1401 ice_dpll_unregister_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
1402 			 const struct dpll_pin_ops *ops, int count)
1403 {
1404 	int i;
1405 
1406 	for (i = 0; i < count; i++)
1407 		dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
1408 }
1409 
1410 /**
1411  * ice_dpll_register_pins - register pins with a dpll
1412  * @dpll: dpll pointer to register pins with
1413  * @pins: pointer to pins array
1414  * @ops: callback ops registered with the pins
1415  * @count: number of pins
1416  *
1417  * Register pins of a given array with given dpll in dpll subsystem.
1418  *
1419  * Return:
1420  * * 0 - success
1421  * * negative - registration failure reason
1422  */
1423 static int
1424 ice_dpll_register_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
1425 		       const struct dpll_pin_ops *ops, int count)
1426 {
1427 	int ret, i;
1428 
1429 	for (i = 0; i < count; i++) {
1430 		ret = dpll_pin_register(dpll, pins[i].pin, ops, &pins[i]);
1431 		if (ret)
1432 			goto unregister_pins;
1433 	}
1434 
1435 	return 0;
1436 
1437 unregister_pins:
1438 	while (--i >= 0)
1439 		dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
1440 	return ret;
1441 }
1442 
1443 /**
1444  * ice_dpll_deinit_direct_pins - deinitialize direct pins
1445  * @cgu: if cgu is present and controlled by this NIC
1446  * @pins: pointer to pins array
1447  * @count: number of pins
1448  * @ops: callback ops registered with the pins
1449  * @first: dpll device pointer
1450  * @second: dpll device pointer
1451  *
1452  * If cgu is owned unregister pins from given dplls.
1453  * Release pins resources to the dpll subsystem.
1454  */
1455 static void
1456 ice_dpll_deinit_direct_pins(bool cgu, struct ice_dpll_pin *pins, int count,
1457 			    const struct dpll_pin_ops *ops,
1458 			    struct dpll_device *first,
1459 			    struct dpll_device *second)
1460 {
1461 	if (cgu) {
1462 		ice_dpll_unregister_pins(first, pins, ops, count);
1463 		ice_dpll_unregister_pins(second, pins, ops, count);
1464 	}
1465 	ice_dpll_release_pins(pins, count);
1466 }
1467 
1468 /**
1469  * ice_dpll_init_direct_pins - initialize direct pins
1470  * @pf: board private structure
1471  * @cgu: if cgu is present and controlled by this NIC
1472  * @pins: pointer to pins array
1473  * @start_idx: on which index shall allocation start in dpll subsystem
1474  * @count: number of pins
1475  * @ops: callback ops registered with the pins
1476  * @first: dpll device pointer
1477  * @second: dpll device pointer
1478  *
1479  * Allocate directly connected pins of a given array in dpll subsystem.
1480  * If cgu is owned register allocated pins with given dplls.
1481  *
1482  * Return:
1483  * * 0 - success
1484  * * negative - registration failure reason
1485  */
1486 static int
1487 ice_dpll_init_direct_pins(struct ice_pf *pf, bool cgu,
1488 			  struct ice_dpll_pin *pins, int start_idx, int count,
1489 			  const struct dpll_pin_ops *ops,
1490 			  struct dpll_device *first, struct dpll_device *second)
1491 {
1492 	int ret;
1493 
1494 	ret = ice_dpll_get_pins(pf, pins, start_idx, count, pf->dplls.clock_id);
1495 	if (ret)
1496 		return ret;
1497 	if (cgu) {
1498 		ret = ice_dpll_register_pins(first, pins, ops, count);
1499 		if (ret)
1500 			goto release_pins;
1501 		ret = ice_dpll_register_pins(second, pins, ops, count);
1502 		if (ret)
1503 			goto unregister_first;
1504 	}
1505 
1506 	return 0;
1507 
1508 unregister_first:
1509 	ice_dpll_unregister_pins(first, pins, ops, count);
1510 release_pins:
1511 	ice_dpll_release_pins(pins, count);
1512 	return ret;
1513 }
1514 
1515 /**
1516  * ice_dpll_deinit_rclk_pin - release rclk pin resources
1517  * @pf: board private structure
1518  *
1519  * Deregister rclk pin from parent pins and release resources in dpll subsystem.
1520  */
1521 static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
1522 {
1523 	struct ice_dpll_pin *rclk = &pf->dplls.rclk;
1524 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
1525 	struct dpll_pin *parent;
1526 	int i;
1527 
1528 	for (i = 0; i < rclk->num_parents; i++) {
1529 		parent = pf->dplls.inputs[rclk->parent_idx[i]].pin;
1530 		if (!parent)
1531 			continue;
1532 		dpll_pin_on_pin_unregister(parent, rclk->pin,
1533 					   &ice_dpll_rclk_ops, rclk);
1534 	}
1535 	if (WARN_ON_ONCE(!vsi || !vsi->netdev))
1536 		return;
1537 	netdev_dpll_pin_clear(vsi->netdev);
1538 	dpll_pin_put(rclk->pin);
1539 }
1540 
1541 /**
1542  * ice_dpll_init_rclk_pins - initialize recovered clock pin
1543  * @pf: board private structure
1544  * @pin: pin to register
1545  * @start_idx: on which index shall allocation start in dpll subsystem
1546  * @ops: callback ops registered with the pins
1547  *
1548  * Allocate resource for recovered clock pin in dpll subsystem. Register the
1549  * pin with the parents it has in the info. Register pin with the pf's main vsi
1550  * netdev.
1551  *
1552  * Return:
1553  * * 0 - success
1554  * * negative - registration failure reason
1555  */
1556 static int
1557 ice_dpll_init_rclk_pins(struct ice_pf *pf, struct ice_dpll_pin *pin,
1558 			int start_idx, const struct dpll_pin_ops *ops)
1559 {
1560 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
1561 	struct dpll_pin *parent;
1562 	int ret, i;
1563 
1564 	ret = ice_dpll_get_pins(pf, pin, start_idx, ICE_DPLL_RCLK_NUM_PER_PF,
1565 				pf->dplls.clock_id);
1566 	if (ret)
1567 		return ret;
1568 	for (i = 0; i < pf->dplls.rclk.num_parents; i++) {
1569 		parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[i]].pin;
1570 		if (!parent) {
1571 			ret = -ENODEV;
1572 			goto unregister_pins;
1573 		}
1574 		ret = dpll_pin_on_pin_register(parent, pf->dplls.rclk.pin,
1575 					       ops, &pf->dplls.rclk);
1576 		if (ret)
1577 			goto unregister_pins;
1578 	}
1579 	if (WARN_ON((!vsi || !vsi->netdev)))
1580 		return -EINVAL;
1581 	netdev_dpll_pin_set(vsi->netdev, pf->dplls.rclk.pin);
1582 
1583 	return 0;
1584 
1585 unregister_pins:
1586 	while (i) {
1587 		parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[--i]].pin;
1588 		dpll_pin_on_pin_unregister(parent, pf->dplls.rclk.pin,
1589 					   &ice_dpll_rclk_ops, &pf->dplls.rclk);
1590 	}
1591 	ice_dpll_release_pins(pin, ICE_DPLL_RCLK_NUM_PER_PF);
1592 	return ret;
1593 }
1594 
1595 /**
1596  * ice_dpll_deinit_pins - deinitialize direct pins
1597  * @pf: board private structure
1598  * @cgu: if cgu is controlled by this pf
1599  *
1600  * If cgu is owned unregister directly connected pins from the dplls.
1601  * Release resources of directly connected pins from the dpll subsystem.
1602  */
1603 static void ice_dpll_deinit_pins(struct ice_pf *pf, bool cgu)
1604 {
1605 	struct ice_dpll_pin *outputs = pf->dplls.outputs;
1606 	struct ice_dpll_pin *inputs = pf->dplls.inputs;
1607 	int num_outputs = pf->dplls.num_outputs;
1608 	int num_inputs = pf->dplls.num_inputs;
1609 	struct ice_dplls *d = &pf->dplls;
1610 	struct ice_dpll *de = &d->eec;
1611 	struct ice_dpll *dp = &d->pps;
1612 
1613 	ice_dpll_deinit_rclk_pin(pf);
1614 	if (cgu) {
1615 		ice_dpll_unregister_pins(dp->dpll, inputs, &ice_dpll_input_ops,
1616 					 num_inputs);
1617 		ice_dpll_unregister_pins(de->dpll, inputs, &ice_dpll_input_ops,
1618 					 num_inputs);
1619 	}
1620 	ice_dpll_release_pins(inputs, num_inputs);
1621 	if (cgu) {
1622 		ice_dpll_unregister_pins(dp->dpll, outputs,
1623 					 &ice_dpll_output_ops, num_outputs);
1624 		ice_dpll_unregister_pins(de->dpll, outputs,
1625 					 &ice_dpll_output_ops, num_outputs);
1626 		ice_dpll_release_pins(outputs, num_outputs);
1627 	}
1628 }
1629 
1630 /**
1631  * ice_dpll_init_pins - init pins and register pins with a dplls
1632  * @pf: board private structure
1633  * @cgu: if cgu is present and controlled by this NIC
1634  *
1635  * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
1636  * subsystem.
1637  *
1638  * Return:
1639  * * 0 - success
1640  * * negative - initialization failure reason
1641  */
1642 static int ice_dpll_init_pins(struct ice_pf *pf, bool cgu)
1643 {
1644 	u32 rclk_idx;
1645 	int ret;
1646 
1647 	ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.inputs, 0,
1648 					pf->dplls.num_inputs,
1649 					&ice_dpll_input_ops,
1650 					pf->dplls.eec.dpll, pf->dplls.pps.dpll);
1651 	if (ret)
1652 		return ret;
1653 	if (cgu) {
1654 		ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.outputs,
1655 						pf->dplls.num_inputs,
1656 						pf->dplls.num_outputs,
1657 						&ice_dpll_output_ops,
1658 						pf->dplls.eec.dpll,
1659 						pf->dplls.pps.dpll);
1660 		if (ret)
1661 			goto deinit_inputs;
1662 	}
1663 	rclk_idx = pf->dplls.num_inputs + pf->dplls.num_outputs + pf->hw.pf_id;
1664 	ret = ice_dpll_init_rclk_pins(pf, &pf->dplls.rclk, rclk_idx,
1665 				      &ice_dpll_rclk_ops);
1666 	if (ret)
1667 		goto deinit_outputs;
1668 
1669 	return 0;
1670 deinit_outputs:
1671 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.outputs,
1672 				    pf->dplls.num_outputs,
1673 				    &ice_dpll_output_ops, pf->dplls.pps.dpll,
1674 				    pf->dplls.eec.dpll);
1675 deinit_inputs:
1676 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.inputs, pf->dplls.num_inputs,
1677 				    &ice_dpll_input_ops, pf->dplls.pps.dpll,
1678 				    pf->dplls.eec.dpll);
1679 	return ret;
1680 }
1681 
1682 /**
1683  * ice_dpll_deinit_dpll - deinitialize dpll device
1684  * @pf: board private structure
1685  * @d: pointer to ice_dpll
1686  * @cgu: if cgu is present and controlled by this NIC
1687  *
1688  * If cgu is owned unregister the dpll from dpll subsystem.
1689  * Release resources of dpll device from dpll subsystem.
1690  */
1691 static void
1692 ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
1693 {
1694 	if (cgu)
1695 		dpll_device_unregister(d->dpll, &ice_dpll_ops, d);
1696 	dpll_device_put(d->dpll);
1697 }
1698 
1699 /**
1700  * ice_dpll_init_dpll - initialize dpll device in dpll subsystem
1701  * @pf: board private structure
1702  * @d: dpll to be initialized
1703  * @cgu: if cgu is present and controlled by this NIC
1704  * @type: type of dpll being initialized
1705  *
1706  * Allocate dpll instance for this board in dpll subsystem, if cgu is controlled
1707  * by this NIC, register dpll with the callback ops.
1708  *
1709  * Return:
1710  * * 0 - success
1711  * * negative - initialization failure reason
1712  */
1713 static int
1714 ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
1715 		   enum dpll_type type)
1716 {
1717 	u64 clock_id = pf->dplls.clock_id;
1718 	int ret;
1719 
1720 	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE);
1721 	if (IS_ERR(d->dpll)) {
1722 		ret = PTR_ERR(d->dpll);
1723 		dev_err(ice_pf_to_dev(pf),
1724 			"dpll_device_get failed (%p) err=%d\n", d, ret);
1725 		return ret;
1726 	}
1727 	d->pf = pf;
1728 	if (cgu) {
1729 		ice_dpll_update_state(pf, d, true);
1730 		ret = dpll_device_register(d->dpll, type, &ice_dpll_ops, d);
1731 		if (ret) {
1732 			dpll_device_put(d->dpll);
1733 			return ret;
1734 		}
1735 	}
1736 
1737 	return 0;
1738 }
1739 
1740 /**
1741  * ice_dpll_deinit_worker - deinitialize dpll kworker
1742  * @pf: board private structure
1743  *
1744  * Stop dpll's kworker, release it's resources.
1745  */
1746 static void ice_dpll_deinit_worker(struct ice_pf *pf)
1747 {
1748 	struct ice_dplls *d = &pf->dplls;
1749 
1750 	kthread_cancel_delayed_work_sync(&d->work);
1751 	kthread_destroy_worker(d->kworker);
1752 }
1753 
1754 /**
1755  * ice_dpll_init_worker - Initialize DPLLs periodic worker
1756  * @pf: board private structure
1757  *
1758  * Create and start DPLLs periodic worker.
1759  *
1760  * Context: Shall be called after pf->dplls.lock is initialized.
1761  * Return:
1762  * * 0 - success
1763  * * negative - create worker failure
1764  */
1765 static int ice_dpll_init_worker(struct ice_pf *pf)
1766 {
1767 	struct ice_dplls *d = &pf->dplls;
1768 	struct kthread_worker *kworker;
1769 
1770 	kthread_init_delayed_work(&d->work, ice_dpll_periodic_work);
1771 	kworker = kthread_create_worker(0, "ice-dplls-%s",
1772 					dev_name(ice_pf_to_dev(pf)));
1773 	if (IS_ERR(kworker))
1774 		return PTR_ERR(kworker);
1775 	d->kworker = kworker;
1776 	d->cgu_state_acq_err_num = 0;
1777 	kthread_queue_delayed_work(d->kworker, &d->work, 0);
1778 
1779 	return 0;
1780 }
1781 
1782 /**
1783  * ice_dpll_init_info_direct_pins - initializes direct pins info
1784  * @pf: board private structure
1785  * @pin_type: type of pins being initialized
1786  *
1787  * Init information for directly connected pins, cache them in pf's pins
1788  * structures.
1789  *
1790  * Return:
1791  * * 0 - success
1792  * * negative - init failure reason
1793  */
1794 static int
1795 ice_dpll_init_info_direct_pins(struct ice_pf *pf,
1796 			       enum ice_dpll_pin_type pin_type)
1797 {
1798 	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
1799 	int num_pins, i, ret = -EINVAL;
1800 	struct ice_hw *hw = &pf->hw;
1801 	struct ice_dpll_pin *pins;
1802 	unsigned long caps;
1803 	u8 freq_supp_num;
1804 	bool input;
1805 
1806 	switch (pin_type) {
1807 	case ICE_DPLL_PIN_TYPE_INPUT:
1808 		pins = pf->dplls.inputs;
1809 		num_pins = pf->dplls.num_inputs;
1810 		input = true;
1811 		break;
1812 	case ICE_DPLL_PIN_TYPE_OUTPUT:
1813 		pins = pf->dplls.outputs;
1814 		num_pins = pf->dplls.num_outputs;
1815 		input = false;
1816 		break;
1817 	default:
1818 		return -EINVAL;
1819 	}
1820 
1821 	for (i = 0; i < num_pins; i++) {
1822 		caps = 0;
1823 		pins[i].idx = i;
1824 		pins[i].prop.board_label = ice_cgu_get_pin_name(hw, i, input);
1825 		pins[i].prop.type = ice_cgu_get_pin_type(hw, i, input);
1826 		if (input) {
1827 			ret = ice_aq_get_cgu_ref_prio(hw, de->dpll_idx, i,
1828 						      &de->input_prio[i]);
1829 			if (ret)
1830 				return ret;
1831 			ret = ice_aq_get_cgu_ref_prio(hw, dp->dpll_idx, i,
1832 						      &dp->input_prio[i]);
1833 			if (ret)
1834 				return ret;
1835 			caps |= (DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
1836 				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE);
1837 			pins[i].prop.phase_range.min =
1838 				pf->dplls.input_phase_adj_max;
1839 			pins[i].prop.phase_range.max =
1840 				-pf->dplls.input_phase_adj_max;
1841 		} else {
1842 			pins[i].prop.phase_range.min =
1843 				pf->dplls.output_phase_adj_max;
1844 			pins[i].prop.phase_range.max =
1845 				-pf->dplls.output_phase_adj_max;
1846 			ret = ice_cgu_get_output_pin_state_caps(hw, i, &caps);
1847 			if (ret)
1848 				return ret;
1849 		}
1850 		pins[i].prop.capabilities = caps;
1851 		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
1852 		if (ret)
1853 			return ret;
1854 		pins[i].prop.freq_supported =
1855 			ice_cgu_get_pin_freq_supp(hw, i, input, &freq_supp_num);
1856 		pins[i].prop.freq_supported_num = freq_supp_num;
1857 		pins[i].pf = pf;
1858 	}
1859 
1860 	return ret;
1861 }
1862 
1863 /**
1864  * ice_dpll_init_info_rclk_pin - initializes rclk pin information
1865  * @pf: board private structure
1866  *
1867  * Init information for rclk pin, cache them in pf->dplls.rclk.
1868  *
1869  * Return:
1870  * * 0 - success
1871  * * negative - init failure reason
1872  */
1873 static int ice_dpll_init_info_rclk_pin(struct ice_pf *pf)
1874 {
1875 	struct ice_dpll_pin *pin = &pf->dplls.rclk;
1876 
1877 	pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
1878 	pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
1879 	pin->pf = pf;
1880 
1881 	return ice_dpll_pin_state_update(pf, pin,
1882 					 ICE_DPLL_PIN_TYPE_RCLK_INPUT, NULL);
1883 }
1884 
1885 /**
1886  * ice_dpll_init_pins_info - init pins info wrapper
1887  * @pf: board private structure
1888  * @pin_type: type of pins being initialized
1889  *
1890  * Wraps functions for pin initialization.
1891  *
1892  * Return:
1893  * * 0 - success
1894  * * negative - init failure reason
1895  */
1896 static int
1897 ice_dpll_init_pins_info(struct ice_pf *pf, enum ice_dpll_pin_type pin_type)
1898 {
1899 	switch (pin_type) {
1900 	case ICE_DPLL_PIN_TYPE_INPUT:
1901 	case ICE_DPLL_PIN_TYPE_OUTPUT:
1902 		return ice_dpll_init_info_direct_pins(pf, pin_type);
1903 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
1904 		return ice_dpll_init_info_rclk_pin(pf);
1905 	default:
1906 		return -EINVAL;
1907 	}
1908 }
1909 
1910 /**
1911  * ice_dpll_deinit_info - release memory allocated for pins info
1912  * @pf: board private structure
1913  *
1914  * Release memory allocated for pins by ice_dpll_init_info function.
1915  */
1916 static void ice_dpll_deinit_info(struct ice_pf *pf)
1917 {
1918 	kfree(pf->dplls.inputs);
1919 	kfree(pf->dplls.outputs);
1920 	kfree(pf->dplls.eec.input_prio);
1921 	kfree(pf->dplls.pps.input_prio);
1922 }
1923 
1924 /**
1925  * ice_dpll_init_info - prepare pf's dpll information structure
1926  * @pf: board private structure
1927  * @cgu: if cgu is present and controlled by this NIC
1928  *
1929  * Acquire (from HW) and set basic dpll information (on pf->dplls struct).
1930  *
1931  * Return:
1932  * * 0 - success
1933  * * negative - init failure reason
1934  */
1935 static int ice_dpll_init_info(struct ice_pf *pf, bool cgu)
1936 {
1937 	struct ice_aqc_get_cgu_abilities abilities;
1938 	struct ice_dpll *de = &pf->dplls.eec;
1939 	struct ice_dpll *dp = &pf->dplls.pps;
1940 	struct ice_dplls *d = &pf->dplls;
1941 	struct ice_hw *hw = &pf->hw;
1942 	int ret, alloc_size, i;
1943 
1944 	d->clock_id = ice_generate_clock_id(pf);
1945 	ret = ice_aq_get_cgu_abilities(hw, &abilities);
1946 	if (ret) {
1947 		dev_err(ice_pf_to_dev(pf),
1948 			"err:%d %s failed to read cgu abilities\n",
1949 			ret, ice_aq_str(hw->adminq.sq_last_status));
1950 		return ret;
1951 	}
1952 
1953 	de->dpll_idx = abilities.eec_dpll_idx;
1954 	dp->dpll_idx = abilities.pps_dpll_idx;
1955 	d->num_inputs = abilities.num_inputs;
1956 	d->num_outputs = abilities.num_outputs;
1957 	d->input_phase_adj_max = le32_to_cpu(abilities.max_in_phase_adj);
1958 	d->output_phase_adj_max = le32_to_cpu(abilities.max_out_phase_adj);
1959 
1960 	alloc_size = sizeof(*d->inputs) * d->num_inputs;
1961 	d->inputs = kzalloc(alloc_size, GFP_KERNEL);
1962 	if (!d->inputs)
1963 		return -ENOMEM;
1964 
1965 	alloc_size = sizeof(*de->input_prio) * d->num_inputs;
1966 	de->input_prio = kzalloc(alloc_size, GFP_KERNEL);
1967 	if (!de->input_prio)
1968 		return -ENOMEM;
1969 
1970 	dp->input_prio = kzalloc(alloc_size, GFP_KERNEL);
1971 	if (!dp->input_prio)
1972 		return -ENOMEM;
1973 
1974 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_INPUT);
1975 	if (ret)
1976 		goto deinit_info;
1977 
1978 	if (cgu) {
1979 		alloc_size = sizeof(*d->outputs) * d->num_outputs;
1980 		d->outputs = kzalloc(alloc_size, GFP_KERNEL);
1981 		if (!d->outputs) {
1982 			ret = -ENOMEM;
1983 			goto deinit_info;
1984 		}
1985 
1986 		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_OUTPUT);
1987 		if (ret)
1988 			goto deinit_info;
1989 	}
1990 
1991 	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
1992 					&pf->dplls.rclk.num_parents);
1993 	if (ret)
1994 		return ret;
1995 	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
1996 		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
1997 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
1998 	if (ret)
1999 		return ret;
2000 	de->mode = DPLL_MODE_AUTOMATIC;
2001 	dp->mode = DPLL_MODE_AUTOMATIC;
2002 
2003 	dev_dbg(ice_pf_to_dev(pf),
2004 		"%s - success, inputs:%u, outputs:%u rclk-parents:%u\n",
2005 		__func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
2006 
2007 	return 0;
2008 
2009 deinit_info:
2010 	dev_err(ice_pf_to_dev(pf),
2011 		"%s - fail: d->inputs:%p, de->input_prio:%p, dp->input_prio:%p, d->outputs:%p\n",
2012 		__func__, d->inputs, de->input_prio,
2013 		dp->input_prio, d->outputs);
2014 	ice_dpll_deinit_info(pf);
2015 	return ret;
2016 }
2017 
2018 /**
2019  * ice_dpll_deinit - Disable the driver/HW support for dpll subsystem
2020  * the dpll device.
2021  * @pf: board private structure
2022  *
2023  * Handles the cleanup work required after dpll initialization, freeing
2024  * resources and unregistering the dpll, pin and all resources used for
2025  * handling them.
2026  *
2027  * Context: Destroys pf->dplls.lock mutex. Call only if ICE_FLAG_DPLL was set.
2028  */
2029 void ice_dpll_deinit(struct ice_pf *pf)
2030 {
2031 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
2032 
2033 	clear_bit(ICE_FLAG_DPLL, pf->flags);
2034 	if (cgu)
2035 		ice_dpll_deinit_worker(pf);
2036 
2037 	ice_dpll_deinit_pins(pf, cgu);
2038 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
2039 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
2040 	ice_dpll_deinit_info(pf);
2041 	mutex_destroy(&pf->dplls.lock);
2042 }
2043 
2044 /**
2045  * ice_dpll_init - initialize support for dpll subsystem
2046  * @pf: board private structure
2047  *
2048  * Set up the device dplls, register them and pins connected within Linux dpll
2049  * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
2050  * configuration requests.
2051  *
2052  * Context: Initializes pf->dplls.lock mutex.
2053  */
2054 void ice_dpll_init(struct ice_pf *pf)
2055 {
2056 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
2057 	struct ice_dplls *d = &pf->dplls;
2058 	int err = 0;
2059 
2060 	err = ice_dpll_init_info(pf, cgu);
2061 	if (err)
2062 		goto err_exit;
2063 	err = ice_dpll_init_dpll(pf, &pf->dplls.eec, cgu, DPLL_TYPE_EEC);
2064 	if (err)
2065 		goto deinit_info;
2066 	err = ice_dpll_init_dpll(pf, &pf->dplls.pps, cgu, DPLL_TYPE_PPS);
2067 	if (err)
2068 		goto deinit_eec;
2069 	err = ice_dpll_init_pins(pf, cgu);
2070 	if (err)
2071 		goto deinit_pps;
2072 	mutex_init(&d->lock);
2073 	if (cgu) {
2074 		err = ice_dpll_init_worker(pf);
2075 		if (err)
2076 			goto deinit_pins;
2077 	}
2078 	set_bit(ICE_FLAG_DPLL, pf->flags);
2079 
2080 	return;
2081 
2082 deinit_pins:
2083 	ice_dpll_deinit_pins(pf, cgu);
2084 deinit_pps:
2085 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
2086 deinit_eec:
2087 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
2088 deinit_info:
2089 	ice_dpll_deinit_info(pf);
2090 err_exit:
2091 	mutex_destroy(&d->lock);
2092 	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
2093 }
2094