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