forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssp.c
More file actions
1167 lines (954 loc) · 29.7 KB
/
ssp.c
File metadata and controls
1167 lines (954 loc) · 29.7 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>
// Rander Wang <rander.wang@linux.intel.com>
#include <sof/audio/component.h>
#include <sof/common.h>
#include <sof/drivers/mn.h>
#include <sof/drivers/timestamp.h>
#include <sof/drivers/ssp.h>
#include <sof/lib/alloc.h>
#include <sof/lib/clk.h>
#include <sof/lib/dai.h>
#include <sof/lib/dma.h>
#include <sof/lib/memory.h>
#include <sof/lib/pm_runtime.h>
#include <sof/lib/uuid.h>
#include <sof/lib/wait.h>
#include <sof/platform.h>
#include <sof/spinlock.h>
#include <sof/trace/trace.h>
#include <ipc/dai.h>
#include <ipc/dai-intel.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <user/trace.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
/* 31458125-95c4-4085-8f3f-497434cb2daf */
DECLARE_SOF_UUID("ssp-dai", ssp_uuid, 0x31458125, 0x95c4, 0x4085,
0x8f, 0x3f, 0x49, 0x74, 0x34, 0xcb, 0x2d, 0xaf);
DECLARE_TR_CTX(ssp_tr, SOF_UUID(ssp_uuid), LOG_LEVEL_INFO);
/* empty SSP transmit FIFO */
static void ssp_empty_tx_fifo(struct dai *dai)
{
int ret;
uint32_t sssr;
/*
* SSSR_TNF is cleared when TX FIFO is empty or full,
* so wait for set TNF then for TFL zero - order matter.
*/
ret = poll_for_register_delay(dai_base(dai) + SSSR, SSSR_TNF, SSSR_TNF,
SSP_MAX_SEND_TIME_PER_SAMPLE);
ret |= poll_for_register_delay(dai_base(dai) + SSCR3, SSCR3_TFL_MASK, 0,
SSP_MAX_SEND_TIME_PER_SAMPLE *
(SSP_FIFO_DEPTH - 1) / 2);
if (ret)
dai_warn(dai, "ssp_empty_tx_fifo() warning: timeout");
sssr = ssp_read(dai, SSSR);
/* clear interrupt */
if (sssr & SSSR_TUR)
ssp_write(dai, SSSR, sssr);
}
static void ssp_empty_rx_fifo_on_start(struct dai *dai)
{
uint32_t retry = SSP_RX_FLUSH_RETRY_MAX;
uint32_t i, sssr;
sssr = ssp_read(dai, SSSR);
if (sssr & SSSR_ROR) {
/* The RX FIFO is in overflow condition, empty it */
for (i = 0; i < SSP_FIFO_DEPTH; i++)
ssp_read(dai, SSDR);
/* Clear the overflow status */
ssp_update_bits(dai, SSSR, SSSR_ROR, SSSR_ROR);
/* Re-read the SSSR register */
sssr = ssp_read(dai, SSSR);
}
while ((sssr & SSSR_RNE) && retry--) {
uint32_t entries = SSCR3_RFL_VAL(ssp_read(dai, SSCR3));
/* Empty the RX FIFO (the DMA is not running at this point) */
for (i = 0; i < entries + 1; i++)
ssp_read(dai, SSDR);
sssr = ssp_read(dai, SSSR);
}
}
static void ssp_empty_rx_fifo_on_stop(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
uint64_t sample_ticks = clock_ticks_per_sample(PLATFORM_DEFAULT_CLOCK,
ssp->params.fsync_rate);
uint32_t retry = SSP_RX_FLUSH_RETRY_MAX;
uint32_t entries[2];
uint32_t i, sssr;
sssr = ssp_read(dai, SSSR);
entries[0] = SSCR3_RFL_VAL(ssp_read(dai, SSCR3));
while ((sssr & SSSR_RNE) && retry--) {
/* Wait one sample time */
wait_delay(sample_ticks);
entries[1] = SSCR3_RFL_VAL(ssp_read(dai, SSCR3));
sssr = ssp_read(dai, SSSR);
if (entries[0] > entries[1]) {
/*
* The DMA is reading the FIFO, check the status in the
* next loop
*/
entries[0] = entries[1];
} else if (!(sssr & SSSR_RFS)) {
/*
* The DMA request is not asserted, read the FIFO
* directly, otherwise let the next loop iteration to
* check the status
*/
for (i = 0; i < entries[1] + 1; i++)
ssp_read(dai, SSDR);
}
sssr = ssp_read(dai, SSSR);
}
/* Just in case clear the overflow status */
ssp_update_bits(dai, SSSR, SSSR_ROR, SSSR_ROR);
}
/* save SSP context prior to entering D3 */
static int ssp_context_store(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
ssp->sscr0 = ssp_read(dai, SSCR0);
ssp->sscr1 = ssp_read(dai, SSCR1);
/* FIXME: need to store sscr2,3,4,5 */
ssp->psp = ssp_read(dai, SSPSP);
return 0;
}
/* restore SSP context after leaving D3 */
static int ssp_context_restore(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
ssp_write(dai, SSCR0, ssp->sscr0);
ssp_write(dai, SSCR1, ssp->sscr1);
/* FIXME: need to restore sscr2,3,4,5 */
ssp_write(dai, SSPSP, ssp->psp);
return 0;
}
static int ssp_mclk_prepare_enable(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
struct sof_ipc_dai_config *config = &ssp->config;
int ret;
if (ssp->clk_active & SSP_CLK_MCLK_ACTIVE)
return 0;
/* MCLK config */
ret = mn_set_mclk(config->ssp.mclk_id, config->ssp.mclk_rate);
if (ret < 0)
dai_err(dai, "ssp_mclk_prepare_enable(): invalid mclk_rate = %d for mclk_id = %d",
config->ssp.mclk_rate, config->ssp.mclk_id);
else
ssp->clk_active |= SSP_CLK_MCLK_ACTIVE;
return ret;
}
static void ssp_mclk_disable_unprepare(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
if (!(ssp->clk_active & SSP_CLK_MCLK_ACTIVE))
return;
mn_release_mclk(ssp->config.ssp.mclk_id);
ssp->clk_active &= ~SSP_CLK_MCLK_ACTIVE;
}
static int ssp_bclk_prepare_enable(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
struct sof_ipc_dai_config *config = &ssp->config;
uint32_t sscr0;
uint32_t mdiv;
bool need_ecs = false;
int ret = 0;
if (ssp->clk_active & SSP_CLK_BCLK_ACTIVE)
return 0;
sscr0 = ssp_read(dai, SSCR0);
#if CONFIG_INTEL_MN
/* BCLK config */
ret = mn_set_bclk(config->dai_index, config->ssp.bclk_rate,
&mdiv, &need_ecs);
if (ret < 0) {
dai_err(dai, "ssp_bclk_prepare_enable(): invalid bclk_rate = %d for dai_index = %d",
config->ssp.bclk_rate, config->dai_index);
goto out;
}
#else
if (ssp_freq[SSP_DEFAULT_IDX].freq % config->ssp.bclk_rate != 0) {
dai_err(dai, "ssp_bclk_prepare_enable(): invalid bclk_rate = %d for dai_index = %d",
config->ssp.bclk_rate, config->dai_index);
ret = -EINVAL;
goto out;
}
mdiv = ssp_freq[SSP_DEFAULT_IDX].freq / config->ssp.bclk_rate;
#endif
if (need_ecs)
sscr0 |= SSCR0_ECS;
/* clock divisor is SCR + 1 */
mdiv -= 1;
/* divisor must be within SCR range */
if (mdiv > (SSCR0_SCR_MASK >> 8)) {
dai_err(dai, "ssp_bclk_prepare_enable(): divisor %d is not within SCR range",
mdiv);
ret = -EINVAL;
goto out;
}
/* set the SCR divisor */
sscr0 &= ~SSCR0_SCR_MASK;
sscr0 |= SSCR0_SCR(mdiv);
ssp_write(dai, SSCR0, sscr0);
dai_info(dai, "ssp_bclk_prepare_enable(): sscr0 = 0x%08x", sscr0);
out:
if (!ret)
ssp->clk_active |= SSP_CLK_BCLK_ACTIVE;
return ret;
}
static void ssp_bclk_disable_unprepare(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
if (!(ssp->clk_active & SSP_CLK_BCLK_ACTIVE))
return;
#if CONFIG_INTEL_MN
mn_release_bclk(dai->index);
#endif
ssp->clk_active &= ~SSP_CLK_BCLK_ACTIVE;
}
/* Digital Audio interface formatting */
static int ssp_set_config(struct dai *dai,
struct sof_ipc_dai_config *config)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
uint32_t sscr0;
uint32_t sscr1;
uint32_t sscr2;
uint32_t sscr3;
uint32_t sspsp;
uint32_t sspsp2;
uint32_t sstsa;
uint32_t ssrsa;
uint32_t ssto;
uint32_t ssioc;
uint32_t bdiv;
uint32_t data_size;
uint32_t frame_end_padding;
uint32_t slot_end_padding;
uint32_t frame_len = 0;
uint32_t bdiv_min;
uint32_t tft;
uint32_t rft;
uint32_t active_tx_slots = 2;
uint32_t active_rx_slots = 2;
uint32_t sample_width = 2;
bool inverted_bclk = false;
bool inverted_frame = false;
bool cfs = false;
bool start_delay = false;
int ret = 0;
spin_lock(&dai->lock);
/* ignore config if SSP is already configured */
if (ssp->state[DAI_DIR_PLAYBACK] > COMP_STATE_READY ||
ssp->state[DAI_DIR_CAPTURE] > COMP_STATE_READY) {
dai_info(dai, "ssp_set_config(): Already configured. Ignore config");
goto clk;
}
dai_info(dai, "ssp_set_config(), config->format = 0x%4x",
config->format);
/* reset SSP settings */
/* sscr0 dynamic settings are DSS, EDSS, SCR, FRDC, ECS */
/*
* FIXME: MOD, ACS, NCS are not set,
* no support for network mode for now
*/
sscr0 = SSCR0_PSP | SSCR0_RIM | SSCR0_TIM;
/* sscr1 dynamic settings are SFRMDIR, SCLKDIR, SCFR, RSRE, TSRE */
sscr1 = SSCR1_TTE | SSCR1_TTELP | SSCR1_TRAIL;
/* sscr2 dynamic setting is LJDFD */
sscr2 = SSCR2_SDFD | SSCR2_TURM1;
/* sscr3 dynamic settings are TFT, RFT */
sscr3 = 0;
/* sspsp dynamic settings are SCMODE, SFRMP, DMYSTRT, SFRMWDTH */
sspsp = 0;
ssp->config = *config;
ssp->params = config->ssp;
/* sspsp2 no dynamic setting */
sspsp2 = 0x0;
/* ssioc dynamic setting is SFCR */
ssioc = SSIOC_SCOE;
/* ssto no dynamic setting */
ssto = 0x0;
/* sstsa dynamic setting is TTSA, default 2 slots */
sstsa = SSTSA_SSTSA(config->ssp.tx_slots);
/* ssrsa dynamic setting is RTSA, default 2 slots */
ssrsa = SSRSA_SSRSA(config->ssp.rx_slots);
switch (config->format & SOF_DAI_FMT_CLOCK_PROVIDER_MASK) {
case SOF_DAI_FMT_CBP_CFP:
sscr1 |= SSCR1_SCLKDIR | SSCR1_SFRMDIR;
break;
case SOF_DAI_FMT_CBC_CFC:
sscr1 |= SSCR1_SCFR;
cfs = true;
break;
case SOF_DAI_FMT_CBP_CFC:
sscr1 |= SSCR1_SCLKDIR;
/* FIXME: this mode has not been tested */
cfs = true;
break;
case SOF_DAI_FMT_CBC_CFP:
sscr1 |= SSCR1_SCFR | SSCR1_SFRMDIR;
/* FIXME: this mode has not been tested */
break;
default:
dai_err(dai, "ssp_set_config(): format & PROVIDER_MASK EINVAL");
ret = -EINVAL;
goto out;
}
/* clock signal polarity */
switch (config->format & SOF_DAI_FMT_INV_MASK) {
case SOF_DAI_FMT_NB_NF:
break;
case SOF_DAI_FMT_NB_IF:
inverted_frame = true; /* handled later with format */
break;
case SOF_DAI_FMT_IB_IF:
inverted_bclk = true; /* handled later with bclk idle */
inverted_frame = true; /* handled later with format */
break;
case SOF_DAI_FMT_IB_NF:
inverted_bclk = true; /* handled later with bclk idle */
break;
default:
dai_err(dai, "ssp_set_config(): format & INV_MASK EINVAL");
ret = -EINVAL;
goto out;
}
/* supporting bclk idle state */
if (ssp->params.clks_control &
SOF_DAI_INTEL_SSP_CLKCTRL_BCLK_IDLE_HIGH) {
/* bclk idle state high */
sspsp |= SSPSP_SCMODE((inverted_bclk ^ 0x3) & 0x3);
} else {
/* bclk idle state low */
sspsp |= SSPSP_SCMODE(inverted_bclk);
}
sscr0 |= SSCR0_MOD | SSCR0_ACS;
/* Additional hardware settings */
/* Receiver Time-out Interrupt Disabled/Enabled */
sscr1 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_TINTE) ?
SSCR1_TINTE : 0;
/* Peripheral Trailing Byte Interrupts Disable/Enable */
sscr1 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_PINTE) ?
SSCR1_PINTE : 0;
/* Enable/disable internal loopback. Output of transmit serial
* shifter connected to input of receive serial shifter, internally.
*/
sscr1 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_LBM) ?
SSCR1_LBM : 0;
/* Transmit data are driven at the same/opposite clock edge specified
* in SSPSP.SCMODE[1:0]
*/
sscr2 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_SMTATF) ?
SSCR2_SMTATF : 0;
/* Receive data are sampled at the same/opposite clock edge specified
* in SSPSP.SCMODE[1:0]
*/
sscr2 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_MMRATF) ?
SSCR2_MMRATF : 0;
/* Enable/disable the fix for PSP consumer mode TXD wait for frame
* de-assertion before starting the second channel
*/
sscr2 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_PSPSTWFDFD) ?
SSCR2_PSPSTWFDFD : 0;
/* Enable/disable the fix for PSP provider mode FSRT with dummy stop &
* frame end padding capability
*/
sscr2 |= (ssp->params.quirks & SOF_DAI_INTEL_SSP_QUIRK_PSPSRWFDFD) ?
SSCR2_PSPSRWFDFD : 0;
if (!config->ssp.mclk_rate ||
config->ssp.mclk_rate > ssp_freq[MAX_SSP_FREQ_INDEX].freq) {
dai_err(dai, "ssp_set_config(): invalid MCLK = %d Hz (valid < %d)",
config->ssp.mclk_rate,
ssp_freq[MAX_SSP_FREQ_INDEX].freq);
ret = -EINVAL;
goto out;
}
if (!config->ssp.bclk_rate ||
config->ssp.bclk_rate > config->ssp.mclk_rate) {
dai_err(dai, "ssp_set_config(): BCLK %d Hz = 0 or > MCLK %d Hz",
config->ssp.bclk_rate, config->ssp.mclk_rate);
ret = -EINVAL;
goto out;
}
/* calc frame width based on BCLK and rate - must be divisable */
if (config->ssp.bclk_rate % config->ssp.fsync_rate) {
dai_err(dai, "ssp_set_config(): BCLK %d is not divisable by rate %d",
config->ssp.bclk_rate, config->ssp.fsync_rate);
ret = -EINVAL;
goto out;
}
/* must be enough BCLKs for data */
bdiv = config->ssp.bclk_rate / config->ssp.fsync_rate;
if (bdiv < config->ssp.tdm_slot_width * config->ssp.tdm_slots) {
dai_err(dai, "ssp_set_config(): not enough BCLKs need %d",
config->ssp.tdm_slot_width *
config->ssp.tdm_slots);
ret = -EINVAL;
goto out;
}
/* tdm_slot_width must be <= 38 for SSP */
if (config->ssp.tdm_slot_width > 38) {
dai_err(dai, "ssp_set_config(): tdm_slot_width %d > 38",
config->ssp.tdm_slot_width);
ret = -EINVAL;
goto out;
}
bdiv_min = config->ssp.tdm_slots *
(config->ssp.tdm_per_slot_padding_flag ?
config->ssp.tdm_slot_width : config->ssp.sample_valid_bits);
if (bdiv < bdiv_min) {
dai_err(dai, "ssp_set_config(): bdiv(%d) < bdiv_min(%d)",
bdiv, bdiv_min);
ret = -EINVAL;
goto out;
}
frame_end_padding = bdiv - bdiv_min;
if (frame_end_padding > SSPSP2_FEP_MASK) {
dai_err(dai, "ssp_set_config(): frame_end_padding too big: %u",
frame_end_padding);
ret = -EINVAL;
goto out;
}
/* format */
switch (config->format & SOF_DAI_FMT_FORMAT_MASK) {
case SOF_DAI_FMT_I2S:
start_delay = true;
sscr0 |= SSCR0_FRDC(config->ssp.tdm_slots);
if (bdiv % 2) {
dai_err(dai, "ssp_set_config(): bdiv %d is not divisible by 2",
bdiv);
ret = -EINVAL;
goto out;
}
/* set asserted frame length to half frame length */
frame_len = bdiv / 2;
/*
* handle frame polarity, I2S default is falling/active low,
* non-inverted(inverted_frame=0) -- active low(SFRMP=0),
* inverted(inverted_frame=1) -- rising/active high(SFRMP=1),
* so, we should set SFRMP to inverted_frame.
*/
sspsp |= SSPSP_SFRMP(inverted_frame);
/*
* for I2S/LEFT_J, the padding has to happen at the end
* of each slot
*/
if (frame_end_padding % 2) {
dai_err(dai, "ssp_set_config(): frame_end_padding %d is not divisible by 2",
frame_end_padding);
ret = -EINVAL;
goto out;
}
slot_end_padding = frame_end_padding / 2;
if (slot_end_padding > SOF_DAI_INTEL_SSP_SLOT_PADDING_MAX) {
/* too big padding */
dai_err(dai, "ssp_set_config(): slot_end_padding > %d",
SOF_DAI_INTEL_SSP_SLOT_PADDING_MAX);
ret = -EINVAL;
goto out;
}
sspsp |= SSPSP_DMYSTOP(slot_end_padding);
slot_end_padding >>= SSPSP_DMYSTOP_BITS;
sspsp |= SSPSP_EDMYSTOP(slot_end_padding);
break;
case SOF_DAI_FMT_LEFT_J:
/* default start_delay value is set to false */
sscr0 |= SSCR0_FRDC(config->ssp.tdm_slots);
/* LJDFD enable */
sscr2 &= ~SSCR2_LJDFD;
if (bdiv % 2) {
dai_err(dai, "ssp_set_config(): bdiv %d is not divisible by 2",
bdiv);
ret = -EINVAL;
goto out;
}
/* set asserted frame length to half frame length */
frame_len = bdiv / 2;
/*
* handle frame polarity, LEFT_J default is rising/active high,
* non-inverted(inverted_frame=0) -- active high(SFRMP=1),
* inverted(inverted_frame=1) -- falling/active low(SFRMP=0),
* so, we should set SFRMP to !inverted_frame.
*/
sspsp |= SSPSP_SFRMP(!inverted_frame);
/*
* for I2S/LEFT_J, the padding has to happen at the end
* of each slot
*/
if (frame_end_padding % 2) {
dai_err(dai, "ssp_set_config(): frame_end_padding %d is not divisible by 2",
frame_end_padding);
ret = -EINVAL;
goto out;
}
slot_end_padding = frame_end_padding / 2;
if (slot_end_padding > 15) {
/* can't handle padding over 15 bits */
dai_err(dai, "ssp_set_config(): slot_end_padding %d > 15 bits",
slot_end_padding);
ret = -EINVAL;
goto out;
}
sspsp |= SSPSP_DMYSTOP(slot_end_padding);
slot_end_padding >>= SSPSP_DMYSTOP_BITS;
sspsp |= SSPSP_EDMYSTOP(slot_end_padding);
break;
case SOF_DAI_FMT_DSP_A:
start_delay = true;
/* fallthrough */
case SOF_DAI_FMT_DSP_B:
/* default start_delay value is set to false */
sscr0 |= SSCR0_MOD | SSCR0_FRDC(config->ssp.tdm_slots);
/* set asserted frame length */
frame_len = 1; /* default */
if (cfs && ssp->params.frame_pulse_width > 0 &&
ssp->params.frame_pulse_width <=
SOF_DAI_INTEL_SSP_FRAME_PULSE_WIDTH_MAX) {
frame_len = ssp->params.frame_pulse_width;
}
/* frame_pulse_width must less or equal 38 */
if (ssp->params.frame_pulse_width >
SOF_DAI_INTEL_SSP_FRAME_PULSE_WIDTH_MAX) {
dai_err(dai, "ssp_set_config(): frame_pulse_width > %d",
SOF_DAI_INTEL_SSP_FRAME_PULSE_WIDTH_MAX);
ret = -EINVAL;
goto out;
}
/*
* handle frame polarity, DSP_B default is rising/active high,
* non-inverted(inverted_frame=0) -- active high(SFRMP=1),
* inverted(inverted_frame=1) -- falling/active low(SFRMP=0),
* so, we should set SFRMP to !inverted_frame.
*/
sspsp |= SSPSP_SFRMP(!inverted_frame);
active_tx_slots = popcount(config->ssp.tx_slots);
active_rx_slots = popcount(config->ssp.rx_slots);
/*
* handle TDM mode, TDM mode has padding at the end of
* each slot. The amount of padding is equal to result of
* subtracting slot width and valid bits per slot.
*/
if (ssp->params.tdm_per_slot_padding_flag) {
frame_end_padding = bdiv - config->ssp.tdm_slots *
config->ssp.tdm_slot_width;
slot_end_padding = config->ssp.tdm_slot_width -
config->ssp.sample_valid_bits;
if (slot_end_padding >
SOF_DAI_INTEL_SSP_SLOT_PADDING_MAX) {
dai_err(dai, "ssp_set_config(): slot_end_padding > %d",
SOF_DAI_INTEL_SSP_SLOT_PADDING_MAX);
ret = -EINVAL;
goto out;
}
sspsp |= SSPSP_DMYSTOP(slot_end_padding);
slot_end_padding >>= SSPSP_DMYSTOP_BITS;
sspsp |= SSPSP_EDMYSTOP(slot_end_padding);
}
sspsp2 |= (frame_end_padding & SSPSP2_FEP_MASK);
break;
default:
dai_err(dai, "ssp_set_config(): invalid format 0x%04x",
config->format);
ret = -EINVAL;
goto out;
}
if (start_delay)
sspsp |= SSPSP_FSRT;
sspsp |= SSPSP_SFRMWDTH(frame_len);
data_size = config->ssp.sample_valid_bits;
if (data_size > 16)
sscr0 |= (SSCR0_EDSS | SSCR0_DSIZE(data_size - 16));
else
sscr0 |= SSCR0_DSIZE(data_size);
/* setting TFT and RFT */
switch (config->ssp.sample_valid_bits) {
case 16:
/* use 2 bytes for each slot */
sample_width = 2;
break;
case 24:
case 32:
/* use 4 bytes for each slot */
sample_width = 4;
break;
default:
dai_err(dai, "ssp_set_config(): sample_valid_bits %d",
config->ssp.sample_valid_bits);
ret = -EINVAL;
goto out;
}
tft = MIN(SSP_FIFO_DEPTH - SSP_FIFO_WATERMARK,
sample_width * active_tx_slots);
rft = MIN(SSP_FIFO_DEPTH - SSP_FIFO_WATERMARK,
sample_width * active_rx_slots);
sscr3 |= SSCR3_TX(tft) | SSCR3_RX(rft);
ssp_write(dai, SSCR0, sscr0);
ssp_write(dai, SSCR1, sscr1);
ssp_write(dai, SSCR2, sscr2);
ssp_write(dai, SSCR3, sscr3);
ssp_write(dai, SSPSP, sspsp);
ssp_write(dai, SSPSP2, sspsp2);
ssp_write(dai, SSIOC, ssioc);
ssp_write(dai, SSTO, ssto);
ssp_write(dai, SSTSA, sstsa);
ssp_write(dai, SSRSA, ssrsa);
dai_info(dai, "ssp_set_config(), sscr0 = 0x%08x, sscr1 = 0x%08x, ssto = 0x%08x, sspsp = 0x%0x",
sscr0, sscr1, ssto, sspsp);
dai_info(dai, "ssp_set_config(), sscr2 = 0x%08x, sspsp2 = 0x%08x, sscr3 = 0x%08x, ssioc = 0x%08x",
sscr2, sspsp2, sscr3, ssioc);
dai_info(dai, "ssp_set_config(), ssrsa = 0x%08x, sstsa = 0x%08x",
ssrsa, sstsa);
ssp->state[DAI_DIR_PLAYBACK] = COMP_STATE_PREPARE;
ssp->state[DAI_DIR_CAPTURE] = COMP_STATE_PREPARE;
clk:
/* MCLK always-on: turn on mclk and never turn it off */
if (ssp->params.clks_control & SOF_DAI_INTEL_SSP_CLKCTRL_MCLK_AON) {
ret = ssp_mclk_prepare_enable(dai);
if (ret < 0)
goto out;
ssp->clk_active |= SSP_CLK_MCLK_AON_REQ;
dai_info(dai, "ssp_set_config(): enable MCLK for SSP%d", dai->index);
}
switch (config->flags & SOF_DAI_CONFIG_FLAGS_MASK) {
case SOF_DAI_CONFIG_FLAGS_HW_PARAMS:
if (ssp->params.clks_control & SOF_DAI_INTEL_SSP_CLKCTRL_MCLK_ES) {
ret = ssp_mclk_prepare_enable(dai);
if (ret < 0)
goto out;
ssp->clk_active |= SSP_CLK_MCLK_ES_REQ;
dai_info(dai, "ssp_set_config(): hw_params stage: enabled MCLK clocks for SSP%d...",
dai->index);
}
if (ssp->params.clks_control & SOF_DAI_INTEL_SSP_CLKCTRL_BCLK_ES) {
bool enable_sse = false;
if (!(ssp->clk_active & SSP_CLK_BCLK_ACTIVE))
enable_sse = true;
ret = ssp_bclk_prepare_enable(dai);
if (ret < 0)
goto out;
ssp->clk_active |= SSP_CLK_BCLK_ES_REQ;
if (enable_sse) {
/* enable TRSE/RSRE before SSE */
ssp_update_bits(dai, SSCR1,
SSCR1_TSRE | SSCR1_RSRE,
SSCR1_TSRE | SSCR1_RSRE);
/* enable port */
ssp_update_bits(dai, SSCR0, SSCR0_SSE, SSCR0_SSE);
dai_info(dai, "ssp_set_config(): SSE set for SSP%d", dai->index);
}
dai_info(dai, "ssp_set_config(): hw_params stage: enabled BCLK clocks for SSP%d...",
dai->index);
}
break;
case SOF_DAI_CONFIG_FLAGS_HW_FREE:
/* disable SSP port if no users */
if (ssp->state[SOF_IPC_STREAM_CAPTURE] != COMP_STATE_PREPARE ||
ssp->state[SOF_IPC_STREAM_PLAYBACK] != COMP_STATE_PREPARE) {
dai_info(dai, "ssp_set_config(): hw_free stage: ignore since SSP%d still in use",
dai->index);
break;
}
if (ssp->params.clks_control & SOF_DAI_INTEL_SSP_CLKCTRL_BCLK_ES) {
dai_info(dai, "ssp_set_config(): hw_free stage: releasing BCLK clocks for SSP%d...",
dai->index);
if (ssp->clk_active & SSP_CLK_BCLK_ACTIVE) {
/* clear TRSE/RSRE before SSE */
ssp_update_bits(dai, SSCR1,
SSCR1_TSRE | SSCR1_RSRE,
0);
ssp_update_bits(dai, SSCR0, SSCR0_SSE, 0);
dai_info(dai, "ssp_set_config(): SSE clear for SSP%d", dai->index);
}
ssp_bclk_disable_unprepare(dai);
ssp->clk_active &= ~SSP_CLK_BCLK_ES_REQ;
}
if (ssp->params.clks_control & SOF_DAI_INTEL_SSP_CLKCTRL_MCLK_ES) {
dai_info(dai, "ssp_set_config: hw_free stage: releasing MCLK clocks for SSP%d...",
dai->index);
ssp_mclk_disable_unprepare(dai);
ssp->clk_active &= ~SSP_CLK_MCLK_ES_REQ;
}
break;
default:
break;
}
out:
spin_unlock(&dai->lock);
return ret;
}
/*
* Portion of the SSP configuration should be applied just before the
* SSP dai is activated, for either power saving or params runtime
* configurable flexibility.
*/
static int ssp_pre_start(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
int ret = 0;
dai_info(dai, "ssp_pre_start()");
/*
* We will test if mclk/bclk is configured in
* ssp_mclk/bclk_prepare_enable/disable functions
*/
if (!(ssp->clk_active & SSP_CLK_MCLK_ES_REQ) &&
!(ssp->clk_active & SSP_CLK_MCLK_AON_REQ)) {
/* MCLK config */
ret = ssp_mclk_prepare_enable(dai);
if (ret < 0)
return ret;
}
if (!(ssp->clk_active & SSP_CLK_BCLK_ES_REQ))
ret = ssp_bclk_prepare_enable(dai);
return ret;
}
/*
* For power saving, we should do kinds of power release when the SSP
* dai is changed to inactive, though the runtime param configuration
* don't have to be reset.
*/
static void ssp_post_stop(struct dai *dai)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
/* release clocks if SSP is inactive */
if (ssp->state[SOF_IPC_STREAM_PLAYBACK] != COMP_STATE_ACTIVE &&
ssp->state[SOF_IPC_STREAM_CAPTURE] != COMP_STATE_ACTIVE) {
if (!(ssp->clk_active & SSP_CLK_BCLK_ES_REQ)) {
dai_info(dai, "ssp_post_stop releasing BCLK clocks for SSP%d...",
dai->index);
ssp_bclk_disable_unprepare(dai);
}
if (!(ssp->clk_active & SSP_CLK_MCLK_ES_REQ) &&
!(ssp->clk_active & SSP_CLK_MCLK_AON_REQ)) {
dai_info(dai, "ssp_post_stop releasing MCLK clocks for SSP%d...",
dai->index);
ssp_mclk_disable_unprepare(dai);
}
}
}
/* get SSP hw params */
static int ssp_get_hw_params(struct dai *dai,
struct sof_ipc_stream_params *params, int dir)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
params->rate = ssp->params.fsync_rate;
params->buffer_fmt = 0;
if (dir == SOF_IPC_STREAM_PLAYBACK)
params->channels = popcount(ssp->params.tx_slots);
else
params->channels = popcount(ssp->params.rx_slots);
switch (ssp->params.sample_valid_bits) {
case 16:
params->frame_fmt = SOF_IPC_FRAME_S16_LE;
break;
case 24:
params->frame_fmt = SOF_IPC_FRAME_S24_4LE;
break;
case 32:
params->frame_fmt = SOF_IPC_FRAME_S32_LE;
break;
default:
dai_err(dai, "ssp_get_hw_params(): not supported format");
return -EINVAL;
}
return 0;
}
/* start the SSP for either playback or capture */
static void ssp_start(struct dai *dai, int direction)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
spin_lock(&dai->lock);
/* RX fifo must be cleared before start */
if (direction == DAI_DIR_CAPTURE)
ssp_empty_rx_fifo_on_start(dai);
/* request mclk/bclk */
ssp_pre_start(dai);
if (!(ssp->clk_active & SSP_CLK_BCLK_ES_REQ)) {
/* enable TRSE/RSRE before SSE */
ssp_update_bits(dai, SSCR1,
SSCR1_TSRE | SSCR1_RSRE,
SSCR1_TSRE | SSCR1_RSRE);
/* enable port */
ssp_update_bits(dai, SSCR0, SSCR0_SSE, SSCR0_SSE);
dai_info(dai, "ssp_start(): SSE set for SSP%d", dai->index);
}
ssp->state[direction] = COMP_STATE_ACTIVE;
dai_info(dai, "ssp_start()");
if (ssp->params.bclk_delay) {
/* drive BCLK early for guaranteed time,
* before first FSYNC, it is required by some codecs
*/
wait_delay(clock_ms_to_ticks(PLATFORM_DEFAULT_CLOCK,
ssp->params.bclk_delay));
}
/* enable DMA */
if (direction == DAI_DIR_PLAYBACK)
ssp_update_bits(dai, SSTSA, SSTSA_TXEN, SSTSA_TXEN);
else
ssp_update_bits(dai, SSRSA, SSRSA_RXEN, SSRSA_RXEN);
/* wait to get valid fifo status */
wait_delay(PLATFORM_SSP_DELAY);
spin_unlock(&dai->lock);
}
/* stop the SSP for either playback or capture */
static void ssp_stop(struct dai *dai, int direction)
{
struct ssp_pdata *ssp = dai_get_drvdata(dai);
spin_lock(&dai->lock);
/* wait to get valid fifo status */
wait_delay(PLATFORM_SSP_DELAY);
/* stop Rx if neeed */
if (direction == DAI_DIR_CAPTURE &&
ssp->state[SOF_IPC_STREAM_CAPTURE] != COMP_STATE_PREPARE) {
ssp_update_bits(dai, SSRSA, SSRSA_RXEN, 0);
ssp_empty_rx_fifo_on_stop(dai);
ssp->state[SOF_IPC_STREAM_CAPTURE] = COMP_STATE_PREPARE;
dai_info(dai, "ssp_stop(), RX stop");
}