From: Oleg Zadorozhnyi An output is driven by its PLL rather than selected by it, so its state says whether it carries a signal: connected while it drives, disconnected while it is muted. Setting the state mutes or un-mutes it by forcing the pad to Hi-Z, the only per-output control the device offers that leaves the divider alone. The force bit and the state bit are separate, and a pad follows the loaded configuration while the force bit is clear, so both are read to decide whether an output is muted and both are written to change it. Which of the four register banks applies depends on the slot and on whether the pad is single-ended or differential. Signed-off-by: Oleg Zadorozhnyi Assisted-by: Claude:claude-4-opus [chat] Signed-off-by: Ali Rouhi --- drivers/dpll/sit9531x/core.c | 221 ++++++++++++++++++++++++++++++++++- drivers/dpll/sit9531x/core.h | 5 + drivers/dpll/sit9531x/dpll.c | 81 +++++++++++++ 3 files changed, 303 insertions(+), 4 deletions(-) diff --git a/drivers/dpll/sit9531x/core.c b/drivers/dpll/sit9531x/core.c index a83bf8ac83c1..bba42fe302a3 100644 --- a/drivers/dpll/sit9531x/core.c +++ b/drivers/dpll/sit9531x/core.c @@ -396,6 +396,47 @@ static int sit9531x_output_forced_hiz(struct sit9531x_dev *sitdev, return 0; } +/* + * sit9531x_output_state_refresh - read an output's mute state back + * + * Used when a mute could not be confirmed at the time it was written. The + * driver does not poll output state, so without this the cached value would + * stand until something else happened to write it. + * + * Caller must hold sitdev->multiop_lock. + */ +int sit9531x_output_state_refresh(struct sit9531x_dev *sitdev, u8 out_idx) +{ + bool muted; + int rc; + + lockdep_assert_held(&sitdev->multiop_lock); + + rc = sit9531x_output_forced_hiz(sitdev, out_idx, &muted); + if (rc) + return rc; + + sitdev->out[out_idx].enabled = !muted; + sitdev->out[out_idx].state_stale = false; + + return 0; +} + +static int sit9531x_hiz_set_bit(struct sit9531x_dev *sitdev, + unsigned int reg, u8 bit, bool set) +{ + u8 cur, new_val; + int rc; + + rc = sit9531x_read_u8(sitdev, reg, &cur); + if (rc) + return rc; + + new_val = set ? (cur | BIT(bit)) : (cur & ~BIT(bit)); + + return sit9531x_write_u8(sitdev, reg, new_val); +} + /* * Enter the output-system programming state: unlock the debug * registers on Page 3 and issue the PRG_CMD state command. Register @@ -474,6 +515,166 @@ static int sit9531x_prg_commit(struct sit9531x_dev *sitdev) return rc2 ? rc2 : rc3; } +/* + * sit9531x_output_hiz_write - mute or unmute an output + * + * Muting takes control of the pin (MASK=1) and drives it low (STATE=0) on + * both the differential and the single-ended register pair, because the + * output must go quiet whichever way it is wired; unmuting hands it back + * to the device's own state machine. The caller must already be in the + * programming state. + */ +static int sit9531x_output_hiz_write(struct sit9531x_dev *sitdev, u8 slot, + bool mute) +{ + struct sit9531x_hiz_regs r; + int rc; + + sit9531x_output_get_hiz_regs(slot, &r); + + if (!mute) { + rc = sit9531x_hiz_set_bit(sitdev, r.diff_mask, r.bit, false); + if (rc) + return rc; + + return sit9531x_hiz_set_bit(sitdev, r.se_mask, r.bit, false); + } + + rc = sit9531x_hiz_set_bit(sitdev, r.diff_mask, r.bit, true); + if (rc) + return rc; + rc = sit9531x_hiz_set_bit(sitdev, r.diff_state, r.bit, false); + if (rc) + return rc; + rc = sit9531x_hiz_set_bit(sitdev, r.se_mask, r.bit, true); + if (rc) + return rc; + + return sit9531x_hiz_set_bit(sitdev, r.se_state, r.bit, false); +} + +/* + * sit9531x_output_disable - mute an output (force Hi-Z) + * @index: logical output index (0..info->num_outputs-1) + * + * Sets MASK and clears STATE on BOTH the DIFF and SE register pairs so that the + * output is muted regardless of its electrical configuration. The + * writes are wrapped in the PRG_CMD / NVM update / loop lock sequence + * so the new state is applied by the hardware. + * + * Caller must hold sitdev->multiop_lock. + */ +int sit9531x_output_disable(struct sit9531x_dev *sitdev, u8 index) +{ + const struct sit9531x_chip_info *info = sitdev->info; + bool muted; + u8 slot; + int rc, ret, state_rc; + + lockdep_assert_held(&sitdev->multiop_lock); + + if (index >= info->num_outputs) + return -EINVAL; + + slot = info->clkout_map[index]; + rc = sit9531x_prg_enter(sitdev); + if (rc) + return rc; + + rc = sit9531x_output_hiz_write(sitdev, slot, true); + + /* + * Always leave the PRG_CMD programming state, even on a mid-sequence + * write failure: prg_enter() unlocked the output loops, so returning + * without prg_commit() would strand the chip in the programming state + * with the loops unlocked. Best effort -- keep the first error. + */ + ret = sit9531x_prg_commit(sitdev); + if (ret && !rc) + rc = ret; + + /* + * Keep the software state aligned to what hardware now drives even + * when one write in the sequence failed. The commit above may have + * applied a partial mask/state combination. + */ + state_rc = sit9531x_output_forced_hiz(sitdev, index, &muted); + if (!state_rc) { + sitdev->out[index].enabled = !muted; + sitdev->out[index].state_stale = false; + } else { + /* + * The writes may well have landed; what failed is the proof. + * Mark the cached state for a read-through rather than + * reporting the value it had before this call. + */ + sitdev->out[index].state_stale = true; + if (!rc) + rc = state_rc; + } + + return rc; +} + +/* + * sit9531x_output_enable - un-mute an output (active state) + * @index: logical output index (0..info->num_outputs-1) + * + * Releases MASK on BOTH register pairs so the output returns to + * whatever the initial_config blob programmed. The writes are wrapped + * in the PRG_CMD / NVM update / loop lock sequence so the new state is + * applied by the hardware. + * + * Caller must hold sitdev->multiop_lock. + */ +int sit9531x_output_enable(struct sit9531x_dev *sitdev, u8 index) +{ + const struct sit9531x_chip_info *info = sitdev->info; + bool muted; + u8 slot; + int rc, ret, state_rc; + + lockdep_assert_held(&sitdev->multiop_lock); + + if (index >= info->num_outputs) + return -EINVAL; + + slot = info->clkout_map[index]; + rc = sit9531x_prg_enter(sitdev); + if (rc) + return rc; + + rc = sit9531x_output_hiz_write(sitdev, slot, false); + + /* + * Always leave the PRG_CMD programming state, even on a mid-sequence + * write failure: prg_enter() unlocked the output loops, so returning + * without prg_commit() would strand the chip in the programming state + * with the loops unlocked. Best effort -- keep the first error. + */ + ret = sit9531x_prg_commit(sitdev); + if (ret && !rc) + rc = ret; + + /* See sit9531x_output_disable(): commit can apply a partial sequence. */ + state_rc = sit9531x_output_forced_hiz(sitdev, index, &muted); + if (!state_rc) { + sitdev->out[index].enabled = !muted; + sitdev->out[index].state_stale = false; + } else { + /* + * The writes may well have landed; what failed is the proof. + * Mark the cached state for a read-through rather than + * reporting the value it had before this call. + */ + sitdev->out[index].state_stale = true; + if (!rc) + rc = state_rc; + } + + return rc; +} + /* * Input priority selection * @@ -1815,6 +2016,8 @@ static int sit9531x_out_state_fetch(struct sit9531x_dev *sitdev, u8 index) if (rc) return rc; + sitdev->out[index].state_stale = false; + /* * DT board-config override: the per-PLL OUTPUT_ENABLE bitmaps * (0x27/0x28) do not unambiguously express output->PLL routing on @@ -2455,13 +2658,23 @@ static bool sit9531x_dpll_pin_is_registrable(struct sit9531x_dpll *sitdpll, { struct sit9531x_dev *sitdev = sitdpll->dev; - if (dir != DPLL_PIN_DIRECTION_INPUT) + if (dir == DPLL_PIN_DIRECTION_INPUT) { + if (index == SIT9531X_MAX_INPUTS) + return true; + if (index == SIT9531X_INTSYNC_PIN_ID) + return false; + + return sit9531x_input_pin_is_registrable(sitdev, index); + } + + if (index == SIT9531X_INTSYNC_OUT_PIN_ID) return false; - if (index == SIT9531X_MAX_INPUTS) - return true; + if (index >= sitdev->info->num_outputs) + return false; - return sit9531x_input_pin_is_registrable(sitdev, index); + return sitdev->out[index].pll_idx == sitdpll->id && + sitdev->out[index].routed; } /* diff --git a/drivers/dpll/sit9531x/core.h b/drivers/dpll/sit9531x/core.h index 1ab8ebfd78da..db9c73a79b04 100644 --- a/drivers/dpll/sit9531x/core.h +++ b/drivers/dpll/sit9531x/core.h @@ -100,6 +100,8 @@ struct sit9531x_ref { * @enabled: output is driving, i.e. not forced into Hi-Z * @cmos: output is wired single-ended; the Hi-Z pair that * speaks for it is the SE one, not the differential + * @state_stale: the cached mute state could not be confirmed against + * hardware and has to be read back before it is reported * @routed: output is mapped to @pll_idx by the initial * configuration; an unrouted output has no DPLL pin * @pll_idx: PLL driving this output (0-3) @@ -109,6 +111,7 @@ struct sit9531x_out { u64 freq; bool enabled; bool cmos; + bool state_stale; bool routed; u8 pll_idx; const char *label; @@ -253,6 +256,8 @@ int sit9531x_input_prio_add(struct sit9531x_dev *sitdev, u8 pll_idx, /* ---- Output enable/disable (Hi-Z control) ---- */ int sit9531x_output_disable(struct sit9531x_dev *sitdev, u8 index); int sit9531x_output_enable(struct sit9531x_dev *sitdev, u8 index); +int sit9531x_output_state_refresh(struct sit9531x_dev *sitdev, + u8 out_idx); /* ---- Output frequency ---- */ int sit9531x_output_freq_set(struct sit9531x_dev *sitdev, u8 out_idx, diff --git a/drivers/dpll/sit9531x/dpll.c b/drivers/dpll/sit9531x/dpll.c index 88122a368053..d550eb626e6d 100644 --- a/drivers/dpll/sit9531x/dpll.c +++ b/drivers/dpll/sit9531x/dpll.c @@ -736,10 +736,91 @@ sit9531x_dpll_output_pin_frequency_set(const struct dpll_pin *pin, return rc; } +/* + * sit9531x_dpll_output_pin_state_on_dpll_get - get output pin state + * + * reports CONNECTED when the output is driven and + * DISCONNECTED when it has been muted via sit9531x_output_disable(). + */ +static int +sit9531x_dpll_output_pin_state_on_dpll_get(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, + enum dpll_pin_state *state, + struct netlink_ext_ack *extack) +{ + struct sit9531x_dpll_pin *dpin = pin_priv; + struct sit9531x_dpll *sitdpll = dpll_priv; + struct sit9531x_dev *sitdev = sitdpll->dev; + const struct sit9531x_out *out; + + /* + * A mute whose read-back failed left the cache unconfirmed; there is + * no poll of output state to correct it, so read it here rather than + * report a value that may predate the request. + */ + if (sitdev->out[dpin->id].state_stale) { + mutex_lock(&sitdev->multiop_lock); + sit9531x_output_state_refresh(sitdev, dpin->id); + mutex_unlock(&sitdev->multiop_lock); + } + + out = sit9531x_out_state_get(sitdev, dpin->id); + *state = out->enabled ? DPLL_PIN_STATE_CONNECTED + : DPLL_PIN_STATE_DISCONNECTED; + return 0; +} + +/* + * sit9531x_dpll_output_pin_state_on_dpll_set - mute/un-mute an output + * + * forces Hi-Z on the output pin via the Page 0x03 + * force/state register pair. + * CONNECTED -> enable (release force, back to factory default) + * DISCONNECTED -> disable (force Hi-Z) + */ +static int +sit9531x_dpll_output_pin_state_on_dpll_set(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, + enum dpll_pin_state state, + struct netlink_ext_ack *extack) +{ + struct sit9531x_dpll_pin *dpin = pin_priv; + struct sit9531x_dpll *sitdpll = dpll_priv; + struct sit9531x_dev *sitdev = sitdpll->dev; + int rc; + + mutex_lock(&sitdev->multiop_lock); + + switch (state) { + case DPLL_PIN_STATE_CONNECTED: + rc = sit9531x_output_enable(sitdev, dpin->id); + break; + case DPLL_PIN_STATE_DISCONNECTED: + rc = sit9531x_output_disable(sitdev, dpin->id); + break; + default: + rc = -EINVAL; + break; + } + + mutex_unlock(&sitdev->multiop_lock); + + if (rc) + NL_SET_ERR_MSG(extack, "Failed to set output pin state"); + + return rc; +} + static const struct dpll_pin_ops sit9531x_dpll_output_pin_ops = { .direction_get = sit9531x_dpll_output_pin_direction_get, .frequency_get = sit9531x_dpll_output_pin_frequency_get, .frequency_set = sit9531x_dpll_output_pin_frequency_set, + .state_on_dpll_get = sit9531x_dpll_output_pin_state_on_dpll_get, + .state_on_dpll_set = sit9531x_dpll_output_pin_state_on_dpll_set, }; const struct dpll_pin_ops * -- 2.39.2 (Apple Git-143)