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(0x0E3B, 3, ZL3073X_FLAG_REF_PHASE_COMP_32), 29 ZL_CHIP_INFO(0x0E93, 1, ZL3073X_FLAG_REF_PHASE_COMP_32), 30 ZL_CHIP_INFO(0x0E94, 2, ZL3073X_FLAG_REF_PHASE_COMP_32), 31 ZL_CHIP_INFO(0x0E95, 3, ZL3073X_FLAG_REF_PHASE_COMP_32), 32 ZL_CHIP_INFO(0x0E96, 4, ZL3073X_FLAG_REF_PHASE_COMP_32), 33 ZL_CHIP_INFO(0x0E97, 5, ZL3073X_FLAG_REF_PHASE_COMP_32), 34 ZL_CHIP_INFO(0x1E93, 1, ZL3073X_FLAG_DIE_TEMP), 35 ZL_CHIP_INFO(0x1E94, 2, ZL3073X_FLAG_DIE_TEMP), 36 ZL_CHIP_INFO(0x1E95, 3, ZL3073X_FLAG_DIE_TEMP), 37 ZL_CHIP_INFO(0x1E96, 4, ZL3073X_FLAG_DIE_TEMP), 38 ZL_CHIP_INFO(0x1E97, 5, ZL3073X_FLAG_DIE_TEMP), 39 ZL_CHIP_INFO(0x1F60, 2, ZL3073X_FLAG_REF_PHASE_COMP_32), 40 ZL_CHIP_INFO(0x2E93, 1, ZL3073X_FLAG_DIE_TEMP), 41 ZL_CHIP_INFO(0x2E94, 2, ZL3073X_FLAG_DIE_TEMP), 42 ZL_CHIP_INFO(0x2E95, 3, ZL3073X_FLAG_DIE_TEMP), 43 ZL_CHIP_INFO(0x2E96, 4, ZL3073X_FLAG_DIE_TEMP), 44 ZL_CHIP_INFO(0x2E97, 5, ZL3073X_FLAG_DIE_TEMP), 45 ZL_CHIP_INFO(0x3FC4, 2, ZL3073X_FLAG_DIE_TEMP), 46 }; 47 48 #define ZL_RANGE_OFFSET 0x80 49 #define ZL_PAGE_SIZE 0x80 50 #define ZL_NUM_PAGES 256 51 #define ZL_PAGE_SEL 0x7F 52 #define ZL_PAGE_SEL_MASK GENMASK(7, 0) 53 #define ZL_NUM_REGS (ZL_NUM_PAGES * ZL_PAGE_SIZE) 54 55 /* Regmap range configuration */ 56 static const struct regmap_range_cfg zl3073x_regmap_range = { 57 .range_min = ZL_RANGE_OFFSET, 58 .range_max = ZL_RANGE_OFFSET + ZL_NUM_REGS - 1, 59 .selector_reg = ZL_PAGE_SEL, 60 .selector_mask = ZL_PAGE_SEL_MASK, 61 .selector_shift = 0, 62 .window_start = 0, 63 .window_len = ZL_PAGE_SIZE, 64 }; 65 66 static bool 67 zl3073x_is_volatile_reg(struct device *dev __maybe_unused, unsigned int reg) 68 { 69 /* Only page selector is non-volatile */ 70 return reg != ZL_PAGE_SEL; 71 } 72 73 const struct regmap_config zl3073x_regmap_config = { 74 .reg_bits = 8, 75 .val_bits = 8, 76 .max_register = ZL_RANGE_OFFSET + ZL_NUM_REGS - 1, 77 .ranges = &zl3073x_regmap_range, 78 .num_ranges = 1, 79 .cache_type = REGCACHE_MAPLE, 80 .volatile_reg = zl3073x_is_volatile_reg, 81 }; 82 EXPORT_SYMBOL_NS_GPL(zl3073x_regmap_config, "ZL3073X"); 83 84 static bool 85 zl3073x_check_reg(struct zl3073x_dev *zldev, unsigned int reg, size_t size) 86 { 87 /* Check that multiop lock is held when accessing registers 88 * from page 10 and above except the page 255 that does not 89 * need this protection. 90 */ 91 if (ZL_REG_PAGE(reg) >= 10 && ZL_REG_PAGE(reg) < 255) 92 lockdep_assert_held(&zldev->multiop_lock); 93 94 /* Check the index is in valid range for indexed register */ 95 if (ZL_REG_OFFSET(reg) > ZL_REG_MAX_OFFSET(reg)) { 96 dev_err(zldev->dev, "Index out of range for reg 0x%04lx\n", 97 ZL_REG_ADDR(reg)); 98 return false; 99 } 100 /* Check the requested size corresponds to register size */ 101 if (ZL_REG_SIZE(reg) != size) { 102 dev_err(zldev->dev, "Invalid size %zu for reg 0x%04lx\n", 103 size, ZL_REG_ADDR(reg)); 104 return false; 105 } 106 107 return true; 108 } 109 110 static int 111 zl3073x_read_reg(struct zl3073x_dev *zldev, unsigned int reg, void *val, 112 size_t size) 113 { 114 int rc; 115 116 if (!zl3073x_check_reg(zldev, reg, size)) 117 return -EINVAL; 118 119 /* Map the register address to virtual range */ 120 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; 121 122 rc = regmap_bulk_read(zldev->regmap, reg, val, size); 123 if (rc) { 124 dev_err(zldev->dev, "Failed to read reg 0x%04x: %pe\n", reg, 125 ERR_PTR(rc)); 126 return rc; 127 } 128 129 return 0; 130 } 131 132 static int 133 zl3073x_write_reg(struct zl3073x_dev *zldev, unsigned int reg, const void *val, 134 size_t size) 135 { 136 int rc; 137 138 if (!zl3073x_check_reg(zldev, reg, size)) 139 return -EINVAL; 140 141 /* Map the register address to virtual range */ 142 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; 143 144 rc = regmap_bulk_write(zldev->regmap, reg, val, size); 145 if (rc) { 146 dev_err(zldev->dev, "Failed to write reg 0x%04x: %pe\n", reg, 147 ERR_PTR(rc)); 148 return rc; 149 } 150 151 return 0; 152 } 153 154 /** 155 * zl3073x_read_u8 - read value from 8bit register 156 * @zldev: zl3073x device pointer 157 * @reg: register to write to 158 * @val: value to write 159 * 160 * Reads value from given 8bit register. 161 * 162 * Returns: 0 on success, <0 on error 163 */ 164 int zl3073x_read_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 *val) 165 { 166 return zl3073x_read_reg(zldev, reg, val, sizeof(*val)); 167 } 168 169 /** 170 * zl3073x_write_u8 - write value to 16bit register 171 * @zldev: zl3073x device pointer 172 * @reg: register to write to 173 * @val: value to write 174 * 175 * Writes value into given 8bit register. 176 * 177 * Returns: 0 on success, <0 on error 178 */ 179 int zl3073x_write_u8(struct zl3073x_dev *zldev, unsigned int reg, u8 val) 180 { 181 return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); 182 } 183 184 /** 185 * zl3073x_read_u16 - read value from 16bit register 186 * @zldev: zl3073x device pointer 187 * @reg: register to write to 188 * @val: value to write 189 * 190 * Reads value from given 16bit register. 191 * 192 * Returns: 0 on success, <0 on error 193 */ 194 int zl3073x_read_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 *val) 195 { 196 int rc; 197 198 rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val)); 199 if (!rc) 200 be16_to_cpus(val); 201 202 return rc; 203 } 204 205 /** 206 * zl3073x_write_u16 - write value to 16bit register 207 * @zldev: zl3073x device pointer 208 * @reg: register to write to 209 * @val: value to write 210 * 211 * Writes value into given 16bit register. 212 * 213 * Returns: 0 on success, <0 on error 214 */ 215 int zl3073x_write_u16(struct zl3073x_dev *zldev, unsigned int reg, u16 val) 216 { 217 cpu_to_be16s(&val); 218 219 return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); 220 } 221 222 /** 223 * zl3073x_read_u32 - read value from 32bit register 224 * @zldev: zl3073x device pointer 225 * @reg: register to write to 226 * @val: value to write 227 * 228 * Reads value from given 32bit register. 229 * 230 * Returns: 0 on success, <0 on error 231 */ 232 int zl3073x_read_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 *val) 233 { 234 int rc; 235 236 rc = zl3073x_read_reg(zldev, reg, val, sizeof(*val)); 237 if (!rc) 238 be32_to_cpus(val); 239 240 return rc; 241 } 242 243 /** 244 * zl3073x_write_u32 - write value to 32bit register 245 * @zldev: zl3073x device pointer 246 * @reg: register to write to 247 * @val: value to write 248 * 249 * Writes value into given 32bit register. 250 * 251 * Returns: 0 on success, <0 on error 252 */ 253 int zl3073x_write_u32(struct zl3073x_dev *zldev, unsigned int reg, u32 val) 254 { 255 cpu_to_be32s(&val); 256 257 return zl3073x_write_reg(zldev, reg, &val, sizeof(val)); 258 } 259 260 /** 261 * zl3073x_read_u48 - read value from 48bit register 262 * @zldev: zl3073x device pointer 263 * @reg: register to write to 264 * @val: value to write 265 * 266 * Reads value from given 48bit register. 267 * 268 * Returns: 0 on success, <0 on error 269 */ 270 int zl3073x_read_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 *val) 271 { 272 u8 buf[6]; 273 int rc; 274 275 rc = zl3073x_read_reg(zldev, reg, buf, sizeof(buf)); 276 if (!rc) 277 *val = get_unaligned_be48(buf); 278 279 return rc; 280 } 281 282 /** 283 * zl3073x_write_u48 - write value to 48bit register 284 * @zldev: zl3073x device pointer 285 * @reg: register to write to 286 * @val: value to write 287 * 288 * Writes value into given 48bit register. 289 * The value must be from the interval -S48_MIN to U48_MAX. 290 * 291 * Returns: 0 on success, <0 on error 292 */ 293 int zl3073x_write_u48(struct zl3073x_dev *zldev, unsigned int reg, u64 val) 294 { 295 u8 buf[6]; 296 297 /* Check the value belongs to <S48_MIN, U48_MAX> 298 * Any value >= S48_MIN has bits 47..63 set. 299 */ 300 if (val > GENMASK_ULL(47, 0) && val < GENMASK_ULL(63, 47)) { 301 dev_err(zldev->dev, "Value 0x%0llx out of range\n", val); 302 return -EINVAL; 303 } 304 305 put_unaligned_be48(val, buf); 306 307 return zl3073x_write_reg(zldev, reg, buf, sizeof(buf)); 308 } 309 310 /** 311 * zl3073x_poll_zero_u8 - wait for register to be cleared by device 312 * @zldev: zl3073x device pointer 313 * @reg: register to poll (has to be 8bit register) 314 * @mask: bit mask for polling 315 * @timeout_us: timeout in microseconds 316 * 317 * Waits for bits specified by @mask in register @reg value to be cleared 318 * by the device. 319 * 320 * Returns: 0 on success, <0 on error 321 */ 322 int zl3073x_poll_zero_u8(struct zl3073x_dev *zldev, unsigned int reg, 323 u8 mask, unsigned int timeout_us) 324 { 325 unsigned int sleep_us = timeout_us / 50; 326 unsigned int val; 327 328 /* Check the register is 8bit */ 329 if (ZL_REG_SIZE(reg) != 1) { 330 dev_err(zldev->dev, "Invalid reg 0x%04lx size for polling\n", 331 ZL_REG_ADDR(reg)); 332 return -EINVAL; 333 } 334 335 /* Map the register address to virtual range */ 336 reg = ZL_REG_ADDR(reg) + ZL_RANGE_OFFSET; 337 338 return regmap_read_poll_timeout(zldev->regmap, reg, val, !(val & mask), 339 sleep_us, timeout_us); 340 } 341 342 int zl3073x_mb_op(struct zl3073x_dev *zldev, unsigned int op_reg, u8 op_val, 343 unsigned int mask_reg, u16 mask_val) 344 { 345 int rc; 346 347 /* Set mask for the operation */ 348 rc = zl3073x_write_u16(zldev, mask_reg, mask_val); 349 if (rc) 350 return rc; 351 352 /* Trigger the operation */ 353 rc = zl3073x_write_u8(zldev, op_reg, op_val); 354 if (rc) 355 return rc; 356 357 /* Wait for the operation to actually finish */ 358 return zl3073x_poll_zero_u8(zldev, op_reg, op_val, 359 ZL_POLL_MB_TIMEOUT_US); 360 } 361 362 /** 363 * zl3073x_do_hwreg_op - Perform HW register read/write operation 364 * @zldev: zl3073x device pointer 365 * @op: operation to perform 366 * 367 * Performs requested operation and waits for its completion. 368 * 369 * Return: 0 on success, <0 on error 370 */ 371 static int 372 zl3073x_do_hwreg_op(struct zl3073x_dev *zldev, u8 op) 373 { 374 int rc; 375 376 /* Set requested operation and set pending bit */ 377 rc = zl3073x_write_u8(zldev, ZL_REG_HWREG_OP, op | ZL_HWREG_OP_PENDING); 378 if (rc) 379 return rc; 380 381 /* Poll for completion - pending bit cleared */ 382 return zl3073x_poll_zero_u8(zldev, ZL_REG_HWREG_OP, ZL_HWREG_OP_PENDING, 383 ZL_POLL_HWREG_TIMEOUT_US); 384 } 385 386 /** 387 * zl3073x_read_hwreg - Read HW register 388 * @zldev: zl3073x device pointer 389 * @addr: HW register address 390 * @value: Value of the HW register 391 * 392 * Reads HW register value and stores it into @value. 393 * 394 * Return: 0 on success, <0 on error 395 */ 396 int zl3073x_read_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 *value) 397 { 398 int rc; 399 400 /* Set address to read data from */ 401 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr); 402 if (rc) 403 return rc; 404 405 /* Perform the read operation */ 406 rc = zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_READ); 407 if (rc) 408 return rc; 409 410 /* Read the received data */ 411 return zl3073x_read_u32(zldev, ZL_REG_HWREG_READ_DATA, value); 412 } 413 414 /** 415 * zl3073x_write_hwreg - Write value to HW register 416 * @zldev: zl3073x device pointer 417 * @addr: HW registers address 418 * @value: Value to be written to HW register 419 * 420 * Stores the requested value into HW register. 421 * 422 * Return: 0 on success, <0 on error 423 */ 424 int zl3073x_write_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value) 425 { 426 int rc; 427 428 /* Set address to write data to */ 429 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_ADDR, addr); 430 if (rc) 431 return rc; 432 433 /* Set data to be written */ 434 rc = zl3073x_write_u32(zldev, ZL_REG_HWREG_WRITE_DATA, value); 435 if (rc) 436 return rc; 437 438 /* Perform the write operation */ 439 return zl3073x_do_hwreg_op(zldev, ZL_HWREG_OP_WRITE); 440 } 441 442 /** 443 * zl3073x_update_hwreg - Update certain bits in HW register 444 * @zldev: zl3073x device pointer 445 * @addr: HW register address 446 * @value: Value to be written into HW register 447 * @mask: Bitmask indicating bits to be updated 448 * 449 * Reads given HW register, updates requested bits specified by value and 450 * mask and writes result back to HW register. 451 * 452 * Return: 0 on success, <0 on error 453 */ 454 int zl3073x_update_hwreg(struct zl3073x_dev *zldev, u32 addr, u32 value, 455 u32 mask) 456 { 457 u32 tmp; 458 int rc; 459 460 rc = zl3073x_read_hwreg(zldev, addr, &tmp); 461 if (rc) 462 return rc; 463 464 tmp &= ~mask; 465 tmp |= value & mask; 466 467 return zl3073x_write_hwreg(zldev, addr, tmp); 468 } 469 470 /** 471 * zl3073x_write_hwreg_seq - Write HW registers sequence 472 * @zldev: pointer to device structure 473 * @seq: pointer to first sequence item 474 * @num_items: number of items in sequence 475 * 476 * Writes given HW registers sequence. 477 * 478 * Return: 0 on success, <0 on error 479 */ 480 int zl3073x_write_hwreg_seq(struct zl3073x_dev *zldev, 481 const struct zl3073x_hwreg_seq_item *seq, 482 size_t num_items) 483 { 484 int i, rc = 0; 485 486 for (i = 0; i < num_items; i++) { 487 dev_dbg(zldev->dev, "Write 0x%0x [0x%0x] to 0x%0x", 488 seq[i].value, seq[i].mask, seq[i].addr); 489 490 if (seq[i].mask == U32_MAX) 491 /* Write value directly */ 492 rc = zl3073x_write_hwreg(zldev, seq[i].addr, 493 seq[i].value); 494 else 495 /* Update only bits specified by the mask */ 496 rc = zl3073x_update_hwreg(zldev, seq[i].addr, 497 seq[i].value, seq[i].mask); 498 if (rc) 499 return rc; 500 501 if (seq->wait) 502 msleep(seq->wait); 503 } 504 505 return rc; 506 } 507 508 static int 509 zl3073x_dev_state_fetch(struct zl3073x_dev *zldev) 510 { 511 int rc; 512 u8 i; 513 514 rc = zl3073x_read_u16(zldev, ZL_REG_OUTPUT_STEP_TIME_MASK, 515 &zldev->out_step_time_mask); 516 if (rc) 517 return rc; 518 519 for (i = 0; i < ZL3073X_NUM_REFS; i++) { 520 rc = zl3073x_ref_state_fetch(zldev, i); 521 if (rc) { 522 dev_err(zldev->dev, 523 "Failed to fetch input state: %pe\n", 524 ERR_PTR(rc)); 525 return rc; 526 } 527 } 528 529 for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) { 530 rc = zl3073x_synth_state_fetch(zldev, i); 531 if (rc) { 532 dev_err(zldev->dev, 533 "Failed to fetch synth state: %pe\n", 534 ERR_PTR(rc)); 535 return rc; 536 } 537 } 538 539 for (i = 0; i < ZL3073X_NUM_OUTS; i++) { 540 rc = zl3073x_out_state_fetch(zldev, i); 541 if (rc) { 542 dev_err(zldev->dev, 543 "Failed to fetch output state: %pe\n", 544 ERR_PTR(rc)); 545 return rc; 546 } 547 } 548 549 for (i = 0; i < zldev->info->num_channels; i++) { 550 rc = zl3073x_chan_state_fetch(zldev, i); 551 if (rc) { 552 dev_err(zldev->dev, 553 "Failed to fetch channel state: %pe\n", 554 ERR_PTR(rc)); 555 return rc; 556 } 557 } 558 559 return rc; 560 } 561 562 static void 563 zl3073x_dev_ref_states_update(struct zl3073x_dev *zldev) 564 { 565 int i, rc; 566 567 for (i = 0; i < ZL3073X_NUM_REFS; i++) { 568 rc = zl3073x_ref_state_update(zldev, i); 569 if (rc) 570 dev_warn(zldev->dev, 571 "Failed to get REF%u status: %pe\n", i, 572 ERR_PTR(rc)); 573 } 574 } 575 576 577 578 /** 579 * zl3073x_ref_phase_offsets_update - update reference phase offsets 580 * @zldev: pointer to zl3073x_dev structure 581 * @channel: DPLL channel number or -1 582 * 583 * The function asks device to update phase offsets latch registers with 584 * the latest measured values. There are 2 sets of latch registers: 585 * 586 * 1) Up to 5 DPLL-to-connected-ref registers that contain phase offset 587 * values between particular DPLL channel and its *connected* input 588 * reference. 589 * 590 * 2) 10 selected-DPLL-to-all-ref registers that contain phase offset values 591 * between selected DPLL channel and all input references. 592 * 593 * If the caller is interested in 2) then it has to pass DPLL channel number 594 * in @channel parameter. If it is interested only in 1) then it should pass 595 * @channel parameter with value of -1. 596 * 597 * Return: 0 on success, <0 on error 598 */ 599 int zl3073x_ref_phase_offsets_update(struct zl3073x_dev *zldev, int channel) 600 { 601 int rc; 602 603 /* Per datasheet we have to wait for 'dpll_ref_phase_err_rqst_rd' 604 * to be zero to ensure that the measured data are coherent. 605 */ 606 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST, 607 ZL_REF_PHASE_ERR_READ_RQST_RD, 608 ZL_POLL_PHASE_ERR_TIMEOUT_US); 609 if (rc) 610 return rc; 611 612 /* Select DPLL channel if it is specified */ 613 if (channel != -1) { 614 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_IDX, channel); 615 if (rc) 616 return rc; 617 } 618 619 /* Request to update phase offsets measurement values */ 620 rc = zl3073x_write_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST, 621 ZL_REF_PHASE_ERR_READ_RQST_RD); 622 if (rc) 623 return rc; 624 625 /* Wait for finish */ 626 return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_PHASE_ERR_READ_RQST, 627 ZL_REF_PHASE_ERR_READ_RQST_RD, 628 ZL_POLL_PHASE_ERR_TIMEOUT_US); 629 } 630 631 /** 632 * zl3073x_ref_freq_meas_latch - latch reference frequency measurements 633 * @zldev: pointer to zl3073x_dev structure 634 * @type: measurement type (ZL_REF_FREQ_MEAS_CTRL_*) 635 * 636 * The function waits for the previous measurement to finish, selects all 637 * references and requests a new measurement of the given type. 638 * 639 * Return: 0 on success, <0 on error 640 */ 641 static int 642 zl3073x_ref_freq_meas_latch(struct zl3073x_dev *zldev, u8 type) 643 { 644 int rc; 645 646 /* Wait for previous measurement to finish */ 647 rc = zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, 648 ZL_REF_FREQ_MEAS_CTRL, 649 ZL_POLL_FREQ_MEAS_TIMEOUT_US); 650 if (rc) 651 return rc; 652 653 /* Select all references for measurement */ 654 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_3_0, 655 GENMASK(7, 0)); /* REF0P..REF3N */ 656 if (rc) 657 return rc; 658 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_MASK_4, 659 GENMASK(1, 0)); /* REF4P..REF4N */ 660 if (rc) 661 return rc; 662 663 /* Request measurement */ 664 rc = zl3073x_write_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, type); 665 if (rc) 666 return rc; 667 668 /* Wait for finish */ 669 return zl3073x_poll_zero_u8(zldev, ZL_REG_REF_FREQ_MEAS_CTRL, 670 ZL_REF_FREQ_MEAS_CTRL, 671 ZL_POLL_FREQ_MEAS_TIMEOUT_US); 672 } 673 674 /** 675 * zl3073x_ref_freq_meas_update - update measured input reference frequencies 676 * @zldev: pointer to zl3073x_dev structure 677 * 678 * The function asks device to latch measured input reference frequencies 679 * and stores the results in the ref state. 680 * 681 * Return: 0 on success, <0 on error 682 */ 683 static int 684 zl3073x_ref_freq_meas_update(struct zl3073x_dev *zldev) 685 { 686 int i, rc; 687 688 rc = zl3073x_ref_freq_meas_latch(zldev, ZL_REF_FREQ_MEAS_CTRL_REF_FREQ); 689 if (rc) 690 return rc; 691 692 /* Read measured frequencies in Hz (unsigned 32-bit, LSB = 1 Hz) */ 693 for (i = 0; i < ZL3073X_NUM_REFS; i++) { 694 u32 value; 695 696 rc = zl3073x_read_u32(zldev, ZL_REG_REF_FREQ(i), &value); 697 if (rc) 698 return rc; 699 700 zldev->ref[i].meas_freq = value; 701 } 702 703 return 0; 704 } 705 706 static void 707 zl3073x_dev_periodic_work(struct kthread_work *work) 708 { 709 struct zl3073x_dev *zldev = container_of(work, struct zl3073x_dev, 710 work.work); 711 struct zl3073x_dpll *zldpll; 712 int rc; 713 714 /* Update input references' states */ 715 zl3073x_dev_ref_states_update(zldev); 716 717 /* Update DPLL-to-connected-ref phase offsets registers */ 718 rc = zl3073x_ref_phase_offsets_update(zldev, -1); 719 if (rc) 720 dev_warn(zldev->dev, "Failed to update phase offsets: %pe\n", 721 ERR_PTR(rc)); 722 723 /* Update measured input reference frequencies if frequency 724 * monitoring is enabled. 725 */ 726 if (READ_ONCE(zldev->freq_monitor)) { 727 rc = zl3073x_ref_freq_meas_update(zldev); 728 if (rc) 729 dev_warn(zldev->dev, 730 "Failed to update measured frequencies: %pe\n", 731 ERR_PTR(rc)); 732 } 733 734 list_for_each_entry(zldpll, &zldev->dplls, list) 735 zl3073x_dpll_changes_check(zldpll); 736 737 /* Run twice a second */ 738 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 739 msecs_to_jiffies(500)); 740 } 741 742 int zl3073x_dev_phase_avg_factor_set(struct zl3073x_dev *zldev, u8 factor) 743 { 744 u8 dpll_meas_ctrl, value; 745 int rc; 746 747 /* Read DPLL phase measurement control register */ 748 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); 749 if (rc) 750 return rc; 751 752 /* Convert requested factor to register value */ 753 value = (factor + 1) & 0x0f; 754 755 /* Update phase measurement control register */ 756 FIELD_MODIFY(ZL_DPLL_MEAS_CTRL_AVG_FACTOR, &dpll_meas_ctrl, value); 757 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); 758 if (rc) 759 return rc; 760 761 /* Save the new factor */ 762 WRITE_ONCE(zldev->phase_avg_factor, factor); 763 764 return 0; 765 } 766 767 /** 768 * zl3073x_dev_phase_meas_setup - setup phase offset measurement 769 * @zldev: pointer to zl3073x_dev structure 770 * 771 * Enable phase offset measurement block, set measurement averaging factor 772 * and enable DPLL-to-its-ref phase measurement for all DPLLs. 773 * 774 * Returns: 0 on success, <0 on error 775 */ 776 static int 777 zl3073x_dev_phase_meas_setup(struct zl3073x_dev *zldev) 778 { 779 struct zl3073x_dpll *zldpll; 780 u8 dpll_meas_ctrl, mask = 0; 781 int rc; 782 783 /* Setup phase measurement averaging factor */ 784 rc = zl3073x_dev_phase_avg_factor_set(zldev, zldev->phase_avg_factor); 785 if (rc) 786 return rc; 787 788 /* Read DPLL phase measurement control register */ 789 rc = zl3073x_read_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, &dpll_meas_ctrl); 790 if (rc) 791 return rc; 792 793 /* Enable DPLL measurement block */ 794 dpll_meas_ctrl |= ZL_DPLL_MEAS_CTRL_EN; 795 796 /* Update phase measurement control register */ 797 rc = zl3073x_write_u8(zldev, ZL_REG_DPLL_MEAS_CTRL, dpll_meas_ctrl); 798 if (rc) 799 return rc; 800 801 /* Enable DPLL-to-connected-ref measurement for each channel */ 802 list_for_each_entry(zldpll, &zldev->dplls, list) 803 mask |= BIT(zldpll->id); 804 805 return zl3073x_write_u8(zldev, ZL_REG_DPLL_PHASE_ERR_READ_MASK, mask); 806 } 807 808 /** 809 * zl3073x_dev_start - Start normal operation 810 * @zldev: zl3073x device pointer 811 * @full: perform full initialization 812 * 813 * The function starts normal operation, which means registering all DPLLs and 814 * their pins, and starting monitoring. If full initialization is requested, 815 * the function additionally initializes the phase offset measurement block and 816 * fetches hardware-invariant parameters. 817 * 818 * Return: 0 on success, <0 on error 819 */ 820 int zl3073x_dev_start(struct zl3073x_dev *zldev, bool full) 821 { 822 struct zl3073x_dpll *zldpll; 823 u8 info; 824 int rc; 825 826 rc = zl3073x_read_u8(zldev, ZL_REG_INFO, &info); 827 if (rc) { 828 dev_err(zldev->dev, "Failed to read device status info\n"); 829 return rc; 830 } 831 832 if (!FIELD_GET(ZL_INFO_READY, info)) { 833 /* The ready bit indicates that the firmware was successfully 834 * configured and is ready for normal operation. If it is 835 * cleared then the configuration stored in flash is wrong 836 * or missing. In this situation the driver will expose 837 * only devlink interface to give an opportunity to flash 838 * the correct config. 839 */ 840 dev_info(zldev->dev, 841 "FW not fully ready - missing or corrupted config\n"); 842 843 return 0; 844 } 845 846 if (full) { 847 /* Fetch device state */ 848 rc = zl3073x_dev_state_fetch(zldev); 849 if (rc) 850 return rc; 851 852 /* Setup phase offset measurement block */ 853 rc = zl3073x_dev_phase_meas_setup(zldev); 854 if (rc) { 855 dev_err(zldev->dev, 856 "Failed to setup phase measurement\n"); 857 return rc; 858 } 859 } 860 861 /* Register all DPLLs */ 862 list_for_each_entry(zldpll, &zldev->dplls, list) { 863 rc = zl3073x_dpll_register(zldpll); 864 if (rc) { 865 dev_err_probe(zldev->dev, rc, 866 "Failed to register DPLL%u\n", 867 zldpll->id); 868 return rc; 869 } 870 } 871 872 /* Perform initial firmware fine phase correction */ 873 rc = zl3073x_dpll_init_fine_phase_adjust(zldev); 874 if (rc) { 875 dev_err_probe(zldev->dev, rc, 876 "Failed to init fine phase correction\n"); 877 return rc; 878 } 879 880 /* Start monitoring */ 881 kthread_queue_delayed_work(zldev->kworker, &zldev->work, 0); 882 883 return 0; 884 } 885 886 /** 887 * zl3073x_dev_stop - Stop normal operation 888 * @zldev: zl3073x device pointer 889 * 890 * The function stops the normal operation that mean deregistration of all 891 * DPLLs and their pins and stop monitoring. 892 * 893 * Return: 0 on success, <0 on error 894 */ 895 void zl3073x_dev_stop(struct zl3073x_dev *zldev) 896 { 897 struct zl3073x_dpll *zldpll; 898 899 /* Stop monitoring */ 900 kthread_cancel_delayed_work_sync(&zldev->work); 901 902 /* Unregister all DPLLs */ 903 list_for_each_entry(zldpll, &zldev->dplls, list) { 904 if (zldpll->dpll_dev) 905 zl3073x_dpll_unregister(zldpll); 906 } 907 } 908 909 static void zl3073x_dev_dpll_fini(void *ptr) 910 { 911 struct zl3073x_dpll *zldpll, *next; 912 struct zl3073x_dev *zldev = ptr; 913 914 /* Stop monitoring and unregister DPLLs */ 915 zl3073x_dev_stop(zldev); 916 917 /* Destroy monitoring thread */ 918 if (zldev->kworker) { 919 kthread_destroy_worker(zldev->kworker); 920 zldev->kworker = NULL; 921 } 922 923 /* Free all DPLLs */ 924 list_for_each_entry_safe(zldpll, next, &zldev->dplls, list) { 925 list_del(&zldpll->list); 926 zl3073x_dpll_free(zldpll); 927 } 928 } 929 930 static int 931 zl3073x_devm_dpll_init(struct zl3073x_dev *zldev) 932 { 933 struct kthread_worker *kworker; 934 struct zl3073x_dpll *zldpll; 935 unsigned int i; 936 int rc; 937 938 INIT_LIST_HEAD(&zldev->dplls); 939 940 /* Allocate all DPLLs */ 941 for (i = 0; i < zldev->info->num_channels; i++) { 942 zldpll = zl3073x_dpll_alloc(zldev, i); 943 if (IS_ERR(zldpll)) { 944 dev_err_probe(zldev->dev, PTR_ERR(zldpll), 945 "Failed to alloc DPLL%u\n", i); 946 rc = PTR_ERR(zldpll); 947 goto error; 948 } 949 950 list_add_tail(&zldpll->list, &zldev->dplls); 951 } 952 953 /* Initialize monitoring thread */ 954 kthread_init_delayed_work(&zldev->work, zl3073x_dev_periodic_work); 955 kworker = kthread_run_worker(0, "zl3073x-%s", dev_name(zldev->dev)); 956 if (IS_ERR(kworker)) { 957 rc = PTR_ERR(kworker); 958 goto error; 959 } 960 zldev->kworker = kworker; 961 962 /* Start normal operation */ 963 rc = zl3073x_dev_start(zldev, true); 964 if (rc) { 965 dev_err_probe(zldev->dev, rc, "Failed to start device\n"); 966 goto error; 967 } 968 969 /* Add devres action to release DPLL related resources */ 970 return devm_add_action_or_reset(zldev->dev, zl3073x_dev_dpll_fini, zldev); 971 972 error: 973 zl3073x_dev_dpll_fini(zldev); 974 975 return rc; 976 } 977 978 /** 979 * zl3073x_dev_probe - initialize zl3073x device 980 * @zldev: pointer to zl3073x device 981 * 982 * Common initialization of zl3073x device structure. 983 * 984 * Returns: 0 on success, <0 on error 985 */ 986 int zl3073x_dev_probe(struct zl3073x_dev *zldev) 987 { 988 u16 id, revision, fw_ver; 989 unsigned int i; 990 u32 cfg_ver; 991 int rc; 992 993 /* Read chip ID */ 994 rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id); 995 if (rc) 996 return rc; 997 998 /* Detect chip variant */ 999 for (i = 0; i < ARRAY_SIZE(zl3073x_chip_ids); i++) { 1000 if (zl3073x_chip_ids[i].id == id) 1001 break; 1002 } 1003 1004 if (i == ARRAY_SIZE(zl3073x_chip_ids)) 1005 return dev_err_probe(zldev->dev, -ENODEV, 1006 "Unknown chip ID: 0x%04x\n", id); 1007 1008 zldev->info = &zl3073x_chip_ids[i]; 1009 1010 /* Read revision, firmware version and custom config version */ 1011 rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision); 1012 if (rc) 1013 return rc; 1014 rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver); 1015 if (rc) 1016 return rc; 1017 rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver); 1018 if (rc) 1019 return rc; 1020 1021 dev_dbg(zldev->dev, "ChipID(%X), ChipRev(%X), FwVer(%u)\n", id, 1022 revision, fw_ver); 1023 dev_dbg(zldev->dev, "Custom config version: %lu.%lu.%lu.%lu\n", 1024 FIELD_GET(GENMASK(31, 24), cfg_ver), 1025 FIELD_GET(GENMASK(23, 16), cfg_ver), 1026 FIELD_GET(GENMASK(15, 8), cfg_ver), 1027 FIELD_GET(GENMASK(7, 0), cfg_ver)); 1028 1029 /* Generate random clock ID as the device has not such property that 1030 * could be used for this purpose. A user can later change this value 1031 * using devlink. 1032 */ 1033 zldev->clock_id = get_random_u64(); 1034 1035 /* Default phase offset averaging factor */ 1036 zldev->phase_avg_factor = 2; 1037 1038 /* Initialize mutex for operations where multiple reads, writes 1039 * and/or polls are required to be done atomically. 1040 */ 1041 rc = devm_mutex_init(zldev->dev, &zldev->multiop_lock); 1042 if (rc) 1043 return dev_err_probe(zldev->dev, rc, 1044 "Failed to initialize mutex\n"); 1045 rc = devm_mutex_init(zldev->dev, &zldev->phase_step_lock); 1046 if (rc) 1047 return dev_err_probe(zldev->dev, rc, 1048 "Failed to initialize mutex\n"); 1049 rc = devm_mutex_init(zldev->dev, &zldev->tie_lock); 1050 if (rc) 1051 return dev_err_probe(zldev->dev, rc, 1052 "Failed to initialize mutex\n"); 1053 1054 /* Register DPLL channels */ 1055 rc = zl3073x_devm_dpll_init(zldev); 1056 if (rc) 1057 return rc; 1058 1059 /* Register the devlink instance and parameters */ 1060 rc = zl3073x_devlink_register(zldev); 1061 if (rc) 1062 return dev_err_probe(zldev->dev, rc, 1063 "Failed to register devlink instance\n"); 1064 1065 return 0; 1066 } 1067 EXPORT_SYMBOL_NS_GPL(zl3073x_dev_probe, "ZL3073X"); 1068 1069 MODULE_AUTHOR("Ivan Vecera <ivecera@redhat.com>"); 1070 MODULE_DESCRIPTION("Microchip ZL3073x core driver"); 1071 MODULE_LICENSE("GPL"); 1072