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