1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/atomic.h> 4 #include <linux/bits.h> 5 #include <linux/bitfield.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/slab.h> 18 #include <linux/sprintf.h> 19 20 #include "core.h" 21 #include "dpll.h" 22 #include "prop.h" 23 #include "regs.h" 24 25 #define ZL3073X_DPLL_REF_NONE ZL3073X_NUM_REFS 26 #define ZL3073X_DPLL_REF_IS_VALID(_ref) ((_ref) != ZL3073X_DPLL_REF_NONE) 27 28 /** 29 * struct zl3073x_dpll_pin - DPLL pin 30 * @list: this DPLL pin list entry 31 * @dpll: DPLL the pin is registered to 32 * @dpll_pin: pointer to registered dpll_pin 33 * @tracker: tracking object for the acquired reference 34 * @fwnode: firmware node handle 35 * @label: package label 36 * @dir: pin direction 37 * @id: pin id 38 * @prio: pin priority <0, 14> 39 * @esync_control: embedded sync is controllable 40 * @phase_gran: phase adjustment granularity 41 * @operstate: last saved operational state 42 * @phase_offset: last saved pin phase offset 43 * @freq_offset: last saved fractional frequency offset 44 * @measured_freq: last saved measured frequency 45 */ 46 struct zl3073x_dpll_pin { 47 struct list_head list; 48 struct zl3073x_dpll *dpll; 49 struct dpll_pin *dpll_pin; 50 dpll_tracker tracker; 51 struct fwnode_handle *fwnode; 52 char label[8]; 53 enum dpll_pin_direction dir; 54 u8 id; 55 u8 prio; 56 bool esync_control; 57 s32 phase_gran; 58 enum dpll_pin_operstate operstate; 59 s64 phase_offset; 60 atomic64_t freq_offset; 61 u32 measured_freq; 62 }; 63 64 /* 65 * Supported esync ranges for input and for output per output pair type 66 */ 67 static const struct dpll_pin_frequency esync_freq_ranges[] = { 68 DPLL_PIN_FREQUENCY_RANGE(0, 1), 69 }; 70 71 /** 72 * zl3073x_dpll_is_input_pin - check if the pin is input one 73 * @pin: pin to check 74 * 75 * Return: true if pin is input, false if pin is output. 76 */ 77 static bool 78 zl3073x_dpll_is_input_pin(struct zl3073x_dpll_pin *pin) 79 { 80 return pin->dir == DPLL_PIN_DIRECTION_INPUT; 81 } 82 83 /** 84 * zl3073x_dpll_is_p_pin - check if the pin is P-pin 85 * @pin: pin to check 86 * 87 * Return: true if the pin is P-pin, false if it is N-pin 88 */ 89 static bool 90 zl3073x_dpll_is_p_pin(struct zl3073x_dpll_pin *pin) 91 { 92 return zl3073x_is_p_pin(pin->id); 93 } 94 95 static int 96 zl3073x_dpll_pin_direction_get(const struct dpll_pin *dpll_pin, void *pin_priv, 97 const struct dpll_device *dpll, void *dpll_priv, 98 enum dpll_pin_direction *direction, 99 struct netlink_ext_ack *extack) 100 { 101 struct zl3073x_dpll_pin *pin = pin_priv; 102 103 *direction = pin->dir; 104 105 return 0; 106 } 107 108 static struct zl3073x_dpll_pin * 109 zl3073x_dpll_pin_get_by_ref(struct zl3073x_dpll *zldpll, u8 ref_id) 110 { 111 struct zl3073x_dpll_pin *pin; 112 113 list_for_each_entry(pin, &zldpll->pins, list) { 114 if (zl3073x_dpll_is_input_pin(pin) && 115 zl3073x_input_pin_ref_get(pin->id) == ref_id) 116 return pin; 117 } 118 119 return NULL; 120 } 121 122 static int 123 zl3073x_dpll_input_pin_esync_get(const struct dpll_pin *dpll_pin, 124 void *pin_priv, 125 const struct dpll_device *dpll, 126 void *dpll_priv, 127 struct dpll_pin_esync *esync, 128 struct netlink_ext_ack *extack) 129 { 130 struct zl3073x_dpll *zldpll = dpll_priv; 131 struct zl3073x_dev *zldev = zldpll->dev; 132 struct zl3073x_dpll_pin *pin = pin_priv; 133 const struct zl3073x_ref *ref; 134 u8 ref_id; 135 136 ref_id = zl3073x_input_pin_ref_get(pin->id); 137 ref = zl3073x_ref_state_get(zldev, ref_id); 138 139 if (!pin->esync_control || zl3073x_ref_freq_get(ref) <= 1) 140 return -EOPNOTSUPP; 141 142 esync->range = esync_freq_ranges; 143 esync->range_num = ARRAY_SIZE(esync_freq_ranges); 144 145 switch (zl3073x_ref_sync_mode_get(ref)) { 146 case ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75: 147 esync->freq = ref->esync_n_div == ZL_REF_ESYNC_DIV_1HZ ? 1 : 0; 148 esync->pulse = 25; 149 break; 150 default: 151 esync->freq = 0; 152 esync->pulse = 0; 153 break; 154 } 155 156 return 0; 157 } 158 159 static int 160 zl3073x_dpll_input_pin_esync_set(const struct dpll_pin *dpll_pin, 161 void *pin_priv, 162 const struct dpll_device *dpll, 163 void *dpll_priv, u64 freq, 164 struct netlink_ext_ack *extack) 165 { 166 struct zl3073x_dpll *zldpll = dpll_priv; 167 struct zl3073x_dev *zldev = zldpll->dev; 168 struct zl3073x_dpll_pin *pin = pin_priv; 169 struct zl3073x_ref ref; 170 u8 ref_id, sync_mode; 171 172 ref_id = zl3073x_input_pin_ref_get(pin->id); 173 ref = *zl3073x_ref_state_get(zldev, ref_id); 174 175 /* Use freq == 0 to disable esync */ 176 if (!freq) 177 sync_mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF; 178 else 179 sync_mode = ZL_REF_SYNC_CTRL_MODE_50_50_ESYNC_25_75; 180 181 zl3073x_ref_sync_mode_set(&ref, sync_mode); 182 183 if (freq) { 184 /* 1 Hz is only supported frequency now */ 185 ref.esync_n_div = ZL_REF_ESYNC_DIV_1HZ; 186 } 187 188 /* Update reference configuration */ 189 return zl3073x_ref_state_set(zldev, ref_id, &ref); 190 } 191 192 static int 193 zl3073x_dpll_input_pin_ref_sync_get(const struct dpll_pin *dpll_pin, 194 void *pin_priv, 195 const struct dpll_pin *ref_sync_pin, 196 void *ref_sync_pin_priv, 197 enum dpll_pin_state *state, 198 struct netlink_ext_ack *extack) 199 { 200 struct zl3073x_dpll_pin *sync_pin = ref_sync_pin_priv; 201 struct zl3073x_dpll_pin *pin = pin_priv; 202 struct zl3073x_dpll *zldpll = pin->dpll; 203 struct zl3073x_dev *zldev = zldpll->dev; 204 const struct zl3073x_ref *ref; 205 u8 ref_id, mode, pair; 206 207 ref_id = zl3073x_input_pin_ref_get(pin->id); 208 ref = zl3073x_ref_state_get(zldev, ref_id); 209 mode = zl3073x_ref_sync_mode_get(ref); 210 pair = zl3073x_ref_sync_pair_get(ref); 211 212 if (mode == ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR && 213 pair == zl3073x_input_pin_ref_get(sync_pin->id)) 214 *state = DPLL_PIN_STATE_CONNECTED; 215 else 216 *state = DPLL_PIN_STATE_DISCONNECTED; 217 218 return 0; 219 } 220 221 static int 222 zl3073x_dpll_input_pin_ref_sync_set(const struct dpll_pin *dpll_pin, 223 void *pin_priv, 224 const struct dpll_pin *ref_sync_pin, 225 void *ref_sync_pin_priv, 226 const enum dpll_pin_state state, 227 struct netlink_ext_ack *extack) 228 { 229 struct zl3073x_dpll_pin *sync_pin = ref_sync_pin_priv; 230 struct zl3073x_dpll_pin *pin = pin_priv; 231 struct zl3073x_dpll *zldpll = pin->dpll; 232 struct zl3073x_dev *zldev = zldpll->dev; 233 u8 mode, ref_id, sync_ref_id; 234 struct zl3073x_chan chan; 235 struct zl3073x_ref ref; 236 int rc; 237 238 ref_id = zl3073x_input_pin_ref_get(pin->id); 239 sync_ref_id = zl3073x_input_pin_ref_get(sync_pin->id); 240 ref = *zl3073x_ref_state_get(zldev, ref_id); 241 242 if (state == DPLL_PIN_STATE_CONNECTED) { 243 const struct zl3073x_ref *sync_ref; 244 u32 ref_freq, sync_freq; 245 246 sync_ref = zl3073x_ref_state_get(zldev, sync_ref_id); 247 ref_freq = zl3073x_ref_freq_get(&ref); 248 sync_freq = zl3073x_ref_freq_get(sync_ref); 249 250 /* Sync signal must be 8 kHz or less and clock reference 251 * must be 1 kHz or more and higher than the sync signal. 252 */ 253 if (sync_freq > 8000) { 254 NL_SET_ERR_MSG(extack, 255 "sync frequency must be 8 kHz or less"); 256 return -EINVAL; 257 } 258 if (ref_freq < 1000) { 259 NL_SET_ERR_MSG(extack, 260 "clock frequency must be 1 kHz or more"); 261 return -EINVAL; 262 } 263 if (ref_freq <= sync_freq) { 264 NL_SET_ERR_MSG(extack, 265 "clock frequency must be higher than sync frequency"); 266 return -EINVAL; 267 } 268 269 zl3073x_ref_sync_pair_set(&ref, sync_ref_id); 270 mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR; 271 } else { 272 mode = ZL_REF_SYNC_CTRL_MODE_REFSYNC_PAIR_OFF; 273 } 274 275 zl3073x_ref_sync_mode_set(&ref, mode); 276 277 rc = zl3073x_ref_state_set(zldev, ref_id, &ref); 278 if (rc) 279 return rc; 280 281 /* Exclude sync source from automatic reference selection by setting 282 * its priority to NONE. On disconnect the priority is left as NONE 283 * and the user must explicitly make the pin selectable again. 284 */ 285 if (state == DPLL_PIN_STATE_CONNECTED) { 286 chan = *zl3073x_chan_state_get(zldev, zldpll->id); 287 zl3073x_chan_ref_prio_set(&chan, sync_ref_id, 288 ZL_DPLL_REF_PRIO_NONE); 289 return zl3073x_chan_state_set(zldev, zldpll->id, &chan); 290 } 291 292 return 0; 293 } 294 295 static int 296 zl3073x_dpll_input_pin_ffo_get(const struct dpll_pin *dpll_pin, void *pin_priv, 297 const struct dpll_device *dpll, void *dpll_priv, 298 struct dpll_ffo_param *ffo, 299 struct netlink_ext_ack *extack) 300 { 301 struct zl3073x_dpll_pin *pin = pin_priv; 302 303 if (pin->operstate != DPLL_PIN_OPERSTATE_ACTIVE) 304 return -ENODATA; 305 306 ffo->ffo = atomic64_read(&pin->freq_offset); 307 308 return 0; 309 } 310 311 static int 312 zl3073x_dpll_input_pin_measured_freq_get(const struct dpll_pin *dpll_pin, 313 void *pin_priv, 314 const struct dpll_device *dpll, 315 void *dpll_priv, u64 *measured_freq, 316 struct netlink_ext_ack *extack) 317 { 318 struct zl3073x_dpll_pin *pin = pin_priv; 319 320 *measured_freq = pin->measured_freq; 321 *measured_freq *= DPLL_PIN_MEASURED_FREQUENCY_DIVIDER; 322 323 return 0; 324 } 325 326 static int 327 zl3073x_dpll_input_pin_frequency_get(const struct dpll_pin *dpll_pin, 328 void *pin_priv, 329 const struct dpll_device *dpll, 330 void *dpll_priv, u64 *frequency, 331 struct netlink_ext_ack *extack) 332 { 333 struct zl3073x_dpll *zldpll = dpll_priv; 334 struct zl3073x_dpll_pin *pin = pin_priv; 335 u8 ref_id; 336 337 ref_id = zl3073x_input_pin_ref_get(pin->id); 338 *frequency = zl3073x_dev_ref_freq_get(zldpll->dev, ref_id); 339 340 return 0; 341 } 342 343 static int 344 zl3073x_dpll_input_pin_frequency_set(const struct dpll_pin *dpll_pin, 345 void *pin_priv, 346 const struct dpll_device *dpll, 347 void *dpll_priv, u64 frequency, 348 struct netlink_ext_ack *extack) 349 { 350 struct zl3073x_dpll *zldpll = dpll_priv; 351 struct zl3073x_dev *zldev = zldpll->dev; 352 struct zl3073x_dpll_pin *pin = pin_priv; 353 struct zl3073x_ref ref; 354 u8 ref_id; 355 356 /* Get reference state */ 357 ref_id = zl3073x_input_pin_ref_get(pin->id); 358 ref = *zl3073x_ref_state_get(zldev, ref_id); 359 360 /* Update frequency */ 361 zl3073x_ref_freq_set(&ref, frequency); 362 363 /* Commit reference state */ 364 return zl3073x_ref_state_set(zldev, ref_id, &ref); 365 } 366 367 /** 368 * zl3073x_dpll_connected_ref_get - get currently connected reference 369 * @zldpll: pointer to zl3073x_dpll 370 * 371 * Looks for currently connected reference the DPLL is locked to. 372 * 373 * Return: reference index if locked, ZL3073X_DPLL_REF_NONE otherwise 374 */ 375 static u8 376 zl3073x_dpll_connected_ref_get(struct zl3073x_dpll *zldpll) 377 { 378 const struct zl3073x_chan *chan = zl3073x_chan_state_get(zldpll->dev, 379 zldpll->id); 380 u8 state; 381 382 /* A reference is connected only when the DPLL is locked to it */ 383 state = zl3073x_chan_refsel_state_get(chan); 384 if (state == ZL_DPLL_REFSEL_STATUS_STATE_LOCK) 385 return zl3073x_chan_refsel_ref_get(chan); 386 387 return ZL3073X_DPLL_REF_NONE; 388 } 389 390 static int 391 zl3073x_dpll_input_pin_phase_offset_get(const struct dpll_pin *dpll_pin, 392 void *pin_priv, 393 const struct dpll_device *dpll, 394 void *dpll_priv, s64 *phase_offset, 395 struct netlink_ext_ack *extack) 396 { 397 struct zl3073x_dpll *zldpll = dpll_priv; 398 struct zl3073x_dev *zldev = zldpll->dev; 399 struct zl3073x_dpll_pin *pin = pin_priv; 400 const struct zl3073x_ref *ref; 401 u8 conn_id, ref_id; 402 s64 ref_phase; 403 404 /* Get currently connected reference */ 405 conn_id = zl3073x_dpll_connected_ref_get(zldpll); 406 407 /* Report phase offset only for currently connected pin if the phase 408 * monitor feature is disabled and only if the input pin signal is 409 * present. 410 */ 411 ref_id = zl3073x_input_pin_ref_get(pin->id); 412 ref = zl3073x_ref_state_get(zldev, ref_id); 413 if ((!zldpll->phase_monitor && ref_id != conn_id) || 414 !zl3073x_ref_is_status_ok(ref)) { 415 *phase_offset = 0; 416 return 0; 417 } 418 419 ref_phase = pin->phase_offset; 420 421 /* The DPLL being locked to a higher freq than the current ref 422 * the phase offset is modded to the period of the signal 423 * the dpll is locked to. 424 */ 425 if (ZL3073X_DPLL_REF_IS_VALID(conn_id) && conn_id != ref_id) { 426 u32 conn_freq, ref_freq; 427 428 /* Get frequency of connected and given ref */ 429 conn_freq = zl3073x_dev_ref_freq_get(zldev, conn_id); 430 ref_freq = zl3073x_ref_freq_get(ref); 431 432 if (conn_freq > ref_freq) { 433 s64 conn_period, div_factor; 434 435 conn_period = div_s64(PSEC_PER_SEC, conn_freq); 436 div_factor = div64_s64(ref_phase, conn_period); 437 ref_phase -= conn_period * div_factor; 438 } 439 } 440 441 *phase_offset = ref_phase * DPLL_PHASE_OFFSET_DIVIDER; 442 443 return 0; 444 } 445 446 static int 447 zl3073x_dpll_input_pin_phase_adjust_get(const struct dpll_pin *dpll_pin, 448 void *pin_priv, 449 const struct dpll_device *dpll, 450 void *dpll_priv, 451 s32 *phase_adjust, 452 struct netlink_ext_ack *extack) 453 { 454 struct zl3073x_dpll *zldpll = dpll_priv; 455 struct zl3073x_dev *zldev = zldpll->dev; 456 struct zl3073x_dpll_pin *pin = pin_priv; 457 const struct zl3073x_ref *ref; 458 s64 phase_comp; 459 u8 ref_id; 460 461 /* Read reference configuration */ 462 ref_id = zl3073x_input_pin_ref_get(pin->id); 463 ref = zl3073x_ref_state_get(zldev, ref_id); 464 465 /* Perform sign extension based on register width */ 466 if (zl3073x_dev_is_ref_phase_comp_32bit(zldev)) 467 phase_comp = sign_extend64(ref->phase_comp, 31); 468 else 469 phase_comp = sign_extend64(ref->phase_comp, 47); 470 471 /* Reverse two's complement negation applied during set and convert 472 * to 32bit signed int 473 */ 474 *phase_adjust = (s32)-phase_comp; 475 476 return 0; 477 } 478 479 static int 480 zl3073x_dpll_input_pin_phase_adjust_set(const struct dpll_pin *dpll_pin, 481 void *pin_priv, 482 const struct dpll_device *dpll, 483 void *dpll_priv, 484 s32 phase_adjust, 485 struct netlink_ext_ack *extack) 486 { 487 struct zl3073x_dpll *zldpll = dpll_priv; 488 struct zl3073x_dev *zldev = zldpll->dev; 489 struct zl3073x_dpll_pin *pin = pin_priv; 490 struct zl3073x_ref ref; 491 u8 ref_id; 492 493 /* Read reference configuration */ 494 ref_id = zl3073x_input_pin_ref_get(pin->id); 495 ref = *zl3073x_ref_state_get(zldev, ref_id); 496 497 /* The value in the register is stored as two's complement negation 498 * of requested value. 499 */ 500 ref.phase_comp = -phase_adjust; 501 502 /* Update reference configuration */ 503 return zl3073x_ref_state_set(zldev, ref_id, &ref); 504 } 505 506 /** 507 * zl3073x_dpll_ref_operstate_get - get operational state for input pin 508 * @pin: pointer to pin 509 * @operstate: place to store operational state 510 * 511 * Returns the actual hardware state of the pin: whether it is actively 512 * used by the DPLL, has no signal, failed qualification, or is simply 513 * not in use. 514 * 515 * Return: 0 on success, <0 on error 516 */ 517 static int 518 zl3073x_dpll_ref_operstate_get(struct zl3073x_dpll_pin *pin, 519 enum dpll_pin_operstate *operstate) 520 { 521 struct zl3073x_dpll *zldpll = pin->dpll; 522 struct zl3073x_dev *zldev = zldpll->dev; 523 const struct zl3073x_ref *ref; 524 u8 ref_id; 525 526 ref_id = zl3073x_input_pin_ref_get(pin->id); 527 528 /* Check if this pin is the currently locked reference */ 529 if (ref_id == zl3073x_dpll_connected_ref_get(zldpll)) { 530 *operstate = DPLL_PIN_OPERSTATE_ACTIVE; 531 return 0; 532 } 533 534 /* Check reference monitor status */ 535 ref = zl3073x_ref_state_get(zldev, ref_id); 536 if (ref->mon_status & ZL_REF_MON_STATUS_LOS) 537 *operstate = DPLL_PIN_OPERSTATE_NO_SIGNAL; 538 else if (!zl3073x_ref_is_status_ok(ref)) 539 *operstate = DPLL_PIN_OPERSTATE_QUAL_FAILED; 540 else 541 *operstate = DPLL_PIN_OPERSTATE_STANDBY; 542 543 return 0; 544 } 545 546 static int 547 zl3073x_dpll_input_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, 548 void *pin_priv, 549 const struct dpll_device *dpll, 550 void *dpll_priv, 551 enum dpll_pin_state *state, 552 struct netlink_ext_ack *extack) 553 { 554 struct zl3073x_dpll *zldpll = dpll_priv; 555 struct zl3073x_dpll_pin *pin = pin_priv; 556 const struct zl3073x_chan *chan; 557 u8 mode, ref; 558 559 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 560 ref = zl3073x_input_pin_ref_get(pin->id); 561 mode = zl3073x_chan_mode_get(chan); 562 563 switch (mode) { 564 case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: 565 if (ref == zl3073x_chan_ref_get(chan)) 566 *state = DPLL_PIN_STATE_CONNECTED; 567 else 568 *state = DPLL_PIN_STATE_DISCONNECTED; 569 break; 570 case ZL_DPLL_MODE_REFSEL_MODE_AUTO: 571 if (zl3073x_chan_ref_is_selectable(chan, ref)) 572 *state = DPLL_PIN_STATE_SELECTABLE; 573 else 574 *state = DPLL_PIN_STATE_DISCONNECTED; 575 break; 576 default: 577 *state = DPLL_PIN_STATE_DISCONNECTED; 578 break; 579 } 580 581 return 0; 582 } 583 584 static int 585 zl3073x_dpll_input_pin_operstate_on_dpll_get(const struct dpll_pin *dpll_pin, 586 void *pin_priv, 587 const struct dpll_device *dpll, 588 void *dpll_priv, 589 enum dpll_pin_operstate *operstate, 590 struct netlink_ext_ack *extack) 591 { 592 struct zl3073x_dpll_pin *pin = pin_priv; 593 594 return zl3073x_dpll_ref_operstate_get(pin, operstate); 595 } 596 597 static int 598 zl3073x_dpll_input_pin_state_on_dpll_set(const struct dpll_pin *dpll_pin, 599 void *pin_priv, 600 const struct dpll_device *dpll, 601 void *dpll_priv, 602 enum dpll_pin_state state, 603 struct netlink_ext_ack *extack) 604 { 605 struct zl3073x_dpll *zldpll = dpll_priv; 606 struct zl3073x_dpll_pin *pin = pin_priv; 607 struct zl3073x_chan chan; 608 u8 mode, ref; 609 int rc; 610 611 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 612 ref = zl3073x_input_pin_ref_get(pin->id); 613 mode = zl3073x_chan_mode_get(&chan); 614 615 switch (mode) { 616 case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: 617 if (state == DPLL_PIN_STATE_CONNECTED) { 618 /* Choose the pin as new selected reference */ 619 zl3073x_chan_ref_set(&chan, ref); 620 } else if (state == DPLL_PIN_STATE_DISCONNECTED) { 621 /* Choose new mode based on lock status */ 622 switch (zldpll->lock_status) { 623 case DPLL_LOCK_STATUS_LOCKED_HO_ACQ: 624 case DPLL_LOCK_STATUS_HOLDOVER: 625 mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER; 626 break; 627 default: 628 mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN; 629 break; 630 } 631 zl3073x_chan_mode_set(&chan, mode); 632 } else { 633 goto invalid_state; 634 } 635 break; 636 case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: 637 case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: 638 if (state == DPLL_PIN_STATE_CONNECTED) { 639 /* Choose the pin as new selected reference */ 640 zl3073x_chan_ref_set(&chan, ref); 641 /* Switch to reflock mode */ 642 zl3073x_chan_mode_set(&chan, 643 ZL_DPLL_MODE_REFSEL_MODE_REFLOCK); 644 } else if (state != DPLL_PIN_STATE_DISCONNECTED) { 645 goto invalid_state; 646 } 647 break; 648 case ZL_DPLL_MODE_REFSEL_MODE_AUTO: 649 if (state == DPLL_PIN_STATE_SELECTABLE) { 650 if (zl3073x_chan_ref_is_selectable(&chan, ref)) 651 return 0; /* Pin is already selectable */ 652 653 /* Restore pin priority in HW */ 654 zl3073x_chan_ref_prio_set(&chan, ref, pin->prio); 655 } else if (state == DPLL_PIN_STATE_DISCONNECTED) { 656 if (!zl3073x_chan_ref_is_selectable(&chan, ref)) 657 return 0; /* Pin is already disconnected */ 658 659 /* Set pin priority to none in HW */ 660 zl3073x_chan_ref_prio_set(&chan, ref, 661 ZL_DPLL_REF_PRIO_NONE); 662 } else { 663 goto invalid_state; 664 } 665 break; 666 default: 667 /* In other modes we cannot change input reference */ 668 NL_SET_ERR_MSG(extack, 669 "Pin state cannot be changed in current mode"); 670 return -EOPNOTSUPP; 671 } 672 673 /* Commit DPLL channel changes */ 674 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 675 if (rc) 676 return rc; 677 678 return 0; 679 invalid_state: 680 NL_SET_ERR_MSG_MOD(extack, "Invalid pin state for this device mode"); 681 return -EINVAL; 682 } 683 684 static int 685 zl3073x_dpll_input_pin_prio_get(const struct dpll_pin *dpll_pin, void *pin_priv, 686 const struct dpll_device *dpll, void *dpll_priv, 687 u32 *prio, struct netlink_ext_ack *extack) 688 { 689 struct zl3073x_dpll_pin *pin = pin_priv; 690 691 *prio = pin->prio; 692 693 return 0; 694 } 695 696 static int 697 zl3073x_dpll_input_pin_prio_set(const struct dpll_pin *dpll_pin, void *pin_priv, 698 const struct dpll_device *dpll, void *dpll_priv, 699 u32 prio, struct netlink_ext_ack *extack) 700 { 701 struct zl3073x_dpll *zldpll = dpll_priv; 702 struct zl3073x_dpll_pin *pin = pin_priv; 703 struct zl3073x_chan chan; 704 u8 ref; 705 int rc; 706 707 if (prio > ZL_DPLL_REF_PRIO_MAX) 708 return -EINVAL; 709 710 /* If the pin is selectable then update HW registers */ 711 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 712 ref = zl3073x_input_pin_ref_get(pin->id); 713 if (zl3073x_chan_ref_is_selectable(&chan, ref)) { 714 zl3073x_chan_ref_prio_set(&chan, ref, prio); 715 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 716 if (rc) 717 return rc; 718 } 719 720 /* Save priority */ 721 pin->prio = prio; 722 723 return 0; 724 } 725 726 static int 727 zl3073x_dpll_output_pin_esync_get(const struct dpll_pin *dpll_pin, 728 void *pin_priv, 729 const struct dpll_device *dpll, 730 void *dpll_priv, 731 struct dpll_pin_esync *esync, 732 struct netlink_ext_ack *extack) 733 { 734 struct zl3073x_dpll *zldpll = dpll_priv; 735 struct zl3073x_dev *zldev = zldpll->dev; 736 struct zl3073x_dpll_pin *pin = pin_priv; 737 const struct zl3073x_synth *synth; 738 const struct zl3073x_out *out; 739 u32 synth_freq, out_freq; 740 u8 out_id; 741 742 out_id = zl3073x_output_pin_out_get(pin->id); 743 out = zl3073x_out_state_get(zldev, out_id); 744 745 /* If N-division is enabled, esync is not supported. The register used 746 * for N-division is also used for the esync divider so both cannot 747 * be used. 748 */ 749 if (zl3073x_out_is_ndiv(out)) 750 return -EOPNOTSUPP; 751 752 /* Get attached synth frequency */ 753 synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(out)); 754 synth_freq = zl3073x_synth_freq_get(synth); 755 out_freq = synth_freq / out->div; 756 757 if (!pin->esync_control || out_freq <= 1) 758 return -EOPNOTSUPP; 759 760 esync->range = esync_freq_ranges; 761 esync->range_num = ARRAY_SIZE(esync_freq_ranges); 762 763 if (zl3073x_out_clock_type_get(out) != ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC) { 764 /* No need to read esync data if it is not enabled */ 765 esync->freq = 0; 766 esync->pulse = 0; 767 768 return 0; 769 } 770 771 /* Compute esync frequency */ 772 esync->freq = out_freq / out->esync_n_period; 773 774 /* By comparing the esync_pulse_width to the half of the pulse width 775 * the esync pulse percentage can be determined. 776 * Note that half pulse width is in units of half synth cycles, which 777 * is why it reduces down to be output_div. 778 */ 779 esync->pulse = (50 * out->esync_n_width) / out->div; 780 781 return 0; 782 } 783 784 static int 785 zl3073x_dpll_output_pin_esync_set(const struct dpll_pin *dpll_pin, 786 void *pin_priv, 787 const struct dpll_device *dpll, 788 void *dpll_priv, u64 freq, 789 struct netlink_ext_ack *extack) 790 { 791 struct zl3073x_dpll *zldpll = dpll_priv; 792 struct zl3073x_dev *zldev = zldpll->dev; 793 struct zl3073x_dpll_pin *pin = pin_priv; 794 const struct zl3073x_synth *synth; 795 struct zl3073x_out out; 796 u32 synth_freq; 797 u8 out_id; 798 799 out_id = zl3073x_output_pin_out_get(pin->id); 800 out = *zl3073x_out_state_get(zldev, out_id); 801 802 /* If N-division is enabled, esync is not supported. The register used 803 * for N-division is also used for the esync divider so both cannot 804 * be used. 805 */ 806 if (zl3073x_out_is_ndiv(&out)) 807 return -EOPNOTSUPP; 808 809 /* Update clock type in output mode */ 810 if (freq) 811 zl3073x_out_clock_type_set(&out, 812 ZL_OUTPUT_MODE_CLOCK_TYPE_ESYNC); 813 else 814 zl3073x_out_clock_type_set(&out, 815 ZL_OUTPUT_MODE_CLOCK_TYPE_NORMAL); 816 817 /* If esync is being disabled just write mailbox and finish */ 818 if (!freq) 819 goto write_mailbox; 820 821 /* Get attached synth frequency */ 822 synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out)); 823 synth_freq = zl3073x_synth_freq_get(synth); 824 825 /* Compute and update esync period */ 826 out.esync_n_period = synth_freq / (u32)freq / out.div; 827 828 /* Half of the period in units of 1/2 synth cycle can be represented by 829 * the output_div. To get the supported esync pulse width of 25% of the 830 * period the output_div can just be divided by 2. Note that this 831 * assumes that output_div is even, otherwise some resolution will be 832 * lost. 833 */ 834 out.esync_n_width = out.div / 2; 835 836 write_mailbox: 837 /* Commit output configuration */ 838 return zl3073x_out_state_set(zldev, out_id, &out); 839 } 840 841 static int 842 zl3073x_dpll_output_pin_frequency_get(const struct dpll_pin *dpll_pin, 843 void *pin_priv, 844 const struct dpll_device *dpll, 845 void *dpll_priv, u64 *frequency, 846 struct netlink_ext_ack *extack) 847 { 848 struct zl3073x_dpll *zldpll = dpll_priv; 849 struct zl3073x_dpll_pin *pin = pin_priv; 850 851 *frequency = zl3073x_dev_output_pin_freq_get(zldpll->dev, pin->id); 852 853 return 0; 854 } 855 856 static int 857 zl3073x_dpll_output_pin_frequency_set(const struct dpll_pin *dpll_pin, 858 void *pin_priv, 859 const struct dpll_device *dpll, 860 void *dpll_priv, u64 frequency, 861 struct netlink_ext_ack *extack) 862 { 863 struct zl3073x_dpll *zldpll = dpll_priv; 864 struct zl3073x_dev *zldev = zldpll->dev; 865 struct zl3073x_dpll_pin *pin = pin_priv; 866 const struct zl3073x_synth *synth; 867 u32 new_div, synth_freq; 868 struct zl3073x_out out; 869 u8 out_id; 870 871 out_id = zl3073x_output_pin_out_get(pin->id); 872 out = *zl3073x_out_state_get(zldev, out_id); 873 874 /* Get attached synth frequency and compute new divisor */ 875 synth = zl3073x_synth_state_get(zldev, zl3073x_out_synth_get(&out)); 876 synth_freq = zl3073x_synth_freq_get(synth); 877 new_div = synth_freq / (u32)frequency; 878 879 /* Check signal format */ 880 if (!zl3073x_out_is_ndiv(&out)) { 881 /* For non N-divided signal formats the frequency is computed 882 * as division of synth frequency and output divisor. 883 */ 884 out.div = new_div; 885 886 /* For 50/50 duty cycle the divisor is equal to width */ 887 out.width = new_div; 888 889 /* Commit output configuration */ 890 return zl3073x_out_state_set(zldev, out_id, &out); 891 } 892 893 if (zl3073x_dpll_is_p_pin(pin)) { 894 /* We are going to change output frequency for P-pin but 895 * if the requested frequency is less than current N-pin 896 * frequency then indicate a failure as we are not able 897 * to compute N-pin divisor to keep its frequency unchanged. 898 * 899 * Update divisor for N-pin to keep N-pin frequency. 900 */ 901 out.esync_n_period = (out.esync_n_period * out.div) / new_div; 902 if (!out.esync_n_period) 903 return -EINVAL; 904 905 /* Update the output divisor */ 906 out.div = new_div; 907 908 /* For 50/50 duty cycle the divisor is equal to width */ 909 out.width = out.div; 910 } else { 911 /* We are going to change frequency of N-pin but if 912 * the requested freq is greater or equal than freq of P-pin 913 * in the output pair we cannot compute divisor for the N-pin. 914 * In this case indicate a failure. 915 * 916 * Update divisor for N-pin 917 */ 918 out.esync_n_period = div64_u64(synth_freq, frequency * out.div); 919 if (!out.esync_n_period) 920 return -EINVAL; 921 } 922 923 /* For 50/50 duty cycle the divisor is equal to width */ 924 out.esync_n_width = out.esync_n_period; 925 926 /* Commit output configuration */ 927 return zl3073x_out_state_set(zldev, out_id, &out); 928 } 929 930 static int 931 zl3073x_dpll_output_pin_phase_adjust_get(const struct dpll_pin *dpll_pin, 932 void *pin_priv, 933 const struct dpll_device *dpll, 934 void *dpll_priv, 935 s32 *phase_adjust, 936 struct netlink_ext_ack *extack) 937 { 938 struct zl3073x_dpll *zldpll = dpll_priv; 939 struct zl3073x_dev *zldev = zldpll->dev; 940 struct zl3073x_dpll_pin *pin = pin_priv; 941 const struct zl3073x_out *out; 942 u8 out_id; 943 944 out_id = zl3073x_output_pin_out_get(pin->id); 945 out = zl3073x_out_state_get(zldev, out_id); 946 947 /* The value in the register is expressed in half synth clock cycles. */ 948 *phase_adjust = out->phase_comp * pin->phase_gran; 949 950 return 0; 951 } 952 953 static int 954 zl3073x_dpll_output_pin_phase_adjust_set(const struct dpll_pin *dpll_pin, 955 void *pin_priv, 956 const struct dpll_device *dpll, 957 void *dpll_priv, 958 s32 phase_adjust, 959 struct netlink_ext_ack *extack) 960 { 961 struct zl3073x_dpll *zldpll = dpll_priv; 962 struct zl3073x_dev *zldev = zldpll->dev; 963 struct zl3073x_dpll_pin *pin = pin_priv; 964 struct zl3073x_out out; 965 u8 out_id; 966 967 out_id = zl3073x_output_pin_out_get(pin->id); 968 out = *zl3073x_out_state_get(zldev, out_id); 969 970 /* The value in the register is expressed in half synth clock cycles. */ 971 out.phase_comp = phase_adjust / pin->phase_gran; 972 973 /* Update output configuration from mailbox */ 974 return zl3073x_out_state_set(zldev, out_id, &out); 975 } 976 977 static int 978 zl3073x_dpll_output_pin_state_on_dpll_get(const struct dpll_pin *dpll_pin, 979 void *pin_priv, 980 const struct dpll_device *dpll, 981 void *dpll_priv, 982 enum dpll_pin_state *state, 983 struct netlink_ext_ack *extack) 984 { 985 /* If the output pin is registered then it is always connected */ 986 *state = DPLL_PIN_STATE_CONNECTED; 987 988 return 0; 989 } 990 991 static int 992 zl3073x_dpll_temp_get(const struct dpll_device *dpll, void *dpll_priv, 993 s32 *temp, struct netlink_ext_ack *extack) 994 { 995 struct zl3073x_dpll *zldpll = dpll_priv; 996 struct zl3073x_dev *zldev = zldpll->dev; 997 u16 val; 998 int rc; 999 1000 rc = zl3073x_read_u16(zldev, ZL_REG_DIE_TEMP_STATUS, &val); 1001 if (rc) 1002 return rc; 1003 1004 /* Register value is in units of 0.1 C, convert to millidegrees */ 1005 *temp = (s16)val * 100; 1006 1007 return 0; 1008 } 1009 1010 static int 1011 zl3073x_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv, 1012 enum dpll_lock_status *status, 1013 enum dpll_lock_status_error *status_error, 1014 struct netlink_ext_ack *extack) 1015 { 1016 struct zl3073x_dpll *zldpll = dpll_priv; 1017 const struct zl3073x_chan *chan; 1018 1019 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1020 1021 switch (zl3073x_chan_mode_get(chan)) { 1022 case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: 1023 case ZL_DPLL_MODE_REFSEL_MODE_NCO: 1024 /* In FREERUN and NCO modes the DPLL is always unlocked */ 1025 *status = DPLL_LOCK_STATUS_UNLOCKED; 1026 1027 return 0; 1028 default: 1029 break; 1030 } 1031 1032 switch (zl3073x_chan_lock_state_get(chan)) { 1033 case ZL_DPLL_MON_STATUS_STATE_LOCK: 1034 if (zl3073x_chan_is_ho_ready(chan)) 1035 *status = DPLL_LOCK_STATUS_LOCKED_HO_ACQ; 1036 else 1037 *status = DPLL_LOCK_STATUS_LOCKED; 1038 break; 1039 case ZL_DPLL_MON_STATUS_STATE_HOLDOVER: 1040 case ZL_DPLL_MON_STATUS_STATE_ACQUIRING: 1041 *status = DPLL_LOCK_STATUS_HOLDOVER; 1042 break; 1043 default: 1044 dev_warn(zldpll->dev->dev, 1045 "Unknown DPLL monitor status: 0x%02x\n", 1046 chan->mon_status); 1047 *status = DPLL_LOCK_STATUS_UNLOCKED; 1048 break; 1049 } 1050 1051 return 0; 1052 } 1053 1054 static int 1055 zl3073x_dpll_supported_modes_get(const struct dpll_device *dpll, 1056 void *dpll_priv, unsigned long *modes, 1057 struct netlink_ext_ack *extack) 1058 { 1059 struct zl3073x_dpll *zldpll = dpll_priv; 1060 const struct zl3073x_chan *chan; 1061 1062 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1063 1064 /* We support switching between automatic and manual mode, except in 1065 * a case where the DPLL channel is configured to run in NCO mode. 1066 * In this case, report only the manual mode to which the NCO is mapped 1067 * as the only supported one. 1068 */ 1069 if (zl3073x_chan_mode_get(chan) != ZL_DPLL_MODE_REFSEL_MODE_NCO) 1070 __set_bit(DPLL_MODE_AUTOMATIC, modes); 1071 1072 __set_bit(DPLL_MODE_MANUAL, modes); 1073 1074 return 0; 1075 } 1076 1077 static int 1078 zl3073x_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv, 1079 enum dpll_mode *mode, struct netlink_ext_ack *extack) 1080 { 1081 struct zl3073x_dpll *zldpll = dpll_priv; 1082 const struct zl3073x_chan *chan; 1083 1084 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1085 1086 switch (zl3073x_chan_mode_get(chan)) { 1087 case ZL_DPLL_MODE_REFSEL_MODE_FREERUN: 1088 case ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER: 1089 case ZL_DPLL_MODE_REFSEL_MODE_NCO: 1090 case ZL_DPLL_MODE_REFSEL_MODE_REFLOCK: 1091 /* Use MANUAL for device FREERUN, HOLDOVER, NCO and 1092 * REFLOCK modes 1093 */ 1094 *mode = DPLL_MODE_MANUAL; 1095 break; 1096 case ZL_DPLL_MODE_REFSEL_MODE_AUTO: 1097 /* Use AUTO for device AUTO mode */ 1098 *mode = DPLL_MODE_AUTOMATIC; 1099 break; 1100 default: 1101 return -EINVAL; 1102 } 1103 1104 return 0; 1105 } 1106 1107 static int 1108 zl3073x_dpll_phase_offset_avg_factor_get(const struct dpll_device *dpll, 1109 void *dpll_priv, u32 *factor, 1110 struct netlink_ext_ack *extack) 1111 { 1112 struct zl3073x_dpll *zldpll = dpll_priv; 1113 1114 *factor = zl3073x_dev_phase_avg_factor_get(zldpll->dev); 1115 1116 return 0; 1117 } 1118 1119 static int 1120 zl3073x_dpll_phase_offset_avg_factor_set(const struct dpll_device *dpll, 1121 void *dpll_priv, u32 factor, 1122 struct netlink_ext_ack *extack) 1123 { 1124 struct zl3073x_dpll *item, *zldpll = dpll_priv; 1125 int rc; 1126 1127 if (factor > 15) { 1128 NL_SET_ERR_MSG_FMT(extack, 1129 "Phase offset average factor has to be from range <0,15>"); 1130 return -EINVAL; 1131 } 1132 1133 rc = zl3073x_dev_phase_avg_factor_set(zldpll->dev, factor); 1134 if (rc) { 1135 NL_SET_ERR_MSG_FMT(extack, 1136 "Failed to set phase offset averaging factor"); 1137 return rc; 1138 } 1139 1140 /* The averaging factor is common for all DPLL channels so after change 1141 * we have to send a notification for other DPLL devices. 1142 */ 1143 list_for_each_entry(item, &zldpll->dev->dplls, list) { 1144 struct dpll_device *dpll_dev = READ_ONCE(item->dpll_dev); 1145 1146 if (item != zldpll && dpll_dev) 1147 __dpll_device_change_ntf(dpll_dev); 1148 } 1149 1150 return 0; 1151 } 1152 1153 static int 1154 zl3073x_dpll_mode_set(const struct dpll_device *dpll, void *dpll_priv, 1155 enum dpll_mode mode, struct netlink_ext_ack *extack) 1156 { 1157 struct zl3073x_dpll *zldpll = dpll_priv; 1158 struct zl3073x_chan chan; 1159 u8 hw_mode, ref; 1160 int rc; 1161 1162 chan = *zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1163 ref = zl3073x_chan_refsel_ref_get(&chan); 1164 1165 if (mode == DPLL_MODE_MANUAL) { 1166 /* We are switching from automatic to manual mode: 1167 * - if we have a valid reference selected during auto mode then 1168 * we will switch to forced reference lock mode and use this 1169 * reference for selection 1170 * - if NO valid reference is selected, we will switch to forced 1171 * holdover mode or freerun mode, depending on the current 1172 * lock status 1173 */ 1174 if (ZL3073X_DPLL_REF_IS_VALID(ref)) 1175 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_REFLOCK; 1176 else if (zldpll->lock_status == DPLL_LOCK_STATUS_UNLOCKED) 1177 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_FREERUN; 1178 else 1179 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_HOLDOVER; 1180 } else { 1181 /* We are switching from manual to automatic mode: 1182 * - if there is a valid reference selected then ensure that 1183 * it is selectable after switch to automatic mode 1184 * - switch to automatic mode 1185 */ 1186 if (ZL3073X_DPLL_REF_IS_VALID(ref) && 1187 !zl3073x_chan_ref_is_selectable(&chan, ref)) { 1188 struct zl3073x_dpll_pin *pin; 1189 1190 pin = zl3073x_dpll_pin_get_by_ref(zldpll, ref); 1191 if (pin) { 1192 /* Restore pin priority in HW */ 1193 zl3073x_chan_ref_prio_set(&chan, ref, 1194 pin->prio); 1195 } 1196 } 1197 1198 hw_mode = ZL_DPLL_MODE_REFSEL_MODE_AUTO; 1199 } 1200 1201 zl3073x_chan_mode_set(&chan, hw_mode); 1202 if (ZL3073X_DPLL_REF_IS_VALID(ref)) 1203 zl3073x_chan_ref_set(&chan, ref); 1204 1205 rc = zl3073x_chan_state_set(zldpll->dev, zldpll->id, &chan); 1206 if (rc) { 1207 NL_SET_ERR_MSG_MOD(extack, 1208 "failed to set reference selection mode"); 1209 return rc; 1210 } 1211 1212 return 0; 1213 } 1214 1215 static int 1216 zl3073x_dpll_phase_offset_monitor_get(const struct dpll_device *dpll, 1217 void *dpll_priv, 1218 enum dpll_feature_state *state, 1219 struct netlink_ext_ack *extack) 1220 { 1221 struct zl3073x_dpll *zldpll = dpll_priv; 1222 1223 if (zldpll->phase_monitor) 1224 *state = DPLL_FEATURE_STATE_ENABLE; 1225 else 1226 *state = DPLL_FEATURE_STATE_DISABLE; 1227 1228 return 0; 1229 } 1230 1231 static int 1232 zl3073x_dpll_phase_offset_monitor_set(const struct dpll_device *dpll, 1233 void *dpll_priv, 1234 enum dpll_feature_state state, 1235 struct netlink_ext_ack *extack) 1236 { 1237 struct zl3073x_dpll *zldpll = dpll_priv; 1238 1239 zldpll->phase_monitor = (state == DPLL_FEATURE_STATE_ENABLE); 1240 1241 return 0; 1242 } 1243 1244 static int 1245 zl3073x_dpll_freq_monitor_get(const struct dpll_device *dpll, 1246 void *dpll_priv, 1247 enum dpll_feature_state *state, 1248 struct netlink_ext_ack *extack) 1249 { 1250 struct zl3073x_dpll *zldpll = dpll_priv; 1251 1252 if (zldpll->dev->freq_monitor) 1253 *state = DPLL_FEATURE_STATE_ENABLE; 1254 else 1255 *state = DPLL_FEATURE_STATE_DISABLE; 1256 1257 return 0; 1258 } 1259 1260 static int 1261 zl3073x_dpll_freq_monitor_set(const struct dpll_device *dpll, 1262 void *dpll_priv, 1263 enum dpll_feature_state state, 1264 struct netlink_ext_ack *extack) 1265 { 1266 struct zl3073x_dpll *item, *zldpll = dpll_priv; 1267 1268 zldpll->dev->freq_monitor = (state == DPLL_FEATURE_STATE_ENABLE); 1269 1270 /* The frequency monitoring is common for all DPLL channels so after 1271 * change we have to send a notification for other DPLL devices. 1272 */ 1273 list_for_each_entry(item, &zldpll->dev->dplls, list) { 1274 struct dpll_device *dpll_dev = READ_ONCE(item->dpll_dev); 1275 1276 if (item != zldpll && dpll_dev) 1277 __dpll_device_change_ntf(dpll_dev); 1278 } 1279 1280 return 0; 1281 } 1282 1283 static const struct dpll_pin_ops zl3073x_dpll_input_pin_ops = { 1284 .supported_ffo = BIT(DPLL_FFO_PIN_DEVICE), 1285 .direction_get = zl3073x_dpll_pin_direction_get, 1286 .esync_get = zl3073x_dpll_input_pin_esync_get, 1287 .esync_set = zl3073x_dpll_input_pin_esync_set, 1288 .ffo_get = zl3073x_dpll_input_pin_ffo_get, 1289 .frequency_get = zl3073x_dpll_input_pin_frequency_get, 1290 .frequency_set = zl3073x_dpll_input_pin_frequency_set, 1291 .measured_freq_get = zl3073x_dpll_input_pin_measured_freq_get, 1292 .operstate_on_dpll_get = zl3073x_dpll_input_pin_operstate_on_dpll_get, 1293 .phase_offset_get = zl3073x_dpll_input_pin_phase_offset_get, 1294 .phase_adjust_get = zl3073x_dpll_input_pin_phase_adjust_get, 1295 .phase_adjust_set = zl3073x_dpll_input_pin_phase_adjust_set, 1296 .prio_get = zl3073x_dpll_input_pin_prio_get, 1297 .prio_set = zl3073x_dpll_input_pin_prio_set, 1298 .ref_sync_get = zl3073x_dpll_input_pin_ref_sync_get, 1299 .ref_sync_set = zl3073x_dpll_input_pin_ref_sync_set, 1300 .state_on_dpll_get = zl3073x_dpll_input_pin_state_on_dpll_get, 1301 .state_on_dpll_set = zl3073x_dpll_input_pin_state_on_dpll_set, 1302 }; 1303 1304 static const struct dpll_pin_ops zl3073x_dpll_output_pin_ops = { 1305 .direction_get = zl3073x_dpll_pin_direction_get, 1306 .esync_get = zl3073x_dpll_output_pin_esync_get, 1307 .esync_set = zl3073x_dpll_output_pin_esync_set, 1308 .frequency_get = zl3073x_dpll_output_pin_frequency_get, 1309 .frequency_set = zl3073x_dpll_output_pin_frequency_set, 1310 .phase_adjust_get = zl3073x_dpll_output_pin_phase_adjust_get, 1311 .phase_adjust_set = zl3073x_dpll_output_pin_phase_adjust_set, 1312 .state_on_dpll_get = zl3073x_dpll_output_pin_state_on_dpll_get, 1313 }; 1314 1315 static const struct dpll_device_ops zl3073x_dpll_device_ops = { 1316 .lock_status_get = zl3073x_dpll_lock_status_get, 1317 .mode_get = zl3073x_dpll_mode_get, 1318 .mode_set = zl3073x_dpll_mode_set, 1319 .phase_offset_avg_factor_get = zl3073x_dpll_phase_offset_avg_factor_get, 1320 .phase_offset_avg_factor_set = zl3073x_dpll_phase_offset_avg_factor_set, 1321 .phase_offset_monitor_get = zl3073x_dpll_phase_offset_monitor_get, 1322 .phase_offset_monitor_set = zl3073x_dpll_phase_offset_monitor_set, 1323 .freq_monitor_get = zl3073x_dpll_freq_monitor_get, 1324 .freq_monitor_set = zl3073x_dpll_freq_monitor_set, 1325 .supported_modes_get = zl3073x_dpll_supported_modes_get, 1326 }; 1327 1328 /** 1329 * zl3073x_dpll_pin_alloc - allocate DPLL pin 1330 * @zldpll: pointer to zl3073x_dpll 1331 * @dir: pin direction 1332 * @id: pin id 1333 * 1334 * Allocates and initializes zl3073x_dpll_pin structure for given 1335 * pin id and direction. 1336 * 1337 * Return: pointer to allocated structure on success, error pointer on error 1338 */ 1339 static struct zl3073x_dpll_pin * 1340 zl3073x_dpll_pin_alloc(struct zl3073x_dpll *zldpll, enum dpll_pin_direction dir, 1341 u8 id) 1342 { 1343 struct zl3073x_dpll_pin *pin; 1344 1345 pin = kzalloc_obj(*pin); 1346 if (!pin) 1347 return ERR_PTR(-ENOMEM); 1348 1349 pin->dpll = zldpll; 1350 pin->dir = dir; 1351 pin->id = id; 1352 1353 return pin; 1354 } 1355 1356 /** 1357 * zl3073x_dpll_pin_free - deallocate DPLL pin 1358 * @pin: pin to free 1359 * 1360 * Deallocates DPLL pin previously allocated by @zl3073x_dpll_pin_alloc. 1361 */ 1362 static void 1363 zl3073x_dpll_pin_free(struct zl3073x_dpll_pin *pin) 1364 { 1365 WARN(pin->dpll_pin, "DPLL pin is still registered\n"); 1366 1367 kfree(pin); 1368 } 1369 1370 /** 1371 * zl3073x_dpll_pin_register - register DPLL pin 1372 * @pin: pointer to DPLL pin 1373 * @index: absolute pin index for registration 1374 * 1375 * Registers given DPLL pin into DPLL sub-system. 1376 * 1377 * Return: 0 on success, <0 on error 1378 */ 1379 static int 1380 zl3073x_dpll_pin_register(struct zl3073x_dpll_pin *pin, u32 index) 1381 { 1382 struct zl3073x_dpll *zldpll = pin->dpll; 1383 struct zl3073x_pin_props *props; 1384 const struct dpll_pin_ops *ops; 1385 int rc; 1386 1387 /* Get pin properties */ 1388 props = zl3073x_pin_props_get(zldpll->dev, pin->dir, pin->id); 1389 if (IS_ERR(props)) 1390 return PTR_ERR(props); 1391 1392 /* Save package label, fwnode, esync capability and phase adjust 1393 * granularity. 1394 */ 1395 strscpy(pin->label, props->package_label); 1396 pin->fwnode = fwnode_handle_get(props->fwnode); 1397 pin->esync_control = props->esync_control; 1398 pin->phase_gran = props->dpll_props.phase_gran; 1399 1400 if (zl3073x_dpll_is_input_pin(pin)) { 1401 const struct zl3073x_chan *chan; 1402 u8 ref; 1403 1404 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1405 ref = zl3073x_input_pin_ref_get(pin->id); 1406 pin->prio = zl3073x_chan_ref_prio_get(chan, ref); 1407 1408 if (pin->prio == ZL_DPLL_REF_PRIO_NONE) 1409 /* Clamp prio to max value */ 1410 pin->prio = ZL_DPLL_REF_PRIO_MAX; 1411 } 1412 1413 /* Create or get existing DPLL pin */ 1414 pin->dpll_pin = dpll_pin_get(zldpll->dev->clock_id, index, THIS_MODULE, 1415 &props->dpll_props, &pin->tracker); 1416 if (IS_ERR(pin->dpll_pin)) { 1417 rc = PTR_ERR(pin->dpll_pin); 1418 goto err_pin_get; 1419 } 1420 dpll_pin_fwnode_set(pin->dpll_pin, props->fwnode); 1421 1422 if (zl3073x_dpll_is_input_pin(pin)) 1423 ops = &zl3073x_dpll_input_pin_ops; 1424 else 1425 ops = &zl3073x_dpll_output_pin_ops; 1426 1427 /* Register the pin */ 1428 rc = dpll_pin_register(zldpll->dpll_dev, pin->dpll_pin, ops, pin); 1429 if (rc) 1430 goto err_register; 1431 1432 /* Free pin properties */ 1433 zl3073x_pin_props_put(props); 1434 1435 return 0; 1436 1437 err_register: 1438 dpll_pin_put(pin->dpll_pin, &pin->tracker); 1439 err_pin_get: 1440 pin->dpll_pin = NULL; 1441 fwnode_handle_put(pin->fwnode); 1442 pin->fwnode = NULL; 1443 zl3073x_pin_props_put(props); 1444 1445 return rc; 1446 } 1447 1448 /** 1449 * zl3073x_dpll_pin_unregister - unregister DPLL pin 1450 * @pin: pointer to DPLL pin 1451 * 1452 * Unregisters pin previously registered by @zl3073x_dpll_pin_register. 1453 */ 1454 static void 1455 zl3073x_dpll_pin_unregister(struct zl3073x_dpll_pin *pin) 1456 { 1457 struct zl3073x_dpll *zldpll = pin->dpll; 1458 const struct dpll_pin_ops *ops; 1459 1460 WARN(!pin->dpll_pin, "DPLL pin is not registered\n"); 1461 1462 if (zl3073x_dpll_is_input_pin(pin)) 1463 ops = &zl3073x_dpll_input_pin_ops; 1464 else 1465 ops = &zl3073x_dpll_output_pin_ops; 1466 1467 /* Unregister the pin */ 1468 dpll_pin_unregister(zldpll->dpll_dev, pin->dpll_pin, ops, pin); 1469 1470 dpll_pin_put(pin->dpll_pin, &pin->tracker); 1471 pin->dpll_pin = NULL; 1472 1473 fwnode_handle_put(pin->fwnode); 1474 pin->fwnode = NULL; 1475 } 1476 1477 /** 1478 * zl3073x_dpll_pins_unregister - unregister all registered DPLL pins 1479 * @zldpll: pointer to zl3073x_dpll structure 1480 * 1481 * Enumerates all DPLL pins registered to given DPLL device and 1482 * unregisters them. 1483 */ 1484 static void 1485 zl3073x_dpll_pins_unregister(struct zl3073x_dpll *zldpll) 1486 { 1487 struct zl3073x_dpll_pin *pin, *next; 1488 1489 list_for_each_entry_safe(pin, next, &zldpll->pins, list) { 1490 zl3073x_dpll_pin_unregister(pin); 1491 list_del(&pin->list); 1492 zl3073x_dpll_pin_free(pin); 1493 } 1494 } 1495 1496 /** 1497 * zl3073x_dpll_pin_is_registrable - check if the pin is registrable 1498 * @zldpll: pointer to zl3073x_dpll structure 1499 * @dir: pin direction 1500 * @index: pin index 1501 * 1502 * Checks if the given pin can be registered to given DPLL. For both 1503 * directions the pin can be registered if it is enabled. In case of 1504 * differential signal type only P-pin is reported as registrable. 1505 * And additionally for the output pin, the pin can be registered only 1506 * if it is connected to synthesizer that is driven by given DPLL. 1507 * 1508 * Return: true if the pin is registrable, false if not 1509 */ 1510 static bool 1511 zl3073x_dpll_pin_is_registrable(struct zl3073x_dpll *zldpll, 1512 enum dpll_pin_direction dir, u8 index) 1513 { 1514 struct zl3073x_dev *zldev = zldpll->dev; 1515 const struct zl3073x_chan *chan; 1516 bool is_diff, is_enabled; 1517 const char *name; 1518 1519 chan = zl3073x_chan_state_get(zldev, zldpll->id); 1520 1521 if (dir == DPLL_PIN_DIRECTION_INPUT) { 1522 u8 ref_id = zl3073x_input_pin_ref_get(index); 1523 const struct zl3073x_ref *ref; 1524 1525 /* Skip the pin if the DPLL is running in NCO mode */ 1526 if (zl3073x_chan_mode_get(chan) == ZL_DPLL_MODE_REFSEL_MODE_NCO) 1527 return false; 1528 1529 name = "REF"; 1530 ref = zl3073x_ref_state_get(zldev, ref_id); 1531 is_diff = zl3073x_ref_is_diff(ref); 1532 is_enabled = zl3073x_ref_is_enabled(ref); 1533 } else { 1534 /* Output P&N pair shares single HW output */ 1535 u8 out = zl3073x_output_pin_out_get(index); 1536 1537 /* Skip the pin if it is connected to different DPLL channel */ 1538 if (zl3073x_dev_out_dpll_get(zldev, out) != zldpll->id) { 1539 dev_dbg(zldev->dev, 1540 "OUT%u is driven by different DPLL\n", out); 1541 1542 return false; 1543 } 1544 1545 name = "OUT"; 1546 is_diff = zl3073x_dev_out_is_diff(zldev, out); 1547 is_enabled = zl3073x_dev_output_pin_is_enabled(zldev, index); 1548 } 1549 1550 /* Skip N-pin if the corresponding input/output is differential */ 1551 if (is_diff && zl3073x_is_n_pin(index)) { 1552 dev_dbg(zldev->dev, "%s%u is differential, skipping N-pin\n", 1553 name, index / 2); 1554 1555 return false; 1556 } 1557 1558 /* Skip the pin if it is disabled */ 1559 if (!is_enabled) { 1560 dev_dbg(zldev->dev, "%s%u%c is disabled\n", name, index / 2, 1561 zl3073x_is_p_pin(index) ? 'P' : 'N'); 1562 1563 return false; 1564 } 1565 1566 return true; 1567 } 1568 1569 /** 1570 * zl3073x_dpll_pins_register - register all registerable DPLL pins 1571 * @zldpll: pointer to zl3073x_dpll structure 1572 * 1573 * Enumerates all possible input/output pins and registers all of them 1574 * that are registrable. 1575 * 1576 * Return: 0 on success, <0 on error 1577 */ 1578 static int 1579 zl3073x_dpll_pins_register(struct zl3073x_dpll *zldpll) 1580 { 1581 struct zl3073x_dpll_pin *pin; 1582 enum dpll_pin_direction dir; 1583 u8 id, index; 1584 int rc; 1585 1586 /* Process input pins */ 1587 for (index = 0; index < ZL3073X_NUM_PINS; index++) { 1588 /* First input pins and then output pins */ 1589 if (index < ZL3073X_NUM_INPUT_PINS) { 1590 id = index; 1591 dir = DPLL_PIN_DIRECTION_INPUT; 1592 } else { 1593 id = index - ZL3073X_NUM_INPUT_PINS; 1594 dir = DPLL_PIN_DIRECTION_OUTPUT; 1595 } 1596 1597 /* Check if the pin registrable to this DPLL */ 1598 if (!zl3073x_dpll_pin_is_registrable(zldpll, dir, id)) 1599 continue; 1600 1601 pin = zl3073x_dpll_pin_alloc(zldpll, dir, id); 1602 if (IS_ERR(pin)) { 1603 rc = PTR_ERR(pin); 1604 goto error; 1605 } 1606 1607 rc = zl3073x_dpll_pin_register(pin, index); 1608 if (rc) { 1609 zl3073x_dpll_pin_free(pin); 1610 goto error; 1611 } 1612 1613 list_add(&pin->list, &zldpll->pins); 1614 } 1615 1616 return 0; 1617 1618 error: 1619 zl3073x_dpll_pins_unregister(zldpll); 1620 1621 return rc; 1622 } 1623 1624 /** 1625 * zl3073x_dpll_device_register - register DPLL device 1626 * @zldpll: pointer to zl3073x_dpll structure 1627 * 1628 * Registers given DPLL device into DPLL sub-system. 1629 * 1630 * Return: 0 on success, <0 on error 1631 */ 1632 static int 1633 zl3073x_dpll_device_register(struct zl3073x_dpll *zldpll) 1634 { 1635 struct zl3073x_dev *zldev = zldpll->dev; 1636 int rc; 1637 1638 zldpll->ops = zl3073x_dpll_device_ops; 1639 if (zldev->info->flags & ZL3073X_FLAG_DIE_TEMP) 1640 zldpll->ops.temp_get = zl3073x_dpll_temp_get; 1641 1642 zldpll->dpll_dev = dpll_device_get(zldev->clock_id, zldpll->id, 1643 THIS_MODULE, &zldpll->tracker); 1644 if (IS_ERR(zldpll->dpll_dev)) { 1645 rc = PTR_ERR(zldpll->dpll_dev); 1646 zldpll->dpll_dev = NULL; 1647 1648 return rc; 1649 } 1650 1651 rc = dpll_device_register(zldpll->dpll_dev, 1652 zl3073x_prop_dpll_type_get(zldev, zldpll->id), 1653 &zldpll->ops, zldpll); 1654 if (rc) { 1655 dpll_device_put(zldpll->dpll_dev, &zldpll->tracker); 1656 zldpll->dpll_dev = NULL; 1657 } 1658 1659 return rc; 1660 } 1661 1662 /** 1663 * zl3073x_dpll_device_unregister - unregister DPLL device 1664 * @zldpll: pointer to zl3073x_dpll structure 1665 * 1666 * Unregisters given DPLL device from DPLL sub-system previously registered 1667 * by @zl3073x_dpll_device_register. 1668 */ 1669 static void 1670 zl3073x_dpll_device_unregister(struct zl3073x_dpll *zldpll) 1671 { 1672 struct dpll_device *dpll_dev = READ_ONCE(zldpll->dpll_dev); 1673 1674 WARN(!dpll_dev, "DPLL device is not registered\n"); 1675 1676 WRITE_ONCE(zldpll->dpll_dev, NULL); 1677 dpll_device_unregister(dpll_dev, &zldpll->ops, zldpll); 1678 dpll_device_put(dpll_dev, &zldpll->tracker); 1679 } 1680 1681 /** 1682 * zl3073x_dpll_pin_phase_offset_check - check for pin phase offset change 1683 * @pin: pin to check 1684 * 1685 * Check for the change of DPLL to connected pin phase offset change. 1686 * 1687 * Return: true on phase offset change, false otherwise 1688 */ 1689 static bool 1690 zl3073x_dpll_pin_phase_offset_check(struct zl3073x_dpll_pin *pin) 1691 { 1692 struct zl3073x_dpll *zldpll = pin->dpll; 1693 struct zl3073x_dev *zldev = zldpll->dev; 1694 unsigned int reg; 1695 s64 phase_offset; 1696 u8 ref_id; 1697 int rc; 1698 1699 /* No phase offset if the ref monitor reports signal errors */ 1700 ref_id = zl3073x_input_pin_ref_get(pin->id); 1701 if (!zl3073x_dev_ref_is_status_ok(zldev, ref_id)) 1702 return false; 1703 1704 /* Select register to read phase offset value depending on pin and 1705 * phase monitor state: 1706 * 1) For connected pin use dpll_phase_err_data register 1707 * 2) For other pins use appropriate ref_phase register if the phase 1708 * monitor feature is enabled. 1709 */ 1710 if (pin->operstate == DPLL_PIN_OPERSTATE_ACTIVE) 1711 reg = ZL_REG_DPLL_PHASE_ERR_DATA(zldpll->id); 1712 else if (zldpll->phase_monitor) 1713 reg = ZL_REG_REF_PHASE(ref_id); 1714 else 1715 return false; 1716 1717 /* Read measured phase offset value */ 1718 rc = zl3073x_read_u48(zldev, reg, &phase_offset); 1719 if (rc) { 1720 dev_err(zldev->dev, "Failed to read ref phase offset: %pe\n", 1721 ERR_PTR(rc)); 1722 1723 return false; 1724 } 1725 1726 /* Convert to ps */ 1727 phase_offset = div_s64(sign_extend64(phase_offset, 47), 100); 1728 1729 /* Compare with previous value */ 1730 if (phase_offset != pin->phase_offset) { 1731 dev_dbg(zldev->dev, "%s phase offset changed: %lld -> %lld\n", 1732 pin->label, pin->phase_offset, phase_offset); 1733 pin->phase_offset = phase_offset; 1734 1735 return true; 1736 } 1737 1738 return false; 1739 } 1740 1741 /** 1742 * zl3073x_dpll_pin_ffo_check - check for FFO change on active pin 1743 * @pin: pin to check 1744 * 1745 * Return: true on change, false otherwise 1746 */ 1747 static bool 1748 zl3073x_dpll_pin_ffo_check(struct zl3073x_dpll_pin *pin) 1749 { 1750 struct zl3073x_dpll *zldpll = pin->dpll; 1751 struct zl3073x_dev *zldev = zldpll->dev; 1752 const struct zl3073x_chan *chan; 1753 s64 ffo; 1754 1755 if (pin->operstate != DPLL_PIN_OPERSTATE_ACTIVE) 1756 return false; 1757 1758 chan = zl3073x_chan_state_get(zldpll->dev, zldpll->id); 1759 ffo = mul_s64_u64_shr(zl3073x_chan_df_offset_get(chan), 1760 244140625, 36); 1761 1762 if (atomic64_xchg(&pin->freq_offset, ffo) != ffo) { 1763 dev_dbg(zldev->dev, "%s freq offset changed to: %lld\n", 1764 pin->label, ffo); 1765 return true; 1766 } 1767 1768 return false; 1769 } 1770 1771 /** 1772 * zl3073x_dpll_pin_measured_freq_check - check for pin measured frequency 1773 * change 1774 * @pin: pin to check 1775 * 1776 * Check for the given pin's measured frequency change. 1777 * 1778 * Return: true on measured frequency change, false otherwise 1779 */ 1780 static bool 1781 zl3073x_dpll_pin_measured_freq_check(struct zl3073x_dpll_pin *pin) 1782 { 1783 struct zl3073x_dpll *zldpll = pin->dpll; 1784 struct zl3073x_dev *zldev = zldpll->dev; 1785 const struct zl3073x_ref *ref; 1786 u8 ref_id; 1787 u32 freq; 1788 1789 if (!zldpll->dev->freq_monitor) 1790 return false; 1791 1792 ref_id = zl3073x_input_pin_ref_get(pin->id); 1793 ref = zl3073x_ref_state_get(zldev, ref_id); 1794 1795 freq = zl3073x_ref_meas_freq_get(ref); 1796 if (pin->measured_freq != freq) { 1797 dev_dbg(zldev->dev, "%s measured freq changed: %u -> %u\n", 1798 pin->label, pin->measured_freq, freq); 1799 pin->measured_freq = freq; 1800 1801 return true; 1802 } 1803 1804 return false; 1805 } 1806 1807 /** 1808 * zl3073x_dpll_changes_check - check for changes and send notifications 1809 * @zldpll: pointer to zl3073x_dpll structure 1810 * 1811 * Checks for changes on given DPLL device and its registered DPLL pins 1812 * and sends notifications about them. 1813 * 1814 * This function is periodically called from @zl3073x_dev_periodic_work. 1815 */ 1816 void 1817 zl3073x_dpll_changes_check(struct zl3073x_dpll *zldpll) 1818 { 1819 struct zl3073x_dev *zldev = zldpll->dev; 1820 enum dpll_lock_status lock_status; 1821 struct device *dev = zldev->dev; 1822 struct zl3073x_dpll_pin *pin; 1823 int rc; 1824 1825 zldpll->check_count++; 1826 1827 /* Get current lock status for the DPLL */ 1828 rc = zl3073x_dpll_lock_status_get(zldpll->dpll_dev, zldpll, 1829 &lock_status, NULL, NULL); 1830 if (rc) { 1831 dev_err(dev, "Failed to get DPLL%u lock status: %pe\n", 1832 zldpll->id, ERR_PTR(rc)); 1833 return; 1834 } 1835 1836 /* If lock status was changed then notify DPLL core */ 1837 if (zldpll->lock_status != lock_status) { 1838 zldpll->lock_status = lock_status; 1839 dpll_device_change_ntf(zldpll->dpll_dev); 1840 } 1841 1842 /* Update phase offset latch registers for this DPLL if the phase 1843 * offset monitor feature is enabled. 1844 */ 1845 if (zldpll->phase_monitor) { 1846 rc = zl3073x_ref_phase_offsets_update(zldev, zldpll->id); 1847 if (rc) { 1848 dev_err(zldev->dev, 1849 "Failed to update phase offsets: %pe\n", 1850 ERR_PTR(rc)); 1851 return; 1852 } 1853 } 1854 1855 list_for_each_entry(pin, &zldpll->pins, list) { 1856 enum dpll_pin_operstate operstate; 1857 bool pin_changed = false; 1858 1859 /* Output pins change checks are not necessary because output 1860 * states are constant. 1861 */ 1862 if (!zl3073x_dpll_is_input_pin(pin)) 1863 continue; 1864 1865 rc = zl3073x_dpll_ref_operstate_get(pin, &operstate); 1866 if (rc) { 1867 dev_err(dev, 1868 "Failed to get %s on DPLL%u oper state: %pe\n", 1869 pin->label, zldpll->id, ERR_PTR(rc)); 1870 return; 1871 } 1872 1873 if (operstate != pin->operstate) { 1874 dev_dbg(dev, "%s oper state changed: %u->%u\n", 1875 pin->label, pin->operstate, operstate); 1876 pin->operstate = operstate; 1877 pin_changed = true; 1878 } 1879 1880 /* Check for phase offset, ffo, and measured freq change 1881 * once per second. 1882 */ 1883 if (zldpll->check_count % 2 == 0) { 1884 if (zl3073x_dpll_pin_phase_offset_check(pin)) 1885 pin_changed = true; 1886 1887 if (zl3073x_dpll_pin_ffo_check(pin)) 1888 pin_changed = true; 1889 1890 if (zl3073x_dpll_pin_measured_freq_check(pin)) 1891 pin_changed = true; 1892 } 1893 1894 if (pin_changed) 1895 dpll_pin_change_ntf(pin->dpll_pin); 1896 } 1897 } 1898 1899 /** 1900 * zl3073x_dpll_init_fine_phase_adjust - do initial fine phase adjustments 1901 * @zldev: pointer to zl3073x device 1902 * 1903 * Performs initial fine phase adjustments needed per datasheet. 1904 * 1905 * Return: 0 on success, <0 on error 1906 */ 1907 int 1908 zl3073x_dpll_init_fine_phase_adjust(struct zl3073x_dev *zldev) 1909 { 1910 int rc; 1911 1912 rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_MASK, 0x1f); 1913 if (rc) 1914 return rc; 1915 1916 rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_INTVL, 0x01); 1917 if (rc) 1918 return rc; 1919 1920 rc = zl3073x_write_u16(zldev, ZL_REG_SYNTH_PHASE_SHIFT_DATA, 0xffff); 1921 if (rc) 1922 return rc; 1923 1924 rc = zl3073x_write_u8(zldev, ZL_REG_SYNTH_PHASE_SHIFT_CTRL, 0x01); 1925 if (rc) 1926 return rc; 1927 1928 return rc; 1929 } 1930 1931 /** 1932 * zl3073x_dpll_alloc - allocate DPLL device 1933 * @zldev: pointer to zl3073x device 1934 * @ch: DPLL channel number 1935 * 1936 * Allocates DPLL device structure for given DPLL channel. 1937 * 1938 * Return: pointer to DPLL device on success, error pointer on error 1939 */ 1940 struct zl3073x_dpll * 1941 zl3073x_dpll_alloc(struct zl3073x_dev *zldev, u8 ch) 1942 { 1943 struct zl3073x_dpll *zldpll; 1944 1945 zldpll = kzalloc_obj(*zldpll); 1946 if (!zldpll) 1947 return ERR_PTR(-ENOMEM); 1948 1949 zldpll->dev = zldev; 1950 zldpll->id = ch; 1951 INIT_LIST_HEAD(&zldpll->pins); 1952 1953 return zldpll; 1954 } 1955 1956 /** 1957 * zl3073x_dpll_free - free DPLL device 1958 * @zldpll: pointer to zl3073x_dpll structure 1959 * 1960 * Deallocates given DPLL device previously allocated by @zl3073x_dpll_alloc. 1961 */ 1962 void 1963 zl3073x_dpll_free(struct zl3073x_dpll *zldpll) 1964 { 1965 WARN(zldpll->dpll_dev, "DPLL device is still registered\n"); 1966 1967 kfree(zldpll); 1968 } 1969 1970 /** 1971 * zl3073x_dpll_ref_sync_pair_register - register ref_sync pairs for a pin 1972 * @pin: pointer to zl3073x_dpll_pin structure 1973 * 1974 * Iterates 'ref-sync-sources' phandles in the pin's firmware node and 1975 * registers each declared pairing. 1976 * 1977 * Return: 0 on success, <0 on error 1978 */ 1979 static int 1980 zl3073x_dpll_ref_sync_pair_register(struct zl3073x_dpll_pin *pin) 1981 { 1982 struct zl3073x_dev *zldev = pin->dpll->dev; 1983 struct fwnode_handle *fwnode; 1984 struct dpll_pin *sync_pin; 1985 dpll_tracker tracker; 1986 int n, rc; 1987 1988 for (n = 0; ; n++) { 1989 /* Get n'th ref-sync source */ 1990 fwnode = fwnode_find_reference(pin->fwnode, "ref-sync-sources", 1991 n); 1992 if (IS_ERR(fwnode)) { 1993 rc = PTR_ERR(fwnode); 1994 break; 1995 } 1996 1997 /* Find associated dpll pin */ 1998 sync_pin = fwnode_dpll_pin_find(fwnode, &tracker); 1999 fwnode_handle_put(fwnode); 2000 if (!sync_pin) { 2001 dev_warn(zldev->dev, "%s: ref-sync source %d not found", 2002 pin->label, n); 2003 continue; 2004 } 2005 2006 /* Register new ref-sync pair */ 2007 rc = dpll_pin_ref_sync_pair_add(pin->dpll_pin, sync_pin); 2008 dpll_pin_put(sync_pin, &tracker); 2009 2010 /* -EBUSY means pairing already exists from another DPLL's 2011 * registration. 2012 */ 2013 if (rc && rc != -EBUSY) { 2014 dev_err(zldev->dev, 2015 "%s: failed to add ref-sync source %d: %pe", 2016 pin->label, n, ERR_PTR(rc)); 2017 break; 2018 } 2019 } 2020 2021 return rc != -ENOENT ? rc : 0; 2022 } 2023 2024 /** 2025 * zl3073x_dpll_ref_sync_pairs_register - register ref_sync pairs for a DPLL 2026 * @zldpll: pointer to zl3073x_dpll structure 2027 * 2028 * Iterates all registered input pins of the given DPLL and establishes 2029 * ref_sync pairings declared by 'ref-sync-sources' phandles in the 2030 * device tree. 2031 * 2032 * Return: 0 on success, <0 on error 2033 */ 2034 static int 2035 zl3073x_dpll_ref_sync_pairs_register(struct zl3073x_dpll *zldpll) 2036 { 2037 struct zl3073x_dpll_pin *pin; 2038 int rc; 2039 2040 list_for_each_entry(pin, &zldpll->pins, list) { 2041 if (!zl3073x_dpll_is_input_pin(pin) || !pin->fwnode) 2042 continue; 2043 2044 rc = zl3073x_dpll_ref_sync_pair_register(pin); 2045 if (rc) 2046 return rc; 2047 } 2048 2049 return 0; 2050 } 2051 2052 /** 2053 * zl3073x_dpll_register - register DPLL device and all its pins 2054 * @zldpll: pointer to zl3073x_dpll structure 2055 * 2056 * Registers given DPLL device and all its pins into DPLL sub-system. 2057 * 2058 * Return: 0 on success, <0 on error 2059 */ 2060 int 2061 zl3073x_dpll_register(struct zl3073x_dpll *zldpll) 2062 { 2063 int rc; 2064 2065 rc = zl3073x_dpll_device_register(zldpll); 2066 if (rc) 2067 return rc; 2068 2069 rc = zl3073x_dpll_pins_register(zldpll); 2070 if (rc) { 2071 zl3073x_dpll_device_unregister(zldpll); 2072 return rc; 2073 } 2074 2075 rc = zl3073x_dpll_ref_sync_pairs_register(zldpll); 2076 if (rc) { 2077 zl3073x_dpll_pins_unregister(zldpll); 2078 zl3073x_dpll_device_unregister(zldpll); 2079 return rc; 2080 } 2081 2082 return 0; 2083 } 2084 2085 /** 2086 * zl3073x_dpll_unregister - unregister DPLL device and its pins 2087 * @zldpll: pointer to zl3073x_dpll structure 2088 * 2089 * Unregisters given DPLL device and all its pins from DPLL sub-system 2090 * previously registered by @zl3073x_dpll_register. 2091 */ 2092 void 2093 zl3073x_dpll_unregister(struct zl3073x_dpll *zldpll) 2094 { 2095 /* Unregister all pins and dpll */ 2096 zl3073x_dpll_pins_unregister(zldpll); 2097 zl3073x_dpll_device_unregister(zldpll); 2098 } 2099