linux/sound/soc/soc-core.c
<<
>>
Prefs
   1/*
   2 * soc-core.c  --  ALSA SoC Audio Layer
   3 *
   4 * Copyright 2005 Wolfson Microelectronics PLC.
   5 * Copyright 2005 Openedhand Ltd.
   6 * Copyright (C) 2010 Slimlogic Ltd.
   7 * Copyright (C) 2010 Texas Instruments Inc.
   8 *
   9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  10 *         with code, comments and ideas from :-
  11 *         Richard Purdie <richard@openedhand.com>
  12 *
  13 *  This program is free software; you can redistribute  it and/or modify it
  14 *  under  the terms of  the GNU General  Public License as published by the
  15 *  Free Software Foundation;  either version 2 of the  License, or (at your
  16 *  option) any later version.
  17 *
  18 *  TODO:
  19 *   o Add hw rules to enforce rates, etc.
  20 *   o More testing with other codecs/machines.
  21 *   o Add more codecs and platforms to ensure good API coverage.
  22 *   o Support TDM on PCM and I2S
  23 */
  24
  25#include <linux/module.h>
  26#include <linux/moduleparam.h>
  27#include <linux/init.h>
  28#include <linux/delay.h>
  29#include <linux/pm.h>
  30#include <linux/bitops.h>
  31#include <linux/debugfs.h>
  32#include <linux/platform_device.h>
  33#include <linux/ctype.h>
  34#include <linux/slab.h>
  35#include <linux/of.h>
  36#include <sound/ac97_codec.h>
  37#include <sound/core.h>
  38#include <sound/jack.h>
  39#include <sound/pcm.h>
  40#include <sound/pcm_params.h>
  41#include <sound/soc.h>
  42#include <sound/soc-dpcm.h>
  43#include <sound/initval.h>
  44
  45#define CREATE_TRACE_POINTS
  46#include <trace/events/asoc.h>
  47
  48#define NAME_SIZE       32
  49
  50static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  51
  52#ifdef CONFIG_DEBUG_FS
  53struct dentry *snd_soc_debugfs_root;
  54EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
  55#endif
  56
  57static DEFINE_MUTEX(client_mutex);
  58static LIST_HEAD(dai_list);
  59static LIST_HEAD(platform_list);
  60static LIST_HEAD(codec_list);
  61
  62/*
  63 * This is a timeout to do a DAPM powerdown after a stream is closed().
  64 * It can be used to eliminate pops between different playback streams, e.g.
  65 * between two audio tracks.
  66 */
  67static int pmdown_time = 5000;
  68module_param(pmdown_time, int, 0);
  69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  70
  71/* returns the minimum number of bytes needed to represent
  72 * a particular given value */
  73static int min_bytes_needed(unsigned long val)
  74{
  75        int c = 0;
  76        int i;
  77
  78        for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
  79                if (val & (1UL << i))
  80                        break;
  81        c = (sizeof val * 8) - c;
  82        if (!c || (c % 8))
  83                c = (c + 8) / 8;
  84        else
  85                c /= 8;
  86        return c;
  87}
  88
  89/* fill buf which is 'len' bytes with a formatted
  90 * string of the form 'reg: value\n' */
  91static int format_register_str(struct snd_soc_codec *codec,
  92                               unsigned int reg, char *buf, size_t len)
  93{
  94        int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  95        int regsize = codec->driver->reg_word_size * 2;
  96        int ret;
  97        char tmpbuf[len + 1];
  98        char regbuf[regsize + 1];
  99
 100        /* since tmpbuf is allocated on the stack, warn the callers if they
 101         * try to abuse this function */
 102        WARN_ON(len > 63);
 103
 104        /* +2 for ': ' and + 1 for '\n' */
 105        if (wordsize + regsize + 2 + 1 != len)
 106                return -EINVAL;
 107
 108        ret = snd_soc_read(codec, reg);
 109        if (ret < 0) {
 110                memset(regbuf, 'X', regsize);
 111                regbuf[regsize] = '\0';
 112        } else {
 113                snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
 114        }
 115
 116        /* prepare the buffer */
 117        snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
 118        /* copy it back to the caller without the '\0' */
 119        memcpy(buf, tmpbuf, len);
 120
 121        return 0;
 122}
 123
 124/* codec register dump */
 125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
 126                                  size_t count, loff_t pos)
 127{
 128        int i, step = 1;
 129        int wordsize, regsize;
 130        int len;
 131        size_t total = 0;
 132        loff_t p = 0;
 133
 134        wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
 135        regsize = codec->driver->reg_word_size * 2;
 136
 137        len = wordsize + regsize + 2 + 1;
 138
 139        if (!codec->driver->reg_cache_size)
 140                return 0;
 141
 142        if (codec->driver->reg_cache_step)
 143                step = codec->driver->reg_cache_step;
 144
 145        for (i = 0; i < codec->driver->reg_cache_size; i += step) {
 146                if (!snd_soc_codec_readable_register(codec, i))
 147                        continue;
 148                if (codec->driver->display_register) {
 149                        count += codec->driver->display_register(codec, buf + count,
 150                                                         PAGE_SIZE - count, i);
 151                } else {
 152                        /* only support larger than PAGE_SIZE bytes debugfs
 153                         * entries for the default case */
 154                        if (p >= pos) {
 155                                if (total + len >= count - 1)
 156                                        break;
 157                                format_register_str(codec, i, buf + total, len);
 158                                total += len;
 159                        }
 160                        p += len;
 161                }
 162        }
 163
 164        total = min(total, count - 1);
 165
 166        return total;
 167}
 168
 169static ssize_t codec_reg_show(struct device *dev,
 170        struct device_attribute *attr, char *buf)
 171{
 172        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
 173
 174        return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
 175}
 176
 177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
 178
 179static ssize_t pmdown_time_show(struct device *dev,
 180                                struct device_attribute *attr, char *buf)
 181{
 182        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
 183
 184        return sprintf(buf, "%ld\n", rtd->pmdown_time);
 185}
 186
 187static ssize_t pmdown_time_set(struct device *dev,
 188                               struct device_attribute *attr,
 189                               const char *buf, size_t count)
 190{
 191        struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
 192        int ret;
 193
 194        ret = strict_strtol(buf, 10, &rtd->pmdown_time);
 195        if (ret)
 196                return ret;
 197
 198        return count;
 199}
 200
 201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
 202
 203#ifdef CONFIG_DEBUG_FS
 204static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
 205                                   size_t count, loff_t *ppos)
 206{
 207        ssize_t ret;
 208        struct snd_soc_codec *codec = file->private_data;
 209        char *buf;
 210
 211        if (*ppos < 0 || !count)
 212                return -EINVAL;
 213
 214        buf = kmalloc(count, GFP_KERNEL);
 215        if (!buf)
 216                return -ENOMEM;
 217
 218        ret = soc_codec_reg_show(codec, buf, count, *ppos);
 219        if (ret >= 0) {
 220                if (copy_to_user(user_buf, buf, ret)) {
 221                        kfree(buf);
 222                        return -EFAULT;
 223                }
 224                *ppos += ret;
 225        }
 226
 227        kfree(buf);
 228        return ret;
 229}
 230
 231static ssize_t codec_reg_write_file(struct file *file,
 232                const char __user *user_buf, size_t count, loff_t *ppos)
 233{
 234        char buf[32];
 235        size_t buf_size;
 236        char *start = buf;
 237        unsigned long reg, value;
 238        struct snd_soc_codec *codec = file->private_data;
 239
 240        buf_size = min(count, (sizeof(buf)-1));
 241        if (copy_from_user(buf, user_buf, buf_size))
 242                return -EFAULT;
 243        buf[buf_size] = 0;
 244
 245        while (*start == ' ')
 246                start++;
 247        reg = simple_strtoul(start, &start, 16);
 248        while (*start == ' ')
 249                start++;
 250        if (strict_strtoul(start, 16, &value))
 251                return -EINVAL;
 252
 253        /* Userspace has been fiddling around behind the kernel's back */
 254        add_taint(TAINT_USER);
 255
 256        snd_soc_write(codec, reg, value);
 257        return buf_size;
 258}
 259
 260static const struct file_operations codec_reg_fops = {
 261        .open = simple_open,
 262        .read = codec_reg_read_file,
 263        .write = codec_reg_write_file,
 264        .llseek = default_llseek,
 265};
 266
 267static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
 268{
 269        struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
 270
 271        codec->debugfs_codec_root = debugfs_create_dir(codec->name,
 272                                                       debugfs_card_root);
 273        if (!codec->debugfs_codec_root) {
 274                dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
 275                return;
 276        }
 277
 278        debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
 279                            &codec->cache_sync);
 280        debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
 281                            &codec->cache_only);
 282
 283        codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
 284                                                 codec->debugfs_codec_root,
 285                                                 codec, &codec_reg_fops);
 286        if (!codec->debugfs_reg)
 287                dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
 288
 289        snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
 290}
 291
 292static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
 293{
 294        debugfs_remove_recursive(codec->debugfs_codec_root);
 295}
 296
 297static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
 298{
 299        struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
 300
 301        platform->debugfs_platform_root = debugfs_create_dir(platform->name,
 302                                                       debugfs_card_root);
 303        if (!platform->debugfs_platform_root) {
 304                dev_warn(platform->dev,
 305                        "Failed to create platform debugfs directory\n");
 306                return;
 307        }
 308
 309        snd_soc_dapm_debugfs_init(&platform->dapm,
 310                platform->debugfs_platform_root);
 311}
 312
 313static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
 314{
 315        debugfs_remove_recursive(platform->debugfs_platform_root);
 316}
 317
 318static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
 319                                    size_t count, loff_t *ppos)
 320{
 321        char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 322        ssize_t len, ret = 0;
 323        struct snd_soc_codec *codec;
 324
 325        if (!buf)
 326                return -ENOMEM;
 327
 328        list_for_each_entry(codec, &codec_list, list) {
 329                len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
 330                               codec->name);
 331                if (len >= 0)
 332                        ret += len;
 333                if (ret > PAGE_SIZE) {
 334                        ret = PAGE_SIZE;
 335                        break;
 336                }
 337        }
 338
 339        if (ret >= 0)
 340                ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
 341
 342        kfree(buf);
 343
 344        return ret;
 345}
 346
 347static const struct file_operations codec_list_fops = {
 348        .read = codec_list_read_file,
 349        .llseek = default_llseek,/* read accesses f_pos */
 350};
 351
 352static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
 353                                  size_t count, loff_t *ppos)
 354{
 355        char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 356        ssize_t len, ret = 0;
 357        struct snd_soc_dai *dai;
 358
 359        if (!buf)
 360                return -ENOMEM;
 361
 362        list_for_each_entry(dai, &dai_list, list) {
 363                len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
 364                if (len >= 0)
 365                        ret += len;
 366                if (ret > PAGE_SIZE) {
 367                        ret = PAGE_SIZE;
 368                        break;
 369                }
 370        }
 371
 372        ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
 373
 374        kfree(buf);
 375
 376        return ret;
 377}
 378
 379static const struct file_operations dai_list_fops = {
 380        .read = dai_list_read_file,
 381        .llseek = default_llseek,/* read accesses f_pos */
 382};
 383
 384static ssize_t platform_list_read_file(struct file *file,
 385                                       char __user *user_buf,
 386                                       size_t count, loff_t *ppos)
 387{
 388        char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 389        ssize_t len, ret = 0;
 390        struct snd_soc_platform *platform;
 391
 392        if (!buf)
 393                return -ENOMEM;
 394
 395        list_for_each_entry(platform, &platform_list, list) {
 396                len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
 397                               platform->name);
 398                if (len >= 0)
 399                        ret += len;
 400                if (ret > PAGE_SIZE) {
 401                        ret = PAGE_SIZE;
 402                        break;
 403                }
 404        }
 405
 406        ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
 407
 408        kfree(buf);
 409
 410        return ret;
 411}
 412
 413static const struct file_operations platform_list_fops = {
 414        .read = platform_list_read_file,
 415        .llseek = default_llseek,/* read accesses f_pos */
 416};
 417
 418static void soc_init_card_debugfs(struct snd_soc_card *card)
 419{
 420        card->debugfs_card_root = debugfs_create_dir(card->name,
 421                                                     snd_soc_debugfs_root);
 422        if (!card->debugfs_card_root) {
 423                dev_warn(card->dev,
 424                         "ASoC: Failed to create card debugfs directory\n");
 425                return;
 426        }
 427
 428        card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
 429                                                    card->debugfs_card_root,
 430                                                    &card->pop_time);
 431        if (!card->debugfs_pop_time)
 432                dev_warn(card->dev,
 433                       "Failed to create pop time debugfs file\n");
 434}
 435
 436static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
 437{
 438        debugfs_remove_recursive(card->debugfs_card_root);
 439}
 440
 441#else
 442
 443static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
 444{
 445}
 446
 447static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
 448{
 449}
 450
 451static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
 452{
 453}
 454
 455static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
 456{
 457}
 458
 459static inline void soc_init_card_debugfs(struct snd_soc_card *card)
 460{
 461}
 462
 463static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
 464{
 465}
 466#endif
 467
 468struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
 469                const char *dai_link, int stream)
 470{
 471        int i;
 472
 473        for (i = 0; i < card->num_links; i++) {
 474                if (card->rtd[i].dai_link->no_pcm &&
 475                        !strcmp(card->rtd[i].dai_link->name, dai_link))
 476                        return card->rtd[i].pcm->streams[stream].substream;
 477        }
 478        dev_dbg(card->dev, "failed to find dai link %s\n", dai_link);
 479        return NULL;
 480}
 481EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
 482
 483struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
 484                const char *dai_link)
 485{
 486        int i;
 487
 488        for (i = 0; i < card->num_links; i++) {
 489                if (!strcmp(card->rtd[i].dai_link->name, dai_link))
 490                        return &card->rtd[i];
 491        }
 492        dev_dbg(card->dev, "failed to find rtd %s\n", dai_link);
 493        return NULL;
 494}
 495EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
 496
 497#ifdef CONFIG_SND_SOC_AC97_BUS
 498/* unregister ac97 codec */
 499static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
 500{
 501        if (codec->ac97->dev.bus)
 502                device_unregister(&codec->ac97->dev);
 503        return 0;
 504}
 505
 506/* stop no dev release warning */
 507static void soc_ac97_device_release(struct device *dev){}
 508
 509/* register ac97 codec to bus */
 510static int soc_ac97_dev_register(struct snd_soc_codec *codec)
 511{
 512        int err;
 513
 514        codec->ac97->dev.bus = &ac97_bus_type;
 515        codec->ac97->dev.parent = codec->card->dev;
 516        codec->ac97->dev.release = soc_ac97_device_release;
 517
 518        dev_set_name(&codec->ac97->dev, "%d-%d:%s",
 519                     codec->card->snd_card->number, 0, codec->name);
 520        err = device_register(&codec->ac97->dev);
 521        if (err < 0) {
 522                snd_printk(KERN_ERR "Can't register ac97 bus\n");
 523                codec->ac97->dev.bus = NULL;
 524                return err;
 525        }
 526        return 0;
 527}
 528#endif
 529
 530#ifdef CONFIG_PM_SLEEP
 531/* powers down audio subsystem for suspend */
 532int snd_soc_suspend(struct device *dev)
 533{
 534        struct snd_soc_card *card = dev_get_drvdata(dev);
 535        struct snd_soc_codec *codec;
 536        int i;
 537
 538        /* If the initialization of this soc device failed, there is no codec
 539         * associated with it. Just bail out in this case.
 540         */
 541        if (list_empty(&card->codec_dev_list))
 542                return 0;
 543
 544        /* Due to the resume being scheduled into a workqueue we could
 545        * suspend before that's finished - wait for it to complete.
 546         */
 547        snd_power_lock(card->snd_card);
 548        snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
 549        snd_power_unlock(card->snd_card);
 550
 551        /* we're going to block userspace touching us until resume completes */
 552        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
 553
 554        /* mute any active DACs */
 555        for (i = 0; i < card->num_rtd; i++) {
 556                struct snd_soc_dai *dai = card->rtd[i].codec_dai;
 557                struct snd_soc_dai_driver *drv = dai->driver;
 558
 559                if (card->rtd[i].dai_link->ignore_suspend)
 560                        continue;
 561
 562                if (drv->ops->digital_mute && dai->playback_active)
 563                        drv->ops->digital_mute(dai, 1);
 564        }
 565
 566        /* suspend all pcms */
 567        for (i = 0; i < card->num_rtd; i++) {
 568                if (card->rtd[i].dai_link->ignore_suspend)
 569                        continue;
 570
 571                snd_pcm_suspend_all(card->rtd[i].pcm);
 572        }
 573
 574        if (card->suspend_pre)
 575                card->suspend_pre(card);
 576
 577        for (i = 0; i < card->num_rtd; i++) {
 578                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 579                struct snd_soc_platform *platform = card->rtd[i].platform;
 580
 581                if (card->rtd[i].dai_link->ignore_suspend)
 582                        continue;
 583
 584                if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
 585                        cpu_dai->driver->suspend(cpu_dai);
 586                if (platform->driver->suspend && !platform->suspended) {
 587                        platform->driver->suspend(cpu_dai);
 588                        platform->suspended = 1;
 589                }
 590        }
 591
 592        /* close any waiting streams and save state */
 593        for (i = 0; i < card->num_rtd; i++) {
 594                flush_delayed_work_sync(&card->rtd[i].delayed_work);
 595                card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
 596        }
 597
 598        for (i = 0; i < card->num_rtd; i++) {
 599
 600                if (card->rtd[i].dai_link->ignore_suspend)
 601                        continue;
 602
 603                snd_soc_dapm_stream_event(&card->rtd[i],
 604                                          SNDRV_PCM_STREAM_PLAYBACK,
 605                                          SND_SOC_DAPM_STREAM_SUSPEND);
 606
 607                snd_soc_dapm_stream_event(&card->rtd[i],
 608                                          SNDRV_PCM_STREAM_CAPTURE,
 609                                          SND_SOC_DAPM_STREAM_SUSPEND);
 610        }
 611
 612        /* suspend all CODECs */
 613        list_for_each_entry(codec, &card->codec_dev_list, card_list) {
 614                /* If there are paths active then the CODEC will be held with
 615                 * bias _ON and should not be suspended. */
 616                if (!codec->suspended && codec->driver->suspend) {
 617                        switch (codec->dapm.bias_level) {
 618                        case SND_SOC_BIAS_STANDBY:
 619                                /*
 620                                 * If the CODEC is capable of idle
 621                                 * bias off then being in STANDBY
 622                                 * means it's doing something,
 623                                 * otherwise fall through.
 624                                 */
 625                                if (codec->dapm.idle_bias_off) {
 626                                        dev_dbg(codec->dev,
 627                                                "idle_bias_off CODEC on over suspend\n");
 628                                        break;
 629                                }
 630                        case SND_SOC_BIAS_OFF:
 631                                codec->driver->suspend(codec);
 632                                codec->suspended = 1;
 633                                codec->cache_sync = 1;
 634                                break;
 635                        default:
 636                                dev_dbg(codec->dev, "CODEC is on over suspend\n");
 637                                break;
 638                        }
 639                }
 640        }
 641
 642        for (i = 0; i < card->num_rtd; i++) {
 643                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 644
 645                if (card->rtd[i].dai_link->ignore_suspend)
 646                        continue;
 647
 648                if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
 649                        cpu_dai->driver->suspend(cpu_dai);
 650        }
 651
 652        if (card->suspend_post)
 653                card->suspend_post(card);
 654
 655        return 0;
 656}
 657EXPORT_SYMBOL_GPL(snd_soc_suspend);
 658
 659/* deferred resume work, so resume can complete before we finished
 660 * setting our codec back up, which can be very slow on I2C
 661 */
 662static void soc_resume_deferred(struct work_struct *work)
 663{
 664        struct snd_soc_card *card =
 665                        container_of(work, struct snd_soc_card, deferred_resume_work);
 666        struct snd_soc_codec *codec;
 667        int i;
 668
 669        /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
 670         * so userspace apps are blocked from touching us
 671         */
 672
 673        dev_dbg(card->dev, "starting resume work\n");
 674
 675        /* Bring us up into D2 so that DAPM starts enabling things */
 676        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
 677
 678        if (card->resume_pre)
 679                card->resume_pre(card);
 680
 681        /* resume AC97 DAIs */
 682        for (i = 0; i < card->num_rtd; i++) {
 683                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 684
 685                if (card->rtd[i].dai_link->ignore_suspend)
 686                        continue;
 687
 688                if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
 689                        cpu_dai->driver->resume(cpu_dai);
 690        }
 691
 692        list_for_each_entry(codec, &card->codec_dev_list, card_list) {
 693                /* If the CODEC was idle over suspend then it will have been
 694                 * left with bias OFF or STANDBY and suspended so we must now
 695                 * resume.  Otherwise the suspend was suppressed.
 696                 */
 697                if (codec->driver->resume && codec->suspended) {
 698                        switch (codec->dapm.bias_level) {
 699                        case SND_SOC_BIAS_STANDBY:
 700                        case SND_SOC_BIAS_OFF:
 701                                codec->driver->resume(codec);
 702                                codec->suspended = 0;
 703                                break;
 704                        default:
 705                                dev_dbg(codec->dev, "CODEC was on over suspend\n");
 706                                break;
 707                        }
 708                }
 709        }
 710
 711        for (i = 0; i < card->num_rtd; i++) {
 712
 713                if (card->rtd[i].dai_link->ignore_suspend)
 714                        continue;
 715
 716                snd_soc_dapm_stream_event(&card->rtd[i],
 717                                          SNDRV_PCM_STREAM_PLAYBACK,
 718                                          SND_SOC_DAPM_STREAM_RESUME);
 719
 720                snd_soc_dapm_stream_event(&card->rtd[i],
 721                                          SNDRV_PCM_STREAM_CAPTURE,
 722                                          SND_SOC_DAPM_STREAM_RESUME);
 723        }
 724
 725        /* unmute any active DACs */
 726        for (i = 0; i < card->num_rtd; i++) {
 727                struct snd_soc_dai *dai = card->rtd[i].codec_dai;
 728                struct snd_soc_dai_driver *drv = dai->driver;
 729
 730                if (card->rtd[i].dai_link->ignore_suspend)
 731                        continue;
 732
 733                if (drv->ops->digital_mute && dai->playback_active)
 734                        drv->ops->digital_mute(dai, 0);
 735        }
 736
 737        for (i = 0; i < card->num_rtd; i++) {
 738                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 739                struct snd_soc_platform *platform = card->rtd[i].platform;
 740
 741                if (card->rtd[i].dai_link->ignore_suspend)
 742                        continue;
 743
 744                if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
 745                        cpu_dai->driver->resume(cpu_dai);
 746                if (platform->driver->resume && platform->suspended) {
 747                        platform->driver->resume(cpu_dai);
 748                        platform->suspended = 0;
 749                }
 750        }
 751
 752        if (card->resume_post)
 753                card->resume_post(card);
 754
 755        dev_dbg(card->dev, "resume work completed\n");
 756
 757        /* userspace can access us now we are back as we were before */
 758        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
 759}
 760
 761/* powers up audio subsystem after a suspend */
 762int snd_soc_resume(struct device *dev)
 763{
 764        struct snd_soc_card *card = dev_get_drvdata(dev);
 765        int i, ac97_control = 0;
 766
 767        /* If the initialization of this soc device failed, there is no codec
 768         * associated with it. Just bail out in this case.
 769         */
 770        if (list_empty(&card->codec_dev_list))
 771                return 0;
 772
 773        /* AC97 devices might have other drivers hanging off them so
 774         * need to resume immediately.  Other drivers don't have that
 775         * problem and may take a substantial amount of time to resume
 776         * due to I/O costs and anti-pop so handle them out of line.
 777         */
 778        for (i = 0; i < card->num_rtd; i++) {
 779                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 780                ac97_control |= cpu_dai->driver->ac97_control;
 781        }
 782        if (ac97_control) {
 783                dev_dbg(dev, "Resuming AC97 immediately\n");
 784                soc_resume_deferred(&card->deferred_resume_work);
 785        } else {
 786                dev_dbg(dev, "Scheduling resume work\n");
 787                if (!schedule_work(&card->deferred_resume_work))
 788                        dev_err(dev, "resume work item may be lost\n");
 789        }
 790
 791        return 0;
 792}
 793EXPORT_SYMBOL_GPL(snd_soc_resume);
 794#else
 795#define snd_soc_suspend NULL
 796#define snd_soc_resume NULL
 797#endif
 798
 799static const struct snd_soc_dai_ops null_dai_ops = {
 800};
 801
 802static int soc_bind_dai_link(struct snd_soc_card *card, int num)
 803{
 804        struct snd_soc_dai_link *dai_link = &card->dai_link[num];
 805        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
 806        struct snd_soc_codec *codec;
 807        struct snd_soc_platform *platform;
 808        struct snd_soc_dai *codec_dai, *cpu_dai;
 809        const char *platform_name;
 810
 811        dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
 812
 813        /* Find CPU DAI from registered DAIs*/
 814        list_for_each_entry(cpu_dai, &dai_list, list) {
 815                if (dai_link->cpu_of_node &&
 816                    (cpu_dai->dev->of_node != dai_link->cpu_of_node))
 817                        continue;
 818                if (dai_link->cpu_name &&
 819                    strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
 820                        continue;
 821                if (dai_link->cpu_dai_name &&
 822                    strcmp(cpu_dai->name, dai_link->cpu_dai_name))
 823                        continue;
 824
 825                rtd->cpu_dai = cpu_dai;
 826        }
 827
 828        if (!rtd->cpu_dai) {
 829                dev_err(card->dev, "CPU DAI %s not registered\n",
 830                        dai_link->cpu_dai_name);
 831                return -EPROBE_DEFER;
 832        }
 833
 834        /* Find CODEC from registered CODECs */
 835        list_for_each_entry(codec, &codec_list, list) {
 836                if (dai_link->codec_of_node) {
 837                        if (codec->dev->of_node != dai_link->codec_of_node)
 838                                continue;
 839                } else {
 840                        if (strcmp(codec->name, dai_link->codec_name))
 841                                continue;
 842                }
 843
 844                rtd->codec = codec;
 845
 846                /*
 847                 * CODEC found, so find CODEC DAI from registered DAIs from
 848                 * this CODEC
 849                 */
 850                list_for_each_entry(codec_dai, &dai_list, list) {
 851                        if (codec->dev == codec_dai->dev &&
 852                                !strcmp(codec_dai->name,
 853                                        dai_link->codec_dai_name)) {
 854
 855                                rtd->codec_dai = codec_dai;
 856                        }
 857                }
 858
 859                if (!rtd->codec_dai) {
 860                        dev_err(card->dev, "CODEC DAI %s not registered\n",
 861                                dai_link->codec_dai_name);
 862                        return -EPROBE_DEFER;
 863                }
 864        }
 865
 866        if (!rtd->codec) {
 867                dev_err(card->dev, "CODEC %s not registered\n",
 868                        dai_link->codec_name);
 869                return -EPROBE_DEFER;
 870        }
 871
 872        /* if there's no platform we match on the empty platform */
 873        platform_name = dai_link->platform_name;
 874        if (!platform_name && !dai_link->platform_of_node)
 875                platform_name = "snd-soc-dummy";
 876
 877        /* find one from the set of registered platforms */
 878        list_for_each_entry(platform, &platform_list, list) {
 879                if (dai_link->platform_of_node) {
 880                        if (platform->dev->of_node !=
 881                            dai_link->platform_of_node)
 882                                continue;
 883                } else {
 884                        if (strcmp(platform->name, platform_name))
 885                                continue;
 886                }
 887
 888                rtd->platform = platform;
 889        }
 890        if (!rtd->platform) {
 891                dev_err(card->dev, "platform %s not registered\n",
 892                        dai_link->platform_name);
 893                return -EPROBE_DEFER;
 894        }
 895
 896        card->num_rtd++;
 897
 898        return 0;
 899}
 900
 901static int soc_remove_platform(struct snd_soc_platform *platform)
 902{
 903        int ret;
 904
 905        if (platform->driver->remove) {
 906                ret = platform->driver->remove(platform);
 907                if (ret < 0)
 908                        pr_err("asoc: failed to remove %s: %d\n",
 909                                platform->name, ret);
 910        }
 911
 912        /* Make sure all DAPM widgets are freed */
 913        snd_soc_dapm_free(&platform->dapm);
 914
 915        soc_cleanup_platform_debugfs(platform);
 916        platform->probed = 0;
 917        list_del(&platform->card_list);
 918        module_put(platform->dev->driver->owner);
 919
 920        return 0;
 921}
 922
 923static void soc_remove_codec(struct snd_soc_codec *codec)
 924{
 925        int err;
 926
 927        if (codec->driver->remove) {
 928                err = codec->driver->remove(codec);
 929                if (err < 0)
 930                        dev_err(codec->dev,
 931                                "asoc: failed to remove %s: %d\n",
 932                                codec->name, err);
 933        }
 934
 935        /* Make sure all DAPM widgets are freed */
 936        snd_soc_dapm_free(&codec->dapm);
 937
 938        soc_cleanup_codec_debugfs(codec);
 939        codec->probed = 0;
 940        list_del(&codec->card_list);
 941        module_put(codec->dev->driver->owner);
 942}
 943
 944static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
 945{
 946        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
 947        struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
 948        int err;
 949
 950        /* unregister the rtd device */
 951        if (rtd->dev_registered) {
 952                device_remove_file(rtd->dev, &dev_attr_pmdown_time);
 953                device_remove_file(rtd->dev, &dev_attr_codec_reg);
 954                device_unregister(rtd->dev);
 955                rtd->dev_registered = 0;
 956        }
 957
 958        /* remove the CODEC DAI */
 959        if (codec_dai && codec_dai->probed &&
 960                        codec_dai->driver->remove_order == order) {
 961                if (codec_dai->driver->remove) {
 962                        err = codec_dai->driver->remove(codec_dai);
 963                        if (err < 0)
 964                                pr_err("asoc: failed to remove %s: %d\n",
 965                                                        codec_dai->name, err);
 966                }
 967                codec_dai->probed = 0;
 968                list_del(&codec_dai->card_list);
 969        }
 970
 971        /* remove the cpu_dai */
 972        if (cpu_dai && cpu_dai->probed &&
 973                        cpu_dai->driver->remove_order == order) {
 974                if (cpu_dai->driver->remove) {
 975                        err = cpu_dai->driver->remove(cpu_dai);
 976                        if (err < 0)
 977                                pr_err("asoc: failed to remove %s: %d\n",
 978                                                        cpu_dai->name, err);
 979                }
 980                cpu_dai->probed = 0;
 981                list_del(&cpu_dai->card_list);
 982
 983                if (!cpu_dai->codec) {
 984                        snd_soc_dapm_free(&cpu_dai->dapm);
 985                        module_put(cpu_dai->dev->driver->owner);
 986                }
 987        }
 988}
 989
 990static void soc_remove_link_components(struct snd_soc_card *card, int num,
 991                                       int order)
 992{
 993        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
 994        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
 995        struct snd_soc_dai *codec_dai = rtd->codec_dai;
 996        struct snd_soc_platform *platform = rtd->platform;
 997        struct snd_soc_codec *codec;
 998
 999        /* remove the platform */
1000        if (platform && platform->probed &&
1001            platform->driver->remove_order == order) {
1002                soc_remove_platform(platform);
1003        }
1004
1005        /* remove the CODEC-side CODEC */
1006        if (codec_dai) {
1007                codec = codec_dai->codec;
1008                if (codec && codec->probed &&
1009                    codec->driver->remove_order == order)
1010                        soc_remove_codec(codec);
1011        }
1012
1013        /* remove any CPU-side CODEC */
1014        if (cpu_dai) {
1015                codec = cpu_dai->codec;
1016                if (codec && codec->probed &&
1017                    codec->driver->remove_order == order)
1018                        soc_remove_codec(codec);
1019        }
1020}
1021
1022static void soc_remove_dai_links(struct snd_soc_card *card)
1023{
1024        int dai, order;
1025
1026        for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1027                        order++) {
1028                for (dai = 0; dai < card->num_rtd; dai++)
1029                        soc_remove_link_dais(card, dai, order);
1030        }
1031
1032        for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1033                        order++) {
1034                for (dai = 0; dai < card->num_rtd; dai++)
1035                        soc_remove_link_components(card, dai, order);
1036        }
1037
1038        card->num_rtd = 0;
1039}
1040
1041static void soc_set_name_prefix(struct snd_soc_card *card,
1042                                struct snd_soc_codec *codec)
1043{
1044        int i;
1045
1046        if (card->codec_conf == NULL)
1047                return;
1048
1049        for (i = 0; i < card->num_configs; i++) {
1050                struct snd_soc_codec_conf *map = &card->codec_conf[i];
1051                if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1052                        codec->name_prefix = map->name_prefix;
1053                        break;
1054                }
1055        }
1056}
1057
1058static int soc_probe_codec(struct snd_soc_card *card,
1059                           struct snd_soc_codec *codec)
1060{
1061        int ret = 0;
1062        const struct snd_soc_codec_driver *driver = codec->driver;
1063        struct snd_soc_dai *dai;
1064
1065        codec->card = card;
1066        codec->dapm.card = card;
1067        soc_set_name_prefix(card, codec);
1068
1069        if (!try_module_get(codec->dev->driver->owner))
1070                return -ENODEV;
1071
1072        soc_init_codec_debugfs(codec);
1073
1074        if (driver->dapm_widgets)
1075                snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1076                                          driver->num_dapm_widgets);
1077
1078        /* Create DAPM widgets for each DAI stream */
1079        list_for_each_entry(dai, &dai_list, list) {
1080                if (dai->dev != codec->dev)
1081                        continue;
1082
1083                snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1084        }
1085
1086        codec->dapm.idle_bias_off = driver->idle_bias_off;
1087
1088        if (driver->probe) {
1089                ret = driver->probe(codec);
1090                if (ret < 0) {
1091                        dev_err(codec->dev,
1092                                "asoc: failed to probe CODEC %s: %d\n",
1093                                codec->name, ret);
1094                        goto err_probe;
1095                }
1096        }
1097
1098        /* If the driver didn't set I/O up try regmap */
1099        if (!codec->write && dev_get_regmap(codec->dev, NULL))
1100                snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1101
1102        if (driver->controls)
1103                snd_soc_add_codec_controls(codec, driver->controls,
1104                                     driver->num_controls);
1105        if (driver->dapm_routes)
1106                snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1107                                        driver->num_dapm_routes);
1108
1109        /* mark codec as probed and add to card codec list */
1110        codec->probed = 1;
1111        list_add(&codec->card_list, &card->codec_dev_list);
1112        list_add(&codec->dapm.list, &card->dapm_list);
1113
1114        return 0;
1115
1116err_probe:
1117        soc_cleanup_codec_debugfs(codec);
1118        module_put(codec->dev->driver->owner);
1119
1120        return ret;
1121}
1122
1123static int soc_probe_platform(struct snd_soc_card *card,
1124                           struct snd_soc_platform *platform)
1125{
1126        int ret = 0;
1127        const struct snd_soc_platform_driver *driver = platform->driver;
1128        struct snd_soc_dai *dai;
1129
1130        platform->card = card;
1131        platform->dapm.card = card;
1132
1133        if (!try_module_get(platform->dev->driver->owner))
1134                return -ENODEV;
1135
1136        soc_init_platform_debugfs(platform);
1137
1138        if (driver->dapm_widgets)
1139                snd_soc_dapm_new_controls(&platform->dapm,
1140                        driver->dapm_widgets, driver->num_dapm_widgets);
1141
1142        /* Create DAPM widgets for each DAI stream */
1143        list_for_each_entry(dai, &dai_list, list) {
1144                if (dai->dev != platform->dev)
1145                        continue;
1146
1147                snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1148        }
1149
1150        platform->dapm.idle_bias_off = 1;
1151
1152        if (driver->probe) {
1153                ret = driver->probe(platform);
1154                if (ret < 0) {
1155                        dev_err(platform->dev,
1156                                "asoc: failed to probe platform %s: %d\n",
1157                                platform->name, ret);
1158                        goto err_probe;
1159                }
1160        }
1161
1162        if (driver->controls)
1163                snd_soc_add_platform_controls(platform, driver->controls,
1164                                     driver->num_controls);
1165        if (driver->dapm_routes)
1166                snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1167                                        driver->num_dapm_routes);
1168
1169        /* mark platform as probed and add to card platform list */
1170        platform->probed = 1;
1171        list_add(&platform->card_list, &card->platform_dev_list);
1172        list_add(&platform->dapm.list, &card->dapm_list);
1173
1174        return 0;
1175
1176err_probe:
1177        soc_cleanup_platform_debugfs(platform);
1178        module_put(platform->dev->driver->owner);
1179
1180        return ret;
1181}
1182
1183static void rtd_release(struct device *dev)
1184{
1185        kfree(dev);
1186}
1187
1188static int soc_post_component_init(struct snd_soc_card *card,
1189                                   struct snd_soc_codec *codec,
1190                                   int num, int dailess)
1191{
1192        struct snd_soc_dai_link *dai_link = NULL;
1193        struct snd_soc_aux_dev *aux_dev = NULL;
1194        struct snd_soc_pcm_runtime *rtd;
1195        const char *temp, *name;
1196        int ret = 0;
1197
1198        if (!dailess) {
1199                dai_link = &card->dai_link[num];
1200                rtd = &card->rtd[num];
1201                name = dai_link->name;
1202        } else {
1203                aux_dev = &card->aux_dev[num];
1204                rtd = &card->rtd_aux[num];
1205                name = aux_dev->name;
1206        }
1207        rtd->card = card;
1208
1209        /* Make sure all DAPM widgets are instantiated */
1210        snd_soc_dapm_new_widgets(&codec->dapm);
1211
1212        /* machine controls, routes and widgets are not prefixed */
1213        temp = codec->name_prefix;
1214        codec->name_prefix = NULL;
1215
1216        /* do machine specific initialization */
1217        if (!dailess && dai_link->init)
1218                ret = dai_link->init(rtd);
1219        else if (dailess && aux_dev->init)
1220                ret = aux_dev->init(&codec->dapm);
1221        if (ret < 0) {
1222                dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1223                return ret;
1224        }
1225        codec->name_prefix = temp;
1226
1227        /* register the rtd device */
1228        rtd->codec = codec;
1229
1230        rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1231        if (!rtd->dev)
1232                return -ENOMEM;
1233        device_initialize(rtd->dev);
1234        rtd->dev->parent = card->dev;
1235        rtd->dev->release = rtd_release;
1236        rtd->dev->init_name = name;
1237        dev_set_drvdata(rtd->dev, rtd);
1238        mutex_init(&rtd->pcm_mutex);
1239        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1240        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1241        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1242        INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1243        ret = device_add(rtd->dev);
1244        if (ret < 0) {
1245                dev_err(card->dev,
1246                        "asoc: failed to register runtime device: %d\n", ret);
1247                return ret;
1248        }
1249        rtd->dev_registered = 1;
1250
1251        /* add DAPM sysfs entries for this codec */
1252        ret = snd_soc_dapm_sys_add(rtd->dev);
1253        if (ret < 0)
1254                dev_err(codec->dev,
1255                        "asoc: failed to add codec dapm sysfs entries: %d\n",
1256                        ret);
1257
1258        /* add codec sysfs entries */
1259        ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1260        if (ret < 0)
1261                dev_err(codec->dev,
1262                        "asoc: failed to add codec sysfs files: %d\n", ret);
1263
1264#ifdef CONFIG_DEBUG_FS
1265        /* add DPCM sysfs entries */
1266        if (!dailess && !dai_link->dynamic)
1267                goto out;
1268
1269        ret = soc_dpcm_debugfs_add(rtd);
1270        if (ret < 0)
1271                dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret);
1272
1273out:
1274#endif
1275        return 0;
1276}
1277
1278static int soc_probe_link_components(struct snd_soc_card *card, int num,
1279                                     int order)
1280{
1281        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1282        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1283        struct snd_soc_dai *codec_dai = rtd->codec_dai;
1284        struct snd_soc_platform *platform = rtd->platform;
1285        int ret;
1286
1287        /* probe the CPU-side component, if it is a CODEC */
1288        if (cpu_dai->codec &&
1289            !cpu_dai->codec->probed &&
1290            cpu_dai->codec->driver->probe_order == order) {
1291                ret = soc_probe_codec(card, cpu_dai->codec);
1292                if (ret < 0)
1293                        return ret;
1294        }
1295
1296        /* probe the CODEC-side component */
1297        if (!codec_dai->codec->probed &&
1298            codec_dai->codec->driver->probe_order == order) {
1299                ret = soc_probe_codec(card, codec_dai->codec);
1300                if (ret < 0)
1301                        return ret;
1302        }
1303
1304        /* probe the platform */
1305        if (!platform->probed &&
1306            platform->driver->probe_order == order) {
1307                ret = soc_probe_platform(card, platform);
1308                if (ret < 0)
1309                        return ret;
1310        }
1311
1312        return 0;
1313}
1314
1315static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1316{
1317        struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1318        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1319        struct snd_soc_codec *codec = rtd->codec;
1320        struct snd_soc_platform *platform = rtd->platform;
1321        struct snd_soc_dai *codec_dai = rtd->codec_dai;
1322        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1323        struct snd_soc_dapm_widget *play_w, *capture_w;
1324        int ret;
1325
1326        dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1327                        card->name, num, order);
1328
1329        /* config components */
1330        cpu_dai->platform = platform;
1331        codec_dai->card = card;
1332        cpu_dai->card = card;
1333
1334        /* set default power off timeout */
1335        rtd->pmdown_time = pmdown_time;
1336
1337        /* probe the cpu_dai */
1338        if (!cpu_dai->probed &&
1339                        cpu_dai->driver->probe_order == order) {
1340                if (!cpu_dai->codec) {
1341                        cpu_dai->dapm.card = card;
1342                        if (!try_module_get(cpu_dai->dev->driver->owner))
1343                                return -ENODEV;
1344
1345                        list_add(&cpu_dai->dapm.list, &card->dapm_list);
1346                        snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1347                }
1348
1349                if (cpu_dai->driver->probe) {
1350                        ret = cpu_dai->driver->probe(cpu_dai);
1351                        if (ret < 0) {
1352                                pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1353                                                        cpu_dai->name, ret);
1354                                module_put(cpu_dai->dev->driver->owner);
1355                                return ret;
1356                        }
1357                }
1358                cpu_dai->probed = 1;
1359                /* mark cpu_dai as probed and add to card dai list */
1360                list_add(&cpu_dai->card_list, &card->dai_dev_list);
1361        }
1362
1363        /* probe the CODEC DAI */
1364        if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1365                if (codec_dai->driver->probe) {
1366                        ret = codec_dai->driver->probe(codec_dai);
1367                        if (ret < 0) {
1368                                pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1369                                                        codec_dai->name, ret);
1370                                return ret;
1371                        }
1372                }
1373
1374                /* mark codec_dai as probed and add to card dai list */
1375                codec_dai->probed = 1;
1376                list_add(&codec_dai->card_list, &card->dai_dev_list);
1377        }
1378
1379        /* complete DAI probe during last probe */
1380        if (order != SND_SOC_COMP_ORDER_LAST)
1381                return 0;
1382
1383        ret = soc_post_component_init(card, codec, num, 0);
1384        if (ret)
1385                return ret;
1386
1387        ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1388        if (ret < 0)
1389                pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
1390
1391        if (!dai_link->params) {
1392                /* create the pcm */
1393                ret = soc_new_pcm(rtd, num);
1394                if (ret < 0) {
1395                        pr_err("asoc: can't create pcm %s :%d\n",
1396                               dai_link->stream_name, ret);
1397                        return ret;
1398                }
1399        } else {
1400                /* link the DAI widgets */
1401                play_w = codec_dai->playback_widget;
1402                capture_w = cpu_dai->capture_widget;
1403                if (play_w && capture_w) {
1404                        ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1405                                                   capture_w, play_w);
1406                        if (ret != 0) {
1407                                dev_err(card->dev, "Can't link %s to %s: %d\n",
1408                                        play_w->name, capture_w->name, ret);
1409                                return ret;
1410                        }
1411                }
1412
1413                play_w = cpu_dai->playback_widget;
1414                capture_w = codec_dai->capture_widget;
1415                if (play_w && capture_w) {
1416                        ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1417                                                   capture_w, play_w);
1418                        if (ret != 0) {
1419                                dev_err(card->dev, "Can't link %s to %s: %d\n",
1420                                        play_w->name, capture_w->name, ret);
1421                                return ret;
1422                        }
1423                }
1424        }
1425
1426        /* add platform data for AC97 devices */
1427        if (rtd->codec_dai->driver->ac97_control)
1428                snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1429
1430        return 0;
1431}
1432
1433#ifdef CONFIG_SND_SOC_AC97_BUS
1434static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1435{
1436        int ret;
1437
1438        /* Only instantiate AC97 if not already done by the adaptor
1439         * for the generic AC97 subsystem.
1440         */
1441        if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1442                /*
1443                 * It is possible that the AC97 device is already registered to
1444                 * the device subsystem. This happens when the device is created
1445                 * via snd_ac97_mixer(). Currently only SoC codec that does so
1446                 * is the generic AC97 glue but others migh emerge.
1447                 *
1448                 * In those cases we don't try to register the device again.
1449                 */
1450                if (!rtd->codec->ac97_created)
1451                        return 0;
1452
1453                ret = soc_ac97_dev_register(rtd->codec);
1454                if (ret < 0) {
1455                        pr_err("asoc: AC97 device register failed:%d\n", ret);
1456                        return ret;
1457                }
1458
1459                rtd->codec->ac97_registered = 1;
1460        }
1461        return 0;
1462}
1463
1464static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1465{
1466        if (codec->ac97_registered) {
1467                soc_ac97_dev_unregister(codec);
1468                codec->ac97_registered = 0;
1469        }
1470}
1471#endif
1472
1473static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1474{
1475        struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1476        struct snd_soc_codec *codec;
1477
1478        /* find CODEC from registered CODECs*/
1479        list_for_each_entry(codec, &codec_list, list) {
1480                if (!strcmp(codec->name, aux_dev->codec_name))
1481                        return 0;
1482        }
1483
1484        dev_err(card->dev, "%s not registered\n", aux_dev->codec_name);
1485
1486        return -EPROBE_DEFER;
1487}
1488
1489static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1490{
1491        struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1492        struct snd_soc_codec *codec;
1493        int ret = -ENODEV;
1494
1495        /* find CODEC from registered CODECs*/
1496        list_for_each_entry(codec, &codec_list, list) {
1497                if (!strcmp(codec->name, aux_dev->codec_name)) {
1498                        if (codec->probed) {
1499                                dev_err(codec->dev,
1500                                        "asoc: codec already probed");
1501                                ret = -EBUSY;
1502                                goto out;
1503                        }
1504                        goto found;
1505                }
1506        }
1507        /* codec not found */
1508        dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1509        return -EPROBE_DEFER;
1510
1511found:
1512        ret = soc_probe_codec(card, codec);
1513        if (ret < 0)
1514                return ret;
1515
1516        ret = soc_post_component_init(card, codec, num, 1);
1517
1518out:
1519        return ret;
1520}
1521
1522static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1523{
1524        struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1525        struct snd_soc_codec *codec = rtd->codec;
1526
1527        /* unregister the rtd device */
1528        if (rtd->dev_registered) {
1529                device_remove_file(rtd->dev, &dev_attr_codec_reg);
1530                device_del(rtd->dev);
1531                rtd->dev_registered = 0;
1532        }
1533
1534        if (codec && codec->probed)
1535                soc_remove_codec(codec);
1536}
1537
1538static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1539                                    enum snd_soc_compress_type compress_type)
1540{
1541        int ret;
1542
1543        if (codec->cache_init)
1544                return 0;
1545
1546        /* override the compress_type if necessary */
1547        if (compress_type && codec->compress_type != compress_type)
1548                codec->compress_type = compress_type;
1549        ret = snd_soc_cache_init(codec);
1550        if (ret < 0) {
1551                dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1552                        ret);
1553                return ret;
1554        }
1555        codec->cache_init = 1;
1556        return 0;
1557}
1558
1559static int snd_soc_instantiate_card(struct snd_soc_card *card)
1560{
1561        struct snd_soc_codec *codec;
1562        struct snd_soc_codec_conf *codec_conf;
1563        enum snd_soc_compress_type compress_type;
1564        struct snd_soc_dai_link *dai_link;
1565        int ret, i, order, dai_fmt;
1566
1567        mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1568
1569        /* bind DAIs */
1570        for (i = 0; i < card->num_links; i++) {
1571                ret = soc_bind_dai_link(card, i);
1572                if (ret != 0)
1573                        goto base_error;
1574        }
1575
1576        /* check aux_devs too */
1577        for (i = 0; i < card->num_aux_devs; i++) {
1578                ret = soc_check_aux_dev(card, i);
1579                if (ret != 0)
1580                        goto base_error;
1581        }
1582
1583        /* initialize the register cache for each available codec */
1584        list_for_each_entry(codec, &codec_list, list) {
1585                if (codec->cache_init)
1586                        continue;
1587                /* by default we don't override the compress_type */
1588                compress_type = 0;
1589                /* check to see if we need to override the compress_type */
1590                for (i = 0; i < card->num_configs; ++i) {
1591                        codec_conf = &card->codec_conf[i];
1592                        if (!strcmp(codec->name, codec_conf->dev_name)) {
1593                                compress_type = codec_conf->compress_type;
1594                                if (compress_type && compress_type
1595                                    != codec->compress_type)
1596                                        break;
1597                        }
1598                }
1599                ret = snd_soc_init_codec_cache(codec, compress_type);
1600                if (ret < 0)
1601                        goto base_error;
1602        }
1603
1604        /* card bind complete so register a sound card */
1605        ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1606                        card->owner, 0, &card->snd_card);
1607        if (ret < 0) {
1608                pr_err("asoc: can't create sound card for card %s: %d\n",
1609                        card->name, ret);
1610                goto base_error;
1611        }
1612        card->snd_card->dev = card->dev;
1613
1614        card->dapm.bias_level = SND_SOC_BIAS_OFF;
1615        card->dapm.dev = card->dev;
1616        card->dapm.card = card;
1617        list_add(&card->dapm.list, &card->dapm_list);
1618
1619#ifdef CONFIG_DEBUG_FS
1620        snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1621#endif
1622
1623#ifdef CONFIG_PM_SLEEP
1624        /* deferred resume work */
1625        INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1626#endif
1627
1628        if (card->dapm_widgets)
1629                snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1630                                          card->num_dapm_widgets);
1631
1632        /* initialise the sound card only once */
1633        if (card->probe) {
1634                ret = card->probe(card);
1635                if (ret < 0)
1636                        goto card_probe_error;
1637        }
1638
1639        /* probe all components used by DAI links on this card */
1640        for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1641                        order++) {
1642                for (i = 0; i < card->num_links; i++) {
1643                        ret = soc_probe_link_components(card, i, order);
1644                        if (ret < 0) {
1645                                pr_err("asoc: failed to instantiate card %s: %d\n",
1646                                       card->name, ret);
1647                                goto probe_dai_err;
1648                        }
1649                }
1650        }
1651
1652        /* probe all DAI links on this card */
1653        for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1654                        order++) {
1655                for (i = 0; i < card->num_links; i++) {
1656                        ret = soc_probe_link_dais(card, i, order);
1657                        if (ret < 0) {
1658                                pr_err("asoc: failed to instantiate card %s: %d\n",
1659                                       card->name, ret);
1660                                goto probe_dai_err;
1661                        }
1662                }
1663        }
1664
1665        for (i = 0; i < card->num_aux_devs; i++) {
1666                ret = soc_probe_aux_dev(card, i);
1667                if (ret < 0) {
1668                        pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1669                               card->name, ret);
1670                        goto probe_aux_dev_err;
1671                }
1672        }
1673
1674        snd_soc_dapm_link_dai_widgets(card);
1675
1676        if (card->controls)
1677                snd_soc_add_card_controls(card, card->controls, card->num_controls);
1678
1679        if (card->dapm_routes)
1680                snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1681                                        card->num_dapm_routes);
1682
1683        snd_soc_dapm_new_widgets(&card->dapm);
1684
1685        for (i = 0; i < card->num_links; i++) {
1686                dai_link = &card->dai_link[i];
1687                dai_fmt = dai_link->dai_fmt;
1688
1689                if (dai_fmt) {
1690                        ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1691                                                  dai_fmt);
1692                        if (ret != 0 && ret != -ENOTSUPP)
1693                                dev_warn(card->rtd[i].codec_dai->dev,
1694                                         "Failed to set DAI format: %d\n",
1695                                         ret);
1696                }
1697
1698                /* If this is a regular CPU link there will be a platform */
1699                if (dai_fmt &&
1700                    (dai_link->platform_name || dai_link->platform_of_node)) {
1701                        ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1702                                                  dai_fmt);
1703                        if (ret != 0 && ret != -ENOTSUPP)
1704                                dev_warn(card->rtd[i].cpu_dai->dev,
1705                                         "Failed to set DAI format: %d\n",
1706                                         ret);
1707                } else if (dai_fmt) {
1708                        /* Flip the polarity for the "CPU" end */
1709                        dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1710                        switch (dai_link->dai_fmt &
1711                                SND_SOC_DAIFMT_MASTER_MASK) {
1712                        case SND_SOC_DAIFMT_CBM_CFM:
1713                                dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1714                                break;
1715                        case SND_SOC_DAIFMT_CBM_CFS:
1716                                dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1717                                break;
1718                        case SND_SOC_DAIFMT_CBS_CFM:
1719                                dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1720                                break;
1721                        case SND_SOC_DAIFMT_CBS_CFS:
1722                                dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1723                                break;
1724                        }
1725
1726                        ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1727                                                  dai_fmt);
1728                        if (ret != 0 && ret != -ENOTSUPP)
1729                                dev_warn(card->rtd[i].cpu_dai->dev,
1730                                         "Failed to set DAI format: %d\n",
1731                                         ret);
1732                }
1733        }
1734
1735        snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1736                 "%s", card->name);
1737        snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1738                 "%s", card->long_name ? card->long_name : card->name);
1739        snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1740                 "%s", card->driver_name ? card->driver_name : card->name);
1741        for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1742                switch (card->snd_card->driver[i]) {
1743                case '_':
1744                case '-':
1745                case '\0':
1746                        break;
1747                default:
1748                        if (!isalnum(card->snd_card->driver[i]))
1749                                card->snd_card->driver[i] = '_';
1750                        break;
1751                }
1752        }
1753
1754        if (card->late_probe) {
1755                ret = card->late_probe(card);
1756                if (ret < 0) {
1757                        dev_err(card->dev, "%s late_probe() failed: %d\n",
1758                                card->name, ret);
1759                        goto probe_aux_dev_err;
1760                }
1761        }
1762
1763        snd_soc_dapm_new_widgets(&card->dapm);
1764
1765        if (card->fully_routed)
1766                list_for_each_entry(codec, &card->codec_dev_list, card_list)
1767                        snd_soc_dapm_auto_nc_codec_pins(codec);
1768
1769        ret = snd_card_register(card->snd_card);
1770        if (ret < 0) {
1771                pr_err("asoc: failed to register soundcard for %s: %d\n",
1772                                                        card->name, ret);
1773                goto probe_aux_dev_err;
1774        }
1775
1776#ifdef CONFIG_SND_SOC_AC97_BUS
1777        /* register any AC97 codecs */
1778        for (i = 0; i < card->num_rtd; i++) {
1779                ret = soc_register_ac97_dai_link(&card->rtd[i]);
1780                if (ret < 0) {
1781                        pr_err("asoc: failed to register AC97 %s: %d\n",
1782                                                        card->name, ret);
1783                        while (--i >= 0)
1784                                soc_unregister_ac97_dai_link(card->rtd[i].codec);
1785                        goto probe_aux_dev_err;
1786                }
1787        }
1788#endif
1789
1790        card->instantiated = 1;
1791        snd_soc_dapm_sync(&card->dapm);
1792        mutex_unlock(&card->mutex);
1793
1794        return 0;
1795
1796probe_aux_dev_err:
1797        for (i = 0; i < card->num_aux_devs; i++)
1798                soc_remove_aux_dev(card, i);
1799
1800probe_dai_err:
1801        soc_remove_dai_links(card);
1802
1803card_probe_error:
1804        if (card->remove)
1805                card->remove(card);
1806
1807        snd_card_free(card->snd_card);
1808
1809base_error:
1810        mutex_unlock(&card->mutex);
1811
1812        return ret;
1813}
1814
1815/* probes a new socdev */
1816static int soc_probe(struct platform_device *pdev)
1817{
1818        struct snd_soc_card *card = platform_get_drvdata(pdev);
1819        int ret = 0;
1820
1821        /*
1822         * no card, so machine driver should be registering card
1823         * we should not be here in that case so ret error
1824         */
1825        if (!card)
1826                return -EINVAL;
1827
1828        dev_warn(&pdev->dev,
1829                 "ASoC machine %s should use snd_soc_register_card()\n",
1830                 card->name);
1831
1832        /* Bodge while we unpick instantiation */
1833        card->dev = &pdev->dev;
1834
1835        ret = snd_soc_register_card(card);
1836        if (ret != 0) {
1837                dev_err(&pdev->dev, "Failed to register card\n");
1838                return ret;
1839        }
1840
1841        return 0;
1842}
1843
1844static int soc_cleanup_card_resources(struct snd_soc_card *card)
1845{
1846        int i;
1847
1848        /* make sure any delayed work runs */
1849        for (i = 0; i < card->num_rtd; i++) {
1850                struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1851                flush_delayed_work_sync(&rtd->delayed_work);
1852        }
1853
1854        /* remove auxiliary devices */
1855        for (i = 0; i < card->num_aux_devs; i++)
1856                soc_remove_aux_dev(card, i);
1857
1858        /* remove and free each DAI */
1859        soc_remove_dai_links(card);
1860
1861        soc_cleanup_card_debugfs(card);
1862
1863        /* remove the card */
1864        if (card->remove)
1865                card->remove(card);
1866
1867        snd_soc_dapm_free(&card->dapm);
1868
1869        snd_card_free(card->snd_card);
1870        return 0;
1871
1872}
1873
1874/* removes a socdev */
1875static int soc_remove(struct platform_device *pdev)
1876{
1877        struct snd_soc_card *card = platform_get_drvdata(pdev);
1878
1879        snd_soc_unregister_card(card);
1880        return 0;
1881}
1882
1883int snd_soc_poweroff(struct device *dev)
1884{
1885        struct snd_soc_card *card = dev_get_drvdata(dev);
1886        int i;
1887
1888        if (!card->instantiated)
1889                return 0;
1890
1891        /* Flush out pmdown_time work - we actually do want to run it
1892         * now, we're shutting down so no imminent restart. */
1893        for (i = 0; i < card->num_rtd; i++) {
1894                struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1895                flush_delayed_work_sync(&rtd->delayed_work);
1896        }
1897
1898        snd_soc_dapm_shutdown(card);
1899
1900        return 0;
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1903
1904const struct dev_pm_ops snd_soc_pm_ops = {
1905        .suspend = snd_soc_suspend,
1906        .resume = snd_soc_resume,
1907        .freeze = snd_soc_suspend,
1908        .thaw = snd_soc_resume,
1909        .poweroff = snd_soc_poweroff,
1910        .restore = snd_soc_resume,
1911};
1912EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1913
1914/* ASoC platform driver */
1915static struct platform_driver soc_driver = {
1916        .driver         = {
1917                .name           = "soc-audio",
1918                .owner          = THIS_MODULE,
1919                .pm             = &snd_soc_pm_ops,
1920        },
1921        .probe          = soc_probe,
1922        .remove         = soc_remove,
1923};
1924
1925/**
1926 * snd_soc_codec_volatile_register: Report if a register is volatile.
1927 *
1928 * @codec: CODEC to query.
1929 * @reg: Register to query.
1930 *
1931 * Boolean function indiciating if a CODEC register is volatile.
1932 */
1933int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1934                                    unsigned int reg)
1935{
1936        if (codec->volatile_register)
1937                return codec->volatile_register(codec, reg);
1938        else
1939                return 0;
1940}
1941EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1942
1943/**
1944 * snd_soc_codec_readable_register: Report if a register is readable.
1945 *
1946 * @codec: CODEC to query.
1947 * @reg: Register to query.
1948 *
1949 * Boolean function indicating if a CODEC register is readable.
1950 */
1951int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1952                                    unsigned int reg)
1953{
1954        if (codec->readable_register)
1955                return codec->readable_register(codec, reg);
1956        else
1957                return 1;
1958}
1959EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1960
1961/**
1962 * snd_soc_codec_writable_register: Report if a register is writable.
1963 *
1964 * @codec: CODEC to query.
1965 * @reg: Register to query.
1966 *
1967 * Boolean function indicating if a CODEC register is writable.
1968 */
1969int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1970                                    unsigned int reg)
1971{
1972        if (codec->writable_register)
1973                return codec->writable_register(codec, reg);
1974        else
1975                return 1;
1976}
1977EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1978
1979int snd_soc_platform_read(struct snd_soc_platform *platform,
1980                                        unsigned int reg)
1981{
1982        unsigned int ret;
1983
1984        if (!platform->driver->read) {
1985                dev_err(platform->dev, "platform has no read back\n");
1986                return -1;
1987        }
1988
1989        ret = platform->driver->read(platform, reg);
1990        dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1991        trace_snd_soc_preg_read(platform, reg, ret);
1992
1993        return ret;
1994}
1995EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1996
1997int snd_soc_platform_write(struct snd_soc_platform *platform,
1998                                         unsigned int reg, unsigned int val)
1999{
2000        if (!platform->driver->write) {
2001                dev_err(platform->dev, "platform has no write back\n");
2002                return -1;
2003        }
2004
2005        dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2006        trace_snd_soc_preg_write(platform, reg, val);
2007        return platform->driver->write(platform, reg, val);
2008}
2009EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2010
2011/**
2012 * snd_soc_new_ac97_codec - initailise AC97 device
2013 * @codec: audio codec
2014 * @ops: AC97 bus operations
2015 * @num: AC97 codec number
2016 *
2017 * Initialises AC97 codec resources for use by ad-hoc devices only.
2018 */
2019int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2020        struct snd_ac97_bus_ops *ops, int num)
2021{
2022        mutex_lock(&codec->mutex);
2023
2024        codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2025        if (codec->ac97 == NULL) {
2026                mutex_unlock(&codec->mutex);
2027                return -ENOMEM;
2028        }
2029
2030        codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2031        if (codec->ac97->bus == NULL) {
2032                kfree(codec->ac97);
2033                codec->ac97 = NULL;
2034                mutex_unlock(&codec->mutex);
2035                return -ENOMEM;
2036        }
2037
2038        codec->ac97->bus->ops = ops;
2039        codec->ac97->num = num;
2040
2041        /*
2042         * Mark the AC97 device to be created by us. This way we ensure that the
2043         * device will be registered with the device subsystem later on.
2044         */
2045        codec->ac97_created = 1;
2046
2047        mutex_unlock(&codec->mutex);
2048        return 0;
2049}
2050EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2051
2052/**
2053 * snd_soc_free_ac97_codec - free AC97 codec device
2054 * @codec: audio codec
2055 *
2056 * Frees AC97 codec device resources.
2057 */
2058void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2059{
2060        mutex_lock(&codec->mutex);
2061#ifdef CONFIG_SND_SOC_AC97_BUS
2062        soc_unregister_ac97_dai_link(codec);
2063#endif
2064        kfree(codec->ac97->bus);
2065        kfree(codec->ac97);
2066        codec->ac97 = NULL;
2067        codec->ac97_created = 0;
2068        mutex_unlock(&codec->mutex);
2069}
2070EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2071
2072unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2073{
2074        unsigned int ret;
2075
2076        ret = codec->read(codec, reg);
2077        dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2078        trace_snd_soc_reg_read(codec, reg, ret);
2079
2080        return ret;
2081}
2082EXPORT_SYMBOL_GPL(snd_soc_read);
2083
2084unsigned int snd_soc_write(struct snd_soc_codec *codec,
2085                           unsigned int reg, unsigned int val)
2086{
2087        dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2088        trace_snd_soc_reg_write(codec, reg, val);
2089        return codec->write(codec, reg, val);
2090}
2091EXPORT_SYMBOL_GPL(snd_soc_write);
2092
2093unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2094                                    unsigned int reg, const void *data, size_t len)
2095{
2096        return codec->bulk_write_raw(codec, reg, data, len);
2097}
2098EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2099
2100/**
2101 * snd_soc_update_bits - update codec register bits
2102 * @codec: audio codec
2103 * @reg: codec register
2104 * @mask: register mask
2105 * @value: new value
2106 *
2107 * Writes new register value.
2108 *
2109 * Returns 1 for change, 0 for no change, or negative error code.
2110 */
2111int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2112                                unsigned int mask, unsigned int value)
2113{
2114        bool change;
2115        unsigned int old, new;
2116        int ret;
2117
2118        if (codec->using_regmap) {
2119                ret = regmap_update_bits_check(codec->control_data, reg,
2120                                               mask, value, &change);
2121        } else {
2122                ret = snd_soc_read(codec, reg);
2123                if (ret < 0)
2124                        return ret;
2125
2126                old = ret;
2127                new = (old & ~mask) | (value & mask);
2128                change = old != new;
2129                if (change)
2130                        ret = snd_soc_write(codec, reg, new);
2131        }
2132
2133        if (ret < 0)
2134                return ret;
2135
2136        return change;
2137}
2138EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2139
2140/**
2141 * snd_soc_update_bits_locked - update codec register bits
2142 * @codec: audio codec
2143 * @reg: codec register
2144 * @mask: register mask
2145 * @value: new value
2146 *
2147 * Writes new register value, and takes the codec mutex.
2148 *
2149 * Returns 1 for change else 0.
2150 */
2151int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2152                               unsigned short reg, unsigned int mask,
2153                               unsigned int value)
2154{
2155        int change;
2156
2157        mutex_lock(&codec->mutex);
2158        change = snd_soc_update_bits(codec, reg, mask, value);
2159        mutex_unlock(&codec->mutex);
2160
2161        return change;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2164
2165/**
2166 * snd_soc_test_bits - test register for change
2167 * @codec: audio codec
2168 * @reg: codec register
2169 * @mask: register mask
2170 * @value: new value
2171 *
2172 * Tests a register with a new value and checks if the new value is
2173 * different from the old value.
2174 *
2175 * Returns 1 for change else 0.
2176 */
2177int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2178                                unsigned int mask, unsigned int value)
2179{
2180        int change;
2181        unsigned int old, new;
2182
2183        old = snd_soc_read(codec, reg);
2184        new = (old & ~mask) | value;
2185        change = old != new;
2186
2187        return change;
2188}
2189EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2190
2191/**
2192 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2193 * @substream: the pcm substream
2194 * @hw: the hardware parameters
2195 *
2196 * Sets the substream runtime hardware parameters.
2197 */
2198int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2199        const struct snd_pcm_hardware *hw)
2200{
2201        struct snd_pcm_runtime *runtime = substream->runtime;
2202        runtime->hw.info = hw->info;
2203        runtime->hw.formats = hw->formats;
2204        runtime->hw.period_bytes_min = hw->period_bytes_min;
2205        runtime->hw.period_bytes_max = hw->period_bytes_max;
2206        runtime->hw.periods_min = hw->periods_min;
2207        runtime->hw.periods_max = hw->periods_max;
2208        runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2209        runtime->hw.fifo_size = hw->fifo_size;
2210        return 0;
2211}
2212EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2213
2214/**
2215 * snd_soc_cnew - create new control
2216 * @_template: control template
2217 * @data: control private data
2218 * @long_name: control long name
2219 * @prefix: control name prefix
2220 *
2221 * Create a new mixer control from a template control.
2222 *
2223 * Returns 0 for success, else error.
2224 */
2225struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2226                                  void *data, const char *long_name,
2227                                  const char *prefix)
2228{
2229        struct snd_kcontrol_new template;
2230        struct snd_kcontrol *kcontrol;
2231        char *name = NULL;
2232        int name_len;
2233
2234        memcpy(&template, _template, sizeof(template));
2235        template.index = 0;
2236
2237        if (!long_name)
2238                long_name = template.name;
2239
2240        if (prefix) {
2241                name_len = strlen(long_name) + strlen(prefix) + 2;
2242                name = kmalloc(name_len, GFP_KERNEL);
2243                if (!name)
2244                        return NULL;
2245
2246                snprintf(name, name_len, "%s %s", prefix, long_name);
2247
2248                template.name = name;
2249        } else {
2250                template.name = long_name;
2251        }
2252
2253        kcontrol = snd_ctl_new1(&template, data);
2254
2255        kfree(name);
2256
2257        return kcontrol;
2258}
2259EXPORT_SYMBOL_GPL(snd_soc_cnew);
2260
2261static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2262        const struct snd_kcontrol_new *controls, int num_controls,
2263        const char *prefix, void *data)
2264{
2265        int err, i;
2266
2267        for (i = 0; i < num_controls; i++) {
2268                const struct snd_kcontrol_new *control = &controls[i];
2269                err = snd_ctl_add(card, snd_soc_cnew(control, data,
2270                                                     control->name, prefix));
2271                if (err < 0) {
2272                        dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2273                        return err;
2274                }
2275        }
2276
2277        return 0;
2278}
2279
2280/**
2281 * snd_soc_add_codec_controls - add an array of controls to a codec.
2282 * Convenience function to add a list of controls. Many codecs were
2283 * duplicating this code.
2284 *
2285 * @codec: codec to add controls to
2286 * @controls: array of controls to add
2287 * @num_controls: number of elements in the array
2288 *
2289 * Return 0 for success, else error.
2290 */
2291int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2292        const struct snd_kcontrol_new *controls, int num_controls)
2293{
2294        struct snd_card *card = codec->card->snd_card;
2295
2296        return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2297                        codec->name_prefix, codec);
2298}
2299EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2300
2301/**
2302 * snd_soc_add_platform_controls - add an array of controls to a platform.
2303 * Convenience function to add a list of controls.
2304 *
2305 * @platform: platform to add controls to
2306 * @controls: array of controls to add
2307 * @num_controls: number of elements in the array
2308 *
2309 * Return 0 for success, else error.
2310 */
2311int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2312        const struct snd_kcontrol_new *controls, int num_controls)
2313{
2314        struct snd_card *card = platform->card->snd_card;
2315
2316        return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2317                        NULL, platform);
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2320
2321/**
2322 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2323 * Convenience function to add a list of controls.
2324 *
2325 * @soc_card: SoC card to add controls to
2326 * @controls: array of controls to add
2327 * @num_controls: number of elements in the array
2328 *
2329 * Return 0 for success, else error.
2330 */
2331int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2332        const struct snd_kcontrol_new *controls, int num_controls)
2333{
2334        struct snd_card *card = soc_card->snd_card;
2335
2336        return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2337                        NULL, soc_card);
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2340
2341/**
2342 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2343 * Convienience function to add a list of controls.
2344 *
2345 * @dai: DAI to add controls to
2346 * @controls: array of controls to add
2347 * @num_controls: number of elements in the array
2348 *
2349 * Return 0 for success, else error.
2350 */
2351int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2352        const struct snd_kcontrol_new *controls, int num_controls)
2353{
2354        struct snd_card *card = dai->card->snd_card;
2355
2356        return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2357                        NULL, dai);
2358}
2359EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2360
2361/**
2362 * snd_soc_info_enum_double - enumerated double mixer info callback
2363 * @kcontrol: mixer control
2364 * @uinfo: control element information
2365 *
2366 * Callback to provide information about a double enumerated
2367 * mixer control.
2368 *
2369 * Returns 0 for success.
2370 */
2371int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2372        struct snd_ctl_elem_info *uinfo)
2373{
2374        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2375
2376        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2377        uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2378        uinfo->value.enumerated.items = e->max;
2379
2380        if (uinfo->value.enumerated.item > e->max - 1)
2381                uinfo->value.enumerated.item = e->max - 1;
2382        strcpy(uinfo->value.enumerated.name,
2383                e->texts[uinfo->value.enumerated.item]);
2384        return 0;
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2387
2388/**
2389 * snd_soc_get_enum_double - enumerated double mixer get callback
2390 * @kcontrol: mixer control
2391 * @ucontrol: control element information
2392 *
2393 * Callback to get the value of a double enumerated mixer.
2394 *
2395 * Returns 0 for success.
2396 */
2397int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2398        struct snd_ctl_elem_value *ucontrol)
2399{
2400        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2401        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2402        unsigned int val, bitmask;
2403
2404        for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2405                ;
2406        val = snd_soc_read(codec, e->reg);
2407        ucontrol->value.enumerated.item[0]
2408                = (val >> e->shift_l) & (bitmask - 1);
2409        if (e->shift_l != e->shift_r)
2410                ucontrol->value.enumerated.item[1] =
2411                        (val >> e->shift_r) & (bitmask - 1);
2412
2413        return 0;
2414}
2415EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2416
2417/**
2418 * snd_soc_put_enum_double - enumerated double mixer put callback
2419 * @kcontrol: mixer control
2420 * @ucontrol: control element information
2421 *
2422 * Callback to set the value of a double enumerated mixer.
2423 *
2424 * Returns 0 for success.
2425 */
2426int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2427        struct snd_ctl_elem_value *ucontrol)
2428{
2429        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2430        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2431        unsigned int val;
2432        unsigned int mask, bitmask;
2433
2434        for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2435                ;
2436        if (ucontrol->value.enumerated.item[0] > e->max - 1)
2437                return -EINVAL;
2438        val = ucontrol->value.enumerated.item[0] << e->shift_l;
2439        mask = (bitmask - 1) << e->shift_l;
2440        if (e->shift_l != e->shift_r) {
2441                if (ucontrol->value.enumerated.item[1] > e->max - 1)
2442                        return -EINVAL;
2443                val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2444                mask |= (bitmask - 1) << e->shift_r;
2445        }
2446
2447        return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2448}
2449EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2450
2451/**
2452 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2453 * @kcontrol: mixer control
2454 * @ucontrol: control element information
2455 *
2456 * Callback to get the value of a double semi enumerated mixer.
2457 *
2458 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2459 * used for handling bitfield coded enumeration for example.
2460 *
2461 * Returns 0 for success.
2462 */
2463int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2464        struct snd_ctl_elem_value *ucontrol)
2465{
2466        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2467        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2468        unsigned int reg_val, val, mux;
2469
2470        reg_val = snd_soc_read(codec, e->reg);
2471        val = (reg_val >> e->shift_l) & e->mask;
2472        for (mux = 0; mux < e->max; mux++) {
2473                if (val == e->values[mux])
2474                        break;
2475        }
2476        ucontrol->value.enumerated.item[0] = mux;
2477        if (e->shift_l != e->shift_r) {
2478                val = (reg_val >> e->shift_r) & e->mask;
2479                for (mux = 0; mux < e->max; mux++) {
2480                        if (val == e->values[mux])
2481                                break;
2482                }
2483                ucontrol->value.enumerated.item[1] = mux;
2484        }
2485
2486        return 0;
2487}
2488EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2489
2490/**
2491 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2492 * @kcontrol: mixer control
2493 * @ucontrol: control element information
2494 *
2495 * Callback to set the value of a double semi enumerated mixer.
2496 *
2497 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2498 * used for handling bitfield coded enumeration for example.
2499 *
2500 * Returns 0 for success.
2501 */
2502int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2503        struct snd_ctl_elem_value *ucontrol)
2504{
2505        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2506        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2507        unsigned int val;
2508        unsigned int mask;
2509
2510        if (ucontrol->value.enumerated.item[0] > e->max - 1)
2511                return -EINVAL;
2512        val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2513        mask = e->mask << e->shift_l;
2514        if (e->shift_l != e->shift_r) {
2515                if (ucontrol->value.enumerated.item[1] > e->max - 1)
2516                        return -EINVAL;
2517                val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2518                mask |= e->mask << e->shift_r;
2519        }
2520
2521        return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2522}
2523EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2524
2525/**
2526 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2527 * @kcontrol: mixer control
2528 * @uinfo: control element information
2529 *
2530 * Callback to provide information about an external enumerated
2531 * single mixer.
2532 *
2533 * Returns 0 for success.
2534 */
2535int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2536        struct snd_ctl_elem_info *uinfo)
2537{
2538        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2539
2540        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2541        uinfo->count = 1;
2542        uinfo->value.enumerated.items = e->max;
2543
2544        if (uinfo->value.enumerated.item > e->max - 1)
2545                uinfo->value.enumerated.item = e->max - 1;
2546        strcpy(uinfo->value.enumerated.name,
2547                e->texts[uinfo->value.enumerated.item]);
2548        return 0;
2549}
2550EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2551
2552/**
2553 * snd_soc_info_volsw_ext - external single mixer info callback
2554 * @kcontrol: mixer control
2555 * @uinfo: control element information
2556 *
2557 * Callback to provide information about a single external mixer control.
2558 *
2559 * Returns 0 for success.
2560 */
2561int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2562        struct snd_ctl_elem_info *uinfo)
2563{
2564        int max = kcontrol->private_value;
2565
2566        if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2567                uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2568        else
2569                uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2570
2571        uinfo->count = 1;
2572        uinfo->value.integer.min = 0;
2573        uinfo->value.integer.max = max;
2574        return 0;
2575}
2576EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2577
2578/**
2579 * snd_soc_info_volsw - single mixer info callback
2580 * @kcontrol: mixer control
2581 * @uinfo: control element information
2582 *
2583 * Callback to provide information about a single mixer control, or a double
2584 * mixer control that spans 2 registers.
2585 *
2586 * Returns 0 for success.
2587 */
2588int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2589        struct snd_ctl_elem_info *uinfo)
2590{
2591        struct soc_mixer_control *mc =
2592                (struct soc_mixer_control *)kcontrol->private_value;
2593        int platform_max;
2594
2595        if (!mc->platform_max)
2596                mc->platform_max = mc->max;
2597        platform_max = mc->platform_max;
2598
2599        if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2600                uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2601        else
2602                uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2603
2604        uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2605        uinfo->value.integer.min = 0;
2606        uinfo->value.integer.max = platform_max;
2607        return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2610
2611/**
2612 * snd_soc_get_volsw - single mixer get callback
2613 * @kcontrol: mixer control
2614 * @ucontrol: control element information
2615 *
2616 * Callback to get the value of a single mixer control, or a double mixer
2617 * control that spans 2 registers.
2618 *
2619 * Returns 0 for success.
2620 */
2621int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2622        struct snd_ctl_elem_value *ucontrol)
2623{
2624        struct soc_mixer_control *mc =
2625                (struct soc_mixer_control *)kcontrol->private_value;
2626        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2627        unsigned int reg = mc->reg;
2628        unsigned int reg2 = mc->rreg;
2629        unsigned int shift = mc->shift;
2630        unsigned int rshift = mc->rshift;
2631        int max = mc->max;
2632        unsigned int mask = (1 << fls(max)) - 1;
2633        unsigned int invert = mc->invert;
2634
2635        ucontrol->value.integer.value[0] =
2636                (snd_soc_read(codec, reg) >> shift) & mask;
2637        if (invert)
2638                ucontrol->value.integer.value[0] =
2639                        max - ucontrol->value.integer.value[0];
2640
2641        if (snd_soc_volsw_is_stereo(mc)) {
2642                if (reg == reg2)
2643                        ucontrol->value.integer.value[1] =
2644                                (snd_soc_read(codec, reg) >> rshift) & mask;
2645                else
2646                        ucontrol->value.integer.value[1] =
2647                                (snd_soc_read(codec, reg2) >> shift) & mask;
2648                if (invert)
2649                        ucontrol->value.integer.value[1] =
2650                                max - ucontrol->value.integer.value[1];
2651        }
2652
2653        return 0;
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2656
2657/**
2658 * snd_soc_put_volsw - single mixer put callback
2659 * @kcontrol: mixer control
2660 * @ucontrol: control element information
2661 *
2662 * Callback to set the value of a single mixer control, or a double mixer
2663 * control that spans 2 registers.
2664 *
2665 * Returns 0 for success.
2666 */
2667int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2668        struct snd_ctl_elem_value *ucontrol)
2669{
2670        struct soc_mixer_control *mc =
2671                (struct soc_mixer_control *)kcontrol->private_value;
2672        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2673        unsigned int reg = mc->reg;
2674        unsigned int reg2 = mc->rreg;
2675        unsigned int shift = mc->shift;
2676        unsigned int rshift = mc->rshift;
2677        int max = mc->max;
2678        unsigned int mask = (1 << fls(max)) - 1;
2679        unsigned int invert = mc->invert;
2680        int err;
2681        bool type_2r = 0;
2682        unsigned int val2 = 0;
2683        unsigned int val, val_mask;
2684
2685        val = (ucontrol->value.integer.value[0] & mask);
2686        if (invert)
2687                val = max - val;
2688        val_mask = mask << shift;
2689        val = val << shift;
2690        if (snd_soc_volsw_is_stereo(mc)) {
2691                val2 = (ucontrol->value.integer.value[1] & mask);
2692                if (invert)
2693                        val2 = max - val2;
2694                if (reg == reg2) {
2695                        val_mask |= mask << rshift;
2696                        val |= val2 << rshift;
2697                } else {
2698                        val2 = val2 << shift;
2699                        type_2r = 1;
2700                }
2701        }
2702        err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2703        if (err < 0)
2704                return err;
2705
2706        if (type_2r)
2707                err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2708
2709        return err;
2710}
2711EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2712
2713/**
2714 * snd_soc_get_volsw_sx - single mixer get callback
2715 * @kcontrol: mixer control
2716 * @ucontrol: control element information
2717 *
2718 * Callback to get the value of a single mixer control, or a double mixer
2719 * control that spans 2 registers.
2720 *
2721 * Returns 0 for success.
2722 */
2723int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2724                      struct snd_ctl_elem_value *ucontrol)
2725{
2726        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2727        struct soc_mixer_control *mc =
2728            (struct soc_mixer_control *)kcontrol->private_value;
2729
2730        unsigned int reg = mc->reg;
2731        unsigned int reg2 = mc->rreg;
2732        unsigned int shift = mc->shift;
2733        unsigned int rshift = mc->rshift;
2734        int max = mc->max;
2735        int min = mc->min;
2736        int mask = (1 << (fls(min + max) - 1)) - 1;
2737
2738        ucontrol->value.integer.value[0] =
2739            ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2740
2741        if (snd_soc_volsw_is_stereo(mc))
2742                ucontrol->value.integer.value[1] =
2743                        ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2744
2745        return 0;
2746}
2747EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2748
2749/**
2750 * snd_soc_put_volsw_sx - double mixer set callback
2751 * @kcontrol: mixer control
2752 * @uinfo: control element information
2753 *
2754 * Callback to set the value of a double mixer control that spans 2 registers.
2755 *
2756 * Returns 0 for success.
2757 */
2758int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2759                         struct snd_ctl_elem_value *ucontrol)
2760{
2761        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2762        struct soc_mixer_control *mc =
2763            (struct soc_mixer_control *)kcontrol->private_value;
2764
2765        unsigned int reg = mc->reg;
2766        unsigned int reg2 = mc->rreg;
2767        unsigned int shift = mc->shift;
2768        unsigned int rshift = mc->rshift;
2769        int max = mc->max;
2770        int min = mc->min;
2771        int mask = (1 << (fls(min + max) - 1)) - 1;
2772        int err = 0;
2773        unsigned short val, val_mask, val2 = 0;
2774
2775        val_mask = mask << shift;
2776        val = (ucontrol->value.integer.value[0] + min) & mask;
2777        val = val << shift;
2778
2779        err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2780        if (err < 0)
2781                return err;
2782
2783        if (snd_soc_volsw_is_stereo(mc)) {
2784                val_mask = mask << rshift;
2785                val2 = (ucontrol->value.integer.value[1] + min) & mask;
2786                val2 = val2 << rshift;
2787
2788                if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2789                        return err;
2790        }
2791        return 0;
2792}
2793EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2794
2795/**
2796 * snd_soc_info_volsw_s8 - signed mixer info callback
2797 * @kcontrol: mixer control
2798 * @uinfo: control element information
2799 *
2800 * Callback to provide information about a signed mixer control.
2801 *
2802 * Returns 0 for success.
2803 */
2804int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2805        struct snd_ctl_elem_info *uinfo)
2806{
2807        struct soc_mixer_control *mc =
2808                (struct soc_mixer_control *)kcontrol->private_value;
2809        int platform_max;
2810        int min = mc->min;
2811
2812        if (!mc->platform_max)
2813                mc->platform_max = mc->max;
2814        platform_max = mc->platform_max;
2815
2816        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2817        uinfo->count = 2;
2818        uinfo->value.integer.min = 0;
2819        uinfo->value.integer.max = platform_max - min;
2820        return 0;
2821}
2822EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2823
2824/**
2825 * snd_soc_get_volsw_s8 - signed mixer get callback
2826 * @kcontrol: mixer control
2827 * @ucontrol: control element information
2828 *
2829 * Callback to get the value of a signed mixer control.
2830 *
2831 * Returns 0 for success.
2832 */
2833int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2834        struct snd_ctl_elem_value *ucontrol)
2835{
2836        struct soc_mixer_control *mc =
2837                (struct soc_mixer_control *)kcontrol->private_value;
2838        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2839        unsigned int reg = mc->reg;
2840        int min = mc->min;
2841        int val = snd_soc_read(codec, reg);
2842
2843        ucontrol->value.integer.value[0] =
2844                ((signed char)(val & 0xff))-min;
2845        ucontrol->value.integer.value[1] =
2846                ((signed char)((val >> 8) & 0xff))-min;
2847        return 0;
2848}
2849EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2850
2851/**
2852 * snd_soc_put_volsw_sgn - signed mixer put callback
2853 * @kcontrol: mixer control
2854 * @ucontrol: control element information
2855 *
2856 * Callback to set the value of a signed mixer control.
2857 *
2858 * Returns 0 for success.
2859 */
2860int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2861        struct snd_ctl_elem_value *ucontrol)
2862{
2863        struct soc_mixer_control *mc =
2864                (struct soc_mixer_control *)kcontrol->private_value;
2865        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2866        unsigned int reg = mc->reg;
2867        int min = mc->min;
2868        unsigned int val;
2869
2870        val = (ucontrol->value.integer.value[0]+min) & 0xff;
2871        val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2872
2873        return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2874}
2875EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2876
2877/**
2878 * snd_soc_info_volsw_range - single mixer info callback with range.
2879 * @kcontrol: mixer control
2880 * @uinfo: control element information
2881 *
2882 * Callback to provide information, within a range, about a single
2883 * mixer control.
2884 *
2885 * returns 0 for success.
2886 */
2887int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2888        struct snd_ctl_elem_info *uinfo)
2889{
2890        struct soc_mixer_control *mc =
2891                (struct soc_mixer_control *)kcontrol->private_value;
2892        int platform_max;
2893        int min = mc->min;
2894
2895        if (!mc->platform_max)
2896                mc->platform_max = mc->max;
2897        platform_max = mc->platform_max;
2898
2899        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2900        uinfo->count = 1;
2901        uinfo->value.integer.min = 0;
2902        uinfo->value.integer.max = platform_max - min;
2903
2904        return 0;
2905}
2906EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2907
2908/**
2909 * snd_soc_put_volsw_range - single mixer put value callback with range.
2910 * @kcontrol: mixer control
2911 * @ucontrol: control element information
2912 *
2913 * Callback to set the value, within a range, for a single mixer control.
2914 *
2915 * Returns 0 for success.
2916 */
2917int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2918        struct snd_ctl_elem_value *ucontrol)
2919{
2920        struct soc_mixer_control *mc =
2921                (struct soc_mixer_control *)kcontrol->private_value;
2922        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2923        unsigned int reg = mc->reg;
2924        unsigned int shift = mc->shift;
2925        int min = mc->min;
2926        int max = mc->max;
2927        unsigned int mask = (1 << fls(max)) - 1;
2928        unsigned int invert = mc->invert;
2929        unsigned int val, val_mask;
2930
2931        val = ((ucontrol->value.integer.value[0] + min) & mask);
2932        if (invert)
2933                val = max - val;
2934        val_mask = mask << shift;
2935        val = val << shift;
2936
2937        return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2938}
2939EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2940
2941/**
2942 * snd_soc_get_volsw_range - single mixer get callback with range
2943 * @kcontrol: mixer control
2944 * @ucontrol: control element information
2945 *
2946 * Callback to get the value, within a range, of a single mixer control.
2947 *
2948 * Returns 0 for success.
2949 */
2950int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2951        struct snd_ctl_elem_value *ucontrol)
2952{
2953        struct soc_mixer_control *mc =
2954                (struct soc_mixer_control *)kcontrol->private_value;
2955        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2956        unsigned int reg = mc->reg;
2957        unsigned int shift = mc->shift;
2958        int min = mc->min;
2959        int max = mc->max;
2960        unsigned int mask = (1 << fls(max)) - 1;
2961        unsigned int invert = mc->invert;
2962
2963        ucontrol->value.integer.value[0] =
2964                (snd_soc_read(codec, reg) >> shift) & mask;
2965        if (invert)
2966                ucontrol->value.integer.value[0] =
2967                        max - ucontrol->value.integer.value[0];
2968        ucontrol->value.integer.value[0] =
2969                ucontrol->value.integer.value[0] - min;
2970
2971        return 0;
2972}
2973EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2974
2975/**
2976 * snd_soc_limit_volume - Set new limit to an existing volume control.
2977 *
2978 * @codec: where to look for the control
2979 * @name: Name of the control
2980 * @max: new maximum limit
2981 *
2982 * Return 0 for success, else error.
2983 */
2984int snd_soc_limit_volume(struct snd_soc_codec *codec,
2985        const char *name, int max)
2986{
2987        struct snd_card *card = codec->card->snd_card;
2988        struct snd_kcontrol *kctl;
2989        struct soc_mixer_control *mc;
2990        int found = 0;
2991        int ret = -EINVAL;
2992
2993        /* Sanity check for name and max */
2994        if (unlikely(!name || max <= 0))
2995                return -EINVAL;
2996
2997        list_for_each_entry(kctl, &card->controls, list) {
2998                if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2999                        found = 1;
3000                        break;
3001                }
3002        }
3003        if (found) {
3004                mc = (struct soc_mixer_control *)kctl->private_value;
3005                if (max <= mc->max) {
3006                        mc->platform_max = max;
3007                        ret = 0;
3008                }
3009        }
3010        return ret;
3011}
3012EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3013
3014int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3015                       struct snd_ctl_elem_info *uinfo)
3016{
3017        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3018        struct soc_bytes *params = (void *)kcontrol->private_value;
3019
3020        uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3021        uinfo->count = params->num_regs * codec->val_bytes;
3022
3023        return 0;
3024}
3025EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3026
3027int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3028                      struct snd_ctl_elem_value *ucontrol)
3029{
3030        struct soc_bytes *params = (void *)kcontrol->private_value;
3031        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3032        int ret;
3033
3034        if (codec->using_regmap)
3035                ret = regmap_raw_read(codec->control_data, params->base,
3036                                      ucontrol->value.bytes.data,
3037                                      params->num_regs * codec->val_bytes);
3038        else
3039                ret = -EINVAL;
3040
3041        /* Hide any masked bytes to ensure consistent data reporting */
3042        if (ret == 0 && params->mask) {
3043                switch (codec->val_bytes) {
3044                case 1:
3045                        ucontrol->value.bytes.data[0] &= ~params->mask;
3046                        break;
3047                case 2:
3048                        ((u16 *)(&ucontrol->value.bytes.data))[0]
3049                                &= ~params->mask;
3050                        break;
3051                case 4:
3052                        ((u32 *)(&ucontrol->value.bytes.data))[0]
3053                                &= ~params->mask;
3054                        break;
3055                default:
3056                        return -EINVAL;
3057                }
3058        }
3059
3060        return ret;
3061}
3062EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3063
3064int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3065                      struct snd_ctl_elem_value *ucontrol)
3066{
3067        struct soc_bytes *params = (void *)kcontrol->private_value;
3068        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3069        int ret, len;
3070        unsigned int val;
3071        void *data;
3072
3073        if (!codec->using_regmap)
3074                return -EINVAL;
3075
3076        data = ucontrol->value.bytes.data;
3077        len = params->num_regs * codec->val_bytes;
3078
3079        /*
3080         * If we've got a mask then we need to preserve the register
3081         * bits.  We shouldn't modify the incoming data so take a
3082         * copy.
3083         */
3084        if (params->mask) {
3085                ret = regmap_read(codec->control_data, params->base, &val);
3086                if (ret != 0)
3087                        return ret;
3088
3089                val &= params->mask;
3090
3091                data = kmemdup(data, len, GFP_KERNEL);
3092                if (!data)
3093                        return -ENOMEM;
3094
3095                switch (codec->val_bytes) {
3096                case 1:
3097                        ((u8 *)data)[0] &= ~params->mask;
3098                        ((u8 *)data)[0] |= val;
3099                        break;
3100                case 2:
3101                        ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3102                        ((u16 *)data)[0] |= cpu_to_be16(val);
3103                        break;
3104                case 4:
3105                        ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3106                        ((u32 *)data)[0] |= cpu_to_be32(val);
3107                        break;
3108                default:
3109                        return -EINVAL;
3110                }
3111        }
3112
3113        ret = regmap_raw_write(codec->control_data, params->base,
3114                               data, len);
3115
3116        if (params->mask)
3117                kfree(data);
3118
3119        return ret;
3120}
3121EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3122
3123/**
3124 * snd_soc_info_xr_sx - signed multi register info callback
3125 * @kcontrol: mreg control
3126 * @uinfo: control element information
3127 *
3128 * Callback to provide information of a control that can
3129 * span multiple codec registers which together
3130 * forms a single signed value in a MSB/LSB manner.
3131 *
3132 * Returns 0 for success.
3133 */
3134int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3135        struct snd_ctl_elem_info *uinfo)
3136{
3137        struct soc_mreg_control *mc =
3138                (struct soc_mreg_control *)kcontrol->private_value;
3139        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3140        uinfo->count = 1;
3141        uinfo->value.integer.min = mc->min;
3142        uinfo->value.integer.max = mc->max;
3143
3144        return 0;
3145}
3146EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3147
3148/**
3149 * snd_soc_get_xr_sx - signed multi register get callback
3150 * @kcontrol: mreg control
3151 * @ucontrol: control element information
3152 *
3153 * Callback to get the value of a control that can span
3154 * multiple codec registers which together forms a single
3155 * signed value in a MSB/LSB manner. The control supports
3156 * specifying total no of bits used to allow for bitfields
3157 * across the multiple codec registers.
3158 *
3159 * Returns 0 for success.
3160 */
3161int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3162        struct snd_ctl_elem_value *ucontrol)
3163{
3164        struct soc_mreg_control *mc =
3165                (struct soc_mreg_control *)kcontrol->private_value;
3166        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3167        unsigned int regbase = mc->regbase;
3168        unsigned int regcount = mc->regcount;
3169        unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3170        unsigned int regwmask = (1<<regwshift)-1;
3171        unsigned int invert = mc->invert;
3172        unsigned long mask = (1UL<<mc->nbits)-1;
3173        long min = mc->min;
3174        long max = mc->max;
3175        long val = 0;
3176        unsigned long regval;
3177        unsigned int i;
3178
3179        for (i = 0; i < regcount; i++) {
3180                regval = snd_soc_read(codec, regbase+i) & regwmask;
3181                val |= regval << (regwshift*(regcount-i-1));
3182        }
3183        val &= mask;
3184        if (min < 0 && val > max)
3185                val |= ~mask;
3186        if (invert)
3187                val = max - val;
3188        ucontrol->value.integer.value[0] = val;
3189
3190        return 0;
3191}
3192EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3193
3194/**
3195 * snd_soc_put_xr_sx - signed multi register get callback
3196 * @kcontrol: mreg control
3197 * @ucontrol: control element information
3198 *
3199 * Callback to set the value of a control that can span
3200 * multiple codec registers which together forms a single
3201 * signed value in a MSB/LSB manner. The control supports
3202 * specifying total no of bits used to allow for bitfields
3203 * across the multiple codec registers.
3204 *
3205 * Returns 0 for success.
3206 */
3207int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3208        struct snd_ctl_elem_value *ucontrol)
3209{
3210        struct soc_mreg_control *mc =
3211                (struct soc_mreg_control *)kcontrol->private_value;
3212        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3213        unsigned int regbase = mc->regbase;
3214        unsigned int regcount = mc->regcount;
3215        unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3216        unsigned int regwmask = (1<<regwshift)-1;
3217        unsigned int invert = mc->invert;
3218        unsigned long mask = (1UL<<mc->nbits)-1;
3219        long max = mc->max;
3220        long val = ucontrol->value.integer.value[0];
3221        unsigned int i, regval, regmask;
3222        int err;
3223
3224        if (invert)
3225                val = max - val;
3226        val &= mask;
3227        for (i = 0; i < regcount; i++) {
3228                regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3229                regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3230                err = snd_soc_update_bits_locked(codec, regbase+i,
3231                                regmask, regval);
3232                if (err < 0)
3233                        return err;
3234        }
3235
3236        return 0;
3237}
3238EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3239
3240/**
3241 * snd_soc_get_strobe - strobe get callback
3242 * @kcontrol: mixer control
3243 * @ucontrol: control element information
3244 *
3245 * Callback get the value of a strobe mixer control.
3246 *
3247 * Returns 0 for success.
3248 */
3249int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3250        struct snd_ctl_elem_value *ucontrol)
3251{
3252        struct soc_mixer_control *mc =
3253                (struct soc_mixer_control *)kcontrol->private_value;
3254        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3255        unsigned int reg = mc->reg;
3256        unsigned int shift = mc->shift;
3257        unsigned int mask = 1 << shift;
3258        unsigned int invert = mc->invert != 0;
3259        unsigned int val = snd_soc_read(codec, reg) & mask;
3260
3261        if (shift != 0 && val != 0)
3262                val = val >> shift;
3263        ucontrol->value.enumerated.item[0] = val ^ invert;
3264
3265        return 0;
3266}
3267EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3268
3269/**
3270 * snd_soc_put_strobe - strobe put callback
3271 * @kcontrol: mixer control
3272 * @ucontrol: control element information
3273 *
3274 * Callback strobe a register bit to high then low (or the inverse)
3275 * in one pass of a single mixer enum control.
3276 *
3277 * Returns 1 for success.
3278 */
3279int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3280        struct snd_ctl_elem_value *ucontrol)
3281{
3282        struct soc_mixer_control *mc =
3283                (struct soc_mixer_control *)kcontrol->private_value;
3284        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3285        unsigned int reg = mc->reg;
3286        unsigned int shift = mc->shift;
3287        unsigned int mask = 1 << shift;
3288        unsigned int invert = mc->invert != 0;
3289        unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3290        unsigned int val1 = (strobe ^ invert) ? mask : 0;
3291        unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3292        int err;
3293
3294        err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3295        if (err < 0)
3296                return err;
3297
3298        err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3299        return err;
3300}
3301EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3302
3303/**
3304 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3305 * @dai: DAI
3306 * @clk_id: DAI specific clock ID
3307 * @freq: new clock frequency in Hz
3308 * @dir: new clock direction - input/output.
3309 *
3310 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3311 */
3312int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3313        unsigned int freq, int dir)
3314{
3315        if (dai->driver && dai->driver->ops->set_sysclk)
3316                return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3317        else if (dai->codec && dai->codec->driver->set_sysclk)
3318                return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3319                                                      freq, dir);
3320        else
3321                return -EINVAL;
3322}
3323EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3324
3325/**
3326 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3327 * @codec: CODEC
3328 * @clk_id: DAI specific clock ID
3329 * @source: Source for the clock
3330 * @freq: new clock frequency in Hz
3331 * @dir: new clock direction - input/output.
3332 *
3333 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3334 */
3335int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3336                             int source, unsigned int freq, int dir)
3337{
3338        if (codec->driver->set_sysclk)
3339                return codec->driver->set_sysclk(codec, clk_id, source,
3340                                                 freq, dir);
3341        else
3342                return -EINVAL;
3343}
3344EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3345
3346/**
3347 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3348 * @dai: DAI
3349 * @div_id: DAI specific clock divider ID
3350 * @div: new clock divisor.
3351 *
3352 * Configures the clock dividers. This is used to derive the best DAI bit and
3353 * frame clocks from the system or master clock. It's best to set the DAI bit
3354 * and frame clocks as low as possible to save system power.
3355 */
3356int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3357        int div_id, int div)
3358{
3359        if (dai->driver && dai->driver->ops->set_clkdiv)
3360                return dai->driver->ops->set_clkdiv(dai, div_id, div);
3361        else
3362                return -EINVAL;
3363}
3364EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3365
3366/**
3367 * snd_soc_dai_set_pll - configure DAI PLL.
3368 * @dai: DAI
3369 * @pll_id: DAI specific PLL ID
3370 * @source: DAI specific source for the PLL
3371 * @freq_in: PLL input clock frequency in Hz
3372 * @freq_out: requested PLL output clock frequency in Hz
3373 *
3374 * Configures and enables PLL to generate output clock based on input clock.
3375 */
3376int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3377        unsigned int freq_in, unsigned int freq_out)
3378{
3379        if (dai->driver && dai->driver->ops->set_pll)
3380                return dai->driver->ops->set_pll(dai, pll_id, source,
3381                                         freq_in, freq_out);
3382        else if (dai->codec && dai->codec->driver->set_pll)
3383                return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3384                                                   freq_in, freq_out);
3385        else
3386                return -EINVAL;
3387}
3388EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3389
3390/*
3391 * snd_soc_codec_set_pll - configure codec PLL.
3392 * @codec: CODEC
3393 * @pll_id: DAI specific PLL ID
3394 * @source: DAI specific source for the PLL
3395 * @freq_in: PLL input clock frequency in Hz
3396 * @freq_out: requested PLL output clock frequency in Hz
3397 *
3398 * Configures and enables PLL to generate output clock based on input clock.
3399 */
3400int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3401                          unsigned int freq_in, unsigned int freq_out)
3402{
3403        if (codec->driver->set_pll)
3404                return codec->driver->set_pll(codec, pll_id, source,
3405                                              freq_in, freq_out);
3406        else
3407                return -EINVAL;
3408}
3409EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3410
3411/**
3412 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3413 * @dai: DAI
3414 * @fmt: SND_SOC_DAIFMT_ format value.
3415 *
3416 * Configures the DAI hardware format and clocking.
3417 */
3418int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3419{
3420        if (dai->driver == NULL)
3421                return -EINVAL;
3422        if (dai->driver->ops->set_fmt == NULL)
3423                return -ENOTSUPP;
3424        return dai->driver->ops->set_fmt(dai, fmt);
3425}
3426EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3427
3428/**
3429 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3430 * @dai: DAI
3431 * @tx_mask: bitmask representing active TX slots.
3432 * @rx_mask: bitmask representing active RX slots.
3433 * @slots: Number of slots in use.
3434 * @slot_width: Width in bits for each slot.
3435 *
3436 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3437 * specific.
3438 */
3439int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3440        unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3441{
3442        if (dai->driver && dai->driver->ops->set_tdm_slot)
3443                return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3444                                slots, slot_width);
3445        else
3446                return -EINVAL;
3447}
3448EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3449
3450/**
3451 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3452 * @dai: DAI
3453 * @tx_num: how many TX channels
3454 * @tx_slot: pointer to an array which imply the TX slot number channel
3455 *           0~num-1 uses
3456 * @rx_num: how many RX channels
3457 * @rx_slot: pointer to an array which imply the RX slot number channel
3458 *           0~num-1 uses
3459 *
3460 * configure the relationship between channel number and TDM slot number.
3461 */
3462int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3463        unsigned int tx_num, unsigned int *tx_slot,
3464        unsigned int rx_num, unsigned int *rx_slot)
3465{
3466        if (dai->driver && dai->driver->ops->set_channel_map)
3467                return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3468                        rx_num, rx_slot);
3469        else
3470                return -EINVAL;
3471}
3472EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3473
3474/**
3475 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3476 * @dai: DAI
3477 * @tristate: tristate enable
3478 *
3479 * Tristates the DAI so that others can use it.
3480 */
3481int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3482{
3483        if (dai->driver && dai->driver->ops->set_tristate)
3484                return dai->driver->ops->set_tristate(dai, tristate);
3485        else
3486                return -EINVAL;
3487}
3488EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3489
3490/**
3491 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3492 * @dai: DAI
3493 * @mute: mute enable
3494 *
3495 * Mutes the DAI DAC.
3496 */
3497int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3498{
3499        if (dai->driver && dai->driver->ops->digital_mute)
3500                return dai->driver->ops->digital_mute(dai, mute);
3501        else
3502                return -ENOTSUPP;
3503}
3504EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3505
3506/**
3507 * snd_soc_register_card - Register a card with the ASoC core
3508 *
3509 * @card: Card to register
3510 *
3511 */
3512int snd_soc_register_card(struct snd_soc_card *card)
3513{
3514        int i, ret;
3515
3516        if (!card->name || !card->dev)
3517                return -EINVAL;
3518
3519        for (i = 0; i < card->num_links; i++) {
3520                struct snd_soc_dai_link *link = &card->dai_link[i];
3521
3522                /*
3523                 * Codec must be specified by 1 of name or OF node,
3524                 * not both or neither.
3525                 */
3526                if (!!link->codec_name == !!link->codec_of_node) {
3527                        dev_err(card->dev,
3528                                "Neither/both codec name/of_node are set for %s\n",
3529                                link->name);
3530                        return -EINVAL;
3531                }
3532                /* Codec DAI name must be specified */
3533                if (!link->codec_dai_name) {
3534                        dev_err(card->dev, "codec_dai_name not set for %s\n",
3535                                link->name);
3536                        return -EINVAL;
3537                }
3538
3539                /*
3540                 * Platform may be specified by either name or OF node, but
3541                 * can be left unspecified, and a dummy platform will be used.
3542                 */
3543                if (link->platform_name && link->platform_of_node) {
3544                        dev_err(card->dev,
3545                                "Both platform name/of_node are set for %s\n", link->name);
3546                        return -EINVAL;
3547                }
3548
3549                /*
3550                 * CPU device may be specified by either name or OF node, but
3551                 * can be left unspecified, and will be matched based on DAI
3552                 * name alone..
3553                 */
3554                if (link->cpu_name && link->cpu_of_node) {
3555                        dev_err(card->dev,
3556                                "Neither/both cpu name/of_node are set for %s\n",
3557                                link->name);
3558                        return -EINVAL;
3559                }
3560                /*
3561                 * At least one of CPU DAI name or CPU device name/node must be
3562                 * specified
3563                 */
3564                if (!link->cpu_dai_name &&
3565                    !(link->cpu_name || link->cpu_of_node)) {
3566                        dev_err(card->dev,
3567                                "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3568                                link->name);
3569                        return -EINVAL;
3570                }
3571        }
3572
3573        dev_set_drvdata(card->dev, card);
3574
3575        snd_soc_initialize_card_lists(card);
3576
3577        soc_init_card_debugfs(card);
3578
3579        card->rtd = devm_kzalloc(card->dev,
3580                                 sizeof(struct snd_soc_pcm_runtime) *
3581                                 (card->num_links + card->num_aux_devs),
3582                                 GFP_KERNEL);
3583        if (card->rtd == NULL)
3584                return -ENOMEM;
3585        card->num_rtd = 0;
3586        card->rtd_aux = &card->rtd[card->num_links];
3587
3588        for (i = 0; i < card->num_links; i++)
3589                card->rtd[i].dai_link = &card->dai_link[i];
3590
3591        INIT_LIST_HEAD(&card->list);
3592        INIT_LIST_HEAD(&card->dapm_dirty);
3593        card->instantiated = 0;
3594        mutex_init(&card->mutex);
3595        mutex_init(&card->dapm_mutex);
3596
3597        ret = snd_soc_instantiate_card(card);
3598        if (ret != 0)
3599                soc_cleanup_card_debugfs(card);
3600
3601        return ret;
3602}
3603EXPORT_SYMBOL_GPL(snd_soc_register_card);
3604
3605/**
3606 * snd_soc_unregister_card - Unregister a card with the ASoC core
3607 *
3608 * @card: Card to unregister
3609 *
3610 */
3611int snd_soc_unregister_card(struct snd_soc_card *card)
3612{
3613        if (card->instantiated)
3614                soc_cleanup_card_resources(card);
3615        dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3616
3617        return 0;
3618}
3619EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3620
3621/*
3622 * Simplify DAI link configuration by removing ".-1" from device names
3623 * and sanitizing names.
3624 */
3625static char *fmt_single_name(struct device *dev, int *id)
3626{
3627        char *found, name[NAME_SIZE];
3628        int id1, id2;
3629
3630        if (dev_name(dev) == NULL)
3631                return NULL;
3632
3633        strlcpy(name, dev_name(dev), NAME_SIZE);
3634
3635        /* are we a "%s.%d" name (platform and SPI components) */
3636        found = strstr(name, dev->driver->name);
3637        if (found) {
3638                /* get ID */
3639                if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3640
3641                        /* discard ID from name if ID == -1 */
3642                        if (*id == -1)
3643                                found[strlen(dev->driver->name)] = '\0';
3644                }
3645
3646        } else {
3647                /* I2C component devices are named "bus-addr"  */
3648                if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3649                        char tmp[NAME_SIZE];
3650
3651                        /* create unique ID number from I2C addr and bus */
3652                        *id = ((id1 & 0xffff) << 16) + id2;
3653
3654                        /* sanitize component name for DAI link creation */
3655                        snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3656                        strlcpy(name, tmp, NAME_SIZE);
3657                } else
3658                        *id = 0;
3659        }
3660
3661        return kstrdup(name, GFP_KERNEL);
3662}
3663
3664/*
3665 * Simplify DAI link naming for single devices with multiple DAIs by removing
3666 * any ".-1" and using the DAI name (instead of device name).
3667 */
3668static inline char *fmt_multiple_name(struct device *dev,
3669                struct snd_soc_dai_driver *dai_drv)
3670{
3671        if (dai_drv->name == NULL) {
3672                pr_err("asoc: error - multiple DAI %s registered with no name\n",
3673                                dev_name(dev));
3674                return NULL;
3675        }
3676
3677        return kstrdup(dai_drv->name, GFP_KERNEL);
3678}
3679
3680/**
3681 * snd_soc_register_dai - Register a DAI with the ASoC core
3682 *
3683 * @dai: DAI to register
3684 */
3685int snd_soc_register_dai(struct device *dev,
3686                struct snd_soc_dai_driver *dai_drv)
3687{
3688        struct snd_soc_codec *codec;
3689        struct snd_soc_dai *dai;
3690
3691        dev_dbg(dev, "dai register %s\n", dev_name(dev));
3692
3693        dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3694        if (dai == NULL)
3695                return -ENOMEM;
3696
3697        /* create DAI component name */
3698        dai->name = fmt_single_name(dev, &dai->id);
3699        if (dai->name == NULL) {
3700                kfree(dai);
3701                return -ENOMEM;
3702        }
3703
3704        dai->dev = dev;
3705        dai->driver = dai_drv;
3706        dai->dapm.dev = dev;
3707        if (!dai->driver->ops)
3708                dai->driver->ops = &null_dai_ops;
3709
3710        mutex_lock(&client_mutex);
3711
3712        list_for_each_entry(codec, &codec_list, list) {
3713                if (codec->dev == dev) {
3714                        dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3715                                dai->name, codec->name);
3716                        dai->codec = codec;
3717                        break;
3718                }
3719        }
3720
3721        list_add(&dai->list, &dai_list);
3722
3723        mutex_unlock(&client_mutex);
3724
3725        pr_debug("Registered DAI '%s'\n", dai->name);
3726
3727        return 0;
3728}
3729EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3730
3731/**
3732 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3733 *
3734 * @dai: DAI to unregister
3735 */
3736void snd_soc_unregister_dai(struct device *dev)
3737{
3738        struct snd_soc_dai *dai;
3739
3740        list_for_each_entry(dai, &dai_list, list) {
3741                if (dev == dai->dev)
3742                        goto found;
3743        }
3744        return;
3745
3746found:
3747        mutex_lock(&client_mutex);
3748        list_del(&dai->list);
3749        mutex_unlock(&client_mutex);
3750
3751        pr_debug("Unregistered DAI '%s'\n", dai->name);
3752        kfree(dai->name);
3753        kfree(dai);
3754}
3755EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3756
3757/**
3758 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3759 *
3760 * @dai: Array of DAIs to register
3761 * @count: Number of DAIs
3762 */
3763int snd_soc_register_dais(struct device *dev,
3764                struct snd_soc_dai_driver *dai_drv, size_t count)
3765{
3766        struct snd_soc_codec *codec;
3767        struct snd_soc_dai *dai;
3768        int i, ret = 0;
3769
3770        dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3771
3772        for (i = 0; i < count; i++) {
3773
3774                dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3775                if (dai == NULL) {
3776                        ret = -ENOMEM;
3777                        goto err;
3778                }
3779
3780                /* create DAI component name */
3781                dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3782                if (dai->name == NULL) {
3783                        kfree(dai);
3784                        ret = -EINVAL;
3785                        goto err;
3786                }
3787
3788                dai->dev = dev;
3789                dai->driver = &dai_drv[i];
3790                if (dai->driver->id)
3791                        dai->id = dai->driver->id;
3792                else
3793                        dai->id = i;
3794                dai->dapm.dev = dev;
3795                if (!dai->driver->ops)
3796                        dai->driver->ops = &null_dai_ops;
3797
3798                mutex_lock(&client_mutex);
3799
3800                list_for_each_entry(codec, &codec_list, list) {
3801                        if (codec->dev == dev) {
3802                                dev_dbg(dev, "Mapped DAI %s to CODEC %s\n",
3803                                        dai->name, codec->name);
3804                                dai->codec = codec;
3805                                break;
3806                        }
3807                }
3808
3809                list_add(&dai->list, &dai_list);
3810
3811                mutex_unlock(&client_mutex);
3812
3813                pr_debug("Registered DAI '%s'\n", dai->name);
3814        }
3815
3816        return 0;
3817
3818err:
3819        for (i--; i >= 0; i--)
3820                snd_soc_unregister_dai(dev);
3821
3822        return ret;
3823}
3824EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3825
3826/**
3827 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3828 *
3829 * @dai: Array of DAIs to unregister
3830 * @count: Number of DAIs
3831 */
3832void snd_soc_unregister_dais(struct device *dev, size_t count)
3833{
3834        int i;
3835
3836        for (i = 0; i < count; i++)
3837                snd_soc_unregister_dai(dev);
3838}
3839EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3840
3841/**
3842 * snd_soc_register_platform - Register a platform with the ASoC core
3843 *
3844 * @platform: platform to register
3845 */
3846int snd_soc_register_platform(struct device *dev,
3847                struct snd_soc_platform_driver *platform_drv)
3848{
3849        struct snd_soc_platform *platform;
3850
3851        dev_dbg(dev, "platform register %s\n", dev_name(dev));
3852
3853        platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3854        if (platform == NULL)
3855                return -ENOMEM;
3856
3857        /* create platform component name */
3858        platform->name = fmt_single_name(dev, &platform->id);
3859        if (platform->name == NULL) {
3860                kfree(platform);
3861                return -ENOMEM;
3862        }
3863
3864        platform->dev = dev;
3865        platform->driver = platform_drv;
3866        platform->dapm.dev = dev;
3867        platform->dapm.platform = platform;
3868        platform->dapm.stream_event = platform_drv->stream_event;
3869        mutex_init(&platform->mutex);
3870
3871        mutex_lock(&client_mutex);
3872        list_add(&platform->list, &platform_list);
3873        mutex_unlock(&client_mutex);
3874
3875        pr_debug("Registered platform '%s'\n", platform->name);
3876
3877        return 0;
3878}
3879EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3880
3881/**
3882 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3883 *
3884 * @platform: platform to unregister
3885 */
3886void snd_soc_unregister_platform(struct device *dev)
3887{
3888        struct snd_soc_platform *platform;
3889
3890        list_for_each_entry(platform, &platform_list, list) {
3891                if (dev == platform->dev)
3892                        goto found;
3893        }
3894        return;
3895
3896found:
3897        mutex_lock(&client_mutex);
3898        list_del(&platform->list);
3899        mutex_unlock(&client_mutex);
3900
3901        pr_debug("Unregistered platform '%s'\n", platform->name);
3902        kfree(platform->name);
3903        kfree(platform);
3904}
3905EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3906
3907static u64 codec_format_map[] = {
3908        SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3909        SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3910        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3911        SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3912        SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3913        SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3914        SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3915        SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3916        SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3917        SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3918        SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3919        SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3920        SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3921        SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3922        SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3923        | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3924};
3925
3926/* Fix up the DAI formats for endianness: codecs don't actually see
3927 * the endianness of the data but we're using the CPU format
3928 * definitions which do need to include endianness so we ensure that
3929 * codec DAIs always have both big and little endian variants set.
3930 */
3931static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3932{
3933        int i;
3934
3935        for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3936                if (stream->formats & codec_format_map[i])
3937                        stream->formats |= codec_format_map[i];
3938}
3939
3940/**
3941 * snd_soc_register_codec - Register a codec with the ASoC core
3942 *
3943 * @codec: codec to register
3944 */
3945int snd_soc_register_codec(struct device *dev,
3946                           const struct snd_soc_codec_driver *codec_drv,
3947                           struct snd_soc_dai_driver *dai_drv,
3948                           int num_dai)
3949{
3950        size_t reg_size;
3951        struct snd_soc_codec *codec;
3952        int ret, i;
3953
3954        dev_dbg(dev, "codec register %s\n", dev_name(dev));
3955
3956        codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3957        if (codec == NULL)
3958                return -ENOMEM;
3959
3960        /* create CODEC component name */
3961        codec->name = fmt_single_name(dev, &codec->id);
3962        if (codec->name == NULL) {
3963                kfree(codec);
3964                return -ENOMEM;
3965        }
3966
3967        if (codec_drv->compress_type)
3968                codec->compress_type = codec_drv->compress_type;
3969        else
3970                codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3971
3972        codec->write = codec_drv->write;
3973        codec->read = codec_drv->read;
3974        codec->volatile_register = codec_drv->volatile_register;
3975        codec->readable_register = codec_drv->readable_register;
3976        codec->writable_register = codec_drv->writable_register;
3977        codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
3978        codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3979        codec->dapm.dev = dev;
3980        codec->dapm.codec = codec;
3981        codec->dapm.seq_notifier = codec_drv->seq_notifier;
3982        codec->dapm.stream_event = codec_drv->stream_event;
3983        codec->dev = dev;
3984        codec->driver = codec_drv;
3985        codec->num_dai = num_dai;
3986        mutex_init(&codec->mutex);
3987
3988        /* allocate CODEC register cache */
3989        if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3990                reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3991                codec->reg_size = reg_size;
3992                /* it is necessary to make a copy of the default register cache
3993                 * because in the case of using a compression type that requires
3994                 * the default register cache to be marked as __devinitconst the
3995                 * kernel might have freed the array by the time we initialize
3996                 * the cache.
3997                 */
3998                if (codec_drv->reg_cache_default) {
3999                        codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4000                                                      reg_size, GFP_KERNEL);
4001                        if (!codec->reg_def_copy) {
4002                                ret = -ENOMEM;
4003                                goto fail;
4004                        }
4005                }
4006        }
4007
4008        if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4009                if (!codec->volatile_register)
4010                        codec->volatile_register = snd_soc_default_volatile_register;
4011                if (!codec->readable_register)
4012                        codec->readable_register = snd_soc_default_readable_register;
4013                if (!codec->writable_register)
4014                        codec->writable_register = snd_soc_default_writable_register;
4015        }
4016
4017        for (i = 0; i < num_dai; i++) {
4018                fixup_codec_formats(&dai_drv[i].playback);
4019                fixup_codec_formats(&dai_drv[i].capture);
4020        }
4021
4022        mutex_lock(&client_mutex);
4023        list_add(&codec->list, &codec_list);
4024        mutex_unlock(&client_mutex);
4025
4026        /* register any DAIs */
4027        if (num_dai) {
4028                ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4029                if (ret < 0)
4030                        dev_err(codec->dev, "Failed to regster DAIs: %d\n",
4031                                ret);
4032        }
4033
4034        pr_debug("Registered codec '%s'\n", codec->name);
4035        return 0;
4036
4037fail:
4038        kfree(codec->reg_def_copy);
4039        codec->reg_def_copy = NULL;
4040        kfree(codec->name);
4041        kfree(codec);
4042        return ret;
4043}
4044EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4045
4046/**
4047 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4048 *
4049 * @codec: codec to unregister
4050 */
4051void snd_soc_unregister_codec(struct device *dev)
4052{
4053        struct snd_soc_codec *codec;
4054        int i;
4055
4056        list_for_each_entry(codec, &codec_list, list) {
4057                if (dev == codec->dev)
4058                        goto found;
4059        }
4060        return;
4061
4062found:
4063        if (codec->num_dai)
4064                for (i = 0; i < codec->num_dai; i++)
4065                        snd_soc_unregister_dai(dev);
4066
4067        mutex_lock(&client_mutex);
4068        list_del(&codec->list);
4069        mutex_unlock(&client_mutex);
4070
4071        pr_debug("Unregistered codec '%s'\n", codec->name);
4072
4073        snd_soc_cache_exit(codec);
4074        kfree(codec->reg_def_copy);
4075        kfree(codec->name);
4076        kfree(codec);
4077}
4078EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4079
4080/* Retrieve a card's name from device tree */
4081int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4082                               const char *propname)
4083{
4084        struct device_node *np = card->dev->of_node;
4085        int ret;
4086
4087        ret = of_property_read_string_index(np, propname, 0, &card->name);
4088        /*
4089         * EINVAL means the property does not exist. This is fine providing
4090         * card->name was previously set, which is checked later in
4091         * snd_soc_register_card.
4092         */
4093        if (ret < 0 && ret != -EINVAL) {
4094                dev_err(card->dev,
4095                        "Property '%s' could not be read: %d\n",
4096                        propname, ret);
4097                return ret;
4098        }
4099
4100        return 0;
4101}
4102EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4103
4104int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4105                                   const char *propname)
4106{
4107        struct device_node *np = card->dev->of_node;
4108        int num_routes;
4109        struct snd_soc_dapm_route *routes;
4110        int i, ret;
4111
4112        num_routes = of_property_count_strings(np, propname);
4113        if (num_routes < 0 || num_routes & 1) {
4114                dev_err(card->dev,
4115                     "Property '%s' does not exist or its length is not even\n",
4116                     propname);
4117                return -EINVAL;
4118        }
4119        num_routes /= 2;
4120        if (!num_routes) {
4121                dev_err(card->dev,
4122                        "Property '%s's length is zero\n",
4123                        propname);
4124                return -EINVAL;
4125        }
4126
4127        routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4128                              GFP_KERNEL);
4129        if (!routes) {
4130                dev_err(card->dev,
4131                        "Could not allocate DAPM route table\n");
4132                return -EINVAL;
4133        }
4134
4135        for (i = 0; i < num_routes; i++) {
4136                ret = of_property_read_string_index(np, propname,
4137                        2 * i, &routes[i].sink);
4138                if (ret) {
4139                        dev_err(card->dev,
4140                                "Property '%s' index %d could not be read: %d\n",
4141                                propname, 2 * i, ret);
4142                        kfree(routes);
4143                        return -EINVAL;
4144                }
4145                ret = of_property_read_string_index(np, propname,
4146                        (2 * i) + 1, &routes[i].source);
4147                if (ret) {
4148                        dev_err(card->dev,
4149                                "Property '%s' index %d could not be read: %d\n",
4150                                propname, (2 * i) + 1, ret);
4151                        kfree(routes);
4152                        return -EINVAL;
4153                }
4154        }
4155
4156        card->num_dapm_routes = num_routes;
4157        card->dapm_routes = routes;
4158
4159        return 0;
4160}
4161EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4162
4163static int __init snd_soc_init(void)
4164{
4165#ifdef CONFIG_DEBUG_FS
4166        snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4167        if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4168                pr_warn("ASoC: Failed to create debugfs directory\n");
4169                snd_soc_debugfs_root = NULL;
4170        }
4171
4172        if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4173                                 &codec_list_fops))
4174                pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4175
4176        if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4177                                 &dai_list_fops))
4178                pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4179
4180        if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4181                                 &platform_list_fops))
4182                pr_warn("ASoC: Failed to create platform list debugfs file\n");
4183#endif
4184
4185        snd_soc_util_init();
4186
4187        return platform_driver_register(&soc_driver);
4188}
4189module_init(snd_soc_init);
4190
4191static void __exit snd_soc_exit(void)
4192{
4193        snd_soc_util_exit();
4194
4195#ifdef CONFIG_DEBUG_FS
4196        debugfs_remove_recursive(snd_soc_debugfs_root);
4197#endif
4198        platform_driver_unregister(&soc_driver);
4199}
4200module_exit(snd_soc_exit);
4201
4202/* Module information */
4203MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4204MODULE_DESCRIPTION("ALSA SoC Core");
4205MODULE_LICENSE("GPL");
4206MODULE_ALIAS("platform:soc-audio");
4207
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.