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