linux/sound/soc/soc-dapm.c
<<
>>
Prefs
   1/*
   2 * soc-dapm.c  --  ALSA SoC Dynamic Audio Power Management
   3 *
   4 * Copyright 2005 Wolfson Microelectronics PLC.
   5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
   6 *
   7 *  This program is free software; you can redistribute  it and/or modify it
   8 *  under  the terms of  the GNU General  Public License as published by the
   9 *  Free Software Foundation;  either version 2 of the  License, or (at your
  10 *  option) any later version.
  11 *
  12 *  Features:
  13 *    o Changes power status of internal codec blocks depending on the
  14 *      dynamic configuration of codec internal audio paths and active
  15 *      DACs/ADCs.
  16 *    o Platform power domain - can support external components i.e. amps and
  17 *      mic/headphone insertion events.
  18 *    o Automatic Mic Bias support
  19 *    o Jack insertion power event initiation - e.g. hp insertion will enable
  20 *      sinks, dacs, etc
  21 *    o Delayed power down of audio subsystem to reduce pops between a quick
  22 *      device reopen.
  23 *
  24 */
  25
  26#include <linux/module.h>
  27#include <linux/moduleparam.h>
  28#include <linux/init.h>
  29#include <linux/async.h>
  30#include <linux/delay.h>
  31#include <linux/pm.h>
  32#include <linux/bitops.h>
  33#include <linux/platform_device.h>
  34#include <linux/jiffies.h>
  35#include <linux/debugfs.h>
  36#include <linux/pm_runtime.h>
  37#include <linux/regulator/consumer.h>
  38#include <linux/clk.h>
  39#include <linux/slab.h>
  40#include <sound/core.h>
  41#include <sound/pcm.h>
  42#include <sound/pcm_params.h>
  43#include <sound/soc.h>
  44#include <sound/initval.h>
  45
  46#include <trace/events/asoc.h>
  47
  48#define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
  49
  50/* dapm power sequences - make this per codec in the future */
  51static int dapm_up_seq[] = {
  52        [snd_soc_dapm_pre] = 0,
  53        [snd_soc_dapm_supply] = 1,
  54        [snd_soc_dapm_regulator_supply] = 1,
  55        [snd_soc_dapm_clock_supply] = 1,
  56        [snd_soc_dapm_micbias] = 2,
  57        [snd_soc_dapm_dai_link] = 2,
  58        [snd_soc_dapm_dai_in] = 3,
  59        [snd_soc_dapm_dai_out] = 3,
  60        [snd_soc_dapm_aif_in] = 3,
  61        [snd_soc_dapm_aif_out] = 3,
  62        [snd_soc_dapm_mic] = 4,
  63        [snd_soc_dapm_mux] = 5,
  64        [snd_soc_dapm_virt_mux] = 5,
  65        [snd_soc_dapm_value_mux] = 5,
  66        [snd_soc_dapm_dac] = 6,
  67        [snd_soc_dapm_switch] = 7,
  68        [snd_soc_dapm_mixer] = 7,
  69        [snd_soc_dapm_mixer_named_ctl] = 7,
  70        [snd_soc_dapm_pga] = 8,
  71        [snd_soc_dapm_adc] = 9,
  72        [snd_soc_dapm_out_drv] = 10,
  73        [snd_soc_dapm_hp] = 10,
  74        [snd_soc_dapm_spk] = 10,
  75        [snd_soc_dapm_line] = 10,
  76        [snd_soc_dapm_post] = 11,
  77};
  78
  79static int dapm_down_seq[] = {
  80        [snd_soc_dapm_pre] = 0,
  81        [snd_soc_dapm_adc] = 1,
  82        [snd_soc_dapm_hp] = 2,
  83        [snd_soc_dapm_spk] = 2,
  84        [snd_soc_dapm_line] = 2,
  85        [snd_soc_dapm_out_drv] = 2,
  86        [snd_soc_dapm_pga] = 4,
  87        [snd_soc_dapm_switch] = 5,
  88        [snd_soc_dapm_mixer_named_ctl] = 5,
  89        [snd_soc_dapm_mixer] = 5,
  90        [snd_soc_dapm_dac] = 6,
  91        [snd_soc_dapm_mic] = 7,
  92        [snd_soc_dapm_micbias] = 8,
  93        [snd_soc_dapm_mux] = 9,
  94        [snd_soc_dapm_virt_mux] = 9,
  95        [snd_soc_dapm_value_mux] = 9,
  96        [snd_soc_dapm_aif_in] = 10,
  97        [snd_soc_dapm_aif_out] = 10,
  98        [snd_soc_dapm_dai_in] = 10,
  99        [snd_soc_dapm_dai_out] = 10,
 100        [snd_soc_dapm_dai_link] = 11,
 101        [snd_soc_dapm_clock_supply] = 12,
 102        [snd_soc_dapm_regulator_supply] = 12,
 103        [snd_soc_dapm_supply] = 12,
 104        [snd_soc_dapm_post] = 13,
 105};
 106
 107static void pop_wait(u32 pop_time)
 108{
 109        if (pop_time)
 110                schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
 111}
 112
 113static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
 114{
 115        va_list args;
 116        char *buf;
 117
 118        if (!pop_time)
 119                return;
 120
 121        buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 122        if (buf == NULL)
 123                return;
 124
 125        va_start(args, fmt);
 126        vsnprintf(buf, PAGE_SIZE, fmt, args);
 127        dev_info(dev, "%s", buf);
 128        va_end(args);
 129
 130        kfree(buf);
 131}
 132
 133static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
 134{
 135        return !list_empty(&w->dirty);
 136}
 137
 138void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
 139{
 140        if (!dapm_dirty_widget(w)) {
 141                dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
 142                         w->name, reason);
 143                list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
 144        }
 145}
 146EXPORT_SYMBOL_GPL(dapm_mark_dirty);
 147
 148void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
 149{
 150        struct snd_soc_card *card = dapm->card;
 151        struct snd_soc_dapm_widget *w;
 152
 153        mutex_lock(&card->dapm_mutex);
 154
 155        list_for_each_entry(w, &card->widgets, list) {
 156                switch (w->id) {
 157                case snd_soc_dapm_input:
 158                case snd_soc_dapm_output:
 159                        dapm_mark_dirty(w, "Rechecking inputs and outputs");
 160                        break;
 161                default:
 162                        break;
 163                }
 164        }
 165
 166        mutex_unlock(&card->dapm_mutex);
 167}
 168EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
 169
 170/* create a new dapm widget */
 171static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
 172        const struct snd_soc_dapm_widget *_widget)
 173{
 174        return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
 175}
 176
 177/* get snd_card from DAPM context */
 178static inline struct snd_card *dapm_get_snd_card(
 179        struct snd_soc_dapm_context *dapm)
 180{
 181        if (dapm->codec)
 182                return dapm->codec->card->snd_card;
 183        else if (dapm->platform)
 184                return dapm->platform->card->snd_card;
 185        else
 186                BUG();
 187
 188        /* unreachable */
 189        return NULL;
 190}
 191
 192/* get soc_card from DAPM context */
 193static inline struct snd_soc_card *dapm_get_soc_card(
 194                struct snd_soc_dapm_context *dapm)
 195{
 196        if (dapm->codec)
 197                return dapm->codec->card;
 198        else if (dapm->platform)
 199                return dapm->platform->card;
 200        else
 201                BUG();
 202
 203        /* unreachable */
 204        return NULL;
 205}
 206
 207static void dapm_reset(struct snd_soc_card *card)
 208{
 209        struct snd_soc_dapm_widget *w;
 210
 211        memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
 212
 213        list_for_each_entry(w, &card->widgets, list) {
 214                w->power_checked = false;
 215                w->inputs = -1;
 216                w->outputs = -1;
 217        }
 218}
 219
 220static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
 221{
 222        if (w->codec)
 223                return snd_soc_read(w->codec, reg);
 224        else if (w->platform)
 225                return snd_soc_platform_read(w->platform, reg);
 226
 227        dev_err(w->dapm->dev, "ASoC: no valid widget read method\n");
 228        return -1;
 229}
 230
 231static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
 232{
 233        if (w->codec)
 234                return snd_soc_write(w->codec, reg, val);
 235        else if (w->platform)
 236                return snd_soc_platform_write(w->platform, reg, val);
 237
 238        dev_err(w->dapm->dev, "ASoC: no valid widget write method\n");
 239        return -1;
 240}
 241
 242static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
 243{
 244        if (w->codec && !w->codec->using_regmap)
 245                mutex_lock(&w->codec->mutex);
 246        else if (w->platform)
 247                mutex_lock(&w->platform->mutex);
 248}
 249
 250static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
 251{
 252        if (w->codec && !w->codec->using_regmap)
 253                mutex_unlock(&w->codec->mutex);
 254        else if (w->platform)
 255                mutex_unlock(&w->platform->mutex);
 256}
 257
 258static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
 259        unsigned short reg, unsigned int mask, unsigned int value)
 260{
 261        bool change;
 262        unsigned int old, new;
 263        int ret;
 264
 265        if (w->codec && w->codec->using_regmap) {
 266                ret = regmap_update_bits_check(w->codec->control_data,
 267                                               reg, mask, value, &change);
 268                if (ret != 0)
 269                        return ret;
 270        } else {
 271                soc_widget_lock(w);
 272                ret = soc_widget_read(w, reg);
 273                if (ret < 0) {
 274                        soc_widget_unlock(w);
 275                        return ret;
 276                }
 277
 278                old = ret;
 279                new = (old & ~mask) | (value & mask);
 280                change = old != new;
 281                if (change) {
 282                        ret = soc_widget_write(w, reg, new);
 283                        if (ret < 0) {
 284                                soc_widget_unlock(w);
 285                                return ret;
 286                        }
 287                }
 288                soc_widget_unlock(w);
 289        }
 290
 291        return change;
 292}
 293
 294/**
 295 * snd_soc_dapm_set_bias_level - set the bias level for the system
 296 * @dapm: DAPM context
 297 * @level: level to configure
 298 *
 299 * Configure the bias (power) levels for the SoC audio device.
 300 *
 301 * Returns 0 for success else error.
 302 */
 303static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
 304                                       enum snd_soc_bias_level level)
 305{
 306        struct snd_soc_card *card = dapm->card;
 307        int ret = 0;
 308
 309        trace_snd_soc_bias_level_start(card, level);
 310
 311        if (card && card->set_bias_level)
 312                ret = card->set_bias_level(card, dapm, level);
 313        if (ret != 0)
 314                goto out;
 315
 316        if (dapm->codec) {
 317                if (dapm->codec->driver->set_bias_level)
 318                        ret = dapm->codec->driver->set_bias_level(dapm->codec,
 319                                                                  level);
 320                else
 321                        dapm->bias_level = level;
 322        } else if (!card || dapm != &card->dapm) {
 323                dapm->bias_level = level;
 324        }
 325
 326        if (ret != 0)
 327                goto out;
 328
 329        if (card && card->set_bias_level_post)
 330                ret = card->set_bias_level_post(card, dapm, level);
 331out:
 332        trace_snd_soc_bias_level_done(card, level);
 333
 334        return ret;
 335}
 336
 337/* set up initial codec paths */
 338static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
 339        struct snd_soc_dapm_path *p, int i)
 340{
 341        switch (w->id) {
 342        case snd_soc_dapm_switch:
 343        case snd_soc_dapm_mixer:
 344        case snd_soc_dapm_mixer_named_ctl: {
 345                int val;
 346                struct soc_mixer_control *mc = (struct soc_mixer_control *)
 347                        w->kcontrol_news[i].private_value;
 348                unsigned int reg = mc->reg;
 349                unsigned int shift = mc->shift;
 350                int max = mc->max;
 351                unsigned int mask = (1 << fls(max)) - 1;
 352                unsigned int invert = mc->invert;
 353
 354                val = soc_widget_read(w, reg);
 355                val = (val >> shift) & mask;
 356                if (invert)
 357                        val = max - val;
 358
 359                p->connect = !!val;
 360        }
 361        break;
 362        case snd_soc_dapm_mux: {
 363                struct soc_enum *e = (struct soc_enum *)
 364                        w->kcontrol_news[i].private_value;
 365                int val, item;
 366
 367                val = soc_widget_read(w, e->reg);
 368                item = (val >> e->shift_l) & e->mask;
 369
 370                if (item < e->max && !strcmp(p->name, e->texts[item]))
 371                        p->connect = 1;
 372                else
 373                        p->connect = 0;
 374        }
 375        break;
 376        case snd_soc_dapm_virt_mux: {
 377                struct soc_enum *e = (struct soc_enum *)
 378                        w->kcontrol_news[i].private_value;
 379
 380                p->connect = 0;
 381                /* since a virtual mux has no backing registers to
 382                 * decide which path to connect, it will try to match
 383                 * with the first enumeration.  This is to ensure
 384                 * that the default mux choice (the first) will be
 385                 * correctly powered up during initialization.
 386                 */
 387                if (!strcmp(p->name, e->texts[0]))
 388                        p->connect = 1;
 389        }
 390        break;
 391        case snd_soc_dapm_value_mux: {
 392                struct soc_enum *e = (struct soc_enum *)
 393                        w->kcontrol_news[i].private_value;
 394                int val, item;
 395
 396                val = soc_widget_read(w, e->reg);
 397                val = (val >> e->shift_l) & e->mask;
 398                for (item = 0; item < e->max; item++) {
 399                        if (val == e->values[item])
 400                                break;
 401                }
 402
 403                if (item < e->max && !strcmp(p->name, e->texts[item]))
 404                        p->connect = 1;
 405                else
 406                        p->connect = 0;
 407        }
 408        break;
 409        /* does not affect routing - always connected */
 410        case snd_soc_dapm_pga:
 411        case snd_soc_dapm_out_drv:
 412        case snd_soc_dapm_output:
 413        case snd_soc_dapm_adc:
 414        case snd_soc_dapm_input:
 415        case snd_soc_dapm_siggen:
 416        case snd_soc_dapm_dac:
 417        case snd_soc_dapm_micbias:
 418        case snd_soc_dapm_vmid:
 419        case snd_soc_dapm_supply:
 420        case snd_soc_dapm_regulator_supply:
 421        case snd_soc_dapm_clock_supply:
 422        case snd_soc_dapm_aif_in:
 423        case snd_soc_dapm_aif_out:
 424        case snd_soc_dapm_dai_in:
 425        case snd_soc_dapm_dai_out:
 426        case snd_soc_dapm_hp:
 427        case snd_soc_dapm_mic:
 428        case snd_soc_dapm_spk:
 429        case snd_soc_dapm_line:
 430        case snd_soc_dapm_dai_link:
 431                p->connect = 1;
 432        break;
 433        /* does affect routing - dynamically connected */
 434        case snd_soc_dapm_pre:
 435        case snd_soc_dapm_post:
 436                p->connect = 0;
 437        break;
 438        }
 439}
 440
 441/* connect mux widget to its interconnecting audio paths */
 442static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
 443        struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
 444        struct snd_soc_dapm_path *path, const char *control_name,
 445        const struct snd_kcontrol_new *kcontrol)
 446{
 447        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
 448        int i;
 449
 450        for (i = 0; i < e->max; i++) {
 451                if (!(strcmp(control_name, e->texts[i]))) {
 452                        list_add(&path->list, &dapm->card->paths);
 453                        list_add(&path->list_sink, &dest->sources);
 454                        list_add(&path->list_source, &src->sinks);
 455                        path->name = (char*)e->texts[i];
 456                        dapm_set_path_status(dest, path, 0);
 457                        return 0;
 458                }
 459        }
 460
 461        return -ENODEV;
 462}
 463
 464/* connect mixer widget to its interconnecting audio paths */
 465static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
 466        struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
 467        struct snd_soc_dapm_path *path, const char *control_name)
 468{
 469        int i;
 470
 471        /* search for mixer kcontrol */
 472        for (i = 0; i < dest->num_kcontrols; i++) {
 473                if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
 474                        list_add(&path->list, &dapm->card->paths);
 475                        list_add(&path->list_sink, &dest->sources);
 476                        list_add(&path->list_source, &src->sinks);
 477                        path->name = dest->kcontrol_news[i].name;
 478                        dapm_set_path_status(dest, path, i);
 479                        return 0;
 480                }
 481        }
 482        return -ENODEV;
 483}
 484
 485static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
 486        struct snd_soc_dapm_widget *kcontrolw,
 487        const struct snd_kcontrol_new *kcontrol_new,
 488        struct snd_kcontrol **kcontrol)
 489{
 490        struct snd_soc_dapm_widget *w;
 491        int i;
 492
 493        *kcontrol = NULL;
 494
 495        list_for_each_entry(w, &dapm->card->widgets, list) {
 496                if (w == kcontrolw || w->dapm != kcontrolw->dapm)
 497                        continue;
 498                for (i = 0; i < w->num_kcontrols; i++) {
 499                        if (&w->kcontrol_news[i] == kcontrol_new) {
 500                                if (w->kcontrols)
 501                                        *kcontrol = w->kcontrols[i];
 502                                return 1;
 503                        }
 504                }
 505        }
 506
 507        return 0;
 508}
 509
 510static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
 511{
 512        kfree(kctl->private_data);
 513}
 514
 515/*
 516 * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
 517 * create it. Either way, add the widget into the control's widget list
 518 */
 519static int dapm_create_or_share_mixmux_kcontrol(struct snd_soc_dapm_widget *w,
 520        int kci, struct snd_soc_dapm_path *path)
 521{
 522        struct snd_soc_dapm_context *dapm = w->dapm;
 523        struct snd_card *card = dapm->card->snd_card;
 524        const char *prefix;
 525        size_t prefix_len;
 526        int shared;
 527        struct snd_kcontrol *kcontrol;
 528        struct snd_soc_dapm_widget_list *wlist;
 529        int wlistentries;
 530        size_t wlistsize;
 531        bool wname_in_long_name, kcname_in_long_name;
 532        char *long_name;
 533        const char *name;
 534        int ret;
 535
 536        if (dapm->codec)
 537                prefix = dapm->codec->name_prefix;
 538        else
 539                prefix = NULL;
 540
 541        if (prefix)
 542                prefix_len = strlen(prefix) + 1;
 543        else
 544                prefix_len = 0;
 545
 546        shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
 547                                         &kcontrol);
 548
 549        if (kcontrol) {
 550                wlist = kcontrol->private_data;
 551                wlistentries = wlist->num_widgets + 1;
 552        } else {
 553                wlist = NULL;
 554                wlistentries = 1;
 555        }
 556
 557        wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 558                        wlistentries * sizeof(struct snd_soc_dapm_widget *);
 559        wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
 560        if (wlist == NULL) {
 561                dev_err(dapm->dev, "ASoC: can't allocate widget list for %s\n",
 562                        w->name);
 563                return -ENOMEM;
 564        }
 565        wlist->num_widgets = wlistentries;
 566        wlist->widgets[wlistentries - 1] = w;
 567
 568        if (!kcontrol) {
 569                if (shared) {
 570                        wname_in_long_name = false;
 571                        kcname_in_long_name = true;
 572                } else {
 573                        switch (w->id) {
 574                        case snd_soc_dapm_switch:
 575                        case snd_soc_dapm_mixer:
 576                                wname_in_long_name = true;
 577                                kcname_in_long_name = true;
 578                                break;
 579                        case snd_soc_dapm_mixer_named_ctl:
 580                                wname_in_long_name = false;
 581                                kcname_in_long_name = true;
 582                                break;
 583                        case snd_soc_dapm_mux:
 584                        case snd_soc_dapm_virt_mux:
 585                        case snd_soc_dapm_value_mux:
 586                                wname_in_long_name = true;
 587                                kcname_in_long_name = false;
 588                                break;
 589                        default:
 590                                kfree(wlist);
 591                                return -EINVAL;
 592                        }
 593                }
 594
 595                if (wname_in_long_name && kcname_in_long_name) {
 596                        /*
 597                         * The control will get a prefix from the control
 598                         * creation process but we're also using the same
 599                         * prefix for widgets so cut the prefix off the
 600                         * front of the widget name.
 601                         */
 602                        long_name = kasprintf(GFP_KERNEL, "%s %s",
 603                                 w->name + prefix_len,
 604                                 w->kcontrol_news[kci].name);
 605                        if (long_name == NULL) {
 606                                kfree(wlist);
 607                                return -ENOMEM;
 608                        }
 609
 610                        name = long_name;
 611                } else if (wname_in_long_name) {
 612                        long_name = NULL;
 613                        name = w->name + prefix_len;
 614                } else {
 615                        long_name = NULL;
 616                        name = w->kcontrol_news[kci].name;
 617                }
 618
 619                kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], wlist, name,
 620                                        prefix);
 621                kcontrol->private_free = dapm_kcontrol_free;
 622                kfree(long_name);
 623                ret = snd_ctl_add(card, kcontrol);
 624                if (ret < 0) {
 625                        dev_err(dapm->dev,
 626                                "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
 627                                w->name, name, ret);
 628                        kfree(wlist);
 629                        return ret;
 630                }
 631        }
 632
 633        kcontrol->private_data = wlist;
 634        w->kcontrols[kci] = kcontrol;
 635        path->kcontrol = kcontrol;
 636
 637        return 0;
 638}
 639
 640/* create new dapm mixer control */
 641static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
 642{
 643        int i, ret;
 644        struct snd_soc_dapm_path *path;
 645
 646        /* add kcontrol */
 647        for (i = 0; i < w->num_kcontrols; i++) {
 648                /* match name */
 649                list_for_each_entry(path, &w->sources, list_sink) {
 650                        /* mixer/mux paths name must match control name */
 651                        if (path->name != (char *)w->kcontrol_news[i].name)
 652                                continue;
 653
 654                        if (w->kcontrols[i]) {
 655                                path->kcontrol = w->kcontrols[i];
 656                                continue;
 657                        }
 658
 659                        ret = dapm_create_or_share_mixmux_kcontrol(w, i, path);
 660                        if (ret < 0)
 661                                return ret;
 662                }
 663        }
 664
 665        return 0;
 666}
 667
 668/* create new dapm mux control */
 669static int dapm_new_mux(struct snd_soc_dapm_widget *w)
 670{
 671        struct snd_soc_dapm_context *dapm = w->dapm;
 672        struct snd_soc_dapm_path *path;
 673        int ret;
 674
 675        if (w->num_kcontrols != 1) {
 676                dev_err(dapm->dev,
 677                        "ASoC: mux %s has incorrect number of controls\n",
 678                        w->name);
 679                return -EINVAL;
 680        }
 681
 682        if (list_empty(&w->sources)) {
 683                dev_err(dapm->dev, "ASoC: mux %s has no paths\n", w->name);
 684                return -EINVAL;
 685        }
 686
 687        path = list_first_entry(&w->sources, struct snd_soc_dapm_path,
 688                                list_sink);
 689
 690        ret = dapm_create_or_share_mixmux_kcontrol(w, 0, path);
 691        if (ret < 0)
 692                return ret;
 693
 694        list_for_each_entry(path, &w->sources, list_sink)
 695                path->kcontrol = w->kcontrols[0];
 696
 697        return 0;
 698}
 699
 700/* create new dapm volume control */
 701static int dapm_new_pga(struct snd_soc_dapm_widget *w)
 702{
 703        if (w->num_kcontrols)
 704                dev_err(w->dapm->dev,
 705                        "ASoC: PGA controls not supported: '%s'\n", w->name);
 706
 707        return 0;
 708}
 709
 710/* reset 'walked' bit for each dapm path */
 711static void dapm_clear_walk_output(struct snd_soc_dapm_context *dapm,
 712                                   struct list_head *sink)
 713{
 714        struct snd_soc_dapm_path *p;
 715
 716        list_for_each_entry(p, sink, list_source) {
 717                if (p->walked) {
 718                        p->walked = 0;
 719                        dapm_clear_walk_output(dapm, &p->sink->sinks);
 720                }
 721        }
 722}
 723
 724static void dapm_clear_walk_input(struct snd_soc_dapm_context *dapm,
 725                                  struct list_head *source)
 726{
 727        struct snd_soc_dapm_path *p;
 728
 729        list_for_each_entry(p, source, list_sink) {
 730                if (p->walked) {
 731                        p->walked = 0;
 732                        dapm_clear_walk_input(dapm, &p->source->sources);
 733                }
 734        }
 735}
 736
 737
 738/* We implement power down on suspend by checking the power state of
 739 * the ALSA card - when we are suspending the ALSA state for the card
 740 * is set to D3.
 741 */
 742static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
 743{
 744        int level = snd_power_get_state(widget->dapm->card->snd_card);
 745
 746        switch (level) {
 747        case SNDRV_CTL_POWER_D3hot:
 748        case SNDRV_CTL_POWER_D3cold:
 749                if (widget->ignore_suspend)
 750                        dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
 751                                widget->name);
 752                return widget->ignore_suspend;
 753        default:
 754                return 1;
 755        }
 756}
 757
 758/* add widget to list if it's not already in the list */
 759static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
 760        struct snd_soc_dapm_widget *w)
 761{
 762        struct snd_soc_dapm_widget_list *wlist;
 763        int wlistsize, wlistentries, i;
 764
 765        if (*list == NULL)
 766                return -EINVAL;
 767
 768        wlist = *list;
 769
 770        /* is this widget already in the list */
 771        for (i = 0; i < wlist->num_widgets; i++) {
 772                if (wlist->widgets[i] == w)
 773                        return 0;
 774        }
 775
 776        /* allocate some new space */
 777        wlistentries = wlist->num_widgets + 1;
 778        wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
 779                        wlistentries * sizeof(struct snd_soc_dapm_widget *);
 780        *list = krealloc(wlist, wlistsize, GFP_KERNEL);
 781        if (*list == NULL) {
 782                dev_err(w->dapm->dev, "ASoC: can't allocate widget list for %s\n",
 783                        w->name);
 784                return -ENOMEM;
 785        }
 786        wlist = *list;
 787
 788        /* insert the widget */
 789        dev_dbg(w->dapm->dev, "ASoC: added %s in widget list pos %d\n",
 790                        w->name, wlist->num_widgets);
 791
 792        wlist->widgets[wlist->num_widgets] = w;
 793        wlist->num_widgets++;
 794        return 1;
 795}
 796
 797/*
 798 * Recursively check for a completed path to an active or physically connected
 799 * output widget. Returns number of complete paths.
 800 */
 801static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
 802        struct snd_soc_dapm_widget_list **list)
 803{
 804        struct snd_soc_dapm_path *path;
 805        int con = 0;
 806
 807        if (widget->outputs >= 0)
 808                return widget->outputs;
 809
 810        DAPM_UPDATE_STAT(widget, path_checks);
 811
 812        switch (widget->id) {
 813        case snd_soc_dapm_supply:
 814        case snd_soc_dapm_regulator_supply:
 815        case snd_soc_dapm_clock_supply:
 816                return 0;
 817        default:
 818                break;
 819        }
 820
 821        switch (widget->id) {
 822        case snd_soc_dapm_adc:
 823        case snd_soc_dapm_aif_out:
 824        case snd_soc_dapm_dai_out:
 825                if (widget->active) {
 826                        widget->outputs = snd_soc_dapm_suspend_check(widget);
 827                        return widget->outputs;
 828                }
 829        default:
 830                break;
 831        }
 832
 833        if (widget->connected) {
 834                /* connected pin ? */
 835                if (widget->id == snd_soc_dapm_output && !widget->ext) {
 836                        widget->outputs = snd_soc_dapm_suspend_check(widget);
 837                        return widget->outputs;
 838                }
 839
 840                /* connected jack or spk ? */
 841                if (widget->id == snd_soc_dapm_hp ||
 842                    widget->id == snd_soc_dapm_spk ||
 843                    (widget->id == snd_soc_dapm_line &&
 844                     !list_empty(&widget->sources))) {
 845                        widget->outputs = snd_soc_dapm_suspend_check(widget);
 846                        return widget->outputs;
 847                }
 848        }
 849
 850        list_for_each_entry(path, &widget->sinks, list_source) {
 851                DAPM_UPDATE_STAT(widget, neighbour_checks);
 852
 853                if (path->weak)
 854                        continue;
 855
 856                if (path->walking)
 857                        return 1;
 858
 859                if (path->walked)
 860                        continue;
 861
 862                trace_snd_soc_dapm_output_path(widget, path);
 863
 864                if (path->sink && path->connect) {
 865                        path->walked = 1;
 866                        path->walking = 1;
 867
 868                        /* do we need to add this widget to the list ? */
 869                        if (list) {
 870                                int err;
 871                                err = dapm_list_add_widget(list, path->sink);
 872                                if (err < 0) {
 873                                        dev_err(widget->dapm->dev,
 874                                                "ASoC: could not add widget %s\n",
 875                                                widget->name);
 876                                        path->walking = 0;
 877                                        return con;
 878                                }
 879                        }
 880
 881                        con += is_connected_output_ep(path->sink, list);
 882
 883                        path->walking = 0;
 884                }
 885        }
 886
 887        widget->outputs = con;
 888
 889        return con;
 890}
 891
 892/*
 893 * Recursively check for a completed path to an active or physically connected
 894 * input widget. Returns number of complete paths.
 895 */
 896static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
 897        struct snd_soc_dapm_widget_list **list)
 898{
 899        struct snd_soc_dapm_path *path;
 900        int con = 0;
 901
 902        if (widget->inputs >= 0)
 903                return widget->inputs;
 904
 905        DAPM_UPDATE_STAT(widget, path_checks);
 906
 907        switch (widget->id) {
 908        case snd_soc_dapm_supply:
 909        case snd_soc_dapm_regulator_supply:
 910        case snd_soc_dapm_clock_supply:
 911                return 0;
 912        default:
 913                break;
 914        }
 915
 916        /* active stream ? */
 917        switch (widget->id) {
 918        case snd_soc_dapm_dac:
 919        case snd_soc_dapm_aif_in:
 920        case snd_soc_dapm_dai_in:
 921                if (widget->active) {
 922                        widget->inputs = snd_soc_dapm_suspend_check(widget);
 923                        return widget->inputs;
 924                }
 925        default:
 926                break;
 927        }
 928
 929        if (widget->connected) {
 930                /* connected pin ? */
 931                if (widget->id == snd_soc_dapm_input && !widget->ext) {
 932                        widget->inputs = snd_soc_dapm_suspend_check(widget);
 933                        return widget->inputs;
 934                }
 935
 936                /* connected VMID/Bias for lower pops */
 937                if (widget->id == snd_soc_dapm_vmid) {
 938                        widget->inputs = snd_soc_dapm_suspend_check(widget);
 939                        return widget->inputs;
 940                }
 941
 942                /* connected jack ? */
 943                if (widget->id == snd_soc_dapm_mic ||
 944                    (widget->id == snd_soc_dapm_line &&
 945                     !list_empty(&widget->sinks))) {
 946                        widget->inputs = snd_soc_dapm_suspend_check(widget);
 947                        return widget->inputs;
 948                }
 949
 950                /* signal generator */
 951                if (widget->id == snd_soc_dapm_siggen) {
 952                        widget->inputs = snd_soc_dapm_suspend_check(widget);
 953                        return widget->inputs;
 954                }
 955        }
 956
 957        list_for_each_entry(path, &widget->sources, list_sink) {
 958                DAPM_UPDATE_STAT(widget, neighbour_checks);
 959
 960                if (path->weak)
 961                        continue;
 962
 963                if (path->walking)
 964                        return 1;
 965
 966                if (path->walked)
 967                        continue;
 968
 969                trace_snd_soc_dapm_input_path(widget, path);
 970
 971                if (path->source && path->connect) {
 972                        path->walked = 1;
 973                        path->walking = 1;
 974
 975                        /* do we need to add this widget to the list ? */
 976                        if (list) {
 977                                int err;
 978                                err = dapm_list_add_widget(list, path->source);
 979                                if (err < 0) {
 980                                        dev_err(widget->dapm->dev,
 981                                                "ASoC: could not add widget %s\n",
 982                                                widget->name);
 983                                        path->walking = 0;
 984                                        return con;
 985                                }
 986                        }
 987
 988                        con += is_connected_input_ep(path->source, list);
 989
 990                        path->walking = 0;
 991                }
 992        }
 993
 994        widget->inputs = con;
 995
 996        return con;
 997}
 998
 999/**
1000 * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
1001 * @dai: the soc DAI.
1002 * @stream: stream direction.
1003 * @list: list of active widgets for this stream.
1004 *
1005 * Queries DAPM graph as to whether an valid audio stream path exists for
1006 * the initial stream specified by name. This takes into account
1007 * current mixer and mux kcontrol settings. Creates list of valid widgets.
1008 *
1009 * Returns the number of valid paths or negative error.
1010 */
1011int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
1012        struct snd_soc_dapm_widget_list **list)
1013{
1014        struct snd_soc_card *card = dai->card;
1015        int paths;
1016
1017        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1018        dapm_reset(card);
1019
1020        if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1021                paths = is_connected_output_ep(dai->playback_widget, list);
1022                dapm_clear_walk_output(&card->dapm,
1023                                       &dai->playback_widget->sinks);
1024        } else {
1025                paths = is_connected_input_ep(dai->capture_widget, list);
1026                dapm_clear_walk_input(&card->dapm,
1027                                      &dai->capture_widget->sources);
1028        }
1029
1030        trace_snd_soc_dapm_connected(paths, stream);
1031        mutex_unlock(&card->dapm_mutex);
1032
1033        return paths;
1034}
1035
1036/*
1037 * Handler for generic register modifier widget.
1038 */
1039int dapm_reg_event(struct snd_soc_dapm_widget *w,
1040                   struct snd_kcontrol *kcontrol, int event)
1041{
1042        unsigned int val;
1043
1044        if (SND_SOC_DAPM_EVENT_ON(event))
1045                val = w->on_val;
1046        else
1047                val = w->off_val;
1048
1049        soc_widget_update_bits_locked(w, -(w->reg + 1),
1050                            w->mask << w->shift, val << w->shift);
1051
1052        return 0;
1053}
1054EXPORT_SYMBOL_GPL(dapm_reg_event);
1055
1056/*
1057 * Handler for regulator supply widget.
1058 */
1059int dapm_regulator_event(struct snd_soc_dapm_widget *w,
1060                   struct snd_kcontrol *kcontrol, int event)
1061{
1062        int ret;
1063
1064        if (SND_SOC_DAPM_EVENT_ON(event)) {
1065                if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1066                        ret = regulator_allow_bypass(w->regulator, false);
1067                        if (ret != 0)
1068                                dev_warn(w->dapm->dev,
1069                                         "ASoC: Failed to bypass %s: %d\n",
1070                                         w->name, ret);
1071                }
1072
1073                return regulator_enable(w->regulator);
1074        } else {
1075                if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
1076                        ret = regulator_allow_bypass(w->regulator, true);
1077                        if (ret != 0)
1078                                dev_warn(w->dapm->dev,
1079                                         "ASoC: Failed to unbypass %s: %d\n",
1080                                         w->name, ret);
1081                }
1082
1083                return regulator_disable_deferred(w->regulator, w->shift);
1084        }
1085}
1086EXPORT_SYMBOL_GPL(dapm_regulator_event);
1087
1088/*
1089 * Handler for clock supply widget.
1090 */
1091int dapm_clock_event(struct snd_soc_dapm_widget *w,
1092                   struct snd_kcontrol *kcontrol, int event)
1093{
1094        if (!w->clk)
1095                return -EIO;
1096
1097#ifdef CONFIG_HAVE_CLK
1098        if (SND_SOC_DAPM_EVENT_ON(event)) {
1099                return clk_prepare_enable(w->clk);
1100        } else {
1101                clk_disable_unprepare(w->clk);
1102                return 0;
1103        }
1104#endif
1105        return 0;
1106}
1107EXPORT_SYMBOL_GPL(dapm_clock_event);
1108
1109static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
1110{
1111        if (w->power_checked)
1112                return w->new_power;
1113
1114        if (w->force)
1115                w->new_power = 1;
1116        else
1117                w->new_power = w->power_check(w);
1118
1119        w->power_checked = true;
1120
1121        return w->new_power;
1122}
1123
1124/* Generic check to see if a widget should be powered.
1125 */
1126static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
1127{
1128        int in, out;
1129
1130        DAPM_UPDATE_STAT(w, power_checks);
1131
1132        in = is_connected_input_ep(w, NULL);
1133        dapm_clear_walk_input(w->dapm, &w->sources);
1134        out = is_connected_output_ep(w, NULL);
1135        dapm_clear_walk_output(w->dapm, &w->sinks);
1136        return out != 0 && in != 0;
1137}
1138
1139/* Check to see if an ADC has power */
1140static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
1141{
1142        int in;
1143
1144        DAPM_UPDATE_STAT(w, power_checks);
1145
1146        if (w->active) {
1147                in = is_connected_input_ep(w, NULL);
1148                dapm_clear_walk_input(w->dapm, &w->sources);
1149                return in != 0;
1150        } else {
1151                return dapm_generic_check_power(w);
1152        }
1153}
1154
1155/* Check to see if a DAC has power */
1156static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
1157{
1158        int out;
1159
1160        DAPM_UPDATE_STAT(w, power_checks);
1161
1162        if (w->active) {
1163                out = is_connected_output_ep(w, NULL);
1164                dapm_clear_walk_output(w->dapm, &w->sinks);
1165                return out != 0;
1166        } else {
1167                return dapm_generic_check_power(w);
1168        }
1169}
1170
1171/* Check to see if a power supply is needed */
1172static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
1173{
1174        struct snd_soc_dapm_path *path;
1175
1176        DAPM_UPDATE_STAT(w, power_checks);
1177
1178        /* Check if one of our outputs is connected */
1179        list_for_each_entry(path, &w->sinks, list_source) {
1180                DAPM_UPDATE_STAT(w, neighbour_checks);
1181
1182                if (path->weak)
1183                        continue;
1184
1185                if (path->connected &&
1186                    !path->connected(path->source, path->sink))
1187                        continue;
1188
1189                if (!path->sink)
1190                        continue;
1191
1192                if (dapm_widget_power_check(path->sink))
1193                        return 1;
1194        }
1195
1196        return 0;
1197}
1198
1199static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
1200{
1201        return 1;
1202}
1203
1204static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
1205                            struct snd_soc_dapm_widget *b,
1206                            bool power_up)
1207{
1208        int *sort;
1209
1210        if (power_up)
1211                sort = dapm_up_seq;
1212        else
1213                sort = dapm_down_seq;
1214
1215        if (sort[a->id] != sort[b->id])
1216                return sort[a->id] - sort[b->id];
1217        if (a->subseq != b->subseq) {
1218                if (power_up)
1219                        return a->subseq - b->subseq;
1220                else
1221                        return b->subseq - a->subseq;
1222        }
1223        if (a->reg != b->reg)
1224                return a->reg - b->reg;
1225        if (a->dapm != b->dapm)
1226                return (unsigned long)a->dapm - (unsigned long)b->dapm;
1227
1228        return 0;
1229}
1230
1231/* Insert a widget in order into a DAPM power sequence. */
1232static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
1233                            struct list_head *list,
1234                            bool power_up)
1235{
1236        struct snd_soc_dapm_widget *w;
1237
1238        list_for_each_entry(w, list, power_list)
1239                if (dapm_seq_compare(new_widget, w, power_up) < 0) {
1240                        list_add_tail(&new_widget->power_list, &w->power_list);
1241                        return;
1242                }
1243
1244        list_add_tail(&new_widget->power_list, list);
1245}
1246
1247static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
1248                                 struct snd_soc_dapm_widget *w, int event)
1249{
1250        struct snd_soc_card *card = dapm->card;
1251        const char *ev_name;
1252        int power, ret;
1253
1254        switch (event) {
1255        case SND_SOC_DAPM_PRE_PMU:
1256                ev_name = "PRE_PMU";
1257                power = 1;
1258                break;
1259        case SND_SOC_DAPM_POST_PMU:
1260                ev_name = "POST_PMU";
1261                power = 1;
1262                break;
1263        case SND_SOC_DAPM_PRE_PMD:
1264                ev_name = "PRE_PMD";
1265                power = 0;
1266                break;
1267        case SND_SOC_DAPM_POST_PMD:
1268                ev_name = "POST_PMD";
1269                power = 0;
1270                break;
1271        case SND_SOC_DAPM_WILL_PMU:
1272                ev_name = "WILL_PMU";
1273                power = 1;
1274                break;
1275        case SND_SOC_DAPM_WILL_PMD:
1276                ev_name = "WILL_PMD";
1277                power = 0;
1278                break;
1279        default:
1280                BUG();
1281                return;
1282        }
1283
1284        if (w->power != power)
1285                return;
1286
1287        if (w->event && (w->event_flags & event)) {
1288                pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
1289                        w->name, ev_name);
1290                trace_snd_soc_dapm_widget_event_start(w, event);
1291                ret = w->event(w, NULL, event);
1292                trace_snd_soc_dapm_widget_event_done(w, event);
1293                if (ret < 0)
1294                        dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
1295                               ev_name, w->name, ret);
1296        }
1297}
1298
1299/* Apply the coalesced changes from a DAPM sequence */
1300static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
1301                                   struct list_head *pending)
1302{
1303        struct snd_soc_card *card = dapm->card;
1304        struct snd_soc_dapm_widget *w;
1305        int reg, power;
1306        unsigned int value = 0;
1307        unsigned int mask = 0;
1308        unsigned int cur_mask;
1309
1310        reg = list_first_entry(pending, struct snd_soc_dapm_widget,
1311                               power_list)->reg;
1312
1313        list_for_each_entry(w, pending, power_list) {
1314                cur_mask = 1 << w->shift;
1315                BUG_ON(reg != w->reg);
1316
1317                if (w->invert)
1318                        power = !w->power;
1319                else
1320                        power = w->power;
1321
1322                mask |= cur_mask;
1323                if (power)
1324                        value |= cur_mask;
1325
1326                pop_dbg(dapm->dev, card->pop_time,
1327                        "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
1328                        w->name, reg, value, mask);
1329
1330                /* Check for events */
1331                dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
1332                dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
1333        }
1334
1335        if (reg >= 0) {
1336                /* Any widget will do, they should all be updating the
1337                 * same register.
1338                 */
1339                w = list_first_entry(pending, struct snd_soc_dapm_widget,
1340                                     power_list);
1341
1342                pop_dbg(dapm->dev, card->pop_time,
1343                        "pop test : Applying 0x%x/0x%x to %x in %dms\n",
1344                        value, mask, reg, card->pop_time);
1345                pop_wait(card->pop_time);
1346                soc_widget_update_bits_locked(w, reg, mask, value);
1347        }
1348
1349        list_for_each_entry(w, pending, power_list) {
1350                dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
1351                dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
1352        }
1353}
1354
1355/* Apply a DAPM power sequence.
1356 *
1357 * We walk over a pre-sorted list of widgets to apply power to.  In
1358 * order to minimise the number of writes to the device required
1359 * multiple widgets will be updated in a single write where possible.
1360 * Currently anything that requires more than a single write is not
1361 * handled.
1362 */
1363static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
1364                         struct list_head *list, int event, bool power_up)
1365{
1366        struct snd_soc_dapm_widget *w, *n;
1367        LIST_HEAD(pending);
1368        int cur_sort = -1;
1369        int cur_subseq = -1;
1370        int cur_reg = SND_SOC_NOPM;
1371        struct snd_soc_dapm_context *cur_dapm = NULL;
1372        int ret, i;
1373        int *sort;
1374
1375        if (power_up)
1376                sort = dapm_up_seq;
1377        else
1378                sort = dapm_down_seq;
1379
1380        list_for_each_entry_safe(w, n, list, power_list) {
1381                ret = 0;
1382
1383                /* Do we need to apply any queued changes? */
1384                if (sort[w->id] != cur_sort || w->reg != cur_reg ||
1385                    w->dapm != cur_dapm || w->subseq != cur_subseq) {
1386                        if (!list_empty(&pending))
1387                                dapm_seq_run_coalesced(cur_dapm, &pending);
1388
1389                        if (cur_dapm && cur_dapm->seq_notifier) {
1390                                for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1391                                        if (sort[i] == cur_sort)
1392                                                cur_dapm->seq_notifier(cur_dapm,
1393                                                                       i,
1394                                                                       cur_subseq);
1395                        }
1396
1397                        INIT_LIST_HEAD(&pending);
1398                        cur_sort = -1;
1399                        cur_subseq = INT_MIN;
1400                        cur_reg = SND_SOC_NOPM;
1401                        cur_dapm = NULL;
1402                }
1403
1404                switch (w->id) {
1405                case snd_soc_dapm_pre:
1406                        if (!w->event)
1407                                list_for_each_entry_safe_continue(w, n, list,
1408                                                                  power_list);
1409
1410                        if (event == SND_SOC_DAPM_STREAM_START)
1411                                ret = w->event(w,
1412                                               NULL, SND_SOC_DAPM_PRE_PMU);
1413                        else if (event == SND_SOC_DAPM_STREAM_STOP)
1414                                ret = w->event(w,
1415                                               NULL, SND_SOC_DAPM_PRE_PMD);
1416                        break;
1417
1418                case snd_soc_dapm_post:
1419                        if (!w->event)
1420                                list_for_each_entry_safe_continue(w, n, list,
1421                                                                  power_list);
1422
1423                        if (event == SND_SOC_DAPM_STREAM_START)
1424                                ret = w->event(w,
1425                                               NULL, SND_SOC_DAPM_POST_PMU);
1426                        else if (event == SND_SOC_DAPM_STREAM_STOP)
1427                                ret = w->event(w,
1428                                               NULL, SND_SOC_DAPM_POST_PMD);
1429                        break;
1430
1431                default:
1432                        /* Queue it up for application */
1433                        cur_sort = sort[w->id];
1434                        cur_subseq = w->subseq;
1435                        cur_reg = w->reg;
1436                        cur_dapm = w->dapm;
1437                        list_move(&w->power_list, &pending);
1438                        break;
1439                }
1440
1441                if (ret < 0)
1442                        dev_err(w->dapm->dev,
1443                                "ASoC: Failed to apply widget power: %d\n", ret);
1444        }
1445
1446        if (!list_empty(&pending))
1447                dapm_seq_run_coalesced(cur_dapm, &pending);
1448
1449        if (cur_dapm && cur_dapm->seq_notifier) {
1450                for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
1451                        if (sort[i] == cur_sort)
1452                                cur_dapm->seq_notifier(cur_dapm,
1453                                                       i, cur_subseq);
1454        }
1455}
1456
1457static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
1458{
1459        struct snd_soc_dapm_update *update = dapm->update;
1460        struct snd_soc_dapm_widget *w;
1461        int ret;
1462
1463        if (!update)
1464                return;
1465
1466        w = update->widget;
1467
1468        if (w->event &&
1469            (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
1470                ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
1471                if (ret != 0)
1472                        dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
1473                               w->name, ret);
1474        }
1475
1476        ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
1477                                  update->val);
1478        if (ret < 0)
1479                dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
1480                        w->name, ret);
1481
1482        if (w->event &&
1483            (w->event_flags & SND_SOC_DAPM_POST_REG)) {
1484                ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
1485                if (ret != 0)
1486                        dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
1487                               w->name, ret);
1488        }
1489}
1490
1491/* Async callback run prior to DAPM sequences - brings to _PREPARE if
1492 * they're changing state.
1493 */
1494static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
1495{
1496        struct snd_soc_dapm_context *d = data;
1497        int ret;
1498
1499        /* If we're off and we're not supposed to be go into STANDBY */
1500        if (d->bias_level == SND_SOC_BIAS_OFF &&
1501            d->target_bias_level != SND_SOC_BIAS_OFF) {
1502                if (d->dev)
1503                        pm_runtime_get_sync(d->dev);
1504
1505                ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1506                if (ret != 0)
1507                        dev_err(d->dev,
1508                                "ASoC: Failed to turn on bias: %d\n", ret);
1509        }
1510
1511        /* Prepare for a STADDBY->ON or ON->STANDBY transition */
1512        if (d->bias_level != d->target_bias_level) {
1513                ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
1514                if (ret != 0)
1515                        dev_err(d->dev,
1516                                "ASoC: Failed to prepare bias: %d\n", ret);
1517        }
1518}
1519
1520/* Async callback run prior to DAPM sequences - brings to their final
1521 * state.
1522 */
1523static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
1524{
1525        struct snd_soc_dapm_context *d = data;
1526        int ret;
1527
1528        /* If we just powered the last thing off drop to standby bias */
1529        if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1530            (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
1531             d->target_bias_level == SND_SOC_BIAS_OFF)) {
1532                ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
1533                if (ret != 0)
1534                        dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
1535                                ret);
1536        }
1537
1538        /* If we're in standby and can support bias off then do that */
1539        if (d->bias_level == SND_SOC_BIAS_STANDBY &&
1540            d->target_bias_level == SND_SOC_BIAS_OFF) {
1541                ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
1542                if (ret != 0)
1543                        dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
1544                                ret);
1545
1546                if (d->dev)
1547                        pm_runtime_put(d->dev);
1548        }
1549
1550        /* If we just powered up then move to active bias */
1551        if (d->bias_level == SND_SOC_BIAS_PREPARE &&
1552            d->target_bias_level == SND_SOC_BIAS_ON) {
1553                ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
1554                if (ret != 0)
1555                        dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
1556                                ret);
1557        }
1558}
1559
1560static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
1561                                       bool power, bool connect)
1562{
1563        /* If a connection is being made or broken then that update
1564         * will have marked the peer dirty, otherwise the widgets are
1565         * not connected and this update has no impact. */
1566        if (!connect)
1567                return;
1568
1569        /* If the peer is already in the state we're moving to then we
1570         * won't have an impact on it. */
1571        if (power != peer->power)
1572                dapm_mark_dirty(peer, "peer state change");
1573}
1574
1575static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
1576                                  struct list_head *up_list,
1577                                  struct list_head *down_list)
1578{
1579        struct snd_soc_dapm_path *path;
1580
1581        if (w->power == power)
1582                return;
1583
1584        trace_snd_soc_dapm_widget_power(w, power);
1585
1586        /* If we changed our power state perhaps our neigbours changed
1587         * also.
1588         */
1589        list_for_each_entry(path, &w->sources, list_sink) {
1590                if (path->source) {
1591                        dapm_widget_set_peer_power(path->source, power,
1592                                                   path->connect);
1593                }
1594        }
1595        switch (w->id) {
1596        case snd_soc_dapm_supply:
1597        case snd_soc_dapm_regulator_supply:
1598        case snd_soc_dapm_clock_supply:
1599                /* Supplies can't affect their outputs, only their inputs */
1600                break;
1601        default:
1602                list_for_each_entry(path, &w->sinks, list_source) {
1603                        if (path->sink) {
1604                                dapm_widget_set_peer_power(path->sink, power,
1605                                                           path->connect);
1606                        }
1607                }
1608                break;
1609        }
1610
1611        if (power)
1612                dapm_seq_insert(w, up_list, true);
1613        else
1614                dapm_seq_insert(w, down_list, false);
1615
1616        w->power = power;
1617}
1618
1619static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
1620                                  struct list_head *up_list,
1621                                  struct list_head *down_list)
1622{
1623        int power;
1624
1625        switch (w->id) {
1626        case snd_soc_dapm_pre:
1627                dapm_seq_insert(w, down_list, false);
1628                break;
1629        case snd_soc_dapm_post:
1630                dapm_seq_insert(w, up_list, true);
1631                break;
1632
1633        default:
1634                power = dapm_widget_power_check(w);
1635
1636                dapm_widget_set_power(w, power, up_list, down_list);
1637                break;
1638        }
1639}
1640
1641/*
1642 * Scan each dapm widget for complete audio path.
1643 * A complete path is a route that has valid endpoints i.e.:-
1644 *
1645 *  o DAC to output pin.
1646 *  o Input Pin to ADC.
1647 *  o Input pin to Output pin (bypass, sidetone)
1648 *  o DAC to ADC (loopback).
1649 */
1650static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
1651{
1652        struct snd_soc_card *card = dapm->card;
1653        struct snd_soc_dapm_widget *w;
1654        struct snd_soc_dapm_context *d;
1655        LIST_HEAD(up_list);
1656        LIST_HEAD(down_list);
1657        ASYNC_DOMAIN_EXCLUSIVE(async_domain);
1658        enum snd_soc_bias_level bias;
1659
1660        trace_snd_soc_dapm_start(card);
1661
1662        list_for_each_entry(d, &card->dapm_list, list) {
1663                if (d->idle_bias_off)
1664                        d->target_bias_level = SND_SOC_BIAS_OFF;
1665                else
1666                        d->target_bias_level = SND_SOC_BIAS_STANDBY;
1667        }
1668
1669        dapm_reset(card);
1670
1671        /* Check which widgets we need to power and store them in
1672         * lists indicating if they should be powered up or down.  We
1673         * only check widgets that have been flagged as dirty but note
1674         * that new widgets may be added to the dirty list while we
1675         * iterate.
1676         */
1677        list_for_each_entry(w, &card->dapm_dirty, dirty) {
1678                dapm_power_one_widget(w, &up_list, &down_list);
1679        }
1680
1681        list_for_each_entry(w, &card->widgets, list) {
1682                switch (w->id) {
1683                case snd_soc_dapm_pre:
1684                case snd_soc_dapm_post:
1685                        /* These widgets always need to be powered */
1686                        break;
1687                default:
1688                        list_del_init(&w->dirty);
1689                        break;
1690                }
1691
1692                if (w->power) {
1693                        d = w->dapm;
1694
1695                        /* Supplies and micbiases only bring the
1696                         * context up to STANDBY as unless something
1697                         * else is active and passing audio they
1698                         * generally don't require full power.  Signal
1699                         * generators are virtual pins and have no
1700                         * power impact themselves.
1701                         */
1702                        switch (w->id) {
1703                        case snd_soc_dapm_siggen:
1704                                break;
1705                        case snd_soc_dapm_supply:
1706                        case snd_soc_dapm_regulator_supply:
1707                        case snd_soc_dapm_clock_supply:
1708                        case snd_soc_dapm_micbias:
1709                                if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
1710                                        d->target_bias_level = SND_SOC_BIAS_STANDBY;
1711                                break;
1712                        default:
1713                                d->target_bias_level = SND_SOC_BIAS_ON;
1714                                break;
1715                        }
1716                }
1717
1718        }
1719
1720        /* Force all contexts in the card to the same bias state if
1721         * they're not ground referenced.
1722         */
1723        bias = SND_SOC_BIAS_OFF;
1724        list_for_each_entry(d, &card->dapm_list, list)
1725                if (d->target_bias_level > bias)
1726                        bias = d->target_bias_level;
1727        list_for_each_entry(d, &card->dapm_list, list)
1728                if (!d->idle_bias_off)
1729                        d->target_bias_level = bias;
1730
1731        trace_snd_soc_dapm_walk_done(card);
1732
1733        /* Run all the bias changes in parallel */
1734        list_for_each_entry(d, &dapm->card->dapm_list, list)
1735                async_schedule_domain(dapm_pre_sequence_async, d,
1736                                        &async_domain);
1737        async_synchronize_full_domain(&async_domain);
1738
1739        list_for_each_entry(w, &down_list, power_list) {
1740                dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMD);
1741        }
1742
1743        list_for_each_entry(w, &up_list, power_list) {
1744                dapm_seq_check_event(dapm, w, SND_SOC_DAPM_WILL_PMU);
1745        }
1746
1747        /* Power down widgets first; try to avoid amplifying pops. */
1748        dapm_seq_run(dapm, &down_list, event, false);
1749
1750        dapm_widget_update(dapm);
1751
1752        /* Now power up. */
1753        dapm_seq_run(dapm, &up_list, event, true);
1754
1755        /* Run all the bias changes in parallel */
1756        list_for_each_entry(d, &dapm->card->dapm_list, list)
1757                async_schedule_domain(dapm_post_sequence_async, d,
1758                                        &async_domain);
1759        async_synchronize_full_domain(&async_domain);
1760
1761        /* do we need to notify any clients that DAPM event is complete */
1762        list_for_each_entry(d, &card->dapm_list, list) {
1763                if (d->stream_event)
1764                        d->stream_event(d, event);
1765        }
1766
1767        pop_dbg(dapm->dev, card->pop_time,
1768                "DAPM sequencing finished, waiting %dms\n", card->pop_time);
1769        pop_wait(card->pop_time);
1770
1771        trace_snd_soc_dapm_done(card);
1772
1773        return 0;
1774}
1775
1776#ifdef CONFIG_DEBUG_FS
1777static ssize_t dapm_widget_power_read_file(struct file *file,
1778                                           char __user *user_buf,
1779                                           size_t count, loff_t *ppos)
1780{
1781        struct snd_soc_dapm_widget *w = file->private_data;
1782        char *buf;
1783        int in, out;
1784        ssize_t ret;
1785        struct snd_soc_dapm_path *p = NULL;
1786
1787        buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1788        if (!buf)
1789                return -ENOMEM;
1790
1791        in = is_connected_input_ep(w, NULL);
1792        dapm_clear_walk_input(w->dapm, &w->sources);
1793        out = is_connected_output_ep(w, NULL);
1794        dapm_clear_walk_output(w->dapm, &w->sinks);
1795
1796        ret = snprintf(buf, PAGE_SIZE, "%s: %s%s  in %d out %d",
1797                       w->name, w->power ? "On" : "Off",
1798                       w->force ? " (forced)" : "", in, out);
1799
1800        if (w->reg >= 0)
1801                ret += snprintf(buf + ret, PAGE_SIZE - ret,
1802                                " - R%d(0x%x) bit %d",
1803                                w->reg, w->reg, w->shift);
1804
1805        ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
1806
1807        if (w->sname)
1808                ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
1809                                w->sname,
1810                                w->active ? "active" : "inactive");
1811
1812        list_for_each_entry(p, &w->sources, list_sink) {
1813                if (p->connected && !p->connected(w, p->sink))
1814                        continue;
1815
1816                if (p->connect)
1817                        ret += snprintf(buf + ret, PAGE_SIZE - ret,
1818                                        " in  \"%s\" \"%s\"\n",
1819                                        p->name ? p->name : "static",
1820                                        p->source->name);
1821        }
1822        list_for_each_entry(p, &w->sinks, list_source) {
1823                if (p->connected && !p->connected(w, p->sink))
1824                        continue;
1825
1826                if (p->connect)
1827                        ret += snprintf(buf + ret, PAGE_SIZE - ret,
1828                                        " out \"%s\" \"%s\"\n",
1829                                        p->name ? p->name : "static",
1830                                        p->sink->name);
1831        }
1832
1833        ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1834
1835        kfree(buf);
1836        return ret;
1837}
1838
1839static const struct file_operations dapm_widget_power_fops = {
1840        .open = simple_open,
1841        .read = dapm_widget_power_read_file,
1842        .llseek = default_llseek,
1843};
1844
1845static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
1846                                   size_t count, loff_t *ppos)
1847{
1848        struct snd_soc_dapm_context *dapm = file->private_data;
1849        char *level;
1850
1851        switch (dapm->bias_level) {
1852        case SND_SOC_BIAS_ON:
1853                level = "On\n";
1854                break;
1855        case SND_SOC_BIAS_PREPARE:
1856                level = "Prepare\n";
1857                break;
1858        case SND_SOC_BIAS_STANDBY:
1859                level = "Standby\n";
1860                break;
1861        case SND_SOC_BIAS_OFF:
1862                level = "Off\n";
1863                break;
1864        default:
1865                BUG();
1866                level = "Unknown\n";
1867                break;
1868        }
1869
1870        return simple_read_from_buffer(user_buf, count, ppos, level,
1871                                       strlen(level));
1872}
1873
1874static const struct file_operations dapm_bias_fops = {
1875        .open = simple_open,
1876        .read = dapm_bias_read_file,
1877        .llseek = default_llseek,
1878};
1879
1880void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1881        struct dentry *parent)
1882{
1883        struct dentry *d;
1884
1885        dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
1886
1887        if (!dapm->debugfs_dapm) {
1888                dev_warn(dapm->dev,
1889                       "ASoC: Failed to create DAPM debugfs directory\n");
1890                return;
1891        }
1892
1893        d = debugfs_create_file("bias_level", 0444,
1894                                dapm->debugfs_dapm, dapm,
1895                                &dapm_bias_fops);
1896        if (!d)
1897                dev_warn(dapm->dev,
1898                         "ASoC: Failed to create bias level debugfs file\n");
1899}
1900
1901static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1902{
1903        struct snd_soc_dapm_context *dapm = w->dapm;
1904        struct dentry *d;
1905
1906        if (!dapm->debugfs_dapm || !w->name)
1907                return;
1908
1909        d = debugfs_create_file(w->name, 0444,
1910                                dapm->debugfs_dapm, w,
1911                                &dapm_widget_power_fops);
1912        if (!d)
1913                dev_warn(w->dapm->dev,
1914                        "ASoC: Failed to create %s debugfs file\n",
1915                        w->name);
1916}
1917
1918static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1919{
1920        debugfs_remove_recursive(dapm->debugfs_dapm);
1921}
1922
1923#else
1924void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
1925        struct dentry *parent)
1926{
1927}
1928
1929static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
1930{
1931}
1932
1933static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
1934{
1935}
1936
1937#endif
1938
1939/* test and update the power status of a mux widget */
1940static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1941                                 struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1942{
1943        struct snd_soc_dapm_path *path;
1944        int found = 0;
1945
1946        if (widget->id != snd_soc_dapm_mux &&
1947            widget->id != snd_soc_dapm_virt_mux &&
1948            widget->id != snd_soc_dapm_value_mux)
1949                return -ENODEV;
1950
1951        /* find dapm widget path assoc with kcontrol */
1952        list_for_each_entry(path, &widget->dapm->card->paths, list) {
1953                if (path->kcontrol != kcontrol)
1954                        continue;
1955
1956                if (!path->name || !e->texts[mux])
1957                        continue;
1958
1959                found = 1;
1960                /* we now need to match the string in the enum to the path */
1961                if (!(strcmp(path->name, e->texts[mux]))) {
1962                        path->connect = 1; /* new connection */
1963                        dapm_mark_dirty(path->source, "mux connection");
1964                } else {
1965                        if (path->connect)
1966                                dapm_mark_dirty(path->source,
1967                                                "mux disconnection");
1968                        path->connect = 0; /* old connection must be powered down */
1969                }
1970        }
1971
1972        if (found) {
1973                dapm_mark_dirty(widget, "mux change");
1974                dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
1975        }
1976
1977        return found;
1978}
1979
1980int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
1981                struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
1982{
1983        struct snd_soc_card *card = widget->dapm->card;
1984        int ret;
1985
1986        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
1987        ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
1988        mutex_unlock(&card->dapm_mutex);
1989        if (ret > 0)
1990                soc_dpcm_runtime_update(widget);
1991        return ret;
1992}
1993EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
1994
1995/* test and update the power status of a mixer or switch widget */
1996static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
1997                                   struct snd_kcontrol *kcontrol, int connect)
1998{
1999        struct snd_soc_dapm_path *path;
2000        int found = 0;
2001
2002        if (widget->id != snd_soc_dapm_mixer &&
2003            widget->id != snd_soc_dapm_mixer_named_ctl &&
2004            widget->id != snd_soc_dapm_switch)
2005                return -ENODEV;
2006
2007        /* find dapm widget path assoc with kcontrol */
2008        list_for_each_entry(path, &widget->dapm->card->paths, list) {
2009                if (path->kcontrol != kcontrol)
2010                        continue;
2011
2012                /* found, now check type */
2013                found = 1;
2014                path->connect = connect;
2015                dapm_mark_dirty(path->source, "mixer connection");
2016        }
2017
2018        if (found) {
2019                dapm_mark_dirty(widget, "mixer update");
2020                dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
2021        }
2022
2023        return found;
2024}
2025
2026int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
2027                                struct snd_kcontrol *kcontrol, int connect)
2028{
2029        struct snd_soc_card *card = widget->dapm->card;
2030        int ret;
2031
2032        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2033        ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
2034        mutex_unlock(&card->dapm_mutex);
2035        if (ret > 0)
2036                soc_dpcm_runtime_update(widget);
2037        return ret;
2038}
2039EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
2040
2041/* show dapm widget status in sys fs */
2042static ssize_t dapm_widget_show(struct device *dev,
2043        struct device_attribute *attr, char *buf)
2044{
2045        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
2046        struct snd_soc_codec *codec =rtd->codec;
2047        struct snd_soc_dapm_widget *w;
2048        int count = 0;
2049        char *state = "not set";
2050
2051        list_for_each_entry(w, &codec->card->widgets, list) {
2052                if (w->dapm != &codec->dapm)
2053                        continue;
2054
2055                /* only display widgets that burnm power */
2056                switch (w->id) {
2057                case snd_soc_dapm_hp:
2058                case snd_soc_dapm_mic:
2059                case snd_soc_dapm_spk:
2060                case snd_soc_dapm_line:
2061                case snd_soc_dapm_micbias:
2062                case snd_soc_dapm_dac:
2063                case snd_soc_dapm_adc:
2064                case snd_soc_dapm_pga:
2065                case snd_soc_dapm_out_drv:
2066                case snd_soc_dapm_mixer:
2067                case snd_soc_dapm_mixer_named_ctl:
2068                case snd_soc_dapm_supply:
2069                case snd_soc_dapm_regulator_supply:
2070                case snd_soc_dapm_clock_supply:
2071                        if (w->name)
2072                                count += sprintf(buf + count, "%s: %s\n",
2073                                        w->name, w->power ? "On":"Off");
2074                break;
2075                default:
2076                break;
2077                }
2078        }
2079
2080        switch (codec->dapm.bias_level) {
2081        case SND_SOC_BIAS_ON:
2082                state = "On";
2083                break;
2084        case SND_SOC_BIAS_PREPARE:
2085                state = "Prepare";
2086                break;
2087        case SND_SOC_BIAS_STANDBY:
2088                state = "Standby";
2089                break;
2090        case SND_SOC_BIAS_OFF:
2091                state = "Off";
2092                break;
2093        }
2094        count += sprintf(buf + count, "PM State: %s\n", state);
2095
2096        return count;
2097}
2098
2099static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
2100
2101int snd_soc_dapm_sys_add(struct device *dev)
2102{
2103        return device_create_file(dev, &dev_attr_dapm_widget);
2104}
2105
2106static void snd_soc_dapm_sys_remove(struct device *dev)
2107{
2108        device_remove_file(dev, &dev_attr_dapm_widget);
2109}
2110
2111static void dapm_free_path(struct snd_soc_dapm_path *path)
2112{
2113        list_del(&path->list_sink);
2114        list_del(&path->list_source);
2115        list_del(&path->list);
2116        kfree(path);
2117}
2118
2119/* free all dapm widgets and resources */
2120static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
2121{
2122        struct snd_soc_dapm_widget *w, *next_w;
2123        struct snd_soc_dapm_path *p, *next_p;
2124
2125        list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2126                if (w->dapm != dapm)
2127                        continue;
2128                list_del(&w->list);
2129                /*
2130                 * remove source and sink paths associated to this widget.
2131                 * While removing the path, remove reference to it from both
2132                 * source and sink widgets so that path is removed only once.
2133                 */
2134                list_for_each_entry_safe(p, next_p, &w->sources, list_sink)
2135                        dapm_free_path(p);
2136
2137                list_for_each_entry_safe(p, next_p, &w->sinks, list_source)
2138                        dapm_free_path(p);
2139
2140                kfree(w->kcontrols);
2141                kfree(w->name);
2142                kfree(w);
2143        }
2144}
2145
2146static struct snd_soc_dapm_widget *dapm_find_widget(
2147                        struct snd_soc_dapm_context *dapm, const char *pin,
2148                        bool search_other_contexts)
2149{
2150        struct snd_soc_dapm_widget *w;
2151        struct snd_soc_dapm_widget *fallback = NULL;
2152
2153        list_for_each_entry(w, &dapm->card->widgets, list) {
2154                if (!strcmp(w->name, pin)) {
2155                        if (w->dapm == dapm)
2156                                return w;
2157                        else
2158                                fallback = w;
2159                }
2160        }
2161
2162        if (search_other_contexts)
2163                return fallback;
2164
2165        return NULL;
2166}
2167
2168static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
2169                                const char *pin, int status)
2170{
2171        struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
2172
2173        if (!w) {
2174                dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
2175                return -EINVAL;
2176        }
2177
2178        if (w->connected != status)
2179                dapm_mark_dirty(w, "pin configuration");
2180
2181        w->connected = status;
2182        if (status == 0)
2183                w->force = 0;
2184
2185        return 0;
2186}
2187
2188/**
2189 * snd_soc_dapm_sync - scan and power dapm paths
2190 * @dapm: DAPM context
2191 *
2192 * Walks all dapm audio paths and powers widgets according to their
2193 * stream or path usage.
2194 *
2195 * Returns 0 for success.
2196 */
2197int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
2198{
2199        int ret;
2200
2201        /*
2202         * Suppress early reports (eg, jacks syncing their state) to avoid
2203         * silly DAPM runs during card startup.
2204         */
2205        if (!dapm->card || !dapm->card->instantiated)
2206                return 0;
2207
2208        mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2209        ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2210        mutex_unlock(&dapm->card->dapm_mutex);
2211        return ret;
2212}
2213EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
2214
2215static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
2216                                  const struct snd_soc_dapm_route *route)
2217{
2218        struct snd_soc_dapm_path *path;
2219        struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
2220        struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
2221        const char *sink;
2222        const char *control = route->control;
2223        const char *source;
2224        char prefixed_sink[80];
2225        char prefixed_source[80];
2226        int ret = 0;
2227
2228        if (dapm->codec && dapm->codec->name_prefix) {
2229                snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2230                         dapm->codec->name_prefix, route->sink);
2231                sink = prefixed_sink;
2232                snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2233                         dapm->codec->name_prefix, route->source);
2234                source = prefixed_source;
2235        } else {
2236                sink = route->sink;
2237                source = route->source;
2238        }
2239
2240        /*
2241         * find src and dest widgets over all widgets but favor a widget from
2242         * current DAPM context
2243         */
2244        list_for_each_entry(w, &dapm->card->widgets, list) {
2245                if (!wsink && !(strcmp(w->name, sink))) {
2246                        wtsink = w;
2247                        if (w->dapm == dapm)
2248                                wsink = w;
2249                        continue;
2250                }
2251                if (!wsource && !(strcmp(w->name, source))) {
2252                        wtsource = w;
2253                        if (w->dapm == dapm)
2254                                wsource = w;
2255                }
2256        }
2257        /* use widget from another DAPM context if not found from this */
2258        if (!wsink)
2259                wsink = wtsink;
2260        if (!wsource)
2261                wsource = wtsource;
2262
2263        if (wsource == NULL) {
2264                dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
2265                        route->source);
2266                return -ENODEV;
2267        }
2268        if (wsink == NULL) {
2269                dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
2270                        route->sink);
2271                return -ENODEV;
2272        }
2273
2274        path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
2275        if (!path)
2276                return -ENOMEM;
2277
2278        path->source = wsource;
2279        path->sink = wsink;
2280        path->connected = route->connected;
2281        INIT_LIST_HEAD(&path->list);
2282        INIT_LIST_HEAD(&path->list_source);
2283        INIT_LIST_HEAD(&path->list_sink);
2284
2285        /* check for external widgets */
2286        if (wsink->id == snd_soc_dapm_input) {
2287                if (wsource->id == snd_soc_dapm_micbias ||
2288                        wsource->id == snd_soc_dapm_mic ||
2289                        wsource->id == snd_soc_dapm_line ||
2290                        wsource->id == snd_soc_dapm_output)
2291                        wsink->ext = 1;
2292        }
2293        if (wsource->id == snd_soc_dapm_output) {
2294                if (wsink->id == snd_soc_dapm_spk ||
2295                        wsink->id == snd_soc_dapm_hp ||
2296                        wsink->id == snd_soc_dapm_line ||
2297                        wsink->id == snd_soc_dapm_input)
2298                        wsource->ext = 1;
2299        }
2300
2301        /* connect static paths */
2302        if (control == NULL) {
2303                list_add(&path->list, &dapm->card->paths);
2304                list_add(&path->list_sink, &wsink->sources);
2305                list_add(&path->list_source, &wsource->sinks);
2306                path->connect = 1;
2307                return 0;
2308        }
2309
2310        /* connect dynamic paths */
2311        switch (wsink->id) {
2312        case snd_soc_dapm_adc:
2313        case snd_soc_dapm_dac:
2314        case snd_soc_dapm_pga:
2315        case snd_soc_dapm_out_drv:
2316        case snd_soc_dapm_input:
2317        case snd_soc_dapm_output:
2318        case snd_soc_dapm_siggen:
2319        case snd_soc_dapm_micbias:
2320        case snd_soc_dapm_vmid:
2321        case snd_soc_dapm_pre:
2322        case snd_soc_dapm_post:
2323        case snd_soc_dapm_supply:
2324        case snd_soc_dapm_regulator_supply:
2325        case snd_soc_dapm_clock_supply:
2326        case snd_soc_dapm_aif_in:
2327        case snd_soc_dapm_aif_out:
2328        case snd_soc_dapm_dai_in:
2329        case snd_soc_dapm_dai_out:
2330        case snd_soc_dapm_dai_link:
2331                list_add(&path->list, &dapm->card->paths);
2332                list_add(&path->list_sink, &wsink->sources);
2333                list_add(&path->list_source, &wsource->sinks);
2334                path->connect = 1;
2335                return 0;
2336        case snd_soc_dapm_mux:
2337        case snd_soc_dapm_virt_mux:
2338        case snd_soc_dapm_value_mux:
2339                ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
2340                        &wsink->kcontrol_news[0]);
2341                if (ret != 0)
2342                        goto err;
2343                break;
2344        case snd_soc_dapm_switch:
2345        case snd_soc_dapm_mixer:
2346        case snd_soc_dapm_mixer_named_ctl:
2347                ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
2348                if (ret != 0)
2349                        goto err;
2350                break;
2351        case snd_soc_dapm_hp:
2352        case snd_soc_dapm_mic:
2353        case snd_soc_dapm_line:
2354        case snd_soc_dapm_spk:
2355                list_add(&path->list, &dapm->card->paths);
2356                list_add(&path->list_sink, &wsink->sources);
2357                list_add(&path->list_source, &wsource->sinks);
2358                path->connect = 0;
2359                return 0;
2360        }
2361
2362        dapm_mark_dirty(wsource, "Route added");
2363        dapm_mark_dirty(wsink, "Route added");
2364
2365        return 0;
2366
2367err:
2368        dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
2369                 source, control, sink);
2370        kfree(path);
2371        return ret;
2372}
2373
2374static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
2375                                  const struct snd_soc_dapm_route *route)
2376{
2377        struct snd_soc_dapm_path *path, *p;
2378        const char *sink;
2379        const char *source;
2380        char prefixed_sink[80];
2381        char prefixed_source[80];
2382
2383        if (route->control) {
2384                dev_err(dapm->dev,
2385                        "ASoC: Removal of routes with controls not supported\n");
2386                return -EINVAL;
2387        }
2388
2389        if (dapm->codec && dapm->codec->name_prefix) {
2390                snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
2391                         dapm->codec->name_prefix, route->sink);
2392                sink = prefixed_sink;
2393                snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
2394                         dapm->codec->name_prefix, route->source);
2395                source = prefixed_source;
2396        } else {
2397                sink = route->sink;
2398                source = route->source;
2399        }
2400
2401        path = NULL;
2402        list_for_each_entry(p, &dapm->card->paths, list) {
2403                if (strcmp(p->source->name, source) != 0)
2404                        continue;
2405                if (strcmp(p->sink->name, sink) != 0)
2406                        continue;
2407                path = p;
2408                break;
2409        }
2410
2411        if (path) {
2412                dapm_mark_dirty(path->source, "Route removed");
2413                dapm_mark_dirty(path->sink, "Route removed");
2414
2415                dapm_free_path(path);
2416        } else {
2417                dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
2418                         source, sink);
2419        }
2420
2421        return 0;
2422}
2423
2424/**
2425 * snd_soc_dapm_add_routes - Add routes between DAPM widgets
2426 * @dapm: DAPM context
2427 * @route: audio routes
2428 * @num: number of routes
2429 *
2430 * Connects 2 dapm widgets together via a named audio path. The sink is
2431 * the widget receiving the audio signal, whilst the source is the sender
2432 * of the audio signal.
2433 *
2434 * Returns 0 for success else error. On error all resources can be freed
2435 * with a call to snd_soc_card_free().
2436 */
2437int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
2438                            const struct snd_soc_dapm_route *route, int num)
2439{
2440        int i, r, ret = 0;
2441
2442        mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2443        for (i = 0; i < num; i++) {
2444                r = snd_soc_dapm_add_route(dapm, route);
2445                if (r < 0) {
2446                        dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
2447                                route->source,
2448                                route->control ? route->control : "direct",
2449                                route->sink);
2450                        ret = r;
2451                }
2452                route++;
2453        }
2454        mutex_unlock(&dapm->card->dapm_mutex);
2455
2456        return ret;
2457}
2458EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
2459
2460/**
2461 * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
2462 * @dapm: DAPM context
2463 * @route: audio routes
2464 * @num: number of routes
2465 *
2466 * Removes routes from the DAPM context.
2467 */
2468int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
2469                            const struct snd_soc_dapm_route *route, int num)
2470{
2471        int i, ret = 0;
2472
2473        mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2474        for (i = 0; i < num; i++) {
2475                snd_soc_dapm_del_route(dapm, route);
2476                route++;
2477        }
2478        mutex_unlock(&dapm->card->dapm_mutex);
2479
2480        return ret;
2481}
2482EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
2483
2484static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
2485                                   const struct snd_soc_dapm_route *route)
2486{
2487        struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
2488                                                              route->source,
2489                                                              true);
2490        struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
2491                                                            route->sink,
2492                                                            true);
2493        struct snd_soc_dapm_path *path;
2494        int count = 0;
2495
2496        if (!source) {
2497                dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
2498                        route->source);
2499                return -ENODEV;
2500        }
2501
2502        if (!sink) {
2503                dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
2504                        route->sink);
2505                return -ENODEV;
2506        }
2507
2508        if (route->control || route->connected)
2509                dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
2510                         route->source, route->sink);
2511
2512        list_for_each_entry(path, &source->sinks, list_source) {
2513                if (path->sink == sink) {
2514                        path->weak = 1;
2515                        count++;
2516                }
2517        }
2518
2519        if (count == 0)
2520                dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
2521                        route->source, route->sink);
2522        if (count > 1)
2523                dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
2524                         count, route->source, route->sink);
2525
2526        return 0;
2527}
2528
2529/**
2530 * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
2531 * @dapm: DAPM context
2532 * @route: audio routes
2533 * @num: number of routes
2534 *
2535 * Mark existing routes matching those specified in the passed array
2536 * as being weak, meaning that they are ignored for the purpose of
2537 * power decisions.  The main intended use case is for sidetone paths
2538 * which couple audio between other independent paths if they are both
2539 * active in order to make the combination work better at the user
2540 * level but which aren't intended to be "used".
2541 *
2542 * Note that CODEC drivers should not use this as sidetone type paths
2543 * can frequently also be used as bypass paths.
2544 */
2545int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
2546                             const struct snd_soc_dapm_route *route, int num)
2547{
2548        int i, err;
2549        int ret = 0;
2550
2551        mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2552        for (i = 0; i < num; i++) {
2553                err = snd_soc_dapm_weak_route(dapm, route);
2554                if (err)
2555                        ret = err;
2556                route++;
2557        }
2558        mutex_unlock(&dapm->card->dapm_mutex);
2559
2560        return ret;
2561}
2562EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
2563
2564/**
2565 * snd_soc_dapm_new_widgets - add new dapm widgets
2566 * @dapm: DAPM context
2567 *
2568 * Checks the codec for any new dapm widgets and creates them if found.
2569 *
2570 * Returns 0 for success.
2571 */
2572int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
2573{
2574        struct snd_soc_dapm_widget *w;
2575        unsigned int val;
2576
2577        mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
2578
2579        list_for_each_entry(w, &dapm->card->widgets, list)
2580        {
2581                if (w->new)
2582                        continue;
2583
2584                if (w->num_kcontrols) {
2585                        w->kcontrols = kzalloc(w->num_kcontrols *
2586                                                sizeof(struct snd_kcontrol *),
2587                                                GFP_KERNEL);
2588                        if (!w->kcontrols) {
2589                                mutex_unlock(&dapm->card->dapm_mutex);
2590                                return -ENOMEM;
2591                        }
2592                }
2593
2594                switch(w->id) {
2595                case snd_soc_dapm_switch:
2596                case snd_soc_dapm_mixer:
2597                case snd_soc_dapm_mixer_named_ctl:
2598                        dapm_new_mixer(w);
2599                        break;
2600                case snd_soc_dapm_mux:
2601                case snd_soc_dapm_virt_mux:
2602                case snd_soc_dapm_value_mux:
2603                        dapm_new_mux(w);
2604                        break;
2605                case snd_soc_dapm_pga:
2606                case snd_soc_dapm_out_drv:
2607                        dapm_new_pga(w);
2608                        break;
2609                default:
2610                        break;
2611                }
2612
2613                /* Read the initial power state from the device */
2614                if (w->reg >= 0) {
2615                        val = soc_widget_read(w, w->reg);
2616                        val &= 1 << w->shift;
2617                        if (w->invert)
2618                                val = !val;
2619
2620                        if (val)
2621                                w->power = 1;
2622                }
2623
2624                w->new = 1;
2625
2626                dapm_mark_dirty(w, "new widget");
2627                dapm_debugfs_add_widget(w);
2628        }
2629
2630        dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
2631        mutex_unlock(&dapm->card->dapm_mutex);
2632        return 0;
2633}
2634EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
2635
2636/**
2637 * snd_soc_dapm_get_volsw - dapm mixer get callback
2638 * @kcontrol: mixer control
2639 * @ucontrol: control element information
2640 *
2641 * Callback to get the value of a dapm mixer control.
2642 *
2643 * Returns 0 for success.
2644 */
2645int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
2646        struct snd_ctl_elem_value *ucontrol)
2647{
2648        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2649        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2650        struct soc_mixer_control *mc =
2651                (struct soc_mixer_control *)kcontrol->private_value;
2652        unsigned int reg = mc->reg;
2653        unsigned int shift = mc->shift;
2654        int max = mc->max;
2655        unsigned int mask = (1 << fls(max)) - 1;
2656        unsigned int invert = mc->invert;
2657
2658        if (snd_soc_volsw_is_stereo(mc))
2659                dev_warn(widget->dapm->dev,
2660                         "ASoC: Control '%s' is stereo, which is not supported\n",
2661                         kcontrol->id.name);
2662
2663        ucontrol->value.integer.value[0] =
2664                (snd_soc_read(widget->codec, reg) >> shift) & mask;
2665        if (invert)
2666                ucontrol->value.integer.value[0] =
2667                        max - ucontrol->value.integer.value[0];
2668
2669        return 0;
2670}
2671EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
2672
2673/**
2674 * snd_soc_dapm_put_volsw - dapm mixer set callback
2675 * @kcontrol: mixer control
2676 * @ucontrol: control element information
2677 *
2678 * Callback to set the value of a dapm mixer control.
2679 *
2680 * Returns 0 for success.
2681 */
2682int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
2683        struct snd_ctl_elem_value *ucontrol)
2684{
2685        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2686        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2687        struct snd_soc_codec *codec = widget->codec;
2688        struct snd_soc_card *card = codec->card;
2689        struct soc_mixer_control *mc =
2690                (struct soc_mixer_control *)kcontrol->private_value;
2691        unsigned int reg = mc->reg;
2692        unsigned int shift = mc->shift;
2693        int max = mc->max;
2694        unsigned int mask = (1 << fls(max)) - 1;
2695        unsigned int invert = mc->invert;
2696        unsigned int val;
2697        int connect, change;
2698        struct snd_soc_dapm_update update;
2699        int wi;
2700
2701        if (snd_soc_volsw_is_stereo(mc))
2702                dev_warn(widget->dapm->dev,
2703                         "ASoC: Control '%s' is stereo, which is not supported\n",
2704                         kcontrol->id.name);
2705
2706        val = (ucontrol->value.integer.value[0] & mask);
2707        connect = !!val;
2708
2709        if (invert)
2710                val = max - val;
2711        mask = mask << shift;
2712        val = val << shift;
2713
2714        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2715
2716        change = snd_soc_test_bits(widget->codec, reg, mask, val);
2717        if (change) {
2718                for (wi = 0; wi < wlist->num_widgets; wi++) {
2719                        widget = wlist->widgets[wi];
2720
2721                        widget->value = val;
2722
2723                        update.kcontrol = kcontrol;
2724                        update.widget = widget;
2725                        update.reg = reg;
2726                        update.mask = mask;
2727                        update.val = val;
2728                        widget->dapm->update = &update;
2729
2730                        soc_dapm_mixer_update_power(widget, kcontrol, connect);
2731
2732                        widget->dapm->update = NULL;
2733                }
2734        }
2735
2736        mutex_unlock(&card->dapm_mutex);
2737        return change;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
2740
2741/**
2742 * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
2743 * @kcontrol: mixer control
2744 * @ucontrol: control element information
2745 *
2746 * Callback to get the value of a dapm enumerated double mixer control.
2747 *
2748 * Returns 0 for success.
2749 */
2750int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
2751        struct snd_ctl_elem_value *ucontrol)
2752{
2753        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2754        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2755        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2756        unsigned int val;
2757
2758        val = snd_soc_read(widget->codec, e->reg);
2759        ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
2760        if (e->shift_l != e->shift_r)
2761                ucontrol->value.enumerated.item[1] =
2762                        (val >> e->shift_r) & e->mask;
2763
2764        return 0;
2765}
2766EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
2767
2768/**
2769 * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
2770 * @kcontrol: mixer control
2771 * @ucontrol: control element information
2772 *
2773 * Callback to set the value of a dapm enumerated double mixer control.
2774 *
2775 * Returns 0 for success.
2776 */
2777int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
2778        struct snd_ctl_elem_value *ucontrol)
2779{
2780        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2781        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2782        struct snd_soc_codec *codec = widget->codec;
2783        struct snd_soc_card *card = codec->card;
2784        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2785        unsigned int val, mux, change;
2786        unsigned int mask;
2787        struct snd_soc_dapm_update update;
2788        int wi;
2789
2790        if (ucontrol->value.enumerated.item[0] > e->max - 1)
2791                return -EINVAL;
2792        mux = ucontrol->value.enumerated.item[0];
2793        val = mux << e->shift_l;
2794        mask = e->mask << e->shift_l;
2795        if (e->shift_l != e->shift_r) {
2796                if (ucontrol->value.enumerated.item[1] > e->max - 1)
2797                        return -EINVAL;
2798                val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2799                mask |= e->mask << e->shift_r;
2800        }
2801
2802        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2803
2804        change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2805        if (change) {
2806                for (wi = 0; wi < wlist->num_widgets; wi++) {
2807                        widget = wlist->widgets[wi];
2808
2809                        widget->value = val;
2810
2811                        update.kcontrol = kcontrol;
2812                        update.widget = widget;
2813                        update.reg = e->reg;
2814                        update.mask = mask;
2815                        update.val = val;
2816                        widget->dapm->update = &update;
2817
2818                        soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2819
2820                        widget->dapm->update = NULL;
2821                }
2822        }
2823
2824        mutex_unlock(&card->dapm_mutex);
2825        return change;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
2828
2829/**
2830 * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
2831 * @kcontrol: mixer control
2832 * @ucontrol: control element information
2833 *
2834 * Returns 0 for success.
2835 */
2836int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
2837                               struct snd_ctl_elem_value *ucontrol)
2838{
2839        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2840        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2841
2842        ucontrol->value.enumerated.item[0] = widget->value;
2843
2844        return 0;
2845}
2846EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
2847
2848/**
2849 * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
2850 * @kcontrol: mixer control
2851 * @ucontrol: control element information
2852 *
2853 * Returns 0 for success.
2854 */
2855int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
2856                               struct snd_ctl_elem_value *ucontrol)
2857{
2858        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2859        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2860        struct snd_soc_codec *codec = widget->codec;
2861        struct snd_soc_card *card = codec->card;
2862        struct soc_enum *e =
2863                (struct soc_enum *)kcontrol->private_value;
2864        int change;
2865        int wi;
2866
2867        if (ucontrol->value.enumerated.item[0] >= e->max)
2868                return -EINVAL;
2869
2870        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2871
2872        change = widget->value != ucontrol->value.enumerated.item[0];
2873        if (change) {
2874                for (wi = 0; wi < wlist->num_widgets; wi++) {
2875                        widget = wlist->widgets[wi];
2876
2877                        widget->value = ucontrol->value.enumerated.item[0];
2878
2879                        soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
2880                }
2881        }
2882
2883        mutex_unlock(&card->dapm_mutex);
2884        return change;
2885}
2886EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
2887
2888/**
2889 * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
2890 *                                      callback
2891 * @kcontrol: mixer control
2892 * @ucontrol: control element information
2893 *
2894 * Callback to get the value of a dapm semi enumerated double mixer control.
2895 *
2896 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2897 * used for handling bitfield coded enumeration for example.
2898 *
2899 * Returns 0 for success.
2900 */
2901int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
2902        struct snd_ctl_elem_value *ucontrol)
2903{
2904        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2905        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2906        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2907        unsigned int reg_val, val, mux;
2908
2909        reg_val = snd_soc_read(widget->codec, e->reg);
2910        val = (reg_val >> e->shift_l) & e->mask;
2911        for (mux = 0; mux < e->max; mux++) {
2912                if (val == e->values[mux])
2913                        break;
2914        }
2915        ucontrol->value.enumerated.item[0] = mux;
2916        if (e->shift_l != e->shift_r) {
2917                val = (reg_val >> e->shift_r) & e->mask;
2918                for (mux = 0; mux < e->max; mux++) {
2919                        if (val == e->values[mux])
2920                                break;
2921                }
2922                ucontrol->value.enumerated.item[1] = mux;
2923        }
2924
2925        return 0;
2926}
2927EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
2928
2929/**
2930 * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
2931 *                                      callback
2932 * @kcontrol: mixer control
2933 * @ucontrol: control element information
2934 *
2935 * Callback to set the value of a dapm semi enumerated double mixer control.
2936 *
2937 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2938 * used for handling bitfield coded enumeration for example.
2939 *
2940 * Returns 0 for success.
2941 */
2942int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
2943        struct snd_ctl_elem_value *ucontrol)
2944{
2945        struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
2946        struct snd_soc_dapm_widget *widget = wlist->widgets[0];
2947        struct snd_soc_codec *codec = widget->codec;
2948        struct snd_soc_card *card = codec->card;
2949        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2950        unsigned int val, mux, change;
2951        unsigned int mask;
2952        struct snd_soc_dapm_update update;
2953        int wi;
2954
2955        if (ucontrol->value.enumerated.item[0] > e->max - 1)
2956                return -EINVAL;
2957        mux = ucontrol->value.enumerated.item[0];
2958        val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2959        mask = e->mask << e->shift_l;
2960        if (e->shift_l != e->shift_r) {
2961                if (ucontrol->value.enumerated.item[1] > e->max - 1)
2962                        return -EINVAL;
2963                val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2964                mask |= e->mask << e->shift_r;
2965        }
2966
2967        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
2968
2969        change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
2970        if (change) {
2971                for (wi = 0; wi < wlist->num_widgets; wi++) {
2972                        widget = wlist->widgets[wi];
2973
2974                        widget->value = val;
2975
2976                        update.kcontrol = kcontrol;
2977                        update.widget = widget;
2978                        update.reg = e->reg;
2979                        update.mask = mask;
2980                        update.val = val;
2981                        widget->dapm->update = &update;
2982
2983                        soc_dapm_mux_update_power(widget, kcontrol, mux, e);
2984
2985                        widget->dapm->update = NULL;
2986                }
2987        }
2988
2989        mutex_unlock(&card->dapm_mutex);
2990        return change;
2991}
2992EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
2993
2994/**
2995 * snd_soc_dapm_info_pin_switch - Info for a pin switch
2996 *
2997 * @kcontrol: mixer control
2998 * @uinfo: control element information
2999 *
3000 * Callback to provide information about a pin switch control.
3001 */
3002int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
3003                                 struct snd_ctl_elem_info *uinfo)
3004{
3005        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3006        uinfo->count = 1;
3007        uinfo->value.integer.min = 0;
3008        uinfo->value.integer.max = 1;
3009
3010        return 0;
3011}
3012EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
3013
3014/**
3015 * snd_soc_dapm_get_pin_switch - Get information for a pin switch
3016 *
3017 * @kcontrol: mixer control
3018 * @ucontrol: Value
3019 */
3020int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
3021                                struct snd_ctl_elem_value *ucontrol)
3022{
3023        struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3024        const char *pin = (const char *)kcontrol->private_value;
3025
3026        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3027
3028        ucontrol->value.integer.value[0] =
3029                snd_soc_dapm_get_pin_status(&card->dapm, pin);
3030
3031        mutex_unlock(&card->dapm_mutex);
3032
3033        return 0;
3034}
3035EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
3036
3037/**
3038 * snd_soc_dapm_put_pin_switch - Set information for a pin switch
3039 *
3040 * @kcontrol: mixer control
3041 * @ucontrol: Value
3042 */
3043int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
3044                                struct snd_ctl_elem_value *ucontrol)
3045{
3046        struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
3047        const char *pin = (const char *)kcontrol->private_value;
3048
3049        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3050
3051        if (ucontrol->value.integer.value[0])
3052                snd_soc_dapm_enable_pin(&card->dapm, pin);
3053        else
3054                snd_soc_dapm_disable_pin(&card->dapm, pin);
3055
3056        mutex_unlock(&card->dapm_mutex);
3057
3058        snd_soc_dapm_sync(&card->dapm);
3059        return 0;
3060}
3061EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
3062
3063static struct snd_soc_dapm_widget *
3064snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
3065                         const struct snd_soc_dapm_widget *widget)
3066{
3067        struct snd_soc_dapm_widget *w;
3068        int ret;
3069
3070        if ((w = dapm_cnew_widget(widget)) == NULL)
3071                return NULL;
3072
3073        switch (w->id) {
3074        case snd_soc_dapm_regulator_supply:
3075                w->regulator = devm_regulator_get(dapm->dev, w->name);
3076                if (IS_ERR(w->regulator)) {
3077                        ret = PTR_ERR(w->regulator);
3078                        dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3079                                w->name, ret);
3080                        return NULL;
3081                }
3082
3083                if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
3084                        ret = regulator_allow_bypass(w->regulator, true);
3085                        if (ret != 0)
3086                                dev_warn(w->dapm->dev,
3087                                         "ASoC: Failed to unbypass %s: %d\n",
3088                                         w->name, ret);
3089                }
3090                break;
3091        case snd_soc_dapm_clock_supply:
3092#ifdef CONFIG_CLKDEV_LOOKUP
3093                w->clk = devm_clk_get(dapm->dev, w->name);
3094                if (IS_ERR(w->clk)) {
3095                        ret = PTR_ERR(w->clk);
3096                        dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
3097                                w->name, ret);
3098                        return NULL;
3099                }
3100#else
3101                return NULL;
3102#endif
3103                break;
3104        default:
3105                break;
3106        }
3107
3108        if (dapm->codec && dapm->codec->name_prefix)
3109                w->name = kasprintf(GFP_KERNEL, "%s %s",
3110                        dapm->codec->name_prefix, widget->name);
3111        else
3112                w->name = kasprintf(GFP_KERNEL, "%s", widget->name);
3113
3114        if (w->name == NULL) {
3115                kfree(w);
3116                return NULL;
3117        }
3118
3119        switch (w->id) {
3120        case snd_soc_dapm_switch:
3121        case snd_soc_dapm_mixer:
3122        case snd_soc_dapm_mixer_named_ctl:
3123                w->power_check = dapm_generic_check_power;
3124                break;
3125        case snd_soc_dapm_mux:
3126        case snd_soc_dapm_virt_mux:
3127        case snd_soc_dapm_value_mux:
3128                w->power_check = dapm_generic_check_power;
3129                break;
3130        case snd_soc_dapm_adc:
3131        case snd_soc_dapm_aif_out:
3132        case snd_soc_dapm_dai_out:
3133                w->power_check = dapm_adc_check_power;
3134                break;
3135        case snd_soc_dapm_dac:
3136        case snd_soc_dapm_aif_in:
3137        case snd_soc_dapm_dai_in:
3138                w->power_check = dapm_dac_check_power;
3139                break;
3140        case snd_soc_dapm_pga:
3141        case snd_soc_dapm_out_drv:
3142        case snd_soc_dapm_input:
3143        case snd_soc_dapm_output:
3144        case snd_soc_dapm_micbias:
3145        case snd_soc_dapm_spk:
3146        case snd_soc_dapm_hp:
3147        case snd_soc_dapm_mic:
3148        case snd_soc_dapm_line:
3149        case snd_soc_dapm_dai_link:
3150                w->power_check = dapm_generic_check_power;
3151                break;
3152        case snd_soc_dapm_supply:
3153        case snd_soc_dapm_regulator_supply:
3154        case snd_soc_dapm_clock_supply:
3155                w->power_check = dapm_supply_check_power;
3156                break;
3157        default:
3158                w->power_check = dapm_always_on_check_power;
3159                break;
3160        }
3161
3162        w->dapm = dapm;
3163        w->codec = dapm->codec;
3164        w->platform = dapm->platform;
3165        INIT_LIST_HEAD(&w->sources);
3166        INIT_LIST_HEAD(&w->sinks);
3167        INIT_LIST_HEAD(&w->list);
3168        INIT_LIST_HEAD(&w->dirty);
3169        list_add(&w->list, &dapm->card->widgets);
3170
3171        /* machine layer set ups unconnected pins and insertions */
3172        w->connected = 1;
3173        return w;
3174}
3175
3176/**
3177 * snd_soc_dapm_new_controls - create new dapm controls
3178 * @dapm: DAPM context
3179 * @widget: widget array
3180 * @num: number of widgets
3181 *
3182 * Creates new DAPM controls based upon the templates.
3183 *
3184 * Returns 0 for success else error.
3185 */
3186int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
3187        const struct snd_soc_dapm_widget *widget,
3188        int num)
3189{
3190        struct snd_soc_dapm_widget *w;
3191        int i;
3192        int ret = 0;
3193
3194        mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
3195        for (i = 0; i < num; i++) {
3196                w = snd_soc_dapm_new_control(dapm, widget);
3197                if (!w) {
3198                        dev_err(dapm->dev,
3199                                "ASoC: Failed to create DAPM control %s\n",
3200                                widget->name);
3201                        ret = -ENOMEM;
3202                        break;
3203                }
3204                widget++;
3205        }
3206        mutex_unlock(&dapm->card->dapm_mutex);
3207        return ret;
3208}
3209EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
3210
3211static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
3212                                  struct snd_kcontrol *kcontrol, int event)
3213{
3214        struct snd_soc_dapm_path *source_p, *sink_p;
3215        struct snd_soc_dai *source, *sink;
3216        const struct snd_soc_pcm_stream *config = w->params;
3217        struct snd_pcm_substream substream;
3218        struct snd_pcm_hw_params *params = NULL;
3219        u64 fmt;
3220        int ret;
3221
3222        BUG_ON(!config);
3223        BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
3224
3225        /* We only support a single source and sink, pick the first */
3226        source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
3227                                    list_sink);
3228        sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
3229                                  list_source);
3230
3231        BUG_ON(!source_p || !sink_p);
3232        BUG_ON(!sink_p->source || !source_p->sink);
3233        BUG_ON(!source_p->source || !sink_p->sink);
3234
3235        source = source_p->source->priv;
3236        sink = sink_p->sink->priv;
3237
3238        /* Be a little careful as we don't want to overflow the mask array */
3239        if (config->formats) {
3240                fmt = ffs(config->formats) - 1;
3241        } else {
3242                dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
3243                         config->formats);
3244                fmt = 0;
3245        }
3246
3247        /* Currently very limited parameter selection */
3248        params = kzalloc(sizeof(*params), GFP_KERNEL);
3249        if (!params) {
3250                ret = -ENOMEM;
3251                goto out;
3252        }
3253        snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
3254
3255        hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
3256                config->rate_min;
3257        hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
3258                config->rate_max;
3259
3260        hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
3261                = config->channels_min;
3262        hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
3263                = config->channels_max;
3264
3265        memset(&substream, 0, sizeof(substream));
3266
3267        switch (event) {
3268        case SND_SOC_DAPM_PRE_PMU:
3269                if (source->driver->ops && source->driver->ops->hw_params) {
3270                        substream.stream = SNDRV_PCM_STREAM_CAPTURE;
3271                        ret = source->driver->ops->hw_params(&substream,
3272                                                             params, source);
3273                        if (ret != 0) {
3274                                dev_err(source->dev,
3275                                        "ASoC: hw_params() failed: %d\n", ret);
3276                                goto out;
3277                        }
3278                }
3279
3280                if (sink->driver->ops && sink->driver->ops->hw_params) {
3281                        substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
3282                        ret = sink->driver->ops->hw_params(&substream, params,
3283                                                           sink);
3284                        if (ret != 0) {
3285                                dev_err(sink->dev,
3286                                        "ASoC: hw_params() failed: %d\n", ret);
3287                                goto out;
3288                        }
3289                }
3290                break;
3291
3292        case SND_SOC_DAPM_POST_PMU:
3293                ret = snd_soc_dai_digital_mute(sink, 0,
3294                                               SNDRV_PCM_STREAM_PLAYBACK);
3295                if (ret != 0 && ret != -ENOTSUPP)
3296                        dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
3297                ret = 0;
3298                break;
3299
3300        case SND_SOC_DAPM_PRE_PMD:
3301                ret = snd_soc_dai_digital_mute(sink, 1,
3302                                               SNDRV_PCM_STREAM_PLAYBACK);
3303                if (ret != 0 && ret != -ENOTSUPP)
3304                        dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
3305                ret = 0;
3306                break;
3307
3308        default:
3309                BUG();
3310                return -EINVAL;
3311        }
3312
3313out:
3314        kfree(params);
3315        return ret;
3316}
3317
3318int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
3319                         const struct snd_soc_pcm_stream *params,
3320                         struct snd_soc_dapm_widget *source,
3321                         struct snd_soc_dapm_widget *sink)
3322{
3323        struct snd_soc_dapm_route routes[2];
3324        struct snd_soc_dapm_widget template;
3325        struct snd_soc_dapm_widget *w;
3326        size_t len;
3327        char *link_name;
3328
3329        len = strlen(source->name) + strlen(sink->name) + 2;
3330        link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
3331        if (!link_name)
3332                return -ENOMEM;
3333        snprintf(link_name, len, "%s-%s", source->name, sink->name);
3334
3335        memset(&template, 0, sizeof(template));
3336        template.reg = SND_SOC_NOPM;
3337        template.id = snd_soc_dapm_dai_link;
3338        template.name = link_name;
3339        template.event = snd_soc_dai_link_event;
3340        template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
3341                SND_SOC_DAPM_PRE_PMD;
3342
3343        dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
3344
3345        w = snd_soc_dapm_new_control(&card->dapm, &template);
3346        if (!w) {
3347                dev_err(card->dev, "ASoC: Failed to create %s widget\n",
3348                        link_name);
3349                return -ENOMEM;
3350        }
3351
3352        w->params = params;
3353
3354        memset(&routes, 0, sizeof(routes));
3355
3356        routes[0].source = source->name;
3357        routes[0].sink = link_name;
3358        routes[1].source = link_name;
3359        routes[1].sink = sink->name;
3360
3361        return snd_soc_dapm_add_routes(&card->dapm, routes,
3362                                       ARRAY_SIZE(routes));
3363}
3364
3365int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
3366                                 struct snd_soc_dai *dai)
3367{
3368        struct snd_soc_dapm_widget template;
3369        struct snd_soc_dapm_widget *w;
3370
3371        WARN_ON(dapm->dev != dai->dev);
3372
3373        memset(&template, 0, sizeof(template));
3374        template.reg = SND_SOC_NOPM;
3375
3376        if (dai->driver->playback.stream_name) {
3377                template.id = snd_soc_dapm_dai_in;
3378                template.name = dai->driver->playback.stream_name;
3379                template.sname = dai->driver->playback.stream_name;
3380
3381                dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3382                        template.name);
3383
3384                w = snd_soc_dapm_new_control(dapm, &template);
3385                if (!w) {
3386                        dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3387                                dai->driver->playback.stream_name);
3388                }
3389
3390                w->priv = dai;
3391                dai->playback_widget = w;
3392        }
3393
3394        if (dai->driver->capture.stream_name) {
3395                template.id = snd_soc_dapm_dai_out;
3396                template.name = dai->driver->capture.stream_name;
3397                template.sname = dai->driver->capture.stream_name;
3398
3399                dev_dbg(dai->dev, "ASoC: adding %s widget\n",
3400                        template.name);
3401
3402                w = snd_soc_dapm_new_control(dapm, &template);
3403                if (!w) {
3404                        dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
3405                                dai->driver->capture.stream_name);
3406                }
3407
3408                w->priv = dai;
3409                dai->capture_widget = w;
3410        }
3411
3412        return 0;
3413}
3414
3415int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
3416{
3417        struct snd_soc_dapm_widget *dai_w, *w;
3418        struct snd_soc_dai *dai;
3419        struct snd_soc_dapm_route r;
3420
3421        memset(&r, 0, sizeof(r));
3422
3423        /* For each DAI widget... */
3424        list_for_each_entry(dai_w, &card->widgets, list) {
3425                switch (dai_w->id) {
3426                case snd_soc_dapm_dai_in:
3427                case snd_soc_dapm_dai_out:
3428                        break;
3429                default:
3430                        continue;
3431                }
3432
3433                dai = dai_w->priv;
3434
3435                /* ...find all widgets with the same stream and link them */
3436                list_for_each_entry(w, &card->widgets, list) {
3437                        if (w->dapm != dai_w->dapm)
3438                                continue;
3439
3440                        switch (w->id) {
3441                        case snd_soc_dapm_dai_in:
3442                        case snd_soc_dapm_dai_out:
3443                                continue;
3444                        default:
3445                                break;
3446                        }
3447
3448                        if (!w->sname)
3449                                continue;
3450
3451                        if (dai->driver->playback.stream_name &&
3452                            strstr(w->sname,
3453                                   dai->driver->playback.stream_name)) {
3454                                r.source = dai->playback_widget->name;
3455                                r.sink = w->name;
3456                                dev_dbg(dai->dev, "%s -> %s\n",
3457                                         r.source, r.sink);
3458
3459                                snd_soc_dapm_add_route(w->dapm, &r);
3460                        }
3461
3462                        if (dai->driver->capture.stream_name &&
3463                            strstr(w->sname,
3464                                   dai->driver->capture.stream_name)) {
3465                                r.source = w->name;
3466                                r.sink = dai->capture_widget->name;
3467                                dev_dbg(dai->dev, "%s -> %s\n",
3468                                        r.source, r.sink);
3469
3470                                snd_soc_dapm_add_route(w->dapm, &r);
3471                        }
3472                }
3473        }
3474
3475        return 0;
3476}
3477
3478static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3479        int event)
3480{
3481
3482        struct snd_soc_dapm_widget *w_cpu, *w_codec;
3483        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3484        struct snd_soc_dai *codec_dai = rtd->codec_dai;
3485
3486        if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
3487                w_cpu = cpu_dai->playback_widget;
3488                w_codec = codec_dai->playback_widget;
3489        } else {
3490                w_cpu = cpu_dai->capture_widget;
3491                w_codec = codec_dai->capture_widget;
3492        }
3493
3494        if (w_cpu) {
3495
3496                dapm_mark_dirty(w_cpu, "stream event");
3497
3498                switch (event) {
3499                case SND_SOC_DAPM_STREAM_START:
3500                        w_cpu->active = 1;
3501                        break;
3502                case SND_SOC_DAPM_STREAM_STOP:
3503                        w_cpu->active = 0;
3504                        break;
3505                case SND_SOC_DAPM_STREAM_SUSPEND:
3506                case SND_SOC_DAPM_STREAM_RESUME:
3507                case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3508                case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3509                        break;
3510                }
3511        }
3512
3513        if (w_codec) {
3514
3515                dapm_mark_dirty(w_codec, "stream event");
3516
3517                switch (event) {
3518                case SND_SOC_DAPM_STREAM_START:
3519                        w_codec->active = 1;
3520                        break;
3521                case SND_SOC_DAPM_STREAM_STOP:
3522                        w_codec->active = 0;
3523                        break;
3524                case SND_SOC_DAPM_STREAM_SUSPEND:
3525                case SND_SOC_DAPM_STREAM_RESUME:
3526                case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
3527                case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
3528                        break;
3529                }
3530        }
3531
3532        dapm_power_widgets(&rtd->card->dapm, event);
3533}
3534
3535/**
3536 * snd_soc_dapm_stream_event - send a stream event to the dapm core
3537 * @rtd: PCM runtime data
3538 * @stream: stream name
3539 * @event: stream event
3540 *
3541 * Sends a stream event to the dapm core. The core then makes any
3542 * necessary widget power changes.
3543 *
3544 * Returns 0 for success else error.
3545 */
3546void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
3547                              int event)
3548{
3549        struct snd_soc_card *card = rtd->card;
3550
3551        mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
3552        soc_dapm_stream_event(rtd, stream, event);
3553        mutex_unlock(&card->dapm_mutex);
3554}
3555
3556/**
3557 * snd_soc_dapm_enable_pin - enable pin.
3558 * @dapm: DAPM context
3559 * @pin: pin name
3560 *
3561 * Enables input/output pin and its parents or children widgets iff there is
3562 * a valid audio route and active audio stream.
3563 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3564 * do any widget power switching.
3565 */
3566int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3567{
3568        return snd_soc_dapm_set_pin(dapm, pin, 1);
3569}
3570EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
3571
3572/**
3573 * snd_soc_dapm_force_enable_pin - force a pin to be enabled
3574 * @dapm: DAPM context
3575 * @pin: pin name
3576 *
3577 * Enables input/output pin regardless of any other state.  This is
3578 * intended for use with microphone bias supplies used in microphone
3579 * jack detection.
3580 *
3581 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3582 * do any widget power switching.
3583 */
3584int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
3585                                  const char *pin)
3586{
3587        struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3588
3589        if (!w) {
3590                dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3591                return -EINVAL;
3592        }
3593
3594        dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
3595        w->connected = 1;
3596        w->force = 1;
3597        dapm_mark_dirty(w, "force enable");
3598
3599        return 0;
3600}
3601EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
3602
3603/**
3604 * snd_soc_dapm_disable_pin - disable pin.
3605 * @dapm: DAPM context
3606 * @pin: pin name
3607 *
3608 * Disables input/output pin and its parents or children widgets.
3609 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3610 * do any widget power switching.
3611 */
3612int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
3613                             const char *pin)
3614{
3615        return snd_soc_dapm_set_pin(dapm, pin, 0);
3616}
3617EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
3618
3619/**
3620 * snd_soc_dapm_nc_pin - permanently disable pin.
3621 * @dapm: DAPM context
3622 * @pin: pin name
3623 *
3624 * Marks the specified pin as being not connected, disabling it along
3625 * any parent or child widgets.  At present this is identical to
3626 * snd_soc_dapm_disable_pin() but in future it will be extended to do
3627 * additional things such as disabling controls which only affect
3628 * paths through the pin.
3629 *
3630 * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
3631 * do any widget power switching.
3632 */
3633int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
3634{
3635        return snd_soc_dapm_set_pin(dapm, pin, 0);
3636}
3637EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
3638
3639/**
3640 * snd_soc_dapm_get_pin_status - get audio pin status
3641 * @dapm: DAPM context
3642 * @pin: audio signal pin endpoint (or start point)
3643 *
3644 * Get audio pin status - connected or disconnected.
3645 *
3646 * Returns 1 for connected otherwise 0.
3647 */
3648int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
3649                                const char *pin)
3650{
3651        struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
3652
3653        if (w)
3654                return w->connected;
3655
3656        return 0;
3657}
3658EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
3659
3660/**
3661 * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
3662 * @dapm: DAPM context
3663 * @pin: audio signal pin endpoint (or start point)
3664 *
3665 * Mark the given endpoint or pin as ignoring suspend.  When the
3666 * system is disabled a path between two endpoints flagged as ignoring
3667 * suspend will not be disabled.  The path must already be enabled via
3668 * normal means at suspend time, it will not be turned on if it was not
3669 * already enabled.
3670 */
3671int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
3672                                const char *pin)
3673{
3674        struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
3675
3676        if (!w) {
3677                dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
3678                return -EINVAL;
3679        }
3680
3681        w->ignore_suspend = 1;
3682
3683        return 0;
3684}
3685EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
3686
3687static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
3688                                              struct snd_soc_dapm_widget *w)
3689{
3690        struct snd_soc_dapm_path *p;
3691
3692        list_for_each_entry(p, &card->paths, list) {
3693                if ((p->source == w) || (p->sink == w)) {
3694                        dev_dbg(card->dev,
3695                            "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
3696                            p->source->name, p->source->id, p->source->dapm,
3697                            p->sink->name, p->sink->id, p->sink->dapm);
3698
3699                        /* Connected to something other than the codec */
3700                        if (p->source->dapm != p->sink->dapm)
3701                                return true;
3702                        /*
3703                         * Loopback connection from codec external pin to
3704                         * codec external pin
3705                         */
3706                        if (p->sink->id == snd_soc_dapm_input) {
3707                                switch (p->source->id) {
3708                                case snd_soc_dapm_output:
3709                                case snd_soc_dapm_micbias:
3710                                        return true;
3711                                default:
3712                                        break;
3713                                }
3714                        }
3715                }
3716        }
3717
3718        return false;
3719}
3720
3721/**
3722 * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
3723 * @codec: The codec whose pins should be processed
3724 *
3725 * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
3726 * which are unused. Pins are used if they are connected externally to the
3727 * codec, whether that be to some other device, or a loop-back connection to
3728 * the codec itself.
3729 */
3730void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
3731{
3732        struct snd_soc_card *card = codec->card;
3733        struct snd_soc_dapm_context *dapm = &codec->dapm;
3734        struct snd_soc_dapm_widget *w;
3735
3736        dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
3737                &card->dapm, &codec->dapm);
3738
3739        list_for_each_entry(w, &card->widgets, list) {
3740                if (w->dapm != dapm)
3741                        continue;
3742                switch (w->id) {
3743                case snd_soc_dapm_input:
3744                case snd_soc_dapm_output:
3745                case snd_soc_dapm_micbias:
3746                        dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
3747                                w->name);
3748                        if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
3749                                dev_dbg(codec->dev,
3750                                        "... Not in map; disabling\n");
3751                                snd_soc_dapm_nc_pin(dapm, w->name);
3752                        }
3753                        break;
3754                default:
3755                        break;
3756                }
3757        }
3758}
3759
3760/**
3761 * snd_soc_dapm_free - free dapm resources
3762 * @dapm: DAPM context
3763 *
3764 * Free all dapm widgets and resources.
3765 */
3766void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
3767{
3768        snd_soc_dapm_sys_remove(dapm->dev);
3769        dapm_debugfs_cleanup(dapm);
3770        dapm_free_widgets(dapm);
3771        list_del(&dapm->list);
3772}
3773EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
3774
3775static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
3776{
3777        struct snd_soc_card *card = dapm->card;
3778        struct snd_soc_dapm_widget *w;
3779        LIST_HEAD(down_list);
3780        int powerdown = 0;
3781
3782        mutex_lock(&card->dapm_mutex);
3783
3784        list_for_each_entry(w, &dapm->card->widgets, list) {
3785                if (w->dapm != dapm)
3786                        continue;
3787                if (w->power) {
3788                        dapm_seq_insert(w, &down_list, false);
3789                        w->power = 0;
3790                        powerdown = 1;
3791                }
3792        }
3793
3794        /* If there were no widgets to power down we're already in
3795         * standby.
3796         */
3797        if (powerdown) {
3798                if (dapm->bias_level == SND_SOC_BIAS_ON)
3799                        snd_soc_dapm_set_bias_level(dapm,
3800                                                    SND_SOC_BIAS_PREPARE);
3801                dapm_seq_run(dapm, &down_list, 0, false);
3802                if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
3803                        snd_soc_dapm_set_bias_level(dapm,
3804                                                    SND_SOC_BIAS_STANDBY);
3805        }
3806
3807        mutex_unlock(&card->dapm_mutex);
3808}
3809
3810/*
3811 * snd_soc_dapm_shutdown - callback for system shutdown
3812 */
3813void snd_soc_dapm_shutdown(struct snd_soc_card *card)
3814{
3815        struct snd_soc_codec *codec;
3816
3817        list_for_each_entry(codec, &card->codec_dev_list, card_list) {
3818                soc_dapm_shutdown_codec(&codec->dapm);
3819                if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
3820                        snd_soc_dapm_set_bias_level(&codec->dapm,
3821                                                    SND_SOC_BIAS_OFF);
3822        }
3823}
3824
3825/* Module information */
3826MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3827MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
3828MODULE_LICENSE("GPL");
3829
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.