1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra20_i2s.c - Tegra20 I2S driver
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (C) 2010,2012 - NVIDIA, Inc.
8 * Based on code copyright/by:
10 * Copyright (c) 2009-2010, NVIDIA Corporation.
11 * Scott Peterson <speterson@nvidia.com>
13 * Copyright (C) 2010 Google, Inc.
14 * Iliyan Malchev <malchev@google.com>
17 #include <linux/clk.h>
18 #include <linux/device.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/dmaengine_pcm.h>
32 #include "tegra20_i2s.h"
34 #define DRV_NAME "tegra20-i2s"
36 static int tegra20_i2s_runtime_suspend(struct device
*dev
)
38 struct tegra20_i2s
*i2s
= dev_get_drvdata(dev
);
40 clk_disable_unprepare(i2s
->clk_i2s
);
45 static int tegra20_i2s_runtime_resume(struct device
*dev
)
47 struct tegra20_i2s
*i2s
= dev_get_drvdata(dev
);
50 ret
= clk_prepare_enable(i2s
->clk_i2s
);
52 dev_err(dev
, "clk_enable failed: %d\n", ret
);
59 static int tegra20_i2s_set_fmt(struct snd_soc_dai
*dai
,
62 struct tegra20_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
63 unsigned int mask
= 0, val
= 0;
65 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
66 case SND_SOC_DAIFMT_NB_NF
:
72 mask
|= TEGRA20_I2S_CTRL_MASTER_ENABLE
;
73 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
74 case SND_SOC_DAIFMT_CBS_CFS
:
75 val
|= TEGRA20_I2S_CTRL_MASTER_ENABLE
;
77 case SND_SOC_DAIFMT_CBM_CFM
:
83 mask
|= TEGRA20_I2S_CTRL_BIT_FORMAT_MASK
|
84 TEGRA20_I2S_CTRL_LRCK_MASK
;
85 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
86 case SND_SOC_DAIFMT_DSP_A
:
87 val
|= TEGRA20_I2S_CTRL_BIT_FORMAT_DSP
;
88 val
|= TEGRA20_I2S_CTRL_LRCK_L_LOW
;
90 case SND_SOC_DAIFMT_DSP_B
:
91 val
|= TEGRA20_I2S_CTRL_BIT_FORMAT_DSP
;
92 val
|= TEGRA20_I2S_CTRL_LRCK_R_LOW
;
94 case SND_SOC_DAIFMT_I2S
:
95 val
|= TEGRA20_I2S_CTRL_BIT_FORMAT_I2S
;
96 val
|= TEGRA20_I2S_CTRL_LRCK_L_LOW
;
98 case SND_SOC_DAIFMT_RIGHT_J
:
99 val
|= TEGRA20_I2S_CTRL_BIT_FORMAT_RJM
;
100 val
|= TEGRA20_I2S_CTRL_LRCK_L_LOW
;
102 case SND_SOC_DAIFMT_LEFT_J
:
103 val
|= TEGRA20_I2S_CTRL_BIT_FORMAT_LJM
;
104 val
|= TEGRA20_I2S_CTRL_LRCK_L_LOW
;
110 regmap_update_bits(i2s
->regmap
, TEGRA20_I2S_CTRL
, mask
, val
);
115 static int tegra20_i2s_hw_params(struct snd_pcm_substream
*substream
,
116 struct snd_pcm_hw_params
*params
,
117 struct snd_soc_dai
*dai
)
119 struct device
*dev
= dai
->dev
;
120 struct tegra20_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
121 unsigned int mask
, val
;
122 int ret
, sample_size
, srate
, i2sclock
, bitcnt
;
124 mask
= TEGRA20_I2S_CTRL_BIT_SIZE_MASK
;
125 switch (params_format(params
)) {
126 case SNDRV_PCM_FORMAT_S16_LE
:
127 val
= TEGRA20_I2S_CTRL_BIT_SIZE_16
;
130 case SNDRV_PCM_FORMAT_S24_LE
:
131 val
= TEGRA20_I2S_CTRL_BIT_SIZE_24
;
134 case SNDRV_PCM_FORMAT_S32_LE
:
135 val
= TEGRA20_I2S_CTRL_BIT_SIZE_32
;
142 mask
|= TEGRA20_I2S_CTRL_FIFO_FORMAT_MASK
;
143 val
|= TEGRA20_I2S_CTRL_FIFO_FORMAT_PACKED
;
145 regmap_update_bits(i2s
->regmap
, TEGRA20_I2S_CTRL
, mask
, val
);
147 srate
= params_rate(params
);
149 /* Final "* 2" required by Tegra hardware */
150 i2sclock
= srate
* params_channels(params
) * sample_size
* 2;
152 ret
= clk_set_rate(i2s
->clk_i2s
, i2sclock
);
154 dev_err(dev
, "Can't set I2S clock rate: %d\n", ret
);
158 bitcnt
= (i2sclock
/ (2 * srate
)) - 1;
159 if (bitcnt
< 0 || bitcnt
> TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US
)
161 val
= bitcnt
<< TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT
;
163 if (i2sclock
% (2 * srate
))
164 val
|= TEGRA20_I2S_TIMING_NON_SYM_ENABLE
;
166 regmap_write(i2s
->regmap
, TEGRA20_I2S_TIMING
, val
);
168 regmap_write(i2s
->regmap
, TEGRA20_I2S_FIFO_SCR
,
169 TEGRA20_I2S_FIFO_SCR_FIFO2_ATN_LVL_FOUR_SLOTS
|
170 TEGRA20_I2S_FIFO_SCR_FIFO1_ATN_LVL_FOUR_SLOTS
);
175 static void tegra20_i2s_start_playback(struct tegra20_i2s
*i2s
)
177 regmap_update_bits(i2s
->regmap
, TEGRA20_I2S_CTRL
,
178 TEGRA20_I2S_CTRL_FIFO1_ENABLE
,
179 TEGRA20_I2S_CTRL_FIFO1_ENABLE
);
182 static void tegra20_i2s_stop_playback(struct tegra20_i2s
*i2s
)
184 regmap_update_bits(i2s
->regmap
, TEGRA20_I2S_CTRL
,
185 TEGRA20_I2S_CTRL_FIFO1_ENABLE
, 0);
188 static void tegra20_i2s_start_capture(struct tegra20_i2s
*i2s
)
190 regmap_update_bits(i2s
->regmap
, TEGRA20_I2S_CTRL
,
191 TEGRA20_I2S_CTRL_FIFO2_ENABLE
,
192 TEGRA20_I2S_CTRL_FIFO2_ENABLE
);
195 static void tegra20_i2s_stop_capture(struct tegra20_i2s
*i2s
)
197 regmap_update_bits(i2s
->regmap
, TEGRA20_I2S_CTRL
,
198 TEGRA20_I2S_CTRL_FIFO2_ENABLE
, 0);
201 static int tegra20_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
,
202 struct snd_soc_dai
*dai
)
204 struct tegra20_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
207 case SNDRV_PCM_TRIGGER_START
:
208 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
209 case SNDRV_PCM_TRIGGER_RESUME
:
210 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
211 tegra20_i2s_start_playback(i2s
);
213 tegra20_i2s_start_capture(i2s
);
215 case SNDRV_PCM_TRIGGER_STOP
:
216 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
217 case SNDRV_PCM_TRIGGER_SUSPEND
:
218 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
219 tegra20_i2s_stop_playback(i2s
);
221 tegra20_i2s_stop_capture(i2s
);
230 static int tegra20_i2s_probe(struct snd_soc_dai
*dai
)
232 struct tegra20_i2s
*i2s
= snd_soc_dai_get_drvdata(dai
);
234 dai
->capture_dma_data
= &i2s
->capture_dma_data
;
235 dai
->playback_dma_data
= &i2s
->playback_dma_data
;
240 static const struct snd_soc_dai_ops tegra20_i2s_dai_ops
= {
241 .set_fmt
= tegra20_i2s_set_fmt
,
242 .hw_params
= tegra20_i2s_hw_params
,
243 .trigger
= tegra20_i2s_trigger
,
246 static const struct snd_soc_dai_driver tegra20_i2s_dai_template
= {
247 .probe
= tegra20_i2s_probe
,
249 .stream_name
= "Playback",
252 .rates
= SNDRV_PCM_RATE_8000_96000
,
253 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
256 .stream_name
= "Capture",
259 .rates
= SNDRV_PCM_RATE_8000_96000
,
260 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
262 .ops
= &tegra20_i2s_dai_ops
,
263 .symmetric_rates
= 1,
266 static const struct snd_soc_component_driver tegra20_i2s_component
= {
270 static bool tegra20_i2s_wr_rd_reg(struct device
*dev
, unsigned int reg
)
273 case TEGRA20_I2S_CTRL
:
274 case TEGRA20_I2S_STATUS
:
275 case TEGRA20_I2S_TIMING
:
276 case TEGRA20_I2S_FIFO_SCR
:
277 case TEGRA20_I2S_PCM_CTRL
:
278 case TEGRA20_I2S_NW_CTRL
:
279 case TEGRA20_I2S_TDM_CTRL
:
280 case TEGRA20_I2S_TDM_TX_RX_CTRL
:
281 case TEGRA20_I2S_FIFO1
:
282 case TEGRA20_I2S_FIFO2
:
289 static bool tegra20_i2s_volatile_reg(struct device
*dev
, unsigned int reg
)
292 case TEGRA20_I2S_STATUS
:
293 case TEGRA20_I2S_FIFO_SCR
:
294 case TEGRA20_I2S_FIFO1
:
295 case TEGRA20_I2S_FIFO2
:
302 static bool tegra20_i2s_precious_reg(struct device
*dev
, unsigned int reg
)
305 case TEGRA20_I2S_FIFO1
:
306 case TEGRA20_I2S_FIFO2
:
313 static const struct regmap_config tegra20_i2s_regmap_config
= {
317 .max_register
= TEGRA20_I2S_FIFO2
,
318 .writeable_reg
= tegra20_i2s_wr_rd_reg
,
319 .readable_reg
= tegra20_i2s_wr_rd_reg
,
320 .volatile_reg
= tegra20_i2s_volatile_reg
,
321 .precious_reg
= tegra20_i2s_precious_reg
,
322 .cache_type
= REGCACHE_FLAT
,
325 static int tegra20_i2s_platform_probe(struct platform_device
*pdev
)
327 struct tegra20_i2s
*i2s
;
328 struct resource
*mem
;
332 i2s
= devm_kzalloc(&pdev
->dev
, sizeof(struct tegra20_i2s
), GFP_KERNEL
);
337 dev_set_drvdata(&pdev
->dev
, i2s
);
339 i2s
->dai
= tegra20_i2s_dai_template
;
340 i2s
->dai
.name
= dev_name(&pdev
->dev
);
342 i2s
->clk_i2s
= clk_get(&pdev
->dev
, NULL
);
343 if (IS_ERR(i2s
->clk_i2s
)) {
344 dev_err(&pdev
->dev
, "Can't retrieve i2s clock\n");
345 ret
= PTR_ERR(i2s
->clk_i2s
);
349 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
350 regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
356 i2s
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, regs
,
357 &tegra20_i2s_regmap_config
);
358 if (IS_ERR(i2s
->regmap
)) {
359 dev_err(&pdev
->dev
, "regmap init failed\n");
360 ret
= PTR_ERR(i2s
->regmap
);
364 i2s
->capture_dma_data
.addr
= mem
->start
+ TEGRA20_I2S_FIFO2
;
365 i2s
->capture_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
366 i2s
->capture_dma_data
.maxburst
= 4;
368 i2s
->playback_dma_data
.addr
= mem
->start
+ TEGRA20_I2S_FIFO1
;
369 i2s
->playback_dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
370 i2s
->playback_dma_data
.maxburst
= 4;
372 pm_runtime_enable(&pdev
->dev
);
373 if (!pm_runtime_enabled(&pdev
->dev
)) {
374 ret
= tegra20_i2s_runtime_resume(&pdev
->dev
);
379 ret
= snd_soc_register_component(&pdev
->dev
, &tegra20_i2s_component
,
382 dev_err(&pdev
->dev
, "Could not register DAI: %d\n", ret
);
387 ret
= tegra_pcm_platform_register(&pdev
->dev
);
389 dev_err(&pdev
->dev
, "Could not register PCM: %d\n", ret
);
390 goto err_unregister_component
;
395 err_unregister_component
:
396 snd_soc_unregister_component(&pdev
->dev
);
398 if (!pm_runtime_status_suspended(&pdev
->dev
))
399 tegra20_i2s_runtime_suspend(&pdev
->dev
);
401 pm_runtime_disable(&pdev
->dev
);
403 clk_put(i2s
->clk_i2s
);
408 static int tegra20_i2s_platform_remove(struct platform_device
*pdev
)
410 struct tegra20_i2s
*i2s
= dev_get_drvdata(&pdev
->dev
);
412 pm_runtime_disable(&pdev
->dev
);
413 if (!pm_runtime_status_suspended(&pdev
->dev
))
414 tegra20_i2s_runtime_suspend(&pdev
->dev
);
416 tegra_pcm_platform_unregister(&pdev
->dev
);
417 snd_soc_unregister_component(&pdev
->dev
);
419 clk_put(i2s
->clk_i2s
);
424 static const struct of_device_id tegra20_i2s_of_match
[] = {
425 { .compatible
= "nvidia,tegra20-i2s", },
429 static const struct dev_pm_ops tegra20_i2s_pm_ops
= {
430 SET_RUNTIME_PM_OPS(tegra20_i2s_runtime_suspend
,
431 tegra20_i2s_runtime_resume
, NULL
)
434 static struct platform_driver tegra20_i2s_driver
= {
437 .of_match_table
= tegra20_i2s_of_match
,
438 .pm
= &tegra20_i2s_pm_ops
,
440 .probe
= tegra20_i2s_platform_probe
,
441 .remove
= tegra20_i2s_platform_remove
,
443 module_platform_driver(tegra20_i2s_driver
);
445 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
446 MODULE_DESCRIPTION("Tegra20 I2S ASoC driver");
447 MODULE_LICENSE("GPL");
448 MODULE_ALIAS("platform:" DRV_NAME
);
449 MODULE_DEVICE_TABLE(of
, tegra20_i2s_of_match
);