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