1 From 87f734fa8214b4ddbfdac9b7ac5dc75a3d86badb Mon Sep 17 00:00:00 2001
2 From: Shengjiu Wang <shengjiu.wang@nxp.com>
3 Date: Tue, 23 Jan 2018 13:26:37 +0800
4 Subject: [PATCH] MLK-16224-4: ASoC: fsl_sai: support multi fifo and DSD
6 The codec always mux the LRCLK pin to DSD data line, so when
7 we want to support DSD, the pinmux is different. For two channel
8 DSD, the DSDL is mapped to TX0, but the DSDR is mapped to TX4,
9 there is address offset for the fifo address of TX0 and TX4, TX4's
10 fifo is not adjacent to TX0's.
12 Usually, if mapping is TX0 and TX1, that will be easy for SAI
13 and SDMA to handle, that SAI can use the FIFO combine mode, SDMA
14 can use the normal script.
17 1. The SDMA should use the multi-fifo script, and SAI can't
18 use the FIFO combine mode.
19 2. driver should to check the dts configuration(fsl,dataline) for
20 which dataline is used corrently
21 3. maxburst is the multiply of datalines
22 4. each channel of DSD occupy one data lane
23 5. according to data lane, set TRCE bits
25 Signed-off-by: Shengjiu Wang <shengjiu.wang@nxp.com>
26 Reviewed-by: Viorel Suman <viorel.suman@nxp.com>
28 sound/soc/fsl/fsl_sai.c | 162 +++++++++++++++++++++++++++++++++++++++++++++---
29 sound/soc/fsl/fsl_sai.h | 12 +++-
30 2 files changed, 164 insertions(+), 10 deletions(-)
32 --- a/sound/soc/fsl/fsl_sai.c
33 +++ b/sound/soc/fsl/fsl_sai.c
34 @@ -267,6 +267,7 @@ static int fsl_sai_set_dai_fmt_tr(struct
35 if (!sai->is_lsb_first)
36 val_cr4 |= FSL_SAI_CR4_MF;
38 + sai->is_dsp_mode = false;
40 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
41 case SND_SOC_DAIFMT_I2S:
42 @@ -305,6 +306,11 @@ static int fsl_sai_set_dai_fmt_tr(struct
43 val_cr2 |= FSL_SAI_CR2_BCP;
44 sai->is_dsp_mode = true;
46 + case SND_SOC_DAIFMT_PDM:
47 + val_cr2 |= FSL_SAI_CR2_BCP;
48 + val_cr4 &= ~FSL_SAI_CR4_MF;
49 + sai->is_dsp_mode = true;
51 case SND_SOC_DAIFMT_RIGHT_J:
54 @@ -492,12 +498,38 @@ static int fsl_sai_hw_params(struct snd_
55 u32 slot_width = word_width;
60 + snd_pcm_format_t format = params_format(params);
65 pins = DIV_ROUND_UP(channels, slots);
67 + if (format == SNDRV_PCM_FORMAT_DSD_U8 ||
68 + format == SNDRV_PCM_FORMAT_DSD_U16_LE ||
69 + format == SNDRV_PCM_FORMAT_DSD_U16_BE ||
70 + format == SNDRV_PCM_FORMAT_DSD_U32_LE ||
71 + format == SNDRV_PCM_FORMAT_DSD_U32_BE) {
74 + if (!IS_ERR_OR_NULL(sai->pins_dsd)) {
75 + ret = pinctrl_select_state(sai->pinctrl, sai->pins_dsd);
77 + dev_err(cpu_dai->dev,
78 + "failed to set proper pins state: %d\n", ret);
83 + pinctrl_pm_select_default_state(cpu_dai->dev);
84 + sai->is_dsd = false;
91 slot_width = sai->slot_width;
93 @@ -527,7 +559,7 @@ static int fsl_sai_hw_params(struct snd_
94 val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
95 val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
97 - if (sai->is_lsb_first)
98 + if (sai->is_lsb_first || sai->is_dsd)
99 val_cr5 |= FSL_SAI_CR5_FBT(0);
101 val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
102 @@ -560,17 +592,71 @@ static int fsl_sai_hw_params(struct snd_
105 if (sai->soc->dataline != 0x1) {
106 - if (sai->dataline[tx] <= 1)
108 + if (sai->dataline[tx] <= 1 || sai->is_multi_lane)
109 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, offset),
110 FSL_SAI_CR4_FCOMB_MASK, 0);
112 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, offset),
113 FSL_SAI_CR4_FCOMB_MASK, FSL_SAI_CR4_FCOMB_SOFT);
115 + if (sai->is_multi_lane) {
117 + sai->dma_params_tx.maxburst =
118 + FSL_SAI_MAXBURST_TX * pins;
120 + sai->dma_params_tx.fifo_num = pins +
121 + (sai->dataline_off_dsd[tx] << 8);
123 + sai->dma_params_tx.fifo_num = pins +
124 + (sai->dataline_off[tx] << 8);
126 + sai->dma_params_rx.maxburst =
127 + FSL_SAI_MAXBURST_RX * pins;
129 + sai->dma_params_rx.fifo_num = pins +
130 + (sai->dataline_off_dsd[tx] << 8);
132 + sai->dma_params_rx.fifo_num = pins +
133 + (sai->dataline_off[tx] << 8);
137 + snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
138 + &sai->dma_params_rx);
141 - regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, offset),
143 + if (__sw_hweight8(sai->dataline_dsd[tx] & 0xFF) < pins) {
144 + dev_err(cpu_dai->dev, "channel not supported\n");
147 + /*find a proper tcre setting*/
148 + for (i = 0; i < 8; i++) {
149 + trce_mask = (1 << (i + 1)) - 1;
150 + if (__sw_hweight8(sai->dataline_dsd[tx] & trce_mask) == pins)
154 + regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, offset),
155 FSL_SAI_CR3_TRCE_MASK,
156 - FSL_SAI_CR3_TRCE((sai->dataline[tx] & ((1 << pins) - 1))));
157 + FSL_SAI_CR3_TRCE((sai->dataline_dsd[tx] & trce_mask)));
159 + if (__sw_hweight8(sai->dataline[tx] & 0xFF) < pins) {
160 + dev_err(cpu_dai->dev, "channel not supported\n");
163 + /*find a proper tcre setting*/
164 + for (i = 0; i < 8; i++) {
165 + trce_mask = (1 << (i + 1)) - 1;
166 + if (__sw_hweight8(sai->dataline[tx] & trce_mask) == pins)
170 + regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, offset),
171 + FSL_SAI_CR3_TRCE_MASK,
172 + FSL_SAI_CR3_TRCE((sai->dataline[tx] & trce_mask)));
175 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, offset),
176 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
177 @@ -610,9 +696,18 @@ static int fsl_sai_trigger(struct snd_pc
178 unsigned char offset = sai->soc->reg_offset;
179 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
180 u8 channels = substream->runtime->channels;
181 + u32 slots = (channels == 1) ? 2 : channels;
182 u32 xcsr, count = 100;
185 + int i = 0, j = 0, k = 0;
188 + slots = sai->slots;
190 + pins = DIV_ROUND_UP(channels, slots);
195 * Asynchronous mode: Clear SYNC for both Tx and Rx.
196 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
197 @@ -631,10 +726,19 @@ static int fsl_sai_trigger(struct snd_pc
198 case SNDRV_PCM_TRIGGER_START:
199 case SNDRV_PCM_TRIGGER_RESUME:
200 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
201 - for (i = 0; tx && i < channels; i++)
202 - regmap_write(sai->regmap, FSL_SAI_TDR0, 0x0);
206 + for (i = 0; tx && i < channels; i++) {
207 + while (tx && i < channels)
208 + if (sai->dataline[tx] & (1 << j)) {
209 + regmap_write(sai->regmap, FSL_SAI_TDR0 + j * 0x4, 0x0);
219 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, offset),
220 FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
221 @@ -994,6 +1098,7 @@ static int fsl_sai_probe(struct platform
225 + int firstbitidx, nextbitidx;
226 struct regmap_config fsl_sai_regmap_config = fsl_sai_v2_regmap_config;
227 unsigned long irqflags = 0;
229 @@ -1048,6 +1153,9 @@ static int fsl_sai_probe(struct platform
233 + if (of_find_property(np, "fsl,sai-multi-lane", NULL))
234 + sai->is_multi_lane = true;
236 /*dataline mask for rx and tx*/
237 ret = of_property_read_u32_index(np, "fsl,dataline", 0, &sai->dataline[0]);
239 @@ -1062,6 +1170,37 @@ static int fsl_sai_probe(struct platform
243 + for (i = 0; i < 2; i++) {
244 + firstbitidx = find_first_bit((const unsigned long *)&sai->dataline[i], 8);
245 + nextbitidx = find_next_bit((const unsigned long *)&sai->dataline[i], 8, firstbitidx+1);
246 + sai->dataline_off[i] = nextbitidx - firstbitidx - 1;
248 + if (sai->dataline_off[i] < 0 || sai->dataline_off[i] >= 7)
249 + sai->dataline_off[i] = 0;
252 + ret = of_property_read_u32_index(np, "fsl,dataline,dsd", 0, &sai->dataline_dsd[0]);
254 + sai->dataline_dsd[0] = 1;
256 + ret = of_property_read_u32_index(np, "fsl,dataline,dsd", 1, &sai->dataline_dsd[1]);
258 + sai->dataline_dsd[1] = 1;
260 + if ((sai->dataline_dsd[0] & (~sai->soc->dataline)) || sai->dataline_dsd[1] & (~sai->soc->dataline)) {
261 + dev_err(&pdev->dev, "dataline setting error, Mask is 0x%x\n", sai->soc->dataline);
265 + for (i = 0; i < 2; i++) {
266 + firstbitidx = find_first_bit((const unsigned long *)&sai->dataline_dsd[i], 8);
267 + nextbitidx = find_next_bit((const unsigned long *)&sai->dataline_dsd[i], 8, firstbitidx+1);
268 + sai->dataline_off_dsd[i] = nextbitidx - firstbitidx - 1;
270 + if (sai->dataline_off_dsd[i] < 0 || sai->dataline_off_dsd[i] >= 7)
271 + sai->dataline_off_dsd[i] = 0;
274 if ((of_find_property(np, "fsl,i2s-xtor", NULL) != NULL) ||
275 (of_find_property(np, "fsl,txm-rxs", NULL) != NULL))
277 @@ -1141,6 +1280,11 @@ static int fsl_sai_probe(struct platform
278 sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
279 sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
281 + sai->pinctrl = devm_pinctrl_get(&pdev->dev);
283 + if (!IS_ERR_OR_NULL(sai->pinctrl))
284 + sai->pins_dsd = pinctrl_lookup_state(sai->pinctrl, "dsd");
286 platform_set_drvdata(pdev, sai);
288 pm_runtime_enable(&pdev->dev);
289 --- a/sound/soc/fsl/fsl_sai.h
290 +++ b/sound/soc/fsl/fsl_sai.h
293 #define FSL_SAI_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
294 SNDRV_PCM_FMTBIT_S24_LE |\
295 - SNDRV_PCM_FMTBIT_S32_LE)
296 + SNDRV_PCM_FMTBIT_S32_LE |\
297 + SNDRV_PCM_FMTBIT_DSD_U8 |\
298 + SNDRV_PCM_FMTBIT_DSD_U16_LE |\
299 + SNDRV_PCM_FMTBIT_DSD_U32_LE)
301 /* SAI Register Map Register */
302 #define FSL_SAI_TCSR(offset) (0x00 + offset) /* SAI Transmit Control */
303 @@ -172,9 +175,14 @@ struct fsl_sai {
307 + bool is_multi_lane;
309 bool is_stream_opened[2];
311 unsigned int dataline[2];
312 + unsigned int dataline_dsd[2];
313 + unsigned int dataline_off[2];
314 + unsigned int dataline_off_dsd[2];
315 unsigned int masterflag[2];
317 unsigned int mclk_id[2];
318 @@ -187,6 +195,8 @@ struct fsl_sai {
319 struct snd_dmaengine_dai_dma_data dma_params_tx;
320 const struct fsl_sai_soc_data *soc;
321 struct pm_qos_request pm_qos_req;
322 + struct pinctrl *pinctrl;
323 + struct pinctrl_state *pins_dsd;