1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra30_i2s.c - Tegra30 I2S driver
5 * Author: Stephen Warren <swarren@nvidia.com>
6 * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.
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/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regmap.h>
26 #include <linux/reset.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/dmaengine_pcm.h>
34 #include "tegra30_ahub.h"
35 #include "tegra30_i2s.h"
37 #define DRV_NAME "tegra30-i2s"
39 static __maybe_unused int tegra30_i2s_runtime_suspend(struct device *dev)
41 struct tegra30_i2s *i2s = dev_get_drvdata(dev);
43 regcache_cache_only(i2s->regmap, true);
45 clk_disable_unprepare(i2s->clk_i2s);
50 static __maybe_unused int tegra30_i2s_runtime_resume(struct device *dev)
52 struct tegra30_i2s *i2s = dev_get_drvdata(dev);
55 ret = clk_prepare_enable(i2s->clk_i2s);
57 dev_err(dev, "clk_enable failed: %d\n", ret);
61 regcache_cache_only(i2s->regmap, false);
62 regcache_mark_dirty(i2s->regmap);
64 ret = regcache_sync(i2s->regmap);
71 clk_disable_unprepare(i2s->clk_i2s);
76 static int tegra30_i2s_set_fmt(struct snd_soc_dai *dai,
79 struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
80 unsigned int mask = 0, val = 0;
82 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
83 case SND_SOC_DAIFMT_NB_NF:
89 mask |= TEGRA30_I2S_CTRL_MASTER_ENABLE;
90 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
91 case SND_SOC_DAIFMT_BP_FP:
92 val |= TEGRA30_I2S_CTRL_MASTER_ENABLE;
94 case SND_SOC_DAIFMT_BC_FC:
100 mask |= TEGRA30_I2S_CTRL_FRAME_FORMAT_MASK |
101 TEGRA30_I2S_CTRL_LRCK_MASK;
102 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
103 case SND_SOC_DAIFMT_DSP_A:
104 val |= TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC;
105 val |= TEGRA30_I2S_CTRL_LRCK_L_LOW;
107 case SND_SOC_DAIFMT_DSP_B:
108 val |= TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC;
109 val |= TEGRA30_I2S_CTRL_LRCK_R_LOW;
111 case SND_SOC_DAIFMT_I2S:
112 val |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
113 val |= TEGRA30_I2S_CTRL_LRCK_L_LOW;
115 case SND_SOC_DAIFMT_RIGHT_J:
116 val |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
117 val |= TEGRA30_I2S_CTRL_LRCK_L_LOW;
119 case SND_SOC_DAIFMT_LEFT_J:
120 val |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
121 val |= TEGRA30_I2S_CTRL_LRCK_L_LOW;
127 pm_runtime_get_sync(dai->dev);
128 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CTRL, mask, val);
129 pm_runtime_put(dai->dev);
134 static int tegra30_i2s_hw_params(struct snd_pcm_substream *substream,
135 struct snd_pcm_hw_params *params,
136 struct snd_soc_dai *dai)
138 struct device *dev = dai->dev;
139 struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
140 unsigned int mask, val, reg;
141 int ret, sample_size, srate, i2sclock, bitcnt;
142 struct tegra30_ahub_cif_conf cif_conf;
144 if (params_channels(params) != 2)
147 mask = TEGRA30_I2S_CTRL_BIT_SIZE_MASK;
148 switch (params_format(params)) {
149 case SNDRV_PCM_FORMAT_S16_LE:
150 val = TEGRA30_I2S_CTRL_BIT_SIZE_16;
157 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CTRL, mask, val);
159 srate = params_rate(params);
161 /* Final "* 2" required by Tegra hardware */
162 i2sclock = srate * params_channels(params) * sample_size * 2;
164 bitcnt = (i2sclock / (2 * srate)) - 1;
165 if (bitcnt < 0 || bitcnt > TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
168 ret = clk_set_rate(i2s->clk_i2s, i2sclock);
170 dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
174 val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
176 if (i2sclock % (2 * srate))
177 val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
179 regmap_write(i2s->regmap, TEGRA30_I2S_TIMING, val);
181 cif_conf.threshold = 0;
182 cif_conf.audio_channels = 2;
183 cif_conf.client_channels = 2;
184 cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
185 cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
187 cif_conf.stereo_conv = 0;
188 cif_conf.replicate = 0;
189 cif_conf.truncate = 0;
190 cif_conf.mono_conv = 0;
192 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
193 cif_conf.direction = TEGRA30_AUDIOCIF_DIRECTION_RX;
194 reg = TEGRA30_I2S_CIF_RX_CTRL;
196 cif_conf.direction = TEGRA30_AUDIOCIF_DIRECTION_TX;
197 reg = TEGRA30_I2S_CIF_TX_CTRL;
200 i2s->soc_data->set_audio_cif(i2s->regmap, reg, &cif_conf);
202 val = (1 << TEGRA30_I2S_OFFSET_RX_DATA_OFFSET_SHIFT) |
203 (1 << TEGRA30_I2S_OFFSET_TX_DATA_OFFSET_SHIFT);
204 regmap_write(i2s->regmap, TEGRA30_I2S_OFFSET, val);
209 static void tegra30_i2s_start_playback(struct tegra30_i2s *i2s)
211 tegra30_ahub_enable_tx_fifo(i2s->playback_fifo_cif);
212 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CTRL,
213 TEGRA30_I2S_CTRL_XFER_EN_TX,
214 TEGRA30_I2S_CTRL_XFER_EN_TX);
217 static void tegra30_i2s_stop_playback(struct tegra30_i2s *i2s)
219 tegra30_ahub_disable_tx_fifo(i2s->playback_fifo_cif);
220 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CTRL,
221 TEGRA30_I2S_CTRL_XFER_EN_TX, 0);
224 static void tegra30_i2s_start_capture(struct tegra30_i2s *i2s)
226 tegra30_ahub_enable_rx_fifo(i2s->capture_fifo_cif);
227 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CTRL,
228 TEGRA30_I2S_CTRL_XFER_EN_RX,
229 TEGRA30_I2S_CTRL_XFER_EN_RX);
232 static void tegra30_i2s_stop_capture(struct tegra30_i2s *i2s)
234 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CTRL,
235 TEGRA30_I2S_CTRL_XFER_EN_RX, 0);
236 tegra30_ahub_disable_rx_fifo(i2s->capture_fifo_cif);
239 static int tegra30_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
240 struct snd_soc_dai *dai)
242 struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
245 case SNDRV_PCM_TRIGGER_START:
246 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
247 case SNDRV_PCM_TRIGGER_RESUME:
248 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
249 tegra30_i2s_start_playback(i2s);
251 tegra30_i2s_start_capture(i2s);
253 case SNDRV_PCM_TRIGGER_STOP:
254 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
255 case SNDRV_PCM_TRIGGER_SUSPEND:
256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
257 tegra30_i2s_stop_playback(i2s);
259 tegra30_i2s_stop_capture(i2s);
268 static int tegra30_i2s_set_tdm(struct snd_soc_dai *dai,
269 unsigned int tx_mask, unsigned int rx_mask,
270 int slots, int slot_width)
272 struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
273 unsigned int mask, val;
275 dev_dbg(dai->dev, "%s: txmask=0x%08x rxmask=0x%08x slots=%d width=%d\n",
276 __func__, tx_mask, rx_mask, slots, slot_width);
278 mask = TEGRA30_I2S_SLOT_CTRL_TOTAL_SLOTS_MASK |
279 TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_MASK |
280 TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_MASK;
282 val = (tx_mask << TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_SHIFT) |
283 (rx_mask << TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_SHIFT) |
284 ((slots - 1) << TEGRA30_I2S_SLOT_CTRL_TOTAL_SLOTS_SHIFT);
286 pm_runtime_get_sync(dai->dev);
287 regmap_update_bits(i2s->regmap, TEGRA30_I2S_SLOT_CTRL, mask, val);
288 /* set the fsync width to minimum of 1 clock width */
289 regmap_update_bits(i2s->regmap, TEGRA30_I2S_CH_CTRL,
290 TEGRA30_I2S_CH_CTRL_FSYNC_WIDTH_MASK, 0x0);
291 pm_runtime_put(dai->dev);
296 static int tegra30_i2s_probe(struct snd_soc_dai *dai)
298 struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
300 dai->capture_dma_data = &i2s->capture_dma_data;
301 dai->playback_dma_data = &i2s->playback_dma_data;
306 static const struct snd_soc_dai_ops tegra30_i2s_dai_ops = {
307 .set_fmt = tegra30_i2s_set_fmt,
308 .hw_params = tegra30_i2s_hw_params,
309 .trigger = tegra30_i2s_trigger,
310 .set_tdm_slot = tegra30_i2s_set_tdm,
313 static const struct snd_soc_dai_driver tegra30_i2s_dai_template = {
314 .probe = tegra30_i2s_probe,
316 .stream_name = "Playback",
319 .rates = SNDRV_PCM_RATE_8000_96000,
320 .formats = SNDRV_PCM_FMTBIT_S16_LE,
323 .stream_name = "Capture",
326 .rates = SNDRV_PCM_RATE_8000_96000,
327 .formats = SNDRV_PCM_FMTBIT_S16_LE,
329 .ops = &tegra30_i2s_dai_ops,
333 static const struct snd_soc_component_driver tegra30_i2s_component = {
335 .legacy_dai_naming = 1,
338 static bool tegra30_i2s_wr_rd_reg(struct device *dev, unsigned int reg)
341 case TEGRA30_I2S_CTRL:
342 case TEGRA30_I2S_TIMING:
343 case TEGRA30_I2S_OFFSET:
344 case TEGRA30_I2S_CH_CTRL:
345 case TEGRA30_I2S_SLOT_CTRL:
346 case TEGRA30_I2S_CIF_RX_CTRL:
347 case TEGRA30_I2S_CIF_TX_CTRL:
348 case TEGRA30_I2S_FLOWCTL:
349 case TEGRA30_I2S_TX_STEP:
350 case TEGRA30_I2S_FLOW_STATUS:
351 case TEGRA30_I2S_FLOW_TOTAL:
352 case TEGRA30_I2S_FLOW_OVER:
353 case TEGRA30_I2S_FLOW_UNDER:
354 case TEGRA30_I2S_LCOEF_1_4_0:
355 case TEGRA30_I2S_LCOEF_1_4_1:
356 case TEGRA30_I2S_LCOEF_1_4_2:
357 case TEGRA30_I2S_LCOEF_1_4_3:
358 case TEGRA30_I2S_LCOEF_1_4_4:
359 case TEGRA30_I2S_LCOEF_1_4_5:
360 case TEGRA30_I2S_LCOEF_2_4_0:
361 case TEGRA30_I2S_LCOEF_2_4_1:
362 case TEGRA30_I2S_LCOEF_2_4_2:
369 static bool tegra30_i2s_volatile_reg(struct device *dev, unsigned int reg)
372 case TEGRA30_I2S_FLOW_STATUS:
373 case TEGRA30_I2S_FLOW_TOTAL:
374 case TEGRA30_I2S_FLOW_OVER:
375 case TEGRA30_I2S_FLOW_UNDER:
382 static const struct regmap_config tegra30_i2s_regmap_config = {
386 .max_register = TEGRA30_I2S_LCOEF_2_4_2,
387 .writeable_reg = tegra30_i2s_wr_rd_reg,
388 .readable_reg = tegra30_i2s_wr_rd_reg,
389 .volatile_reg = tegra30_i2s_volatile_reg,
390 .cache_type = REGCACHE_FLAT,
393 static const struct tegra30_i2s_soc_data tegra30_i2s_config = {
394 .set_audio_cif = tegra30_ahub_set_cif,
397 static const struct tegra30_i2s_soc_data tegra124_i2s_config = {
398 .set_audio_cif = tegra124_ahub_set_cif,
401 static const struct of_device_id tegra30_i2s_of_match[] = {
402 { .compatible = "nvidia,tegra124-i2s", .data = &tegra124_i2s_config },
403 { .compatible = "nvidia,tegra30-i2s", .data = &tegra30_i2s_config },
407 static int tegra30_i2s_platform_probe(struct platform_device *pdev)
409 struct tegra30_i2s *i2s;
410 const struct tegra30_i2s_soc_data *soc_data;
415 i2s = devm_kzalloc(&pdev->dev, sizeof(struct tegra30_i2s), GFP_KERNEL);
420 dev_set_drvdata(&pdev->dev, i2s);
422 soc_data = of_device_get_match_data(&pdev->dev);
424 dev_err(&pdev->dev, "Error: No device match found\n");
428 i2s->soc_data = soc_data;
430 i2s->dai = tegra30_i2s_dai_template;
431 i2s->dai.name = dev_name(&pdev->dev);
433 ret = of_property_read_u32_array(pdev->dev.of_node,
434 "nvidia,ahub-cif-ids", cif_ids,
435 ARRAY_SIZE(cif_ids));
439 i2s->playback_i2s_cif = cif_ids[0];
440 i2s->capture_i2s_cif = cif_ids[1];
442 i2s->clk_i2s = devm_clk_get(&pdev->dev, NULL);
443 if (IS_ERR(i2s->clk_i2s)) {
444 dev_err(&pdev->dev, "Can't retrieve i2s clock\n");
445 ret = PTR_ERR(i2s->clk_i2s);
449 regs = devm_platform_ioremap_resource(pdev, 0);
455 i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
456 &tegra30_i2s_regmap_config);
457 if (IS_ERR(i2s->regmap)) {
458 dev_err(&pdev->dev, "regmap init failed\n");
459 ret = PTR_ERR(i2s->regmap);
462 regcache_cache_only(i2s->regmap, true);
464 pm_runtime_enable(&pdev->dev);
466 i2s->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
467 i2s->playback_dma_data.maxburst = 4;
468 ret = tegra30_ahub_allocate_tx_fifo(&i2s->playback_fifo_cif,
469 i2s->playback_dma_chan,
470 sizeof(i2s->playback_dma_chan),
471 &i2s->playback_dma_data.addr);
473 dev_err(&pdev->dev, "Could not alloc TX FIFO: %d\n", ret);
476 ret = tegra30_ahub_set_rx_cif_source(i2s->playback_i2s_cif,
477 i2s->playback_fifo_cif);
479 dev_err(&pdev->dev, "Could not route TX FIFO: %d\n", ret);
480 goto err_free_tx_fifo;
483 i2s->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
484 i2s->capture_dma_data.maxburst = 4;
485 ret = tegra30_ahub_allocate_rx_fifo(&i2s->capture_fifo_cif,
486 i2s->capture_dma_chan,
487 sizeof(i2s->capture_dma_chan),
488 &i2s->capture_dma_data.addr);
490 dev_err(&pdev->dev, "Could not alloc RX FIFO: %d\n", ret);
491 goto err_unroute_tx_fifo;
493 ret = tegra30_ahub_set_rx_cif_source(i2s->capture_fifo_cif,
494 i2s->capture_i2s_cif);
496 dev_err(&pdev->dev, "Could not route TX FIFO: %d\n", ret);
497 goto err_free_rx_fifo;
500 ret = snd_soc_register_component(&pdev->dev, &tegra30_i2s_component,
503 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
505 goto err_unroute_rx_fifo;
508 ret = tegra_pcm_platform_register_with_chan_names(&pdev->dev,
509 &i2s->dma_config, i2s->playback_dma_chan,
510 i2s->capture_dma_chan);
512 dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
513 goto err_unregister_component;
518 err_unregister_component:
519 snd_soc_unregister_component(&pdev->dev);
521 tegra30_ahub_unset_rx_cif_source(i2s->capture_fifo_cif);
523 tegra30_ahub_free_rx_fifo(i2s->capture_fifo_cif);
525 tegra30_ahub_unset_rx_cif_source(i2s->playback_i2s_cif);
527 tegra30_ahub_free_tx_fifo(i2s->playback_fifo_cif);
529 pm_runtime_disable(&pdev->dev);
534 static int tegra30_i2s_platform_remove(struct platform_device *pdev)
536 struct tegra30_i2s *i2s = dev_get_drvdata(&pdev->dev);
538 tegra_pcm_platform_unregister(&pdev->dev);
539 snd_soc_unregister_component(&pdev->dev);
541 tegra30_ahub_unset_rx_cif_source(i2s->capture_fifo_cif);
542 tegra30_ahub_free_rx_fifo(i2s->capture_fifo_cif);
544 tegra30_ahub_unset_rx_cif_source(i2s->playback_i2s_cif);
545 tegra30_ahub_free_tx_fifo(i2s->playback_fifo_cif);
547 pm_runtime_disable(&pdev->dev);
552 static const struct dev_pm_ops tegra30_i2s_pm_ops = {
553 SET_RUNTIME_PM_OPS(tegra30_i2s_runtime_suspend,
554 tegra30_i2s_runtime_resume, NULL)
555 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
556 pm_runtime_force_resume)
559 static struct platform_driver tegra30_i2s_driver = {
562 .of_match_table = tegra30_i2s_of_match,
563 .pm = &tegra30_i2s_pm_ops,
565 .probe = tegra30_i2s_platform_probe,
566 .remove = tegra30_i2s_platform_remove,
568 module_platform_driver(tegra30_i2s_driver);
570 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
571 MODULE_DESCRIPTION("Tegra30 I2S ASoC driver");
572 MODULE_LICENSE("GPL");
573 MODULE_ALIAS("platform:" DRV_NAME);
574 MODULE_DEVICE_TABLE(of, tegra30_i2s_of_match);