Repository navigation
Expand file tree
/
Copy pathdai.c
More file actions
1065 lines (881 loc) · 28.4 KB
/
Copy pathdai.c
File metadata and controls
1065 lines (881 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2016 Intel Corporation. All rights reserved.
//
// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
// Keyon Jie <yang.jie@linux.intel.com>
#include <sof/audio/buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/audio/format.h>
#include <sof/audio/pipeline.h>
#include <sof/common.h>
#include <sof/debug/panic.h>
#include <sof/drivers/interrupt.h>
#include <sof/ipc/msg.h>
#include <sof/drivers/interrupt.h>
#include <sof/drivers/timer.h>
#include <sof/lib/alloc.h>
#include <sof/lib/cache.h>
#include <sof/lib/dai.h>
#include <sof/lib/memory.h>
#include <sof/lib/notifier.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/string.h>
#include <sof/ut.h>
#include <sof/trace/trace.h>
#include <ipc/dai.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
static const struct comp_driver comp_dai;
LOG_MODULE_REGISTER(dai_comp, CONFIG_SOF_LOG_LEVEL);
/* c2b00d27-ffbc-4150-a51a-245c79c5e54b */
DECLARE_SOF_RT_UUID("dai", dai_comp_uuid, 0xc2b00d27, 0xffbc, 0x4150,
0xa5, 0x1a, 0x24, 0x5c, 0x79, 0xc5, 0xe5, 0x4b);
DECLARE_TR_CTX(dai_comp_tr, SOF_UUID(dai_comp_uuid), LOG_LEVEL_INFO);
static void dai_atomic_trigger(void *arg, enum notify_id type, void *data);
/* Assign DAI to a group */
int dai_assign_group(struct comp_dev *dev, uint32_t group_id)
{
struct dai_data *dd = comp_get_drvdata(dev);
if (dd->group) {
if (dd->group->group_id != group_id) {
comp_err(dev, "dai_assign_group(), DAI already in group %d, requested %d",
dd->group->group_id, group_id);
return -EINVAL;
}
/* No need to re-assign to the same group, do nothing */
return 0;
}
dd->group = dai_group_get(group_id, DAI_CREAT);
if (!dd->group) {
comp_err(dev, "dai_assign_group(), failed to assign group %d",
group_id);
return -EINVAL;
}
comp_dbg(dev, "dai_assign_group(), group %d num %d",
group_id, dd->group->num_dais);
/* Register for the atomic trigger event */
notifier_register(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER,
dai_atomic_trigger, 0);
return 0;
}
/* this is called by DMA driver every time descriptor has completed */
static void dai_dma_cb(void *arg, enum notify_id type, void *data)
{
struct dma_cb_data *next = data;
struct comp_dev *dev = arg;
struct dai_data *dd = comp_get_drvdata(dev);
uint32_t bytes = next->elem.size;
struct comp_buffer *source;
struct comp_buffer *sink;
void *buffer_ptr;
int ret;
comp_dbg(dev, "dai_dma_cb()");
next->status = DMA_CB_STATUS_RELOAD;
/* stop dma copy for pause/stop/xrun */
if (dev->state != COMP_STATE_ACTIVE || dd->xrun) {
/* stop the DAI */
dai_trigger(dd->dai, COMP_TRIGGER_STOP, dev->direction);
/* tell DMA not to reload */
next->status = DMA_CB_STATUS_END;
}
/* is our pipeline handling an XRUN ? */
if (dd->xrun) {
/* make sure we only playback silence during an XRUN */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
/* fill buffer with silence */
buffer_zero(dd->dma_buffer);
return;
}
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
ret = dma_buffer_copy_to(dd->local_buffer, dd->dma_buffer,
dd->process, bytes);
buffer_ptr = dd->local_buffer->stream.r_ptr;
} else {
ret = dma_buffer_copy_from(dd->dma_buffer, dd->local_buffer,
dd->process, bytes);
buffer_ptr = dd->local_buffer->stream.w_ptr;
}
/* assert dma_buffer_copy succeed */
if (ret < 0) {
source = dev->direction == SOF_IPC_STREAM_PLAYBACK ?
dd->local_buffer : dd->dma_buffer;
sink = dev->direction == SOF_IPC_STREAM_PLAYBACK ?
dd->dma_buffer : dd->local_buffer;
comp_err(dev, "dai_dma_cb() dma buffer copy failed, dir %d bytes %d avail %d free %d",
dev->direction, bytes,
audio_stream_get_avail_samples(&source->stream) *
audio_stream_frame_bytes(&source->stream),
audio_stream_get_free_samples(&sink->stream) *
audio_stream_frame_bytes(&sink->stream));
return;
}
/* update host position (in bytes offset) for drivers */
dev->position += bytes;
if (dd->dai_pos) {
dd->dai_pos_blks += bytes;
*dd->dai_pos = dd->dai_pos_blks +
(char *)buffer_ptr -
(char *)dd->dma_buffer->stream.addr;
}
}
static struct comp_dev *dai_new(const struct comp_driver *drv,
struct comp_ipc_config *config,
void *spec)
{
struct comp_dev *dev;
struct ipc_config_dai *dai = spec;
struct dai_data *dd;
uint32_t dir, caps, dma_dev;
comp_cl_dbg(&comp_dai, "dai_new()");
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
dd = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, sizeof(*dd));
if (!dd) {
rfree(dev);
return NULL;
}
comp_set_drvdata(dev, dd);
dd->dai = dai_get(dai->type, dai->dai_index, DAI_CREAT);
if (!dd->dai) {
comp_cl_err(&comp_dai, "dai_new(): dai_get() failed to create DAI.");
goto error;
}
dd->ipc_config = *dai;
/* request GP LP DMA with shared access privilege */
dir = dai->direction == SOF_IPC_STREAM_PLAYBACK ?
DMA_DIR_MEM_TO_DEV : DMA_DIR_DEV_TO_MEM;
caps = dai_get_info(dd->dai, DAI_INFO_DMA_CAPS);
dma_dev = dai_get_info(dd->dai, DAI_INFO_DMA_DEV);
dd->dma = dma_get(dir, caps, dma_dev, DMA_ACCESS_SHARED);
if (!dd->dma) {
comp_cl_err(&comp_dai, "dai_new(): dma_get() failed to get shared access to DMA.");
goto error;
}
dma_sg_init(&dd->config.elem_array);
dd->dai_pos = NULL;
dd->dai_pos_blks = 0;
dd->xrun = 0;
dd->chan = NULL;
dev->state = COMP_STATE_READY;
return dev;
error:
rfree(dd);
rfree(dev);
return NULL;
}
static void dai_free(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
if (dd->group) {
notifier_unregister(dev, dd->group, NOTIFIER_ID_DAI_TRIGGER);
dai_group_put(dd->group);
}
if (dd->chan) {
notifier_unregister(dev, dd->chan, NOTIFIER_ID_DMA_COPY);
dma_channel_put(dd->chan);
dd->chan->dev_data = NULL;
}
dma_put(dd->dma);
dai_put(dd->dai);
if (dd->dai_spec_config)
rfree(dd->dai_spec_config);
rfree(dd);
rfree(dev);
}
static int dai_comp_get_hw_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params,
int dir)
{
struct dai_data *dd = comp_get_drvdata(dev);
int ret = 0;
comp_dbg(dev, "dai_hw_params()");
/* fetching hw dai stream params */
ret = dai_get_hw_params(dd->dai, params, dir);
if (ret < 0) {
comp_err(dev, "dai_comp_get_hw_params(): dai_get_hw_params failed ret %d",
ret);
return ret;
}
/* dai_comp_get_hw_params() function fetches hardware dai parameters,
* which then are propagating back through the pipeline, so that any
* component can convert specific stream parameter. Here, we overwrite
* frame_fmt hardware parameter as DAI component is able to convert
* stream with different frame_fmt's (using pcm converter)
*/
params->frame_fmt = dev->ipc_config.frame_fmt;
return 0;
}
static int dai_comp_hw_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct dai_data *dd = comp_get_drvdata(dev);
int ret;
comp_dbg(dev, "dai_comp_hw_params()");
/* configure hw dai stream params */
ret = dai_hw_params(dd->dai, params);
if (ret < 0) {
comp_err(dev, "dai_comp_hw_params(): dai_hw_params failed ret %d",
ret);
return ret;
}
return 0;
}
static int dai_verify_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct sof_ipc_stream_params hw_params;
dai_comp_get_hw_params(dev, &hw_params, params->direction);
/* checks whether pcm parameters match hardware DAI parameter set
* during dai_set_config(). If hardware parameter is equal to 0, it
* means that it can vary, so any value is acceptable. We do not check
* format parameter, because DAI is able to change format using
* pcm_converter functions.
*/
if (hw_params.rate && hw_params.rate != params->rate) {
comp_err(dev, "dai_verify_params(): pcm rate parameter %d does not match hardware rate %d",
params->rate, hw_params.rate);
return -EINVAL;
}
if (hw_params.channels && hw_params.channels != params->channels) {
comp_err(dev, "dai_verify_params(): pcm channels parameter %d does not match hardware channels %d",
params->channels, hw_params.channels);
return -EINVAL;
}
/* set component period frames */
component_set_nearest_period_frames(dev, params->rate);
return 0;
}
/* set component audio SSP and DMA configuration */
static int dai_playback_params(struct comp_dev *dev, uint32_t period_bytes,
uint32_t period_count)
{
struct dai_data *dd = comp_get_drvdata(dev);
struct dma_sg_config *config = &dd->config;
uint32_t local_fmt = dd->local_buffer->stream.frame_fmt;
uint32_t dma_fmt = dd->dma_buffer->stream.frame_fmt;
uint32_t fifo;
int err;
/* set processing function */
dd->process = pcm_get_conversion_function(local_fmt, dma_fmt);
if (!dd->process) {
comp_err(dev, "dai_playback_params(): converter function NULL: local fmt %d dma fmt %d\n",
local_fmt, dma_fmt);
return -EINVAL;
}
/* set up DMA configuration */
config->direction = DMA_DIR_MEM_TO_DEV;
config->src_width = get_sample_bytes(dma_fmt);
config->dest_width = config->src_width;
config->cyclic = 1;
config->irq_disabled = pipeline_is_timer_driven(dev->pipeline);
config->dest_dev = dai_get_handshake(dd->dai, dev->direction,
dd->stream_id);
config->is_scheduling_source = comp_is_scheduling_source(dev);
config->period = dev->pipeline->period;
comp_info(dev, "dai_playback_params() dest_dev = %d stream_id = %d src_width = %d dest_width = %d",
config->dest_dev, dd->stream_id,
config->src_width, config->dest_width);
if (!config->elem_array.elems) {
fifo = dai_get_fifo(dd->dai, dev->direction,
dd->stream_id);
comp_info(dev, "dai_playback_params() fifo 0x%x", fifo);
err = dma_sg_alloc(&config->elem_array, SOF_MEM_ZONE_RUNTIME,
config->direction,
period_count,
period_bytes,
(uintptr_t)(dd->dma_buffer->stream.addr),
fifo);
if (err < 0) {
comp_err(dev, "dai_playback_params(): dma_sg_alloc() for period_count %d period_bytes %d failed with err = %d",
period_count, period_bytes, err);
return err;
}
}
return 0;
}
static int dai_capture_params(struct comp_dev *dev, uint32_t period_bytes,
uint32_t period_count)
{
struct dai_data *dd = comp_get_drvdata(dev);
struct dma_sg_config *config = &dd->config;
uint32_t local_fmt = dd->local_buffer->stream.frame_fmt;
uint32_t dma_fmt = dd->dma_buffer->stream.frame_fmt;
uint32_t fifo;
int err;
/* set processing function */
dd->process = pcm_get_conversion_function(dma_fmt, local_fmt);
if (!dd->process) {
comp_err(dev, "dai_capture_params(): converter function NULL: local fmt %d dma fmt %d\n",
local_fmt, dma_fmt);
return -EINVAL;
}
/* set up DMA configuration */
config->direction = DMA_DIR_DEV_TO_MEM;
config->cyclic = 1;
config->irq_disabled = pipeline_is_timer_driven(dev->pipeline);
config->src_dev = dai_get_handshake(dd->dai, dev->direction,
dd->stream_id);
config->is_scheduling_source = comp_is_scheduling_source(dev);
config->period = dev->pipeline->period;
/* TODO: Make this code platform-specific or move it driver callback */
if (dai_get_info(dd->dai, DAI_INFO_TYPE) == SOF_DAI_INTEL_DMIC) {
/* For DMIC the DMA src and dest widths should always be 4 bytes
* due to 32 bit FIFO packer. Setting width to 2 bytes for
* 16 bit format would result in recording at double rate.
*/
config->src_width = 4;
config->dest_width = 4;
} else {
config->src_width = get_sample_bytes(dma_fmt);
config->dest_width = config->src_width;
}
comp_info(dev, "dai_capture_params() src_dev = %d stream_id = %d src_width = %d dest_width = %d",
config->src_dev, dd->stream_id,
config->src_width, config->dest_width);
if (!config->elem_array.elems) {
fifo = dai_get_fifo(dd->dai, dev->direction,
dd->stream_id);
comp_info(dev, "dai_capture_params() fifo 0x%x", fifo);
err = dma_sg_alloc(&config->elem_array, SOF_MEM_ZONE_RUNTIME,
config->direction,
period_count,
period_bytes,
(uintptr_t)(dd->dma_buffer->stream.addr),
fifo);
if (err < 0) {
comp_err(dev, "dai_capture_params(): dma_sg_alloc() for period_count %d period_bytes %d failed with err = %d",
period_count, period_bytes, err);
return err;
}
}
return 0;
}
static int dai_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct sof_ipc_stream_params hw_params = *params;
struct dai_data *dd = comp_get_drvdata(dev);
uint32_t frame_size;
uint32_t period_count;
uint32_t period_bytes;
uint32_t buffer_size;
uint32_t addr_align;
uint32_t align;
int err;
comp_dbg(dev, "dai_params()");
/* configure dai_data first */
err = ipc_dai_data_config(dev);
if (err < 0)
return err;
err = dai_verify_params(dev, params);
if (err < 0) {
comp_err(dev, "dai_params(): pcm params verification failed.");
return -EINVAL;
}
/* params verification passed, so now configure hw dai stream params */
err = dai_comp_hw_params(dev, params);
if (err < 0) {
comp_err(dev, "dai_params(): dai_comp_hw_params failed err %d", err);
return err;
}
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
dd->local_buffer = list_first_item(&dev->bsource_list,
struct comp_buffer,
sink_list);
else
dd->local_buffer = list_first_item(&dev->bsink_list,
struct comp_buffer,
source_list);
/* check if already configured */
if (dev->state == COMP_STATE_PREPARE) {
comp_info(dev, "dai_params() component has been already configured.");
return 0;
}
/* can set params on only init state */
if (dev->state != COMP_STATE_READY) {
comp_err(dev, "dai_params(): Component is in state %d, expected COMP_STATE_READY.",
dev->state);
return -EINVAL;
}
err = dma_get_attribute(dd->dma, DMA_ATTR_BUFFER_ADDRESS_ALIGNMENT,
&addr_align);
if (err < 0) {
comp_err(dev, "dai_params(): could not get dma buffer address alignment, err = %d",
err);
return err;
}
err = dma_get_attribute(dd->dma, DMA_ATTR_BUFFER_ALIGNMENT, &align);
if (err < 0 || !align) {
comp_err(dev, "dai_params(): could not get valid dma buffer alignment, err = %d, align = %u",
err, align);
return -EINVAL;
}
err = dma_get_attribute(dd->dma, DMA_ATTR_BUFFER_PERIOD_COUNT,
&period_count);
if (err < 0 || !period_count) {
comp_err(dev, "dai_params(): could not get valid dma buffer period count, err = %d, period_count = %u",
err, period_count);
return -EINVAL;
}
/* calculate frame size */
frame_size = get_frame_bytes(dev->ipc_config.frame_fmt,
dd->local_buffer->stream.channels);
/* calculate period size */
period_bytes = dev->frames * frame_size;
if (!period_bytes) {
comp_err(dev, "dai_params(): invalid period_bytes.");
return -EINVAL;
}
dd->period_bytes = period_bytes;
/* calculate DMA buffer size */
buffer_size = ALIGN_UP(period_count * period_bytes, align);
/* alloc DMA buffer or change its size if exists */
if (dd->dma_buffer) {
err = buffer_set_size(dd->dma_buffer, buffer_size);
if (err < 0) {
comp_err(dev, "dai_params(): buffer_set_size() failed, buffer_size = %u",
buffer_size);
return err;
}
} else {
dd->dma_buffer = buffer_alloc(buffer_size, SOF_MEM_CAPS_DMA,
addr_align);
if (!dd->dma_buffer) {
comp_err(dev, "dai_params(): failed to alloc dma buffer");
return -ENOMEM;
}
/*
* dma_buffer should reffer to hardware dai parameters.
* Here, we overwrite frame_fmt hardware parameter as DAI
* component is able to convert stream with different
* frame_fmt's (using pcm converter).
*/
hw_params.frame_fmt = dev->ipc_config.frame_fmt;
buffer_set_params(dd->dma_buffer, &hw_params,
BUFFER_UPDATE_FORCE);
}
return dev->direction == SOF_IPC_STREAM_PLAYBACK ?
dai_playback_params(dev, period_bytes, period_count) :
dai_capture_params(dev, period_bytes, period_count);
}
static int dai_config_prepare(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
int channel = 0;
/* cannot configure DAI while active */
if (dev->state == COMP_STATE_ACTIVE) {
comp_info(dev, "dai_config_prepare(): Component is in active state.");
return 0;
}
if (!dd->dai_spec_config) {
comp_err(dev, "dai specific config is not set yet!");
return -EINVAL;
}
if (dd->chan) {
comp_info(dev, "dai_config_prepare(): dma channel index %d already configured",
dd->chan->index);
return 0;
}
channel = dai_config_dma_channel(dev, dd->dai_spec_config);
comp_info(dev, "dai_config_prepare(), channel = %d", channel);
/* do nothing for asking for channel free, for compatibility. */
if (channel == DMA_CHAN_INVALID) {
comp_err(dev, "dai_config is not set yet!");
return -EINVAL;
}
/* allocate DMA channel */
dd->chan = dma_channel_get(dd->dma, channel);
if (!dd->chan) {
comp_err(dev, "dai_config_prepare(): dma_channel_get() failed");
dd->chan = NULL;
return -EIO;
}
dd->chan->dev_data = dd;
comp_info(dev, "dai_config_prepare(): new configured dma channel index %d",
dd->chan->index);
/* setup callback */
notifier_register(dev, dd->chan, NOTIFIER_ID_DMA_COPY,
dai_dma_cb, 0);
return 0;
}
static int dai_prepare(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
int ret = 0;
comp_info(dev, "dai_prepare()");
ret = dai_config_prepare(dev);
if (ret < 0)
return ret;
ret = comp_set_state(dev, COMP_TRIGGER_PREPARE);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
dev->position = 0;
if (!dd->chan) {
comp_err(dev, "dai_prepare(): Missing dd->chan.");
comp_set_state(dev, COMP_TRIGGER_RESET);
return -EINVAL;
}
if (!dd->config.elem_array.elems) {
comp_err(dev, "dai_prepare(): Missing dd->config.elem_array.elems.");
comp_set_state(dev, COMP_TRIGGER_RESET);
return -EINVAL;
}
/* clear dma buffer to avoid pop noise */
buffer_zero(dd->dma_buffer);
/* dma reconfig not required if XRUN handling */
if (dd->xrun) {
/* after prepare, we have recovered from xrun */
dd->xrun = 0;
return ret;
}
ret = dma_set_config(dd->chan, &dd->config);
if (ret < 0)
comp_set_state(dev, COMP_TRIGGER_RESET);
return ret;
}
static int dai_reset(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
struct dma_sg_config *config = &dd->config;
comp_info(dev, "dai_reset()");
/*
* DMA channel release should be skipped now for DAI's that support the two-step stop option.
* It will be done when the host sends the DAI_CONFIG IPC during hw_free.
*/
if (!dd->delayed_dma_stop)
dai_dma_release(dev);
dma_sg_free(&config->elem_array);
if (dd->dma_buffer) {
buffer_free(dd->dma_buffer);
dd->dma_buffer = NULL;
}
dd->dai_pos_blks = 0;
if (dd->dai_pos)
*dd->dai_pos = 0;
dd->dai_pos = NULL;
dd->wallclock = 0;
dev->position = 0;
dd->xrun = 0;
comp_set_state(dev, COMP_TRIGGER_RESET);
return 0;
}
static void dai_update_start_position(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
/* update starting wallclock */
platform_dai_wallclock(dev, &dd->wallclock);
/* update start position */
dd->start_position = dev->position;
}
/* used to pass standard and bespoke command (with data) to component */
static int dai_comp_trigger_internal(struct comp_dev *dev, int cmd)
{
struct dai_data *dd = comp_get_drvdata(dev);
int ret;
comp_dbg(dev, "dai_comp_trigger_internal(), command = %u", cmd);
ret = comp_set_state(dev, cmd);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
switch (cmd) {
case COMP_TRIGGER_START:
comp_dbg(dev, "dai_comp_trigger_internal(), START");
/* only start the DAI if we are not XRUN handling */
if (dd->xrun == 0) {
ret = dma_start(dd->chan);
if (ret < 0)
return ret;
/* start the DAI */
dai_trigger(dd->dai, cmd, dev->direction);
} else {
dd->xrun = 0;
}
dai_update_start_position(dev);
break;
case COMP_TRIGGER_RELEASE:
/* before release, we clear the buffer data to 0s,
* then there is no history data sent out after release.
* this is only supported at capture mode.
*/
if (dev->direction == SOF_IPC_STREAM_CAPTURE)
buffer_zero(dd->dma_buffer);
/* only start the DAI if we are not XRUN handling */
if (dd->xrun == 0) {
/* recover valid start position */
ret = dma_release(dd->chan);
if (ret < 0)
return ret;
/* start the DAI */
dai_trigger(dd->dai, cmd, dev->direction);
ret = dma_start(dd->chan);
if (ret < 0)
return ret;
} else {
dd->xrun = 0;
}
dai_update_start_position(dev);
break;
case COMP_TRIGGER_XRUN:
comp_info(dev, "dai_comp_trigger_internal(), XRUN");
dd->xrun = 1;
COMPILER_FALLTHROUGH;
case COMP_TRIGGER_STOP:
comp_dbg(dev, "dai_comp_trigger_internal(), STOP");
/*
* Some platforms cannot just simple disable
* DMA channel during the transfer,
* because it will hang the whole DMA controller.
* Therefore, stop the DMA first and let the DAI
* drain the FIFO in order to stop the channel
* as soon as possible.
*/
#if CONFIG_DMA_SUSPEND_DRAIN
ret = dma_stop(dd->chan);
dai_trigger(dd->dai, cmd, dev->direction);
#else
dai_trigger(dd->dai, cmd, dev->direction);
ret = dma_stop(dd->chan);
#endif
break;
case COMP_TRIGGER_PAUSE:
comp_dbg(dev, "dai_comp_trigger_internal(), PAUSE");
ret = dma_pause(dd->chan);
dai_trigger(dd->dai, cmd, dev->direction);
break;
case COMP_TRIGGER_PRE_START:
case COMP_TRIGGER_PRE_RELEASE:
/* only start the DAI if we are not XRUN handling */
if (dd->xrun)
dd->xrun = 0;
else
dai_trigger(dd->dai, cmd, dev->direction);
break;
}
return ret;
}
static int dai_comp_trigger(struct comp_dev *dev, int cmd)
{
struct dai_data *dd = comp_get_drvdata(dev);
struct dai_group *group = dd->group;
uint32_t irq_flags;
int ret = 0;
/* DAI not in a group, use normal trigger */
if (!group) {
comp_dbg(dev, "dai_comp_trigger(), non-atomic trigger");
return dai_comp_trigger_internal(dev, cmd);
}
/* DAI is grouped, so only trigger when the entire group is ready */
if (!group->trigger_counter) {
/* First DAI to receive the trigger command,
* prepare for atomic trigger
*/
comp_dbg(dev, "dai_comp_trigger(), begin atomic trigger for group %d",
group->group_id);
group->trigger_cmd = cmd;
group->trigger_counter = group->num_dais - 1;
} else if (group->trigger_cmd != cmd) {
/* Already processing a different trigger command */
comp_err(dev, "dai_comp_trigger(), already processing atomic trigger");
ret = -EAGAIN;
} else {
/* Count down the number of remaining DAIs required
* to receive the trigger command before atomic trigger
* takes place
*/
group->trigger_counter--;
comp_dbg(dev, "dai_comp_trigger(), trigger counter %d, group %d",
group->trigger_counter, group->group_id);
if (!group->trigger_counter) {
/* The counter has reached 0, which means
* all DAIs have received the same trigger command
* and we may begin the actual trigger process
* synchronously.
*/
irq_local_disable(irq_flags);
notifier_event(group, NOTIFIER_ID_DAI_TRIGGER,
BIT(cpu_get_id()), NULL, 0);
irq_local_enable(irq_flags);
/* return error of last trigger */
ret = group->trigger_ret;
}
}
return ret;
}
static void dai_atomic_trigger(void *arg, enum notify_id type, void *data)
{
struct comp_dev *dev = arg;
struct dai_data *dd = comp_get_drvdata(dev);
struct dai_group *group = dd->group;
/* Atomic context set by the last DAI to receive trigger command */
group->trigger_ret = dai_comp_trigger_internal(dev, group->trigger_cmd);
}
/* report xrun occurrence */
static void dai_report_xrun(struct comp_dev *dev, uint32_t bytes)
{
struct dai_data *dd = comp_get_drvdata(dev);
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
comp_err(dev, "dai_report_xrun(): underrun due to no data available");
comp_underrun(dev, dd->local_buffer, bytes);
} else {
comp_err(dev, "dai_report_xrun(): overrun due to no space available");
comp_overrun(dev, dd->local_buffer, bytes);
}
}
/* copy and process stream data from source to sink buffers */
static int dai_copy(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
uint32_t dma_fmt = dd->dma_buffer->stream.frame_fmt;
const uint32_t sampling = get_sample_bytes(dma_fmt);
struct comp_buffer *buf = dd->local_buffer;
uint32_t avail_bytes = 0;
uint32_t free_bytes = 0;
uint32_t copy_bytes = 0;
uint32_t src_samples;
uint32_t sink_samples;
uint32_t samples;
int ret = 0;
comp_dbg(dev, "dai_copy()");
/* get data sizes from DMA */
ret = dma_get_data_size(dd->chan, &avail_bytes, &free_bytes);
if (ret < 0) {
dai_report_xrun(dev, 0);
return ret;
}
buf = buffer_acquire(buf);
/* calculate minimum size to copy */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
src_samples = audio_stream_get_avail_samples(&buf->stream);
sink_samples = free_bytes / sampling;
samples = MIN(src_samples, sink_samples);
} else {
src_samples = avail_bytes / sampling;
sink_samples = audio_stream_get_free_samples(&buf->stream);
samples = MIN(src_samples, sink_samples);
}
/* limit bytes per copy to one period for the whole pipeline
* in order to avoid high load spike
*/
samples = MIN(samples, dd->period_bytes / sampling);
copy_bytes = samples * sampling;
buffer_release(buf);
comp_dbg(dev, "dai_copy(), dir: %d copy_bytes= 0x%x, frames= %d",
dev->direction, copy_bytes,
samples / buf->stream.channels);
/* Check possibility of glitch occurrence */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK &&
copy_bytes + avail_bytes < dd->period_bytes)
comp_warn(dev, "dai_copy(): Copy_bytes %d + avail bytes %d < period bytes %d, possible glitch",
copy_bytes, avail_bytes, dd->period_bytes);
else if (dev->direction == SOF_IPC_STREAM_CAPTURE &&
copy_bytes + free_bytes < dd->period_bytes)
comp_warn(dev, "dai_copy(): Copy_bytes %d + free bytes %d < period bytes %d, possible glitch",
copy_bytes, free_bytes, dd->period_bytes);
/* return if nothing to copy */
if (!copy_bytes) {
comp_warn(dev, "dai_copy(): nothing to copy");
return 0;
}
ret = dma_copy(dd->chan, copy_bytes, 0);
if (ret < 0) {
dai_report_xrun(dev, copy_bytes);
return ret;
}
dai_dma_position_update(dev);
return ret;
}
/**
* \brief Get DAI parameters and configure timestamping
* \param[in, out] dev DAI device.
* \return Error code.
*
* This function retrieves various DAI parameters such as type, direction, index, and DMA
* controller information those are needed when configuring HW timestamping. Note that
* DAI must be prepared before this function is used (for DMA information). If not, an error
* is returned.
*/
static int dai_ts_config(struct comp_dev *dev)
{
struct dai_data *dd = comp_get_drvdata(dev);
struct timestamp_cfg *cfg = &dd->ts_config;
struct ipc_config_dai *dai = &dd->ipc_config;
comp_dbg(dev, "dai_ts_config()");
if (!dd->chan) {
comp_err(dev, "dai_ts_config(), No DMA channel information");
return -EINVAL;
}
cfg->type = dd->dai->drv->type;
cfg->direction = dai->direction;
cfg->index = dd->dai->index;
cfg->dma_id = dd->dma->plat_data.id;
cfg->dma_chan_index = dd->chan->index;
cfg->dma_chan_count = dd->dma->plat_data.channels;
if (!dd->dai->drv->ts_ops.ts_config)
return -ENXIO;
return dd->dai->drv->ts_ops.ts_config(dd->dai, cfg);
}
static int dai_ts_start(struct comp_dev *dev)