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