linux/sound/soc/soc-core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2//
   3// soc-core.c  --  ALSA SoC Audio Layer
   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// Author: Liam Girdwood <lrg@slimlogic.co.uk>
  11//         with code, comments and ideas from :-
  12//         Richard Purdie <richard@openedhand.com>
  13//
  14//  TODO:
  15//   o Add hw rules to enforce rates, etc.
  16//   o More testing with other codecs/machines.
  17//   o Add more codecs and platforms to ensure good API coverage.
  18//   o Support TDM on PCM and I2S
  19
  20#include <linux/module.h>
  21#include <linux/moduleparam.h>
  22#include <linux/init.h>
  23#include <linux/delay.h>
  24#include <linux/pm.h>
  25#include <linux/bitops.h>
  26#include <linux/debugfs.h>
  27#include <linux/platform_device.h>
  28#include <linux/pinctrl/consumer.h>
  29#include <linux/ctype.h>
  30#include <linux/slab.h>
  31#include <linux/of.h>
  32#include <linux/of_graph.h>
  33#include <linux/dmi.h>
  34#include <linux/acpi.h>
  35#include <linux/string_choices.h>
  36#include <sound/core.h>
  37#include <sound/pcm.h>
  38#include <sound/pcm_params.h>
  39#include <sound/soc.h>
  40#include <sound/soc-dpcm.h>
  41#include <sound/soc-topology.h>
  42#include <sound/soc-link.h>
  43#include <sound/initval.h>
  44
  45#define CREATE_TRACE_POINTS
  46#include <trace/events/asoc.h>
  47
  48static DEFINE_MUTEX(client_mutex);
  49static LIST_HEAD(component_list);
  50static LIST_HEAD(unbind_card_list);
  51
  52#define for_each_component(component)                   \
  53        list_for_each_entry(component, &component_list, list)
  54
  55/*
  56 * This is used if driver don't need to have CPU/Codec/Platform
  57 * dai_link. see soc.h
  58 */
  59struct snd_soc_dai_link_component null_dailink_component[0];
  60EXPORT_SYMBOL_GPL(null_dailink_component);
  61
  62/*
  63 * This is a timeout to do a DAPM powerdown after a stream is closed().
  64 * It can be used to eliminate pops between different playback streams, e.g.
  65 * between two audio tracks.
  66 */
  67static int pmdown_time = 5000;
  68module_param(pmdown_time, int, 0);
  69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  70
  71static ssize_t pmdown_time_show(struct device *dev,
  72                                struct device_attribute *attr, char *buf)
  73{
  74        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  75
  76        return sysfs_emit(buf, "%ld\n", rtd->pmdown_time);
  77}
  78
  79static ssize_t pmdown_time_store(struct device *dev,
  80                                 struct device_attribute *attr,
  81                                 const char *buf, size_t count)
  82{
  83        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  84        int ret;
  85
  86        ret = kstrtol(buf, 10, &rtd->pmdown_time);
  87        if (ret)
  88                return ret;
  89
  90        return count;
  91}
  92
  93static DEVICE_ATTR_RW(pmdown_time);
  94
  95static struct attribute *soc_dev_attrs[] = {
  96        &dev_attr_pmdown_time.attr,
  97        NULL
  98};
  99
 100static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
 101                                       struct attribute *attr, int idx)
 102{
 103        struct device *dev = kobj_to_dev(kobj);
 104        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
 105
 106        if (!rtd)
 107                return 0;
 108
 109        if (attr == &dev_attr_pmdown_time.attr)
 110                return attr->mode; /* always visible */
 111        return rtd->dai_link->num_codecs ? attr->mode : 0; /* enabled only with codec */
 112}
 113
 114static const struct attribute_group soc_dapm_dev_group = {
 115        .attrs = snd_soc_dapm_dev_attrs,
 116        .is_visible = soc_dev_attr_is_visible,
 117};
 118
 119static const struct attribute_group soc_dev_group = {
 120        .attrs = soc_dev_attrs,
 121        .is_visible = soc_dev_attr_is_visible,
 122};
 123
 124static const struct attribute_group *soc_dev_attr_groups[] = {
 125        &soc_dapm_dev_group,
 126        &soc_dev_group,
 127        NULL
 128};
 129
 130#ifdef CONFIG_DEBUG_FS
 131struct dentry *snd_soc_debugfs_root;
 132EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
 133
 134static void soc_init_component_debugfs(struct snd_soc_component *component)
 135{
 136        if (!component->card->debugfs_card_root)
 137                return;
 138
 139        if (component->debugfs_prefix) {
 140                char *name;
 141
 142                name = kasprintf(GFP_KERNEL, "%s:%s",
 143                        component->debugfs_prefix, component->name);
 144                if (name) {
 145                        component->debugfs_root = debugfs_create_dir(name,
 146                                component->card->debugfs_card_root);
 147                        kfree(name);
 148                }
 149        } else {
 150                component->debugfs_root = debugfs_create_dir(component->name,
 151                                component->card->debugfs_card_root);
 152        }
 153
 154        snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
 155                component->debugfs_root);
 156}
 157
 158static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
 159{
 160        if (!component->debugfs_root)
 161                return;
 162        debugfs_remove_recursive(component->debugfs_root);
 163        component->debugfs_root = NULL;
 164}
 165
 166static int dai_list_show(struct seq_file *m, void *v)
 167{
 168        struct snd_soc_component *component;
 169        struct snd_soc_dai *dai;
 170
 171        mutex_lock(&client_mutex);
 172
 173        for_each_component(component)
 174                for_each_component_dais(component, dai)
 175                        seq_printf(m, "%s\n", dai->name);
 176
 177        mutex_unlock(&client_mutex);
 178
 179        return 0;
 180}
 181DEFINE_SHOW_ATTRIBUTE(dai_list);
 182
 183static int component_list_show(struct seq_file *m, void *v)
 184{
 185        struct snd_soc_component *component;
 186
 187        mutex_lock(&client_mutex);
 188
 189        for_each_component(component)
 190                seq_printf(m, "%s\n", component->name);
 191
 192        mutex_unlock(&client_mutex);
 193
 194        return 0;
 195}
 196DEFINE_SHOW_ATTRIBUTE(component_list);
 197
 198static void soc_init_card_debugfs(struct snd_soc_card *card)
 199{
 200        card->debugfs_card_root = debugfs_create_dir(card->name,
 201                                                     snd_soc_debugfs_root);
 202
 203        debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
 204                           &card->pop_time);
 205
 206        snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
 207}
 208
 209static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
 210{
 211        debugfs_remove_recursive(card->debugfs_card_root);
 212        card->debugfs_card_root = NULL;
 213}
 214
 215static void snd_soc_debugfs_init(void)
 216{
 217        snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
 218
 219        debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
 220                            &dai_list_fops);
 221
 222        debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
 223                            &component_list_fops);
 224}
 225
 226static void snd_soc_debugfs_exit(void)
 227{
 228        debugfs_remove_recursive(snd_soc_debugfs_root);
 229}
 230
 231#else
 232
 233static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
 234static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
 235static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
 236static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
 237static inline void snd_soc_debugfs_init(void) { }
 238static inline void snd_soc_debugfs_exit(void) { }
 239
 240#endif
 241
 242static int snd_soc_is_match_dai_args(const struct of_phandle_args *args1,
 243                                     const struct of_phandle_args *args2)
 244{
 245        if (!args1 || !args2)
 246                return 0;
 247
 248        if (args1->np != args2->np)
 249                return 0;
 250
 251        for (int i = 0; i < args1->args_count; i++)
 252                if (args1->args[i] != args2->args[i])
 253                        return 0;
 254
 255        return 1;
 256}
 257
 258static inline int snd_soc_dlc_component_is_empty(struct snd_soc_dai_link_component *dlc)
 259{
 260        return !(dlc->dai_args || dlc->name || dlc->of_node);
 261}
 262
 263static inline int snd_soc_dlc_component_is_invalid(struct snd_soc_dai_link_component *dlc)
 264{
 265        return (dlc->name && dlc->of_node);
 266}
 267
 268static inline int snd_soc_dlc_dai_is_empty(struct snd_soc_dai_link_component *dlc)
 269{
 270        return !(dlc->dai_args || dlc->dai_name);
 271}
 272
 273static int snd_soc_is_matching_dai(const struct snd_soc_dai_link_component *dlc,
 274                                   struct snd_soc_dai *dai)
 275{
 276        if (!dlc)
 277                return 0;
 278
 279        if (dlc->dai_args)
 280                return snd_soc_is_match_dai_args(dai->driver->dai_args, dlc->dai_args);
 281
 282        if (!dlc->dai_name)
 283                return 1;
 284
 285        /* see snd_soc_dai_name_get() */
 286
 287        if (dai->driver->name &&
 288            strcmp(dlc->dai_name, dai->driver->name) == 0)
 289                return 1;
 290
 291        if (strcmp(dlc->dai_name, dai->name) == 0)
 292                return 1;
 293
 294        if (dai->component->name &&
 295            strcmp(dlc->dai_name, dai->component->name) == 0)
 296                return 1;
 297
 298        return 0;
 299}
 300
 301const char *snd_soc_dai_name_get(const struct snd_soc_dai *dai)
 302{
 303        /* see snd_soc_is_matching_dai() */
 304        if (dai->driver->name)
 305                return dai->driver->name;
 306
 307        if (dai->name)
 308                return dai->name;
 309
 310        if (dai->component->name)
 311                return dai->component->name;
 312
 313        return NULL;
 314}
 315EXPORT_SYMBOL_GPL(snd_soc_dai_name_get);
 316
 317static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
 318                                     struct snd_soc_component *component)
 319{
 320        struct snd_soc_component *comp;
 321        int i;
 322
 323        for_each_rtd_components(rtd, i, comp) {
 324                /* already connected */
 325                if (comp == component)
 326                        return 0;
 327        }
 328
 329        /* see for_each_rtd_components */
 330        rtd->num_components++; // increment flex array count at first
 331        rtd->components[rtd->num_components - 1] = component;
 332
 333        return 0;
 334}
 335
 336struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
 337                                                const char *driver_name)
 338{
 339        struct snd_soc_component *component;
 340        int i;
 341
 342        if (!driver_name)
 343                return NULL;
 344
 345        /*
 346         * NOTE
 347         *
 348         * snd_soc_rtdcom_lookup() will find component from rtd by using
 349         * specified driver name.
 350         * But, if many components which have same driver name are connected
 351         * to 1 rtd, this function will return 1st found component.
 352         */
 353        for_each_rtd_components(rtd, i, component) {
 354                const char *component_name = component->driver->name;
 355
 356                if (!component_name)
 357                        continue;
 358
 359                if ((component_name == driver_name) ||
 360                    strcmp(component_name, driver_name) == 0)
 361                        return component;
 362        }
 363
 364        return NULL;
 365}
 366EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
 367
 368struct snd_soc_component
 369*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
 370{
 371        struct snd_soc_component *component;
 372
 373        for_each_component(component) {
 374                if (dev != component->dev)
 375                        continue;
 376
 377                if (!driver_name)
 378                        return component;
 379
 380                if (!component->driver->name)
 381                        continue;
 382
 383                if (component->driver->name == driver_name)
 384                        return component;
 385
 386                if (strcmp(component->driver->name, driver_name) == 0)
 387                        return component;
 388        }
 389
 390        return NULL;
 391}
 392EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
 393
 394struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
 395                                                   const char *driver_name)
 396{
 397        struct snd_soc_component *component;
 398
 399        mutex_lock(&client_mutex);
 400        component = snd_soc_lookup_component_nolocked(dev, driver_name);
 401        mutex_unlock(&client_mutex);
 402
 403        return component;
 404}
 405EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
 406
 407struct snd_soc_pcm_runtime
 408*snd_soc_get_pcm_runtime(struct snd_soc_card *card,
 409                         struct snd_soc_dai_link *dai_link)
 410{
 411        struct snd_soc_pcm_runtime *rtd;
 412
 413        for_each_card_rtds(card, rtd) {
 414                if (rtd->dai_link == dai_link)
 415                        return rtd;
 416        }
 417        dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
 418        return NULL;
 419}
 420EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
 421
 422/*
 423 * Power down the audio subsystem pmdown_time msecs after close is called.
 424 * This is to ensure there are no pops or clicks in between any music tracks
 425 * due to DAPM power cycling.
 426 */
 427void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
 428{
 429        struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
 430        int playback = SNDRV_PCM_STREAM_PLAYBACK;
 431
 432        snd_soc_dpcm_mutex_lock(rtd);
 433
 434        dev_dbg(rtd->dev,
 435                "ASoC: pop wq checking: %s status: %s waiting: %s\n",
 436                codec_dai->driver->playback.stream_name,
 437                snd_soc_dai_stream_active(codec_dai, playback) ?
 438                "active" : "inactive",
 439                str_yes_no(rtd->pop_wait));
 440
 441        /* are we waiting on this codec DAI stream */
 442        if (rtd->pop_wait == 1) {
 443                rtd->pop_wait = 0;
 444                snd_soc_dapm_stream_event(rtd, playback,
 445                                          SND_SOC_DAPM_STREAM_STOP);
 446        }
 447
 448        snd_soc_dpcm_mutex_unlock(rtd);
 449}
 450EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
 451
 452static void soc_release_rtd_dev(struct device *dev)
 453{
 454        /* "dev" means "rtd->dev" */
 455        kfree(dev);
 456}
 457
 458static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
 459{
 460        if (!rtd)
 461                return;
 462
 463        list_del(&rtd->list);
 464
 465        if (delayed_work_pending(&rtd->delayed_work))
 466                flush_delayed_work(&rtd->delayed_work);
 467        snd_soc_pcm_component_free(rtd);
 468
 469        /*
 470         * we don't need to call kfree() for rtd->dev
 471         * see
 472         *      soc_release_rtd_dev()
 473         *
 474         * We don't need rtd->dev NULL check, because
 475         * it is alloced *before* rtd.
 476         * see
 477         *      soc_new_pcm_runtime()
 478         *
 479         * We don't need to mind freeing for rtd,
 480         * because it was created from dev (= rtd->dev)
 481         * see
 482         *      soc_new_pcm_runtime()
 483         *
 484         *              rtd = devm_kzalloc(dev, ...);
 485         *              rtd->dev = dev
 486         */
 487        device_unregister(rtd->dev);
 488}
 489
 490static void close_delayed_work(struct work_struct *work) {
 491        struct snd_soc_pcm_runtime *rtd =
 492                        container_of(work, struct snd_soc_pcm_runtime,
 493                                     delayed_work.work);
 494
 495        if (rtd->close_delayed_work_func)
 496                rtd->close_delayed_work_func(rtd);
 497}
 498
 499static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
 500        struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
 501{
 502        struct snd_soc_pcm_runtime *rtd;
 503        struct device *dev;
 504        int ret;
 505        int stream;
 506
 507        /*
 508         * for rtd->dev
 509         */
 510        dev = kzalloc(sizeof(struct device), GFP_KERNEL);
 511        if (!dev)
 512                return NULL;
 513
 514        dev->parent     = card->dev;
 515        dev->release    = soc_release_rtd_dev;
 516
 517        dev_set_name(dev, "%s", dai_link->name);
 518
 519        ret = device_register(dev);
 520        if (ret < 0) {
 521                put_device(dev); /* soc_release_rtd_dev */
 522                return NULL;
 523        }
 524
 525        /*
 526         * for rtd
 527         */
 528        rtd = devm_kzalloc(dev,
 529                           struct_size(rtd, components,
 530                                       dai_link->num_cpus +
 531                                       dai_link->num_codecs +
 532                                       dai_link->num_platforms),
 533                           GFP_KERNEL);
 534        if (!rtd) {
 535                device_unregister(dev);
 536                return NULL;
 537        }
 538
 539        rtd->dev = dev;
 540        INIT_LIST_HEAD(&rtd->list);
 541        for_each_pcm_streams(stream) {
 542                INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
 543                INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
 544        }
 545        dev_set_drvdata(dev, rtd);
 546        INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
 547
 548        /*
 549         * for rtd->dais
 550         */
 551        rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
 552                                        sizeof(struct snd_soc_dai *),
 553                                        GFP_KERNEL);
 554        if (!rtd->dais)
 555                goto free_rtd;
 556
 557        /*
 558         * dais = [][][][][][][][][][][][][][][][][][]
 559         *        ^cpu_dais         ^codec_dais
 560         *        |--- num_cpus ---|--- num_codecs --|
 561         * see
 562         *      snd_soc_rtd_to_cpu()
 563         *      snd_soc_rtd_to_codec()
 564         */
 565        rtd->card       = card;
 566        rtd->dai_link   = dai_link;
 567        rtd->id         = card->num_rtd++;
 568        rtd->pmdown_time = pmdown_time;                 /* default power off timeout */
 569
 570        /* see for_each_card_rtds */
 571        list_add_tail(&rtd->list, &card->rtd_list);
 572
 573        ret = device_add_groups(dev, soc_dev_attr_groups);
 574        if (ret < 0)
 575                goto free_rtd;
 576
 577        return rtd;
 578
 579free_rtd:
 580        soc_free_pcm_runtime(rtd);
 581        return NULL;
 582}
 583
 584static void snd_soc_fill_dummy_dai(struct snd_soc_card *card)
 585{
 586        struct snd_soc_dai_link *dai_link;
 587        int i;
 588
 589        /*
 590         * COMP_DUMMY() creates size 0 array on dai_link.
 591         * Fill it as dummy DAI in case of CPU/Codec here.
 592         * Do nothing for Platform.
 593         */
 594        for_each_card_prelinks(card, i, dai_link) {
 595                if (dai_link->num_cpus == 0 && dai_link->cpus) {
 596                        dai_link->num_cpus      = 1;
 597                        dai_link->cpus          = &snd_soc_dummy_dlc;
 598                }
 599                if (dai_link->num_codecs == 0 && dai_link->codecs) {
 600                        dai_link->num_codecs    = 1;
 601                        dai_link->codecs        = &snd_soc_dummy_dlc;
 602                }
 603        }
 604}
 605
 606static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
 607{
 608        struct snd_soc_pcm_runtime *rtd;
 609
 610        for_each_card_rtds(card, rtd)
 611                flush_delayed_work(&rtd->delayed_work);
 612}
 613
 614#ifdef CONFIG_PM_SLEEP
 615static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
 616{
 617        struct snd_soc_pcm_runtime *rtd;
 618        struct snd_soc_dai *dai;
 619        int playback = SNDRV_PCM_STREAM_PLAYBACK;
 620        int i;
 621
 622        for_each_card_rtds(card, rtd) {
 623
 624                if (rtd->dai_link->ignore_suspend)
 625                        continue;
 626
 627                for_each_rtd_dais(rtd, i, dai) {
 628                        if (snd_soc_dai_stream_active(dai, playback))
 629                                snd_soc_dai_digital_mute(dai, mute, playback);
 630                }
 631        }
 632}
 633
 634static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
 635{
 636        struct snd_soc_pcm_runtime *rtd;
 637        int stream;
 638
 639        for_each_card_rtds(card, rtd) {
 640
 641                if (rtd->dai_link->ignore_suspend)
 642                        continue;
 643
 644                for_each_pcm_streams(stream)
 645                        snd_soc_dapm_stream_event(rtd, stream, event);
 646        }
 647}
 648
 649/* powers down audio subsystem for suspend */
 650int snd_soc_suspend(struct device *dev)
 651{
 652        struct snd_soc_card *card = dev_get_drvdata(dev);
 653        struct snd_soc_component *component;
 654        struct snd_soc_pcm_runtime *rtd;
 655        int i;
 656
 657        /* If the card is not initialized yet there is nothing to do */
 658        if (!snd_soc_card_is_instantiated(card))
 659                return 0;
 660
 661        /*
 662         * Due to the resume being scheduled into a workqueue we could
 663         * suspend before that's finished - wait for it to complete.
 664         */
 665        snd_power_wait(card->snd_card);
 666
 667        /* we're going to block userspace touching us until resume completes */
 668        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
 669
 670        /* mute any active DACs */
 671        soc_playback_digital_mute(card, 1);
 672
 673        /* suspend all pcms */
 674        for_each_card_rtds(card, rtd) {
 675                if (rtd->dai_link->ignore_suspend)
 676                        continue;
 677
 678                snd_pcm_suspend_all(rtd->pcm);
 679        }
 680
 681        snd_soc_card_suspend_pre(card);
 682
 683        /* close any waiting streams */
 684        snd_soc_flush_all_delayed_work(card);
 685
 686        soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
 687
 688        /* Recheck all endpoints too, their state is affected by suspend */
 689        snd_soc_dapm_mark_endpoints_dirty(card);
 690        snd_soc_dapm_sync(&card->dapm);
 691
 692        /* suspend all COMPONENTs */
 693        for_each_card_rtds(card, rtd) {
 694
 695                if (rtd->dai_link->ignore_suspend)
 696                        continue;
 697
 698                for_each_rtd_components(rtd, i, component) {
 699                        struct snd_soc_dapm_context *dapm =
 700                                snd_soc_component_get_dapm(component);
 701
 702                        /*
 703                         * ignore if component was already suspended
 704                         */
 705                        if (snd_soc_component_is_suspended(component))
 706                                continue;
 707
 708                        /*
 709                         * If there are paths active then the COMPONENT will be
 710                         * held with bias _ON and should not be suspended.
 711                         */
 712                        switch (snd_soc_dapm_get_bias_level(dapm)) {
 713                        case SND_SOC_BIAS_STANDBY:
 714                                /*
 715                                 * If the COMPONENT is capable of idle
 716                                 * bias off then being in STANDBY
 717                                 * means it's doing something,
 718                                 * otherwise fall through.
 719                                 */
 720                                if (dapm->idle_bias_off) {
 721                                        dev_dbg(component->dev,
 722                                                "ASoC: idle_bias_off CODEC on over suspend\n");
 723                                        break;
 724                                }
 725                                fallthrough;
 726
 727                        case SND_SOC_BIAS_OFF:
 728                                snd_soc_component_suspend(component);
 729                                if (component->regmap)
 730                                        regcache_mark_dirty(component->regmap);
 731                                /* deactivate pins to sleep state */
 732                                pinctrl_pm_select_sleep_state(component->dev);
 733                                break;
 734                        default:
 735                                dev_dbg(component->dev,
 736                                        "ASoC: COMPONENT is on over suspend\n");
 737                                break;
 738                        }
 739                }
 740        }
 741
 742        snd_soc_card_suspend_post(card);
 743
 744        return 0;
 745}
 746EXPORT_SYMBOL_GPL(snd_soc_suspend);
 747
 748/*
 749 * deferred resume work, so resume can complete before we finished
 750 * setting our codec back up, which can be very slow on I2C
 751 */
 752static void soc_resume_deferred(struct work_struct *work)
 753{
 754        struct snd_soc_card *card =
 755                        container_of(work, struct snd_soc_card,
 756                                     deferred_resume_work);
 757        struct snd_soc_component *component;
 758
 759        /*
 760         * our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
 761         * so userspace apps are blocked from touching us
 762         */
 763
 764        dev_dbg(card->dev, "ASoC: starting resume work\n");
 765
 766        /* Bring us up into D2 so that DAPM starts enabling things */
 767        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
 768
 769        snd_soc_card_resume_pre(card);
 770
 771        for_each_card_components(card, component) {
 772                if (snd_soc_component_is_suspended(component))
 773                        snd_soc_component_resume(component);
 774        }
 775
 776        soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
 777
 778        /* unmute any active DACs */
 779        soc_playback_digital_mute(card, 0);
 780
 781        snd_soc_card_resume_post(card);
 782
 783        dev_dbg(card->dev, "ASoC: resume work completed\n");
 784
 785        /* Recheck all endpoints too, their state is affected by suspend */
 786        snd_soc_dapm_mark_endpoints_dirty(card);
 787        snd_soc_dapm_sync(&card->dapm);
 788
 789        /* userspace can access us now we are back as we were before */
 790        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
 791}
 792
 793/* powers up audio subsystem after a suspend */
 794int snd_soc_resume(struct device *dev)
 795{
 796        struct snd_soc_card *card = dev_get_drvdata(dev);
 797        struct snd_soc_component *component;
 798
 799        /* If the card is not initialized yet there is nothing to do */
 800        if (!snd_soc_card_is_instantiated(card))
 801                return 0;
 802
 803        /* activate pins from sleep state */
 804        for_each_card_components(card, component)
 805                if (snd_soc_component_active(component))
 806                        pinctrl_pm_select_default_state(component->dev);
 807
 808        dev_dbg(dev, "ASoC: Scheduling resume work\n");
 809        if (!schedule_work(&card->deferred_resume_work))
 810                dev_err(dev, "ASoC: resume work item may be lost\n");
 811
 812        return 0;
 813}
 814EXPORT_SYMBOL_GPL(snd_soc_resume);
 815
 816static void soc_resume_init(struct snd_soc_card *card)
 817{
 818        /* deferred resume work */
 819        INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
 820}
 821#else
 822#define snd_soc_suspend NULL
 823#define snd_soc_resume NULL
 824static inline void soc_resume_init(struct snd_soc_card *card) { }
 825#endif
 826
 827static struct device_node
 828*soc_component_to_node(struct snd_soc_component *component)
 829{
 830        struct device_node *of_node;
 831
 832        of_node = component->dev->of_node;
 833        if (!of_node && component->dev->parent)
 834                of_node = component->dev->parent->of_node;
 835
 836        return of_node;
 837}
 838
 839struct of_phandle_args *snd_soc_copy_dai_args(struct device *dev,
 840                                              const struct of_phandle_args *args)
 841{
 842        struct of_phandle_args *ret = devm_kzalloc(dev, sizeof(*ret), GFP_KERNEL);
 843
 844        if (!ret)
 845                return NULL;
 846
 847        *ret = *args;
 848
 849        return ret;
 850}
 851EXPORT_SYMBOL_GPL(snd_soc_copy_dai_args);
 852
 853static int snd_soc_is_matching_component(
 854        const struct snd_soc_dai_link_component *dlc,
 855        struct snd_soc_component *component)
 856{
 857        struct device_node *component_of_node;
 858
 859        if (!dlc)
 860                return 0;
 861
 862        if (dlc->dai_args) {
 863                struct snd_soc_dai *dai;
 864
 865                for_each_component_dais(component, dai)
 866                        if (snd_soc_is_matching_dai(dlc, dai))
 867                                return 1;
 868                return 0;
 869        }
 870
 871        component_of_node = soc_component_to_node(component);
 872
 873        if (dlc->of_node && component_of_node != dlc->of_node)
 874                return 0;
 875        if (dlc->name && strcmp(component->name, dlc->name))
 876                return 0;
 877
 878        return 1;
 879}
 880
 881static struct snd_soc_component *soc_find_component(
 882        const struct snd_soc_dai_link_component *dlc)
 883{
 884        struct snd_soc_component *component;
 885
 886        lockdep_assert_held(&client_mutex);
 887
 888        /*
 889         * NOTE
 890         *
 891         * It returns *1st* found component, but some driver
 892         * has few components by same of_node/name
 893         * ex)
 894         *      CPU component and generic DMAEngine component
 895         */
 896        for_each_component(component)
 897                if (snd_soc_is_matching_component(dlc, component))
 898                        return component;
 899
 900        return NULL;
 901}
 902
 903/**
 904 * snd_soc_find_dai - Find a registered DAI
 905 *
 906 * @dlc: name of the DAI or the DAI driver and optional component info to match
 907 *
 908 * This function will search all registered components and their DAIs to
 909 * find the DAI of the same name. The component's of_node and name
 910 * should also match if being specified.
 911 *
 912 * Return: pointer of DAI, or NULL if not found.
 913 */
 914struct snd_soc_dai *snd_soc_find_dai(
 915        const struct snd_soc_dai_link_component *dlc)
 916{
 917        struct snd_soc_component *component;
 918        struct snd_soc_dai *dai;
 919
 920        lockdep_assert_held(&client_mutex);
 921
 922        /* Find CPU DAI from registered DAIs */
 923        for_each_component(component)
 924                if (snd_soc_is_matching_component(dlc, component))
 925                        for_each_component_dais(component, dai)
 926                                if (snd_soc_is_matching_dai(dlc, dai))
 927                                        return dai;
 928
 929        return NULL;
 930}
 931EXPORT_SYMBOL_GPL(snd_soc_find_dai);
 932
 933struct snd_soc_dai *snd_soc_find_dai_with_mutex(
 934        const struct snd_soc_dai_link_component *dlc)
 935{
 936        struct snd_soc_dai *dai;
 937
 938        mutex_lock(&client_mutex);
 939        dai = snd_soc_find_dai(dlc);
 940        mutex_unlock(&client_mutex);
 941
 942        return dai;
 943}
 944EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
 945
 946static int soc_dai_link_sanity_check(struct snd_soc_card *card,
 947                                     struct snd_soc_dai_link *link)
 948{
 949        int i;
 950        struct snd_soc_dai_link_component *dlc;
 951
 952        /* Codec check */
 953        for_each_link_codecs(link, i, dlc) {
 954                /*
 955                 * Codec must be specified by 1 of name or OF node,
 956                 * not both or neither.
 957                 */
 958                if (snd_soc_dlc_component_is_invalid(dlc))
 959                        goto component_invalid;
 960
 961                if (snd_soc_dlc_component_is_empty(dlc))
 962                        goto component_empty;
 963
 964                /* Codec DAI name must be specified */
 965                if (snd_soc_dlc_dai_is_empty(dlc))
 966                        goto dai_empty;
 967
 968                /*
 969                 * Defer card registration if codec component is not added to
 970                 * component list.
 971                 */
 972                if (!soc_find_component(dlc))
 973                        goto component_not_found;
 974        }
 975
 976        /* Platform check */
 977        for_each_link_platforms(link, i, dlc) {
 978                /*
 979                 * Platform may be specified by either name or OF node, but it
 980                 * can be left unspecified, then no components will be inserted
 981                 * in the rtdcom list
 982                 */
 983                if (snd_soc_dlc_component_is_invalid(dlc))
 984                        goto component_invalid;
 985
 986                if (snd_soc_dlc_component_is_empty(dlc))
 987                        goto component_empty;
 988
 989                /*
 990                 * Defer card registration if platform component is not added to
 991                 * component list.
 992                 */
 993                if (!soc_find_component(dlc))
 994                        goto component_not_found;
 995        }
 996
 997        /* CPU check */
 998        for_each_link_cpus(link, i, dlc) {
 999                /*
1000                 * CPU device may be specified by either name or OF node, but
1001                 * can be left unspecified, and will be matched based on DAI
1002                 * name alone..
1003                 */
1004                if (snd_soc_dlc_component_is_invalid(dlc))
1005                        goto component_invalid;
1006
1007
1008                if (snd_soc_dlc_component_is_empty(dlc)) {
1009                        /*
1010                         * At least one of CPU DAI name or CPU device name/node must be specified
1011                         */
1012                        if (snd_soc_dlc_dai_is_empty(dlc))
1013                                goto component_dai_empty;
1014                } else {
1015                        /*
1016                         * Defer card registration if Component is not added
1017                         */
1018                        if (!soc_find_component(dlc))
1019                                goto component_not_found;
1020                }
1021        }
1022
1023        return 0;
1024
1025component_invalid:
1026        dev_err(card->dev, "ASoC: Both Component name/of_node are set for %s\n", link->name);
1027        return -EINVAL;
1028
1029component_empty:
1030        dev_err(card->dev, "ASoC: Neither Component name/of_node are set for %s\n", link->name);
1031        return -EINVAL;
1032
1033component_not_found:
1034        dev_dbg(card->dev, "ASoC: Component %s not found for link %s\n", dlc->name, link->name);
1035        return -EPROBE_DEFER;
1036
1037dai_empty:
1038        dev_err(card->dev, "ASoC: DAI name is not set for %s\n", link->name);
1039        return -EINVAL;
1040
1041component_dai_empty:
1042        dev_err(card->dev, "ASoC: Neither DAI/Component name/of_node are set for %s\n", link->name);
1043        return -EINVAL;
1044}
1045
1046#define MAX_DEFAULT_CH_MAP_SIZE 8
1047static struct snd_soc_dai_link_ch_map default_ch_map_sync[MAX_DEFAULT_CH_MAP_SIZE] = {
1048        { .cpu = 0, .codec = 0 },
1049        { .cpu = 1, .codec = 1 },
1050        { .cpu = 2, .codec = 2 },
1051        { .cpu = 3, .codec = 3 },
1052        { .cpu = 4, .codec = 4 },
1053        { .cpu = 5, .codec = 5 },
1054        { .cpu = 6, .codec = 6 },
1055        { .cpu = 7, .codec = 7 },
1056};
1057static struct snd_soc_dai_link_ch_map default_ch_map_1cpu[MAX_DEFAULT_CH_MAP_SIZE] = {
1058        { .cpu = 0, .codec = 0 },
1059        { .cpu = 0, .codec = 1 },
1060        { .cpu = 0, .codec = 2 },
1061        { .cpu = 0, .codec = 3 },
1062        { .cpu = 0, .codec = 4 },
1063        { .cpu = 0, .codec = 5 },
1064        { .cpu = 0, .codec = 6 },
1065        { .cpu = 0, .codec = 7 },
1066};
1067static struct snd_soc_dai_link_ch_map default_ch_map_1codec[MAX_DEFAULT_CH_MAP_SIZE] = {
1068        { .cpu = 0, .codec = 0 },
1069        { .cpu = 1, .codec = 0 },
1070        { .cpu = 2, .codec = 0 },
1071        { .cpu = 3, .codec = 0 },
1072        { .cpu = 4, .codec = 0 },
1073        { .cpu = 5, .codec = 0 },
1074        { .cpu = 6, .codec = 0 },
1075        { .cpu = 7, .codec = 0 },
1076};
1077static int snd_soc_compensate_channel_connection_map(struct snd_soc_card *card,
1078                                                     struct snd_soc_dai_link *dai_link)
1079{
1080        struct snd_soc_dai_link_ch_map *ch_maps;
1081        int i;
1082
1083        /*
1084         * dai_link->ch_maps indicates how CPU/Codec are connected.
1085         * It will be a map seen from a larger number of DAI.
1086         * see
1087         *      soc.h :: [dai_link->ch_maps Image sample]
1088         */
1089
1090        /* it should have ch_maps if connection was N:M */
1091        if (dai_link->num_cpus > 1 && dai_link->num_codecs > 1 &&
1092            dai_link->num_cpus != dai_link->num_codecs && !dai_link->ch_maps) {
1093                dev_err(card->dev, "need to have ch_maps when N:M connection (%s)",
1094                        dai_link->name);
1095                return -EINVAL;
1096        }
1097
1098        /* do nothing if it has own maps */
1099        if (dai_link->ch_maps)
1100                goto sanity_check;
1101
1102        /* check default map size */
1103        if (dai_link->num_cpus   > MAX_DEFAULT_CH_MAP_SIZE ||
1104            dai_link->num_codecs > MAX_DEFAULT_CH_MAP_SIZE) {
1105                dev_err(card->dev, "soc-core.c needs update default_connection_maps");
1106                return -EINVAL;
1107        }
1108
1109        /* Compensate missing map for ... */
1110        if (dai_link->num_cpus == dai_link->num_codecs)
1111                dai_link->ch_maps = default_ch_map_sync;        /* for 1:1 or N:N */
1112        else if (dai_link->num_cpus <  dai_link->num_codecs)
1113                dai_link->ch_maps = default_ch_map_1cpu;        /* for 1:N */
1114        else
1115                dai_link->ch_maps = default_ch_map_1codec;      /* for N:1 */
1116
1117sanity_check:
1118        dev_dbg(card->dev, "dai_link %s\n", dai_link->stream_name);
1119        for_each_link_ch_maps(dai_link, i, ch_maps) {
1120                if ((ch_maps->cpu   >= dai_link->num_cpus) ||
1121                    (ch_maps->codec >= dai_link->num_codecs)) {
1122                        dev_err(card->dev,
1123                                "unexpected dai_link->ch_maps[%d] index (cpu(%d/%d) codec(%d/%d))",
1124                                i,
1125                                ch_maps->cpu,   dai_link->num_cpus,
1126                                ch_maps->codec, dai_link->num_codecs);
1127                        return -EINVAL;
1128                }
1129
1130                dev_dbg(card->dev, "  [%d] cpu%d <-> codec%d\n",
1131                        i, ch_maps->cpu, ch_maps->codec);
1132        }
1133
1134        return 0;
1135}
1136
1137/**
1138 * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
1139 * @card: The ASoC card to which the pcm_runtime has
1140 * @rtd: The pcm_runtime to remove
1141 *
1142 * This function removes a pcm_runtime from the ASoC card.
1143 */
1144void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
1145                                struct snd_soc_pcm_runtime *rtd)
1146{
1147        if (!rtd)
1148                return;
1149
1150        lockdep_assert_held(&client_mutex);
1151
1152        /*
1153         * Notify the machine driver for extra destruction
1154         */
1155        snd_soc_card_remove_dai_link(card, rtd->dai_link);
1156
1157        soc_free_pcm_runtime(rtd);
1158}
1159EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
1160
1161/**
1162 * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
1163 * @card: The ASoC card to which the pcm_runtime is added
1164 * @dai_link: The DAI link to find pcm_runtime
1165 *
1166 * This function adds a pcm_runtime ASoC card by using dai_link.
1167 *
1168 * Note: Topology can use this API to add pcm_runtime when probing the
1169 * topology component. And machine drivers can still define static
1170 * DAI links in dai_link array.
1171 */
1172static int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
1173                                   struct snd_soc_dai_link *dai_link)
1174{
1175        struct snd_soc_pcm_runtime *rtd;
1176        struct snd_soc_dai_link_component *codec, *platform, *cpu;
1177        struct snd_soc_component *component;
1178        int i, id, ret;
1179
1180        lockdep_assert_held(&client_mutex);
1181
1182        /*
1183         * Notify the machine driver for extra initialization
1184         */
1185        ret = snd_soc_card_add_dai_link(card, dai_link);
1186        if (ret < 0)
1187                return ret;
1188
1189        if (dai_link->ignore)
1190                return 0;
1191
1192        dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
1193
1194        ret = soc_dai_link_sanity_check(card, dai_link);
1195        if (ret < 0)
1196                return ret;
1197
1198        rtd = soc_new_pcm_runtime(card, dai_link);
1199        if (!rtd)
1200                return -ENOMEM;
1201
1202        for_each_link_cpus(dai_link, i, cpu) {
1203                snd_soc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
1204                if (!snd_soc_rtd_to_cpu(rtd, i)) {
1205                        dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
1206                                 cpu->dai_name);
1207                        goto _err_defer;
1208                }
1209                snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_cpu(rtd, i)->component);
1210        }
1211
1212        /* Find CODEC from registered CODECs */
1213        for_each_link_codecs(dai_link, i, codec) {
1214                snd_soc_rtd_to_codec(rtd, i) = snd_soc_find_dai(codec);
1215                if (!snd_soc_rtd_to_codec(rtd, i)) {
1216                        dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n",
1217                                 codec->dai_name);
1218                        goto _err_defer;
1219                }
1220
1221                snd_soc_rtd_add_component(rtd, snd_soc_rtd_to_codec(rtd, i)->component);
1222        }
1223
1224        /* Find PLATFORM from registered PLATFORMs */
1225        for_each_link_platforms(dai_link, i, platform) {
1226                for_each_component(component) {
1227                        if (!snd_soc_is_matching_component(platform, component))
1228                                continue;
1229
1230                        if (snd_soc_component_is_dummy(component) && component->num_dai)
1231                                continue;
1232
1233                        snd_soc_rtd_add_component(rtd, component);
1234                }
1235        }
1236
1237        /*
1238         * Most drivers will register their PCMs using DAI link ordering but
1239         * topology based drivers can use the DAI link id field to set PCM
1240         * device number and then use rtd + a base offset of the BEs.
1241         *
1242         * FIXME
1243         *
1244         * This should be implemented by using "dai_link" feature instead of
1245         * "component" feature.
1246         */
1247        id = rtd->id;
1248        for_each_rtd_components(rtd, i, component) {
1249                if (!component->driver->use_dai_pcm_id)
1250                        continue;
1251
1252                if (rtd->dai_link->no_pcm)
1253                        id += component->driver->be_pcm_base;
1254                else
1255                        id = rtd->dai_link->id;
1256        }
1257        rtd->id = id;
1258
1259        return 0;
1260
1261_err_defer:
1262        snd_soc_remove_pcm_runtime(card, rtd);
1263        return -EPROBE_DEFER;
1264}
1265
1266int snd_soc_add_pcm_runtimes(struct snd_soc_card *card,
1267                             struct snd_soc_dai_link *dai_link,
1268                             int num_dai_link)
1269{
1270        for (int i = 0; i < num_dai_link; i++) {
1271                int ret;
1272
1273                ret = snd_soc_compensate_channel_connection_map(card, dai_link + i);
1274                if (ret < 0)
1275                        return ret;
1276
1277                ret = snd_soc_add_pcm_runtime(card, dai_link + i);
1278                if (ret < 0)
1279                        return ret;
1280        }
1281
1282        return 0;
1283}
1284EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtimes);
1285
1286static void snd_soc_runtime_get_dai_fmt(struct snd_soc_pcm_runtime *rtd)
1287{
1288        struct snd_soc_dai_link *dai_link = rtd->dai_link;
1289        struct snd_soc_dai *dai, *not_used;
1290        u64 pos, possible_fmt;
1291        unsigned int mask = 0, dai_fmt = 0;
1292        int i, j, priority, pri, until;
1293
1294        /*
1295         * Get selectable format from each DAIs.
1296         *
1297         ****************************
1298         *            NOTE
1299         * Using .auto_selectable_formats is not mandatory,
1300         * we can select format manually from Sound Card.
1301         * When use it, driver should list well tested format only.
1302         ****************************
1303         *
1304         * ex)
1305         *      auto_selectable_formats (= SND_SOC_POSSIBLE_xxx)
1306         *               (A)     (B)     (C)
1307         *      DAI0_: { 0x000F, 0x00F0, 0x0F00 };
1308         *      DAI1 : { 0xF000, 0x0F00 };
1309         *               (X)     (Y)
1310         *
1311         * "until" will be 3 in this case (MAX array size from DAI0 and DAI1)
1312         * Here is dev_dbg() message and comments
1313         *
1314         * priority = 1
1315         * DAI0: (pri, fmt) = (1, 000000000000000F) // 1st check (A) DAI1 is not selected
1316         * DAI1: (pri, fmt) = (0, 0000000000000000) //               Necessary Waste
1317         * DAI0: (pri, fmt) = (1, 000000000000000F) // 2nd check (A)
1318         * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1319         * priority = 2
1320         * DAI0: (pri, fmt) = (2, 00000000000000FF) // 3rd check (A) + (B)
1321         * DAI1: (pri, fmt) = (1, 000000000000F000) //           (X)
1322         * DAI0: (pri, fmt) = (2, 00000000000000FF) // 4th check (A) + (B)
1323         * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1324         * priority = 3
1325         * DAI0: (pri, fmt) = (3, 0000000000000FFF) // 5th check (A) + (B) + (C)
1326         * DAI1: (pri, fmt) = (2, 000000000000FF00) //           (X) + (Y)
1327         * found auto selected format: 0000000000000F00
1328         */
1329        until = snd_soc_dai_get_fmt_max_priority(rtd);
1330        for (priority = 1; priority <= until; priority++) {
1331                for_each_rtd_dais(rtd, j, not_used) {
1332
1333                        possible_fmt = ULLONG_MAX;
1334                        for_each_rtd_dais(rtd, i, dai) {
1335                                u64 fmt = 0;
1336
1337                                pri = (j >= i) ? priority : priority - 1;
1338                                fmt = snd_soc_dai_get_fmt(dai, pri);
1339                                possible_fmt &= fmt;
1340                        }
1341                        if (possible_fmt)
1342                                goto found;
1343                }
1344        }
1345        /* Not Found */
1346        return;
1347found:
1348        /*
1349         * convert POSSIBLE_DAIFMT to DAIFMT
1350         *
1351         * Some basic/default settings on each is defined as 0.
1352         * see
1353         *      SND_SOC_DAIFMT_NB_NF
1354         *      SND_SOC_DAIFMT_GATED
1355         *
1356         * SND_SOC_DAIFMT_xxx_MASK can't notice it if Sound Card specify
1357         * these value, and will be overwrite to auto selected value.
1358         *
1359         * To avoid such issue, loop from 63 to 0 here.
1360         * Small number of SND_SOC_POSSIBLE_xxx will be Hi priority.
1361         * Basic/Default settings of each part and above are defined
1362         * as Hi priority (= small number) of SND_SOC_POSSIBLE_xxx.
1363         */
1364        for (i = 63; i >= 0; i--) {
1365                pos = 1ULL << i;
1366                switch (possible_fmt & pos) {
1367                /*
1368                 * for format
1369                 */
1370                case SND_SOC_POSSIBLE_DAIFMT_I2S:
1371                case SND_SOC_POSSIBLE_DAIFMT_RIGHT_J:
1372                case SND_SOC_POSSIBLE_DAIFMT_LEFT_J:
1373                case SND_SOC_POSSIBLE_DAIFMT_DSP_A:
1374                case SND_SOC_POSSIBLE_DAIFMT_DSP_B:
1375                case SND_SOC_POSSIBLE_DAIFMT_AC97:
1376                case SND_SOC_POSSIBLE_DAIFMT_PDM:
1377                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) | i;
1378                        break;
1379                /*
1380                 * for clock
1381                 */
1382                case SND_SOC_POSSIBLE_DAIFMT_CONT:
1383                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_CONT;
1384                        break;
1385                case SND_SOC_POSSIBLE_DAIFMT_GATED:
1386                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_MASK) | SND_SOC_DAIFMT_GATED;
1387                        break;
1388                /*
1389                 * for clock invert
1390                 */
1391                case SND_SOC_POSSIBLE_DAIFMT_NB_NF:
1392                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_NF;
1393                        break;
1394                case SND_SOC_POSSIBLE_DAIFMT_NB_IF:
1395                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_NB_IF;
1396                        break;
1397                case SND_SOC_POSSIBLE_DAIFMT_IB_NF:
1398                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_NF;
1399                        break;
1400                case SND_SOC_POSSIBLE_DAIFMT_IB_IF:
1401                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_INV_MASK) | SND_SOC_DAIFMT_IB_IF;
1402                        break;
1403                /*
1404                 * for clock provider / consumer
1405                 */
1406                case SND_SOC_POSSIBLE_DAIFMT_CBP_CFP:
1407                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFP;
1408                        break;
1409                case SND_SOC_POSSIBLE_DAIFMT_CBC_CFP:
1410                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFP;
1411                        break;
1412                case SND_SOC_POSSIBLE_DAIFMT_CBP_CFC:
1413                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBP_CFC;
1414                        break;
1415                case SND_SOC_POSSIBLE_DAIFMT_CBC_CFC:
1416                        dai_fmt = (dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) | SND_SOC_DAIFMT_CBC_CFC;
1417                        break;
1418                }
1419        }
1420
1421        /*
1422         * Some driver might have very complex limitation.
1423         * In such case, user want to auto-select non-limitation part,
1424         * and want to manually specify complex part.
1425         *
1426         * Or for example, if both CPU and Codec can be clock provider,
1427         * but because of its quality, user want to specify it manually.
1428         *
1429         * Use manually specified settings if sound card did.
1430         */
1431        if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK))
1432                mask |= SND_SOC_DAIFMT_FORMAT_MASK;
1433        if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_MASK))
1434                mask |= SND_SOC_DAIFMT_CLOCK_MASK;
1435        if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_INV_MASK))
1436                mask |= SND_SOC_DAIFMT_INV_MASK;
1437        if (!(dai_link->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK))
1438                mask |= SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
1439
1440        dai_link->dai_fmt |= (dai_fmt & mask);
1441}
1442
1443/**
1444 * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime
1445 * @rtd: The runtime for which the DAI link format should be changed
1446 * @dai_fmt: The new DAI link format
1447 *
1448 * This function updates the DAI link format for all DAIs connected to the DAI
1449 * link for the specified runtime.
1450 *
1451 * Note: For setups with a static format set the dai_fmt field in the
1452 * corresponding snd_dai_link struct instead of using this function.
1453 *
1454 * Returns 0 on success, otherwise a negative error code.
1455 */
1456int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd,
1457                                unsigned int dai_fmt)
1458{
1459        struct snd_soc_dai *cpu_dai;
1460        struct snd_soc_dai *codec_dai;
1461        unsigned int ext_fmt;
1462        unsigned int i;
1463        int ret;
1464
1465        if (!dai_fmt)
1466                return 0;
1467
1468        /*
1469         * dai_fmt has 4 types
1470         *      1. SND_SOC_DAIFMT_FORMAT_MASK
1471         *      2. SND_SOC_DAIFMT_CLOCK
1472         *      3. SND_SOC_DAIFMT_INV
1473         *      4. SND_SOC_DAIFMT_CLOCK_PROVIDER
1474         *
1475         * 4. CLOCK_PROVIDER is set from Codec perspective in dai_fmt. So it will be flipped
1476         * when this function calls set_fmt() for CPU (CBx_CFx -> Bx_Cx). see below.
1477         * This mean, we can't set CPU/Codec both are clock consumer for example.
1478         * New idea handles 4. in each dai->ext_fmt. It can keep compatibility.
1479         *
1480         * Legacy
1481         *      dai_fmt  includes 1, 2, 3, 4
1482         *
1483         * New idea
1484         *      dai_fmt  includes 1, 2, 3
1485         *      ext_fmt  includes 4
1486         */
1487        for_each_rtd_codec_dais(rtd, i, codec_dai) {
1488                ext_fmt = rtd->dai_link->codecs[i].ext_fmt;
1489                ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt | ext_fmt);
1490                if (ret != 0 && ret != -ENOTSUPP)
1491                        return ret;
1492        }
1493
1494        /* Flip the polarity for the "CPU" end of link */
1495        /* Will effect only for 4. SND_SOC_DAIFMT_CLOCK_PROVIDER */
1496        dai_fmt = snd_soc_daifmt_clock_provider_flipped(dai_fmt);
1497
1498        for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1499                ext_fmt = rtd->dai_link->cpus[i].ext_fmt;
1500                ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt | ext_fmt);
1501                if (ret != 0 && ret != -ENOTSUPP)
1502                        return ret;
1503        }
1504
1505        return 0;
1506}
1507EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt);
1508
1509static int soc_init_pcm_runtime(struct snd_soc_card *card,
1510                                struct snd_soc_pcm_runtime *rtd)
1511{
1512        struct snd_soc_dai_link *dai_link = rtd->dai_link;
1513        struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
1514        int ret;
1515
1516        /* do machine specific initialization */
1517        ret = snd_soc_link_init(rtd);
1518        if (ret < 0)
1519                return ret;
1520
1521        snd_soc_runtime_get_dai_fmt(rtd);
1522        ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt);
1523        if (ret)
1524                goto err;
1525
1526        /* add DPCM sysfs entries */
1527        soc_dpcm_debugfs_add(rtd);
1528
1529        /* create compress_device if possible */
1530        ret = snd_soc_dai_compress_new(cpu_dai, rtd);
1531        if (ret != -ENOTSUPP)
1532                goto err;
1533
1534        /* create the pcm */
1535        ret = soc_new_pcm(rtd);
1536        if (ret < 0) {
1537                dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1538                        dai_link->stream_name, ret);
1539                goto err;
1540        }
1541
1542        ret = snd_soc_pcm_dai_new(rtd);
1543        if (ret < 0)
1544                goto err;
1545
1546        rtd->initialized = true;
1547
1548        return 0;
1549err:
1550        snd_soc_link_exit(rtd);
1551        return ret;
1552}
1553
1554static void soc_set_name_prefix(struct snd_soc_card *card,
1555                                struct snd_soc_component *component)
1556{
1557        struct device_node *of_node = soc_component_to_node(component);
1558        const char *str;
1559        int ret, i;
1560
1561        for (i = 0; i < card->num_configs; i++) {
1562                struct snd_soc_codec_conf *map = &card->codec_conf[i];
1563
1564                if (snd_soc_is_matching_component(&map->dlc, component) &&
1565                    map->name_prefix) {
1566                        component->name_prefix = map->name_prefix;
1567                        return;
1568                }
1569        }
1570
1571        /*
1572         * If there is no configuration table or no match in the table,
1573         * check if a prefix is provided in the node
1574         */
1575        ret = of_property_read_string(of_node, "sound-name-prefix", &str);
1576        if (ret < 0)
1577                return;
1578
1579        component->name_prefix = str;
1580}
1581
1582static void soc_remove_component(struct snd_soc_component *component,
1583                                 int probed)
1584{
1585
1586        if (!component->card)
1587                return;
1588
1589        if (probed)
1590                snd_soc_component_remove(component);
1591
1592        list_del_init(&component->card_list);
1593        snd_soc_dapm_free(snd_soc_component_get_dapm(component));
1594        soc_cleanup_component_debugfs(component);
1595        component->card = NULL;
1596        snd_soc_component_module_put_when_remove(component);
1597}
1598
1599static int soc_probe_component(struct snd_soc_card *card,
1600                               struct snd_soc_component *component)
1601{
1602        struct snd_soc_dapm_context *dapm =
1603                snd_soc_component_get_dapm(component);
1604        struct snd_soc_dai *dai;
1605        int probed = 0;
1606        int ret;
1607
1608        if (snd_soc_component_is_dummy(component))
1609                return 0;
1610
1611        if (component->card) {
1612                if (component->card != card) {
1613                        dev_err(component->dev,
1614                                "Trying to bind component \"%s\" to card \"%s\" but is already bound to card \"%s\"\n",
1615                                component->name, card->name, component->card->name);
1616                        return -ENODEV;
1617                }
1618                return 0;
1619        }
1620
1621        ret = snd_soc_component_module_get_when_probe(component);
1622        if (ret < 0)
1623                return ret;
1624
1625        component->card = card;
1626        soc_set_name_prefix(card, component);
1627
1628        soc_init_component_debugfs(component);
1629
1630        snd_soc_dapm_init(dapm, card, component);
1631
1632        ret = snd_soc_dapm_new_controls(dapm,
1633                                        component->driver->dapm_widgets,
1634                                        component->driver->num_dapm_widgets);
1635
1636        if (ret != 0) {
1637                dev_err(component->dev,
1638                        "Failed to create new controls %d\n", ret);
1639                goto err_probe;
1640        }
1641
1642        for_each_component_dais(component, dai) {
1643                ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1644                if (ret != 0) {
1645                        dev_err(component->dev,
1646                                "Failed to create DAI widgets %d\n", ret);
1647                        goto err_probe;
1648                }
1649        }
1650
1651        ret = snd_soc_component_probe(component);
1652        if (ret < 0)
1653                goto err_probe;
1654
1655        WARN(dapm->idle_bias_off &&
1656             dapm->bias_level != SND_SOC_BIAS_OFF,
1657             "codec %s can not start from non-off bias with idle_bias_off==1\n",
1658             component->name);
1659        probed = 1;
1660
1661        /*
1662         * machine specific init
1663         * see
1664         *      snd_soc_component_set_aux()
1665         */
1666        ret = snd_soc_component_init(component);
1667        if (ret < 0)
1668                goto err_probe;
1669
1670        ret = snd_soc_add_component_controls(component,
1671                                             component->driver->controls,
1672                                             component->driver->num_controls);
1673        if (ret < 0)
1674                goto err_probe;
1675
1676        ret = snd_soc_dapm_add_routes(dapm,
1677                                      component->driver->dapm_routes,
1678                                      component->driver->num_dapm_routes);
1679        if (ret < 0)
1680                goto err_probe;
1681
1682        /* see for_each_card_components */
1683        list_add(&component->card_list, &card->component_dev_list);
1684
1685err_probe:
1686        if (ret < 0)
1687                soc_remove_component(component, probed);
1688
1689        return ret;
1690}
1691
1692static void soc_remove_link_dais(struct snd_soc_card *card)
1693{
1694        struct snd_soc_pcm_runtime *rtd;
1695        int order;
1696
1697        for_each_comp_order(order) {
1698                for_each_card_rtds(card, rtd) {
1699                        /* remove all rtd connected DAIs in good order */
1700                        snd_soc_pcm_dai_remove(rtd, order);
1701                }
1702        }
1703}
1704
1705static int soc_probe_link_dais(struct snd_soc_card *card)
1706{
1707        struct snd_soc_pcm_runtime *rtd;
1708        int order, ret;
1709
1710        for_each_comp_order(order) {
1711                for_each_card_rtds(card, rtd) {
1712                        /* probe all rtd connected DAIs in good order */
1713                        ret = snd_soc_pcm_dai_probe(rtd, order);
1714                        if (ret)
1715                                return ret;
1716                }
1717        }
1718
1719        return 0;
1720}
1721
1722static void soc_remove_link_components(struct snd_soc_card *card)
1723{
1724        struct snd_soc_component *component;
1725        struct snd_soc_pcm_runtime *rtd;
1726        int i, order;
1727
1728        for_each_comp_order(order) {
1729                for_each_card_rtds(card, rtd) {
1730                        for_each_rtd_components(rtd, i, component) {
1731                                if (component->driver->remove_order != order)
1732                                        continue;
1733
1734                                soc_remove_component(component, 1);
1735                        }
1736                }
1737        }
1738}
1739
1740static int soc_probe_link_components(struct snd_soc_card *card)
1741{
1742        struct snd_soc_component *component;
1743        struct snd_soc_pcm_runtime *rtd;
1744        int i, ret, order;
1745
1746        for_each_comp_order(order) {
1747                for_each_card_rtds(card, rtd) {
1748                        for_each_rtd_components(rtd, i, component) {
1749                                if (component->driver->probe_order != order)
1750                                        continue;
1751
1752                                ret = soc_probe_component(card, component);
1753                                if (ret < 0)
1754                                        return ret;
1755                        }
1756                }
1757        }
1758
1759        return 0;
1760}
1761
1762static void soc_unbind_aux_dev(struct snd_soc_card *card)
1763{
1764        struct snd_soc_component *component, *_component;
1765
1766        for_each_card_auxs_safe(card, component, _component) {
1767                /* for snd_soc_component_init() */
1768                snd_soc_component_set_aux(component, NULL);
1769                list_del(&component->card_aux_list);
1770        }
1771}
1772
1773static int soc_bind_aux_dev(struct snd_soc_card *card)
1774{
1775        struct snd_soc_component *component;
1776        struct snd_soc_aux_dev *aux;
1777        int i;
1778
1779        for_each_card_pre_auxs(card, i, aux) {
1780                /* codecs, usually analog devices */
1781                component = soc_find_component(&aux->dlc);
1782                if (!component)
1783                        return -EPROBE_DEFER;
1784
1785                /* for snd_soc_component_init() */
1786                snd_soc_component_set_aux(component, aux);
1787                /* see for_each_card_auxs */
1788                list_add(&component->card_aux_list, &card->aux_comp_list);
1789        }
1790        return 0;
1791}
1792
1793static int soc_probe_aux_devices(struct snd_soc_card *card)
1794{
1795        struct snd_soc_component *component;
1796        int order;
1797        int ret;
1798
1799        for_each_comp_order(order) {
1800                for_each_card_auxs(card, component) {
1801                        if (component->driver->probe_order != order)
1802                                continue;
1803
1804                        ret = soc_probe_component(card, component);
1805                        if (ret < 0)
1806                                return ret;
1807                }
1808        }
1809
1810        return 0;
1811}
1812
1813static void soc_remove_aux_devices(struct snd_soc_card *card)
1814{
1815        struct snd_soc_component *comp, *_comp;
1816        int order;
1817
1818        for_each_comp_order(order) {
1819                for_each_card_auxs_safe(card, comp, _comp) {
1820                        if (comp->driver->remove_order == order)
1821                                soc_remove_component(comp, 1);
1822                }
1823        }
1824}
1825
1826#ifdef CONFIG_DMI
1827/*
1828 * If a DMI filed contain strings in this blacklist (e.g.
1829 * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken
1830 * as invalid and dropped when setting the card long name from DMI info.
1831 */
1832static const char * const dmi_blacklist[] = {
1833        "To be filled by OEM",
1834        "TBD by OEM",
1835        "Default String",
1836        "Board Manufacturer",
1837        "Board Vendor Name",
1838        "Board Product Name",
1839        NULL,   /* terminator */
1840};
1841
1842/*
1843 * Trim special characters, and replace '-' with '_' since '-' is used to
1844 * separate different DMI fields in the card long name. Only number and
1845 * alphabet characters and a few separator characters are kept.
1846 */
1847static void cleanup_dmi_name(char *name)
1848{
1849        int i, j = 0;
1850
1851        for (i = 0; name[i]; i++) {
1852                if (isalnum(name[i]) || (name[i] == '.')
1853                    || (name[i] == '_'))
1854                        name[j++] = name[i];
1855                else if (name[i] == '-')
1856                        name[j++] = '_';
1857        }
1858
1859        name[j] = '\0';
1860}
1861
1862/*
1863 * Check if a DMI field is valid, i.e. not containing any string
1864 * in the black list.
1865 */
1866static int is_dmi_valid(const char *field)
1867{
1868        int i = 0;
1869
1870        while (dmi_blacklist[i]) {
1871                if (strstr(field, dmi_blacklist[i]))
1872                        return 0;
1873                i++;
1874        }
1875
1876        return 1;
1877}
1878
1879/*
1880 * Append a string to card->dmi_longname with character cleanups.
1881 */
1882static void append_dmi_string(struct snd_soc_card *card, const char *str)
1883{
1884        char *dst = card->dmi_longname;
1885        size_t dst_len = sizeof(card->dmi_longname);
1886        size_t len;
1887
1888        len = strlen(dst);
1889        snprintf(dst + len, dst_len - len, "-%s", str);
1890
1891        len++;  /* skip the separator "-" */
1892        if (len < dst_len)
1893                cleanup_dmi_name(dst + len);
1894}
1895
1896/**
1897 * snd_soc_set_dmi_name() - Register DMI names to card
1898 * @card: The card to register DMI names
1899 *
1900 * An Intel machine driver may be used by many different devices but are
1901 * difficult for userspace to differentiate, since machine drivers usually
1902 * use their own name as the card short name and leave the card long name
1903 * blank. To differentiate such devices and fix bugs due to lack of
1904 * device-specific configurations, this function allows DMI info to be used
1905 * as the sound card long name, in the format of
1906 * "vendor-product-version-board"
1907 * (Character '-' is used to separate different DMI fields here).
1908 * This will help the user space to load the device-specific Use Case Manager
1909 * (UCM) configurations for the card.
1910 *
1911 * Possible card long names may be:
1912 * DellInc.-XPS139343-01-0310JH
1913 * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA
1914 * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX
1915 *
1916 * This function also supports flavoring the card longname to provide
1917 * the extra differentiation, like "vendor-product-version-board-flavor".
1918 *
1919 * We only keep number and alphabet characters and a few separator characters
1920 * in the card long name since UCM in the user space uses the card long names
1921 * as card configuration directory names and AudoConf cannot support special
1922 * characters like SPACE.
1923 *
1924 * Returns 0 on success, otherwise a negative error code.
1925 */
1926static int snd_soc_set_dmi_name(struct snd_soc_card *card)
1927{
1928        const char *vendor, *product, *board;
1929
1930        if (card->long_name)
1931                return 0; /* long name already set by driver or from DMI */
1932
1933        if (!dmi_available)
1934                return 0;
1935
1936        /* make up dmi long name as: vendor-product-version-board */
1937        vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
1938        if (!vendor || !is_dmi_valid(vendor)) {
1939                dev_warn(card->dev, "ASoC: no DMI vendor name!\n");
1940                return 0;
1941        }
1942
1943        snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor);
1944        cleanup_dmi_name(card->dmi_longname);
1945
1946        product = dmi_get_system_info(DMI_PRODUCT_NAME);
1947        if (product && is_dmi_valid(product)) {
1948                const char *product_version = dmi_get_system_info(DMI_PRODUCT_VERSION);
1949
1950                append_dmi_string(card, product);
1951
1952                /*
1953                 * some vendors like Lenovo may only put a self-explanatory
1954                 * name in the product version field
1955                 */
1956                if (product_version && is_dmi_valid(product_version))
1957                        append_dmi_string(card, product_version);
1958        }
1959
1960        board = dmi_get_system_info(DMI_BOARD_NAME);
1961        if (board && is_dmi_valid(board)) {
1962                if (!product || strcasecmp(board, product))
1963                        append_dmi_string(card, board);
1964        } else if (!product) {
1965                /* fall back to using legacy name */
1966                dev_warn(card->dev, "ASoC: no DMI board/product name!\n");
1967                return 0;
1968        }
1969
1970        /* set the card long name */
1971        card->long_name = card->dmi_longname;
1972
1973        return 0;
1974}
1975#else
1976static inline int snd_soc_set_dmi_name(struct snd_soc_card *card)
1977{
1978        return 0;
1979}
1980#endif /* CONFIG_DMI */
1981
1982static void soc_check_tplg_fes(struct snd_soc_card *card)
1983{
1984        struct snd_soc_component *component;
1985        const struct snd_soc_component_driver *comp_drv;
1986        struct snd_soc_dai_link *dai_link;
1987        int i;
1988
1989        for_each_component(component) {
1990
1991                /* does this component override BEs ? */
1992                if (!component->driver->ignore_machine)
1993                        continue;
1994
1995                /* for this machine ? */
1996                if (!strcmp(component->driver->ignore_machine,
1997                            card->dev->driver->name))
1998                        goto match;
1999                if (strcmp(component->driver->ignore_machine,
2000                           dev_name(card->dev)))
2001                        continue;
2002match:
2003                /* machine matches, so override the rtd data */
2004                for_each_card_prelinks(card, i, dai_link) {
2005
2006                        /* ignore this FE */
2007                        if (dai_link->dynamic) {
2008                                dai_link->ignore = true;
2009                                continue;
2010                        }
2011
2012                        dev_dbg(card->dev, "info: override BE DAI link %s\n",
2013                                card->dai_link[i].name);
2014
2015                        /* override platform component */
2016                        if (!dai_link->platforms) {
2017                                dev_err(card->dev, "init platform error");
2018                                continue;
2019                        }
2020
2021                        if (component->dev->of_node)
2022                                dai_link->platforms->of_node = component->dev->of_node;
2023                        else
2024                                dai_link->platforms->name = component->name;
2025
2026                        /* convert non BE into BE */
2027                        dai_link->no_pcm = 1;
2028
2029                        /*
2030                         * override any BE fixups
2031                         * see
2032                         *      snd_soc_link_be_hw_params_fixup()
2033                         */
2034                        dai_link->be_hw_params_fixup =
2035                                component->driver->be_hw_params_fixup;
2036
2037                        /*
2038                         * most BE links don't set stream name, so set it to
2039                         * dai link name if it's NULL to help bind widgets.
2040                         */
2041                        if (!dai_link->stream_name)
2042                                dai_link->stream_name = dai_link->name;
2043                }
2044
2045                /* Inform userspace we are using alternate topology */
2046                if (component->driver->topology_name_prefix) {
2047
2048                        /* topology shortname created? */
2049                        if (!card->topology_shortname_created) {
2050                                comp_drv = component->driver;
2051
2052                                snprintf(card->topology_shortname, 32, "%s-%s",
2053                                         comp_drv->topology_name_prefix,
2054                                         card->name);
2055                                card->topology_shortname_created = true;
2056                        }
2057
2058                        /* use topology shortname */
2059                        card->name = card->topology_shortname;
2060                }
2061        }
2062}
2063
2064#define soc_setup_card_name(card, name, name1, name2) \
2065        __soc_setup_card_name(card, name, sizeof(name), name1, name2)
2066static void __soc_setup_card_name(struct snd_soc_card *card,
2067                                  char *name, int len,
2068                                  const char *name1, const char *name2)
2069{
2070        const char *src = name1 ? name1 : name2;
2071        int i;
2072
2073        snprintf(name, len, "%s", src);
2074
2075        if (name != card->snd_card->driver)
2076                return;
2077
2078        /*
2079         * Name normalization (driver field)
2080         *
2081         * The driver name is somewhat special, as it's used as a key for
2082         * searches in the user-space.
2083         *
2084         * ex)
2085         *      "abcd??efg" -> "abcd__efg"
2086         */
2087        for (i = 0; i < len; i++) {
2088                switch (name[i]) {
2089                case '_':
2090                case '-':
2091                case '\0':
2092                        break;
2093                default:
2094                        if (!isalnum(name[i]))
2095                                name[i] = '_';
2096                        break;
2097                }
2098        }
2099
2100        /*
2101         * The driver field should contain a valid string from the user view.
2102         * The wrapping usually does not work so well here. Set a smaller string
2103         * in the specific ASoC driver.
2104         */
2105        if (strlen(src) > len - 1)
2106                dev_err(card->dev, "ASoC: driver name too long '%s' -> '%s'\n", src, name);
2107}
2108
2109static void soc_cleanup_card_resources(struct snd_soc_card *card)
2110{
2111        struct snd_soc_pcm_runtime *rtd, *n;
2112
2113        if (card->snd_card)
2114                snd_card_disconnect_sync(card->snd_card);
2115
2116        snd_soc_dapm_shutdown(card);
2117
2118        /* release machine specific resources */
2119        for_each_card_rtds(card, rtd)
2120                if (rtd->initialized)
2121                        snd_soc_link_exit(rtd);
2122        /* remove and free each DAI */
2123        soc_remove_link_dais(card);
2124        soc_remove_link_components(card);
2125
2126        for_each_card_rtds_safe(card, rtd, n)
2127                snd_soc_remove_pcm_runtime(card, rtd);
2128
2129        /* remove auxiliary devices */
2130        soc_remove_aux_devices(card);
2131        soc_unbind_aux_dev(card);
2132
2133        snd_soc_dapm_free(&card->dapm);
2134        soc_cleanup_card_debugfs(card);
2135
2136        /* remove the card */
2137        snd_soc_card_remove(card);
2138
2139        if (card->snd_card) {
2140                snd_card_free(card->snd_card);
2141                card->snd_card = NULL;
2142        }
2143}
2144
2145static void snd_soc_unbind_card(struct snd_soc_card *card)
2146{
2147        if (snd_soc_card_is_instantiated(card)) {
2148                card->instantiated = false;
2149                snd_soc_flush_all_delayed_work(card);
2150
2151                soc_cleanup_card_resources(card);
2152        }
2153}
2154
2155static int snd_soc_bind_card(struct snd_soc_card *card)
2156{
2157        struct snd_soc_pcm_runtime *rtd;
2158        struct snd_soc_component *component;
2159        int ret;
2160
2161        snd_soc_card_mutex_lock_root(card);
2162        snd_soc_fill_dummy_dai(card);
2163
2164        snd_soc_dapm_init(&card->dapm, card, NULL);
2165
2166        /* check whether any platform is ignore machine FE and using topology */
2167        soc_check_tplg_fes(card);
2168
2169        /* bind aux_devs too */
2170        ret = soc_bind_aux_dev(card);
2171        if (ret < 0)
2172                goto probe_end;
2173
2174        /* add predefined DAI links to the list */
2175        card->num_rtd = 0;
2176        ret = snd_soc_add_pcm_runtimes(card, card->dai_link, card->num_links);
2177        if (ret < 0)
2178                goto probe_end;
2179
2180        /* card bind complete so register a sound card */
2181        ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
2182                        card->owner, 0, &card->snd_card);
2183        if (ret < 0) {
2184                dev_err(card->dev,
2185                        "ASoC: can't create sound card for card %s: %d\n",
2186                        card->name, ret);
2187                goto probe_end;
2188        }
2189
2190        soc_init_card_debugfs(card);
2191
2192        soc_resume_init(card);
2193
2194        ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
2195                                        card->num_dapm_widgets);
2196        if (ret < 0)
2197                goto probe_end;
2198
2199        ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets,
2200                                        card->num_of_dapm_widgets);
2201        if (ret < 0)
2202                goto probe_end;
2203
2204        /* initialise the sound card only once */
2205        ret = snd_soc_card_probe(card);
2206        if (ret < 0)
2207                goto probe_end;
2208
2209        /* probe all components used by DAI links on this card */
2210        ret = soc_probe_link_components(card);
2211        if (ret < 0) {
2212                if (ret != -EPROBE_DEFER) {
2213                        dev_err(card->dev,
2214                                "ASoC: failed to instantiate card %d\n", ret);
2215                }
2216                goto probe_end;
2217        }
2218
2219        /* probe auxiliary components */
2220        ret = soc_probe_aux_devices(card);
2221        if (ret < 0) {
2222                dev_err(card->dev,
2223                        "ASoC: failed to probe aux component %d\n", ret);
2224                goto probe_end;
2225        }
2226
2227        /* probe all DAI links on this card */
2228        ret = soc_probe_link_dais(card);
2229        if (ret < 0) {
2230                dev_err(card->dev,
2231                        "ASoC: failed to instantiate card %d\n", ret);
2232                goto probe_end;
2233        }
2234
2235        for_each_card_rtds(card, rtd) {
2236                ret = soc_init_pcm_runtime(card, rtd);
2237                if (ret < 0)
2238                        goto probe_end;
2239        }
2240
2241        snd_soc_dapm_link_dai_widgets(card);
2242        snd_soc_dapm_connect_dai_link_widgets(card);
2243
2244        ret = snd_soc_add_card_controls(card, card->controls,
2245                                        card->num_controls);
2246        if (ret < 0)
2247                goto probe_end;
2248
2249        ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
2250                                      card->num_dapm_routes);
2251        if (ret < 0)
2252                goto probe_end;
2253
2254        ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes,
2255                                      card->num_of_dapm_routes);
2256        if (ret < 0)
2257                goto probe_end;
2258
2259        /* try to set some sane longname if DMI is available */
2260        snd_soc_set_dmi_name(card);
2261
2262        soc_setup_card_name(card, card->snd_card->shortname,
2263                            card->name, NULL);
2264        soc_setup_card_name(card, card->snd_card->longname,
2265                            card->long_name, card->name);
2266        soc_setup_card_name(card, card->snd_card->driver,
2267                            card->driver_name, card->name);
2268
2269        if (card->components) {
2270                /* the current implementation of snd_component_add() accepts */
2271                /* multiple components in the string separated by space, */
2272                /* but the string collision (identical string) check might */
2273                /* not work correctly */
2274                ret = snd_component_add(card->snd_card, card->components);
2275                if (ret < 0) {
2276                        dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n",
2277                                card->name, ret);
2278                        goto probe_end;
2279                }
2280        }
2281
2282        ret = snd_soc_card_late_probe(card);
2283        if (ret < 0)
2284                goto probe_end;
2285
2286        snd_soc_dapm_new_widgets(card);
2287        snd_soc_card_fixup_controls(card);
2288
2289        ret = snd_card_register(card->snd_card);
2290        if (ret < 0) {
2291                dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
2292                                ret);
2293                goto probe_end;
2294        }
2295
2296        card->instantiated = 1;
2297        snd_soc_dapm_mark_endpoints_dirty(card);
2298        snd_soc_dapm_sync(&card->dapm);
2299
2300        /* deactivate pins to sleep state */
2301        for_each_card_components(card, component)
2302                if (!snd_soc_component_active(component))
2303                        pinctrl_pm_select_sleep_state(component->dev);
2304
2305probe_end:
2306        if (ret < 0)
2307                soc_cleanup_card_resources(card);
2308        snd_soc_card_mutex_unlock(card);
2309
2310        return ret;
2311}
2312
2313static void devm_card_bind_release(struct device *dev, void *res)
2314{
2315        snd_soc_unregister_card(*(struct snd_soc_card **)res);
2316}
2317
2318static int devm_snd_soc_bind_card(struct device *dev, struct snd_soc_card *card)
2319{
2320        struct snd_soc_card **ptr;
2321        int ret;
2322
2323        ptr = devres_alloc(devm_card_bind_release, sizeof(*ptr), GFP_KERNEL);
2324        if (!ptr)
2325                return -ENOMEM;
2326
2327        ret = snd_soc_bind_card(card);
2328        if (ret == 0 || ret == -EPROBE_DEFER) {
2329                *ptr = card;
2330                devres_add(dev, ptr);
2331        } else {
2332                devres_free(ptr);
2333        }
2334
2335        return ret;
2336}
2337
2338static int snd_soc_rebind_card(struct snd_soc_card *card)
2339{
2340        int ret;
2341
2342        if (card->devres_dev) {
2343                devres_destroy(card->devres_dev, devm_card_bind_release, NULL, NULL);
2344                ret = devm_snd_soc_bind_card(card->devres_dev, card);
2345        } else {
2346                ret = snd_soc_bind_card(card);
2347        }
2348
2349        if (ret != -EPROBE_DEFER)
2350                list_del_init(&card->list);
2351
2352        return ret;
2353}
2354
2355/* probes a new socdev */
2356static int soc_probe(struct platform_device *pdev)
2357{
2358        struct snd_soc_card *card = platform_get_drvdata(pdev);
2359
2360        /*
2361         * no card, so machine driver should be registering card
2362         * we should not be here in that case so ret error
2363         */
2364        if (!card)
2365                return -EINVAL;
2366
2367        dev_warn(&pdev->dev,
2368                 "ASoC: machine %s should use snd_soc_register_card()\n",
2369                 card->name);
2370
2371        /* Bodge while we unpick instantiation */
2372        card->dev = &pdev->dev;
2373
2374        return devm_snd_soc_register_card(&pdev->dev, card);
2375}
2376
2377int snd_soc_poweroff(struct device *dev)
2378{
2379        struct snd_soc_card *card = dev_get_drvdata(dev);
2380        struct snd_soc_component *component;
2381
2382        if (!snd_soc_card_is_instantiated(card))
2383                return 0;
2384
2385        /*
2386         * Flush out pmdown_time work - we actually do want to run it
2387         * now, we're shutting down so no imminent restart.
2388         */
2389        snd_soc_flush_all_delayed_work(card);
2390
2391        snd_soc_dapm_shutdown(card);
2392
2393        /* deactivate pins to sleep state */
2394        for_each_card_components(card, component)
2395                pinctrl_pm_select_sleep_state(component->dev);
2396
2397        return 0;
2398}
2399EXPORT_SYMBOL_GPL(snd_soc_poweroff);
2400
2401const struct dev_pm_ops snd_soc_pm_ops = {
2402        .suspend = snd_soc_suspend,
2403        .resume = snd_soc_resume,
2404        .freeze = snd_soc_suspend,
2405        .thaw = snd_soc_resume,
2406        .poweroff = snd_soc_poweroff,
2407        .restore = snd_soc_resume,
2408};
2409EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
2410
2411/* ASoC platform driver */
2412static struct platform_driver soc_driver = {
2413        .driver         = {
2414                .name           = "soc-audio",
2415                .pm             = &snd_soc_pm_ops,
2416        },
2417        .probe          = soc_probe,
2418};
2419
2420/**
2421 * snd_soc_cnew - create new control
2422 * @_template: control template
2423 * @data: control private data
2424 * @long_name: control long name
2425 * @prefix: control name prefix
2426 *
2427 * Create a new mixer control from a template control.
2428 *
2429 * Returns 0 for success, else error.
2430 */
2431struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2432                                  void *data, const char *long_name,
2433                                  const char *prefix)
2434{
2435        struct snd_kcontrol_new template;
2436        struct snd_kcontrol *kcontrol;
2437        char *name = NULL;
2438
2439        memcpy(&template, _template, sizeof(template));
2440        template.index = 0;
2441
2442        if (!long_name)
2443                long_name = template.name;
2444
2445        if (prefix) {
2446                name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
2447                if (!name)
2448                        return NULL;
2449
2450                template.name = name;
2451        } else {
2452                template.name = long_name;
2453        }
2454
2455        kcontrol = snd_ctl_new1(&template, data);
2456
2457        kfree(name);
2458
2459        return kcontrol;
2460}
2461EXPORT_SYMBOL_GPL(snd_soc_cnew);
2462
2463static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2464        const struct snd_kcontrol_new *controls, int num_controls,
2465        const char *prefix, void *data)
2466{
2467        int i;
2468
2469        for (i = 0; i < num_controls; i++) {
2470                const struct snd_kcontrol_new *control = &controls[i];
2471                int err = snd_ctl_add(card, snd_soc_cnew(control, data,
2472                                                         control->name, prefix));
2473                if (err < 0) {
2474                        dev_err(dev, "ASoC: Failed to add %s: %d\n",
2475                                control->name, err);
2476                        return err;
2477                }
2478        }
2479
2480        return 0;
2481}
2482
2483/**
2484 * snd_soc_add_component_controls - Add an array of controls to a component.
2485 *
2486 * @component: Component to add controls to
2487 * @controls: Array of controls to add
2488 * @num_controls: Number of elements in the array
2489 *
2490 * Return: 0 for success, else error.
2491 */
2492int snd_soc_add_component_controls(struct snd_soc_component *component,
2493        const struct snd_kcontrol_new *controls, unsigned int num_controls)
2494{
2495        struct snd_card *card = component->card->snd_card;
2496
2497        return snd_soc_add_controls(card, component->dev, controls,
2498                        num_controls, component->name_prefix, component);
2499}
2500EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2501
2502/**
2503 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2504 * Convenience function to add a list of controls.
2505 *
2506 * @soc_card: SoC card to add controls to
2507 * @controls: array of controls to add
2508 * @num_controls: number of elements in the array
2509 *
2510 * Return 0 for success, else error.
2511 */
2512int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2513        const struct snd_kcontrol_new *controls, int num_controls)
2514{
2515        struct snd_card *card = soc_card->snd_card;
2516
2517        return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2518                        NULL, soc_card);
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2521
2522/**
2523 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2524 * Convenience function to add a list of controls.
2525 *
2526 * @dai: DAI to add controls to
2527 * @controls: array of controls to add
2528 * @num_controls: number of elements in the array
2529 *
2530 * Return 0 for success, else error.
2531 */
2532int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2533        const struct snd_kcontrol_new *controls, int num_controls)
2534{
2535        struct snd_card *card = dai->component->card->snd_card;
2536
2537        return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2538                        NULL, dai);
2539}
2540EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2541
2542/**
2543 * snd_soc_register_card - Register a card with the ASoC core
2544 *
2545 * @card: Card to register
2546 *
2547 */
2548int snd_soc_register_card(struct snd_soc_card *card)
2549{
2550        int ret;
2551
2552        if (!card->name || !card->dev)
2553                return -EINVAL;
2554
2555        dev_set_drvdata(card->dev, card);
2556
2557        INIT_LIST_HEAD(&card->widgets);
2558        INIT_LIST_HEAD(&card->paths);
2559        INIT_LIST_HEAD(&card->dapm_list);
2560        INIT_LIST_HEAD(&card->aux_comp_list);
2561        INIT_LIST_HEAD(&card->component_dev_list);
2562        INIT_LIST_HEAD(&card->list);
2563        INIT_LIST_HEAD(&card->rtd_list);
2564        INIT_LIST_HEAD(&card->dapm_dirty);
2565        INIT_LIST_HEAD(&card->dobj_list);
2566
2567        card->instantiated = 0;
2568        mutex_init(&card->mutex);
2569        mutex_init(&card->dapm_mutex);
2570        mutex_init(&card->pcm_mutex);
2571
2572        mutex_lock(&client_mutex);
2573
2574        if (card->devres_dev) {
2575                ret = devm_snd_soc_bind_card(card->devres_dev, card);
2576                if (ret == -EPROBE_DEFER) {
2577                        list_add(&card->list, &unbind_card_list);
2578                        ret = 0;
2579                }
2580        } else {
2581                ret = snd_soc_bind_card(card);
2582        }
2583
2584        mutex_unlock(&client_mutex);
2585
2586        return ret;
2587}
2588EXPORT_SYMBOL_GPL(snd_soc_register_card);
2589
2590/**
2591 * snd_soc_unregister_card - Unregister a card with the ASoC core
2592 *
2593 * @card: Card to unregister
2594 *
2595 */
2596void snd_soc_unregister_card(struct snd_soc_card *card)
2597{
2598        mutex_lock(&client_mutex);
2599        snd_soc_unbind_card(card);
2600        list_del(&card->list);
2601        mutex_unlock(&client_mutex);
2602        dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
2603}
2604EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2605
2606/*
2607 * Simplify DAI link configuration by removing ".-1" from device names
2608 * and sanitizing names.
2609 */
2610static char *fmt_single_name(struct device *dev, int *id)
2611{
2612        const char *devname = dev_name(dev);
2613        char *found, *name;
2614        unsigned int id1, id2;
2615        int __id;
2616
2617        if (devname == NULL)
2618                return NULL;
2619
2620        name = devm_kstrdup(dev, devname, GFP_KERNEL);
2621        if (!name)
2622                return NULL;
2623
2624        /* are we a "%s.%d" name (platform and SPI components) */
2625        found = strstr(name, dev->driver->name);
2626        if (found) {
2627                /* get ID */
2628                if (sscanf(&found[strlen(dev->driver->name)], ".%d", &__id) == 1) {
2629
2630                        /* discard ID from name if ID == -1 */
2631                        if (__id == -1)
2632                                found[strlen(dev->driver->name)] = '\0';
2633                }
2634
2635        /* I2C component devices are named "bus-addr" */
2636        } else if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2637
2638                /* create unique ID number from I2C addr and bus */
2639                __id = ((id1 & 0xffff) << 16) + id2;
2640
2641                devm_kfree(dev, name);
2642
2643                /* sanitize component name for DAI link creation */
2644                name = devm_kasprintf(dev, GFP_KERNEL, "%s.%s", dev->driver->name, devname);
2645        } else {
2646                __id = 0;
2647        }
2648
2649        if (id)
2650                *id = __id;
2651
2652        return name;
2653}
2654
2655/*
2656 * Simplify DAI link naming for single devices with multiple DAIs by removing
2657 * any ".-1" and using the DAI name (instead of device name).
2658 */
2659static inline char *fmt_multiple_name(struct device *dev,
2660                struct snd_soc_dai_driver *dai_drv)
2661{
2662        if (dai_drv->name == NULL) {
2663                dev_err(dev,
2664                        "ASoC: error - multiple DAI %s registered with no name\n",
2665                        dev_name(dev));
2666                return NULL;
2667        }
2668
2669        return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL);
2670}
2671
2672void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2673{
2674        dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
2675        list_del(&dai->list);
2676}
2677EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2678
2679/**
2680 * snd_soc_register_dai - Register a DAI dynamically & create its widgets
2681 *
2682 * @component: The component the DAIs are registered for
2683 * @dai_drv: DAI driver to use for the DAI
2684 * @legacy_dai_naming: if %true, use legacy single-name format;
2685 *      if %false, use multiple-name format;
2686 *
2687 * Topology can use this API to register DAIs when probing a component.
2688 * These DAIs's widgets will be freed in the card cleanup and the DAIs
2689 * will be freed in the component cleanup.
2690 */
2691struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component,
2692                                         struct snd_soc_dai_driver *dai_drv,
2693                                         bool legacy_dai_naming)
2694{
2695        struct device *dev = component->dev;
2696        struct snd_soc_dai *dai;
2697
2698        lockdep_assert_held(&client_mutex);
2699
2700        dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL);
2701        if (dai == NULL)
2702                return NULL;
2703
2704        /*
2705         * Back in the old days when we still had component-less DAIs,
2706         * instead of having a static name, component-less DAIs would
2707         * inherit the name of the parent device so it is possible to
2708         * register multiple instances of the DAI. We still need to keep
2709         * the same naming style even though those DAIs are not
2710         * component-less anymore.
2711         */
2712        if (legacy_dai_naming &&
2713            (dai_drv->id == 0 || dai_drv->name == NULL)) {
2714                dai->name = fmt_single_name(dev, &dai->id);
2715        } else {
2716                dai->name = fmt_multiple_name(dev, dai_drv);
2717                if (dai_drv->id)
2718                        dai->id = dai_drv->id;
2719                else
2720                        dai->id = component->num_dai;
2721        }
2722        if (!dai->name)
2723                return NULL;
2724
2725        dai->component = component;
2726        dai->dev = dev;
2727        dai->driver = dai_drv;
2728
2729        /* see for_each_component_dais */
2730        list_add_tail(&dai->list, &component->dai_list);
2731        component->num_dai++;
2732
2733        dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
2734        return dai;
2735}
2736EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2737
2738/**
2739 * snd_soc_unregister_dais - Unregister DAIs from the ASoC core
2740 *
2741 * @component: The component for which the DAIs should be unregistered
2742 */
2743static void snd_soc_unregister_dais(struct snd_soc_component *component)
2744{
2745        struct snd_soc_dai *dai, *_dai;
2746
2747        for_each_component_dais_safe(component, dai, _dai)
2748                snd_soc_unregister_dai(dai);
2749}
2750
2751/**
2752 * snd_soc_register_dais - Register a DAI with the ASoC core
2753 *
2754 * @component: The component the DAIs are registered for
2755 * @dai_drv: DAI driver to use for the DAIs
2756 * @count: Number of DAIs
2757 */
2758static int snd_soc_register_dais(struct snd_soc_component *component,
2759                                 struct snd_soc_dai_driver *dai_drv,
2760                                 size_t count)
2761{
2762        struct snd_soc_dai *dai;
2763        unsigned int i;
2764        int ret;
2765
2766        for (i = 0; i < count; i++) {
2767                dai = snd_soc_register_dai(component, dai_drv + i, count == 1 &&
2768                                           component->driver->legacy_dai_naming);
2769                if (dai == NULL) {
2770                        ret = -ENOMEM;
2771                        goto err;
2772                }
2773        }
2774
2775        return 0;
2776
2777err:
2778        snd_soc_unregister_dais(component);
2779
2780        return ret;
2781}
2782
2783#define ENDIANNESS_MAP(name) \
2784        (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE)
2785static u64 endianness_format_map[] = {
2786        ENDIANNESS_MAP(S16_),
2787        ENDIANNESS_MAP(U16_),
2788        ENDIANNESS_MAP(S24_),
2789        ENDIANNESS_MAP(U24_),
2790        ENDIANNESS_MAP(S32_),
2791        ENDIANNESS_MAP(U32_),
2792        ENDIANNESS_MAP(S24_3),
2793        ENDIANNESS_MAP(U24_3),
2794        ENDIANNESS_MAP(S20_3),
2795        ENDIANNESS_MAP(U20_3),
2796        ENDIANNESS_MAP(S18_3),
2797        ENDIANNESS_MAP(U18_3),
2798        ENDIANNESS_MAP(FLOAT_),
2799        ENDIANNESS_MAP(FLOAT64_),
2800        ENDIANNESS_MAP(IEC958_SUBFRAME_),
2801};
2802
2803/*
2804 * Fix up the DAI formats for endianness: codecs don't actually see
2805 * the endianness of the data but we're using the CPU format
2806 * definitions which do need to include endianness so we ensure that
2807 * codec DAIs always have both big and little endian variants set.
2808 */
2809static void convert_endianness_formats(struct snd_soc_pcm_stream *stream)
2810{
2811        int i;
2812
2813        for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++)
2814                if (stream->formats & endianness_format_map[i])
2815                        stream->formats |= endianness_format_map[i];
2816}
2817
2818static void snd_soc_del_component_unlocked(struct snd_soc_component *component)
2819{
2820        struct snd_soc_card *card = component->card;
2821        bool instantiated;
2822
2823        snd_soc_unregister_dais(component);
2824
2825        if (card) {
2826                instantiated = card->instantiated;
2827                snd_soc_unbind_card(card);
2828                if (instantiated)
2829                        list_add(&card->list, &unbind_card_list);
2830        }
2831
2832        list_del(&component->list);
2833}
2834
2835int snd_soc_component_initialize(struct snd_soc_component *component,
2836                                 const struct snd_soc_component_driver *driver,
2837                                 struct device *dev)
2838{
2839        INIT_LIST_HEAD(&component->dai_list);
2840        INIT_LIST_HEAD(&component->dobj_list);
2841        INIT_LIST_HEAD(&component->card_list);
2842        INIT_LIST_HEAD(&component->list);
2843        mutex_init(&component->io_mutex);
2844
2845        if (!component->name) {
2846                component->name = fmt_single_name(dev, NULL);
2847                if (!component->name) {
2848                        dev_err(dev, "ASoC: Failed to allocate name\n");
2849                        return -ENOMEM;
2850                }
2851        }
2852
2853        component->dev          = dev;
2854        component->driver       = driver;
2855
2856#ifdef CONFIG_DEBUG_FS
2857        if (!component->debugfs_prefix)
2858                component->debugfs_prefix = driver->debugfs_prefix;
2859#endif
2860
2861        return 0;
2862}
2863EXPORT_SYMBOL_GPL(snd_soc_component_initialize);
2864
2865int snd_soc_add_component(struct snd_soc_component *component,
2866                          struct snd_soc_dai_driver *dai_drv,
2867                          int num_dai)
2868{
2869        struct snd_soc_card *card, *c;
2870        int ret;
2871        int i;
2872
2873        mutex_lock(&client_mutex);
2874
2875        if (component->driver->endianness) {
2876                for (i = 0; i < num_dai; i++) {
2877                        convert_endianness_formats(&dai_drv[i].playback);
2878                        convert_endianness_formats(&dai_drv[i].capture);
2879                }
2880        }
2881
2882        ret = snd_soc_register_dais(component, dai_drv, num_dai);
2883        if (ret < 0) {
2884                dev_err(component->dev, "ASoC: Failed to register DAIs: %d\n",
2885                        ret);
2886                goto err_cleanup;
2887        }
2888
2889        if (!component->driver->write && !component->driver->read) {
2890                if (!component->regmap)
2891                        component->regmap = dev_get_regmap(component->dev,
2892                                                           NULL);
2893                if (component->regmap)
2894                        snd_soc_component_setup_regmap(component);
2895        }
2896
2897        /* see for_each_component */
2898        list_add(&component->list, &component_list);
2899
2900        list_for_each_entry_safe(card, c, &unbind_card_list, list)
2901                snd_soc_rebind_card(card);
2902
2903err_cleanup:
2904        if (ret < 0)
2905                snd_soc_del_component_unlocked(component);
2906
2907        mutex_unlock(&client_mutex);
2908        return ret;
2909}
2910EXPORT_SYMBOL_GPL(snd_soc_add_component);
2911
2912int snd_soc_register_component(struct device *dev,
2913                        const struct snd_soc_component_driver *component_driver,
2914                        struct snd_soc_dai_driver *dai_drv,
2915                        int num_dai)
2916{
2917        struct snd_soc_component *component;
2918        int ret;
2919
2920        component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
2921        if (!component)
2922                return -ENOMEM;
2923
2924        ret = snd_soc_component_initialize(component, component_driver, dev);
2925        if (ret < 0)
2926                return ret;
2927
2928        return snd_soc_add_component(component, dai_drv, num_dai);
2929}
2930EXPORT_SYMBOL_GPL(snd_soc_register_component);
2931
2932/**
2933 * snd_soc_unregister_component_by_driver - Unregister component using a given driver
2934 * from the ASoC core
2935 *
2936 * @dev: The device to unregister
2937 * @component_driver: The component driver to unregister
2938 */
2939void snd_soc_unregister_component_by_driver(struct device *dev,
2940                                            const struct snd_soc_component_driver *component_driver)
2941{
2942        const char *driver_name = NULL;
2943
2944        if (component_driver)
2945                driver_name = component_driver->name;
2946
2947        mutex_lock(&client_mutex);
2948        while (1) {
2949                struct snd_soc_component *component = snd_soc_lookup_component_nolocked(dev, driver_name);
2950
2951                if (!component)
2952                        break;
2953
2954                snd_soc_del_component_unlocked(component);
2955        }
2956        mutex_unlock(&client_mutex);
2957}
2958EXPORT_SYMBOL_GPL(snd_soc_unregister_component_by_driver);
2959
2960/* Retrieve a card's name from device tree */
2961int snd_soc_of_parse_card_name(struct snd_soc_card *card,
2962                               const char *propname)
2963{
2964        struct device_node *np;
2965        int ret;
2966
2967        if (!card->dev) {
2968                pr_err("card->dev is not set before calling %s\n", __func__);
2969                return -EINVAL;
2970        }
2971
2972        np = card->dev->of_node;
2973
2974        ret = of_property_read_string_index(np, propname, 0, &card->name);
2975        /*
2976         * EINVAL means the property does not exist. This is fine providing
2977         * card->name was previously set, which is checked later in
2978         * snd_soc_register_card.
2979         */
2980        if (ret < 0 && ret != -EINVAL) {
2981                dev_err(card->dev,
2982                        "ASoC: Property '%s' could not be read: %d\n",
2983                        propname, ret);
2984                return ret;
2985        }
2986
2987        return 0;
2988}
2989EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
2990
2991static const struct snd_soc_dapm_widget simple_widgets[] = {
2992        SND_SOC_DAPM_MIC("Microphone", NULL),
2993        SND_SOC_DAPM_LINE("Line", NULL),
2994        SND_SOC_DAPM_HP("Headphone", NULL),
2995        SND_SOC_DAPM_SPK("Speaker", NULL),
2996};
2997
2998int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
2999                                          const char *propname)
3000{
3001        struct device_node *np = card->dev->of_node;
3002        struct snd_soc_dapm_widget *widgets;
3003        const char *template, *wname;
3004        int i, j, num_widgets;
3005
3006        num_widgets = of_property_count_strings(np, propname);
3007        if (num_widgets < 0) {
3008                dev_err(card->dev,
3009                        "ASoC: Property '%s' does not exist\n", propname);
3010                return -EINVAL;
3011        }
3012        if (!num_widgets) {
3013                dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
3014                        propname);
3015                return -EINVAL;
3016        }
3017        if (num_widgets & 1) {
3018                dev_err(card->dev,
3019                        "ASoC: Property '%s' length is not even\n", propname);
3020                return -EINVAL;
3021        }
3022
3023        num_widgets /= 2;
3024
3025        widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
3026                               GFP_KERNEL);
3027        if (!widgets) {
3028                dev_err(card->dev,
3029                        "ASoC: Could not allocate memory for widgets\n");
3030                return -ENOMEM;
3031        }
3032
3033        for (i = 0; i < num_widgets; i++) {
3034                int ret = of_property_read_string_index(np, propname,
3035                                                        2 * i, &template);
3036                if (ret) {
3037                        dev_err(card->dev,
3038                                "ASoC: Property '%s' index %d read error:%d\n",
3039                                propname, 2 * i, ret);
3040                        return -EINVAL;
3041                }
3042
3043                for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
3044                        if (!strncmp(template, simple_widgets[j].name,
3045                                     strlen(simple_widgets[j].name))) {
3046                                widgets[i] = simple_widgets[j];
3047                                break;
3048                        }
3049                }
3050
3051                if (j >= ARRAY_SIZE(simple_widgets)) {
3052                        dev_err(card->dev,
3053                                "ASoC: DAPM widget '%s' is not supported\n",
3054                                template);
3055                        return -EINVAL;
3056                }
3057
3058                ret = of_property_read_string_index(np, propname,
3059                                                    (2 * i) + 1,
3060                                                    &wname);
3061                if (ret) {
3062                        dev_err(card->dev,
3063                                "ASoC: Property '%s' index %d read error:%d\n",
3064                                propname, (2 * i) + 1, ret);
3065                        return -EINVAL;
3066                }
3067
3068                widgets[i].name = wname;
3069        }
3070
3071        card->of_dapm_widgets = widgets;
3072        card->num_of_dapm_widgets = num_widgets;
3073
3074        return 0;
3075}
3076EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
3077
3078int snd_soc_of_parse_pin_switches(struct snd_soc_card *card, const char *prop)
3079{
3080        const unsigned int nb_controls_max = 16;
3081        const char **strings, *control_name;
3082        struct snd_kcontrol_new *controls;
3083        struct device *dev = card->dev;
3084        unsigned int i, nb_controls;
3085        int ret;
3086
3087        if (!of_property_present(dev->of_node, prop))
3088                return 0;
3089
3090        strings = devm_kcalloc(dev, nb_controls_max,
3091                               sizeof(*strings), GFP_KERNEL);
3092        if (!strings)
3093                return -ENOMEM;
3094
3095        ret = of_property_read_string_array(dev->of_node, prop,
3096                                            strings, nb_controls_max);
3097        if (ret < 0)
3098                return ret;
3099
3100        nb_controls = (unsigned int)ret;
3101
3102        controls = devm_kcalloc(dev, nb_controls,
3103                                sizeof(*controls), GFP_KERNEL);
3104        if (!controls)
3105                return -ENOMEM;
3106
3107        for (i = 0; i < nb_controls; i++) {
3108                control_name = devm_kasprintf(dev, GFP_KERNEL,
3109                                              "%s Switch", strings[i]);
3110                if (!control_name)
3111                        return -ENOMEM;
3112
3113                controls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
3114                controls[i].name = control_name;
3115                controls[i].info = snd_soc_dapm_info_pin_switch;
3116                controls[i].get = snd_soc_dapm_get_pin_switch;
3117                controls[i].put = snd_soc_dapm_put_pin_switch;
3118                controls[i].private_value = (unsigned long)strings[i];
3119        }
3120
3121        card->controls = controls;
3122        card->num_controls = nb_controls;
3123
3124        return 0;
3125}
3126EXPORT_SYMBOL_GPL(snd_soc_of_parse_pin_switches);
3127
3128int snd_soc_of_get_slot_mask(struct device_node *np,
3129                             const char *prop_name,
3130                             unsigned int *mask)
3131{
3132        u32 val;
3133        const __be32 *of_slot_mask = of_get_property(np, prop_name, &val);
3134        int i;
3135
3136        if (!of_slot_mask)
3137                return 0;
3138        val /= sizeof(u32);
3139        for (i = 0; i < val; i++)
3140                if (be32_to_cpup(&of_slot_mask[i]))
3141                        *mask |= (1 << i);
3142
3143        return val;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask);
3146
3147int snd_soc_of_parse_tdm_slot(struct device_node *np,
3148                              unsigned int *tx_mask,
3149                              unsigned int *rx_mask,
3150                              unsigned int *slots,
3151                              unsigned int *slot_width)
3152{
3153        u32 val;
3154        int ret;
3155
3156        if (tx_mask)
3157                snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask);
3158        if (rx_mask)
3159                snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask);
3160
3161        ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
3162        if (ret && ret != -EINVAL)
3163                return ret;
3164        if (!ret && slots)
3165                *slots = val;
3166
3167        ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
3168        if (ret && ret != -EINVAL)
3169                return ret;
3170        if (!ret && slot_width)
3171                *slot_width = val;
3172
3173        return 0;
3174}
3175EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
3176
3177void snd_soc_dlc_use_cpu_as_platform(struct snd_soc_dai_link_component *platforms,
3178                                     struct snd_soc_dai_link_component *cpus)
3179{
3180        platforms->of_node      = cpus->of_node;
3181        platforms->dai_args     = cpus->dai_args;
3182}
3183EXPORT_SYMBOL_GPL(snd_soc_dlc_use_cpu_as_platform);
3184
3185void snd_soc_of_parse_node_prefix(struct device_node *np,
3186                                  struct snd_soc_codec_conf *codec_conf,
3187                                  struct device_node *of_node,
3188                                  const char *propname)
3189{
3190        const char *str;
3191        int ret;
3192
3193        ret = of_property_read_string(np, propname, &str);
3194        if (ret < 0) {
3195                /* no prefix is not error */
3196                return;
3197        }
3198
3199        codec_conf->dlc.of_node = of_node;
3200        codec_conf->name_prefix = str;
3201}
3202EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix);
3203
3204int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3205                                   const char *propname)
3206{
3207        struct device_node *np = card->dev->of_node;
3208        int num_routes;
3209        struct snd_soc_dapm_route *routes;
3210        int i;
3211
3212        num_routes = of_property_count_strings(np, propname);
3213        if (num_routes < 0 || num_routes & 1) {
3214                dev_err(card->dev,
3215                        "ASoC: Property '%s' does not exist or its length is not even\n",
3216                        propname);
3217                return -EINVAL;
3218        }
3219        num_routes /= 2;
3220
3221        routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes),
3222                              GFP_KERNEL);
3223        if (!routes) {
3224                dev_err(card->dev,
3225                        "ASoC: Could not allocate DAPM route table\n");
3226                return -ENOMEM;
3227        }
3228
3229        for (i = 0; i < num_routes; i++) {
3230                int ret = of_property_read_string_index(np, propname,
3231                                                        2 * i, &routes[i].sink);
3232                if (ret) {
3233                        dev_err(card->dev,
3234                                "ASoC: Property '%s' index %d could not be read: %d\n",
3235                                propname, 2 * i, ret);
3236                        return -EINVAL;
3237                }
3238                ret = of_property_read_string_index(np, propname,
3239                        (2 * i) + 1, &routes[i].source);
3240                if (ret) {
3241                        dev_err(card->dev,
3242                                "ASoC: Property '%s' index %d could not be read: %d\n",
3243                                propname, (2 * i) + 1, ret);
3244                        return -EINVAL;
3245                }
3246        }
3247
3248        card->num_of_dapm_routes = num_routes;
3249        card->of_dapm_routes = routes;
3250
3251        return 0;
3252}
3253EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3254
3255int snd_soc_of_parse_aux_devs(struct snd_soc_card *card, const char *propname)
3256{
3257        struct device_node *node = card->dev->of_node;
3258        struct snd_soc_aux_dev *aux;
3259        int num, i;
3260
3261        num = of_count_phandle_with_args(node, propname, NULL);
3262        if (num == -ENOENT) {
3263                return 0;
3264        } else if (num < 0) {
3265                dev_err(card->dev, "ASOC: Property '%s' could not be read: %d\n",
3266                        propname, num);
3267                return num;
3268        }
3269
3270        aux = devm_kcalloc(card->dev, num, sizeof(*aux), GFP_KERNEL);
3271        if (!aux)
3272                return -ENOMEM;
3273        card->aux_dev = aux;
3274        card->num_aux_devs = num;
3275
3276        for_each_card_pre_auxs(card, i, aux) {
3277                aux->dlc.of_node = of_parse_phandle(node, propname, i);
3278                if (!aux->dlc.of_node)
3279                        return -EINVAL;
3280        }
3281
3282        return 0;
3283}
3284EXPORT_SYMBOL_GPL(snd_soc_of_parse_aux_devs);
3285
3286unsigned int snd_soc_daifmt_clock_provider_flipped(unsigned int dai_fmt)
3287{
3288        unsigned int inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
3289
3290        switch (dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
3291        case SND_SOC_DAIFMT_CBP_CFP:
3292                inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
3293                break;
3294        case SND_SOC_DAIFMT_CBP_CFC:
3295                inv_dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
3296                break;
3297        case SND_SOC_DAIFMT_CBC_CFP:
3298                inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
3299                break;
3300        case SND_SOC_DAIFMT_CBC_CFC:
3301                inv_dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
3302                break;
3303        }
3304
3305        return inv_dai_fmt;
3306}
3307EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_flipped);
3308
3309unsigned int snd_soc_daifmt_clock_provider_from_bitmap(unsigned int bit_frame)
3310{
3311        /*
3312         * bit_frame is return value from
3313         *      snd_soc_daifmt_parse_clock_provider_raw()
3314         */
3315
3316        /* Codec base */
3317        switch (bit_frame) {
3318        case 0x11:
3319                return SND_SOC_DAIFMT_CBP_CFP;
3320        case 0x10:
3321                return SND_SOC_DAIFMT_CBP_CFC;
3322        case 0x01:
3323                return SND_SOC_DAIFMT_CBC_CFP;
3324        default:
3325                return SND_SOC_DAIFMT_CBC_CFC;
3326        }
3327
3328        return 0;
3329}
3330EXPORT_SYMBOL_GPL(snd_soc_daifmt_clock_provider_from_bitmap);
3331
3332unsigned int snd_soc_daifmt_parse_format(struct device_node *np,
3333                                         const char *prefix)
3334{
3335        int ret;
3336        char prop[128];
3337        unsigned int format = 0;
3338        int bit, frame;
3339        const char *str;
3340        struct {
3341                char *name;
3342                unsigned int val;
3343        } of_fmt_table[] = {
3344                { "i2s",        SND_SOC_DAIFMT_I2S },
3345                { "right_j",    SND_SOC_DAIFMT_RIGHT_J },
3346                { "left_j",     SND_SOC_DAIFMT_LEFT_J },
3347                { "dsp_a",      SND_SOC_DAIFMT_DSP_A },
3348                { "dsp_b",      SND_SOC_DAIFMT_DSP_B },
3349                { "ac97",       SND_SOC_DAIFMT_AC97 },
3350                { "pdm",        SND_SOC_DAIFMT_PDM},
3351                { "msb",        SND_SOC_DAIFMT_MSB },
3352                { "lsb",        SND_SOC_DAIFMT_LSB },
3353        };
3354
3355        if (!prefix)
3356                prefix = "";
3357
3358        /*
3359         * check "dai-format = xxx"
3360         * or    "[prefix]format = xxx"
3361         * SND_SOC_DAIFMT_FORMAT_MASK area
3362         */
3363        ret = of_property_read_string(np, "dai-format", &str);
3364        if (ret < 0) {
3365                snprintf(prop, sizeof(prop), "%sformat", prefix);
3366                ret = of_property_read_string(np, prop, &str);
3367        }
3368        if (ret == 0) {
3369                int i;
3370
3371                for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
3372                        if (strcmp(str, of_fmt_table[i].name) == 0) {
3373                                format |= of_fmt_table[i].val;
3374                                break;
3375                        }
3376                }
3377        }
3378
3379        /*
3380         * check "[prefix]continuous-clock"
3381         * SND_SOC_DAIFMT_CLOCK_MASK area
3382         */
3383        snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
3384        if (of_property_read_bool(np, prop))
3385                format |= SND_SOC_DAIFMT_CONT;
3386        else
3387                format |= SND_SOC_DAIFMT_GATED;
3388
3389        /*
3390         * check "[prefix]bitclock-inversion"
3391         * check "[prefix]frame-inversion"
3392         * SND_SOC_DAIFMT_INV_MASK area
3393         */
3394        snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
3395        bit = of_property_read_bool(np, prop);
3396
3397        snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
3398        frame = of_property_read_bool(np, prop);
3399
3400        switch ((bit << 4) + frame) {
3401        case 0x11:
3402                format |= SND_SOC_DAIFMT_IB_IF;
3403                break;
3404        case 0x10:
3405                format |= SND_SOC_DAIFMT_IB_NF;
3406                break;
3407        case 0x01:
3408                format |= SND_SOC_DAIFMT_NB_IF;
3409                break;
3410        default:
3411                /* SND_SOC_DAIFMT_NB_NF is default */
3412                break;
3413        }
3414
3415        return format;
3416}
3417EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_format);
3418
3419unsigned int snd_soc_daifmt_parse_clock_provider_raw(struct device_node *np,
3420                                                     const char *prefix,
3421                                                     struct device_node **bitclkmaster,
3422                                                     struct device_node **framemaster)
3423{
3424        char prop[128];
3425        unsigned int bit, frame;
3426
3427        if (!np)
3428                return 0;
3429
3430        if (!prefix)
3431                prefix = "";
3432
3433        /*
3434         * check "[prefix]bitclock-master"
3435         * check "[prefix]frame-master"
3436         */
3437        snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
3438        bit = of_property_present(np, prop);
3439        if (bit && bitclkmaster)
3440                *bitclkmaster = of_parse_phandle(np, prop, 0);
3441
3442        snprintf(prop, sizeof(prop), "%sframe-master", prefix);
3443        frame = of_property_present(np, prop);
3444        if (frame && framemaster)
3445                *framemaster = of_parse_phandle(np, prop, 0);
3446
3447        /*
3448         * return bitmap.
3449         * It will be parameter of
3450         *      snd_soc_daifmt_clock_provider_from_bitmap()
3451         */
3452        return (bit << 4) + frame;
3453}
3454EXPORT_SYMBOL_GPL(snd_soc_daifmt_parse_clock_provider_raw);
3455
3456int snd_soc_get_stream_cpu(const struct snd_soc_dai_link *dai_link, int stream)
3457{
3458        /*
3459         * [Normal]
3460         *
3461         * Playback
3462         *      CPU  : SNDRV_PCM_STREAM_PLAYBACK
3463         *      Codec: SNDRV_PCM_STREAM_PLAYBACK
3464         *
3465         * Capture
3466         *      CPU  : SNDRV_PCM_STREAM_CAPTURE
3467         *      Codec: SNDRV_PCM_STREAM_CAPTURE
3468         */
3469        if (!dai_link->c2c_params)
3470                return stream;
3471
3472        /*
3473         * [Codec2Codec]
3474         *
3475         * Playback
3476         *      CPU  : SNDRV_PCM_STREAM_CAPTURE
3477         *      Codec: SNDRV_PCM_STREAM_PLAYBACK
3478         *
3479         * Capture
3480         *      CPU  : SNDRV_PCM_STREAM_PLAYBACK
3481         *      Codec: SNDRV_PCM_STREAM_CAPTURE
3482         */
3483        if (stream == SNDRV_PCM_STREAM_CAPTURE)
3484                return SNDRV_PCM_STREAM_PLAYBACK;
3485
3486        return SNDRV_PCM_STREAM_CAPTURE;
3487}
3488EXPORT_SYMBOL_GPL(snd_soc_get_stream_cpu);
3489
3490int snd_soc_get_dai_id(struct device_node *ep)
3491{
3492        struct snd_soc_component *component;
3493        struct snd_soc_dai_link_component dlc = {
3494                .of_node = of_graph_get_port_parent(ep),
3495        };
3496        int ret;
3497
3498
3499        /*
3500         * For example HDMI case, HDMI has video/sound port,
3501         * but ALSA SoC needs sound port number only.
3502         * Thus counting HDMI DT port/endpoint doesn't work.
3503         * Then, it should have .of_xlate_dai_id
3504         */
3505        ret = -ENOTSUPP;
3506        mutex_lock(&client_mutex);
3507        component = soc_find_component(&dlc);
3508        if (component)
3509                ret = snd_soc_component_of_xlate_dai_id(component, ep);
3510        mutex_unlock(&client_mutex);
3511
3512        of_node_put(dlc.of_node);
3513
3514        return ret;
3515}
3516EXPORT_SYMBOL_GPL(snd_soc_get_dai_id);
3517
3518int snd_soc_get_dlc(const struct of_phandle_args *args, struct snd_soc_dai_link_component *dlc)
3519{
3520        struct snd_soc_component *pos;
3521        int ret = -EPROBE_DEFER;
3522
3523        mutex_lock(&client_mutex);
3524        for_each_component(pos) {
3525                struct device_node *component_of_node = soc_component_to_node(pos);
3526
3527                if (component_of_node != args->np || !pos->num_dai)
3528                        continue;
3529
3530                ret = snd_soc_component_of_xlate_dai_name(pos, args, &dlc->dai_name);
3531                if (ret == -ENOTSUPP) {
3532                        struct snd_soc_dai *dai;
3533                        int id = -1;
3534
3535                        switch (args->args_count) {
3536                        case 0:
3537                                id = 0; /* same as dai_drv[0] */
3538                                break;
3539                        case 1:
3540                                id = args->args[0];
3541                                break;
3542                        default:
3543                                /* not supported */
3544                                break;
3545                        }
3546
3547                        if (id < 0 || id >= pos->num_dai) {
3548                                ret = -EINVAL;
3549                                continue;
3550                        }
3551
3552                        ret = 0;
3553
3554                        /* find target DAI */
3555                        for_each_component_dais(pos, dai) {
3556                                if (id == 0)
3557                                        break;
3558                                id--;
3559                        }
3560
3561                        dlc->dai_name   = snd_soc_dai_name_get(dai);
3562                } else if (ret) {
3563                        /*
3564                         * if another error than ENOTSUPP is returned go on and
3565                         * check if another component is provided with the same
3566                         * node. This may happen if a device provides several
3567                         * components
3568                         */
3569                        continue;
3570                }
3571
3572                break;
3573        }
3574
3575        if (ret == 0)
3576                dlc->of_node = args->np;
3577
3578        mutex_unlock(&client_mutex);
3579        return ret;
3580}
3581EXPORT_SYMBOL_GPL(snd_soc_get_dlc);
3582
3583int snd_soc_of_get_dlc(struct device_node *of_node,
3584                       struct of_phandle_args *args,
3585                       struct snd_soc_dai_link_component *dlc,
3586                       int index)
3587{
3588        struct of_phandle_args __args;
3589        int ret;
3590
3591        if (!args)
3592                args = &__args;
3593
3594        ret = of_parse_phandle_with_args(of_node, "sound-dai",
3595                                         "#sound-dai-cells", index, args);
3596        if (ret)
3597                return ret;
3598
3599        return snd_soc_get_dlc(args, dlc);
3600}
3601EXPORT_SYMBOL_GPL(snd_soc_of_get_dlc);
3602
3603int snd_soc_get_dai_name(const struct of_phandle_args *args,
3604                         const char **dai_name)
3605{
3606        struct snd_soc_dai_link_component dlc;
3607        int ret = snd_soc_get_dlc(args, &dlc);
3608
3609        if (ret == 0)
3610                *dai_name = dlc.dai_name;
3611
3612        return ret;
3613}
3614EXPORT_SYMBOL_GPL(snd_soc_get_dai_name);
3615
3616int snd_soc_of_get_dai_name(struct device_node *of_node,
3617                            const char **dai_name, int index)
3618{
3619        struct snd_soc_dai_link_component dlc;
3620        int ret = snd_soc_of_get_dlc(of_node, NULL, &dlc, index);
3621
3622        if (ret == 0)
3623                *dai_name = dlc.dai_name;
3624
3625        return ret;
3626}
3627EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
3628
3629struct snd_soc_dai *snd_soc_get_dai_via_args(const struct of_phandle_args *dai_args)
3630{
3631        struct snd_soc_dai *dai;
3632        struct snd_soc_component *component;
3633
3634        mutex_lock(&client_mutex);
3635        for_each_component(component) {
3636                for_each_component_dais(component, dai)
3637                        if (snd_soc_is_match_dai_args(dai->driver->dai_args, dai_args))
3638                                goto found;
3639        }
3640        dai = NULL;
3641found:
3642        mutex_unlock(&client_mutex);
3643        return dai;
3644}
3645EXPORT_SYMBOL_GPL(snd_soc_get_dai_via_args);
3646
3647static void __snd_soc_of_put_component(struct snd_soc_dai_link_component *component)
3648{
3649        if (component->of_node) {
3650                of_node_put(component->of_node);
3651                component->of_node = NULL;
3652        }
3653}
3654
3655static int __snd_soc_of_get_dai_link_component_alloc(
3656        struct device *dev, struct device_node *of_node,
3657        struct snd_soc_dai_link_component **ret_component,
3658        int *ret_num)
3659{
3660        struct snd_soc_dai_link_component *component;
3661        int num;
3662
3663        /* Count the number of CPUs/CODECs */
3664        num = of_count_phandle_with_args(of_node, "sound-dai", "#sound-dai-cells");
3665        if (num <= 0) {
3666                if (num == -ENOENT)
3667                        dev_err(dev, "No 'sound-dai' property\n");
3668                else
3669                        dev_err(dev, "Bad phandle in 'sound-dai'\n");
3670                return num;
3671        }
3672        component = devm_kcalloc(dev, num, sizeof(*component), GFP_KERNEL);
3673        if (!component)
3674                return -ENOMEM;
3675
3676        *ret_component  = component;
3677        *ret_num        = num;
3678
3679        return 0;
3680}
3681
3682/*
3683 * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array
3684 * @dai_link: DAI link
3685 *
3686 * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs().
3687 */
3688void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link)
3689{
3690        struct snd_soc_dai_link_component *component;
3691        int index;
3692
3693        for_each_link_codecs(dai_link, index, component)
3694                __snd_soc_of_put_component(component);
3695}
3696EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs);
3697
3698/*
3699 * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree
3700 * @dev: Card device
3701 * @of_node: Device node
3702 * @dai_link: DAI link
3703 *
3704 * Builds an array of CODEC DAI components from the DAI link property
3705 * 'sound-dai'.
3706 * The array is set in the DAI link and the number of DAIs is set accordingly.
3707 * The device nodes in the array (of_node) must be dereferenced by calling
3708 * snd_soc_of_put_dai_link_codecs() on @dai_link.
3709 *
3710 * Returns 0 for success
3711 */
3712int snd_soc_of_get_dai_link_codecs(struct device *dev,
3713                                   struct device_node *of_node,
3714                                   struct snd_soc_dai_link *dai_link)
3715{
3716        struct snd_soc_dai_link_component *component;
3717        int index, ret;
3718
3719        ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3720                                         &dai_link->codecs, &dai_link->num_codecs);
3721        if (ret < 0)
3722                return ret;
3723
3724        /* Parse the list */
3725        for_each_link_codecs(dai_link, index, component) {
3726                ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3727                if (ret)
3728                        goto err;
3729        }
3730        return 0;
3731err:
3732        snd_soc_of_put_dai_link_codecs(dai_link);
3733        dai_link->codecs = NULL;
3734        dai_link->num_codecs = 0;
3735        return ret;
3736}
3737EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs);
3738
3739/*
3740 * snd_soc_of_put_dai_link_cpus - Dereference device nodes in the codecs array
3741 * @dai_link: DAI link
3742 *
3743 * Dereference device nodes acquired by snd_soc_of_get_dai_link_cpus().
3744 */
3745void snd_soc_of_put_dai_link_cpus(struct snd_soc_dai_link *dai_link)
3746{
3747        struct snd_soc_dai_link_component *component;
3748        int index;
3749
3750        for_each_link_cpus(dai_link, index, component)
3751                __snd_soc_of_put_component(component);
3752}
3753EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_cpus);
3754
3755/*
3756 * snd_soc_of_get_dai_link_cpus - Parse a list of CPU DAIs in the devicetree
3757 * @dev: Card device
3758 * @of_node: Device node
3759 * @dai_link: DAI link
3760 *
3761 * Is analogous to snd_soc_of_get_dai_link_codecs but parses a list of CPU DAIs
3762 * instead.
3763 *
3764 * Returns 0 for success
3765 */
3766int snd_soc_of_get_dai_link_cpus(struct device *dev,
3767                                 struct device_node *of_node,
3768                                 struct snd_soc_dai_link *dai_link)
3769{
3770        struct snd_soc_dai_link_component *component;
3771        int index, ret;
3772
3773        /* Count the number of CPUs */
3774        ret = __snd_soc_of_get_dai_link_component_alloc(dev, of_node,
3775                                         &dai_link->cpus, &dai_link->num_cpus);
3776        if (ret < 0)
3777                return ret;
3778
3779        /* Parse the list */
3780        for_each_link_cpus(dai_link, index, component) {
3781                ret = snd_soc_of_get_dlc(of_node, NULL, component, index);
3782                if (ret)
3783                        goto err;
3784        }
3785        return 0;
3786err:
3787        snd_soc_of_put_dai_link_cpus(dai_link);
3788        dai_link->cpus = NULL;
3789        dai_link->num_cpus = 0;
3790        return ret;
3791}
3792EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_cpus);
3793
3794static int __init snd_soc_init(void)
3795{
3796        int ret;
3797
3798        snd_soc_debugfs_init();
3799        ret = snd_soc_util_init();
3800        if (ret)
3801                goto err_util_init;
3802
3803        ret = platform_driver_register(&soc_driver);
3804        if (ret)
3805                goto err_register;
3806        return 0;
3807
3808err_register:
3809        snd_soc_util_exit();
3810err_util_init:
3811        snd_soc_debugfs_exit();
3812        return ret;
3813}
3814module_init(snd_soc_init);
3815
3816static void __exit snd_soc_exit(void)
3817{
3818        snd_soc_util_exit();
3819        snd_soc_debugfs_exit();
3820
3821        platform_driver_unregister(&soc_driver);
3822}
3823module_exit(snd_soc_exit);
3824
3825/* Module information */
3826MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3827MODULE_DESCRIPTION("ALSA SoC Core");
3828MODULE_LICENSE("GPL");
3829MODULE_ALIAS("platform:soc-audio");
3830