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