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