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