blob: 7fd224bb732408bd52fbd267f0f8f97efbc24006 [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>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/dma-mapping.h>
24#include <linux/clk.h>
25#include <linux/delay.h>
Dong Aisheng76067542011-09-07 20:51:50 +080026#include <linux/time.h>
Huang Shijie39468602012-02-16 14:17:32 +080027#include <linux/fsl/mxs-dma.h>
Shawn Guof7558652012-05-06 23:00:50 +080028#include <linux/pinctrl/consumer.h>
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080029#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/pcm_params.h>
32#include <sound/soc.h>
Dong Aisheng76067542011-09-07 20:51:50 +080033#include <sound/saif.h>
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080034#include <asm/mach-types.h>
35#include <mach/hardware.h>
36#include <mach/mxs.h>
37
38#include "mxs-saif.h"
39
40static struct mxs_saif *mxs_saif[2];
41
Dong Aisheng76067542011-09-07 20:51:50 +080042/*
43 * SAIF is a little different with other normal SOC DAIs on clock using.
44 *
45 * For MXS, two SAIF modules are instantiated on-chip.
46 * Each SAIF has a set of clock pins and can be operating in master
47 * mode simultaneously if they are connected to different off-chip codecs.
48 * Also, one of the two SAIFs can master or drive the clock pins while the
49 * other SAIF, in slave mode, receives clocking from the master SAIF.
50 * This also means that both SAIFs must operate at the same sample rate.
51 *
52 * We abstract this as each saif has a master, the master could be
53 * himself or other saifs. In the generic saif driver, saif does not need
54 * to know the different clkmux. Saif only needs to know who is his master
55 * and operating his master to generate the proper clock rate for him.
56 * The master id is provided in mach-specific layer according to different
57 * clkmux setting.
58 */
59
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080060static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
61 int clk_id, unsigned int freq, int dir)
62{
63 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
64
65 switch (clk_id) {
66 case MXS_SAIF_MCLK:
67 saif->mclk = freq;
68 break;
69 default:
70 return -EINVAL;
71 }
72 return 0;
73}
74
75/*
Dong Aisheng76067542011-09-07 20:51:50 +080076 * Since SAIF may work on EXTMASTER mode, IOW, it's working BITCLK&LRCLK
77 * is provided by other SAIF, we provide a interface here to get its master
78 * from its master_id.
79 * Note that the master could be himself.
80 */
81static inline struct mxs_saif *mxs_saif_get_master(struct mxs_saif * saif)
82{
83 return mxs_saif[saif->master_id];
84}
85
86/*
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080087 * Set SAIF clock and MCLK
88 */
89static int mxs_saif_set_clk(struct mxs_saif *saif,
90 unsigned int mclk,
91 unsigned int rate)
92{
93 u32 scr;
94 int ret;
Dong Aisheng76067542011-09-07 20:51:50 +080095 struct mxs_saif *master_saif;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +080096
Dong Aisheng76067542011-09-07 20:51:50 +080097 dev_dbg(saif->dev, "mclk %d rate %d\n", mclk, rate);
98
99 /* Set master saif to generate proper clock */
100 master_saif = mxs_saif_get_master(saif);
101 if (!master_saif)
102 return -EINVAL;
103
104 dev_dbg(saif->dev, "master saif%d\n", master_saif->id);
105
106 /* Checking if can playback and capture simutaneously */
107 if (master_saif->ongoing && rate != master_saif->cur_rate) {
108 dev_err(saif->dev,
109 "can not change clock, master saif%d(rate %d) is ongoing\n",
110 master_saif->id, master_saif->cur_rate);
111 return -EINVAL;
112 }
113
114 scr = __raw_readl(master_saif->base + SAIF_CTRL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800115 scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
116 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
117
118 /*
119 * Set SAIF clock
120 *
121 * The SAIF clock should be either 384*fs or 512*fs.
122 * If MCLK is used, the SAIF clk ratio need to match mclk ratio.
123 * For 32x mclk, set saif clk as 512*fs.
124 * For 48x mclk, set saif clk as 384*fs.
125 *
126 * If MCLK is not used, we just set saif clk to 512*fs.
127 */
Fabio Estevam6b35f922012-01-19 10:23:22 -0200128 clk_prepare_enable(master_saif->clk);
129
Dong Aisheng76067542011-09-07 20:51:50 +0800130 if (master_saif->mclk_in_use) {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800131 if (mclk % 32 == 0) {
132 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
Dong Aisheng76067542011-09-07 20:51:50 +0800133 ret = clk_set_rate(master_saif->clk, 512 * rate);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800134 } else if (mclk % 48 == 0) {
135 scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
Dong Aisheng76067542011-09-07 20:51:50 +0800136 ret = clk_set_rate(master_saif->clk, 384 * rate);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800137 } else {
138 /* SAIF MCLK should be either 32x or 48x */
Fabio Estevam6b35f922012-01-19 10:23:22 -0200139 clk_disable_unprepare(master_saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800140 return -EINVAL;
141 }
142 } else {
Dong Aisheng76067542011-09-07 20:51:50 +0800143 ret = clk_set_rate(master_saif->clk, 512 * rate);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800144 scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
145 }
146
Fabio Estevam6b35f922012-01-19 10:23:22 -0200147 clk_disable_unprepare(master_saif->clk);
148
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800149 if (ret)
150 return ret;
151
Dong Aisheng76067542011-09-07 20:51:50 +0800152 master_saif->cur_rate = rate;
153
154 if (!master_saif->mclk_in_use) {
155 __raw_writel(scr, master_saif->base + SAIF_CTRL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800156 return 0;
157 }
158
159 /*
160 * Program the over-sample rate for MCLK output
161 *
162 * The available MCLK range is 32x, 48x... 512x. The rate
163 * could be from 8kHz to 192kH.
164 */
165 switch (mclk / rate) {
166 case 32:
167 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
168 break;
169 case 64:
170 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
171 break;
172 case 128:
173 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
174 break;
175 case 256:
176 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
177 break;
178 case 512:
179 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
180 break;
181 case 48:
182 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
183 break;
184 case 96:
185 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
186 break;
187 case 192:
188 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
189 break;
190 case 384:
191 scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
192 break;
193 default:
194 return -EINVAL;
195 }
196
Dong Aisheng76067542011-09-07 20:51:50 +0800197 __raw_writel(scr, master_saif->base + SAIF_CTRL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800198
199 return 0;
200}
201
202/*
203 * Put and disable MCLK.
204 */
205int mxs_saif_put_mclk(unsigned int saif_id)
206{
207 struct mxs_saif *saif = mxs_saif[saif_id];
208 u32 stat;
209
210 if (!saif)
211 return -EINVAL;
212
213 stat = __raw_readl(saif->base + SAIF_STAT);
214 if (stat & BM_SAIF_STAT_BUSY) {
215 dev_err(saif->dev, "error: busy\n");
216 return -EBUSY;
217 }
218
Shawn Guo67939b22011-12-20 14:15:44 +0800219 clk_disable_unprepare(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800220
221 /* disable MCLK output */
222 __raw_writel(BM_SAIF_CTRL_CLKGATE,
223 saif->base + SAIF_CTRL + MXS_SET_ADDR);
224 __raw_writel(BM_SAIF_CTRL_RUN,
225 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
226
227 saif->mclk_in_use = 0;
228 return 0;
229}
230
231/*
232 * Get MCLK and set clock rate, then enable it
233 *
234 * This interface is used for codecs who are using MCLK provided
235 * by saif.
236 */
237int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
238 unsigned int rate)
239{
240 struct mxs_saif *saif = mxs_saif[saif_id];
241 u32 stat;
242 int ret;
Dong Aisheng76067542011-09-07 20:51:50 +0800243 struct mxs_saif *master_saif;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800244
245 if (!saif)
246 return -EINVAL;
247
Dong Aishengbbe8ff52011-08-21 23:45:40 +0800248 /* Clear Reset */
249 __raw_writel(BM_SAIF_CTRL_SFTRST,
250 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
251
252 /* FIXME: need clear clk gate for register r/w */
253 __raw_writel(BM_SAIF_CTRL_CLKGATE,
254 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
255
Dong Aisheng76067542011-09-07 20:51:50 +0800256 master_saif = mxs_saif_get_master(saif);
257 if (saif != master_saif) {
258 dev_err(saif->dev, "can not get mclk from a non-master saif\n");
259 return -EINVAL;
260 }
261
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800262 stat = __raw_readl(saif->base + SAIF_STAT);
263 if (stat & BM_SAIF_STAT_BUSY) {
264 dev_err(saif->dev, "error: busy\n");
265 return -EBUSY;
266 }
267
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800268 saif->mclk_in_use = 1;
269 ret = mxs_saif_set_clk(saif, mclk, rate);
270 if (ret)
271 return ret;
272
Shawn Guo67939b22011-12-20 14:15:44 +0800273 ret = clk_prepare_enable(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800274 if (ret)
275 return ret;
276
277 /* enable MCLK output */
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800278 __raw_writel(BM_SAIF_CTRL_RUN,
279 saif->base + SAIF_CTRL + MXS_SET_ADDR);
280
281 return 0;
282}
283
284/*
285 * SAIF DAI format configuration.
286 * Should only be called when port is inactive.
287 */
288static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
289{
290 u32 scr, stat;
291 u32 scr0;
292 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
293
294 stat = __raw_readl(saif->base + SAIF_STAT);
295 if (stat & BM_SAIF_STAT_BUSY) {
296 dev_err(cpu_dai->dev, "error: busy\n");
297 return -EBUSY;
298 }
299
300 scr0 = __raw_readl(saif->base + SAIF_CTRL);
301 scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
302 & ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
303 scr = 0;
304
305 /* DAI mode */
306 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
307 case SND_SOC_DAIFMT_I2S:
308 /* data frame low 1clk before data */
309 scr |= BM_SAIF_CTRL_DELAY;
310 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
311 break;
312 case SND_SOC_DAIFMT_LEFT_J:
313 /* data frame high with data */
314 scr &= ~BM_SAIF_CTRL_DELAY;
315 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
316 scr &= ~BM_SAIF_CTRL_JUSTIFY;
317 break;
318 default:
319 return -EINVAL;
320 }
321
322 /* DAI clock inversion */
323 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
324 case SND_SOC_DAIFMT_IB_IF:
325 scr |= BM_SAIF_CTRL_BITCLK_EDGE;
326 scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
327 break;
328 case SND_SOC_DAIFMT_IB_NF:
329 scr |= BM_SAIF_CTRL_BITCLK_EDGE;
330 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
331 break;
332 case SND_SOC_DAIFMT_NB_IF:
333 scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
334 scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
335 break;
336 case SND_SOC_DAIFMT_NB_NF:
337 scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
338 scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
339 break;
340 }
341
342 /*
343 * Note: We simply just support master mode since SAIF TX can only
344 * work as master.
Dong Aisheng76067542011-09-07 20:51:50 +0800345 * Here the master is relative to codec side.
346 * Saif internally could be slave when working on EXTMASTER mode.
347 * We just hide this to machine driver.
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800348 */
349 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
350 case SND_SOC_DAIFMT_CBS_CFS:
Dong Aisheng76067542011-09-07 20:51:50 +0800351 if (saif->id == saif->master_id)
352 scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
353 else
354 scr |= BM_SAIF_CTRL_SLAVE_MODE;
355
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800356 __raw_writel(scr | scr0, saif->base + SAIF_CTRL);
357 break;
358 default:
359 return -EINVAL;
360 }
361
362 return 0;
363}
364
365static int mxs_saif_startup(struct snd_pcm_substream *substream,
366 struct snd_soc_dai *cpu_dai)
367{
368 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
369 snd_soc_dai_set_dma_data(cpu_dai, substream, &saif->dma_param);
370
371 /* clear error status to 0 for each re-open */
372 saif->fifo_underrun = 0;
373 saif->fifo_overrun = 0;
374
375 /* Clear Reset for normal operations */
376 __raw_writel(BM_SAIF_CTRL_SFTRST,
377 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
378
Dong Aishengbbe8ff52011-08-21 23:45:40 +0800379 /* clear clock gate */
380 __raw_writel(BM_SAIF_CTRL_CLKGATE,
381 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
382
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800383 return 0;
384}
385
386/*
387 * Should only be called when port is inactive.
388 * although can be called multiple times by upper layers.
389 */
390static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
391 struct snd_pcm_hw_params *params,
392 struct snd_soc_dai *cpu_dai)
393{
394 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
395 u32 scr, stat;
396 int ret;
397
398 /* mclk should already be set */
399 if (!saif->mclk && saif->mclk_in_use) {
400 dev_err(cpu_dai->dev, "set mclk first\n");
401 return -EINVAL;
402 }
403
404 stat = __raw_readl(saif->base + SAIF_STAT);
405 if (stat & BM_SAIF_STAT_BUSY) {
406 dev_err(cpu_dai->dev, "error: busy\n");
407 return -EBUSY;
408 }
409
410 /*
411 * Set saif clk based on sample rate.
412 * If mclk is used, we also set mclk, if not, saif->mclk is
413 * default 0, means not used.
414 */
415 ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
416 if (ret) {
417 dev_err(cpu_dai->dev, "unable to get proper clk\n");
418 return ret;
419 }
420
421 scr = __raw_readl(saif->base + SAIF_CTRL);
422
423 scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
424 scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
425 switch (params_format(params)) {
426 case SNDRV_PCM_FORMAT_S16_LE:
427 scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
428 break;
429 case SNDRV_PCM_FORMAT_S20_3LE:
430 scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
431 scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
432 break;
433 case SNDRV_PCM_FORMAT_S24_LE:
434 scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
435 scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
436 break;
437 default:
438 return -EINVAL;
439 }
440
441 /* Tx/Rx config */
442 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
443 /* enable TX mode */
444 scr &= ~BM_SAIF_CTRL_READ_MODE;
445 } else {
446 /* enable RX mode */
447 scr |= BM_SAIF_CTRL_READ_MODE;
448 }
449
450 __raw_writel(scr, saif->base + SAIF_CTRL);
451 return 0;
452}
453
454static int mxs_saif_prepare(struct snd_pcm_substream *substream,
455 struct snd_soc_dai *cpu_dai)
456{
457 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
458
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800459 /* enable FIFO error irqs */
460 __raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
461 saif->base + SAIF_CTRL + MXS_SET_ADDR);
462
463 return 0;
464}
465
466static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
467 struct snd_soc_dai *cpu_dai)
468{
469 struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
Dong Aisheng76067542011-09-07 20:51:50 +0800470 struct mxs_saif *master_saif;
471 u32 delay;
472
473 master_saif = mxs_saif_get_master(saif);
474 if (!master_saif)
475 return -EINVAL;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800476
477 switch (cmd) {
478 case SNDRV_PCM_TRIGGER_START:
479 case SNDRV_PCM_TRIGGER_RESUME:
480 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
481 dev_dbg(cpu_dai->dev, "start\n");
482
Dong Aisheng76067542011-09-07 20:51:50 +0800483 clk_enable(master_saif->clk);
484 if (!master_saif->mclk_in_use)
485 __raw_writel(BM_SAIF_CTRL_RUN,
486 master_saif->base + SAIF_CTRL + MXS_SET_ADDR);
487
488 /*
489 * If the saif's master is not himself, we also need to enable
490 * itself clk for its internal basic logic to work.
491 */
492 if (saif != master_saif) {
493 clk_enable(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800494 __raw_writel(BM_SAIF_CTRL_RUN,
495 saif->base + SAIF_CTRL + MXS_SET_ADDR);
Dong Aisheng76067542011-09-07 20:51:50 +0800496 }
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800497
498 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
499 /*
500 * write a data to saif data register to trigger
501 * the transfer
502 */
503 __raw_writel(0, saif->base + SAIF_DATA);
504 } else {
505 /*
506 * read a data from saif data register to trigger
507 * the receive
508 */
509 __raw_readl(saif->base + SAIF_DATA);
510 }
511
Dong Aisheng76067542011-09-07 20:51:50 +0800512 master_saif->ongoing = 1;
513
514 dev_dbg(saif->dev, "CTRL 0x%x STAT 0x%x\n",
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800515 __raw_readl(saif->base + SAIF_CTRL),
516 __raw_readl(saif->base + SAIF_STAT));
517
Dong Aisheng76067542011-09-07 20:51:50 +0800518 dev_dbg(master_saif->dev, "CTRL 0x%x STAT 0x%x\n",
519 __raw_readl(master_saif->base + SAIF_CTRL),
520 __raw_readl(master_saif->base + SAIF_STAT));
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800521 break;
522 case SNDRV_PCM_TRIGGER_SUSPEND:
523 case SNDRV_PCM_TRIGGER_STOP:
524 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
525 dev_dbg(cpu_dai->dev, "stop\n");
526
Dong Aisheng76067542011-09-07 20:51:50 +0800527 /* wait a while for the current sample to complete */
528 delay = USEC_PER_SEC / master_saif->cur_rate;
529
530 if (!master_saif->mclk_in_use) {
531 __raw_writel(BM_SAIF_CTRL_RUN,
532 master_saif->base + SAIF_CTRL + MXS_CLR_ADDR);
533 udelay(delay);
534 }
535 clk_disable(master_saif->clk);
536
537 if (saif != master_saif) {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800538 __raw_writel(BM_SAIF_CTRL_RUN,
539 saif->base + SAIF_CTRL + MXS_CLR_ADDR);
Dong Aisheng76067542011-09-07 20:51:50 +0800540 udelay(delay);
541 clk_disable(saif->clk);
542 }
543
544 master_saif->ongoing = 0;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800545
546 break;
547 default:
548 return -EINVAL;
549 }
550
551 return 0;
552}
553
554#define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
555#define MXS_SAIF_FORMATS \
556 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
557 SNDRV_PCM_FMTBIT_S24_LE)
558
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100559static const struct snd_soc_dai_ops mxs_saif_dai_ops = {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800560 .startup = mxs_saif_startup,
561 .trigger = mxs_saif_trigger,
562 .prepare = mxs_saif_prepare,
563 .hw_params = mxs_saif_hw_params,
564 .set_sysclk = mxs_saif_set_dai_sysclk,
565 .set_fmt = mxs_saif_set_dai_fmt,
566};
567
568static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
569{
570 struct mxs_saif *saif = dev_get_drvdata(dai->dev);
571
572 snd_soc_dai_set_drvdata(dai, saif);
573
574 return 0;
575}
576
577static struct snd_soc_dai_driver mxs_saif_dai = {
578 .name = "mxs-saif",
579 .probe = mxs_saif_dai_probe,
580 .playback = {
581 .channels_min = 2,
582 .channels_max = 2,
583 .rates = MXS_SAIF_RATES,
584 .formats = MXS_SAIF_FORMATS,
585 },
586 .capture = {
587 .channels_min = 2,
588 .channels_max = 2,
589 .rates = MXS_SAIF_RATES,
590 .formats = MXS_SAIF_FORMATS,
591 },
592 .ops = &mxs_saif_dai_ops,
593};
594
595static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
596{
597 struct mxs_saif *saif = dev_id;
598 unsigned int stat;
599
600 stat = __raw_readl(saif->base + SAIF_STAT);
601 if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
602 BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
603 return IRQ_NONE;
604
605 if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
606 dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
607 __raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
608 saif->base + SAIF_STAT + MXS_CLR_ADDR);
609 }
610
611 if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
612 dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
613 __raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
614 saif->base + SAIF_STAT + MXS_CLR_ADDR);
615 }
616
617 dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
618 __raw_readl(saif->base + SAIF_CTRL),
619 __raw_readl(saif->base + SAIF_STAT));
620
621 return IRQ_HANDLED;
622}
623
624static int mxs_saif_probe(struct platform_device *pdev)
625{
Julia Lawall226d0f22011-10-18 17:06:39 +0200626 struct resource *iores, *dmares;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800627 struct mxs_saif *saif;
Dong Aisheng76067542011-09-07 20:51:50 +0800628 struct mxs_saif_platform_data *pdata;
Shawn Guof7558652012-05-06 23:00:50 +0800629 struct pinctrl *pinctrl;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800630 int ret = 0;
631
Julia Lawall0bb98ba2011-08-21 13:18:45 +0200632 if (pdev->id >= ARRAY_SIZE(mxs_saif))
633 return -EINVAL;
634
Julia Lawall830eb872012-02-10 09:17:01 +0100635 saif = devm_kzalloc(&pdev->dev, sizeof(*saif), GFP_KERNEL);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800636 if (!saif)
637 return -ENOMEM;
638
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800639 mxs_saif[pdev->id] = saif;
Dong Aisheng76067542011-09-07 20:51:50 +0800640 saif->id = pdev->id;
641
Dong Aisheng77882582011-11-22 23:52:21 +0800642 pdata = pdev->dev.platform_data;
643 if (pdata && !pdata->master_mode) {
644 saif->master_id = pdata->master_id;
Dong Aisheng76067542011-09-07 20:51:50 +0800645 if (saif->master_id < 0 ||
Dong Aisheng77882582011-11-22 23:52:21 +0800646 saif->master_id >= ARRAY_SIZE(mxs_saif) ||
647 saif->master_id == saif->id) {
648 dev_err(&pdev->dev, "get wrong master id\n");
Dong Aisheng76067542011-09-07 20:51:50 +0800649 return -EINVAL;
Dong Aisheng77882582011-11-22 23:52:21 +0800650 }
651 } else {
652 saif->master_id = saif->id;
Dong Aisheng76067542011-09-07 20:51:50 +0800653 }
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800654
Shawn Guof7558652012-05-06 23:00:50 +0800655 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
656 if (IS_ERR(pinctrl)) {
657 ret = PTR_ERR(pinctrl);
658 return ret;
659 }
660
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800661 saif->clk = clk_get(&pdev->dev, NULL);
662 if (IS_ERR(saif->clk)) {
663 ret = PTR_ERR(saif->clk);
664 dev_err(&pdev->dev, "Cannot get the clock: %d\n",
665 ret);
Julia Lawall830eb872012-02-10 09:17:01 +0100666 return ret;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800667 }
668
Julia Lawall226d0f22011-10-18 17:06:39 +0200669 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800670
Julia Lawall830eb872012-02-10 09:17:01 +0100671 saif->base = devm_request_and_ioremap(&pdev->dev, iores);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800672 if (!saif->base) {
673 dev_err(&pdev->dev, "ioremap failed\n");
674 ret = -ENODEV;
Julia Lawall830eb872012-02-10 09:17:01 +0100675 goto failed_get_resource;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800676 }
677
Julia Lawall226d0f22011-10-18 17:06:39 +0200678 dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
679 if (!dmares) {
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800680 ret = -ENODEV;
681 dev_err(&pdev->dev, "failed to get dma resource: %d\n",
682 ret);
Julia Lawall830eb872012-02-10 09:17:01 +0100683 goto failed_get_resource;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800684 }
Julia Lawall226d0f22011-10-18 17:06:39 +0200685 saif->dma_param.chan_num = dmares->start;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800686
687 saif->irq = platform_get_irq(pdev, 0);
688 if (saif->irq < 0) {
689 ret = saif->irq;
690 dev_err(&pdev->dev, "failed to get irq resource: %d\n",
691 ret);
Julia Lawall830eb872012-02-10 09:17:01 +0100692 goto failed_get_resource;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800693 }
694
695 saif->dev = &pdev->dev;
Julia Lawall830eb872012-02-10 09:17:01 +0100696 ret = devm_request_irq(&pdev->dev, saif->irq, mxs_saif_irq, 0,
697 "mxs-saif", saif);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800698 if (ret) {
699 dev_err(&pdev->dev, "failed to request irq\n");
Julia Lawall830eb872012-02-10 09:17:01 +0100700 goto failed_get_resource;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800701 }
702
703 saif->dma_param.chan_irq = platform_get_irq(pdev, 1);
704 if (saif->dma_param.chan_irq < 0) {
705 ret = saif->dma_param.chan_irq;
706 dev_err(&pdev->dev, "failed to get dma irq resource: %d\n",
707 ret);
Julia Lawall830eb872012-02-10 09:17:01 +0100708 goto failed_get_resource;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800709 }
710
711 platform_set_drvdata(pdev, saif);
712
713 ret = snd_soc_register_dai(&pdev->dev, &mxs_saif_dai);
714 if (ret) {
715 dev_err(&pdev->dev, "register DAI failed\n");
Julia Lawall830eb872012-02-10 09:17:01 +0100716 goto failed_get_resource;
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800717 }
718
719 saif->soc_platform_pdev = platform_device_alloc(
720 "mxs-pcm-audio", pdev->id);
721 if (!saif->soc_platform_pdev) {
722 ret = -ENOMEM;
723 goto failed_pdev_alloc;
724 }
725
726 platform_set_drvdata(saif->soc_platform_pdev, saif);
727 ret = platform_device_add(saif->soc_platform_pdev);
728 if (ret) {
729 dev_err(&pdev->dev, "failed to add soc platform device\n");
730 goto failed_pdev_add;
731 }
732
733 return 0;
734
735failed_pdev_add:
736 platform_device_put(saif->soc_platform_pdev);
737failed_pdev_alloc:
738 snd_soc_unregister_dai(&pdev->dev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800739failed_get_resource:
740 clk_put(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800741
742 return ret;
743}
744
745static int __devexit mxs_saif_remove(struct platform_device *pdev)
746{
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800747 struct mxs_saif *saif = platform_get_drvdata(pdev);
748
749 platform_device_unregister(saif->soc_platform_pdev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800750 snd_soc_unregister_dai(&pdev->dev);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800751 clk_put(saif->clk);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800752
753 return 0;
754}
755
756static struct platform_driver mxs_saif_driver = {
757 .probe = mxs_saif_probe,
758 .remove = __devexit_p(mxs_saif_remove),
759
760 .driver = {
761 .name = "mxs-saif",
762 .owner = THIS_MODULE,
763 },
764};
765
Axel Lin85aa0962011-11-24 14:21:29 +0800766module_platform_driver(mxs_saif_driver);
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800767
Dong Aisheng2a24f2c2011-07-21 12:36:56 +0800768MODULE_AUTHOR("Freescale Semiconductor, Inc.");
769MODULE_DESCRIPTION("MXS ASoC SAIF driver");
770MODULE_LICENSE("GPL");