linux/sound/soc/soc-pcm.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2//
   3// soc-pcm.c  --  ALSA SoC PCM
   4//
   5// Copyright 2005 Wolfson Microelectronics PLC.
   6// Copyright 2005 Openedhand Ltd.
   7// Copyright (C) 2010 Slimlogic Ltd.
   8// Copyright (C) 2010 Texas Instruments Inc.
   9//
  10// Authors: Liam Girdwood <lrg@ti.com>
  11//          Mark Brown <broonie@opensource.wolfsonmicro.com>
  12
  13#include <linux/kernel.h>
  14#include <linux/init.h>
  15#include <linux/delay.h>
  16#include <linux/pinctrl/consumer.h>
  17#include <linux/pm_runtime.h>
  18#include <linux/slab.h>
  19#include <linux/workqueue.h>
  20#include <linux/export.h>
  21#include <linux/debugfs.h>
  22#include <sound/core.h>
  23#include <sound/pcm.h>
  24#include <sound/pcm_params.h>
  25#include <sound/soc.h>
  26#include <sound/soc-dpcm.h>
  27#include <sound/soc-link.h>
  28#include <sound/initval.h>
  29
  30#define DPCM_MAX_BE_USERS       8
  31
  32static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
  33{
  34        return (rtd)->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
  35}
  36static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
  37{
  38        return (rtd)->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
  39}
  40
  41#ifdef CONFIG_DEBUG_FS
  42static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
  43{
  44        switch (state) {
  45        case SND_SOC_DPCM_STATE_NEW:
  46                return "new";
  47        case SND_SOC_DPCM_STATE_OPEN:
  48                return "open";
  49        case SND_SOC_DPCM_STATE_HW_PARAMS:
  50                return "hw_params";
  51        case SND_SOC_DPCM_STATE_PREPARE:
  52                return "prepare";
  53        case SND_SOC_DPCM_STATE_START:
  54                return "start";
  55        case SND_SOC_DPCM_STATE_STOP:
  56                return "stop";
  57        case SND_SOC_DPCM_STATE_SUSPEND:
  58                return "suspend";
  59        case SND_SOC_DPCM_STATE_PAUSED:
  60                return "paused";
  61        case SND_SOC_DPCM_STATE_HW_FREE:
  62                return "hw_free";
  63        case SND_SOC_DPCM_STATE_CLOSE:
  64                return "close";
  65        }
  66
  67        return "unknown";
  68}
  69
  70static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  71                               int stream, char *buf, size_t size)
  72{
  73        struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  74        struct snd_soc_dpcm *dpcm;
  75        ssize_t offset = 0;
  76        unsigned long flags;
  77
  78        /* FE state */
  79        offset += scnprintf(buf + offset, size - offset,
  80                           "[%s - %s]\n", fe->dai_link->name,
  81                           stream ? "Capture" : "Playback");
  82
  83        offset += scnprintf(buf + offset, size - offset, "State: %s\n",
  84                           dpcm_state_string(fe->dpcm[stream].state));
  85
  86        if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  87            (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  88                offset += scnprintf(buf + offset, size - offset,
  89                                   "Hardware Params: "
  90                                   "Format = %s, Channels = %d, Rate = %d\n",
  91                                   snd_pcm_format_name(params_format(params)),
  92                                   params_channels(params),
  93                                   params_rate(params));
  94
  95        /* BEs state */
  96        offset += scnprintf(buf + offset, size - offset, "Backends:\n");
  97
  98        if (list_empty(&fe->dpcm[stream].be_clients)) {
  99                offset += scnprintf(buf + offset, size - offset,
 100                                   " No active DSP links\n");
 101                goto out;
 102        }
 103
 104        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
 105        for_each_dpcm_be(fe, stream, dpcm) {
 106                struct snd_soc_pcm_runtime *be = dpcm->be;
 107                params = &dpcm->hw_params;
 108
 109                offset += scnprintf(buf + offset, size - offset,
 110                                   "- %s\n", be->dai_link->name);
 111
 112                offset += scnprintf(buf + offset, size - offset,
 113                                   "   State: %s\n",
 114                                   dpcm_state_string(be->dpcm[stream].state));
 115
 116                if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
 117                    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
 118                        offset += scnprintf(buf + offset, size - offset,
 119                                           "   Hardware Params: "
 120                                           "Format = %s, Channels = %d, Rate = %d\n",
 121                                           snd_pcm_format_name(params_format(params)),
 122                                           params_channels(params),
 123                                           params_rate(params));
 124        }
 125        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
 126out:
 127        return offset;
 128}
 129
 130static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
 131                                    size_t count, loff_t *ppos)
 132{
 133        struct snd_soc_pcm_runtime *fe = file->private_data;
 134        ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
 135        int stream;
 136        char *buf;
 137
 138        if (fe->num_cpus > 1) {
 139                dev_err(fe->dev,
 140                        "%s doesn't support Multi CPU yet\n", __func__);
 141                return -EINVAL;
 142        }
 143
 144        buf = kmalloc(out_count, GFP_KERNEL);
 145        if (!buf)
 146                return -ENOMEM;
 147
 148        for_each_pcm_streams(stream)
 149                if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
 150                        offset += dpcm_show_state(fe, stream,
 151                                                  buf + offset,
 152                                                  out_count - offset);
 153
 154        ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
 155
 156        kfree(buf);
 157        return ret;
 158}
 159
 160static const struct file_operations dpcm_state_fops = {
 161        .open = simple_open,
 162        .read = dpcm_state_read_file,
 163        .llseek = default_llseek,
 164};
 165
 166void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
 167{
 168        if (!rtd->dai_link->dynamic)
 169                return;
 170
 171        if (!rtd->card->debugfs_card_root)
 172                return;
 173
 174        rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
 175                                                    rtd->card->debugfs_card_root);
 176
 177        debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
 178                            rtd, &dpcm_state_fops);
 179}
 180
 181static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
 182{
 183        char *name;
 184
 185        name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
 186                         stream ? "capture" : "playback");
 187        if (name) {
 188                dpcm->debugfs_state = debugfs_create_dir(
 189                        name, dpcm->fe->debugfs_dpcm_root);
 190                debugfs_create_u32("state", 0644, dpcm->debugfs_state,
 191                                   &dpcm->state);
 192                kfree(name);
 193        }
 194}
 195
 196static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
 197{
 198        debugfs_remove_recursive(dpcm->debugfs_state);
 199}
 200
 201#else
 202static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
 203                                             int stream)
 204{
 205}
 206
 207static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
 208{
 209}
 210#endif
 211
 212/* Set FE's runtime_update state; the state is protected via PCM stream lock
 213 * for avoiding the race with trigger callback.
 214 * If the state is unset and a trigger is pending while the previous operation,
 215 * process the pending trigger action here.
 216 */
 217static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
 218static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
 219                                     int stream, enum snd_soc_dpcm_update state)
 220{
 221        struct snd_pcm_substream *substream =
 222                snd_soc_dpcm_get_substream(fe, stream);
 223
 224        snd_pcm_stream_lock_irq(substream);
 225        if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
 226                dpcm_fe_dai_do_trigger(substream,
 227                                       fe->dpcm[stream].trigger_pending - 1);
 228                fe->dpcm[stream].trigger_pending = 0;
 229        }
 230        fe->dpcm[stream].runtime_update = state;
 231        snd_pcm_stream_unlock_irq(substream);
 232}
 233
 234static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
 235                                     int stream, enum snd_soc_dpcm_update state)
 236{
 237        be->dpcm[stream].runtime_update = state;
 238}
 239
 240/**
 241 * snd_soc_runtime_action() - Increment/Decrement active count for
 242 * PCM runtime components
 243 * @rtd: ASoC PCM runtime that is activated
 244 * @stream: Direction of the PCM stream
 245 * @action: Activate stream if 1. Deactivate if -1.
 246 *
 247 * Increments/Decrements the active count for all the DAIs and components
 248 * attached to a PCM runtime.
 249 * Should typically be called when a stream is opened.
 250 *
 251 * Must be called with the rtd->card->pcm_mutex being held
 252 */
 253void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
 254                            int stream, int action)
 255{
 256        struct snd_soc_dai *dai;
 257        int i;
 258
 259        lockdep_assert_held(&rtd->card->pcm_mutex);
 260
 261        for_each_rtd_dais(rtd, i, dai)
 262                snd_soc_dai_action(dai, stream, action);
 263}
 264EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
 265
 266/**
 267 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
 268 * @rtd: The ASoC PCM runtime that should be checked.
 269 *
 270 * This function checks whether the power down delay should be ignored for a
 271 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
 272 * been configured to ignore the delay, or if none of the components benefits
 273 * from having the delay.
 274 */
 275bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
 276{
 277        struct snd_soc_component *component;
 278        bool ignore = true;
 279        int i;
 280
 281        if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
 282                return true;
 283
 284        for_each_rtd_components(rtd, i, component)
 285                ignore &= !component->driver->use_pmdown_time;
 286
 287        return ignore;
 288}
 289
 290/**
 291 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
 292 * @substream: the pcm substream
 293 * @hw: the hardware parameters
 294 *
 295 * Sets the substream runtime hardware parameters.
 296 */
 297int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
 298        const struct snd_pcm_hardware *hw)
 299{
 300        substream->runtime->hw = *hw;
 301
 302        return 0;
 303}
 304EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
 305
 306/* DPCM stream event, send event to FE and all active BEs. */
 307int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
 308        int event)
 309{
 310        struct snd_soc_dpcm *dpcm;
 311
 312        for_each_dpcm_be(fe, dir, dpcm) {
 313
 314                struct snd_soc_pcm_runtime *be = dpcm->be;
 315
 316                dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
 317                                be->dai_link->name, event, dir);
 318
 319                if ((event == SND_SOC_DAPM_STREAM_STOP) &&
 320                    (be->dpcm[dir].users >= 1))
 321                        continue;
 322
 323                snd_soc_dapm_stream_event(be, dir, event);
 324        }
 325
 326        snd_soc_dapm_stream_event(fe, dir, event);
 327
 328        return 0;
 329}
 330
 331static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
 332                                   struct snd_pcm_hw_params *params)
 333{
 334        if (params) {
 335                dai->rate        = params_rate(params);
 336                dai->channels    = params_channels(params);
 337                dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
 338        } else {
 339                dai->rate        = 0;
 340                dai->channels    = 0;
 341                dai->sample_bits = 0;
 342        }
 343}
 344
 345static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
 346                                        struct snd_soc_dai *soc_dai)
 347{
 348        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 349        int ret;
 350
 351        if (!snd_soc_dai_active(soc_dai))
 352                return 0;
 353
 354#define __soc_pcm_apply_symmetry(name, NAME)                            \
 355        if (soc_dai->name && (soc_dai->driver->symmetric_##name ||      \
 356                              rtd->dai_link->symmetric_##name)) {       \
 357                dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
 358                        #name, soc_dai->name);                          \
 359                                                                        \
 360                ret = snd_pcm_hw_constraint_single(substream->runtime,  \
 361                                                   SNDRV_PCM_HW_PARAM_##NAME,\
 362                                                   soc_dai->name);      \
 363                if (ret < 0) {                                          \
 364                        dev_err(soc_dai->dev,                           \
 365                                "ASoC: Unable to apply %s constraint: %d\n",\
 366                                #name, ret);                            \
 367                        return ret;                                     \
 368                }                                                       \
 369        }
 370
 371        __soc_pcm_apply_symmetry(rate,          RATE);
 372        __soc_pcm_apply_symmetry(channels,      CHANNELS);
 373        __soc_pcm_apply_symmetry(sample_bits,   SAMPLE_BITS);
 374
 375        return 0;
 376}
 377
 378static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
 379                                struct snd_pcm_hw_params *params)
 380{
 381        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 382        struct snd_soc_dai d;
 383        struct snd_soc_dai *dai;
 384        struct snd_soc_dai *cpu_dai;
 385        unsigned int symmetry, i;
 386
 387        d.name = __func__;
 388        soc_pcm_set_dai_params(&d, params);
 389
 390#define __soc_pcm_params_symmetry(xxx)                                  \
 391        symmetry = rtd->dai_link->symmetric_##xxx;                      \
 392        for_each_rtd_dais(rtd, i, dai)                                  \
 393                symmetry |= dai->driver->symmetric_##xxx;               \
 394                                                                        \
 395        if (symmetry)                                                   \
 396                for_each_rtd_cpu_dais(rtd, i, cpu_dai)                  \
 397                        if (!snd_soc_dai_is_dummy(cpu_dai) &&           \
 398                            cpu_dai->xxx && cpu_dai->xxx != d.xxx) {    \
 399                                dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
 400                                        #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
 401                                return -EINVAL;                         \
 402                        }
 403
 404        /* reject unmatched parameters when applying symmetry */
 405        __soc_pcm_params_symmetry(rate);
 406        __soc_pcm_params_symmetry(channels);
 407        __soc_pcm_params_symmetry(sample_bits);
 408
 409        return 0;
 410}
 411
 412static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
 413{
 414        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 415        struct snd_soc_dai_link *link = rtd->dai_link;
 416        struct snd_soc_dai *dai;
 417        unsigned int symmetry, i;
 418
 419        symmetry = link->symmetric_rate ||
 420                link->symmetric_channels ||
 421                link->symmetric_sample_bits;
 422
 423        for_each_rtd_dais(rtd, i, dai)
 424                symmetry = symmetry ||
 425                        dai->driver->symmetric_rate ||
 426                        dai->driver->symmetric_channels ||
 427                        dai->driver->symmetric_sample_bits;
 428
 429        if (symmetry)
 430                substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
 431}
 432
 433static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
 434{
 435        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 436        int ret;
 437
 438        if (!bits)
 439                return;
 440
 441        ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
 442        if (ret != 0)
 443                dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
 444                                 bits, ret);
 445}
 446
 447static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
 448{
 449        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 450        struct snd_soc_dai *cpu_dai;
 451        struct snd_soc_dai *codec_dai;
 452        struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
 453        int stream = substream->stream;
 454        int i;
 455        unsigned int bits = 0, cpu_bits = 0;
 456
 457        for_each_rtd_codec_dais(rtd, i, codec_dai) {
 458                pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
 459
 460                if (pcm_codec->sig_bits == 0) {
 461                        bits = 0;
 462                        break;
 463                }
 464                bits = max(pcm_codec->sig_bits, bits);
 465        }
 466
 467        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
 468                pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
 469
 470                if (pcm_cpu->sig_bits == 0) {
 471                        cpu_bits = 0;
 472                        break;
 473                }
 474                cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
 475        }
 476
 477        soc_pcm_set_msb(substream, bits);
 478        soc_pcm_set_msb(substream, cpu_bits);
 479}
 480
 481static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
 482{
 483        hw->rates               = UINT_MAX;
 484        hw->rate_min            = 0;
 485        hw->rate_max            = UINT_MAX;
 486        hw->channels_min        = 0;
 487        hw->channels_max        = UINT_MAX;
 488        hw->formats             = ULLONG_MAX;
 489}
 490
 491static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
 492                                   struct snd_soc_pcm_stream *p)
 493{
 494        hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
 495
 496        /* setup hw->rate_min/max via hw->rates first */
 497        snd_pcm_hw_limit_rates(hw);
 498
 499        /* update hw->rate_min/max by snd_soc_pcm_stream */
 500        hw->rate_min = max(hw->rate_min, p->rate_min);
 501        hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
 502}
 503
 504static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
 505                                   struct snd_soc_pcm_stream *p)
 506{
 507        hw->channels_min = max(hw->channels_min, p->channels_min);
 508        hw->channels_max = min(hw->channels_max, p->channels_max);
 509}
 510
 511static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
 512                                     struct snd_soc_pcm_stream *p)
 513{
 514        hw->formats &= p->formats;
 515}
 516
 517/**
 518 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
 519 * @rtd: ASoC PCM runtime
 520 * @hw: PCM hardware parameters (output)
 521 * @stream: Direction of the PCM stream
 522 *
 523 * Calculates the subset of stream parameters supported by all DAIs
 524 * associated with the PCM stream.
 525 */
 526int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
 527                            struct snd_pcm_hardware *hw, int stream)
 528{
 529        struct snd_soc_dai *codec_dai;
 530        struct snd_soc_dai *cpu_dai;
 531        struct snd_soc_pcm_stream *codec_stream;
 532        struct snd_soc_pcm_stream *cpu_stream;
 533        unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
 534        int i;
 535
 536        soc_pcm_hw_init(hw);
 537
 538        /* first calculate min/max only for CPUs in the DAI link */
 539        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
 540
 541                /*
 542                 * Skip CPUs which don't support the current stream type.
 543                 * Otherwise, since the rate, channel, and format values will
 544                 * zero in that case, we would have no usable settings left,
 545                 * causing the resulting setup to fail.
 546                 */
 547                if (!snd_soc_dai_stream_valid(cpu_dai, stream))
 548                        continue;
 549
 550                cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
 551
 552                soc_pcm_hw_update_chan(hw, cpu_stream);
 553                soc_pcm_hw_update_rate(hw, cpu_stream);
 554                soc_pcm_hw_update_format(hw, cpu_stream);
 555        }
 556        cpu_chan_min = hw->channels_min;
 557        cpu_chan_max = hw->channels_max;
 558
 559        /* second calculate min/max only for CODECs in the DAI link */
 560        for_each_rtd_codec_dais(rtd, i, codec_dai) {
 561
 562                /*
 563                 * Skip CODECs which don't support the current stream type.
 564                 * Otherwise, since the rate, channel, and format values will
 565                 * zero in that case, we would have no usable settings left,
 566                 * causing the resulting setup to fail.
 567                 */
 568                if (!snd_soc_dai_stream_valid(codec_dai, stream))
 569                        continue;
 570
 571                codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
 572
 573                soc_pcm_hw_update_chan(hw, codec_stream);
 574                soc_pcm_hw_update_rate(hw, codec_stream);
 575                soc_pcm_hw_update_format(hw, codec_stream);
 576        }
 577
 578        /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
 579        if (!hw->channels_min)
 580                return -EINVAL;
 581
 582        /*
 583         * chan min/max cannot be enforced if there are multiple CODEC DAIs
 584         * connected to CPU DAI(s), use CPU DAI's directly and let
 585         * channel allocation be fixed up later
 586         */
 587        if (rtd->num_codecs > 1) {
 588                hw->channels_min = cpu_chan_min;
 589                hw->channels_max = cpu_chan_max;
 590        }
 591
 592        return 0;
 593}
 594EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
 595
 596static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
 597{
 598        struct snd_pcm_hardware *hw = &substream->runtime->hw;
 599        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 600        u64 formats = hw->formats;
 601
 602        /*
 603         * At least one CPU and one CODEC should match. Otherwise, we should
 604         * have bailed out on a higher level, since there would be no CPU or
 605         * CODEC to support the transfer direction in that case.
 606         */
 607        snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
 608
 609        if (formats)
 610                hw->formats &= formats;
 611}
 612
 613static int soc_pcm_components_open(struct snd_pcm_substream *substream)
 614{
 615        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 616        struct snd_soc_component *component;
 617        int i, ret = 0;
 618
 619        for_each_rtd_components(rtd, i, component) {
 620                ret = snd_soc_component_module_get_when_open(component, substream);
 621                if (ret < 0)
 622                        break;
 623
 624                ret = snd_soc_component_open(component, substream);
 625                if (ret < 0)
 626                        break;
 627        }
 628
 629        return ret;
 630}
 631
 632static int soc_pcm_components_close(struct snd_pcm_substream *substream,
 633                                    int rollback)
 634{
 635        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 636        struct snd_soc_component *component;
 637        int i, r, ret = 0;
 638
 639        for_each_rtd_components(rtd, i, component) {
 640                r = snd_soc_component_close(component, substream, rollback);
 641                if (r < 0)
 642                        ret = r; /* use last ret */
 643
 644                snd_soc_component_module_put_when_close(component, substream, rollback);
 645        }
 646
 647        return ret;
 648}
 649
 650static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
 651{
 652        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 653        struct snd_soc_component *component;
 654        struct snd_soc_dai *dai;
 655        int i;
 656
 657        mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
 658
 659        if (!rollback)
 660                snd_soc_runtime_deactivate(rtd, substream->stream);
 661
 662        for_each_rtd_dais(rtd, i, dai)
 663                snd_soc_dai_shutdown(dai, substream, rollback);
 664
 665        snd_soc_link_shutdown(substream, rollback);
 666
 667        soc_pcm_components_close(substream, rollback);
 668
 669
 670        mutex_unlock(&rtd->card->pcm_mutex);
 671
 672        snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
 673
 674        for_each_rtd_components(rtd, i, component)
 675                if (!snd_soc_component_active(component))
 676                        pinctrl_pm_select_sleep_state(component->dev);
 677
 678        return 0;
 679}
 680
 681/*
 682 * Called by ALSA when a PCM substream is closed. Private data can be
 683 * freed here. The cpu DAI, codec DAI, machine and components are also
 684 * shutdown.
 685 */
 686static int soc_pcm_close(struct snd_pcm_substream *substream)
 687{
 688        return soc_pcm_clean(substream, 0);
 689}
 690
 691static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
 692{
 693        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 694        struct snd_pcm_hardware *hw = &substream->runtime->hw;
 695        const char *name_cpu = soc_cpu_dai_name(rtd);
 696        const char *name_codec = soc_codec_dai_name(rtd);
 697        const char *err_msg;
 698        struct device *dev = rtd->dev;
 699
 700        err_msg = "rates";
 701        if (!hw->rates)
 702                goto config_err;
 703
 704        err_msg = "formats";
 705        if (!hw->formats)
 706                goto config_err;
 707
 708        err_msg = "channels";
 709        if (!hw->channels_min || !hw->channels_max ||
 710             hw->channels_min  >  hw->channels_max)
 711                goto config_err;
 712
 713        dev_dbg(dev, "ASoC: %s <-> %s info:\n",         name_codec,
 714                                                        name_cpu);
 715        dev_dbg(dev, "ASoC: rate mask 0x%x\n",          hw->rates);
 716        dev_dbg(dev, "ASoC: ch   min %d max %d\n",      hw->channels_min,
 717                                                        hw->channels_max);
 718        dev_dbg(dev, "ASoC: rate min %d max %d\n",      hw->rate_min,
 719                                                        hw->rate_max);
 720
 721        return 0;
 722
 723config_err:
 724        dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
 725                name_codec, name_cpu, err_msg);
 726        return -EINVAL;
 727}
 728
 729/*
 730 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
 731 * then initialized and any private data can be allocated. This also calls
 732 * startup for the cpu DAI, component, machine and codec DAI.
 733 */
 734static int soc_pcm_open(struct snd_pcm_substream *substream)
 735{
 736        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 737        struct snd_soc_component *component;
 738        struct snd_soc_dai *dai;
 739        int i, ret = 0;
 740
 741        for_each_rtd_components(rtd, i, component)
 742                pinctrl_pm_select_default_state(component->dev);
 743
 744        ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
 745        if (ret < 0)
 746                goto pm_err;
 747
 748        mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
 749
 750        ret = soc_pcm_components_open(substream);
 751        if (ret < 0)
 752                goto err;
 753
 754        ret = snd_soc_link_startup(substream);
 755        if (ret < 0)
 756                goto err;
 757
 758        /* startup the audio subsystem */
 759        for_each_rtd_dais(rtd, i, dai) {
 760                ret = snd_soc_dai_startup(dai, substream);
 761                if (ret < 0)
 762                        goto err;
 763
 764                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 765                        dai->tx_mask = 0;
 766                else
 767                        dai->rx_mask = 0;
 768        }
 769
 770        /* Dynamic PCM DAI links compat checks use dynamic capabilities */
 771        if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
 772                goto dynamic;
 773
 774        /* Check that the codec and cpu DAIs are compatible */
 775        soc_pcm_init_runtime_hw(substream);
 776
 777        soc_pcm_update_symmetry(substream);
 778
 779        ret = soc_hw_sanity_check(substream);
 780        if (ret < 0)
 781                goto err;
 782
 783        soc_pcm_apply_msb(substream);
 784
 785        /* Symmetry only applies if we've already got an active stream. */
 786        for_each_rtd_dais(rtd, i, dai) {
 787                ret = soc_pcm_apply_symmetry(substream, dai);
 788                if (ret != 0)
 789                        goto err;
 790        }
 791dynamic:
 792        snd_soc_runtime_activate(rtd, substream->stream);
 793        ret = 0;
 794err:
 795        mutex_unlock(&rtd->card->pcm_mutex);
 796pm_err:
 797        if (ret < 0) {
 798                soc_pcm_clean(substream, 1);
 799                dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
 800        }
 801
 802        return ret;
 803}
 804
 805static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
 806{
 807        /*
 808         * Currently nothing to do for c2c links
 809         * Since c2c links are internal nodes in the DAPM graph and
 810         * don't interface with the outside world or application layer
 811         * we don't have to do any special handling on close.
 812         */
 813}
 814
 815/*
 816 * Called by ALSA when the PCM substream is prepared, can set format, sample
 817 * rate, etc.  This function is non atomic and can be called multiple times,
 818 * it can refer to the runtime info.
 819 */
 820static int soc_pcm_prepare(struct snd_pcm_substream *substream)
 821{
 822        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 823        struct snd_soc_dai *dai;
 824        int i, ret = 0;
 825
 826        mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
 827
 828        ret = snd_soc_link_prepare(substream);
 829        if (ret < 0)
 830                goto out;
 831
 832        ret = snd_soc_pcm_component_prepare(substream);
 833        if (ret < 0)
 834                goto out;
 835
 836        ret = snd_soc_pcm_dai_prepare(substream);
 837        if (ret < 0)
 838                goto out;
 839
 840        /* cancel any delayed stream shutdown that is pending */
 841        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
 842            rtd->pop_wait) {
 843                rtd->pop_wait = 0;
 844                cancel_delayed_work(&rtd->delayed_work);
 845        }
 846
 847        snd_soc_dapm_stream_event(rtd, substream->stream,
 848                        SND_SOC_DAPM_STREAM_START);
 849
 850        for_each_rtd_dais(rtd, i, dai)
 851                snd_soc_dai_digital_mute(dai, 0, substream->stream);
 852
 853out:
 854        mutex_unlock(&rtd->card->pcm_mutex);
 855
 856        if (ret < 0)
 857                dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
 858
 859        return ret;
 860}
 861
 862static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
 863                                       unsigned int mask)
 864{
 865        struct snd_interval *interval;
 866        int channels = hweight_long(mask);
 867
 868        interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 869        interval->min = channels;
 870        interval->max = channels;
 871}
 872
 873static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
 874{
 875        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 876        struct snd_soc_dai *dai;
 877        int i;
 878
 879        mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
 880
 881        /* clear the corresponding DAIs parameters when going to be inactive */
 882        for_each_rtd_dais(rtd, i, dai) {
 883                int active = snd_soc_dai_stream_active(dai, substream->stream);
 884
 885                if (snd_soc_dai_active(dai) == 1)
 886                        soc_pcm_set_dai_params(dai, NULL);
 887
 888                if (active == 1)
 889                        snd_soc_dai_digital_mute(dai, 1, substream->stream);
 890        }
 891
 892        /* run the stream event */
 893        snd_soc_dapm_stream_stop(rtd, substream->stream);
 894
 895        /* free any machine hw params */
 896        snd_soc_link_hw_free(substream, rollback);
 897
 898        /* free any component resources */
 899        snd_soc_pcm_component_hw_free(substream, rollback);
 900
 901        /* now free hw params for the DAIs  */
 902        for_each_rtd_dais(rtd, i, dai) {
 903                if (!snd_soc_dai_stream_valid(dai, substream->stream))
 904                        continue;
 905
 906                snd_soc_dai_hw_free(dai, substream, rollback);
 907        }
 908
 909        mutex_unlock(&rtd->card->pcm_mutex);
 910        return 0;
 911}
 912
 913/*
 914 * Frees resources allocated by hw_params, can be called multiple times
 915 */
 916static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
 917{
 918        return soc_pcm_hw_clean(substream, 0);
 919}
 920
 921/*
 922 * Called by ALSA when the hardware params are set by application. This
 923 * function can also be called multiple times and can allocate buffers
 924 * (using snd_pcm_lib_* ). It's non-atomic.
 925 */
 926static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
 927                                struct snd_pcm_hw_params *params)
 928{
 929        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 930        struct snd_soc_dai *cpu_dai;
 931        struct snd_soc_dai *codec_dai;
 932        int i, ret = 0;
 933
 934        mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
 935
 936        ret = soc_pcm_params_symmetry(substream, params);
 937        if (ret)
 938                goto out;
 939
 940        ret = snd_soc_link_hw_params(substream, params);
 941        if (ret < 0)
 942                goto out;
 943
 944        for_each_rtd_codec_dais(rtd, i, codec_dai) {
 945                struct snd_pcm_hw_params codec_params;
 946
 947                /*
 948                 * Skip CODECs which don't support the current stream type,
 949                 * the idea being that if a CODEC is not used for the currently
 950                 * set up transfer direction, it should not need to be
 951                 * configured, especially since the configuration used might
 952                 * not even be supported by that CODEC. There may be cases
 953                 * however where a CODEC needs to be set up although it is
 954                 * actually not being used for the transfer, e.g. if a
 955                 * capture-only CODEC is acting as an LRCLK and/or BCLK master
 956                 * for the DAI link including a playback-only CODEC.
 957                 * If this becomes necessary, we will have to augment the
 958                 * machine driver setup with information on how to act, so
 959                 * we can do the right thing here.
 960                 */
 961                if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
 962                        continue;
 963
 964                /* copy params for each codec */
 965                codec_params = *params;
 966
 967                /* fixup params based on TDM slot masks */
 968                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
 969                    codec_dai->tx_mask)
 970                        soc_pcm_codec_params_fixup(&codec_params,
 971                                                   codec_dai->tx_mask);
 972
 973                if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
 974                    codec_dai->rx_mask)
 975                        soc_pcm_codec_params_fixup(&codec_params,
 976                                                   codec_dai->rx_mask);
 977
 978                ret = snd_soc_dai_hw_params(codec_dai, substream,
 979                                            &codec_params);
 980                if(ret < 0)
 981                        goto out;
 982
 983                soc_pcm_set_dai_params(codec_dai, &codec_params);
 984                snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
 985        }
 986
 987        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
 988                /*
 989                 * Skip CPUs which don't support the current stream
 990                 * type. See soc_pcm_init_runtime_hw() for more details
 991                 */
 992                if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
 993                        continue;
 994
 995                ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
 996                if (ret < 0)
 997                        goto out;
 998
 999                /* store the parameters for each DAI */
