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