xref: /linux/drivers/dpll/zl3073x/core.c (revision 90e63d5354951d37fa2b3b91e6f17b95d2bf9bee)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/array_size.h>
4 #include <linux/bitfield.h>
5 #include <linux/bits.h>
6 #include <linux/dev_printk.h>
7 #include <linux/device.h>
8 #include <linux/export.h>
9 #include <linux/math64.h>
10 #include <linux/module.h>
11 #include <linux/netlink.h>
12 #include <linux/regmap.h>
13 #include <linux/sprintf.h>
14 #include <linux/string_choices.h>
15 #include <linux/unaligned.h>
16 #include <net/devlink.h>
17 
18 #include "core.h"
19 #include "devlink.h"
20 #include "dpll.h"
21 #include "regs.h"
22 
23 #define ZL_CHIP_INFO(_id, _nchannels, _flags)				\
24 	{ .id = (_id), .num_channels = (_nchannels), .flags = (_flags) }
25 
26 static const struct zl3073x_chip_info zl3073x_chip_ids[] = {
27 	ZL_CHIP_INFO(0x0E30, 2, ZL3073X_FLAG_REF_PHASE_COMP_32),
28 	ZL_CHIP_INFO(0x0E93, 1, ZL3073X_FLAG_REF_PHASE_COMP_32),
29 	ZL_CHIP_INFO(0x0E94, 2, ZL3073X_FLAG_REF_PHASE_COMP_32),
30 	ZL_CHIP_INFO(0x0E95, 3, ZL3073X_FLAG_REF_PHASE_COMP_32),
31 	ZL_CHIP_INFO(0x0E96, 4, ZL3073X_FLAG_REF_PHASE_COMP_32),
32 	ZL_CHIP_INFO(0x0E97, 5, ZL3073X_FLAG_REF_PHASE_COMP_32),
33 	ZL_CHIP_INFO(0x1E93, 1, ZL3073X_FLAG_DIE_TEMP),
34 	ZL_CHIP_INFO(0x1E94, 2, ZL3073X_FLAG_DIE_TEMP),
35 	ZL_CHIP_INFO(0x1E95, 3, ZL3073X_FLAG_DIE_TEMP),
36 	ZL_CHIP_INFO(0x1E96, 4, ZL3073X_FLAG_DIE_TEMP),
37 	ZL_CHIP_INFO(0x1E97, 5, ZL3073X_FLAG_DIE_TEMP),
38 	ZL_CHIP_INFO(0x1F60, 2, ZL3073X_FLAG_REF_PHASE_COMP_32),
39 	ZL_CHIP_INFO(0x2E93, 1, ZL3073X_FLAG_DIE_TEMP),
40 	ZL_CHIP_INFO(0x2E94, 2, ZL3073X_FLAG_DIE_TEMP),
41 	ZL_CHIP_INFO(0x2E95, 3, ZL3073X_FLAG_DIE_TEMP),
42 	ZL_CHIP_INFO(0x2E96, 4, ZL3073X_FLAG_DIE_TEMP),
43 	ZL_CHIP_INFO(0x2E97, 5, ZL3073X_FLAG_DIE_TEMP),
44 	ZL_CHIP_INFO(0x3FC4, 2, ZL3073X_FLAG_DIE_TEMP),
45 };
46 
47 #define ZL_RANGE_OFFSET		0x80
48 #define ZL_PAGE_SIZE		0x80
49 #define ZL_NUM_PAGES		256
50 #define ZL_PAGE_SEL		0x7F
51 #define ZL_PAGE_SEL_MASK	GENMASK(7, 0)
52 #define ZL_NUM_REGS		(ZL_NUM_PAGES * ZL_PAGE_SIZE)
53 
54 /* Regmap range configuration */
55 static const struct regmap_range_cfg zl3073x_regmap_range = {
56 	.range_min	= ZL_RANGE_OFFSET,
57 	.range_max	= ZL_RANGE_OFFSET + ZL_NUM_REGS - 1,
58 	.selector_reg	= ZL_PAGE_SEL,
59 	.selector_mask	= ZL_PAGE_SEL_MASK,
60 	.selector_shift	= 0,
61 	.window_start	= 0,
62 	.window_len	= ZL_PAGE_SIZE,
63 };
64 
65 static bool
66 zl3073x_is_volatile_reg(struct device *dev __maybe_unused, unsigned int reg)
67 {
68 	/* Only page selector is non-volatile */
69 	return reg != ZL_PAGE_SEL;
70 }
71 
72 const struct regmap_config zl3073x_regmap_config = {
73 	.reg_bits	= 8,
74 	.val_bits	= 8,
75 	.max_register	= ZL_RANGE_OFFSET + ZL_NUM_REGS - 1,
76 	.ranges		= &zl3073x_regmap_range,
77 	.num_ranges	= 1,
78 	.cache_type	= REGCACHE_MAPLE,
79 	.volatile_reg	= zl3073x_is_volatile_reg,
80 };
81 EXPORT_SYMBOL_NS_GPL(zl3073x_regmap_config, "ZL3073X");
82 
83 static bool
84 zl3073x_check_reg(struct zl3073x_dev *zldev, unsigned int reg, size_t size)
85 {
86 	/* Check that multiop lock is held when accessing registers
87 	 * from page 10 and above except the page 255 that does not
88 	 * need this protection.
89 	 */
90 	if (ZL_REG_PAGE(reg) >= 10 && ZL_REG_PAGE(reg) < 255)
91 		lockdep_assert_held(&zldev->multiop_lock);
92 
93 	/* Check the index is in valid range for indexed register */
94 	if (ZL_REG_OFFSET(reg) > ZL_REG_MAX_OFFSET(reg)) {
95 		dev_err(zldev->dev, "Index out of range for reg 0x%04lx\n",
96 			ZL_REG_ADDR(reg));
97 		return false;
98 	}
99 	/* Check the requested size corresponds to register size */
100 	if (ZL_REG_SIZE(reg) != size) {
101 		dev_err(zldev->dev, "Invalid size %zu for reg 0x%04lx\n",
102 			size, ZL_REG_ADDR(reg));
103 		return false;
104 	}
105 
106 	return true;
107 }
108 
109 static int
110 zl3073x_read_reg(struct zl3073x_dev *zldev, unsigned int reg, void *val,
111 		 size_t size)
112 {
113 	int rc;
114 
115 	if (!zl3073x_check_reg(zldev, reg, size))
116 		return -EINVAL;
117 
118 	/* Map the register address to virtual range */
119 	reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET;
120 
121 	rc = regmap_bulk_read(zldev->regmap, reg, val, size);
122 	if (rc) {
123 		dev_err(zldev->dev, "Failed to read reg 0x%04x: %pe\n", reg,
124 			ERR_PTR(rc));
125 		return rc;
126 	}
127 
128 	return 0;
129 }
130 
131 static int
132 zl3073x_write_reg(struct zl3073x_dev *zldev, unsigned int reg, const void *val,
133 		  size_t size)
134 {
135 	int rc;
136 
137 	if (!zl3073x_check_reg(zldev, reg, size))
138 		return -EINVAL;
139 
140 	/* Map the register address to virtual range */
141 	reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET;
142 
143 	rc = regmap_bulk_write(zldev->regmap, reg, val, size);
144 	if (rc) {
145 		dev_err(zldev->dev, "Failed to write reg 0x%04x: %pe\n", reg,
146 			ERR_PTR(rc));
147 		return rc;
148 	}
149 
150 	return 0;
151 }
152 
153 /**
154  * zl3073x_read_u8 - read value from 8bit register
155  * @zldev: zl3073x device pointer
156  * @reg: register to write to
157  * @val: value to write
158  *
159  * Reads value from given 8bit register.
160  *
161  * Returns: 0 on success, <0 on error
162  */
163 int zl3073x_read_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 *val)
164 {
165 	return zl3073x_read_reg(zldev, reg, val, sizeof(*val));
166 }
167 
168 /**
169  * zl3073x_write_u8 - write value to 16bit register
170  * @zldev: zl3073x device pointer
171  * @reg: register to write to
172  * @val: value to write
173  *
174  * Writes value into given 8bit register.
175  *
176  * Returns: 0 on success, <0 on error
177  */
178 int zl3073x_write_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 val)
179 {
180 	return zl3073x_write_reg(zldev, reg, &val, sizeof(val));
181 }
182 
183 /**
184  * zl3073x_read_u16 - read value from 16bit register
185  * @zldev: zl3073x device pointer
186  * @reg: register to write to
187  * @val: value to write
188  *
189  * Reads value from given 16bit register.
190  *
191  * Returns: 0 on success, <0 on error
192  */
193 int zl3073x_read_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 *val)
194 {
195 	int rc;
196 
197 	rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val));
198 	if (!rc)
199 		be16_to_cpus(val);
200 
201 	return rc;
202 }
203 
204 /**
205  * zl3073x_write_u16 - write value to 16bit register
206  * @zldev: zl3073x device pointer
207  * @reg: register to write to
208  * @val: value to write
209  *
210  * Writes value into given 16bit register.
211  *
212  * Returns: 0 on success, <0 on error
213  */
214 int zl3073x_write_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 val)
215 {
216 	cpu_to_be16s(&val);
217 
218 	return zl3073x_write_reg(zldev, reg, &val, sizeof(val));
219 }
220 
221 /**
222  * zl3073x_read_u32 - read value from 32bit register
223  * @zldev: zl3073x device pointer
224  * @reg: register to write to
225  * @val: value to write
226  *
227  * Reads value from given 32bit register.
228  *
229  * Returns: 0 on success, <0 on error
230  */
231 int zl3073x_read_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 *val)
232 {
233 	int rc;
234 
235 	rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val));
236 	if (!rc)
237 		be32_to_cpus(val);
238 
239 	return rc;
240 }
241 
242 /**
243  * zl3073x_write_u32 - write value to 32bit register
244  * @zldev: zl3073x device pointer
245  * @reg: register to write to
246  * @val: value to write
247  *
248  * Writes value into given 32bit register.
249  *
250  * Returns: 0 on success, <0 on error
251  */
252 int zl3073x_write_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 val)
253 {
254 	cpu_to_be32s(&val);
255 
256 	return zl3073x_write_reg(zldev, reg, &val, sizeof(val));
257 }
258 
259 /**
260  * zl3073x_read_u48 - read value from 48bit register
261  * @zldev: zl3073x device pointer
262  * @reg: register to write to
263  * @val: value to write
264  *
265  * Reads value from given 48bit register.
266  *
267  * Returns: 0 on success, <0 on error
268  */
269 int zl3073x_read_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 *val)
270 {
271 	u8 buf[6];
272 	int rc;
273 
274 	rc = zl3073x_read_reg(zldev, reg, buf, sizeof(buf));
275 	if (!rc)
276 		*val = get_unaligned_be48(buf);
277 
278 	return rc;
279 }
280 
281 /**
282  * zl3073x_write_u48 - write value to 48bit register
283  * @zldev: zl3073x device pointer
284  * @reg: register to write to
285  * @val: value to write
286  *
287  * Writes value into given 48bit register.
288  * The value must be from the interval -S48_MIN to U48_MAX.
289  *
290  * Returns: 0 on success, <0 on error
291  */
292 int zl3073x_write_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 val)
293 {
294 	u8 buf[6];
295 
296 	/* Check the value belongs to <S48_MIN, U48_MAX>
297 	 * Any value >= S48_MIN has bits 47..63 set.
298 	 */
299 	if (val > GENMASK_ULL(47, 0) && val < GENMASK_ULL(63, 47)) {
300 		dev_err(zldev->dev, "Value 0x%0llx out of range\n", val);
301 		return -EINVAL;
302 	}
303 
304 	put_unaligned_be48(val, buf);
305 
306 	return zl3073x_write_reg(zldev, reg, buf, sizeof(buf));
307 }
308 
309 /**
310  * zl3073x_poll_zero_u8 - wait for register to be cleared by device
311  * @zldev: zl3073x device pointer
312  * @reg: register to poll (has to be 8bit register)
313  * @mask: bit mask for polling
314  *
315  * Waits for bits specified by @mask in register @reg value to be cleared
316  * by the device.
317  *
318  * Returns: 0 on success, <0 on error
319  */
320 int zl3073x_poll_zero_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 mask)
321 {
322 	/* Register polling sleep & timeout */
323 #define ZL_POLL_SLEEP_US   10
324 #define ZL_POLL_TIMEOUT_US 2000000
325 	unsigned int val;
326 
327 	/* Check the register is 8bit */
328 	if (ZL_REG_SIZE(reg) != 1) {
329 		dev_err(zldev->dev, "Invalid reg 0x%04lx size for polling\n",
330 			ZL_REG_ADDR(reg));
331 		return -EINVAL;
332 	}
333 
334 	/* Map the register address to virtual range */
335 	reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET;
336 
337 	return regmap_read_poll_timeout(zldev->regmap, reg, val, !(val & mask),
338 					ZL_POLL_SLEEP_US, ZL_POLL_TIMEOUT_US);
339 }
340 
341 int zl3073x_mb_op(struct zl3073x_dev *zldev, unsigned int op_reg, u8 op_val,
342 		  unsigned int mask_reg, u16 mask_val)
343 {
344 	int rc;
345 
346 	/* Set mask for the operation */
347 	rc = zl3073x_write_u16(zldev, mask_reg, mask_val);
348 	if (rc)
349 		return rc;
350 
351 	/* Trigger the operation */
352 	rc = zl3073x_write_u8(zldev, op_reg, op_val);
353 	if (rc)
354 		return rc;
355 
356 	/* Wait for the operation to actually finish */
357 	return zl3073x_poll_zero_u8(zldev, op_reg, op_val);
358 }
359 
360 /**
361  * zl3073x_do_hwreg_op - Perform HW register read/write operation
362  * @zldev: zl3073x device pointer
363  * @op: operation to perform
364  *
365  * Performs requested operation and waits for its completion.
366  *
367  * Return: 0 on success, <0 on error
368  */
369 static int
370 zl3073x_do_hwreg_op(struct zl3073x_dev *zldev, u8 op)
371 {
372 	int rc;
373 
374 	/* Set requested operation and set pending bit */
375 	rc = zl3073x_write_u8(zldev, ZL_REG_HWREG_OP, op | ZL_HWREG_OP_PENDING);
376 	if (rc)
377 		return rc;
378 
379 	/* Poll for completion - pending bit cleared */
380 	return zl3073x_poll_zero_u8(zldev, ZL_REG_HWREG_OP,
381 				    ZL_HWREG_OP_PENDING);
382 }
383 
384 /**
385  * zl3073x_read_hwreg - Read HW register
386  * @zldev: zl3073x device pointer
387  * @addr: HW register address
388  * @value: Value of the HW register
389  *
390  * Reads HW register value and stores it into @value.
391  *
392  * Return: 0 on success, <0 on error
393  */
394 int zl3073x_read_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 *value)
395 {
396 	int rc;
397 
398 	/* Set address to read data from */
399 	rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr);
400 	if (rc)
401 		return rc;
402 
403 	/* Perform the read operation */
404 	rc = zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_READ);
405 	if (rc)
406 		return rc;
407 
408 	/* Read the received data */
409 	return zl3073x_read_u32(zldev, ZL_REG_HWREG_READ_DATA, value);
410 }
411 
412 /**
413  * zl3073x_write_hwreg - Write value to HW register
414  * @zldev: zl3073x device pointer
415  * @addr: HW registers address
416  * @value: Value to be written to HW register
417  *
418  * Stores the requested value into HW register.
419  *
420  * Return: 0 on success, <0 on error
421  */
422 int zl3073x_write_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value)
423 {
424 	int rc;
425 
426 	/* Set address to write data to */
427 	rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr);
428 	if (rc)
429 		return rc;
430 
431 	/* Set data to be written */
432 	rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_WRITE_DATA, value);
433 	if (rc)
434 		return rc;
435 
436 	/* Perform the write operation */
437 	return zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_WRITE);
438 }
439 
440 /**
441  * zl3073x_update_hwreg - Update certain bits in HW register
442  * @zldev: zl3073x device pointer
443  * @addr: HW register address
444  * @value: Value to be written into HW register
445  * @mask: Bitmask indicating bits to be updated
446  *
447  * Reads given HW register, updates requested bits specified by value and
448  * mask and writes result back to HW register.
449  *
450  * Return: 0 on success, <0 on error
451  */
452 int zl3073x_update_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value,
453 			 u32 mask)
454 {
455 	u32 tmp;
456 	int rc;
457 
458 	rc = zl3073x_read_hwreg(zldev, addr, &tmp);
459 	if (rc)
460 		return rc;
461 
462 	tmp &= ~mask;
463 	tmp |= value & mask;
464 
465 	return zl3073x_write_hwreg(zldev, addr, tmp);
466 }
467 
468 /**
469  * zl3073x_write_hwreg_seq - Write HW registers sequence
470  * @zldev: pointer to device structure
471  * @seq: pointer to first sequence item
472  * @num_items: number of items in sequence
473  *
474  * Writes given HW registers sequence.
475  *
476  * Return: 0 on success, <0 on error
477  */
478 int zl3073x_write_hwreg_seq(struct zl3073x_dev *zldev,
479 			    const struct zl3073x_hwreg_seq_item *seq,
480 			    size_t num_items)
481 {
482 	int i, rc = 0;
483 
484 	for (i = 0; i < num_items; i++) {
485 		dev_dbg(zldev->dev, "Write 0x%0x [0x%0x] to 0x%0x",
486 			seq[i].value, seq[i].mask, seq[i].addr);
487 
488 		if (seq[i].mask == U32_MAX)
489 			/* Write value directly */
490 			rc = zl3073x_write_hwreg(zldev, seq[i].addr,
491 						 seq[i].value);
492 		else
493 			/* Update only bits specified by the mask */
494 			rc = zl3073x_update_hwreg(zldev, seq[i].addr,
495 						  seq[i].value, seq[i].mask);
496 		if (rc)
497 			return rc;
498 
499 		if (seq->wait)
500 			msleep(seq->wait);
501 	}
502 
503 	return rc;
504 }
505 
506 static int
507 zl3073x_dev_state_fetch(struct zl3073x_dev *zldev)
508 {
509 	int rc;
510 	u8 i;
511 
512 	for (i = 0; i < ZL3073X_NUM_REFS; i++) {
513 		rc = zl3073x_ref_state_fetch(zldev, i);
514 		if (rc) {
515 			dev_err(zldev->dev,
516 				"Failed to fetch input state: %pe\n",
517 				ERR_PTR(rc));
518 			return rc;
519 		}
520 	}
521 
522 	for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) {
523 		rc = zl3073x_synth_state_fetch(zldev, i);
524 		if (rc) {
525 			dev_err(zldev->dev,
526 				"Failed to fetch synth state: %pe\n",
527 				ERR_PTR(rc));
528 			return rc;
529 		}
530 	}
531 
532 	for (i = 0; i < ZL3073X_NUM_OUTS; i++) {
533 		rc = zl3073x_out_state_fetch(zldev, i);
534 		if (rc) {
535 			dev_err(zldev->dev,
536 				"Failed to fetch output state: %pe\n",
537 				ERR_PTR(rc));
538 			return rc;
539 		}
540 	}
541 
542 	for (i = 0; i < zldev->info->num_channels; i++) {
543 		rc = zl3073x_chan_state_fetch(zldev, i);
544 		if (rc) {
545 			dev_err(zldev->dev,
546 				"Failed to fetch channel state: %pe\n",
547 				ERR_PTR(rc));
548 			return rc;
549 		}
550 	}
551 
552 	return rc;
553 }
554 
555 static void
556 zl3073x_dev_ref_states_update(struct zl3073x_dev *zldev)
557 {
558 	int i, rc;
559 
560 	for (i = 0; i < ZL3073X_NUM_REFS; i++) {
561 		rc = zl3073x_ref_state_update(zldev, i);
562 		if (rc)
563 			dev_warn(zldev->dev,
564 				 "Failed to get REF%u status: %pe\n", i,
565 				 ERR_PTR(rc));
566 	}
567 }
568 
569 static void
570 zl3073x_dev_chan_states_update(struct zl3073x_dev *zldev)
571 {
572 	int i, rc;
573 
574 	for (i = 0; i < zldev->info->num_channels; i++) {
575 		rc = zl3073x_chan_state_update(zldev, i);
576 		if (rc)
577 			dev_warn(zldev->dev,
578 				 "Failed to get DPLL%u state: %pe\n", i,
579 				 ERR_PTR(rc));
580 	}
581 }
582 
583 /**
584  * zl3073x_ref_phase_offsets_update - update reference phase offsets
585  * @zldev: pointer to zl3073x_dev structure
586  * @channel: DPLL channel number or -1
587  *
588  * The function asks device to update phase offsets latch registers with
589  * the latest measured values. There are 2 sets of latch registers:
590  *
591  * 1) Up to 5 DPLL-to-connected-ref registers that contain phase offset
592  *    values between particular DPLL channel and its *connected* input
593  *    reference.
594  *
595  * 2) 10 selected-DPLL-to-all-ref registers that contain phase offset values
596  *    between selected DPLL channel and all input references.
597  *
598  * If the caller is interested in 2) then it has to pass DPLL channel number
599  * in @channel parameter. If it is interested only in 1) then it should pass
600  * @channel parameter with value of -1.
601  *
602  * Return: 0 on success, <0 on error
603  */
604 int zl3073x_ref_phase_offsets_update(struct zl3073x_dev *zldev, int channel)
605 {
606 	int rc;
607 
608 	/* Per datasheet we have to wait for 'dpll_ref_phase_err_rqst_rd'
609 	 * to be zero to ensure that the measured data are coherent.
610 	 */
611 	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
612 				  ZL_REF_PHASE_ERR_READ_RQST_RD);
613 	if (rc)
614 		return rc;
615 
616 	/* Select DPLL channel if it is specified */
617 	if (channel != -1) {
618 		rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_IDX, channel);
619 		if (rc)
620 			return rc;
621 	}
622 
623 	/* Request to update phase offsets measurement values */
624 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
625 			      ZL_REF_PHASE_ERR_READ_RQST_RD);
626 	if (rc)
627 		return rc;
628 
629 	/* Wait for finish */
630 	return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
631 				    ZL_REF_PHASE_ERR_READ_RQST_RD);
632 }
633 
634 /**
635  * zl3073x_ref_freq_meas_latch - latch reference frequency measurements
636  * @zldev: pointer to zl3073x_dev structure
637  * @type: measurement type (ZL_REF_FREQ_MEAS_CTRL_*)
638  *
639  * The function waits for the previous measurement to finish, selects all
640  * references and requests a new measurement of the given type.
641  *
642  * Return: 0 on success, <0 on error
643  */
644 static int
645 zl3073x_ref_freq_meas_latch(struct zl3073x_dev *zldev, u8 type)
646 {
647 	int rc;
648 
649 	/* Wait for previous measurement to finish */
650 	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
651 				  ZL_REF_FREQ_MEAS_CTRL);
652 	if (rc)
653 		return rc;
654 
655 	/* Select all references for measurement */
656 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_3_0,
657 			      GENMASK(7, 0)); /* REF0P..REF3N */
658 	if (rc)
659 		return rc;
660 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_4,
661 			      GENMASK(1, 0)); /* REF4P..REF4N */
662 	if (rc)
663 		return rc;
664 
665 	/* Request measurement */
666 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, type);
667 	if (rc)
668 		return rc;
669 
670 	/* Wait for finish */
671 	return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
672 				    ZL_REF_FREQ_MEAS_CTRL);
673 }
674 
675 /**
676  * zl3073x_ref_freq_meas_update - update measured input reference frequencies
677  * @zldev: pointer to zl3073x_dev structure
678  *
679  * The function asks device to latch measured input reference frequencies
680  * and stores the results in the ref state.
681  *
682  * Return: 0 on success, <0 on error
683  */
684 static int
685 zl3073x_ref_freq_meas_update(struct zl3073x_dev *zldev)
686 {
687 	int i, rc;
688 
689 	rc = zl3073x_ref_freq_meas_latch(zldev, ZL_REF_FREQ_MEAS_CTRL_REF_FREQ);
690 	if (rc)
691 		return rc;
692 
693 	/* Read measured frequencies in Hz (unsigned 32-bit, LSB = 1 Hz) */
694 	for (i = 0; i < ZL3073X_NUM_REFS; i++) {
695 		u32 value;
696 
697 		rc = zl3073x_read_u32(zldev, ZL_REG_REF_FREQ(i), &value);
698 		if (rc)
699 			return rc;
700 
701 		zldev->ref[i].meas_freq = value;
702 	}
703 
704 	return 0;
705 }
706 
707 static void
708 zl3073x_dev_periodic_work(struct kthread_work *work)
709 {
710 	struct zl3073x_dev *zldev = container_of(work, struct zl3073x_dev,
711 						 work.work);
712 	struct zl3073x_dpll *zldpll;
713 	int rc;
714 
715 	/* Update input references' states */
716 	zl3073x_dev_ref_states_update(zldev);
717 
718 	/* Update DPLL channels' states */
719 	zl3073x_dev_chan_states_update(zldev);
720 
721 	/* Update DPLL-to-connected-ref phase offsets registers */
722 	rc = zl3073x_ref_phase_offsets_update(zldev, -1);
723 	if (rc)
724 		dev_warn(zldev->dev, "Failed to update phase offsets: %pe\n",
725 			 ERR_PTR(rc));
726 
727 	/* Update measured input reference frequencies if frequency
728 	 * monitoring is enabled.
729 	 */
730 	if (zldev->freq_monitor) {
731 		rc = zl3073x_ref_freq_meas_update(zldev);
732 		if (rc)
733 			dev_warn(zldev->dev,
734 				 "Failed to update measured frequencies: %pe\n",
735 				 ERR_PTR(rc));
736 	}
737 
738 	list_for_each_entry(zldpll, &zldev->dplls, list)
739 		zl3073x_dpll_changes_check(zldpll);
740 
741 	/* Run twice a second */
742 	kthread_queue_delayed_work(zldev->kworker, &zldev->work,
743 				   msecs_to_jiffies(500));
744 }
745 
746 int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor)
747 {
748 	u8 dpll_meas_ctrl, value;
749 	int rc;
750 
751 	/* Read DPLL phase measurement control register */
752 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl);
753 	if (rc)
754 		return rc;
755 
756 	/* Convert requested factor to register value */
757 	value = (factor + 1) & 0x0f;
758 
759 	/* Update phase measurement control register */
760 	FIELD_MODIFY(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, &dpll_meas_ctrl, value);
761 	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl);
762 	if (rc)
763 		return rc;
764 
765 	/* Save the new factor */
766 	zldev->phase_avg_factor = factor;
767 
768 	return 0;
769 }
770 
771 /**
772  * zl3073x_dev_phase_meas_setup - setup phase offset measurement
773  * @zldev: pointer to zl3073x_dev structure
774  *
775  * Enable phase offset measurement block, set measurement averaging factor
776  * and enable DPLL-to-its-ref phase measurement for all DPLLs.
777  *
778  * Returns: 0 on success, <0 on error
779  */
780 static int
781 zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev)
782 {
783 	struct zl3073x_dpll *zldpll;
784 	u8 dpll_meas_ctrl, mask = 0;
785 	int rc;
786 
787 	/* Setup phase measurement averaging factor */
788 	rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor);
789 	if (rc)
790 		return rc;
791 
792 	/* Read DPLL phase measurement control register */
793 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl);
794 	if (rc)
795 		return rc;
796 
797 	/* Enable DPLL measurement block */
798 	dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN;
799 
800 	/* Update phase measurement control register */
801 	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl);
802 	if (rc)
803 		return rc;
804 
805 	/* Enable DPLL-to-connected-ref measurement for each channel */
806 	list_for_each_entry(zldpll, &zldev->dplls, list)
807 		mask |= BIT(zldpll->id);
808 
809 	return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask);
810 }
811 
812 /**
813  * zl3073x_dev_start - Start normal operation
814  * @zldev: zl3073x device pointer
815  * @full: perform full initialization
816  *
817  * The function starts normal operation, which means registering all DPLLs and
818  * their pins, and starting monitoring. If full initialization is requested,
819  * the function additionally initializes the phase offset measurement block and
820  * fetches hardware-invariant parameters.
821  *
822  * Return: 0 on success, <0 on error
823  */
824 int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full)
825 {
826 	struct zl3073x_dpll *zldpll;
827 	u8 info;
828 	int rc;
829 
830 	rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info);
831 	if (rc) {
832 		dev_err(zldev->dev, "Failed to read device status info\n");
833 		return rc;
834 	}
835 
836 	if (!FIELD_GET(ZL_INFO_READY, info)) {
837 		/* The ready bit indicates that the firmware was successfully
838 		 * configured and is ready for normal operation. If it is
839 		 * cleared then the configuration stored in flash is wrong
840 		 * or missing. In this situation the driver will expose
841 		 * only devlink interface to give an opportunity to flash
842 		 * the correct config.
843 		 */
844 		dev_info(zldev->dev,
845 			 "FW not fully ready - missing or corrupted config\n");
846 
847 		return 0;
848 	}
849 
850 	if (full) {
851 		/* Fetch device state */
852 		rc = zl3073x_dev_state_fetch(zldev);
853 		if (rc)
854 			return rc;
855 
856 		/* Setup phase offset measurement block */
857 		rc = zl3073x_dev_phase_meas_setup(zldev);
858 		if (rc) {
859 			dev_err(zldev->dev,
860 				"Failed to setup phase measurement\n");
861 			return rc;
862 		}
863 	}
864 
865 	/* Register all DPLLs */
866 	list_for_each_entry(zldpll, &zldev->dplls, list) {
867 		rc = zl3073x_dpll_register(zldpll);
868 		if (rc) {
869 			dev_err_probe(zldev->dev, rc,
870 				      "Failed to register DPLL%u\n",
871 				      zldpll->id);
872 			return rc;
873 		}
874 	}
875 
876 	/* Perform initial firmware fine phase correction */
877 	rc = zl3073x_dpll_init_fine_phase_adjust(zldev);
878 	if (rc) {
879 		dev_err_probe(zldev->dev, rc,
880 			      "Failed to init fine phase correction\n");
881 		return rc;
882 	}
883 
884 	/* Start monitoring */
885 	kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0);
886 
887 	return 0;
888 }
889 
890 /**
891  * zl3073x_dev_stop - Stop normal operation
892  * @zldev: zl3073x device pointer
893  *
894  * The function stops the normal operation that mean deregistration of all
895  * DPLLs and their pins and stop monitoring.
896  *
897  * Return: 0 on success, <0 on error
898  */
899 void zl3073x_dev_stop(struct zl3073x_dev *zldev)
900 {
901 	struct zl3073x_dpll *zldpll;
902 
903 	/* Stop monitoring */
904 	kthread_cancel_delayed_work_sync(&zldev->work);
905 
906 	/* Unregister all DPLLs */
907 	list_for_each_entry(zldpll, &zldev->dplls, list) {
908 		if (zldpll->dpll_dev)
909 			zl3073x_dpll_unregister(zldpll);
910 	}
911 }
912 
913 static void zl3073x_dev_dpll_fini(void *ptr)
914 {
915 	struct zl3073x_dpll *zldpll, *next;
916 	struct zl3073x_dev *zldev = ptr;
917 
918 	/* Stop monitoring and unregister DPLLs */
919 	zl3073x_dev_stop(zldev);
920 
921 	/* Destroy monitoring thread */
922 	if (zldev->kworker) {
923 		kthread_destroy_worker(zldev->kworker);
924 		zldev->kworker = NULL;
925 	}
926 
927 	/* Free all DPLLs */
928 	list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) {
929 		list_del(&zldpll->list);
930 		zl3073x_dpll_free(zldpll);
931 	}
932 }
933 
934 static int
935 zl3073x_devm_dpll_init(struct zl3073x_dev *zldev)
936 {
937 	struct kthread_worker *kworker;
938 	struct zl3073x_dpll *zldpll;
939 	unsigned int i;
940 	int rc;
941 
942 	INIT_LIST_HEAD(&zldev->dplls);
943 
944 	/* Allocate all DPLLs */
945 	for (i = 0; i < zldev->info->num_channels; i++) {
946 		zldpll = zl3073x_dpll_alloc(zldev, i);
947 		if (IS_ERR(zldpll)) {
948 			dev_err_probe(zldev->dev, PTR_ERR(zldpll),
949 				      "Failed to alloc DPLL%u\n", i);
950 			rc = PTR_ERR(zldpll);
951 			goto error;
952 		}
953 
954 		list_add_tail(&zldpll->list, &zldev->dplls);
955 	}
956 
957 	/* Initialize monitoring thread */
958 	kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work);
959 	kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev));
960 	if (IS_ERR(kworker)) {
961 		rc = PTR_ERR(kworker);
962 		goto error;
963 	}
964 	zldev->kworker = kworker;
965 
966 	/* Start normal operation */
967 	rc = zl3073x_dev_start(zldev, true);
968 	if (rc) {
969 		dev_err_probe(zldev->dev, rc, "Failed to start device\n");
970 		goto error;
971 	}
972 
973 	/* Add devres action to release DPLL related resources */
974 	return devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev);
975 
976 error:
977 	zl3073x_dev_dpll_fini(zldev);
978 
979 	return rc;
980 }
981 
982 /**
983  * zl3073x_dev_probe - initialize zl3073x device
984  * @zldev: pointer to zl3073x device
985  *
986  * Common initialization of zl3073x device structure.
987  *
988  * Returns: 0 on success, <0 on error
989  */
990 int zl3073x_dev_probe(struct zl3073x_dev *zldev)
991 {
992 	u16 id, revision, fw_ver;
993 	unsigned int i;
994 	u32 cfg_ver;
995 	int rc;
996 
997 	/* Read chip ID */
998 	rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id);
999 	if (rc)
1000 		return rc;
1001 
1002 	/* Detect chip variant */
1003 	for (i = 0; i < ARRAY_SIZE(zl3073x_chip_ids); i++) {
1004 		if (zl3073x_chip_ids[i].id == id)
1005 			break;
1006 	}
1007 
1008 	if (i == ARRAY_SIZE(zl3073x_chip_ids))
1009 		return dev_err_probe(zldev->dev, -ENODEV,
1010 				     "Unknown chip ID: 0x%04x\n", id);
1011 
1012 	zldev->info = &zl3073x_chip_ids[i];
1013 
1014 	/* Read revision, firmware version and custom config version */
1015 	rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision);
1016 	if (rc)
1017 		return rc;
1018 	rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver);
1019 	if (rc)
1020 		return rc;
1021 	rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver);
1022 	if (rc)
1023 		return rc;
1024 
1025 	dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id,
1026 		revision, fw_ver);
1027 	dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n",
1028 		FIELD_GET(GENMASK(31, 24), cfg_ver),
1029 		FIELD_GET(GENMASK(23, 16), cfg_ver),
1030 		FIELD_GET(GENMASK(15, 8), cfg_ver),
1031 		FIELD_GET(GENMASK(7, 0), cfg_ver));
1032 
1033 	/* Generate random clock ID as the device has not such property that
1034 	 * could be used for this purpose. A user can later change this value
1035 	 * using devlink.
1036 	 */
1037 	zldev->clock_id = get_random_u64();
1038 
1039 	/* Default phase offset averaging factor */
1040 	zldev->phase_avg_factor = 2;
1041 
1042 	/* Initialize mutex for operations where multiple reads, writes
1043 	 * and/or polls are required to be done atomically.
1044 	 */
1045 	rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock);
1046 	if (rc)
1047 		return dev_err_probe(zldev->dev, rc,
1048 				     "Failed to initialize mutex\n");
1049 
1050 	/* Register DPLL channels */
1051 	rc = zl3073x_devm_dpll_init(zldev);
1052 	if (rc)
1053 		return rc;
1054 
1055 	/* Register the devlink instance and parameters */
1056 	rc = zl3073x_devlink_register(zldev);
1057 	if (rc)
1058 		return dev_err_probe(zldev->dev, rc,
1059 				     "Failed to register devlink instance\n");
1060 
1061 	return 0;
1062 }
1063 EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X");
1064 
1065 MODULE_AUTHOR("Ivan Vecera <ivecera@redhat.com>");
1066 MODULE_DESCRIPTION("Microchip ZL3073x core driver");
1067 MODULE_LICENSE("GPL");
1068