-/* get rate format from rate */
-static inline int soc_get_rate_format(int rate)
-{
- int i;
-
- for (i = 0; i < ARRAY_SIZE(rates); i++) {
- if (rates[i] == rate)
- return 1 << i;
- }
- return 0;
-}
-
-/* gets the audio system mclk/sysclk for the given parameters */
-static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
- struct snd_soc_clock_info *info)
-{
- struct snd_soc_device *socdev = rtd->socdev;
- struct snd_soc_machine *machine = socdev->machine;
- int i;
-
- /* find the matching machine config and get it's mclk for the given
- * sample rate and hardware format */
- for(i = 0; i < machine->num_links; i++) {
- if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
- machine->dai_link[i].config_sysclk)
- return machine->dai_link[i].config_sysclk(rtd, info);
- }
- return 0;
-}
-
-/* changes a bitclk multiplier mask to a divider mask */
-static u16 soc_bfs_mult_to_div(u16 bfs, int rate, unsigned int mclk,
- unsigned int pcmfmt, unsigned int chn)
-{
- int i, j;
- u16 bfs_ = 0;
- int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
-
- if (size <= 0)
- return 0;
-
- /* the minimum bit clock that has enough bandwidth */
- min = size * rate * chn;
- dbgc("mult --> div min bclk %d with mclk %d\n", min, mclk);
-
- for (i = 0; i < 16; i++) {
- if ((bfs >> i) & 0x1) {
- j = rate * SND_SOC_FSB_REAL(1<<i);
-
- if (j >= min) {
- bfs_ |= SND_SOC_FSBD(mclk/j);
- dbgc("mult --> div support mult %d\n",
- SND_SOC_FSB_REAL(1<<i));
- }
- }
- }
-
- return bfs_;
-}
-
-/* changes a bitclk divider mask to a multiplier mask */
-static u16 soc_bfs_div_to_mult(u16 bfs, int rate, unsigned int mclk,
- unsigned int pcmfmt, unsigned int chn)
-{
- int i, j;
- u16 bfs_ = 0;
-
- int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
-
- if (size <= 0)
- return 0;
-
- /* the minimum bit clock that has enough bandwidth */
- min = size * rate * chn;
- dbgc("div to mult min bclk %d with mclk %d\n", min, mclk);
-
- for (i = 0; i < 16; i++) {
- if ((bfs >> i) & 0x1) {
- j = mclk / (SND_SOC_FSBD_REAL(1<<i));
- if (j >= min) {
- bfs_ |= SND_SOC_FSB(j/rate);
- dbgc("div --> mult support div %d\n",
- SND_SOC_FSBD_REAL(1<<i));
- }
- }
- }
-
- return bfs_;
-}
-
-/* Matches codec DAI and SoC CPU DAI hardware parameters */
-static int soc_hw_match_params(struct snd_pcm_substream *substream,
- struct snd_pcm_hw_params *params)
-{
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct snd_soc_dai_mode *codec_dai_mode = NULL;
- struct snd_soc_dai_mode *cpu_dai_mode = NULL;
- struct snd_soc_clock_info clk_info;
- unsigned int fs, mclk, codec_bfs, cpu_bfs, rate = params_rate(params),
- chn, j, k, cpu_bclk, codec_bclk, pcmrate;
- u16 fmt = 0;
-
- dbg("asoc: match version %s\n", SND_SOC_VERSION);
- clk_info.rate = rate;
- pcmrate = soc_get_rate_format(rate);
-
- /* try and find a match from the codec and cpu DAI capabilities */
- for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
- for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
- codec_dai_mode = &rtd->codec_dai->caps.mode[j];
- cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
-
- if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
- pcmrate)) {
- dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
- continue;
- }
-
- fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
- if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
- dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
- continue;
- }
-
- if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
- dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
- j, k);
- continue;
- }
-
- if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
- dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
- continue;
- }
-
- if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
- dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
- continue;
- }
-
- if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
- dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
- continue;
- }
-
- /* todo - still need to add tdm selection */
- rtd->cpu_dai->dai_runtime.fmt =
- rtd->codec_dai->dai_runtime.fmt =
- 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
- 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
- 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
- clk_info.bclk_master =
- rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
-
- /* make sure the ratio between rate and master
- * clock is acceptable*/
- fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
- if (fs == 0) {
- dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
- continue;
- }
- clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
- rtd->codec_dai->dai_runtime.fs = fs;
-
- /* calculate audio system clocking using slowest clocks possible*/
- mclk = soc_get_mclk(rtd, &clk_info);
- if (mclk == 0) {
- dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
- dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
- clk_info.bclk_master);
- continue;
- }
-
- /* calculate word size (per channel) and frame size */
- rtd->codec_dai->dai_runtime.pcmfmt =
- rtd->cpu_dai->dai_runtime.pcmfmt =
- 1 << params_format(params);
-
- chn = params_channels(params);
- /* i2s always has left and right */
- if (params_channels(params) == 1 &&
- rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
- SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
- chn <<= 1;
-
- /* Calculate bfs - the ratio between bitclock and the sample rate
- * We must take into consideration the dividers and multipliers
- * used in the codec and cpu DAI modes. We always choose the
- * lowest possible clocks to reduce power.
- */
- if (codec_dai_mode->flags & cpu_dai_mode->flags &
- SND_SOC_DAI_BFS_DIV) {
- /* cpu & codec bfs dividers */
- rtd->cpu_dai->dai_runtime.bfs =
- rtd->codec_dai->dai_runtime.bfs =
- 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
- } else if (codec_dai_mode->flags & SND_SOC_DAI_BFS_DIV) {
- /* normalise bfs codec divider & cpu mult */
- codec_bfs = soc_bfs_div_to_mult(codec_dai_mode->bfs, rate,
- mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
- rtd->cpu_dai->dai_runtime.bfs =
- 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
- cpu_bfs = soc_bfs_mult_to_div(cpu_dai_mode->bfs, rate, mclk,
- rtd->codec_dai->dai_runtime.pcmfmt, chn);
- rtd->codec_dai->dai_runtime.bfs =
- 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
- } else if (cpu_dai_mode->flags & SND_SOC_DAI_BFS_DIV) {
- /* normalise bfs codec mult & cpu divider */
- codec_bfs = soc_bfs_mult_to_div(codec_dai_mode->bfs, rate,
- mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
- rtd->cpu_dai->dai_runtime.bfs =
- 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
- cpu_bfs = soc_bfs_div_to_mult(cpu_dai_mode->bfs, rate, mclk,
- rtd->codec_dai->dai_runtime.pcmfmt, chn);
- rtd->codec_dai->dai_runtime.bfs =
- 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
- } else {
- /* codec & cpu bfs rate multipliers */
- rtd->cpu_dai->dai_runtime.bfs =
- rtd->codec_dai->dai_runtime.bfs =
- 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
- }
-
- /* make sure the bit clock speed is acceptable */
- if (!rtd->cpu_dai->dai_runtime.bfs ||
- !rtd->codec_dai->dai_runtime.bfs) {
- dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
- dbgc("asoc: cpu_dai %x codec %x\n",
- rtd->cpu_dai->dai_runtime.bfs,
- rtd->codec_dai->dai_runtime.bfs);
- dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
- continue;
- }
-
- goto found;
- }
- }
- printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
- return -EINVAL;
-
-found:
- /* we have matching DAI's, so complete the runtime info */
- rtd->codec_dai->dai_runtime.pcmrate =
- rtd->cpu_dai->dai_runtime.pcmrate =
- soc_get_rate_format(rate);
-
- rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
- rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
- rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
- rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
-
- /* for debug atm */
- dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
- if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
- codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
- SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
- dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
- rtd->codec_dai->dai_runtime.fs, mclk,
- SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
- } else {
- codec_bclk = params_rate(params) *
- SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs);
- dbg("asoc: codec fs %d mclk %d bfs mult %d bclk %d\n",
- rtd->codec_dai->dai_runtime.fs, mclk,
- SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
- }
- if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
- cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
- SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
- dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
- rtd->cpu_dai->dai_runtime.fs, mclk,
- SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
- } else {
- cpu_bclk = params_rate(params) *
- SND_SOC_FSB_REAL(rtd->cpu_dai->dai_runtime.bfs);
- dbg("asoc: cpu fs %d mclk %d bfs mult %d bclk %d\n",
- rtd->cpu_dai->dai_runtime.fs, mclk,
- SND_SOC_FSB_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
- }
-
- /*
- * Check we have matching bitclocks. If we don't then it means the
- * sysclock returned by either the codec or cpu DAI (selected by the
- * machine sysclock function) is wrong compared with the supported DAI
- * modes for the codec or cpu DAI.
- */
- if (cpu_bclk != codec_bclk){
- printk(KERN_ERR
- "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
- );
- printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
- }
-
- switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
- case SND_SOC_DAIFMT_CBM_CFM:
- dbg("asoc: DAI codec BCLK master, LRC master\n");
- break;
- case SND_SOC_DAIFMT_CBS_CFM:
- dbg("asoc: DAI codec BCLK slave, LRC master\n");
- break;
- case SND_SOC_DAIFMT_CBM_CFS:
- dbg("asoc: DAI codec BCLK master, LRC slave\n");
- break;
- case SND_SOC_DAIFMT_CBS_CFS:
- dbg("asoc: DAI codec BCLK slave, LRC slave\n");
- break;
- }
- dbg("asoc: mode %x, invert %x\n",
- rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
- rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
- dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
- params_channels(params), params_format(params));
-
- return 0;
-}
-
-static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
-{
- int i;
- u32 rates = 0;
-
- for(i = 0; i < nmodes; i++)
- rates |= modes[i].pcmrate;
-
- return rates;
-}
-
-static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
-{
- int i;
- u64 formats = 0;
-
- for(i = 0; i < nmodes; i++)
- formats |= modes[i].pcmfmt;
-
- return formats;
-}
-