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