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