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