1 From d12d1c5f8615192b545faa69a553f64d4d9cc9ab Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Fri, 28 May 2010 19:54:02 +0200
4 Subject: [PATCH] Add jz4740 SoC sound drivers
7 sound/soc/Kconfig | 1 +
8 sound/soc/Makefile | 1 +
9 sound/soc/jz4740/Kconfig | 13 +
10 sound/soc/jz4740/Makefile | 9 +
11 sound/soc/jz4740/jz4740-i2s.c | 568 +++++++++++++++++++++++++++++++++++++++++
12 sound/soc/jz4740/jz4740-i2s.h | 18 ++
13 sound/soc/jz4740/jz4740-pcm.c | 350 +++++++++++++++++++++++++
14 sound/soc/jz4740/jz4740-pcm.h | 22 ++
15 8 files changed, 982 insertions(+), 0 deletions(-)
16 create mode 100644 sound/soc/jz4740/Kconfig
17 create mode 100644 sound/soc/jz4740/Makefile
18 create mode 100644 sound/soc/jz4740/jz4740-i2s.c
19 create mode 100644 sound/soc/jz4740/jz4740-i2s.h
20 create mode 100644 sound/soc/jz4740/jz4740-pcm.c
21 create mode 100644 sound/soc/jz4740/jz4740-pcm.h
23 diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig
24 index b1749bc..5a7a724 100644
25 --- a/sound/soc/Kconfig
26 +++ b/sound/soc/Kconfig
27 @@ -36,6 +36,7 @@ source "sound/soc/s3c24xx/Kconfig"
28 source "sound/soc/s6000/Kconfig"
29 source "sound/soc/sh/Kconfig"
30 source "sound/soc/txx9/Kconfig"
31 +source "sound/soc/jz4740/Kconfig"
34 source "sound/soc/codecs/Kconfig"
35 diff --git a/sound/soc/Makefile b/sound/soc/Makefile
36 index 1470141..fdbe74d 100644
37 --- a/sound/soc/Makefile
38 +++ b/sound/soc/Makefile
39 @@ -14,3 +14,4 @@ obj-$(CONFIG_SND_SOC) += s3c24xx/
40 obj-$(CONFIG_SND_SOC) += s6000/
41 obj-$(CONFIG_SND_SOC) += sh/
42 obj-$(CONFIG_SND_SOC) += txx9/
43 +obj-$(CONFIG_SND_SOC) += jz4740/
44 diff --git a/sound/soc/jz4740/Kconfig b/sound/soc/jz4740/Kconfig
46 index 0000000..39df949
48 +++ b/sound/soc/jz4740/Kconfig
50 +config SND_JZ4740_SOC
51 + tristate "SoC Audio for Ingenic JZ4740 SoC"
52 + depends on SOC_JZ4740 && SND_SOC
54 + Say Y or M if you want to add support for codecs attached to
55 + the Jz4740 AC97, I2S or SSP interface. You will also need
56 + to select the audio interfaces to support below.
58 +config SND_JZ4740_SOC_I2S
59 + depends on SND_JZ4740_SOC
60 + tristate "SoC Audio (I2S protocol) for Ingenic jz4740 chip"
62 + Say Y if you want to use I2S protocol and I2S codec on Ingenic Jz4740 QI_LB60 board.
63 diff --git a/sound/soc/jz4740/Makefile b/sound/soc/jz4740/Makefile
65 index 0000000..1be8d19
67 +++ b/sound/soc/jz4740/Makefile
70 +# Jz4740 Platform Support
72 +snd-soc-jz4740-objs := jz4740-pcm.o
73 +snd-soc-jz4740-i2s-objs := jz4740-i2s.o
75 +obj-$(CONFIG_SND_JZ4740_SOC) += snd-soc-jz4740.o
76 +obj-$(CONFIG_SND_JZ4740_SOC_I2S) += snd-soc-jz4740-i2s.o
78 diff --git a/sound/soc/jz4740/jz4740-i2s.c b/sound/soc/jz4740/jz4740-i2s.c
80 index 0000000..2b139fd
82 +++ b/sound/soc/jz4740/jz4740-i2s.c
85 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
87 + * This program is free software; you can redistribute it and/or modify it
88 + * under the terms of the GNU General Public License as published by the
89 + * Free Software Foundation; either version 2 of the License, or (at your
90 + * option) any later version.
92 + * You should have received a copy of the GNU General Public License along
93 + * with this program; if not, write to the Free Software Foundation, Inc.,
94 + * 675 Mass Ave, Cambridge, MA 02139, USA.
98 +#include <linux/init.h>
99 +#include <linux/io.h>
100 +#include <linux/kernel.h>
101 +#include <linux/module.h>
102 +#include <linux/platform_device.h>
103 +#include <linux/slab.h>
105 +#include <linux/clk.h>
106 +#include <linux/delay.h>
108 +#include <linux/dma-mapping.h>
110 +#include <sound/core.h>
111 +#include <sound/pcm.h>
112 +#include <sound/pcm_params.h>
113 +#include <sound/soc.h>
114 +#include <sound/soc-dapm.h>
115 +#include <sound/initval.h>
117 +#include "jz4740-i2s.h"
118 +#include "jz4740-pcm.h"
120 +#define JZ_REG_AIC_CONF 0x00
121 +#define JZ_REG_AIC_CTRL 0x04
122 +#define JZ_REG_AIC_I2S_FMT 0x10
123 +#define JZ_REG_AIC_FIFO_STATUS 0x14
124 +#define JZ_REG_AIC_I2S_STATUS 0x1c
125 +#define JZ_REG_AIC_CLK_DIV 0x30
126 +#define JZ_REG_AIC_FIFO 0x34
128 +#define JZ_AIC_CONF_FIFO_RX_THRESHOLD_MASK (0xf << 12)
129 +#define JZ_AIC_CONF_FIFO_TX_THRESHOLD_MASK (0xf << 8)
130 +#define JZ_AIC_CONF_OVERFLOW_PLAY_LAST BIT(6)
131 +#define JZ_AIC_CONF_INTERNAL_CODEC BIT(5)
132 +#define JZ_AIC_CONF_I2S BIT(4)
133 +#define JZ_AIC_CONF_RESET BIT(3)
134 +#define JZ_AIC_CONF_BIT_CLK_MASTER BIT(2)
135 +#define JZ_AIC_CONF_SYNC_CLK_MASTER BIT(1)
136 +#define JZ_AIC_CONF_ENABLE BIT(0)
138 +#define JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET 12
139 +#define JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET 8
141 +#define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK (0x7 << 19)
142 +#define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK (0x7 << 16)
143 +#define JZ_AIC_CTRL_ENABLE_RX_DMA BIT(15)
144 +#define JZ_AIC_CTRL_ENABLE_TX_DMA BIT(14)
145 +#define JZ_AIC_CTRL_MONO_TO_STEREO BIT(11)
146 +#define JZ_AIC_CTRL_SWITCH_ENDIANNESS BIT(10)
147 +#define JZ_AIC_CTRL_SIGNED_TO_UNSIGNED BIT(9)
148 +#define JZ_AIC_CTRL_FLUSH BIT(8)
149 +#define JZ_AIC_CTRL_ENABLE_ROR_INT BIT(6)
150 +#define JZ_AIC_CTRL_ENABLE_TUR_INT BIT(5)
151 +#define JZ_AIC_CTRL_ENABLE_RFS_INT BIT(4)
152 +#define JZ_AIC_CTRL_ENABLE_TFS_INT BIT(3)
153 +#define JZ_AIC_CTRL_ENABLE_LOOPBACK BIT(2)
154 +#define JZ_AIC_CTRL_ENABLE_PLAYBACK BIT(1)
155 +#define JZ_AIC_CTRL_ENABLE_CAPTURE BIT(0)
157 +#define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET 19
158 +#define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET 16
160 +#define JZ_AIC_I2S_FMT_DISABLE_BIT_CLK BIT(12)
161 +#define JZ_AIC_I2S_FMT_ENABLE_SYS_CLK BIT(4)
162 +#define JZ_AIC_I2S_FMT_MSB BIT(0)
164 +#define JZ_AIC_I2S_STATUS_BUSY BIT(2)
166 +#define JZ_AIC_CLK_DIV_MASK 0xf
169 + struct resource *mem;
170 + void __iomem *base;
171 + dma_addr_t phys_base;
173 + struct clk *clk_aic;
174 + struct clk *clk_i2s;
176 + struct jz4740_pcm_config pcm_config_playback;
177 + struct jz4740_pcm_config pcm_config_capture;
180 +static inline uint32_t jz4740_i2s_read(const struct jz4740_i2s *i2s,
183 + return readl(i2s->base + reg);
186 +static inline void jz4740_i2s_write(const struct jz4740_i2s *i2s,
187 + unsigned int reg, uint32_t value)
189 + writel(value, i2s->base + reg);
192 +static inline struct jz4740_i2s *jz4740_dai_to_i2s(struct snd_soc_dai *dai)
194 + return dai->private_data;
197 +static int jz4740_i2s_startup(struct snd_pcm_substream *substream,
198 + struct snd_soc_dai *dai)
200 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
201 + uint32_t conf, ctrl;
207 + ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
208 + ctrl |= JZ_AIC_CTRL_FLUSH;
209 + jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
211 + clk_enable(i2s->clk_i2s);
213 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
214 + conf |= JZ_AIC_CONF_ENABLE;
215 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
220 +static void jz4740_i2s_shutdown(struct snd_pcm_substream *substream,
221 + struct snd_soc_dai *dai)
223 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
229 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
230 + conf &= ~JZ_AIC_CONF_ENABLE;
231 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
233 + clk_disable(i2s->clk_i2s);
237 +static int jz4740_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
238 + struct snd_soc_dai *dai)
240 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
241 + bool playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
247 + mask = JZ_AIC_CTRL_ENABLE_PLAYBACK | JZ_AIC_CTRL_ENABLE_TX_DMA;
249 + mask = JZ_AIC_CTRL_ENABLE_CAPTURE | JZ_AIC_CTRL_ENABLE_RX_DMA;
251 + ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
254 + case SNDRV_PCM_TRIGGER_START:
255 + case SNDRV_PCM_TRIGGER_RESUME:
256 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
259 + case SNDRV_PCM_TRIGGER_STOP:
260 + case SNDRV_PCM_TRIGGER_SUSPEND:
261 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
268 + jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
274 +static int jz4740_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
276 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
278 + uint32_t format = 0;
281 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
283 + conf &= ~(JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER);
285 + switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
286 + case SND_SOC_DAIFMT_CBS_CFS:
287 + conf |= JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER;
288 + format |= JZ_AIC_I2S_FMT_ENABLE_SYS_CLK;
290 + case SND_SOC_DAIFMT_CBM_CFS:
291 + conf |= JZ_AIC_CONF_SYNC_CLK_MASTER;
293 + case SND_SOC_DAIFMT_CBS_CFM:
294 + conf |= JZ_AIC_CONF_BIT_CLK_MASTER;
296 + case SND_SOC_DAIFMT_CBM_CFM:
302 + switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
303 + case SND_SOC_DAIFMT_MSB:
304 + format |= JZ_AIC_I2S_FMT_MSB;
306 + case SND_SOC_DAIFMT_I2S:
312 + switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
313 + case SND_SOC_DAIFMT_NB_NF:
319 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
320 + jz4740_i2s_write(i2s, JZ_REG_AIC_I2S_FMT, format);
325 +static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
326 + struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
328 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
329 + bool playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
330 + enum jz4740_dma_width dma_width;
331 + struct jz4740_pcm_config *pcm_config;
332 + unsigned int sample_size;
335 + ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
337 + switch (params_format(params)) {
338 + case SNDRV_PCM_FORMAT_S8:
340 + dma_width = JZ4740_DMA_WIDTH_8BIT;
342 + case SNDRV_PCM_FORMAT_S16:
344 + dma_width = JZ4740_DMA_WIDTH_16BIT;
351 + ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK;
352 + ctrl |= sample_size << JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET;
354 + ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK;
355 + ctrl |= sample_size << JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET;
358 + switch (params_channels(params)) {
363 + ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO;
366 + default: /* Falltrough */
370 + jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
373 + pcm_config = &i2s->pcm_config_playback;
374 + pcm_config->dma_config.dst_width = dma_width;
376 + pcm_config = &i2s->pcm_config_capture;
377 + pcm_config->dma_config.src_width = dma_width;
381 + snd_soc_dai_set_dma_data(dai, substream, pcm_config);
386 +static int jz4740_i2s_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
388 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
391 + case JZ4740_I2S_BIT_CLK:
392 + if (div & 1 || div > 16)
394 + jz4740_i2s_write(i2s, JZ_REG_AIC_CLK_DIV, div - 1);
403 +static int jz4740_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
404 + unsigned int freq, int dir)
406 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
408 + struct clk *parent;
411 + case JZ4740_I2S_CLKSRC_EXT:
412 + parent = clk_get(NULL, "ext");
413 + clk_set_parent(i2s->clk_i2s, parent);
415 + case JZ4740_I2S_CLKSRC_PLL:
416 + parent = clk_get(NULL, "pll half");
417 + clk_set_parent(i2s->clk_i2s, parent);
418 + ret = clk_set_rate(i2s->clk_i2s, freq);
428 +static int jz4740_i2s_suspend(struct snd_soc_dai *dai)
430 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
434 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
435 + conf &= ~JZ_AIC_CONF_ENABLE;
436 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
438 + clk_disable(i2s->clk_i2s);
441 + clk_disable(i2s->clk_aic);
446 +static int jz4740_i2s_resume(struct snd_soc_dai *dai)
448 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
451 + clk_enable(i2s->clk_aic);
454 + clk_enable(i2s->clk_i2s);
456 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
457 + conf |= JZ_AIC_CONF_ENABLE;
458 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
465 +static int jz4740_i2s_probe(struct platform_device *pdev, struct snd_soc_dai *dai)
467 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
470 + conf = (7 << JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
471 + (8 << JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
472 + JZ_AIC_CONF_OVERFLOW_PLAY_LAST |
474 + JZ_AIC_CONF_INTERNAL_CODEC;
476 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, JZ_AIC_CONF_RESET);
477 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
483 +static struct snd_soc_dai_ops jz4740_i2s_dai_ops = {
484 + .startup = jz4740_i2s_startup,
485 + .shutdown = jz4740_i2s_shutdown,
486 + .trigger = jz4740_i2s_trigger,
487 + .hw_params = jz4740_i2s_hw_params,
488 + .set_fmt = jz4740_i2s_set_fmt,
489 + .set_clkdiv = jz4740_i2s_set_clkdiv,
490 + .set_sysclk = jz4740_i2s_set_sysclk,
493 +#define JZ4740_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
494 + SNDRV_PCM_FMTBIT_S16_LE)
496 +struct snd_soc_dai jz4740_i2s_dai = {
497 + .name = "jz4740-i2s",
498 + .probe = jz4740_i2s_probe,
502 + .rates = SNDRV_PCM_RATE_8000_48000,
503 + .formats = JZ4740_I2S_FMTS,
508 + .rates = SNDRV_PCM_RATE_8000_48000,
509 + .formats = JZ4740_I2S_FMTS,
511 + .symmetric_rates = 1,
512 + .ops = &jz4740_i2s_dai_ops,
513 + .suspend = jz4740_i2s_suspend,
514 + .resume = jz4740_i2s_resume,
516 +EXPORT_SYMBOL_GPL(jz4740_i2s_dai);
518 +static void __devinit jz4740_i2c_init_pcm_config(struct jz4740_i2s *i2s)
520 + struct jz4740_dma_config *dma_config;
523 + dma_config = &i2s->pcm_config_playback.dma_config;
524 + dma_config->src_width = JZ4740_DMA_WIDTH_32BIT,
525 + dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
526 + dma_config->request_type = JZ4740_DMA_TYPE_AIC_TRANSMIT;
527 + dma_config->flags = JZ4740_DMA_SRC_AUTOINC;
528 + dma_config->mode = JZ4740_DMA_MODE_SINGLE;
529 + i2s->pcm_config_playback.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
532 + dma_config = &i2s->pcm_config_capture.dma_config;
533 + dma_config->dst_width = JZ4740_DMA_WIDTH_32BIT,
534 + dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
535 + dma_config->request_type = JZ4740_DMA_TYPE_AIC_RECEIVE;
536 + dma_config->flags = JZ4740_DMA_DST_AUTOINC;
537 + dma_config->mode = JZ4740_DMA_MODE_SINGLE;
538 + i2s->pcm_config_capture.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
541 +static int __devinit jz4740_i2s_dev_probe(struct platform_device *pdev)
543 + struct jz4740_i2s *i2s;
546 + i2s = kzalloc(sizeof(*i2s), GFP_KERNEL);
551 + i2s->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
558 + i2s->mem = request_mem_region(i2s->mem->start, resource_size(i2s->mem),
566 + i2s->base = ioremap_nocache(i2s->mem->start, resource_size(i2s->mem));
570 + goto err_release_mem_region;
573 + i2s->phys_base = i2s->mem->start;
575 + i2s->clk_aic = clk_get(&pdev->dev, "aic");
576 + if (IS_ERR(i2s->clk_aic)) {
577 + ret = PTR_ERR(i2s->clk_aic);
581 + i2s->clk_i2s = clk_get(&pdev->dev, "i2s");
582 + if (IS_ERR(i2s->clk_i2s)) {
583 + ret = PTR_ERR(i2s->clk_i2s);
587 + clk_enable(i2s->clk_aic);
589 + jz4740_i2c_init_pcm_config(i2s);
591 + jz4740_i2s_dai.private_data = i2s;
592 + ret = snd_soc_register_dai(&jz4740_i2s_dai);
594 + platform_set_drvdata(pdev, i2s);
599 + iounmap(i2s->base);
600 +err_release_mem_region:
601 + release_mem_region(i2s->mem->start, resource_size(i2s->mem));
608 +static int __devexit jz4740_i2s_dev_remove(struct platform_device *pdev)
610 + struct jz4740_i2s *i2s = platform_get_drvdata(pdev);
612 + snd_soc_unregister_dai(&jz4740_i2s_dai);
614 + clk_disable(i2s->clk_aic);
615 + clk_put(i2s->clk_i2s);
616 + clk_put(i2s->clk_aic);
618 + iounmap(i2s->base);
619 + release_mem_region(i2s->mem->start, resource_size(i2s->mem));
621 + platform_set_drvdata(pdev, NULL);
627 +static struct platform_driver jz4740_i2s_driver = {
628 + .probe = jz4740_i2s_dev_probe,
629 + .remove = __devexit_p(jz4740_i2s_dev_remove),
631 + .name = "jz4740-i2s",
632 + .owner = THIS_MODULE,
636 +static int __init jz4740_i2s_init(void)
638 + return platform_driver_register(&jz4740_i2s_driver);
640 +module_init(jz4740_i2s_init);
642 +static void __exit jz4740_i2s_exit(void)
644 + platform_driver_unregister(&jz4740_i2s_driver);
646 +module_exit(jz4740_i2s_exit);
648 +MODULE_AUTHOR("Lars-Peter Clausen, <lars@metafoo.de>");
649 +MODULE_DESCRIPTION("Ingenic JZ4740 SoC I2S driver");
650 +MODULE_LICENSE("GPL");
651 +MODULE_ALIAS("platform:jz4740-i2s");
652 diff --git a/sound/soc/jz4740/jz4740-i2s.h b/sound/soc/jz4740/jz4740-i2s.h
654 index 0000000..da22ed8
656 +++ b/sound/soc/jz4740/jz4740-i2s.h
659 + * This program is free software; you can redistribute it and/or modify
660 + * it under the terms of the GNU General Public License version 2 as
661 + * published by the Free Software Foundation.
664 +#ifndef _JZ4740_I2S_H
665 +#define _JZ4740_I2S_H
667 +/* I2S clock source */
668 +#define JZ4740_I2S_CLKSRC_EXT 0
669 +#define JZ4740_I2S_CLKSRC_PLL 1
671 +#define JZ4740_I2S_BIT_CLK 0
673 +extern struct snd_soc_dai jz4740_i2s_dai;
676 diff --git a/sound/soc/jz4740/jz4740-pcm.c b/sound/soc/jz4740/jz4740-pcm.c
678 index 0000000..fd1c203
680 +++ b/sound/soc/jz4740/jz4740-pcm.c
683 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
685 + * This program is free software; you can redistribute it and/or modify it
686 + * under the terms of the GNU General Public License as published by the
687 + * Free Software Foundation; either version 2 of the License, or (at your
688 + * option) any later version.
690 + * You should have received a copy of the GNU General Public License along
691 + * with this program; if not, write to the Free Software Foundation, Inc.,
692 + * 675 Mass Ave, Cambridge, MA 02139, USA.
696 +#include <linux/init.h>
697 +#include <linux/interrupt.h>
698 +#include <linux/kernel.h>
699 +#include <linux/module.h>
700 +#include <linux/slab.h>
702 +#include <linux/dma-mapping.h>
704 +#include <sound/core.h>
705 +#include <sound/pcm.h>
706 +#include <sound/pcm_params.h>
707 +#include <sound/soc.h>
709 +#include <asm/mach-jz4740/dma.h>
710 +#include "jz4740-pcm.h"
712 +struct jz4740_runtime_data {
713 + unsigned int dma_period;
714 + dma_addr_t dma_start;
715 + dma_addr_t dma_pos;
716 + dma_addr_t dma_end;
718 + struct jz4740_dma_chan *dma;
720 + dma_addr_t fifo_addr;
723 +/* identify hardware playback capabilities */
724 +static const struct snd_pcm_hardware jz4740_pcm_hardware = {
725 + .info = SNDRV_PCM_INFO_MMAP |
726 + SNDRV_PCM_INFO_MMAP_VALID |
727 + SNDRV_PCM_INFO_INTERLEAVED |
728 + SNDRV_PCM_INFO_BLOCK_TRANSFER,
729 + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
731 + .rates = SNDRV_PCM_RATE_8000_48000,
734 + .period_bytes_min = 16,
735 + .period_bytes_max = 2 * PAGE_SIZE,
737 + .periods_max = 128,
738 + .buffer_bytes_max = 128 * 2 * PAGE_SIZE,
742 +static void jz4740_pcm_start_transfer(struct jz4740_runtime_data *prtd, int stream)
744 + unsigned int count;
746 + if (prtd->dma_pos + prtd->dma_period > prtd->dma_end)
747 + count = prtd->dma_end - prtd->dma_pos;
749 + count = prtd->dma_period;
751 + jz4740_dma_disable(prtd->dma);
753 + if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
754 + jz4740_dma_set_src_addr(prtd->dma, prtd->dma_pos);
755 + jz4740_dma_set_dst_addr(prtd->dma, prtd->fifo_addr);
757 + jz4740_dma_set_src_addr(prtd->dma, prtd->fifo_addr);
758 + jz4740_dma_set_dst_addr(prtd->dma, prtd->dma_pos);
761 + jz4740_dma_set_transfer_count(prtd->dma, count);
763 + jz4740_dma_enable(prtd->dma);
765 + prtd->dma_pos += prtd->dma_period;
766 + if (prtd->dma_pos >= prtd->dma_end)
767 + prtd->dma_pos = prtd->dma_start;
770 +static void jz4740_pcm_dma_transfer_done(struct jz4740_dma_chan *dma, int err,
773 + struct snd_pcm_substream *substream = dev_id;
774 + struct snd_pcm_runtime *runtime = substream->runtime;
775 + struct jz4740_runtime_data *prtd = runtime->private_data;
777 + snd_pcm_period_elapsed(substream);
779 + jz4740_pcm_start_transfer(prtd, substream->stream);
782 +static int jz4740_pcm_hw_params(struct snd_pcm_substream *substream,
783 + struct snd_pcm_hw_params *params)
785 + struct snd_pcm_runtime *runtime = substream->runtime;
786 + struct jz4740_runtime_data *prtd = runtime->private_data;
787 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
788 + struct jz4740_pcm_config *config;
790 + config = snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
792 + const char *dma_channel_name;
793 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
794 + dma_channel_name = "PCM Playback";
796 + dma_channel_name = "PCM Capture";
798 + prtd->dma = jz4740_dma_request(substream, dma_channel_name);
804 + jz4740_dma_configure(prtd->dma, &config->dma_config);
805 + prtd->fifo_addr = config->fifo_addr;
807 + jz4740_dma_set_complete_cb(prtd->dma, jz4740_pcm_dma_transfer_done);
809 + snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
810 + runtime->dma_bytes = params_buffer_bytes(params);
812 + prtd->dma_period = params_period_bytes(params);
813 + prtd->dma_start = runtime->dma_addr;
814 + prtd->dma_pos = prtd->dma_start;
815 + prtd->dma_end = prtd->dma_start + runtime->dma_bytes;
820 +static int jz4740_pcm_hw_free(struct snd_pcm_substream *substream)
822 + struct jz4740_runtime_data *prtd = substream->runtime->private_data;
824 + snd_pcm_set_runtime_buffer(substream, NULL);
826 + jz4740_dma_free(prtd->dma);
833 +static int jz4740_pcm_prepare(struct snd_pcm_substream *substream)
835 + struct jz4740_runtime_data *prtd = substream->runtime->private_data;
841 + prtd->dma_pos = prtd->dma_start;
846 +static int jz4740_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
848 + struct snd_pcm_runtime *runtime = substream->runtime;
849 + struct jz4740_runtime_data *prtd = runtime->private_data;
854 + case SNDRV_PCM_TRIGGER_START:
855 + case SNDRV_PCM_TRIGGER_RESUME:
856 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
857 + jz4740_pcm_start_transfer(prtd, substream->stream);
859 + case SNDRV_PCM_TRIGGER_STOP:
860 + case SNDRV_PCM_TRIGGER_SUSPEND:
861 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
862 + jz4740_dma_disable(prtd->dma);
871 +static snd_pcm_uframes_t jz4740_pcm_pointer(struct snd_pcm_substream *substream)
873 + struct snd_pcm_runtime *runtime = substream->runtime;
874 + struct jz4740_runtime_data *prtd = runtime->private_data;
875 + unsigned long count, pos;
876 + snd_pcm_uframes_t offset;
877 + struct jz4740_dma_chan *dma = prtd->dma;
879 + count = jz4740_dma_get_residue(dma);
880 + if (prtd->dma_pos == prtd->dma_start)
881 + pos = prtd->dma_end - prtd->dma_start - count;
883 + pos = prtd->dma_pos - prtd->dma_start - count;
885 + offset = bytes_to_frames(runtime, pos);
886 + if (offset >= runtime->buffer_size)
892 +static int jz4740_pcm_open(struct snd_pcm_substream *substream)
894 + struct snd_pcm_runtime *runtime = substream->runtime;
895 + struct jz4740_runtime_data *prtd;
897 + snd_soc_set_runtime_hwparams(substream, &jz4740_pcm_hardware);
898 + prtd = kzalloc(sizeof(struct jz4740_runtime_data), GFP_KERNEL);
903 + runtime->private_data = prtd;
907 +static int jz4740_pcm_close(struct snd_pcm_substream *substream)
909 + struct snd_pcm_runtime *runtime = substream->runtime;
910 + struct jz4740_runtime_data *prtd = runtime->private_data;
917 +static int jz4740_pcm_mmap(struct snd_pcm_substream *substream,
918 + struct vm_area_struct *vma)
920 + return remap_pfn_range(vma, vma->vm_start,
921 + substream->dma_buffer.addr >> PAGE_SHIFT,
922 + vma->vm_end - vma->vm_start, vma->vm_page_prot);
925 +static struct snd_pcm_ops jz4740_pcm_ops = {
926 + .open = jz4740_pcm_open,
927 + .close = jz4740_pcm_close,
928 + .ioctl = snd_pcm_lib_ioctl,
929 + .hw_params = jz4740_pcm_hw_params,
930 + .hw_free = jz4740_pcm_hw_free,
931 + .prepare = jz4740_pcm_prepare,
932 + .trigger = jz4740_pcm_trigger,
933 + .pointer = jz4740_pcm_pointer,
934 + .mmap = jz4740_pcm_mmap,
937 +static int jz4740_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
939 + struct snd_pcm_substream *substream = pcm->streams[stream].substream;
940 + struct snd_dma_buffer *buf = &substream->dma_buffer;
941 + size_t size = jz4740_pcm_hardware.buffer_bytes_max;
943 + buf->dev.type = SNDRV_DMA_TYPE_DEV;
944 + buf->dev.dev = pcm->card->dev;
945 + buf->private_data = NULL;
947 + buf->area = dma_alloc_noncoherent(pcm->card->dev, size,
948 + &buf->addr, GFP_KERNEL);
957 +static void jz4740_pcm_free(struct snd_pcm *pcm)
959 + struct snd_pcm_substream *substream;
960 + struct snd_dma_buffer *buf;
963 + for (stream = 0; stream < 2; stream++) {
964 + substream = pcm->streams[stream].substream;
968 + buf = &substream->dma_buffer;
972 + dma_free_noncoherent(pcm->card->dev, buf->bytes,
973 + buf->area, buf->addr);
978 +static u64 jz4740_pcm_dmamask = DMA_BIT_MASK(32);
980 +int jz4740_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
981 + struct snd_pcm *pcm)
985 + if (!card->dev->dma_mask)
986 + card->dev->dma_mask = &jz4740_pcm_dmamask;
988 + if (!card->dev->coherent_dma_mask)
989 + card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
991 + if (dai->playback.channels_min) {
992 + ret = jz4740_pcm_preallocate_dma_buffer(pcm,
993 + SNDRV_PCM_STREAM_PLAYBACK);
998 + if (dai->capture.channels_min) {
999 + ret = jz4740_pcm_preallocate_dma_buffer(pcm,
1000 + SNDRV_PCM_STREAM_CAPTURE);
1009 +struct snd_soc_platform jz4740_soc_platform = {
1010 + .name = "jz4740-pcm",
1011 + .pcm_ops = &jz4740_pcm_ops,
1012 + .pcm_new = jz4740_pcm_new,
1013 + .pcm_free = jz4740_pcm_free,
1015 +EXPORT_SYMBOL_GPL(jz4740_soc_platform);
1017 +static int __init jz4740_soc_platform_init(void)
1019 + return snd_soc_register_platform(&jz4740_soc_platform);
1021 +module_init(jz4740_soc_platform_init);
1023 +static void __exit jz4740_soc_platform_exit(void)
1025 + snd_soc_unregister_platform(&jz4740_soc_platform);
1027 +module_exit(jz4740_soc_platform_exit);
1029 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1030 +MODULE_DESCRIPTION("Ingenic SoC JZ4740 PCM driver");
1031 +MODULE_LICENSE("GPL");
1032 diff --git a/sound/soc/jz4740/jz4740-pcm.h b/sound/soc/jz4740/jz4740-pcm.h
1033 new file mode 100644
1034 index 0000000..e3f221e
1036 +++ b/sound/soc/jz4740/jz4740-pcm.h
1040 + * This program is free software; you can redistribute it and/or modify
1041 + * it under the terms of the GNU General Public License version 2 as
1042 + * published by the Free Software Foundation.
1045 +#ifndef _JZ4740_PCM_H
1046 +#define _JZ4740_PCM_H
1048 +#include <linux/dma-mapping.h>
1049 +#include <asm/mach-jz4740/dma.h>
1051 +/* platform data */
1052 +extern struct snd_soc_platform jz4740_soc_platform;
1054 +struct jz4740_pcm_config {
1055 + struct jz4740_dma_config dma_config;
1056 + phys_addr_t fifo_addr;