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