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