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_ffo_update - update reference fractional frequency offsets 636 * @zldev: pointer to zl3073x_dev structure 637 * 638 * The function asks device to update fractional frequency offsets latch 639 * registers the latest measured values, reads and stores them into 640 * 641 * Return: 0 on success, <0 on error 642 */ 643 static int 644 zl3073x_ref_ffo_update(struct zl3073x_dev *zldev) 645 { 646 int i, rc; 647 648 /* Per datasheet we have to wait for 'ref_freq_meas_ctrl' to be zero 649 * to ensure that the measured data are coherent. 650 */ 651 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, 652 ZL_REF_FREQ_MEAS_CTRL); 653 if (rc) 654 return rc; 655 656 /* Select all references for measurement */ 657 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_3_0, 658 GENMASK(7, 0)); /* REF0P..REF3N */ 659 if (rc) 660 return rc; 661 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_4, 662 GENMASK(1, 0)); /* REF4P..REF4N */ 663 if (rc) 664 return rc; 665 666 /* Request frequency offset measurement */ 667 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, 668 ZL_REF_FREQ_MEAS_CTRL_REF_FREQ_OFF); 669 if (rc) 670 return rc; 671 672 /* Wait for finish */ 673 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, 674 ZL_REF_FREQ_MEAS_CTRL); 675 if (rc) 676 return rc; 677 678 /* Read DPLL-to-REFx frequency offset measurements */ 679 for (i = 0; i < ZL3073X_NUM_REFS; i++) { 680 s32 value; 681 682 /* Read value stored in units of 2^-32 signed */ 683 rc = zl3073x_read_u32(zldev, ZL_REG_REF_FREQ(i), &value); 684 if (rc) 685 return rc; 686 687 /* Convert to ppt 688 * ffo = (10^12 * value) / 2^32 689 * ffo = ( 5^12 * value) / 2^20 690 */ 691 zldev->ref[i].ffo = mul_s64_u64_shr(value, 244140625, 20); 692 } 693 694 return 0; 695 } 696 697 static void 698 zl3073x_dev_periodic_work(struct kthread_work *work) 699 { 700 struct zl3073x_dev *zldev = container_of(work, struct zl3073x_dev, 701 work.work); 702 struct zl3073x_dpll *zldpll; 703 int rc; 704 705 /* Update input references' states */ 706 zl3073x_dev_ref_states_update(zldev); 707 708 /* Update DPLL channels' states */ 709 zl3073x_dev_chan_states_update(zldev); 710 711 /* Update DPLL-to-connected-ref phase offsets registers */ 712 rc = zl3073x_ref_phase_offsets_update(zldev, -1); 713 if (rc) 714 dev_warn(zldev->dev, "Failed to update phase offsets: %pe\n", 715 ERR_PTR(rc)); 716 717 /* Update references' fractional frequency offsets */ 718 rc = zl3073x_ref_ffo_update(zldev); 719 if (rc) 720 dev_warn(zldev->dev, 721 "Failed to update fractional frequency offsets: %pe\n", 722 ERR_PTR(rc)); 723 724 list_for_each_entry(zldpll, &zldev->dplls, list) 725 zl3073x_dpll_changes_check(zldpll); 726 727 /* Run twice a second */ 728 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 729 msecs_to_jiffies(500)); 730 } 731 732 int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor) 733 { 734 u8 dpll_meas_ctrl, value; 735 int rc; 736 737 /* Read DPLL phase measurement control register */ 738 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); 739 if (rc) 740 return rc; 741 742 /* Convert requested factor to register value */ 743 value = (factor + 1) & 0x0f; 744 745 /* Update phase measurement control register */ 746 dpll_meas_ctrl &= ~ZL_DPLL_MEAS_CTRL_AVG_FACTOR; 747 dpll_meas_ctrl |= FIELD_PREP(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, value); 748 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); 749 if (rc) 750 return rc; 751 752 /* Save the new factor */ 753 zldev->phase_avg_factor = factor; 754 755 return 0; 756 } 757 758 /** 759 * zl3073x_dev_phase_meas_setup - setup phase offset measurement 760 * @zldev: pointer to zl3073x_dev structure 761 * 762 * Enable phase offset measurement block, set measurement averaging factor 763 * and enable DPLL-to-its-ref phase measurement for all DPLLs. 764 * 765 * Returns: 0 on success, <0 on error 766 */ 767 static int 768 zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev) 769 { 770 struct zl3073x_dpll *zldpll; 771 u8 dpll_meas_ctrl, mask = 0; 772 int rc; 773 774 /* Setup phase measurement averaging factor */ 775 rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor); 776 if (rc) 777 return rc; 778 779 /* Read DPLL phase measurement control register */ 780 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); 781 if (rc) 782 return rc; 783 784 /* Enable DPLL measurement block */ 785 dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN; 786 787 /* Update phase measurement control register */ 788 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); 789 if (rc) 790 return rc; 791 792 /* Enable DPLL-to-connected-ref measurement for each channel */ 793 list_for_each_entry(zldpll, &zldev->dplls, list) 794 mask |= BIT(zldpll->id); 795 796 return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask); 797 } 798 799 /** 800 * zl3073x_dev_start - Start normal operation 801 * @zldev: zl3073x device pointer 802 * @full: perform full initialization 803 * 804 * The function starts normal operation, which means registering all DPLLs and 805 * their pins, and starting monitoring. If full initialization is requested, 806 * the function additionally initializes the phase offset measurement block and 807 * fetches hardware-invariant parameters. 808 * 809 * Return: 0 on success, <0 on error 810 */ 811 int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full) 812 { 813 struct zl3073x_dpll *zldpll; 814 u8 info; 815 int rc; 816 817 rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info); 818 if (rc) { 819 dev_err(zldev->dev, "Failed to read device status info\n"); 820 return rc; 821 } 822 823 if (!FIELD_GET(ZL_INFO_READY, info)) { 824 /* The ready bit indicates that the firmware was successfully 825 * configured and is ready for normal operation. If it is 826 * cleared then the configuration stored in flash is wrong 827 * or missing. In this situation the driver will expose 828 * only devlink interface to give an opportunity to flash 829 * the correct config. 830 */ 831 dev_info(zldev->dev, 832 "FW not fully ready - missing or corrupted config\n"); 833 834 return 0; 835 } 836 837 if (full) { 838 /* Fetch device state */ 839 rc = zl3073x_dev_state_fetch(zldev); 840 if (rc) 841 return rc; 842 843 /* Setup phase offset measurement block */ 844 rc = zl3073x_dev_phase_meas_setup(zldev); 845 if (rc) { 846 dev_err(zldev->dev, 847 "Failed to setup phase measurement\n"); 848 return rc; 849 } 850 } 851 852 /* Register all DPLLs */ 853 list_for_each_entry(zldpll, &zldev->dplls, list) { 854 rc = zl3073x_dpll_register(zldpll); 855 if (rc) { 856 dev_err_probe(zldev->dev, rc, 857 "Failed to register DPLL%u\n", 858 zldpll->id); 859 return rc; 860 } 861 } 862 863 /* Perform initial firmware fine phase correction */ 864 rc = zl3073x_dpll_init_fine_phase_adjust(zldev); 865 if (rc) { 866 dev_err_probe(zldev->dev, rc, 867 "Failed to init fine phase correction\n"); 868 return rc; 869 } 870 871 /* Start monitoring */ 872 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0); 873 874 return 0; 875 } 876 877 /** 878 * zl3073x_dev_stop - Stop normal operation 879 * @zldev: zl3073x device pointer 880 * 881 * The function stops the normal operation that mean deregistration of all 882 * DPLLs and their pins and stop monitoring. 883 * 884 * Return: 0 on success, <0 on error 885 */ 886 void zl3073x_dev_stop(struct zl3073x_dev *zldev) 887 { 888 struct zl3073x_dpll *zldpll; 889 890 /* Stop monitoring */ 891 kthread_cancel_delayed_work_sync(&zldev->work); 892 893 /* Unregister all DPLLs */ 894 list_for_each_entry(zldpll, &zldev->dplls, list) { 895 if (zldpll->dpll_dev) 896 zl3073x_dpll_unregister(zldpll); 897 } 898 } 899 900 static void zl3073x_dev_dpll_fini(void *ptr) 901 { 902 struct zl3073x_dpll *zldpll, *next; 903 struct zl3073x_dev *zldev = ptr; 904 905 /* Stop monitoring and unregister DPLLs */ 906 zl3073x_dev_stop(zldev); 907 908 /* Destroy monitoring thread */ 909 if (zldev->kworker) { 910 kthread_destroy_worker(zldev->kworker); 911 zldev->kworker = NULL; 912 } 913 914 /* Free all DPLLs */ 915 list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) { 916 list_del(&zldpll->list); 917 zl3073x_dpll_free(zldpll); 918 } 919 } 920 921 static int 922 zl3073x_devm_dpll_init(struct zl3073x_dev *zldev) 923 { 924 struct kthread_worker *kworker; 925 struct zl3073x_dpll *zldpll; 926 unsigned int i; 927 int rc; 928 929 INIT_LIST_HEAD(&zldev->dplls); 930 931 /* Allocate all DPLLs */ 932 for (i = 0; i < zldev->info->num_channels; i++) { 933 zldpll = zl3073x_dpll_alloc(zldev, i); 934 if (IS_ERR(zldpll)) { 935 dev_err_probe(zldev->dev, PTR_ERR(zldpll), 936 "Failed to alloc DPLL%u\n", i); 937 rc = PTR_ERR(zldpll); 938 goto error; 939 } 940 941 list_add_tail(&zldpll->list, &zldev->dplls); 942 } 943 944 /* Initialize monitoring thread */ 945 kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work); 946 kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev)); 947 if (IS_ERR(kworker)) { 948 rc = PTR_ERR(kworker); 949 goto error; 950 } 951 zldev->kworker = kworker; 952 953 /* Start normal operation */ 954 rc = zl3073x_dev_start(zldev, true); 955 if (rc) { 956 dev_err_probe(zldev->dev, rc, "Failed to start device\n"); 957 goto error; 958 } 959 960 /* Add devres action to release DPLL related resources */ 961 return devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev); 962 963 error: 964 zl3073x_dev_dpll_fini(zldev); 965 966 return rc; 967 } 968 969 /** 970 * zl3073x_dev_probe - initialize zl3073x device 971 * @zldev: pointer to zl3073x device 972 * 973 * Common initialization of zl3073x device structure. 974 * 975 * Returns: 0 on success, <0 on error 976 */ 977 int zl3073x_dev_probe(struct zl3073x_dev *zldev) 978 { 979 u16 id, revision, fw_ver; 980 unsigned int i; 981 u32 cfg_ver; 982 int rc; 983 984 /* Read chip ID */ 985 rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id); 986 if (rc) 987 return rc; 988 989 /* Detect chip variant */ 990 for (i = 0; i < ARRAY_SIZE(zl3073x_chip_ids); i++) { 991 if (zl3073x_chip_ids[i].id == id) 992 break; 993 } 994 995 if (i == ARRAY_SIZE(zl3073x_chip_ids)) 996 return dev_err_probe(zldev->dev, -ENODEV, 997 "Unknown chip ID: 0x%04x\n", id); 998 999 zldev->info = &zl3073x_chip_ids[i]; 1000 1001 /* Read revision, firmware version and custom config version */ 1002 rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision); 1003 if (rc) 1004 return rc; 1005 rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver); 1006 if (rc) 1007 return rc; 1008 rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver); 1009 if (rc) 1010 return rc; 1011 1012 dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id, 1013 revision, fw_ver); 1014 dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n", 1015 FIELD_GET(GENMASK(31, 24), cfg_ver), 1016 FIELD_GET(GENMASK(23, 16), cfg_ver), 1017 FIELD_GET(GENMASK(15, 8), cfg_ver), 1018 FIELD_GET(GENMASK(7, 0), cfg_ver)); 1019 1020 /* Generate random clock ID as the device has not such property that 1021 * could be used for this purpose. A user can later change this value 1022 * using devlink. 1023 */ 1024 zldev->clock_id = get_random_u64(); 1025 1026 /* Default phase offset averaging factor */ 1027 zldev->phase_avg_factor = 2; 1028 1029 /* Initialize mutex for operations where multiple reads, writes 1030 * and/or polls are required to be done atomically. 1031 */ 1032 rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock); 1033 if (rc) 1034 return dev_err_probe(zldev->dev, rc, 1035 "Failed to initialize mutex\n"); 1036 1037 /* Register DPLL channels */ 1038 rc = zl3073x_devm_dpll_init(zldev); 1039 if (rc) 1040 return rc; 1041 1042 /* Register the devlink instance and parameters */ 1043 rc = zl3073x_devlink_register(zldev); 1044 if (rc) 1045 return dev_err_probe(zldev->dev, rc, 1046 "Failed to register devlink instance\n"); 1047 1048 return 0; 1049 } 1050 EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X"); 1051 1052 MODULE_AUTHOR("Ivan Vecera <ivecera@redhat.com>"); 1053 MODULE_DESCRIPTION("Microchip ZL3073x core driver"); 1054 MODULE_LICENSE("GPL"); 1055