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