2
* Copyright 2011 Freescale Semiconductor, Inc.
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.
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.
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.
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>
26
#include <sound/core.h>
27
#include <sound/pcm.h>
28
#include <sound/pcm_params.h>
29
#include <sound/soc.h>
31
#include <asm/mach-types.h>
32
#include <mach/hardware.h>
37
static struct mxs_saif *mxs_saif[2];
39
static int mxs_saif_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
40
int clk_id, unsigned int freq, int dir)
42
struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
55
* Set SAIF clock and MCLK
57
static int mxs_saif_set_clk(struct mxs_saif *saif,
64
scr = __raw_readl(saif->base + SAIF_CTRL);
65
scr &= ~BM_SAIF_CTRL_BITCLK_MULT_RATE;
66
scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
71
* The SAIF clock should be either 384*fs or 512*fs.
72
* If MCLK is used, the SAIF clk ratio need to match mclk ratio.
73
* For 32x mclk, set saif clk as 512*fs.
74
* For 48x mclk, set saif clk as 384*fs.
76
* If MCLK is not used, we just set saif clk to 512*fs.
78
if (saif->mclk_in_use) {
80
scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
81
ret = clk_set_rate(saif->clk, 512 * rate);
82
} else if (mclk % 48 == 0) {
83
scr |= BM_SAIF_CTRL_BITCLK_BASE_RATE;
84
ret = clk_set_rate(saif->clk, 384 * rate);
86
/* SAIF MCLK should be either 32x or 48x */
90
ret = clk_set_rate(saif->clk, 512 * rate);
91
scr &= ~BM_SAIF_CTRL_BITCLK_BASE_RATE;
97
if (!saif->mclk_in_use) {
98
__raw_writel(scr, saif->base + SAIF_CTRL);
103
* Program the over-sample rate for MCLK output
105
* The available MCLK range is 32x, 48x... 512x. The rate
106
* could be from 8kHz to 192kH.
108
switch (mclk / rate) {
110
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(4);
113
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
116
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
119
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
122
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
125
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(3);
128
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(2);
131
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(1);
134
scr |= BF_SAIF_CTRL_BITCLK_MULT_RATE(0);
140
__raw_writel(scr, saif->base + SAIF_CTRL);
146
* Put and disable MCLK.
148
int mxs_saif_put_mclk(unsigned int saif_id)
150
struct mxs_saif *saif = mxs_saif[saif_id];
156
stat = __raw_readl(saif->base + SAIF_STAT);
157
if (stat & BM_SAIF_STAT_BUSY) {
158
dev_err(saif->dev, "error: busy\n");
162
clk_disable(saif->clk);
164
/* disable MCLK output */
165
__raw_writel(BM_SAIF_CTRL_CLKGATE,
166
saif->base + SAIF_CTRL + MXS_SET_ADDR);
167
__raw_writel(BM_SAIF_CTRL_RUN,
168
saif->base + SAIF_CTRL + MXS_CLR_ADDR);
170
saif->mclk_in_use = 0;
175
* Get MCLK and set clock rate, then enable it
177
* This interface is used for codecs who are using MCLK provided
180
int mxs_saif_get_mclk(unsigned int saif_id, unsigned int mclk,
183
struct mxs_saif *saif = mxs_saif[saif_id];
190
stat = __raw_readl(saif->base + SAIF_STAT);
191
if (stat & BM_SAIF_STAT_BUSY) {
192
dev_err(saif->dev, "error: busy\n");
197
__raw_writel(BM_SAIF_CTRL_SFTRST,
198
saif->base + SAIF_CTRL + MXS_CLR_ADDR);
200
saif->mclk_in_use = 1;
201
ret = mxs_saif_set_clk(saif, mclk, rate);
205
ret = clk_enable(saif->clk);
209
/* enable MCLK output */
210
__raw_writel(BM_SAIF_CTRL_CLKGATE,
211
saif->base + SAIF_CTRL + MXS_CLR_ADDR);
212
__raw_writel(BM_SAIF_CTRL_RUN,
213
saif->base + SAIF_CTRL + MXS_SET_ADDR);
219
* SAIF DAI format configuration.
220
* Should only be called when port is inactive.
222
static int mxs_saif_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
226
struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
228
stat = __raw_readl(saif->base + SAIF_STAT);
229
if (stat & BM_SAIF_STAT_BUSY) {
230
dev_err(cpu_dai->dev, "error: busy\n");
234
scr0 = __raw_readl(saif->base + SAIF_CTRL);
235
scr0 = scr0 & ~BM_SAIF_CTRL_BITCLK_EDGE & ~BM_SAIF_CTRL_LRCLK_POLARITY \
236
& ~BM_SAIF_CTRL_JUSTIFY & ~BM_SAIF_CTRL_DELAY;
240
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
241
case SND_SOC_DAIFMT_I2S:
242
/* data frame low 1clk before data */
243
scr |= BM_SAIF_CTRL_DELAY;
244
scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
246
case SND_SOC_DAIFMT_LEFT_J:
247
/* data frame high with data */
248
scr &= ~BM_SAIF_CTRL_DELAY;
249
scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
250
scr &= ~BM_SAIF_CTRL_JUSTIFY;
256
/* DAI clock inversion */
257
switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
258
case SND_SOC_DAIFMT_IB_IF:
259
scr |= BM_SAIF_CTRL_BITCLK_EDGE;
260
scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
262
case SND_SOC_DAIFMT_IB_NF:
263
scr |= BM_SAIF_CTRL_BITCLK_EDGE;
264
scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
266
case SND_SOC_DAIFMT_NB_IF:
267
scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
268
scr |= BM_SAIF_CTRL_LRCLK_POLARITY;
270
case SND_SOC_DAIFMT_NB_NF:
271
scr &= ~BM_SAIF_CTRL_BITCLK_EDGE;
272
scr &= ~BM_SAIF_CTRL_LRCLK_POLARITY;
277
* Note: We simply just support master mode since SAIF TX can only
280
switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
281
case SND_SOC_DAIFMT_CBS_CFS:
282
scr &= ~BM_SAIF_CTRL_SLAVE_MODE;
283
__raw_writel(scr | scr0, saif->base + SAIF_CTRL);
292
static int mxs_saif_startup(struct snd_pcm_substream *substream,
293
struct snd_soc_dai *cpu_dai)
295
struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
296
snd_soc_dai_set_dma_data(cpu_dai, substream, &saif->dma_param);
298
/* clear error status to 0 for each re-open */
299
saif->fifo_underrun = 0;
300
saif->fifo_overrun = 0;
302
/* Clear Reset for normal operations */
303
__raw_writel(BM_SAIF_CTRL_SFTRST,
304
saif->base + SAIF_CTRL + MXS_CLR_ADDR);
310
* Should only be called when port is inactive.
311
* although can be called multiple times by upper layers.
313
static int mxs_saif_hw_params(struct snd_pcm_substream *substream,
314
struct snd_pcm_hw_params *params,
315
struct snd_soc_dai *cpu_dai)
317
struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
321
/* mclk should already be set */
322
if (!saif->mclk && saif->mclk_in_use) {
323
dev_err(cpu_dai->dev, "set mclk first\n");
327
stat = __raw_readl(saif->base + SAIF_STAT);
328
if (stat & BM_SAIF_STAT_BUSY) {
329
dev_err(cpu_dai->dev, "error: busy\n");
334
* Set saif clk based on sample rate.
335
* If mclk is used, we also set mclk, if not, saif->mclk is
336
* default 0, means not used.
338
ret = mxs_saif_set_clk(saif, saif->mclk, params_rate(params));
340
dev_err(cpu_dai->dev, "unable to get proper clk\n");
344
scr = __raw_readl(saif->base + SAIF_CTRL);
346
scr &= ~BM_SAIF_CTRL_WORD_LENGTH;
347
scr &= ~BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
348
switch (params_format(params)) {
349
case SNDRV_PCM_FORMAT_S16_LE:
350
scr |= BF_SAIF_CTRL_WORD_LENGTH(0);
352
case SNDRV_PCM_FORMAT_S20_3LE:
353
scr |= BF_SAIF_CTRL_WORD_LENGTH(4);
354
scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
356
case SNDRV_PCM_FORMAT_S24_LE:
357
scr |= BF_SAIF_CTRL_WORD_LENGTH(8);
358
scr |= BM_SAIF_CTRL_BITCLK_48XFS_ENABLE;
365
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
367
scr &= ~BM_SAIF_CTRL_READ_MODE;
370
scr |= BM_SAIF_CTRL_READ_MODE;
373
__raw_writel(scr, saif->base + SAIF_CTRL);
377
static int mxs_saif_prepare(struct snd_pcm_substream *substream,
378
struct snd_soc_dai *cpu_dai)
380
struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
382
/* clear clock gate */
383
__raw_writel(BM_SAIF_CTRL_CLKGATE,
384
saif->base + SAIF_CTRL + MXS_CLR_ADDR);
386
/* enable FIFO error irqs */
387
__raw_writel(BM_SAIF_CTRL_FIFO_ERROR_IRQ_EN,
388
saif->base + SAIF_CTRL + MXS_SET_ADDR);
393
static int mxs_saif_trigger(struct snd_pcm_substream *substream, int cmd,
394
struct snd_soc_dai *cpu_dai)
396
struct mxs_saif *saif = snd_soc_dai_get_drvdata(cpu_dai);
399
case SNDRV_PCM_TRIGGER_START:
400
case SNDRV_PCM_TRIGGER_RESUME:
401
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
402
dev_dbg(cpu_dai->dev, "start\n");
404
clk_enable(saif->clk);
405
if (!saif->mclk_in_use)
406
__raw_writel(BM_SAIF_CTRL_RUN,
407
saif->base + SAIF_CTRL + MXS_SET_ADDR);
409
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
411
* write a data to saif data register to trigger
414
__raw_writel(0, saif->base + SAIF_DATA);
417
* read a data from saif data register to trigger
420
__raw_readl(saif->base + SAIF_DATA);
423
dev_dbg(cpu_dai->dev, "CTRL 0x%x STAT 0x%x\n",
424
__raw_readl(saif->base + SAIF_CTRL),
425
__raw_readl(saif->base + SAIF_STAT));
428
case SNDRV_PCM_TRIGGER_SUSPEND:
429
case SNDRV_PCM_TRIGGER_STOP:
430
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
431
dev_dbg(cpu_dai->dev, "stop\n");
433
clk_disable(saif->clk);
434
if (!saif->mclk_in_use)
435
__raw_writel(BM_SAIF_CTRL_RUN,
436
saif->base + SAIF_CTRL + MXS_CLR_ADDR);
446
#define MXS_SAIF_RATES SNDRV_PCM_RATE_8000_192000
447
#define MXS_SAIF_FORMATS \
448
(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
449
SNDRV_PCM_FMTBIT_S24_LE)
451
static struct snd_soc_dai_ops mxs_saif_dai_ops = {
452
.startup = mxs_saif_startup,
453
.trigger = mxs_saif_trigger,
454
.prepare = mxs_saif_prepare,
455
.hw_params = mxs_saif_hw_params,
456
.set_sysclk = mxs_saif_set_dai_sysclk,
457
.set_fmt = mxs_saif_set_dai_fmt,
460
static int mxs_saif_dai_probe(struct snd_soc_dai *dai)
462
struct mxs_saif *saif = dev_get_drvdata(dai->dev);
464
snd_soc_dai_set_drvdata(dai, saif);
469
static struct snd_soc_dai_driver mxs_saif_dai = {
471
.probe = mxs_saif_dai_probe,
475
.rates = MXS_SAIF_RATES,
476
.formats = MXS_SAIF_FORMATS,
481
.rates = MXS_SAIF_RATES,
482
.formats = MXS_SAIF_FORMATS,
484
.ops = &mxs_saif_dai_ops,
487
static irqreturn_t mxs_saif_irq(int irq, void *dev_id)
489
struct mxs_saif *saif = dev_id;
492
stat = __raw_readl(saif->base + SAIF_STAT);
493
if (!(stat & (BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ |
494
BM_SAIF_STAT_FIFO_OVERFLOW_IRQ)))
497
if (stat & BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ) {
498
dev_dbg(saif->dev, "underrun!!! %d\n", ++saif->fifo_underrun);
499
__raw_writel(BM_SAIF_STAT_FIFO_UNDERFLOW_IRQ,
500
saif->base + SAIF_STAT + MXS_CLR_ADDR);
503
if (stat & BM_SAIF_STAT_FIFO_OVERFLOW_IRQ) {
504
dev_dbg(saif->dev, "overrun!!! %d\n", ++saif->fifo_overrun);
505
__raw_writel(BM_SAIF_STAT_FIFO_OVERFLOW_IRQ,
506
saif->base + SAIF_STAT + MXS_CLR_ADDR);
509
dev_dbg(saif->dev, "SAIF_CTRL %x SAIF_STAT %x\n",
510
__raw_readl(saif->base + SAIF_CTRL),
511
__raw_readl(saif->base + SAIF_STAT));
516
static int mxs_saif_probe(struct platform_device *pdev)
518
struct resource *res;
519
struct mxs_saif *saif;
522
saif = kzalloc(sizeof(*saif), GFP_KERNEL);
526
if (pdev->id >= ARRAY_SIZE(mxs_saif))
528
mxs_saif[pdev->id] = saif;
530
saif->clk = clk_get(&pdev->dev, NULL);
531
if (IS_ERR(saif->clk)) {
532
ret = PTR_ERR(saif->clk);
533
dev_err(&pdev->dev, "Cannot get the clock: %d\n",
538
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
541
dev_err(&pdev->dev, "failed to get io resource: %d\n",
543
goto failed_get_resource;
546
if (!request_mem_region(res->start, resource_size(res), "mxs-saif")) {
547
dev_err(&pdev->dev, "request_mem_region failed\n");
549
goto failed_get_resource;
552
saif->base = ioremap(res->start, resource_size(res));
554
dev_err(&pdev->dev, "ioremap failed\n");
559
res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
562
dev_err(&pdev->dev, "failed to get dma resource: %d\n",
566
saif->dma_param.chan_num = res->start;
568
saif->irq = platform_get_irq(pdev, 0);
571
dev_err(&pdev->dev, "failed to get irq resource: %d\n",
573
goto failed_get_irq1;
576
saif->dev = &pdev->dev;
577
ret = request_irq(saif->irq, mxs_saif_irq, 0, "mxs-saif", saif);
579
dev_err(&pdev->dev, "failed to request irq\n");
580
goto failed_get_irq1;
583
saif->dma_param.chan_irq = platform_get_irq(pdev, 1);
584
if (saif->dma_param.chan_irq < 0) {
585
ret = saif->dma_param.chan_irq;
586
dev_err(&pdev->dev, "failed to get dma irq resource: %d\n",
588
goto failed_get_irq2;
591
platform_set_drvdata(pdev, saif);
593
ret = snd_soc_register_dai(&pdev->dev, &mxs_saif_dai);
595
dev_err(&pdev->dev, "register DAI failed\n");
596
goto failed_register;
599
saif->soc_platform_pdev = platform_device_alloc(
600
"mxs-pcm-audio", pdev->id);
601
if (!saif->soc_platform_pdev) {
603
goto failed_pdev_alloc;
606
platform_set_drvdata(saif->soc_platform_pdev, saif);
607
ret = platform_device_add(saif->soc_platform_pdev);
609
dev_err(&pdev->dev, "failed to add soc platform device\n");
610
goto failed_pdev_add;
616
platform_device_put(saif->soc_platform_pdev);
618
snd_soc_unregister_dai(&pdev->dev);
621
free_irq(saif->irq, saif);
625
release_mem_region(res->start, resource_size(res));
634
static int __devexit mxs_saif_remove(struct platform_device *pdev)
636
struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
637
struct mxs_saif *saif = platform_get_drvdata(pdev);
639
platform_device_unregister(saif->soc_platform_pdev);
641
snd_soc_unregister_dai(&pdev->dev);
644
release_mem_region(res->start, resource_size(res));
645
free_irq(saif->irq, saif);
653
static struct platform_driver mxs_saif_driver = {
654
.probe = mxs_saif_probe,
655
.remove = __devexit_p(mxs_saif_remove),
659
.owner = THIS_MODULE,
663
static int __init mxs_saif_init(void)
665
return platform_driver_register(&mxs_saif_driver);
668
static void __exit mxs_saif_exit(void)
670
platform_driver_unregister(&mxs_saif_driver);
673
module_init(mxs_saif_init);
674
module_exit(mxs_saif_exit);
675
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
676
MODULE_DESCRIPTION("MXS ASoC SAIF driver");
677
MODULE_LICENSE("GPL");