blob: 752675da06585b9c0607cb84ef7780b77dc7a5f8 [file] [log] [blame]
Dong Aisheng2a24f2c2011-07-21 12:36:56 +08001/*
2 * Copyright 2011 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#include <linux/module.h>
20#include <linux/init.h>
Shawn Guo08641c72012-05-11 22:24:17 +080021#include <linux/of.h>
22#include <linux/of_device.h>
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080023#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/dma-mapping.h>
26#include <linux/clk.h>
27#include <linux/delay.h>
Dong Aisheng76067542011-09-07 20:51:50 +080028#include <linux/time.h>
Huang Shijie39468602012-02-16 14:17:32 +080029#include <linux/fsl/mxs-dma.h>
Shawn Guof7558652012-05-06 23:00:50 +080030#include <linux/pinctrl/consumer.h>
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080035#include <asm/mach-types.h>
36#include <mach/hardware.h>
37#include <mach/mxs.h>
38
39#include "mxs-saif.h"
40
41static struct mxs_saif *mxs_saif[2];
42
Dong Aisheng76067542011-09-07 20:51:50 +080043/*
44 * SAIF is a little different with other normal SOC DAIs on clock using.
45 *
46 * For MXS, two SAIF modules are instantiated on-chip.
47 * Each SAIF has a set of clock pins and can be operating in master
48 * mode simultaneously if they are connected to different off-chip codecs.
49 * Also, one of the two SAIFs can master or drive the clock pins while the
50 * other SAIF, in slave mode, receives clocking from the master SAIF.
51 * This also means that both SAIFs must operate at the same sample rate.
52 *
53 * We abstract this as each saif has a master, the master could be
54 * himself or other saifs. In the generic saif driver, saif does not need
55 * to know the different clkmux. Saif only needs to know who is his master
56 * and operating his master to generate the proper clock rate for him.
57 * The master id is provided in mach-specific layer according to different
58 * clkmux setting.
59 */
60
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080061static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
62 int clk_id, unsigned int freq, int dir)
63{
64 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
65
66 switch (clk_id) {
67 case MXS_SAIF_MCLK:
68 saif->mclk = freq;
69 break;
70 default:
71 return -EINVAL;
72 }
73 return 0;
74}
75
76/*
Dong Aisheng76067542011-09-07 20:51:50 +080077 * Since SAIF may work on EXTMASTER mode, IOW, it's working BITCLK&LRCLK
78 * is provided by other SAIF, we provide a interface here to get its master
79 * from its master_id.
80 * Note that the master could be himself.
81 */
82static inline struct mxs_saif *mxs_saif_get_master(struct mxs_saif * saif)
83{
84 return mxs_saif[saif->master_id];
85}
86
87/*
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080088 * Set SAIF clock and MCLK
89 */
90static int mxs_saif_set_clk(struct mxs_saif *saif,
91 unsigned int mclk,
92 unsigned int rate)
93{
94 u32 scr;
95 int ret;
Dong Aisheng76067542011-09-07 20:51:50 +080096 struct mxs_saif *master_saif;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080097
Dong Aisheng76067542011-09-07 20:51:50 +080098 dev_dbg(saif->dev, "mclk %d rate %d\n", mclk, rate);
99
100 /* Set master saif to generate proper clock */
101 master_saif = mxs_saif_get_master(saif);
102 if (!master_saif)
103 return -EINVAL;
104
105 dev_dbg(saif->dev, "master saif%d\n", master_saif->id);
106
107 /* Checking if can playback and capture simutaneously */
108 if (master_saif->ongoing && rate != master_saif->cur_rate) {
109 dev_err(saif->dev,
110 "can not change clock, master saif%d(rate %d) is ongoing\n",
111 master_saif->id, master_saif->cur_rate);
112 return -EINVAL;
113 }
114
115 scr = __raw_readl(master_saif->base + SAIF_CTRL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800116 scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
117 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
118
119 /*
120 * Set SAIF clock
121 *
122 * The SAIF clock should be either 384*fs or 512*fs.
123 * If MCLK is used, the SAIF clk ratio need to match mclk ratio.
124 * For 32x mclk, set saif clk as 512*fs.
125 * For 48x mclk, set saif clk as 384*fs.
126 *
127 * If MCLK is not used, we just set saif clk to 512*fs.
128 */
Fabio Estevam6b35f922012-01-19 10:23:22 -0200129 clk_prepare_enable(master_saif->clk);
130
Dong Aisheng76067542011-09-07 20:51:50 +0800131 if (master_saif->mclk_in_use) {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800132 if (mclk % 32 == 0) {
133 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
Dong Aisheng76067542011-09-07 20:51:50 +0800134 ret = clk_set_rate(master_saif->clk, 512 * rate);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800135 } else if (mclk % 48 == 0) {
136 scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
Dong Aisheng76067542011-09-07 20:51:50 +0800137 ret = clk_set_rate(master_saif->clk, 384 * rate);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800138 } else {
139 /* SAIF MCLK should be either 32x or 48x */
Fabio Estevam6b35f922012-01-19 10:23:22 -0200140 clk_disable_unprepare(master_saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800141 return -EINVAL;
142 }
143 } else {
Dong Aisheng76067542011-09-07 20:51:50 +0800144 ret = clk_set_rate(master_saif->clk, 512 * rate);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800145 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
146 }
147
Fabio Estevam6b35f922012-01-19 10:23:22 -0200148 clk_disable_unprepare(master_saif->clk);
149
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800150 if (ret)
151 return ret;
152
Dong Aisheng76067542011-09-07 20:51:50 +0800153 master_saif->cur_rate = rate;
154
155 if (!master_saif->mclk_in_use) {
156 __raw_writel(scr, master_saif->base + SAIF_CTRL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800157 return 0;
158 }
159
160 /*
161 * Program the over-sample rate for MCLK output
162 *
163 * The available MCLK range is 32x, 48x... 512x. The rate
164 * could be from 8kHz to 192kH.
165 */
166 switch (mclk / rate) {
167 case 32:
168 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
169 break;
170 case 64:
171 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
172 break;
173 case 128:
174 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
175 break;
176 case 256:
177 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
178 break;
179 case 512:
180 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
181 break;
182 case 48:
183 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
184 break;
185 case 96:
186 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
187 break;
188 case 192:
189 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
190 break;
191 case 384:
192 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
193 break;
194 default:
195 return -EINVAL;
196 }
197
Dong Aisheng76067542011-09-07 20:51:50 +0800198 __raw_writel(scr, master_saif->base + SAIF_CTRL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800199
200 return 0;
201}
202
203/*
204 * Put and disable MCLK.
205 */
206int mxs_saif_put_mclk(unsigned int saif_id)
207{
208 struct mxs_saif *saif = mxs_saif[saif_id];
209 u32 stat;
210
211 if (!saif)
212 return -EINVAL;
213
214 stat = __raw_readl(saif->base + SAIF_STAT);
215 if (stat & BM_SAIF_STAT_BUSY) {
216 dev_err(saif->dev, "error: busy\n");
217 return -EBUSY;
218 }
219
Shawn Guo67939b22011-12-20 14:15:44 +0800220 clk_disable_unprepare(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800221
222 /* disable MCLK output */
223 __raw_writel(BM_SAIF_CTRL_CLKGATE,
224 saif->base + SAIF_CTRL + MXS_SET_ADDR);
225 __raw_writel(BM_SAIF_CTRL_RUN,
226 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
227
228 saif->mclk_in_use = 0;
229 return 0;
230}
Lothar Waßmanncf7d0f02012-11-22 13:31:09 +0100231EXPORT_SYMBOL_GPL(mxs_saif_put_mclk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800232
233/*
234 * Get MCLK and set clock rate, then enable it
235 *
236 * This interface is used for codecs who are using MCLK provided
237 * by saif.
238 */
239int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
240 unsigned int rate)
241{
242 struct mxs_saif *saif = mxs_saif[saif_id];
243 u32 stat;
244 int ret;
Dong Aisheng76067542011-09-07 20:51:50 +0800245 struct mxs_saif *master_saif;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800246
247 if (!saif)
248 return -EINVAL;
249
Dong Aishengbbe8ff52011-08-21 23:45:40 +0800250 /* Clear Reset */
251 __raw_writel(BM_SAIF_CTRL_SFTRST,
252 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
253
254 /* FIXME: need clear clk gate for register r/w */
255 __raw_writel(BM_SAIF_CTRL_CLKGATE,
256 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
257
Dong Aisheng76067542011-09-07 20:51:50 +0800258 master_saif = mxs_saif_get_master(saif);
259 if (saif != master_saif) {
260 dev_err(saif->dev, "can not get mclk from a non-master saif\n");
261 return -EINVAL;
262 }
263
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800264 stat = __raw_readl(saif->base + SAIF_STAT);
265 if (stat & BM_SAIF_STAT_BUSY) {
266 dev_err(saif->dev, "error: busy\n");
267 return -EBUSY;
268 }
269
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800270 saif->mclk_in_use = 1;
271 ret = mxs_saif_set_clk(saif, mclk, rate);
272 if (ret)
273 return ret;
274
Shawn Guo67939b22011-12-20 14:15:44 +0800275 ret = clk_prepare_enable(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800276 if (ret)
277 return ret;
278
279 /* enable MCLK output */
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800280 __raw_writel(BM_SAIF_CTRL_RUN,
281 saif->base + SAIF_CTRL + MXS_SET_ADDR);
282
283 return 0;
284}
Lothar Waßmanncf7d0f02012-11-22 13:31:09 +0100285EXPORT_SYMBOL_GPL(mxs_saif_get_mclk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800286
287/*
288 * SAIF DAI format configuration.
289 * Should only be called when port is inactive.
290 */
291static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
292{
293 u32 scr, stat;
294 u32 scr0;
295 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
296
297 stat = __raw_readl(saif->base + SAIF_STAT);
298 if (stat & BM_SAIF_STAT_BUSY) {
299 dev_err(cpu_dai->dev, "error: busy\n");
300 return -EBUSY;
301 }
302
303 scr0 = __raw_readl(saif->base + SAIF_CTRL);
304 scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
305 & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
306 scr = 0;
307
308 /* DAI mode */
309 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
310 case SND_SOC_DAIFMT_I2S:
311 /* data frame low 1clk before data */
312 scr |= BM_SAIF_CTRL_DELAY;
313 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
314 break;
315 case SND_SOC_DAIFMT_LEFT_J:
316 /* data frame high with data */
317 scr &= ~BM_SAIF_CTRL_DELAY;
318 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
319 scr &= ~BM_SAIF_CTRL_JUSTIFY;
320 break;
321 default:
322 return -EINVAL;
323 }
324
325 /* DAI clock inversion */
326 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
327 case SND_SOC_DAIFMT_IB_IF:
328 scr |= BM_SAIF_CTRL_BITCLK_EDGE;
329 scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
330 break;
331 case SND_SOC_DAIFMT_IB_NF:
332 scr |= BM_SAIF_CTRL_BITCLK_EDGE;
333 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
334 break;
335 case SND_SOC_DAIFMT_NB_IF:
336 scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
337 scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
338 break;
339 case SND_SOC_DAIFMT_NB_NF:
340 scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
341 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
342 break;
343 }
344
345 /*
346 * Note: We simply just support master mode since SAIF TX can only
347 * work as master.
Dong Aisheng76067542011-09-07 20:51:50 +0800348 * Here the master is relative to codec side.
349 * Saif internally could be slave when working on EXTMASTER mode.
350 * We just hide this to machine driver.
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800351 */
352 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
353 case SND_SOC_DAIFMT_CBS_CFS:
Dong Aisheng76067542011-09-07 20:51:50 +0800354 if (saif->id == saif->master_id)
355 scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
356 else
357 scr |= BM_SAIF_CTRL_SLAVE_MODE;
358
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800359 __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
360 break;
361 default:
362 return -EINVAL;
363 }
364
365 return 0;
366}
367
368static int mxs_saif_startup(struct snd_pcm_substream *substream,
369 struct snd_soc_dai *cpu_dai)
370{
371 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
372 snd_soc_dai_set_dma_data(cpu_dai, substream, &saif->dma_param);
373
374 /* clear error status to 0 for each re-open */
375 saif->fifo_underrun = 0;
376 saif->fifo_overrun = 0;
377
378 /* Clear Reset for normal operations */
379 __raw_writel(BM_SAIF_CTRL_SFTRST,
380 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
381
Dong Aishengbbe8ff52011-08-21 23:45:40 +0800382 /* clear clock gate */
383 __raw_writel(BM_SAIF_CTRL_CLKGATE,
384 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
385
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800386 return 0;
387}
388
389/*
390 * Should only be called when port is inactive.
391 * although can be called multiple times by upper layers.
392 */
393static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
394 struct snd_pcm_hw_params *params,
395 struct snd_soc_dai *cpu_dai)
396{
397 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
Dong Aishengc2e1d902012-07-20 17:20:24 +0800398 struct mxs_saif *master_saif;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800399 u32 scr, stat;
400 int ret;
401
Dong Aishengc2e1d902012-07-20 17:20:24 +0800402 master_saif = mxs_saif_get_master(saif);
403 if (!master_saif)
404 return -EINVAL;
405
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800406 /* mclk should already be set */
407 if (!saif->mclk && saif->mclk_in_use) {
408 dev_err(cpu_dai->dev, "set mclk first\n");
409 return -EINVAL;
410 }
411
412 stat = __raw_readl(saif->base + SAIF_STAT);
413 if (stat & BM_SAIF_STAT_BUSY) {
414 dev_err(cpu_dai->dev, "error: busy\n");
415 return -EBUSY;
416 }
417
418 /*
419 * Set saif clk based on sample rate.
420 * If mclk is used, we also set mclk, if not, saif->mclk is
421 * default 0, means not used.
422 */
423 ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
424 if (ret) {
425 dev_err(cpu_dai->dev, "unable to get proper clk\n");
426 return ret;
427 }
428
Dong Aishengc2e1d902012-07-20 17:20:24 +0800429 /* prepare clk in hw_param, enable in trigger */
430 clk_prepare(saif->clk);
Dong Aishengd0ba4c02012-07-20 17:20:25 +0800431 if (saif != master_saif) {
432 /*
433 * Set an initial clock rate for the saif internal logic to work
434 * properly. This is important when working in EXTMASTER mode
435 * that uses the other saif's BITCLK&LRCLK but it still needs a
436 * basic clock which should be fast enough for the internal
437 * logic.
438 */
439 clk_enable(saif->clk);
440 ret = clk_set_rate(saif->clk, 24000000);
441 clk_disable(saif->clk);
442 if (ret)
443 return ret;
444
Dong Aishengc2e1d902012-07-20 17:20:24 +0800445 clk_prepare(master_saif->clk);
Dong Aishengd0ba4c02012-07-20 17:20:25 +0800446 }
Dong Aishengc2e1d902012-07-20 17:20:24 +0800447
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800448 scr = __raw_readl(saif->base + SAIF_CTRL);
449
450 scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
451 scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
452 switch (params_format(params)) {
453 case SNDRV_PCM_FORMAT_S16_LE:
454 scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
455 break;
456 case SNDRV_PCM_FORMAT_S20_3LE:
457 scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
458 scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
459 break;
460 case SNDRV_PCM_FORMAT_S24_LE:
461 scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
462 scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
463 break;
464 default:
465 return -EINVAL;
466 }
467
468 /* Tx/Rx config */
469 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
470 /* enable TX mode */
471 scr &= ~BM_SAIF_CTRL_READ_MODE;
472 } else {
473 /* enable RX mode */
474 scr |= BM_SAIF_CTRL_READ_MODE;
475 }
476
477 __raw_writel(scr, saif->base + SAIF_CTRL);
478 return 0;
479}
480
481static int mxs_saif_prepare(struct snd_pcm_substream *substream,
482 struct snd_soc_dai *cpu_dai)
483{
484 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
485
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800486 /* enable FIFO error irqs */
487 __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
488 saif->base + SAIF_CTRL + MXS_SET_ADDR);
489
490 return 0;
491}
492
493static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
494 struct snd_soc_dai *cpu_dai)
495{
496 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
Dong Aisheng76067542011-09-07 20:51:50 +0800497 struct mxs_saif *master_saif;
498 u32 delay;
499
500 master_saif = mxs_saif_get_master(saif);
501 if (!master_saif)
502 return -EINVAL;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800503
504 switch (cmd) {
505 case SNDRV_PCM_TRIGGER_START:
506 case SNDRV_PCM_TRIGGER_RESUME:
507 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
508 dev_dbg(cpu_dai->dev, "start\n");
509
Dong Aisheng76067542011-09-07 20:51:50 +0800510 clk_enable(master_saif->clk);
511 if (!master_saif->mclk_in_use)
512 __raw_writel(BM_SAIF_CTRL_RUN,
513 master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
514
515 /*
516 * If the saif's master is not himself, we also need to enable
517 * itself clk for its internal basic logic to work.
518 */
519 if (saif != master_saif) {
520 clk_enable(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800521 __raw_writel(BM_SAIF_CTRL_RUN,
522 saif->base + SAIF_CTRL + MXS_SET_ADDR);
Dong Aisheng76067542011-09-07 20:51:50 +0800523 }
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800524
525 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
526 /*
Fabio Estevamf55f1472012-11-01 15:57:11 -0200527 * write data to saif data register to trigger
528 * the transfer.
529 * For 24-bit format the 32-bit FIFO register stores
530 * only one channel, so we need to write twice.
531 * This is also safe for the other non 24-bit formats.
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800532 */
533 __raw_writel(0, saif->base + SAIF_DATA);
Fabio Estevamf55f1472012-11-01 15:57:11 -0200534 __raw_writel(0, saif->base + SAIF_DATA);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800535 } else {
536 /*
Fabio Estevamf55f1472012-11-01 15:57:11 -0200537 * read data from saif data register to trigger
538 * the receive.
539 * For 24-bit format the 32-bit FIFO register stores
540 * only one channel, so we need to read twice.
541 * This is also safe for the other non 24-bit formats.
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800542 */
543 __raw_readl(saif->base + SAIF_DATA);
Fabio Estevamf55f1472012-11-01 15:57:11 -0200544 __raw_readl(saif->base + SAIF_DATA);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800545 }
546
Dong Aisheng76067542011-09-07 20:51:50 +0800547 master_saif->ongoing = 1;
548
549 dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800550 __raw_readl(saif->base + SAIF_CTRL),
551 __raw_readl(saif->base + SAIF_STAT));
552
Dong Aisheng76067542011-09-07 20:51:50 +0800553 dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
554 __raw_readl(master_saif->base + SAIF_CTRL),
555 __raw_readl(master_saif->base + SAIF_STAT));
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800556 break;
557 case SNDRV_PCM_TRIGGER_SUSPEND:
558 case SNDRV_PCM_TRIGGER_STOP:
559 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
560 dev_dbg(cpu_dai->dev, "stop\n");
561
Dong Aisheng76067542011-09-07 20:51:50 +0800562 /* wait a while for the current sample to complete */
563 delay = USEC_PER_SEC / master_saif->cur_rate;
564
565 if (!master_saif->mclk_in_use) {
566 __raw_writel(BM_SAIF_CTRL_RUN,
567 master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
568 udelay(delay);
569 }
570 clk_disable(master_saif->clk);
571
572 if (saif != master_saif) {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800573 __raw_writel(BM_SAIF_CTRL_RUN,
574 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
Dong Aisheng76067542011-09-07 20:51:50 +0800575 udelay(delay);
576 clk_disable(saif->clk);
577 }
578
579 master_saif->ongoing = 0;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800580
581 break;
582 default:
583 return -EINVAL;
584 }
585
586 return 0;
587}
588
589#define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
590#define MXS_SAIF_FORMATS \
591 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
592 SNDRV_PCM_FMTBIT_S24_LE)
593
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100594static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800595 .startup = mxs_saif_startup,
596 .trigger = mxs_saif_trigger,
597 .prepare = mxs_saif_prepare,
598 .hw_params = mxs_saif_hw_params,
599 .set_sysclk = mxs_saif_set_dai_sysclk,
600 .set_fmt = mxs_saif_set_dai_fmt,
601};
602
603static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
604{
605 struct mxs_saif *saif = dev_get_drvdata(dai->dev);
606
607 snd_soc_dai_set_drvdata(dai, saif);
608
609 return 0;
610}
611
612static struct snd_soc_dai_driver mxs_saif_dai = {
613 .name = "mxs-saif",
614 .probe = mxs_saif_dai_probe,
615 .playback = {
616 .channels_min = 2,
617 .channels_max = 2,
618 .rates = MXS_SAIF_RATES,
619 .formats = MXS_SAIF_FORMATS,
620 },
621 .capture = {
622 .channels_min = 2,
623 .channels_max = 2,
624 .rates = MXS_SAIF_RATES,
625 .formats = MXS_SAIF_FORMATS,
626 },
627 .ops = &mxs_saif_dai_ops,
628};
629
630static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
631{
632 struct mxs_saif *saif = dev_id;
633 unsigned int stat;
634
635 stat = __raw_readl(saif->base + SAIF_STAT);
636 if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
637 BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
638 return IRQ_NONE;
639
640 if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
641 dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
642 __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
643 saif->base + SAIF_STAT + MXS_CLR_ADDR);
644 }
645
646 if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
647 dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
648 __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
649 saif->base + SAIF_STAT + MXS_CLR_ADDR);
650 }
651
652 dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
653 __raw_readl(saif->base + SAIF_CTRL),
654 __raw_readl(saif->base + SAIF_STAT));
655
656 return IRQ_HANDLED;
657}
658
Bill Pembertonfd582732012-12-07 09:26:27 -0500659static int mxs_saif_probe(struct platform_device *pdev)
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800660{
Shawn Guo08641c72012-05-11 22:24:17 +0800661 struct device_node *np = pdev->dev.of_node;
Julia Lawall226d0f22011-10-18 17:06:39 +0200662 struct resource *iores, *dmares;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800663 struct mxs_saif *saif;
Shawn Guof7558652012-05-06 23:00:50 +0800664 struct pinctrl *pinctrl;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800665 int ret = 0;
Fabio Estevam4498a3c2012-11-14 18:28:58 -0200666 struct device_node *master;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800667
Fabio Estevam4498a3c2012-11-14 18:28:58 -0200668 if (!np)
Julia Lawall0bb98ba2011-08-21 13:18:45 +0200669 return -EINVAL;
670
Julia Lawall830eb872012-02-10 09:17:01 +0100671 saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800672 if (!saif)
673 return -ENOMEM;
674
Fabio Estevam4498a3c2012-11-14 18:28:58 -0200675 saif->id = of_alias_get_id(np, "saif");
676 if (saif->id < 0)
677 return saif->id;
678 /*
679 * If there is no "fsl,saif-master" phandle, it's a saif
680 * master. Otherwise, it's a slave and its phandle points
681 * to the master.
682 */
683 master = of_parse_phandle(np, "fsl,saif-master", 0);
684 if (!master) {
685 saif->master_id = saif->id;
Dong Aisheng77882582011-11-22 23:52:21 +0800686 } else {
Fabio Estevam4498a3c2012-11-14 18:28:58 -0200687 saif->master_id = of_alias_get_id(master, "saif");
688 if (saif->master_id < 0)
689 return saif->master_id;
Dong Aisheng76067542011-09-07 20:51:50 +0800690 }
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800691
Shawn Guo08641c72012-05-11 22:24:17 +0800692 if (saif->master_id < 0 || saif->master_id >= ARRAY_SIZE(mxs_saif)) {
693 dev_err(&pdev->dev, "get wrong master id\n");
694 return -EINVAL;
695 }
696
697 mxs_saif[saif->id] = saif;
698
Shawn Guof7558652012-05-06 23:00:50 +0800699 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
700 if (IS_ERR(pinctrl)) {
701 ret = PTR_ERR(pinctrl);
702 return ret;
703 }
704
Fabio Estevam730963f2012-08-07 01:29:43 -0300705 saif->clk = devm_clk_get(&pdev->dev, NULL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800706 if (IS_ERR(saif->clk)) {
707 ret = PTR_ERR(saif->clk);
708 dev_err(&pdev->dev, "Cannot get the clock: %d\n",
709 ret);
Julia Lawall830eb872012-02-10 09:17:01 +0100710 return ret;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800711 }
712
Julia Lawall226d0f22011-10-18 17:06:39 +0200713 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800714
Julia Lawall830eb872012-02-10 09:17:01 +0100715 saif->base = devm_request_and_ioremap(&pdev->dev, iores);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800716 if (!saif->base) {
717 dev_err(&pdev->dev, "ioremap failed\n");
Fabio Estevam730963f2012-08-07 01:29:43 -0300718 return -ENODEV;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800719 }
720
Julia Lawall226d0f22011-10-18 17:06:39 +0200721 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
722 if (!dmares) {
Shawn Guo08641c72012-05-11 22:24:17 +0800723 /*
724 * TODO: This is a temporary solution and should be changed
725 * to use generic DMA binding later when the helplers get in.
726 */
727 ret = of_property_read_u32(np, "fsl,saif-dma-channel",
728 &saif->dma_param.chan_num);
729 if (ret) {
730 dev_err(&pdev->dev, "failed to get dma channel\n");
Fabio Estevam730963f2012-08-07 01:29:43 -0300731 return ret;
Shawn Guo08641c72012-05-11 22:24:17 +0800732 }
733 } else {
734 saif->dma_param.chan_num = dmares->start;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800735 }
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800736
737 saif->irq = platform_get_irq(pdev, 0);
738 if (saif->irq < 0) {
739 ret = saif->irq;
740 dev_err(&pdev->dev, "failed to get irq resource: %d\n",
741 ret);
Fabio Estevam730963f2012-08-07 01:29:43 -0300742 return ret;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800743 }
744
745 saif->dev = &pdev->dev;
Julia Lawall830eb872012-02-10 09:17:01 +0100746 ret = devm_request_irq(&pdev->dev, saif->irq, mxs_saif_irq, 0,
747 "mxs-saif", saif);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800748 if (ret) {
749 dev_err(&pdev->dev, "failed to request irq\n");
Fabio Estevam730963f2012-08-07 01:29:43 -0300750 return ret;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800751 }
752
753 saif->dma_param.chan_irq = platform_get_irq(pdev, 1);
754 if (saif->dma_param.chan_irq < 0) {
755 ret = saif->dma_param.chan_irq;
756 dev_err(&pdev->dev, "failed to get dma irq resource: %d\n",
757 ret);
Fabio Estevam730963f2012-08-07 01:29:43 -0300758 return ret;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800759 }
760
761 platform_set_drvdata(pdev, saif);
762
763 ret = snd_soc_register_dai(&pdev->dev, &mxs_saif_dai);
764 if (ret) {
765 dev_err(&pdev->dev, "register DAI failed\n");
Fabio Estevam730963f2012-08-07 01:29:43 -0300766 return ret;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800767 }
768
Shawn Guo4da3fe72012-05-11 22:24:16 +0800769 ret = mxs_pcm_platform_register(&pdev->dev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800770 if (ret) {
Shawn Guo4da3fe72012-05-11 22:24:16 +0800771 dev_err(&pdev->dev, "register PCM failed: %d\n", ret);
772 goto failed_pdev_alloc;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800773 }
774
775 return 0;
776
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800777failed_pdev_alloc:
778 snd_soc_unregister_dai(&pdev->dev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800779
780 return ret;
781}
782
Bill Pembertonfd582732012-12-07 09:26:27 -0500783static int mxs_saif_remove(struct platform_device *pdev)
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800784{
Shawn Guo4da3fe72012-05-11 22:24:16 +0800785 mxs_pcm_platform_unregister(&pdev->dev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800786 snd_soc_unregister_dai(&pdev->dev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800787
788 return 0;
789}
790
Shawn Guo08641c72012-05-11 22:24:17 +0800791static const struct of_device_id mxs_saif_dt_ids[] = {
792 { .compatible = "fsl,imx28-saif", },
793 { /* sentinel */ }
794};
795MODULE_DEVICE_TABLE(of, mxs_saif_dt_ids);
796
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800797static struct platform_driver mxs_saif_driver = {
798 .probe = mxs_saif_probe,
Bill Pembertonfd582732012-12-07 09:26:27 -0500799 .remove = mxs_saif_remove,
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800800
801 .driver = {
802 .name = "mxs-saif",
803 .owner = THIS_MODULE,
Shawn Guo08641c72012-05-11 22:24:17 +0800804 .of_match_table = mxs_saif_dt_ids,
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800805 },
806};
807
Axel Lin85aa0962011-11-24 14:21:29 +0800808module_platform_driver(mxs_saif_driver);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800809
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800810MODULE_AUTHOR("Freescale Semiconductor, Inc.");
811MODULE_DESCRIPTION("MXS ASoC SAIF driver");
812MODULE_LICENSE("GPL");
Fabio Estevam9f4c3f12012-10-31 01:20:05 -0200813MODULE_ALIAS("platform:mxs-saif");