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