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