blob: 1c93282fbd2696a9475499ee7e4b0d51ddb0f615 [file] [log] [blame]
Xiubo Li43550822013-12-17 11:24:38 +08001/*
2 * Freescale ALSA SoC Digital Audio Interface (SAI) driver.
3 *
4 * Copyright 2012-2013 Freescale Semiconductor, Inc.
5 *
6 * This program is free software, you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 2 of the License, or(at your
9 * option) any later version.
10 *
11 */
12
13#include <linux/clk.h>
14#include <linux/delay.h>
15#include <linux/dmaengine.h>
16#include <linux/module.h>
17#include <linux/of_address.h>
Xiubo Li78957fc2014-02-08 14:38:28 +080018#include <linux/regmap.h>
Xiubo Li43550822013-12-17 11:24:38 +080019#include <linux/slab.h>
20#include <sound/core.h>
21#include <sound/dmaengine_pcm.h>
22#include <sound/pcm_params.h>
23
24#include "fsl_sai.h"
Nicolin Chenc7540642014-04-01 19:34:09 +080025#include "imx-pcm.h"
Xiubo Li43550822013-12-17 11:24:38 +080026
Nicolin Chene2681a12014-03-27 19:06:59 +080027#define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
28 FSL_SAI_CSR_FEIE)
29
30static irqreturn_t fsl_sai_isr(int irq, void *devid)
31{
32 struct fsl_sai *sai = (struct fsl_sai *)devid;
33 struct device *dev = &sai->pdev->dev;
Nicolin Chen413312a2014-03-28 19:39:25 +080034 u32 flags, xcsr, mask;
35 bool irq_none = true;
Nicolin Chene2681a12014-03-27 19:06:59 +080036
Nicolin Chen413312a2014-03-28 19:39:25 +080037 /*
38 * Both IRQ status bits and IRQ mask bits are in the xCSR but
39 * different shifts. And we here create a mask only for those
40 * IRQs that we activated.
41 */
Nicolin Chene2681a12014-03-27 19:06:59 +080042 mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
43
44 /* Tx IRQ */
45 regmap_read(sai->regmap, FSL_SAI_TCSR, &xcsr);
Nicolin Chen413312a2014-03-28 19:39:25 +080046 flags = xcsr & mask;
Nicolin Chene2681a12014-03-27 19:06:59 +080047
Nicolin Chen413312a2014-03-28 19:39:25 +080048 if (flags)
49 irq_none = false;
50 else
51 goto irq_rx;
52
53 if (flags & FSL_SAI_CSR_WSF)
Nicolin Chene2681a12014-03-27 19:06:59 +080054 dev_dbg(dev, "isr: Start of Tx word detected\n");
55
Nicolin Chen413312a2014-03-28 19:39:25 +080056 if (flags & FSL_SAI_CSR_SEF)
Nicolin Chene2681a12014-03-27 19:06:59 +080057 dev_warn(dev, "isr: Tx Frame sync error detected\n");
58
Nicolin Chen413312a2014-03-28 19:39:25 +080059 if (flags & FSL_SAI_CSR_FEF) {
Nicolin Chene2681a12014-03-27 19:06:59 +080060 dev_warn(dev, "isr: Transmit underrun detected\n");
61 /* FIFO reset for safety */
62 xcsr |= FSL_SAI_CSR_FR;
63 }
64
Nicolin Chen413312a2014-03-28 19:39:25 +080065 if (flags & FSL_SAI_CSR_FWF)
Nicolin Chene2681a12014-03-27 19:06:59 +080066 dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
67
Nicolin Chen413312a2014-03-28 19:39:25 +080068 if (flags & FSL_SAI_CSR_FRF)
Nicolin Chene2681a12014-03-27 19:06:59 +080069 dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
70
Nicolin Chen413312a2014-03-28 19:39:25 +080071 flags &= FSL_SAI_CSR_xF_W_MASK;
72 xcsr &= ~FSL_SAI_CSR_xF_MASK;
Nicolin Chene2681a12014-03-27 19:06:59 +080073
Nicolin Chen413312a2014-03-28 19:39:25 +080074 if (flags)
75 regmap_write(sai->regmap, FSL_SAI_TCSR, flags | xcsr);
76
77irq_rx:
Nicolin Chene2681a12014-03-27 19:06:59 +080078 /* Rx IRQ */
79 regmap_read(sai->regmap, FSL_SAI_RCSR, &xcsr);
Nicolin Chen413312a2014-03-28 19:39:25 +080080 flags = xcsr & mask;
Nicolin Chene2681a12014-03-27 19:06:59 +080081
Nicolin Chen413312a2014-03-28 19:39:25 +080082 if (flags)
83 irq_none = false;
84 else
85 goto out;
86
87 if (flags & FSL_SAI_CSR_WSF)
Nicolin Chene2681a12014-03-27 19:06:59 +080088 dev_dbg(dev, "isr: Start of Rx word detected\n");
89
Nicolin Chen413312a2014-03-28 19:39:25 +080090 if (flags & FSL_SAI_CSR_SEF)
Nicolin Chene2681a12014-03-27 19:06:59 +080091 dev_warn(dev, "isr: Rx Frame sync error detected\n");
92
Nicolin Chen413312a2014-03-28 19:39:25 +080093 if (flags & FSL_SAI_CSR_FEF) {
Nicolin Chene2681a12014-03-27 19:06:59 +080094 dev_warn(dev, "isr: Receive overflow detected\n");
95 /* FIFO reset for safety */
96 xcsr |= FSL_SAI_CSR_FR;
97 }
98
Nicolin Chen413312a2014-03-28 19:39:25 +080099 if (flags & FSL_SAI_CSR_FWF)
Nicolin Chene2681a12014-03-27 19:06:59 +0800100 dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
101
Nicolin Chen413312a2014-03-28 19:39:25 +0800102 if (flags & FSL_SAI_CSR_FRF)
Nicolin Chene2681a12014-03-27 19:06:59 +0800103 dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
104
Nicolin Chen413312a2014-03-28 19:39:25 +0800105 flags &= FSL_SAI_CSR_xF_W_MASK;
106 xcsr &= ~FSL_SAI_CSR_xF_MASK;
Nicolin Chene2681a12014-03-27 19:06:59 +0800107
Nicolin Chen413312a2014-03-28 19:39:25 +0800108 if (flags)
109 regmap_write(sai->regmap, FSL_SAI_TCSR, flags | xcsr);
110
111out:
112 if (irq_none)
113 return IRQ_NONE;
114 else
115 return IRQ_HANDLED;
Nicolin Chene2681a12014-03-27 19:06:59 +0800116}
117
Xiubo Li43550822013-12-17 11:24:38 +0800118static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
119 int clk_id, unsigned int freq, int fsl_dir)
120{
Xiubo Li43550822013-12-17 11:24:38 +0800121 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
Nicolin Chen4e3a99f2013-12-20 16:41:05 +0800122 u32 val_cr2, reg_cr2;
Xiubo Li43550822013-12-17 11:24:38 +0800123
124 if (fsl_dir == FSL_FMT_TRANSMITTER)
125 reg_cr2 = FSL_SAI_TCR2;
126 else
127 reg_cr2 = FSL_SAI_RCR2;
128
Xiubo Li78957fc2014-02-08 14:38:28 +0800129 regmap_read(sai->regmap, reg_cr2, &val_cr2);
130
Xiubo Li633ff8f2014-01-08 16:13:05 +0800131 val_cr2 &= ~FSL_SAI_CR2_MSEL_MASK;
132
Xiubo Li43550822013-12-17 11:24:38 +0800133 switch (clk_id) {
134 case FSL_SAI_CLK_BUS:
Xiubo Li43550822013-12-17 11:24:38 +0800135 val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
136 break;
137 case FSL_SAI_CLK_MAST1:
Xiubo Li43550822013-12-17 11:24:38 +0800138 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
139 break;
140 case FSL_SAI_CLK_MAST2:
Xiubo Li43550822013-12-17 11:24:38 +0800141 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
142 break;
143 case FSL_SAI_CLK_MAST3:
Xiubo Li43550822013-12-17 11:24:38 +0800144 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
145 break;
146 default:
147 return -EINVAL;
148 }
Xiubo Li633ff8f2014-01-08 16:13:05 +0800149
Xiubo Li78957fc2014-02-08 14:38:28 +0800150 regmap_write(sai->regmap, reg_cr2, val_cr2);
Xiubo Li43550822013-12-17 11:24:38 +0800151
152 return 0;
153}
154
155static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
156 int clk_id, unsigned int freq, int dir)
157{
Nicolin Chen4e3a99f2013-12-20 16:41:05 +0800158 int ret;
Xiubo Li43550822013-12-17 11:24:38 +0800159
160 if (dir == SND_SOC_CLOCK_IN)
161 return 0;
162
Xiubo Li43550822013-12-17 11:24:38 +0800163 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
164 FSL_FMT_TRANSMITTER);
165 if (ret) {
Nicolin Chen190af122013-12-20 16:41:04 +0800166 dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
Xiubo Li78957fc2014-02-08 14:38:28 +0800167 return ret;
Xiubo Li43550822013-12-17 11:24:38 +0800168 }
169
170 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
171 FSL_FMT_RECEIVER);
Xiubo Li78957fc2014-02-08 14:38:28 +0800172 if (ret)
Nicolin Chen190af122013-12-20 16:41:04 +0800173 dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
Xiubo Li43550822013-12-17 11:24:38 +0800174
Nicolin Chen1fb2d9d2013-12-20 16:41:00 +0800175 return ret;
Xiubo Li43550822013-12-17 11:24:38 +0800176}
177
178static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
179 unsigned int fmt, int fsl_dir)
180{
Xiubo Li43550822013-12-17 11:24:38 +0800181 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
Xiubo Lie5d0fa92013-12-25 12:40:04 +0800182 u32 val_cr2, val_cr4, reg_cr2, reg_cr4;
Xiubo Li43550822013-12-17 11:24:38 +0800183
184 if (fsl_dir == FSL_FMT_TRANSMITTER) {
185 reg_cr2 = FSL_SAI_TCR2;
Xiubo Li43550822013-12-17 11:24:38 +0800186 reg_cr4 = FSL_SAI_TCR4;
187 } else {
188 reg_cr2 = FSL_SAI_RCR2;
Xiubo Li43550822013-12-17 11:24:38 +0800189 reg_cr4 = FSL_SAI_RCR4;
190 }
191
Xiubo Li78957fc2014-02-08 14:38:28 +0800192 regmap_read(sai->regmap, reg_cr2, &val_cr2);
193 regmap_read(sai->regmap, reg_cr4, &val_cr4);
Xiubo Li43550822013-12-17 11:24:38 +0800194
195 if (sai->big_endian_data)
Xiubo Li43550822013-12-17 11:24:38 +0800196 val_cr4 &= ~FSL_SAI_CR4_MF;
Xiubo Li72aa62b2013-12-31 15:33:22 +0800197 else
198 val_cr4 |= FSL_SAI_CR4_MF;
Xiubo Li43550822013-12-17 11:24:38 +0800199
Xiubo Li13cde092014-02-25 17:54:51 +0800200 /* DAI mode */
Xiubo Li43550822013-12-17 11:24:38 +0800201 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
202 case SND_SOC_DAIFMT_I2S:
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800203 /*
204 * Frame low, 1clk before data, one word length for frame sync,
205 * frame sync starts one serial clock cycle earlier,
206 * that is, together with the last bit of the previous
207 * data word.
208 */
Nicolin Chenef33bc32014-04-04 15:09:47 +0800209 val_cr2 |= FSL_SAI_CR2_BCP;
Xiubo Li13cde092014-02-25 17:54:51 +0800210 val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
Xiubo Li43550822013-12-17 11:24:38 +0800211 break;
Xiubo Li13cde092014-02-25 17:54:51 +0800212 case SND_SOC_DAIFMT_LEFT_J:
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800213 /*
214 * Frame high, one word length for frame sync,
215 * frame sync asserts with the first bit of the frame.
216 */
Nicolin Chenef33bc32014-04-04 15:09:47 +0800217 val_cr2 |= FSL_SAI_CR2_BCP;
Xiubo Li13cde092014-02-25 17:54:51 +0800218 val_cr4 &= ~(FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP);
219 break;
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800220 case SND_SOC_DAIFMT_DSP_A:
221 /*
222 * Frame high, 1clk before data, one bit for frame sync,
223 * frame sync starts one serial clock cycle earlier,
224 * that is, together with the last bit of the previous
225 * data word.
226 */
Nicolin Chenef33bc32014-04-04 15:09:47 +0800227 val_cr2 |= FSL_SAI_CR2_BCP;
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800228 val_cr4 &= ~FSL_SAI_CR4_FSP;
229 val_cr4 |= FSL_SAI_CR4_FSE;
230 sai->is_dsp_mode = true;
231 break;
232 case SND_SOC_DAIFMT_DSP_B:
233 /*
234 * Frame high, one bit for frame sync,
235 * frame sync asserts with the first bit of the frame.
236 */
Nicolin Chenef33bc32014-04-04 15:09:47 +0800237 val_cr2 |= FSL_SAI_CR2_BCP;
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800238 val_cr4 &= ~(FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP);
239 sai->is_dsp_mode = true;
240 break;
Xiubo Li13cde092014-02-25 17:54:51 +0800241 case SND_SOC_DAIFMT_RIGHT_J:
242 /* To be done */
Xiubo Li43550822013-12-17 11:24:38 +0800243 default:
244 return -EINVAL;
245 }
246
Xiubo Li13cde092014-02-25 17:54:51 +0800247 /* DAI clock inversion */
Xiubo Li43550822013-12-17 11:24:38 +0800248 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
249 case SND_SOC_DAIFMT_IB_IF:
Xiubo Li13cde092014-02-25 17:54:51 +0800250 /* Invert both clocks */
251 val_cr2 ^= FSL_SAI_CR2_BCP;
252 val_cr4 ^= FSL_SAI_CR4_FSP;
Xiubo Li43550822013-12-17 11:24:38 +0800253 break;
254 case SND_SOC_DAIFMT_IB_NF:
Xiubo Li13cde092014-02-25 17:54:51 +0800255 /* Invert bit clock */
256 val_cr2 ^= FSL_SAI_CR2_BCP;
Xiubo Li43550822013-12-17 11:24:38 +0800257 break;
258 case SND_SOC_DAIFMT_NB_IF:
Xiubo Li13cde092014-02-25 17:54:51 +0800259 /* Invert frame clock */
260 val_cr4 ^= FSL_SAI_CR4_FSP;
Xiubo Li43550822013-12-17 11:24:38 +0800261 break;
262 case SND_SOC_DAIFMT_NB_NF:
Xiubo Li13cde092014-02-25 17:54:51 +0800263 /* Nothing to do for both normal cases */
Xiubo Li43550822013-12-17 11:24:38 +0800264 break;
265 default:
266 return -EINVAL;
267 }
268
Xiubo Li13cde092014-02-25 17:54:51 +0800269 /* DAI clock master masks */
Xiubo Li43550822013-12-17 11:24:38 +0800270 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
271 case SND_SOC_DAIFMT_CBS_CFS:
272 val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
273 val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
274 break;
275 case SND_SOC_DAIFMT_CBM_CFM:
276 val_cr2 &= ~FSL_SAI_CR2_BCD_MSTR;
277 val_cr4 &= ~FSL_SAI_CR4_FSD_MSTR;
278 break;
Xiubo Li13cde092014-02-25 17:54:51 +0800279 case SND_SOC_DAIFMT_CBS_CFM:
280 val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
281 val_cr4 &= ~FSL_SAI_CR4_FSD_MSTR;
282 break;
283 case SND_SOC_DAIFMT_CBM_CFS:
284 val_cr2 &= ~FSL_SAI_CR2_BCD_MSTR;
285 val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
286 break;
Xiubo Li43550822013-12-17 11:24:38 +0800287 default:
288 return -EINVAL;
289 }
290
Xiubo Li78957fc2014-02-08 14:38:28 +0800291 regmap_write(sai->regmap, reg_cr2, val_cr2);
292 regmap_write(sai->regmap, reg_cr4, val_cr4);
Xiubo Li43550822013-12-17 11:24:38 +0800293
294 return 0;
295}
296
297static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
298{
Nicolin Chen4e3a99f2013-12-20 16:41:05 +0800299 int ret;
Xiubo Li43550822013-12-17 11:24:38 +0800300
Xiubo Li43550822013-12-17 11:24:38 +0800301 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
302 if (ret) {
Nicolin Chen190af122013-12-20 16:41:04 +0800303 dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
Xiubo Li78957fc2014-02-08 14:38:28 +0800304 return ret;
Xiubo Li43550822013-12-17 11:24:38 +0800305 }
306
307 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
Xiubo Li78957fc2014-02-08 14:38:28 +0800308 if (ret)
Nicolin Chen190af122013-12-20 16:41:04 +0800309 dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
Xiubo Li43550822013-12-17 11:24:38 +0800310
Nicolin Chen1fb2d9d2013-12-20 16:41:00 +0800311 return ret;
Xiubo Li43550822013-12-17 11:24:38 +0800312}
313
314static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
315 struct snd_pcm_hw_params *params,
316 struct snd_soc_dai *cpu_dai)
317{
Nicolin Chen4e3a99f2013-12-20 16:41:05 +0800318 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
Nicolin Chen1d700302013-12-20 16:41:01 +0800319 u32 val_cr4, val_cr5, val_mr, reg_cr4, reg_cr5, reg_mr;
Xiubo Li43550822013-12-17 11:24:38 +0800320 unsigned int channels = params_channels(params);
Nicolin Chen1d700302013-12-20 16:41:01 +0800321 u32 word_width = snd_pcm_format_width(params_format(params));
Xiubo Li43550822013-12-17 11:24:38 +0800322
323 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
324 reg_cr4 = FSL_SAI_TCR4;
325 reg_cr5 = FSL_SAI_TCR5;
326 reg_mr = FSL_SAI_TMR;
327 } else {
328 reg_cr4 = FSL_SAI_RCR4;
329 reg_cr5 = FSL_SAI_RCR5;
330 reg_mr = FSL_SAI_RMR;
331 }
332
Xiubo Li78957fc2014-02-08 14:38:28 +0800333 regmap_read(sai->regmap, reg_cr4, &val_cr4);
334 regmap_read(sai->regmap, reg_cr4, &val_cr5);
335
Xiubo Li43550822013-12-17 11:24:38 +0800336 val_cr4 &= ~FSL_SAI_CR4_SYWD_MASK;
337 val_cr4 &= ~FSL_SAI_CR4_FRSZ_MASK;
338
Xiubo Li43550822013-12-17 11:24:38 +0800339 val_cr5 &= ~FSL_SAI_CR5_WNW_MASK;
340 val_cr5 &= ~FSL_SAI_CR5_W0W_MASK;
341 val_cr5 &= ~FSL_SAI_CR5_FBT_MASK;
342
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800343 if (!sai->is_dsp_mode)
344 val_cr4 |= FSL_SAI_CR4_SYWD(word_width);
345
Xiubo Li43550822013-12-17 11:24:38 +0800346 val_cr5 |= FSL_SAI_CR5_WNW(word_width);
347 val_cr5 |= FSL_SAI_CR5_W0W(word_width);
348
Xiubo Li496a39d2013-12-31 15:33:21 +0800349 val_cr5 &= ~FSL_SAI_CR5_FBT_MASK;
Xiubo Li43550822013-12-17 11:24:38 +0800350 if (sai->big_endian_data)
Xiubo Li43550822013-12-17 11:24:38 +0800351 val_cr5 |= FSL_SAI_CR5_FBT(0);
Xiubo Li72aa62b2013-12-31 15:33:22 +0800352 else
353 val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
Xiubo Li43550822013-12-17 11:24:38 +0800354
355 val_cr4 |= FSL_SAI_CR4_FRSZ(channels);
Nicolin Chend22e28c2013-12-20 16:41:02 +0800356 val_mr = ~0UL - ((1 << channels) - 1);
Xiubo Li43550822013-12-17 11:24:38 +0800357
Xiubo Li78957fc2014-02-08 14:38:28 +0800358 regmap_write(sai->regmap, reg_cr4, val_cr4);
359 regmap_write(sai->regmap, reg_cr5, val_cr5);
360 regmap_write(sai->regmap, reg_mr, val_mr);
Xiubo Li43550822013-12-17 11:24:38 +0800361
362 return 0;
363}
364
365static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
366 struct snd_soc_dai *cpu_dai)
367{
368 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
Nicolin Chene6b39842014-04-01 11:17:06 +0800369 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Xiubo Li78957fc2014-02-08 14:38:28 +0800370 u32 tcsr, rcsr;
Xiubo Li496a39d2013-12-31 15:33:21 +0800371
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800372 /*
373 * The transmitter bit clock and frame sync are to be
374 * used by both the transmitter and receiver.
375 */
Xiubo Li78957fc2014-02-08 14:38:28 +0800376 regmap_update_bits(sai->regmap, FSL_SAI_TCR2, FSL_SAI_CR2_SYNC,
377 ~FSL_SAI_CR2_SYNC);
378 regmap_update_bits(sai->regmap, FSL_SAI_RCR2, FSL_SAI_CR2_SYNC,
379 FSL_SAI_CR2_SYNC);
Xiubo Li496a39d2013-12-31 15:33:21 +0800380
Xiubo Li78957fc2014-02-08 14:38:28 +0800381 regmap_read(sai->regmap, FSL_SAI_TCSR, &tcsr);
382 regmap_read(sai->regmap, FSL_SAI_RCSR, &rcsr);
Xiubo Li43550822013-12-17 11:24:38 +0800383
Xiubo Lia3f7dcc2014-02-27 08:45:01 +0800384 /*
385 * It is recommended that the transmitter is the last enabled
386 * and the first disabled.
387 */
Xiubo Li43550822013-12-17 11:24:38 +0800388 switch (cmd) {
389 case SNDRV_PCM_TRIGGER_START:
390 case SNDRV_PCM_TRIGGER_RESUME:
391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Nicolin Chene6b39842014-04-01 11:17:06 +0800392 if (!(tcsr & FSL_SAI_CSR_FRDE || rcsr & FSL_SAI_CSR_FRDE)) {
393 regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
394 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
395 regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
396 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
397 }
Xiubo Lie5d0fa92013-12-25 12:40:04 +0800398
Nicolin Chene6b39842014-04-01 11:17:06 +0800399 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
Nicolin Chen8abba5d2014-04-01 11:17:07 +0800400 FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
401 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
Nicolin Chene6b39842014-04-01 11:17:06 +0800402 FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
Xiubo Li43550822013-12-17 11:24:38 +0800403 break;
Xiubo Li43550822013-12-17 11:24:38 +0800404 case SNDRV_PCM_TRIGGER_STOP:
405 case SNDRV_PCM_TRIGGER_SUSPEND:
406 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Nicolin Chene6b39842014-04-01 11:17:06 +0800407 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
408 FSL_SAI_CSR_FRDE, 0);
Nicolin Chen8abba5d2014-04-01 11:17:07 +0800409 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
410 FSL_SAI_CSR_xIE_MASK, 0);
Xiubo Lie5d0fa92013-12-25 12:40:04 +0800411
Nicolin Chene6b39842014-04-01 11:17:06 +0800412 if (!(tcsr & FSL_SAI_CSR_FRDE || rcsr & FSL_SAI_CSR_FRDE)) {
413 regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
414 FSL_SAI_CSR_TERE, 0);
415 regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
416 FSL_SAI_CSR_TERE, 0);
417 }
Xiubo Li43550822013-12-17 11:24:38 +0800418 break;
419 default:
420 return -EINVAL;
421 }
422
423 return 0;
424}
425
426static int fsl_sai_startup(struct snd_pcm_substream *substream,
427 struct snd_soc_dai *cpu_dai)
428{
Xiubo Li43550822013-12-17 11:24:38 +0800429 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
Nicolin Chenca3e35c2014-04-10 23:26:15 +0800430 struct device *dev = &sai->pdev->dev;
Xiubo Li78957fc2014-02-08 14:38:28 +0800431 u32 reg;
Nicolin Chenca3e35c2014-04-10 23:26:15 +0800432 int ret;
433
434 ret = clk_prepare_enable(sai->bus_clk);
435 if (ret) {
436 dev_err(dev, "failed to enable bus clock: %d\n", ret);
437 return ret;
438 }
Xiubo Li43550822013-12-17 11:24:38 +0800439
Xiubo Li78957fc2014-02-08 14:38:28 +0800440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
441 reg = FSL_SAI_TCR3;
442 else
443 reg = FSL_SAI_RCR3;
444
445 regmap_update_bits(sai->regmap, reg, FSL_SAI_CR3_TRCE,
446 FSL_SAI_CR3_TRCE);
447
448 return 0;
Xiubo Li43550822013-12-17 11:24:38 +0800449}
450
451static void fsl_sai_shutdown(struct snd_pcm_substream *substream,
452 struct snd_soc_dai *cpu_dai)
453{
454 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
Xiubo Li78957fc2014-02-08 14:38:28 +0800455 u32 reg;
Xiubo Li43550822013-12-17 11:24:38 +0800456
Xiubo Li78957fc2014-02-08 14:38:28 +0800457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
458 reg = FSL_SAI_TCR3;
459 else
460 reg = FSL_SAI_RCR3;
461
462 regmap_update_bits(sai->regmap, reg, FSL_SAI_CR3_TRCE,
463 ~FSL_SAI_CR3_TRCE);
Nicolin Chenca3e35c2014-04-10 23:26:15 +0800464
465 clk_disable_unprepare(sai->bus_clk);
Xiubo Li43550822013-12-17 11:24:38 +0800466}
467
468static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
469 .set_sysclk = fsl_sai_set_dai_sysclk,
470 .set_fmt = fsl_sai_set_dai_fmt,
471 .hw_params = fsl_sai_hw_params,
472 .trigger = fsl_sai_trigger,
473 .startup = fsl_sai_startup,
474 .shutdown = fsl_sai_shutdown,
475};
476
477static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
478{
479 struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
Xiubo Lie6dc12d2013-12-25 11:20:14 +0800480
Nicolin Chen8abba5d2014-04-01 11:17:07 +0800481 regmap_update_bits(sai->regmap, FSL_SAI_TCSR, 0xffffffff, 0x0);
482 regmap_update_bits(sai->regmap, FSL_SAI_RCSR, 0xffffffff, 0x0);
Xiubo Li78957fc2014-02-08 14:38:28 +0800483 regmap_update_bits(sai->regmap, FSL_SAI_TCR1, FSL_SAI_CR1_RFW_MASK,
484 FSL_SAI_MAXBURST_TX * 2);
485 regmap_update_bits(sai->regmap, FSL_SAI_RCR1, FSL_SAI_CR1_RFW_MASK,
486 FSL_SAI_MAXBURST_RX - 1);
Xiubo Li43550822013-12-17 11:24:38 +0800487
Xiubo Lidd9f4062013-12-20 12:35:33 +0800488 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
489 &sai->dma_params_rx);
Xiubo Li43550822013-12-17 11:24:38 +0800490
491 snd_soc_dai_set_drvdata(cpu_dai, sai);
492
493 return 0;
494}
495
Xiubo Li43550822013-12-17 11:24:38 +0800496static struct snd_soc_dai_driver fsl_sai_dai = {
497 .probe = fsl_sai_dai_probe,
Xiubo Li43550822013-12-17 11:24:38 +0800498 .playback = {
499 .channels_min = 1,
500 .channels_max = 2,
501 .rates = SNDRV_PCM_RATE_8000_96000,
502 .formats = FSL_SAI_FORMATS,
503 },
504 .capture = {
505 .channels_min = 1,
506 .channels_max = 2,
507 .rates = SNDRV_PCM_RATE_8000_96000,
508 .formats = FSL_SAI_FORMATS,
509 },
510 .ops = &fsl_sai_pcm_dai_ops,
511};
512
513static const struct snd_soc_component_driver fsl_component = {
514 .name = "fsl-sai",
515};
516
Xiubo Li78957fc2014-02-08 14:38:28 +0800517static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
518{
519 switch (reg) {
520 case FSL_SAI_TCSR:
521 case FSL_SAI_TCR1:
522 case FSL_SAI_TCR2:
523 case FSL_SAI_TCR3:
524 case FSL_SAI_TCR4:
525 case FSL_SAI_TCR5:
526 case FSL_SAI_TFR:
527 case FSL_SAI_TMR:
528 case FSL_SAI_RCSR:
529 case FSL_SAI_RCR1:
530 case FSL_SAI_RCR2:
531 case FSL_SAI_RCR3:
532 case FSL_SAI_RCR4:
533 case FSL_SAI_RCR5:
534 case FSL_SAI_RDR:
535 case FSL_SAI_RFR:
536 case FSL_SAI_RMR:
537 return true;
538 default:
539 return false;
540 }
541}
542
543static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
544{
545 switch (reg) {
546 case FSL_SAI_TFR:
547 case FSL_SAI_RFR:
548 case FSL_SAI_TDR:
549 case FSL_SAI_RDR:
550 return true;
551 default:
552 return false;
553 }
554
555}
556
557static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
558{
559 switch (reg) {
560 case FSL_SAI_TCSR:
561 case FSL_SAI_TCR1:
562 case FSL_SAI_TCR2:
563 case FSL_SAI_TCR3:
564 case FSL_SAI_TCR4:
565 case FSL_SAI_TCR5:
566 case FSL_SAI_TDR:
567 case FSL_SAI_TMR:
568 case FSL_SAI_RCSR:
569 case FSL_SAI_RCR1:
570 case FSL_SAI_RCR2:
571 case FSL_SAI_RCR3:
572 case FSL_SAI_RCR4:
573 case FSL_SAI_RCR5:
574 case FSL_SAI_RMR:
575 return true;
576 default:
577 return false;
578 }
579}
580
581static struct regmap_config fsl_sai_regmap_config = {
582 .reg_bits = 32,
583 .reg_stride = 4,
584 .val_bits = 32,
585
586 .max_register = FSL_SAI_RMR,
587 .readable_reg = fsl_sai_readable_reg,
588 .volatile_reg = fsl_sai_volatile_reg,
589 .writeable_reg = fsl_sai_writeable_reg,
590};
591
Xiubo Li43550822013-12-17 11:24:38 +0800592static int fsl_sai_probe(struct platform_device *pdev)
593{
Nicolin Chen4e3a99f2013-12-20 16:41:05 +0800594 struct device_node *np = pdev->dev.of_node;
Xiubo Li43550822013-12-17 11:24:38 +0800595 struct fsl_sai *sai;
596 struct resource *res;
Xiubo Li78957fc2014-02-08 14:38:28 +0800597 void __iomem *base;
Nicolin Chenca3e35c2014-04-10 23:26:15 +0800598 char tmp[8];
599 int irq, ret, i;
Xiubo Li43550822013-12-17 11:24:38 +0800600
601 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
602 if (!sai)
603 return -ENOMEM;
604
Nicolin Chene2681a12014-03-27 19:06:59 +0800605 sai->pdev = pdev;
606
Nicolin Chenc7540642014-04-01 19:34:09 +0800607 if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx6sx-sai"))
608 sai->sai_on_imx = true;
609
Xiubo Li78957fc2014-02-08 14:38:28 +0800610 sai->big_endian_regs = of_property_read_bool(np, "big-endian-regs");
611 if (sai->big_endian_regs)
612 fsl_sai_regmap_config.val_format_endian = REGMAP_ENDIAN_BIG;
Xiubo Li43550822013-12-17 11:24:38 +0800613
Xiubo Li78957fc2014-02-08 14:38:28 +0800614 sai->big_endian_data = of_property_read_bool(np, "big-endian-data");
615
616 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
617 base = devm_ioremap_resource(&pdev->dev, res);
618 if (IS_ERR(base))
619 return PTR_ERR(base);
620
621 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
Nicolin Chenca3e35c2014-04-10 23:26:15 +0800622 "bus", base, &fsl_sai_regmap_config);
623
624 /* Compatible with old DTB cases */
625 if (IS_ERR(sai->regmap))
626 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
627 "sai", base, &fsl_sai_regmap_config);
Xiubo Li78957fc2014-02-08 14:38:28 +0800628 if (IS_ERR(sai->regmap)) {
629 dev_err(&pdev->dev, "regmap init failed\n");
630 return PTR_ERR(sai->regmap);
Xiubo Li43550822013-12-17 11:24:38 +0800631 }
632
Nicolin Chenca3e35c2014-04-10 23:26:15 +0800633 /* No error out for old DTB cases but only mark the clock NULL */
634 sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
635 if (IS_ERR(sai->bus_clk)) {
636 dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
637 PTR_ERR(sai->bus_clk));
638 sai->bus_clk = NULL;
639 }
640
641 for (i = 0; i < FSL_SAI_MCLK_MAX; i++) {
642 sprintf(tmp, "mclk%d", i + 1);
643 sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
644 if (IS_ERR(sai->mclk_clk[i])) {
645 dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
646 i + 1, PTR_ERR(sai->mclk_clk[i]));
647 sai->mclk_clk[i] = NULL;
648 }
649 }
650
Nicolin Chene2681a12014-03-27 19:06:59 +0800651 irq = platform_get_irq(pdev, 0);
652 if (irq < 0) {
653 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
654 return irq;
655 }
656
657 ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, 0, np->name, sai);
658 if (ret) {
659 dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
660 return ret;
661 }
662
Xiubo Li43550822013-12-17 11:24:38 +0800663 sai->dma_params_rx.addr = res->start + FSL_SAI_RDR;
664 sai->dma_params_tx.addr = res->start + FSL_SAI_TDR;
665 sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
666 sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
667
Xiubo Li43550822013-12-17 11:24:38 +0800668 platform_set_drvdata(pdev, sai);
669
670 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
671 &fsl_sai_dai, 1);
672 if (ret)
673 return ret;
674
Nicolin Chenc7540642014-04-01 19:34:09 +0800675 if (sai->sai_on_imx)
676 return imx_pcm_dma_init(pdev);
677 else
678 return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL,
679 SND_DMAENGINE_PCM_FLAG_NO_RESIDUE);
Xiubo Li43550822013-12-17 11:24:38 +0800680}
681
682static const struct of_device_id fsl_sai_ids[] = {
683 { .compatible = "fsl,vf610-sai", },
Nicolin Chenc7540642014-04-01 19:34:09 +0800684 { .compatible = "fsl,imx6sx-sai", },
Xiubo Li43550822013-12-17 11:24:38 +0800685 { /* sentinel */ }
686};
687
688static struct platform_driver fsl_sai_driver = {
689 .probe = fsl_sai_probe,
Xiubo Li43550822013-12-17 11:24:38 +0800690 .driver = {
691 .name = "fsl-sai",
692 .owner = THIS_MODULE,
693 .of_match_table = fsl_sai_ids,
694 },
695};
696module_platform_driver(fsl_sai_driver);
697
698MODULE_DESCRIPTION("Freescale Soc SAI Interface");
699MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
700MODULE_ALIAS("platform:fsl-sai");
701MODULE_LICENSE("GPL");