1000                soc_pcm_set_dai_params(cpu_dai, params);
1001                snd_soc_dapm_update_dai(substream, params, cpu_dai);
1002        }
1003
1004        ret = snd_soc_pcm_component_hw_params(substream, params);
1005out:
1006        mutex_unlock(&rtd->card->pcm_mutex);
1007
1008        if (ret < 0) {
1009                soc_pcm_hw_clean(substream, 1);
1010                dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1011        }
1012
1013        return ret;
1014}
1015
1016static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1017{
1018        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1019        int ret = -EINVAL, _ret = 0;
1020        int rollback = 0;
1021
1022        switch (cmd) {
1023        case SNDRV_PCM_TRIGGER_START:
1024        case SNDRV_PCM_TRIGGER_RESUME:
1025        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1026                ret = snd_soc_link_trigger(substream, cmd, 0);
1027                if (ret < 0)
1028                        goto start_err;
1029
1030                ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1031                if (ret < 0)
1032                        goto start_err;
1033
1034                ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1035start_err:
1036                if (ret < 0)
1037                        rollback = 1;
1038        }
1039
1040        if (rollback) {
1041                _ret = ret;
1042                switch (cmd) {
1043                case SNDRV_PCM_TRIGGER_START:
1044                        cmd = SNDRV_PCM_TRIGGER_STOP;
1045                        break;
1046                case SNDRV_PCM_TRIGGER_RESUME:
1047                        cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1048                        break;
1049                case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1050                        cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1051                        break;
1052                }
1053        }
1054
1055        switch (cmd) {
1056        case SNDRV_PCM_TRIGGER_STOP:
1057        case SNDRV_PCM_TRIGGER_SUSPEND:
1058        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1059                if (rtd->dai_link->stop_dma_first) {
1060                        ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1061                        if (ret < 0)
1062                                break;
1063
1064                        ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1065                        if (ret < 0)
1066                                break;
1067                } else {
1068                        ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1069                        if (ret < 0)
1070                                break;
1071
1072                        ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1073                        if (ret < 0)
1074                                break;
1075                }
1076                ret = snd_soc_link_trigger(substream, cmd, rollback);
1077                break;
1078        }
1079
1080        if (_ret)
1081                ret = _ret;
1082
1083        return ret;
1084}
1085
1086/*
1087 * soc level wrapper for pointer callback
1088 * If cpu_dai, codec_dai, component driver has the delay callback, then
1089 * the runtime->delay will be updated accordingly.
1090 */
1091static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1092{
1093        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1094        struct snd_soc_dai *cpu_dai;
1095        struct snd_soc_dai *codec_dai;
1096        struct snd_pcm_runtime *runtime = substream->runtime;
1097        snd_pcm_uframes_t offset = 0;
1098        snd_pcm_sframes_t delay = 0;
1099        snd_pcm_sframes_t codec_delay = 0;
1100        snd_pcm_sframes_t cpu_delay = 0;
1101        int i;
1102
1103        /* clearing the previous total delay */
1104        runtime->delay = 0;
1105
1106        offset = snd_soc_pcm_component_pointer(substream);
1107
1108        /* base delay if assigned in pointer callback */
1109        delay = runtime->delay;
1110
1111        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1112                cpu_delay = max(cpu_delay,
1113                                snd_soc_dai_delay(cpu_dai, substream));
1114        }
1115        delay += cpu_delay;
1116
1117        for_each_rtd_codec_dais(rtd, i, codec_dai) {
1118                codec_delay = max(codec_delay,
1119                                  snd_soc_dai_delay(codec_dai, substream));
1120        }
1121        delay += codec_delay;
1122
1123        runtime->delay = delay;
1124
1125        return offset;
1126}
1127
1128/* connect a FE and BE */
1129static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1130                struct snd_soc_pcm_runtime *be, int stream)
1131{
1132        struct snd_soc_dpcm *dpcm;
1133        unsigned long flags;
1134
1135        /* only add new dpcms */
1136        for_each_dpcm_be(fe, stream, dpcm) {
1137                if (dpcm->be == be && dpcm->fe == fe)
1138                        return 0;
1139        }
1140
1141        dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1142        if (!dpcm)
1143                return -ENOMEM;
1144
1145        dpcm->be = be;
1146        dpcm->fe = fe;
1147        be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1148        dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1149        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1150        list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1151        list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1152        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1153
1154        dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1155                        stream ? "capture" : "playback",  fe->dai_link->name,
1156                        stream ? "<-" : "->", be->dai_link->name);
1157
1158        dpcm_create_debugfs_state(dpcm, stream);
1159
1160        return 1;
1161}
1162
1163/* reparent a BE onto another FE */
1164static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1165                        struct snd_soc_pcm_runtime *be, int stream)
1166{
1167        struct snd_soc_dpcm *dpcm;
1168        struct snd_pcm_substream *fe_substream, *be_substream;
1169
1170        /* reparent if BE is connected to other FEs */
1171        if (!be->dpcm[stream].users)
1172                return;
1173
1174        be_substream = snd_soc_dpcm_get_substream(be, stream);
1175
1176        for_each_dpcm_fe(be, stream, dpcm) {
1177                if (dpcm->fe == fe)
1178                        continue;
1179
1180                dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1181                        stream ? "capture" : "playback",
1182                        dpcm->fe->dai_link->name,
1183                        stream ? "<-" : "->", dpcm->be->dai_link->name);
1184
1185                fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1186                be_substream->runtime = fe_substream->runtime;
1187                break;
1188        }
1189}
1190
1191/* disconnect a BE and FE */
1192void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1193{
1194        struct snd_soc_dpcm *dpcm, *d;
1195        unsigned long flags;
1196
1197        for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1198                dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1199                                stream ? "capture" : "playback",
1200                                dpcm->be->dai_link->name);
1201
1202                if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1203                        continue;
1204
1205                dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1206                        stream ? "capture" : "playback", fe->dai_link->name,
1207                        stream ? "<-" : "->", dpcm->be->dai_link->name);
1208
1209                /* BEs still alive need new FE */
1210                dpcm_be_reparent(fe, dpcm->be, stream);
1211
1212                dpcm_remove_debugfs_state(dpcm);
1213
1214                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1215                list_del(&dpcm->list_be);
1216                list_del(&dpcm->list_fe);
1217                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1218                kfree(dpcm);
1219        }
1220}
1221
1222/* get BE for DAI widget and stream */
1223static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1224                struct snd_soc_dapm_widget *widget, int stream)
1225{
1226        struct snd_soc_pcm_runtime *be;
1227        struct snd_soc_dapm_widget *w;
1228        struct snd_soc_dai *dai;
1229        int i;
1230
1231        dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1232
1233        for_each_card_rtds(card, be) {
1234
1235                if (!be->dai_link->no_pcm)
1236                        continue;
1237
1238                for_each_rtd_dais(be, i, dai) {
1239                        w = snd_soc_dai_get_widget(dai, stream);
1240
1241                        dev_dbg(card->dev, "ASoC: try BE : %s\n",
1242                                w ? w->name : "(not set)");
1243
1244                        if (w == widget)
1245                                return be;
1246                }
1247        }
1248
1249        /* Widget provided is not a BE */
1250        return NULL;
1251}
1252
1253static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1254                struct snd_soc_dapm_widget *widget)
1255{
1256        struct snd_soc_dapm_widget *w;
1257        int i;
1258
1259        for_each_dapm_widgets(list, i, w)
1260                if (widget == w)
1261                        return 1;
1262
1263        return 0;
1264}
1265
1266static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1267                enum snd_soc_dapm_direction dir)
1268{
1269        struct snd_soc_card *card = widget->dapm->card;
1270        struct snd_soc_pcm_runtime *rtd;
1271        int stream;
1272
1273        /* adjust dir to stream */
1274        if (dir == SND_SOC_DAPM_DIR_OUT)
1275                stream = SNDRV_PCM_STREAM_PLAYBACK;
1276        else
1277                stream = SNDRV_PCM_STREAM_CAPTURE;
1278
1279        rtd = dpcm_get_be(card, widget, stream);
1280        if (rtd)
1281                return true;
1282
1283        return false;
1284}
1285
1286int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1287        int stream, struct snd_soc_dapm_widget_list **list)
1288{
1289        struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1290        int paths;
1291
1292        if (fe->num_cpus > 1) {
1293                dev_err(fe->dev,
1294                        "%s doesn't support Multi CPU yet\n", __func__);
1295                return -EINVAL;
1296        }
1297
1298        /* get number of valid DAI paths and their widgets */
1299        paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1300                        fe->card->component_chaining ?
1301                                NULL : dpcm_end_walk_at_be);
1302
1303        if (paths > 0)
1304                dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1305                        stream ? "capture" : "playback");
1306        else if (paths == 0)
1307                dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1308                         stream ? "capture" : "playback");
1309
1310        return paths;
1311}
1312
1313void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1314{
1315        snd_soc_dapm_dai_free_widgets(list);
1316}
1317
1318static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1319                              struct snd_soc_dapm_widget_list *list)
1320{
1321        struct snd_soc_dapm_widget *widget;
1322        struct snd_soc_dai *dai;
1323        unsigned int i;
1324
1325        /* is there a valid DAI widget for this BE */
1326        for_each_rtd_dais(dpcm->be, i, dai) {
1327                widget = snd_soc_dai_get_widget(dai, stream);
1328
1329                /*
1330                 * The BE is pruned only if none of the dai
1331                 * widgets are in the active list.
1332                 */
1333                if (widget && widget_in_list(list, widget))
1334                        return true;
1335        }
1336
1337        return false;
1338}
1339
1340static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1341                            struct snd_soc_dapm_widget_list **list_)
1342{
1343        struct snd_soc_dpcm *dpcm;
1344        int prune = 0;
1345
1346        /* Destroy any old FE <--> BE connections */
1347        for_each_dpcm_be(fe, stream, dpcm) {
1348                if (dpcm_be_is_active(dpcm, stream, *list_))
1349                        continue;
1350
1351                dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1352                        stream ? "capture" : "playback",
1353                        dpcm->be->dai_link->name, fe->dai_link->name);
1354                dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1355                dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1356                prune++;
1357        }
1358
1359        dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1360        return prune;
1361}
1362
1363static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1364        struct snd_soc_dapm_widget_list **list_)
1365{
1366        struct snd_soc_card *card = fe->card;
1367        struct snd_soc_dapm_widget_list *list = *list_;
1368        struct snd_soc_pcm_runtime *be;
1369        struct snd_soc_dapm_widget *widget;
1370        int i, new = 0, err;
1371
1372        /* Create any new FE <--> BE connections */
1373        for_each_dapm_widgets(list, i, widget) {
1374
1375                switch (widget->id) {
1376                case snd_soc_dapm_dai_in:
1377                        if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1378                                continue;
1379                        break;
1380                case snd_soc_dapm_dai_out:
1381                        if (stream != SNDRV_PCM_STREAM_CAPTURE)
1382                                continue;
1383                        break;
1384                default:
1385                        continue;
1386                }
1387
1388                /* is there a valid BE rtd for this widget */
1389                be = dpcm_get_be(card, widget, stream);
1390                if (!be) {
1391                        dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1392                                widget->name);
1393                        continue;
1394                }
1395
1396                /* don't connect if FE is not running */
1397                if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1398                        continue;
1399
1400                /* newly connected FE and BE */
1401                err = dpcm_be_connect(fe, be, stream);
1402                if (err < 0) {
1403                        dev_err(fe->dev, "ASoC: can't connect %s\n",
1404                                widget->name);
1405                        break;
1406                } else if (err == 0) /* already connected */
1407                        continue;
1408
1409                /* new */
1410                dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1411                new++;
1412        }
1413
1414        dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1415        return new;
1416}
1417
1418/*
1419 * Find the corresponding BE DAIs that source or sink audio to this
1420 * FE substream.
1421 */
1422int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1423        int stream, struct snd_soc_dapm_widget_list **list, int new)
1424{
1425        if (new)
1426                return dpcm_add_paths(fe, stream, list);
1427        else
1428                return dpcm_prune_paths(fe, stream, list);
1429}
1430
1431void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1432{
1433        struct snd_soc_dpcm *dpcm;
1434        unsigned long flags;
1435
1436        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1437        for_each_dpcm_be(fe, stream, dpcm)
1438                dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1439        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1440}
1441
1442void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1443                      int do_hw_free, struct snd_soc_dpcm *last)
1444{
1445        struct snd_soc_dpcm *dpcm;
1446
1447        /* disable any enabled and non active backends */
1448        for_each_dpcm_be(fe, stream, dpcm) {
1449                struct snd_soc_pcm_runtime *be = dpcm->be;
1450                struct snd_pcm_substream *be_substream =
1451                        snd_soc_dpcm_get_substream(be, stream);
1452
1453                if (dpcm == last)
1454                        return;
1455
1456                /* is this op for this BE ? */
1457                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1458                        continue;
1459
1460                if (be->dpcm[stream].users == 0) {
1461                        dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1462                                stream ? "capture" : "playback",
1463                                be->dpcm[stream].state);
1464                        continue;
1465                }
1466
1467                if (--be->dpcm[stream].users != 0)
1468                        continue;
1469
1470                if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1471                        if (!do_hw_free)
1472                                continue;
1473
1474                        if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1475                                soc_pcm_hw_free(be_substream);
1476                                be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1477                        }
1478                }
1479
1480                soc_pcm_close(be_substream);
1481                be_substream->runtime = NULL;
1482                be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1483        }
1484}
1485
1486int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1487{
1488        struct snd_soc_pcm_runtime *be;
1489        struct snd_soc_dpcm *dpcm;
1490        int err, count = 0;
1491
1492        /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1493        for_each_dpcm_be(fe, stream, dpcm) {
1494                struct snd_pcm_substream *be_substream;
1495
1496                be = dpcm->be;
1497                be_substream = snd_soc_dpcm_get_substream(be, stream);
1498
1499                if (!be_substream) {
1500                        dev_err(be->dev, "ASoC: no backend %s stream\n",
1501                                stream ? "capture" : "playback");
1502                        continue;
1503                }
1504
1505                /* is this op for this BE ? */
1506                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1507                        continue;
1508
1509                /* first time the dpcm is open ? */
1510                if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1511                        dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1512                                stream ? "capture" : "playback",
1513                                be->dpcm[stream].state);
1514                        continue;
1515                }
1516
1517                if (be->dpcm[stream].users++ != 0)
1518                        continue;
1519
1520                if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1521                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1522                        continue;
1523
1524                dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1525                        stream ? "capture" : "playback", be->dai_link->name);
1526
1527                be_substream->runtime = be->dpcm[stream].runtime;
1528                err = soc_pcm_open(be_substream);
1529                if (err < 0) {
1530                        be->dpcm[stream].users--;
1531                        if (be->dpcm[stream].users < 0)
1532                                dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1533                                        stream ? "capture" : "playback",
1534                                        be->dpcm[stream].state);
1535
1536                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1537                        goto unwind;
1538                }
1539
1540                be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1541                count++;
1542        }
1543
1544        return count;
1545
1546unwind:
1547        dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1548
1549        dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1550                __func__, be->dai_link->name, err);
1551
1552        return err;
1553}
1554
1555static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1556{
1557        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1558        struct snd_pcm_runtime *runtime = substream->runtime;
1559        struct snd_pcm_hardware *hw = &runtime->hw;
1560        struct snd_soc_dai *dai;
1561        int stream = substream->stream;
1562        int i;
1563
1564        soc_pcm_hw_init(hw);
1565
1566        for_each_rtd_cpu_dais(fe, i, dai) {
1567                struct snd_soc_pcm_stream *cpu_stream;
1568
1569                /*
1570                 * Skip CPUs which don't support the current stream
1571                 * type. See soc_pcm_init_runtime_hw() for more details
1572                 */
1573                if (!snd_soc_dai_stream_valid(dai, stream))
1574                        continue;
1575
1576                cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1577
1578                soc_pcm_hw_update_rate(hw, cpu_stream);
1579                soc_pcm_hw_update_chan(hw, cpu_stream);
1580                soc_pcm_hw_update_format(hw, cpu_stream);
1581        }
1582
1583}
1584
1585static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1586{
1587        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1588        struct snd_pcm_runtime *runtime = substream->runtime;
1589        struct snd_pcm_hardware *hw = &runtime->hw;
1590        struct snd_soc_dpcm *dpcm;
1591        struct snd_soc_dai *dai;
1592        int stream = substream->stream;
1593
1594        if (!fe->dai_link->dpcm_merged_format)
1595                return;
1596
1597        /*
1598         * It returns merged BE codec format
1599         * if FE want to use it (= dpcm_merged_format)
1600         */
1601
1602        for_each_dpcm_be(fe, stream, dpcm) {
1603                struct snd_soc_pcm_runtime *be = dpcm->be;
1604                struct snd_soc_pcm_stream *codec_stream;
1605                int i;
1606
1607                for_each_rtd_codec_dais(be, i, dai) {
1608                        /*
1609                         * Skip CODECs which don't support the current stream
1610                         * type. See soc_pcm_init_runtime_hw() for more details
1611                         */
1612                        if (!snd_soc_dai_stream_valid(dai, stream))
1613                                continue;
1614
1615                        codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1616
1617                        soc_pcm_hw_update_format(hw, codec_stream);
1618                }
1619        }
1620}
1621
1622static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1623{
1624        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1625        struct snd_pcm_runtime *runtime = substream->runtime;
1626        struct snd_pcm_hardware *hw = &runtime->hw;
1627        struct snd_soc_dpcm *dpcm;
1628        int stream = substream->stream;
1629
1630        if (!fe->dai_link->dpcm_merged_chan)
1631                return;
1632
1633        /*
1634         * It returns merged BE codec channel;
1635         * if FE want to use it (= dpcm_merged_chan)
1636         */
1637
1638        for_each_dpcm_be(fe, stream, dpcm) {
1639                struct snd_soc_pcm_runtime *be = dpcm->be;
1640                struct snd_soc_pcm_stream *codec_stream;
1641                struct snd_soc_pcm_stream *cpu_stream;
1642                struct snd_soc_dai *dai;
1643                int i;
1644
1645                for_each_rtd_cpu_dais(be, i, dai) {
1646                        /*
1647                         * Skip CPUs which don't support the current stream
1648                         * type. See soc_pcm_init_runtime_hw() for more details
1649                         */
1650                        if (!snd_soc_dai_stream_valid(dai, stream))
1651                                continue;
1652
1653                        cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1654
1655                        soc_pcm_hw_update_chan(hw, cpu_stream);
1656                }
1657
1658                /*
1659                 * chan min/max cannot be enforced if there are multiple CODEC
1660                 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1661                 */
1662                if (be->num_codecs == 1) {
1663                        codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1664
1665                        soc_pcm_hw_update_chan(hw, codec_stream);
1666                }
1667        }
1668}
1669
1670static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1671{
1672        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1673        struct snd_pcm_runtime *runtime = substream->runtime;
1674        struct snd_pcm_hardware *hw = &runtime->hw;
1675        struct snd_soc_dpcm *dpcm;
1676        int stream = substream->stream;
1677
1678        if (!fe->dai_link->dpcm_merged_rate)
1679                return;
1680
1681        /*
1682         * It returns merged BE codec channel;
1683         * if FE want to use it (= dpcm_merged_chan)
1684         */
1685
1686        for_each_dpcm_be(fe, stream, dpcm) {
1687                struct snd_soc_pcm_runtime *be = dpcm->be;
1688                struct snd_soc_pcm_stream *pcm;
1689                struct snd_soc_dai *dai;
1690                int i;
1691
1692                for_each_rtd_dais(be, i, dai) {
1693                        /*
1694                         * Skip DAIs which don't support the current stream
1695                         * type. See soc_pcm_init_runtime_hw() for more details
1696                         */
1697                        if (!snd_soc_dai_stream_valid(dai, stream))
1698                                continue;
1699
1700                        pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1701
1702                        soc_pcm_hw_update_rate(hw, pcm);
1703                }
1704        }
1705}
1706
1707static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1708                               int stream)
1709{
1710        struct snd_soc_dpcm *dpcm;
1711        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1712        struct snd_soc_dai *fe_cpu_dai;
1713        int err = 0;
1714        int i;
1715
1716        /* apply symmetry for FE */
1717        soc_pcm_update_symmetry(fe_substream);
1718
1719        for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1720                /* Symmetry only applies if we've got an active stream. */
1721                err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1722                if (err < 0)
1723                        goto error;
1724        }
1725
1726        /* apply symmetry for BE */
1727        for_each_dpcm_be(fe, stream, dpcm) {
1728                struct snd_soc_pcm_runtime *be = dpcm->be;
1729                struct snd_pcm_substream *be_substream =
1730                        snd_soc_dpcm_get_substream(be, stream);
1731                struct snd_soc_pcm_runtime *rtd;
1732                struct snd_soc_dai *dai;
1733
1734                /* A backend may not have the requested substream */
1735                if (!be_substream)
1736                        continue;
1737
1738                rtd = asoc_substream_to_rtd(be_substream);
1739                if (rtd->dai_link->be_hw_params_fixup)
1740                        continue;
1741
1742                soc_pcm_update_symmetry(be_substream);
1743
1744                /* Symmetry only applies if we've got an active stream. */
1745                for_each_rtd_dais(rtd, i, dai) {
1746                        err = soc_pcm_apply_symmetry(fe_substream, dai);
1747                        if (err < 0)
1748                                goto error;
1749                }
1750        }
1751error:
1752        if (err < 0)
1753                dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
1754
1755        return err;
1756}
1757
1758static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1759{
1760        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1761        int stream = fe_substream->stream, ret = 0;
1762
1763        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1764
1765        ret = dpcm_be_dai_startup(fe, stream);
1766        if (ret < 0)
1767                goto be_err;
1768
1769        dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1770
1771        /* start the DAI frontend */
1772        ret = soc_pcm_open(fe_substream);
1773        if (ret < 0)
1774                goto unwind;
1775
1776        fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1777
1778        dpcm_runtime_setup_fe(fe_substream);
1779
1780        dpcm_runtime_setup_be_format(fe_substream);
1781        dpcm_runtime_setup_be_chan(fe_substream);
1782        dpcm_runtime_setup_be_rate(fe_substream);
1783
1784        ret = dpcm_apply_symmetry(fe_substream, stream);
1785
1786unwind:
1787        if (ret < 0)
1788                dpcm_be_dai_startup_unwind(fe, stream);
1789be_err:
1790        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1791
1792        if (ret < 0)
1793                dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
1794
1795        return ret;
1796}
1797
1798static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1799{
1800        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1801        int stream = substream->stream;
1802
1803        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1804
1805        /* shutdown the BEs */
1806        dpcm_be_dai_shutdown(fe, stream);
1807
1808        dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1809
1810        /* now shutdown the frontend */
1811        soc_pcm_close(substream);
1812
1813        /* run the stream stop event */
1814        dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1815
1816        fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1817        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1818        return 0;
1819}
1820
1821void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1822{
1823        struct snd_soc_dpcm *dpcm;
1824
1825        /* only hw_params backends that are either sinks or sources
1826         * to this frontend DAI */
1827        for_each_dpcm_be(fe, stream, dpcm) {
1828
1829                struct snd_soc_pcm_runtime *be = dpcm->be;
1830                struct snd_pcm_substream *be_substream =
1831                        snd_soc_dpcm_get_substream(be, stream);
1832
1833                /* is this op for this BE ? */
1834                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1835                        continue;
1836
1837                /* only free hw when no longer used - check all FEs */
1838                if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1839                                continue;
1840
1841                /* do not free hw if this BE is used by other FE */
1842                if (be->dpcm[stream].users > 1)
1843                        continue;
1844
1845                if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1846                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1847                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1848                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1849                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1850                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1851                        continue;
1852
1853                dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1854                        be->dai_link->name);
1855
1856                soc_pcm_hw_free(be_substream);
1857
1858                be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1859        }
1860}
1861
1862static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1863{
1864        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1865        int stream = substream->stream;
1866
1867        mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1868        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1869
1870        dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1871
1872        /* call hw_free on the frontend */
1873        soc_pcm_hw_free(substream);
1874
1875        /* only hw_params backends that are either sinks or sources
1876         * to this frontend DAI */
1877        dpcm_be_dai_hw_free(fe, stream);
1878
1879        fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1880        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1881
1882        mutex_unlock(&fe->card->mutex);
1883        return 0;
1884}
1885
1886int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1887{
1888        struct snd_soc_pcm_runtime *be;
1889        struct snd_pcm_substream *be_substream;
1890        struct snd_soc_dpcm *dpcm;
1891        int ret;
1892
1893        for_each_dpcm_be(fe, stream, dpcm) {
1894                be = dpcm->be;
1895                be_substream = snd_soc_dpcm_get_substream(be, stream);
1896
1897                /* is this op for this BE ? */
1898                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1899                        continue;
1900
1901                /* copy params for each dpcm */
1902                memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1903                                sizeof(struct snd_pcm_hw_params));
1904
1905                /* perform any hw_params fixups */
1906                ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1907                if (ret < 0)
1908                        goto unwind;
1909
1910                /* copy the fixed-up hw params for BE dai */
1911                memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1912                       sizeof(struct snd_pcm_hw_params));
1913
1914                /* only allow hw_params() if no connected FEs are running */
1915                if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1916                        continue;
1917
1918                if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1919                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1920                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1921                        continue;
1922
1923                dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1924                        be->dai_link->name);
1925
1926                ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1927                if (ret < 0)
1928                        goto unwind;
1929
1930                be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1931        }
1932        return 0;
1933
1934unwind:
1935        dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1936                __func__, be->dai_link->name, ret);
1937
1938        /* disable any enabled and non active backends */
1939        for_each_dpcm_be_rollback(fe, stream, dpcm) {
1940                be = dpcm->be;
1941                be_substream = snd_soc_dpcm_get_substream(be, stream);
1942
1943                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1944                        continue;
1945
1946                /* only allow hw_free() if no connected FEs are running */
1947                if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1948                        continue;
1949
1950                if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1951                   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1952                   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1953                   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1954                        continue;
1955
1956                soc_pcm_hw_free(be_substream);
1957        }
1958
1959        return ret;
1960}
1961
1962static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1963                                 struct snd_pcm_hw_params *params)
1964{
1965        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1966        int ret, stream = substream->stream;
1967
1968        mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1969        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1970
1971        memcpy(&fe->dpcm[stream].hw_params, params,
1972                        sizeof(struct snd_pcm_hw_params));
1973        ret = dpcm_be_dai_hw_params(fe, stream);
1974        if (ret < 0)
1975                goto out;
1976
1977        dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1978                        fe->dai_link->name, params_rate(params),
1979                        params_channels(params), params_format(params));
1980
1981        /* call hw_params on the frontend */
1982        ret = soc_pcm_hw_params(substream, params);
1983        if (ret < 0)
1984                dpcm_be_dai_hw_free(fe, stream);
1985        else
1986                fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1987
1988out:
1989        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1990        mutex_unlock(&fe->card->mutex);
1991
1992        if (ret < 0)
1993                dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
1994
1995        return ret;
1996}
1997
1998int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1999                               int cmd)
2000{
2001        struct snd_soc_pcm_runtime *be;
2002        struct snd_soc_dpcm *dpcm;
2003        int ret = 0;
2004        unsigned long flags;
2005        enum snd_soc_dpcm_state state;
2006
2007        for_each_dpcm_be(fe, stream, dpcm) {
2008                struct snd_pcm_substream *be_substream;
2009
2010                be = dpcm->be;
2011                be_substream = snd_soc_dpcm_get_substream(be, stream);
2012
2013                /* is this op for this BE ? */
2014                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2015                        continue;
2016
2017                dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2018                        be->dai_link->name, cmd);
2019
2020                switch (cmd) {
2021                case SNDRV_PCM_TRIGGER_START:
2022                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2023                        if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2024                            (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2025                            (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) {
2026                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2027                                continue;
2028                        }
2029                        state = be->dpcm[stream].state;
2030                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2031                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2032
2033                        ret = soc_pcm_trigger(be_substream, cmd);
2034                        if (ret) {
2035                                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2036                                be->dpcm[stream].state = state;
2037                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2038                                goto end;
2039                        }
2040
2041                        break;
2042                case SNDRV_PCM_TRIGGER_RESUME:
2043                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2044                        if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) {
2045                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2046                                continue;
2047                        }
2048
2049                        state = be->dpcm[stream].state;
2050                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2051                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2052
2053                        ret = soc_pcm_trigger(be_substream, cmd);
2054                        if (ret) {
2055                                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2056                                be->dpcm[stream].state = state;
2057                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2058                                goto end;
2059                        }
2060
2061                        break;
2062                case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2063                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2064                        if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) {
2065                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2066                                continue;
2067                        }
2068
2069                        state = be->dpcm[stream].state;
2070                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2071                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2072
2073                        ret = soc_pcm_trigger(be_substream, cmd);
2074                        if (ret) {
2075                                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2076                                be->dpcm[stream].state = state;
2077                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2078                                goto end;
2079                        }
2080
2081                        break;
2082                case SNDRV_PCM_TRIGGER_STOP:
2083                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2084                        if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2085                            (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED)) {
2086                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2087                                continue;
2088                        }
2089                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2090
2091                        if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2092                                continue;
2093
2094                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2095                        state = be->dpcm[stream].state;
2096                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2097                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2098
2099                        ret = soc_pcm_trigger(be_substream, cmd);
2100                        if (ret) {
2101                                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2102                                be->dpcm[stream].state = state;
2103                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2104                                goto end;
2105                        }
2106
2107                        break;
2108                case SNDRV_PCM_TRIGGER_SUSPEND:
2109                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2110                        if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) {
2111                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2112                                continue;
2113                        }
2114                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2115
2116                        if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2117                                continue;
2118
2119                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2120                        state = be->dpcm[stream].state;
2121                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2122                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2123
2124                        ret = soc_pcm_trigger(be_substream, cmd);
2125                        if (ret) {
2126                                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2127                                be->dpcm[stream].state = state;
2128                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2129                                goto end;
2130                        }
2131
2132                        break;
2133                case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2134                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2135                        if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) {
2136                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2137                                continue;
2138                        }
2139                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2140
2141                        if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2142                                continue;
2143
2144                        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2145                        state = be->dpcm[stream].state;
2146                        be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2147                        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2148
2149                        ret = soc_pcm_trigger(be_substream, cmd);
2150                        if (ret) {
2151                                spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2152                                be->dpcm[stream].state = state;
2153                                spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2154                                goto end;
2155                        }
2156
2157                        break;
2158                }
2159        }
2160end:
2161        if (ret < 0)
2162                dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2163                        __func__, be->dai_link->name, ret);
2164        return ret;
2165}
2166EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2167
2168static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2169                                  int cmd, bool fe_first)
2170{
2171        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2172        int ret;
2173
2174        /* call trigger on the frontend before the backend. */
2175        if (fe_first) {
2176                dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2177                        fe->dai_link->name, cmd);
2178
2179                ret = soc_pcm_trigger(substream, cmd);
2180                if (ret < 0)
2181                        return ret;
2182
2183                ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2184                return ret;
2185        }
2186
2187        /* call trigger on the frontend after the backend. */
2188        ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2189        if (ret < 0)
2190                return ret;
2191
2192        dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2193                fe->dai_link->name, cmd);
2194
2195        ret = soc_pcm_trigger(substream, cmd);
2196
2197        return ret;
2198}
2199
2200static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2201{
2202        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2203        int stream = substream->stream;
2204        int ret = 0;
2205        enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2206
2207        fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2208
2209        switch (trigger) {
2210        case SND_SOC_DPCM_TRIGGER_PRE:
2211                switch (cmd) {
2212                case SNDRV_PCM_TRIGGER_START:
2213                case SNDRV_PCM_TRIGGER_RESUME:
2214                case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2215                case SNDRV_PCM_TRIGGER_DRAIN:
2216                        ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2217                        break;
2218                case SNDRV_PCM_TRIGGER_STOP:
2219                case SNDRV_PCM_TRIGGER_SUSPEND:
2220                case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2221                        ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2222                        break;
2223                default:
2224                        ret = -EINVAL;
2225                        break;
2226                }
2227                break;
2228        case SND_SOC_DPCM_TRIGGER_POST:
2229                switch (cmd) {
2230                case SNDRV_PCM_TRIGGER_START:
2231                case SNDRV_PCM_TRIGGER_RESUME:
2232                case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2233                case SNDRV_PCM_TRIGGER_DRAIN:
2234                        ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2235                        break;
2236                case SNDRV_PCM_TRIGGER_STOP:
2237                case SNDRV_PCM_TRIGGER_SUSPEND:
2238                case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2239                        ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2240                        break;
2241                default:
2242                        ret = -EINVAL;
2243                        break;
2244                }
2245                break;
2246        case SND_SOC_DPCM_TRIGGER_BESPOKE:
2247                /* bespoke trigger() - handles both FE and BEs */
2248
2249                dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2250                                fe->dai_link->name, cmd);
2251
2252                ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2253                break;
2254        default:
2255                dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2256                                fe->dai_link->name);
2257                ret = -EINVAL;
2258                goto out;
2259        }
2260
2261        if (ret < 0) {
2262                dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2263                        cmd, ret);
2264                goto out;
2265        }
2266
2267        switch (cmd) {
2268        case SNDRV_PCM_TRIGGER_START:
2269        case SNDRV_PCM_TRIGGER_RESUME:
2270        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2271                fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2272                break;
2273        case SNDRV_PCM_TRIGGER_STOP:
2274        case SNDRV_PCM_TRIGGER_SUSPEND:
2275                fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2276                break;
2277        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2278                fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2279                break;
2280        }
2281
2282out:
2283        fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2284        return ret;
2285}
2286
2287static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2288{
2289        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2290        int stream = substream->stream;
2291
2292        /* if FE's runtime_update is already set, we're in race;
2293         * process this trigger later at exit
2294         */
2295        if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2296                fe->dpcm[stream].trigger_pending = cmd + 1;
2297                return 0; /* delayed, assuming it's successful */
2298        }
2299
2300        /* we're alone, let's trigger */
2301        return dpcm_fe_dai_do_trigger(substream, cmd);
2302}
2303
2304int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2305{
2306        struct snd_soc_dpcm *dpcm;
2307        int ret = 0;
2308
2309        for_each_dpcm_be(fe, stream, dpcm) {
2310
2311                struct snd_soc_pcm_runtime *be = dpcm->be;
2312                struct snd_pcm_substream *be_substream =
2313                        snd_soc_dpcm_get_substream(be, stream);
2314
2315                /* is this op for this BE ? */
2316                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2317                        continue;
2318
2319                if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2320                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2321                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2322                    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2323                        continue;
2324
2325                dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2326                        be->dai_link->name);
2327
2328                ret = soc_pcm_prepare(be_substream);
2329                if (ret < 0)
2330                        break;
2331
2332                be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2333        }
2334
2335        if (ret < 0)
2336                dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2337
2338        return ret;
2339}
2340
2341static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2342{
2343        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2344        int stream = substream->stream, ret = 0;
2345
2346        mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2347
2348        dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2349
2350        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2351
2352        /* there is no point preparing this FE if there are no BEs */
2353        if (list_empty(&fe->dpcm[stream].be_clients)) {
2354                dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2355                                fe->dai_link->name);
2356                ret = -EINVAL;
2357                goto out;
2358        }
2359
2360        ret = dpcm_be_dai_prepare(fe, stream);
2361        if (ret < 0)
2362                goto out;
2363
2364        /* call prepare on the frontend */
2365        ret = soc_pcm_prepare(substream);
2366        if (ret < 0)
2367                goto out;
2368
2369        fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2370
2371out:
2372        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2373        mutex_unlock(&fe->card->mutex);
2374
2375        if (ret < 0)
2376                dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2377
2378        return ret;
2379}
2380
2381static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2382{
2383        struct snd_pcm_substream *substream =
2384                snd_soc_dpcm_get_substream(fe, stream);
2385        enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2386        int err;
2387
2388        dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2389                        stream ? "capture" : "playback", fe->dai_link->name);
2390
2391        if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2392                /* call bespoke trigger - FE takes care of all BE triggers */
2393                dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2394                                fe->dai_link->name);
2395
2396                err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2397        } else {
2398                dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2399                        fe->dai_link->name);
2400
2401                err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2402        }
2403
2404        dpcm_be_dai_hw_free(fe, stream);
2405
2406        dpcm_be_dai_shutdown(fe, stream);
2407
2408        /* run the stream event for each BE */
2409        dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2410
2411        if (err < 0)
2412                dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
2413
2414        return err;
2415}
2416
2417static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2418{
2419        struct snd_pcm_substream *substream =
2420                snd_soc_dpcm_get_substream(fe, stream);
2421        struct snd_soc_dpcm *dpcm;
2422        enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2423        int ret = 0;
2424        unsigned long flags;
2425
2426        dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2427                        stream ? "capture" : "playback", fe->dai_link->name);
2428
2429        /* Only start the BE if the FE is ready */
2430        if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2431                fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2432                dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2433                        fe->dai_link->name, fe->dpcm[stream].state);
2434                ret = -EINVAL;
2435                goto disconnect;
2436        }
2437
2438        /* startup must always be called for new BEs */
2439        ret = dpcm_be_dai_startup(fe, stream);
2440        if (ret < 0)
2441                goto disconnect;
2442
2443        /* keep going if FE state is > open */
2444        if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2445                return 0;
2446
2447        ret = dpcm_be_dai_hw_params(fe, stream);
2448        if (ret < 0)
2449                goto close;
2450
2451        /* keep going if FE state is > hw_params */
2452        if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2453                return 0;
2454
2455        ret = dpcm_be_dai_prepare(fe, stream);
2456        if (ret < 0)
2457                goto hw_free;
2458
2459        /* run the stream event for each BE */
2460        dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2461
2462        /* keep going if FE state is > prepare */
2463        if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2464                fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2465                return 0;
2466
2467        if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2468                /* call trigger on the frontend - FE takes care of all BE triggers */
2469                dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2470                                fe->dai_link->name);
2471
2472                ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2473                if (ret < 0)
2474                        goto hw_free;
2475        } else {
2476                dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2477                        fe->dai_link->name);
2478
2479                ret = dpcm_be_dai_trigger(fe, stream,
2480                                        SNDRV_PCM_TRIGGER_START);
2481                if (ret < 0)
2482                        goto hw_free;
2483        }
2484
2485        return 0;
2486
2487hw_free:
2488        dpcm_be_dai_hw_free(fe, stream);
2489close:
2490        dpcm_be_dai_shutdown(fe, stream);
2491disconnect:
2492        /* disconnect any pending BEs */
2493        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2494        for_each_dpcm_be(fe, stream, dpcm) {
2495                struct snd_soc_pcm_runtime *be = dpcm->be;
2496
2497                /* is this op for this BE ? */
2498                if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2499                        continue;
2500
2501                if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2502                        be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2503                                dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2504        }
2505        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2506
2507        if (ret < 0)
2508                dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2509
2510        return ret;
2511}
2512
2513static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2514{
2515        struct snd_soc_dapm_widget_list *list;
2516        int stream;
2517        int count, paths;
2518
2519        if (!fe->dai_link->dynamic)
2520                return 0;
2521
2522        if (fe->num_cpus > 1) {
2523                dev_err(fe->dev,
2524                        "%s doesn't support Multi CPU yet\n", __func__);
2525                return -EINVAL;
2526        }
2527
2528        /* only check active links */
2529        if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2530                return 0;
2531
2532        /* DAPM sync will call this to update DSP paths */
2533        dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2534                new ? "new" : "old", fe->dai_link->name);
2535
2536        for_each_pcm_streams(stream) {
2537
2538                /* skip if FE doesn't have playback/capture capability */
2539                if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0),   stream) ||
2540                    !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2541                        continue;
2542
2543                /* skip if FE isn't currently playing/capturing */
2544                if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2545                    !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2546                        continue;
2547
2548                paths = dpcm_path_get(fe, stream, &list);
2549                if (paths < 0)
2550                        return paths;
2551
2552                /* update any playback/capture paths */
2553                count = dpcm_process_paths(fe, stream, &list, new);
2554                if (count) {
2555                        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2556                        if (new)
2557                                dpcm_run_update_startup(fe, stream);
2558                        else
2559                                dpcm_run_update_shutdown(fe, stream);
2560                        dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2561
2562                        dpcm_clear_pending_state(fe, stream);
2563                        dpcm_be_disconnect(fe, stream);
2564                }
2565
2566                dpcm_path_put(&list);
2567        }
2568
2569        return 0;
2570}
2571
2572/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2573 * any DAI links.
2574 */
2575int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2576{
2577        struct snd_soc_pcm_runtime *fe;
2578        int ret = 0;
2579
2580        mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2581        /* shutdown all old paths first */
2582        for_each_card_rtds(card, fe) {
2583                ret = soc_dpcm_fe_runtime_update(fe, 0);
2584                if (ret)
2585                        goto out;
2586        }
2587
2588        /* bring new paths up */
2589        for_each_card_rtds(card, fe) {
2590                ret = soc_dpcm_fe_runtime_update(fe, 1);
2591                if (ret)
2592                        goto out;
2593        }
2594
2595out:
2596        mutex_unlock(&card->mutex);
2597        return ret;
2598}
2599EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2600
2601static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2602{
2603        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2604        struct snd_soc_dpcm *dpcm;
2605        int stream = fe_substream->stream;
2606
2607        /* mark FE's links ready to prune */
2608        for_each_dpcm_be(fe, stream, dpcm)
2609                dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2610
2611        dpcm_be_disconnect(fe, stream);
2612
2613        fe->dpcm[stream].runtime = NULL;
2614}
2615
2616static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2617{
2618        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2619        int ret;
2620
2621        mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2622        ret = dpcm_fe_dai_shutdown(fe_substream);
2623
2624        dpcm_fe_dai_cleanup(fe_substream);
2625
2626        mutex_unlock(&fe->card->mutex);
2627        return ret;
2628}
2629
2630static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2631{
2632        struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2633        struct snd_soc_dapm_widget_list *list;
2634        int ret;
2635        int stream = fe_substream->stream;
2636
2637        mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2638        fe->dpcm[stream].runtime = fe_substream->runtime;
2639
2640        ret = dpcm_path_get(fe, stream, &list);
2641        if (ret < 0)
2642                goto open_end;
2643
2644        /* calculate valid and active FE <-> BE dpcms */
2645        dpcm_process_paths(fe, stream, &list, 1);
2646
2647        ret = dpcm_fe_dai_startup(fe_substream);
2648        if (ret < 0)
2649                dpcm_fe_dai_cleanup(fe_substream);
2650
2651        dpcm_clear_pending_state(fe, stream);
2652        dpcm_path_put(&list);
2653open_end:
2654        mutex_unlock(&fe->card->mutex);
2655        return ret;
2656}
2657
2658static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2659                                    int *playback, int *capture)
2660{
2661        struct snd_soc_dai *codec_dai;
2662        struct snd_soc_dai *cpu_dai;
2663        int stream;
2664        int i;
2665
2666        if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2667                dev_err(rtd->dev,
2668                        "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2669                return -EINVAL;
2670        }
2671
2672        if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2673                if (rtd->dai_link->dpcm_playback) {
2674                        stream = SNDRV_PCM_STREAM_PLAYBACK;
2675
2676                        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2677                                if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2678                                        *playback = 1;
2679                                        break;
2680                                }
2681                        }
2682                        if (!*playback) {
2683                                dev_err(rtd->card->dev,
2684                                        "No CPU DAIs support playback for stream %s\n",
2685                                        rtd->dai_link->stream_name);
2686                                return -EINVAL;
2687                        }
2688                }
2689                if (rtd->dai_link->dpcm_capture) {
2690                        stream = SNDRV_PCM_STREAM_CAPTURE;
2691
2692                        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2693                                if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2694                                        *capture = 1;
2695                                        break;
2696                                }
2697                        }
2698
2699                        if (!*capture) {
2700                                dev_err(rtd->card->dev,
2701                                        "No CPU DAIs support capture for stream %s\n",
2702                                        rtd->dai_link->stream_name);
2703                                return -EINVAL;
2704                        }
2705                }
2706        } else {
2707                /* Adapt stream for codec2codec links */
2708                int cpu_capture = rtd->dai_link->params ?
2709                        SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2710                int cpu_playback = rtd->dai_link->params ?
2711                        SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2712
2713                for_each_rtd_codec_dais(rtd, i, codec_dai) {
2714                        if (rtd->num_cpus == 1) {
2715                                cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2716                        } else if (rtd->num_cpus == rtd->num_codecs) {
2717                                cpu_dai = asoc_rtd_to_cpu(rtd, i);
2718                        } else {
2719                                dev_err(rtd->card->dev,
2720                                        "N cpus to M codecs link is not supported yet\n");
2721                                return -EINVAL;
2722                        }
2723
2724                        if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2725                            snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
2726                                *playback = 1;
2727                        if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2728                            snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
2729                                *capture = 1;
2730                }
2731        }
2732
2733        if (rtd->dai_link->playback_only) {
2734                *playback = 1;
2735                *capture = 0;
2736        }
2737
2738        if (rtd->dai_link->capture_only) {
2739                *playback = 0;
2740                *capture = 1;
2741        }
2742
2743        return 0;
2744}
2745
2746static int soc_create_pcm(struct snd_pcm **pcm,
2747                          struct snd_soc_pcm_runtime *rtd,
2748                          int playback, int capture, int num)
2749{
2750        char new_name[64];
2751        int ret;
2752
2753        /* create the PCM */
2754        if (rtd->dai_link->params) {
2755                snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2756                         rtd->dai_link->stream_name);
2757
2758                ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2759                                           playback, capture, pcm);
2760        } else if (rtd->dai_link->no_pcm) {
2761                snprintf(new_name, sizeof(new_name), "(%s)",
2762                        rtd->dai_link->stream_name);
2763
2764                ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2765                                playback, capture, pcm);
2766        } else {
2767                if (rtd->dai_link->dynamic)
2768                        snprintf(new_name, sizeof(new_name), "%s (*)",
2769                                rtd->dai_link->stream_name);
2770                else
2771                        snprintf(new_name, sizeof(new_name), "%s %s-%d",
2772                                rtd->dai_link->stream_name,
2773                                soc_codec_dai_name(rtd), num);
2774
2775                ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2776                        capture, pcm);
2777        }
2778        if (ret < 0) {
2779                dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2780                        new_name, rtd->dai_link->name, ret);
2781                return ret;
2782        }
2783        dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2784
2785        return 0;
2786}
2787
2788/* create a new pcm */
2789int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2790{
2791        struct snd_soc_component *component;
2792        struct snd_pcm *pcm;
2793        int ret = 0, playback = 0, capture = 0;
2794        int i;
2795
2796        ret = soc_get_playback_capture(rtd, &playback, &capture);
2797        if (ret < 0)
2798                return ret;
2799
2800        ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2801        if (ret < 0)
2802                return ret;
2803
2804        /* DAPM dai link stream work */
2805        if (rtd->dai_link->params)
2806                rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2807        else
2808                rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2809
2810        rtd->pcm = pcm;
2811        pcm->nonatomic = rtd->dai_link->nonatomic;
2812        pcm->private_data = rtd;
2813
2814        if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2815                if (playback)
2816                        pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2817                if (capture)
2818                        pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2819                goto out;
2820        }
2821
2822        /* ASoC PCM operations */
2823        if (rtd->dai_link->dynamic) {
2824                rtd->ops.open           = dpcm_fe_dai_open;
2825                rtd->ops.hw_params      = dpcm_fe_dai_hw_params;
2826                rtd->ops.prepare        = dpcm_fe_dai_prepare;
2827                rtd->ops.trigger        = dpcm_fe_dai_trigger;
2828                rtd->ops.hw_free        = dpcm_fe_dai_hw_free;
2829                rtd->ops.close          = dpcm_fe_dai_close;
2830                rtd->ops.pointer        = soc_pcm_pointer;
2831        } else {
2832                rtd->ops.open           = soc_pcm_open;
2833                rtd->ops.hw_params      = soc_pcm_hw_params;
2834                rtd->ops.prepare        = soc_pcm_prepare;
2835                rtd->ops.trigger        = soc_pcm_trigger;
2836                rtd->ops.hw_free        = soc_pcm_hw_free;
2837                rtd->ops.close          = soc_pcm_close;
2838                rtd->ops.pointer        = soc_pcm_pointer;
2839        }
2840
2841        for_each_rtd_components(rtd, i, component) {
2842                const struct snd_soc_component_driver *drv = component->driver;
2843
2844                if (drv->ioctl)
2845                        rtd->ops.ioctl          = snd_soc_pcm_component_ioctl;
2846                if (drv->sync_stop)
2847                        rtd->ops.sync_stop      = snd_soc_pcm_component_sync_stop;
2848                if (drv->copy_user)
2849                        rtd->ops.copy_user      = snd_soc_pcm_component_copy_user;
2850                if (drv->page)
2851                        rtd->ops.page           = snd_soc_pcm_component_page;
2852                if (drv->mmap)
2853                        rtd->ops.mmap           = snd_soc_pcm_component_mmap;
2854                if (drv->ack)
2855                        rtd->ops.ack            = snd_soc_pcm_component_ack;
2856        }
2857
2858        if (playback)
2859                snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2860
2861        if (capture)
2862                snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2863
2864        ret = snd_soc_pcm_component_new(rtd);
2865        if (ret < 0)
2866                return ret;
2867
2868        pcm->no_device_suspend = true;
2869out:
2870        dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2871                soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2872        return ret;
2873}
2874
2875/* is the current PCM operation for this FE ? */
2876int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2877{
2878        if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2879                return 1;
2880        return 0;
2881}
2882EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2883
2884/* is the current PCM operation for this BE ? */
2885int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2886                struct snd_soc_pcm_runtime *be, int stream)
2887{
2888        if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2889           ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2890                  be->dpcm[stream].runtime_update))
2891                return 1;
2892        return 0;
2893}
2894EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2895
2896/* get the substream for this BE */
2897struct snd_pcm_substream *
2898        snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2899{
2900        return be->pcm->streams[stream].substream;
2901}
2902EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2903
2904static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2905                                    struct snd_soc_pcm_runtime *be,
2906                                    int stream,
2907                                    const enum snd_soc_dpcm_state *states,
2908                                    int num_states)
2909{
2910        struct snd_soc_dpcm *dpcm;
2911        int state;
2912        int ret = 1;
2913        unsigned long flags;
2914        int i;
2915
2916        spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2917        for_each_dpcm_fe(be, stream, dpcm) {
2918
2919                if (dpcm->fe == fe)
2920                        continue;
2921
2922                state = dpcm->fe->dpcm[stream].state;
2923                for (i = 0; i < num_states; i++) {
2924                        if (state == states[i]) {
2925                                ret = 0;
2926                                break;
2927                        }
2928                }
2929        }
2930        spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2931
2932        /* it's safe to do this BE DAI */
2933        return ret;
2934}
2935
2936/*
2937 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2938 * are not running, paused or suspended for the specified stream direction.
2939 */
2940int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2941                struct snd_soc_pcm_runtime *be, int stream)
2942{
2943        const enum snd_soc_dpcm_state state[] = {
2944                SND_SOC_DPCM_STATE_START,
2945                SND_SOC_DPCM_STATE_PAUSED,
2946                SND_SOC_DPCM_STATE_SUSPEND,
2947        };
2948
2949        return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2950}
2951EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2952
2953/*
2954 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2955 * running, paused or suspended for the specified stream direction.
2956 */
2957int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2958                struct snd_soc_pcm_runtime *be, int stream)
2959{
2960        const enum snd_soc_dpcm_state state[] = {
2961                SND_SOC_DPCM_STATE_START,
2962                SND_SOC_DPCM_STATE_PAUSED,
2963                SND_SOC_DPCM_STATE_SUSPEND,
2964                SND_SOC_DPCM_STATE_PREPARE,
2965        };
2966
2967        return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2968}
2969EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2970