Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/drivers/intel/ssp/mn.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ static int find_bclk_source(uint32_t bclk,
return -1;
}

/**
* \brief Finds index of SSP clock with the given clock source encoded index.
* \return the index in ssp_freq if could find it, -EINVAL otherwise.
*/
static int find_clk_ssp_index(uint32_t src_enc)
{
int i;

/* searching for the encode value matched bclk source */
for (i = 0; i <= MAX_SSP_FREQ_INDEX; i++)
if (ssp_freq_sources[i] == src_enc)
return i;

return -EINVAL;
}

/**
* \brief Checks if given clock is used as source for any BCLK.
* \param[in] clk_src Bit clock source.
Expand Down Expand Up @@ -405,6 +421,35 @@ static inline int setup_initial_bclk_mn_source(uint32_t bclk, uint32_t *scr_div,
return 0;
}

/**
* \brief Reset M/N source clock for BCLK.
* If no port is using bclk, reset to use SSP_CLOCK_XTAL_OSCILLATOR
Comment thread
plbossart marked this conversation as resolved.
Resolved
* as the default clock source.
*/
static inline void reset_bclk_mn_source(void)
{
struct mn *mn = mn_get();
uint32_t mdivc;
int clk_index = find_clk_ssp_index(SSP_CLOCK_XTAL_OSCILLATOR);
Comment thread
lgirdwood marked this conversation as resolved.
Resolved

if (clk_index < 0) {
tr_err(&mn_tr, "BCLK reset failed, no SSP_CLOCK_XTAL_OSCILLATOR source!");
return;
}

mdivc = mn_reg_read(MN_MDIVCTRL, 0);

/* reset to use XTAL Oscillator */
mdivc &= ~MNDSS(MN_SOURCE_CLKS_MASK);
mdivc |= MNDSS(SSP_CLOCK_XTAL_OSCILLATOR);

mn_reg_write(MN_MDIVCTRL, 0, mdivc);

mn->bclk_source_mn_clock = clk_index;

platform_shared_commit(mn, sizeof(*mn));
}

/**
* \brief Finds valid M/(N * SCR) values for source clock that is already locked
* because other ports use it.
Expand Down Expand Up @@ -533,10 +578,16 @@ int mn_set_bclk(uint32_t dai_index, uint32_t bclk_rate,
void mn_release_bclk(uint32_t dai_index)
{
struct mn *mn = mn_get();
bool mn_in_use;

spin_lock(&mn->lock);
mn->bclk_sources[dai_index] = MN_BCLK_SOURCE_NONE;
platform_shared_commit(mn, sizeof(*mn));

mn_in_use = is_bclk_source_in_use(MN_BCLK_SOURCE_MN);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's incorrect. There are platforms where we can have different sources for BCLK and MCLK, but other where there's a single source.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart but do we use the MN for MCLK too?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

M/N comes after selecting a source, and yes it's possible to apply it there as well IIRC.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart we have MNDSS and MCDSS for all cAVS, at least I am seeing them in spec for APL/CNL/TGL. This driver is for cAVS platforms only, so no need to take care BYT/BDW ones here.

@ranj063 yes, please see the definition and block diagram for MNDSS(for BCLK) and MCDSS(for MCLK). We have M/N for BCLK, but only divider (similar to N) for MCLK, which means we can only set the MCLK frequency as 1/N of the MCLK source frequency.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keyonjie I am 100% positive that there was a change in the clock tree somewhere between APL and TGL. In fact here it is:

see commit 0a97c1a

    drivers: intel: mn: bclk sources
    
    All BCLKs that use M/N share the same source clock, so we need to
    track sources for each BCLK to prevent case in which next SSP config
    would override source clock for previous SSP config.
    
    Since cAVS 2.0, all clocks go through M/N, bypassing XTAL is
    no longer supported.
    For earlier version MN driver checks if XTAL can be used if it
    would result in more efficient settings.
    As a result it fixes ECS-related code for cAVS 2.0+ and is backwards
    compatible with previous logic when there is only SSP0 used.
    Previously topologies that used more than 1 SSP could override
    source clock for previously configured SSP ports, now it will try
    to use current clock with M/N or report error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plbossart Yes, M/N can not be bypassed on cAVS2.x but we can set both M and N to 1.

/* release the M/N clock source if not used */
if (!mn_in_use)
reset_bclk_mn_source();
spin_unlock(&mn->lock);
}

