2 * linux/sound/soc/ep93xx-i2s.c
5 * Copyright (C) 2010 Ryan Mallon <ryan@bluewatersys.com>
7 * Based on the original driver by:
8 * Copyright (C) 2007 Chase Douglas <chasedouglas@gmail>
9 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/clk.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
29 #include <mach/hardware.h>
30 #include <mach/ep93xx-regs.h>
33 #include "ep93xx-pcm.h"
34 #include "ep93xx-i2s.h"
36 #define EP93XX_I2S_TXCLKCFG 0x00
37 #define EP93XX_I2S_RXCLKCFG 0x04
38 #define EP93XX_I2S_GLCTRL 0x0C
40 #define EP93XX_I2S_TXLINCTRLDATA 0x28
41 #define EP93XX_I2S_TXCTRL 0x2C
42 #define EP93XX_I2S_TXWRDLEN 0x30
43 #define EP93XX_I2S_TX0EN 0x34
45 #define EP93XX_I2S_RXLINCTRLDATA 0x58
46 #define EP93XX_I2S_RXCTRL 0x5C
47 #define EP93XX_I2S_RXWRDLEN 0x60
48 #define EP93XX_I2S_RX0EN 0x64
50 #define EP93XX_I2S_WRDLEN_16 (0 << 0)
51 #define EP93XX_I2S_WRDLEN_24 (1 << 0)
52 #define EP93XX_I2S_WRDLEN_32 (2 << 0)
54 #define EP93XX_I2S_LINCTRLDATA_R_JUST (1 << 2) /* Right justify */
56 #define EP93XX_I2S_CLKCFG_LRS (1 << 0) /* lrclk polarity */
57 #define EP93XX_I2S_CLKCFG_CKP (1 << 1) /* Bit clock polarity */
58 #define EP93XX_I2S_CLKCFG_REL (1 << 2) /* First bit transition */
59 #define EP93XX_I2S_CLKCFG_MASTER (1 << 3) /* Master mode */
60 #define EP93XX_I2S_CLKCFG_NBCG (1 << 4) /* Not bit clock gating */
62 struct ep93xx_i2s_info {
66 struct ep93xx_pcm_dma_params *dma_params;
71 struct ep93xx_pcm_dma_params ep93xx_i2s_dma_params[] = {
72 [SNDRV_PCM_STREAM_PLAYBACK] = {
73 .name = "i2s-pcm-out",
74 .dma_port = EP93XX_DMA_M2P_PORT_I2S1,
76 [SNDRV_PCM_STREAM_CAPTURE] = {
78 .dma_port = EP93XX_DMA_M2P_PORT_I2S1,
82 static inline void ep93xx_i2s_write_reg(struct ep93xx_i2s_info *info,
83 unsigned reg, unsigned val)
85 __raw_writel(val, info->regs + reg);
88 static inline unsigned ep93xx_i2s_read_reg(struct ep93xx_i2s_info *info,
91 return __raw_readl(info->regs + reg);
94 static void ep93xx_i2s_enable(struct ep93xx_i2s_info *info, int stream)
99 if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
100 (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
102 clk_enable(info->mclk);
103 clk_enable(info->sclk);
104 clk_enable(info->lrclk);
107 ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 1);
111 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
112 base_reg = EP93XX_I2S_TX0EN;
114 base_reg = EP93XX_I2S_RX0EN;
115 for (i = 0; i < 3; i++)
116 ep93xx_i2s_write_reg(info, base_reg + (i * 4), 1);
119 static void ep93xx_i2s_disable(struct ep93xx_i2s_info *info, int stream)
125 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
126 base_reg = EP93XX_I2S_TX0EN;
128 base_reg = EP93XX_I2S_RX0EN;
129 for (i = 0; i < 3; i++)
130 ep93xx_i2s_write_reg(info, base_reg + (i * 4), 0);
132 if ((ep93xx_i2s_read_reg(info, EP93XX_I2S_TX0EN) & 0x1) == 0 &&
133 (ep93xx_i2s_read_reg(info, EP93XX_I2S_RX0EN) & 0x1) == 0) {
135 ep93xx_i2s_write_reg(info, EP93XX_I2S_GLCTRL, 0);
138 clk_disable(info->lrclk);
139 clk_disable(info->sclk);
140 clk_disable(info->mclk);
144 static int ep93xx_i2s_startup(struct snd_pcm_substream *substream,
145 struct snd_soc_dai *dai)
147 struct snd_soc_pcm_runtime *rtd = substream->private_data;
148 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
149 struct ep93xx_i2s_info *info = rtd->dai->cpu_dai->private_data;
151 snd_soc_dai_set_dma_data(cpu_dai, substream,
152 &info->dma_params[substream->stream]);
156 static void ep93xx_i2s_shutdown(struct snd_pcm_substream *substream,
157 struct snd_soc_dai *dai)
159 struct snd_soc_pcm_runtime *rtd = substream->private_data;
160 struct ep93xx_i2s_info *info = rtd->dai->cpu_dai->private_data;
162 ep93xx_i2s_disable(info, substream->stream);
165 static int ep93xx_i2s_set_dai_fmt(struct snd_soc_dai *cpu_dai,
168 struct ep93xx_i2s_info *info = cpu_dai->private_data;
169 unsigned int clk_cfg, lin_ctrl;
171 clk_cfg = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXCLKCFG);
172 lin_ctrl = ep93xx_i2s_read_reg(info, EP93XX_I2S_RXLINCTRLDATA);
174 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
175 case SND_SOC_DAIFMT_I2S:
176 clk_cfg |= EP93XX_I2S_CLKCFG_REL;
177 lin_ctrl &= ~EP93XX_I2S_LINCTRLDATA_R_JUST;
180 case SND_SOC_DAIFMT_LEFT_J:
181 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
182 lin_ctrl &= ~EP93XX_I2S_LINCTRLDATA_R_JUST;
185 case SND_SOC_DAIFMT_RIGHT_J:
186 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
187 lin_ctrl |= EP93XX_I2S_LINCTRLDATA_R_JUST;
194 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
195 case SND_SOC_DAIFMT_CBS_CFS:
197 clk_cfg |= EP93XX_I2S_CLKCFG_MASTER;
200 case SND_SOC_DAIFMT_CBM_CFM:
201 /* Codec is master */
202 clk_cfg &= ~EP93XX_I2S_CLKCFG_MASTER;
209 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
210 case SND_SOC_DAIFMT_NB_NF:
211 /* Negative bit clock, lrclk low on left word */
212 clk_cfg &= ~(EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_REL);
215 case SND_SOC_DAIFMT_NB_IF:
216 /* Negative bit clock, lrclk low on right word */
217 clk_cfg &= ~EP93XX_I2S_CLKCFG_CKP;
218 clk_cfg |= EP93XX_I2S_CLKCFG_REL;
221 case SND_SOC_DAIFMT_IB_NF:
222 /* Positive bit clock, lrclk low on left word */
223 clk_cfg |= EP93XX_I2S_CLKCFG_CKP;
224 clk_cfg &= ~EP93XX_I2S_CLKCFG_REL;
227 case SND_SOC_DAIFMT_IB_IF:
228 /* Positive bit clock, lrclk low on right word */
229 clk_cfg |= EP93XX_I2S_CLKCFG_CKP | EP93XX_I2S_CLKCFG_REL;
233 /* Write new register values */
234 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXCLKCFG, clk_cfg);
235 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXCLKCFG, clk_cfg);
236 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXLINCTRLDATA, lin_ctrl);
237 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXLINCTRLDATA, lin_ctrl);
241 static int ep93xx_i2s_hw_params(struct snd_pcm_substream *substream,
242 struct snd_pcm_hw_params *params,
243 struct snd_soc_dai *dai)
245 struct snd_soc_pcm_runtime *rtd = substream->private_data;
246 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
247 struct ep93xx_i2s_info *info = cpu_dai->private_data;
248 unsigned word_len, div, sdiv, lrdiv;
251 switch (params_format(params)) {
252 case SNDRV_PCM_FORMAT_S16_LE:
253 word_len = EP93XX_I2S_WRDLEN_16;
256 case SNDRV_PCM_FORMAT_S24_LE:
257 word_len = EP93XX_I2S_WRDLEN_24;
260 case SNDRV_PCM_FORMAT_S32_LE:
261 word_len = EP93XX_I2S_WRDLEN_32;
268 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
269 ep93xx_i2s_write_reg(info, EP93XX_I2S_TXWRDLEN, word_len);
271 ep93xx_i2s_write_reg(info, EP93XX_I2S_RXWRDLEN, word_len);
274 * Calculate the sdiv (bit clock) and lrdiv (left/right clock) values.
275 * If the lrclk is pulse length is larger than the word size, then the
276 * bit clock will be gated for the unused bits.
278 div = (clk_get_rate(info->mclk) / params_rate(params)) *
279 params_channels(params);
280 for (sdiv = 2; sdiv <= 4; sdiv += 2)
281 for (lrdiv = 32; lrdiv <= 128; lrdiv <<= 1)
282 if (sdiv * lrdiv == div) {
290 err = clk_set_rate(info->sclk, clk_get_rate(info->mclk) / sdiv);
294 err = clk_set_rate(info->lrclk, clk_get_rate(info->sclk) / lrdiv);
298 ep93xx_i2s_enable(info, substream->stream);
302 static int ep93xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
303 unsigned int freq, int dir)
305 struct ep93xx_i2s_info *info = cpu_dai->private_data;
307 if (dir == SND_SOC_CLOCK_IN || clk_id != 0)
310 return clk_set_rate(info->mclk, freq);
314 static int ep93xx_i2s_suspend(struct snd_soc_dai *dai)
316 struct ep93xx_i2s_info *info = dai->private_data;
321 ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_PLAYBACK);
322 ep93xx_i2s_disable(info, SNDRV_PCM_STREAM_CAPTURE);
325 static int ep93xx_i2s_resume(struct snd_soc_dai *dai)
327 struct ep93xx_i2s_info *info = dai->private_data;
332 ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_PLAYBACK);
333 ep93xx_i2s_enable(info, SNDRV_PCM_STREAM_CAPTURE);
336 #define ep93xx_i2s_suspend NULL
337 #define ep93xx_i2s_resume NULL
340 static struct snd_soc_dai_ops ep93xx_i2s_dai_ops = {
341 .startup = ep93xx_i2s_startup,
342 .shutdown = ep93xx_i2s_shutdown,
343 .hw_params = ep93xx_i2s_hw_params,
344 .set_sysclk = ep93xx_i2s_set_sysclk,
345 .set_fmt = ep93xx_i2s_set_dai_fmt,
348 #define EP93XX_I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
349 SNDRV_PCM_FMTBIT_S24_LE | \
350 SNDRV_PCM_FMTBIT_S32_LE)
352 struct snd_soc_dai ep93xx_i2s_dai = {
353 .name = "ep93xx-i2s",
356 .suspend = ep93xx_i2s_suspend,
357 .resume = ep93xx_i2s_resume,
361 .rates = SNDRV_PCM_RATE_8000_48000,
362 .formats = EP93XX_I2S_FORMATS,
367 .rates = SNDRV_PCM_RATE_8000_48000,
368 .formats = EP93XX_I2S_FORMATS,
370 .ops = &ep93xx_i2s_dai_ops,
372 EXPORT_SYMBOL_GPL(ep93xx_i2s_dai);
374 static int ep93xx_i2s_probe(struct platform_device *pdev)
376 struct ep93xx_i2s_info *info;
377 struct resource *res;
380 info = kzalloc(sizeof(struct ep93xx_i2s_info), GFP_KERNEL);
386 ep93xx_i2s_dai.dev = &pdev->dev;
387 ep93xx_i2s_dai.private_data = info;
388 info->dma_params = ep93xx_i2s_dma_params;
390 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
396 info->mem = request_mem_region(res->start, resource_size(res),
403 info->regs = ioremap(info->mem->start, resource_size(info->mem));
406 goto fail_release_mem;
409 info->mclk = clk_get(&pdev->dev, "mclk");
410 if (IS_ERR(info->mclk)) {
411 err = PTR_ERR(info->mclk);
415 info->sclk = clk_get(&pdev->dev, "sclk");
416 if (IS_ERR(info->sclk)) {
417 err = PTR_ERR(info->sclk);
421 info->lrclk = clk_get(&pdev->dev, "lrclk");
422 if (IS_ERR(info->lrclk)) {
423 err = PTR_ERR(info->lrclk);
427 err = snd_soc_register_dai(&ep93xx_i2s_dai);
434 clk_put(info->lrclk);
442 release_mem_region(info->mem->start, resource_size(info->mem));
448 static int __devexit ep93xx_i2s_remove(struct platform_device *pdev)
450 struct ep93xx_i2s_info *info = ep93xx_i2s_dai.private_data;
452 snd_soc_unregister_dai(&ep93xx_i2s_dai);
453 clk_put(info->lrclk);
457 release_mem_region(info->mem->start, resource_size(info->mem));
462 static struct platform_driver ep93xx_i2s_driver = {
463 .probe = ep93xx_i2s_probe,
464 .remove = __devexit_p(ep93xx_i2s_remove),
466 .name = "ep93xx-i2s",
467 .owner = THIS_MODULE,
471 static int __init ep93xx_i2s_init(void)
473 return platform_driver_register(&ep93xx_i2s_driver);
476 static void __exit ep93xx_i2s_exit(void)
478 platform_driver_unregister(&ep93xx_i2s_driver);
481 module_init(ep93xx_i2s_init);
482 module_exit(ep93xx_i2s_exit);
484 MODULE_ALIAS("platform:ep93xx-i2s");
485 MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com>");
486 MODULE_DESCRIPTION("EP93XX I2S driver");
487 MODULE_LICENSE("GPL");