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 /**
133 * zl3073x_ref_freq_factorize - factorize given frequency
134 * @freq: input frequency
135 * @base: base frequency
136 * @mult: multiplier
137 *
138 * Checks if the given frequency can be factorized using one of the
139 * supported base frequencies. If so the base frequency and multiplier
140 * are stored into appropriate parameters if they are not NULL.
141 *
142 * Return: 0 on success, -EINVAL if the frequency cannot be factorized
143 */
144 int
zl3073x_ref_freq_factorize(u32 freq,u16 * base,u16 * mult)145 zl3073x_ref_freq_factorize(u32 freq, u16 *base, u16 *mult)
146 {
147 static const u16 base_freqs[] = {
148 1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125,
149 128, 160, 200, 250, 256, 320, 400, 500, 625, 640, 800, 1000,
150 1250, 1280, 1600, 2000, 2500, 3125, 3200, 4000, 5000, 6250,
151 6400, 8000, 10000, 12500, 15625, 16000, 20000, 25000, 31250,
152 32000, 40000, 50000, 62500,
153 };
154 u32 div;
155 int i;
156
157 for (i = 0; i < ARRAY_SIZE(base_freqs); i++) {
158 div = freq / base_freqs[i];
159
160 if (div <= U16_MAX && (freq % base_freqs[i]) == 0) {
161 if (base)
162 *base = base_freqs[i];
163 if (mult)
164 *mult = div;
165
166 return 0;
167 }
168 }
169
170 return -EINVAL;
171 }
172
173 static bool
zl3073x_check_reg(struct zl3073x_dev * zldev,unsigned int reg,size_t size)174 zl3073x_check_reg(struct zl3073x_dev *zldev, unsigned int reg, size_t size)
175 {
176 /* Check that multiop lock is held when accessing registers
177 * from page 10 and above except the page 255 that does not
178 * need this protection.
179 */
180 if (ZL_REG_PAGE(reg) >= 10 && ZL_REG_PAGE(reg) < 255)
181 lockdep_assert_held(&zldev->multiop_lock);
182
183 /* Check the index is in valid range for indexed register */
184 if (ZL_REG_OFFSET(reg) > ZL_REG_MAX_OFFSET(reg)) {
185 dev_err(zldev->dev, "Index out of range for reg 0x%04lx\n",
186 ZL_REG_ADDR(reg));
187 return false;
188 }
189 /* Check the requested size corresponds to register size */
190 if (ZL_REG_SIZE(reg) != size) {
191 dev_err(zldev->dev, "Invalid size %zu for reg 0x%04lx\n",
192 size, ZL_REG_ADDR(reg));
193 return false;
194 }
195
196 return true;
197 }
198
199 static int
zl3073x_read_reg(struct zl3073x_dev * zldev,unsigned int reg,void * val,size_t size)200 zl3073x_read_reg(struct zl3073x_dev *zldev, unsigned int reg, void *val,
201 size_t size)
202 {
203 int rc;
204
205 if (!zl3073x_check_reg(zldev, reg, size))
206 return -EINVAL;
207
208 /* Map the register address to virtual range */
209 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET;
210
211 rc = regmap_bulk_read(zldev->regmap, reg, val, size);
212 if (rc) {
213 dev_err(zldev->dev, "Failed to read reg 0x%04x: %pe\n", reg,
214 ERR_PTR(rc));
215 return rc;
216 }
217
218 return 0;
219 }
220
221 static int
zl3073x_write_reg(struct zl3073x_dev * zldev,unsigned int reg,const void * val,size_t size)222 zl3073x_write_reg(struct zl3073x_dev *zldev, unsigned int reg, const void *val,
223 size_t size)
224 {
225 int rc;
226
227 if (!zl3073x_check_reg(zldev, reg, size))
228 return -EINVAL;
229
230 /* Map the register address to virtual range */
231 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET;
232
233 rc = regmap_bulk_write(zldev->regmap, reg, val, size);
234 if (rc) {
235 dev_err(zldev->dev, "Failed to write reg 0x%04x: %pe\n", reg,
236 ERR_PTR(rc));
237 return rc;
238 }
239
240 return 0;
241 }
242
243 /**
244 * zl3073x_read_u8 - read value from 8bit register
245 * @zldev: zl3073x device pointer
246 * @reg: register to write to
247 * @val: value to write
248 *
249 * Reads value from given 8bit register.
250 *
251 * Returns: 0 on success, <0 on error
252 */
zl3073x_read_u8(struct zl3073x_dev * zldev,unsigned int reg,u8 * val)253 int zl3073x_read_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 *val)
254 {
255 return zl3073x_read_reg(zldev, reg, val, sizeof(*val));
256 }
257
258 /**
259 * zl3073x_write_u8 - write value to 16bit register
260 * @zldev: zl3073x device pointer
261 * @reg: register to write to
262 * @val: value to write
263 *
264 * Writes value into given 8bit register.
265 *
266 * Returns: 0 on success, <0 on error
267 */
zl3073x_write_u8(struct zl3073x_dev * zldev,unsigned int reg,u8 val)268 int zl3073x_write_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 val)
269 {
270 return zl3073x_write_reg(zldev, reg, &val, sizeof(val));
271 }
272
273 /**
274 * zl3073x_read_u16 - read value from 16bit register
275 * @zldev: zl3073x device pointer
276 * @reg: register to write to
277 * @val: value to write
278 *
279 * Reads value from given 16bit register.
280 *
281 * Returns: 0 on success, <0 on error
282 */
zl3073x_read_u16(struct zl3073x_dev * zldev,unsigned int reg,u16 * val)283 int zl3073x_read_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 *val)
284 {
285 int rc;
286
287 rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val));
288 if (!rc)
289 be16_to_cpus(val);
290
291 return rc;
292 }
293
294 /**
295 * zl3073x_write_u16 - write value to 16bit register
296 * @zldev: zl3073x device pointer
297 * @reg: register to write to
298 * @val: value to write
299 *
300 * Writes value into given 16bit register.
301 *
302 * Returns: 0 on success, <0 on error
303 */
zl3073x_write_u16(struct zl3073x_dev * zldev,unsigned int reg,u16 val)304 int zl3073x_write_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 val)
305 {
306 cpu_to_be16s(&val);
307
308 return zl3073x_write_reg(zldev, reg, &val, sizeof(val));
309 }
310
311 /**
312 * zl3073x_read_u32 - read value from 32bit register
313 * @zldev: zl3073x device pointer
314 * @reg: register to write to
315 * @val: value to write
316 *
317 * Reads value from given 32bit register.
318 *
319 * Returns: 0 on success, <0 on error
320 */
zl3073x_read_u32(struct zl3073x_dev * zldev,unsigned int reg,u32 * val)321 int zl3073x_read_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 *val)
322 {
323 int rc;
324
325 rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val));
326 if (!rc)
327 be32_to_cpus(val);
328
329 return rc;
330 }
331
332 /**
333 * zl3073x_write_u32 - write value to 32bit register
334 * @zldev: zl3073x device pointer
335 * @reg: register to write to
336 * @val: value to write
337 *
338 * Writes value into given 32bit register.
339 *
340 * Returns: 0 on success, <0 on error
341 */
zl3073x_write_u32(struct zl3073x_dev * zldev,unsigned int reg,u32 val)342 int zl3073x_write_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 val)
343 {
344 cpu_to_be32s(&val);
345
346 return zl3073x_write_reg(zldev, reg, &val, sizeof(val));
347 }
348
349 /**
350 * zl3073x_read_u48 - read value from 48bit register
351 * @zldev: zl3073x device pointer
352 * @reg: register to write to
353 * @val: value to write
354 *
355 * Reads value from given 48bit register.
356 *
357 * Returns: 0 on success, <0 on error
358 */
zl3073x_read_u48(struct zl3073x_dev * zldev,unsigned int reg,u64 * val)359 int zl3073x_read_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 *val)
360 {
361 u8 buf[6];
362 int rc;
363
364 rc = zl3073x_read_reg(zldev, reg, buf, sizeof(buf));
365 if (!rc)
366 *val = get_unaligned_be48(buf);
367
368 return rc;
369 }
370
371 /**
372 * zl3073x_write_u48 - write value to 48bit register
373 * @zldev: zl3073x device pointer
374 * @reg: register to write to
375 * @val: value to write
376 *
377 * Writes value into given 48bit register.
378 * The value must be from the interval -S48_MIN to U48_MAX.
379 *
380 * Returns: 0 on success, <0 on error
381 */
zl3073x_write_u48(struct zl3073x_dev * zldev,unsigned int reg,u64 val)382 int zl3073x_write_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 val)
383 {
384 u8 buf[6];
385
386 /* Check the value belongs to <S48_MIN, U48_MAX>
387 * Any value >= S48_MIN has bits 47..63 set.
388 */
389 if (val > GENMASK_ULL(47, 0) && val < GENMASK_ULL(63, 47)) {
390 dev_err(zldev->dev, "Value 0x%0llx out of range\n", val);
391 return -EINVAL;
392 }
393
394 put_unaligned_be48(val, buf);
395
396 return zl3073x_write_reg(zldev, reg, buf, sizeof(buf));
397 }
398
399 /**
400 * zl3073x_poll_zero_u8 - wait for register to be cleared by device
401 * @zldev: zl3073x device pointer
402 * @reg: register to poll (has to be 8bit register)
403 * @mask: bit mask for polling
404 *
405 * Waits for bits specified by @mask in register @reg value to be cleared
406 * by the device.
407 *
408 * Returns: 0 on success, <0 on error
409 */
zl3073x_poll_zero_u8(struct zl3073x_dev * zldev,unsigned int reg,u8 mask)410 int zl3073x_poll_zero_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 mask)
411 {
412 /* Register polling sleep & timeout */
413 #define ZL_POLL_SLEEP_US 10
414 #define ZL_POLL_TIMEOUT_US 2000000
415 unsigned int val;
416
417 /* Check the register is 8bit */
418 if (ZL_REG_SIZE(reg) != 1) {
419 dev_err(zldev->dev, "Invalid reg 0x%04lx size for polling\n",
420 ZL_REG_ADDR(reg));
421 return -EINVAL;
422 }
423
424 /* Map the register address to virtual range */
425 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET;
426
427 return regmap_read_poll_timeout(zldev->regmap, reg, val, !(val & mask),
428 ZL_POLL_SLEEP_US, ZL_POLL_TIMEOUT_US);
429 }
430
zl3073x_mb_op(struct zl3073x_dev * zldev,unsigned int op_reg,u8 op_val,unsigned int mask_reg,u16 mask_val)431 int zl3073x_mb_op(struct zl3073x_dev *zldev, unsigned int op_reg, u8 op_val,
432 unsigned int mask_reg, u16 mask_val)
433 {
434 int rc;
435
436 /* Set mask for the operation */
437 rc = zl3073x_write_u16(zldev, mask_reg, mask_val);
438 if (rc)
439 return rc;
440
441 /* Trigger the operation */
442 rc = zl3073x_write_u8(zldev, op_reg, op_val);
443 if (rc)
444 return rc;
445
446 /* Wait for the operation to actually finish */
447 return zl3073x_poll_zero_u8(zldev, op_reg, op_val);
448 }
449
450 /**
451 * zl3073x_do_hwreg_op - Perform HW register read/write operation
452 * @zldev: zl3073x device pointer
453 * @op: operation to perform
454 *
455 * Performs requested operation and waits for its completion.
456 *
457 * Return: 0 on success, <0 on error
458 */
459 static int
zl3073x_do_hwreg_op(struct zl3073x_dev * zldev,u8 op)460 zl3073x_do_hwreg_op(struct zl3073x_dev *zldev, u8 op)
461 {
462 int rc;
463
464 /* Set requested operation and set pending bit */
465 rc = zl3073x_write_u8(zldev, ZL_REG_HWREG_OP, op | ZL_HWREG_OP_PENDING);
466 if (rc)
467 return rc;
468
469 /* Poll for completion - pending bit cleared */
470 return zl3073x_poll_zero_u8(zldev, ZL_REG_HWREG_OP,
471 ZL_HWREG_OP_PENDING);
472 }
473
474 /**
475 * zl3073x_read_hwreg - Read HW register
476 * @zldev: zl3073x device pointer
477 * @addr: HW register address
478 * @value: Value of the HW register
479 *
480 * Reads HW register value and stores it into @value.
481 *
482 * Return: 0 on success, <0 on error
483 */
zl3073x_read_hwreg(struct zl3073x_dev * zldev,u32 addr,u32 * value)484 int zl3073x_read_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 *value)
485 {
486 int rc;
487
488 /* Set address to read data from */
489 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr);
490 if (rc)
491 return rc;
492
493 /* Perform the read operation */
494 rc = zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_READ);
495 if (rc)
496 return rc;
497
498 /* Read the received data */
499 return zl3073x_read_u32(zldev, ZL_REG_HWREG_READ_DATA, value);
500 }
501
502 /**
503 * zl3073x_write_hwreg - Write value to HW register
504 * @zldev: zl3073x device pointer
505 * @addr: HW registers address
506 * @value: Value to be written to HW register
507 *
508 * Stores the requested value into HW register.
509 *
510 * Return: 0 on success, <0 on error
511 */
zl3073x_write_hwreg(struct zl3073x_dev * zldev,u32 addr,u32 value)512 int zl3073x_write_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value)
513 {
514 int rc;
515
516 /* Set address to write data to */
517 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr);
518 if (rc)
519 return rc;
520
521 /* Set data to be written */
522 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_WRITE_DATA, value);
523 if (rc)
524 return rc;
525
526 /* Perform the write operation */
527 return zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_WRITE);
528 }
529
530 /**
531 * zl3073x_update_hwreg - Update certain bits in HW register
532 * @zldev: zl3073x device pointer
533 * @addr: HW register address
534 * @value: Value to be written into HW register
535 * @mask: Bitmask indicating bits to be updated
536 *
537 * Reads given HW register, updates requested bits specified by value and
538 * mask and writes result back to HW register.
539 *
540 * Return: 0 on success, <0 on error
541 */
zl3073x_update_hwreg(struct zl3073x_dev * zldev,u32 addr,u32 value,u32 mask)542 int zl3073x_update_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value,
543 u32 mask)
544 {
545 u32 tmp;
546 int rc;
547
548 rc = zl3073x_read_hwreg(zldev, addr, &tmp);
549 if (rc)
550 return rc;
551
552 tmp &= ~mask;
553 tmp |= value & mask;
554
555 return zl3073x_write_hwreg(zldev, addr, tmp);
556 }
557
558 /**
559 * zl3073x_write_hwreg_seq - Write HW registers sequence
560 * @zldev: pointer to device structure
561 * @seq: pointer to first sequence item
562 * @num_items: number of items in sequence
563 *
564 * Writes given HW registers sequence.
565 *
566 * Return: 0 on success, <0 on error
567 */
zl3073x_write_hwreg_seq(struct zl3073x_dev * zldev,const struct zl3073x_hwreg_seq_item * seq,size_t num_items)568 int zl3073x_write_hwreg_seq(struct zl3073x_dev *zldev,
569 const struct zl3073x_hwreg_seq_item *seq,
570 size_t num_items)
571 {
572 int i, rc = 0;
573
574 for (i = 0; i < num_items; i++) {
575 dev_dbg(zldev->dev, "Write 0x%0x [0x%0x] to 0x%0x",
576 seq[i].value, seq[i].mask, seq[i].addr);
577
578 if (seq[i].mask == U32_MAX)
579 /* Write value directly */
580 rc = zl3073x_write_hwreg(zldev, seq[i].addr,
581 seq[i].value);
582 else
583 /* Update only bits specified by the mask */
584 rc = zl3073x_update_hwreg(zldev, seq[i].addr,
585 seq[i].value, seq[i].mask);
586 if (rc)
587 return rc;
588
589 if (seq->wait)
590 msleep(seq->wait);
591 }
592
593 return rc;
594 }
595
596 /**
597 * zl3073x_ref_state_fetch - get input reference state
598 * @zldev: pointer to zl3073x_dev structure
599 * @index: input reference index to fetch state for
600 *
601 * Function fetches information for the given input reference that are
602 * invariant and stores them for later use.
603 *
604 * Return: 0 on success, <0 on error
605 */
606 static int
zl3073x_ref_state_fetch(struct zl3073x_dev * zldev,u8 index)607 zl3073x_ref_state_fetch(struct zl3073x_dev *zldev, u8 index)
608 {
609 struct zl3073x_ref *input = &zldev->ref[index];
610 u8 ref_config;
611 int rc;
612
613 /* If the input is differential then the configuration for N-pin
614 * reference is ignored and P-pin config is used for both.
615 */
616 if (zl3073x_is_n_pin(index) &&
617 zl3073x_ref_is_diff(zldev, index - 1)) {
618 input->enabled = zl3073x_ref_is_enabled(zldev, index - 1);
619 input->diff = true;
620
621 return 0;
622 }
623
624 guard(mutex)(&zldev->multiop_lock);
625
626 /* Read reference configuration */
627 rc = zl3073x_mb_op(zldev, ZL_REG_REF_MB_SEM, ZL_REF_MB_SEM_RD,
628 ZL_REG_REF_MB_MASK, BIT(index));
629 if (rc)
630 return rc;
631
632 /* Read ref_config register */
633 rc = zl3073x_read_u8(zldev, ZL_REG_REF_CONFIG, &ref_config);
634 if (rc)
635 return rc;
636
637 input->enabled = FIELD_GET(ZL_REF_CONFIG_ENABLE, ref_config);
638 input->diff = FIELD_GET(ZL_REF_CONFIG_DIFF_EN, ref_config);
639
640 dev_dbg(zldev->dev, "REF%u is %s and configured as %s\n", index,
641 str_enabled_disabled(input->enabled),
642 input->diff ? "differential" : "single-ended");
643
644 return rc;
645 }
646
647 /**
648 * zl3073x_out_state_fetch - get output state
649 * @zldev: pointer to zl3073x_dev structure
650 * @index: output index to fetch state for
651 *
652 * Function fetches information for the given output (not output pin)
653 * that are invariant and stores them for later use.
654 *
655 * Return: 0 on success, <0 on error
656 */
657 static int
zl3073x_out_state_fetch(struct zl3073x_dev * zldev,u8 index)658 zl3073x_out_state_fetch(struct zl3073x_dev *zldev, u8 index)
659 {
660 struct zl3073x_out *out = &zldev->out[index];
661 u8 output_ctrl, output_mode;
662 int rc;
663
664 /* Read output configuration */
665 rc = zl3073x_read_u8(zldev, ZL_REG_OUTPUT_CTRL(index), &output_ctrl);
666 if (rc)
667 return rc;
668
669 /* Store info about output enablement and synthesizer the output
670 * is connected to.
671 */
672 out->enabled = FIELD_GET(ZL_OUTPUT_CTRL_EN, output_ctrl);
673 out->synth = FIELD_GET(ZL_OUTPUT_CTRL_SYNTH_SEL, output_ctrl);
674
675 dev_dbg(zldev->dev, "OUT%u is %s and connected to SYNTH%u\n", index,
676 str_enabled_disabled(out->enabled), out->synth);
677
678 guard(mutex)(&zldev->multiop_lock);
679
680 /* Read output configuration */
681 rc = zl3073x_mb_op(zldev, ZL_REG_OUTPUT_MB_SEM, ZL_OUTPUT_MB_SEM_RD,
682 ZL_REG_OUTPUT_MB_MASK, BIT(index));
683 if (rc)
684 return rc;
685
686 /* Read output_mode */
687 rc = zl3073x_read_u8(zldev, ZL_REG_OUTPUT_MODE, &output_mode);
688 if (rc)
689 return rc;
690
691 /* Extract and store output signal format */
692 out->signal_format = FIELD_GET(ZL_OUTPUT_MODE_SIGNAL_FORMAT,
693 output_mode);
694
695 dev_dbg(zldev->dev, "OUT%u has signal format 0x%02x\n", index,
696 out->signal_format);
697
698 return rc;
699 }
700
701 /**
702 * zl3073x_synth_state_fetch - get synth state
703 * @zldev: pointer to zl3073x_dev structure
704 * @index: synth index to fetch state for
705 *
706 * Function fetches information for the given synthesizer that are
707 * invariant and stores them for later use.
708 *
709 * Return: 0 on success, <0 on error
710 */
711 static int
zl3073x_synth_state_fetch(struct zl3073x_dev * zldev,u8 index)712 zl3073x_synth_state_fetch(struct zl3073x_dev *zldev, u8 index)
713 {
714 struct zl3073x_synth *synth = &zldev->synth[index];
715 u16 base, m, n;
716 u8 synth_ctrl;
717 u32 mult;
718 int rc;
719
720 /* Read synth control register */
721 rc = zl3073x_read_u8(zldev, ZL_REG_SYNTH_CTRL(index), &synth_ctrl);
722 if (rc)
723 return rc;
724
725 /* Store info about synth enablement and DPLL channel the synth is
726 * driven by.
727 */
728 synth->enabled = FIELD_GET(ZL_SYNTH_CTRL_EN, synth_ctrl);
729 synth->dpll = FIELD_GET(ZL_SYNTH_CTRL_DPLL_SEL, synth_ctrl);
730
731 dev_dbg(zldev->dev, "SYNTH%u is %s and driven by DPLL%u\n", index,
732 str_enabled_disabled(synth->enabled), synth->dpll);
733
734 guard(mutex)(&zldev->multiop_lock);
735
736 /* Read synth configuration */
737 rc = zl3073x_mb_op(zldev, ZL_REG_SYNTH_MB_SEM, ZL_SYNTH_MB_SEM_RD,
738 ZL_REG_SYNTH_MB_MASK, BIT(index));
739 if (rc)
740 return rc;
741
742 /* The output frequency is determined by the following formula:
743 * base * multiplier * numerator / denominator
744 *
745 * Read registers with these values
746 */
747 rc = zl3073x_read_u16(zldev, ZL_REG_SYNTH_FREQ_BASE, &base);
748 if (rc)
749 return rc;
750
751 rc = zl3073x_read_u32(zldev, ZL_REG_SYNTH_FREQ_MULT, &mult);
752 if (rc)
753 return rc;
754
755 rc = zl3073x_read_u16(zldev, ZL_REG_SYNTH_FREQ_M, &m);
756 if (rc)
757 return rc;
758
759 rc = zl3073x_read_u16(zldev, ZL_REG_SYNTH_FREQ_N, &n);
760 if (rc)
761 return rc;
762
763 /* Check denominator for zero to avoid div by 0 */
764 if (!n) {
765 dev_err(zldev->dev,
766 "Zero divisor for SYNTH%u retrieved from device\n",
767 index);
768 return -EINVAL;
769 }
770
771 /* Compute and store synth frequency */
772 zldev->synth[index].freq = div_u64(mul_u32_u32(base * m, mult), n);
773
774 dev_dbg(zldev->dev, "SYNTH%u frequency: %u Hz\n", index,
775 zldev->synth[index].freq);
776
777 return rc;
778 }
779
780 static int
zl3073x_dev_state_fetch(struct zl3073x_dev * zldev)781 zl3073x_dev_state_fetch(struct zl3073x_dev *zldev)
782 {
783 int rc;
784 u8 i;
785
786 for (i = 0; i < ZL3073X_NUM_REFS; i++) {
787 rc = zl3073x_ref_state_fetch(zldev, i);
788 if (rc) {
789 dev_err(zldev->dev,
790 "Failed to fetch input state: %pe\n",
791 ERR_PTR(rc));
792 return rc;
793 }
794 }
795
796 for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) {
797 rc = zl3073x_synth_state_fetch(zldev, i);
798 if (rc) {
799 dev_err(zldev->dev,
800 "Failed to fetch synth state: %pe\n",
801 ERR_PTR(rc));
802 return rc;
803 }
804 }
805
806 for (i = 0; i < ZL3073X_NUM_OUTS; i++) {
807 rc = zl3073x_out_state_fetch(zldev, i);
808 if (rc) {
809 dev_err(zldev->dev,
810 "Failed to fetch output state: %pe\n",
811 ERR_PTR(rc));
812 return rc;
813 }
814 }
815
816 return rc;
817 }
818
819 /**
820 * zl3073x_ref_phase_offsets_update - update reference phase offsets
821 * @zldev: pointer to zl3073x_dev structure
822 * @channel: DPLL channel number or -1
823 *
824 * The function asks device to update phase offsets latch registers with
825 * the latest measured values. There are 2 sets of latch registers:
826 *
827 * 1) Up to 5 DPLL-to-connected-ref registers that contain phase offset
828 * values between particular DPLL channel and its *connected* input
829 * reference.
830 *
831 * 2) 10 selected-DPLL-to-all-ref registers that contain phase offset values
832 * between selected DPLL channel and all input references.
833 *
834 * If the caller is interested in 2) then it has to pass DPLL channel number
835 * in @channel parameter. If it is interested only in 1) then it should pass
836 * @channel parameter with value of -1.
837 *
838 * Return: 0 on success, <0 on error
839 */
zl3073x_ref_phase_offsets_update(struct zl3073x_dev * zldev,int channel)840 int zl3073x_ref_phase_offsets_update(struct zl3073x_dev *zldev, int channel)
841 {
842 int rc;
843
844 /* Per datasheet we have to wait for 'dpll_ref_phase_err_rqst_rd'
845 * to be zero to ensure that the measured data are coherent.
846 */
847 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
848 ZL_REF_PHASE_ERR_READ_RQST_RD);
849 if (rc)
850 return rc;
851
852 /* Select DPLL channel if it is specified */
853 if (channel != -1) {
854 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_IDX, channel);
855 if (rc)
856 return rc;
857 }
858
859 /* Request to update phase offsets measurement values */
860 rc = zl3073x_write_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
861 ZL_REF_PHASE_ERR_READ_RQST_RD);
862 if (rc)
863 return rc;
864
865 /* Wait for finish */
866 return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST,
867 ZL_REF_PHASE_ERR_READ_RQST_RD);
868 }
869
870 /**
871 * zl3073x_ref_ffo_update - update reference fractional frequency offsets
872 * @zldev: pointer to zl3073x_dev structure
873 *
874 * The function asks device to update fractional frequency offsets latch
875 * registers the latest measured values, reads and stores them into
876 *
877 * Return: 0 on success, <0 on error
878 */
879 static int
zl3073x_ref_ffo_update(struct zl3073x_dev * zldev)880 zl3073x_ref_ffo_update(struct zl3073x_dev *zldev)
881 {
882 int i, rc;
883
884 /* Per datasheet we have to wait for 'ref_freq_meas_ctrl' to be zero
885 * to ensure that the measured data are coherent.
886 */
887 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
888 ZL_REF_FREQ_MEAS_CTRL);
889 if (rc)
890 return rc;
891
892 /* Select all references for measurement */
893 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_3_0,
894 GENMASK(7, 0)); /* REF0P..REF3N */
895 if (rc)
896 return rc;
897 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_4,
898 GENMASK(1, 0)); /* REF4P..REF4N */
899 if (rc)
900 return rc;
901
902 /* Request frequency offset measurement */
903 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
904 ZL_REF_FREQ_MEAS_CTRL_REF_FREQ_OFF);
905 if (rc)
906 return rc;
907
908 /* Wait for finish */
909 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL,
910 ZL_REF_FREQ_MEAS_CTRL);
911 if (rc)
912 return rc;
913
914 /* Read DPLL-to-REFx frequency offset measurements */
915 for (i = 0; i < ZL3073X_NUM_REFS; i++) {
916 s32 value;
917
918 /* Read value stored in units of 2^-32 signed */
919 rc = zl3073x_read_u32(zldev, ZL_REG_REF_FREQ(i), &value);
920 if (rc)
921 return rc;
922
923 /* Convert to ppm -> ffo = (10^6 * value) / 2^32 */
924 zldev->ref[i].ffo = mul_s64_u64_shr(value, 1000000, 32);
925 }
926
927 return 0;
928 }
929
930 static void
zl3073x_dev_periodic_work(struct kthread_work * work)931 zl3073x_dev_periodic_work(struct kthread_work *work)
932 {
933 struct zl3073x_dev *zldev = container_of(work, struct zl3073x_dev,
934 work.work);
935 struct zl3073x_dpll *zldpll;
936 int rc;
937
938 /* Update DPLL-to-connected-ref phase offsets registers */
939 rc = zl3073x_ref_phase_offsets_update(zldev, -1);
940 if (rc)
941 dev_warn(zldev->dev, "Failed to update phase offsets: %pe\n",
942 ERR_PTR(rc));
943
944 /* Update references' fractional frequency offsets */
945 rc = zl3073x_ref_ffo_update(zldev);
946 if (rc)
947 dev_warn(zldev->dev,
948 "Failed to update fractional frequency offsets: %pe\n",
949 ERR_PTR(rc));
950
951 list_for_each_entry(zldpll, &zldev->dplls, list)
952 zl3073x_dpll_changes_check(zldpll);
953
954 /* Run twice a second */
955 kthread_queue_delayed_work(zldev->kworker, &zldev->work,
956 msecs_to_jiffies(500));
957 }
958
zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev * zldev,u8 factor)959 int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor)
960 {
961 u8 dpll_meas_ctrl, value;
962 int rc;
963
964 /* Read DPLL phase measurement control register */
965 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl);
966 if (rc)
967 return rc;
968
969 /* Convert requested factor to register value */
970 value = (factor + 1) & 0x0f;
971
972 /* Update phase measurement control register */
973 dpll_meas_ctrl &= ~ZL_DPLL_MEAS_CTRL_AVG_FACTOR;
974 dpll_meas_ctrl |= FIELD_PREP(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, value);
975 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl);
976 if (rc)
977 return rc;
978
979 /* Save the new factor */
980 zldev->phase_avg_factor = factor;
981
982 return 0;
983 }
984
985 /**
986 * zl3073x_dev_phase_meas_setup - setup phase offset measurement
987 * @zldev: pointer to zl3073x_dev structure
988 *
989 * Enable phase offset measurement block, set measurement averaging factor
990 * and enable DPLL-to-its-ref phase measurement for all DPLLs.
991 *
992 * Returns: 0 on success, <0 on error
993 */
994 static int
zl3073x_dev_phase_meas_setup(struct zl3073x_dev * zldev)995 zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev)
996 {
997 struct zl3073x_dpll *zldpll;
998 u8 dpll_meas_ctrl, mask = 0;
999 int rc;
1000
1001 /* Setup phase measurement averaging factor */
1002 rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor);
1003 if (rc)
1004 return rc;
1005
1006 /* Read DPLL phase measurement control register */
1007 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl);
1008 if (rc)
1009 return rc;
1010
1011 /* Enable DPLL measurement block */
1012 dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN;
1013
1014 /* Update phase measurement control register */
1015 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl);
1016 if (rc)
1017 return rc;
1018
1019 /* Enable DPLL-to-connected-ref measurement for each channel */
1020 list_for_each_entry(zldpll, &zldev->dplls, list)
1021 mask |= BIT(zldpll->id);
1022
1023 return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask);
1024 }
1025
1026 /**
1027 * zl3073x_dev_start - Start normal operation
1028 * @zldev: zl3073x device pointer
1029 * @full: perform full initialization
1030 *
1031 * The function starts normal operation, which means registering all DPLLs and
1032 * their pins, and starting monitoring. If full initialization is requested,
1033 * the function additionally initializes the phase offset measurement block and
1034 * fetches hardware-invariant parameters.
1035 *
1036 * Return: 0 on success, <0 on error
1037 */
zl3073x_dev_start(struct zl3073x_dev * zldev,bool full)1038 int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full)
1039 {
1040 struct zl3073x_dpll *zldpll;
1041 u8 info;
1042 int rc;
1043
1044 rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info);
1045 if (rc) {
1046 dev_err(zldev->dev, "Failed to read device status info\n");
1047 return rc;
1048 }
1049
1050 if (!FIELD_GET(ZL_INFO_READY, info)) {
1051 /* The ready bit indicates that the firmware was successfully
1052 * configured and is ready for normal operation. If it is
1053 * cleared then the configuration stored in flash is wrong
1054 * or missing. In this situation the driver will expose
1055 * only devlink interface to give an opportunity to flash
1056 * the correct config.
1057 */
1058 dev_info(zldev->dev,
1059 "FW not fully ready - missing or corrupted config\n");
1060
1061 return 0;
1062 }
1063
1064 if (full) {
1065 /* Fetch device state */
1066 rc = zl3073x_dev_state_fetch(zldev);
1067 if (rc)
1068 return rc;
1069
1070 /* Setup phase offset measurement block */
1071 rc = zl3073x_dev_phase_meas_setup(zldev);
1072 if (rc) {
1073 dev_err(zldev->dev,
1074 "Failed to setup phase measurement\n");
1075 return rc;
1076 }
1077 }
1078
1079 /* Register all DPLLs */
1080 list_for_each_entry(zldpll, &zldev->dplls, list) {
1081 rc = zl3073x_dpll_register(zldpll);
1082 if (rc) {
1083 dev_err_probe(zldev->dev, rc,
1084 "Failed to register DPLL%u\n",
1085 zldpll->id);
1086 return rc;
1087 }
1088 }
1089
1090 /* Perform initial firmware fine phase correction */
1091 rc = zl3073x_dpll_init_fine_phase_adjust(zldev);
1092 if (rc) {
1093 dev_err_probe(zldev->dev, rc,
1094 "Failed to init fine phase correction\n");
1095 return rc;
1096 }
1097
1098 /* Start monitoring */
1099 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0);
1100
1101 return 0;
1102 }
1103
1104 /**
1105 * zl3073x_dev_stop - Stop normal operation
1106 * @zldev: zl3073x device pointer
1107 *
1108 * The function stops the normal operation that mean deregistration of all
1109 * DPLLs and their pins and stop monitoring.
1110 *
1111 * Return: 0 on success, <0 on error
1112 */
zl3073x_dev_stop(struct zl3073x_dev * zldev)1113 void zl3073x_dev_stop(struct zl3073x_dev *zldev)
1114 {
1115 struct zl3073x_dpll *zldpll;
1116
1117 /* Stop monitoring */
1118 kthread_cancel_delayed_work_sync(&zldev->work);
1119
1120 /* Unregister all DPLLs */
1121 list_for_each_entry(zldpll, &zldev->dplls, list) {
1122 if (zldpll->dpll_dev)
1123 zl3073x_dpll_unregister(zldpll);
1124 }
1125 }
1126
zl3073x_dev_dpll_fini(void * ptr)1127 static void zl3073x_dev_dpll_fini(void *ptr)
1128 {
1129 struct zl3073x_dpll *zldpll, *next;
1130 struct zl3073x_dev *zldev = ptr;
1131
1132 /* Stop monitoring and unregister DPLLs */
1133 zl3073x_dev_stop(zldev);
1134
1135 /* Destroy monitoring thread */
1136 if (zldev->kworker) {
1137 kthread_destroy_worker(zldev->kworker);
1138 zldev->kworker = NULL;
1139 }
1140
1141 /* Free all DPLLs */
1142 list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) {
1143 list_del(&zldpll->list);
1144 zl3073x_dpll_free(zldpll);
1145 }
1146 }
1147
1148 static int
zl3073x_devm_dpll_init(struct zl3073x_dev * zldev,u8 num_dplls)1149 zl3073x_devm_dpll_init(struct zl3073x_dev *zldev, u8 num_dplls)
1150 {
1151 struct kthread_worker *kworker;
1152 struct zl3073x_dpll *zldpll;
1153 unsigned int i;
1154 int rc;
1155
1156 INIT_LIST_HEAD(&zldev->dplls);
1157
1158 /* Allocate all DPLLs */
1159 for (i = 0; i < num_dplls; i++) {
1160 zldpll = zl3073x_dpll_alloc(zldev, i);
1161 if (IS_ERR(zldpll)) {
1162 dev_err_probe(zldev->dev, PTR_ERR(zldpll),
1163 "Failed to alloc DPLL%u\n", i);
1164 rc = PTR_ERR(zldpll);
1165 goto error;
1166 }
1167
1168 list_add_tail(&zldpll->list, &zldev->dplls);
1169 }
1170
1171 /* Initialize monitoring thread */
1172 kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work);
1173 kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev));
1174 if (IS_ERR(kworker)) {
1175 rc = PTR_ERR(kworker);
1176 goto error;
1177 }
1178 zldev->kworker = kworker;
1179
1180 /* Start normal operation */
1181 rc = zl3073x_dev_start(zldev, true);
1182 if (rc) {
1183 dev_err_probe(zldev->dev, rc, "Failed to start device\n");
1184 goto error;
1185 }
1186
1187 /* Add devres action to release DPLL related resources */
1188 rc = devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev);
1189 if (rc)
1190 goto error;
1191
1192 return 0;
1193
1194 error:
1195 zl3073x_dev_dpll_fini(zldev);
1196
1197 return rc;
1198 }
1199
1200 /**
1201 * zl3073x_dev_probe - initialize zl3073x device
1202 * @zldev: pointer to zl3073x device
1203 * @chip_info: chip info based on compatible
1204 *
1205 * Common initialization of zl3073x device structure.
1206 *
1207 * Returns: 0 on success, <0 on error
1208 */
zl3073x_dev_probe(struct zl3073x_dev * zldev,const struct zl3073x_chip_info * chip_info)1209 int zl3073x_dev_probe(struct zl3073x_dev *zldev,
1210 const struct zl3073x_chip_info *chip_info)
1211 {
1212 u16 id, revision, fw_ver;
1213 unsigned int i;
1214 u32 cfg_ver;
1215 int rc;
1216
1217 /* Read chip ID */
1218 rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id);
1219 if (rc)
1220 return rc;
1221
1222 /* Check it matches */
1223 for (i = 0; i < chip_info->num_ids; i++) {
1224 if (id == chip_info->ids[i])
1225 break;
1226 }
1227
1228 if (i == chip_info->num_ids) {
1229 return dev_err_probe(zldev->dev, -ENODEV,
1230 "Unknown or non-match chip ID: 0x%0x\n",
1231 id);
1232 }
1233
1234 /* Read revision, firmware version and custom config version */
1235 rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision);
1236 if (rc)
1237 return rc;
1238 rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver);
1239 if (rc)
1240 return rc;
1241 rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver);
1242 if (rc)
1243 return rc;
1244
1245 dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id,
1246 revision, fw_ver);
1247 dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n",
1248 FIELD_GET(GENMASK(31, 24), cfg_ver),
1249 FIELD_GET(GENMASK(23, 16), cfg_ver),
1250 FIELD_GET(GENMASK(15, 8), cfg_ver),
1251 FIELD_GET(GENMASK(7, 0), cfg_ver));
1252
1253 /* Generate random clock ID as the device has not such property that
1254 * could be used for this purpose. A user can later change this value
1255 * using devlink.
1256 */
1257 zldev->clock_id = get_random_u64();
1258
1259 /* Default phase offset averaging factor */
1260 zldev->phase_avg_factor = 2;
1261
1262 /* Initialize mutex for operations where multiple reads, writes
1263 * and/or polls are required to be done atomically.
1264 */
1265 rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock);
1266 if (rc)
1267 return dev_err_probe(zldev->dev, rc,
1268 "Failed to initialize mutex\n");
1269
1270 /* Register DPLL channels */
1271 rc = zl3073x_devm_dpll_init(zldev, chip_info->num_channels);
1272 if (rc)
1273 return rc;
1274
1275 /* Register the devlink instance and parameters */
1276 rc = zl3073x_devlink_register(zldev);
1277 if (rc)
1278 return dev_err_probe(zldev->dev, rc,
1279 "Failed to register devlink instance\n");
1280
1281 return 0;
1282 }
1283 EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X");
1284
1285 MODULE_AUTHOR("Ivan Vecera <ivecera@redhat.com>");
1286 MODULE_DESCRIPTION("Microchip ZL3073x core driver");
1287 MODULE_LICENSE("GPL");
1288