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