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