1
2
3
4
5
6
7
8
9#include <sound/soc.h>
10#include <sound/soc-dai.h>
11#include <sound/soc-link.h>
12
13#define soc_dai_ret(dai, ret) _soc_dai_ret(dai, __func__, ret)
14static inline int _soc_dai_ret(struct snd_soc_dai *dai,
15 const char *func, int ret)
16{
17
18 if (ret >= 0)
19 return ret;
20
21
22 switch (ret) {
23 case -EPROBE_DEFER:
24 case -ENOTSUPP:
25 break;
26 default:
27 dev_err(dai->dev,
28 "ASoC: error at %s on %s: %d\n",
29 func, dai->name, ret);
30 }
31
32 return ret;
33}
34
35
36
37
38
39#define soc_dai_mark_push(dai, substream, tgt) ((dai)->mark_##tgt = substream)
40#define soc_dai_mark_pop(dai, substream, tgt) ((dai)->mark_##tgt = NULL)
41#define soc_dai_mark_match(dai, substream, tgt) ((dai)->mark_##tgt == substream)
42
43
44
45
46
47
48
49
50
51
52int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
53 unsigned int freq, int dir)
54{
55 int ret;
56
57 if (dai->driver->ops &&
58 dai->driver->ops->set_sysclk)
59 ret = dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
60 else
61 ret = snd_soc_component_set_sysclk(dai->component, clk_id, 0,
62 freq, dir);
63
64 return soc_dai_ret(dai, ret);
65}
66EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
67
68
69
70
71
72
73
74
75
76
77
78int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
79 int div_id, int div)
80{
81 int ret = -EINVAL;
82
83 if (dai->driver->ops &&
84 dai->driver->ops->set_clkdiv)
85 ret = dai->driver->ops->set_clkdiv(dai, div_id, div);
86
87 return soc_dai_ret(dai, ret);
88}
89EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
90
91
92
93
94
95
96
97
98
99
100
101int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
102 unsigned int freq_in, unsigned int freq_out)
103{
104 int ret;
105
106 if (dai->driver->ops &&
107 dai->driver->ops->set_pll)
108 ret = dai->driver->ops->set_pll(dai, pll_id, source,
109 freq_in, freq_out);
110 else
111 ret = snd_soc_component_set_pll(dai->component, pll_id, source,
112 freq_in, freq_out);
113
114 return soc_dai_ret(dai, ret);
115}
116EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
117
118
119
120
121
122
123
124
125int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
126{
127 int ret = -EINVAL;
128
129 if (dai->driver->ops &&
130 dai->driver->ops->set_bclk_ratio)
131 ret = dai->driver->ops->set_bclk_ratio(dai, ratio);
132
133 return soc_dai_ret(dai, ret);
134}
135EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
136
137int snd_soc_dai_get_fmt_max_priority(struct snd_soc_pcm_runtime *rtd)
138{
139 struct snd_soc_dai *dai;
140 int i, max = 0;
141
142
143
144
145 for_each_rtd_dais(rtd, i, dai) {
146 if (dai->driver->ops &&
147 dai->driver->ops->num_auto_selectable_formats)
148 max = max(max, dai->driver->ops->num_auto_selectable_formats);
149 else
150 return 0;
151 }
152
153 return max;
154}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169u64 snd_soc_dai_get_fmt(struct snd_soc_dai *dai, int priority)
170{
171 const struct snd_soc_dai_ops *ops = dai->driver->ops;
172 u64 fmt = 0;
173 int i, max = 0, until = priority;
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 if (ops)
189 max = ops->num_auto_selectable_formats;
190
191 if (max < until)
192 until = max;
193
194 for (i = 0; i < until; i++)
195 fmt |= ops->auto_selectable_formats[i];
196
197 return fmt;
198}
199
200
201
202
203
204
205
206
207int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
208{
209 int ret = -ENOTSUPP;
210
211 if (dai->driver->ops &&
212 dai->driver->ops->set_fmt)
213 ret = dai->driver->ops->set_fmt(dai, fmt);
214
215 return soc_dai_ret(dai, ret);
216}
217EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
218
219
220
221
222
223
224
225
226
227static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
228 unsigned int *tx_mask,
229 unsigned int *rx_mask)
230{
231 if (*tx_mask || *rx_mask)
232 return 0;
233
234 if (!slots)
235 return -EINVAL;
236
237 *tx_mask = (1 << slots) - 1;
238 *rx_mask = (1 << slots) - 1;
239
240 return 0;
241}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
267 unsigned int tx_mask, unsigned int rx_mask,
268 int slots, int slot_width)
269{
270 int ret = -ENOTSUPP;
271
272 if (dai->driver->ops &&
273 dai->driver->ops->xlate_tdm_slot_mask)
274 dai->driver->ops->xlate_tdm_slot_mask(slots,
275 &tx_mask, &rx_mask);
276 else
277 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
278
279 dai->tx_mask = tx_mask;
280 dai->rx_mask = rx_mask;
281
282 if (dai->driver->ops &&
283 dai->driver->ops->set_tdm_slot)
284 ret = dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
285 slots, slot_width);
286 return soc_dai_ret(dai, ret);
287}
288EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
289
290
291
292
293
294
295
296
297
298
299
300
301
302int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
303 unsigned int tx_num, unsigned int *tx_slot,
304 unsigned int rx_num, unsigned int *rx_slot)
305{
306 int ret = -ENOTSUPP;
307
308 if (dai->driver->ops &&
309 dai->driver->ops->set_channel_map)
310 ret = dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
311 rx_num, rx_slot);
312 return soc_dai_ret(dai, ret);
313}
314EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
315
316
317
318
319
320
321
322
323
324
325
326int snd_soc_dai_get_channel_map(struct snd_soc_dai *dai,
327 unsigned int *tx_num, unsigned int *tx_slot,
328 unsigned int *rx_num, unsigned int *rx_slot)
329{
330 int ret = -ENOTSUPP;
331
332 if (dai->driver->ops &&
333 dai->driver->ops->get_channel_map)
334 ret = dai->driver->ops->get_channel_map(dai, tx_num, tx_slot,
335 rx_num, rx_slot);
336 return soc_dai_ret(dai, ret);
337}
338EXPORT_SYMBOL_GPL(snd_soc_dai_get_channel_map);
339
340
341
342
343
344
345
346
347int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
348{
349 int ret = -EINVAL;
350
351 if (dai->driver->ops &&
352 dai->driver->ops->set_tristate)
353 ret = dai->driver->ops->set_tristate(dai, tristate);
354
355 return soc_dai_ret(dai, ret);
356}
357EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
358
359
360
361
362
363
364
365
366
367int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
368 int direction)
369{
370 int ret = -ENOTSUPP;
371
372
373
374
375
376 if (dai->driver->ops &&
377 dai->driver->ops->mute_stream &&
378 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
379 !dai->driver->ops->no_capture_mute))
380 ret = dai->driver->ops->mute_stream(dai, mute, direction);
381
382 return soc_dai_ret(dai, ret);
383}
384EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
385
386int snd_soc_dai_hw_params(struct snd_soc_dai *dai,
387 struct snd_pcm_substream *substream,
388 struct snd_pcm_hw_params *params)
389{
390 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
391 int ret = 0;
392
393 if (dai->driver->ops &&
394 dai->driver->ops->hw_params) {
395
396 ret = snd_soc_link_be_hw_params_fixup(rtd, params);
397 if (ret < 0)
398 goto end;
399
400 ret = dai->driver->ops->hw_params(substream, params, dai);
401 }
402
403
404 if (ret == 0)
405 soc_dai_mark_push(dai, substream, hw_params);
406end:
407 return soc_dai_ret(dai, ret);
408}
409
410void snd_soc_dai_hw_free(struct snd_soc_dai *dai,
411 struct snd_pcm_substream *substream,
412 int rollback)
413{
414 if (rollback && !soc_dai_mark_match(dai, substream, hw_params))
415 return;
416
417 if (dai->driver->ops &&
418 dai->driver->ops->hw_free)
419 dai->driver->ops->hw_free(substream, dai);
420
421
422 soc_dai_mark_pop(dai, substream, hw_params);
423}
424
425int snd_soc_dai_startup(struct snd_soc_dai *dai,
426 struct snd_pcm_substream *substream)
427{
428 int ret = 0;
429
430 if (dai->driver->ops &&
431 dai->driver->ops->startup)
432 ret = dai->driver->ops->startup(substream, dai);
433
434
435 if (ret == 0)
436 soc_dai_mark_push(dai, substream, startup);
437
438 return soc_dai_ret(dai, ret);
439}
440
441void snd_soc_dai_shutdown(struct snd_soc_dai *dai,
442 struct snd_pcm_substream *substream,
443 int rollback)
444{
445 if (rollback && !soc_dai_mark_match(dai, substream, startup))
446 return;
447
448 if (dai->driver->ops &&
449 dai->driver->ops->shutdown)
450 dai->driver->ops->shutdown(substream, dai);
451
452
453 soc_dai_mark_pop(dai, substream, startup);
454}
455
456snd_pcm_sframes_t snd_soc_dai_delay(struct snd_soc_dai *dai,
457 struct snd_pcm_substream *substream)
458{
459 int delay = 0;
460
461 if (dai->driver->ops &&
462 dai->driver->ops->delay)
463 delay = dai->driver->ops->delay(substream, dai);
464
465 return delay;
466}
467
468int snd_soc_dai_compress_new(struct snd_soc_dai *dai,
469 struct snd_soc_pcm_runtime *rtd, int num)
470{
471 int ret = -ENOTSUPP;
472 if (dai->driver->compress_new)
473 ret = dai->driver->compress_new(rtd, num);
474 return soc_dai_ret(dai, ret);
475}
476
477
478
479
480
481
482bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int dir)
483{
484 struct snd_soc_pcm_stream *stream = snd_soc_dai_get_pcm_stream(dai, dir);
485
486
487 return stream->channels_min;
488}
489
490
491
492
493void snd_soc_dai_link_set_capabilities(struct snd_soc_dai_link *dai_link)
494{
495 struct snd_soc_dai_link_component *cpu;
496 struct snd_soc_dai_link_component *codec;
497 struct snd_soc_dai *dai;
498 bool supported[SNDRV_PCM_STREAM_LAST + 1];
499 bool supported_cpu;
500 bool supported_codec;
501 int direction;
502 int i;
503
504 for_each_pcm_streams(direction) {
505 supported_cpu = false;
506 supported_codec = false;
507
508 for_each_link_cpus(dai_link, i, cpu) {
509 dai = snd_soc_find_dai_with_mutex(cpu);
510 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
511 supported_cpu = true;
512 break;
513 }
514 }
515 for_each_link_codecs(dai_link, i, codec) {
516 dai = snd_soc_find_dai_with_mutex(codec);
517 if (dai && snd_soc_dai_stream_valid(dai, direction)) {
518 supported_codec = true;
519 break;
520 }
521 }
522 supported[direction] = supported_cpu && supported_codec;
523 }
524
525 dai_link->dpcm_playback = supported[SNDRV_PCM_STREAM_PLAYBACK];
526 dai_link->dpcm_capture = supported[SNDRV_PCM_STREAM_CAPTURE];
527}
528EXPORT_SYMBOL_GPL(snd_soc_dai_link_set_capabilities);
529
530void snd_soc_dai_action(struct snd_soc_dai *dai,
531 int stream, int action)
532{
533
534 dai->stream_active[stream] += action;
535
536
537 dai->component->active += action;
538}
539EXPORT_SYMBOL_GPL(snd_soc_dai_action);
540
541int snd_soc_dai_active(struct snd_soc_dai *dai)
542{
543 int stream, active;
544
545 active = 0;
546 for_each_pcm_streams(stream)
547 active += dai->stream_active[stream];
548
549 return active;
550}
551EXPORT_SYMBOL_GPL(snd_soc_dai_active);
552
553int snd_soc_pcm_dai_probe(struct snd_soc_pcm_runtime *rtd, int order)
554{
555 struct snd_soc_dai *dai;
556 int i;
557
558 for_each_rtd_dais(rtd, i, dai) {
559 if (dai->driver->probe_order != order)
560 continue;
561
562 if (dai->driver->probe) {
563 int ret = dai->driver->probe(dai);
564
565 if (ret < 0)
566 return soc_dai_ret(dai, ret);
567 }
568
569 dai->probed = 1;
570 }
571
572 return 0;
573}
574
575int snd_soc_pcm_dai_remove(struct snd_soc_pcm_runtime *rtd, int order)
576{
577 struct snd_soc_dai *dai;
578 int i, r, ret = 0;
579
580 for_each_rtd_dais(rtd, i, dai) {
581 if (dai->driver->remove_order != order)
582 continue;
583
584 if (dai->probed &&
585 dai->driver->remove) {
586 r = dai->driver->remove(dai);
587 if (r < 0)
588 ret = r;
589 }
590
591 dai->probed = 0;
592 }
593
594 return ret;
595}
596
597int snd_soc_pcm_dai_new(struct snd_soc_pcm_runtime *rtd)
598{
599 struct snd_soc_dai *dai;
600 int i, ret = 0;
601
602 for_each_rtd_dais(rtd, i, dai) {
603 if (dai->driver->pcm_new) {
604 ret = dai->driver->pcm_new(rtd, dai);
605 if (ret < 0)
606 return soc_dai_ret(dai, ret);
607 }
608 }
609
610 return 0;
611}
612
613int snd_soc_pcm_dai_prepare(struct snd_pcm_substream *substream)
614{
615 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
616 struct snd_soc_dai *dai;
617 int i, ret;
618
619 for_each_rtd_dais(rtd, i, dai) {
620 if (dai->driver->ops &&
621 dai->driver->ops->prepare) {
622 ret = dai->driver->ops->prepare(substream, dai);
623 if (ret < 0)
624 return soc_dai_ret(dai, ret);
625 }
626 }
627
628 return 0;
629}
630
631static int soc_dai_trigger(struct snd_soc_dai *dai,
632 struct snd_pcm_substream *substream, int cmd)
633{
634 int ret = 0;
635
636 if (dai->driver->ops &&
637 dai->driver->ops->trigger)
638 ret = dai->driver->ops->trigger(substream, cmd, dai);
639
640 return soc_dai_ret(dai, ret);
641}
642
643int snd_soc_pcm_dai_trigger(struct snd_pcm_substream *substream,
644 int cmd, int rollback)
645{
646 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
647 struct snd_soc_dai *dai;
648 int i, r, ret = 0;
649
650 switch (cmd) {
651 case SNDRV_PCM_TRIGGER_START:
652 case SNDRV_PCM_TRIGGER_RESUME:
653 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
654 for_each_rtd_dais(rtd, i, dai) {
655 ret = soc_dai_trigger(dai, substream, cmd);
656 if (ret < 0)
657 break;
658 soc_dai_mark_push(dai, substream, trigger);
659 }
660 break;
661 case SNDRV_PCM_TRIGGER_STOP:
662 case SNDRV_PCM_TRIGGER_SUSPEND:
663 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
664 for_each_rtd_dais(rtd, i, dai) {
665 if (rollback && !soc_dai_mark_match(dai, substream, trigger))
666 continue;
667
668 r = soc_dai_trigger(dai, substream, cmd);
669 if (r < 0)
670 ret = r;
671 soc_dai_mark_pop(dai, substream, trigger);
672 }
673 }
674
675 return ret;
676}
677
678int snd_soc_pcm_dai_bespoke_trigger(struct snd_pcm_substream *substream,
679 int cmd)
680{
681 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
682 struct snd_soc_dai *dai;
683 int i, ret;
684
685 for_each_rtd_dais(rtd, i, dai) {
686 if (dai->driver->ops &&
687 dai->driver->ops->bespoke_trigger) {
688 ret = dai->driver->ops->bespoke_trigger(substream,
689 cmd, dai);
690 if (ret < 0)
691 return soc_dai_ret(dai, ret);
692 }
693 }
694
695 return 0;
696}
697
698int snd_soc_dai_compr_startup(struct snd_soc_dai *dai,
699 struct snd_compr_stream *cstream)
700{
701 int ret = 0;
702
703 if (dai->driver->cops &&
704 dai->driver->cops->startup)
705 ret = dai->driver->cops->startup(cstream, dai);
706
707
708 if (ret == 0)
709 soc_dai_mark_push(dai, cstream, compr_startup);
710
711 return soc_dai_ret(dai, ret);
712}
713EXPORT_SYMBOL_GPL(snd_soc_dai_compr_startup);
714
715void snd_soc_dai_compr_shutdown(struct snd_soc_dai *dai,
716 struct snd_compr_stream *cstream,
717 int rollback)
718{
719 if (rollback && !soc_dai_mark_match(dai, cstream, compr_startup))
720 return;
721
722 if (dai->driver->cops &&
723 dai->driver->cops->shutdown)
724 dai->driver->cops->shutdown(cstream, dai);
725
726
727 soc_dai_mark_pop(dai, cstream, compr_startup);
728}
729EXPORT_SYMBOL_GPL(snd_soc_dai_compr_shutdown);
730
731int snd_soc_dai_compr_trigger(struct snd_soc_dai *dai,
732 struct snd_compr_stream *cstream, int cmd)
733{
734 int ret = 0;
735
736 if (dai->driver->cops &&
737 dai->driver->cops->trigger)
738 ret = dai->driver->cops->trigger(cstream, cmd, dai);
739
740 return soc_dai_ret(dai, ret);
741}
742EXPORT_SYMBOL_GPL(snd_soc_dai_compr_trigger);
743
744int snd_soc_dai_compr_set_params(struct snd_soc_dai *dai,
745 struct snd_compr_stream *cstream,
746 struct snd_compr_params *params)
747{
748 int ret = 0;
749
750 if (dai->driver->cops &&
751 dai->driver->cops->set_params)
752 ret = dai->driver->cops->set_params(cstream, params, dai);
753
754 return soc_dai_ret(dai, ret);
755}
756EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_params);
757
758int snd_soc_dai_compr_get_params(struct snd_soc_dai *dai,
759 struct snd_compr_stream *cstream,
760 struct snd_codec *params)
761{
762 int ret = 0;
763
764 if (dai->driver->cops &&
765 dai->driver->cops->get_params)
766 ret = dai->driver->cops->get_params(cstream, params, dai);
767
768 return soc_dai_ret(dai, ret);
769}
770EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_params);
771
772int snd_soc_dai_compr_ack(struct snd_soc_dai *dai,
773 struct snd_compr_stream *cstream,
774 size_t bytes)
775{
776 int ret = 0;
777
778 if (dai->driver->cops &&
779 dai->driver->cops->ack)
780 ret = dai->driver->cops->ack(cstream, bytes, dai);
781
782 return soc_dai_ret(dai, ret);
783}
784EXPORT_SYMBOL_GPL(snd_soc_dai_compr_ack);
785
786int snd_soc_dai_compr_pointer(struct snd_soc_dai *dai,
787 struct snd_compr_stream *cstream,
788 struct snd_compr_tstamp *tstamp)
789{
790 int ret = 0;
791
792 if (dai->driver->cops &&
793 dai->driver->cops->pointer)
794 ret = dai->driver->cops->pointer(cstream, tstamp, dai);
795
796 return soc_dai_ret(dai, ret);
797}
798EXPORT_SYMBOL_GPL(snd_soc_dai_compr_pointer);
799
800int snd_soc_dai_compr_set_metadata(struct snd_soc_dai *dai,
801 struct snd_compr_stream *cstream,
802 struct snd_compr_metadata *metadata)
803{
804 int ret = 0;
805
806 if (dai->driver->cops &&
807 dai->driver->cops->set_metadata)
808 ret = dai->driver->cops->set_metadata(cstream, metadata, dai);
809
810 return soc_dai_ret(dai, ret);
811}
812EXPORT_SYMBOL_GPL(snd_soc_dai_compr_set_metadata);
813
814int snd_soc_dai_compr_get_metadata(struct snd_soc_dai *dai,
815 struct snd_compr_stream *cstream,
816 struct snd_compr_metadata *metadata)
817{
818 int ret = 0;
819
820 if (dai->driver->cops &&
821 dai->driver->cops->get_metadata)
822 ret = dai->driver->cops->get_metadata(cstream, metadata, dai);
823
824 return soc_dai_ret(dai, ret);
825}
826EXPORT_SYMBOL_GPL(snd_soc_dai_compr_get_metadata);
827