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