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