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/initval.h>
  43
  44#define CREATE_TRACE_POINTS
  45#include <trace/events/asoc.h>
  46
  47#define NAME_SIZE       32
  48
  49static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  50
  51#ifdef CONFIG_DEBUG_FS
  52struct dentry *snd_soc_debugfs_root;
  53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
  54#endif
  55
  56static DEFINE_MUTEX(client_mutex);
  57static LIST_HEAD(card_list);
  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 int codec_reg_open_file(struct inode *inode, struct file *file)
 205{
 206        file->private_data = inode->i_private;
 207        return 0;
 208}
 209
 210static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
 211                                   size_t count, loff_t *ppos)
 212{
 213        ssize_t ret;
 214        struct snd_soc_codec *codec = file->private_data;
 215        char *buf;
 216
 217        if (*ppos < 0 || !count)
 218                return -EINVAL;
 219
 220        buf = kmalloc(count, GFP_KERNEL);
 221        if (!buf)
 222                return -ENOMEM;
 223
 224        ret = soc_codec_reg_show(codec, buf, count, *ppos);
 225        if (ret >= 0) {
 226                if (copy_to_user(user_buf, buf, ret)) {
 227                        kfree(buf);
 228                        return -EFAULT;
 229                }
 230                *ppos += ret;
 231        }
 232
 233        kfree(buf);
 234        return ret;
 235}
 236
 237static ssize_t codec_reg_write_file(struct file *file,
 238                const char __user *user_buf, size_t count, loff_t *ppos)
 239{
 240        char buf[32];
 241        size_t buf_size;
 242        char *start = buf;
 243        unsigned long reg, value;
 244        struct snd_soc_codec *codec = file->private_data;
 245
 246        buf_size = min(count, (sizeof(buf)-1));
 247        if (copy_from_user(buf, user_buf, buf_size))
 248                return -EFAULT;
 249        buf[buf_size] = 0;
 250
 251        while (*start == ' ')
 252                start++;
 253        reg = simple_strtoul(start, &start, 16);
 254        while (*start == ' ')
 255                start++;
 256        if (strict_strtoul(start, 16, &value))
 257                return -EINVAL;
 258
 259        /* Userspace has been fiddling around behind the kernel's back */
 260        add_taint(TAINT_USER);
 261
 262        snd_soc_write(codec, reg, value);
 263        return buf_size;
 264}
 265
 266static const struct file_operations codec_reg_fops = {
 267        .open = codec_reg_open_file,
 268        .read = codec_reg_read_file,
 269        .write = codec_reg_write_file,
 270        .llseek = default_llseek,
 271};
 272
 273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
 274{
 275        struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
 276
 277        codec->debugfs_codec_root = debugfs_create_dir(codec->name,
 278                                                       debugfs_card_root);
 279        if (!codec->debugfs_codec_root) {
 280                printk(KERN_WARNING
 281                       "ASoC: Failed to create codec debugfs directory\n");
 282                return;
 283        }
 284
 285        debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
 286                            &codec->cache_sync);
 287        debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
 288                            &codec->cache_only);
 289
 290        codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
 291                                                 codec->debugfs_codec_root,
 292                                                 codec, &codec_reg_fops);
 293        if (!codec->debugfs_reg)
 294                printk(KERN_WARNING
 295                       "ASoC: Failed to create codec register debugfs file\n");
 296
 297        snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
 298}
 299
 300static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
 301{
 302        debugfs_remove_recursive(codec->debugfs_codec_root);
 303}
 304
 305static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
 306                                    size_t count, loff_t *ppos)
 307{
 308        char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 309        ssize_t len, ret = 0;
 310        struct snd_soc_codec *codec;
 311
 312        if (!buf)
 313                return -ENOMEM;
 314
 315        list_for_each_entry(codec, &codec_list, list) {
 316                len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
 317                               codec->name);
 318                if (len >= 0)
 319                        ret += len;
 320                if (ret > PAGE_SIZE) {
 321                        ret = PAGE_SIZE;
 322                        break;
 323                }
 324        }
 325
 326        if (ret >= 0)
 327                ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
 328
 329        kfree(buf);
 330
 331        return ret;
 332}
 333
 334static const struct file_operations codec_list_fops = {
 335        .read = codec_list_read_file,
 336        .llseek = default_llseek,/* read accesses f_pos */
 337};
 338
 339static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
 340                                  size_t count, loff_t *ppos)
 341{
 342        char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 343        ssize_t len, ret = 0;
 344        struct snd_soc_dai *dai;
 345
 346        if (!buf)
 347                return -ENOMEM;
 348
 349        list_for_each_entry(dai, &dai_list, list) {
 350                len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
 351                if (len >= 0)
 352                        ret += len;
 353                if (ret > PAGE_SIZE) {
 354                        ret = PAGE_SIZE;
 355                        break;
 356                }
 357        }
 358
 359        ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
 360
 361        kfree(buf);
 362
 363        return ret;
 364}
 365
 366static const struct file_operations dai_list_fops = {
 367        .read = dai_list_read_file,
 368        .llseek = default_llseek,/* read accesses f_pos */
 369};
 370
 371static ssize_t platform_list_read_file(struct file *file,
 372                                       char __user *user_buf,
 373                                       size_t count, loff_t *ppos)
 374{
 375        char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
 376        ssize_t len, ret = 0;
 377        struct snd_soc_platform *platform;
 378
 379        if (!buf)
 380                return -ENOMEM;
 381
 382        list_for_each_entry(platform, &platform_list, list) {
 383                len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
 384                               platform->name);
 385                if (len >= 0)
 386                        ret += len;
 387                if (ret > PAGE_SIZE) {
 388                        ret = PAGE_SIZE;
 389                        break;
 390                }
 391        }
 392
 393        ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
 394
 395        kfree(buf);
 396
 397        return ret;
 398}
 399
 400static const struct file_operations platform_list_fops = {
 401        .read = platform_list_read_file,
 402        .llseek = default_llseek,/* read accesses f_pos */
 403};
 404
 405static void soc_init_card_debugfs(struct snd_soc_card *card)
 406{
 407        card->debugfs_card_root = debugfs_create_dir(card->name,
 408                                                     snd_soc_debugfs_root);
 409        if (!card->debugfs_card_root) {
 410                dev_warn(card->dev,
 411                         "ASoC: Failed to create card debugfs directory\n");
 412                return;
 413        }
 414
 415        card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
 416                                                    card->debugfs_card_root,
 417                                                    &card->pop_time);
 418        if (!card->debugfs_pop_time)
 419                dev_warn(card->dev,
 420                       "Failed to create pop time debugfs file\n");
 421}
 422
 423static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
 424{
 425        debugfs_remove_recursive(card->debugfs_card_root);
 426}
 427
 428#else
 429
 430static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
 431{
 432}
 433
 434static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
 435{
 436}
 437
 438static inline void soc_init_card_debugfs(struct snd_soc_card *card)
 439{
 440}
 441
 442static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
 443{
 444}
 445#endif
 446
 447#ifdef CONFIG_SND_SOC_AC97_BUS
 448/* unregister ac97 codec */
 449static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
 450{
 451        if (codec->ac97->dev.bus)
 452                device_unregister(&codec->ac97->dev);
 453        return 0;
 454}
 455
 456/* stop no dev release warning */
 457static void soc_ac97_device_release(struct device *dev){}
 458
 459/* register ac97 codec to bus */
 460static int soc_ac97_dev_register(struct snd_soc_codec *codec)
 461{
 462        int err;
 463
 464        codec->ac97->dev.bus = &ac97_bus_type;
 465        codec->ac97->dev.parent = codec->card->dev;
 466        codec->ac97->dev.release = soc_ac97_device_release;
 467
 468        dev_set_name(&codec->ac97->dev, "%d-%d:%s",
 469                     codec->card->snd_card->number, 0, codec->name);
 470        err = device_register(&codec->ac97->dev);
 471        if (err < 0) {
 472                snd_printk(KERN_ERR "Can't register ac97 bus\n");
 473                codec->ac97->dev.bus = NULL;
 474                return err;
 475        }
 476        return 0;
 477}
 478#endif
 479
 480#ifdef CONFIG_PM_SLEEP
 481/* powers down audio subsystem for suspend */
 482int snd_soc_suspend(struct device *dev)
 483{
 484        struct snd_soc_card *card = dev_get_drvdata(dev);
 485        struct snd_soc_codec *codec;
 486        int i;
 487
 488        /* If the initialization of this soc device failed, there is no codec
 489         * associated with it. Just bail out in this case.
 490         */
 491        if (list_empty(&card->codec_dev_list))
 492                return 0;
 493
 494        /* Due to the resume being scheduled into a workqueue we could
 495        * suspend before that's finished - wait for it to complete.
 496         */
 497        snd_power_lock(card->snd_card);
 498        snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
 499        snd_power_unlock(card->snd_card);
 500
 501        /* we're going to block userspace touching us until resume completes */
 502        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
 503
 504        /* mute any active DACs */
 505        for (i = 0; i < card->num_rtd; i++) {
 506                struct snd_soc_dai *dai = card->rtd[i].codec_dai;
 507                struct snd_soc_dai_driver *drv = dai->driver;
 508
 509                if (card->rtd[i].dai_link->ignore_suspend)
 510                        continue;
 511
 512                if (drv->ops->digital_mute && dai->playback_active)
 513                        drv->ops->digital_mute(dai, 1);
 514        }
 515
 516        /* suspend all pcms */
 517        for (i = 0; i < card->num_rtd; i++) {
 518                if (card->rtd[i].dai_link->ignore_suspend)
 519                        continue;
 520
 521                snd_pcm_suspend_all(card->rtd[i].pcm);
 522        }
 523
 524        if (card->suspend_pre)
 525                card->suspend_pre(card);
 526
 527        for (i = 0; i < card->num_rtd; i++) {
 528                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 529                struct snd_soc_platform *platform = card->rtd[i].platform;
 530
 531                if (card->rtd[i].dai_link->ignore_suspend)
 532                        continue;
 533
 534                if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
 535                        cpu_dai->driver->suspend(cpu_dai);
 536                if (platform->driver->suspend && !platform->suspended) {
 537                        platform->driver->suspend(cpu_dai);
 538                        platform->suspended = 1;
 539                }
 540        }
 541
 542        /* close any waiting streams and save state */
 543        for (i = 0; i < card->num_rtd; i++) {
 544                flush_delayed_work_sync(&card->rtd[i].delayed_work);
 545                card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
 546        }
 547
 548        for (i = 0; i < card->num_rtd; i++) {
 549                struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
 550
 551                if (card->rtd[i].dai_link->ignore_suspend)
 552                        continue;
 553
 554                if (driver->playback.stream_name != NULL)
 555                        snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
 556                                SND_SOC_DAPM_STREAM_SUSPEND);
 557
 558                if (driver->capture.stream_name != NULL)
 559                        snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
 560                                SND_SOC_DAPM_STREAM_SUSPEND);
 561        }
 562
 563        /* suspend all CODECs */
 564        list_for_each_entry(codec, &card->codec_dev_list, card_list) {
 565                /* If there are paths active then the CODEC will be held with
 566                 * bias _ON and should not be suspended. */
 567                if (!codec->suspended && codec->driver->suspend) {
 568                        switch (codec->dapm.bias_level) {
 569                        case SND_SOC_BIAS_STANDBY:
 570                                /*
 571                                 * If the CODEC is capable of idle
 572                                 * bias off then being in STANDBY
 573                                 * means it's doing something,
 574                                 * otherwise fall through.
 575                                 */
 576                                if (codec->dapm.idle_bias_off) {
 577                                        dev_dbg(codec->dev,
 578                                                "idle_bias_off CODEC on over suspend\n");
 579                                        break;
 580                                }
 581                        case SND_SOC_BIAS_OFF:
 582                                codec->driver->suspend(codec);
 583                                codec->suspended = 1;
 584                                codec->cache_sync = 1;
 585                                break;
 586                        default:
 587                                dev_dbg(codec->dev, "CODEC is on over suspend\n");
 588                                break;
 589                        }
 590                }
 591        }
 592
 593        for (i = 0; i < card->num_rtd; i++) {
 594                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 595
 596                if (card->rtd[i].dai_link->ignore_suspend)
 597                        continue;
 598
 599                if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
 600                        cpu_dai->driver->suspend(cpu_dai);
 601        }
 602
 603        if (card->suspend_post)
 604                card->suspend_post(card);
 605
 606        return 0;
 607}
 608EXPORT_SYMBOL_GPL(snd_soc_suspend);
 609
 610/* deferred resume work, so resume can complete before we finished
 611 * setting our codec back up, which can be very slow on I2C
 612 */
 613static void soc_resume_deferred(struct work_struct *work)
 614{
 615        struct snd_soc_card *card =
 616                        container_of(work, struct snd_soc_card, deferred_resume_work);
 617        struct snd_soc_codec *codec;
 618        int i;
 619
 620        /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
 621         * so userspace apps are blocked from touching us
 622         */
 623
 624        dev_dbg(card->dev, "starting resume work\n");
 625
 626        /* Bring us up into D2 so that DAPM starts enabling things */
 627        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
 628
 629        if (card->resume_pre)
 630                card->resume_pre(card);
 631
 632        /* resume AC97 DAIs */
 633        for (i = 0; i < card->num_rtd; i++) {
 634                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 635
 636                if (card->rtd[i].dai_link->ignore_suspend)
 637                        continue;
 638
 639                if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
 640                        cpu_dai->driver->resume(cpu_dai);
 641        }
 642
 643        list_for_each_entry(codec, &card->codec_dev_list, card_list) {
 644                /* If the CODEC was idle over suspend then it will have been
 645                 * left with bias OFF or STANDBY and suspended so we must now
 646                 * resume.  Otherwise the suspend was suppressed.
 647                 */
 648                if (codec->driver->resume && codec->suspended) {
 649                        switch (codec->dapm.bias_level) {
 650                        case SND_SOC_BIAS_STANDBY:
 651                        case SND_SOC_BIAS_OFF:
 652                                codec->driver->resume(codec);
 653                                codec->suspended = 0;
 654                                break;
 655                        default:
 656                                dev_dbg(codec->dev, "CODEC was on over suspend\n");
 657                                break;
 658                        }
 659                }
 660        }
 661
 662        for (i = 0; i < card->num_rtd; i++) {
 663                struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
 664
 665                if (card->rtd[i].dai_link->ignore_suspend)
 666                        continue;
 667
 668                if (driver->playback.stream_name != NULL)
 669                        snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
 670                                SND_SOC_DAPM_STREAM_RESUME);
 671
 672                if (driver->capture.stream_name != NULL)
 673                        snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
 674                                SND_SOC_DAPM_STREAM_RESUME);
 675        }
 676
 677        /* unmute any active DACs */
 678        for (i = 0; i < card->num_rtd; i++) {
 679                struct snd_soc_dai *dai = card->rtd[i].codec_dai;
 680                struct snd_soc_dai_driver *drv = dai->driver;
 681
 682                if (card->rtd[i].dai_link->ignore_suspend)
 683                        continue;
 684
 685                if (drv->ops->digital_mute && dai->playback_active)
 686                        drv->ops->digital_mute(dai, 0);
 687        }
 688
 689        for (i = 0; i < card->num_rtd; i++) {
 690                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 691                struct snd_soc_platform *platform = card->rtd[i].platform;
 692
 693                if (card->rtd[i].dai_link->ignore_suspend)
 694                        continue;
 695
 696                if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
 697                        cpu_dai->driver->resume(cpu_dai);
 698                if (platform->driver->resume && platform->suspended) {
 699                        platform->driver->resume(cpu_dai);
 700                        platform->suspended = 0;
 701                }
 702        }
 703
 704        if (card->resume_post)
 705                card->resume_post(card);
 706
 707        dev_dbg(card->dev, "resume work completed\n");
 708
 709        /* userspace can access us now we are back as we were before */
 710        snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
 711}
 712
 713/* powers up audio subsystem after a suspend */
 714int snd_soc_resume(struct device *dev)
 715{
 716        struct snd_soc_card *card = dev_get_drvdata(dev);
 717        int i, ac97_control = 0;
 718
 719        /* If the initialization of this soc device failed, there is no codec
 720         * associated with it. Just bail out in this case.
 721         */
 722        if (list_empty(&card->codec_dev_list))
 723                return 0;
 724
 725        /* AC97 devices might have other drivers hanging off them so
 726         * need to resume immediately.  Other drivers don't have that
 727         * problem and may take a substantial amount of time to resume
 728         * due to I/O costs and anti-pop so handle them out of line.
 729         */
 730        for (i = 0; i < card->num_rtd; i++) {
 731                struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
 732                ac97_control |= cpu_dai->driver->ac97_control;
 733        }
 734        if (ac97_control) {
 735                dev_dbg(dev, "Resuming AC97 immediately\n");
 736                soc_resume_deferred(&card->deferred_resume_work);
 737        } else {
 738                dev_dbg(dev, "Scheduling resume work\n");
 739                if (!schedule_work(&card->deferred_resume_work))
 740                        dev_err(dev, "resume work item may be lost\n");
 741        }
 742
 743        return 0;
 744}
 745EXPORT_SYMBOL_GPL(snd_soc_resume);
 746#else
 747#define snd_soc_suspend NULL
 748#define snd_soc_resume NULL
 749#endif
 750
 751static const struct snd_soc_dai_ops null_dai_ops = {
 752};
 753
 754static int soc_bind_dai_link(struct snd_soc_card *card, int num)
 755{
 756        struct snd_soc_dai_link *dai_link = &card->dai_link[num];
 757        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
 758        struct snd_soc_codec *codec;
 759        struct snd_soc_platform *platform;
 760        struct snd_soc_dai *codec_dai, *cpu_dai;
 761        const char *platform_name;
 762
 763        if (rtd->complete)
 764                return 1;
 765        dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
 766
 767        /* do we already have the CPU DAI for this link ? */
 768        if (rtd->cpu_dai) {
 769                goto find_codec;
 770        }
 771        /* no, then find CPU DAI from registered DAIs*/
 772        list_for_each_entry(cpu_dai, &dai_list, list) {
 773                if (dai_link->cpu_dai_of_node) {
 774                        if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
 775                                continue;
 776                } else {
 777                        if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
 778                                continue;
 779                }
 780
 781                rtd->cpu_dai = cpu_dai;
 782                goto find_codec;
 783        }
 784        dev_dbg(card->dev, "CPU DAI %s not registered\n",
 785                        dai_link->cpu_dai_name);
 786
 787find_codec:
 788        /* do we already have the CODEC for this link ? */
 789        if (rtd->codec) {
 790                goto find_platform;
 791        }
 792
 793        /* no, then find CODEC from registered CODECs*/
 794        list_for_each_entry(codec, &codec_list, list) {
 795                if (dai_link->codec_of_node) {
 796                        if (codec->dev->of_node != dai_link->codec_of_node)
 797                                continue;
 798                } else {
 799                        if (strcmp(codec->name, dai_link->codec_name))
 800                                continue;
 801                }
 802
 803                rtd->codec = codec;
 804
 805                /*
 806                 * CODEC found, so find CODEC DAI from registered DAIs from
 807                 * this CODEC
 808                 */
 809                list_for_each_entry(codec_dai, &dai_list, list) {
 810                        if (codec->dev == codec_dai->dev &&
 811                                !strcmp(codec_dai->name,
 812                                        dai_link->codec_dai_name)) {
 813
 814                                rtd->codec_dai = codec_dai;
 815                                goto find_platform;
 816                        }
 817                }
 818                dev_dbg(card->dev, "CODEC DAI %s not registered\n",
 819                                dai_link->codec_dai_name);
 820
 821                goto find_platform;
 822        }
 823        dev_dbg(card->dev, "CODEC %s not registered\n",
 824                        dai_link->codec_name);
 825
 826find_platform:
 827        /* do we need a platform? */
 828        if (rtd->platform)
 829                goto out;
 830
 831        /* if there's no platform we match on the empty platform */
 832        platform_name = dai_link->platform_name;
 833        if (!platform_name && !dai_link->platform_of_node)
 834                platform_name = "snd-soc-dummy";
 835
 836        /* no, then find one from the set of registered platforms */
 837        list_for_each_entry(platform, &platform_list, list) {
 838                if (dai_link->platform_of_node) {
 839                        if (platform->dev->of_node !=
 840                            dai_link->platform_of_node)
 841                                continue;
 842                } else {
 843                        if (strcmp(platform->name, platform_name))
 844                                continue;
 845                }
 846
 847                rtd->platform = platform;
 848                goto out;
 849        }
 850
 851        dev_dbg(card->dev, "platform %s not registered\n",
 852                        dai_link->platform_name);
 853        return 0;
 854
 855out:
 856        /* mark rtd as complete if we found all 4 of our client devices */
 857        if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
 858                rtd->complete = 1;
 859                card->num_rtd++;
 860        }
 861        return 1;
 862}
 863
 864static void soc_remove_codec(struct snd_soc_codec *codec)
 865{
 866        int err;
 867
 868        if (codec->driver->remove) {
 869                err = codec->driver->remove(codec);
 870                if (err < 0)
 871                        dev_err(codec->dev,
 872                                "asoc: failed to remove %s: %d\n",
 873                                codec->name, err);
 874        }
 875
 876        /* Make sure all DAPM widgets are freed */
 877        snd_soc_dapm_free(&codec->dapm);
 878
 879        soc_cleanup_codec_debugfs(codec);
 880        codec->probed = 0;
 881        list_del(&codec->card_list);
 882        module_put(codec->dev->driver->owner);
 883}
 884
 885static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
 886{
 887        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
 888        struct snd_soc_codec *codec = rtd->codec;
 889        struct snd_soc_platform *platform = rtd->platform;
 890        struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
 891        int err;
 892
 893        /* unregister the rtd device */
 894        if (rtd->dev_registered) {
 895                device_remove_file(rtd->dev, &dev_attr_pmdown_time);
 896                device_remove_file(rtd->dev, &dev_attr_codec_reg);
 897                device_unregister(rtd->dev);
 898                rtd->dev_registered = 0;
 899        }
 900
 901        /* remove the CODEC DAI */
 902        if (codec_dai && codec_dai->probed &&
 903                        codec_dai->driver->remove_order == order) {
 904                if (codec_dai->driver->remove) {
 905                        err = codec_dai->driver->remove(codec_dai);
 906                        if (err < 0)
 907                                printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
 908                }
 909                codec_dai->probed = 0;
 910                list_del(&codec_dai->card_list);
 911        }
 912
 913        /* remove the platform */
 914        if (platform && platform->probed &&
 915                        platform->driver->remove_order == order) {
 916                if (platform->driver->remove) {
 917                        err = platform->driver->remove(platform);
 918                        if (err < 0)
 919                                printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
 920                }
 921
 922                /* Make sure all DAPM widgets are freed */
 923                snd_soc_dapm_free(&platform->dapm);
 924
 925                platform->probed = 0;
 926                list_del(&platform->card_list);
 927                module_put(platform->dev->driver->owner);
 928        }
 929
 930        /* remove the CODEC */
 931        if (codec && codec->probed &&
 932                        codec->driver->remove_order == order)
 933                soc_remove_codec(codec);
 934
 935        /* remove the cpu_dai */
 936        if (cpu_dai && cpu_dai->probed &&
 937                        cpu_dai->driver->remove_order == order) {
 938                if (cpu_dai->driver->remove) {
 939                        err = cpu_dai->driver->remove(cpu_dai);
 940                        if (err < 0)
 941                                printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
 942                }
 943                cpu_dai->probed = 0;
 944                list_del(&cpu_dai->card_list);
 945                module_put(cpu_dai->dev->driver->owner);
 946        }
 947}
 948
 949static void soc_remove_dai_links(struct snd_soc_card *card)
 950{
 951        int dai, order;
 952
 953        for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
 954                        order++) {
 955                for (dai = 0; dai < card->num_rtd; dai++)
 956                        soc_remove_dai_link(card, dai, order);
 957        }
 958        card->num_rtd = 0;
 959}
 960
 961static void soc_set_name_prefix(struct snd_soc_card *card,
 962                                struct snd_soc_codec *codec)
 963{
 964        int i;
 965
 966        if (card->codec_conf == NULL)
 967                return;
 968
 969        for (i = 0; i < card->num_configs; i++) {
 970                struct snd_soc_codec_conf *map = &card->codec_conf[i];
 971                if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
 972                        codec->name_prefix = map->name_prefix;
 973                        break;
 974                }
 975        }
 976}
 977
 978static int soc_probe_codec(struct snd_soc_card *card,
 979                           struct snd_soc_codec *codec)
 980{
 981        int ret = 0;
 982        const struct snd_soc_codec_driver *driver = codec->driver;
 983
 984        codec->card = card;
 985        codec->dapm.card = card;
 986        soc_set_name_prefix(card, codec);
 987
 988        if (!try_module_get(codec->dev->driver->owner))
 989                return -ENODEV;
 990
 991        soc_init_codec_debugfs(codec);
 992
 993        if (driver->dapm_widgets)
 994                snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
 995                                          driver->num_dapm_widgets);
 996
 997        codec->dapm.idle_bias_off = driver->idle_bias_off;
 998
 999        if (driver->probe) {
1000                ret = driver->probe(codec);
1001                if (ret < 0) {
1002                        dev_err(codec->dev,
1003                                "asoc: failed to probe CODEC %s: %d\n",
1004                                codec->name, ret);
1005                        goto err_probe;
1006                }
1007        }
1008
1009        if (driver->controls)
1010                snd_soc_add_controls(codec, driver->controls,
1011                                     driver->num_controls);
1012        if (driver->dapm_routes)
1013                snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1014                                        driver->num_dapm_routes);
1015
1016        /* mark codec as probed and add to card codec list */
1017        codec->probed = 1;
1018        list_add(&codec->card_list, &card->codec_dev_list);
1019        list_add(&codec->dapm.list, &card->dapm_list);
1020
1021        return 0;
1022
1023err_probe:
1024        soc_cleanup_codec_debugfs(codec);
1025        module_put(codec->dev->driver->owner);
1026
1027        return ret;
1028}
1029
1030static int soc_probe_platform(struct snd_soc_card *card,
1031                           struct snd_soc_platform *platform)
1032{
1033        int ret = 0;
1034        const struct snd_soc_platform_driver *driver = platform->driver;
1035
1036        platform->card = card;
1037        platform->dapm.card = card;
1038
1039        if (!try_module_get(platform->dev->driver->owner))
1040                return -ENODEV;
1041
1042        if (driver->dapm_widgets)
1043                snd_soc_dapm_new_controls(&platform->dapm,
1044                        driver->dapm_widgets, driver->num_dapm_widgets);
1045
1046        if (driver->probe) {
1047                ret = driver->probe(platform);
1048                if (ret < 0) {
1049                        dev_err(platform->dev,
1050                                "asoc: failed to probe platform %s: %d\n",
1051                                platform->name, ret);
1052                        goto err_probe;
1053                }
1054        }
1055
1056        if (driver->controls)
1057                snd_soc_add_platform_controls(platform, driver->controls,
1058                                     driver->num_controls);
1059        if (driver->dapm_routes)
1060                snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1061                                        driver->num_dapm_routes);
1062
1063        /* mark platform as probed and add to card platform list */
1064        platform->probed = 1;
1065        list_add(&platform->card_list, &card->platform_dev_list);
1066        list_add(&platform->dapm.list, &card->dapm_list);
1067
1068        return 0;
1069
1070err_probe:
1071        module_put(platform->dev->driver->owner);
1072
1073        return ret;
1074}
1075
1076static void rtd_release(struct device *dev)
1077{
1078        kfree(dev);
1079}
1080
1081static int soc_post_component_init(struct snd_soc_card *card,
1082                                   struct snd_soc_codec *codec,
1083                                   int num, int dailess)
1084{
1085        struct snd_soc_dai_link *dai_link = NULL;
1086        struct snd_soc_aux_dev *aux_dev = NULL;
1087        struct snd_soc_pcm_runtime *rtd;
1088        const char *temp, *name;
1089        int ret = 0;
1090
1091        if (!dailess) {
1092                dai_link = &card->dai_link[num];
1093                rtd = &card->rtd[num];
1094                name = dai_link->name;
1095        } else {
1096                aux_dev = &card->aux_dev[num];
1097                rtd = &card->rtd_aux[num];
1098                name = aux_dev->name;
1099        }
1100        rtd->card = card;
1101
1102        /* Make sure all DAPM widgets are instantiated */
1103        snd_soc_dapm_new_widgets(&codec->dapm);
1104
1105        /* machine controls, routes and widgets are not prefixed */
1106        temp = codec->name_prefix;
1107        codec->name_prefix = NULL;
1108
1109        /* do machine specific initialization */
1110        if (!dailess && dai_link->init)
1111                ret = dai_link->init(rtd);
1112        else if (dailess && aux_dev->init)
1113                ret = aux_dev->init(&codec->dapm);
1114        if (ret < 0) {
1115                dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1116                return ret;
1117        }
1118        codec->name_prefix = temp;
1119
1120        /* register the rtd device */
1121        rtd->codec = codec;
1122
1123        rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1124        if (!rtd->dev)
1125                return -ENOMEM;
1126        device_initialize(rtd->dev);
1127        rtd->dev->parent = card->dev;
1128        rtd->dev->release = rtd_release;
1129        rtd->dev->init_name = name;
1130        dev_set_drvdata(rtd->dev, rtd);
1131        mutex_init(&rtd->pcm_mutex);
1132        ret = device_add(rtd->dev);
1133        if (ret < 0) {
1134                dev_err(card->dev,
1135                        "asoc: failed to register runtime device: %d\n", ret);
1136                return ret;
1137        }
1138        rtd->dev_registered = 1;
1139
1140        /* add DAPM sysfs entries for this codec */
1141        ret = snd_soc_dapm_sys_add(rtd->dev);
1142        if (ret < 0)
1143                dev_err(codec->dev,
1144                        "asoc: failed to add codec dapm sysfs entries: %d\n",
1145                        ret);
1146
1147        /* add codec sysfs entries */
1148        ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1149        if (ret < 0)
1150                dev_err(codec->dev,
1151                        "asoc: failed to add codec sysfs files: %d\n", ret);
1152
1153        return 0;
1154}
1155
1156static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
1157{
1158        struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1159        struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1160        struct snd_soc_codec *codec = rtd->codec;
1161        struct snd_soc_platform *platform = rtd->platform;
1162        struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1163        int ret;
1164
1165        dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1166                        card->name, num, order);
1167
1168        /* config components */
1169        codec_dai->codec = codec;
1170        cpu_dai->platform = platform;
1171        codec_dai->card = card;
1172        cpu_dai->card = card;
1173
1174        /* set default power off timeout */
1175        rtd->pmdown_time = pmdown_time;
1176
1177        /* probe the cpu_dai */
1178        if (!cpu_dai->probed &&
1179                        cpu_dai->driver->probe_order == order) {
1180                if (!try_module_get(cpu_dai->dev->driver->owner))
1181                        return -ENODEV;
1182
1183                if (cpu_dai->driver->probe) {
1184                        ret = cpu_dai->driver->probe(cpu_dai);
1185                        if (ret < 0) {
1186                                printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1187                                                cpu_dai->name);
1188                                module_put(cpu_dai->dev->driver->owner);
1189                                return ret;
1190                        }
1191                }
1192                cpu_dai->probed = 1;
1193                /* mark cpu_dai as probed and add to card dai list */
1194                list_add(&cpu_dai->card_list, &card->dai_dev_list);
1195        }
1196
1197        /* probe the CODEC */
1198        if (!codec->probed &&
1199                        codec->driver->probe_order == order) {
1200                ret = soc_probe_codec(card, codec);
1201                if (ret < 0)
1202                        return ret;
1203        }
1204
1205        /* probe the platform */
1206        if (!platform->probed &&
1207                        platform->driver->probe_order == order) {
1208                ret = soc_probe_platform(card, platform);
1209                if (ret < 0)
1210                        return ret;
1211        }
1212
1213        /* probe the CODEC DAI */
1214        if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1215                if (codec_dai->driver->probe) {
1216                        ret = codec_dai->driver->probe(codec_dai);
1217                        if (ret < 0) {
1218                                printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1219                                                codec_dai->name);
1220                                return ret;
1221                        }
1222                }
1223
1224                /* mark codec_dai as probed and add to card dai list */
1225                codec_dai->probed = 1;
1226                list_add(&codec_dai->card_list, &card->dai_dev_list);
1227        }
1228
1229        /* complete DAI probe during last probe */
1230        if (order != SND_SOC_COMP_ORDER_LAST)
1231                return 0;
1232
1233        ret = soc_post_component_init(card, codec, num, 0);
1234        if (ret)
1235                return ret;
1236
1237        ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1238        if (ret < 0)
1239                printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1240
1241        /* create the pcm */
1242        ret = soc_new_pcm(rtd, num);
1243        if (ret < 0) {
1244                printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1245                return ret;
1246        }
1247
1248        /* add platform data for AC97 devices */
1249        if (rtd->codec_dai->driver->ac97_control)
1250                snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1251
1252        return 0;
1253}
1254
1255#ifdef CONFIG_SND_SOC_AC97_BUS
1256static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1257{
1258        int ret;
1259
1260        /* Only instantiate AC97 if not already done by the adaptor
1261         * for the generic AC97 subsystem.
1262         */
1263        if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1264                /*
1265                 * It is possible that the AC97 device is already registered to
1266                 * the device subsystem. This happens when the device is created
1267                 * via snd_ac97_mixer(). Currently only SoC codec that does so
1268                 * is the generic AC97 glue but others migh emerge.
1269                 *
1270                 * In those cases we don't try to register the device again.
1271                 */
1272                if (!rtd->codec->ac97_created)
1273                        return 0;
1274
1275                ret = soc_ac97_dev_register(rtd->codec);
1276                if (ret < 0) {
1277                        printk(KERN_ERR "asoc: AC97 device register failed\n");
1278                        return ret;
1279                }
1280
1281                rtd->codec->ac97_registered = 1;
1282        }
1283        return 0;
1284}
1285
1286static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1287{
1288        if (codec->ac97_registered) {
1289                soc_ac97_dev_unregister(codec);
1290                codec->ac97_registered = 0;
1291        }
1292}
1293#endif
1294
1295static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1296{
1297        struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1298        struct snd_soc_codec *codec;
1299        int ret = -ENODEV;
1300
1301        /* find CODEC from registered CODECs*/
1302        list_for_each_entry(codec, &codec_list, list) {
1303                if (!strcmp(codec->name, aux_dev->codec_name)) {
1304                        if (codec->probed) {
1305                                dev_err(codec->dev,
1306                                        "asoc: codec already probed");
1307                                ret = -EBUSY;
1308                                goto out;
1309                        }
1310                        goto found;
1311                }
1312        }
1313        /* codec not found */
1314        dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1315        goto out;
1316
1317found:
1318        ret = soc_probe_codec(card, codec);
1319        if (ret < 0)
1320                return ret;
1321
1322        ret = soc_post_component_init(card, codec, num, 1);
1323
1324out:
1325        return ret;
1326}
1327
1328static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1329{
1330        struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1331        struct snd_soc_codec *codec = rtd->codec;
1332
1333        /* unregister the rtd device */
1334        if (rtd->dev_registered) {
1335                device_remove_file(rtd->dev, &dev_attr_codec_reg);
1336                device_del(rtd->dev);
1337                rtd->dev_registered = 0;
1338        }
1339
1340        if (codec && codec->probed)
1341                soc_remove_codec(codec);
1342}
1343
1344static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1345                                    enum snd_soc_compress_type compress_type)
1346{
1347        int ret;
1348
1349        if (codec->cache_init)
1350                return 0;
1351
1352        /* override the compress_type if necessary */
1353        if (compress_type && codec->compress_type != compress_type)
1354                codec->compress_type = compress_type;
1355        ret = snd_soc_cache_init(codec);
1356        if (ret < 0) {
1357                dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1358                        ret);
1359                return ret;
1360        }
1361        codec->cache_init = 1;
1362        return 0;
1363}
1364
1365static void snd_soc_instantiate_card(struct snd_soc_card *card)
1366{
1367        struct snd_soc_codec *codec;
1368        struct snd_soc_codec_conf *codec_conf;
1369        enum snd_soc_compress_type compress_type;
1370        struct snd_soc_dai_link *dai_link;
1371        int ret, i, order;
1372
1373        mutex_lock(&card->mutex);
1374
1375        if (card->instantiated) {
1376                mutex_unlock(&card->mutex);
1377                return;
1378        }
1379
1380        /* bind DAIs */
1381        for (i = 0; i < card->num_links; i++)
1382                soc_bind_dai_link(card, i);
1383
1384        /* bind completed ? */
1385        if (card->num_rtd != card->num_links) {
1386                mutex_unlock(&card->mutex);
1387                return;
1388        }
1389
1390        /* initialize the register cache for each available codec */
1391        list_for_each_entry(codec, &codec_list, list) {
1392                if (codec->cache_init)
1393                        continue;
1394                /* by default we don't override the compress_type */
1395                compress_type = 0;
1396                /* check to see if we need to override the compress_type */
1397                for (i = 0; i < card->num_configs; ++i) {
1398                        codec_conf = &card->codec_conf[i];
1399                        if (!strcmp(codec->name, codec_conf->dev_name)) {
1400                                compress_type = codec_conf->compress_type;
1401                                if (compress_type && compress_type
1402                                    != codec->compress_type)
1403                                        break;
1404                        }
1405                }
1406                ret = snd_soc_init_codec_cache(codec, compress_type);
1407                if (ret < 0) {
1408                        mutex_unlock(&card->mutex);
1409                        return;
1410                }
1411        }
1412
1413        /* card bind complete so register a sound card */
1414        ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1415                        card->owner, 0, &card->snd_card);
1416        if (ret < 0) {
1417                printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1418                        card->name);
1419                mutex_unlock(&card->mutex);
1420                return;
1421        }
1422        card->snd_card->dev = card->dev;
1423
1424        card->dapm.bias_level = SND_SOC_BIAS_OFF;
1425        card->dapm.dev = card->dev;
1426        card->dapm.card = card;
1427        list_add(&card->dapm.list, &card->dapm_list);
1428
1429#ifdef CONFIG_DEBUG_FS
1430        snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1431#endif
1432
1433#ifdef CONFIG_PM_SLEEP
1434        /* deferred resume work */
1435        INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1436#endif
1437
1438        if (card->dapm_widgets)
1439                snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1440                                          card->num_dapm_widgets);
1441
1442        /* initialise the sound card only once */
1443        if (card->probe) {
1444                ret = card->probe(card);
1445                if (ret < 0)
1446                        goto card_probe_error;
1447        }
1448
1449        /* early DAI link probe */
1450        for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1451                        order++) {
1452                for (i = 0; i < card->num_links; i++) {
1453                        ret = soc_probe_dai_link(card, i, order);
1454                        if (ret < 0) {
1455                                pr_err("asoc: failed to instantiate card %s: %d\n",
1456                               card->name, ret);
1457                                goto probe_dai_err;
1458                        }
1459                }
1460        }
1461
1462        for (i = 0; i < card->num_aux_devs; i++) {
1463                ret = soc_probe_aux_dev(card, i);
1464                if (ret < 0) {
1465                        pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1466                               card->name, ret);
1467                        goto probe_aux_dev_err;
1468                }
1469        }
1470
1471        /* We should have a non-codec control add function but we don't */
1472        if (card->controls)
1473                snd_soc_add_controls(list_first_entry(&card->codec_dev_list,
1474                                                      struct snd_soc_codec,
1475                                                      card_list),
1476                                     card->controls,
1477                                     card->num_controls);
1478
1479        if (card->dapm_routes)
1480                snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1481                                        card->num_dapm_routes);
1482
1483        snd_soc_dapm_new_widgets(&card->dapm);
1484
1485        for (i = 0; i < card->num_links; i++) {
1486                dai_link = &card->dai_link[i];
1487
1488                if (dai_link->dai_fmt) {
1489                        ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1490                                                  dai_link->dai_fmt);
1491                        if (ret != 0)
1492                                dev_warn(card->rtd[i].codec_dai->dev,
1493                                         "Failed to set DAI format: %d\n",
1494                                         ret);
1495
1496                        ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1497                                                  dai_link->dai_fmt);
1498                        if (ret != 0)
1499                                dev_warn(card->rtd[i].cpu_dai->dev,
1500                                         "Failed to set DAI format: %d\n",
1501                                         ret);
1502                }
1503        }
1504
1505        snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1506                 "%s", card->name);
1507        snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1508                 "%s", card->long_name ? card->long_name : card->name);
1509        snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1510                 "%s", card->driver_name ? card->driver_name : card->name);
1511        for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1512                switch (card->snd_card->driver[i]) {
1513                case '_':
1514                case '-':
1515                case '\0':
1516                        break;
1517                default:
1518                        if (!isalnum(card->snd_card->driver[i]))
1519                                card->snd_card->driver[i] = '_';
1520                        break;
1521                }
1522        }
1523
1524        if (card->late_probe) {
1525                ret = card->late_probe(card);
1526                if (ret < 0) {
1527                        dev_err(card->dev, "%s late_probe() failed: %d\n",
1528                                card->name, ret);
1529                        goto probe_aux_dev_err;
1530                }
1531        }
1532
1533        snd_soc_dapm_new_widgets(&card->dapm);
1534
1535        if (card->fully_routed)
1536                list_for_each_entry(codec, &card->codec_dev_list, card_list)
1537                        snd_soc_dapm_auto_nc_codec_pins(codec);
1538
1539        ret = snd_card_register(card->snd_card);
1540        if (ret < 0) {
1541                printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1542                goto probe_aux_dev_err;
1543        }
1544
1545#ifdef CONFIG_SND_SOC_AC97_BUS
1546        /* register any AC97 codecs */
1547        for (i = 0; i < card->num_rtd; i++) {
1548                ret = soc_register_ac97_dai_link(&card->rtd[i]);
1549                if (ret < 0) {
1550                        printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1551                        while (--i >= 0)
1552                                soc_unregister_ac97_dai_link(card->rtd[i].codec);
1553                        goto probe_aux_dev_err;
1554                }
1555        }
1556#endif
1557
1558        card->instantiated = 1;
1559        snd_soc_dapm_sync(&card->dapm);
1560        mutex_unlock(&card->mutex);
1561        return;
1562
1563probe_aux_dev_err:
1564        for (i = 0; i < card->num_aux_devs; i++)
1565                soc_remove_aux_dev(card, i);
1566
1567probe_dai_err:
1568        soc_remove_dai_links(card);
1569
1570card_probe_error:
1571        if (card->remove)
1572                card->remove(card);
1573
1574        snd_card_free(card->snd_card);
1575
1576        mutex_unlock(&card->mutex);
1577}
1578
1579/*
1580 * Attempt to initialise any uninitialised cards.  Must be called with
1581 * client_mutex.
1582 */
1583static void snd_soc_instantiate_cards(void)
1584{
1585        struct snd_soc_card *card;
1586        list_for_each_entry(card, &card_list, list)
1587                snd_soc_instantiate_card(card);
1588}
1589
1590/* probes a new socdev */
1591static int soc_probe(struct platform_device *pdev)
1592{
1593        struct snd_soc_card *card = platform_get_drvdata(pdev);
1594        int ret = 0;
1595
1596        /*
1597         * no card, so machine driver should be registering card
1598         * we should not be here in that case so ret error
1599         */
1600        if (!card)
1601                return -EINVAL;
1602
1603        /* Bodge while we unpick instantiation */
1604        card->dev = &pdev->dev;
1605
1606        ret = snd_soc_register_card(card);
1607        if (ret != 0) {
1608                dev_err(&pdev->dev, "Failed to register card\n");
1609                return ret;
1610        }
1611
1612        return 0;
1613}
1614
1615static int soc_cleanup_card_resources(struct snd_soc_card *card)
1616{
1617        int i;
1618
1619        /* make sure any delayed work runs */
1620        for (i = 0; i < card->num_rtd; i++) {
1621                struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1622                flush_delayed_work_sync(&rtd->delayed_work);
1623        }
1624
1625        /* remove auxiliary devices */
1626        for (i = 0; i < card->num_aux_devs; i++)
1627                soc_remove_aux_dev(card, i);
1628
1629        /* remove and free each DAI */
1630        soc_remove_dai_links(card);
1631
1632        soc_cleanup_card_debugfs(card);
1633
1634        /* remove the card */
1635        if (card->remove)
1636                card->remove(card);
1637
1638        snd_soc_dapm_free(&card->dapm);
1639
1640        kfree(card->rtd);
1641        snd_card_free(card->snd_card);
1642        return 0;
1643
1644}
1645
1646/* removes a socdev */
1647static int soc_remove(struct platform_device *pdev)
1648{
1649        struct snd_soc_card *card = platform_get_drvdata(pdev);
1650
1651        snd_soc_unregister_card(card);
1652        return 0;
1653}
1654
1655int snd_soc_poweroff(struct device *dev)
1656{
1657        struct snd_soc_card *card = dev_get_drvdata(dev);
1658        int i;
1659
1660        if (!card->instantiated)
1661                return 0;
1662
1663        /* Flush out pmdown_time work - we actually do want to run it
1664         * now, we're shutting down so no imminent restart. */
1665        for (i = 0; i < card->num_rtd; i++) {
1666                struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1667                flush_delayed_work_sync(&rtd->delayed_work);
1668        }
1669
1670        snd_soc_dapm_shutdown(card);
1671
1672        return 0;
1673}
1674EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1675
1676const struct dev_pm_ops snd_soc_pm_ops = {
1677        .suspend = snd_soc_suspend,
1678        .resume = snd_soc_resume,
1679        .poweroff = snd_soc_poweroff,
1680};
1681EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1682
1683/* ASoC platform driver */
1684static struct platform_driver soc_driver = {
1685        .driver         = {
1686                .name           = "soc-audio",
1687                .owner          = THIS_MODULE,
1688                .pm             = &snd_soc_pm_ops,
1689        },
1690        .probe          = soc_probe,
1691        .remove         = soc_remove,
1692};
1693
1694/**
1695 * snd_soc_codec_volatile_register: Report if a register is volatile.
1696 *
1697 * @codec: CODEC to query.
1698 * @reg: Register to query.
1699 *
1700 * Boolean function indiciating if a CODEC register is volatile.
1701 */
1702int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1703                                    unsigned int reg)
1704{
1705        if (codec->volatile_register)
1706                return codec->volatile_register(codec, reg);
1707        else
1708                return 0;
1709}
1710EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1711
1712/**
1713 * snd_soc_codec_readable_register: Report if a register is readable.
1714 *
1715 * @codec: CODEC to query.
1716 * @reg: Register to query.
1717 *
1718 * Boolean function indicating if a CODEC register is readable.
1719 */
1720int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1721                                    unsigned int reg)
1722{
1723        if (codec->readable_register)
1724                return codec->readable_register(codec, reg);
1725        else
1726                return 1;
1727}
1728EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1729
1730/**
1731 * snd_soc_codec_writable_register: Report if a register is writable.
1732 *
1733 * @codec: CODEC to query.
1734 * @reg: Register to query.
1735 *
1736 * Boolean function indicating if a CODEC register is writable.
1737 */
1738int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1739                                    unsigned int reg)
1740{
1741        if (codec->writable_register)
1742                return codec->writable_register(codec, reg);
1743        else
1744                return 1;
1745}
1746EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1747
1748int snd_soc_platform_read(struct snd_soc_platform *platform,
1749                                        unsigned int reg)
1750{
1751        unsigned int ret;
1752
1753        if (!platform->driver->read) {
1754                dev_err(platform->dev, "platform has no read back\n");
1755                return -1;
1756        }
1757
1758        ret = platform->driver->read(platform, reg);
1759        dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
1760        trace_snd_soc_preg_read(platform, reg, ret);
1761
1762        return ret;
1763}
1764EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1765
1766int snd_soc_platform_write(struct snd_soc_platform *platform,
1767                                         unsigned int reg, unsigned int val)
1768{
1769        if (!platform->driver->write) {
1770                dev_err(platform->dev, "platform has no write back\n");
1771                return -1;
1772        }
1773
1774        dev_dbg(platform->dev, "write %x = %x\n", reg, val);
1775        trace_snd_soc_preg_write(platform, reg, val);
1776        return platform->driver->write(platform, reg, val);
1777}
1778EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1779
1780/**
1781 * snd_soc_new_ac97_codec - initailise AC97 device
1782 * @codec: audio codec
1783 * @ops: AC97 bus operations
1784 * @num: AC97 codec number
1785 *
1786 * Initialises AC97 codec resources for use by ad-hoc devices only.
1787 */
1788int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1789        struct snd_ac97_bus_ops *ops, int num)
1790{
1791        mutex_lock(&codec->mutex);
1792
1793        codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1794        if (codec->ac97 == NULL) {
1795                mutex_unlock(&codec->mutex);
1796                return -ENOMEM;
1797        }
1798
1799        codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1800        if (codec->ac97->bus == NULL) {
1801                kfree(codec->ac97);
1802                codec->ac97 = NULL;
1803                mutex_unlock(&codec->mutex);
1804                return -ENOMEM;
1805        }
1806
1807        codec->ac97->bus->ops = ops;
1808        codec->ac97->num = num;
1809
1810        /*
1811         * Mark the AC97 device to be created by us. This way we ensure that the
1812         * device will be registered with the device subsystem later on.
1813         */
1814        codec->ac97_created = 1;
1815
1816        mutex_unlock(&codec->mutex);
1817        return 0;
1818}
1819EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1820
1821/**
1822 * snd_soc_free_ac97_codec - free AC97 codec device
1823 * @codec: audio codec
1824 *
1825 * Frees AC97 codec device resources.
1826 */
1827void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1828{
1829        mutex_lock(&codec->mutex);
1830#ifdef CONFIG_SND_SOC_AC97_BUS
1831        soc_unregister_ac97_dai_link(codec);
1832#endif
1833        kfree(codec->ac97->bus);
1834        kfree(codec->ac97);
1835        codec->ac97 = NULL;
1836        codec->ac97_created = 0;
1837        mutex_unlock(&codec->mutex);
1838}
1839EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1840
1841unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1842{
1843        unsigned int ret;
1844
1845        ret = codec->read(codec, reg);
1846        dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
1847        trace_snd_soc_reg_read(codec, reg, ret);
1848
1849        return ret;
1850}
1851EXPORT_SYMBOL_GPL(snd_soc_read);
1852
1853unsigned int snd_soc_write(struct snd_soc_codec *codec,
1854                           unsigned int reg, unsigned int val)
1855{
1856        dev_dbg(codec->dev, "write %x = %x\n", reg, val);
1857        trace_snd_soc_reg_write(codec, reg, val);
1858        return codec->write(codec, reg, val);
1859}
1860EXPORT_SYMBOL_GPL(snd_soc_write);
1861
1862unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1863                                    unsigned int reg, const void *data, size_t len)
1864{
1865        return codec->bulk_write_raw(codec, reg, data, len);
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1868
1869/**
1870 * snd_soc_update_bits - update codec register bits
1871 * @codec: audio codec
1872 * @reg: codec register
1873 * @mask: register mask
1874 * @value: new value
1875 *
1876 * Writes new register value.
1877 *
1878 * Returns 1 for change, 0 for no change, or negative error code.
1879 */
1880int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1881                                unsigned int mask, unsigned int value)
1882{
1883        int change;
1884        unsigned int old, new;
1885        int ret;
1886
1887        ret = snd_soc_read(codec, reg);
1888        if (ret < 0)
1889                return ret;
1890
1891        old = ret;
1892        new = (old & ~mask) | (value & mask);
1893        change = old != new;
1894        if (change) {
1895                ret = snd_soc_write(codec, reg, new);
1896                if (ret < 0)
1897                        return ret;
1898        }
1899
1900        return change;
1901}
1902EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1903
1904/**
1905 * snd_soc_update_bits_locked - update codec register bits
1906 * @codec: audio codec
1907 * @reg: codec register
1908 * @mask: register mask
1909 * @value: new value
1910 *
1911 * Writes new register value, and takes the codec mutex.
1912 *
1913 * Returns 1 for change else 0.
1914 */
1915int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1916                               unsigned short reg, unsigned int mask,
1917                               unsigned int value)
1918{
1919        int change;
1920
1921        mutex_lock(&codec->mutex);
1922        change = snd_soc_update_bits(codec, reg, mask, value);
1923        mutex_unlock(&codec->mutex);
1924
1925        return change;
1926}
1927EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1928
1929/**
1930 * snd_soc_test_bits - test register for change
1931 * @codec: audio codec
1932 * @reg: codec register
1933 * @mask: register mask
1934 * @value: new value
1935 *
1936 * Tests a register with a new value and checks if the new value is
1937 * different from the old value.
1938 *
1939 * Returns 1 for change else 0.
1940 */
1941int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1942                                unsigned int mask, unsigned int value)
1943{
1944        int change;
1945        unsigned int old, new;
1946
1947        old = snd_soc_read(codec, reg);
1948        new = (old & ~mask) | value;
1949        change = old != new;
1950
1951        return change;
1952}
1953EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1954
1955/**
1956 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1957 * @substream: the pcm substream
1958 * @hw: the hardware parameters
1959 *
1960 * Sets the substream runtime hardware parameters.
1961 */
1962int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1963        const struct snd_pcm_hardware *hw)
1964{
1965        struct snd_pcm_runtime *runtime = substream->runtime;
1966        runtime->hw.info = hw->info;
1967        runtime->hw.formats = hw->formats;
1968        runtime->hw.period_bytes_min = hw->period_bytes_min;
1969        runtime->hw.period_bytes_max = hw->period_bytes_max;
1970        runtime->hw.periods_min = hw->periods_min;
1971        runtime->hw.periods_max = hw->periods_max;
1972        runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1973        runtime->hw.fifo_size = hw->fifo_size;
1974        return 0;
1975}
1976EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1977
1978/**
1979 * snd_soc_cnew - create new control
1980 * @_template: control template
1981 * @data: control private data
1982 * @long_name: control long name
1983 * @prefix: control name prefix
1984 *
1985 * Create a new mixer control from a template control.
1986 *
1987 * Returns 0 for success, else error.
1988 */
1989struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1990                                  void *data, char *long_name,
1991                                  const char *prefix)
1992{
1993        struct snd_kcontrol_new template;
1994        struct snd_kcontrol *kcontrol;
1995        char *name = NULL;
1996        int name_len;
1997
1998        memcpy(&template, _template, sizeof(template));
1999        template.index = 0;
2000
2001        if (!long_name)
2002                long_name = template.name;
2003
2004        if (prefix) {
2005                name_len = strlen(long_name) + strlen(prefix) + 2;
2006                name = kmalloc(name_len, GFP_KERNEL);
2007                if (!name)
2008                        return NULL;
2009
2010                snprintf(name, name_len, "%s %s", prefix, long_name);
2011
2012                template.name = name;
2013        } else {
2014                template.name = long_name;
2015        }
2016
2017        kcontrol = snd_ctl_new1(&template, data);
2018
2019        kfree(name);
2020
2021        return kcontrol;
2022}
2023EXPORT_SYMBOL_GPL(snd_soc_cnew);
2024
2025/**
2026 * snd_soc_add_controls - add an array of controls to a codec.
2027 * Convienience function to add a list of controls. Many codecs were
2028 * duplicating this code.
2029 *
2030 * @codec: codec to add controls to
2031 * @controls: array of controls to add
2032 * @num_controls: number of elements in the array
2033 *
2034 * Return 0 for success, else error.
2035 */
2036int snd_soc_add_controls(struct snd_soc_codec *codec,
2037        const struct snd_kcontrol_new *controls, int num_controls)
2038{
2039        struct snd_card *card = codec->card->snd_card;
2040        int err, i;
2041
2042        for (i = 0; i < num_controls; i++) {
2043                const struct snd_kcontrol_new *control = &controls[i];
2044                err = snd_ctl_add(card, snd_soc_cnew(control, codec,
2045                                                     control->name,
2046                                                     codec->name_prefix));
2047                if (err < 0) {
2048                        dev_err(codec->dev, "%s: Failed to add %s: %d\n",
2049                                codec->name, control->name, err);
2050                        return err;
2051                }
2052        }
2053
2054        return 0;
2055}
2056EXPORT_SYMBOL_GPL(snd_soc_add_controls);
2057
2058/**
2059 * snd_soc_add_platform_controls - add an array of controls to a platform.
2060 * Convienience function to add a list of controls.
2061 *
2062 * @platform: platform to add controls to
2063 * @controls: array of controls to add
2064 * @num_controls: number of elements in the array
2065 *
2066 * Return 0 for success, else error.
2067 */
2068int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2069        const struct snd_kcontrol_new *controls, int num_controls)
2070{
2071        struct snd_card *card = platform->card->snd_card;
2072        int err, i;
2073
2074        for (i = 0; i < num_controls; i++) {
2075                const struct snd_kcontrol_new *control = &controls[i];
2076                err = snd_ctl_add(card, snd_soc_cnew(control, platform,
2077                                control->name, NULL));
2078                if (err < 0) {
2079                        dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
2080                        return err;
2081                }
2082        }
2083
2084        return 0;
2085}
2086EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2087
2088/**
2089 * snd_soc_info_enum_double - enumerated double mixer info callback
2090 * @kcontrol: mixer control
2091 * @uinfo: control element information
2092 *
2093 * Callback to provide information about a double enumerated
2094 * mixer control.
2095 *
2096 * Returns 0 for success.
2097 */
2098int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2099        struct snd_ctl_elem_info *uinfo)
2100{
2101        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2102
2103        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2104        uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2105        uinfo->value.enumerated.items = e->max;
2106
2107        if (uinfo->value.enumerated.item > e->max - 1)
2108                uinfo->value.enumerated.item = e->max - 1;
2109        strcpy(uinfo->value.enumerated.name,
2110                e->texts[uinfo->value.enumerated.item]);
2111        return 0;
2112}
2113EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2114
2115/**
2116 * snd_soc_get_enum_double - enumerated double mixer get callback
2117 * @kcontrol: mixer control
2118 * @ucontrol: control element information
2119 *
2120 * Callback to get the value of a double enumerated mixer.
2121 *
2122 * Returns 0 for success.
2123 */
2124int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2125        struct snd_ctl_elem_value *ucontrol)
2126{
2127        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2128        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2129        unsigned int val, bitmask;
2130
2131        for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2132                ;
2133        val = snd_soc_read(codec, e->reg);
2134        ucontrol->value.enumerated.item[0]
2135                = (val >> e->shift_l) & (bitmask - 1);
2136        if (e->shift_l != e->shift_r)
2137                ucontrol->value.enumerated.item[1] =
2138                        (val >> e->shift_r) & (bitmask - 1);
2139
2140        return 0;
2141}
2142EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2143
2144/**
2145 * snd_soc_put_enum_double - enumerated double mixer put callback
2146 * @kcontrol: mixer control
2147 * @ucontrol: control element information
2148 *
2149 * Callback to set the value of a double enumerated mixer.
2150 *
2151 * Returns 0 for success.
2152 */
2153int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2154        struct snd_ctl_elem_value *ucontrol)
2155{
2156        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2157        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2158        unsigned int val;
2159        unsigned int mask, bitmask;
2160
2161        for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
2162                ;
2163        if (ucontrol->value.enumerated.item[0] > e->max - 1)
2164                return -EINVAL;
2165        val = ucontrol->value.enumerated.item[0] << e->shift_l;
2166        mask = (bitmask - 1) << e->shift_l;
2167        if (e->shift_l != e->shift_r) {
2168                if (ucontrol->value.enumerated.item[1] > e->max - 1)
2169                        return -EINVAL;
2170                val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2171                mask |= (bitmask - 1) << e->shift_r;
2172        }
2173
2174        return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2177
2178/**
2179 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2180 * @kcontrol: mixer control
2181 * @ucontrol: control element information
2182 *
2183 * Callback to get the value of a double semi enumerated mixer.
2184 *
2185 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2186 * used for handling bitfield coded enumeration for example.
2187 *
2188 * Returns 0 for success.
2189 */
2190int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2191        struct snd_ctl_elem_value *ucontrol)
2192{
2193        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2194        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2195        unsigned int reg_val, val, mux;
2196
2197        reg_val = snd_soc_read(codec, e->reg);
2198        val = (reg_val >> e->shift_l) & e->mask;
2199        for (mux = 0; mux < e->max; mux++) {
2200                if (val == e->values[mux])
2201                        break;
2202        }
2203        ucontrol->value.enumerated.item[0] = mux;
2204        if (e->shift_l != e->shift_r) {
2205                val = (reg_val >> e->shift_r) & e->mask;
2206                for (mux = 0; mux < e->max; mux++) {
2207                        if (val == e->values[mux])
2208                                break;
2209                }
2210                ucontrol->value.enumerated.item[1] = mux;
2211        }
2212
2213        return 0;
2214}
2215EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2216
2217/**
2218 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2219 * @kcontrol: mixer control
2220 * @ucontrol: control element information
2221 *
2222 * Callback to set the value of a double semi enumerated mixer.
2223 *
2224 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2225 * used for handling bitfield coded enumeration for example.
2226 *
2227 * Returns 0 for success.
2228 */
2229int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2230        struct snd_ctl_elem_value *ucontrol)
2231{
2232        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2233        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2234        unsigned int val;
2235        unsigned int mask;
2236
2237        if (ucontrol->value.enumerated.item[0] > e->max - 1)
2238                return -EINVAL;
2239        val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2240        mask = e->mask << e->shift_l;
2241        if (e->shift_l != e->shift_r) {
2242                if (ucontrol->value.enumerated.item[1] > e->max - 1)
2243                        return -EINVAL;
2244                val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2245                mask |= e->mask << e->shift_r;
2246        }
2247
2248        return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2249}
2250EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2251
2252/**
2253 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2254 * @kcontrol: mixer control
2255 * @uinfo: control element information
2256 *
2257 * Callback to provide information about an external enumerated
2258 * single mixer.
2259 *
2260 * Returns 0 for success.
2261 */
2262int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2263        struct snd_ctl_elem_info *uinfo)
2264{
2265        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2266
2267        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2268        uinfo->count = 1;
2269        uinfo->value.enumerated.items = e->max;
2270
2271        if (uinfo->value.enumerated.item > e->max - 1)
2272                uinfo->value.enumerated.item = e->max - 1;
2273        strcpy(uinfo->value.enumerated.name,
2274                e->texts[uinfo->value.enumerated.item]);
2275        return 0;
2276}
2277EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2278
2279/**
2280 * snd_soc_info_volsw_ext - external single mixer info callback
2281 * @kcontrol: mixer control
2282 * @uinfo: control element information
2283 *
2284 * Callback to provide information about a single external mixer control.
2285 *
2286 * Returns 0 for success.
2287 */
2288int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2289        struct snd_ctl_elem_info *uinfo)
2290{
2291        int max = kcontrol->private_value;
2292
2293        if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2294                uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2295        else
2296                uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2297
2298        uinfo->count = 1;
2299        uinfo->value.integer.min = 0;
2300        uinfo->value.integer.max = max;
2301        return 0;
2302}
2303EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2304
2305/**
2306 * snd_soc_info_volsw - single mixer info callback
2307 * @kcontrol: mixer control
2308 * @uinfo: control element information
2309 *
2310 * Callback to provide information about a single mixer control, or a double
2311 * mixer control that spans 2 registers.
2312 *
2313 * Returns 0 for success.
2314 */
2315int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2316        struct snd_ctl_elem_info *uinfo)
2317{
2318        struct soc_mixer_control *mc =
2319                (struct soc_mixer_control *)kcontrol->private_value;
2320        int platform_max;
2321
2322        if (!mc->platform_max)
2323                mc->platform_max = mc->max;
2324        platform_max = mc->platform_max;
2325
2326        if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2327                uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2328        else
2329                uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2330
2331        uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2332        uinfo->value.integer.min = 0;
2333        uinfo->value.integer.max = platform_max;
2334        return 0;
2335}
2336EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2337
2338/**
2339 * snd_soc_get_volsw - single mixer get callback
2340 * @kcontrol: mixer control
2341 * @ucontrol: control element information
2342 *
2343 * Callback to get the value of a single mixer control, or a double mixer
2344 * control that spans 2 registers.
2345 *
2346 * Returns 0 for success.
2347 */
2348int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2349        struct snd_ctl_elem_value *ucontrol)
2350{
2351        struct soc_mixer_control *mc =
2352                (struct soc_mixer_control *)kcontrol->private_value;
2353        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2354        unsigned int reg = mc->reg;
2355        unsigned int reg2 = mc->rreg;
2356        unsigned int shift = mc->shift;
2357        unsigned int rshift = mc->rshift;
2358        int max = mc->max;
2359        unsigned int mask = (1 << fls(max)) - 1;
2360        unsigned int invert = mc->invert;
2361
2362        ucontrol->value.integer.value[0] =
2363                (snd_soc_read(codec, reg) >> shift) & mask;
2364        if (invert)
2365                ucontrol->value.integer.value[0] =
2366                        max - ucontrol->value.integer.value[0];
2367
2368        if (snd_soc_volsw_is_stereo(mc)) {
2369                if (reg == reg2)
2370                        ucontrol->value.integer.value[1] =
2371                                (snd_soc_read(codec, reg) >> rshift) & mask;
2372                else
2373                        ucontrol->value.integer.value[1] =
2374                                (snd_soc_read(codec, reg2) >> shift) & mask;
2375                if (invert)
2376                        ucontrol->value.integer.value[1] =
2377                                max - ucontrol->value.integer.value[1];
2378        }
2379
2380        return 0;
2381}
2382EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2383
2384/**
2385 * snd_soc_put_volsw - single mixer put callback
2386 * @kcontrol: mixer control
2387 * @ucontrol: control element information
2388 *
2389 * Callback to set the value of a single mixer control, or a double mixer
2390 * control that spans 2 registers.
2391 *
2392 * Returns 0 for success.
2393 */
2394int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2395        struct snd_ctl_elem_value *ucontrol)
2396{
2397        struct soc_mixer_control *mc =
2398                (struct soc_mixer_control *)kcontrol->private_value;
2399        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2400        unsigned int reg = mc->reg;
2401        unsigned int reg2 = mc->rreg;
2402        unsigned int shift = mc->shift;
2403        unsigned int rshift = mc->rshift;
2404        int max = mc->max;
2405        unsigned int mask = (1 << fls(max)) - 1;
2406        unsigned int invert = mc->invert;
2407        int err;
2408        bool type_2r = 0;
2409        unsigned int val2 = 0;
2410        unsigned int val, val_mask;
2411
2412        val = (ucontrol->value.integer.value[0] & mask);
2413        if (invert)
2414                val = max - val;
2415        val_mask = mask << shift;
2416        val = val << shift;
2417        if (snd_soc_volsw_is_stereo(mc)) {
2418                val2 = (ucontrol->value.integer.value[1] & mask);
2419                if (invert)
2420                        val2 = max - val2;
2421                if (reg == reg2) {
2422                        val_mask |= mask << rshift;
2423                        val |= val2 << rshift;
2424                } else {
2425                        val2 = val2 << shift;
2426                        type_2r = 1;
2427                }
2428        }
2429        err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2430        if (err < 0)
2431                return err;
2432
2433        if (type_2r)
2434                err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2435
2436        return err;
2437}
2438EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2439
2440/**
2441 * snd_soc_info_volsw_s8 - signed mixer info callback
2442 * @kcontrol: mixer control
2443 * @uinfo: control element information
2444 *
2445 * Callback to provide information about a signed mixer control.
2446 *
2447 * Returns 0 for success.
2448 */
2449int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2450        struct snd_ctl_elem_info *uinfo)
2451{
2452        struct soc_mixer_control *mc =
2453                (struct soc_mixer_control *)kcontrol->private_value;
2454        int platform_max;
2455        int min = mc->min;
2456
2457        if (!mc->platform_max)
2458                mc->platform_max = mc->max;
2459        platform_max = mc->platform_max;
2460
2461        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2462        uinfo->count = 2;
2463        uinfo->value.integer.min = 0;
2464        uinfo->value.integer.max = platform_max - min;
2465        return 0;
2466}
2467EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2468
2469/**
2470 * snd_soc_get_volsw_s8 - signed mixer get callback
2471 * @kcontrol: mixer control
2472 * @ucontrol: control element information
2473 *
2474 * Callback to get the value of a signed mixer control.
2475 *
2476 * Returns 0 for success.
2477 */
2478int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2479        struct snd_ctl_elem_value *ucontrol)
2480{
2481        struct soc_mixer_control *mc =
2482                (struct soc_mixer_control *)kcontrol->private_value;
2483        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2484        unsigned int reg = mc->reg;
2485        int min = mc->min;
2486        int val = snd_soc_read(codec, reg);
2487
2488        ucontrol->value.integer.value[0] =
2489                ((signed char)(val & 0xff))-min;
2490        ucontrol->value.integer.value[1] =
2491                ((signed char)((val >> 8) & 0xff))-min;
2492        return 0;
2493}
2494EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2495
2496/**
2497 * snd_soc_put_volsw_sgn - signed mixer put callback
2498 * @kcontrol: mixer control
2499 * @ucontrol: control element information
2500 *
2501 * Callback to set the value of a signed mixer control.
2502 *
2503 * Returns 0 for success.
2504 */
2505int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2506        struct snd_ctl_elem_value *ucontrol)
2507{
2508        struct soc_mixer_control *mc =
2509                (struct soc_mixer_control *)kcontrol->private_value;
2510        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2511        unsigned int reg = mc->reg;
2512        int min = mc->min;
2513        unsigned int val;
2514
2515        val = (ucontrol->value.integer.value[0]+min) & 0xff;
2516        val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2517
2518        return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2519}
2520EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2521
2522/**
2523 * snd_soc_limit_volume - Set new limit to an existing volume control.
2524 *
2525 * @codec: where to look for the control
2526 * @name: Name of the control
2527 * @max: new maximum limit
2528 *
2529 * Return 0 for success, else error.
2530 */
2531int snd_soc_limit_volume(struct snd_soc_codec *codec,
2532        const char *name, int max)
2533{
2534        struct snd_card *card = codec->card->snd_card;
2535        struct snd_kcontrol *kctl;
2536        struct soc_mixer_control *mc;
2537        int found = 0;
2538        int ret = -EINVAL;
2539
2540        /* Sanity check for name and max */
2541        if (unlikely(!name || max <= 0))
2542                return -EINVAL;
2543
2544        list_for_each_entry(kctl, &card->controls, list) {
2545                if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2546                        found = 1;
2547                        break;
2548                }
2549        }
2550        if (found) {
2551                mc = (struct soc_mixer_control *)kctl->private_value;
2552                if (max <= mc->max) {
2553                        mc->platform_max = max;
2554                        ret = 0;
2555                }
2556        }
2557        return ret;
2558}
2559EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2560
2561/**
2562 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2563 *  mixer info callback
2564 * @kcontrol: mixer control
2565 * @uinfo: control element information
2566 *
2567 * Returns 0 for success.
2568 */
2569int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2570                        struct snd_ctl_elem_info *uinfo)
2571{
2572        struct soc_mixer_control *mc =
2573                (struct soc_mixer_control *)kcontrol->private_value;
2574        int max = mc->max;
2575        int min = mc->min;
2576
2577        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2578        uinfo->count = 2;
2579        uinfo->value.integer.min = 0;
2580        uinfo->value.integer.max = max-min;
2581
2582        return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2585
2586/**
2587 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2588 *  mixer get callback
2589 * @kcontrol: mixer control
2590 * @uinfo: control element information
2591 *
2592 * Returns 0 for success.
2593 */
2594int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2595                        struct snd_ctl_elem_value *ucontrol)
2596{
2597        struct soc_mixer_control *mc =
2598                (struct soc_mixer_control *)kcontrol->private_value;
2599        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2600        unsigned int mask = (1<<mc->shift)-1;
2601        int min = mc->min;
2602        int val = snd_soc_read(codec, mc->reg) & mask;
2603        int valr = snd_soc_read(codec, mc->rreg) & mask;
2604
2605        ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2606        ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2607        return 0;
2608}
2609EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2610
2611/**
2612 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2613 *  mixer put callback
2614 * @kcontrol: mixer control
2615 * @uinfo: control element information
2616 *
2617 * Returns 0 for success.
2618 */
2619int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2620                        struct snd_ctl_elem_value *ucontrol)
2621{
2622        struct soc_mixer_control *mc =
2623                (struct soc_mixer_control *)kcontrol->private_value;
2624        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2625        unsigned int mask = (1<<mc->shift)-1;
2626        int min = mc->min;
2627        int ret;
2628        unsigned int val, valr, oval, ovalr;
2629
2630        val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2631        val &= mask;
2632        valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2633        valr &= mask;
2634
2635        oval = snd_soc_read(codec, mc->reg) & mask;
2636        ovalr = snd_soc_read(codec, mc->rreg) & mask;
2637
2638        ret = 0;
2639        if (oval != val) {
2640                ret = snd_soc_write(codec, mc->reg, val);
2641                if (ret < 0)
2642                        return ret;
2643        }
2644        if (ovalr != valr) {
2645                ret = snd_soc_write(codec, mc->rreg, valr);
2646                if (ret < 0)
2647                        return ret;
2648        }
2649
2650        return 0;
2651}
2652EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2653
2654/**
2655 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2656 * @dai: DAI
2657 * @clk_id: DAI specific clock ID
2658 * @freq: new clock frequency in Hz
2659 * @dir: new clock direction - input/output.
2660 *
2661 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2662 */
2663int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2664        unsigned int freq, int dir)
2665{
2666        if (dai->driver && dai->driver->ops->set_sysclk)
2667                return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2668        else if (dai->codec && dai->codec->driver->set_sysclk)
2669                return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
2670                                                      freq, dir);
2671        else
2672                return -EINVAL;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2675
2676/**
2677 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2678 * @codec: CODEC
2679 * @clk_id: DAI specific clock ID
2680 * @source: Source for the clock
2681 * @freq: new clock frequency in Hz
2682 * @dir: new clock direction - input/output.
2683 *
2684 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2685 */
2686int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
2687                             int source, unsigned int freq, int dir)
2688{
2689        if (codec->driver->set_sysclk)
2690                return codec->driver->set_sysclk(codec, clk_id, source,
2691                                                 freq, dir);
2692        else
2693                return -EINVAL;
2694}
2695EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2696
2697/**
2698 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2699 * @dai: DAI
2700 * @div_id: DAI specific clock divider ID
2701 * @div: new clock divisor.
2702 *
2703 * Configures the clock dividers. This is used to derive the best DAI bit and
2704 * frame clocks from the system or master clock. It's best to set the DAI bit
2705 * and frame clocks as low as possible to save system power.
2706 */
2707int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2708        int div_id, int div)
2709{
2710        if (dai->driver && dai->driver->ops->set_clkdiv)
2711                return dai->driver->ops->set_clkdiv(dai, div_id, div);
2712        else
2713                return -EINVAL;
2714}
2715EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2716
2717/**
2718 * snd_soc_dai_set_pll - configure DAI PLL.
2719 * @dai: DAI
2720 * @pll_id: DAI specific PLL ID
2721 * @source: DAI specific source for the PLL
2722 * @freq_in: PLL input clock frequency in Hz
2723 * @freq_out: requested PLL output clock frequency in Hz
2724 *
2725 * Configures and enables PLL to generate output clock based on input clock.
2726 */
2727int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2728        unsigned int freq_in, unsigned int freq_out)
2729{
2730        if (dai->driver && dai->driver->ops->set_pll)
2731                return dai->driver->ops->set_pll(dai, pll_id, source,
2732                                         freq_in, freq_out);
2733        else if (dai->codec && dai->codec->driver->set_pll)
2734                return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2735                                                   freq_in, freq_out);
2736        else
2737                return -EINVAL;
2738}
2739EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2740
2741/*
2742 * snd_soc_codec_set_pll - configure codec PLL.
2743 * @codec: CODEC
2744 * @pll_id: DAI specific PLL ID
2745 * @source: DAI specific source for the PLL
2746 * @freq_in: PLL input clock frequency in Hz
2747 * @freq_out: requested PLL output clock frequency in Hz
2748 *
2749 * Configures and enables PLL to generate output clock based on input clock.
2750 */
2751int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2752                          unsigned int freq_in, unsigned int freq_out)
2753{
2754        if (codec->driver->set_pll)
2755                return codec->driver->set_pll(codec, pll_id, source,
2756                                              freq_in, freq_out);
2757        else
2758                return -EINVAL;
2759}
2760EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2761
2762/**
2763 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2764 * @dai: DAI
2765 * @fmt: SND_SOC_DAIFMT_ format value.
2766 *
2767 * Configures the DAI hardware format and clocking.
2768 */
2769int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2770{
2771        if (dai->driver && dai->driver->ops->set_fmt)
2772                return dai->driver->ops->set_fmt(dai, fmt);
2773        else
2774                return -EINVAL;
2775}
2776EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2777
2778/**
2779 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2780 * @dai: DAI
2781 * @tx_mask: bitmask representing active TX slots.
2782 * @rx_mask: bitmask representing active RX slots.
2783 * @slots: Number of slots in use.
2784 * @slot_width: Width in bits for each slot.
2785 *
2786 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2787 * specific.
2788 */
2789int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2790        unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2791{
2792        if (dai->driver && dai->driver->ops->set_tdm_slot)
2793                return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2794                                slots, slot_width);
2795        else
2796                return -EINVAL;
2797}
2798EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2799
2800/**
2801 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2802 * @dai: DAI
2803 * @tx_num: how many TX channels
2804 * @tx_slot: pointer to an array which imply the TX slot number channel
2805 *           0~num-1 uses
2806 * @rx_num: how many RX channels
2807 * @rx_slot: pointer to an array which imply the RX slot number channel
2808 *           0~num-1 uses
2809 *
2810 * configure the relationship between channel number and TDM slot number.
2811 */
2812int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2813        unsigned int tx_num, unsigned int *tx_slot,
2814        unsigned int rx_num, unsigned int *rx_slot)
2815{
2816        if (dai->driver && dai->driver->ops->set_channel_map)
2817                return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2818                        rx_num, rx_slot);
2819        else
2820                return -EINVAL;
2821}
2822EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2823
2824/**
2825 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2826 * @dai: DAI
2827 * @tristate: tristate enable
2828 *
2829 * Tristates the DAI so that others can use it.
2830 */
2831int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2832{
2833        if (dai->driver && dai->driver->ops->set_tristate)
2834                return dai->driver->ops->set_tristate(dai, tristate);
2835        else
2836                return -EINVAL;
2837}
2838EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2839
2840/**
2841 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2842 * @dai: DAI
2843 * @mute: mute enable
2844 *
2845 * Mutes the DAI DAC.
2846 */
2847int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2848{
2849        if (dai->driver && dai->driver->ops->digital_mute)
2850                return dai->driver->ops->digital_mute(dai, mute);
2851        else
2852                return -EINVAL;
2853}
2854EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2855
2856/**
2857 * snd_soc_register_card - Register a card with the ASoC core
2858 *
2859 * @card: Card to register
2860 *
2861 */
2862int snd_soc_register_card(struct snd_soc_card *card)
2863{
2864        int i;
2865
2866        if (!card->name || !card->dev)
2867                return -EINVAL;
2868
2869        for (i = 0; i < card->num_links; i++) {
2870                struct snd_soc_dai_link *link = &card->dai_link[i];
2871
2872                /*
2873                 * Codec must be specified by 1 of name or OF node,
2874                 * not both or neither.
2875                 */
2876                if (!!link->codec_name == !!link->codec_of_node) {
2877                        dev_err(card->dev,
2878                                "Neither/both codec name/of_node are set\n");
2879                        return -EINVAL;
2880                }
2881
2882                /*
2883                 * Platform may be specified by either name or OF node, but
2884                 * can be left unspecified, and a dummy platform will be used.
2885                 */
2886                if (link->platform_name && link->platform_of_node) {
2887                        dev_err(card->dev,
2888                                "Both platform name/of_node are set\n");
2889                        return -EINVAL;
2890                }
2891
2892                /*
2893                 * CPU DAI must be specified by 1 of name or OF node,
2894                 * not both or neither.
2895                 */
2896                if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
2897                        dev_err(card->dev,
2898                                "Neither/both cpu_dai name/of_node are set\n");
2899                        return -EINVAL;
2900                }
2901        }
2902
2903        dev_set_drvdata(card->dev, card);
2904
2905        snd_soc_initialize_card_lists(card);
2906
2907        soc_init_card_debugfs(card);
2908
2909        card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
2910                            (card->num_links + card->num_aux_devs),
2911                            GFP_KERNEL);
2912        if (card->rtd == NULL)
2913                return -ENOMEM;
2914        card->rtd_aux = &card->rtd[card->num_links];
2915
2916        for (i = 0; i < card->num_links; i++)
2917                card->rtd[i].dai_link = &card->dai_link[i];
2918
2919        INIT_LIST_HEAD(&card->list);
2920        INIT_LIST_HEAD(&card->dapm_dirty);
2921        card->instantiated = 0;
2922        mutex_init(&card->mutex);
2923
2924        mutex_lock(&client_mutex);
2925        list_add(&card->list, &card_list);
2926        snd_soc_instantiate_cards();
2927        mutex_unlock(&client_mutex);
2928
2929        dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2930
2931        return 0;
2932}
2933EXPORT_SYMBOL_GPL(snd_soc_register_card);
2934
2935/**
2936 * snd_soc_unregister_card - Unregister a card with the ASoC core
2937 *
2938 * @card: Card to unregister
2939 *
2940 */
2941int snd_soc_unregister_card(struct snd_soc_card *card)
2942{
2943        if (card->instantiated)
2944                soc_cleanup_card_resources(card);
2945        mutex_lock(&client_mutex);
2946        list_del(&card->list);
2947        mutex_unlock(&client_mutex);
2948        dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2949
2950        return 0;
2951}
2952EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
2953
2954/*
2955 * Simplify DAI link configuration by removing ".-1" from device names
2956 * and sanitizing names.
2957 */
2958static char *fmt_single_name(struct device *dev, int *id)
2959{
2960        char *found, name[NAME_SIZE];
2961        int id1, id2;
2962
2963        if (dev_name(dev) == NULL)
2964                return NULL;
2965
2966        strlcpy(name, dev_name(dev), NAME_SIZE);
2967
2968        /* are we a "%s.%d" name (platform and SPI components) */
2969        found = strstr(name, dev->driver->name);
2970        if (found) {
2971                /* get ID */
2972                if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2973
2974                        /* discard ID from name if ID == -1 */
2975                        if (*id == -1)
2976                                found[strlen(dev->driver->name)] = '\0';
2977                }
2978
2979        } else {
2980                /* I2C component devices are named "bus-addr"  */
2981                if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2982                        char tmp[NAME_SIZE];
2983
2984                        /* create unique ID number from I2C addr and bus */
2985                        *id = ((id1 & 0xffff) << 16) + id2;
2986
2987                        /* sanitize component name for DAI link creation */
2988                        snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2989                        strlcpy(name, tmp, NAME_SIZE);
2990                } else
2991                        *id = 0;
2992        }
2993
2994        return kstrdup(name, GFP_KERNEL);
2995}
2996
2997/*
2998 * Simplify DAI link naming for single devices with multiple DAIs by removing
2999 * any ".-1" and using the DAI name (instead of device name).
3000 */
3001static inline char *fmt_multiple_name(struct device *dev,
3002                struct snd_soc_dai_driver *dai_drv)
3003{
3004        if (dai_drv->name == NULL) {
3005                printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
3006                                dev_name(dev));
3007                return NULL;
3008        }
3009
3010        return kstrdup(dai_drv->name, GFP_KERNEL);
3011}
3012
3013/**
3014 * snd_soc_register_dai - Register a DAI with the ASoC core
3015 *
3016 * @dai: DAI to register
3017 */
3018int snd_soc_register_dai(struct device *dev,
3019                struct snd_soc_dai_driver *dai_drv)
3020{
3021        struct snd_soc_dai *dai;
3022
3023        dev_dbg(dev, "dai register %s\n", dev_name(dev));
3024
3025        dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3026        if (dai == NULL)
3027                return -ENOMEM;
3028
3029        /* create DAI component name */
3030        dai->name = fmt_single_name(dev, &dai->id);
3031        if (dai->name == NULL) {
3032                kfree(dai);
3033                return -ENOMEM;
3034        }
3035
3036        dai->dev = dev;
3037        dai->driver = dai_drv;
3038        if (!dai->driver->ops)
3039                dai->driver->ops = &null_dai_ops;
3040
3041        mutex_lock(&client_mutex);
3042        list_add(&dai->list, &dai_list);
3043        snd_soc_instantiate_cards();
3044        mutex_unlock(&client_mutex);
3045
3046        pr_debug("Registered DAI '%s'\n", dai->name);
3047
3048        return 0;
3049}
3050EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3051
3052/**
3053 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3054 *
3055 * @dai: DAI to unregister
3056 */
3057void snd_soc_unregister_dai(struct device *dev)
3058{
3059        struct snd_soc_dai *dai;
3060
3061        list_for_each_entry(dai, &dai_list, list) {
3062                if (dev == dai->dev)
3063                        goto found;
3064        }
3065        return;
3066
3067found:
3068        mutex_lock(&client_mutex);
3069        list_del(&dai->list);
3070        mutex_unlock(&client_mutex);
3071
3072        pr_debug("Unregistered DAI '%s'\n", dai->name);
3073        kfree(dai->name);
3074        kfree(dai);
3075}
3076EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3077
3078/**
3079 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3080 *
3081 * @dai: Array of DAIs to register
3082 * @count: Number of DAIs
3083 */
3084int snd_soc_register_dais(struct device *dev,
3085                struct snd_soc_dai_driver *dai_drv, size_t count)
3086{
3087        struct snd_soc_dai *dai;
3088        int i, ret = 0;
3089
3090        dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
3091
3092        for (i = 0; i < count; i++) {
3093
3094                dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3095                if (dai == NULL) {
3096                        ret = -ENOMEM;
3097                        goto err;
3098                }
3099
3100                /* create DAI component name */
3101                dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3102                if (dai->name == NULL) {
3103                        kfree(dai);
3104                        ret = -EINVAL;
3105                        goto err;
3106                }
3107
3108                dai->dev = dev;
3109                dai->driver = &dai_drv[i];
3110                if (dai->driver->id)
3111                        dai->id = dai->driver->id;
3112                else
3113                        dai->id = i;
3114                if (!dai->driver->ops)
3115                        dai->driver->ops = &null_dai_ops;
3116
3117                mutex_lock(&client_mutex);
3118                list_add(&dai->list, &dai_list);
3119                mutex_unlock(&client_mutex);
3120
3121                pr_debug("Registered DAI '%s'\n", dai->name);
3122        }
3123
3124        mutex_lock(&client_mutex);
3125        snd_soc_instantiate_cards();
3126        mutex_unlock(&client_mutex);
3127        return 0;
3128
3129err:
3130        for (i--; i >= 0; i--)
3131                snd_soc_unregister_dai(dev);
3132
3133        return ret;
3134}
3135EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3136
3137/**
3138 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3139 *
3140 * @dai: Array of DAIs to unregister
3141 * @count: Number of DAIs
3142 */
3143void snd_soc_unregister_dais(struct device *dev, size_t count)
3144{
3145        int i;
3146
3147        for (i = 0; i < count; i++)
3148                snd_soc_unregister_dai(dev);
3149}
3150EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3151
3152/**
3153 * snd_soc_register_platform - Register a platform with the ASoC core
3154 *
3155 * @platform: platform to register
3156 */
3157int snd_soc_register_platform(struct device *dev,
3158                struct snd_soc_platform_driver *platform_drv)
3159{
3160        struct snd_soc_platform *platform;
3161
3162        dev_dbg(dev, "platform register %s\n", dev_name(dev));
3163
3164        platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3165        if (platform == NULL)
3166                return -ENOMEM;
3167
3168        /* create platform component name */
3169        platform->name = fmt_single_name(dev, &platform->id);
3170        if (platform->name == NULL) {
3171                kfree(platform);
3172                return -ENOMEM;
3173        }
3174
3175        platform->dev = dev;
3176        platform->driver = platform_drv;
3177        platform->dapm.dev = dev;
3178        platform->dapm.platform = platform;
3179        platform->dapm.stream_event = platform_drv->stream_event;
3180
3181        mutex_lock(&client_mutex);
3182        list_add(&platform->list, &platform_list);
3183        snd_soc_instantiate_cards();
3184        mutex_unlock(&client_mutex);
3185
3186        pr_debug("Registered platform '%s'\n", platform->name);
3187
3188        return 0;
3189}
3190EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3191
3192/**
3193 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3194 *
3195 * @platform: platform to unregister
3196 */
3197void snd_soc_unregister_platform(struct device *dev)
3198{
3199        struct snd_soc_platform *platform;
3200
3201        list_for_each_entry(platform, &platform_list, list) {
3202                if (dev == platform->dev)
3203                        goto found;
3204        }
3205        return;
3206
3207found:
3208        mutex_lock(&client_mutex);
3209        list_del(&platform->list);
3210        mutex_unlock(&client_mutex);
3211
3212        pr_debug("Unregistered platform '%s'\n", platform->name);
3213        kfree(platform->name);
3214        kfree(platform);
3215}
3216EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3217
3218static u64 codec_format_map[] = {
3219        SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3220        SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3221        SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3222        SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3223        SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3224        SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3225        SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3226        SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3227        SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3228        SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3229        SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3230        SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3231        SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3232        SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3233        SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3234        | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3235};
3236
3237/* Fix up the DAI formats for endianness: codecs don't actually see
3238 * the endianness of the data but we're using the CPU format
3239 * definitions which do need to include endianness so we ensure that
3240 * codec DAIs always have both big and little endian variants set.
3241 */
3242static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3243{
3244        int i;
3245
3246        for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3247                if (stream->formats & codec_format_map[i])
3248                        stream->formats |= codec_format_map[i];
3249}
3250
3251/**
3252 * snd_soc_register_codec - Register a codec with the ASoC core
3253 *
3254 * @codec: codec to register
3255 */
3256int snd_soc_register_codec(struct device *dev,
3257                           const struct snd_soc_codec_driver *codec_drv,
3258                           struct snd_soc_dai_driver *dai_drv,
3259                           int num_dai)
3260{
3261        size_t reg_size;
3262        struct snd_soc_codec *codec;
3263        int ret, i;
3264
3265        dev_dbg(dev, "codec register %s\n", dev_name(dev));
3266
3267        codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3268        if (codec == NULL)
3269                return -ENOMEM;
3270
3271        /* create CODEC component name */
3272        codec->name = fmt_single_name(dev, &codec->id);
3273        if (codec->name == NULL) {
3274                kfree(codec);
3275                return -ENOMEM;
3276        }
3277
3278        if (codec_drv->compress_type)
3279                codec->compress_type = codec_drv->compress_type;
3280        else
3281                codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3282
3283        codec->write = codec_drv->write;
3284        codec->read = codec_drv->read;
3285        codec->volatile_register = codec_drv->volatile_register;
3286        codec->readable_register = codec_drv->readable_register;
3287        codec->writable_register = codec_drv->writable_register;
3288        codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3289        codec->dapm.dev = dev;
3290        codec->dapm.codec = codec;
3291        codec->dapm.seq_notifier = codec_drv->seq_notifier;
3292        codec->dapm.stream_event = codec_drv->stream_event;
3293        codec->dev = dev;
3294        codec->driver = codec_drv;
3295        codec->num_dai = num_dai;
3296        mutex_init(&codec->mutex);
3297
3298        /* allocate CODEC register cache */
3299        if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3300                reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
3301                codec->reg_size = reg_size;
3302                /* it is necessary to make a copy of the default register cache
3303                 * because in the case of using a compression type that requires
3304                 * the default register cache to be marked as __devinitconst the
3305                 * kernel might have freed the array by the time we initialize
3306                 * the cache.
3307                 */
3308                if (codec_drv->reg_cache_default) {
3309                        codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3310                                                      reg_size, GFP_KERNEL);
3311                        if (!codec->reg_def_copy) {
3312                                ret = -ENOMEM;
3313                                goto fail;
3314                        }
3315                }
3316        }
3317
3318        if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3319                if (!codec->volatile_register)
3320                        codec->volatile_register = snd_soc_default_volatile_register;
3321                if (!codec->readable_register)
3322                        codec->readable_register = snd_soc_default_readable_register;
3323                if (!codec->writable_register)
3324                        codec->writable_register = snd_soc_default_writable_register;
3325        }
3326
3327        for (i = 0; i < num_dai; i++) {
3328                fixup_codec_formats(&dai_drv[i].playback);
3329                fixup_codec_formats(&dai_drv[i].capture);
3330        }
3331
3332        /* register any DAIs */
3333        if (num_dai) {
3334                ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3335                if (ret < 0)
3336                        goto fail;
3337        }
3338
3339        mutex_lock(&client_mutex);
3340        list_add(&codec->list, &codec_list);
3341        snd_soc_instantiate_cards();
3342        mutex_unlock(&client_mutex);
3343
3344        pr_debug("Registered codec '%s'\n", codec->name);
3345        return 0;
3346
3347fail:
3348        kfree(codec->reg_def_copy);
3349        codec->reg_def_copy = NULL;
3350        kfree(codec->name);
3351        kfree(codec);
3352        return ret;
3353}
3354EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3355
3356/**
3357 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3358 *
3359 * @codec: codec to unregister
3360 */
3361void snd_soc_unregister_codec(struct device *dev)
3362{
3363        struct snd_soc_codec *codec;
3364        int i;
3365
3366        list_for_each_entry(codec, &codec_list, list) {
3367                if (dev == codec->dev)
3368                        goto found;
3369        }
3370        return;
3371
3372found:
3373        if (codec->num_dai)
3374                for (i = 0; i < codec->num_dai; i++)
3375                        snd_soc_unregister_dai(dev);
3376
3377        mutex_lock(&client_mutex);
3378        list_del(&codec->list);
3379        mutex_unlock(&client_mutex);
3380
3381        pr_debug("Unregistered codec '%s'\n", codec->name);
3382
3383        snd_soc_cache_exit(codec);
3384        kfree(codec->reg_def_copy);
3385        kfree(codec->name);
3386        kfree(codec);
3387}
3388EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3389
3390/* Retrieve a card's name from device tree */
3391int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3392                               const char *propname)
3393{
3394        struct device_node *np = card->dev->of_node;
3395        int ret;
3396
3397        ret = of_property_read_string_index(np, propname, 0, &card->name);
3398        /*
3399         * EINVAL means the property does not exist. This is fine providing
3400         * card->name was previously set, which is checked later in
3401         * snd_soc_register_card.
3402         */
3403        if (ret < 0 && ret != -EINVAL) {
3404                dev_err(card->dev,
3405                        "Property '%s' could not be read: %d\n",
3406                        propname, ret);
3407                return ret;
3408        }
3409
3410        return 0;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3413
3414int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3415                                   const char *propname)
3416{
3417        struct device_node *np = card->dev->of_node;
3418        int num_routes;
3419        struct snd_soc_dapm_route *routes;
3420        int i, ret;
3421
3422        num_routes = of_property_count_strings(np, propname);
3423        if (num_routes & 1) {
3424                dev_err(card->dev,
3425                        "Property '%s's length is not even\n",
3426                        propname);
3427                return -EINVAL;
3428        }
3429        num_routes /= 2;
3430        if (!num_routes) {
3431                dev_err(card->dev,
3432                        "Property '%s's length is zero\n",
3433                        propname);
3434                return -EINVAL;
3435        }
3436
3437        routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3438                              GFP_KERNEL);
3439        if (!routes) {
3440                dev_err(card->dev,
3441                        "Could not allocate DAPM route table\n");
3442                return -EINVAL;
3443        }
3444
3445        for (i = 0; i < num_routes; i++) {
3446                ret = of_property_read_string_index(np, propname,
3447                        2 * i, &routes[i].sink);
3448                if (ret) {
3449                        dev_err(card->dev,
3450                                "Property '%s' index %d could not be read: %d\n",
3451                                propname, 2 * i, ret);
3452                        return -EINVAL;
3453                }
3454                ret = of_property_read_string_index(np, propname,
3455                        (2 * i) + 1, &routes[i].source);
3456                if (ret) {
3457                        dev_err(card->dev,
3458                                "Property '%s' index %d could not be read: %d\n",
3459                                propname, (2 * i) + 1, ret);
3460                        return -EINVAL;
3461                }
3462        }
3463
3464        card->num_dapm_routes = num_routes;
3465        card->dapm_routes = routes;
3466
3467        return 0;
3468}
3469EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3470
3471static int __init snd_soc_init(void)
3472{
3473#ifdef CONFIG_DEBUG_FS
3474        snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3475        if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
3476                printk(KERN_WARNING
3477                       "ASoC: Failed to create debugfs directory\n");
3478                snd_soc_debugfs_root = NULL;
3479        }
3480
3481        if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
3482                                 &codec_list_fops))
3483                pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3484
3485        if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
3486                                 &dai_list_fops))
3487                pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3488
3489        if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
3490                                 &platform_list_fops))
3491                pr_warn("ASoC: Failed to create platform list debugfs file\n");
3492#endif
3493
3494        snd_soc_util_init();
3495
3496        return platform_driver_register(&soc_driver);
3497}
3498module_init(snd_soc_init);
3499
3500static void __exit snd_soc_exit(void)
3501{
3502        snd_soc_util_exit();
3503
3504#ifdef CONFIG_DEBUG_FS
3505        debugfs_remove_recursive(snd_soc_debugfs_root);
3506#endif
3507        platform_driver_unregister(&soc_driver);
3508}
3509module_exit(snd_soc_exit);
3510
3511/* Module information */
3512MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3513MODULE_DESCRIPTION("ALSA SoC Core");
3514MODULE_LICENSE("GPL");
3515MODULE_ALIAS("platform:soc-audio");
3516