1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/array_size.h> 4 #include <linux/bitfield.h> 5 #include <linux/bits.h> 6 #include <linux/dev_printk.h> 7 #include <linux/device.h> 8 #include <linux/export.h> 9 #include <linux/math64.h> 10 #include <linux/module.h> 11 #include <linux/netlink.h> 12 #include <linux/regmap.h> 13 #include <linux/sprintf.h> 14 #include <linux/string_choices.h> 15 #include <linux/unaligned.h> 16 #include <net/devlink.h> 17 18 #include "core.h" 19 #include "devlink.h" 20 #include "dpll.h" 21 #include "regs.h" 22 23 #define ZL_CHIP_INFO(_id, _nchannels, _flags) \ 24 { .id = (_id), .num_channels = (_nchannels), .flags = (_flags) } 25 26 static const struct zl3073x_chip_info zl3073x_chip_ids[] = { 27 ZL_CHIP_INFO(0x0E30, 2, ZL3073X_FLAG_REF_PHASE_COMP_32), 28 ZL_CHIP_INFO(0x0E93, 1, ZL3073X_FLAG_REF_PHASE_COMP_32), 29 ZL_CHIP_INFO(0x0E94, 2, ZL3073X_FLAG_REF_PHASE_COMP_32), 30 ZL_CHIP_INFO(0x0E95, 3, ZL3073X_FLAG_REF_PHASE_COMP_32), 31 ZL_CHIP_INFO(0x0E96, 4, ZL3073X_FLAG_REF_PHASE_COMP_32), 32 ZL_CHIP_INFO(0x0E97, 5, ZL3073X_FLAG_REF_PHASE_COMP_32), 33 ZL_CHIP_INFO(0x1E93, 1, ZL3073X_FLAG_DIE_TEMP), 34 ZL_CHIP_INFO(0x1E94, 2, ZL3073X_FLAG_DIE_TEMP), 35 ZL_CHIP_INFO(0x1E95, 3, ZL3073X_FLAG_DIE_TEMP), 36 ZL_CHIP_INFO(0x1E96, 4, ZL3073X_FLAG_DIE_TEMP), 37 ZL_CHIP_INFO(0x1E97, 5, ZL3073X_FLAG_DIE_TEMP), 38 ZL_CHIP_INFO(0x1F60, 2, ZL3073X_FLAG_REF_PHASE_COMP_32), 39 ZL_CHIP_INFO(0x2E93, 1, ZL3073X_FLAG_DIE_TEMP), 40 ZL_CHIP_INFO(0x2E94, 2, ZL3073X_FLAG_DIE_TEMP), 41 ZL_CHIP_INFO(0x2E95, 3, ZL3073X_FLAG_DIE_TEMP), 42 ZL_CHIP_INFO(0x2E96, 4, ZL3073X_FLAG_DIE_TEMP), 43 ZL_CHIP_INFO(0x2E97, 5, ZL3073X_FLAG_DIE_TEMP), 44 ZL_CHIP_INFO(0x3FC4, 2, ZL3073X_FLAG_DIE_TEMP), 45 }; 46 47 #define ZL_RANGE_OFFSET 0x80 48 #define ZL_PAGE_SIZE 0x80 49 #define ZL_NUM_PAGES 256 50 #define ZL_PAGE_SEL 0x7F 51 #define ZL_PAGE_SEL_MASK GENMASK(7, 0) 52 #define ZL_NUM_REGS (ZL_NUM_PAGES * ZL_PAGE_SIZE) 53 54 /* Regmap range configuration */ 55 static const struct regmap_range_cfg zl3073x_regmap_range = { 56 .range_min = ZL_RANGE_OFFSET, 57 .range_max = ZL_RANGE_OFFSET + ZL_NUM_REGS - 1, 58 .selector_reg = ZL_PAGE_SEL, 59 .selector_mask = ZL_PAGE_SEL_MASK, 60 .selector_shift = 0, 61 .window_start = 0, 62 .window_len = ZL_PAGE_SIZE, 63 }; 64 65 static bool 66 zl3073x_is_volatile_reg(struct device *dev __maybe_unused, unsigned int reg) 67 { 68 /* Only page selector is non-volatile */ 69 return reg != ZL_PAGE_SEL; 70 } 71 72 const struct regmap_config zl3073x_regmap_config = { 73 .reg_bits = 8, 74 .val_bits = 8, 75 .max_register = ZL_RANGE_OFFSET + ZL_NUM_REGS - 1, 76 .ranges = &zl3073x_regmap_range, 77 .num_ranges = 1, 78 .cache_type = REGCACHE_MAPLE, 79 .volatile_reg = zl3073x_is_volatile_reg, 80 }; 81 EXPORT_SYMBOL_NS_GPL(zl3073x_regmap_config, "ZL3073X"); 82 83 static bool 84 zl3073x_check_reg(struct zl3073x_dev *zldev, unsigned int reg, size_t size) 85 { 86 /* Check that multiop lock is held when accessing registers 87 * from page 10 and above except the page 255 that does not 88 * need this protection. 89 */ 90 if (ZL_REG_PAGE(reg) >= 10 && ZL_REG_PAGE(reg) < 255) 91 lockdep_assert_held(&zldev->multiop_lock); 92 93 /* Check the index is in valid range for indexed register */ 94 if (ZL_REG_OFFSET(reg) > ZL_REG_MAX_OFFSET(reg)) { 95 dev_err(zldev->dev, "Index out of range for reg 0x%04lx\n", 96 ZL_REG_ADDR(reg)); 97 return false; 98 } 99 /* Check the requested size corresponds to register size */ 100 if (ZL_REG_SIZE(reg) != size) { 101 dev_err(zldev->dev, "Invalid size %zu for reg 0x%04lx\n", 102 size, ZL_REG_ADDR(reg)); 103 return false; 104 } 105 106 return true; 107 } 108 109 static int 110 zl3073x_read_reg(struct zl3073x_dev *zldev, unsigned int reg, void *val, 111 size_t size) 112 { 113 int rc; 114 115 if (!zl3073x_check_reg(zldev, reg, size)) 116 return -EINVAL; 117 118 /* Map the register address to virtual range */ 119 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; 120 121 rc = regmap_bulk_read(zldev->regmap, reg, val, size); 122 if (rc) { 123 dev_err(zldev->dev, "Failed to read reg 0x%04x: %pe\n", reg, 124 ERR_PTR(rc)); 125 return rc; 126 } 127 128 return 0; 129 } 130 131 static int 132 zl3073x_write_reg(struct zl3073x_dev *zldev, unsigned int reg, const void *val, 133 size_t size) 134 { 135 int rc; 136 137 if (!zl3073x_check_reg(zldev, reg, size)) 138 return -EINVAL; 139 140 /* Map the register address to virtual range */ 141 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; 142 143 rc = regmap_bulk_write(zldev->regmap, reg, val, size); 144 if (rc) { 145 dev_err(zldev->dev, "Failed to write reg 0x%04x: %pe\n", reg, 146 ERR_PTR(rc)); 147 return rc; 148 } 149 150 return 0; 151 } 152 153 /** 154 * zl3073x_read_u8 - read value from 8bit register 155 * @zldev: zl3073x device pointer 156 * @reg: register to write to 157 * @val: value to write 158 * 159 * Reads value from given 8bit register. 160 * 161 * Returns: 0 on success, <0 on error 162 */ 163 int zl3073x_read_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 *val) 164 { 165 return zl3073x_read_reg(zldev, reg, val, sizeof(*val)); 166 } 167 168 /** 169 * zl3073x_write_u8 - write value to 16bit register 170 * @zldev: zl3073x device pointer 171 * @reg: register to write to 172 * @val: value to write 173 * 174 * Writes value into given 8bit register. 175 * 176 * Returns: 0 on success, <0 on error 177 */ 178 int zl3073x_write_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 val) 179 { 180 return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); 181 } 182 183 /** 184 * zl3073x_read_u16 - read value from 16bit register 185 * @zldev: zl3073x device pointer 186 * @reg: register to write to 187 * @val: value to write 188 * 189 * Reads value from given 16bit register. 190 * 191 * Returns: 0 on success, <0 on error 192 */ 193 int zl3073x_read_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 *val) 194 { 195 int rc; 196 197 rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val)); 198 if (!rc) 199 be16_to_cpus(val); 200 201 return rc; 202 } 203 204 /** 205 * zl3073x_write_u16 - write value to 16bit register 206 * @zldev: zl3073x device pointer 207 * @reg: register to write to 208 * @val: value to write 209 * 210 * Writes value into given 16bit register. 211 * 212 * Returns: 0 on success, <0 on error 213 */ 214 int zl3073x_write_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 val) 215 { 216 cpu_to_be16s(&val); 217 218 return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); 219 } 220 221 /** 222 * zl3073x_read_u32 - read value from 32bit register 223 * @zldev: zl3073x device pointer 224 * @reg: register to write to 225 * @val: value to write 226 * 227 * Reads value from given 32bit register. 228 * 229 * Returns: 0 on success, <0 on error 230 */ 231 int zl3073x_read_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 *val) 232 { 233 int rc; 234 235 rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val)); 236 if (!rc) 237 be32_to_cpus(val); 238 239 return rc; 240 } 241 242 /** 243 * zl3073x_write_u32 - write value to 32bit register 244 * @zldev: zl3073x device pointer 245 * @reg: register to write to 246 * @val: value to write 247 * 248 * Writes value into given 32bit register. 249 * 250 * Returns: 0 on success, <0 on error 251 */ 252 int zl3073x_write_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 val) 253 { 254 cpu_to_be32s(&val); 255 256 return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); 257 } 258 259 /** 260 * zl3073x_read_u48 - read value from 48bit register 261 * @zldev: zl3073x device pointer 262 * @reg: register to write to 263 * @val: value to write 264 * 265 * Reads value from given 48bit register. 266 * 267 * Returns: 0 on success, <0 on error 268 */ 269 int zl3073x_read_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 *val) 270 { 271 u8 buf[6]; 272 int rc; 273 274 rc = zl3073x_read_reg(zldev, reg, buf, sizeof(buf)); 275 if (!rc) 276 *val = get_unaligned_be48(buf); 277 278 return rc; 279 } 280 281 /** 282 * zl3073x_write_u48 - write value to 48bit register 283 * @zldev: zl3073x device pointer 284 * @reg: register to write to 285 * @val: value to write 286 * 287 * Writes value into given 48bit register. 288 * The value must be from the interval -S48_MIN to U48_MAX. 289 * 290 * Returns: 0 on success, <0 on error 291 */ 292 int zl3073x_write_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 val) 293 { 294 u8 buf[6]; 295 296 /* Check the value belongs to <S48_MIN, U48_MAX> 297 * Any value >= S48_MIN has bits 47..63 set. 298 */ 299 if (val > GENMASK_ULL(47, 0) && val < GENMASK_ULL(63, 47)) { 300 dev_err(zldev->dev, "Value 0x%0llx out of range\n", val); 301 return -EINVAL; 302 } 303 304 put_unaligned_be48(val, buf); 305 306 return zl3073x_write_reg(zldev, reg, buf, sizeof(buf)); 307 } 308 309 /** 310 * zl3073x_poll_zero_u8 - wait for register to be cleared by device 311 * @zldev: zl3073x device pointer 312 * @reg: register to poll (has to be 8bit register) 313 * @mask: bit mask for polling 314 * 315 * Waits for bits specified by @mask in register @reg value to be cleared 316 * by the device. 317 * 318 * Returns: 0 on success, <0 on error 319 */ 320 int zl3073x_poll_zero_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 mask) 321 { 322 /* Register polling sleep & timeout */ 323 #define ZL_POLL_SLEEP_US 10 324 #define ZL_POLL_TIMEOUT_US 2000000 325 unsigned int val; 326 327 /* Check the register is 8bit */ 328 if (ZL_REG_SIZE(reg) != 1) { 329 dev_err(zldev->dev, "Invalid reg 0x%04lx size for polling\n", 330 ZL_REG_ADDR(reg)); 331 return -EINVAL; 332 } 333 334 /* Map the register address to virtual range */ 335 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; 336 337 return regmap_read_poll_timeout(zldev->regmap, reg, val, !(val & mask), 338 ZL_POLL_SLEEP_US, ZL_POLL_TIMEOUT_US); 339 } 340 341 int zl3073x_mb_op(struct zl3073x_dev *zldev, unsigned int op_reg, u8 op_val, 342 unsigned int mask_reg, u16 mask_val) 343 { 344 int rc; 345 346 /* Set mask for the operation */ 347 rc = zl3073x_write_u16(zldev, mask_reg, mask_val); 348 if (rc) 349 return rc; 350 351 /* Trigger the operation */ 352 rc = zl3073x_write_u8(zldev, op_reg, op_val); 353 if (rc) 354 return rc; 355 356 /* Wait for the operation to actually finish */ 357 return zl3073x_poll_zero_u8(zldev, op_reg, op_val); 358 } 359 360 /** 361 * zl3073x_do_hwreg_op - Perform HW register read/write operation 362 * @zldev: zl3073x device pointer 363 * @op: operation to perform 364 * 365 * Performs requested operation and waits for its completion. 366 * 367 * Return: 0 on success, <0 on error 368 */ 369 static int 370 zl3073x_do_hwreg_op(struct zl3073x_dev *zldev, u8 op) 371 { 372 int rc; 373 374 /* Set requested operation and set pending bit */ 375 rc = zl3073x_write_u8(zldev, ZL_REG_HWREG_OP, op | ZL_HWREG_OP_PENDING); 376 if (rc) 377 return rc; 378 379 /* Poll for completion - pending bit cleared */ 380 return zl3073x_poll_zero_u8(zldev, ZL_REG_HWREG_OP, 381 ZL_HWREG_OP_PENDING); 382 } 383 384 /** 385 * zl3073x_read_hwreg - Read HW register 386 * @zldev: zl3073x device pointer 387 * @addr: HW register address 388 * @value: Value of the HW register 389 * 390 * Reads HW register value and stores it into @value. 391 * 392 * Return: 0 on success, <0 on error 393 */ 394 int zl3073x_read_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 *value) 395 { 396 int rc; 397 398 /* Set address to read data from */ 399 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr); 400 if (rc) 401 return rc; 402 403 /* Perform the read operation */ 404 rc = zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_READ); 405 if (rc) 406 return rc; 407 408 /* Read the received data */ 409 return zl3073x_read_u32(zldev, ZL_REG_HWREG_READ_DATA, value); 410 } 411 412 /** 413 * zl3073x_write_hwreg - Write value to HW register 414 * @zldev: zl3073x device pointer 415 * @addr: HW registers address 416 * @value: Value to be written to HW register 417 * 418 * Stores the requested value into HW register. 419 * 420 * Return: 0 on success, <0 on error 421 */ 422 int zl3073x_write_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value) 423 { 424 int rc; 425 426 /* Set address to write data to */ 427 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr); 428 if (rc) 429 return rc; 430 431 /* Set data to be written */ 432 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_WRITE_DATA, value); 433 if (rc) 434 return rc; 435 436 /* Perform the write operation */ 437 return zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_WRITE); 438 } 439 440 /** 441 * zl3073x_update_hwreg - Update certain bits in HW register 442 * @zldev: zl3073x device pointer 443 * @addr: HW register address 444 * @value: Value to be written into HW register 445 * @mask: Bitmask indicating bits to be updated 446 * 447 * Reads given HW register, updates requested bits specified by value and 448 * mask and writes result back to HW register. 449 * 450 * Return: 0 on success, <0 on error 451 */ 452 int zl3073x_update_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value, 453 u32 mask) 454 { 455 u32 tmp; 456 int rc; 457 458 rc = zl3073x_read_hwreg(zldev, addr, &tmp); 459 if (rc) 460 return rc; 461 462 tmp &= ~mask; 463 tmp |= value & mask; 464 465 return zl3073x_write_hwreg(zldev, addr, tmp); 466 } 467 468 /** 469 * zl3073x_write_hwreg_seq - Write HW registers sequence 470 * @zldev: pointer to device structure 471 * @seq: pointer to first sequence item 472 * @num_items: number of items in sequence 473 * 474 * Writes given HW registers sequence. 475 * 476 * Return: 0 on success, <0 on error 477 */ 478 int zl3073x_write_hwreg_seq(struct zl3073x_dev *zldev, 479 const struct zl3073x_hwreg_seq_item *seq, 480 size_t num_items) 481 { 482 int i, rc = 0; 483 484 for (i = 0; i < num_items; i++) { 485 dev_dbg(zldev->dev, "Write 0x%0x [0x%0x] to 0x%0x", 486 seq[i].value, seq[i].mask, seq[i].addr); 487 488 if (seq[i].mask == U32_MAX) 489 /* Write value directly */ 490 rc = zl3073x_write_hwreg(zldev, seq[i].addr, 491 seq[i].value); 492 else 493 /* Update only bits specified by the mask */ 494 rc = zl3073x_update_hwreg(zldev, seq[i].addr, 495 seq[i].value, seq[i].mask); 496 if (rc) 497 return rc; 498 499 if (seq->wait) 500 msleep(seq->wait); 501 } 502 503 return rc; 504 } 505 506 static int 507 zl3073x_dev_state_fetch(struct zl3073x_dev *zldev) 508 { 509 int rc; 510 u8 i; 511 512 for (i = 0; i < ZL3073X_NUM_REFS; i++) { 513 rc = zl3073x_ref_state_fetch(zldev, i); 514 if (rc) { 515 dev_err(zldev->dev, 516 "Failed to fetch input state: %pe\n", 517 ERR_PTR(rc)); 518 return rc; 519 } 520 } 521 522 for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) { 523 rc = zl3073x_synth_state_fetch(zldev, i); 524 if (rc) { 525 dev_err(zldev->dev, 526 "Failed to fetch synth state: %pe\n", 527 ERR_PTR(rc)); 528 return rc; 529 } 530 } 531 532 for (i = 0; i < ZL3073X_NUM_OUTS; i++) { 533 rc = zl3073x_out_state_fetch(zldev, i); 534 if (rc) { 535 dev_err(zldev->dev, 536 "Failed to fetch output state: %pe\n", 537 ERR_PTR(rc)); 538 return rc; 539 } 540 } 541 542 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 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 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 */ 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 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 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 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 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 any DPLL has 766 * frequency monitoring enabled. 767 */ 768 list_for_each_entry(zldpll, &zldev->dplls, list) { 769 if (zldpll->freq_monitor) { 770 rc = zl3073x_ref_freq_meas_update(zldev); 771 if (rc) 772 dev_warn(zldev->dev, 773 "Failed to update measured frequencies: %pe\n", 774 ERR_PTR(rc)); 775 break; 776 } 777 } 778 779 /* Update references' fractional frequency offsets */ 780 rc = zl3073x_ref_ffo_update(zldev); 781 if (rc) 782 dev_warn(zldev->dev, 783 "Failed to update fractional frequency offsets: %pe\n", 784 ERR_PTR(rc)); 785 786 list_for_each_entry(zldpll, &zldev->dplls, list) 787 zl3073x_dpll_changes_check(zldpll); 788 789 /* Run twice a second */ 790 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 791 msecs_to_jiffies(500)); 792 } 793 794 int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor) 795 { 796 u8 dpll_meas_ctrl, value; 797 int rc; 798 799 /* Read DPLL phase measurement control register */ 800 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); 801 if (rc) 802 return rc; 803 804 /* Convert requested factor to register value */ 805 value = (factor + 1) & 0x0f; 806 807 /* Update phase measurement control register */ 808 FIELD_MODIFY(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, &dpll_meas_ctrl, value); 809 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); 810 if (rc) 811 return rc; 812 813 /* Save the new factor */ 814 zldev->phase_avg_factor = factor; 815 816 return 0; 817 } 818 819 /** 820 * zl3073x_dev_phase_meas_setup - setup phase offset measurement 821 * @zldev: pointer to zl3073x_dev structure 822 * 823 * Enable phase offset measurement block, set measurement averaging factor 824 * and enable DPLL-to-its-ref phase measurement for all DPLLs. 825 * 826 * Returns: 0 on success, <0 on error 827 */ 828 static int 829 zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev) 830 { 831 struct zl3073x_dpll *zldpll; 832 u8 dpll_meas_ctrl, mask = 0; 833 int rc; 834 835 /* Setup phase measurement averaging factor */ 836 rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor); 837 if (rc) 838 return rc; 839 840 /* Read DPLL phase measurement control register */ 841 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); 842 if (rc) 843 return rc; 844 845 /* Enable DPLL measurement block */ 846 dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN; 847 848 /* Update phase measurement control register */ 849 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); 850 if (rc) 851 return rc; 852 853 /* Enable DPLL-to-connected-ref measurement for each channel */ 854 list_for_each_entry(zldpll, &zldev->dplls, list) 855 mask |= BIT(zldpll->id); 856 857 return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask); 858 } 859 860 /** 861 * zl3073x_dev_start - Start normal operation 862 * @zldev: zl3073x device pointer 863 * @full: perform full initialization 864 * 865 * The function starts normal operation, which means registering all DPLLs and 866 * their pins, and starting monitoring. If full initialization is requested, 867 * the function additionally initializes the phase offset measurement block and 868 * fetches hardware-invariant parameters. 869 * 870 * Return: 0 on success, <0 on error 871 */ 872 int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full) 873 { 874 struct zl3073x_dpll *zldpll; 875 u8 info; 876 int rc; 877 878 rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info); 879 if (rc) { 880 dev_err(zldev->dev, "Failed to read device status info\n"); 881 return rc; 882 } 883 884 if (!FIELD_GET(ZL_INFO_READY, info)) { 885 /* The ready bit indicates that the firmware was successfully 886 * configured and is ready for normal operation. If it is 887 * cleared then the configuration stored in flash is wrong 888 * or missing. In this situation the driver will expose 889 * only devlink interface to give an opportunity to flash 890 * the correct config. 891 */ 892 dev_info(zldev->dev, 893 "FW not fully ready - missing or corrupted config\n"); 894 895 return 0; 896 } 897 898 if (full) { 899 /* Fetch device state */ 900 rc = zl3073x_dev_state_fetch(zldev); 901 if (rc) 902 return rc; 903 904 /* Setup phase offset measurement block */ 905 rc = zl3073x_dev_phase_meas_setup(zldev); 906 if (rc) { 907 dev_err(zldev->dev, 908 "Failed to setup phase measurement\n"); 909 return rc; 910 } 911 } 912 913 /* Register all DPLLs */ 914 list_for_each_entry(zldpll, &zldev->dplls, list) { 915 rc = zl3073x_dpll_register(zldpll); 916 if (rc) { 917 dev_err_probe(zldev->dev, rc, 918 "Failed to register DPLL%u\n", 919 zldpll->id); 920 return rc; 921 } 922 } 923 924 /* Perform initial firmware fine phase correction */ 925 rc = zl3073x_dpll_init_fine_phase_adjust(zldev); 926 if (rc) { 927 dev_err_probe(zldev->dev, rc, 928 "Failed to init fine phase correction\n"); 929 return rc; 930 } 931 932 /* Start monitoring */ 933 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0); 934 935 return 0; 936 } 937 938 /** 939 * zl3073x_dev_stop - Stop normal operation 940 * @zldev: zl3073x device pointer 941 * 942 * The function stops the normal operation that mean deregistration of all 943 * DPLLs and their pins and stop monitoring. 944 * 945 * Return: 0 on success, <0 on error 946 */ 947 void zl3073x_dev_stop(struct zl3073x_dev *zldev) 948 { 949 struct zl3073x_dpll *zldpll; 950 951 /* Stop monitoring */ 952 kthread_cancel_delayed_work_sync(&zldev->work); 953 954 /* Unregister all DPLLs */ 955 list_for_each_entry(zldpll, &zldev->dplls, list) { 956 if (zldpll->dpll_dev) 957 zl3073x_dpll_unregister(zldpll); 958 } 959 } 960 961 static void zl3073x_dev_dpll_fini(void *ptr) 962 { 963 struct zl3073x_dpll *zldpll, *next; 964 struct zl3073x_dev *zldev = ptr; 965 966 /* Stop monitoring and unregister DPLLs */ 967 zl3073x_dev_stop(zldev); 968 969 /* Destroy monitoring thread */ 970 if (zldev->kworker) { 971 kthread_destroy_worker(zldev->kworker); 972 zldev->kworker = NULL; 973 } 974 975 /* Free all DPLLs */ 976 list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) { 977 list_del(&zldpll->list); 978 zl3073x_dpll_free(zldpll); 979 } 980 } 981 982 static int 983 zl3073x_devm_dpll_init(struct zl3073x_dev *zldev) 984 { 985 struct kthread_worker *kworker; 986 struct zl3073x_dpll *zldpll; 987 unsigned int i; 988 int rc; 989 990 INIT_LIST_HEAD(&zldev->dplls); 991 992 /* Allocate all DPLLs */ 993 for (i = 0; i < zldev->info->num_channels; i++) { 994 zldpll = zl3073x_dpll_alloc(zldev, i); 995 if (IS_ERR(zldpll)) { 996 dev_err_probe(zldev->dev, PTR_ERR(zldpll), 997 "Failed to alloc DPLL%u\n", i); 998 rc = PTR_ERR(zldpll); 999 goto error; 1000 } 1001 1002 list_add_tail(&zldpll->list, &zldev->dplls); 1003 } 1004 1005 /* Initialize monitoring thread */ 1006 kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work); 1007 kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev)); 1008 if (IS_ERR(kworker)) { 1009 rc = PTR_ERR(kworker); 1010 goto error; 1011 } 1012 zldev->kworker = kworker; 1013 1014 /* Start normal operation */ 1015 rc = zl3073x_dev_start(zldev, true); 1016 if (rc) { 1017 dev_err_probe(zldev->dev, rc, "Failed to start device\n"); 1018 goto error; 1019 } 1020 1021 /* Add devres action to release DPLL related resources */ 1022 return devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev); 1023 1024 error: 1025 zl3073x_dev_dpll_fini(zldev); 1026 1027 return rc; 1028 } 1029 1030 /** 1031 * zl3073x_dev_probe - initialize zl3073x device 1032 * @zldev: pointer to zl3073x device 1033 * 1034 * Common initialization of zl3073x device structure. 1035 * 1036 * Returns: 0 on success, <0 on error 1037 */ 1038 int zl3073x_dev_probe(struct zl3073x_dev *zldev) 1039 { 1040 u16 id, revision, fw_ver; 1041 unsigned int i; 1042 u32 cfg_ver; 1043 int rc; 1044 1045 /* Read chip ID */ 1046 rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id); 1047 if (rc) 1048 return rc; 1049 1050 /* Detect chip variant */ 1051 for (i = 0; i < ARRAY_SIZE(zl3073x_chip_ids); i++) { 1052 if (zl3073x_chip_ids[i].id == id) 1053 break; 1054 } 1055 1056 if (i == ARRAY_SIZE(zl3073x_chip_ids)) 1057 return dev_err_probe(zldev->dev, -ENODEV, 1058 "Unknown chip ID: 0x%04x\n", id); 1059 1060 zldev->info = &zl3073x_chip_ids[i]; 1061 1062 /* Read revision, firmware version and custom config version */ 1063 rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision); 1064 if (rc) 1065 return rc; 1066 rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver); 1067 if (rc) 1068 return rc; 1069 rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver); 1070 if (rc) 1071 return rc; 1072 1073 dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id, 1074 revision, fw_ver); 1075 dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n", 1076 FIELD_GET(GENMASK(31, 24), cfg_ver), 1077 FIELD_GET(GENMASK(23, 16), cfg_ver), 1078 FIELD_GET(GENMASK(15, 8), cfg_ver), 1079 FIELD_GET(GENMASK(7, 0), cfg_ver)); 1080 1081 /* Generate random clock ID as the device has not such property that 1082 * could be used for this purpose. A user can later change this value 1083 * using devlink. 1084 */ 1085 zldev->clock_id = get_random_u64(); 1086 1087 /* Default phase offset averaging factor */ 1088 zldev->phase_avg_factor = 2; 1089 1090 /* Initialize mutex for operations where multiple reads, writes 1091 * and/or polls are required to be done atomically. 1092 */ 1093 rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock); 1094 if (rc) 1095 return dev_err_probe(zldev->dev, rc, 1096 "Failed to initialize mutex\n"); 1097 1098 /* Register DPLL channels */ 1099 rc = zl3073x_devm_dpll_init(zldev); 1100 if (rc) 1101 return rc; 1102 1103 /* Register the devlink instance and parameters */ 1104 rc = zl3073x_devlink_register(zldev); 1105 if (rc) 1106 return dev_err_probe(zldev->dev, rc, 1107 "Failed to register devlink instance\n"); 1108 1109 return 0; 1110 } 1111 EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X"); 1112 1113 MODULE_AUTHOR("Ivan Vecera <ivecera@redhat.com>"); 1114 MODULE_DESCRIPTION("Microchip ZL3073x core driver"); 1115 MODULE_LICENSE("GPL"); 1116