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