Expand Down
130 changes: 92 additions & 38 deletions src/drivers/intel/ssp/ssp.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ static int ssp_set_config(struct dai *dai,
uint32_t ssrsa;
uint32_t ssto;
uint32_t ssioc;
uint32_t mdiv;
uint32_t bdiv;
uint32_t data_size;
uint32_t frame_end_padding;
Expand All @@ -157,7 +156,6 @@ static int ssp_set_config(struct dai *dai,
bool inverted_frame = false;
bool cfs = false;
bool start_delay = false;
bool need_ecs = false;

int ret = 0;

Expand Down Expand Up @@ -332,42 +330,6 @@ static int ssp_set_config(struct dai *dai,
goto out;
}

#if CONFIG_INTEL_MN
/* BCLK config */
ret = mn_set_bclk(config->dai_index, config->ssp.bclk_rate,
&mdiv, &need_ecs);
if (ret < 0) {
dai_err(dai, "invalid bclk_rate = %d for dai_index = %d",
config->ssp.bclk_rate, config->dai_index);
goto out;
}
#else
if (ssp_freq[SSP_DEFAULT_IDX].freq % config->ssp.bclk_rate != 0) {
dai_err(dai, "invalid bclk_rate = %d for dai_index = %d",
config->ssp.bclk_rate, config->dai_index);
goto out;
}

mdiv = ssp_freq[SSP_DEFAULT_IDX].freq / config->ssp.bclk_rate;
#endif

if (need_ecs)
sscr0 |= SSCR0_ECS;

/* clock divisor is SCR + 1 */
mdiv -= 1;

/* divisor must be within SCR range */
if (mdiv > (SSCR0_SCR_MASK >> 8)) {
dai_err(dai, "ssp_set_config(): divisor %d is not within SCR range",
mdiv);
ret = -EINVAL;
goto out;
}

/* set the SCR divisor */
sscr0 |= SSCR0_SCR(mdiv);

/* calc frame width based on BCLK and rate - must be divisable */
if (config->ssp.bclk_rate % config->ssp.fsync_rate) {
dai_err(dai, "ssp_set_config(): BCLK %d is not divisable by rate %d",
Expand Down Expand Up @@ -659,6 +621,93 @@ static int ssp_set_config(struct dai *dai,
return ret;
}

/*
* Portion of the SSP configuration should be applied just before the
* SSP dai is activated, for either power saving or params runtime
* configurable flexibility.
*/
static int ssp_pre_start(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
struct sof_ipc_dai_config *config = &ssp->config;
uint32_t sscr0;
uint32_t mdiv;
bool need_ecs = false;

int ret = 0;

dai_info(dai, "ssp_pre_start()");

/* SSP active means bclk already configured. */
if (ssp->state[SOF_IPC_STREAM_PLAYBACK] == COMP_STATE_ACTIVE ||
ssp->state[SOF_IPC_STREAM_CAPTURE] == COMP_STATE_ACTIVE)
return 0;

sscr0 = ssp_read(dai, SSCR0);

#if CONFIG_INTEL_MN
/* BCLK config */
ret = mn_set_bclk(config->dai_index, config->ssp.bclk_rate,
&mdiv, &need_ecs);
if (ret < 0) {
dai_err(dai, "invalid bclk_rate = %d for dai_index = %d",
config->ssp.bclk_rate, config->dai_index);
goto out;
}
#else
if (ssp_freq[SSP_DEFAULT_IDX].freq % config->ssp.bclk_rate != 0) {
dai_err(dai, "invalid bclk_rate = %d for dai_index = %d",
config->ssp.bclk_rate, config->dai_index);
goto out;
}

mdiv = ssp_freq[SSP_DEFAULT_IDX].freq / config->ssp.bclk_rate;
#endif

if (need_ecs)
sscr0 |= SSCR0_ECS;

/* clock divisor is SCR + 1 */
mdiv -= 1;

/* divisor must be within SCR range */
if (mdiv > (SSCR0_SCR_MASK >> 8)) {
dai_err(dai, "ssp_pre_start(): divisor %d is not within SCR range",
mdiv);
ret = -EINVAL;
goto out;
}

/* set the SCR divisor */
sscr0 &= ~SSCR0_SCR_MASK;
sscr0 |= SSCR0_SCR(mdiv);

ssp_write(dai, SSCR0, sscr0);

dai_info(dai, "ssp_set_config(), sscr0 = 0x%08x", sscr0);
out:
platform_shared_commit(ssp, sizeof(*ssp));

return ret;
}

/*
* For power saving, we should do kinds of power release when the SSP
* dai is changed to inactive, though the runtime param configuration
* don't have to be reset.
*/
static void ssp_post_stop(struct dai *dai)
{
#if CONFIG_INTEL_MN
struct ssp_pdata *ssp = dai_get_drvdata(dai);

/* release bclk if SSP is inactive */
if (ssp->state[SOF_IPC_STREAM_PLAYBACK] != COMP_STATE_ACTIVE &&
ssp->state[SOF_IPC_STREAM_CAPTURE] != COMP_STATE_ACTIVE)
mn_release_bclk(dai->index);
#endif
}

/* get SSP hw params */
static int ssp_get_hw_params(struct dai *dai,
struct sof_ipc_stream_params *params, int dir)
Expand Down Expand Up @@ -698,6 +747,9 @@ static void ssp_start(struct dai *dai, int direction)

spin_lock(&dai->lock);

/* request mclk/bclk */
ssp_pre_start(dai);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a first patch that moves all the code from ssp_set_config() to ssp_pre_start(), and then add the M/N changes.

@ranj063 ranj063 Nov 19, 2020 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to do this for pause/release as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a first patch that moves all the code from ssp_set_config() to ssp_pre_start(), and then add the M/N changes.

Do you mean split this commit to 2? I thought about that, but compiler may complaint for the first commit that "function defined but not used".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to do this for pause/release as well?

It will be called when ssp_start/stop is called with this change applied, so it will not be applied at ssp_pause() at the moment. For this pause/release scenario, it could be tricky as we don't release clear the RXEN bit at the moment, so forcing to release the bclk might lead to jitter (samples lost), but we are good with this series as we don't do this for pause.


/* enable port */
ssp_update_bits(dai, SSCR0, SSCR0_SSE, SSCR0_SSE);
ssp->state[direction] = COMP_STATE_ACTIVE;
Expand Down Expand Up @@ -764,6 +816,8 @@ static void ssp_stop(struct dai *dai, int direction)
dai_info(dai, "ssp_stop(), SSP port disabled");
}

ssp_post_stop(dai);

spin_unlock(&dai->lock);
}

Expand Down