1 From 6548719b6a1ce4e202d58625fbfb94e6c894ced9 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 24 Apr 2010 12:35:07 +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 | 572 +++++++++++++++++++++++++++++++++++++++++
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, 986 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..d3daa27
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 capture_pcm_config;
177 + struct jz4740_pcm_config playback_pcm_config;
180 +static struct jz4740_dma_config jz4740_i2s_dma_playback_config = {
181 + .src_width = JZ4740_DMA_WIDTH_16BIT,
182 + .dst_width = JZ4740_DMA_WIDTH_32BIT,
183 + .transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE,
184 + .request_type = JZ4740_DMA_TYPE_AIC_TRANSMIT,
185 + .flags = JZ4740_DMA_SRC_AUTOINC,
186 + .mode = JZ4740_DMA_MODE_SINGLE,
189 +static struct jz4740_dma_config jz4740_i2s_dma_capture_config = {
190 + .src_width = JZ4740_DMA_WIDTH_32BIT,
191 + .dst_width = JZ4740_DMA_WIDTH_16BIT,
192 + .transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE,
193 + .request_type = JZ4740_DMA_TYPE_AIC_RECEIVE,
194 + .flags = JZ4740_DMA_DST_AUTOINC,
195 + .mode = JZ4740_DMA_MODE_SINGLE,
199 +static inline uint32_t jz4740_i2s_read(const struct jz4740_i2s *i2s,
202 + return readl(i2s->base + reg);
205 +static inline void jz4740_i2s_write(const struct jz4740_i2s *i2s,
206 + unsigned int reg, uint32_t value)
208 + writel(value, i2s->base + reg);
211 +static inline struct jz4740_i2s *jz4740_dai_to_i2s(struct snd_soc_dai *dai)
213 + return dai->private_data;
216 +static int jz4740_i2s_startup(struct snd_pcm_substream *substream,
217 + struct snd_soc_dai *dai)
219 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
220 + uint32_t conf, ctrl;
225 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
226 + ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
228 + conf |= JZ_AIC_CONF_ENABLE;
229 + ctrl |= JZ_AIC_CTRL_FLUSH;
232 + jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
233 + clk_enable(i2s->clk_i2s);
234 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
239 +static void jz4740_i2s_shutdown(struct snd_pcm_substream *substream, struct
242 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
248 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
249 + conf &= ~JZ_AIC_CONF_ENABLE;
250 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
252 + clk_disable(i2s->clk_i2s);
256 +static int jz4740_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
257 + struct snd_soc_dai *dai)
259 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
260 + bool playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
266 + mask = JZ_AIC_CTRL_ENABLE_PLAYBACK | JZ_AIC_CTRL_ENABLE_TX_DMA;
268 + mask = JZ_AIC_CTRL_ENABLE_CAPTURE | JZ_AIC_CTRL_ENABLE_RX_DMA;
271 + ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
274 + case SNDRV_PCM_TRIGGER_START:
275 + case SNDRV_PCM_TRIGGER_RESUME:
276 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
279 + case SNDRV_PCM_TRIGGER_STOP:
280 + case SNDRV_PCM_TRIGGER_SUSPEND:
281 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
288 + jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
294 +static int jz4740_i2s_set_fmt(struct snd_soc_dai *dai,
297 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
299 + uint32_t format = 0;
302 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
304 + conf &= ~(JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER);
306 + switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
307 + case SND_SOC_DAIFMT_CBS_CFS:
308 + conf |= JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER;
309 + format |= JZ_AIC_I2S_FMT_ENABLE_SYS_CLK;
311 + case SND_SOC_DAIFMT_CBM_CFS:
312 + conf |= JZ_AIC_CONF_SYNC_CLK_MASTER;
314 + case SND_SOC_DAIFMT_CBS_CFM:
315 + conf |= JZ_AIC_CONF_BIT_CLK_MASTER;
317 + case SND_SOC_DAIFMT_CBM_CFM:
323 + switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
324 + case SND_SOC_DAIFMT_MSB:
325 + format |= JZ_AIC_I2S_FMT_MSB;
327 + case SND_SOC_DAIFMT_I2S:
333 + switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
334 + case SND_SOC_DAIFMT_NB_NF:
340 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
341 + jz4740_i2s_write(i2s, JZ_REG_AIC_I2S_FMT, format);
346 +static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
347 + struct snd_pcm_hw_params *params,
348 + struct snd_soc_dai *dai)
350 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
351 + bool playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
352 + enum jz4740_dma_width dma_width;
353 + struct jz4740_pcm_config *pcm_config;
354 + unsigned int sample_size;
357 + ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
359 + switch (params_format(params)) {
360 + case SNDRV_PCM_FORMAT_S8:
362 + dma_width = JZ4740_DMA_WIDTH_8BIT;
364 + case SNDRV_PCM_FORMAT_S16:
366 + dma_width = JZ4740_DMA_WIDTH_16BIT;
373 + ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK;
374 + ctrl |= sample_size << JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET;
376 + ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK;
377 + ctrl |= sample_size << JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET;
380 + switch (params_channels(params)) {
385 + ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO;
388 + default: /* Falltrough */
392 + jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
395 + jz4740_i2s_dma_playback_config.src_width = dma_width;
396 + pcm_config = &i2s->playback_pcm_config;
397 + pcm_config->dma_config = &jz4740_i2s_dma_playback_config;
399 + jz4740_i2s_dma_capture_config.dst_width = dma_width;
400 + pcm_config = &i2s->capture_pcm_config;
401 + pcm_config->dma_config = &jz4740_i2s_dma_capture_config;
403 + pcm_config->fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
405 + snd_soc_dai_set_dma_data(dai, substream, pcm_config);
410 +static int jz4740_i2s_set_clkdiv(struct snd_soc_dai *dai,
411 + int div_id, int div)
413 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
416 + case JZ4740_I2S_BIT_CLK:
417 + if (div & 1 || div > 16)
419 + jz4740_i2s_write(i2s, JZ_REG_AIC_CLK_DIV, div - 1);
428 +static int jz4740_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
429 + unsigned int freq, int dir)
431 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
433 + struct clk *parent;
436 + case JZ4740_I2S_CLKSRC_EXT:
437 + parent = clk_get(NULL, "ext");
438 + clk_set_parent(i2s->clk_i2s, parent);
440 + case JZ4740_I2S_CLKSRC_PLL:
441 + parent = clk_get(NULL, "pll half");
442 + clk_set_parent(i2s->clk_i2s, parent);
443 + ret = clk_set_rate(i2s->clk_i2s, freq);
453 +static int jz4740_i2s_suspend(struct snd_soc_dai *dai)
455 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
459 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
460 + conf &= ~JZ_AIC_CONF_ENABLE;
461 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
463 + clk_disable(i2s->clk_i2s);
466 + clk_disable(i2s->clk_aic);
471 +static int jz4740_i2s_resume(struct snd_soc_dai *dai)
473 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
476 + clk_enable(i2s->clk_aic);
479 + clk_enable(i2s->clk_i2s);
481 + conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
482 + conf |= JZ_AIC_CONF_ENABLE;
483 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
490 +static int jz4740_i2s_probe(struct platform_device *pdev, struct snd_soc_dai *dai)
492 + struct jz4740_i2s *i2s = jz4740_dai_to_i2s(dai);
495 + conf = (7 << JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
496 + (8 << JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
497 + JZ_AIC_CONF_OVERFLOW_PLAY_LAST |
499 + JZ_AIC_CONF_INTERNAL_CODEC;
501 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, JZ_AIC_CONF_RESET);
502 + jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
508 +static struct snd_soc_dai_ops jz4740_i2s_dai_ops = {
509 + .startup = jz4740_i2s_startup,
510 + .shutdown = jz4740_i2s_shutdown,
511 + .trigger = jz4740_i2s_trigger,
512 + .hw_params = jz4740_i2s_hw_params,
513 + .set_fmt = jz4740_i2s_set_fmt,
514 + .set_clkdiv = jz4740_i2s_set_clkdiv,
515 + .set_sysclk = jz4740_i2s_set_sysclk,
518 +#define JZ4740_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
519 + SNDRV_PCM_FMTBIT_S16_LE)
521 +struct snd_soc_dai jz4740_i2s_dai = {
522 + .name = "jz4740-i2s",
523 + .probe = jz4740_i2s_probe,
527 + .rates = SNDRV_PCM_RATE_8000_44100,
528 + .formats = JZ4740_I2S_FMTS,
533 + .rates = SNDRV_PCM_RATE_8000_44100,
534 + .formats = JZ4740_I2S_FMTS,
536 + .symmetric_rates = 1,
537 + .ops = &jz4740_i2s_dai_ops,
538 + .suspend = jz4740_i2s_suspend,
539 + .resume = jz4740_i2s_resume,
541 +EXPORT_SYMBOL_GPL(jz4740_i2s_dai);
543 +static int __devinit jz4740_i2s_dev_probe(struct platform_device *pdev)
545 + struct jz4740_i2s *i2s;
548 + i2s = kzalloc(sizeof(*i2s), GFP_KERNEL);
553 + i2s->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
560 + i2s->mem = request_mem_region(i2s->mem->start, resource_size(i2s->mem),
568 + i2s->base = ioremap_nocache(i2s->mem->start, resource_size(i2s->mem));
572 + goto err_release_mem_region;
575 + i2s->phys_base = i2s->mem->start;
577 + jz4740_i2s_dai.private_data = i2s;
579 + ret = snd_soc_register_dai(&jz4740_i2s_dai);
581 + i2s->clk_aic = clk_get(&pdev->dev, "aic");
583 + if (IS_ERR(i2s->clk_aic)) {
584 + ret = PTR_ERR(i2s->clk_aic);
589 + i2s->clk_i2s = clk_get(&pdev->dev, "i2s");
591 + if (IS_ERR(i2s->clk_i2s)) {
592 + ret = PTR_ERR(i2s->clk_i2s);
596 + clk_enable(i2s->clk_aic);
598 + platform_set_drvdata(pdev, i2s);
603 + iounmap(i2s->base);
604 +err_release_mem_region:
605 + release_mem_region(i2s->mem->start, resource_size(i2s->mem));
612 +static int __devexit jz4740_i2s_dev_remove(struct platform_device *pdev)
614 + struct jz4740_i2s *i2s = platform_get_drvdata(pdev);
616 + snd_soc_unregister_dai(&jz4740_i2s_dai);
618 + clk_disable(i2s->clk_aic);
619 + clk_put(i2s->clk_i2s);
620 + clk_put(i2s->clk_aic);
622 + iounmap(i2s->base);
623 + release_mem_region(i2s->mem->start, resource_size(i2s->mem));
625 + platform_set_drvdata(pdev, NULL);
631 +static struct platform_driver jz4740_i2s_driver = {
632 + .probe = jz4740_i2s_dev_probe,
633 + .remove = __devexit_p(jz4740_i2s_dev_remove),
635 + .name = "jz4740-i2s",
636 + .owner = THIS_MODULE,
640 +static int __init jz4740_i2s_init(void)
642 + return platform_driver_register(&jz4740_i2s_driver);
644 +module_init(jz4740_i2s_init);
646 +static void __exit jz4740_i2s_exit(void)
648 + platform_driver_unregister(&jz4740_i2s_driver);
650 +module_exit(jz4740_i2s_exit);
652 +MODULE_AUTHOR("Lars-Peter Clausen, <lars@metafoo.de>");
653 +MODULE_DESCRIPTION("Ingenic JZ4740 SoC I2S driver");
654 +MODULE_LICENSE("GPL");
655 +MODULE_ALIAS("platform:jz4740-i2s");
656 diff --git a/sound/soc/jz4740/jz4740-i2s.h b/sound/soc/jz4740/jz4740-i2s.h
658 index 0000000..da22ed8
660 +++ b/sound/soc/jz4740/jz4740-i2s.h
663 + * This program is free software; you can redistribute it and/or modify
664 + * it under the terms of the GNU General Public License version 2 as
665 + * published by the Free Software Foundation.
668 +#ifndef _JZ4740_I2S_H
669 +#define _JZ4740_I2S_H
671 +/* I2S clock source */
672 +#define JZ4740_I2S_CLKSRC_EXT 0
673 +#define JZ4740_I2S_CLKSRC_PLL 1
675 +#define JZ4740_I2S_BIT_CLK 0
677 +extern struct snd_soc_dai jz4740_i2s_dai;
680 diff --git a/sound/soc/jz4740/jz4740-pcm.c b/sound/soc/jz4740/jz4740-pcm.c
682 index 0000000..e55ffbf
684 +++ b/sound/soc/jz4740/jz4740-pcm.c
687 + * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
689 + * This program is free software; you can redistribute it and/or modify it
690 + * under the terms of the GNU General Public License as published by the
691 + * Free Software Foundation; either version 2 of the License, or (at your
692 + * option) any later version.
694 + * You should have received a copy of the GNU General Public License along
695 + * with this program; if not, write to the Free Software Foundation, Inc.,
696 + * 675 Mass Ave, Cambridge, MA 02139, USA.
700 +#include <linux/init.h>
701 +#include <linux/interrupt.h>
702 +#include <linux/kernel.h>
703 +#include <linux/module.h>
704 +#include <linux/slab.h>
706 +#include <linux/dma-mapping.h>
708 +#include <sound/core.h>
709 +#include <sound/pcm.h>
710 +#include <sound/pcm_params.h>
711 +#include <sound/soc.h>
713 +#include <asm/mach-jz4740/dma.h>
714 +#include "jz4740-pcm.h"
716 +struct jz4740_runtime_data {
717 + unsigned int dma_period;
718 + dma_addr_t dma_start;
719 + dma_addr_t dma_pos;
720 + dma_addr_t dma_end;
722 + struct jz4740_dma_chan *dma;
724 + dma_addr_t fifo_addr;
727 +/* identify hardware playback capabilities */
728 +static const struct snd_pcm_hardware jz4740_pcm_hardware = {
729 + .info = SNDRV_PCM_INFO_MMAP |
730 + SNDRV_PCM_INFO_MMAP_VALID |
731 + SNDRV_PCM_INFO_INTERLEAVED |
732 + SNDRV_PCM_INFO_BLOCK_TRANSFER,
733 + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
735 + .rates = SNDRV_PCM_RATE_8000_48000,
738 + .period_bytes_min = 32,
739 + .period_bytes_max = 2 * PAGE_SIZE,
741 + .periods_max = 128,
742 + .buffer_bytes_max = 128 * 2 * PAGE_SIZE,
746 +static void jz4740_pcm_start_transfer(struct jz4740_runtime_data *prtd, int stream)
748 + unsigned int count;
750 + if (prtd->dma_pos + prtd->dma_period > prtd->dma_end)
751 + count = prtd->dma_end - prtd->dma_pos;
753 + count = prtd->dma_period;
755 + jz4740_dma_disable(prtd->dma);
757 + if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
758 + jz4740_dma_set_src_addr(prtd->dma, prtd->dma_pos);
759 + jz4740_dma_set_dst_addr(prtd->dma, prtd->fifo_addr);
761 + jz4740_dma_set_src_addr(prtd->dma, prtd->fifo_addr);
762 + jz4740_dma_set_dst_addr(prtd->dma, prtd->dma_pos);
765 + jz4740_dma_set_transfer_count(prtd->dma, count);
767 + jz4740_dma_enable(prtd->dma);
769 + prtd->dma_pos += prtd->dma_period;
770 + if (prtd->dma_pos >= prtd->dma_end)
771 + prtd->dma_pos = prtd->dma_start;
774 +static void jz4740_pcm_dma_transfer_done(struct jz4740_dma_chan *dma, int err,
777 + struct snd_pcm_substream *substream = dev_id;
778 + struct snd_pcm_runtime *runtime = substream->runtime;
779 + struct jz4740_runtime_data *prtd = runtime->private_data;
781 + snd_pcm_period_elapsed(substream);
783 + jz4740_pcm_start_transfer(prtd, substream->stream);
786 +static int jz4740_pcm_hw_params(struct snd_pcm_substream *substream,
787 + struct snd_pcm_hw_params *params)
789 + struct snd_pcm_runtime *runtime = substream->runtime;
790 + struct jz4740_runtime_data *prtd = runtime->private_data;
791 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
792 + struct jz4740_pcm_config *config;
794 + config = snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
796 + const char *dma_channel_name;
797 + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
798 + dma_channel_name = "PCM Playback";
800 + dma_channel_name = "PCM Capture";
802 + prtd->dma = jz4740_dma_request(substream, dma_channel_name);
808 + jz4740_dma_configure(prtd->dma, config->dma_config);
809 + prtd->fifo_addr = config->fifo_addr;
811 + jz4740_dma_set_complete_cb(prtd->dma, jz4740_pcm_dma_transfer_done);
813 + snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
814 + runtime->dma_bytes = params_buffer_bytes(params);
816 + prtd->dma_period = params_period_bytes(params);
817 + prtd->dma_start = runtime->dma_addr;
818 + prtd->dma_pos = prtd->dma_start;
819 + prtd->dma_end = prtd->dma_start + runtime->dma_bytes;
824 +static int jz4740_pcm_hw_free(struct snd_pcm_substream *substream)
826 + struct jz4740_runtime_data *prtd = substream->runtime->private_data;
828 + snd_pcm_set_runtime_buffer(substream, NULL);
830 + jz4740_dma_free(prtd->dma);
837 +static int jz4740_pcm_prepare(struct snd_pcm_substream *substream)
839 + struct jz4740_runtime_data *prtd = substream->runtime->private_data;
845 + prtd->dma_pos = prtd->dma_start;
850 +static int jz4740_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
852 + struct snd_pcm_runtime *runtime = substream->runtime;
853 + struct jz4740_runtime_data *prtd = runtime->private_data;
858 + case SNDRV_PCM_TRIGGER_START:
859 + case SNDRV_PCM_TRIGGER_RESUME:
860 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
861 + jz4740_pcm_start_transfer(prtd, substream->stream);
863 + case SNDRV_PCM_TRIGGER_STOP:
864 + case SNDRV_PCM_TRIGGER_SUSPEND:
865 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
866 + jz4740_dma_disable(prtd->dma);
875 +static snd_pcm_uframes_t jz4740_pcm_pointer(struct snd_pcm_substream *substream)
877 + struct snd_pcm_runtime *runtime = substream->runtime;
878 + struct jz4740_runtime_data *prtd = runtime->private_data;
879 + unsigned long count, pos;
880 + snd_pcm_uframes_t offset;
881 + struct jz4740_dma_chan *dma = prtd->dma;
883 + count = jz4740_dma_get_residue(dma);
884 + if (prtd->dma_pos == prtd->dma_start)
885 + pos = prtd->dma_end - prtd->dma_start - count;
887 + pos = prtd->dma_pos - prtd->dma_start - count;
889 + offset = bytes_to_frames(runtime, pos);
890 + if (offset >= runtime->buffer_size)
896 +static int jz4740_pcm_open(struct snd_pcm_substream *substream)
898 + struct snd_pcm_runtime *runtime = substream->runtime;
899 + struct jz4740_runtime_data *prtd;
901 + snd_soc_set_runtime_hwparams(substream, &jz4740_pcm_hardware);
902 + prtd = kzalloc(sizeof(struct jz4740_runtime_data), GFP_KERNEL);
907 + runtime->private_data = prtd;
911 +static int jz4740_pcm_close(struct snd_pcm_substream *substream)
913 + struct snd_pcm_runtime *runtime = substream->runtime;
914 + struct jz4740_runtime_data *prtd = runtime->private_data;
921 +static int jz4740_pcm_mmap(struct snd_pcm_substream *substream,
922 + struct vm_area_struct *vma)
924 + return remap_pfn_range(vma, vma->vm_start,
925 + substream->dma_buffer.addr >> PAGE_SHIFT,
926 + vma->vm_end - vma->vm_start, vma->vm_page_prot);
929 +static struct snd_pcm_ops jz4740_pcm_ops = {
930 + .open = jz4740_pcm_open,
931 + .close = jz4740_pcm_close,
932 + .ioctl = snd_pcm_lib_ioctl,
933 + .hw_params = jz4740_pcm_hw_params,
934 + .hw_free = jz4740_pcm_hw_free,
935 + .prepare = jz4740_pcm_prepare,
936 + .trigger = jz4740_pcm_trigger,
937 + .pointer = jz4740_pcm_pointer,
938 + .mmap = jz4740_pcm_mmap,
941 +static int jz4740_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
943 + struct snd_pcm_substream *substream = pcm->streams[stream].substream;
944 + struct snd_dma_buffer *buf = &substream->dma_buffer;
945 + size_t size = jz4740_pcm_hardware.buffer_bytes_max;
947 + buf->dev.type = SNDRV_DMA_TYPE_DEV;
948 + buf->dev.dev = pcm->card->dev;
949 + buf->private_data = NULL;
951 + buf->area = dma_alloc_noncoherent(pcm->card->dev, size,
952 + &buf->addr, GFP_KERNEL);
961 +static void jz4740_pcm_free(struct snd_pcm *pcm)
963 + struct snd_pcm_substream *substream;
964 + struct snd_dma_buffer *buf;
967 + for (stream = 0; stream < 2; stream++) {
968 + substream = pcm->streams[stream].substream;
972 + buf = &substream->dma_buffer;
976 + dma_free_noncoherent(pcm->card->dev, buf->bytes,
977 + buf->area, buf->addr);
982 +static u64 jz4740_pcm_dmamask = DMA_BIT_MASK(32);
984 +int jz4740_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
985 + struct snd_pcm *pcm)
989 + if (!card->dev->dma_mask)
990 + card->dev->dma_mask = &jz4740_pcm_dmamask;
992 + if (!card->dev->coherent_dma_mask)
993 + card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
995 + if (dai->playback.channels_min) {
996 + ret = jz4740_pcm_preallocate_dma_buffer(pcm,
997 + SNDRV_PCM_STREAM_PLAYBACK);
1002 + if (dai->capture.channels_min) {
1003 + ret = jz4740_pcm_preallocate_dma_buffer(pcm,
1004 + SNDRV_PCM_STREAM_CAPTURE);
1013 +struct snd_soc_platform jz4740_soc_platform = {
1014 + .name = "jz4740-pcm",
1015 + .pcm_ops = &jz4740_pcm_ops,
1016 + .pcm_new = jz4740_pcm_new,
1017 + .pcm_free = jz4740_pcm_free,
1019 +EXPORT_SYMBOL_GPL(jz4740_soc_platform);
1021 +static int __init jz4740_soc_platform_init(void)
1023 + return snd_soc_register_platform(&jz4740_soc_platform);
1025 +module_init(jz4740_soc_platform_init);
1027 +static void __exit jz4740_soc_platform_exit(void)
1029 + snd_soc_unregister_platform(&jz4740_soc_platform);
1031 +module_exit(jz4740_soc_platform_exit);
1033 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1034 +MODULE_DESCRIPTION("Ingenic SoC JZ4740 PCM driver");
1035 +MODULE_LICENSE("GPL");
1036 diff --git a/sound/soc/jz4740/jz4740-pcm.h b/sound/soc/jz4740/jz4740-pcm.h
1037 new file mode 100644
1038 index 0000000..2a11800
1040 +++ b/sound/soc/jz4740/jz4740-pcm.h
1044 + * This program is free software; you can redistribute it and/or modify
1045 + * it under the terms of the GNU General Public License version 2 as
1046 + * published by the Free Software Foundation.
1049 +#ifndef _JZ4740_PCM_H
1050 +#define _JZ4740_PCM_H
1052 +#include <linux/dma-mapping.h>
1053 +#include <asm/mach-jz4740/dma.h>
1055 +/* platform data */
1056 +extern struct snd_soc_platform jz4740_soc_platform;
1058 +struct jz4740_pcm_config {
1059 + struct jz4740_dma_config *dma_config;
1060 + phys_addr_t fifo_addr;