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