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