xref: /linux/drivers/dpll/zl3073x/core.c (revision 2ed4b46b4fc77749cb0f8dd31a01441b82c8dbaa)
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 	return rc;
543 }
544 
545 static void
546 zl3073x_dev_ref_status_update(struct zl3073x_dev *zldev)
547 {
548 	int i, rc;
549 
550 	for (i = 0; i < ZL3073X_NUM_REFS; i++) {
551 		rc = zl3073x_read_u8(zldev, ZL_REG_REF_MON_STATUS(i),
552 				     &zldev->ref[i].mon_status);
553 		if (rc)
554 			dev_warn(zldev->dev,
555 				 "Failed to get REF%u status: %pe\n", i,
556 				 ERR_PTR(rc));
557 	}
558 }
559 
560 /**
561  * zl3073x_ref_phase_offsets_update - update reference phase offsets
562  * @zldev: pointer to zl3073x_dev structure
563  * @channel: DPLL channel number or -1
564  *
565  * The function asks device to update phase offsets latch registers with
566  * the latest measured values. There are 2 sets of latch registers:
567  *
568  * 1) Up to 5 DPLL-to-connected-ref registers that contain phase offset
569  *    values between particular DPLL channel and its *connected* input
570  *    reference.
571  *
572  * 2) 10 selected-DPLL-to-all-ref registers that contain phase offset values
573  *    between selected DPLL channel and all input references.
574  *
575  * If the caller is interested in 2) then it has to pass DPLL channel number
576  * in @channel parameter. If it is interested only in 1) then it should pass
577  * @channel parameter with value of -1.
578  *
579  * Return: 0 on success, <0 on error
580  */
581 int zl3073x_ref_phase_offsets_update(struct zl3073x_dev *zldev, int channel)
582 {
583 	int rc;
584 
585 	/* Per datasheet we have to wait for 'dpll_ref_phase_err_rqst_rd'
586 	 * to be zero to ensure that the measured data are coherent.
587 	 */
588 	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
589 				  ZL_REF_PHASE_ERR_READ_RQST_RD);
590 	if (rc)
591 		return rc;
592 
593 	/* Select DPLL channel if it is specified */
594 	if (channel != -1) {
595 		rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_IDX, channel);
596 		if (rc)
597 			return rc;
598 	}
599 
600 	/* Request to update phase offsets measurement values */
601 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
602 			      ZL_REF_PHASE_ERR_READ_RQST_RD);
603 	if (rc)
604 		return rc;
605 
606 	/* Wait for finish */
607 	return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
608 				    ZL_REF_PHASE_ERR_READ_RQST_RD);
609 }
610 
611 /**
612  * zl3073x_ref_ffo_update - update reference fractional frequency offsets
613  * @zldev: pointer to zl3073x_dev structure
614  *
615  * The function asks device to update fractional frequency offsets latch
616  * registers the latest measured values, reads and stores them into
617  *
618  * Return: 0 on success, <0 on error
619  */
620 static int
621 zl3073x_ref_ffo_update(struct zl3073x_dev *zldev)
622 {
623 	int i, rc;
624 
625 	/* Per datasheet we have to wait for 'ref_freq_meas_ctrl' to be zero
626 	 * to ensure that the measured data are coherent.
627 	 */
628 	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
629 				  ZL_REF_FREQ_MEAS_CTRL);
630 	if (rc)
631 		return rc;
632 
633 	/* Select all references for measurement */
634 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_3_0,
635 			      GENMASK(7, 0)); /* REF0P..REF3N */
636 	if (rc)
637 		return rc;
638 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_4,
639 			      GENMASK(1, 0)); /* REF4P..REF4N */
640 	if (rc)
641 		return rc;
642 
643 	/* Request frequency offset measurement */
644 	rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
645 			      ZL_REF_FREQ_MEAS_CTRL_REF_FREQ_OFF);
646 	if (rc)
647 		return rc;
648 
649 	/* Wait for 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 	/* Read DPLL-to-REFx frequency offset measurements */
656 	for (i = 0; i < ZL3073X_NUM_REFS; i++) {
657 		s32 value;
658 
659 		/* Read value stored in units of 2^-32 signed */
660 		rc = zl3073x_read_u32(zldev, ZL_REG_REF_FREQ(i), &value);
661 		if (rc)
662 			return rc;
663 
664 		/* Convert to ppt
665 		 * ffo = (10^12 * value) / 2^32
666 		 * ffo = ( 5^12 * value) / 2^20
667 		 */
668 		zldev->ref[i].ffo = mul_s64_u64_shr(value, 244140625, 20);
669 	}
670 
671 	return 0;
672 }
673 
674 static void
675 zl3073x_dev_periodic_work(struct kthread_work *work)
676 {
677 	struct zl3073x_dev *zldev = container_of(work, struct zl3073x_dev,
678 						 work.work);
679 	struct zl3073x_dpll *zldpll;
680 	int rc;
681 
682 	/* Update input references status */
683 	zl3073x_dev_ref_status_update(zldev);
684 
685 	/* Update DPLL-to-connected-ref phase offsets registers */
686 	rc = zl3073x_ref_phase_offsets_update(zldev, -1);
687 	if (rc)
688 		dev_warn(zldev->dev, "Failed to update phase offsets: %pe\n",
689 			 ERR_PTR(rc));
690 
691 	/* Update references' fractional frequency offsets */
692 	rc = zl3073x_ref_ffo_update(zldev);
693 	if (rc)
694 		dev_warn(zldev->dev,
695 			 "Failed to update fractional frequency offsets: %pe\n",
696 			 ERR_PTR(rc));
697 
698 	list_for_each_entry(zldpll, &zldev->dplls, list)
699 		zl3073x_dpll_changes_check(zldpll);
700 
701 	/* Run twice a second */
702 	kthread_queue_delayed_work(zldev->kworker, &zldev->work,
703 				   msecs_to_jiffies(500));
704 }
705 
706 int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor)
707 {
708 	u8 dpll_meas_ctrl, value;
709 	int rc;
710 
711 	/* Read DPLL phase measurement control register */
712 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl);
713 	if (rc)
714 		return rc;
715 
716 	/* Convert requested factor to register value */
717 	value = (factor + 1) & 0x0f;
718 
719 	/* Update phase measurement control register */
720 	dpll_meas_ctrl &= ~ZL_DPLL_MEAS_CTRL_AVG_FACTOR;
721 	dpll_meas_ctrl |= FIELD_PREP(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, value);
722 	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl);
723 	if (rc)
724 		return rc;
725 
726 	/* Save the new factor */
727 	zldev->phase_avg_factor = factor;
728 
729 	return 0;
730 }
731 
732 /**
733  * zl3073x_dev_phase_meas_setup - setup phase offset measurement
734  * @zldev: pointer to zl3073x_dev structure
735  *
736  * Enable phase offset measurement block, set measurement averaging factor
737  * and enable DPLL-to-its-ref phase measurement for all DPLLs.
738  *
739  * Returns: 0 on success, <0 on error
740  */
741 static int
742 zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev)
743 {
744 	struct zl3073x_dpll *zldpll;
745 	u8 dpll_meas_ctrl, mask = 0;
746 	int rc;
747 
748 	/* Setup phase measurement averaging factor */
749 	rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor);
750 	if (rc)
751 		return rc;
752 
753 	/* Read DPLL phase measurement control register */
754 	rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl);
755 	if (rc)
756 		return rc;
757 
758 	/* Enable DPLL measurement block */
759 	dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN;
760 
761 	/* Update phase measurement control register */
762 	rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl);
763 	if (rc)
764 		return rc;
765 
766 	/* Enable DPLL-to-connected-ref measurement for each channel */
767 	list_for_each_entry(zldpll, &zldev->dplls, list)
768 		mask |= BIT(zldpll->id);
769 
770 	return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask);
771 }
772 
773 /**
774  * zl3073x_dev_start - Start normal operation
775  * @zldev: zl3073x device pointer
776  * @full: perform full initialization
777  *
778  * The function starts normal operation, which means registering all DPLLs and
779  * their pins, and starting monitoring. If full initialization is requested,
780  * the function additionally initializes the phase offset measurement block and
781  * fetches hardware-invariant parameters.
782  *
783  * Return: 0 on success, <0 on error
784  */
785 int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full)
786 {
787 	struct zl3073x_dpll *zldpll;
788 	u8 info;
789 	int rc;
790 
791 	rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info);
792 	if (rc) {
793 		dev_err(zldev->dev, "Failed to read device status info\n");
794 		return rc;
795 	}
796 
797 	if (!FIELD_GET(ZL_INFO_READY, info)) {
798 		/* The ready bit indicates that the firmware was successfully
799 		 * configured and is ready for normal operation. If it is
800 		 * cleared then the configuration stored in flash is wrong
801 		 * or missing. In this situation the driver will expose
802 		 * only devlink interface to give an opportunity to flash
803 		 * the correct config.
804 		 */
805 		dev_info(zldev->dev,
806 			 "FW not fully ready - missing or corrupted config\n");
807 
808 		return 0;
809 	}
810 
811 	if (full) {
812 		/* Fetch device state */
813 		rc = zl3073x_dev_state_fetch(zldev);
814 		if (rc)
815 			return rc;
816 
817 		/* Setup phase offset measurement block */
818 		rc = zl3073x_dev_phase_meas_setup(zldev);
819 		if (rc) {
820 			dev_err(zldev->dev,
821 				"Failed to setup phase measurement\n");
822 			return rc;
823 		}
824 	}
825 
826 	/* Register all DPLLs */
827 	list_for_each_entry(zldpll, &zldev->dplls, list) {
828 		rc = zl3073x_dpll_register(zldpll);
829 		if (rc) {
830 			dev_err_probe(zldev->dev, rc,
831 				      "Failed to register DPLL%u\n",
832 				      zldpll->id);
833 			return rc;
834 		}
835 	}
836 
837 	/* Perform initial firmware fine phase correction */
838 	rc = zl3073x_dpll_init_fine_phase_adjust(zldev);
839 	if (rc) {
840 		dev_err_probe(zldev->dev, rc,
841 			      "Failed to init fine phase correction\n");
842 		return rc;
843 	}
844 
845 	/* Start monitoring */
846 	kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0);
847 
848 	return 0;
849 }
850 
851 /**
852  * zl3073x_dev_stop - Stop normal operation
853  * @zldev: zl3073x device pointer
854  *
855  * The function stops the normal operation that mean deregistration of all
856  * DPLLs and their pins and stop monitoring.
857  *
858  * Return: 0 on success, <0 on error
859  */
860 void zl3073x_dev_stop(struct zl3073x_dev *zldev)
861 {
862 	struct zl3073x_dpll *zldpll;
863 
864 	/* Stop monitoring */
865 	kthread_cancel_delayed_work_sync(&zldev->work);
866 
867 	/* Unregister all DPLLs */
868 	list_for_each_entry(zldpll, &zldev->dplls, list) {
869 		if (zldpll->dpll_dev)
870 			zl3073x_dpll_unregister(zldpll);
871 	}
872 }
873 
874 static void zl3073x_dev_dpll_fini(void *ptr)
875 {
876 	struct zl3073x_dpll *zldpll, *next;
877 	struct zl3073x_dev *zldev = ptr;
878 
879 	/* Stop monitoring and unregister DPLLs */
880 	zl3073x_dev_stop(zldev);
881 
882 	/* Destroy monitoring thread */
883 	if (zldev->kworker) {
884 		kthread_destroy_worker(zldev->kworker);
885 		zldev->kworker = NULL;
886 	}
887 
888 	/* Free all DPLLs */
889 	list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) {
890 		list_del(&zldpll->list);
891 		zl3073x_dpll_free(zldpll);
892 	}
893 }
894 
895 static int
896 zl3073x_devm_dpll_init(struct zl3073x_dev *zldev)
897 {
898 	struct kthread_worker *kworker;
899 	struct zl3073x_dpll *zldpll;
900 	unsigned int i;
901 	int rc;
902 
903 	INIT_LIST_HEAD(&zldev->dplls);
904 
905 	/* Allocate all DPLLs */
906 	for (i = 0; i < zldev->info->num_channels; i++) {
907 		zldpll = zl3073x_dpll_alloc(zldev, i);
908 		if (IS_ERR(zldpll)) {
909 			dev_err_probe(zldev->dev, PTR_ERR(zldpll),
910 				      "Failed to alloc DPLL%u\n", i);
911 			rc = PTR_ERR(zldpll);
912 			goto error;
913 		}
914 
915 		list_add_tail(&zldpll->list, &zldev->dplls);
916 	}
917 
918 	/* Initialize monitoring thread */
919 	kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work);
920 	kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev));
921 	if (IS_ERR(kworker)) {
922 		rc = PTR_ERR(kworker);
923 		goto error;
924 	}
925 	zldev->kworker = kworker;
926 
927 	/* Start normal operation */
928 	rc = zl3073x_dev_start(zldev, true);
929 	if (rc) {
930 		dev_err_probe(zldev->dev, rc, "Failed to start device\n");
931 		goto error;
932 	}
933 
934 	/* Add devres action to release DPLL related resources */
935 	return devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev);
936 
937 error:
938 	zl3073x_dev_dpll_fini(zldev);
939 
940 	return rc;
941 }
942 
943 /**
944  * zl3073x_dev_probe - initialize zl3073x device
945  * @zldev: pointer to zl3073x device
946  *
947  * Common initialization of zl3073x device structure.
948  *
949  * Returns: 0 on success, <0 on error
950  */
951 int zl3073x_dev_probe(struct zl3073x_dev *zldev)
952 {
953 	u16 id, revision, fw_ver;
954 	unsigned int i;
955 	u32 cfg_ver;
956 	int rc;
957 
958 	/* Read chip ID */
959 	rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id);
960 	if (rc)
961 		return rc;
962 
963 	/* Detect chip variant */
964 	for (i = 0; i < ARRAY_SIZE(zl3073x_chip_ids); i++) {
965 		if (zl3073x_chip_ids[i].id == id)
966 			break;
967 	}
968 
969 	if (i == ARRAY_SIZE(zl3073x_chip_ids))
970 		return dev_err_probe(zldev->dev, -ENODEV,
971 				     "Unknown chip ID: 0x%04x\n", id);
972 
973 	zldev->info = &zl3073x_chip_ids[i];
974 
975 	/* Read revision, firmware version and custom config version */
976 	rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision);
977 	if (rc)
978 		return rc;
979 	rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver);
980 	if (rc)
981 		return rc;
982 	rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver);
983 	if (rc)
984 		return rc;
985 
986 	dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id,
987 		revision, fw_ver);
988 	dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n",
989 		FIELD_GET(GENMASK(31, 24), cfg_ver),
990 		FIELD_GET(GENMASK(23, 16), cfg_ver),
991 		FIELD_GET(GENMASK(15, 8), cfg_ver),
992 		FIELD_GET(GENMASK(7, 0), cfg_ver));
993 
994 	/* Generate random clock ID as the device has not such property that
995 	 * could be used for this purpose. A user can later change this value
996 	 * using devlink.
997 	 */
998 	zldev->clock_id = get_random_u64();
999 
1000 	/* Default phase offset averaging factor */
1001 	zldev->phase_avg_factor = 2;
1002 
1003 	/* Initialize mutex for operations where multiple reads, writes
1004 	 * and/or polls are required to be done atomically.
1005 	 */
1006 	rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock);
1007 	if (rc)
1008 		return dev_err_probe(zldev->dev, rc,
1009 				     "Failed to initialize mutex\n");
1010 
1011 	/* Register DPLL channels */
1012 	rc = zl3073x_devm_dpll_init(zldev);
1013 	if (rc)
1014 		return rc;
1015 
1016 	/* Register the devlink instance and parameters */
1017 	rc = zl3073x_devlink_register(zldev);
1018 	if (rc)
1019 		return dev_err_probe(zldev->dev, rc,
1020 				     "Failed to register devlink instance\n");
1021 
1022 	return 0;
1023 }
1024 EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X");
1025 
1026 MODULE_AUTHOR("Ivan Vecera <ivecera@redhat.com>");
1027 MODULE_DESCRIPTION("Microchip ZL3073x core driver");
1028 MODULE_LICENSE("GPL");
1029