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