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