Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/dpll/zl3073x/core.c
29268 views
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
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
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
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
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
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
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
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
*/
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
*/
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
*/
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
*/
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
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
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
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
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
*/
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
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
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
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
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
*/
1038
int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full)
1039
{
1040
struct zl3073x_dpll *zldpll;
1041
int rc;
1042
1043
if (full) {
1044
/* Fetch device state */
1045
rc = zl3073x_dev_state_fetch(zldev);
1046
if (rc)
1047
return rc;
1048
1049
/* Setup phase offset measurement block */
1050
rc = zl3073x_dev_phase_meas_setup(zldev);
1051
if (rc) {
1052
dev_err(zldev->dev,
1053
"Failed to setup phase measurement\n");
1054
return rc;
1055
}
1056
}
1057
1058
/* Register all DPLLs */
1059
list_for_each_entry(zldpll, &zldev->dplls, list) {
1060
rc = zl3073x_dpll_register(zldpll);
1061
if (rc) {
1062
dev_err_probe(zldev->dev, rc,
1063
"Failed to register DPLL%u\n",
1064
zldpll->id);
1065
return rc;
1066
}
1067
}
1068
1069
/* Perform initial firmware fine phase correction */
1070
rc = zl3073x_dpll_init_fine_phase_adjust(zldev);
1071
if (rc) {
1072
dev_err_probe(zldev->dev, rc,
1073
"Failed to init fine phase correction\n");
1074
return rc;
1075
}
1076
1077
/* Start monitoring */
1078
kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0);
1079
1080
return 0;
1081
}
1082
1083
/**
1084
* zl3073x_dev_stop - Stop normal operation
1085
* @zldev: zl3073x device pointer
1086
*
1087
* The function stops the normal operation that mean deregistration of all
1088
* DPLLs and their pins and stop monitoring.
1089
*
1090
* Return: 0 on success, <0 on error
1091
*/
1092
void zl3073x_dev_stop(struct zl3073x_dev *zldev)
1093
{
1094
struct zl3073x_dpll *zldpll;
1095
1096
/* Stop monitoring */
1097
kthread_cancel_delayed_work_sync(&zldev->work);
1098
1099
/* Unregister all DPLLs */
1100
list_for_each_entry(zldpll, &zldev->dplls, list) {
1101
if (zldpll->dpll_dev)
1102
zl3073x_dpll_unregister(zldpll);
1103
}
1104
}
1105
1106
static void zl3073x_dev_dpll_fini(void *ptr)
1107
{
1108
struct zl3073x_dpll *zldpll, *next;
1109
struct zl3073x_dev *zldev = ptr;
1110
1111
/* Stop monitoring and unregister DPLLs */
1112
zl3073x_dev_stop(zldev);
1113
1114
/* Destroy monitoring thread */
1115
if (zldev->kworker) {
1116
kthread_destroy_worker(zldev->kworker);
1117
zldev->kworker = NULL;
1118
}
1119
1120
/* Free all DPLLs */
1121
list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) {
1122
list_del(&zldpll->list);
1123
zl3073x_dpll_free(zldpll);
1124
}
1125
}
1126
1127
static int
1128
zl3073x_devm_dpll_init(struct zl3073x_dev *zldev, u8 num_dplls)
1129
{
1130
struct kthread_worker *kworker;
1131
struct zl3073x_dpll *zldpll;
1132
unsigned int i;
1133
int rc;
1134
1135
INIT_LIST_HEAD(&zldev->dplls);
1136
1137
/* Allocate all DPLLs */
1138
for (i = 0; i < num_dplls; i++) {
1139
zldpll = zl3073x_dpll_alloc(zldev, i);
1140
if (IS_ERR(zldpll)) {
1141
dev_err_probe(zldev->dev, PTR_ERR(zldpll),
1142
"Failed to alloc DPLL%u\n", i);
1143
rc = PTR_ERR(zldpll);
1144
goto error;
1145
}
1146
1147
list_add_tail(&zldpll->list, &zldev->dplls);
1148
}
1149
1150
/* Initialize monitoring thread */
1151
kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work);
1152
kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev));
1153
if (IS_ERR(kworker)) {
1154
rc = PTR_ERR(kworker);
1155
goto error;
1156
}
1157
zldev->kworker = kworker;
1158
1159
/* Start normal operation */
1160
rc = zl3073x_dev_start(zldev, true);
1161
if (rc) {
1162
dev_err_probe(zldev->dev, rc, "Failed to start device\n");
1163
goto error;
1164
}
1165
1166
/* Add devres action to release DPLL related resources */
1167
rc = devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev);
1168
if (rc)
1169
goto error;
1170
1171
return 0;
1172
1173
error:
1174
zl3073x_dev_dpll_fini(zldev);
1175
1176
return rc;
1177
}
1178
1179
/**
1180
* zl3073x_dev_probe - initialize zl3073x device
1181
* @zldev: pointer to zl3073x device
1182
* @chip_info: chip info based on compatible
1183
*
1184
* Common initialization of zl3073x device structure.
1185
*
1186
* Returns: 0 on success, <0 on error
1187
*/
1188
int zl3073x_dev_probe(struct zl3073x_dev *zldev,
1189
const struct zl3073x_chip_info *chip_info)
1190
{
1191
u16 id, revision, fw_ver;
1192
unsigned int i;
1193
u32 cfg_ver;
1194
int rc;
1195
1196
/* Read chip ID */
1197
rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id);
1198
if (rc)
1199
return rc;
1200
1201
/* Check it matches */
1202
for (i = 0; i < chip_info->num_ids; i++) {
1203
if (id == chip_info->ids[i])
1204
break;
1205
}
1206
1207
if (i == chip_info->num_ids) {
1208
return dev_err_probe(zldev->dev, -ENODEV,
1209
"Unknown or non-match chip ID: 0x%0x\n",
1210
id);
1211
}
1212
1213
/* Read revision, firmware version and custom config version */
1214
rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision);
1215
if (rc)
1216
return rc;
1217
rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver);
1218
if (rc)
1219
return rc;
1220
rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver);
1221
if (rc)
1222
return rc;
1223
1224
dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id,
1225
revision, fw_ver);
1226
dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n",
1227
FIELD_GET(GENMASK(31, 24), cfg_ver),
1228
FIELD_GET(GENMASK(23, 16), cfg_ver),
1229
FIELD_GET(GENMASK(15, 8), cfg_ver),
1230
FIELD_GET(GENMASK(7, 0), cfg_ver));
1231
1232
/* Generate random clock ID as the device has not such property that
1233
* could be used for this purpose. A user can later change this value
1234
* using devlink.
1235
*/
1236
zldev->clock_id = get_random_u64();
1237
1238
/* Default phase offset averaging factor */
1239
zldev->phase_avg_factor = 2;
1240
1241
/* Initialize mutex for operations where multiple reads, writes
1242
* and/or polls are required to be done atomically.
1243
*/
1244
rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock);
1245
if (rc)
1246
return dev_err_probe(zldev->dev, rc,
1247
"Failed to initialize mutex\n");
1248
1249
/* Register DPLL channels */
1250
rc = zl3073x_devm_dpll_init(zldev, chip_info->num_channels);
1251
if (rc)
1252
return rc;
1253
1254
/* Register the devlink instance and parameters */
1255
rc = zl3073x_devlink_register(zldev);
1256
if (rc)
1257
return dev_err_probe(zldev->dev, rc,
1258
"Failed to register devlink instance\n");
1259
1260
return 0;
1261
}
1262
EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X");
1263
1264
MODULE_AUTHOR("Ivan Vecera <[email protected]>");
1265
MODULE_DESCRIPTION("Microchip ZL3073x core driver");
1266
MODULE_LICENSE("GPL");
1267
1268