2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * You should have received a copy of the GNU General Public License along
10 * with this program; if not, write to the Free Software Foundation, Inc.,
11 * 675 Mass Ave, Cambridge, MA 02139, USA.
15 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #include <linux/clk.h>
23 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/initval.h>
33 #include "jz4740-i2s.h"
34 #include "jz4740-pcm.h"
36 #define JZ_REG_AIC_CONF 0x00
37 #define JZ_REG_AIC_CTRL 0x04
38 #define JZ_REG_AIC_I2S_FMT 0x10
39 #define JZ_REG_AIC_FIFO_STATUS 0x14
40 #define JZ_REG_AIC_I2S_STATUS 0x1c
41 #define JZ_REG_AIC_CLK_DIV 0x30
42 #define JZ_REG_AIC_FIFO 0x34
44 #define JZ_AIC_CONF_FIFO_RX_THRESHOLD_MASK (0xf << 12)
45 #define JZ_AIC_CONF_FIFO_TX_THRESHOLD_MASK (0xf << 8)
46 #define JZ_AIC_CONF_OVERFLOW_PLAY_LAST BIT(6)
47 #define JZ_AIC_CONF_INTERNAL_CODEC BIT(5)
48 #define JZ_AIC_CONF_I2S BIT(4)
49 #define JZ_AIC_CONF_RESET BIT(3)
50 #define JZ_AIC_CONF_BIT_CLK_MASTER BIT(2)
51 #define JZ_AIC_CONF_SYNC_CLK_MASTER BIT(1)
52 #define JZ_AIC_CONF_ENABLE BIT(0)
54 #define JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET 12
55 #define JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET 8
57 #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK (0x7 << 19)
58 #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK (0x7 << 16)
59 #define JZ_AIC_CTRL_ENABLE_RX_DMA BIT(15)
60 #define JZ_AIC_CTRL_ENABLE_TX_DMA BIT(14)
61 #define JZ_AIC_CTRL_MONO_TO_STEREO BIT(11)
62 #define JZ_AIC_CTRL_SWITCH_ENDIANNESS BIT(10)
63 #define JZ_AIC_CTRL_SIGNED_TO_UNSIGNED BIT(9)
64 #define JZ_AIC_CTRL_FLUSH BIT(8)
65 #define JZ_AIC_CTRL_ENABLE_ROR_INT BIT(6)
66 #define JZ_AIC_CTRL_ENABLE_TUR_INT BIT(5)
67 #define JZ_AIC_CTRL_ENABLE_RFS_INT BIT(4)
68 #define JZ_AIC_CTRL_ENABLE_TFS_INT BIT(3)
69 #define JZ_AIC_CTRL_ENABLE_LOOPBACK BIT(2)
70 #define JZ_AIC_CTRL_ENABLE_PLAYBACK BIT(1)
71 #define JZ_AIC_CTRL_ENABLE_CAPTURE BIT(0)
73 #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET 19
74 #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET 16
76 #define JZ_AIC_I2S_FMT_DISABLE_BIT_CLK BIT(12)
77 #define JZ_AIC_I2S_FMT_ENABLE_SYS_CLK BIT(4)
78 #define JZ_AIC_I2S_FMT_MSB BIT(0)
80 #define JZ_AIC_I2S_STATUS_BUSY BIT(2)
82 #define JZ_AIC_CLK_DIV_MASK 0xf
92 struct jz4740_pcm_config pcm_config_playback;
93 struct jz4740_pcm_config pcm_config_capture;
96 static inline uint32_t jz4740_i2s_read(const struct jz4740_i2s *i2s,
99 return readl(i2s->base + reg);
102 static inline void jz4740_i2s_write(const struct jz4740_i2s *i2s,
103 unsigned int reg, uint32_t value)
105 writel(value, i2s->base + reg);
108 static int jz4740_i2s_startup(struct snd_pcm_substream *substream,
109 struct snd_soc_dai *dai)
111 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
117 ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
118 ctrl |= JZ_AIC_CTRL_FLUSH;
119 jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
121 clk_enable(i2s->clk_i2s);
123 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
124 conf |= JZ_AIC_CONF_ENABLE;
125 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
130 static void jz4740_i2s_shutdown(struct snd_pcm_substream *substream,
131 struct snd_soc_dai *dai)
133 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
139 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
140 conf &= ~JZ_AIC_CONF_ENABLE;
141 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
143 clk_disable(i2s->clk_i2s);
146 static int jz4740_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
147 struct snd_soc_dai *dai)
149 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
154 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
155 mask = JZ_AIC_CTRL_ENABLE_PLAYBACK | JZ_AIC_CTRL_ENABLE_TX_DMA;
157 mask = JZ_AIC_CTRL_ENABLE_CAPTURE | JZ_AIC_CTRL_ENABLE_RX_DMA;
159 ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
162 case SNDRV_PCM_TRIGGER_START:
163 case SNDRV_PCM_TRIGGER_RESUME:
164 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
167 case SNDRV_PCM_TRIGGER_STOP:
168 case SNDRV_PCM_TRIGGER_SUSPEND:
169 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
176 jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
181 static int jz4740_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
183 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
188 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
190 conf &= ~(JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER);
192 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
193 case SND_SOC_DAIFMT_CBS_CFS:
194 conf |= JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER;
195 format |= JZ_AIC_I2S_FMT_ENABLE_SYS_CLK;
197 case SND_SOC_DAIFMT_CBM_CFS:
198 conf |= JZ_AIC_CONF_SYNC_CLK_MASTER;
200 case SND_SOC_DAIFMT_CBS_CFM:
201 conf |= JZ_AIC_CONF_BIT_CLK_MASTER;
203 case SND_SOC_DAIFMT_CBM_CFM:
209 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
210 case SND_SOC_DAIFMT_MSB:
211 format |= JZ_AIC_I2S_FMT_MSB;
213 case SND_SOC_DAIFMT_I2S:
219 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
220 case SND_SOC_DAIFMT_NB_NF:
226 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
227 jz4740_i2s_write(i2s, JZ_REG_AIC_I2S_FMT, format);
232 static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
235 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
236 enum jz4740_dma_width dma_width;
237 struct jz4740_pcm_config *pcm_config;
238 unsigned int sample_size;
241 ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
243 switch (params_format(params)) {
244 case SNDRV_PCM_FORMAT_S8:
246 dma_width = JZ4740_DMA_WIDTH_8BIT;
248 case SNDRV_PCM_FORMAT_S16:
250 dma_width = JZ4740_DMA_WIDTH_16BIT;
256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
257 ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK;
258 ctrl |= sample_size << JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET;
259 if (params_channels(params) == 1)
260 ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO;
262 ctrl &= ~JZ_AIC_CTRL_MONO_TO_STEREO;
264 pcm_config = &i2s->pcm_config_playback;
265 pcm_config->dma_config.dst_width = dma_width;
268 ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK;
269 ctrl |= sample_size << JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET;
271 pcm_config = &i2s->pcm_config_capture;
272 pcm_config->dma_config.src_width = dma_width;
275 jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
277 snd_soc_dai_set_dma_data(dai, substream, pcm_config);
282 static int jz4740_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
283 unsigned int freq, int dir)
285 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
290 case JZ4740_I2S_CLKSRC_EXT:
291 parent = clk_get(NULL, "ext");
292 clk_set_parent(i2s->clk_i2s, parent);
294 case JZ4740_I2S_CLKSRC_PLL:
295 parent = clk_get(NULL, "pll half");
296 clk_set_parent(i2s->clk_i2s, parent);
297 ret = clk_set_rate(i2s->clk_i2s, freq);
307 static int jz4740_i2s_suspend(struct snd_soc_dai *dai)
309 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
313 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
314 conf &= ~JZ_AIC_CONF_ENABLE;
315 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
317 clk_disable(i2s->clk_i2s);
320 clk_disable(i2s->clk_aic);
325 static int jz4740_i2s_resume(struct snd_soc_dai *dai)
327 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
330 clk_enable(i2s->clk_aic);
333 clk_enable(i2s->clk_i2s);
335 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
336 conf |= JZ_AIC_CONF_ENABLE;
337 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
343 static void jz4740_i2c_init_pcm_config(struct jz4740_i2s *i2s)
345 struct jz4740_dma_config *dma_config;
348 dma_config = &i2s->pcm_config_playback.dma_config;
349 dma_config->src_width = JZ4740_DMA_WIDTH_32BIT,
350 dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
351 dma_config->request_type = JZ4740_DMA_TYPE_AIC_TRANSMIT;
352 dma_config->flags = JZ4740_DMA_SRC_AUTOINC;
353 dma_config->mode = JZ4740_DMA_MODE_SINGLE;
354 i2s->pcm_config_playback.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
357 dma_config = &i2s->pcm_config_capture.dma_config;
358 dma_config->dst_width = JZ4740_DMA_WIDTH_32BIT,
359 dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
360 dma_config->request_type = JZ4740_DMA_TYPE_AIC_RECEIVE;
361 dma_config->flags = JZ4740_DMA_DST_AUTOINC;
362 dma_config->mode = JZ4740_DMA_MODE_SINGLE;
363 i2s->pcm_config_capture.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
366 static int jz4740_i2s_dai_probe(struct snd_soc_dai *dai)
368 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
371 clk_enable(i2s->clk_aic);
373 jz4740_i2c_init_pcm_config(i2s);
375 conf = (7 << JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
376 (8 << JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
377 JZ_AIC_CONF_OVERFLOW_PLAY_LAST |
379 JZ_AIC_CONF_INTERNAL_CODEC;
381 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, JZ_AIC_CONF_RESET);
382 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
387 static int jz4740_i2s_dai_remove(struct snd_soc_dai *dai)
389 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
391 clk_disable(i2s->clk_aic);
395 static struct snd_soc_dai_ops jz4740_i2s_dai_ops = {
396 .startup = jz4740_i2s_startup,
397 .shutdown = jz4740_i2s_shutdown,
398 .trigger = jz4740_i2s_trigger,
399 .hw_params = jz4740_i2s_hw_params,
400 .set_fmt = jz4740_i2s_set_fmt,
401 .set_sysclk = jz4740_i2s_set_sysclk,
404 #define JZ4740_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
405 SNDRV_PCM_FMTBIT_S16_LE)
407 static struct snd_soc_dai_driver jz4740_i2s_dai = {
408 .probe = jz4740_i2s_dai_probe,
409 .remove = jz4740_i2s_dai_remove,
413 .rates = SNDRV_PCM_RATE_8000_48000,
414 .formats = JZ4740_I2S_FMTS,
419 .rates = SNDRV_PCM_RATE_8000_48000,
420 .formats = JZ4740_I2S_FMTS,
422 .symmetric_rates = 1,
423 .ops = &jz4740_i2s_dai_ops,
424 .suspend = jz4740_i2s_suspend,
425 .resume = jz4740_i2s_resume,
428 static int __devinit jz4740_i2s_dev_probe(struct platform_device *pdev)
430 struct jz4740_i2s *i2s;
433 i2s = kzalloc(sizeof(*i2s), GFP_KERNEL);
438 i2s->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
444 i2s->mem = request_mem_region(i2s->mem->start, resource_size(i2s->mem),
451 i2s->base = ioremap_nocache(i2s->mem->start, resource_size(i2s->mem));
454 goto err_release_mem_region;
457 i2s->phys_base = i2s->mem->start;
459 i2s->clk_aic = clk_get(&pdev->dev, "aic");
460 if (IS_ERR(i2s->clk_aic)) {
461 ret = PTR_ERR(i2s->clk_aic);
465 i2s->clk_i2s = clk_get(&pdev->dev, "i2s");
466 if (IS_ERR(i2s->clk_i2s)) {
467 ret = PTR_ERR(i2s->clk_i2s);
468 goto err_clk_put_aic;
471 platform_set_drvdata(pdev, i2s);
472 ret = snd_soc_register_dai(&pdev->dev, &jz4740_i2s_dai);
475 dev_err(&pdev->dev, "Failed to register DAI\n");
476 goto err_clk_put_i2s;
482 clk_put(i2s->clk_i2s);
484 clk_put(i2s->clk_aic);
487 err_release_mem_region:
488 release_mem_region(i2s->mem->start, resource_size(i2s->mem));
495 static int __devexit jz4740_i2s_dev_remove(struct platform_device *pdev)
497 struct jz4740_i2s *i2s = platform_get_drvdata(pdev);
499 snd_soc_unregister_dai(&pdev->dev);
501 clk_put(i2s->clk_i2s);
502 clk_put(i2s->clk_aic);
505 release_mem_region(i2s->mem->start, resource_size(i2s->mem));
507 platform_set_drvdata(pdev, NULL);
513 static struct platform_driver jz4740_i2s_driver = {
514 .probe = jz4740_i2s_dev_probe,
515 .remove = __devexit_p(jz4740_i2s_dev_remove),
517 .name = "jz4740-i2s",
518 .owner = THIS_MODULE,
522 static int __init jz4740_i2s_init(void)
524 return platform_driver_register(&jz4740_i2s_driver);
526 module_init(jz4740_i2s_init);
528 static void __exit jz4740_i2s_exit(void)
530 platform_driver_unregister(&jz4740_i2s_driver);
532 module_exit(jz4740_i2s_exit);
534 MODULE_AUTHOR("Lars-Peter Clausen, <lars@metafoo.de>");
535 MODULE_DESCRIPTION("Ingenic JZ4740 SoC I2S driver");
536 MODULE_LICENSE("GPL");
537 MODULE_ALIAS("platform:jz4740-i2s");