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