Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.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         }
1111
1112         /* If the driver didn't set I/O up try regmap */
1113         if (!codec->write && dev_get_regmap(codec->dev, NULL))
1114                 snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
1115
1116         if (driver->controls)
1117                 snd_soc_add_codec_controls(codec, driver->controls,
1118                                      driver->num_controls);
1119         if (driver->dapm_routes)
1120                 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1121                                         driver->num_dapm_routes);
1122
1123         /* mark codec as probed and add to card codec list */
1124         codec->probed = 1;
1125         list_add(&codec->card_list, &card->codec_dev_list);
1126         list_add(&codec->dapm.list, &card->dapm_list);
1127
1128         return 0;
1129
1130 err_probe:
1131         soc_cleanup_codec_debugfs(codec);
1132         module_put(codec->dev->driver->owner);
1133
1134         return ret;
1135 }
1136
1137 static int soc_probe_platform(struct snd_soc_card *card,
1138                            struct snd_soc_platform *platform)
1139 {
1140         int ret = 0;
1141         const struct snd_soc_platform_driver *driver = platform->driver;
1142         struct snd_soc_dai *dai;
1143
1144         platform->card = card;
1145         platform->dapm.card = card;
1146
1147         if (!try_module_get(platform->dev->driver->owner))
1148                 return -ENODEV;
1149
1150         soc_init_platform_debugfs(platform);
1151
1152         if (driver->dapm_widgets)
1153                 snd_soc_dapm_new_controls(&platform->dapm,
1154                         driver->dapm_widgets, driver->num_dapm_widgets);
1155
1156         /* Create DAPM widgets for each DAI stream */
1157         list_for_each_entry(dai, &dai_list, list) {
1158                 if (dai->dev != platform->dev)
1159                         continue;
1160
1161                 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
1162         }
1163
1164         platform->dapm.idle_bias_off = 1;
1165
1166         if (driver->probe) {
1167                 ret = driver->probe(platform);
1168                 if (ret < 0) {
1169                         dev_err(platform->dev,
1170                                 "ASoC: failed to probe platform %d\n", ret);
1171                         goto err_probe;
1172                 }
1173         }
1174
1175         if (driver->controls)
1176                 snd_soc_add_platform_controls(platform, driver->controls,
1177                                      driver->num_controls);
1178         if (driver->dapm_routes)
1179                 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1180                                         driver->num_dapm_routes);
1181
1182         /* mark platform as probed and add to card platform list */
1183         platform->probed = 1;
1184         list_add(&platform->card_list, &card->platform_dev_list);
1185         list_add(&platform->dapm.list, &card->dapm_list);
1186
1187         return 0;
1188
1189 err_probe:
1190         soc_cleanup_platform_debugfs(platform);
1191         module_put(platform->dev->driver->owner);
1192
1193         return ret;
1194 }
1195
1196 static void rtd_release(struct device *dev)
1197 {
1198         kfree(dev);
1199 }
1200
1201 static int soc_post_component_init(struct snd_soc_card *card,
1202                                    struct snd_soc_codec *codec,
1203                                    int num, int dailess)
1204 {
1205         struct snd_soc_dai_link *dai_link = NULL;
1206         struct snd_soc_aux_dev *aux_dev = NULL;
1207         struct snd_soc_pcm_runtime *rtd;
1208         const char *temp, *name;
1209         int ret = 0;
1210
1211         if (!dailess) {
1212                 dai_link = &card->dai_link[num];
1213                 rtd = &card->rtd[num];
1214                 name = dai_link->name;
1215         } else {
1216                 aux_dev = &card->aux_dev[num];
1217                 rtd = &card->rtd_aux[num];
1218                 name = aux_dev->name;
1219         }
1220         rtd->card = card;
1221
1222         /* Make sure all DAPM widgets are instantiated */
1223         snd_soc_dapm_new_widgets(&codec->dapm);
1224
1225         /* machine controls, routes and widgets are not prefixed */
1226         temp = codec->name_prefix;
1227         codec->name_prefix = NULL;
1228
1229         /* do machine specific initialization */
1230         if (!dailess && dai_link->init)
1231                 ret = dai_link->init(rtd);
1232         else if (dailess && aux_dev->init)
1233                 ret = aux_dev->init(&codec->dapm);
1234         if (ret < 0) {
1235                 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
1236                 return ret;
1237         }
1238         codec->name_prefix = temp;
1239
1240         /* register the rtd device */
1241         rtd->codec = codec;
1242
1243         rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1244         if (!rtd->dev)
1245                 return -ENOMEM;
1246         device_initialize(rtd->dev);
1247         rtd->dev->parent = card->dev;
1248         rtd->dev->release = rtd_release;
1249         rtd->dev->init_name = name;
1250         dev_set_drvdata(rtd->dev, rtd);
1251         mutex_init(&rtd->pcm_mutex);
1252         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1253         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1254         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1255         INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
1256         ret = device_add(rtd->dev);
1257         if (ret < 0) {
1258                 /* calling put_device() here to free the rtd->dev */
1259                 put_device(rtd->dev);
1260                 dev_err(card->dev,
1261                         "ASoC: failed to register runtime device: %d\n", ret);
1262                 return ret;
1263         }
1264         rtd->dev_registered = 1;
1265
1266         /* add DAPM sysfs entries for this codec */
1267         ret = snd_soc_dapm_sys_add(rtd->dev);
1268         if (ret < 0)
1269                 dev_err(codec->dev,
1270                         "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
1271
1272         /* add codec sysfs entries */
1273         ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1274         if (ret < 0)
1275                 dev_err(codec->dev,
1276                         "ASoC: failed to add codec sysfs files: %d\n", ret);
1277
1278 #ifdef CONFIG_DEBUG_FS
1279         /* add DPCM sysfs entries */
1280         if (!dailess && !dai_link->dynamic)
1281                 goto out;
1282
1283         ret = soc_dpcm_debugfs_add(rtd);
1284         if (ret < 0)
1285                 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
1286
1287 out:
1288 #endif
1289         return 0;
1290 }
1291
1292 static int soc_probe_link_components(struct snd_soc_card *card, int num,
1293                                      int order)
1294 {
1295         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1296         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1297         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1298         struct snd_soc_platform *platform = rtd->platform;
1299         int ret;
1300
1301         /* probe the CPU-side component, if it is a CODEC */
1302         if (cpu_dai->codec &&
1303             !cpu_dai->codec->probed &&
1304             cpu_dai->codec->driver->probe_order == order) {
1305                 ret = soc_probe_codec(card, cpu_dai->codec);
1306                 if (ret < 0)
1307                         return ret;
1308         }
1309
1310         /* probe the CODEC-side component */
1311         if (!codec_dai->codec->probed &&
1312             codec_dai->codec->driver->probe_order == order) {
1313                 ret = soc_probe_codec(card, codec_dai->codec);
1314                 if (ret < 0)
1315                         return ret;
1316         }
1317
1318         /* probe the platform */
1319         if (!platform->probed &&
1320             platform->driver->probe_order == order) {
1321                 ret = soc_probe_platform(card, platform);
1322                 if (ret < 0)
1323                         return ret;
1324         }
1325
1326         return 0;
1327 }
1328
1329 static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
1330 {
1331         struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1332         struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1333         struct snd_soc_codec *codec = rtd->codec;
1334         struct snd_soc_platform *platform = rtd->platform;
1335         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1336         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1337         struct snd_soc_dapm_widget *play_w, *capture_w;
1338         int ret;
1339
1340         dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
1341                         card->name, num, order);
1342
1343         /* config components */
1344         cpu_dai->platform = platform;
1345         codec_dai->card = card;
1346         cpu_dai->card = card;
1347
1348         /* set default power off timeout */
1349         rtd->pmdown_time = pmdown_time;
1350
1351         /* probe the cpu_dai */
1352         if (!cpu_dai->probed &&
1353                         cpu_dai->driver->probe_order == order) {
1354                 if (!cpu_dai->codec) {
1355                         cpu_dai->dapm.card = card;
1356                         if (!try_module_get(cpu_dai->dev->driver->owner))
1357                                 return -ENODEV;
1358
1359                         list_add(&cpu_dai->dapm.list, &card->dapm_list);
1360                         snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
1361                 }
1362
1363                 if (cpu_dai->driver->probe) {
1364                         ret = cpu_dai->driver->probe(cpu_dai);
1365                         if (ret < 0) {
1366                                 dev_err(cpu_dai->dev,
1367                                         "ASoC: failed to probe CPU DAI %s: %d\n",
1368                                         cpu_dai->name, ret);
1369                                 module_put(cpu_dai->dev->driver->owner);
1370                                 return ret;
1371                         }
1372                 }
1373                 cpu_dai->probed = 1;
1374                 /* mark cpu_dai as probed and add to card dai list */
1375                 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1376         }
1377
1378         /* probe the CODEC DAI */
1379         if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1380                 if (codec_dai->driver->probe) {
1381                         ret = codec_dai->driver->probe(codec_dai);
1382                         if (ret < 0) {
1383                                 dev_err(codec_dai->dev,
1384                                         "ASoC: failed to probe CODEC DAI %s: %d\n",
1385                                         codec_dai->name, ret);
1386                                 return ret;
1387                         }
1388                 }
1389
1390                 /* mark codec_dai as probed and add to card dai list */
1391                 codec_dai->probed = 1;
1392                 list_add(&codec_dai->card_list, &card->dai_dev_list);
1393         }
1394
1395         /* complete DAI probe during last probe */
1396         if (order != SND_SOC_COMP_ORDER_LAST)
1397                 return 0;
1398
1399         ret = soc_post_component_init(card, codec, num, 0);
1400         if (ret)
1401                 return ret;
1402
1403         ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
1404         if (ret < 0)
1405                 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1406                         ret);
1407
1408         if (cpu_dai->driver->compress_dai) {
1409                 /*create compress_device"*/
1410                 ret = soc_new_compress(rtd, num);
1411                 if (ret < 0) {
1412                         dev_err(card->dev, "ASoC: can't create compress %s\n",
1413                                          dai_link->stream_name);
1414                         return ret;
1415                 }
1416         } else {
1417
1418                 if (!dai_link->params) {
1419                         /* create the pcm */
1420                         ret = soc_new_pcm(rtd, num);
1421                         if (ret < 0) {
1422                                 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
1423                                        dai_link->stream_name, ret);
1424                                 return ret;
1425                         }
1426                 } else {
1427                         /* link the DAI widgets */
1428                         play_w = codec_dai->playback_widget;
1429                         capture_w = cpu_dai->capture_widget;
1430                         if (play_w && capture_w) {
1431                                 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1432                                                    capture_w, play_w);
1433                                 if (ret != 0) {
1434                                         dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1435                                                 play_w->name, capture_w->name, ret);
1436                                         return ret;
1437                                 }
1438                         }
1439
1440                         play_w = cpu_dai->playback_widget;
1441                         capture_w = codec_dai->capture_widget;
1442                         if (play_w && capture_w) {
1443                                 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1444                                                    capture_w, play_w);
1445                                 if (ret != 0) {
1446                                         dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1447                                                 play_w->name, capture_w->name, ret);
1448                                         return ret;
1449                                 }
1450                         }
1451                 }
1452         }
1453
1454         /* add platform data for AC97 devices */
1455         if (rtd->codec_dai->driver->ac97_control)
1456                 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1457
1458         return 0;
1459 }
1460
1461 #ifdef CONFIG_SND_SOC_AC97_BUS
1462 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1463 {
1464         int ret;
1465
1466         /* Only instantiate AC97 if not already done by the adaptor
1467          * for the generic AC97 subsystem.
1468          */
1469         if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1470                 /*
1471                  * It is possible that the AC97 device is already registered to
1472                  * the device subsystem. This happens when the device is created
1473                  * via snd_ac97_mixer(). Currently only SoC codec that does so
1474                  * is the generic AC97 glue but others migh emerge.
1475                  *
1476                  * In those cases we don't try to register the device again.
1477                  */
1478                 if (!rtd->codec->ac97_created)
1479                         return 0;
1480
1481                 ret = soc_ac97_dev_register(rtd->codec);
1482                 if (ret < 0) {
1483                         dev_err(rtd->codec->dev,
1484                                 "ASoC: AC97 device register failed: %d\n", ret);
1485                         return ret;
1486                 }
1487
1488                 rtd->codec->ac97_registered = 1;
1489         }
1490         return 0;
1491 }
1492
1493 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1494 {
1495         if (codec->ac97_registered) {
1496                 soc_ac97_dev_unregister(codec);
1497                 codec->ac97_registered = 0;
1498         }
1499 }
1500 #endif
1501
1502 static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1503 {
1504         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1505         struct snd_soc_codec *codec;
1506
1507         /* find CODEC from registered CODECs*/
1508         list_for_each_entry(codec, &codec_list, list) {
1509                 if (!strcmp(codec->name, aux_dev->codec_name))
1510                         return 0;
1511         }
1512
1513         dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
1514
1515         return -EPROBE_DEFER;
1516 }
1517
1518 static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1519 {
1520         struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1521         struct snd_soc_codec *codec;
1522         int ret = -ENODEV;
1523
1524         /* find CODEC from registered CODECs*/
1525         list_for_each_entry(codec, &codec_list, list) {
1526                 if (!strcmp(codec->name, aux_dev->codec_name)) {
1527                         if (codec->probed) {
1528                                 dev_err(codec->dev,
1529                                         "ASoC: codec already probed");
1530                                 ret = -EBUSY;
1531                                 goto out;
1532                         }
1533                         goto found;
1534                 }
1535         }
1536         /* codec not found */
1537         dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
1538         return -EPROBE_DEFER;
1539
1540 found:
1541         ret = soc_probe_codec(card, codec);
1542         if (ret < 0)
1543                 return ret;
1544
1545         ret = soc_post_component_init(card, codec, num, 1);
1546
1547 out:
1548         return ret;
1549 }
1550
1551 static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1552 {
1553         struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1554         struct snd_soc_codec *codec = rtd->codec;
1555
1556         /* unregister the rtd device */
1557         if (rtd->dev_registered) {
1558                 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1559                 device_unregister(rtd->dev);
1560                 rtd->dev_registered = 0;
1561         }
1562
1563         if (codec && codec->probed)
1564                 soc_remove_codec(codec);
1565 }
1566
1567 static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1568                                     enum snd_soc_compress_type compress_type)
1569 {
1570         int ret;
1571
1572         if (codec->cache_init)
1573                 return 0;
1574
1575         /* override the compress_type if necessary */
1576         if (compress_type && codec->compress_type != compress_type)
1577                 codec->compress_type = compress_type;
1578         ret = snd_soc_cache_init(codec);
1579         if (ret < 0) {
1580                 dev_err(codec->dev, "ASoC: Failed to set cache compression"
1581                         " type: %d\n", ret);
1582                 return ret;
1583         }
1584         codec->cache_init = 1;
1585         return 0;
1586 }
1587
1588 static int snd_soc_instantiate_card(struct snd_soc_card *card)
1589 {
1590         struct snd_soc_codec *codec;
1591         struct snd_soc_codec_conf *codec_conf;
1592         enum snd_soc_compress_type compress_type;
1593         struct snd_soc_dai_link *dai_link;
1594         int ret, i, order, dai_fmt;
1595
1596         mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
1597
1598         /* bind DAIs */
1599         for (i = 0; i < card->num_links; i++) {
1600                 ret = soc_bind_dai_link(card, i);
1601                 if (ret != 0)
1602                         goto base_error;
1603         }
1604
1605         /* check aux_devs too */
1606         for (i = 0; i < card->num_aux_devs; i++) {
1607                 ret = soc_check_aux_dev(card, i);
1608                 if (ret != 0)
1609                         goto base_error;
1610         }
1611
1612         /* initialize the register cache for each available codec */
1613         list_for_each_entry(codec, &codec_list, list) {
1614                 if (codec->cache_init)
1615                         continue;
1616                 /* by default we don't override the compress_type */
1617                 compress_type = 0;
1618                 /* check to see if we need to override the compress_type */
1619                 for (i = 0; i < card->num_configs; ++i) {
1620                         codec_conf = &card->codec_conf[i];
1621                         if (!strcmp(codec->name, codec_conf->dev_name)) {
1622                                 compress_type = codec_conf->compress_type;
1623                                 if (compress_type && compress_type
1624                                     != codec->compress_type)
1625                                         break;
1626                         }
1627                 }
1628                 ret = snd_soc_init_codec_cache(codec, compress_type);
1629                 if (ret < 0)
1630                         goto base_error;
1631         }
1632
1633         /* card bind complete so register a sound card */
1634         ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1635                         card->owner, 0, &card->snd_card);
1636         if (ret < 0) {
1637                 dev_err(card->dev, "ASoC: can't create sound card for"
1638                         " card %s: %d\n", card->name, ret);
1639                 goto base_error;
1640         }
1641         card->snd_card->dev = card->dev;
1642
1643         card->dapm.bias_level = SND_SOC_BIAS_OFF;
1644         card->dapm.dev = card->dev;
1645         card->dapm.card = card;
1646         list_add(&card->dapm.list, &card->dapm_list);
1647
1648 #ifdef CONFIG_DEBUG_FS
1649         snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1650 #endif
1651
1652 #ifdef CONFIG_PM_SLEEP
1653         /* deferred resume work */
1654         INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1655 #endif
1656
1657         if (card->dapm_widgets)
1658                 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1659                                           card->num_dapm_widgets);
1660
1661         /* initialise the sound card only once */
1662         if (card->probe) {
1663                 ret = card->probe(card);
1664                 if (ret < 0)
1665                         goto card_probe_error;
1666         }
1667
1668         /* probe all components used by DAI links on this card */
1669         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1670                         order++) {
1671                 for (i = 0; i < card->num_links; i++) {
1672                         ret = soc_probe_link_components(card, i, order);
1673                         if (ret < 0) {
1674                                 dev_err(card->dev,
1675                                         "ASoC: failed to instantiate card %d\n",
1676                                         ret);
1677                                 goto probe_dai_err;
1678                         }
1679                 }
1680         }
1681
1682         /* probe all DAI links on this card */
1683         for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1684                         order++) {
1685                 for (i = 0; i < card->num_links; i++) {
1686                         ret = soc_probe_link_dais(card, i, order);
1687                         if (ret < 0) {
1688                                 dev_err(card->dev,
1689                                         "ASoC: failed to instantiate card %d\n",
1690                                         ret);
1691                                 goto probe_dai_err;
1692                         }
1693                 }
1694         }
1695
1696         for (i = 0; i < card->num_aux_devs; i++) {
1697                 ret = soc_probe_aux_dev(card, i);
1698                 if (ret < 0) {
1699                         dev_err(card->dev,
1700                                 "ASoC: failed to add auxiliary devices %d\n",
1701                                 ret);
1702                         goto probe_aux_dev_err;
1703                 }
1704         }
1705
1706         snd_soc_dapm_link_dai_widgets(card);
1707
1708         if (card->controls)
1709                 snd_soc_add_card_controls(card, card->controls, card->num_controls);
1710
1711         if (card->dapm_routes)
1712                 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1713                                         card->num_dapm_routes);
1714
1715         snd_soc_dapm_new_widgets(&card->dapm);
1716
1717         for (i = 0; i < card->num_links; i++) {
1718                 dai_link = &card->dai_link[i];
1719                 dai_fmt = dai_link->dai_fmt;
1720
1721                 if (dai_fmt) {
1722                         ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1723                                                   dai_fmt);
1724                         if (ret != 0 && ret != -ENOTSUPP)
1725                                 dev_warn(card->rtd[i].codec_dai->dev,
1726                                          "ASoC: Failed to set DAI format: %d\n",
1727                                          ret);
1728                 }
1729
1730                 /* If this is a regular CPU link there will be a platform */
1731                 if (dai_fmt &&
1732                     (dai_link->platform_name || dai_link->platform_of_node)) {
1733                         ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1734                                                   dai_fmt);
1735                         if (ret != 0 && ret != -ENOTSUPP)
1736                                 dev_warn(card->rtd[i].cpu_dai->dev,
1737                                          "ASoC: Failed to set DAI format: %d\n",
1738                                          ret);
1739                 } else if (dai_fmt) {
1740                         /* Flip the polarity for the "CPU" end */
1741                         dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1742                         switch (dai_link->dai_fmt &
1743                                 SND_SOC_DAIFMT_MASTER_MASK) {
1744                         case SND_SOC_DAIFMT_CBM_CFM:
1745                                 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1746                                 break;
1747                         case SND_SOC_DAIFMT_CBM_CFS:
1748                                 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1749                                 break;
1750                         case SND_SOC_DAIFMT_CBS_CFM:
1751                                 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1752                                 break;
1753                         case SND_SOC_DAIFMT_CBS_CFS:
1754                                 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1755                                 break;
1756                         }
1757
1758                         ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1759                                                   dai_fmt);
1760                         if (ret != 0 && ret != -ENOTSUPP)
1761                                 dev_warn(card->rtd[i].cpu_dai->dev,
1762                                          "ASoC: Failed to set DAI format: %d\n",
1763                                          ret);
1764                 }
1765         }
1766
1767         snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1768                  "%s", card->name);
1769         snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1770                  "%s", card->long_name ? card->long_name : card->name);
1771         snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1772                  "%s", card->driver_name ? card->driver_name : card->name);
1773         for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1774                 switch (card->snd_card->driver[i]) {
1775                 case '_':
1776                 case '-':
1777                 case '\0':
1778                         break;
1779                 default:
1780                         if (!isalnum(card->snd_card->driver[i]))
1781                                 card->snd_card->driver[i] = '_';
1782                         break;
1783                 }
1784         }
1785
1786         if (card->late_probe) {
1787                 ret = card->late_probe(card);
1788                 if (ret < 0) {
1789                         dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
1790                                 card->name, ret);
1791                         goto probe_aux_dev_err;
1792                 }
1793         }
1794
1795         snd_soc_dapm_new_widgets(&card->dapm);
1796
1797         if (card->fully_routed)
1798                 list_for_each_entry(codec, &card->codec_dev_list, card_list)
1799                         snd_soc_dapm_auto_nc_codec_pins(codec);
1800
1801         ret = snd_card_register(card->snd_card);
1802         if (ret < 0) {
1803                 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1804                                 ret);
1805                 goto probe_aux_dev_err;
1806         }
1807
1808 #ifdef CONFIG_SND_SOC_AC97_BUS
1809         /* register any AC97 codecs */
1810         for (i = 0; i < card->num_rtd; i++) {
1811                 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1812                 if (ret < 0) {
1813                         dev_err(card->dev, "ASoC: failed to register AC97:"
1814                                 " %d\n", ret);
1815                         while (--i >= 0)
1816                                 soc_unregister_ac97_dai_link(card->rtd[i].codec);
1817                         goto probe_aux_dev_err;
1818                 }
1819         }
1820 #endif
1821
1822         card->instantiated = 1;
1823         snd_soc_dapm_sync(&card->dapm);
1824         mutex_unlock(&card->mutex);
1825
1826         return 0;
1827
1828 probe_aux_dev_err:
1829         for (i = 0; i < card->num_aux_devs; i++)
1830                 soc_remove_aux_dev(card, i);
1831
1832 probe_dai_err:
1833         soc_remove_dai_links(card);
1834
1835 card_probe_error:
1836         if (card->remove)
1837                 card->remove(card);
1838
1839         snd_card_free(card->snd_card);
1840
1841 base_error:
1842         mutex_unlock(&card->mutex);
1843
1844         return ret;
1845 }
1846
1847 /* probes a new socdev */
1848 static int soc_probe(struct platform_device *pdev)
1849 {
1850         struct snd_soc_card *card = platform_get_drvdata(pdev);
1851
1852         /*
1853          * no card, so machine driver should be registering card
1854          * we should not be here in that case so ret error
1855          */
1856         if (!card)
1857                 return -EINVAL;
1858
1859         dev_warn(&pdev->dev,
1860                  "ASoC: machine %s should use snd_soc_register_card()\n",
1861                  card->name);
1862
1863         /* Bodge while we unpick instantiation */
1864         card->dev = &pdev->dev;
1865
1866         return snd_soc_register_card(card);
1867 }
1868
1869 static int soc_cleanup_card_resources(struct snd_soc_card *card)
1870 {
1871         int i;
1872
1873         /* make sure any delayed work runs */
1874         for (i = 0; i < card->num_rtd; i++) {
1875                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1876                 flush_delayed_work(&rtd->delayed_work);
1877         }
1878
1879         /* remove auxiliary devices */
1880         for (i = 0; i < card->num_aux_devs; i++)
1881                 soc_remove_aux_dev(card, i);
1882
1883         /* remove and free each DAI */
1884         soc_remove_dai_links(card);
1885
1886         soc_cleanup_card_debugfs(card);
1887
1888         /* remove the card */
1889         if (card->remove)
1890                 card->remove(card);
1891
1892         snd_soc_dapm_free(&card->dapm);
1893
1894         snd_card_free(card->snd_card);
1895         return 0;
1896
1897 }
1898
1899 /* removes a socdev */
1900 static int soc_remove(struct platform_device *pdev)
1901 {
1902         struct snd_soc_card *card = platform_get_drvdata(pdev);
1903
1904         snd_soc_unregister_card(card);
1905         return 0;
1906 }
1907
1908 int snd_soc_poweroff(struct device *dev)
1909 {
1910         struct snd_soc_card *card = dev_get_drvdata(dev);
1911         int i;
1912
1913         if (!card->instantiated)
1914                 return 0;
1915
1916         /* Flush out pmdown_time work - we actually do want to run it
1917          * now, we're shutting down so no imminent restart. */
1918         for (i = 0; i < card->num_rtd; i++) {
1919                 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1920                 flush_delayed_work(&rtd->delayed_work);
1921         }
1922
1923         snd_soc_dapm_shutdown(card);
1924
1925         return 0;
1926 }
1927 EXPORT_SYMBOL_GPL(snd_soc_poweroff);
1928
1929 const struct dev_pm_ops snd_soc_pm_ops = {
1930         .suspend = snd_soc_suspend,
1931         .resume = snd_soc_resume,
1932         .freeze = snd_soc_suspend,
1933         .thaw = snd_soc_resume,
1934         .poweroff = snd_soc_poweroff,
1935         .restore = snd_soc_resume,
1936 };
1937 EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
1938
1939 /* ASoC platform driver */
1940 static struct platform_driver soc_driver = {
1941         .driver         = {
1942                 .name           = "soc-audio",
1943                 .owner          = THIS_MODULE,
1944                 .pm             = &snd_soc_pm_ops,
1945         },
1946         .probe          = soc_probe,
1947         .remove         = soc_remove,
1948 };
1949
1950 /**
1951  * snd_soc_codec_volatile_register: Report if a register is volatile.
1952  *
1953  * @codec: CODEC to query.
1954  * @reg: Register to query.
1955  *
1956  * Boolean function indiciating if a CODEC register is volatile.
1957  */
1958 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1959                                     unsigned int reg)
1960 {
1961         if (codec->volatile_register)
1962                 return codec->volatile_register(codec, reg);
1963         else
1964                 return 0;
1965 }
1966 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1967
1968 /**
1969  * snd_soc_codec_readable_register: Report if a register is readable.
1970  *
1971  * @codec: CODEC to query.
1972  * @reg: Register to query.
1973  *
1974  * Boolean function indicating if a CODEC register is readable.
1975  */
1976 int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1977                                     unsigned int reg)
1978 {
1979         if (codec->readable_register)
1980                 return codec->readable_register(codec, reg);
1981         else
1982                 return 1;
1983 }
1984 EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1985
1986 /**
1987  * snd_soc_codec_writable_register: Report if a register is writable.
1988  *
1989  * @codec: CODEC to query.
1990  * @reg: Register to query.
1991  *
1992  * Boolean function indicating if a CODEC register is writable.
1993  */
1994 int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1995                                     unsigned int reg)
1996 {
1997         if (codec->writable_register)
1998                 return codec->writable_register(codec, reg);
1999         else
2000                 return 1;
2001 }
2002 EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2003
2004 int snd_soc_platform_read(struct snd_soc_platform *platform,
2005                                         unsigned int reg)
2006 {
2007         unsigned int ret;
2008
2009         if (!platform->driver->read) {
2010                 dev_err(platform->dev, "ASoC: platform has no read back\n");
2011                 return -1;
2012         }
2013
2014         ret = platform->driver->read(platform, reg);
2015         dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
2016         trace_snd_soc_preg_read(platform, reg, ret);
2017
2018         return ret;
2019 }
2020 EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2021
2022 int snd_soc_platform_write(struct snd_soc_platform *platform,
2023                                          unsigned int reg, unsigned int val)
2024 {
2025         if (!platform->driver->write) {
2026                 dev_err(platform->dev, "ASoC: platform has no write back\n");
2027                 return -1;
2028         }
2029
2030         dev_dbg(platform->dev, "write %x = %x\n", reg, val);
2031         trace_snd_soc_preg_write(platform, reg, val);
2032         return platform->driver->write(platform, reg, val);
2033 }
2034 EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2035
2036 /**
2037  * snd_soc_new_ac97_codec - initailise AC97 device
2038  * @codec: audio codec
2039  * @ops: AC97 bus operations
2040  * @num: AC97 codec number
2041  *
2042  * Initialises AC97 codec resources for use by ad-hoc devices only.
2043  */
2044 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2045         struct snd_ac97_bus_ops *ops, int num)
2046 {
2047         mutex_lock(&codec->mutex);
2048
2049         codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2050         if (codec->ac97 == NULL) {
2051                 mutex_unlock(&codec->mutex);
2052                 return -ENOMEM;
2053         }
2054
2055         codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2056         if (codec->ac97->bus == NULL) {
2057                 kfree(codec->ac97);
2058                 codec->ac97 = NULL;
2059                 mutex_unlock(&codec->mutex);
2060                 return -ENOMEM;
2061         }
2062
2063         codec->ac97->bus->ops = ops;
2064         codec->ac97->num = num;
2065
2066         /*
2067          * Mark the AC97 device to be created by us. This way we ensure that the
2068          * device will be registered with the device subsystem later on.
2069          */
2070         codec->ac97_created = 1;
2071
2072         mutex_unlock(&codec->mutex);
2073         return 0;
2074 }
2075 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2076
2077 /**
2078  * snd_soc_free_ac97_codec - free AC97 codec device
2079  * @codec: audio codec
2080  *
2081  * Frees AC97 codec device resources.
2082  */
2083 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2084 {
2085         mutex_lock(&codec->mutex);
2086 #ifdef CONFIG_SND_SOC_AC97_BUS
2087         soc_unregister_ac97_dai_link(codec);
2088 #endif
2089         kfree(codec->ac97->bus);
2090         kfree(codec->ac97);
2091         codec->ac97 = NULL;
2092         codec->ac97_created = 0;
2093         mutex_unlock(&codec->mutex);
2094 }
2095 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2096
2097 unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2098 {
2099         unsigned int ret;
2100
2101         ret = codec->read(codec, reg);
2102         dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
2103         trace_snd_soc_reg_read(codec, reg, ret);
2104
2105         return ret;
2106 }
2107 EXPORT_SYMBOL_GPL(snd_soc_read);
2108
2109 unsigned int snd_soc_write(struct snd_soc_codec *codec,
2110                            unsigned int reg, unsigned int val)
2111 {
2112         dev_dbg(codec->dev, "write %x = %x\n", reg, val);
2113         trace_snd_soc_reg_write(codec, reg, val);
2114         return codec->write(codec, reg, val);
2115 }
2116 EXPORT_SYMBOL_GPL(snd_soc_write);
2117
2118 unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
2119                                     unsigned int reg, const void *data, size_t len)
2120 {
2121         return codec->bulk_write_raw(codec, reg, data, len);
2122 }
2123 EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
2124
2125 /**
2126  * snd_soc_update_bits - update codec register bits
2127  * @codec: audio codec
2128  * @reg: codec register
2129  * @mask: register mask
2130  * @value: new value
2131  *
2132  * Writes new register value.
2133  *
2134  * Returns 1 for change, 0 for no change, or negative error code.
2135  */
2136 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
2137                                 unsigned int mask, unsigned int value)
2138 {
2139         bool change;
2140         unsigned int old, new;
2141         int ret;
2142
2143         if (codec->using_regmap) {
2144                 ret = regmap_update_bits_check(codec->control_data, reg,
2145                                                mask, value, &change);
2146         } else {
2147                 ret = snd_soc_read(codec, reg);
2148                 if (ret < 0)
2149                         return ret;
2150
2151                 old = ret;
2152                 new = (old & ~mask) | (value & mask);
2153                 change = old != new;
2154                 if (change)
2155                         ret = snd_soc_write(codec, reg, new);
2156         }
2157
2158         if (ret < 0)
2159                 return ret;
2160
2161         return change;
2162 }
2163 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2164
2165 /**
2166  * snd_soc_update_bits_locked - update codec register bits
2167  * @codec: audio codec
2168  * @reg: codec register
2169  * @mask: register mask
2170  * @value: new value
2171  *
2172  * Writes new register value, and takes the codec mutex.
2173  *
2174  * Returns 1 for change else 0.
2175  */
2176 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2177                                unsigned short reg, unsigned int mask,
2178                                unsigned int value)
2179 {
2180         int change;
2181
2182         mutex_lock(&codec->mutex);
2183         change = snd_soc_update_bits(codec, reg, mask, value);
2184         mutex_unlock(&codec->mutex);
2185
2186         return change;
2187 }
2188 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
2189
2190 /**
2191  * snd_soc_test_bits - test register for change
2192  * @codec: audio codec
2193  * @reg: codec register
2194  * @mask: register mask
2195  * @value: new value
2196  *
2197  * Tests a register with a new value and checks if the new value is
2198  * different from the old value.
2199  *
2200  * Returns 1 for change else 0.
2201  */
2202 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
2203                                 unsigned int mask, unsigned int value)
2204 {
2205         int change;
2206         unsigned int old, new;
2207
2208         old = snd_soc_read(codec, reg);
2209         new = (old & ~mask) | value;
2210         change = old != new;
2211
2212         return change;
2213 }
2214 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2215
2216 /**
2217  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2218  * @substream: the pcm substream
2219  * @hw: the hardware parameters
2220  *
2221  * Sets the substream runtime hardware parameters.
2222  */
2223 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2224         const struct snd_pcm_hardware *hw)
2225 {
2226         struct snd_pcm_runtime *runtime = substream->runtime;
2227         runtime->hw.info = hw->info;
2228         runtime->hw.formats = hw->formats;
2229         runtime->hw.period_bytes_min = hw->period_bytes_min;
2230         runtime->hw.period_bytes_max = hw->period_bytes_max;
2231         runtime->hw.periods_min = hw->periods_min;
2232         runtime->hw.periods_max = hw->periods_max;
2233         runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2234         runtime->hw.fifo_size = hw->fifo_size;
2235         return 0;
2236 }
2237 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2238
2239 /**
2240  * snd_soc_cnew - create new control
2241  * @_template: control template
2242  * @data: control private data
2243  * @long_name: control long name
2244  * @prefix: control name prefix
2245  *
2246  * Create a new mixer control from a template control.
2247  *
2248  * Returns 0 for success, else error.
2249  */
2250 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
2251                                   void *data, const char *long_name,
2252                                   const char *prefix)
2253 {
2254         struct snd_kcontrol_new template;
2255         struct snd_kcontrol *kcontrol;
2256         char *name = NULL;
2257         int name_len;
2258
2259         memcpy(&template, _template, sizeof(template));
2260         template.index = 0;
2261
2262         if (!long_name)
2263                 long_name = template.name;
2264
2265         if (prefix) {
2266                 name_len = strlen(long_name) + strlen(prefix) + 2;
2267                 name = kmalloc(name_len, GFP_KERNEL);
2268                 if (!name)
2269                         return NULL;
2270
2271                 snprintf(name, name_len, "%s %s", prefix, long_name);
2272
2273                 template.name = name;
2274         } else {
2275                 template.name = long_name;
2276         }
2277
2278         kcontrol = snd_ctl_new1(&template, data);
2279
2280         kfree(name);
2281
2282         return kcontrol;
2283 }
2284 EXPORT_SYMBOL_GPL(snd_soc_cnew);
2285
2286 static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2287         const struct snd_kcontrol_new *controls, int num_controls,
2288         const char *prefix, void *data)
2289 {
2290         int err, i;
2291
2292         for (i = 0; i < num_controls; i++) {
2293                 const struct snd_kcontrol_new *control = &controls[i];
2294                 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2295                                                      control->name, prefix));
2296                 if (err < 0) {
2297                         dev_err(dev, "ASoC: Failed to add %s: %d\n",
2298                                 control->name, err);
2299                         return err;
2300                 }
2301         }
2302
2303         return 0;
2304 }
2305
2306 /**
2307  * snd_soc_add_codec_controls - add an array of controls to a codec.
2308  * Convenience function to add a list of controls. Many codecs were
2309  * duplicating this code.
2310  *
2311  * @codec: codec to add controls to
2312  * @controls: array of controls to add
2313  * @num_controls: number of elements in the array
2314  *
2315  * Return 0 for success, else error.
2316  */
2317 int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
2318         const struct snd_kcontrol_new *controls, int num_controls)
2319 {
2320         struct snd_card *card = codec->card->snd_card;
2321
2322         return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2323                         codec->name_prefix, codec);
2324 }
2325 EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
2326
2327 /**
2328  * snd_soc_add_platform_controls - add an array of controls to a platform.
2329  * Convenience function to add a list of controls.
2330  *
2331  * @platform: platform to add controls to
2332  * @controls: array of controls to add
2333  * @num_controls: number of elements in the array
2334  *
2335  * Return 0 for success, else error.
2336  */
2337 int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2338         const struct snd_kcontrol_new *controls, int num_controls)
2339 {
2340         struct snd_card *card = platform->card->snd_card;
2341
2342         return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2343                         NULL, platform);
2344 }
2345 EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2346
2347 /**
2348  * snd_soc_add_card_controls - add an array of controls to a SoC card.
2349  * Convenience function to add a list of controls.
2350  *
2351  * @soc_card: SoC card to add controls to
2352  * @controls: array of controls to add
2353  * @num_controls: number of elements in the array
2354  *
2355  * Return 0 for success, else error.
2356  */
2357 int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2358         const struct snd_kcontrol_new *controls, int num_controls)
2359 {
2360         struct snd_card *card = soc_card->snd_card;
2361
2362         return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2363                         NULL, soc_card);
2364 }
2365 EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2366
2367 /**
2368  * snd_soc_add_dai_controls - add an array of controls to a DAI.
2369  * Convienience function to add a list of controls.
2370  *
2371  * @dai: DAI to add controls to
2372  * @controls: array of controls to add
2373  * @num_controls: number of elements in the array
2374  *
2375  * Return 0 for success, else error.
2376  */
2377 int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2378         const struct snd_kcontrol_new *controls, int num_controls)
2379 {
2380         struct snd_card *card = dai->card->snd_card;
2381
2382         return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2383                         NULL, dai);
2384 }
2385 EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2386
2387 /**
2388  * snd_soc_info_enum_double - enumerated double mixer info callback
2389  * @kcontrol: mixer control
2390  * @uinfo: control element information
2391  *
2392  * Callback to provide information about a double enumerated
2393  * mixer control.
2394  *
2395  * Returns 0 for success.
2396  */
2397 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2398         struct snd_ctl_elem_info *uinfo)
2399 {
2400         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2401
2402         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2403         uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
2404         uinfo->value.enumerated.items = e->max;
2405
2406         if (uinfo->value.enumerated.item > e->max - 1)
2407                 uinfo->value.enumerated.item = e->max - 1;
2408         strcpy(uinfo->value.enumerated.name,
2409                 e->texts[uinfo->value.enumerated.item]);
2410         return 0;
2411 }
2412 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2413
2414 /**
2415  * snd_soc_get_enum_double - enumerated double mixer get callback
2416  * @kcontrol: mixer control
2417  * @ucontrol: control element information
2418  *
2419  * Callback to get the value of a double enumerated mixer.
2420  *
2421  * Returns 0 for success.
2422  */
2423 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2424         struct snd_ctl_elem_value *ucontrol)
2425 {
2426         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2427         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2428         unsigned int val;
2429
2430         val = snd_soc_read(codec, e->reg);
2431         ucontrol->value.enumerated.item[0]
2432                 = (val >> e->shift_l) & e->mask;
2433         if (e->shift_l != e->shift_r)
2434                 ucontrol->value.enumerated.item[1] =
2435                         (val >> e->shift_r) & e->mask;
2436
2437         return 0;
2438 }
2439 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2440
2441 /**
2442  * snd_soc_put_enum_double - enumerated double mixer put callback
2443  * @kcontrol: mixer control
2444  * @ucontrol: control element information
2445  *
2446  * Callback to set the value of a double enumerated mixer.
2447  *
2448  * Returns 0 for success.
2449  */
2450 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2451         struct snd_ctl_elem_value *ucontrol)
2452 {
2453         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2454         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2455         unsigned int val;
2456         unsigned int mask;
2457
2458         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2459                 return -EINVAL;
2460         val = ucontrol->value.enumerated.item[0] << e->shift_l;
2461         mask = e->mask << e->shift_l;
2462         if (e->shift_l != e->shift_r) {
2463                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2464                         return -EINVAL;
2465                 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2466                 mask |= e->mask << e->shift_r;
2467         }
2468
2469         return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2470 }
2471 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2472
2473 /**
2474  * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2475  * @kcontrol: mixer control
2476  * @ucontrol: control element information
2477  *
2478  * Callback to get the value of a double semi enumerated mixer.
2479  *
2480  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2481  * used for handling bitfield coded enumeration for example.
2482  *
2483  * Returns 0 for success.
2484  */
2485 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2486         struct snd_ctl_elem_value *ucontrol)
2487 {
2488         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2489         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2490         unsigned int reg_val, val, mux;
2491
2492         reg_val = snd_soc_read(codec, e->reg);
2493         val = (reg_val >> e->shift_l) & e->mask;
2494         for (mux = 0; mux < e->max; mux++) {
2495                 if (val == e->values[mux])
2496                         break;
2497         }
2498         ucontrol->value.enumerated.item[0] = mux;
2499         if (e->shift_l != e->shift_r) {
2500                 val = (reg_val >> e->shift_r) & e->mask;
2501                 for (mux = 0; mux < e->max; mux++) {
2502                         if (val == e->values[mux])
2503                                 break;
2504                 }
2505                 ucontrol->value.enumerated.item[1] = mux;
2506         }
2507
2508         return 0;
2509 }
2510 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2511
2512 /**
2513  * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2514  * @kcontrol: mixer control
2515  * @ucontrol: control element information
2516  *
2517  * Callback to set the value of a double semi enumerated mixer.
2518  *
2519  * Semi enumerated mixer: the enumerated items are referred as values. Can be
2520  * used for handling bitfield coded enumeration for example.
2521  *
2522  * Returns 0 for success.
2523  */
2524 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2525         struct snd_ctl_elem_value *ucontrol)
2526 {
2527         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2528         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2529         unsigned int val;
2530         unsigned int mask;
2531
2532         if (ucontrol->value.enumerated.item[0] > e->max - 1)
2533                 return -EINVAL;
2534         val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2535         mask = e->mask << e->shift_l;
2536         if (e->shift_l != e->shift_r) {
2537                 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2538                         return -EINVAL;
2539                 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2540                 mask |= e->mask << e->shift_r;
2541         }
2542
2543         return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2544 }
2545 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2546
2547 /**
2548  * snd_soc_info_enum_ext - external enumerated single mixer info callback
2549  * @kcontrol: mixer control
2550  * @uinfo: control element information
2551  *
2552  * Callback to provide information about an external enumerated
2553  * single mixer.
2554  *
2555  * Returns 0 for success.
2556  */
2557 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2558         struct snd_ctl_elem_info *uinfo)
2559 {
2560         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2561
2562         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2563         uinfo->count = 1;
2564         uinfo->value.enumerated.items = e->max;
2565
2566         if (uinfo->value.enumerated.item > e->max - 1)
2567                 uinfo->value.enumerated.item = e->max - 1;
2568         strcpy(uinfo->value.enumerated.name,
2569                 e->texts[uinfo->value.enumerated.item]);
2570         return 0;
2571 }
2572 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2573
2574 /**
2575  * snd_soc_info_volsw_ext - external single mixer info callback
2576  * @kcontrol: mixer control
2577  * @uinfo: control element information
2578  *
2579  * Callback to provide information about a single external mixer control.
2580  *
2581  * Returns 0 for success.
2582  */
2583 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2584         struct snd_ctl_elem_info *uinfo)
2585 {
2586         int max = kcontrol->private_value;
2587
2588         if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2589                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2590         else
2591                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2592
2593         uinfo->count = 1;
2594         uinfo->value.integer.min = 0;
2595         uinfo->value.integer.max = max;
2596         return 0;
2597 }
2598 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2599
2600 /**
2601  * snd_soc_info_volsw - single mixer info callback
2602  * @kcontrol: mixer control
2603  * @uinfo: control element information
2604  *
2605  * Callback to provide information about a single mixer control, or a double
2606  * mixer control that spans 2 registers.
2607  *
2608  * Returns 0 for success.
2609  */
2610 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2611         struct snd_ctl_elem_info *uinfo)
2612 {
2613         struct soc_mixer_control *mc =
2614                 (struct soc_mixer_control *)kcontrol->private_value;
2615         int platform_max;
2616
2617         if (!mc->platform_max)
2618                 mc->platform_max = mc->max;
2619         platform_max = mc->platform_max;
2620
2621         if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2622                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2623         else
2624                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2625
2626         uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2627         uinfo->value.integer.min = 0;
2628         uinfo->value.integer.max = platform_max;
2629         return 0;
2630 }
2631 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2632
2633 /**
2634  * snd_soc_get_volsw - single mixer get callback
2635  * @kcontrol: mixer control
2636  * @ucontrol: control element information
2637  *
2638  * Callback to get the value of a single mixer control, or a double mixer
2639  * control that spans 2 registers.
2640  *
2641  * Returns 0 for success.
2642  */
2643 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2644         struct snd_ctl_elem_value *ucontrol)
2645 {
2646         struct soc_mixer_control *mc =
2647                 (struct soc_mixer_control *)kcontrol->private_value;
2648         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2649         unsigned int reg = mc->reg;
2650         unsigned int reg2 = mc->rreg;
2651         unsigned int shift = mc->shift;
2652         unsigned int rshift = mc->rshift;
2653         int max = mc->max;
2654         unsigned int mask = (1 << fls(max)) - 1;
2655         unsigned int invert = mc->invert;
2656
2657         ucontrol->value.integer.value[0] =
2658                 (snd_soc_read(codec, reg) >> shift) & mask;
2659         if (invert)
2660                 ucontrol->value.integer.value[0] =
2661                         max - ucontrol->value.integer.value[0];
2662
2663         if (snd_soc_volsw_is_stereo(mc)) {
2664                 if (reg == reg2)
2665                         ucontrol->value.integer.value[1] =
2666                                 (snd_soc_read(codec, reg) >> rshift) & mask;
2667                 else
2668                         ucontrol->value.integer.value[1] =
2669                                 (snd_soc_read(codec, reg2) >> shift) & mask;
2670                 if (invert)
2671                         ucontrol->value.integer.value[1] =
2672                                 max - ucontrol->value.integer.value[1];
2673         }
2674
2675         return 0;
2676 }
2677 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2678
2679 /**
2680  * snd_soc_put_volsw - single mixer put callback
2681  * @kcontrol: mixer control
2682  * @ucontrol: control element information
2683  *
2684  * Callback to set the value of a single mixer control, or a double mixer
2685  * control that spans 2 registers.
2686  *
2687  * Returns 0 for success.
2688  */
2689 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2690         struct snd_ctl_elem_value *ucontrol)
2691 {
2692         struct soc_mixer_control *mc =
2693                 (struct soc_mixer_control *)kcontrol->private_value;
2694         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2695         unsigned int reg = mc->reg;
2696         unsigned int reg2 = mc->rreg;
2697         unsigned int shift = mc->shift;
2698         unsigned int rshift = mc->rshift;
2699         int max = mc->max;
2700         unsigned int mask = (1 << fls(max)) - 1;
2701         unsigned int invert = mc->invert;
2702         int err;
2703         bool type_2r = 0;
2704         unsigned int val2 = 0;
2705         unsigned int val, val_mask;
2706
2707         val = (ucontrol->value.integer.value[0] & mask);
2708         if (invert)
2709                 val = max - val;
2710         val_mask = mask << shift;
2711         val = val << shift;
2712         if (snd_soc_volsw_is_stereo(mc)) {
2713                 val2 = (ucontrol->value.integer.value[1] & mask);
2714                 if (invert)
2715                         val2 = max - val2;
2716                 if (reg == reg2) {
2717                         val_mask |= mask << rshift;
2718                         val |= val2 << rshift;
2719                 } else {
2720                         val2 = val2 << shift;
2721                         type_2r = 1;
2722                 }
2723         }
2724         err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2725         if (err < 0)
2726                 return err;
2727
2728         if (type_2r)
2729                 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2730
2731         return err;
2732 }
2733 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2734
2735 /**
2736  * snd_soc_get_volsw_sx - single mixer get callback
2737  * @kcontrol: mixer control
2738  * @ucontrol: control element information
2739  *
2740  * Callback to get the value of a single mixer control, or a double mixer
2741  * control that spans 2 registers.
2742  *
2743  * Returns 0 for success.
2744  */
2745 int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2746                       struct snd_ctl_elem_value *ucontrol)
2747 {
2748         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2749         struct soc_mixer_control *mc =
2750             (struct soc_mixer_control *)kcontrol->private_value;
2751
2752         unsigned int reg = mc->reg;
2753         unsigned int reg2 = mc->rreg;
2754         unsigned int shift = mc->shift;
2755         unsigned int rshift = mc->rshift;
2756         int max = mc->max;
2757         int min = mc->min;
2758         int mask = (1 << (fls(min + max) - 1)) - 1;
2759
2760         ucontrol->value.integer.value[0] =
2761             ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2762
2763         if (snd_soc_volsw_is_stereo(mc))
2764                 ucontrol->value.integer.value[1] =
2765                         ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2766
2767         return 0;
2768 }
2769 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2770
2771 /**
2772  * snd_soc_put_volsw_sx - double mixer set callback
2773  * @kcontrol: mixer control
2774  * @uinfo: control element information
2775  *
2776  * Callback to set the value of a double mixer control that spans 2 registers.
2777  *
2778  * Returns 0 for success.
2779  */
2780 int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2781                          struct snd_ctl_elem_value *ucontrol)
2782 {
2783         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2784         struct soc_mixer_control *mc =
2785             (struct soc_mixer_control *)kcontrol->private_value;
2786
2787         unsigned int reg = mc->reg;
2788         unsigned int reg2 = mc->rreg;
2789         unsigned int shift = mc->shift;
2790         unsigned int rshift = mc->rshift;
2791         int max = mc->max;
2792         int min = mc->min;
2793         int mask = (1 << (fls(min + max) - 1)) - 1;
2794         int err = 0;
2795         unsigned short val, val_mask, val2 = 0;
2796
2797         val_mask = mask << shift;
2798         val = (ucontrol->value.integer.value[0] + min) & mask;
2799         val = val << shift;
2800
2801         err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2802         if (err < 0)
2803                 return err;
2804
2805         if (snd_soc_volsw_is_stereo(mc)) {
2806                 val_mask = mask << rshift;
2807                 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2808                 val2 = val2 << rshift;
2809
2810                 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2811                         return err;
2812         }
2813         return 0;
2814 }
2815 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2816
2817 /**
2818  * snd_soc_info_volsw_s8 - signed mixer info callback
2819  * @kcontrol: mixer control
2820  * @uinfo: control element information
2821  *
2822  * Callback to provide information about a signed mixer control.
2823  *
2824  * Returns 0 for success.
2825  */
2826 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2827         struct snd_ctl_elem_info *uinfo)
2828 {
2829         struct soc_mixer_control *mc =
2830                 (struct soc_mixer_control *)kcontrol->private_value;
2831         int platform_max;
2832         int min = mc->min;
2833
2834         if (!mc->platform_max)
2835                 mc->platform_max = mc->max;
2836         platform_max = mc->platform_max;
2837
2838         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2839         uinfo->count = 2;
2840         uinfo->value.integer.min = 0;
2841         uinfo->value.integer.max = platform_max - min;
2842         return 0;
2843 }
2844 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2845
2846 /**
2847  * snd_soc_get_volsw_s8 - signed mixer get callback
2848  * @kcontrol: mixer control
2849  * @ucontrol: control element information
2850  *
2851  * Callback to get the value of a signed mixer control.
2852  *
2853  * Returns 0 for success.
2854  */
2855 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2856         struct snd_ctl_elem_value *ucontrol)
2857 {
2858         struct soc_mixer_control *mc =
2859                 (struct soc_mixer_control *)kcontrol->private_value;
2860         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2861         unsigned int reg = mc->reg;
2862         int min = mc->min;
2863         int val = snd_soc_read(codec, reg);
2864
2865         ucontrol->value.integer.value[0] =
2866                 ((signed char)(val & 0xff))-min;
2867         ucontrol->value.integer.value[1] =
2868                 ((signed char)((val >> 8) & 0xff))-min;
2869         return 0;
2870 }
2871 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2872
2873 /**
2874  * snd_soc_put_volsw_sgn - signed mixer put callback
2875  * @kcontrol: mixer control
2876  * @ucontrol: control element information
2877  *
2878  * Callback to set the value of a signed mixer control.
2879  *
2880  * Returns 0 for success.
2881  */
2882 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2883         struct snd_ctl_elem_value *ucontrol)
2884 {
2885         struct soc_mixer_control *mc =
2886                 (struct soc_mixer_control *)kcontrol->private_value;
2887         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2888         unsigned int reg = mc->reg;
2889         int min = mc->min;
2890         unsigned int val;
2891
2892         val = (ucontrol->value.integer.value[0]+min) & 0xff;
2893         val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2894
2895         return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2896 }
2897 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2898
2899 /**
2900  * snd_soc_info_volsw_range - single mixer info callback with range.
2901  * @kcontrol: mixer control
2902  * @uinfo: control element information
2903  *
2904  * Callback to provide information, within a range, about a single
2905  * mixer control.
2906  *
2907  * returns 0 for success.
2908  */
2909 int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2910         struct snd_ctl_elem_info *uinfo)
2911 {
2912         struct soc_mixer_control *mc =
2913                 (struct soc_mixer_control *)kcontrol->private_value;
2914         int platform_max;
2915         int min = mc->min;
2916
2917         if (!mc->platform_max)
2918                 mc->platform_max = mc->max;
2919         platform_max = mc->platform_max;
2920
2921         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2922         uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
2923         uinfo->value.integer.min = 0;
2924         uinfo->value.integer.max = platform_max - min;
2925
2926         return 0;
2927 }
2928 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2929
2930 /**
2931  * snd_soc_put_volsw_range - single mixer put value callback with range.
2932  * @kcontrol: mixer control
2933  * @ucontrol: control element information
2934  *
2935  * Callback to set the value, within a range, for a single mixer control.
2936  *
2937  * Returns 0 for success.
2938  */
2939 int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2940         struct snd_ctl_elem_value *ucontrol)
2941 {
2942         struct soc_mixer_control *mc =
2943                 (struct soc_mixer_control *)kcontrol->private_value;
2944         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2945         unsigned int reg = mc->reg;
2946         unsigned int rreg = mc->rreg;
2947         unsigned int shift = mc->shift;
2948         int min = mc->min;
2949         int max = mc->max;
2950         unsigned int mask = (1 << fls(max)) - 1;
2951         unsigned int invert = mc->invert;
2952         unsigned int val, val_mask;
2953         int ret;
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         ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2962         if (ret != 0)
2963                 return ret;
2964
2965         if (snd_soc_volsw_is_stereo(mc)) {
2966                 val = ((ucontrol->value.integer.value[1] + min) & mask);
2967                 if (invert)
2968                         val = max - val;
2969                 val_mask = mask << shift;
2970                 val = val << shift;
2971
2972                 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2973         }
2974
2975         return ret;
2976 }
2977 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2978
2979 /**
2980  * snd_soc_get_volsw_range - single mixer get callback with range
2981  * @kcontrol: mixer control
2982  * @ucontrol: control element information
2983  *
2984  * Callback to get the value, within a range, of a single mixer control.
2985  *
2986  * Returns 0 for success.
2987  */
2988 int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2989         struct snd_ctl_elem_value *ucontrol)
2990 {
2991         struct soc_mixer_control *mc =
2992                 (struct soc_mixer_control *)kcontrol->private_value;
2993         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2994         unsigned int reg = mc->reg;
2995         unsigned int rreg = mc->rreg;
2996         unsigned int shift = mc->shift;
2997         int min = mc->min;
2998         int max = mc->max;
2999         unsigned int mask = (1 << fls(max)) - 1;
3000         unsigned int invert = mc->invert;
3001
3002         ucontrol->value.integer.value[0] =
3003                 (snd_soc_read(codec, reg) >> shift) & mask;
3004         if (invert)
3005                 ucontrol->value.integer.value[0] =
3006                         max - ucontrol->value.integer.value[0];
3007         ucontrol->value.integer.value[0] =
3008                 ucontrol->value.integer.value[0] - min;
3009
3010         if (snd_soc_volsw_is_stereo(mc)) {
3011                 ucontrol->value.integer.value[1] =
3012                         (snd_soc_read(codec, rreg) >> shift) & mask;
3013                 if (invert)
3014                         ucontrol->value.integer.value[1] =
3015                                 max - ucontrol->value.integer.value[1];
3016                 ucontrol->value.integer.value[1] =
3017                         ucontrol->value.integer.value[1] - min;
3018         }
3019
3020         return 0;
3021 }
3022 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3023
3024 /**
3025  * snd_soc_limit_volume - Set new limit to an existing volume control.
3026  *
3027  * @codec: where to look for the control
3028  * @name: Name of the control
3029  * @max: new maximum limit
3030  *
3031  * Return 0 for success, else error.
3032  */
3033 int snd_soc_limit_volume(struct snd_soc_codec *codec,
3034         const char *name, int max)
3035 {
3036         struct snd_card *card = codec->card->snd_card;
3037         struct snd_kcontrol *kctl;
3038         struct soc_mixer_control *mc;
3039         int found = 0;
3040         int ret = -EINVAL;
3041
3042         /* Sanity check for name and max */
3043         if (unlikely(!name || max <= 0))
3044                 return -EINVAL;
3045
3046         list_for_each_entry(kctl, &card->controls, list) {
3047                 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3048                         found = 1;
3049                         break;
3050                 }
3051         }
3052         if (found) {
3053                 mc = (struct soc_mixer_control *)kctl->private_value;
3054                 if (max <= mc->max) {
3055                         mc->platform_max = max;
3056                         ret = 0;
3057                 }
3058         }
3059         return ret;
3060 }
3061 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3062
3063 int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3064                        struct snd_ctl_elem_info *uinfo)
3065 {
3066         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3067         struct soc_bytes *params = (void *)kcontrol->private_value;
3068
3069         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3070         uinfo->count = params->num_regs * codec->val_bytes;
3071
3072         return 0;
3073 }
3074 EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3075
3076 int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3077                       struct snd_ctl_elem_value *ucontrol)
3078 {
3079         struct soc_bytes *params = (void *)kcontrol->private_value;
3080         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3081         int ret;
3082
3083         if (codec->using_regmap)
3084                 ret = regmap_raw_read(codec->control_data, params->base,
3085                                       ucontrol->value.bytes.data,
3086                                       params->num_regs * codec->val_bytes);
3087         else
3088                 ret = -EINVAL;
3089
3090         /* Hide any masked bytes to ensure consistent data reporting */
3091         if (ret == 0 && params->mask) {
3092                 switch (codec->val_bytes) {
3093                 case 1:
3094                         ucontrol->value.bytes.data[0] &= ~params->mask;
3095                         break;
3096                 case 2:
3097                         ((u16 *)(&ucontrol->value.bytes.data))[0]
3098                                 &= ~params->mask;
3099                         break;
3100                 case 4:
3101                         ((u32 *)(&ucontrol->value.bytes.data))[0]
3102                                 &= ~params->mask;
3103                         break;
3104                 default:
3105                         return -EINVAL;
3106                 }
3107         }
3108
3109         return ret;
3110 }
3111 EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3112
3113 int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3114                       struct snd_ctl_elem_value *ucontrol)
3115 {
3116         struct soc_bytes *params = (void *)kcontrol->private_value;
3117         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3118         int ret, len;
3119         unsigned int val;
3120         void *data;
3121
3122         if (!codec->using_regmap)
3123                 return -EINVAL;
3124
3125         data = ucontrol->value.bytes.data;
3126         len = params->num_regs * codec->val_bytes;
3127
3128         /*
3129          * If we've got a mask then we need to preserve the register
3130          * bits.  We shouldn't modify the incoming data so take a
3131          * copy.
3132          */
3133         if (params->mask) {
3134                 ret = regmap_read(codec->control_data, params->base, &val);
3135                 if (ret != 0)
3136                         return ret;
3137
3138                 val &= params->mask;
3139
3140                 data = kmemdup(data, len, GFP_KERNEL);
3141                 if (!data)
3142                         return -ENOMEM;
3143
3144                 switch (codec->val_bytes) {
3145                 case 1:
3146                         ((u8 *)data)[0] &= ~params->mask;
3147                         ((u8 *)data)[0] |= val;
3148                         break;
3149                 case 2:
3150                         ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
3151                         ((u16 *)data)[0] |= cpu_to_be16(val);
3152                         break;
3153                 case 4:
3154                         ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
3155                         ((u32 *)data)[0] |= cpu_to_be32(val);
3156                         break;
3157                 default:
3158                         return -EINVAL;
3159                 }
3160         }
3161
3162         ret = regmap_raw_write(codec->control_data, params->base,
3163                                data, len);
3164
3165         if (params->mask)
3166                 kfree(data);
3167
3168         return ret;
3169 }
3170 EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3171
3172 /**
3173  * snd_soc_info_xr_sx - signed multi register info callback
3174  * @kcontrol: mreg control
3175  * @uinfo: control element information
3176  *
3177  * Callback to provide information of a control that can
3178  * span multiple codec registers which together
3179  * forms a single signed value in a MSB/LSB manner.
3180  *
3181  * Returns 0 for success.
3182  */
3183 int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3184         struct snd_ctl_elem_info *uinfo)
3185 {
3186         struct soc_mreg_control *mc =
3187                 (struct soc_mreg_control *)kcontrol->private_value;
3188         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3189         uinfo->count = 1;
3190         uinfo->value.integer.min = mc->min;
3191         uinfo->value.integer.max = mc->max;
3192
3193         return 0;
3194 }
3195 EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3196
3197 /**
3198  * snd_soc_get_xr_sx - signed multi register get callback
3199  * @kcontrol: mreg control
3200  * @ucontrol: control element information
3201  *
3202  * Callback to get the value of a control that can span
3203  * multiple codec registers which together forms a single
3204  * signed value in a MSB/LSB manner. The control supports
3205  * specifying total no of bits used to allow for bitfields
3206  * across the multiple codec registers.
3207  *
3208  * Returns 0 for success.
3209  */
3210 int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3211         struct snd_ctl_elem_value *ucontrol)
3212 {
3213         struct soc_mreg_control *mc =
3214                 (struct soc_mreg_control *)kcontrol->private_value;
3215         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3216         unsigned int regbase = mc->regbase;
3217         unsigned int regcount = mc->regcount;
3218         unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3219         unsigned int regwmask = (1<<regwshift)-1;
3220         unsigned int invert = mc->invert;
3221         unsigned long mask = (1UL<<mc->nbits)-1;
3222         long min = mc->min;
3223         long max = mc->max;
3224         long val = 0;
3225         unsigned long regval;
3226         unsigned int i;
3227
3228         for (i = 0; i < regcount; i++) {
3229                 regval = snd_soc_read(codec, regbase+i) & regwmask;
3230                 val |= regval << (regwshift*(regcount-i-1));
3231         }
3232         val &= mask;
3233         if (min < 0 && val > max)
3234                 val |= ~mask;
3235         if (invert)
3236                 val = max - val;
3237         ucontrol->value.integer.value[0] = val;
3238
3239         return 0;
3240 }
3241 EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3242
3243 /**
3244  * snd_soc_put_xr_sx - signed multi register get callback
3245  * @kcontrol: mreg control
3246  * @ucontrol: control element information
3247  *
3248  * Callback to set the value of a control that can span
3249  * multiple codec registers which together forms a single
3250  * signed value in a MSB/LSB manner. The control supports
3251  * specifying total no of bits used to allow for bitfields
3252  * across the multiple codec registers.
3253  *
3254  * Returns 0 for success.
3255  */
3256 int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3257         struct snd_ctl_elem_value *ucontrol)
3258 {
3259         struct soc_mreg_control *mc =
3260                 (struct soc_mreg_control *)kcontrol->private_value;
3261         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3262         unsigned int regbase = mc->regbase;
3263         unsigned int regcount = mc->regcount;
3264         unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3265         unsigned int regwmask = (1<<regwshift)-1;
3266         unsigned int invert = mc->invert;
3267         unsigned long mask = (1UL<<mc->nbits)-1;
3268         long max = mc->max;
3269         long val = ucontrol->value.integer.value[0];
3270         unsigned int i, regval, regmask;
3271         int err;
3272
3273         if (invert)
3274                 val = max - val;
3275         val &= mask;
3276         for (i = 0; i < regcount; i++) {
3277                 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3278                 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3279                 err = snd_soc_update_bits_locked(codec, regbase+i,
3280                                 regmask, regval);
3281                 if (err < 0)
3282                         return err;
3283         }
3284
3285         return 0;
3286 }
3287 EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3288
3289 /**
3290  * snd_soc_get_strobe - strobe get callback
3291  * @kcontrol: mixer control
3292  * @ucontrol: control element information
3293  *
3294  * Callback get the value of a strobe mixer control.
3295  *
3296  * Returns 0 for success.
3297  */
3298 int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3299         struct snd_ctl_elem_value *ucontrol)
3300 {
3301         struct soc_mixer_control *mc =
3302                 (struct soc_mixer_control *)kcontrol->private_value;
3303         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3304         unsigned int reg = mc->reg;
3305         unsigned int shift = mc->shift;
3306         unsigned int mask = 1 << shift;
3307         unsigned int invert = mc->invert != 0;
3308         unsigned int val = snd_soc_read(codec, reg) & mask;
3309
3310         if (shift != 0 && val != 0)
3311                 val = val >> shift;
3312         ucontrol->value.enumerated.item[0] = val ^ invert;
3313
3314         return 0;
3315 }
3316 EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3317
3318 /**
3319  * snd_soc_put_strobe - strobe put callback
3320  * @kcontrol: mixer control
3321  * @ucontrol: control element information
3322  *
3323  * Callback strobe a register bit to high then low (or the inverse)
3324  * in one pass of a single mixer enum control.
3325  *
3326  * Returns 1 for success.
3327  */
3328 int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3329         struct snd_ctl_elem_value *ucontrol)
3330 {
3331         struct soc_mixer_control *mc =
3332                 (struct soc_mixer_control *)kcontrol->private_value;
3333         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3334         unsigned int reg = mc->reg;
3335         unsigned int shift = mc->shift;
3336         unsigned int mask = 1 << shift;
3337         unsigned int invert = mc->invert != 0;
3338         unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3339         unsigned int val1 = (strobe ^ invert) ? mask : 0;
3340         unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3341         int err;
3342
3343         err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3344         if (err < 0)
3345                 return err;
3346
3347         err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3348         return err;
3349 }
3350 EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3351
3352 /**
3353  * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3354  * @dai: DAI
3355  * @clk_id: DAI specific clock ID
3356  * @freq: new clock frequency in Hz
3357  * @dir: new clock direction - input/output.
3358  *
3359  * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3360  */
3361 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3362         unsigned int freq, int dir)
3363 {
3364         if (dai->driver && dai->driver->ops->set_sysclk)
3365                 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
3366         else if (dai->codec && dai->codec->driver->set_sysclk)
3367                 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
3368                                                       freq, dir);
3369         else
3370                 return -EINVAL;
3371 }
3372 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3373
3374 /**
3375  * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3376  * @codec: CODEC
3377  * @clk_id: DAI specific clock ID
3378  * @source: Source for the clock
3379  * @freq: new clock frequency in Hz
3380  * @dir: new clock direction - input/output.
3381  *
3382  * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3383  */
3384 int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
3385                              int source, unsigned int freq, int dir)
3386 {
3387         if (codec->driver->set_sysclk)
3388                 return codec->driver->set_sysclk(codec, clk_id, source,
3389                                                  freq, dir);
3390         else
3391                 return -EINVAL;
3392 }
3393 EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3394
3395 /**
3396  * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3397  * @dai: DAI
3398  * @div_id: DAI specific clock divider ID
3399  * @div: new clock divisor.
3400  *
3401  * Configures the clock dividers. This is used to derive the best DAI bit and
3402  * frame clocks from the system or master clock. It's best to set the DAI bit
3403  * and frame clocks as low as possible to save system power.
3404  */
3405 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3406         int div_id, int div)
3407 {
3408         if (dai->driver && dai->driver->ops->set_clkdiv)
3409                 return dai->driver->ops->set_clkdiv(dai, div_id, div);
3410         else
3411                 return -EINVAL;
3412 }
3413 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3414
3415 /**
3416  * snd_soc_dai_set_pll - configure DAI PLL.
3417  * @dai: DAI
3418  * @pll_id: DAI specific PLL ID
3419  * @source: DAI specific source for the PLL
3420  * @freq_in: PLL input clock frequency in Hz
3421  * @freq_out: requested PLL output clock frequency in Hz
3422  *
3423  * Configures and enables PLL to generate output clock based on input clock.
3424  */
3425 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3426         unsigned int freq_in, unsigned int freq_out)
3427 {
3428         if (dai->driver && dai->driver->ops->set_pll)
3429                 return dai->driver->ops->set_pll(dai, pll_id, source,
3430                                          freq_in, freq_out);
3431         else if (dai->codec && dai->codec->driver->set_pll)
3432                 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3433                                                    freq_in, freq_out);
3434         else
3435                 return -EINVAL;
3436 }
3437 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3438
3439 /*
3440  * snd_soc_codec_set_pll - configure codec PLL.
3441  * @codec: CODEC
3442  * @pll_id: DAI specific PLL ID
3443  * @source: DAI specific source for the PLL
3444  * @freq_in: PLL input clock frequency in Hz
3445  * @freq_out: requested PLL output clock frequency in Hz
3446  *
3447  * Configures and enables PLL to generate output clock based on input clock.
3448  */
3449 int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3450                           unsigned int freq_in, unsigned int freq_out)
3451 {
3452         if (codec->driver->set_pll)
3453                 return codec->driver->set_pll(codec, pll_id, source,
3454                                               freq_in, freq_out);
3455         else
3456                 return -EINVAL;
3457 }
3458 EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3459
3460 /**
3461  * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3462  * @dai: DAI
3463  * @fmt: SND_SOC_DAIFMT_ format value.
3464  *
3465  * Configures the DAI hardware format and clocking.
3466  */
3467 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3468 {
3469         if (dai->driver == NULL)
3470                 return -EINVAL;
3471         if (dai->driver->ops->set_fmt == NULL)
3472                 return -ENOTSUPP;
3473         return dai->driver->ops->set_fmt(dai, fmt);
3474 }
3475 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3476
3477 /**
3478  * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3479  * @dai: DAI
3480  * @tx_mask: bitmask representing active TX slots.
3481  * @rx_mask: bitmask representing active RX slots.
3482  * @slots: Number of slots in use.
3483  * @slot_width: Width in bits for each slot.
3484  *
3485  * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3486  * specific.
3487  */
3488 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
3489         unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
3490 {
3491         if (dai->driver && dai->driver->ops->set_tdm_slot)
3492                 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
3493                                 slots, slot_width);
3494         else
3495                 return -EINVAL;
3496 }
3497 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3498
3499 /**
3500  * snd_soc_dai_set_channel_map - configure DAI audio channel map
3501  * @dai: DAI
3502  * @tx_num: how many TX channels
3503  * @tx_slot: pointer to an array which imply the TX slot number channel
3504  *           0~num-1 uses
3505  * @rx_num: how many RX channels
3506  * @rx_slot: pointer to an array which imply the RX slot number channel
3507  *           0~num-1 uses
3508  *
3509  * configure the relationship between channel number and TDM slot number.
3510  */
3511 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3512         unsigned int tx_num, unsigned int *tx_slot,
3513         unsigned int rx_num, unsigned int *rx_slot)
3514 {
3515         if (dai->driver && dai->driver->ops->set_channel_map)
3516                 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
3517                         rx_num, rx_slot);
3518         else
3519                 return -EINVAL;
3520 }
3521 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3522
3523 /**
3524  * snd_soc_dai_set_tristate - configure DAI system or master clock.
3525  * @dai: DAI
3526  * @tristate: tristate enable
3527  *
3528  * Tristates the DAI so that others can use it.
3529  */
3530 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3531 {
3532         if (dai->driver && dai->driver->ops->set_tristate)
3533                 return dai->driver->ops->set_tristate(dai, tristate);
3534         else
3535                 return -EINVAL;
3536 }
3537 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3538
3539 /**
3540  * snd_soc_dai_digital_mute - configure DAI system or master clock.
3541  * @dai: DAI
3542  * @mute: mute enable
3543  *
3544  * Mutes the DAI DAC.
3545  */
3546 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3547 {
3548         if (dai->driver && dai->driver->ops->digital_mute)
3549                 return dai->driver->ops->digital_mute(dai, mute);
3550         else
3551                 return -ENOTSUPP;
3552 }
3553 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3554
3555 /**
3556  * snd_soc_register_card - Register a card with the ASoC core
3557  *
3558  * @card: Card to register
3559  *
3560  */
3561 int snd_soc_register_card(struct snd_soc_card *card)
3562 {
3563         int i, ret;
3564
3565         if (!card->name || !card->dev)
3566                 return -EINVAL;
3567
3568         for (i = 0; i < card->num_links; i++) {
3569                 struct snd_soc_dai_link *link = &card->dai_link[i];
3570
3571                 /*
3572                  * Codec must be specified by 1 of name or OF node,
3573                  * not both or neither.
3574                  */
3575                 if (!!link->codec_name == !!link->codec_of_node) {
3576                         dev_err(card->dev, "ASoC: Neither/both codec"
3577                                 " name/of_node are set for %s\n", link->name);
3578                         return -EINVAL;
3579                 }
3580                 /* Codec DAI name must be specified */
3581                 if (!link->codec_dai_name) {
3582                         dev_err(card->dev, "ASoC: codec_dai_name not"
3583                                 " set for %s\n", link->name);
3584                         return -EINVAL;
3585                 }
3586
3587                 /*
3588                  * Platform may be specified by either name or OF node, but
3589                  * can be left unspecified, and a dummy platform will be used.
3590                  */
3591                 if (link->platform_name && link->platform_of_node) {
3592                         dev_err(card->dev, "ASoC: Both platform name/of_node"
3593                                 " are set for %s\n", link->name);
3594                         return -EINVAL;
3595                 }
3596
3597                 /*
3598                  * CPU device may be specified by either name or OF node, but
3599                  * can be left unspecified, and will be matched based on DAI
3600                  * name alone..
3601                  */
3602                 if (link->cpu_name && link->cpu_of_node) {
3603                         dev_err(card->dev, "ASoC: Neither/both "
3604                                 "cpu name/of_node are set for %s\n",link->name);
3605                         return -EINVAL;
3606                 }
3607                 /*
3608                  * At least one of CPU DAI name or CPU device name/node must be
3609                  * specified
3610                  */
3611                 if (!link->cpu_dai_name &&
3612                     !(link->cpu_name || link->cpu_of_node)) {
3613                         dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
3614                                 "cpu_name/of_node are set for %s\n", link->name);
3615                         return -EINVAL;
3616                 }
3617         }
3618
3619         dev_set_drvdata(card->dev, card);
3620
3621         snd_soc_initialize_card_lists(card);
3622
3623         soc_init_card_debugfs(card);
3624
3625         card->rtd = devm_kzalloc(card->dev,
3626                                  sizeof(struct snd_soc_pcm_runtime) *
3627                                  (card->num_links + card->num_aux_devs),
3628                                  GFP_KERNEL);
3629         if (card->rtd == NULL)
3630                 return -ENOMEM;
3631         card->num_rtd = 0;
3632         card->rtd_aux = &card->rtd[card->num_links];
3633
3634         for (i = 0; i < card->num_links; i++)
3635                 card->rtd[i].dai_link = &card->dai_link[i];
3636
3637         INIT_LIST_HEAD(&card->list);
3638         INIT_LIST_HEAD(&card->dapm_dirty);
3639         card->instantiated = 0;
3640         mutex_init(&card->mutex);
3641         mutex_init(&card->dapm_mutex);
3642
3643         ret = snd_soc_instantiate_card(card);
3644         if (ret != 0)
3645                 soc_cleanup_card_debugfs(card);
3646
3647         return ret;
3648 }
3649 EXPORT_SYMBOL_GPL(snd_soc_register_card);
3650
3651 /**
3652  * snd_soc_unregister_card - Unregister a card with the ASoC core
3653  *
3654  * @card: Card to unregister
3655  *
3656  */
3657 int snd_soc_unregister_card(struct snd_soc_card *card)
3658 {
3659         if (card->instantiated)
3660                 soc_cleanup_card_resources(card);
3661         dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
3662
3663         return 0;
3664 }
3665 EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
3666
3667 /*
3668  * Simplify DAI link configuration by removing ".-1" from device names
3669  * and sanitizing names.
3670  */
3671 static char *fmt_single_name(struct device *dev, int *id)
3672 {
3673         char *found, name[NAME_SIZE];
3674         int id1, id2;
3675
3676         if (dev_name(dev) == NULL)
3677                 return NULL;
3678
3679         strlcpy(name, dev_name(dev), NAME_SIZE);
3680
3681         /* are we a "%s.%d" name (platform and SPI components) */
3682         found = strstr(name, dev->driver->name);
3683         if (found) {
3684                 /* get ID */
3685                 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3686
3687                         /* discard ID from name if ID == -1 */
3688                         if (*id == -1)
3689                                 found[strlen(dev->driver->name)] = '\0';
3690                 }
3691
3692         } else {
3693                 /* I2C component devices are named "bus-addr"  */
3694                 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3695                         char tmp[NAME_SIZE];
3696
3697                         /* create unique ID number from I2C addr and bus */
3698                         *id = ((id1 & 0xffff) << 16) + id2;
3699
3700                         /* sanitize component name for DAI link creation */
3701                         snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
3702                         strlcpy(name, tmp, NAME_SIZE);
3703                 } else
3704                         *id = 0;
3705         }
3706
3707         return kstrdup(name, GFP_KERNEL);
3708 }
3709
3710 /*
3711  * Simplify DAI link naming for single devices with multiple DAIs by removing
3712  * any ".-1" and using the DAI name (instead of device name).
3713  */
3714 static inline char *fmt_multiple_name(struct device *dev,
3715                 struct snd_soc_dai_driver *dai_drv)
3716 {
3717         if (dai_drv->name == NULL) {
3718                 dev_err(dev, "ASoC: error - multiple DAI %s registered with"
3719                                 " no name\n", dev_name(dev));
3720                 return NULL;
3721         }
3722
3723         return kstrdup(dai_drv->name, GFP_KERNEL);
3724 }
3725
3726 /**
3727  * snd_soc_register_dai - Register a DAI with the ASoC core
3728  *
3729  * @dai: DAI to register
3730  */
3731 int snd_soc_register_dai(struct device *dev,
3732                 struct snd_soc_dai_driver *dai_drv)
3733 {
3734         struct snd_soc_codec *codec;
3735         struct snd_soc_dai *dai;
3736
3737         dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
3738
3739         dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3740         if (dai == NULL)
3741                 return -ENOMEM;
3742
3743         /* create DAI component name */
3744         dai->name = fmt_single_name(dev, &dai->id);
3745         if (dai->name == NULL) {
3746                 kfree(dai);
3747                 return -ENOMEM;
3748         }
3749
3750         dai->dev = dev;
3751         dai->driver = dai_drv;
3752         dai->dapm.dev = dev;
3753         if (!dai->driver->ops)
3754                 dai->driver->ops = &null_dai_ops;
3755
3756         mutex_lock(&client_mutex);
3757
3758         list_for_each_entry(codec, &codec_list, list) {
3759                 if (codec->dev == dev) {
3760                         dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
3761                                 dai->name, codec->name);
3762                         dai->codec = codec;
3763                         break;
3764                 }
3765         }
3766
3767         if (!dai->codec)
3768                 dai->dapm.idle_bias_off = 1;
3769
3770         list_add(&dai->list, &dai_list);
3771
3772         mutex_unlock(&client_mutex);
3773
3774         dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
3775
3776         return 0;
3777 }
3778 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3779
3780 /**
3781  * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3782  *
3783  * @dai: DAI to unregister
3784  */
3785 void snd_soc_unregister_dai(struct device *dev)
3786 {
3787         struct snd_soc_dai *dai;
3788
3789         list_for_each_entry(dai, &dai_list, list) {
3790                 if (dev == dai->dev)
3791                         goto found;
3792         }
3793         return;
3794
3795 found:
3796         mutex_lock(&client_mutex);
3797         list_del(&dai->list);
3798         mutex_unlock(&client_mutex);
3799
3800         dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
3801         kfree(dai->name);
3802         kfree(dai);
3803 }
3804 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3805
3806 /**
3807  * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3808  *
3809  * @dai: Array of DAIs to register
3810  * @count: Number of DAIs
3811  */
3812 int snd_soc_register_dais(struct device *dev,
3813                 struct snd_soc_dai_driver *dai_drv, size_t count)
3814 {
3815         struct snd_soc_codec *codec;
3816         struct snd_soc_dai *dai;
3817         int i, ret = 0;
3818
3819         dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
3820
3821         for (i = 0; i < count; i++) {
3822
3823                 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3824                 if (dai == NULL) {
3825                         ret = -ENOMEM;
3826                         goto err;
3827                 }
3828
3829                 /* create DAI component name */
3830                 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3831                 if (dai->name == NULL) {
3832                         kfree(dai);
3833                         ret = -EINVAL;
3834                         goto err;
3835                 }
3836
3837                 dai->dev = dev;
3838                 dai->driver = &dai_drv[i];
3839                 if (dai->driver->id)
3840                         dai->id = dai->driver->id;
3841                 else
3842                         dai->id = i;
3843                 dai->dapm.dev = dev;
3844                 if (!dai->driver->ops)
3845                         dai->driver->ops = &null_dai_ops;
3846
3847                 mutex_lock(&client_mutex);
3848
3849                 list_for_each_entry(codec, &codec_list, list) {
3850                         if (codec->dev == dev) {
3851                                 dev_dbg(dev, "ASoC: Mapped DAI %s to "
3852                                         "CODEC %s\n", dai->name, codec->name);
3853                                 dai->codec = codec;
3854                                 break;
3855                         }
3856                 }
3857
3858                 if (!dai->codec)
3859                         dai->dapm.idle_bias_off = 1;
3860
3861                 list_add(&dai->list, &dai_list);
3862
3863                 mutex_unlock(&client_mutex);
3864
3865                 dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
3866         }
3867
3868         return 0;
3869
3870 err:
3871         for (i--; i >= 0; i--)
3872                 snd_soc_unregister_dai(dev);
3873
3874         return ret;
3875 }
3876 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3877
3878 /**
3879  * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3880  *
3881  * @dai: Array of DAIs to unregister
3882  * @count: Number of DAIs
3883  */
3884 void snd_soc_unregister_dais(struct device *dev, size_t count)
3885 {
3886         int i;
3887
3888         for (i = 0; i < count; i++)
3889                 snd_soc_unregister_dai(dev);
3890 }
3891 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3892
3893 /**
3894  * snd_soc_register_platform - Register a platform with the ASoC core
3895  *
3896  * @platform: platform to register
3897  */
3898 int snd_soc_register_platform(struct device *dev,
3899                 struct snd_soc_platform_driver *platform_drv)
3900 {
3901         struct snd_soc_platform *platform;
3902
3903         dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3904
3905         platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3906         if (platform == NULL)
3907                 return -ENOMEM;
3908
3909         /* create platform component name */
3910         platform->name = fmt_single_name(dev, &platform->id);
3911         if (platform->name == NULL) {
3912                 kfree(platform);
3913                 return -ENOMEM;
3914         }
3915
3916         platform->dev = dev;
3917         platform->driver = platform_drv;
3918         platform->dapm.dev = dev;
3919         platform->dapm.platform = platform;
3920         platform->dapm.stream_event = platform_drv->stream_event;
3921         mutex_init(&platform->mutex);
3922
3923         mutex_lock(&client_mutex);
3924         list_add(&platform->list, &platform_list);
3925         mutex_unlock(&client_mutex);
3926
3927         dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
3928
3929         return 0;
3930 }
3931 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3932
3933 /**
3934  * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3935  *
3936  * @platform: platform to unregister
3937  */
3938 void snd_soc_unregister_platform(struct device *dev)
3939 {
3940         struct snd_soc_platform *platform;
3941
3942         list_for_each_entry(platform, &platform_list, list) {
3943                 if (dev == platform->dev)
3944                         goto found;
3945         }
3946         return;
3947
3948 found:
3949         mutex_lock(&client_mutex);
3950         list_del(&platform->list);
3951         mutex_unlock(&client_mutex);
3952
3953         dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
3954         kfree(platform->name);
3955         kfree(platform);
3956 }
3957 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3958
3959 static u64 codec_format_map[] = {
3960         SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3961         SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3962         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3963         SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3964         SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3965         SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3966         SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3967         SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3968         SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3969         SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3970         SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3971         SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3972         SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3973         SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3974         SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3975         | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3976 };
3977
3978 /* Fix up the DAI formats for endianness: codecs don't actually see
3979  * the endianness of the data but we're using the CPU format
3980  * definitions which do need to include endianness so we ensure that
3981  * codec DAIs always have both big and little endian variants set.
3982  */
3983 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3984 {
3985         int i;
3986
3987         for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3988                 if (stream->formats & codec_format_map[i])
3989                         stream->formats |= codec_format_map[i];
3990 }
3991
3992 /**
3993  * snd_soc_register_codec - Register a codec with the ASoC core
3994  *
3995  * @codec: codec to register
3996  */
3997 int snd_soc_register_codec(struct device *dev,
3998                            const struct snd_soc_codec_driver *codec_drv,
3999                            struct snd_soc_dai_driver *dai_drv,
4000                            int num_dai)
4001 {
4002         size_t reg_size;
4003         struct snd_soc_codec *codec;
4004         int ret, i;
4005
4006         dev_dbg(dev, "codec register %s\n", dev_name(dev));
4007
4008         codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4009         if (codec == NULL)
4010                 return -ENOMEM;
4011
4012         /* create CODEC component name */
4013         codec->name = fmt_single_name(dev, &codec->id);
4014         if (codec->name == NULL) {
4015                 kfree(codec);
4016                 return -ENOMEM;
4017         }
4018
4019         if (codec_drv->compress_type)
4020                 codec->compress_type = codec_drv->compress_type;
4021         else
4022                 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
4023
4024         codec->write = codec_drv->write;
4025         codec->read = codec_drv->read;
4026         codec->volatile_register = codec_drv->volatile_register;
4027         codec->readable_register = codec_drv->readable_register;
4028         codec->writable_register = codec_drv->writable_register;
4029         codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
4030         codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4031         codec->dapm.dev = dev;
4032         codec->dapm.codec = codec;
4033         codec->dapm.seq_notifier = codec_drv->seq_notifier;
4034         codec->dapm.stream_event = codec_drv->stream_event;
4035         codec->dev = dev;
4036         codec->driver = codec_drv;
4037         codec->num_dai = num_dai;
4038         mutex_init(&codec->mutex);
4039
4040         /* allocate CODEC register cache */
4041         if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
4042                 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
4043                 codec->reg_size = reg_size;
4044                 /* it is necessary to make a copy of the default register cache
4045                  * because in the case of using a compression type that requires
4046                  * the default register cache to be marked as the
4047                  * kernel might have freed the array by the time we initialize
4048                  * the cache.
4049                  */
4050                 if (codec_drv->reg_cache_default) {
4051                         codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
4052                                                       reg_size, GFP_KERNEL);
4053                         if (!codec->reg_def_copy) {
4054                                 ret = -ENOMEM;
4055                                 goto fail;
4056                         }
4057                 }
4058         }
4059
4060         if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
4061                 if (!codec->volatile_register)
4062                         codec->volatile_register = snd_soc_default_volatile_register;
4063                 if (!codec->readable_register)
4064                         codec->readable_register = snd_soc_default_readable_register;
4065                 if (!codec->writable_register)
4066                         codec->writable_register = snd_soc_default_writable_register;
4067         }
4068
4069         for (i = 0; i < num_dai; i++) {
4070                 fixup_codec_formats(&dai_drv[i].playback);
4071                 fixup_codec_formats(&dai_drv[i].capture);
4072         }
4073
4074         mutex_lock(&client_mutex);
4075         list_add(&codec->list, &codec_list);
4076         mutex_unlock(&client_mutex);
4077
4078         /* register any DAIs */
4079         if (num_dai) {
4080                 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
4081                 if (ret < 0)
4082                         dev_err(codec->dev, "ASoC: Failed to regster"
4083                                 " DAIs: %d\n", ret);
4084         }
4085
4086         dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
4087         return 0;
4088
4089 fail:
4090         kfree(codec->name);
4091         kfree(codec);
4092         return ret;
4093 }
4094 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4095
4096 /**
4097  * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4098  *
4099  * @codec: codec to unregister
4100  */
4101 void snd_soc_unregister_codec(struct device *dev)
4102 {
4103         struct snd_soc_codec *codec;
4104         int i;
4105
4106         list_for_each_entry(codec, &codec_list, list) {
4107                 if (dev == codec->dev)
4108                         goto found;
4109         }
4110         return;
4111
4112 found:
4113         if (codec->num_dai)
4114                 for (i = 0; i < codec->num_dai; i++)
4115                         snd_soc_unregister_dai(dev);
4116
4117         mutex_lock(&client_mutex);
4118         list_del(&codec->list);
4119         mutex_unlock(&client_mutex);
4120
4121         dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
4122
4123         snd_soc_cache_exit(codec);
4124         kfree(codec->reg_def_copy);
4125         kfree(codec->name);
4126         kfree(codec);
4127 }
4128 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4129
4130 /* Retrieve a card's name from device tree */
4131 int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4132                                const char *propname)
4133 {
4134         struct device_node *np = card->dev->of_node;
4135         int ret;
4136
4137         ret = of_property_read_string_index(np, propname, 0, &card->name);
4138         /*
4139          * EINVAL means the property does not exist. This is fine providing
4140          * card->name was previously set, which is checked later in
4141          * snd_soc_register_card.
4142          */
4143         if (ret < 0 && ret != -EINVAL) {
4144                 dev_err(card->dev,
4145                         "ASoC: Property '%s' could not be read: %d\n",
4146                         propname, ret);
4147                 return ret;
4148         }
4149
4150         return 0;
4151 }
4152 EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4153
4154 int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4155                                    const char *propname)
4156 {
4157         struct device_node *np = card->dev->of_node;
4158         int num_routes;
4159         struct snd_soc_dapm_route *routes;
4160         int i, ret;
4161
4162         num_routes = of_property_count_strings(np, propname);
4163         if (num_routes < 0 || num_routes & 1) {
4164                 dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
4165                         " length is not even\n", propname);
4166                 return -EINVAL;
4167         }
4168         num_routes /= 2;
4169         if (!num_routes) {
4170                 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4171                         propname);
4172                 return -EINVAL;
4173         }
4174
4175         routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4176                               GFP_KERNEL);
4177         if (!routes) {
4178                 dev_err(card->dev,
4179                         "ASoC: Could not allocate DAPM route table\n");
4180                 return -EINVAL;
4181         }
4182
4183         for (i = 0; i < num_routes; i++) {
4184                 ret = of_property_read_string_index(np, propname,
4185                         2 * i, &routes[i].sink);
4186                 if (ret) {
4187                         dev_err(card->dev,
4188                                 "ASoC: Property '%s' index %d could not be read: %d\n",
4189                                 propname, 2 * i, ret);
4190                         kfree(routes);
4191                         return -EINVAL;
4192                 }
4193                 ret = of_property_read_string_index(np, propname,
4194                         (2 * i) + 1, &routes[i].source);
4195                 if (ret) {
4196                         dev_err(card->dev,
4197                                 "ASoC: Property '%s' index %d could not be read: %d\n",
4198                                 propname, (2 * i) + 1, ret);
4199                         kfree(routes);
4200                         return -EINVAL;
4201                 }
4202         }
4203
4204         card->num_dapm_routes = num_routes;
4205         card->dapm_routes = routes;
4206
4207         return 0;
4208 }
4209 EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4210
4211 static int __init snd_soc_init(void)
4212 {
4213 #ifdef CONFIG_DEBUG_FS
4214         snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4215         if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
4216                 pr_warn("ASoC: Failed to create debugfs directory\n");
4217                 snd_soc_debugfs_root = NULL;
4218         }
4219
4220         if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
4221                                  &codec_list_fops))
4222                 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4223
4224         if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
4225                                  &dai_list_fops))
4226                 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
4227
4228         if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
4229                                  &platform_list_fops))
4230                 pr_warn("ASoC: Failed to create platform list debugfs file\n");
4231 #endif
4232
4233         snd_soc_util_init();
4234
4235         return platform_driver_register(&soc_driver);
4236 }
4237 module_init(snd_soc_init);
4238
4239 static void __exit snd_soc_exit(void)
4240 {
4241         snd_soc_util_exit();
4242
4243 #ifdef CONFIG_DEBUG_FS
4244         debugfs_remove_recursive(snd_soc_debugfs_root);
4245 #endif
4246         platform_driver_unregister(&soc_driver);
4247 }
4248 module_exit(snd_soc_exit);
4249
4250 /* Module information */
4251 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
4252 MODULE_DESCRIPTION("ALSA SoC Core");
4253 MODULE_LICENSE("GPL");
4254 MODULE_ALIAS("platform:soc-audio");