1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/bits.h> 4 #include <linux/bitfield.h> 5 #include <linux/cleanup.h> 6 #include <linux/bug.h> 7 #include <linux/container_of.h> 8 #include <linux/dev_printk.h> 9 #include <linux/dpll.h> 10 #include <linux/err.h> 11 #include <linux/kthread.h> 12 #include <linux/math64.h> 13 #include <linux/module.h> 14 #include <linux/netlink.h> 15 #include <linux/platform_device.h> 16 #include <linux/property.h> 17 #include <linux/ptp_clock_kernel.h> 18 #include <linux/slab.h> 19 #include <linux/sprintf.h> 20 21 #include "core.h" 22 #include "dpll.h" 23 #include "prop.h" 24 #include "regs.h" 25 26 #define ZL3073X_DPLL_REF_NONE ZL3073X_NUM_REFS 27 #define ZL3073X_DPLL_REF_IS_VALID(_ref) ((_ref) != ZL3073X_DPLL_REF_NONE) 28 29 /** 30 * struct zl3073x_dpll_pin - DPLL pin 31 * @list: this DPLL pin list entry 32 * @dpll: DPLL the pin is registered to 33 * @dpll_pin: pointer to registered dpll_pin 34 * @tracker: tracking object for the acquired reference 35 * @fwnode: firmware node handle 36 * @label: package label 37 * @dir: pin direction 38 * @id: pin id 39 * @prio: pin priority <0, 14> 40 * @esync_control: embedded sync is controllable 41 * @phase_gran: phase adjustment granularity 42 * @operstate: last saved operational state 43 * @phase_offset: last saved pin phase offset 44 * @freq_offset: last saved fractional frequency offset 45 * @measured_freq: last saved measured frequency 46 */ 47 struct zl3073x_dpll_pin { 48 struct list_head list; 49 struct zl3073x_dpll *dpll; 50 struct dpll_pin *dpll_pin; 51 dpll_tracker tracker; 52 struct fwnode_handle *fwnode; 53 char label[8]; 54 enum dpll_pin_direction dir; 55 u8 id; 56 u8 prio; 57 bool esync_control; 58 s32 phase_gran; 59 enum dpll_pin_operstate operstate; 60 s64 phase_offset; 61 s64 freq_offset; 62 u32 measured_freq; 63 }; 64 65 /* 66 * Supported esync ranges for input and for output per output pair type 67 */ 68 static const struct dpll_pin_frequency esync_freq_ranges[] = { 69 DPLL_PIN_FREQUENCY_RANGE(0, 1), 70 }; 71 72 /** 73 * zl3073x_dpll_is_input_pin - check if the pin is input one 74 * @pin: pin to check 75 * 76 * Return: true if pin is input, false if pin is output. 77 */ 78 static bool 79 zl3073x_dpll_is_input_pin(struct zl3073x_dpll_pin *pin) 80 { 81 return pin->dir == DPLL_PIN_DIRECTION_INPUT; 82 } 83 84 /** 85 * zl3073x_dpll_is_nco_pin - check if the pin is a virtual NCO pin 86 * @pin: pin to check 87 * 88 * Return: true if pin is a virtual NCO pin, false otherwise. 89 */ 90 static bool 91 zl3073x_dpll_is_nco_pin(struct zl3073x_dpll_pin *pin) 92 { 93 return pin->id == ZL3073X_NCO_PIN_ID; 94 } 95 96 /** 97 * zl3073x_dpll_is_p_pin - check if the pin is P-pin 98 * @pin: pin to check 99 * 100 * Return: true if the pin is P-pin, false if it is N-pin 101 */ 102 static bool 103 zl3073x_dpll_is_p_pin(struct zl3073x_dpll_pin *pin) 104 { 105 return zl3073x_is_p_pin(pin->id); 106 } 107 108 static int 109 zl3073x_dpll_pin_direction_get(const struct dpll_pin *dpll_pin, void *pin_priv, 110 const struct dpll_device *dpll, void *dpll_priv, 111 enum dpll_pin_direction *direction, 112 struct netlink_ext_ack *extack) 113 { 114 struct zl3073x_dpll_pin *pin = pin_priv; 115 116 *direction = pin->dir; 117 118 return 0; 119 } 120 121 static struct zl3073x_dpll_pin * 122 zl3073x_dpll_pin_get_by_ref(struct zl3073x_dpll *zldpll, u8 ref_id) 123 { 124 struct zl3073x_dpll_pin *pin; 125 126 list_for_each_entry(pin, &zldpll->pins, list) { 127 if (zl3073x_dpll_is_input_pin(pin) && 128 zl3073x_input_pin_ref_get(pin->id) == ref_id) 129 return pin; 130 } 131 132 return NULL; 133 } 134 135 static struct zl3073x_dpll_pin * 136 zl3073x_dpll_nco_pin_get(struct zl3073x_dpll *zldpll) 137 { 138 struct zl3073x_dpll_pin *pin; 139 140 list_for_each_entry(pin, &zldpll->pins, list) { 141 if (zl3073x_dpll_is_nco_pin(pin)) 142 return pin; 143 } 144 145 return NULL; 146 } 147 148 static int 149 zl3073x_dpll_input_pin_esync_get(const struct dpll_pin *dpll_pin, 150 void *pin_priv, 151 const struct dpll_device *dpll, 152 void *dpll_priv, 153 struct dpll_pin_esync *esync, 154 struct netlink_ext_ack *extack) 155 { 156 struct zl3073x_dpll *zldpll = dpll_priv; 157 struct zl3073x_dev *zldev = zldpll->dev; 158 struct zl3073x_dpll_pin *pin = pin_priv; 159 const struct zl3073x_ref *ref; 160 u8 ref_id; 161 162 guard(mutex)(&zldpll->lock); 163 164 ref_id = zl3073x_input_pin_ref_get(pin->id); 165 ref = zl3073x_ref_state_get(zldev, ref_id); 166 167 if (!pin->esync_control || zl3073x_ref_freq_get(ref) <= 1) 168 return -EOPNOTSUPP; 169 170 esync->range = esync_freq_ranges; 171 esync->range_num = ARRAY_SIZE(esync_freq_ranges); 172 173 switch (zl3073x_ref_sync_mode_get(ref)) { 174 case ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75: 175 esync->freq = ref->esync_n_div == ZL_REF_ESYNC_DIV_1HZ ? 1 : 0; 176 esync->pulse = 25; 177 break; 178 default: 179 esync->freq = 0; 180 esync->pulse = 0; 181 break; 182 } 183 184 return 0; 185 } 186 187 static int 188 zl3073x_dpll_input_pin_esync_set(const struct dpll_pin *dpll_pin, 189 void *pin_priv, 190 const struct dpll_device *dpll, 191 void *dpll_priv, u64 freq, 192 struct netlink_ext_ack *extack) 193 { 194 struct zl3073x_dpll *zldpll = dpll_priv; 195 struct zl3073x_dev *zldev = zldpll->dev; 196 struct zl3073x_dpll_pin *pin = pin_priv; 197 struct zl3073x_ref ref; 198 u8 ref_id, sync_mode; 199 200 guard(mutex)(&zldpll->lock); 201 202 ref_id = zl3073x_input_pin_ref_get(pin->id); 203 ref = *zl3073x_ref_state_get(zldev, ref_id); 204 205 /* Use freq == 0 to disable esync */ 206 if (!freq) 207 sync_mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF; 208 else 209 sync_mode = ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75; 210 211 zl3073x_ref_sync_mode_set(&ref, sync_mode); 212 213 if (freq) { 214 /* 1 Hz is only supported frequency now */ 215 ref.esync_n_div = ZL_REF_ESYNC_DIV_1HZ; 216 } 217 218 /* Update reference configuration */ 219 return zl3073x_ref_state_set(zldev, ref_id, &ref); 220 } 221 222 static int 223 zl3073x_dpll_input_pin_ref_sync_get(const struct dpll_pin *dpll_pin, 224 void *pin_priv, 225 const struct dpll_pin *ref_sync_pin, 226 void *ref_sync_pin_priv, 227 enum dpll_pin_state *state, 228 struct netlink_ext_ack *extack) 229 { 230 struct zl3073x_dpll_pin *sync_pin = ref_sync_pin_priv; 231 struct zl3073x_dpll_pin *pin = pin_priv; 232 struct zl3073x_dpll *zldpll = pin->dpll; 233 struct zl3073x_dev *zldev = zldpll->dev; 234 const struct zl3073x_ref *ref; 235 u8 ref_id, mode, pair; 236 237 guard(mutex)(&zldpll->lock); 238 239 ref_id = zl3073x_input_pin_ref_get(pin->id); 240 ref = zl3073x_ref_state_get(zldev, ref_id); 241 mode = zl3073x_ref_sync_mode_get(ref); 242 pair = zl3073x_ref_sync_pair_get(ref); 243 244 if (mode == ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR && 245 pair == zl3073x_input_pin_ref_get(sync_pin->id)) 246 *state = DPLL_PIN_STATE_CONNECTED; 247 else 248 *state = DPLL_PIN_STATE_DISCONNECTED; 249 250 return 0; 251 } 252 253 static int 254 zl3073x_dpll_input_pin_ref_sync_set(const struct dpll_pin *dpll_pin, 255 void *pin_priv, 256 const struct dpll_pin *ref_sync_pin, 257 void *ref_sync_pin_priv, 258 const enum dpll_pin_state state, 259 struct netlink_ext_ack *extack) 260 { 261 struct zl3073x_dpll_pin *sync_pin = ref_sync_pin_priv; 262 struct zl3073x_dpll_pin *pin = pin_priv; 263 struct zl3073x_dpll *zldpll = pin->dpll; 264 struct zl3073x_dev *zldev = zldpll->dev; 265 u8 mode, ref_id, sync_ref_id; 266 struct zl3073x_chan chan; 267 struct zl3073x_ref ref; 268 bool sync_ntf = false; 269 int rc; 270 271 mutex_lock(&zldpll->lock); 272 273 ref_id = zl3073x_input_pin_ref_get(pin->id); 274 sync_ref_id = zl3073x_input_pin_ref_get(sync_pin->id); 275 ref = *zl3073x_ref_state_get(zldev, ref_id); 276 277 if (state == DPLL_PIN_STATE_CONNECTED) { 278 const struct zl3073x_ref *sync_ref; 279 u32 ref_freq, sync_freq; 280 281 sync_ref = zl3073x_ref_state_get(zldev, sync_ref_id); 282 ref_freq = zl3073x_ref_freq_get(&ref); 283 sync_freq = zl3073x_ref_freq_get(sync_ref); 284 285 /* Sync signal must be 8 kHz or less and clock reference 286 * must be 1 kHz or more and higher than the sync signal. 287 */ 288 if (sync_freq > 8000) { 289 NL_SET_ERR_MSG(extack, 290 "sync frequency must be 8 kHz or less"); 291 rc = -EINVAL; 292 goto unlock; 293 } 294 if (ref_freq < 1000) { 295 NL_SET_ERR_MSG(extack, 296 "clock frequency must be 1 kHz or more"); 297 rc = -EINVAL; 298 goto unlock; 299 } 300 if (ref_freq <= sync_freq) { 301 NL_SET_ERR_MSG(extack, 302 "clock frequency must be higher than sync frequency"); 303 rc = -EINVAL; 304 goto unlock; 305 } 306 307 zl3073x_ref_sync_pair_set(&ref, sync_ref_id); 308 mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR; 309 } else { 310 mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF; 311 } 312 313 zl3073x_ref_sync_mode_set(&ref, mode); 314 315 rc = zl3073x_ref_state_set(zldev, ref_id, &ref); 316 if (rc) 317 goto unlock; 318 319 /* All code paths accessing per-channel reference priorities are 320 * serialized by the subsystem dpll_lock, so it is safe to release 321 * our lock here before iterating over the other channels. 322 */ 323 mutex_unlock(&zldpll->lock); 324 325 if (state != DPLL_PIN_STATE_CONNECTED) 326 return 0; 327 328 /* The datasheet recommends excluding the sync source from automatic 329 * reference selection by setting its priority to NONE on all DPLL 330 * channels. This is advisory - the ref sync pair is already 331 * configured, so a failure here is not fatal. On disconnect the 332 * priority is left as NONE and the user must explicitly make the 333 * pin selectable again. 334 */ 335 list_for_each_entry(zldpll, &zldev->dplls, list) { 336 u8 prio; 337 338 mutex_lock(&zldpll->lock); 339 340 chan = *zl3073x_chan_state_get(zldev, zldpll->id); 341 prio = zl3073x_chan_ref_prio_get(&chan, sync_ref_id); 342 if (prio == ZL_DPLL_REF_PRIO_NONE) { 343 mutex_unlock(&zldpll->lock); 344 continue; /* Ref is already non-selectable */ 345 } 346 347 zl3073x_chan_ref_prio_set(&chan, sync_ref_id, 348 ZL_DPLL_REF_PRIO_NONE); 349 if (zl3073x_chan_state_set(zldev, zldpll->id, &chan)) 350 dev_warn(zldev->dev, 351 "Failed to set ref prio on DPLL%u\n", 352 zldpll->id); 353 else 354 sync_ntf = true; 355 356 mutex_unlock(&zldpll->lock); 357 } 358 if (sync_ntf) 359 __dpll_pin_change_ntf(sync_pin->dpll_pin); 360 361 return 0; 362 unlock: 363 mutex_unlock(&zldpll->lock); 364 return rc; 365 } 366 367 static int 368 zl3073x_dpll_input_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv, 369 const struct dpll_device *dpll, void *dpll_priv, 370 struct dpll_ffo_param *ffo, 371 struct netlink_ext_ack *extack) 372 { 373 struct zl3073x_dpll *zldpll = dpll_priv; 374 struct zl3073x_dpll_pin *pin = pin_priv; 375 376 guard(mutex)(&zldpll->lock); 377 378 if (pin->operstate != DPLL_PIN_OPERSTATE_ACTIVE) 379 return -ENODATA; 380 381 ffo->ffo = pin->freq_offset; 382 383 return 0; 384 } 385 386 static int 387 zl3073x_dpll_input_pin_measured_freq_get(const struct dpll_pin *dpll_pin, 388 void *pin_priv, 389 const struct dpll_device *dpll, 390 void *dpll_priv, u64 *measured_freq, 391 struct netlink_ext_ack *extack) 392 { 393 struct zl3073x_dpll *zldpll = dpll_priv; 394 struct zl3073x_dpll_pin *pin = pin_priv; 395 396 guard(mutex)(&zldpll->lock); 397 398 *measured_freq = pin->measured_freq; 399 *measured_freq *= DPLL_PIN_MEASURED_FREQUENCY_DIVIDER; 400 401 return 0; 402 } 403 404 static int 405 zl3073x_dpll_input_pin_frequency_get(const struct dpll_pin *dpll_pin, 406 void *pin_priv, 407 const struct dpll_device *dpll, 408 void *dpll_priv, u64 *frequency, 409 struct netlink_ext_ack *extack) 410 { 411 struct zl3073x_dpll *zldpll = dpll_priv; 412 struct zl3073x_dpll_pin *pin = pin_priv; 413 u8 ref_id; 414 415 guard(mutex)(&zldpll->lock); 416 417 ref_id = zl3073x_input_pin_ref_get(pin->id); 418 *frequency = zl3073x_dev_ref_freq_get(zldpll->dev, ref_id); 419 420 return 0; 421 } 422 423 static int 424 zl3073x_dpll_input_pin_frequency_set(const struct dpll_pin *dpll_pin, 425 void *pin_priv, 426 const struct dpll_device *dpll, 427 void *dpll_priv, u64 frequency, 428 struct netlink_ext_ack *extack) 429 { 430 struct zl3073x_dpll *zldpll = dpll_priv; 431 struct zl3073x_dev *zldev = zldpll->dev; 432 struct zl3073x_dpll_pin *pin = pin_priv; 433 struct zl3073x_ref ref; 434 u8 ref_id; 435 436 guard(mutex)(&zldpll->lock); 437 438 /* Get reference state */ 439 ref_id = zl3073x_input_pin_ref_get(pin->id); 440 ref = *zl3073x_ref_state_get(zldev, ref_id); 441 442 /* Update frequency */ 443 zl3073x_ref_freq_set(&ref, frequency); 444 445 /* Commit reference state */ 446 return zl3073x_ref_state_set(zldev, ref_id, &ref); 447 } 448 449 /** 450 * zl3073x_dpll_connected_ref_get - get currently connected reference 451 * @zldpll: pointer to zl3073x_dpll 452 * 453 * Looks for currently connected reference the DPLL is locked to. 454 * 455 * Return: reference index if locked, ZL3073X_DPLL_REF_NONE otherwise 456 */ 457 static u8 458 zl3073x_dpll_connected_ref_get(struct zl3073x_dpll *zldpll) 459 { 460 const struct zl3073x_chan *chan = zl3073x_chan_state_get(zldpll->dev, 461 zldpll->id); 462 u8 state; 463 464 /* A reference is connected only when the DPLL is locked to it */ 465 state = zl3073x_chan_refsel_state_get(chan); 466 if (state == ZL_DPLL_REFSEL_STATUS_STATE_LOCK) 467 return zl3073x_chan_refsel_ref_get(chan); 468 469 return ZL3073X_DPLL_REF_NONE; 470 } 471 472 static int 473 zl3073x_dpll_input_pin_phase_offset_get(const struct dpll_pin *dpll_pin, 474 void *pin_priv, 475 const struct dpll_device *dpll, 476 void *dpll_priv, s64 *phase_offset, 477 struct netlink_ext_ack *extack) 478 { 479 struct zl3073x_dpll *zldpll = dpll_priv; 480 struct zl3073x_dev *zldev = zldpll->dev; 481 struct zl3073x_dpll_pin *pin = pin_priv; 482 const struct zl3073x_ref *ref; 483 u8 conn_id, ref_id; 484 s64 ref_phase; 485 486 guard(mutex)(&zldpll->lock); 487 488 /* Get currently connected reference */ 489 conn_id = zl3073x_dpll_connected_ref_get(zldpll); 490 491 /* Report phase offset only for currently connected pin if the phase 492 * monitor feature is disabled and only if the input pin signal is 493 * present. 494 */ 495 ref_id = zl3073x_input_pin_ref_get(pin->id); 496 ref = zl3073x_ref_state_get(zldev, ref_id); 497 if ((!zldpll->phase_monitor && ref_id != conn_id) || 498 !zl3073x_ref_is_status_ok(ref)) { 499 *phase_offset = 0; 500 return 0; 501 } 502 503 ref_phase = pin->phase_offset; 504 505 /* The DPLL being locked to a higher freq than the current ref 506 * the phase offset is modded to the period of the signal 507 * the dpll is locked to. 508 */ 509 if (ZL3073X_DPLL_REF_IS_VALID(conn_id) && conn_id != ref_id) { 510 u32 conn_freq, ref_freq; 511 512 /* Get frequency of connected and given ref */ 513 conn_freq = zl3073x_dev_ref_freq_get(zldev, conn_id); 514 ref_freq = zl3073x_ref_freq_get(ref); 515 516 if (conn_freq > ref_freq) { 517 s64 conn_period, div_factor; 518 519 conn_period = div_s64(PSEC_PER_SEC, conn_freq); 520 div_factor = div64_s64(ref_phase, conn_period); 521 ref_phase -= conn_period * div_factor; 522 } 523 } 524 525 *phase_offset = ref_phase * DPLL_PHASE_OFFSET_DIVIDER; 526 527 return 0; 528 } 529 530 static int 531 zl3073x_dpll_input_pin_phase_adjust_get(const struct dpll_pin *dpll_pin, 532 void *pin_priv, 533 const struct dpll_device *dpll, 534 void *dpll_priv, 535 s32 *phase_adjust, 536 struct netlink_ext_ack *extack) 537 { 538 struct zl3073x_dpll *zldpll = dpll_priv; 539 struct zl3073x_dev *zldev = zldpll->dev; 540 struct zl3073x_dpll_pin *pin = pin_priv; 541 const struct zl3073x_ref *ref; 542 s64 phase_comp; 543 u8 ref_id; 544 545 guard(mutex)(&zldpll->lock); 546 547 /* Read reference configuration */ 548 ref_id = zl3073x_input_pin_ref_get(pin->id); 549 ref = zl3073x_ref_state_get(zldev, ref_id); 550 551 /* Perform sign extension based on register width */ 552 if (zl3073x_dev_is_ref_phase_comp_32bit(zldev)) 553 phase_comp = sign_extend64(ref->phase_comp, 31); 554 else 555 phase_comp = sign_extend64(ref->phase_comp, 47); 556 557 /* Reverse two's complement negation applied during set and convert 558 * to 32bit signed int 559 */ 560 *phase_adjust = (s32)-phase_comp; 561 562 return 0; 563 } 564 565 static int 566 zl3073x_dpll_input_pin_phase_adjust_set(const struct dpll_pin *dpll_pin, 567 void *pin_priv, 568 const struct dpll_device *dpll, 569 void *dpll_priv, 570 s32 phase_adjust, 571 struct netlink_ext_ack *extack) 572 { 573 struct zl3073x_dpll *zldpll = dpll_priv; 574 struct zl3073x_dev *zldev = zldpll->dev; 575 struct zl3073x_dpll_pin *pin = pin_priv; 576 struct zl3073x_ref ref; 577 u8 ref_id; 578 579 guard(mutex)(&zldpll->lock); 580 581 /* Read reference configuration */ 582 ref_id = zl3073x_input_pin_ref_get(pin->id); 583 ref = *zl3073x_ref_state_get(zldev, ref_id); 584 585 /* The value in the register is stored as two's complement negation 586 * of requested value. 587 */ 588 ref.phase_comp = -phase_adjust; 589 590 /* Update reference configuration */ 591 return zl3073x_ref_state_set(zldev, ref_id, &ref); 592 } 593 594 /** 595 * zl3073x_dpll_ref_operstate_get - get operational state for input pin 596 * @pin: pointer to pin 597 * @operstate: place to store operational state 598 * 599 * Returns the actual hardware state of the pin: whether it is actively 600 * used by the DPLL, has no signal, failed qualification, or is simply 601 * not in use. 602 * 603 * Return: 0 on success, <0 on error 604 */ 605 static int 606 zl3073x_dpll_ref_operstate_get(struct zl3073x_dpll_pin *pin, 607 enum dpll_pin_operstate *operstate) 608 { 609 struct zl3073x_dpll *zldpll = pin->dpll; 610 struct zl3073x_dev *zldev = zldpll->dev; 611 const struct zl3073x_ref *ref; 612 u8 ref_id; 613 614 lockdep_assert_held(&zldpll->lock); 615 616 ref_id = zl3073x_input_pin_ref_get(pin->id); 617 618 /* Check if this pin is the currently locked reference */ 619 if (ref_id == zl3073x_dpll_connected_ref_get(zldpll)) { 620 *operstate = DPLL_PIN_OPERSTATE_ACTIVE; 621 return 0; 622 } 623 624 /* Check reference monitor status */ 625 ref = zl3073x_ref_state_get(zldev, ref_id); 626 if (ref->mon_status & ZL_REF_MON_STATUS_LOS) 627 *operstate = DPLL_PIN_OPERSTATE_NO_SIGNAL; 628 else if (!zl3073x_ref_is_status_ok(ref)) 629 *operstate = DPLL_PIN_OPERSTATE_QUAL_FAILED; 630 else 631 *operstate = DPLL_PIN_OPERSTATE_STANDBY; 632 633 return 0; 634 } 635 636 static int 637 zl3073x_dpll_input_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, 638 void *pin_priv, 639 const struct dpll_device *dpll, 640 void *dpll_priv, 641 enum dpll_pin_state *state, 642 struct netlink_ext_ack *extack) 643 { 644 struct zl3073x_dpll *zldpll = dpll_priv; 645 struct zl3073x_dpll_pin *pin = pin_priv; 646 const struct zl3073x_chan *chan; 647 u8 mode, ref; 648 649 guard(mutex)(&zldpll->lock); 650 651 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 652 ref = zl3073x_input_pin_ref_get(pin->id); 653 mode = zl3073x_chan_mode_get(chan); 654 655 switch (mode) { 656 case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: 657 if (ref == zl3073x_chan_ref_get(chan)) 658 *state = DPLL_PIN_STATE_CONNECTED; 659 else 660 *state = DPLL_PIN_STATE_DISCONNECTED; 661 break; 662 case ZL_DPLL_MODE_REFSEL_MODE_AUTO: 663 if (zl3073x_chan_ref_is_selectable(chan, ref)) 664 *state = DPLL_PIN_STATE_SELECTABLE; 665 else 666 *state = DPLL_PIN_STATE_DISCONNECTED; 667 break; 668 default: 669 *state = DPLL_PIN_STATE_DISCONNECTED; 670 break; 671 } 672 673 return 0; 674 } 675 676 static int 677 zl3073x_dpll_input_pin_operstate_on_dpll_get(const struct dpll_pin *dpll_pin, 678 void *pin_priv, 679 const struct dpll_device *dpll, 680 void *dpll_priv, 681 enum dpll_pin_operstate *operstate, 682 struct netlink_ext_ack *extack) 683 { 684 struct zl3073x_dpll *zldpll = dpll_priv; 685 struct zl3073x_dpll_pin *pin = pin_priv; 686 687 guard(mutex)(&zldpll->lock); 688 689 return zl3073x_dpll_ref_operstate_get(pin, operstate); 690 } 691 692 static int 693 zl3073x_dpll_input_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin, 694 void *pin_priv, 695 const struct dpll_device *dpll, 696 void *dpll_priv, 697 enum dpll_pin_state state, 698 struct netlink_ext_ack *extack) 699 { 700 struct zl3073x_dpll *zldpll = dpll_priv; 701 struct zl3073x_dpll_pin *pin = pin_priv; 702 struct zl3073x_dpll_pin *nco_pin = NULL; 703 struct zl3073x_chan chan; 704 u8 mode, ref; 705 int rc = 0; 706 707 mutex_lock(&zldpll->lock); 708 709 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 710 ref = zl3073x_input_pin_ref_get(pin->id); 711 mode = zl3073x_chan_mode_get(&chan); 712 713 switch (mode) { 714 case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: 715 if (state == DPLL_PIN_STATE_CONNECTED) { 716 /* Choose the pin as new selected reference */ 717 zl3073x_chan_ref_set(&chan, ref); 718 } else if (state == DPLL_PIN_STATE_DISCONNECTED) { 719 /* Choose new mode based on lock status */ 720 switch (zldpll->lock_status) { 721 case DPLL_LOCK_STATUS_LOCKED_HO_ACQ: 722 case DPLL_LOCK_STATUS_HOLDOVER: 723 mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER; 724 break; 725 default: 726 mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN; 727 break; 728 } 729 zl3073x_chan_mode_set(&chan, mode); 730 } else { 731 goto invalid_state; 732 } 733 break; 734 case ZL_DPLL_MODE_REFSEL_MODE_NCO: 735 if (state == DPLL_PIN_STATE_CONNECTED) 736 nco_pin = zl3073x_dpll_nco_pin_get(zldpll); 737 fallthrough; 738 case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: 739 case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: 740 if (state == DPLL_PIN_STATE_CONNECTED) { 741 /* Choose the pin as new selected reference */ 742 zl3073x_chan_ref_set(&chan, ref); 743 /* Switch to reflock mode */ 744 zl3073x_chan_mode_set(&chan, 745 ZL_DPLL_MODE_REFSEL_MODE_REFLOCK); 746 } else if (state != DPLL_PIN_STATE_DISCONNECTED) { 747 goto invalid_state; 748 } 749 break; 750 case ZL_DPLL_MODE_REFSEL_MODE_AUTO: 751 if (state == DPLL_PIN_STATE_SELECTABLE) { 752 if (zl3073x_chan_ref_is_selectable(&chan, ref)) 753 goto unlock; /* Pin is already selectable */ 754 755 /* Restore pin priority in HW */ 756 zl3073x_chan_ref_prio_set(&chan, ref, pin->prio); 757 } else if (state == DPLL_PIN_STATE_DISCONNECTED) { 758 if (!zl3073x_chan_ref_is_selectable(&chan, ref)) 759 goto unlock; /* Pin is already disconnected */ 760 761 /* Set pin priority to none in HW */ 762 zl3073x_chan_ref_prio_set(&chan, ref, 763 ZL_DPLL_REF_PRIO_NONE); 764 } else { 765 goto invalid_state; 766 } 767 break; 768 default: 769 /* In other modes we cannot change input reference */ 770 NL_SET_ERR_MSG(extack, 771 "Pin state cannot be changed in current mode"); 772 rc = -EOPNOTSUPP; 773 goto unlock; 774 } 775 776 /* Commit DPLL channel changes */ 777 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 778 goto unlock; 779 780 invalid_state: 781 NL_SET_ERR_MSG_MOD(extack, "Invalid pin state for this device mode"); 782 rc = -EINVAL; 783 unlock: 784 mutex_unlock(&zldpll->lock); 785 786 /* If leaving NCO mode, notify userspace about the NCO pin 787 * state change - the periodic worker skips the NCO pin. 788 */ 789 if (!rc && nco_pin) 790 __dpll_pin_change_ntf(nco_pin->dpll_pin); 791 792 return rc; 793 } 794 795 static int 796 zl3073x_dpll_input_pin_prio_get(const struct dpll_pin *dpll_pin, void *pin_priv, 797 const struct dpll_device *dpll, void *dpll_priv, 798 u32 *prio, struct netlink_ext_ack *extack) 799 { 800 struct zl3073x_dpll *zldpll = dpll_priv; 801 struct zl3073x_dpll_pin *pin = pin_priv; 802 803 guard(mutex)(&zldpll->lock); 804 805 *prio = pin->prio; 806 807 return 0; 808 } 809 810 static int 811 zl3073x_dpll_input_pin_prio_set(const struct dpll_pin *dpll_pin, void *pin_priv, 812 const struct dpll_device *dpll, void *dpll_priv, 813 u32 prio, struct netlink_ext_ack *extack) 814 { 815 struct zl3073x_dpll *zldpll = dpll_priv; 816 struct zl3073x_dpll_pin *pin = pin_priv; 817 struct zl3073x_chan chan; 818 u8 ref; 819 int rc; 820 821 guard(mutex)(&zldpll->lock); 822 823 if (prio > ZL_DPLL_REF_PRIO_MAX) 824 return -EINVAL; 825 826 /* If the pin is selectable then update HW registers */ 827 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 828 ref = zl3073x_input_pin_ref_get(pin->id); 829 if (zl3073x_chan_ref_is_selectable(&chan, ref)) { 830 zl3073x_chan_ref_prio_set(&chan, ref, prio); 831 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 832 if (rc) 833 return rc; 834 } 835 836 /* Save priority */ 837 pin->prio = prio; 838 839 return 0; 840 } 841 842 static int 843 zl3073x_dpll_output_pin_esync_get(const struct dpll_pin *dpll_pin, 844 void *pin_priv, 845 const struct dpll_device *dpll, 846 void *dpll_priv, 847 struct dpll_pin_esync *esync, 848 struct netlink_ext_ack *extack) 849 { 850 struct zl3073x_dpll *zldpll = dpll_priv; 851 struct zl3073x_dev *zldev = zldpll->dev; 852 struct zl3073x_dpll_pin *pin = pin_priv; 853 const struct zl3073x_synth *synth; 854 const struct zl3073x_out *out; 855 u32 synth_freq, out_freq; 856 u8 out_id; 857 858 guard(mutex)(&zldpll->lock); 859 860 out_id = zl3073x_output_pin_out_get(pin->id); 861 out = zl3073x_out_state_get(zldev, out_id); 862 863 /* If N-division is enabled, esync is not supported. The register used 864 * for N-division is also used for the esync divider so both cannot 865 * be used. 866 */ 867 if (zl3073x_out_is_ndiv(out)) 868 return -EOPNOTSUPP; 869 870 /* Get attached synth frequency */ 871 synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(out)); 872 synth_freq = zl3073x_synth_freq_get(synth); 873 out_freq = synth_freq / out->div; 874 875 if (!pin->esync_control || out_freq <= 1) 876 return -EOPNOTSUPP; 877 878 esync->range = esync_freq_ranges; 879 esync->range_num = ARRAY_SIZE(esync_freq_ranges); 880 881 if (zl3073x_out_clock_type_get(out) != ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC) { 882 /* No need to read esync data if it is not enabled */ 883 esync->freq = 0; 884 esync->pulse = 0; 885 886 return 0; 887 } 888 889 /* Compute esync frequency */ 890 esync->freq = out_freq / out->esync_n_period; 891 892 /* By comparing the esync_pulse_width to the half of the pulse width 893 * the esync pulse percentage can be determined. 894 * Note that half pulse width is in units of half synth cycles, which 895 * is why it reduces down to be output_div. 896 */ 897 esync->pulse = (50 * out->esync_n_width) / out->div; 898 899 return 0; 900 } 901 902 static int 903 zl3073x_dpll_output_pin_esync_set(const struct dpll_pin *dpll_pin, 904 void *pin_priv, 905 const struct dpll_device *dpll, 906 void *dpll_priv, u64 freq, 907 struct netlink_ext_ack *extack) 908 { 909 struct zl3073x_dpll *zldpll = dpll_priv; 910 struct zl3073x_dev *zldev = zldpll->dev; 911 struct zl3073x_dpll_pin *pin = pin_priv; 912 const struct zl3073x_synth *synth; 913 struct zl3073x_out out; 914 u32 synth_freq; 915 u8 out_id; 916 917 guard(mutex)(&zldpll->lock); 918 919 out_id = zl3073x_output_pin_out_get(pin->id); 920 out = *zl3073x_out_state_get(zldev, out_id); 921 922 /* If N-division is enabled, esync is not supported. The register used 923 * for N-division is also used for the esync divider so both cannot 924 * be used. 925 */ 926 if (zl3073x_out_is_ndiv(&out)) 927 return -EOPNOTSUPP; 928 929 /* Update clock type in output mode */ 930 if (freq) 931 zl3073x_out_clock_type_set(&out, 932 ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC); 933 else 934 zl3073x_out_clock_type_set(&out, 935 ZL_OUTPUT_MODE_CLOCK_TYPE_NORMAL); 936 937 /* If esync is being disabled just write mailbox and finish */ 938 if (!freq) 939 return zl3073x_out_state_set(zldev, out_id, &out); 940 941 /* Get attached synth frequency */ 942 synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out)); 943 synth_freq = zl3073x_synth_freq_get(synth); 944 945 /* Compute and update esync period */ 946 out.esync_n_period = synth_freq / (u32)freq / out.div; 947 948 /* Half of the period in units of 1/2 synth cycle can be represented by 949 * the output_div. To get the supported esync pulse width of 25% of the 950 * period the output_div can just be divided by 2. Note that this 951 * assumes that output_div is even, otherwise some resolution will be 952 * lost. 953 */ 954 out.esync_n_width = out.div / 2; 955 956 /* Commit output configuration */ 957 return zl3073x_out_state_set(zldev, out_id, &out); 958 } 959 960 static int 961 zl3073x_dpll_output_pin_frequency_get(const struct dpll_pin *dpll_pin, 962 void *pin_priv, 963 const struct dpll_device *dpll, 964 void *dpll_priv, u64 *frequency, 965 struct netlink_ext_ack *extack) 966 { 967 struct zl3073x_dpll *zldpll = dpll_priv; 968 struct zl3073x_dpll_pin *pin = pin_priv; 969 970 guard(mutex)(&zldpll->lock); 971 972 *frequency = zl3073x_dev_output_pin_freq_get(zldpll->dev, pin->id); 973 974 return 0; 975 } 976 977 static int 978 zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin, 979 void *pin_priv, 980 const struct dpll_device *dpll, 981 void *dpll_priv, u64 frequency, 982 struct netlink_ext_ack *extack) 983 { 984 struct zl3073x_dpll *zldpll = dpll_priv; 985 struct zl3073x_dev *zldev = zldpll->dev; 986 struct zl3073x_dpll_pin *pin = pin_priv; 987 const struct zl3073x_synth *synth; 988 u32 new_div, synth_freq; 989 struct zl3073x_out out; 990 u8 out_id; 991 992 guard(mutex)(&zldpll->lock); 993 994 out_id = zl3073x_output_pin_out_get(pin->id); 995 out = *zl3073x_out_state_get(zldev, out_id); 996 997 /* Get attached synth frequency and compute new divisor */ 998 synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out)); 999 synth_freq = zl3073x_synth_freq_get(synth); 1000 new_div = synth_freq / (u32)frequency; 1001 1002 /* Check signal format */ 1003 if (!zl3073x_out_is_ndiv(&out)) { 1004 /* For non N-divided signal formats the frequency is computed 1005 * as division of synth frequency and output divisor. 1006 */ 1007 out.div = new_div; 1008 1009 /* For 50/50 duty cycle the divisor is equal to width */ 1010 out.width = new_div; 1011 1012 /* Commit output configuration */ 1013 return zl3073x_out_state_set(zldev, out_id, &out); 1014 } 1015 1016 if (zl3073x_dpll_is_p_pin(pin)) { 1017 /* We are going to change output frequency for P-pin but 1018 * if the requested frequency is less than current N-pin 1019 * frequency then indicate a failure as we are not able 1020 * to compute N-pin divisor to keep its frequency unchanged. 1021 * 1022 * Update divisor for N-pin to keep N-pin frequency. 1023 */ 1024 out.esync_n_period = (out.esync_n_period * out.div) / new_div; 1025 if (!out.esync_n_period) 1026 return -EINVAL; 1027 1028 /* Update the output divisor */ 1029 out.div = new_div; 1030 1031 /* For 50/50 duty cycle the divisor is equal to width */ 1032 out.width = out.div; 1033 } else { 1034 /* We are going to change frequency of N-pin but if 1035 * the requested freq is greater or equal than freq of P-pin 1036 * in the output pair we cannot compute divisor for the N-pin. 1037 * In this case indicate a failure. 1038 * 1039 * Update divisor for N-pin 1040 */ 1041 out.esync_n_period = div64_u64(synth_freq, frequency * out.div); 1042 if (!out.esync_n_period) 1043 return -EINVAL; 1044 } 1045 1046 /* For 50/50 duty cycle the divisor is equal to width */ 1047 out.esync_n_width = out.esync_n_period; 1048 1049 /* Commit output configuration */ 1050 return zl3073x_out_state_set(zldev, out_id, &out); 1051 } 1052 1053 static int 1054 zl3073x_dpll_output_pin_phase_adjust_get(const struct dpll_pin *dpll_pin, 1055 void *pin_priv, 1056 const struct dpll_device *dpll, 1057 void *dpll_priv, 1058 s32 *phase_adjust, 1059 struct netlink_ext_ack *extack) 1060 { 1061 struct zl3073x_dpll *zldpll = dpll_priv; 1062 struct zl3073x_dev *zldev = zldpll->dev; 1063 struct zl3073x_dpll_pin *pin = pin_priv; 1064 const struct zl3073x_out *out; 1065 u8 out_id; 1066 1067 guard(mutex)(&zldpll->lock); 1068 1069 out_id = zl3073x_output_pin_out_get(pin->id); 1070 out = zl3073x_out_state_get(zldev, out_id); 1071 1072 /* The value in the register is expressed in half synth clock cycles. */ 1073 *phase_adjust = out->phase_comp * pin->phase_gran; 1074 1075 return 0; 1076 } 1077 1078 static int 1079 zl3073x_dpll_output_pin_phase_adjust_set(const struct dpll_pin *dpll_pin, 1080 void *pin_priv, 1081 const struct dpll_device *dpll, 1082 void *dpll_priv, 1083 s32 phase_adjust, 1084 struct netlink_ext_ack *extack) 1085 { 1086 struct zl3073x_dpll *zldpll = dpll_priv; 1087 struct zl3073x_dev *zldev = zldpll->dev; 1088 struct zl3073x_dpll_pin *pin = pin_priv; 1089 struct zl3073x_out out; 1090 u8 out_id; 1091 1092 guard(mutex)(&zldpll->lock); 1093 1094 out_id = zl3073x_output_pin_out_get(pin->id); 1095 out = *zl3073x_out_state_get(zldev, out_id); 1096 1097 /* The value in the register is expressed in half synth clock cycles. */ 1098 out.phase_comp = phase_adjust / pin->phase_gran; 1099 1100 /* Update output configuration from mailbox */ 1101 return zl3073x_out_state_set(zldev, out_id, &out); 1102 } 1103 1104 static int 1105 zl3073x_dpll_output_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, 1106 void *pin_priv, 1107 const struct dpll_device *dpll, 1108 void *dpll_priv, 1109 enum dpll_pin_state *state, 1110 struct netlink_ext_ack *extack) 1111 { 1112 /* If the output pin is registered then it is always connected */ 1113 *state = DPLL_PIN_STATE_CONNECTED; 1114 1115 return 0; 1116 } 1117 1118 static int 1119 zl3073x_dpll_nco_pin_operstate_on_dpll_get(const struct dpll_pin *dpll_pin, 1120 void *pin_priv, 1121 const struct dpll_device *dpll, 1122 void *dpll_priv, 1123 enum dpll_pin_operstate *operstate, 1124 struct netlink_ext_ack *extack) 1125 { 1126 struct zl3073x_dpll *zldpll = dpll_priv; 1127 const struct zl3073x_chan *chan; 1128 1129 guard(mutex)(&zldpll->lock); 1130 1131 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1132 if (zl3073x_chan_mode_is_nco(chan)) 1133 *operstate = DPLL_PIN_OPERSTATE_ACTIVE; 1134 else 1135 *operstate = DPLL_PIN_OPERSTATE_STANDBY; 1136 1137 return 0; 1138 } 1139 1140 static int 1141 zl3073x_dpll_nco_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, 1142 void *pin_priv, 1143 const struct dpll_device *dpll, 1144 void *dpll_priv, 1145 enum dpll_pin_state *state, 1146 struct netlink_ext_ack *extack) 1147 { 1148 struct zl3073x_dpll *zldpll = dpll_priv; 1149 const struct zl3073x_chan *chan; 1150 1151 guard(mutex)(&zldpll->lock); 1152 1153 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1154 if (zl3073x_chan_mode_is_nco(chan)) 1155 *state = DPLL_PIN_STATE_CONNECTED; 1156 else 1157 *state = DPLL_PIN_STATE_DISCONNECTED; 1158 1159 return 0; 1160 } 1161 1162 static int 1163 zl3073x_dpll_nco_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin, 1164 void *pin_priv, 1165 const struct dpll_device *dpll, 1166 void *dpll_priv, 1167 enum dpll_pin_state state, 1168 struct netlink_ext_ack *extack) 1169 { 1170 struct zl3073x_dpll_pin *ref_pin = NULL; 1171 struct zl3073x_dpll *zldpll = dpll_priv; 1172 struct zl3073x_chan chan; 1173 u8 ref; 1174 int rc; 1175 1176 mutex_lock(&zldpll->lock); 1177 1178 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1179 1180 switch (state) { 1181 case DPLL_PIN_STATE_CONNECTED: 1182 if (zl3073x_chan_mode_is_nco(&chan)) { 1183 mutex_unlock(&zldpll->lock); 1184 return 0; 1185 } 1186 if (zl3073x_chan_mode_is_auto(&chan)) { 1187 NL_SET_ERR_MSG(extack, 1188 "NCO pin cannot be connected in automatic mode"); 1189 mutex_unlock(&zldpll->lock); 1190 return -EINVAL; 1191 } 1192 if (zl3073x_chan_mode_is_reflock(&chan)) { 1193 /* Get currently connected pin */ 1194 ref = zl3073x_chan_ref_get(&chan); 1195 ref_pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref); 1196 } 1197 rc = zl3073x_chan_nco_mode_set(zldpll->dev, zldpll->id); 1198 break; 1199 case DPLL_PIN_STATE_DISCONNECTED: 1200 if (!zl3073x_chan_mode_is_nco(&chan)) { 1201 mutex_unlock(&zldpll->lock); 1202 return 0; 1203 } 1204 /* Switch to freerun - holdover averaging was not 1205 * updated during NCO mode. 1206 */ 1207 zl3073x_chan_mode_set(&chan, 1208 ZL_DPLL_MODE_REFSEL_MODE_FREERUN); 1209 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 1210 break; 1211 default: 1212 NL_SET_ERR_MSG(extack, "invalid pin state for NCO pin"); 1213 mutex_unlock(&zldpll->lock); 1214 return -EINVAL; 1215 } 1216 1217 mutex_unlock(&zldpll->lock); 1218 1219 if (!rc && ref_pin) 1220 __dpll_pin_change_ntf(ref_pin->dpll_pin); 1221 1222 return rc; 1223 } 1224 1225 static int 1226 zl3073x_dpll_nco_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv, 1227 const struct dpll_device *dpll, void *dpll_priv, 1228 struct dpll_ffo_param *ffo, 1229 struct netlink_ext_ack *extack) 1230 { 1231 struct zl3073x_dpll *zldpll = dpll_priv; 1232 const struct zl3073x_chan *chan; 1233 s64 df_offset; 1234 1235 guard(mutex)(&zldpll->lock); 1236 1237 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1238 if (!zl3073x_chan_mode_is_nco(chan)) 1239 return -ENODATA; 1240 1241 /* Do not report FFO if a failure occurred during switching to NCO. */ 1242 df_offset = zl3073x_chan_df_offset_get(chan); 1243 if (df_offset == ZL_DPLL_DF_OFFSET_UNKNOWN) 1244 return -ENODATA; 1245 1246 /* dpll_df_offset register has inverted sign per datasheet: 1247 * f_offset = f_nom * (-df_offset) / 2^48 1248 * NCO pin reports DPLL output offset from nominal, so negate. 1249 * Convert to PPT: ppt = -df * 5^12 / 2^36 1250 */ 1251 ffo->ffo = -mul_s64_u64_shr(df_offset, 244140625, 36); 1252 1253 return 0; 1254 } 1255 1256 static int 1257 zl3073x_dpll_temp_get(const struct dpll_device *dpll, void *dpll_priv, 1258 s32 *temp, struct netlink_ext_ack *extack) 1259 { 1260 struct zl3073x_dpll *zldpll = dpll_priv; 1261 struct zl3073x_dev *zldev = zldpll->dev; 1262 u16 val; 1263 int rc; 1264 1265 guard(mutex)(&zldpll->lock); 1266 1267 rc = zl3073x_read_u16(zldev, ZL_REG_DIE_TEMP_STATUS, &val); 1268 if (rc) 1269 return rc; 1270 1271 /* Register value is in units of 0.1 C, convert to millidegrees */ 1272 *temp = (s16)val * 100; 1273 1274 return 0; 1275 } 1276 1277 static int 1278 __zl3073x_dpll_lock_status_get(struct zl3073x_dpll *zldpll, 1279 enum dpll_lock_status *status) 1280 { 1281 const struct zl3073x_chan *chan; 1282 1283 lockdep_assert_held(&zldpll->lock); 1284 1285 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1286 1287 switch (zl3073x_chan_mode_get(chan)) { 1288 case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: 1289 case ZL_DPLL_MODE_REFSEL_MODE_NCO: 1290 /* In FREERUN and NCO modes the DPLL is always unlocked */ 1291 *status = DPLL_LOCK_STATUS_UNLOCKED; 1292 1293 return 0; 1294 default: 1295 break; 1296 } 1297 1298 switch (zl3073x_chan_lock_state_get(chan)) { 1299 case ZL_DPLL_MON_STATUS_STATE_LOCK: 1300 if (zl3073x_chan_is_ho_ready(chan)) 1301 *status = DPLL_LOCK_STATUS_LOCKED_HO_ACQ; 1302 else 1303 *status = DPLL_LOCK_STATUS_LOCKED; 1304 break; 1305 case ZL_DPLL_MON_STATUS_STATE_HOLDOVER: 1306 case ZL_DPLL_MON_STATUS_STATE_ACQUIRING: 1307 *status = DPLL_LOCK_STATUS_HOLDOVER; 1308 break; 1309 default: 1310 dev_warn(zldpll->dev->dev, 1311 "Unknown DPLL monitor status: 0x%02x\n", 1312 chan->mon_status); 1313 *status = DPLL_LOCK_STATUS_UNLOCKED; 1314 break; 1315 } 1316 1317 return 0; 1318 } 1319 1320 static int 1321 zl3073x_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv, 1322 enum dpll_lock_status *status, 1323 enum dpll_lock_status_error *status_error, 1324 struct netlink_ext_ack *extack) 1325 { 1326 struct zl3073x_dpll *zldpll = dpll_priv; 1327 1328 guard(mutex)(&zldpll->lock); 1329 1330 return __zl3073x_dpll_lock_status_get(zldpll, status); 1331 } 1332 1333 static int 1334 zl3073x_dpll_supported_modes_get(const struct dpll_device *dpll, 1335 void *dpll_priv, unsigned long *modes, 1336 struct netlink_ext_ack *extack) 1337 { 1338 __set_bit(DPLL_MODE_AUTOMATIC, modes); 1339 __set_bit(DPLL_MODE_MANUAL, modes); 1340 1341 return 0; 1342 } 1343 1344 static int 1345 zl3073x_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv, 1346 enum dpll_mode *mode, struct netlink_ext_ack *extack) 1347 { 1348 struct zl3073x_dpll *zldpll = dpll_priv; 1349 const struct zl3073x_chan *chan; 1350 1351 guard(mutex)(&zldpll->lock); 1352 1353 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1354 1355 switch (zl3073x_chan_mode_get(chan)) { 1356 case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: 1357 case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: 1358 case ZL_DPLL_MODE_REFSEL_MODE_NCO: 1359 case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: 1360 /* Use MANUAL for device FREERUN, HOLDOVER, NCO and 1361 * REFLOCK modes 1362 */ 1363 *mode = DPLL_MODE_MANUAL; 1364 break; 1365 case ZL_DPLL_MODE_REFSEL_MODE_AUTO: 1366 /* Use AUTO for device AUTO mode */ 1367 *mode = DPLL_MODE_AUTOMATIC; 1368 break; 1369 default: 1370 return -EINVAL; 1371 } 1372 1373 return 0; 1374 } 1375 1376 static int 1377 zl3073x_dpll_phase_offset_avg_factor_get(const struct dpll_device *dpll, 1378 void *dpll_priv, u32 *factor, 1379 struct netlink_ext_ack *extack) 1380 { 1381 struct zl3073x_dpll *zldpll = dpll_priv; 1382 1383 *factor = zl3073x_dev_phase_avg_factor_get(zldpll->dev); 1384 1385 return 0; 1386 } 1387 1388 static int 1389 zl3073x_dpll_phase_offset_avg_factor_set(const struct dpll_device *dpll, 1390 void *dpll_priv, u32 factor, 1391 struct netlink_ext_ack *extack) 1392 { 1393 struct zl3073x_dpll *item, *zldpll = dpll_priv; 1394 int rc; 1395 1396 if (factor > 15) { 1397 NL_SET_ERR_MSG_FMT(extack, 1398 "Phase offset average factor has to be from range <0,15>"); 1399 return -EINVAL; 1400 } 1401 1402 rc = zl3073x_dev_phase_avg_factor_set(zldpll->dev, factor); 1403 if (rc) { 1404 NL_SET_ERR_MSG_FMT(extack, 1405 "Failed to set phase offset averaging factor"); 1406 return rc; 1407 } 1408 1409 /* The averaging factor is common for all DPLL channels so after 1410 * change we have to send a notification for other DPLL devices. 1411 */ 1412 list_for_each_entry(item, &zldpll->dev->dplls, list) { 1413 struct dpll_device *dpll_dev = READ_ONCE(item->dpll_dev); 1414 1415 if (item != zldpll && dpll_dev) 1416 __dpll_device_change_ntf(dpll_dev); 1417 } 1418 1419 return 0; 1420 } 1421 1422 static int 1423 zl3073x_dpll_mode_set(const struct dpll_device *dpll, void *dpll_priv, 1424 enum dpll_mode mode, struct netlink_ext_ack *extack) 1425 { 1426 struct zl3073x_dpll *zldpll = dpll_priv; 1427 struct zl3073x_dpll_pin *nco_pin = NULL; 1428 struct zl3073x_chan chan; 1429 u8 hw_mode, ref; 1430 int rc; 1431 1432 mutex_lock(&zldpll->lock); 1433 1434 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1435 ref = zl3073x_chan_refsel_ref_get(&chan); 1436 1437 if (mode == DPLL_MODE_MANUAL) { 1438 /* We are switching from automatic to manual mode: 1439 * - if we have a valid reference selected during auto mode then 1440 * we will switch to forced reference lock mode and use this 1441 * reference for selection 1442 * - if NO valid reference is selected, we will switch to forced 1443 * holdover mode or freerun mode, depending on the current 1444 * lock status 1445 */ 1446 if (ZL3073X_DPLL_REF_IS_VALID(ref)) 1447 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK; 1448 else if (zldpll->lock_status == DPLL_LOCK_STATUS_UNLOCKED) 1449 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN; 1450 else 1451 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER; 1452 } else { 1453 if (zl3073x_chan_mode_is_nco(&chan)) 1454 nco_pin = zl3073x_dpll_nco_pin_get(zldpll); 1455 1456 /* We are switching from manual to automatic mode: 1457 * - if there is a valid reference selected then ensure that 1458 * it is selectable after switch to automatic mode 1459 * - switch to automatic mode 1460 */ 1461 if (ZL3073X_DPLL_REF_IS_VALID(ref) && 1462 !zl3073x_chan_ref_is_selectable(&chan, ref)) { 1463 struct zl3073x_dpll_pin *pin; 1464 1465 pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref); 1466 if (pin) { 1467 /* Restore pin priority in HW */ 1468 zl3073x_chan_ref_prio_set(&chan, ref, 1469 pin->prio); 1470 } 1471 } 1472 1473 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_AUTO; 1474 } 1475 1476 zl3073x_chan_mode_set(&chan, hw_mode); 1477 if (ZL3073X_DPLL_REF_IS_VALID(ref)) 1478 zl3073x_chan_ref_set(&chan, ref); 1479 1480 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 1481 if (rc) { 1482 NL_SET_ERR_MSG_MOD(extack, 1483 "failed to set reference selection mode"); 1484 mutex_unlock(&zldpll->lock); 1485 return rc; 1486 } 1487 1488 mutex_unlock(&zldpll->lock); 1489 1490 /* If leaving NCO mode, notify userspace about the NCO pin 1491 * state change - the periodic worker skips the NCO pin. 1492 */ 1493 if (nco_pin) 1494 __dpll_pin_change_ntf(nco_pin->dpll_pin); 1495 1496 return 0; 1497 } 1498 1499 static int 1500 zl3073x_dpll_phase_offset_monitor_get(const struct dpll_device *dpll, 1501 void *dpll_priv, 1502 enum dpll_feature_state *state, 1503 struct netlink_ext_ack *extack) 1504 { 1505 struct zl3073x_dpll *zldpll = dpll_priv; 1506 1507 guard(mutex)(&zldpll->lock); 1508 1509 if (zldpll->phase_monitor) 1510 *state = DPLL_FEATURE_STATE_ENABLE; 1511 else 1512 *state = DPLL_FEATURE_STATE_DISABLE; 1513 1514 return 0; 1515 } 1516 1517 static int 1518 zl3073x_dpll_phase_offset_monitor_set(const struct dpll_device *dpll, 1519 void *dpll_priv, 1520 enum dpll_feature_state state, 1521 struct netlink_ext_ack *extack) 1522 { 1523 struct zl3073x_dpll *zldpll = dpll_priv; 1524 1525 guard(mutex)(&zldpll->lock); 1526 1527 zldpll->phase_monitor = (state == DPLL_FEATURE_STATE_ENABLE); 1528 1529 return 0; 1530 } 1531 1532 static int 1533 zl3073x_dpll_freq_monitor_get(const struct dpll_device *dpll, 1534 void *dpll_priv, 1535 enum dpll_feature_state *state, 1536 struct netlink_ext_ack *extack) 1537 { 1538 struct zl3073x_dpll *zldpll = dpll_priv; 1539 1540 if (READ_ONCE(zldpll->dev->freq_monitor)) 1541 *state = DPLL_FEATURE_STATE_ENABLE; 1542 else 1543 *state = DPLL_FEATURE_STATE_DISABLE; 1544 1545 return 0; 1546 } 1547 1548 static int 1549 zl3073x_dpll_freq_monitor_set(const struct dpll_device *dpll, 1550 void *dpll_priv, 1551 enum dpll_feature_state state, 1552 struct netlink_ext_ack *extack) 1553 { 1554 struct zl3073x_dpll *item, *zldpll = dpll_priv; 1555 struct zl3073x_dev *zldev = zldpll->dev; 1556 1557 WRITE_ONCE(zldev->freq_monitor, state == DPLL_FEATURE_STATE_ENABLE); 1558 1559 /* The frequency monitoring is common for all DPLL channels so after 1560 * change we have to send a notification for other DPLL devices. 1561 */ 1562 list_for_each_entry(item, &zldev->dplls, list) { 1563 struct dpll_device *dpll_dev = READ_ONCE(item->dpll_dev); 1564 1565 if (item != zldpll && dpll_dev) 1566 __dpll_device_change_ntf(dpll_dev); 1567 } 1568 1569 return 0; 1570 } 1571 1572 static const struct dpll_pin_ops zl3073x_dpll_input_pin_ops = { 1573 .supported_ffo = BIT(DPLL_FFO_PIN_DEVICE), 1574 .direction_get = zl3073x_dpll_pin_direction_get, 1575 .esync_get = zl3073x_dpll_input_pin_esync_get, 1576 .esync_set = zl3073x_dpll_input_pin_esync_set, 1577 .ffo_get = zl3073x_dpll_input_pin_ffo_get, 1578 .frequency_get = zl3073x_dpll_input_pin_frequency_get, 1579 .frequency_set = zl3073x_dpll_input_pin_frequency_set, 1580 .measured_freq_get = zl3073x_dpll_input_pin_measured_freq_get, 1581 .operstate_on_dpll_get = zl3073x_dpll_input_pin_operstate_on_dpll_get, 1582 .phase_offset_get = zl3073x_dpll_input_pin_phase_offset_get, 1583 .phase_adjust_get = zl3073x_dpll_input_pin_phase_adjust_get, 1584 .phase_adjust_set = zl3073x_dpll_input_pin_phase_adjust_set, 1585 .prio_get = zl3073x_dpll_input_pin_prio_get, 1586 .prio_set = zl3073x_dpll_input_pin_prio_set, 1587 .ref_sync_get = zl3073x_dpll_input_pin_ref_sync_get, 1588 .ref_sync_set = zl3073x_dpll_input_pin_ref_sync_set, 1589 .state_on_dpll_get = zl3073x_dpll_input_pin_state_on_dpll_get, 1590 .state_on_dpll_set = zl3073x_dpll_input_pin_state_on_dpll_set, 1591 }; 1592 1593 static const struct dpll_pin_ops zl3073x_dpll_output_pin_ops = { 1594 .direction_get = zl3073x_dpll_pin_direction_get, 1595 .esync_get = zl3073x_dpll_output_pin_esync_get, 1596 .esync_set = zl3073x_dpll_output_pin_esync_set, 1597 .frequency_get = zl3073x_dpll_output_pin_frequency_get, 1598 .frequency_set = zl3073x_dpll_output_pin_frequency_set, 1599 .phase_adjust_get = zl3073x_dpll_output_pin_phase_adjust_get, 1600 .phase_adjust_set = zl3073x_dpll_output_pin_phase_adjust_set, 1601 .state_on_dpll_get = zl3073x_dpll_output_pin_state_on_dpll_get, 1602 }; 1603 1604 static const struct dpll_pin_ops zl3073x_dpll_nco_pin_ops = { 1605 .supported_ffo = BIT(DPLL_FFO_PIN_DEVICE), 1606 .direction_get = zl3073x_dpll_pin_direction_get, 1607 .ffo_get = zl3073x_dpll_nco_pin_ffo_get, 1608 .operstate_on_dpll_get = zl3073x_dpll_nco_pin_operstate_on_dpll_get, 1609 .state_on_dpll_get = zl3073x_dpll_nco_pin_state_on_dpll_get, 1610 .state_on_dpll_set = zl3073x_dpll_nco_pin_state_on_dpll_set, 1611 }; 1612 1613 static const struct dpll_device_ops zl3073x_dpll_device_ops = { 1614 .lock_status_get = zl3073x_dpll_lock_status_get, 1615 .mode_get = zl3073x_dpll_mode_get, 1616 .mode_set = zl3073x_dpll_mode_set, 1617 .phase_offset_avg_factor_get = zl3073x_dpll_phase_offset_avg_factor_get, 1618 .phase_offset_avg_factor_set = zl3073x_dpll_phase_offset_avg_factor_set, 1619 .phase_offset_monitor_get = zl3073x_dpll_phase_offset_monitor_get, 1620 .phase_offset_monitor_set = zl3073x_dpll_phase_offset_monitor_set, 1621 .freq_monitor_get = zl3073x_dpll_freq_monitor_get, 1622 .freq_monitor_set = zl3073x_dpll_freq_monitor_set, 1623 .supported_modes_get = zl3073x_dpll_supported_modes_get, 1624 }; 1625 1626 /** 1627 * zl3073x_dpll_pin_alloc - allocate DPLL pin 1628 * @zldpll: pointer to zl3073x_dpll 1629 * @dir: pin direction 1630 * @id: pin id 1631 * 1632 * Allocates and initializes zl3073x_dpll_pin structure for given 1633 * pin id and direction. 1634 * 1635 * Return: pointer to allocated structure on success, error pointer on error 1636 */ 1637 static struct zl3073x_dpll_pin * 1638 zl3073x_dpll_pin_alloc(struct zl3073x_dpll *zldpll, enum dpll_pin_direction dir, 1639 u8 id) 1640 { 1641 struct zl3073x_dpll_pin *pin; 1642 1643 pin = kzalloc_obj(*pin); 1644 if (!pin) 1645 return ERR_PTR(-ENOMEM); 1646 1647 pin->dpll = zldpll; 1648 pin->dir = dir; 1649 pin->id = id; 1650 1651 return pin; 1652 } 1653 1654 /** 1655 * zl3073x_dpll_pin_free - deallocate DPLL pin 1656 * @pin: pin to free 1657 * 1658 * Deallocates DPLL pin previously allocated by @zl3073x_dpll_pin_alloc. 1659 */ 1660 static void 1661 zl3073x_dpll_pin_free(struct zl3073x_dpll_pin *pin) 1662 { 1663 WARN(pin->dpll_pin, "DPLL pin is still registered\n"); 1664 1665 kfree(pin); 1666 } 1667 1668 /** 1669 * zl3073x_dpll_pin_register - register DPLL pin 1670 * @pin: pointer to DPLL pin 1671 * @index: absolute pin index for registration 1672 * 1673 * Registers given DPLL pin into DPLL sub-system. 1674 * 1675 * Return: 0 on success, <0 on error 1676 */ 1677 static int 1678 zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index) 1679 { 1680 struct zl3073x_dpll *zldpll = pin->dpll; 1681 struct zl3073x_pin_props *props; 1682 const struct dpll_pin_ops *ops; 1683 int rc; 1684 1685 /* Get pin properties */ 1686 props = zl3073x_pin_props_get(zldpll->dev, pin->dir, pin->id); 1687 if (IS_ERR(props)) 1688 return PTR_ERR(props); 1689 1690 /* Save package label, fwnode, esync capability and phase adjust 1691 * granularity. 1692 */ 1693 strscpy(pin->label, props->package_label); 1694 pin->fwnode = fwnode_handle_get(props->fwnode); 1695 pin->esync_control = props->esync_control; 1696 pin->phase_gran = props->dpll_props.phase_gran; 1697 1698 if (zl3073x_dpll_is_input_pin(pin)) { 1699 const struct zl3073x_chan *chan; 1700 u8 ref; 1701 1702 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1703 ref = zl3073x_input_pin_ref_get(pin->id); 1704 pin->prio = zl3073x_chan_ref_prio_get(chan, ref); 1705 1706 if (pin->prio == ZL_DPLL_REF_PRIO_NONE) 1707 /* Clamp prio to max value */ 1708 pin->prio = ZL_DPLL_REF_PRIO_MAX; 1709 } 1710 1711 /* Create or get existing DPLL pin */ 1712 pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, index, THIS_MODULE, 1713 &props->dpll_props, &pin->tracker); 1714 if (IS_ERR(pin->dpll_pin)) { 1715 rc = PTR_ERR(pin->dpll_pin); 1716 goto err_pin_get; 1717 } 1718 dpll_pin_fwnode_set(pin->dpll_pin, props->fwnode); 1719 1720 if (zl3073x_dpll_is_input_pin(pin)) 1721 ops = &zl3073x_dpll_input_pin_ops; 1722 else 1723 ops = &zl3073x_dpll_output_pin_ops; 1724 1725 /* Register the pin */ 1726 rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin, ops, pin); 1727 if (rc) 1728 goto err_register; 1729 1730 /* Free pin properties */ 1731 zl3073x_pin_props_put(props); 1732 1733 return 0; 1734 1735 err_register: 1736 dpll_pin_put(pin->dpll_pin, &pin->tracker); 1737 err_pin_get: 1738 pin->dpll_pin = NULL; 1739 fwnode_handle_put(pin->fwnode); 1740 pin->fwnode = NULL; 1741 zl3073x_pin_props_put(props); 1742 1743 return rc; 1744 } 1745 1746 /** 1747 * zl3073x_dpll_pin_unregister - unregister DPLL pin 1748 * @pin: pointer to DPLL pin 1749 * 1750 * Unregisters pin previously registered by @zl3073x_dpll_pin_register. 1751 */ 1752 static void 1753 zl3073x_dpll_pin_unregister(struct zl3073x_dpll_pin *pin) 1754 { 1755 struct zl3073x_dpll *zldpll = pin->dpll; 1756 const struct dpll_pin_ops *ops; 1757 1758 WARN(!pin->dpll_pin, "DPLL pin is not registered\n"); 1759 1760 if (zl3073x_dpll_is_nco_pin(pin)) 1761 ops = &zl3073x_dpll_nco_pin_ops; 1762 else if (zl3073x_dpll_is_input_pin(pin)) 1763 ops = &zl3073x_dpll_input_pin_ops; 1764 else 1765 ops = &zl3073x_dpll_output_pin_ops; 1766 1767 /* Unregister the pin */ 1768 dpll_pin_unregister(zldpll->dpll_dev, pin->dpll_pin, ops, pin); 1769 1770 dpll_pin_put(pin->dpll_pin, &pin->tracker); 1771 pin->dpll_pin = NULL; 1772 1773 fwnode_handle_put(pin->fwnode); 1774 pin->fwnode = NULL; 1775 } 1776 1777 /** 1778 * zl3073x_dpll_pins_unregister - unregister all registered DPLL pins 1779 * @zldpll: pointer to zl3073x_dpll structure 1780 * 1781 * Enumerates all DPLL pins registered to given DPLL device and 1782 * unregisters them. 1783 */ 1784 static void 1785 zl3073x_dpll_pins_unregister(struct zl3073x_dpll *zldpll) 1786 { 1787 struct zl3073x_dpll_pin *pin, *next; 1788 1789 list_for_each_entry_safe(pin, next, &zldpll->pins, list) { 1790 zl3073x_dpll_pin_unregister(pin); 1791 list_del(&pin->list); 1792 zl3073x_dpll_pin_free(pin); 1793 } 1794 } 1795 1796 /** 1797 * zl3073x_dpll_pin_is_registrable - check if the pin is registrable 1798 * @zldpll: pointer to zl3073x_dpll structure 1799 * @dir: pin direction 1800 * @index: pin index 1801 * 1802 * Checks if the given pin can be registered to given DPLL. For both 1803 * directions the pin can be registered if it is enabled. In case of 1804 * differential signal type only P-pin is reported as registrable. 1805 * And additionally for the output pin, the pin can be registered only 1806 * if it is connected to synthesizer that is driven by given DPLL. 1807 * 1808 * Return: true if the pin is registrable, false if not 1809 */ 1810 static bool 1811 zl3073x_dpll_pin_is_registrable(struct zl3073x_dpll *zldpll, 1812 enum dpll_pin_direction dir, u8 index) 1813 { 1814 struct zl3073x_dev *zldev = zldpll->dev; 1815 bool is_diff, is_enabled; 1816 const char *name; 1817 1818 if (dir == DPLL_PIN_DIRECTION_INPUT) { 1819 u8 ref_id = zl3073x_input_pin_ref_get(index); 1820 const struct zl3073x_ref *ref; 1821 1822 name = "REF"; 1823 ref = zl3073x_ref_state_get(zldev, ref_id); 1824 is_diff = zl3073x_ref_is_diff(ref); 1825 is_enabled = zl3073x_ref_is_enabled(ref); 1826 } else { 1827 /* Output P&N pair shares single HW output */ 1828 u8 out = zl3073x_output_pin_out_get(index); 1829 1830 /* Skip the pin if it is connected to different DPLL channel */ 1831 if (zl3073x_dev_out_dpll_get(zldev, out) != zldpll->id) { 1832 dev_dbg(zldev->dev, 1833 "OUT%u is driven by different DPLL\n", out); 1834 1835 return false; 1836 } 1837 1838 name = "OUT"; 1839 is_diff = zl3073x_dev_out_is_diff(zldev, out); 1840 is_enabled = zl3073x_dev_output_pin_is_enabled(zldev, index); 1841 } 1842 1843 /* Skip N-pin if the corresponding input/output is differential */ 1844 if (is_diff && zl3073x_is_n_pin(index)) { 1845 dev_dbg(zldev->dev, "%s%u is differential, skipping N-pin\n", 1846 name, index / 2); 1847 1848 return false; 1849 } 1850 1851 /* Skip the pin if it is disabled */ 1852 if (!is_enabled) { 1853 dev_dbg(zldev->dev, "%s%u%c is disabled\n", name, index / 2, 1854 zl3073x_is_p_pin(index) ? 'P' : 'N'); 1855 1856 return false; 1857 } 1858 1859 return true; 1860 } 1861 1862 static const struct dpll_pin_properties zl3073x_dpll_nco_pin_props = { 1863 .type = DPLL_PIN_TYPE_INT_NCO, 1864 .package_label = "NCO", 1865 .capabilities = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE, 1866 }; 1867 1868 static int 1869 zl3073x_dpll_nco_pin_register(struct zl3073x_dpll *zldpll) 1870 { 1871 struct zl3073x_dpll_pin *pin; 1872 struct zl3073x_chan chan; 1873 int rc; 1874 1875 /* Ensure that ctrl bits are configured for NCO operation: 1876 * - nco_auto_read: auto-capture tracking offset on NCO entry 1877 * - tod_step_reset: apply negated ToD step on NCO exit 1878 * - tie_clear: PPS DPLLs re-align outputs on NCO exit 1879 */ 1880 mutex_lock(&zldpll->lock); 1881 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1882 FIELD_MODIFY(ZL_DPLL_CTRL_NCO_AUTO_READ, &chan.ctrl, 1); 1883 FIELD_MODIFY(ZL_DPLL_CTRL_TOD_STEP_RST, &chan.ctrl, 1); 1884 FIELD_MODIFY(ZL_DPLL_CTRL_TIE_CLEAR, &chan.ctrl, 1885 zldpll->type == DPLL_TYPE_PPS ? 1 : 0); 1886 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 1887 mutex_unlock(&zldpll->lock); 1888 if (rc) 1889 return rc; 1890 1891 pin = zl3073x_dpll_pin_alloc(zldpll, DPLL_PIN_DIRECTION_INPUT, 1892 ZL3073X_NCO_PIN_ID); 1893 if (IS_ERR(pin)) 1894 return PTR_ERR(pin); 1895 1896 pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, ZL3073X_NCO_PIN_ID, 1897 THIS_MODULE, &zl3073x_dpll_nco_pin_props, 1898 &pin->tracker); 1899 if (IS_ERR(pin->dpll_pin)) { 1900 rc = PTR_ERR(pin->dpll_pin); 1901 goto err_pin_get; 1902 } 1903 1904 rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin, 1905 &zl3073x_dpll_nco_pin_ops, pin); 1906 if (rc) 1907 goto err_register; 1908 1909 list_add(&pin->list, &zldpll->pins); 1910 1911 return 0; 1912 1913 err_register: 1914 dpll_pin_put(pin->dpll_pin, &pin->tracker); 1915 err_pin_get: 1916 pin->dpll_pin = NULL; 1917 kfree(pin); 1918 1919 return rc; 1920 } 1921 1922 /** 1923 * zl3073x_dpll_pins_register - register all registerable DPLL pins 1924 * @zldpll: pointer to zl3073x_dpll structure 1925 * 1926 * Enumerates all possible input/output pins and registers all of them 1927 * that are registrable. 1928 * 1929 * Return: 0 on success, <0 on error 1930 */ 1931 static int 1932 zl3073x_dpll_pins_register(struct zl3073x_dpll *zldpll) 1933 { 1934 struct zl3073x_dpll_pin *pin; 1935 enum dpll_pin_direction dir; 1936 u8 id, index; 1937 int rc; 1938 1939 /* Process input pins */ 1940 for (index = 0; index < ZL3073X_NUM_PINS; index++) { 1941 /* First input pins and then output pins */ 1942 if (index < ZL3073X_NUM_INPUT_PINS) { 1943 id = index; 1944 dir = DPLL_PIN_DIRECTION_INPUT; 1945 } else { 1946 id = index - ZL3073X_NUM_INPUT_PINS; 1947 dir = DPLL_PIN_DIRECTION_OUTPUT; 1948 } 1949 1950 /* Check if the pin registrable to this DPLL */ 1951 if (!zl3073x_dpll_pin_is_registrable(zldpll, dir, id)) 1952 continue; 1953 1954 pin = zl3073x_dpll_pin_alloc(zldpll, dir, id); 1955 if (IS_ERR(pin)) { 1956 rc = PTR_ERR(pin); 1957 goto error; 1958 } 1959 1960 rc = zl3073x_dpll_pin_register(pin, index); 1961 if (rc) { 1962 zl3073x_dpll_pin_free(pin); 1963 goto error; 1964 } 1965 1966 list_add(&pin->list, &zldpll->pins); 1967 } 1968 1969 /* Register NCO virtual input pin */ 1970 rc = zl3073x_dpll_nco_pin_register(zldpll); 1971 if (rc) 1972 goto error; 1973 1974 return 0; 1975 1976 error: 1977 zl3073x_dpll_pins_unregister(zldpll); 1978 1979 return rc; 1980 } 1981 1982 /** 1983 * zl3073x_dpll_device_register - register DPLL device 1984 * @zldpll: pointer to zl3073x_dpll structure 1985 * 1986 * Registers given DPLL device into DPLL sub-system. 1987 * 1988 * Return: 0 on success, <0 on error 1989 */ 1990 static int 1991 zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll) 1992 { 1993 struct zl3073x_dev *zldev = zldpll->dev; 1994 int rc; 1995 1996 zldpll->ops = zl3073x_dpll_device_ops; 1997 if (zldev->info->flags & ZL3073X_FLAG_DIE_TEMP) 1998 zldpll->ops.temp_get = zl3073x_dpll_temp_get; 1999 2000 zldpll->dpll_dev = dpll_device_get(zldev->clock_id, zldpll->id, 2001 THIS_MODULE, &zldpll->tracker); 2002 if (IS_ERR(zldpll->dpll_dev)) { 2003 rc = PTR_ERR(zldpll->dpll_dev); 2004 zldpll->dpll_dev = NULL; 2005 2006 return rc; 2007 } 2008 2009 zldpll->type = zl3073x_prop_dpll_type_get(zldev, zldpll->id); 2010 rc = dpll_device_register(zldpll->dpll_dev, zldpll->type, 2011 &zldpll->ops, zldpll); 2012 if (rc) { 2013 dpll_device_put(zldpll->dpll_dev, &zldpll->tracker); 2014 zldpll->dpll_dev = NULL; 2015 } 2016 2017 return rc; 2018 } 2019 2020 /** 2021 * zl3073x_dpll_device_unregister - unregister DPLL device 2022 * @zldpll: pointer to zl3073x_dpll structure 2023 * 2024 * Unregisters given DPLL device from DPLL sub-system previously registered 2025 * by @zl3073x_dpll_device_register. 2026 */ 2027 static void 2028 zl3073x_dpll_device_unregister(struct zl3073x_dpll *zldpll) 2029 { 2030 struct dpll_device *dpll_dev = READ_ONCE(zldpll->dpll_dev); 2031 2032 WARN(!dpll_dev, "DPLL device is not registered\n"); 2033 2034 WRITE_ONCE(zldpll->dpll_dev, NULL); 2035 dpll_device_unregister(dpll_dev, &zldpll->ops, zldpll); 2036 dpll_device_put(dpll_dev, &zldpll->tracker); 2037 } 2038 2039 /** 2040 * zl3073x_dpll_pin_phase_offset_check - check for pin phase offset change 2041 * @pin: pin to check 2042 * 2043 * Check for the change of DPLL to connected pin phase offset change. 2044 * 2045 * Return: true on phase offset change, false otherwise 2046 */ 2047 static bool 2048 zl3073x_dpll_pin_phase_offset_check(struct zl3073x_dpll_pin *pin) 2049 { 2050 struct zl3073x_dpll *zldpll = pin->dpll; 2051 struct zl3073x_dev *zldev = zldpll->dev; 2052 unsigned int reg; 2053 s64 phase_offset; 2054 u8 ref_id; 2055 int rc; 2056 2057 lockdep_assert_held(&zldpll->lock); 2058 2059 /* No phase offset if the ref monitor reports signal errors */ 2060 ref_id = zl3073x_input_pin_ref_get(pin->id); 2061 if (!zl3073x_dev_ref_is_status_ok(zldev, ref_id)) 2062 return false; 2063 2064 /* Select register to read phase offset value depending on pin and 2065 * phase monitor state: 2066 * 1) For connected pin use dpll_phase_err_data register 2067 * 2) For other pins use appropriate ref_phase register if the phase 2068 * monitor feature is enabled. 2069 */ 2070 if (pin->operstate == DPLL_PIN_OPERSTATE_ACTIVE) 2071 reg = ZL_REG_DPLL_PHASE_ERR_DATA(zldpll->id); 2072 else if (zldpll->phase_monitor) 2073 reg = ZL_REG_REF_PHASE(ref_id); 2074 else 2075 return false; 2076 2077 /* Read measured phase offset value */ 2078 rc = zl3073x_read_u48(zldev, reg, &phase_offset); 2079 if (rc) { 2080 dev_err(zldev->dev, "Failed to read ref phase offset: %pe\n", 2081 ERR_PTR(rc)); 2082 2083 return false; 2084 } 2085 2086 /* Convert to ps */ 2087 phase_offset = div_s64(sign_extend64(phase_offset, 47), 100); 2088 2089 /* Compare with previous value */ 2090 if (phase_offset != pin->phase_offset) { 2091 dev_dbg(zldev->dev, "%s phase offset changed: %lld -> %lld\n", 2092 pin->label, pin->phase_offset, phase_offset); 2093 pin->phase_offset = phase_offset; 2094 2095 return true; 2096 } 2097 2098 return false; 2099 } 2100 2101 /** 2102 * zl3073x_dpll_pin_ffo_check - check for FFO change on active pin 2103 * @pin: pin to check 2104 * 2105 * Return: true on change, false otherwise 2106 */ 2107 static bool 2108 zl3073x_dpll_pin_ffo_check(struct zl3073x_dpll_pin *pin) 2109 { 2110 struct zl3073x_dpll *zldpll = pin->dpll; 2111 struct zl3073x_dev *zldev = zldpll->dev; 2112 const struct zl3073x_chan *chan; 2113 s64 ffo; 2114 2115 lockdep_assert_held(&zldpll->lock); 2116 2117 if (pin->operstate != DPLL_PIN_OPERSTATE_ACTIVE) 2118 return false; 2119 2120 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 2121 if (zl3073x_chan_df_offset_get(chan) == ZL_DPLL_DF_OFFSET_UNKNOWN) 2122 return false; 2123 2124 /* dpll_df_offset register has inverted sign per datasheet: 2125 * f_offset = f_nom * (-df_offset) / 2^48 2126 * Input pin FFO is pin-vs-DPLL (opposite of DPLL-vs-reference), 2127 * so the two inversions cancel out: ppt = df * 5^12 / 2^36 2128 */ 2129 ffo = mul_s64_u64_shr(zl3073x_chan_df_offset_get(chan), 2130 244140625, 36); 2131 2132 if (pin->freq_offset != ffo) { 2133 dev_dbg(zldev->dev, "%s freq offset changed to: %lld\n", 2134 pin->label, ffo); 2135 pin->freq_offset = ffo; 2136 return true; 2137 } 2138 2139 return false; 2140 } 2141 2142 /** 2143 * zl3073x_dpll_pin_measured_freq_check - check for pin measured frequency 2144 * change 2145 * @pin: pin to check 2146 * 2147 * Check for the given pin's measured frequency change. 2148 * 2149 * Return: true on measured frequency change, false otherwise 2150 */ 2151 static bool 2152 zl3073x_dpll_pin_measured_freq_check(struct zl3073x_dpll_pin *pin) 2153 { 2154 struct zl3073x_dpll *zldpll = pin->dpll; 2155 struct zl3073x_dev *zldev = zldpll->dev; 2156 const struct zl3073x_ref *ref; 2157 u8 ref_id; 2158 u32 freq; 2159 2160 lockdep_assert_held(&zldpll->lock); 2161 2162 if (!READ_ONCE(zldpll->dev->freq_monitor)) 2163 return false; 2164 2165 ref_id = zl3073x_input_pin_ref_get(pin->id); 2166 ref = zl3073x_ref_state_get(zldev, ref_id); 2167 2168 freq = zl3073x_ref_meas_freq_get(ref); 2169 if (pin->measured_freq != freq) { 2170 dev_dbg(zldev->dev, "%s measured freq changed: %u -> %u\n", 2171 pin->label, pin->measured_freq, freq); 2172 pin->measured_freq = freq; 2173 2174 return true; 2175 } 2176 2177 return false; 2178 } 2179 2180 /** 2181 * zl3073x_dpll_changes_check - check for changes and send notifications 2182 * @zldpll: pointer to zl3073x_dpll structure 2183 * 2184 * Checks for changes on given DPLL device and its registered DPLL pins 2185 * and sends notifications about them. 2186 * 2187 * This function is periodically called from @zl3073x_dev_periodic_work. 2188 */ 2189 void 2190 zl3073x_dpll_changes_check(struct zl3073x_dpll *zldpll) 2191 { 2192 DECLARE_BITMAP(changed_pins, ZL3073X_NUM_INPUT_PINS); 2193 struct zl3073x_dev *zldev = zldpll->dev; 2194 enum dpll_lock_status lock_status; 2195 struct device *dev = zldev->dev; 2196 struct zl3073x_dpll_pin *pin; 2197 bool dev_changed = false; 2198 int rc; 2199 2200 bitmap_zero(changed_pins, ZL3073X_NUM_INPUT_PINS); 2201 2202 mutex_lock(&zldpll->lock); 2203 2204 zldpll->check_count++; 2205 2206 rc = zl3073x_chan_state_update(zldev, zldpll->id); 2207 if (rc) { 2208 dev_err(dev, "Failed to get DPLL%u state: %pe\n", 2209 zldpll->id, ERR_PTR(rc)); 2210 goto unlock; 2211 } 2212 2213 rc = __zl3073x_dpll_lock_status_get(zldpll, &lock_status); 2214 if (rc) { 2215 dev_err(dev, "Failed to get DPLL%u lock status: %pe\n", 2216 zldpll->id, ERR_PTR(rc)); 2217 goto unlock; 2218 } 2219 2220 if (zldpll->lock_status != lock_status) { 2221 zldpll->lock_status = lock_status; 2222 dev_changed = true; 2223 } 2224 2225 /* Update phase offset latch registers for this DPLL if the phase 2226 * offset monitor feature is enabled. 2227 */ 2228 if (zldpll->phase_monitor) { 2229 rc = zl3073x_ref_phase_offsets_update(zldev, zldpll->id); 2230 if (rc) { 2231 dev_err(zldev->dev, 2232 "Failed to update phase offsets: %pe\n", 2233 ERR_PTR(rc)); 2234 goto unlock; 2235 } 2236 } 2237 2238 list_for_each_entry(pin, &zldpll->pins, list) { 2239 enum dpll_pin_operstate operstate; 2240 2241 /* Only physical input pins need monitoring */ 2242 if (!zl3073x_dpll_is_input_pin(pin) || 2243 zl3073x_dpll_is_nco_pin(pin)) 2244 continue; 2245 2246 rc = zl3073x_dpll_ref_operstate_get(pin, &operstate); 2247 if (rc) { 2248 dev_err(dev, 2249 "Failed to get %s on DPLL%u oper state: %pe\n", 2250 pin->label, zldpll->id, ERR_PTR(rc)); 2251 goto unlock; 2252 } 2253 2254 if (operstate != pin->operstate) { 2255 dev_dbg(dev, "%s oper state changed: %u->%u\n", 2256 pin->label, pin->operstate, operstate); 2257 pin->operstate = operstate; 2258 set_bit(pin->id, changed_pins); 2259 } 2260 2261 if (zldpll->check_count % 2 == 0) { 2262 if (zl3073x_dpll_pin_phase_offset_check(pin)) 2263 set_bit(pin->id, changed_pins); 2264 2265 if (zl3073x_dpll_pin_ffo_check(pin)) 2266 set_bit(pin->id, changed_pins); 2267 2268 if (zl3073x_dpll_pin_measured_freq_check(pin)) 2269 set_bit(pin->id, changed_pins); 2270 } 2271 } 2272 2273 unlock: 2274 mutex_unlock(&zldpll->lock); 2275 2276 /* Send notifications outside the lock to avoid ABBA deadlock 2277 * with dpll_lock taken by notification functions. 2278 */ 2279 if (dev_changed) 2280 dpll_device_change_ntf(zldpll->dpll_dev); 2281 2282 list_for_each_entry(pin, &zldpll->pins, list) { 2283 if (zl3073x_dpll_is_input_pin(pin) && 2284 !zl3073x_dpll_is_nco_pin(pin) && 2285 test_bit(pin->id, changed_pins)) 2286 dpll_pin_change_ntf(pin->dpll_pin); 2287 } 2288 } 2289 2290 /** 2291 * zl3073x_dpll_init_fine_phase_adjust - do initial fine phase adjustments 2292 * @zldev: pointer to zl3073x device 2293 * 2294 * Performs initial fine phase adjustments needed per datasheet. 2295 * 2296 * Return: 0 on success, <0 on error 2297 */ 2298 int 2299 zl3073x_dpll_init_fine_phase_adjust(struct zl3073x_dev *zldev) 2300 { 2301 int rc; 2302 2303 rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_MASK, 0x1f); 2304 if (rc) 2305 return rc; 2306 2307 rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_INTVL, 0x01); 2308 if (rc) 2309 return rc; 2310 2311 rc = zl3073x_write_u16(zldev, ZL_REG_SYNTH_PHASE_SHIFT_DATA, 0xffff); 2312 if (rc) 2313 return rc; 2314 2315 return zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_CTRL, 0x01); 2316 } 2317 2318 /* Maximum frequency adjustment: +-1% of nominal in ppb */ 2319 #define ZL3073X_DPLL_PTP_MAX_ADJ 10000000 2320 2321 /** 2322 * zl3073x_dpll_ptp_gettimex64 - read current time from ToD counters 2323 * @info: PTP clock info 2324 * @ts: timespec to store current time 2325 * @sts: optional system timestamp pair for cross-timestamping 2326 * 2327 * Return: 0 on success, <0 on error 2328 */ 2329 static int zl3073x_dpll_ptp_gettimex64(struct ptp_clock_info *info, 2330 struct timespec64 *ts, 2331 struct ptp_system_timestamp *sts) 2332 { 2333 struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, 2334 ptp_info); 2335 2336 guard(mutex)(&zldpll->lock); 2337 2338 return zl3073x_chan_tod_read(zldpll->dev, zldpll->id, false, ts, sts); 2339 } 2340 2341 /** 2342 * zl3073x_dpll_ptp_settime64 - set ToD counters to given time 2343 * @info: PTP clock info 2344 * @ts: timespec with time to set 2345 * 2346 * Return: 0 on success, <0 on error 2347 */ 2348 static int zl3073x_dpll_ptp_settime64(struct ptp_clock_info *info, 2349 const struct timespec64 *ts) 2350 { 2351 struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, 2352 ptp_info); 2353 2354 guard(mutex)(&zldpll->lock); 2355 2356 return zl3073x_chan_tod_write(zldpll->dev, zldpll->id, *ts); 2357 } 2358 2359 /** 2360 * zl3073x_dpll_ptp_adjtime_phase_step - adjust sub-second time via phase step 2361 * @zldpll: DPLL channel 2362 * @delta: time adjustment in nanoseconds (must be within (-NSEC_PER_SEC, 2363 * NSEC_PER_SEC)) 2364 * 2365 * Uses the output phase step mechanism with tod_step=1 to adjust both 2366 * the output clock phase and the ToD counter simultaneously. This keeps 2367 * outputs and ToD coherent. Only valid for NCO. 2368 * 2369 * Outputs are grouped by synthesizer since the phase step value is in 2370 * synthesizer clock cycles. The first synth group with enabled outputs 2371 * uses tod_step to adjust both outputs and the ToD counter. Remaining 2372 * groups step outputs only. If no synth has enabled outputs, the ToD 2373 * counter is stepped alone using an empty output mask (the FW uses 2374 * the first enabled synth's period for the conversion). 2375 * 2376 * Return: 2377 * * %0 - success (or partial success if a later synth group 2378 * failed after the first was already stepped) 2379 * * %-EOPNOTSUPP - no synths available 2380 * * negative - error 2381 */ 2382 static int zl3073x_dpll_ptp_adjtime_phase_step(struct zl3073x_dpll *zldpll, 2383 s64 delta) 2384 { 2385 u16 synth_mask[ZL3073X_NUM_SYNTHS] = {}; 2386 struct zl3073x_dev *zldev = zldpll->dev; 2387 const struct zl3073x_synth *synth; 2388 struct zl3073x_dpll_pin *pin; 2389 u32 first_synth_freq = 0; 2390 bool tod_stepped = false; 2391 s32 step_cycles; 2392 u32 synth_freq; 2393 int rc; 2394 u8 i; 2395 2396 /* Build per-synth output masks from registered output pins */ 2397 list_for_each_entry(pin, &zldpll->pins, list) { 2398 u8 out_id, synth_id; 2399 2400 if (zl3073x_dpll_is_input_pin(pin)) 2401 continue; 2402 2403 out_id = zl3073x_output_pin_out_get(pin->id); 2404 2405 if (!zl3073x_dev_out_is_stepped(zldev, out_id)) 2406 continue; 2407 2408 synth_id = zl3073x_dev_out_synth_get(zldev, out_id); 2409 if (synth_id >= ZL3073X_NUM_SYNTHS) { 2410 dev_warn(zldev->dev, "Unexpected synth id for OUT%u\n", 2411 out_id); 2412 continue; 2413 } 2414 synth_mask[synth_id] |= BIT(out_id); 2415 } 2416 2417 /* Process each synth group */ 2418 for (i = 0; i < ZL3073X_NUM_SYNTHS; i++) { 2419 synth = zl3073x_synth_state_get(zldev, i); 2420 if (!zl3073x_synth_is_enabled(synth) || 2421 zl3073x_synth_dpll_get(synth) != zldpll->id) 2422 continue; 2423 2424 synth_freq = zl3073x_synth_freq_get(synth); 2425 2426 /* Remember first enabled synth freq for ToD-only fallback */ 2427 if (!first_synth_freq) 2428 first_synth_freq = synth_freq; 2429 2430 if (!synth_mask[i]) 2431 continue; 2432 2433 /* Safe for s32: max synth freq is 750 MHz */ 2434 step_cycles = div_s64(delta * synth_freq, NSEC_PER_SEC); 2435 2436 rc = zl3073x_chan_phase_step(zldev, zldpll->id, 2437 synth_mask[i], step_cycles, 2438 !tod_stepped); 2439 if (rc) { 2440 if (tod_stepped) { 2441 dev_warn(zldev->dev, 2442 "Partial phase step failure\n"); 2443 return 0; 2444 } 2445 return rc; 2446 } 2447 tod_stepped = true; 2448 } 2449 2450 if (!first_synth_freq) 2451 return -EOPNOTSUPP; 2452 2453 /* No enabled outputs found; step ToD counter only using the 2454 * first enabled synth's period (empty output mask). 2455 */ 2456 if (!tod_stepped) { 2457 step_cycles = div_s64(delta * first_synth_freq, NSEC_PER_SEC); 2458 return zl3073x_chan_phase_step(zldev, zldpll->id, 0, 2459 step_cycles, true); 2460 } 2461 2462 return 0; 2463 } 2464 2465 /** 2466 * zl3073x_dpll_ptp_adjtime - adjust PTP clock time 2467 * @info: PTP clock info 2468 * @delta: time adjustment in nanoseconds 2469 * 2470 * For NCO, large deltas (>= 1 second) are split into a ToD 2471 * read-modify-write for the seconds part and an output phase step for 2472 * the sub-second remainder. Sub-second deltas use phase step directly, 2473 * falling back to ToD read-modify-write if phase step or TIE write 2474 * fails. In AUTO/REFLOCK modes, large deltas are split into ToD 2475 * read-modify-write for seconds and TIE write for the sub-second 2476 * remainder. Sub-second deltas use TIE write directly. 2477 * 2478 * If the seconds part was already committed when the sub-second 2479 * mechanism fails, returns 0 to prevent the PTP servo from retrying 2480 * the full delta and applying seconds again. 2481 * 2482 * Return: 0 on success (or partial success), <0 on error 2483 */ 2484 static int zl3073x_dpll_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 2485 { 2486 struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, 2487 ptp_info); 2488 struct zl3073x_dev *zldev = zldpll->dev; 2489 const struct zl3073x_chan *chan; 2490 bool sec_adjusted = false; 2491 struct timespec64 ts; 2492 int rc; 2493 2494 if (!delta) 2495 return 0; 2496 2497 guard(mutex)(&zldpll->lock); 2498 2499 /* Modes without phase step or TIE use plain ToD adjust */ 2500 chan = zl3073x_chan_state_get(zldev, zldpll->id); 2501 if (!zl3073x_chan_mode_is_nco(chan) && 2502 !zl3073x_chan_mode_supports_tie(chan)) 2503 return zl3073x_chan_tod_adjust(zldev, zldpll->id, 2504 ns_to_timespec64(delta)); 2505 2506 /* Split off seconds via ToD read-modify-write so the sub-second 2507 * remainder can be applied through the output-coherent mechanism 2508 * (phase step or TIE write). 2509 */ 2510 if (delta >= NSEC_PER_SEC || delta <= -NSEC_PER_SEC) { 2511 s32 remainder; 2512 2513 ts.tv_sec = div_s64_rem(delta, NSEC_PER_SEC, &remainder); 2514 ts.tv_nsec = 0; 2515 delta = remainder; 2516 2517 rc = zl3073x_chan_tod_adjust(zldev, zldpll->id, ts); 2518 if (rc) 2519 return rc; 2520 2521 /* No sub-second remainder, done */ 2522 if (!delta) 2523 return 0; 2524 2525 /* Wait for the ToD write to be applied at the 1 Hz edge 2526 * before issuing phase step or TIE write, so the pending 2527 * WR_NEXT_1HZ does not overwrite the sub-second adjustment. 2528 */ 2529 rc = zl3073x_chan_tod_ready_wait(zldev, zldpll->id); 2530 if (rc) 2531 return rc; 2532 2533 sec_adjusted = true; 2534 } 2535 2536 /* Apply sub-second delta via phase step (NCO) or TIE write */ 2537 if (zl3073x_chan_mode_is_nco(chan)) { 2538 rc = zl3073x_dpll_ptp_adjtime_phase_step(zldpll, delta); 2539 if (!rc) 2540 return 0; 2541 } else { 2542 rc = zl3073x_chan_tie_write(zldev, zldpll->id, delta); 2543 if (!rc) 2544 return 0; 2545 } 2546 2547 /* Phase step or TIE write failed, fall back to ToD adjust */ 2548 rc = zl3073x_chan_tod_adjust(zldev, zldpll->id, 2549 ns_to_timespec64(delta)); 2550 2551 /* In the unlikely event that both phase step/TIE write and fallback 2552 * ToD adjust fail after seconds were already committed, return 2553 * success to prevent the PTP servo from retrying the full delta and 2554 * applying seconds again. The sub-second residual will self-correct 2555 * in the next servo cycle. 2556 */ 2557 if (rc && sec_adjusted) { 2558 dev_warn(zldev->dev, 2559 "Sub-second adjustment failed after seconds applied\n"); 2560 return 0; 2561 } 2562 2563 return rc; 2564 } 2565 2566 /** 2567 * zl3073x_dpll_ptp_adjfine - adjust PTP clock frequency 2568 * @info: PTP clock info 2569 * @scaled_ppm: frequency adjustment in scaled ppm (ppm * 2^16) 2570 * 2571 * Only supported for NCO. Writes the delta frequency offset register. 2572 * 2573 * Return: 2574 * * %0 - success or @scaled_ppm is zero (no-op) 2575 * * %-EOPNOTSUPP - NCO pin is not connected and @scaled_ppm is non-zero 2576 * * negative - other error 2577 */ 2578 static int 2579 zl3073x_dpll_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 2580 { 2581 struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, 2582 ptp_info); 2583 const struct zl3073x_chan *chan; 2584 s64 offset; 2585 2586 /* Convert scaled_ppm to df_offset in 2^-48 steps: 2587 * df_offset = -(scaled_ppm * 2^32) / 10^6 2588 * 2589 * Simplify to avoid overflow: 2590 * df_offset = -(scaled_ppm * 2^26) / 5^6 2591 * df_offset = -(scaled_ppm * 67108864) / 15625 2592 */ 2593 offset = -div_s64((s64)scaled_ppm * 67108864LL, 15625); 2594 2595 guard(mutex)(&zldpll->lock); 2596 2597 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 2598 if (!zl3073x_chan_mode_is_nco(chan)) 2599 return scaled_ppm ? -EOPNOTSUPP : 0; 2600 if (offset == chan->df_offset) 2601 return 0; 2602 2603 return zl3073x_chan_df_offset_set(zldpll->dev, zldpll->id, offset); 2604 } 2605 2606 /** 2607 * zl3073x_dpll_ptp_adjphase - adjust PTP clock phase 2608 * @info: PTP clock info 2609 * @delta: phase adjustment in nanoseconds 2610 * 2611 * Only supported in AUTO and REFLOCK modes. Uses TIE write for 2612 * nanosecond resolution phase adjustment. 2613 * 2614 * Return: 2615 * * %0 - success or @delta is zero (no-op) 2616 * * %-EOPNOTSUPP - mode does not support TIE and @delta is non-zero 2617 * * negative - other error 2618 */ 2619 static int zl3073x_dpll_ptp_adjphase(struct ptp_clock_info *info, s32 delta) 2620 { 2621 struct zl3073x_dpll *zldpll = container_of(info, struct zl3073x_dpll, 2622 ptp_info); 2623 struct zl3073x_dev *zldev = zldpll->dev; 2624 const struct zl3073x_chan *chan; 2625 2626 if (!delta) 2627 return 0; 2628 2629 guard(mutex)(&zldpll->lock); 2630 2631 chan = zl3073x_chan_state_get(zldev, zldpll->id); 2632 2633 if (!zl3073x_chan_mode_supports_tie(chan)) 2634 return -EOPNOTSUPP; 2635 2636 return zl3073x_chan_tie_write(zldev, zldpll->id, delta); 2637 } 2638 2639 static s32 2640 zl3073x_dpll_ptp_getmaxphase(struct ptp_clock_info *info __always_unused) 2641 { 2642 /* HW limits TIE write to +-1 second. Return the constant HW 2643 * limit and let adjphase handle mode-specific checks. 2644 */ 2645 return NSEC_PER_SEC - 1; 2646 } 2647 2648 static const struct ptp_clock_info zl3073x_dpll_ptp_clock_info = { 2649 .owner = THIS_MODULE, 2650 .max_adj = ZL3073X_DPLL_PTP_MAX_ADJ, 2651 .gettimex64 = zl3073x_dpll_ptp_gettimex64, 2652 .settime64 = zl3073x_dpll_ptp_settime64, 2653 .adjtime = zl3073x_dpll_ptp_adjtime, 2654 .adjfine = zl3073x_dpll_ptp_adjfine, 2655 .adjphase = zl3073x_dpll_ptp_adjphase, 2656 .getmaxphase = zl3073x_dpll_ptp_getmaxphase, 2657 }; 2658 2659 /** 2660 * zl3073x_dpll_ptp_register - register PTP clock for a DPLL channel 2661 * @zldpll: DPLL channel to register PTP clock for 2662 * 2663 * Return: 0 on success, <0 on error 2664 */ 2665 static int zl3073x_dpll_ptp_register(struct zl3073x_dpll *zldpll) 2666 { 2667 struct zl3073x_dev *zldev = zldpll->dev; 2668 struct ptp_clock *ptp_clock; 2669 2670 zldpll->ptp_info = zl3073x_dpll_ptp_clock_info; 2671 snprintf(zldpll->ptp_info.name, sizeof(zldpll->ptp_info.name), 2672 "%s-dpll%u", dev_name(zldev->dev), zldpll->id); 2673 2674 ptp_clock = ptp_clock_register(&zldpll->ptp_info, zldev->dev); 2675 if (IS_ERR(ptp_clock)) { 2676 dev_err(zldev->dev, "Failed to register PTP clock for DPLL%u\n", 2677 zldpll->id); 2678 return PTR_ERR(ptp_clock); 2679 } 2680 2681 zldpll->ptp_clock = ptp_clock; 2682 2683 return 0; 2684 } 2685 2686 /** 2687 * zl3073x_dpll_ptp_unregister - unregister PTP clock for a DPLL channel 2688 * @zldpll: DPLL channel to unregister PTP clock for 2689 */ 2690 static void zl3073x_dpll_ptp_unregister(struct zl3073x_dpll *zldpll) 2691 { 2692 if (!IS_ERR_OR_NULL(zldpll->ptp_clock)) { 2693 ptp_clock_unregister(zldpll->ptp_clock); 2694 zldpll->ptp_clock = NULL; 2695 } 2696 } 2697 2698 /** 2699 * zl3073x_dpll_ref_sync_pair_register - register ref_sync pairs for a pin 2700 * @pin: pointer to zl3073x_dpll_pin structure 2701 * 2702 * Iterates 'ref-sync-sources' phandles in the pin's firmware node and 2703 * registers each declared pairing. 2704 * 2705 * Return: 0 on success, <0 on error 2706 */ 2707 static int 2708 zl3073x_dpll_ref_sync_pair_register(struct zl3073x_dpll_pin *pin) 2709 { 2710 struct zl3073x_dev *zldev = pin->dpll->dev; 2711 struct fwnode_handle *fwnode; 2712 struct dpll_pin *sync_pin; 2713 dpll_tracker tracker; 2714 int n, rc; 2715 2716 for (n = 0; ; n++) { 2717 /* Get n'th ref-sync source */ 2718 fwnode = fwnode_find_reference(pin->fwnode, "ref-sync-sources", 2719 n); 2720 if (IS_ERR(fwnode)) { 2721 rc = PTR_ERR(fwnode); 2722 break; 2723 } 2724 2725 /* Find associated dpll pin */ 2726 sync_pin = fwnode_dpll_pin_find(fwnode, &tracker); 2727 fwnode_handle_put(fwnode); 2728 if (!sync_pin) { 2729 dev_warn(zldev->dev, "%s: ref-sync source %d not found", 2730 pin->label, n); 2731 continue; 2732 } 2733 2734 /* Register new ref-sync pair */ 2735 rc = dpll_pin_ref_sync_pair_add(pin->dpll_pin, sync_pin); 2736 dpll_pin_put(sync_pin, &tracker); 2737 2738 /* -EBUSY means pairing already exists from another DPLL's 2739 * registration. 2740 */ 2741 if (rc && rc != -EBUSY) { 2742 dev_err(zldev->dev, 2743 "%s: failed to add ref-sync source %d: %pe", 2744 pin->label, n, ERR_PTR(rc)); 2745 break; 2746 } 2747 } 2748 2749 return rc != -ENOENT ? rc : 0; 2750 } 2751 2752 /** 2753 * zl3073x_dpll_ref_sync_pairs_register - register ref_sync pairs for a DPLL 2754 * @zldpll: pointer to zl3073x_dpll structure 2755 * 2756 * Iterates all registered input pins of the given DPLL and establishes 2757 * ref_sync pairings declared by 'ref-sync-sources' phandles in the 2758 * device tree. 2759 * 2760 * Return: 0 on success, <0 on error 2761 */ 2762 static int 2763 zl3073x_dpll_ref_sync_pairs_register(struct zl3073x_dpll *zldpll) 2764 { 2765 struct zl3073x_dpll_pin *pin; 2766 int rc; 2767 2768 list_for_each_entry(pin, &zldpll->pins, list) { 2769 if (!zl3073x_dpll_is_input_pin(pin) || !pin->fwnode) 2770 continue; 2771 2772 rc = zl3073x_dpll_ref_sync_pair_register(pin); 2773 if (rc) 2774 return rc; 2775 } 2776 2777 return 0; 2778 } 2779 2780 /** 2781 * zl3073x_dpll_alloc - allocate DPLL device 2782 * @zldev: pointer to zl3073x device 2783 * @ch: DPLL channel number 2784 * 2785 * Allocates DPLL device structure for given DPLL channel. 2786 * 2787 * Return: pointer to DPLL device on success, error pointer on error 2788 */ 2789 struct zl3073x_dpll * 2790 zl3073x_dpll_alloc(struct zl3073x_dev *zldev, u8 ch) 2791 { 2792 struct zl3073x_dpll *zldpll; 2793 2794 zldpll = kzalloc_obj(*zldpll); 2795 if (!zldpll) 2796 return ERR_PTR(-ENOMEM); 2797 2798 zldpll->dev = zldev; 2799 zldpll->id = ch; 2800 mutex_init(&zldpll->lock); 2801 INIT_LIST_HEAD(&zldpll->pins); 2802 2803 return zldpll; 2804 } 2805 2806 /** 2807 * zl3073x_dpll_free - free DPLL device 2808 * @zldpll: pointer to zl3073x_dpll structure 2809 * 2810 * Deallocates given DPLL device previously allocated by @zl3073x_dpll_alloc. 2811 */ 2812 void 2813 zl3073x_dpll_free(struct zl3073x_dpll *zldpll) 2814 { 2815 WARN(zldpll->dpll_dev, "DPLL device is still registered\n"); 2816 2817 mutex_destroy(&zldpll->lock); 2818 kfree(zldpll); 2819 } 2820 2821 /** 2822 * zl3073x_dpll_register - register DPLL device and all its pins 2823 * @zldpll: pointer to zl3073x_dpll structure 2824 * 2825 * Registers given DPLL device and all its pins into DPLL sub-system. 2826 * 2827 * Return: 0 on success, <0 on error 2828 */ 2829 int 2830 zl3073x_dpll_register(struct zl3073x_dpll *zldpll) 2831 { 2832 int rc; 2833 2834 rc = zl3073x_dpll_device_register(zldpll); 2835 if (rc) 2836 return rc; 2837 2838 rc = zl3073x_dpll_pins_register(zldpll); 2839 if (rc) { 2840 zl3073x_dpll_device_unregister(zldpll); 2841 return rc; 2842 } 2843 2844 rc = zl3073x_dpll_ref_sync_pairs_register(zldpll); 2845 if (rc) { 2846 zl3073x_dpll_pins_unregister(zldpll); 2847 zl3073x_dpll_device_unregister(zldpll); 2848 return rc; 2849 } 2850 2851 rc = zl3073x_dpll_ptp_register(zldpll); 2852 if (rc) { 2853 zl3073x_dpll_pins_unregister(zldpll); 2854 zl3073x_dpll_device_unregister(zldpll); 2855 return rc; 2856 } 2857 2858 return 0; 2859 } 2860 2861 /** 2862 * zl3073x_dpll_unregister - unregister DPLL device and its pins 2863 * @zldpll: pointer to zl3073x_dpll structure 2864 * 2865 * Unregisters given DPLL device and all its pins from DPLL sub-system 2866 * previously registered by @zl3073x_dpll_register. 2867 */ 2868 void 2869 zl3073x_dpll_unregister(struct zl3073x_dpll *zldpll) 2870 { 2871 zl3073x_dpll_ptp_unregister(zldpll); 2872 zl3073x_dpll_pins_unregister(zldpll); 2873 zl3073x_dpll_device_unregister(zldpll); 2874 } 2875