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