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