1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
6 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/pcm_params.h>
22 #include <sound/soc.h>
23 #include <sound/initval.h>
24 #include <sound/dmaengine_pcm.h>
26 #include "jz4740-i2s.h"
28 #define JZ_REG_AIC_CONF 0x00
29 #define JZ_REG_AIC_CTRL 0x04
30 #define JZ_REG_AIC_I2S_FMT 0x10
31 #define JZ_REG_AIC_FIFO_STATUS 0x14
32 #define JZ_REG_AIC_I2S_STATUS 0x1c
33 #define JZ_REG_AIC_CLK_DIV 0x30
34 #define JZ_REG_AIC_FIFO 0x34
36 #define JZ_AIC_CONF_FIFO_RX_THRESHOLD_MASK (0xf << 12)
37 #define JZ_AIC_CONF_FIFO_TX_THRESHOLD_MASK (0xf << 8)
38 #define JZ_AIC_CONF_OVERFLOW_PLAY_LAST BIT(6)
39 #define JZ_AIC_CONF_INTERNAL_CODEC BIT(5)
40 #define JZ_AIC_CONF_I2S BIT(4)
41 #define JZ_AIC_CONF_RESET BIT(3)
42 #define JZ_AIC_CONF_BIT_CLK_MASTER BIT(2)
43 #define JZ_AIC_CONF_SYNC_CLK_MASTER BIT(1)
44 #define JZ_AIC_CONF_ENABLE BIT(0)
46 #define JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET 12
47 #define JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET 8
48 #define JZ4760_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET 24
49 #define JZ4760_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET 16
51 #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK (0x7 << 19)
52 #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK (0x7 << 16)
53 #define JZ_AIC_CTRL_ENABLE_RX_DMA BIT(15)
54 #define JZ_AIC_CTRL_ENABLE_TX_DMA BIT(14)
55 #define JZ_AIC_CTRL_MONO_TO_STEREO BIT(11)
56 #define JZ_AIC_CTRL_SWITCH_ENDIANNESS BIT(10)
57 #define JZ_AIC_CTRL_SIGNED_TO_UNSIGNED BIT(9)
58 #define JZ_AIC_CTRL_FLUSH BIT(8)
59 #define JZ_AIC_CTRL_ENABLE_ROR_INT BIT(6)
60 #define JZ_AIC_CTRL_ENABLE_TUR_INT BIT(5)
61 #define JZ_AIC_CTRL_ENABLE_RFS_INT BIT(4)
62 #define JZ_AIC_CTRL_ENABLE_TFS_INT BIT(3)
63 #define JZ_AIC_CTRL_ENABLE_LOOPBACK BIT(2)
64 #define JZ_AIC_CTRL_ENABLE_PLAYBACK BIT(1)
65 #define JZ_AIC_CTRL_ENABLE_CAPTURE BIT(0)
67 #define JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET 19
68 #define JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET 16
70 #define JZ_AIC_I2S_FMT_DISABLE_BIT_CLK BIT(12)
71 #define JZ_AIC_I2S_FMT_DISABLE_BIT_ICLK BIT(13)
72 #define JZ_AIC_I2S_FMT_ENABLE_SYS_CLK BIT(4)
73 #define JZ_AIC_I2S_FMT_MSB BIT(0)
75 #define JZ_AIC_I2S_STATUS_BUSY BIT(2)
77 #define JZ_AIC_CLK_DIV_MASK 0xf
78 #define I2SDIV_DV_SHIFT 0
79 #define I2SDIV_DV_MASK (0xf << I2SDIV_DV_SHIFT)
80 #define I2SDIV_IDV_SHIFT 8
81 #define I2SDIV_IDV_MASK (0xf << I2SDIV_IDV_SHIFT)
83 enum jz47xx_i2s_version {
91 enum jz47xx_i2s_version version;
92 struct snd_soc_dai_driver *dai;
101 struct snd_dmaengine_dai_dma_data playback_dma_data;
102 struct snd_dmaengine_dai_dma_data capture_dma_data;
104 const struct i2s_soc_info *soc_info;
107 static inline uint32_t jz4740_i2s_read(const struct jz4740_i2s *i2s,
110 return readl(i2s->base + reg);
113 static inline void jz4740_i2s_write(const struct jz4740_i2s *i2s,
114 unsigned int reg, uint32_t value)
116 writel(value, i2s->base + reg);
119 static int jz4740_i2s_startup(struct snd_pcm_substream *substream,
120 struct snd_soc_dai *dai)
122 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
126 if (snd_soc_dai_active(dai))
129 ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
130 ctrl |= JZ_AIC_CTRL_FLUSH;
131 jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
133 ret = clk_prepare_enable(i2s->clk_i2s);
137 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
138 conf |= JZ_AIC_CONF_ENABLE;
139 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
144 static void jz4740_i2s_shutdown(struct snd_pcm_substream *substream,
145 struct snd_soc_dai *dai)
147 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
150 if (snd_soc_dai_active(dai))
153 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
154 conf &= ~JZ_AIC_CONF_ENABLE;
155 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
157 clk_disable_unprepare(i2s->clk_i2s);
160 static int jz4740_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
161 struct snd_soc_dai *dai)
163 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
168 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
169 mask = JZ_AIC_CTRL_ENABLE_PLAYBACK | JZ_AIC_CTRL_ENABLE_TX_DMA;
171 mask = JZ_AIC_CTRL_ENABLE_CAPTURE | JZ_AIC_CTRL_ENABLE_RX_DMA;
173 ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
176 case SNDRV_PCM_TRIGGER_START:
177 case SNDRV_PCM_TRIGGER_RESUME:
178 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
181 case SNDRV_PCM_TRIGGER_STOP:
182 case SNDRV_PCM_TRIGGER_SUSPEND:
183 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
190 jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
195 static int jz4740_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
197 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
202 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
204 conf &= ~(JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER);
206 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
207 case SND_SOC_DAIFMT_BP_FP:
208 conf |= JZ_AIC_CONF_BIT_CLK_MASTER | JZ_AIC_CONF_SYNC_CLK_MASTER;
209 format |= JZ_AIC_I2S_FMT_ENABLE_SYS_CLK;
211 case SND_SOC_DAIFMT_BC_FP:
212 conf |= JZ_AIC_CONF_SYNC_CLK_MASTER;
214 case SND_SOC_DAIFMT_BP_FC:
215 conf |= JZ_AIC_CONF_BIT_CLK_MASTER;
217 case SND_SOC_DAIFMT_BC_FC:
223 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
224 case SND_SOC_DAIFMT_MSB:
225 format |= JZ_AIC_I2S_FMT_MSB;
227 case SND_SOC_DAIFMT_I2S:
233 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
234 case SND_SOC_DAIFMT_NB_NF:
240 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
241 jz4740_i2s_write(i2s, JZ_REG_AIC_I2S_FMT, format);
246 static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
247 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
249 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
250 unsigned int sample_size;
251 uint32_t ctrl, div_reg;
254 ctrl = jz4740_i2s_read(i2s, JZ_REG_AIC_CTRL);
256 div_reg = jz4740_i2s_read(i2s, JZ_REG_AIC_CLK_DIV);
257 div = clk_get_rate(i2s->clk_i2s) / (64 * params_rate(params));
259 switch (params_format(params)) {
260 case SNDRV_PCM_FORMAT_S8:
263 case SNDRV_PCM_FORMAT_S16:
270 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
271 ctrl &= ~JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_MASK;
272 ctrl |= sample_size << JZ_AIC_CTRL_OUTPUT_SAMPLE_SIZE_OFFSET;
273 if (params_channels(params) == 1)
274 ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO;
276 ctrl &= ~JZ_AIC_CTRL_MONO_TO_STEREO;
278 div_reg &= ~I2SDIV_DV_MASK;
279 div_reg |= (div - 1) << I2SDIV_DV_SHIFT;
281 ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK;
282 ctrl |= sample_size << JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET;
284 if (i2s->soc_info->version >= JZ_I2S_JZ4770) {
285 div_reg &= ~I2SDIV_IDV_MASK;
286 div_reg |= (div - 1) << I2SDIV_IDV_SHIFT;
288 div_reg &= ~I2SDIV_DV_MASK;
289 div_reg |= (div - 1) << I2SDIV_DV_SHIFT;
293 jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
294 jz4740_i2s_write(i2s, JZ_REG_AIC_CLK_DIV, div_reg);
299 static int jz4740_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
300 unsigned int freq, int dir)
302 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
307 case JZ4740_I2S_CLKSRC_EXT:
308 parent = clk_get(NULL, "ext");
310 return PTR_ERR(parent);
311 clk_set_parent(i2s->clk_i2s, parent);
313 case JZ4740_I2S_CLKSRC_PLL:
314 parent = clk_get(NULL, "pll half");
316 return PTR_ERR(parent);
317 clk_set_parent(i2s->clk_i2s, parent);
318 ret = clk_set_rate(i2s->clk_i2s, freq);
328 static int jz4740_i2s_suspend(struct snd_soc_component *component)
330 struct jz4740_i2s *i2s = snd_soc_component_get_drvdata(component);
333 if (snd_soc_component_active(component)) {
334 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
335 conf &= ~JZ_AIC_CONF_ENABLE;
336 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
338 clk_disable_unprepare(i2s->clk_i2s);
341 clk_disable_unprepare(i2s->clk_aic);
346 static int jz4740_i2s_resume(struct snd_soc_component *component)
348 struct jz4740_i2s *i2s = snd_soc_component_get_drvdata(component);
352 ret = clk_prepare_enable(i2s->clk_aic);
356 if (snd_soc_component_active(component)) {
357 ret = clk_prepare_enable(i2s->clk_i2s);
359 clk_disable_unprepare(i2s->clk_aic);
363 conf = jz4740_i2s_read(i2s, JZ_REG_AIC_CONF);
364 conf |= JZ_AIC_CONF_ENABLE;
365 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
371 static int jz4740_i2s_dai_probe(struct snd_soc_dai *dai)
373 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
377 ret = clk_prepare_enable(i2s->clk_aic);
381 snd_soc_dai_init_dma_data(dai, &i2s->playback_dma_data,
382 &i2s->capture_dma_data);
384 if (i2s->soc_info->version >= JZ_I2S_JZ4760) {
385 conf = (7 << JZ4760_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
386 (8 << JZ4760_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
387 JZ_AIC_CONF_OVERFLOW_PLAY_LAST |
389 JZ_AIC_CONF_INTERNAL_CODEC;
391 conf = (7 << JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
392 (8 << JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
393 JZ_AIC_CONF_OVERFLOW_PLAY_LAST |
395 JZ_AIC_CONF_INTERNAL_CODEC;
398 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, JZ_AIC_CONF_RESET);
399 jz4740_i2s_write(i2s, JZ_REG_AIC_CONF, conf);
404 static int jz4740_i2s_dai_remove(struct snd_soc_dai *dai)
406 struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
408 clk_disable_unprepare(i2s->clk_aic);
412 static const struct snd_soc_dai_ops jz4740_i2s_dai_ops = {
413 .startup = jz4740_i2s_startup,
414 .shutdown = jz4740_i2s_shutdown,
415 .trigger = jz4740_i2s_trigger,
416 .hw_params = jz4740_i2s_hw_params,
417 .set_fmt = jz4740_i2s_set_fmt,
418 .set_sysclk = jz4740_i2s_set_sysclk,
421 #define JZ4740_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
422 SNDRV_PCM_FMTBIT_S16_LE)
424 static struct snd_soc_dai_driver jz4740_i2s_dai = {
425 .probe = jz4740_i2s_dai_probe,
426 .remove = jz4740_i2s_dai_remove,
430 .rates = SNDRV_PCM_RATE_8000_48000,
431 .formats = JZ4740_I2S_FMTS,
436 .rates = SNDRV_PCM_RATE_8000_48000,
437 .formats = JZ4740_I2S_FMTS,
440 .ops = &jz4740_i2s_dai_ops,
443 static const struct i2s_soc_info jz4740_i2s_soc_info = {
444 .version = JZ_I2S_JZ4740,
445 .dai = &jz4740_i2s_dai,
448 static const struct i2s_soc_info jz4760_i2s_soc_info = {
449 .version = JZ_I2S_JZ4760,
450 .dai = &jz4740_i2s_dai,
453 static struct snd_soc_dai_driver jz4770_i2s_dai = {
454 .probe = jz4740_i2s_dai_probe,
455 .remove = jz4740_i2s_dai_remove,
459 .rates = SNDRV_PCM_RATE_8000_48000,
460 .formats = JZ4740_I2S_FMTS,
465 .rates = SNDRV_PCM_RATE_8000_48000,
466 .formats = JZ4740_I2S_FMTS,
468 .ops = &jz4740_i2s_dai_ops,
471 static const struct i2s_soc_info jz4770_i2s_soc_info = {
472 .version = JZ_I2S_JZ4770,
473 .dai = &jz4770_i2s_dai,
476 static const struct i2s_soc_info jz4780_i2s_soc_info = {
477 .version = JZ_I2S_JZ4780,
478 .dai = &jz4770_i2s_dai,
481 static const struct snd_soc_component_driver jz4740_i2s_component = {
482 .name = "jz4740-i2s",
483 .suspend = jz4740_i2s_suspend,
484 .resume = jz4740_i2s_resume,
485 .legacy_dai_naming = 1,
488 static const struct of_device_id jz4740_of_matches[] = {
489 { .compatible = "ingenic,jz4740-i2s", .data = &jz4740_i2s_soc_info },
490 { .compatible = "ingenic,jz4760-i2s", .data = &jz4760_i2s_soc_info },
491 { .compatible = "ingenic,jz4770-i2s", .data = &jz4770_i2s_soc_info },
492 { .compatible = "ingenic,jz4780-i2s", .data = &jz4780_i2s_soc_info },
495 MODULE_DEVICE_TABLE(of, jz4740_of_matches);
497 static int jz4740_i2s_dev_probe(struct platform_device *pdev)
499 struct device *dev = &pdev->dev;
500 struct jz4740_i2s *i2s;
501 struct resource *mem;
504 i2s = devm_kzalloc(dev, sizeof(*i2s), GFP_KERNEL);
508 i2s->soc_info = device_get_match_data(dev);
510 i2s->base = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
511 if (IS_ERR(i2s->base))
512 return PTR_ERR(i2s->base);
514 i2s->playback_dma_data.maxburst = 16;
515 i2s->playback_dma_data.addr = mem->start + JZ_REG_AIC_FIFO;
517 i2s->capture_dma_data.maxburst = 16;
518 i2s->capture_dma_data.addr = mem->start + JZ_REG_AIC_FIFO;
520 i2s->clk_aic = devm_clk_get(dev, "aic");
521 if (IS_ERR(i2s->clk_aic))
522 return PTR_ERR(i2s->clk_aic);
524 i2s->clk_i2s = devm_clk_get(dev, "i2s");
525 if (IS_ERR(i2s->clk_i2s))
526 return PTR_ERR(i2s->clk_i2s);
528 platform_set_drvdata(pdev, i2s);
530 ret = devm_snd_soc_register_component(dev, &jz4740_i2s_component,
531 i2s->soc_info->dai, 1);
535 return devm_snd_dmaengine_pcm_register(dev, NULL,
536 SND_DMAENGINE_PCM_FLAG_COMPAT);
539 static struct platform_driver jz4740_i2s_driver = {
540 .probe = jz4740_i2s_dev_probe,
542 .name = "jz4740-i2s",
543 .of_match_table = jz4740_of_matches,
547 module_platform_driver(jz4740_i2s_driver);
549 MODULE_AUTHOR("Lars-Peter Clausen, <lars@metafoo.de>");
550 MODULE_DESCRIPTION("Ingenic JZ4740 SoC I2S driver");
551 MODULE_LICENSE("GPL");
552 MODULE_ALIAS("platform:jz4740-i2s");