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